From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010001.outbound.protection.outlook.com [52.101.85.1]) (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 04F21383C9C; Mon, 20 Apr 2026 20:10:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.85.1 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776715859; cv=fail; b=IObdsEcdPOYtC/xZQRzfoIx85zIAuqPChDraWBXIQwMrMT5DwzhNcbUOtBPkTCpUVuoE89wveDSBebQQf3D2qtkDw/QCsVG5UF4EFlQWw3+jqjGjZTTx9ZY8wADSq1p96ozXYydURIvMy9p9ZO5iAFAiAU5kobG7I/7NLQ+G540= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776715859; c=relaxed/simple; bh=2STyPvtT4G/QYQ/aoUQXAk0Stn9T1xQDImfDA6ybOV0=; h=From:To:Cc:Subject:Date:Message-Id:Content-Type:MIME-Version; b=LRj1E7r6VJiR68HZ9SVVRgeW0V8CivdUbjZB7yI/UslPi7u4aweb9RpoYIqcM8m80jXeDkdypNvZDZf3YJK2ypMCHVkMctA+uEJbMIGp0q/kZOZdbGq+bH5ZoViImLzWFFXV0DS5TS3diNRWKAfXaaqTIKKrseGnazvGp5Srh2M= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=ud077J3u; arc=fail smtp.client-ip=52.101.85.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="ud077J3u" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sZrIkjG/PJCeM7pkdI4LEa5opMZg/9PIX+EcJvsMjkvHA2ZHw2hw7AL1Hotic3E5BndGiNkxa6SvzZWeLIzrTTP3+oEKIzdDL0OQnYl1EezWx9Iw5Sq3g8EUtQ8AERr+YCxTJYe65AlPjF6/movPJDE/9x+qwTrFh2ZvhtoHiTU2fCF/z8GtIWfHKNnsxEzomiUH4hrqMKqCVXLHFDfH9q8wWlKYs3+S5NijOGeq3bjpvMyRj1QdSgpeFln7kYB3m/fdJ3q6+wY0zofWjHkJqcsO8AGu4vpeX/L1Q3lJz4XZar/bfCercPNo9M7T1ANIZ2OHqlrKsY1T1pwQ3p1alA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=k6vkah4oKTnsVsKENe/dzkV+z0PiUPQEtsDfBoSG8Vk=; b=ULW4lrDjr6U7jpNfb8RkWA+TmK0vLQ5D7gaMtvjm4+zIuHEp74HQ5ry+VnqCSRwcV7xG/RiPSWmD/0Z/fJuEGIfTGn5qC0LDnZd4QoGiJNHC171Uoc32lMHS5Pdiuf8qJB/CPV/A15j3z2uOTZtNTQLh06TehpdDaKaylwq233TkDqSWfodaCE2nIDtMCtqA8b58e1D3LP8JS3CQG448+PxqGOjywKNAj1xa6mn2r5SUKIBCXEiwU9jdxqJuOJcsa0L/mhtf49VpCCE9urjzNaD4lkTFU5QHbpvhNjJLdqeG7DXHYxuyA1eiMVGVQMI6ieE2w3Q5o9WSXEnorzzhpg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=k6vkah4oKTnsVsKENe/dzkV+z0PiUPQEtsDfBoSG8Vk=; b=ud077J3u4Q+ZEeWUS463jOYu9QU32/j0qB1ytgSdvk5WJh2Xk2FXu5+J5hVCD/sMRtjrXT0K9SUnZ5Ae9CgTXuLUQqeFUnlbKvuZA6dRho1ug+Fe6D3OhkJFcYGe+k8GRPJ0lhGTWEiuXu9iyZoJD4vlhjuJsRrmLOUl5ifOoeltgvdDmor47TLJdlmjqqgdseujLg1Bh8fg/8J4IXD2GwAcdX7YAop07LcrF0eMOcFLwKtaG/ufJhOg3pPkkcrqk2n7g1cdAPJa+DJXOoAIyuekZ7tX6Nor3oFaw/8SKssvYjpeeC0QX1XKxrtEoTU94ROxCM6t8Cw7A1VszJfg1g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DS0PR12MB6486.namprd12.prod.outlook.com (2603:10b6:8:c5::21) by MW4PR12MB5642.namprd12.prod.outlook.com (2603:10b6:303:187::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9846.8; Mon, 20 Apr 2026 20:10:52 +0000 Received: from DS0PR12MB6486.namprd12.prod.outlook.com ([fe80::88a9:f314:c95f:8b33]) by DS0PR12MB6486.namprd12.prod.outlook.com ([fe80::88a9:f314:c95f:8b33%4]) with mapi id 15.20.9846.011; Mon, 20 Apr 2026 20:10:52 +0000 From: Joel Fernandes To: linux-kernel@vger.kernel.org, "Liam R. Howlett" , Alice Ryhl , Andrew Ballance , Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich Cc: Joel Fernandes , maple-tree@lists.infradead.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org Subject: [PATCH RFC] rust: maple_tree: implement Send and Sync for MapleTree Date: Mon, 20 Apr 2026 16:10:39 -0400 Message-Id: <20260420201040.1894760-1-joelagnelf@nvidia.com> X-Mailer: git-send-email 2.34.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: CY5PR15CA0015.namprd15.prod.outlook.com (2603:10b6:930:14::29) To DS0PR12MB6486.namprd12.prod.outlook.com (2603:10b6:8:c5::21) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS0PR12MB6486:EE_|MW4PR12MB5642:EE_ X-MS-Office365-Filtering-Correlation-Id: e32db0a2-9dbf-4e74-48dd-08de9f18ebdb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014|921020|56012099003|18002099003; X-Microsoft-Antispam-Message-Info: IXEA1bO9eCcN8LMe2nDdD7AEccbpl8W10/hgNNrbLijhb/sikCkbSVJVjFfLLXVWafXGSW/d9a1WX8uFSX8x84pc42EhBALy5igFS74WBmooIvtpdk7hlnbg7ox/5J4yevgHKrz/wgVACJVrJidWiynJ8f8yUvTe1CRnw3TNZzmsnbkNwRPX+AZDTqMp/PDcWJk5YQnMvK3kSfRDvdxrRTlZVMHbm5gK6mvwZUkexZCrWnZJ7QJwkWgg8vafUudbhpHSFP63/c8wG6rWEvv/aXtjrMbR5hTHdsmZJHs9ElUrsFj7DMrDcaEeLb03KG3aahkniYKD7MKvDByap6r1afu1Ea7Q2CJud4/oBHyPE1c8uHqiMAnagSX52GqkfGg4AEtt0W65cnevZ47/LIBVIzuR/Cc35KbRU8xqJYSvnfZ1QDjBOu/agjU0sR5MLpYy6LpVKQQkfXT+fJ5xXF+HriGQRaJUPB5CD7xYM59vsIR3CyGvQCjyGkkNglKpjBrgMGu581HTjz03exiGlorcAKqeRkozCeMNKFhqyxpU9YPI40aOGkRENTe74xhGlzeQGj8oItJfBceaUihtzPp30oW/IcGHRgtwBDmRj6tZJOkFo5IvW2kJhO44z5cL2NfwNrjGP4pNWAys4UTMnERmmPeNucyUJA4jlCWb75ke8Zk+xxtpCAvrkfJwL5fveAz85ZmC9R6Yk1m025M8hgp2080fdCX4+Rq3lp8zfFKOI9ucgl3pk7ZPj9rvQmwIY2jDGK7C0ayXMGrEyosh6c5eiQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS0PR12MB6486.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014)(921020)(56012099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?kWbbY5dbb323nM2w2lZhc47H5gm89ae9P6gpEDbQUSyMOGLu3Jm7hMiFtUug?= =?us-ascii?Q?0A6oTDfgmjtWxHeS2vhwrx3D8MlbJo6b1rb8ufGOkP4/O5QeQ5gmwDn2fzKI?= =?us-ascii?Q?1Sfc0rVH2n8sbeAlDqklckT83Vb0l0TtXqrUwvEFwSDz7pmL+4fhP8K93N0B?= =?us-ascii?Q?BE1SW2Ri2v5+0HpJWrbPfz3UUE7aBzTcjEjhlxhZMuevmVS/0Px47bVKB5AT?= =?us-ascii?Q?NNSU8fn0/hE/67lJTo03kcGYIrwlyedBL5VSuSo+paud08jTOIIIcBbOUNeK?= =?us-ascii?Q?9JuY4P9+EZcPVNm39Xihzj6mx8qXrocv1APnxbwSADSIbvcgV+PGOmbfl8d1?= =?us-ascii?Q?XoLiE+XVAHvzJtp4AFldsQdQGI4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= =?us-ascii?Q?0xF9jsziEJ0DJOWkUGcbAX3DjxP9o8j2NrCb2rtVtQxGxLxZelAisHIoSvve?= =?us-ascii?Q?m1gBSTRGlhvgVy0CIopFXjTMsyzV5BddBM4DT0rwWoYNMKqpCvIeb4ZEgArH?= =?us-ascii?Q?vqQ634upvH2NmUqTYROccOIfata8+WgjBbcCw8j162dGSjRnIWdqqoFsP0qc?= =?us-ascii?Q?qsazC8EfIKpbugd0dTQmmx58Ll9ANjz8lBtFDiQUORBjrJh3QS2jLrV4g5Qj?= =?us-ascii?Q?AGbYC5MLk/UvO0aX/YxQ13hMwFactkSVo1y5jIQs3NKkFkfUi9GoWC4k534h?= =?us-ascii?Q?gNL7jl1tOQdPXaSqdel5X9fDi9DUj3BIpzXqKgqPZYWbrVi7Y8mLl0Fl+Pw6?= =?us-ascii?Q?99yK8bZ7ryH6t3aWTIVt9+ER3xiLVOjCW27uzRyvYSlfG6gvTs0pOV1Jk2/y?= =?us-ascii?Q?0iTwv9KCsoyWYS4Q1Z/tujwctYFvyZ1mtr3CE49nf20/c5EXduqD3RTgnSeQ?= =?us-ascii?Q?zFESsWQ1og=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e32db0a2-9dbf-4e74-48dd-08de9f18ebdb X-MS-Exchange-CrossTenant-AuthSource: DS0PR12MB6486.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2026 20:10:51.9988 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6pMApyMP3+hf8gl3k5ZVQQsk+q6aCW+fN5pz5yhqVFzEzTo0YD5ImQlzB4J7EHEuFQ0PN30JAEcbpe+r3oqO2Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB5642 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 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 to fix this. Signed-off-by: Joel Fernandes --- rust/kernel/maple_tree.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/rust/kernel/maple_tree.rs b/rust/kernel/maple_tree.rs index 265d6396a78a1..1ecf05871e710 100644 --- a/rust/kernel/maple_tree.rs +++ b/rust/kernel/maple_tree.rs @@ -16,7 +16,7 @@ alloc::Flags, error::to_result, prelude::*, - types::{ForeignOwnable, Opaque}, + types::{ForeignOwnable, NotThreadSafe, Opaque}, }; /// A maple tree optimized for storing non-overlapping ranges. @@ -240,7 +240,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 +305,31 @@ fn drop(mut self: Pin<&mut Self>) { } } +// SAFETY: `MapleTree` is `Send` iff `T` is `Send`. All access to the tree +// goes through the internal `ma_lock` spinlock or via `&mut MapleTree`. +unsafe impl Send for MapleTree {} +// SAFETY: All shared access through `&MapleTree` either acquires `ma_lock`. +unsafe impl Sync for MapleTree {} + /// 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. + // Prevent `MapleGuard` from auto-deriving `Send` because `&MapleTree` + // is `Send` due to `MapleTree` being `Sync`. + _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 +338,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 +390,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