From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10AEE48C8DA; Wed, 6 May 2026 15:46:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778082408; cv=none; b=l/73U0LA1VboLnFlqwUsBg6/dvbfKxvP6G2kTLGFWFSWap1k7QZXauWdRC5t0ziYqTwUW4SZ282K6juaQq5rGA5mLhjq4DlzAw3ABQ+IAleo/bvnMHQUqtJ6K9dxtMXQqHvwblL6UDpvO/xnN7ZeEdNtpYahy0p2vTQmJsUujDQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778082408; c=relaxed/simple; bh=UrrPk+6g39IIpATE+r/ALmveb23Z8oczij81iLseye8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=N3CBcAuB0tM8RbulaaCltHIihCG9rKWAKkqA7gden10yXspE8NW0A6dLqZ9pdonwlp4iM5KQCnHHSjK75dWKwhZUowoM1jm48dXL1J9CvjI4Kh60vpUeNbLA9jlqo66Fn8P6D29/LMcdtjpzCbItVlAdxTNSiN6ywE6lex0BsG0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZSicA03h; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZSicA03h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C6AEC4AF0B; Wed, 6 May 2026 15:46:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778082407; bh=UrrPk+6g39IIpATE+r/ALmveb23Z8oczij81iLseye8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZSicA03hDhykXPH55kbICZ0k18n6jdHIW3qWJfHWu4GgdXsMIrQq2rPPjkdNlC+QZ YUbZXh7JTwXsXTabbOFt5or7yzrpPycZzKGAANQ1KDxYikilP+myVdI7wS3BRVWK8x v5f+YHmEoK6tjz0j947mepujTwD9ZyPzk26ifXTBLh6GMrnqmU8JNpX3F16FRjBA0r c3gD/izfM33PzzTrRI+Ae0+KdsuVYFXsim+xBcy+ky/zpOL5BlQH9pTfb7m9pUNpHC 2o+fUC6lly4dJu2Uk06WCfGz9gDv6s7HqqLx+6Q8pi0VKIZucsqYobaJRcT/GlzP24 82P+rcLeHMTlg== Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id 4C5E3F40070; Wed, 6 May 2026 11:46:46 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 06 May 2026 11:46:46 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgddutdegleelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhunhcu hfgvnhhguceosghoqhhunheskhgvrhhnvghlrdhorhhgqeenucggtffrrghtthgvrhhnpe euffdvtdegvdektdetgfevvdehgeevhfdtheeuieeijeeitdeutdefgefhjeduleenucff ohhmrghinhepkhgvrhhnvghlrdhorhhgpdhtrhgvvgdrmhgrnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghu thhhphgvrhhsohhnrghlihhthidqudeijedtleekgeejuddqudejjeekheehhedvqdgsoh hquhhnpeepkhgvrhhnvghlrdhorhhgsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthht ohepfeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjohgvlhgrghhnvghlfh esnhhvihguihgrrdgtohhmpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgv rhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepohhjvggurgeskhgvrhhnvghlrdhorh hgpdhrtghpthhtohepghgrrhihsehgrghrhihguhhordhnvghtpdhrtghpthhtohepsghj ohhrnhefpghghhesphhrohhtohhnmhgrihhlrdgtohhmpdhrtghpthhtoheplhhoshhsih hnsehkvghrnhgvlhdrohhrghdprhgtphhtthhopegrrdhhihhnuggsohhrgheskhgvrhhn vghlrdhorhhgpdhrtghpthhtoheprghlihgtvghrhihhlhesghhoohhglhgvrdgtohhmpd hrtghpthhtohepthhmghhrohhsshesuhhmihgthhdrvgguuh X-ME-Proxy: Feedback-ID: i8dbe485b:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 6 May 2026 11:46:45 -0400 (EDT) Date: Wed, 6 May 2026 08:46:44 -0700 From: Boqun Feng To: Joel Fernandes Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Gary Guo , Bjorn Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Dave Airlie , Daniel Almeida , dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, nova-gpu@lists.linux.dev, Nikola Djukic , David Airlie , Boqun Feng , John Hubbard , Alistair Popple , Timur Tabi , Edwin Peer , Alexandre Courbot , Andrea Righi , Andy Ritger , Zhi Wang , Balbir Singh , Philipp Stanner , alexeyi@nvidia.com, Eliot Courtney , joel@joelfernandes.org, linux-doc@vger.kernel.org Subject: Re: [PATCH v2] rust: maple_tree: implement Send and Sync for MapleTree Message-ID: References: <20260506135122.GA1432412@joelbox2> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260506135122.GA1432412@joelbox2> On Wed, May 06, 2026 at 09:51:22AM -0400, Joel Fernandes wrote: > The C maple_tree struct contains a *mut c_void, which prevents Rust from > auto-deriving Send/Sync. Following is an example error message when using > MapleTree in nova-core's Vmm. > > This propagates up through MapleTreeAlloc to Vmm, BarUser, Gpu, and NovaCore, > causing NovaCore to fail the Send bound required by pci::Driver: > > error[E0277]: `*mut c_void` cannot be sent between threads safely > --> drivers/gpu/nova-core/driver.rs:77:22 > | > 77 | impl pci::Driver for NovaCore { > | ^^^^^^^^ `*mut c_void` cannot be sent between threads safely > | > = help: within `MapleTreeAlloc<()>`, the trait `Send` is not implemented for `*mut c_void` > note: required because it appears within the type `kernel::bindings::maple_tree` > note: required because it appears within the type `Opaque` > note: required because it appears within the type `MapleTree<()>` > note: required because it appears within the type `MapleTreeAlloc<()>` > = note: required for `Box, Kmalloc>` to implement `Send` > note: required because it appears within the type `core::pin::Pin, Kmalloc>>` > note: required because it appears within the type `Vmm` > note: required because it appears within the type `BarUser` > note: required because it appears within the type `Gpu` > note: required because it appears within the type `NovaCore` > note: required by a bound in `kernel::pci::Driver` > --> rust/kernel/pci.rs:294:19 > > Implement Send and Sync for MapleTree. The tree contains no thread-local > state, and all shared access goes through the internal ma_lock spinlock. > > Signed-off-by: Joel Fernandes > --- > RFC->v2: Just adjusted a few comments as suggested by Gary. > > Sending this separately as discussed in the nova mm patch series that needs it: > https://lore.kernel.org/all/252a4eef-f4f4-4edf-8154-06cae4ad8518@nvidia.com/ > > rust/kernel/maple_tree.rs | 29 +++++++++++++++++++++++------ > 1 file changed, 23 insertions(+), 6 deletions(-) > > diff --git a/rust/kernel/maple_tree.rs b/rust/kernel/maple_tree.rs > index 265d6396a78a..2400c905270d 100644 > --- a/rust/kernel/maple_tree.rs > +++ b/rust/kernel/maple_tree.rs > @@ -16,7 +16,11 @@ > alloc::Flags, > error::to_result, > prelude::*, > - types::{ForeignOwnable, Opaque}, > + types::{ > + ForeignOwnable, > + NotThreadSafe, > + Opaque, // > + }, > }; > > /// A maple tree optimized for storing non-overlapping ranges. > @@ -240,7 +244,10 @@ pub fn lock(&self) -> MapleGuard<'_, T> { > unsafe { bindings::spin_lock(self.ma_lock()) }; > > // INVARIANT: We just took the spinlock. > - MapleGuard(self) > + MapleGuard { > + tree: self, > + _not_send: NotThreadSafe, > + } > } > > #[inline] > @@ -302,19 +309,29 @@ fn drop(mut self: Pin<&mut Self>) { > } > } > > +// SAFETY: `MapleTree` is `Send` if `T` is `Send` because `MapleTree` owns its elements. > +unsafe impl Send for MapleTree {} > +// SAFETY: `&MapleTree` never hands out `&T`; all entry access is serialized > +// by `ma_lock` or `&mut Guard`, so `T: Send` suffices (`T: Sync` not required). > +unsafe impl Sync for MapleTree {} I think you missed this: https://lore.kernel.org/rust-for-linux/aepRx2jgIKmoRp-r@tardis.local/ ;-) But on a second thought, should that (we have methods of `&MapleTree` -> `&T`) happen, we could always guard those methods with T: Sync. Maybe it's worth to keep a note on that but, anyway Reviewed-by: Boqun Feng Regards, Boqun > + > /// A reference to a [`MapleTree`] that owns the inner lock. > /// > /// # Invariants > /// > /// This guard owns the inner spinlock. > #[must_use = "if unused, the lock will be immediately unlocked"] > -pub struct MapleGuard<'tree, T: ForeignOwnable>(&'tree MapleTree); > +pub struct MapleGuard<'tree, T: ForeignOwnable> { > + tree: &'tree MapleTree, > + // A held spinlock must be released on the same CPU that acquired it. > + _not_send: NotThreadSafe, > +} > > impl<'tree, T: ForeignOwnable> Drop for MapleGuard<'tree, T> { > #[inline] > fn drop(&mut self) { > // SAFETY: By the type invariants, we hold this spinlock. > - unsafe { bindings::spin_unlock(self.0.ma_lock()) }; > + unsafe { bindings::spin_unlock(self.tree.ma_lock()) }; > } > } > > @@ -323,7 +340,7 @@ impl<'tree, T: ForeignOwnable> MapleGuard<'tree, T> { > pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T> { > // SAFETY: The `MaState` borrows this `MapleGuard`, so it can also borrow the `MapleGuard`s > // read/write permissions to the maple tree. > - unsafe { MaState::new_raw(self.0, first, end) } > + unsafe { MaState::new_raw(self.tree, first, end) } > } > > /// Load the value at the given index. > @@ -375,7 +392,7 @@ pub fn ma_state(&mut self, first: usize, end: usize) -> MaState<'_, T> { > #[inline] > pub fn load(&mut self, index: usize) -> Option> { > // SAFETY: `self.tree` contains a valid maple tree. > - let ret = unsafe { bindings::mtree_load(self.0.tree.get(), index) }; > + let ret = unsafe { bindings::mtree_load(self.tree.tree.get(), index) }; > if ret.is_null() { > return None; > } > -- > 2.34.1 >