From nobody Fri Dec 19 10:34:28 2025 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 3C2DF269CEB; Tue, 18 Feb 2025 13:02:21 +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=1739883742; cv=none; b=nvlzNlDM2tZtHb4lucBO6zbQDbQq/tHAQOCF96wsb6Lemh+XuLcJo17AG9sX7lLsoqlDEjtD1ulfeS7J86o4zpqUoc3AB8EYJZTRWTWtOIeGToj1hmSwPJkZChTzlbEo03cLZOe61zguC0vya4DHB0n/oQHjqZf9U/70O+dz028= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739883742; c=relaxed/simple; bh=eORqxd0mJn05akI2ESSnkwHFWYB8nod8rI3Ghzxpw9g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aeEeq6YfJCM/JCICAmSFmS8UJX5xf7004ipoVMcKqKM6wm/ZgwG5BqeiDzL5S9vlh5C94FTEBgYmdTmJzDxsdoDTCMA9VH51PVPmTn2liqCO9I7Vgxue9SBmU9ncqCp7neOwKygRlcZNp7bw2pTFIMmkWlrQ4nRdeiAx5MImxYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=na8orcHN; 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="na8orcHN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60DFCC4CEE2; Tue, 18 Feb 2025 13:02:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739883741; bh=eORqxd0mJn05akI2ESSnkwHFWYB8nod8rI3Ghzxpw9g=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=na8orcHNWaxPtN5CoIJLgH4PhCwiW7zHqA62CFqEyEEzqqB2VY9NyEtwYn/p8hz3B ncPT1gHHJCSzt3TvD+c4+tw/MlARr4qkWhLUxKFaxUIWqeF6o8NuHP6wZlN0oI6RfM CkLOCxFi3tq5ZWu9PMSA9LF/rnlrnfN0i7IdQ49tbOjJnEu/V+jfyouSfm38q94KHh gVow/G0L3mW3PfJAi/e1n3EZGuUj1PH5De1+CW+N6cruPW3XTSE0oTKQvB9ha3FT7x KEv9xRsXDeSqwAGL3neYr7LNaoSOo5gYbn4yd/algxuc5m4uJ6KNYt9cDInkbFtcQv IsIXzwQIV1J6A== From: Andreas Hindborg Date: Tue, 18 Feb 2025 13:57:16 +0100 Subject: [PATCH v3 1/4] rust: sync: change ` as ForeignOwnable>::PointedTo` to `T` 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250218-configfs-v3-1-0e40c0778187@kernel.org> References: <20250218-configfs-v3-0-0e40c0778187@kernel.org> In-Reply-To: <20250218-configfs-v3-0-0e40c0778187@kernel.org> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Danilo Krummrich , Joel Becker , Christoph Hellwig , Fiona Behrens , Charalampos Mitrodimas Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3517; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=eORqxd0mJn05akI2ESSnkwHFWYB8nod8rI3Ghzxpw9g=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBntIO+fsEE+kNjovn0VrilnLGRuRMSAXn7BSarl s0TdSE1kQmJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCZ7SDvgAKCRDhuBo+eShj dyOWEAClTZUEf7xX5fDUfakBFemdXABwwkJ3YP6ID8r1waBuVzdJMkTW88nkIjp08fix2X96uaB EIftNsf6lsXF6R8QZiH7FBF4Ay16u60LGTEQiVyMN2I27A+2m1mvairYwDqWRkc/7lgIeYMYfQ3 xEoMY+t8PJ66daMhqWR0NxYno39w+/dXKI4Ogk4wwbRETlzXUf3O/xfxPNjIA1M479/ZbTQX55c /02gjDOVoWhgyAm9lYgqXpVxmukjD2ab5kXJ6PFNiKP3c3/i6sRMGV4FkZwV7z8geyiPq1uTMuX bytai9FhM2dd0sLWqG4uNHli3B7aNYwI+DfwmVZUdKIz1/ceJKu8fOcSX9X1bZFvStIwKwKL6Up 2b6uSYs9gK+hPWW5y0AjEl3SQWqKg0JTgzY3p5gz7/rsi9T2PdfN/jZVbGiE5ha+Czcuz8h+p6a e0O1VEjMR6xw/H0X/SG71yWHydlXdeQlxayVbpaxvvP2atRRyd4gTz9bPPeBy5D1aHZ45WY5pa5 SaXTzq7h+cdoVlDygp92mRBq8lzJVnL4IgwCQN54XRssAQ/1IEq7qQcslA8PSd2/KNC/usFMqax T27Bi1Q9yInaZj8VtsbJipKHMXDhbS4Q0O0VglQ2+iCwuIkE16bYNFbxtxYPS6Vd2TW6om6HGxh OYOBsLpDNU0C9Yg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Using `ArcInner` as `PoinedTo` in the `ForeignOwnable` implementation for `Arc` is a bit unfortunate. Using `T` as `PointedTo` does not remove any functionality, but allows `ArcInner` to be private. Further, it allows downstream users to write code that is generic over `Box` and `Arc`, when downstream users need access to `T` after calling `into_foreign`. Reviewed-by: Fiona Behrens Signed-off-by: Andreas Hindborg --- This patch is a dependency for Rust `configfs` abstractions. It allows both `Box` and `Arc` to be used as pointer types in the `configfs` hierarchy. --- rust/kernel/sync/arc.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index dfe4abf82c25c..3d77a31e116f1 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -143,7 +143,7 @@ pub struct Arc { #[doc(hidden)] #[pin_data] #[repr(C)] -pub struct ArcInner { +struct ArcInner { refcount: Opaque, data: T, } @@ -345,18 +345,25 @@ pub fn into_unique_or_drop(self) -> Option>> { =20 // SAFETY: The `into_foreign` function returns a pointer that is well-alig= ned. unsafe impl ForeignOwnable for Arc { - type PointedTo =3D ArcInner; + type PointedTo =3D T; type Borrowed<'a> =3D ArcBorrow<'a, T>; type BorrowedMut<'a> =3D Self::Borrowed<'a>; =20 fn into_foreign(self) -> *mut Self::PointedTo { - ManuallyDrop::new(self).ptr.as_ptr() + let this =3D ManuallyDrop::new(self).ptr.as_ptr(); + // SAFETY: `x` is a valid pointer to `Self` so the projection belo= w is + // in bounds of the allocation. + unsafe { core::ptr::addr_of_mut!((*this).data) } } =20 unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> Self { + // SAFETY: We did the reverse offset calculation in `into_foreign`= , so + // the offset calculation below is in bounds of the allocation. + let inner_ptr =3D unsafe { kernel::container_of!(ptr, ArcInner,= data).cast_mut() }; + // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr) }; + let inner =3D unsafe { NonNull::new_unchecked(inner_ptr) }; =20 // SAFETY: By the safety requirement of this function, we know tha= t `ptr` came from // a previous call to `Arc::into_foreign`, which guarantees that `= ptr` is valid and @@ -365,9 +372,13 @@ unsafe fn from_foreign(ptr: *mut Self::PointedTo) -> S= elf { } =20 unsafe fn borrow<'a>(ptr: *mut Self::PointedTo) -> ArcBorrow<'a, T> { + // SAFETY: We did the reverse offset calculation in `into_foreign`= , so + // the offset calculation below is in bounds of the allocation. + let inner_ptr =3D unsafe { kernel::container_of!(ptr, ArcInner,= data).cast_mut() }; + // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr) }; + let inner =3D unsafe { NonNull::new_unchecked(inner_ptr) }; =20 // SAFETY: The safety requirements of `from_foreign` ensure that t= he object remains alive // for the lifetime of the returned value. --=20 2.47.0