From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796054001; b=V0b+0i9zGeoYiuNUtgGKBE8jAcaH/NG28zwEoBdviwrDIIGqSKfiM4l/vPIcjF2cKDyc 9ZW6T84N1p237FHRBb/wRWT4ophA4TMV7BnWaPp3Q4QPKPpLobBEjRiAZ2a9BoIg6ppPr nKL+OqmP6Lp12Mc2q9Uyvv7QPX1Vg6NkBttcULuLKVozGJjQ9Vc9qJnZxg0ul3v5yrYeM gu/I1xMNjtMoCyYBecfQj15IgtcfUejf8f3l0lhc3JbJsz0DfmzzH+FTz2EbbSk1uLFUg dFLj4rXVanWqzXwtbYIanv5kdhqkwDUYB8M5VMAmIGqJfsqb11hizgYpx2m8Ml7IRzw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796054001; h=Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To:References: Date:Message-ID:MIME-Version:Content-Type; bh=xfE4wdL9Uhb9cej7imbuJlncxhyfuApIYsNvkDRYq9U=; b=2g8aoUBPsh1o9/o8FM+ZTxqrG6KiuQBB6Nv3gXKxc1/Uth+iqJUlIQJkQGWauwKkF6JH BvvPdyKyG1/N5FkQDMdMfTbqQ7K9THHPQ56Y/c/MDT0k6vXO5bG6ol3XJ+X4JqBOrmK0z +YrSuQjj+WH+nbPK8oMIBwFW60n8NzKwjp6yk5UMWam8SV622iei2q90l+YSrLvMnG2VY cvqrPCZRASSGD7v4LRNV1XpQGHRkkDRibXR1yN6kIPeeSo+4em6nHbrSleT2lGx2i+jGl OdtztJCiG1RN3+8E5kwwZHv66Qr1oHf43cRU61weuDxFNtOVG99G8wVGylBiwmwgIyw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LP+mJaLH; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0093606A32C; Mon, 30 Nov 2026 15:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LP+mJaLH Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EB251F00898; Mon, 30 Nov 2026 15:53:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796054000; bh=xfE4wdL9Uhb9cej7imbuJlncxhyfuApIYsNvkDRYq9U=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=LP+mJaLHyupUJ8kS6vLpJKNw9Vl2Brs7eaQ8KiEIxGDdaAmBI9HkKDy+obVO9f/J5 PW2bnYpMoYkkOyYHzJ4RMAiW0i8DN8QZ06fJe1ieUIVS1p6IwP6MAOgrlpdn5z0bB4 T1aSKrDcVcHQtpArTNqwPH1KkDsNF+qFLngAHDrxhdoIBh6EtQR0SZ5iJcPv7TiaXp uPT5wK5lt/wH0pO2BRo6CnJ6eujcvUyIpRzt3/zhbwGa1ECvzoL+NUzpTL9ErcX1Xp sD2IapgbTVPAEIJtbgEBZuXwTKF/U66ZpgUfYkkQ2kTquMalJOAhTL0H+9ujN6bOl2 tWKGKUa5fi/pg== From: Andreas Hindborg To: Bilal Hashim , Miguel Ojeda Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Bilal Hashim Subject: Re: [PATCH] rust: configfs: initialize AttributeList at compile time without UnsafeCell In-Reply-To: <20261127120646.664-1-AeeSec@126.com> References: <20261127120646.664-1-AeeSec@126.com> Date: Mon, 30 Nov 2026 16:53:10 +0100 Message-ID: <87y0aas4d5.fsf@t14s.mail-host-address-is-not-set> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain Hi Bilal, "Bilal Hashim" writes: > The configfs_attrs! macro currently declares a global static AttributeList > wrapping an UnsafeCell<[*mut c_void; N]> and populates it at runtime via > AttributeList::add() on every macro invocation. > > Although the static identifier is lexically scoped inside the macro block, > the static storage is globally shared across all invocations and threads. > When a function invoking configfs_attrs! (such as make_group() in > drivers/block/rnull/configfs.rs) executes concurrently on multiple > threads, or while C configfs concurrently reads ct_attrs_const, the > unsynchronized &mut writes into the static UnsafeCell cause aliasing UB > and a data race. > > Replace UnsafeCell and the runtime AttributeList::add() calls with a pure > const AttributeList initialized at compile time inside the static item. > This eliminates the data race, removes the runtime pointer stores from > make_group(), and allows the attribute table to reside in read-only > memory. Thanks for the patch! When you submit a fix you need to add the "Fixes:" trailer, see [1]. Fixes: 446cafc295bf ("rust: configfs: introduce rust support for configfs") I can confirm there is a soundness problem, but not as described. C configfs calls `make_group` with `subsys->su_mutex` held, and child groups inherit the subsystem of their parent, so as far as I can tell, `make_group` does not run concurrently within one subsystem. There are two ways to get a race: 1) If the same `GroupOperations` implementation is used for two subsystems, the two `make_group` calls take different mutexes and can write the list concurrently. 2) `populate_attrs()` reads the list after `su_mutex` is dropped, with only the parent directory inode lock held. With nested groups, a mkdir in one directory can read the list while a mkdir in another directory of the same subsystem writes it in `make_group`. `GrandChild` in samples/rust/rust_configfs.rs has this shape. In both cases every write stores the same value. Please update the commit message accordingly. Please update the documentation that shows the expansion of the macro. In general I think, your approach for fixing the issue is good. An alternative would be a synchronized check if the list is already initialized before calling `add`. But handling the situation at compile time is better. See below for comments more detailed comments. > > Tested with LLVM=-22 (verified zero runtime stores to DEVICECONFIG_ATTRS > in LLVM IR) and booted in QEMU with CONFIG_PROVE_LOCKING=y, exercising > /sys/kernel/config/rnull mkdir/read/rmdir and 372/372 Rust KUnit > doctests. > > Signed-off-by: Bilal Hashim > --- > --- a/rust/kernel/configfs.rs > +++ b/rust/kernel/configfs.rs > @@ -117,7 +117,6 @@ > use crate::sync::Arc; > use crate::sync::ArcBorrow; > use crate::types::Opaque; > -use core::cell::UnsafeCell; > use core::marker::PhantomData; > > /// A configfs subsystem. > @@ -714,43 +713,32 @@ > pub struct AttributeList( > /// Null terminated Array of pointers to [`Attribute`]. The type is [`c_void`] > /// to conform to the C API. > - UnsafeCell<[*mut kernel::ffi::c_void; N]>, > + [*const kernel::ffi::c_void; N], > PhantomData, > ); > > // SAFETY: Ownership of `AttributeList` can safely be transferred to other threads. > unsafe impl Send for AttributeList {} > > -// SAFETY: We do not provide any operations on `AttributeList` that need synchronization. > +// SAFETY: `AttributeList` is immutable after compile-time construction. You don't need the compile-time construction part. Just state that `AttributeList` is immutable after construction. > unsafe impl Sync for AttributeList {} > > impl AttributeList { > - /// # Safety > - /// > - /// This function must only be called by the [`kernel::configfs_attrs`] > - /// macro. > + /// Create a compile-time constant attribute list. > #[doc(hidden)] > - pub const unsafe fn new() -> Self { > - Self(UnsafeCell::new([core::ptr::null_mut(); N]), PhantomData) > + pub const fn new(attrs: [*const kernel::ffi::c_void; N]) -> Self { This needs to be unsafe with a requirement that `attrs` is the shape we expect. We should also add this as an invariant `AttributeList` so we can rely on it in `ItemType::new`, but that is orthogonal to this patch. > + Self(attrs, PhantomData) > } > > - /// # Safety > - /// > - /// The caller must ensure that there are no other concurrent accesses to > - /// `self`. That is, the caller has exclusive access to `self.` > + /// Convert a static `Attribute` reference into a raw pointer for the list. > #[doc(hidden)] > - pub const unsafe fn add( > - &'static self, > + pub const fn attr_ptr( > attribute: &'static Attribute, > - ) where > + ) -> *const kernel::ffi::c_void > + where > O: AttributeOperations, > { > - // We need a space at the end of our list for a null terminator. > - const { assert!(I < N - 1, "Invalid attribute index") }; > - > - // SAFETY: By function safety requirements, we have exclusive access to > - // `self` and the reference created below will be exclusive. > - unsafe { (&mut *self.0.get())[I] = core::ptr::from_ref(attribute).cast_mut().cast() }; > + core::ptr::from_ref(attribute).cast() I'm uncertain that having this function brings us anything. Could we open code this line instead? > } > } > > @@ -984,22 +972,7 @@ > @no_child($($no_child)?), > @attrs($($aname $aattr)*), > @eat($($rname $rattr,)*), > - @assign($($assign)* { > - const N: usize = $cnt; > - // The following macro text expands to a call to `Attribute::add`. > - > - // SAFETY: By design of this macro, the name of the variable we > - // invoke the `add` method on below, is not visible outside of > - // the macro expansion. The macro does not operate concurrently > - // on this variable, and thus we have exclusive access to the > - // variable. > - unsafe { > - $crate::macros::paste!( > - [< $data:upper _ATTRS >] > - .add::(&[< $data:upper _ $name:upper _ATTR >]) > - ) > - }; > - }), > + @assign($($assign)*), Is @assign dead code now? I think we can remove it. > @cnt(1usize + $cnt), > ) > }; > @@ -1052,12 +1025,16 @@ > // We need space for a null terminator. > const N: usize = $cnt + 1usize; > > - // SAFETY: We are expanding `configfs_attrs`. > static [< $data:upper _ATTRS >]: > $crate::configfs::AttributeList = > - unsafe { $crate::configfs::AttributeList::new() }; > - > - $($assign)* > + $crate::configfs::AttributeList::new([ > + $( > + $crate::configfs::AttributeList::::attr_ptr( > + &[< $data:upper _ $name:upper _ATTR >] > + ), > + )* > + ::core::ptr::null(), > + ]); > > $( > const [<$no_child:upper>]: bool = true; Best regards, Andreas Hindborg [1] https://docs.kernel.org/process/submitting-patches.html#describe-your-changes