From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6505125E44B for ; Sun, 11 May 2025 18:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746987717; cv=none; b=NLw6xaxMcfEyXSvgx24RJ+TnbPIyE9jIsuoIuJb+4tZ3IGDSyy51gmi8q73Ly9hSboHmaoC92zv0UsSvijELkDqN0pQZd8ROZbHbSkJBu5+1fgxVsU5WICOr2GyvDTzVnVSWaclL7eDNnyQ69uqBYjAPn7R7JdaEXCvyg72zwZY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746987717; c=relaxed/simple; bh=QhTkajzKAxWgUuhE+SsnQ0WLgzbrN9mbaO5miIxAQv0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bj8QcNkyCt9cnaUDgUW01fqmpdlLzY36PIXFhdes0+62Xe9wdi2izeRRq7F/XxYlU+HmaqNVDQCUNVzBWfA4kMgOazON2fMlwJL5orKyfoTrLcFBGvtih3ZKwpcHYQQkgI/2ge0s71DdPKc5G31BMvLA8WDqrBibow1fjby1saY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ABGexc1R; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ABGexc1R" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-43ce71582e9so27061265e9.1 for ; Sun, 11 May 2025 11:21:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746987713; x=1747592513; darn=lists.linux.dev; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/3HklZYW0UfqWPqGSjmRU8CIidceE6c0Ikzvi2T3rJ8=; b=ABGexc1ROKK1fRYDow0I0xcXMfU7S7HfnoF7IYDWZYm1+SDwk8q6xMlpfBK9ubSCCN +9mFwvLgHbv+zP8DkGMlDsDVGX1Qke1vbgS6V2FjfNUpxqy2i5AV+UK88XaM9bScRr4C wKD5c5XqAGGP5uTKCKO0XlOSSvFUpvBzRc2WtQXYnKFpQeO0RG0P2Bs6bevLmNv4VESd ThXV1Y6Y5zMug4Me09DvszZZU6mDha8a1ivKwTUfGbsR2v+7ASEmse9TrSMnuQMfadgb B+nJfmuWukT1DfUHevB1e5U9VfWP/L4uXhbij7u9v4oUDGRL9pe2Nfa2DHMt/hGnDnZb MU0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746987713; x=1747592513; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/3HklZYW0UfqWPqGSjmRU8CIidceE6c0Ikzvi2T3rJ8=; b=YEjhpuakU/pPG7ggqQJiiFHbmf3S64eZP2g7Gd6YEAwRm+pSlc4bTEzePwBYsSVvjV iZ6rN1Q2FSjSSUEbTf97168xNNc/w05hx+Kuf2K706IkSDzHoTVr+spPa1gatm6PHJ3q cFxGRT8+7CPjiADE4A5qNVpgKCe6whZJIAlMDXR8JDZ1dR0jTw9wu+6/rIGJQcCdDyYN YGX6Tw0XdoNNrvqwOMYHNxL+ZN+nyExJH+wGkRr4NdUbyX5Z+pNLsaZyNrR1rsIgqB+R ufi8DyTAd7ql1DAO/CLdMC0VtJ+3GT/dGFRmKc6H8hMl/WAQlSTVP4F4qRmdNmqUAIYD KrAA== X-Forwarded-Encrypted: i=1; AJvYcCUyMGoyXONmGNazgYpPcGjm16gkgp9dtHPkRKhYBNMEXtMPb4NfPicla1VZJuY8E62pUkVA@lists.linux.dev X-Gm-Message-State: AOJu0YwasiWl19ZOGsLsp2eaGTsCMHbF0c8K3ij6IhBBKvFg6UCQNCSq Ld5S9vT6uozeXRjy08t0ns+12dzTMhalvMixkoJxNrmbaA+QMg7z X-Gm-Gg: ASbGncu6uiG/Y1kp3C/oOzpPt95CNtYAOBUciZEdGANtao7zfgQMePyZsM/OaBdMRH8 olsrBMkmAS0Lqh5d3cWmzWDI4v6/cYBobxVhjGY8YdQVqa/Aj4vi1V0mc+Jnc/bi3seJdQ9ckzK ERdvf+QqLKDKrHSR1BM1fB/e+sN91Y8e2ZdIIYN9RFFym85G2RRlmxfmWZoS+xi/Gp7cNjmirwC HlybqMX0OvykPMBGR5sbkmUlbiAM7O9utne2VXSWvBqwLAwUn8pbZ+YOZXoQVp+13/9EfP5gAlF zBdoH801GPK8BuIRViFulOJ3PU+Nsv8NmBsHujk= X-Google-Smtp-Source: AGHT+IGkeJH/ofwQXOSbok2LaZm3fwBgxHG1sSQWfzDEjcj5xXr/tGevPyUoM/i1Kb1WLFHfESlq0w== X-Received: by 2002:a05:600c:83ca:b0:43d:2313:7b4a with SMTP id 5b1f17b1804b1-442d6d18302mr108685075e9.3.1746987713202; Sun, 11 May 2025 11:21:53 -0700 (PDT) Received: from [10.0.1.56] ([2001:871:22a:99c5::1ad1]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-442d67d5c7bsm100418495e9.4.2025.05.11.11.21.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 May 2025 11:21:52 -0700 (PDT) From: Christian Schrefl Date: Sun, 11 May 2025 20:21:38 +0200 Subject: [PATCH v4 1/3] rust: add UnsafePinned type Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20250511-rust_unsafe_pinned-v4-1-a86c32e47e3d@gmail.com> References: <20250511-rust_unsafe_pinned-v4-0-a86c32e47e3d@gmail.com> In-Reply-To: <20250511-rust_unsafe_pinned-v4-0-a86c32e47e3d@gmail.com> To: Sky , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , =?utf-8?q?Gerald_Wisb=C3=B6ck?= , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, llvm@lists.linux.dev, Christian Schrefl X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1746987711; l=8462; i=chrisi.schrefl@gmail.com; s=20250119; h=from:subject:message-id; bh=QhTkajzKAxWgUuhE+SsnQ0WLgzbrN9mbaO5miIxAQv0=; b=Uy/fXqbRpuiMliNJI1DyK1Mu1s7D8QQByzo32leRblrToRzx25zquo/r2F6ljtuxJwf18PB6o eLZqrbk8UGlCwgO63k44/hrHcGifhqV9sUKYdC+8F4ZgkXXGGceKzM9 X-Developer-Key: i=chrisi.schrefl@gmail.com; a=ed25519; pk=EIyitYCrzxWlybrqoGqiL2jyvO7Vp9X40n0dQ6HE4oU= `UnsafePinned` is useful for cases where a value might be shared with C code but not directly used by it. In particular this is added for storing additional data in the `MiscDeviceRegistration` which will be shared between `fops->open` and the containing struct. Similar to `Opaque` but guarantees that the value is always initialized and that the inner value is dropped when `UnsafePinned` is dropped. This was originally proposed for the IRQ abstractions [0] and is also useful for other where the inner data may be aliased, but is always valid and automatic `Drop` is desired. Since then the `UnsafePinned` type was added to upstream Rust [1] by Sky as a unstable feature, therefore this patch implements the subset of the upstream API for the `UnsafePinned` type required for additional data in `MiscDeviceRegistration` and in the implementation of the `Opaque` type. Some differences to the upstream type definition are required in the kernel implementation, because upstream type uses some compiler changes to opt out of certain optimizations, this is documented in the documentation and a comment on the `UnsafePinned` type. The documentation on is based on the upstream rust documentation with minor modifications for the kernel implementation. Link: https://lore.kernel.org/rust-for-linux/CAH5fLgiOASgjoYKFz6kWwzLaH07DqP2ph+3YyCDh2+gYqGpABA@mail.gmail.com [0] Link: https://github.com/rust-lang/rust/pull/137043 [1] Suggested-by: Alice Ryhl Reviewed-by: Gerald Wisböck Reviewed-by: Alice Ryhl Co-developed-by: Sky Signed-off-by: Sky Signed-off-by: Christian Schrefl --- rust/kernel/types.rs | 6 ++ rust/kernel/types/unsafe_pinned.rs | 111 +++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 9d0471afc9648f2973235488b441eb109069adb1..705f420fdfbc4a576de1c4546578f2f04cdf615e 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -253,6 +253,9 @@ fn drop(&mut self) { /// /// [`Opaque`] is meant to be used with FFI objects that are never interpreted by Rust code. /// +/// In cases where the contained data is only used by Rust, is not allowed to be +/// uninitialized and automatic [`Drop`] is desired [`UnsafePinned`] should be used instead. +/// /// It is used to wrap structs from the C side, like for example `Opaque`. /// It gets rid of all the usual assumptions that Rust has for a value: /// @@ -578,3 +581,6 @@ pub enum Either { /// [`NotThreadSafe`]: type@NotThreadSafe #[allow(non_upper_case_globals)] pub const NotThreadSafe: NotThreadSafe = PhantomData; + +mod unsafe_pinned; +pub use unsafe_pinned::UnsafePinned; diff --git a/rust/kernel/types/unsafe_pinned.rs b/rust/kernel/types/unsafe_pinned.rs new file mode 100644 index 0000000000000000000000000000000000000000..315248cb88c089239bd672c889b5107060175ec3 --- /dev/null +++ b/rust/kernel/types/unsafe_pinned.rs @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! The contents of this file partially come from the Rust standard library, hosted in +//! the repository, licensed under +//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, +//! see . +//! +//! This file provides a implementation / polyfill of a subset of the upstream +//! rust `UnsafePinned` type. For details on the difference to the upstream +//! implementation see the comment on the [`UnsafePinned`] struct definition. + +use core::{cell::UnsafeCell, marker::PhantomPinned}; +use pin_init::{cast_pin_init, PinInit, Wrapper}; + +/// This type provides a way to opt-out of typical aliasing rules; +/// specifically, `&mut UnsafePinned` is not guaranteed to be a unique pointer. +/// +/// However, even if you define your type like `pub struct Wrapper(UnsafePinned<...>)`, it is still +/// very risky to have an `&mut Wrapper` that aliases anything else. Many functions that work +/// generically on `&mut T` assume that the memory that stores `T` is uniquely owned (such as +/// `mem::swap`). In other words, while having aliasing with `&mut Wrapper` is not immediate +/// Undefined Behavior, it is still unsound to expose such a mutable reference to code you do not +/// control! Techniques such as pinning via [`Pin`](core::pin::Pin) are needed to ensure soundness. +/// +/// Similar to [`UnsafeCell`], [`UnsafePinned`] will not usually show up in +/// the public API of a library. It is an internal implementation detail of libraries that need to +/// support aliasing mutable references. +/// +/// Further note that this does *not* lift the requirement that shared references must be read-only! +/// Use [`UnsafeCell`] for that. +/// +/// This type blocks niches the same way [`UnsafeCell`] does. +/// +/// # Kernel implementation notes +/// +/// This implementation works because of the "`!Unpin` hack" in rustc, which allows (some kinds of) +/// mutual aliasing of `!Unpin` types. This hack might be removed at some point, after which only +/// the `core::pin::UnsafePinned` type will allow this behavior. In order to simplify the migration +/// to future rust versions only this polyfill of this type should be used when this behavior is +/// required. +// +// As opposed to the upstream Rust type this contains a `PhantomPinned` and `UnsafeCell` +// - `PhantomPinned` to ensure the struct always is `!Unpin` and thus enables the `!Unpin` hack. +// This causes the LLVM `noalias` and `dereferenceable` attributes to be removed from +// `&mut !Unpin` types. +// - In order to disable niche optimizations this implementation uses `UnsafeCell` internally, +// the upstream version however currently does not. This will most likely change in the future +// but for now we don't expose this in the documentation, since adding the guarantee is simpler +// than removing it. Meaning that for now the fact that `UnsafePinned` contains an `UnsafeCell` +// must not be relied on (Other than the niche blocking). +// See this Rust tracking issue: https://github.com/rust-lang/rust/issues/137750 +#[repr(transparent)] +pub struct UnsafePinned { + _ph: PhantomPinned, + value: UnsafeCell, +} + +impl UnsafePinned { + /// Constructs a new instance of [`UnsafePinned`] which will wrap the specified value. + /// + /// All access to the inner value through `&UnsafePinned` or `&mut UnsafePinned` or + /// `Pin<&mut UnsafePinned>` requires `unsafe` code. + #[inline(always)] + #[must_use] + pub const fn new(value: T) -> Self { + UnsafePinned { + value: UnsafeCell::new(value), + _ph: PhantomPinned, + } + } +} +impl UnsafePinned { + /// Get read-only access to the contents of a shared `UnsafePinned`. + /// + /// Note that `&UnsafePinned` is read-only if `&T` is read-only. This means that if there is + /// mutation of the `T`, future reads from the `*const T` returned here are UB! Use + /// [`UnsafeCell`] if you also need interior mutability. + /// + /// [`UnsafeCell`]: core::cell::UnsafeCell + /// + /// ```rust,no_run + /// use kernel::types::UnsafePinned; + /// + /// unsafe { + /// let mut x = UnsafePinned::new(0); + /// let ptr = x.get(); // read-only pointer, assumes immutability + /// # // Upstream Rust uses `UnsafePinned::get_mut_unchecked` here. + /// UnsafePinned::raw_get_mut(&raw mut x).write(1); + /// ptr.read(); // UB! + /// } + /// ``` + #[inline(always)] + #[must_use] + pub const fn get(&self) -> *const T { + self.value.get() + } + + /// Gets a mutable pointer to the wrapped value. + #[inline(always)] + #[must_use] + pub const fn raw_get_mut(this: *mut Self) -> *mut T { + this as *mut T + } +} + +impl Wrapper for UnsafePinned { + fn pin_init(init: impl PinInit) -> impl PinInit { + // SAFETY: `UnsafePinned` has a compatible layout to `T`. + unsafe { cast_pin_init(init) } + } +} -- 2.49.0