From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1B6BD35C6A0; Wed, 22 Jul 2026 14:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784731825; cv=none; b=cB6M9EYWUVyOzsvnNpoCoNxfTnOeEBpjL+0ZHVW4UuKyQxBU04BZf/fvy98U6e1GpwoOYBnUiVr8xORkX3ipwSx8EQBog/rqI2olBoqxHRhf7fC/Loml7Iq8uvCzWAVHoZLJi5DXBm4vGqBGI+TnQo+Kg9LI00mWph1IrUTziFM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784731825; c=relaxed/simple; bh=TGSVS27VsGgTdKeWuqJIXGATo3lWZJfA/ki8bAvqyz8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l6a0MjTQ4N3NljUBfIYc6NwrPjE/GU24i3FXlopp75VvIr/TJMAC/y53qaUK3sA29TdnzaV0SGws2QeVldceJ0xM1mcT/iFLW/JjThBp0JIhklBF1HkrDJuKcJdHyzNHir4d3ltWNEj8QIFoG7npqFfJOExuyFlIpN9sRiEeDVs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CdyiLiy6; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CdyiLiy6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC3DA1F00A3A; Wed, 22 Jul 2026 14:50:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784731823; bh=LXJwHWfyIyYAol7hr0XIHN7RcUAOgHD73uS/AqOnnf4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CdyiLiy6QQZFkwdpmQspAU4qGFtRk7dLsGHJnIWbp+o8I7nYrzv7M2bePkHhawT64 TXu5Yc54zp3Dg14JUPZ24EEYv8Gd2QnPertb6B/ey6Yefq6O2DHn/87SrGdmI63kz+ NqSxBBWeIhr+JEfKI6fTfbTz85t9DeHXbQsdWafUwzBCLdsV6unGA0phw0T7t5UraA B+KxuPlQcl08Xup7R6myhFpRTBSzxCnPgHPFiPDHnmXddysDKLWmlrylAnDpEPfCK4 nF3/+fCDFC+TzoGQpBfCnNnzdemVAg5VA3LVpHjrA4x3djK0BQcSTxgzhC8C5tUSM7 ppKTcFAVsU5ww== From: Philipp Stanner To: Miguel Ojeda , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?UTF-8?q?Onur=20=C3=96zkan?= , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , Lyude Paul , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Greg Kroah-Hartman , "Yury Norov (NVIDIA)" , Asahi Lina , Lorenzo Stoakes , FUJITA Tomonori , Eliot Courtney , Mirko Adzic , Daniel del Castillo , Alistair Francis Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, rcu@vger.kernel.org Subject: [PATCH v6 1/6] rust: types: implement ForeignOwnable for ARef Date: Wed, 22 Jul 2026 16:49:25 +0200 Message-ID: <20260722144931.1070312-3-phasta@kernel.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260722144931.1070312-2-phasta@kernel.org> References: <20260722144931.1070312-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Danilo Krummrich Implement ForeignOwnable for ARef, making it possible for C code to own an ARef. Since ARef represents shared ownership, BorrowedMut is &T rather than &mut T, matching the semantics of the underlying reference-counted type. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl Tested-by: Daniel Almeida --- rust/kernel/sync/aref.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs index b721b2e00b98..540766613659 100644 --- a/rust/kernel/sync/aref.rs +++ b/rust/kernel/sync/aref.rs @@ -24,6 +24,11 @@ ptr::NonNull, // }; +use crate::{ + prelude::*, + types::ForeignOwnable, // +}; + /// Types that are _always_ reference counted. /// /// It allows such types to define their own custom ref increment and decrement functions. @@ -188,6 +193,41 @@ fn eq(&self, other: &ARef) -> bool { } impl Eq for ARef {} +// SAFETY: `into_foreign` returns a pointer from `NonNull::as_ptr`, so it's non-null. The +// `ARef` invariant guarantees that `ptr` points to a valid `T`, so it's aligned to `T`. +unsafe impl ForeignOwnable for ARef { + const FOREIGN_ALIGN: usize = core::mem::align_of::(); + + type Borrowed<'a> = &'a T; + type BorrowedMut<'a> = &'a T; + + fn into_foreign(self) -> *mut c_void { + ARef::into_raw(self).as_ptr().cast() + } + + unsafe fn from_foreign(ptr: *mut c_void) -> Self { + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous + // call to `Self::into_foreign`. + let ptr = unsafe { NonNull::new_unchecked(ptr.cast()) }; + + // SAFETY: `ptr` came from `into_foreign`, which consumed an `ARef` without decrementing + // the refcount, so we can transfer the ownership to the new `ARef`. + unsafe { ARef::from_raw(ptr) } + } + + unsafe fn borrow<'a>(ptr: *mut c_void) -> &'a T { + // SAFETY: The safety requirements of this method ensure that the object remains alive and + // immutable for the duration of 'a. + unsafe { &*ptr.cast() } + } + + unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> &'a T { + // SAFETY: The safety requirements for `borrow_mut` are a superset of the safety + // requirements for `borrow`. + unsafe { ::borrow(ptr) } + } +} + impl PartialEq<&'_ U> for ARef where T: AlwaysRefCounted + PartialEq, -- 2.55.0