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 D42B124EA90; Sat, 30 May 2026 14:36:24 +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=1780151785; cv=none; b=JquFIaaIfex2x1uBwev4KVhzdmmPD+zXs94q+tRhc/euc5gkQ4ZwprIZOXN8aSDZz/SASCNjvtmIWAiG0XrZD0mWyOEUC0KmknlIpFJ8eFHwME7TWLVufziZb8ijYketHotc+RM/P2+xdGMRw+lPT9nBX3rdDPTo3SIMtI2GsTM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780151785; c=relaxed/simple; bh=iusRewpZ34TnQ5G2HLBSE7H0Lnu251CvtBRxV7HZGC0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UPx8GK3juxeBvBWp+awHW+fqaXCXdQ6BJgWFbksbA1l3M9MEJzZMGE90FrjBM59mh5QS1PyRaXCrrzvgrL/wgMYkdCAE7SdcfFqJ+O5ULxm/5rHqCt2GAYLaJaunTMxoZ+9TNcp16B5zLIAjlom352y/c1eGT7V8HqukchTR3qk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CVR7LIu9; 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="CVR7LIu9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3121E1F00898; Sat, 30 May 2026 14:36:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780151784; bh=pDJyMWyNek1/CTCATK0JZj8GklloT+oL+OiV9mbnCoA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CVR7LIu9D96HTbHsL8aBjWenUaBybuuukmD4fMNjxcMZnl63zeJhQ8xZBs0if29ua RutT4/vJgD70WtBIhs7aHz7+KoobG2F2itlJcCPiHANBtwgd0ZPgqdeaqEB/j5EIQF z2R8gfk0d9wJFpdGs/FXn/ikx6lpcn3R1Lh4jmhp+4/rPSzI+L3QzULgEsFRPmIjyV bqHR8sEKi4+XG+mT/+2MEIq+M8uoZ6dzlEWhQpEFJ6RgFo8wRJW53uXppvZ3NHqEar agGq7cBi/W+qCHpg+h4mbcNQ2Nn2DkAZtfYLJ7LK6e6eoPT1oQcVFI+hHFE4uMZ7Lz hoQc9Z5K74OmQ== 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 , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Philipp Stanner , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Daniel Almeida , Greg Kroah-Hartman , Igor Korotin , Lorenzo Stoakes , Alexandre Courbot , FUJITA Tomonori , Krishna Ketan Rai , Shankari Anand , manos@pitsidianak.is, Boris Brezillon 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 1/4] rust: types: implement ForeignOwnable for ARef Date: Sat, 30 May 2026 16:35:09 +0200 Message-ID: <20260530143541.229628-3-phasta@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260530143541.229628-2-phasta@kernel.org> References: <20260530143541.229628-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@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 --- rust/kernel/sync/aref.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs index 9989f56d0605..82907383c44b 100644 --- a/rust/kernel/sync/aref.rs +++ b/rust/kernel/sync/aref.rs @@ -17,6 +17,10 @@ //! [`Arc`]: crate::sync::Arc //! [`Arc`]: crate::sync::Arc +use crate::{ + prelude::*, + types::ForeignOwnable, // +}; use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref, ptr::NonNull}; /// Types that are _always_ reference counted. @@ -183,6 +187,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.54.0