From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 31F1FCDB46F for ; Mon, 22 Jun 2026 13:57:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 82C2D10E643; Mon, 22 Jun 2026 13:57:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="fRZo6/Q2"; dkim-atps=neutral Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id DC0BB10E6FB for ; Mon, 22 Jun 2026 13:57:13 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 5E0E6601F3; Mon, 22 Jun 2026 13:57:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D7481F00A3A; Mon, 22 Jun 2026 13:57:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782136633; bh=EBsxttHC2/DLBodLweCzlXw/YoBMkYiSD7bRJRJM+hA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=fRZo6/Q2a+793MV9qHOPWZrKsODm55wGLV2V9nxP3tfY2O7lTaqvSyztdcQt6o8OM tiBjPkUwkqrxWjOAAcGnZVL2H4ORd/FWCD9VqmUiaiVsiJxY1zm4WVQ2t7YHu5tDyD PRuv5LXNM+J6eX87bYSkN+oAjMTeaktQyrl9Qda/AHqzFaw2aPm4mqf41LMu5qIxsR wT8FGw1GWRJoVwylWX8Og5S6WAHIJADr6LRXB8nknj6alSR6E01CNDaNQYH3ORTvnl QiCm5f1RziVNoWKFNGRAwbvoHO5WJQfxxindRZXW8HrtO43Q2LBdG1JZhJhH26KGLc 3cbVQbx48UdfQ== 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 , Daniel Almeida , Greg Kroah-Hartman , Alexandre Courbot , Asahi Lina , Matthew Maurer , Lorenzo Stoakes , Joel Fernandes , Burak Emir , FUJITA Tomonori , Krishna Ketan Rai , Tamir Duberstein , =?UTF-8?q?Onur=20=C3=96zkan?= , Eliot Courtney , Mirko Adzic , Alistair Francis , Shankari Anand 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 Subject: [PATCH v4 1/5] rust: types: implement ForeignOwnable for ARef Date: Mon, 22 Jun 2026 15:56:49 +0200 Message-ID: <20260622135654.334961-3-phasta@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260622135654.334961-2-phasta@kernel.org> References: <20260622135654.334961-2-phasta@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 --- 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