lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DDXF7IB7MZCV.9J6U7KTR5ZD0@nvidia.com>
Date: Sat, 01 Nov 2025 23:16:33 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Danilo Krummrich" <dakr@...nel.org>, <gregkh@...uxfoundation.org>,
 <rafael@...nel.org>, <ojeda@...nel.org>, <alex.gaynor@...il.com>,
 <boqun.feng@...il.com>, <gary@...yguo.net>, <bjorn3_gh@...tonmail.com>,
 <lossin@...nel.org>, <a.hindborg@...nel.org>, <aliceryhl@...gle.com>,
 <tmgross@...ch.edu>, <mmaurer@...gle.com>
Cc: <rust-for-linux@...r.kernel.org>, <linux-fsdevel@...r.kernel.org>,
 <linux-kernel@...r.kernel.org>, "Alexander Viro" <viro@...iv.linux.org.uk>,
 "Christian Brauner" <brauner@...nel.org>, "Jan Kara" <jack@...e.cz>
Subject: Re: [PATCH v3 01/10] rust: fs: add new type file::Offset

On Wed Oct 22, 2025 at 11:30 PM JST, Danilo Krummrich wrote:
> Add a new type for file offsets, i.e. bindings::loff_t. Trying to avoid
> using raw bindings types, this seems to be the better alternative
> compared to just using i64.
>
> Cc: Alexander Viro <viro@...iv.linux.org.uk>
> Cc: Christian Brauner <brauner@...nel.org>
> Cc: Jan Kara <jack@...e.cz>
> Signed-off-by: Danilo Krummrich <dakr@...nel.org>
> ---
>  rust/kernel/fs/file.rs | 142 ++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 141 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
> index cf06e73a6da0..681b8a9e5d52 100644
> --- a/rust/kernel/fs/file.rs
> +++ b/rust/kernel/fs/file.rs
> @@ -15,7 +15,147 @@
>      sync::aref::{ARef, AlwaysRefCounted},
>      types::{NotThreadSafe, Opaque},
>  };
> -use core::ptr;
> +use core::{num::TryFromIntError, ptr};
> +
> +/// Representation of an offset within a [`File`].
> +///
> +/// Transparent wrapper around `bindings::loff_t`.
> +#[repr(transparent)]
> +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
> +pub struct Offset(bindings::loff_t);
> +
> +impl Offset {
> +    /// The largest value that can be represented by this type.
> +    pub const MAX: Self = Self(bindings::loff_t::MAX);
> +
> +    /// The smallest value that can be represented by this type.
> +    pub const MIN: Self = Self(bindings::loff_t::MIN);
> +
> +    /// Create a mutable [`Offset`] reference from the raw `*mut bindings::loff_t`.
> +    ///
> +    /// # Safety
> +    ///
> +    /// - `offset` must be a valid pointer to a `bindings::loff_t`.
> +    /// - The caller must guarantee exclusive access to `offset`.
> +    #[inline]
> +    pub const unsafe fn from_raw<'a>(offset: *mut bindings::loff_t) -> &'a mut Self {
> +        // SAFETY: By the safety requirements of this function
> +        // - `offset` is a valid pointer to a `bindings::loff_t`,
> +        // - we have exclusive access to `offset`.
> +        unsafe { &mut *offset.cast() }
> +    }
> +
> +    /// Returns `true` if the [`Offset`] is negative.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use kernel::fs::file::Offset;
> +    ///
> +    /// let offset = Offset::from(1);
> +    /// assert!(!offset.is_negative());
> +    ///
> +    /// let offset = Offset::from(-1);
> +    /// assert!(offset.is_negative());
> +    /// ```
> +    #[inline]
> +    pub const fn is_negative(self) -> bool {
> +        self.0.is_negative()
> +    }

For symmetry with Rust's primitive types from which this method is
borrowed, do we want to also add `is_positive`?

Reviewed-by: Alexandre Courbot <acourbot@...dia.com>


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ