From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 407FB13D88E for ; Wed, 14 Aug 2024 08:06:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622763; cv=none; b=HnPqnAi/t3jtndH3iXYh9jr2EqKAEnOMOndDYcjrJ/attAqwhWThCufRi/CJMrgWMDrBWqXJGPWUC5RjZ/zKCW/qk1lcM6z2PEpbXU6WgBK7ngn4gg2LQFl+Tq2c2r0rYZygehJywVL3Mjk80oDq4wAagQUlXiBGINbFuMh6HCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622763; c=relaxed/simple; bh=to5V9uzTlPBNFNGla22QaxneooMIy/yOvQICTxnvla8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Zbyh1G97TM9T8ACJBLA98M9Ta8TjAGJ5MGYAFIwlshyuTbUMRtQoV385TPHDKE8bCrVCU/1MhDHJXtDI5KN+p/RqjczJb4fMstfMsVdOAneY9j+36QbRVnSNmDdo6bktnHHxHtyTmfCUWXtTHcXfYpRYx1lJY6rmqfjwS/MJ2UM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lpbmThUN; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lpbmThUN" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4281b7196bbso48773445e9.0 for ; Wed, 14 Aug 2024 01:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622759; x=1724227559; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cS8w042FemB04meCg0s9sGOe2wtiJS+z6C7ez/1yX1U=; b=lpbmThUNr2SzVU4PbMZuuBMPKytnC33yeu45cJ8atUZyHQPpBslE0CizijHqLgNUtV a+BnVKpNUeqZwihCbGXiyIqpGt5pmYt9+CHWOFo7nwUDKvQfxtnJgSXmA//WmQq5G3ZH +pL+/7n7TZG8ne3X9WLE4ESTof2Rjmydyb4YUMCQSh61GDVupSWOTy/SdXR4AMrKIpGr 52xe/uFk7p2rHdZKrlxOpfzVGUAKr0P5KxkmXRfXaLh4AI7VvM5m2PUJqBWuuhYsYuuF kZ1+UXol37ezdDEPDA4wfzWUOe3uZ/zJ/0/bLm2W+7r1Jicfkjv9u21RSdSCIAWMQH4m f5Aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622759; x=1724227559; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cS8w042FemB04meCg0s9sGOe2wtiJS+z6C7ez/1yX1U=; b=xDe/zmgD/k6A8NWHBqvWJ5/3jXhsEt5jbEj32Bw+R9y1uQAuAKZCNgk1QajxT1OApd guFfEYOBxHt3j3kUlt35kJepo66jMz8i2hlCTJRvLrmwcHJUvA4COG3INGnPMbkIGr+W j10LRWTvtlDO8huh4iRpGP33UsLjupiYnzPIw7ttK3mYqpyxuDKs0LeT1k0OnIlhtAaz Cz/ovIPHhKAH4fWQfZuwLfnU5SC/vfnnxzN1NDZTX76VOHWQFdPnhIL4Z6MjisLPTqJS MN9ng7+JLzfASiANqA7xWHr9D/xLCccigN4Yr2MToK/JTW1MVqOE4Kd7dpXBpG+fvWvY fvhw== X-Forwarded-Encrypted: i=1; AJvYcCWK0v9OcYXcEVC0f3at4G/BmtQFMLKHytolwnkbnLIB8yLwxNk6ESk2VGGR/yRJ5bXYeNshxPbKyGUPEIrnxwUmb8i/WyjBzLsus5X+ X-Gm-Message-State: AOJu0Ywtxcfs05z2x4yCCauliD5uOyd+Uwiu/zuk4cNOp9qj6pQoZP57 yK+4yDGDsYgrg7Y/EGz9vFo5N+7JBpavw6oeqK1dI2s/dfA+kJqnK5u5L+Z0S3CKZo8skLPrbsq Uk3WXNR9gc0z2Xg== X-Google-Smtp-Source: AGHT+IHG6pEJIWLU9mt7Zghg8hgN3WIt5DM3XzHjfDM6pd1KFbLaVZ0ECjHsRl8XIe2Pxvf09MAEI820HChGkGA= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:4c99:b0:424:a5b0:9a9 with SMTP id 5b1f17b1804b1-429dd273b35mr63605e9.7.1723622759320; Wed, 14 Aug 2024 01:05:59 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:20 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4060; i=aliceryhl@google.com; h=from:subject:message-id; bh=swQo6k413j3se9yButdYhvfnah6TceJmmk7q/aYZXEE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVcIR+udRWbqZ+Yn2f+g1A12sPAkWbH7C8Lt TFC+mN8PByJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXAAKCRAEWL7uWMY5 Rpe9D/98+0m6jaOygMTBRlIaPJVGy79dX4G13YldUIQyhqT+nmbqTsyaAeXDL/9v+PocYr7pZ2w T3A/JNRKH5sjidtbckjVluCqZGDubN5U+02jQxZzh7pWcI/0ZRWQCkNC/MqDYeVO+mrCcwxzZAI OlZC9038K+BrOyoo/hPwHtf15X09bxuUR4SoeiCouyGnHopOjyvmVSPwX5vL0IfMqbwApj5+sl6 S7s3jElQyfttf/WFT3iNjWqY3oJGRtbjCDz1m/NHO701vgJCbgaATOxw1lCMeT02a6CpM33dWeH rlAdJ62Fr9qNRgq5LzgkVBEE7II33V8JmQ/pggn5VsZXrw2X2WKExvmPdP6bKcHWUYL6urRNfpu NfPhTDTUeusOayY416A4MPj60cuEyqlIH+X/+UF4cJKPHKXvZqgGGD4442pXGcHRgU76nbh+WEl J0wNfbZTWI8QPXL8I1t0exVfWRGwfdWVMUagd2r5PMqVO73wIZuDwg9+XVNWzmbuyvViGBqUaMc dt8aIkzbXEp7DCtX99+PbZu2Zz/rUpZ+KAjcmo0mEnMk/FtVKyXjt/TKAptorwYI+hDIzDv21iC /8g4vMuqyXYf3uGn7tPZkHX6SjRLxELMKWZfGGRO6h8QKL00pKunR6ioP8OqCm4YtlRo9jz8m2F 7wQAwF/1dcRvu0g== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-1-f5f5e8075da0@google.com> Subject: [PATCH v5 01/10] rust: init: add `assert_pinned` macro From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Benno Lossin Add a macro to statically check if a field of a struct is marked with `#[pin]` ie that it is structurally pinned. This can be used when `unsafe` code needs to rely on fields being structurally pinned. The macro has a special "inline" mode for the case where the type depends on generic parameters from the surrounding scope. Signed-off-by: Benno Lossin Co-developed-by: Alice Ryhl Signed-off-by: Alice Ryhl --- rust/kernel/init.rs | 67 ++++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/init/__internal.rs | 29 ++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 495c09ebe3a3..1263f486abc4 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -742,6 +742,73 @@ macro_rules! try_init { }; } =20 +/// Asserts that a field on a struct using `#[pin_data]` is marked with `#= [pin]` ie. that it is +/// structurally pinned. +/// +/// # Example +/// +/// This will succeed: +/// ``` +/// use kernel::assert_pinned; +/// #[pin_data] +/// struct MyStruct { +/// #[pin] +/// some_field: u64, +/// } +/// +/// assert_pinned!(MyStruct, some_field, u64); +/// ``` +/// +/// This will fail: +/// ```compile_fail +/// use kernel::assert_pinned; +/// #[pin_data] +/// struct MyStruct { +/// some_field: u64, +/// } +/// +/// assert_pinned!(MyStruct, some_field, u64); +/// ``` +/// +/// Some uses of the macro may trigger the `can't use generic parameters f= rom outer item` error. To +/// work around this, you may pass the `inline` parameter to the macro. Th= e `inline` parameter can +/// only be used when the macro is invoked from a function body. +/// ``` +/// use kernel::assert_pinned; +/// #[pin_data] +/// struct Foo { +/// #[pin] +/// elem: T, +/// } +/// +/// impl Foo { +/// pub fn project(self: Pin<&mut Self>) -> Pin<&mut T> { +/// assert_pinned!(Foo, elem, T, inline); +/// +/// // SAFETY: The field is structurally pinned. +/// unsafe { self.map_unchecked_mut(|me| &mut me.elem) } +/// } +/// } +/// ``` +#[macro_export] +macro_rules! assert_pinned { + ($ty:ty, $field:ident, $field_ty:ty, inline) =3D> { + let _ =3D move |ptr: *mut $field_ty| { + // SAFETY: This code is unreachable. + let data =3D unsafe { <$ty as $crate::init::__internal::HasPin= Data>::__pin_data() }; + let init =3D $crate::init::__internal::AlwaysFail::<$field_ty>= ::new(); + // SAFETY: This code is unreachable. + unsafe { data.$field(ptr, init) }.ok(); + }; + }; + + ($ty:ty, $field:ident, $field_ty:ty) =3D> { + const _: () =3D { + $crate::assert_pinned!($ty, $field, $field_ty, inline); + }; + }; +} + /// A pin-initializer for the type `T`. /// /// To use this initializer, you will need a suitable memory location that= can hold a `T`. This can diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index db3372619ecd..13cefd37512f 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -228,3 +228,32 @@ pub unsafe fn new() -> Self { Self(()) } } + +/// Initializer that always fails. +/// +/// Used by [`assert_pinned!`]. +/// +/// [`assert_pinned!`]: crate::assert_pinned +pub struct AlwaysFail { + _t: PhantomData, +} + +impl AlwaysFail { + /// Creates a new initializer that always fails. + pub fn new() -> Self { + Self { _t: PhantomData } + } +} + +impl Default for AlwaysFail { + fn default() -> Self { + Self::new() + } +} + +// SAFETY: `__pinned_init` always fails, which is always okay. +unsafe impl PinInit for AlwaysFail { + unsafe fn __pinned_init(self, _slot: *mut T) -> Result<(), ()> { + Err(()) + } +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (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 79D2B13F42A for ; Wed, 14 Aug 2024 08:06:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622767; cv=none; b=gtqRozsp3QRd0vZicmlGwvYxqzqJ17Ses8+cbFiLwX3Qf0EjdEp9rV9tUQ1k7+8YAxKXKU0WhGcaIXkFlHQxjpIBCG92Th2FWnUWK/bKk9KW99weX6nQ4JDXlt52tVomZqF0SjDZPdC08B8pcRkHBi38rXLXabXpOz56UHm4nAc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622767; c=relaxed/simple; bh=RTOUzFCL9e4o7YmaN6V8KTg9ssYBxofhtcN5frBXE40=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PQAT+utT86ob/pIGodor9hXlsh7VtPpQcuEg1qmBce0QCphI59rzViooAOA1+dVegR5qf/1rmhVS5zqxaGdc3pbxCKgAlUlNPlr1+dr6Xww2+Do94237Etc30Es33vMAY0opVC1Rdxmyh623Tryekg8gIYIlikBYcaQ7lZqF47Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=A+aHUVG/; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="A+aHUVG/" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4281310bf7aso45011175e9.1 for ; Wed, 14 Aug 2024 01:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622762; x=1724227562; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gbfgAGvA710CFpAKP9RmDT9+gw+olEYHRYVTDCVqe6I=; b=A+aHUVG/NTruAvgauVmN6ZHGe28YLAmbza9tXu/3NxG3RF9JqTk+y4dpEuCTpdO1DF FuiK001vmfZczZXKwkc/RLr+c2LQZHGGkITGudK5e6tKeENtFD63pzL05kqnF2gkA1ux 65vtdO/zZkr/ejn2ImGhskksUcVa1IJZXK3Ie7zoxprYQSA9lZLcPUgzs4JUzjKz73HJ B4/ibXRXQ+4rqDxRqTog8D/c0H4oV8cBh0a7cGndMWZxGXDrNuMdHGrY2cWfWjY01ugg aFpN5d/xLHt4MH/TKL7m4OGyYaJzOo6WZp6F3ui3uemcuYXKpr/5bcy2/7jweJ8OP6FI HceQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622762; x=1724227562; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gbfgAGvA710CFpAKP9RmDT9+gw+olEYHRYVTDCVqe6I=; b=Zvj9xCvihigFQ7oahFn8EskrQ1f3roQFD99eIK9wwTWDMmKcVppCiiah35RcxT4VQP 2PMWu8HQl1WjjI++5a0OOk/9Nxfe6fBJtDu24u+c1y+dq+lXesVnTfJ8+StpIP6gcg+F GiZUyWMlKIiUJ1pBRg/TiwpYGdrJ7fBqrJA3o9HjW4I8nqVavjLylVNYYTy5dG4PMGCn PN3f/i6FA2Lpv3Tv7QPgBRZ+8Rt2+pIRXh5yBwr8pwwgiqXs6Y2kAKXQpFQXc06Fz76v GbYT0KD4N3VrLGeYDnYF8z5VPiqJ51PvjsSPmouTRTO5Qfcwy8i7/sJy112ID4l8Y1aB nEvQ== X-Forwarded-Encrypted: i=1; AJvYcCX+Wpv9b4o5jXzbKkTzjHXS8nla8mdZMS0TQzDZR/4YUy45UZ07VuBGWxKVmezPcsiqY5nacT3HxPNdE14=@vger.kernel.org X-Gm-Message-State: AOJu0YzrPDrOO5yda1kKtutMv8+DzfEFgnsqj9HCRBIY566sdlvrovAQ 6HbEjfS7e2fQjqBcZBnq0JuM5xNkY1907ww2ohz3ARfQyJctVfyZ81FyQsNCWwqDwYpESy1mlOG OTJoOtu2etKZNkg== X-Google-Smtp-Source: AGHT+IEWTShI9VkgfCcEDsu2Pz2mWkwmLZMKuvNQVXj4pKsWsDr8W5HESC9nvZ8zxU0dZhgFQr4+JSQpwh9EuyM= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:714:b0:428:1718:957f with SMTP id 5b1f17b1804b1-429dd26a90fmr40035e9.5.1723622761709; Wed, 14 Aug 2024 01:06:01 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:21 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=17972; i=aliceryhl@google.com; h=from:subject:message-id; bh=RTOUzFCL9e4o7YmaN6V8KTg9ssYBxofhtcN5frBXE40=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVcuhQvrarQowP8V9X4cScychH87LppiBlhR x4W1YQvTN2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXAAKCRAEWL7uWMY5 RvO/EACsRWz1zTXUUktNOdo4A4mOnOYoqGH8vGKpPIDzkRz2b3Nn8Kw5AlRy4KPvoHkmCk2sgJb PRBqphz08feeSBVznG7wMY8ueZ8z/+Z0L0LLWUxykOI/UZ7PwY45Ug2xQBs9emxFPhLPrHUdmXK T3I+qJl651DyytlVjoCyTSiEVdZkopz3qjuOMkRUBLArfDujQSt1o58OUKYFuVD4pHtZvi8e1AY WJOZB6TIu0sRaVBKClKzNx3iRqlqg+t9w2/uI5jURATNuWKLtepmMhIAakWHWGWXZNYYg96GwUJ 6L6u2EyuV9fa0TWaHBNXQPNfEZJNp6U3nsetJ5Jwcsy/A9+jvXXqOhGCjAwgvt9YWAHFjr9O15i EWKRw9gpzEA8Oj6XwsbGqAGX1D+8ffWSQ6IngE+M9VScF59q0ASzL9zWkB+X8Qcmud0aFltDZZb I+J7BQhRJxbfv4Xs8yEUZIz5KNnBg4fLcNsl8ea2Eq2/fTCJH70T5RbTJCX3WSyGYzaa3Jjs2YE 3Guo2Q5PLOOrmmiRAFESaF8x9aJ7G1nnfXIR2FnfhRg23heHQh6IhZYZYw8IZw7CGqqS0KWY62A sXmpiuaKDSIqkgsVYzxI32iF8esz3PzPrk6DVAAUglhLo+xXTCNRrRd8G5/o3CmiPYHAKLNra7d ag8uJR4EUe6b8qg== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-2-f5f5e8075da0@google.com> Subject: [PATCH v5 02/10] rust: list: add ListArc From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The `ListArc` type can be thought of as a special reference to a refcounted object that owns the permission to manipulate the `next`/`prev` pointers stored in the refcounted object. By ensuring that each object has only one `ListArc` reference, the owner of that reference is assured exclusive access to the `next`/`prev` pointers. When a `ListArc` is inserted into a `List`, the `List` takes ownership of the `ListArc` reference. There are various strategies for ensuring that a value has only one `ListArc` reference. The simplest is to convert a `UniqueArc` into a `ListArc`. However, the refcounted object could also keep track of whether a `ListArc` exists using a boolean, which could allow for the creation of new `ListArc` references from an `Arc` reference. Whatever strategy is used, the relevant tracking is referred to as "the tracking inside `T`", and the `ListArcSafe` trait (and its subtraits) are used to update the tracking when a `ListArc` is created or destroyed. Note that we allow the case where the tracking inside `T` thinks that a `ListArc` exists, but actually, there isn't a `ListArc`. However, we do not allow the opposite situation where a `ListArc` exists, but the tracking thinks it doesn't. This is because the former can at most result in us failing to create a `ListArc` when the operation could succeed, whereas the latter can result in the creation of two `ListArc` references. Only the latter situation can lead to memory safety issues. This patch introduces the `impl_list_arc_safe!` macro that allows you to implement `ListArcSafe` for types using the strategy where a `ListArc` can only be created from a `UniqueArc`. Other strategies are introduced in later patches. This is part of the linked list that Rust Binder will use for many different things. The strategy where a `ListArc` can only be created from a `UniqueArc` is actually sufficient for most of the objects that Rust Binder needs to insert into linked lists. Usually, these are todo items that are created and then immediately inserted into a queue. The const generic ID allows objects to have several prev/next pointer pairs so that the same object can be inserted into several different lists. You are able to have several `ListArc` references as long as they correspond to different pointer pairs. The ID itself is purely a compile-time concept and will not be present in the final binary. Both the `List` and the `ListArc` will need to agree on the ID for them to work together. Rust Binder uses this in a few places (e.g. death recipients) where the same object can be inserted into both generic todo lists and some other lists for tracking the status of the object. The ID is a const generic rather than a type parameter because the `pair_from_unique` method needs to be able to assert that the two ids are different. There's no easy way to assert that when using types instead of integers. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/lib.rs | 1 + rust/kernel/list.rs | 8 ++ rust/kernel/list/arc.rs | 352 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 361 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 274bdc1b0a82..9baea9e9ee1a 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -38,6 +38,7 @@ pub mod ioctl; #[cfg(CONFIG_KUNIT)] pub mod kunit; +pub mod list; #[cfg(CONFIG_NET)] pub mod net; pub mod page; diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs new file mode 100644 index 000000000000..fb16ea43b2ba --- /dev/null +++ b/rust/kernel/list.rs @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! A linked list implementation. + +mod arc; +pub use self::arc::{impl_list_arc_safe, ListArc, ListArcSafe}; diff --git a/rust/kernel/list/arc.rs b/rust/kernel/list/arc.rs new file mode 100644 index 000000000000..966076da4a75 --- /dev/null +++ b/rust/kernel/list/arc.rs @@ -0,0 +1,352 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! A wrapper around `Arc` for linked lists. + +use crate::alloc::{AllocError, Flags}; +use crate::prelude::*; +use crate::sync::{Arc, ArcBorrow, UniqueArc}; +use core::marker::Unsize; +use core::ops::Deref; +use core::pin::Pin; + +/// Declares that this type has some way to ensure that there is exactly o= ne `ListArc` instance for +/// this id. +/// +/// Types that implement this trait should include some kind of logic for = keeping track of whether +/// a [`ListArc`] exists or not. We refer to this logic as "the tracking i= nside `T`". +/// +/// We allow the case where the tracking inside `T` thinks that a [`ListAr= c`] exists, but actually, +/// there isn't a [`ListArc`]. However, we do not allow the opposite situa= tion where a [`ListArc`] +/// exists, but the tracking thinks it doesn't. This is because the former= can at most result in us +/// failing to create a [`ListArc`] when the operation could succeed, wher= eas the latter can result +/// in the creation of two [`ListArc`] references. Only the latter situati= on can lead to memory +/// safety issues. +/// +/// A consequence of the above is that you may implement the tracking insi= de `T` by not actually +/// keeping track of anything. To do this, you always claim that a [`ListA= rc`] exists, even if +/// there isn't one. This implementation is allowed by the above rule, but= it means that +/// [`ListArc`] references can only be created if you have ownership of *a= ll* references to the +/// refcounted object, as you otherwise have no way of knowing whether a [= `ListArc`] exists. +pub trait ListArcSafe { + /// Informs the tracking inside this type that it now has a [`ListArc`= ] reference. + /// + /// This method may be called even if the tracking inside this type th= inks that a `ListArc` + /// reference exists. (But only if that's not actually the case.) + /// + /// # Safety + /// + /// Must not be called if a [`ListArc`] already exist for this value. + unsafe fn on_create_list_arc_from_unique(self: Pin<&mut Self>); + + /// Informs the tracking inside this type that there is no [`ListArc`]= reference anymore. + /// + /// # Safety + /// + /// Must only be called if there is no [`ListArc`] reference, but the = tracking thinks there is. + unsafe fn on_drop_list_arc(&self); +} + +/// Declares that this type supports [`ListArc`]. +/// +/// When using this macro, it will only be possible to create a [`ListArc`= ] from a [`UniqueArc`]. +#[macro_export] +macro_rules! impl_list_arc_safe { + (impl$({$($generics:tt)*})? ListArcSafe<$num:tt> for $t:ty { untracked= ; } $($rest:tt)*) =3D> { + impl$(<$($generics)*>)? $crate::list::ListArcSafe<$num> for $t { + unsafe fn on_create_list_arc_from_unique(self: ::core::pin::Pi= n<&mut Self>) {} + unsafe fn on_drop_list_arc(&self) {} + } + $crate::list::impl_list_arc_safe! { $($rest)* } + }; + + () =3D> {}; +} +pub use impl_list_arc_safe; + +/// A wrapper around [`Arc`] that's guaranteed unique for the given id. +/// +/// The `ListArc` type can be thought of as a special reference to a refco= unted object that owns the +/// permission to manipulate the `next`/`prev` pointers stored in the refc= ounted object. By ensuring +/// that each object has only one `ListArc` reference, the owner of that r= eference is assured +/// exclusive access to the `next`/`prev` pointers. When a `ListArc` is in= serted into a `List`, the +/// `List` takes ownership of the `ListArc` reference. +/// +/// There are various strategies to ensuring that a value has only one `Li= stArc` reference. The +/// simplest is to convert a [`UniqueArc`] into a `ListArc`. However, the = refcounted object could +/// also keep track of whether a `ListArc` exists using a boolean, which c= ould allow for the +/// creation of new `ListArc` references from an [`Arc`] reference. Whatev= er strategy is used, the +/// relevant tracking is referred to as "the tracking inside `T`", and the= [`ListArcSafe`] trait +/// (and its subtraits) are used to update the tracking when a `ListArc` i= s created or destroyed. +/// +/// Note that we allow the case where the tracking inside `T` thinks that = a `ListArc` exists, but +/// actually, there isn't a `ListArc`. However, we do not allow the opposi= te situation where a +/// `ListArc` exists, but the tracking thinks it doesn't. This is because = the former can at most +/// result in us failing to create a `ListArc` when the operation could su= cceed, whereas the latter +/// can result in the creation of two `ListArc` references. +/// +/// While this `ListArc` is unique for the given id, there still might exi= st normal `Arc` +/// references to the object. +/// +/// # Invariants +/// +/// * Each reference counted object has at most one `ListArc` for each val= ue of `ID`. +/// * The tracking inside `T` is aware that a `ListArc` reference exists. +#[repr(transparent)] +pub struct ListArc +where + T: ListArcSafe + ?Sized, +{ + arc: Arc, +} + +impl, const ID: u64> ListArc { + /// Constructs a new reference counted instance of `T`. + #[inline] + pub fn new(contents: T, flags: Flags) -> Result { + Ok(Self::from(UniqueArc::new(contents, flags)?)) + } + + /// Use the given initializer to in-place initialize a `T`. + /// + /// If `T: !Unpin` it will not be able to move afterwards. + // We don't implement `InPlaceInit` because `ListArc` is implicitly pi= nned. This is similar to + // what we do for `Arc`. + #[inline] + pub fn pin_init(init: impl PinInit, flags: Flags) -> Result + where + E: From, + { + Ok(Self::from(UniqueArc::try_pin_init(init, flags)?)) + } + + /// Use the given initializer to in-place initialize a `T`. + /// + /// This is equivalent to [`ListArc::pin_init`], since a [`ListArc`= ] is always pinned. + #[inline] + pub fn init(init: impl Init, flags: Flags) -> Result + where + E: From, + { + Ok(Self::from(UniqueArc::try_init(init, flags)?)) + } +} + +impl From> for ListArc +where + T: ListArcSafe + ?Sized, +{ + /// Convert a [`UniqueArc`] into a [`ListArc`]. + #[inline] + fn from(unique: UniqueArc) -> Self { + Self::from(Pin::from(unique)) + } +} + +impl From>> for ListArc +where + T: ListArcSafe + ?Sized, +{ + /// Convert a pinned [`UniqueArc`] into a [`ListArc`]. + #[inline] + fn from(mut unique: Pin>) -> Self { + // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. + unsafe { T::on_create_list_arc_from_unique(unique.as_mut()) }; + let arc =3D Arc::from(unique); + // SAFETY: We just called `on_create_list_arc_from_unique` on an a= rc without a `ListArc`, + // so we can create a `ListArc`. + unsafe { Self::transmute_from_arc(arc) } + } +} + +impl ListArc +where + T: ListArcSafe + ?Sized, +{ + /// Creates two `ListArc`s from a [`UniqueArc`]. + /// + /// The two ids must be different. + #[inline] + pub fn pair_from_unique(unique: UniqueArc) -> (Self= , ListArc) + where + T: ListArcSafe, + { + Self::pair_from_pin_unique(Pin::from(unique)) + } + + /// Creates two `ListArc`s from a pinned [`UniqueArc`]. + /// + /// The two ids must be different. + #[inline] + pub fn pair_from_pin_unique( + mut unique: Pin>, + ) -> (Self, ListArc) + where + T: ListArcSafe, + { + build_assert!(ID !=3D ID2); + + // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. + unsafe { >::on_create_list_arc_from_unique(un= ique.as_mut()) }; + // SAFETY: We have a `UniqueArc`, so there is no `ListArc`. + unsafe { >::on_create_list_arc_from_unique(u= nique.as_mut()) }; + + let arc1 =3D Arc::from(unique); + let arc2 =3D Arc::clone(&arc1); + + // SAFETY: We just called `on_create_list_arc_from_unique` on an a= rc without a `ListArc` + // for both IDs (which are different), so we can create two `ListA= rc`s. + unsafe { + ( + Self::transmute_from_arc(arc1), + ListArc::transmute_from_arc(arc2), + ) + } + } + + /// Transmutes an [`Arc`] into a `ListArc` without updating the tracki= ng inside `T`. + /// + /// # Safety + /// + /// * The value must not already have a `ListArc` reference. + /// * The tracking inside `T` must think that there is a `ListArc` ref= erence. + #[inline] + unsafe fn transmute_from_arc(arc: Arc) -> Self { + // INVARIANT: By the safety requirements, the invariants on `ListA= rc` are satisfied. + Self { arc } + } + + /// Transmutes a `ListArc` into an [`Arc`] without updating the tracki= ng inside `T`. + /// + /// After this call, the tracking inside `T` will still think that the= re is a `ListArc` + /// reference. + #[inline] + fn transmute_to_arc(self) -> Arc { + // Use a transmute to skip destructor. + // + // SAFETY: ListArc is repr(transparent). + unsafe { core::mem::transmute(self) } + } + + /// Convert ownership of this `ListArc` into a raw pointer. + /// + /// The returned pointer is indistinguishable from pointers returned b= y [`Arc::into_raw`]. The + /// tracking inside `T` will still think that a `ListArc` exists after= this call. + #[inline] + pub fn into_raw(self) -> *const T { + Arc::into_raw(Self::transmute_to_arc(self)) + } + + /// Take ownership of the `ListArc` from a raw pointer. + /// + /// # Safety + /// + /// * `ptr` must satisfy the safety requirements of [`Arc::from_raw`]. + /// * The value must not already have a `ListArc` reference. + /// * The tracking inside `T` must think that there is a `ListArc` ref= erence. + #[inline] + pub unsafe fn from_raw(ptr: *const T) -> Self { + // SAFETY: The pointer satisfies the safety requirements for `Arc:= :from_raw`. + let arc =3D unsafe { Arc::from_raw(ptr) }; + // SAFETY: The value doesn't already have a `ListArc` reference, b= ut the tracking thinks it + // does. + unsafe { Self::transmute_from_arc(arc) } + } + + /// Converts the `ListArc` into an [`Arc`]. + #[inline] + pub fn into_arc(self) -> Arc { + let arc =3D Self::transmute_to_arc(self); + // SAFETY: There is no longer a `ListArc`, but the tracking thinks= there is. + unsafe { T::on_drop_list_arc(&arc) }; + arc + } + + /// Clone a `ListArc` into an [`Arc`]. + #[inline] + pub fn clone_arc(&self) -> Arc { + self.arc.clone() + } + + /// Returns a reference to an [`Arc`] from the given [`ListArc`]. + /// + /// This is useful when the argument of a function call is an [`&Arc`]= (e.g., in a method + /// receiver), but we have a [`ListArc`] instead. + /// + /// [`&Arc`]: Arc + #[inline] + pub fn as_arc(&self) -> &Arc { + &self.arc + } + + /// Returns an [`ArcBorrow`] from the given [`ListArc`]. + /// + /// This is useful when the argument of a function call is an [`ArcBor= row`] (e.g., in a method + /// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`= ] is free when optimised. + #[inline] + pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> { + self.arc.as_arc_borrow() + } + + /// Compare whether two [`ListArc`] pointers reference the same underl= ying object. + #[inline] + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + Arc::ptr_eq(&this.arc, &other.arc) + } +} + +impl Deref for ListArc +where + T: ListArcSafe + ?Sized, +{ + type Target =3D T; + + #[inline] + fn deref(&self) -> &Self::Target { + self.arc.deref() + } +} + +impl Drop for ListArc +where + T: ListArcSafe + ?Sized, +{ + #[inline] + fn drop(&mut self) { + // SAFETY: There is no longer a `ListArc`, but the tracking thinks= there is by the type + // invariants on `Self`. + unsafe { T::on_drop_list_arc(&self.arc) }; + } +} + +impl AsRef> for ListArc +where + T: ListArcSafe + ?Sized, +{ + #[inline] + fn as_ref(&self) -> &Arc { + self.as_arc() + } +} + +// This is to allow [`ListArc`] (and variants) to be used as the type of `= self`. +impl core::ops::Receiver for ListArc where T: Lis= tArcSafe + ?Sized {} + +// This is to allow coercion from `ListArc` to `ListArc` if `T` can = be converted to the +// dynamically-sized type (DST) `U`. +impl core::ops::CoerceUnsized> for Lis= tArc +where + T: ListArcSafe + Unsize + ?Sized, + U: ListArcSafe + ?Sized, +{ +} + +// This is to allow `ListArc` to be dispatched on when `ListArc` can= be coerced into +// `ListArc`. +impl core::ops::DispatchFromDyn> for L= istArc +where + T: ListArcSafe + Unsize + ?Sized, + U: ListArcSafe + ?Sized, +{ +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 EA4581422AB for ; Wed, 14 Aug 2024 08:06:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622767; cv=none; b=puHHUp1kPm+loDcPwBKrnwMhZSAj6YEetUIL8Eb73R/JGwWh/9n+MvIboYdHM32i7WCjdwlfn/AP+Q5Q4WZgw6QKckW3ZSfPz3gBK6uu3oZYq/iHWKVZKqfVvclL86osUrYDw8C6Fzuot3qWEAbKD/Bw+QCtDRrctOuxv3k42XY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622767; c=relaxed/simple; bh=xnc/YeHBMoQaOvOzlunHyZEPrhxk1PkpkgFQgdA3Xak=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=olUXgv+zr/0DQCJSOmpcCDx2xbbQvAGM8+dmg72ZPnH/FDQQaUrMa499jjliL64CVby7vHoRHGASbUmwxNcj2NykK/2y41nQkIZajn5TKVg3NY74/linxVU5i1+9m1wx/pZl2Tov4kzyeCTPVD/xbCn/n4VEuo6hUKRueUVEgks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V2DlGWkq; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V2DlGWkq" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-368448dfe12so320645f8f.1 for ; Wed, 14 Aug 2024 01:06:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622764; x=1724227564; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yi9Z6wpf5ind79uw6LrrGYLWeY0/HZjpQZjY71BcRmQ=; b=V2DlGWkqGhVW5NUfgw5t9d+Ge/iS9ilbIZvkr5XBiG7rdq3YIcfEJ/WJxZ4EwfFyrN PPcNgYkcS2stRkqt/j/edqQdVp5CspNguTUOHnfqWdkAUpSXx7H02xIKVYjBwynLgYDz dcFsepe+mvMGfPfCroRjiqENe9LvnCaltkoeh4ltqHcrd1g718z97Cc8bMkZpJhppd1N i2F4DX7zH0EVl0f5xEDMHgzjSAf3D2E5OdKnAhP4RPLJ+ge66igHO6VQ042EtnIMfemC 2eHLYN/jMaQBpCvKmnmsBKtJt3xBvM0jFA4W4LitTSEf5dnq4RnO81TVpSLq1u3NSZFz AMSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622764; x=1724227564; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yi9Z6wpf5ind79uw6LrrGYLWeY0/HZjpQZjY71BcRmQ=; b=ei2Qju/VEdRtpkFdNcTGB7l9etY/zAfWmO6zyFxMoQswluQXRdV2okhqffQKJJb3v+ SNRsVViI2crGBURy/5hGU457mxR9dodBmfFHMooD3pl3Koz1T510GfF9+BSmCQi5vCjz MH0IcdsyE81w9eDaXQreQ5O/iy8dc/7+9kKvQXlPW32shP8ACMbdpQOrUgQHGr/NNajL dPBlRP21LjhtI/f8Xd5gyzbLP+omQElSielHOXi1arxORqWYQNlElEGk9DB6SPM898jh YnpRnE3KGI4IaU9raHtz/oX9mD4lB1P+MADavZjPEyZg14H1p/Nd4gRAfmifBf2sUfWT jSOA== X-Forwarded-Encrypted: i=1; AJvYcCV4l0YRT7j2pbUacGr4GsjIltC3K6dF9kNc8Pi2u3Nzc8JOecizzTFK6I5UPobyEyoVRbaeQ3l5Ja/8y6rqmWGx4xZqDppfCn71fcrs X-Gm-Message-State: AOJu0YwOA49ZWFs4xT3TEBEqTe5mipWLG0zoh0P5M+9J3el6+3EfbeFY qYpdF5tKSJa17My61VH7GKRlmkh/wGKgBU3FU5sz3jrY2ap6cfkvQX9b97EwM8LF1tXXYXFIuay xyv+ZNMEm59w7Gw== X-Google-Smtp-Source: AGHT+IHg8hpTvx6+/E6ZhSBhsl3XwfPesWpPswCzOXnR31xIWDBQuWw4nDBknyWEM/jxePL4xjTv4zXk9/uCJpg= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5d:5889:0:b0:364:75ab:b06b with SMTP id ffacd0b85a97d-37177585517mr9993f8f.3.1723622764111; Wed, 14 Aug 2024 01:06:04 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:22 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=11298; i=aliceryhl@google.com; h=from:subject:message-id; bh=xnc/YeHBMoQaOvOzlunHyZEPrhxk1PkpkgFQgdA3Xak=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVdKp3WgXP+fHa2JhwCKV+E3D8kgibsK6/co piN3hNG4bOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXQAKCRAEWL7uWMY5 RlhPD/9A+iTf+Od3oxGeulQdMZzfo8L9cbdHiRrW83gXYjIzY/PI7hK1bh/QC9cleg8fCeShwyv U/vGp+kdPGRNNq3m5fWzWcsXGK60NK4ptbxocS7f0H8DNqDs4/BOhArY5HR0QycGPc5DA0yCsSz /ATuWoBW+AWYFjh1BNCWklBWFWczdRIG4M6Bsccx2/Gksqo3LSRTOZ5J1S17GN6GIgfdHnEjEza SdmHzwDDqMa87XRsnKwZQ53Fbale+m7JaYvm9ip4hsdVO+FuR3/lwjxyNhqqIyR071fzfn5bmUF 5dZS19Z/7ha81QDuxuj6/7hCr7Xx92MYeVhdVi5BzcRyJhzlqS00BMjJEjF24R573gJLUu+fk9J gb5fdIx32iNkg4mpbyAxcq21nNChU3VPD4MjCiqm7jwdQwLsIhoCQcda/AgDpHvaTTwQJrkcH0G iRyUe2OhpTAxTQoqLkUCuhCKqDa94Ac40nLrtSyEF6qzV5WLpKna7fRyIm1PI69uBgbKy6/lmvk gUI5z6oOyhKc5yccXeLs37g2qZa1UgH941vjEILX0Yeb4bhO/SVUGzVAHyXY/lSi1cZmnIcZYFY oczer3iFht74SgQh20pM21nNIQRACD5WaqB23S+Frjzbeg7LhMh75HufVSEos6MiAc2jNC45Nl+ Jr+HXljoeNCTF8A== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-3-f5f5e8075da0@google.com> Subject: [PATCH v5 03/10] rust: list: add tracking for ListArc From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add the ability to track whether a ListArc exists for a given value, allowing for the creation of ListArcs without going through UniqueArc. The `impl_list_arc_safe!` macro is extended with a `tracked_by` strategy that defers the tracking of ListArcs to a field of the struct. Additionally, the AtomicListArcTracker type is introduced, which can track whether a ListArc exists using an atomic. By deferring the tracking to a field of type AtomicListArcTracker, structs gain the ability to create ListArcs without going through a UniqueArc. Rust Binder uses this for some objects where we want to be able to insert them into a linked list at any time. Using the AtomicListArcTracker, we are able to check whether an item is already in the list, and if not, we can create a `ListArc` and push it. The macro has the ability to defer the tracking of ListArcs to a field, using whatever strategy that field has. Since we don't add any strategies other than AtomicListArcTracker, another similar option would be to hard-code that the field should be an AtomicListArcTracker. However, Rust Binder has a case where the AtomicListArcTracker is not stored directly in the struct, but in a sub-struct. Furthermore, the outer struct is generic: struct Wrapper { links: ListLinks, inner: T, } Here, the Wrapper struct implements ListArcSafe with `tracked_by inner`, and then the various types used with `inner` also uses the macro to implement ListArcSafe. Some of them use the untracked strategy, and some of them use tracked_by with an AtomicListArcTracker. This way, Wrapper just inherits whichever choice `inner` has made. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 2 +- rust/kernel/list/arc.rs | 171 ++++++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 170 insertions(+), 3 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index fb16ea43b2ba..8e1533ee987b 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -5,4 +5,4 @@ //! A linked list implementation. =20 mod arc; -pub use self::arc::{impl_list_arc_safe, ListArc, ListArcSafe}; +pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; diff --git a/rust/kernel/list/arc.rs b/rust/kernel/list/arc.rs index 966076da4a75..c5921a7d5966 100644 --- a/rust/kernel/list/arc.rs +++ b/rust/kernel/list/arc.rs @@ -7,9 +7,10 @@ use crate::alloc::{AllocError, Flags}; use crate::prelude::*; use crate::sync::{Arc, ArcBorrow, UniqueArc}; -use core::marker::Unsize; +use core::marker::{PhantomPinned, Unsize}; use core::ops::Deref; use core::pin::Pin; +use core::sync::atomic::{AtomicBool, Ordering}; =20 /// Declares that this type has some way to ensure that there is exactly o= ne `ListArc` instance for /// this id. @@ -48,9 +49,38 @@ pub trait ListArcSafe { unsafe fn on_drop_list_arc(&self); } =20 +/// Declares that this type is able to safely attempt to create `ListArc`s= at any time. +/// +/// # Safety +/// +/// The guarantees of `try_new_list_arc` must be upheld. +pub unsafe trait TryNewListArc: ListArcSafe { + /// Attempts to convert an `Arc` into an `ListArc`. Return= s `true` if the + /// conversion was successful. + /// + /// This method should not be called directly. Use [`ListArc::try_from= _arc`] instead. + /// + /// # Guarantees + /// + /// If this call returns `true`, then there is no [`ListArc`] pointing= to this value. + /// Additionally, this call will have transitioned the tracking inside= `Self` from not thinking + /// that a [`ListArc`] exists, to thinking that a [`ListArc`] exists. + fn try_new_list_arc(&self) -> bool; +} + /// Declares that this type supports [`ListArc`]. /// -/// When using this macro, it will only be possible to create a [`ListArc`= ] from a [`UniqueArc`]. +/// This macro supports a few different strategies for implementing the tr= acking inside the type: +/// +/// * The `untracked` strategy does not actually keep track of whether a [= `ListArc`] exists. When +/// using this strategy, the only way to create a [`ListArc`] is using a= [`UniqueArc`]. +/// * The `tracked_by` strategy defers the tracking to a field of the stru= ct. The user much specify +/// which field to defer the tracking to. The field must implement [`Lis= tArcSafe`]. If the field +/// implements [`TryNewListArc`], then the type will also implement [`Tr= yNewListArc`]. +/// +/// The `tracked_by` strategy is usually used by deferring to a field of t= ype +/// [`AtomicTracker`]. However, it is also possible to defer the tracking = to another struct +/// using also using this macro. #[macro_export] macro_rules! impl_list_arc_safe { (impl$({$($generics:tt)*})? ListArcSafe<$num:tt> for $t:ty { untracked= ; } $($rest:tt)*) =3D> { @@ -61,6 +91,39 @@ unsafe fn on_drop_list_arc(&self) {} $crate::list::impl_list_arc_safe! { $($rest)* } }; =20 + (impl$({$($generics:tt)*})? ListArcSafe<$num:tt> for $t:ty { + tracked_by $field:ident : $fty:ty; + } $($rest:tt)*) =3D> { + impl$(<$($generics)*>)? $crate::list::ListArcSafe<$num> for $t { + unsafe fn on_create_list_arc_from_unique(self: ::core::pin::Pi= n<&mut Self>) { + $crate::assert_pinned!($t, $field, $fty, inline); + + // SAFETY: This field is structurally pinned as per the ab= ove assertion. + let field =3D unsafe { + ::core::pin::Pin::map_unchecked_mut(self, |me| &mut me= .$field) + }; + // SAFETY: The caller promises that there is no `ListArc`. + unsafe { + <$fty as $crate::list::ListArcSafe<$num>>::on_create_l= ist_arc_from_unique(field) + }; + } + unsafe fn on_drop_list_arc(&self) { + // SAFETY: The caller promises that there is no `ListArc` = reference, and also + // promises that the tracking thinks there is a `ListArc` = reference. + unsafe { <$fty as $crate::list::ListArcSafe<$num>>::on_dro= p_list_arc(&self.$field) }; + } + } + unsafe impl$(<$($generics)*>)? $crate::list::TryNewListArc<$num> f= or $t + where + $fty: TryNewListArc<$num>, + { + fn try_new_list_arc(&self) -> bool { + <$fty as $crate::list::TryNewListArc<$num>>::try_new_list_= arc(&self.$field) + } + } + $crate::list::impl_list_arc_safe! { $($rest)* } + }; + () =3D> {}; } pub use impl_list_arc_safe; @@ -205,6 +268,52 @@ pub fn pair_from_pin_unique( } } =20 + /// Try to create a new `ListArc`. + /// + /// This fails if this value already has a `ListArc`. + pub fn try_from_arc(arc: Arc) -> Result> + where + T: TryNewListArc, + { + if arc.try_new_list_arc() { + // SAFETY: The `try_new_list_arc` method returned true, so we = made the tracking think + // that a `ListArc` exists. This lets us create a `ListArc`. + Ok(unsafe { Self::transmute_from_arc(arc) }) + } else { + Err(arc) + } + } + + /// Try to create a new `ListArc`. + /// + /// This fails if this value already has a `ListArc`. + pub fn try_from_arc_borrow(arc: ArcBorrow<'_, T>) -> Option + where + T: TryNewListArc, + { + if arc.try_new_list_arc() { + // SAFETY: The `try_new_list_arc` method returned true, so we = made the tracking think + // that a `ListArc` exists. This lets us create a `ListArc`. + Some(unsafe { Self::transmute_from_arc(Arc::from(arc)) }) + } else { + None + } + } + + /// Try to create a new `ListArc`. + /// + /// If it's not possible to create a new `ListArc`, then the `Arc` is = dropped. This will never + /// run the destructor of the value. + pub fn try_from_arc_or_drop(arc: Arc) -> Option + where + T: TryNewListArc, + { + match Self::try_from_arc(arc) { + Ok(list_arc) =3D> Some(list_arc), + Err(arc) =3D> Arc::into_unique_or_drop(arc).map(Self::from), + } + } + /// Transmutes an [`Arc`] into a `ListArc` without updating the tracki= ng inside `T`. /// /// # Safety @@ -350,3 +459,61 @@ impl core::ops::DispatchFromDyn> for ListArc U: ListArcSafe + ?Sized, { } + +/// A utility for tracking whether a [`ListArc`] exists using an atomic. +/// +/// # Invariant +/// +/// If the boolean is `false`, then there is no [`ListArc`] for this value. +#[repr(transparent)] +pub struct AtomicTracker { + inner: AtomicBool, + // This value needs to be pinned to justify the INVARIANT: comment in = `AtomicTracker::new`. + _pin: PhantomPinned, +} + +impl AtomicTracker { + /// Creates a new initializer for this type. + pub fn new() -> impl PinInit { + // INVARIANT: Pin-init initializers can't be used on an existing `= Arc`, so this value will + // not be constructed in an `Arc` that already has a `ListArc`. + Self { + inner: AtomicBool::new(false), + _pin: PhantomPinned, + } + } + + fn project_inner(self: Pin<&mut Self>) -> &mut AtomicBool { + // SAFETY: The `inner` field is not structurally pinned, so we may= obtain a mutable + // reference to it even if we only have a pinned reference to `sel= f`. + unsafe { &mut Pin::into_inner_unchecked(self).inner } + } +} + +impl ListArcSafe for AtomicTracker { + unsafe fn on_create_list_arc_from_unique(self: Pin<&mut Self>) { + // INVARIANT: We just created a ListArc, so the boolean should be = true. + *self.project_inner().get_mut() =3D true; + } + + unsafe fn on_drop_list_arc(&self) { + // INVARIANT: We just dropped a ListArc, so the boolean should be = false. + self.inner.store(false, Ordering::Release); + } +} + +// SAFETY: If this method returns `true`, then by the type invariant there= is no `ListArc` before +// this call, so it is okay to create a new `ListArc`. +// +// The acquire ordering will synchronize with the release store from the d= estruction of any +// previous `ListArc`, so if there was a previous `ListArc`, then the dest= ruction of the previous +// `ListArc` happens-before the creation of the new `ListArc`. +unsafe impl TryNewListArc for AtomicTracker { + fn try_new_list_arc(&self) -> bool { + // INVARIANT: If this method returns true, then the boolean used t= o be false, and is no + // longer false, so it is okay for the caller to create a new [`Li= stArc`]. + self.inner + .compare_exchange(false, true, Ordering::Acquire, Ordering::Re= laxed) + .is_ok() + } +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 CE9A613D51E for ; Wed, 14 Aug 2024 08:06:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622769; cv=none; b=mAQkCvuXFdgNIc5XakJC0mp4R7XGeWCL+XZpK0mM+m3Do3Le+fFs091oi4Dy0tnXFqF6pOgcQfg2oYWriqVKE3anmPe+O4+lGmGocM6hm2ZWwBqr85l4TtQw0/PheNPMLgDURhCUKvQYnaFrqhg4XvuSN543FuxYpjxU31X3wqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622769; c=relaxed/simple; bh=ZGRtM04u3g5Up6WlihqLvzD2O+qnfmGpQGoUaB/u7j0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H9wUJYJeowDJ5eKvbx2MLy5SCPaCL7jlPEWtjm89fuVaLB5KViM7oWNfWyCa/hFj4GVfHn7Jm+e6Q9eldJ6ERqZDP7G2eYM5WjkujLGYajQlSsowiocU8RMIlmFiCFpR9ZvXeEY5Rs4tQrTWNloRhdgqTlTfUApvZlI612obxuM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UqEeTCmi; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UqEeTCmi" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0e4cd64909so10899498276.3 for ; Wed, 14 Aug 2024 01:06:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622767; x=1724227567; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=d/RqOCLiFdemdAlZLK6KKO29T4r+56k9VFtBAkRLaRo=; b=UqEeTCmivf+wpaG6EkUiLUV8eXPh7WaunGryBXDE8QVbGRdurvsxiG7lny54EX0oQ8 KHWeXQ0UwXZx8upYcdyydhWbPvCtVhNwwGqvAG/RNroYrmeAxWtU/nAeWhF72hAylDzy sS2RkWopt+qOcMv7b0O7qfh2HLMaxeUHFr8YIduBqK0imDlXtH2izPuEbb1YpwWf4Kgz grKyM775yv9WWxcWtnowrnBA1UxCYvxsAymnqihO1KbqMJQPVgOHr4CgHJ/nsj6Pglzb FDE2G9hJWxz+JuIybAMZc6oCOwozM3yHWFrHqMXKJFaKDDcnP4JATO/MNxhE409Ro0np 9ibw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622767; x=1724227567; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=d/RqOCLiFdemdAlZLK6KKO29T4r+56k9VFtBAkRLaRo=; b=TVv6aym9OHhluPvJzwHV08TBDrCGrJLY1OnO90IeLNPmxxA/LvsnPFLiGIgiZAzQga 40ESqluNOt5Ko5/OD8sn/2ZooYWHb5mAcuIce/2Np8BXvqU+VG/YhAYH3fp3CTdKJe2D aao758O616RIfE2zj7KhQXJX2PGTC3mdE4NfXpD6G3Zxl+VrPrG2YimKkrJptJa2nW00 KbD2bT74lyFfxhyVFCd8o+zB0D+hOSlSthl+s20cp5NegDlrNyJ12QfnCIjTI3fAIhyG EqngwIgWNozsJeOPc0nued4sqwN+Y3ADFbb3GvL14u9SDSRrlHH12LuqU56URpDqvlAX k2hQ== X-Forwarded-Encrypted: i=1; AJvYcCXZEeFGyx+yAq8VHbr6DfMw7t9Ti6sd72ZKKfWjbOTgHgwEt+E1MwYAEobQH1Drg51bM4r7C5WOjHKVWR/3wXw1KaWQ/413CBIjgn+V X-Gm-Message-State: AOJu0YxZOfQpp9IAl7RPoL6IxyLo7dzDZZF2hvJE7IKcaOH4Z+Z7FFOf to6H8uFqfVP/lj/G7D9ombc66zBqe/JOLF2mTsSChbIXD7trNTPiYDDrNJLnY5aZHG1s/S3PUKA tbO3PyTLmPjsGSg== X-Google-Smtp-Source: AGHT+IGESfcd3ZtITp1JXLds/mxHvLWIRxSoRx+UcHnTvQKMVxNmpKT2THk9RSjcdG/X/Qyd1hg538pgt3hD+RU= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:2608:0:b0:e0e:445b:606 with SMTP id 3f1490d57ef6-e115586755amr4662276.0.1723622766668; Wed, 14 Aug 2024 01:06:06 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:23 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6274; i=aliceryhl@google.com; h=from:subject:message-id; bh=ZGRtM04u3g5Up6WlihqLvzD2O+qnfmGpQGoUaB/u7j0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVd6JArS/zg3Cb/LIJe3ugUi6A1hNKOrkLt9 XZXxL9k452JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXQAKCRAEWL7uWMY5 RpEDD/9uiAnUQ4xE/rNDmHVsxO4N+QMSR/YJPRL/+dhnjQllgHHoiebDcfg4nofwswRXAouyki8 oBk7XKjEuv17WmEzxvYy9WxIDxS6WUn5c+kIWNn/XsITHBKA8VU+Z4cC61gbS3fsM+RUJVPzDdk GkYYCmw1AH5ijrhsKmCOZz3SnMswOjTpLxpQZzMtSDf0OJZAjH/Cn6LONYF/CSjcZMuUqg7REkk V1s/g98OWmyPZNeR8yIPXEeoFR5jN8ytYCCaA5QB2+CZ5GxGbeiGuoZPzYtj1nWdRej1BuNlAph YdVAVtxK5rehoNoXoxgPJKspizOmJt3/5ntjO5Hqkh5/FP/XX4yTx+g+Q9EPzFr/Vk7YuA+VZct 4T6GQqa448xSDNDdcjCcymNB+sjwWC542o/19MO0i3YYAzQj4k8DgA07pBFpo+wx4JGCg+LPCKs 5QjoL79K2uhVFe7n8ugnhXDgVsoQI2r9CMgcGqkY82CUZhtyzfOF9aTDqekq07z3mSP6tdB2N6F VPsPLm7KcQ9Hf6PPkQL/shVrRVrZdJtBMeJANF5nvS3TTphSiFegJD1RqInvY97aqPHtWF5UUpy gVtbZ4xAfXO26uNUjY08TqyK4uIe+SqCWJ09eeHNgI9pxqVigwxIdTiFHBmx3/Db2Jr9r7pfWip RhwPrvELPaN+vKw== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-4-f5f5e8075da0@google.com> Subject: [PATCH v5 04/10] rust: list: add struct with prev/next pointers From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Define the ListLinks struct, which wraps the prev/next pointers that will be used to insert values into a List in a future patch. Also define the ListItem trait, which is implemented by structs that have a ListLinks field. The ListItem trait provides four different methods that are all essentially container_of or the reverse of container_of. Two of them are used before inserting/after removing an item from the list, and the two others are used when looking at a value without changing whether it is in a list. This distinction is introduced because it is needed for the patch that adds support for heterogeneous lists, which are implemented by adding a third pointer field with a fat pointer to the full struct. When inserting into the heterogeneous list, the pointer-to-self is updated to have the right vtable, and the container_of operation is implemented by just returning that pointer instead of using the real container_of operation. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 119 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 119 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 8e1533ee987b..074ae863ff5a 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -4,5 +4,124 @@ =20 //! A linked list implementation. =20 +use crate::init::PinInit; +use crate::types::Opaque; +use core::ptr; + mod arc; pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; + +/// Implemented by types where a [`ListArc`] can be inserted into a = `List`. +/// +/// # Safety +/// +/// Implementers must ensure that they provide the guarantees documented o= n methods provided by +/// this trait. +/// +/// [`ListArc`]: ListArc +pub unsafe trait ListItem: ListArcSafe { + /// Views the [`ListLinks`] for this value. + /// + /// # Guarantees + /// + /// If there is a previous call to `prepare_to_insert` and there is no= call to `post_remove` + /// since the most recent such call, then this returns the same pointe= r as the one returned by + /// the most recent call to `prepare_to_insert`. + /// + /// Otherwise, the returned pointer points at a read-only [`ListLinks`= ] with two null pointers. + /// + /// # Safety + /// + /// The provided pointer must point at a valid value. (It need not be = in an `Arc`.) + unsafe fn view_links(me: *const Self) -> *mut ListLinks; + + /// View the full value given its [`ListLinks`] field. + /// + /// Can only be used when the value is in a list. + /// + /// # Guarantees + /// + /// * Returns the same pointer as the one passed to the most recent ca= ll to `prepare_to_insert`. + /// * The returned pointer is valid until the next call to `post_remov= e`. + /// + /// # Safety + /// + /// * The provided pointer must originate from the most recent call to= `prepare_to_insert`, or + /// from a call to `view_links` that happened after the most recent = call to + /// `prepare_to_insert`. + /// * Since the most recent call to `prepare_to_insert`, the `post_rem= ove` method must not have + /// been called. + unsafe fn view_value(me: *mut ListLinks) -> *const Self; + + /// This is called when an item is inserted into a `List`. + /// + /// # Guarantees + /// + /// The caller is granted exclusive access to the returned [`ListLinks= `] until `post_remove` is + /// called. + /// + /// # Safety + /// + /// * The provided pointer must point at a valid value in an [`Arc`]. + /// * Calls to `prepare_to_insert` and `post_remove` on the same value= must alternate. + /// * The caller must own the [`ListArc`] for this value. + /// * The caller must not give up ownership of the [`ListArc`] unless = `post_remove` has been + /// called after this call to `prepare_to_insert`. + /// + /// [`Arc`]: crate::sync::Arc + unsafe fn prepare_to_insert(me: *const Self) -> *mut ListLinks; + + /// This undoes a previous call to `prepare_to_insert`. + /// + /// # Guarantees + /// + /// The returned pointer is the pointer that was originally passed to = `prepare_to_insert`. + /// + /// # Safety + /// + /// The provided pointer must be the pointer returned by the most rece= nt call to + /// `prepare_to_insert`. + unsafe fn post_remove(me: *mut ListLinks) -> *const Self; +} + +#[repr(C)] +#[derive(Copy, Clone)] +struct ListLinksFields { + next: *mut ListLinksFields, + prev: *mut ListLinksFields, +} + +/// The prev/next pointers for an item in a linked list. +/// +/// # Invariants +/// +/// The fields are null if and only if this item is not in a list. +#[repr(transparent)] +pub struct ListLinks { + // This type is `!Unpin` for aliasing reasons as the pointers are part= of an intrusive linked + // list. + #[allow(dead_code)] + inner: Opaque, +} + +// SAFETY: The only way to access/modify the pointers inside of `ListLinks= ` is via holding the +// associated `ListArc`. Since that type correctly implements `Send= `, it is impossible to +// move this an instance of this type to a different thread if the pointee= s are `!Send`. +unsafe impl Send for ListLinks {} +// SAFETY: The type is opaque so immutable references to a ListLinks are u= seless. Therefore, it's +// okay to have immutable access to a ListLinks from several threads at on= ce. +unsafe impl Sync for ListLinks {} + +impl ListLinks { + /// Creates a new initializer for this type. + pub fn new() -> impl PinInit { + // INVARIANT: Pin-init initializers can't be used on an existing `= Arc`, so this value will + // not be constructed in an `Arc` that already has a `ListArc`. + ListLinks { + inner: Opaque::new(ListLinksFields { + prev: ptr::null_mut(), + next: ptr::null_mut(), + }), + } + } +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 11A5014386C for ; Wed, 14 Aug 2024 08:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622774; cv=none; b=Mo3dRHtIpYxV5x1rxbhY+dQlJVecl5+eNsKOloJBAMWmO200e3klY7jJF6i/XD8v+YoHWUtaj+dDewjPa3PbURu3ESZJfG9aThwg8HJKM8tVyjbHtaSUR1mJGGW1RnVermfxXOUcv+1WIgMXzqnkFhtKT3v1rHnyXpKvek/GycM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622774; c=relaxed/simple; bh=qdxse/zQeBUwN7La6igBJ4pZmUKkwuTv3eH6GZBk19Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pDjVOvUFyEEvo14UqrgXyK/lJAgPBRgpQWXZTsTh8TUPvzNC4FiRECka8Xag/aA09XQ7K3BaVCOtYVgXworrPlDsh9Qfffekd+9oVCx8yb8lu3t0pXm30ZPQPwW1S5vX37OIv/jUlJ+coJRvMyJXGdkMtvqOuQuchg0as3GyL58= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NqaUpLIl; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NqaUpLIl" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-368448dfe12so320711f8f.1 for ; Wed, 14 Aug 2024 01:06:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622769; x=1724227569; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dSEMDEujkX2rv1p5gjNHkGYbhQ0hRO1KR3+H5BUTJx0=; b=NqaUpLIl5pbzfMLF4U0DsgRtdygLIRQFAG2a4RLXnkmpj9wpfY9Lr7GCeGIpBMy3Ou I6z3X4phhv6cZppXCtmlruZjdwD8mFh2nalrJ0syUWcMjwfuDzv9i3Xz9ma6EnIUX4J9 5P0/oIILWyt3ZTLYVgkZbvtQWghRKLpkwhy6bm1grQEto1lZ5QksEUZbH+QvQC/2lo17 Mn8LLwcQclv+ireF4w7NyKRJFDqY8X+xbhZRItOMyuIGzJY63aF5GInwhfKOFaCoWV8y q9rh7C8+Q7WHpBJmlfw4r5RaXsiyzB6WX+3L2hWoC6xm05gbM314XfngfMzXnpbHQdWx sVMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622769; x=1724227569; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dSEMDEujkX2rv1p5gjNHkGYbhQ0hRO1KR3+H5BUTJx0=; b=pNVJCPv0mv/rlcD93bYFdhCw8odjwDYtR0l14qSRoznyhL869jAwWai6+M1kL5ZNDr mCIInpSlloWGEZ4e8g6grbe7+ww2punrFNe+V9JWwqkEkd9l4JpMRpuE5VtLkEX0lo+J 60pJ6P01tnPPJdxucR/njdl+h1oaVMZ7v/I/iELByTTRUWweey0x5qULAtmW18RFR/ia F92/6t/eimWJI5PNIQprqCdSQVhpJnnjG7U73RwKoWK3LIPAybkrsEoW1dSN/WLgnaYv 5LPjyAhtp9bactt7QInHpCL65wDGj7dkpVNdGNJ6KEPEruSM8haCsRw8bl1wr7vRXoym wPYw== X-Forwarded-Encrypted: i=1; AJvYcCW558TtxNx0oCmEnB3O1NjU0v8OeFPSMNqZyinBdzfi66mk+0/LINO1J9yPcxpgWt2mzwpRj7ShE2E+adSwchIFirKUPggovtmv3SCa X-Gm-Message-State: AOJu0Yzu9EhgJrWWOtol4DqixK222DX5CF1IyA4YaMQKkr9Kq1nOSNEc czQ6vS1UB8FF8DrUz0gKFYjIhzpYD0EkhjEsUcfHWWTSLWPUHL7P9rOS14og/srLYqTyp1WE4oc Eyze4ypVRIMppkA== X-Google-Smtp-Source: AGHT+IHw1++eC7xuLFCD4Ims9QPf6ZV/bQLzTGyuizcstn5dXz3b7oKHTFwMTFwF7RmkyElXV16fxZPPCv7x+x4= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5d:680d:0:b0:360:7198:6ca0 with SMTP id ffacd0b85a97d-371774f636emr11365f8f.2.1723622769130; Wed, 14 Aug 2024 01:06:09 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:24 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8517; i=aliceryhl@google.com; h=from:subject:message-id; bh=qdxse/zQeBUwN7La6igBJ4pZmUKkwuTv3eH6GZBk19Q=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVeb+JBrhpDAp3hv8qGZdOwgc6cgA2tH/7pl o6RSRCer/6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXgAKCRAEWL7uWMY5 RmBoD/0UWq88uX47dTn9LvTUT/ggWRhQ7qX92urGpJmyP4GKksYgKAkyWLeltyR801HaDpWac8G 5B6N4NODJksRz1dZgKub9SLbvjpiHrdrSfG/aVOb3zoZBiQywL4NanoTfoau7BzdzEH71yPWlOp SQkjS7AzU5NmqXJiWmKpcnt2uo4DB7Ebi/ygi1KOVdZYgJkV/Bi1j+LKGj+jBTipYHncB65kUjM BLmZvlbuqgX4HkcNv8NFzXRqT2izBOAKfUYt28wsS+vf2/i70Hhe6Nyv84KV27F2YrCuEn4n5Ui f9sgrSoZ0+ZoNyN4TCzScVkVk6HkE/8qZvys5Z+AWewX03G4eg9gcqmKVMRoQMpxNKj81mgKyzM Jwt0qLEzzvfbngiJNIMgRuLXHkaNFvdE8UCn9hd/Jtq1meH9qdvzOc2YMAHUcKqebighKlC++A0 DVg9y+FiiCx0u6BV4uEGuTdHInEzIAwH3WDOsYxuR2M9Sjj3sjIp1XCOfvaZONS3HA4oqrhZO+W 4SdHRPdESF7XCOBboaAF8xKRtaCJmsri/rgvm+mxA7WEB1ZWWk67M1J5BdFvcrRsjqqn4i7EL2E 4mKm4C9xugUtxKBG2VSYZorxKSMVrKK8iXjVXdV1VXke/2YteUZ9usE+EmXtDWyg7bMAxTl5XUo WaTplHoANHjzOVg== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-5-f5f5e8075da0@google.com> Subject: [PATCH v5 05/10] rust: list: add macro for implementing ListItem From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Adds a macro for safely implementing the ListItem trait. As part of the implementation of the macro, we also provide a HasListLinks trait similar to the workqueue's HasWorkItem trait. The HasListLinks trait is only necessary if you are implementing ListItem using the impl_list_item macro. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 3 + rust/kernel/list/impl_list_item_mod.rs | 143 +++++++++++++++++++++++++++++= ++++ 2 files changed, 146 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 074ae863ff5a..670d53989b8f 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -8,6 +8,9 @@ use crate::types::Opaque; use core::ptr; =20 +mod impl_list_item_mod; +pub use self::impl_list_item_mod::{impl_has_list_links, impl_list_item, Ha= sListLinks}; + mod arc; pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; =20 diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs new file mode 100644 index 000000000000..1bcb14774aeb --- /dev/null +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Helpers for implementing list traits safely. + +use crate::list::ListLinks; + +/// Declares that this type has a `ListLinks` field at a fixed offset. +/// +/// This trait is only used to help implement `ListItem` safely. If `ListI= tem` is implemented +/// manually, then this trait is not needed. Use the [`impl_has_list_links= !`] macro to implement +/// this trait. +/// +/// # Safety +/// +/// All values of this type must have a `ListLinks` field at the given= offset. +/// +/// The behavior of `raw_get_list_links` must not be changed. +pub unsafe trait HasListLinks { + /// The offset of the `ListLinks` field. + const OFFSET: usize; + + /// Returns a pointer to the [`ListLinks`] field. + /// + /// # Safety + /// + /// The provided pointer must point at a valid struct of type `Self`. + /// + /// [`ListLinks`]: ListLinks + // We don't really need this method, but it's necessary for the implem= entation of + // `impl_has_list_links!` to be correct. + #[inline] + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks { + // SAFETY: The caller promises that the pointer is valid. The impl= ementer promises that the + // `OFFSET` constant is correct. + unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks } + } +} + +/// Implements the [`HasListLinks`] trait for the given type. +#[macro_export] +macro_rules! impl_has_list_links { + ($(impl$(<$($implarg:ident),*>)? + HasListLinks$(<$id:tt>)? + for $self:ident $(<$($selfarg:ty),*>)? + { self$(.$field:ident)* } + )*) =3D> {$( + // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the + // right type. + // + // The behavior of `raw_get_list_links` is not changed since the `= addr_of_mut!` macro is + // equivalent to the pointer offset operation in the trait definit= ion. + unsafe impl$(<$($implarg),*>)? $crate::list::HasListLinks$(<$id>)?= for + $self $(<$($selfarg),*>)? + { + const OFFSET: usize =3D ::core::mem::offset_of!(Self, $($field= ).*) as usize; + + #[inline] + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::l= ist::ListLinks$(<$id>)? { + // SAFETY: The caller promises that the pointer is not dan= gling. We know that this + // expression doesn't follow any pointers, as the `offset_= of!` invocation above + // would otherwise not compile. + unsafe { ::core::ptr::addr_of_mut!((*ptr)$(.$field)*) } + } + } + )*}; +} +pub use impl_has_list_links; + +/// Implements the [`ListItem`] trait for the given type. +/// +/// Requires that the type implements [`HasListLinks`]. Use the [`impl_has= _list_links!`] macro to +/// implement that trait. +/// +/// [`ListItem`]: crate::list::ListItem +#[macro_export] +macro_rules! impl_list_item { + ( + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + using ListLinks; + })* + ) =3D> {$( + // SAFETY: See GUARANTEES comment on each method. + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t= { + // GUARANTEES: + // * This returns the same pointer as `prepare_to_insert` beca= use `prepare_to_insert` + // is implemented in terms of `view_links`. + // * By the type invariants of `ListLinks`, the `ListLinks` ha= s two null pointers when + // this value is not in a list. + unsafe fn view_links(me: *const Self) -> *mut $crate::list::Li= stLinks<$num> { + // SAFETY: The caller guarantees that `me` points at a val= id value of type `Self`. + unsafe { + >::raw_get_li= st_links(me.cast_mut()) + } + } + + // GUARANTEES: + // * `me` originates from the most recent call to `prepare_to_= insert`, which just added + // `offset` to the pointer passed to `prepare_to_insert`. Th= is method subtracts + // `offset` from `me` so it returns the pointer originally p= assed to + // `prepare_to_insert`. + // * The pointer remains valid until the next call to `post_re= move` because the caller + // of the most recent call to `prepare_to_insert` promised t= o retain ownership of the + // `ListArc` containing `Self` until the next call to `post_= remove`. The value cannot + // be destroyed while a `ListArc` reference exists. + unsafe fn view_value(me: *mut $crate::list::ListLinks<$num>) -= > *const Self { + let offset =3D >:= :OFFSET; + // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it + // points at the field at offset `offset` in a value of ty= pe `Self`. Thus, + // subtracting `offset` from `me` is still in-bounds of th= e allocation. + unsafe { (me as *const u8).sub(offset) as *const Self } + } + + // GUARANTEES: + // This implementation of `ListItem` will not give out exclusi= ve access to the same + // `ListLinks` several times because calls to `prepare_to_inse= rt` and `post_remove` + // must alternate and exclusive access is given up when `post_= remove` is called. + // + // Other invocations of `impl_list_item!` also cannot give out= exclusive access to the + // same `ListLinks` because you can only implement `ListItem` = once for each value of + // `ID`, and the `ListLinks` fields only work with the specifi= ed `ID`. + unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::l= ist::ListLinks<$num> { + // SAFETY: The caller promises that `me` points at a valid= value. + unsafe { >::view_link= s(me) } + } + + // GUARANTEES: + // * `me` originates from the most recent call to `prepare_to_= insert`, which just added + // `offset` to the pointer passed to `prepare_to_insert`. Th= is method subtracts + // `offset` from `me` so it returns the pointer originally p= assed to + // `prepare_to_insert`. + unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) = -> *const Self { + let offset =3D >:= :OFFSET; + // SAFETY: `me` originates from the most recent call to `p= repare_to_insert`, so it + // points at the field at offset `offset` in a value of ty= pe `Self`. Thus, + // subtracting `offset` from `me` is still in-bounds of th= e allocation. + unsafe { (me as *const u8).sub(offset) as *const Self } + } + } + )*}; +} +pub use impl_list_item; --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 C539313D531 for ; Wed, 14 Aug 2024 08:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622777; cv=none; b=T7NEIKcrQK/sylflVdH/N7Aa2aztQdt000DlArDMPpkfpDJFe2zaKpnr5hnHtInETa4OcAs/TQnTVSirGYXeCn5B1oXhAE8wvlSsWuGojsAU0I2ZOjotph4+jeSnYRZ3EL2cavgIJDhCk6BJiQT5rGjEBVqnk7H3Sio7tFAcKiY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622777; c=relaxed/simple; bh=iE5BUobdaUXoE7DrPJDKOBE+/KVoLiI+OKEKvkvjTdQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GPq6teogDSDLOvObaHS20ubuiUKKG/KdHR5SSx0c2iiZvyYzepxnNxbZiA/NNdqX7PvSs83Mgo+pjGlgOtZcUszrtstjjAZYbvlXCWoR33rE6DZQg7wJIeTZ1G6RoaLr0zFI22mtEhQRjwPGxO2wjK0zG/DLk87B4g9cUawnIGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=c1Qk9i4M; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c1Qk9i4M" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4281310bf7aso45011885e9.1 for ; Wed, 14 Aug 2024 01:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622772; x=1724227572; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=g5SVLo3I7t3VZwktEnZCFUT5k7rjJkC5UNvGelK4Neo=; b=c1Qk9i4MpdJsGzy0oGwTW0IHEdUAVr1+NMOHHjqzReaRT0iuyfEIUSgT9vXXFlyzCe Ua8CcTibf6v2+6ijJGZetbrPoRGoAixYYjYtfDiJGVmw1lhKjTwGu4V/FdKK7r+hm9tl oEK1hzT9uCpwDET+n82PQhSb+4A5MShSC/TbCsIoEW0RcLX4+aBLU0GM/9GfP+920mnD ko9hYvHrPZ/rcrV+chUyYdDDWjsM/EvV970qRqO2y0hBWJQuwa4GE9NfdG8o0Six9nvG 40nf+htoydbOsGP/6FqQWdHdj4RHSceveGO0g143RX5em65AQhCeR3XFGhEZY8lT0jd5 QzoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622772; x=1724227572; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=g5SVLo3I7t3VZwktEnZCFUT5k7rjJkC5UNvGelK4Neo=; b=ojE/eg5NE0Y9TYXAjeNWwS2+PYPk7+ckK8rJiNKMkHhdryVYRZBeGP84v1L+Sue23Z fI/34LLXea6wrD0wkwUEP/XT1sET96wWVeVrzTAJFxHt9XIImqCGp0fYBhf8k1dLkFvW F1JDuG6oPZVKpGBwvAqVnvbc55/mhztoEmnErndN72pErW53pRr+WE+L7MzIlQuC7xZk w7ZFR0oszLRJDjqW4bn5UOprsgpvLBfTMg1DRlF2/ioC2TbeJ78uB5tldrIGcXD9Rrz/ CzYvJNua3cNHq79v20umcjZvAKI3Jww3reQ93ZrgHpwRAMhah+aY3V8O+BWcy+ut7QpM yYAg== X-Forwarded-Encrypted: i=1; AJvYcCUhurw+m8gxWbiOeuzDuqPOOb41TaHZcWTXCjAz8w96p32PvLMlYpa+2cEBZVV8Oove1u0AqtSR2jDbfY2h21TM/TfA/r0Nte8hS84U X-Gm-Message-State: AOJu0YxuOBHBt9RyviQxQ1ix1N75duLAXBMYJSm3KUPIKrlMNTVIx2Fq HRAm1++Blybs74WLV9qLs1zdw8BZJxl3pyek83KKBdxhh1mJ5ZJ9lfHKW9drtPBviGicHRvORSD CoBLmuIHi/YsEvA== X-Google-Smtp-Source: AGHT+IGamAxiaZ0cArkmmYAzWJHmi4njPXNMi9SOr2n+ceXNrQHpm4OuaZt149fe+WIF1ewix6qX+6qG5me65ww= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:49a7:b0:428:1001:2ee1 with SMTP id 5b1f17b1804b1-429dd273d99mr39735e9.7.1723622771807; Wed, 14 Aug 2024 01:06:11 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:25 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=19102; i=aliceryhl@google.com; h=from:subject:message-id; bh=iE5BUobdaUXoE7DrPJDKOBE+/KVoLiI+OKEKvkvjTdQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVf2hJsIY4xrqewlT6SN4Kgv6/Z82X8OFvDz rpNAVSqG7mJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXwAKCRAEWL7uWMY5 RnpdEACao3/GHbsJwwyuJE92aaXDvEfiDjWBmlkts3pyM2G1Iss00MItws0Uy6yghC6Pnsk0TAK lHUdP6L8c2L4TR11hgUMievlyOqW2IlqnOMJK0JkVn+6Qk//8jbjPXQCPpgx21lJLqQGCjxhTOq Nj1AjaSkj/Z4LjWoSDfuYbJYizkzteEBFWZOUKnGvJGLTmNifMt/+7UpEqlHiPT752BBpE9izyE WPTPQZwm11csOccUVVuNLPdRi7n7xdgNJEtz91SuhE0Wo5vebCQOq3DCByAr7su/ySMSMIApbN4 HQlvq85ksU0/PFZALxUe21wBY48RsutR0+pWOXYbLD4PPbJDvYVDI/NKXhcNhL2/vABCo8gmhJZ hefodCBfHawMWJlctg/p7xxTqtJAIwpGWwQ2klzLbQvnm90GC7xs3PyE+JH/UUZy3g2BxVwVDXI gZzEM1BhiREOdqRQlU15y6H6e7x351QvuxjSIRNcYkgE749yxWhVjvxzdxEFV35YvD71d7vY6lR cXUhr7lblhwtbYufu9vcT4y/vtoLcDxNuQWAg5tJk8VcaLlEqhZkH4YOa4gg3nP2MFh6rPQvWIO VCrkHcF2osrHGwxmIixsV8DreK8KVg8LNPyWBP6OH75K5oTur3VUpAxGB6Yg6gx2RZfh2LoaY7J t6FATJQ9yIw0Eyw== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-6-f5f5e8075da0@google.com> Subject: [PATCH v5 06/10] rust: list: add List From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add the actual linked list itself. The linked list uses the following design: The List type itself just has a single pointer to the first element of the list. And the actual list items then form a cycle. So the last item is `first->prev`. This is slightly different from the usual kernel linked list. Matching that exactly would amount to giving List two pointers, and having it be part of the cycle of items. This alternate design has the advantage that the cycle is never completely empty, which can reduce the number of branches in some cases. However, it also has the disadvantage that List must be pinned, which this design is trying to avoid. Having the list items form a cycle rather than having null pointers at the beginning/end is convenient for several reasons. For one, it lets us store only one pointer in List, and it simplifies the implementation of several functions. Unfortunately, the `remove` function that removes an arbitrary element from the list has to be unsafe. This is needed because there is no way to handle the case where you pass an element from the wrong list. For example, if it is the first element of some other list, then that other list's `first` pointer would not be updated. Similarly, it could be a data race if you try to remove it from two different lists in parallel. (There's no problem with passing `remove` an item that's not in any list. Additionally, other removal methods such as `pop_front` need not be unsafe, as they can't be used to remove items from another list.) A future patch in this series will introduce support for cursors that can be used to remove arbitrary items without unsafe code. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 330 ++++++++++++++++++++++++++++++++++++++++++++= +++- rust/kernel/list/arc.rs | 6 +- 2 files changed, 331 insertions(+), 5 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 670d53989b8f..551c46a2401b 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -6,6 +6,7 @@ =20 use crate::init::PinInit; use crate::types::Opaque; +use core::marker::PhantomData; use core::ptr; =20 mod impl_list_item_mod; @@ -14,7 +15,42 @@ mod arc; pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; =20 -/// Implemented by types where a [`ListArc`] can be inserted into a = `List`. +/// A linked list. +/// +/// All elements in this linked list will be [`ListArc`] references to the= value. Since a value can +/// only have one `ListArc` (for each pair of prev/next pointers), this en= sures that the same +/// prev/next pointers are not used for several linked lists. +/// +/// # Invariants +/// +/// * If the list is empty, then `first` is null. Otherwise, `first` point= s at the `ListLinks` +/// field of the first element in the list. +/// * All prev/next pointers in `ListLinks` fields of items in the list ar= e valid and form a cycle. +/// * For every item in the list, the list owns the associated [`ListArc`]= reference and has +/// exclusive access to the `ListLinks` field. +pub struct List, const ID: u64 =3D 0> { + first: *mut ListLinksFields, + _ty: PhantomData>, +} + +// SAFETY: This is a container of `ListArc`, and access to the cont= ainer allows the same +// type of access to the `ListArc` elements. +unsafe impl Send for List +where + ListArc: Send, + T: ?Sized + ListItem, +{ +} +// SAFETY: This is a container of `ListArc`, and access to the cont= ainer allows the same +// type of access to the `ListArc` elements. +unsafe impl Sync for List +where + ListArc: Sync, + T: ?Sized + ListItem, +{ +} + +/// Implemented by types where a [`ListArc`] can be inserted into a = [`List`]. /// /// # Safety /// @@ -56,7 +92,7 @@ pub unsafe trait ListItem: ListArcSa= fe { /// been called. unsafe fn view_value(me: *mut ListLinks) -> *const Self; =20 - /// This is called when an item is inserted into a `List`. + /// This is called when an item is inserted into a [`List`]. /// /// # Guarantees /// @@ -103,7 +139,6 @@ struct ListLinksFields { pub struct ListLinks { // This type is `!Unpin` for aliasing reasons as the pointers are part= of an intrusive linked // list. - #[allow(dead_code)] inner: Opaque, } =20 @@ -127,4 +162,293 @@ pub fn new() -> impl PinInit { }), } } + + /// # Safety + /// + /// `me` must be dereferencable. + #[inline] + unsafe fn fields(me: *mut Self) -> *mut ListLinksFields { + // SAFETY: The caller promises that the pointer is valid. + unsafe { Opaque::raw_get(ptr::addr_of!((*me).inner)) } + } + + /// # Safety + /// + /// `me` must be dereferencable. + #[inline] + unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self { + me.cast() + } +} + +impl, const ID: u64> List { + /// Creates a new empty list. + pub const fn new() -> Self { + Self { + first: ptr::null_mut(), + _ty: PhantomData, + } + } + + /// Returns whether this list is empty. + pub fn is_empty(&self) -> bool { + self.first.is_null() + } + + /// Add the provided item to the back of the list. + pub fn push_back(&mut self, item: ListArc) { + let raw_item =3D ListArc::into_raw(item); + // SAFETY: + // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`. + // * Since we have ownership of the `ListArc`, `post_remove` must = have been called after + // the most recent call to `prepare_to_insert`, if any. + // * We own the `ListArc`. + // * Removing items from this list is always done using `remove_in= ternal_inner`, which + // calls `post_remove` before giving up ownership. + let list_links =3D unsafe { T::prepare_to_insert(raw_item) }; + // SAFETY: We have not yet called `post_remove`, so `list_links` i= s still valid. + let item =3D unsafe { ListLinks::fields(list_links) }; + + if self.first.is_null() { + self.first =3D item; + // SAFETY: The caller just gave us ownership of these fields. + // INVARIANT: A linked list with one item should be cyclic. + unsafe { + (*item).next =3D item; + (*item).prev =3D item; + } + } else { + let next =3D self.first; + // SAFETY: By the type invariant, this pointer is valid or nul= l. We just checked that + // it's not null, so it must be valid. + let prev =3D unsafe { (*next).prev }; + // SAFETY: Pointers in a linked list are never dangling, and t= he caller just gave us + // ownership of the fields on `item`. + // INVARIANT: This correctly inserts `item` between `prev` and= `next`. + unsafe { + (*item).next =3D next; + (*item).prev =3D prev; + (*prev).next =3D item; + (*next).prev =3D item; + } + } + } + + /// Add the provided item to the front of the list. + pub fn push_front(&mut self, item: ListArc) { + let raw_item =3D ListArc::into_raw(item); + // SAFETY: + // * We just got `raw_item` from a `ListArc`, so it's in an `Arc`. + // * If this requirement is violated, then the previous caller of = `prepare_to_insert` + // violated the safety requirement that they can't give up owner= ship of the `ListArc` + // until they call `post_remove`. + // * We own the `ListArc`. + // * Removing items] from this list is always done using `remove_i= nternal_inner`, which + // calls `post_remove` before giving up ownership. + let list_links =3D unsafe { T::prepare_to_insert(raw_item) }; + // SAFETY: We have not yet called `post_remove`, so `list_links` i= s still valid. + let item =3D unsafe { ListLinks::fields(list_links) }; + + if self.first.is_null() { + // SAFETY: The caller just gave us ownership of these fields. + // INVARIANT: A linked list with one item should be cyclic. + unsafe { + (*item).next =3D item; + (*item).prev =3D item; + } + } else { + let next =3D self.first; + // SAFETY: We just checked that `next` is non-null. + let prev =3D unsafe { (*next).prev }; + // SAFETY: Pointers in a linked list are never dangling, and t= he caller just gave us + // ownership of the fields on `item`. + // INVARIANT: This correctly inserts `item` between `prev` and= `next`. + unsafe { + (*item).next =3D next; + (*item).prev =3D prev; + (*prev).next =3D item; + (*next).prev =3D item; + } + } + self.first =3D item; + } + + /// Removes the last item from this list. + pub fn pop_back(&mut self) -> Option> { + if self.first.is_null() { + return None; + } + + // SAFETY: We just checked that the list is not empty. + let last =3D unsafe { (*self.first).prev }; + // SAFETY: The last item of this list is in this list. + Some(unsafe { self.remove_internal(last) }) + } + + /// Removes the first item from this list. + pub fn pop_front(&mut self) -> Option> { + if self.first.is_null() { + return None; + } + + // SAFETY: The first item of this list is in this list. + Some(unsafe { self.remove_internal(self.first) }) + } + + /// Removes the provided item from this list and returns it. + /// + /// This returns `None` if the item is not in the list. (Note that by = the safety requirements, + /// this means that the item is not in any list.) + /// + /// # Safety + /// + /// `item` must not be in a different linked list (with the same id). + pub unsafe fn remove(&mut self, item: &T) -> Option> { + let mut item =3D unsafe { ListLinks::fields(T::view_links(item)) }; + // SAFETY: The user provided a reference, and reference are never = dangling. + // + // As for why this is not a data race, there are two cases: + // + // * If `item` is not in any list, then these fields are read-onl= y and null. + // * If `item` is in this list, then we have exclusive access to = these fields since we + // have a mutable reference to the list. + // + // In either case, there's no race. + let ListLinksFields { next, prev } =3D unsafe { *item }; + + debug_assert_eq!(next.is_null(), prev.is_null()); + if !next.is_null() { + // This is really a no-op, but this ensures that `item` is a r= aw pointer that was + // obtained without going through a pointer->reference->pointe= r conversion rountrip. + // This ensures that the list is valid under the more restrict= ive strict provenance + // ruleset. + // + // SAFETY: We just checked that `next` is not null, and it's n= ot dangling by the + // list invariants. + unsafe { + debug_assert_eq!(item, (*next).prev); + item =3D (*next).prev; + } + + // SAFETY: We just checked that `item` is in a list, so the ca= ller guarantees that it + // is in this list. The pointers are in the right order. + Some(unsafe { self.remove_internal_inner(item, next, prev) }) + } else { + None + } + } + + /// Removes the provided item from the list. + /// + /// # Safety + /// + /// `item` must point at an item in this list. + unsafe fn remove_internal(&mut self, item: *mut ListLinksFields) -> Li= stArc { + // SAFETY: The caller promises that this pointer is not dangling, = and there's no data race + // since we have a mutable reference to the list containing `item`. + let ListLinksFields { next, prev } =3D unsafe { *item }; + // SAFETY: The pointers are ok and in the right order. + unsafe { self.remove_internal_inner(item, next, prev) } + } + + /// Removes the provided item from the list. + /// + /// # Safety + /// + /// The `item` pointer must point at an item in this list, and we must= have `(*item).next =3D=3D + /// next` and `(*item).prev =3D=3D prev`. + unsafe fn remove_internal_inner( + &mut self, + item: *mut ListLinksFields, + next: *mut ListLinksFields, + prev: *mut ListLinksFields, + ) -> ListArc { + // SAFETY: We have exclusive access to the pointers of items in th= e list, and the prev/next + // pointers are always valid for items in a list. + // + // INVARIANT: There are three cases: + // * If the list has at least three items, then after removing th= e item, `prev` and `next` + // will be next to each other. + // * If the list has two items, then the remaining item will poin= t at itself. + // * If the list has one item, then `next =3D=3D prev =3D=3D item= `, so these writes have no + // effect. The list remains unchanged and `item` is still in th= e list for now. + unsafe { + (*next).prev =3D prev; + (*prev).next =3D next; + } + // SAFETY: We have exclusive access to items in the list. + // INVARIANT: `item` is being removed, so the pointers should be n= ull. + unsafe { + (*item).prev =3D ptr::null_mut(); + (*item).next =3D ptr::null_mut(); + } + // INVARIANT: There are three cases: + // * If `item` was not the first item, then `self.first` should r= emain unchanged. + // * If `item` was the first item and there is another item, then= we just updated + // `prev->next` to `next`, which is the new first item, and set= ting `item->next` to null + // did not modify `prev->next`. + // * If `item` was the only item in the list, then `prev =3D=3D i= tem`, and we just set + // `item->next` to null, so this correctly sets `first` to null= now that the list is + // empty. + if self.first =3D=3D item { + // SAFETY: The `prev` pointer is the value that `item->prev` h= ad when it was in this + // list, so it must be valid. There is no race since `prev` is= still in the list and we + // still have exclusive access to the list. + self.first =3D unsafe { (*prev).next }; + } + + // SAFETY: `item` used to be in the list, so it is dereferencable = by the type invariants + // of `List`. + let list_links =3D unsafe { ListLinks::from_fields(item) }; + // SAFETY: Any pointer in the list originates from a `prepare_to_i= nsert` call. + let raw_item =3D unsafe { T::post_remove(list_links) }; + // SAFETY: The above call to `post_remove` guarantees that we can = recreate the `ListArc`. + unsafe { ListArc::from_raw(raw_item) } + } + + /// Moves all items from `other` into `self`. + /// + /// The items of `other` are added to the back of `self`, so the last = item of `other` becomes + /// the last item of `self`. + pub fn push_all_back(&mut self, other: &mut List) { + // First, we insert the elements into `self`. At the end, we make = `other` empty. + if self.is_empty() { + // INVARIANT: All of the elements in `other` become elements o= f `self`. + self.first =3D other.first; + } else if !other.is_empty() { + let other_first =3D other.first; + // SAFETY: The other list is not empty, so this pointer is val= id. + let other_last =3D unsafe { (*other_first).prev }; + let self_first =3D self.first; + // SAFETY: The self list is not empty, so this pointer is vali= d. + let self_last =3D unsafe { (*self_first).prev }; + + // SAFETY: We have exclusive access to both lists, so we can u= pdate the pointers. + // INVARIANT: This correctly sets the pointers to merge both l= ists. We do not need to + // update `self.first` because the first element of `self` doe= s not change. + unsafe { + (*self_first).prev =3D other_last; + (*other_last).next =3D self_first; + (*self_last).next =3D other_first; + (*other_first).prev =3D self_last; + } + } + + // INVARIANT: The other list is now empty, so update its pointer. + other.first =3D ptr::null_mut(); + } +} + +impl, const ID: u64> Default for List { + fn default() -> Self { + List::new() + } +} + +impl, const ID: u64> Drop for List { + fn drop(&mut self) { + while let Some(item) =3D self.pop_front() { + drop(item); + } + } } diff --git a/rust/kernel/list/arc.rs b/rust/kernel/list/arc.rs index c5921a7d5966..d801b9dc6291 100644 --- a/rust/kernel/list/arc.rs +++ b/rust/kernel/list/arc.rs @@ -133,8 +133,8 @@ fn try_new_list_arc(&self) -> bool { /// The `ListArc` type can be thought of as a special reference to a refco= unted object that owns the /// permission to manipulate the `next`/`prev` pointers stored in the refc= ounted object. By ensuring /// that each object has only one `ListArc` reference, the owner of that r= eference is assured -/// exclusive access to the `next`/`prev` pointers. When a `ListArc` is in= serted into a `List`, the -/// `List` takes ownership of the `ListArc` reference. +/// exclusive access to the `next`/`prev` pointers. When a `ListArc` is in= serted into a [`List`], +/// the [`List`] takes ownership of the `ListArc` reference. /// /// There are various strategies to ensuring that a value has only one `Li= stArc` reference. The /// simplest is to convert a [`UniqueArc`] into a `ListArc`. However, the = refcounted object could @@ -156,6 +156,8 @@ fn try_new_list_arc(&self) -> bool { /// /// * Each reference counted object has at most one `ListArc` for each val= ue of `ID`. /// * The tracking inside `T` is aware that a `ListArc` reference exists. +/// +/// [`List`]: crate::list::List #[repr(transparent)] pub struct ListArc where --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 2F0BB148312 for ; Wed, 14 Aug 2024 08:06:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622778; cv=none; b=jbfD6yrZ65zfb6a6QxbyDMl0AvgM0ELzZt8l00nBM5M5rKnvC5A2jung9O1zkPZgnN8RqkqEjP0djpIYMyCNJ/U7faIovXL+oxy/+peOv+sEK4+9hnuTbCyk+ebSIWyKkROZkSjAFzutBP+oLk/jduKG19m6c1HBu05Bzvd/skE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622778; c=relaxed/simple; bh=dGtWLeCSuTNEwUugDyA1XdJqmUZPsl32zsHFqetb4/k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BzEN9TkvwpItaGkRiLEVRyOeX2AsxWDbTZT4PKNQ0j0G1AQXT4fX53d60y/LSeO7tKmz19tILeEq0k80lGB8tq/0mpAx6sEet3xPrpAx2sPMEotUgzIJ4IOqMgHVCS24ZEe8txVPw/kDv1hy6ErCI5uvK9AMK4ggX6Omq8oT3uE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zpfk77cQ; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zpfk77cQ" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-36848f30d39so3631106f8f.3 for ; Wed, 14 Aug 2024 01:06:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622774; x=1724227574; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Zzj0Y8qT0OVh2xjwyqiYL1BRt2Dsmc+jX/UXC6YevlE=; b=zpfk77cQBe7hAw3WPNQdnv6eiqaaDTRRUP6Y8irQvybs+J4/PvhEDDNrsg0nijaCci Yl9Vf6sEOO/XqJUxUtP99gYFalzq1Q339Lq+SkMfJFuH8kCZT9Dl7j/pZPD9Yy/OM9zw XdEti1EGa8jN1EOA7VwYmQd632Di8T3Ant0TlDiZo/NiZsnKG5fhpYEqgXzy2YuzrfM7 DNmzJcfw34/vJNWYQlB7Q2PGfiLt+Neqf8WKgxLgSPoVpPru7+baUwF7a3dw0DqgQIY4 iRQQPVwyCCHQeuCpOdTuXnuSdgrCfeHTzTcPYsZ6/kmA+iTp1xFty85AqAvHTaKwqWT5 K50w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622774; x=1724227574; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Zzj0Y8qT0OVh2xjwyqiYL1BRt2Dsmc+jX/UXC6YevlE=; b=rec0bcZiS3PeEsIxWcQsGnRE4Jwi27K1z4UCAk30maX/j4Hf0gVVNEym18b5LwrO69 25jVwUCymLhzJ8ZmK3bx+78a5umQ5RNbrM9Y7SxoJK2Y0h1rSMM5/4rnllmHa/eB5e1x NxbILX1XRlsxhc8/2kDH+HzHi90a9N/CJVXBqn6XXBRoMnek+voCLvicVJXLGuegCtlK KPoOb5PmfzdLKLerQfKYiKWpZvUsBgkja9w/EgtGNbAPhTe09I18NZWgGu32sHeJYIOf gAB+idlw+tAXwFFL4E7WM2B2CU1+AMzaoddesncliflElEcgFrcNeyjB6SOgebEjo9b6 VAvw== X-Forwarded-Encrypted: i=1; AJvYcCW+mZ8Q8aZH7S68SbvAbUuW5S+EQpgJhykLK+JyIrB7rJeRrJpc16td/5sQpOGs0SaWSVwDYDo0QKiZY26NrC1w6y3P16uNLK0wGfin X-Gm-Message-State: AOJu0YzXYaKAkIwh2HAgBIbHy3FTa9TrS0UxsvmcgBG/MjJLV2ISI4md lcPpnbpGkqEx/Vb/1o+bTYJ1lftvzaDFyRTsnPpPymhFzJiIlnV0eiumtoGAYeaE5a/zi0J0phv 3xlQB8fajwXAcdA== X-Google-Smtp-Source: AGHT+IEf6YoaCDMsRG7GznUzLKEpOLTGCT7RvZ7lwGKcm+8duQ7/MuPCrVm/1WBElrLQn5b0njZpo7YqRmyRpeU= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5d:69d1:0:b0:368:4910:16ed with SMTP id ffacd0b85a97d-37177785b92mr3218f8f.5.1723622774225; Wed, 14 Aug 2024 01:06:14 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:26 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5025; i=aliceryhl@google.com; h=from:subject:message-id; bh=dGtWLeCSuTNEwUugDyA1XdJqmUZPsl32zsHFqetb4/k=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVfrIu15K142PsJ8dJR1RWGMccf7FITauuV3 Vl9RoE+XTmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlXwAKCRAEWL7uWMY5 RhVtEACGLqSckv4oIE7zO1/sZxd7rQeK2aK1hlpaKAXO8Bw+JXv3aWCbb+yhvbVUBkYwdUyJPYJ oGk3TdXMTywqJhEiXBj6KcPVnlCS2KFgsRwFHonPf7SSiQ0RshD4tW85bs2TuXQUg84P3giB0SH XOM4Mt/LGwrrmLZBjP9X63kZIeKDb4pZ2UF1VK6Npuoxh8PSjuo03K2NaJZlrjC/W1fuT4t+E2K o9SfBSdnyWjEcSt+Mpl8tHd7vjinLDfWJG74bd07S5KniIyNcgJs5VSJljsS4JqG53lOBd9GdSB tYromwxGF0ZHCrj4uTQU4V6e+AOZ+uLAE9Gt7WQs8MezM85a+CaVhCcl1cYZxDfjPDlo7tvAGnF cestm4A7rsQUeZB9m70zyHxHZIVGuzNpcfsMndQzZxnu6DRgSPnBxgN5PuxnWczKKxawuML+rXR qiJVmFYgpYGNswepnc2sr8FzKop7jAAW3yTj74UCyNvV0guiXUvdaXVcn209mx+EwBkvBknxPx3 lJHHvcEMZSW1QAh26lQ86eSNi10b8QEGDwMXR5Qz150VSuC4UtF3qpvrhTkQOJZFRXEWbsanXD1 nyEXjnlHUVr6mJECdbi9L2F+pBxxXWUH18KP3Q225jkxOebQhs8vfOOhFEVzxpadyItNiVSwFB2 G1urdF+GicwxfXg== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-7-f5f5e8075da0@google.com> Subject: [PATCH v5 07/10] rust: list: add iterators From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Rust Binder has lists containing stuff such as all contexts or all processes, and sometimes needs to iterate over them. This patch enables Rust Binder to do that using a normal for loop. The iterator returns the ArcBorrow type, so it is possible to grab a refcount to values while iterating. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 102 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 551c46a2401b..0d680156b8b1 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -5,7 +5,9 @@ //! A linked list implementation. =20 use crate::init::PinInit; +use crate::sync::ArcBorrow; use crate::types::Opaque; +use core::iter::{DoubleEndedIterator, FusedIterator}; use core::marker::PhantomData; use core::ptr; =20 @@ -437,6 +439,17 @@ pub fn push_all_back(&mut self, other: &mut List) { // INVARIANT: The other list is now empty, so update its pointer. other.first =3D ptr::null_mut(); } + + /// Creates an iterator over the list. + pub fn iter(&self) -> Iter<'_, T, ID> { + // INVARIANT: If the list is empty, both pointers are null. Otherw= ise, both pointers point + // at the first element of the same list. + Iter { + current: self.first, + stop: self.first, + _ty: PhantomData, + } + } } =20 impl, const ID: u64> Default for List { @@ -452,3 +465,92 @@ fn drop(&mut self) { } } } + +/// An iterator over a [`List`]. +/// +/// # Invariants +/// +/// * There must be a [`List`] that is immutably borrowed for the duration= of `'a`. +/// * The `current` pointer is null or points at a value in that [`List`]. +/// * The `stop` pointer is equal to the `first` field of that [`List`]. +#[derive(Clone)] +pub struct Iter<'a, T: ?Sized + ListItem, const ID: u64 =3D 0> { + current: *mut ListLinksFields, + stop: *mut ListLinksFields, + _ty: PhantomData<&'a ListArc>, +} + +impl<'a, T: ?Sized + ListItem, const ID: u64> Iterator for Iter<'a, T,= ID> { + type Item =3D ArcBorrow<'a, T>; + + fn next(&mut self) -> Option> { + if self.current.is_null() { + return None; + } + + let current =3D self.current; + + // SAFETY: We just checked that `current` is not null, so it is in= a list, and hence not + // dangling. There's no race because the iterator holds an immutab= le borrow to the list. + let next =3D unsafe { (*current).next }; + // INVARIANT: If `current` was the last element of the list, then = this updates it to null. + // Otherwise, we update it to the next element. + self.current =3D if next !=3D self.stop { + next + } else { + ptr::null_mut() + }; + + // SAFETY: The `current` pointer points at a value in the list. + let item =3D unsafe { T::view_value(ListLinks::from_fields(current= )) }; + // SAFETY: + // * All values in a list are stored in an `Arc`. + // * The value cannot be removed from the list for the duration of= the lifetime annotated + // on the returned `ArcBorrow`, because removing it from the lis= t would require mutable + // access to the list. However, the `ArcBorrow` is annotated wit= h the iterator's + // lifetime, and the list is immutably borrowed for that lifetim= e. + // * Values in a list never have a `UniqueArc` reference. + Some(unsafe { ArcBorrow::from_raw(item) }) + } +} + +impl<'a, T: ?Sized + ListItem, const ID: u64> FusedIterator for Iter<'= a, T, ID> {} + +impl<'a, T: ?Sized + ListItem, const ID: u64> IntoIterator for &'a Lis= t { + type IntoIter =3D Iter<'a, T, ID>; + type Item =3D ArcBorrow<'a, T>; + + fn into_iter(self) -> Iter<'a, T, ID> { + self.iter() + } +} + +/// An owning iterator into a [`List`]. +pub struct IntoIter, const ID: u64 =3D 0> { + list: List, +} + +impl, const ID: u64> Iterator for IntoIter= { + type Item =3D ListArc; + + fn next(&mut self) -> Option> { + self.list.pop_front() + } +} + +impl, const ID: u64> FusedIterator for IntoIter {} + +impl, const ID: u64> DoubleEndedIterator for Into= Iter { + fn next_back(&mut self) -> Option> { + self.list.pop_back() + } +} + +impl, const ID: u64> IntoIterator for List= { + type IntoIter =3D IntoIter; + type Item =3D ListArc; + + fn into_iter(self) -> IntoIter { + IntoIter { list: self } + } +} --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 954FF14900E for ; Wed, 14 Aug 2024 08:06:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622780; cv=none; b=CEDLyvOO1BLN4uNJTdG3+bZnWUnPHLnYIKk09TFgEMo1YSLRQubWV/yHftMJlbv6n0ZvOo8s+XN9davrUq38UZugXDxNBk28xtvM+WkL68D3WNLxGMstD2gXSEAyuuswf36yAwW2PxwDTplAMuakcL4cpjmEr7CIcpb3dc3vuGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622780; c=relaxed/simple; bh=luJNd/MfagCpX5xgyCXvBHu7a6jvo5DfxROIQE96O0Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=galw3QZ+o5+bNhU0QL/QWuCNJOhOC6M1C2kncgC3rqoQxjZ1m2HzkZPu11wG4IJXqthq1/rAhmgHTViKxN0OWDRYmOR4Xp6egSUzBvp7EllANoJMhwQ+Wzps4znAdISRhRZK4RikHmX4w8LfdHfWRqK+yVGHas3Z7g8jXz3i/7E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RwzMHaCv; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RwzMHaCv" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4281310bf7aso45012695e9.1 for ; Wed, 14 Aug 2024 01:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622777; x=1724227577; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eU2WZjJDSLa2MV/5GK/awSXKL16LXQcf7wHaBKswmn4=; b=RwzMHaCvqXlcqn/4h4mFeJtHoioRGpOJcjBwa7lXGL/4lN9aq6XUGb24jxQEH/cqPw KpdSWFQVNuc7ZGFjB7i+DSjdRM02/ld13geDXDGf0G4HO9+pSSYLT7zXb45A5ZBMU9UU eBt0n72gSkcAbX3WvyVEMHicDHrn8NW6pqjLNd2SlY3zcgmKdjaWo/gcyWZRyzJ5DVDu vPrh3rt/zlUN+bsc1LkH/+aG+7VCaKAsh6ec1US1QTOeb1p9F6rctoIeoyeIk4LBtGve o+VB9jM0uGLgTw2JFmCrTD2etTWwc2NOX6bF/+8ajcSTH1dBKsZpnW75mCWjL6ifHGFF P/6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622777; x=1724227577; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eU2WZjJDSLa2MV/5GK/awSXKL16LXQcf7wHaBKswmn4=; b=OerONsiYj52aTwgRGK60xQVHEOfjh8GsiXwInmlhzNgVl/LjAvtesqRAZWQWKxSymn m+8tBIKTGDFPWZEJsM4Z4tDMBBcMD5iUtjqdhKkdLN349ZLd+0edcWff4NNp6N4AGr1+ lp0zzKE1wY7JPh3/ExY8aA2QsJ96xzkyWsRRQdd3xl8k5wozHdaAueQXZchWMSU8Rqzn 7OzacOU4NuySEWUOqsdK8oUtPFLEV0XjuLiNiOa7Uy2/sp4/duJDCr4R9gL+RwoAoo33 Z8LEFCx1y3aRCgyfBjrEZsmsEQ0tRBYrBtFHc7M1SqIOPR0EVPWC/V51nUPrTKsxrvvo qdyg== X-Forwarded-Encrypted: i=1; AJvYcCU7SWE8oaVpsjGWvooWz2oXWckAApBs4pQ7GDVNF3E6Xm7l61sIjzsSQ0nOzq+wNpyHdVerrmmZoCjYP+s=@vger.kernel.org X-Gm-Message-State: AOJu0YydHYK35aZqlQo/BZf4s3Y/suvyojQQUKYKJN0yU4QGHcpz8tB2 WF8MshLmolo53szfuaO0BFqFir2Z5jtEDUgcd1hNSzD8Tm5FxsdUmerMeip7Ig0d7m1yeSd5BGY 9imeo3zlyRKwc0g== X-Google-Smtp-Source: AGHT+IHe21novGmBRhAc1cgxeZ/rnzGM0+Bl4ZHLaMVY4DaNNS190O+pXlPP0izBlxfFWOHjeHfq+Qv3y1Z6Gcw= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:6d48:b0:426:6f38:8990 with SMTP id 5b1f17b1804b1-429dd2647eemr40315e9.3.1723622776781; Wed, 14 Aug 2024 01:06:16 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:27 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5333; i=aliceryhl@google.com; h=from:subject:message-id; bh=luJNd/MfagCpX5xgyCXvBHu7a6jvo5DfxROIQE96O0Y=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVgRWbQqHhnoystThILzkwOTzoEfhLP4TvEn eYN+ni2Hs6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlYAAKCRAEWL7uWMY5 RqvPD/4otcbv3KqvSf+EDcftbS+DTlKRGlweDQfZ8U43+xbUiA3KDeUIYjbqeGHVCnfiJYIVnLZ LM5ZtEcWXy82qFs7cEqgTbk0xU+k8w/8ouMAYI87T0w7dlR+5577YbQs7heiKTOuP4FQpEuYk2l 9svkv0ooW39NFki47X820faJdT/bruKKmymHBm31zhjXuVQ4rKGiGpFNgqhLHUdDoOS4hw4Wwow 88VrVkMdSevmKHkQFq8FlLXy2TOKAPJwMw7TxqeNyEjix8Ii+y//ZRfpZdCsKNmoLK1id+FeDoy Nh1+DQsuIjOt2HljYpfkiOGkgrhbnduyBlKAweL0XGwWHrGzP4UJ//aIyOKaK6opwuR7nv31dwJ 3r0BaYjfP22xz+WtRbORi6kEBTK2mqvK5K8BhyIVvxFCggS+8Lk6gv6k/3k46hPZfynrh5knrOV zGBbXZ7VDfERoYkJo1tfF2auCIk/nNpiOyHaeIkEuMawMzdSDeRDldHlB9QRpAPJ4QnO7m8FR3g wC3HUD7nY2ogltx6brpu4OiuXZS7M6VTyh3NEVcTYMcY3mHN0Kh5GaG0idglhcMDXFOwgVbPVIr iLlclJAEEEDO97nJIvhP7hBn1pnUB3NrhfxKuTrJfnv51N4zSWg7xle+kB5Tp9kBOaSAtyOuQdg ydxJrHCFQSvXgcQ== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-8-f5f5e8075da0@google.com> Subject: [PATCH v5 08/10] rust: list: add cursor From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The cursor is very similar to the list iterator, but it has one important feature that the iterator doesn't: it can be used to remove items from the linked list. This feature cannot be added to the iterator because the references you get from the iterator are considered borrows of the original list, rather than borrows of the iterator. This means that there's no way to prevent code like this: let item =3D iter.next(); iter.remove(); use(item); If `iter` was a cursor instead of an iterator, then `item` will be considered a borrow of `iter`. Since `remove` destroys `iter`, this means that the borrow-checker will prevent uses of `item` after the call to `remove`. So there is a trade-off between supporting use in traditional for loops, and supporting removal of elements as you iterate. Iterators and cursors represents two different choices on that spectrum. Rust Binder needs cursors for the list of death notifications that a process is currently handling. When userspace tells Binder that it has finished processing the death notification, Binder will iterate the list to search for the relevant item and remove it. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 82 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 0d680156b8b1..904cfa454dff 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -440,6 +440,20 @@ pub fn push_all_back(&mut self, other: &mut List) { other.first =3D ptr::null_mut(); } =20 + /// Returns a cursor to the first element of the list. + /// + /// If the list is empty, this returns `None`. + pub fn cursor_front(&mut self) -> Option> { + if self.first.is_null() { + None + } else { + Some(Cursor { + current: self.first, + list: self, + }) + } + } + /// Creates an iterator over the list. pub fn iter(&self) -> Iter<'_, T, ID> { // INVARIANT: If the list is empty, both pointers are null. Otherw= ise, both pointers point @@ -514,6 +528,74 @@ fn next(&mut self) -> Option> { } } =20 +/// A cursor into a [`List`]. +/// +/// # Invariants +/// +/// The `current` pointer points a value in `list`. +pub struct Cursor<'a, T: ?Sized + ListItem, const ID: u64 =3D 0> { + current: *mut ListLinksFields, + list: &'a mut List, +} + +impl<'a, T: ?Sized + ListItem, const ID: u64> Cursor<'a, T, ID> { + /// Access the current element of this cursor. + pub fn current(&self) -> ArcBorrow<'_, T> { + // SAFETY: The `current` pointer points a value in the list. + let me =3D unsafe { T::view_value(ListLinks::from_fields(self.curr= ent)) }; + // SAFETY: + // * All values in a list are stored in an `Arc`. + // * The value cannot be removed from the list for the duration of= the lifetime annotated + // on the returned `ArcBorrow`, because removing it from the lis= t would require mutable + // access to the cursor or the list. However, the `ArcBorrow` ho= lds an immutable borrow + // on the cursor, which in turn holds a mutable borrow on the li= st, so any such + // mutable access requires first releasing the immutable borrow = on the cursor. + // * Values in a list never have a `UniqueArc` reference, because = the list has a `ListArc` + // reference, and `UniqueArc` references must be unique. + unsafe { ArcBorrow::from_raw(me) } + } + + /// Move the cursor to the next element. + pub fn next(self) -> Option> { + // SAFETY: The `current` field is always in a list. + let next =3D unsafe { (*self.current).next }; + + if next =3D=3D self.list.first { + None + } else { + // INVARIANT: Since `self.current` is in the `list`, its `next= ` pointer is also in the + // `list`. + Some(Cursor { + current: next, + list: self.list, + }) + } + } + + /// Move the cursor to the previous element. + pub fn prev(self) -> Option> { + // SAFETY: The `current` field is always in a list. + let prev =3D unsafe { (*self.current).prev }; + + if self.current =3D=3D self.list.first { + None + } else { + // INVARIANT: Since `self.current` is in the `list`, its `prev= ` pointer is also in the + // `list`. + Some(Cursor { + current: prev, + list: self.list, + }) + } + } + + /// Remove the current element from the list. + pub fn remove(self) -> ListArc { + // SAFETY: The `current` pointer always points at a member of the = list. + unsafe { self.list.remove_internal(self.current) } + } +} + impl<'a, T: ?Sized + ListItem, const ID: u64> FusedIterator for Iter<'= a, T, ID> {} =20 impl<'a, T: ?Sized + ListItem, const ID: u64> IntoIterator for &'a Lis= t { --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 BB234149C76 for ; Wed, 14 Aug 2024 08:06:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622782; cv=none; b=muOKHXXtEQLlrCE5y97uV3KYTizxS/J7C3A8HPkI5gWNnKg96JyDvOy2uTgNtFjk0ypXQ+7l/Z0NSQQJqj6skG+FXPc87porgz2wOfNNz33C8oeYggkYaB5sIJzi0BWbOLq6HFdUrmBQdA0kSrhg2qCh3X0Gbbv2LQmF1cITAg0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622782; c=relaxed/simple; bh=qxOnt3B/il49VIsNhZQiolE1dcglvPZWrKlXJi+q0nY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rZYLc9sCE6MDsXl4I1P7j/60AIk4fLBDdQdPn76TUiIBI71/BZtl//ZLJl87LVNdOeAbyAezmmzuvoCXP7G9jJHquDyLIzs0IcAlo2LjSSLHL3ePTEhDEOHIla1K+db8RSQ7EQJ7PYfHHYPcH8bpUmeR7PbT8lBAN+12/mwHg4U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TIY+w6D/; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TIY+w6D/" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-36835f6ebdcso3941858f8f.1 for ; Wed, 14 Aug 2024 01:06:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622779; x=1724227579; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R7kWCK2VoUqBufThd68FvbHXMOxTe06YHLnYmz4zMnA=; b=TIY+w6D/5S7UsIGVtCW7YVQaoLzbGrnU83eC4WU+qTWh1Ypk5oRowmevAsq1EDNQar 1YRxiOWqqwlZwkrFVVTrFBUMpdCMuxsgkj4/CK7hPHjWLRvTxBnkMd/woIdyBd/MjzA3 VZf7cQUMjq05ZJpT1YxeGvL5BKE74BYb6LpXduVkmROAU0wsIT760RMGLftt5vSlvnQs 9dY7cN/UK0a0TY7uW1ozH1K4MjSlvB1yCDEgjPrVATt5B6NolVgBJXqqfkuToobUyS6Q RY2EmgMSgLAI3vHpFaZRFntOV8zJbPCf7rekGLe6I9RYFWdStQvao9rlxo6KsZGJutaT R2/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622779; x=1724227579; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R7kWCK2VoUqBufThd68FvbHXMOxTe06YHLnYmz4zMnA=; b=KkGHr3DKzQpH5e7dz2xQJQpd37Bkthc7SfkHhxNBnFYQ0ZGNAXm70SZgvdq67e1eQV 1rj3HPDLtZj5SwVyE81wfokvAwGRLfr8tES/FRKmWHi3nWvBfwsTBwRtw8NWTmrrbEuK VXbUsG68HsSwyH0iq3GfOB7pFI6O2kw55d4jPB6IJMd/Or5m6eYJiLMI1JnXxBUd1m+v eS1BIleIFAyKCoyb5qnsPfypTPeKMt96z4nwXNcXjVqW9pjcCJU9IqPphYvvF05yXuTJ UOnY/4M4+W7yekXH68/almf2Awy2okH6BHzETE4SXumUg0VcR1UnHAMiQxWT7C6QGzB8 rkKQ== X-Forwarded-Encrypted: i=1; AJvYcCWWf75ZhxsIYlplGvahC6Oet8XaccFUAxGRczi37y2iaFV9Gw5E9S+HXmVYwAs0se3A8YtoeP6ctacjtXzM9gXFR8t0GFIAcZXGcwZB X-Gm-Message-State: AOJu0YzK9jCZ/9cU1Dy/+C9/hPLyDostLiiOs8fiE1/d6uZcAX/1cIeZ fcb3X0BIBRyiWl15va0CZawas9dcziKjTuTV6C3e2UKA0Vr23QLUONFsHgJgJjBlwx4T5np6yba 4qQzXzVkraHiuLA== X-Google-Smtp-Source: AGHT+IE4208iwPdBYeJ4g5leQlxcXm00lPjnY9qa7sTcRG5r4C4Jrl4dAcjUyPqiyJm8D4xWCsdgG3jwDo3D2nY= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5d:6dc7:0:b0:368:4b3e:e35b with SMTP id ffacd0b85a97d-371777fb0bemr5797f8f.7.1723622779081; Wed, 14 Aug 2024 01:06:19 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:28 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=11971; i=aliceryhl@google.com; h=from:subject:message-id; bh=qxOnt3B/il49VIsNhZQiolE1dcglvPZWrKlXJi+q0nY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVhkzlmRyvElrNUcjrIS9ObnnPVV1o0AKRz/ GZYJS0anfSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlYQAKCRAEWL7uWMY5 RuZmEACRb28wZ56Q3QxZLYMrvcLyCi+jG1EvBr70Qr16IiAAMweAbDPAfDmN+fpK4g8FyjrGhsk lcrBi4RkI0n2ztbw7bao2aKmzm7pymIpcb1h+OUWRrHn3j3PopocAz75jP/rIaL0n8XdTqErjGE A1Im8XBBVZ1m+V725pbFMlGU++8gnUV1yTnrBmgaR3zZtOxFkuAE2cKk8afMWKVUxQAdy+y3dG+ pJbrjmARNjUF5nt/OonK6vOSqg/RAKu/06RjecfAbGehafIl6mKuhjRt7c4Am/dTq6J1kvINvg2 V2EfuhVLpUxwK+58mMTW8g/NKTbfEJu7g+Wpp0GcPTy/ghw5gRbUrW/QSwbQfJ9fX8F24zSs4t2 IzJ17RHykpvO3i4dlf4c82hsUG1gmR1zMsTHajEcruosW4zxr8+BomHn/SUH+CYgSnJts2/zVKI o1oY6mk+twsRXCdMEfIjRimv4ay13qsc2QIYNF9QPLFOsf8qdeu7R3u7KIZcdzoCzgXLLJobdy5 bxqpEjcsZav3JIEMUTyTDOv73G/n9vWFHJXaoB2LXvyYVvlz/hTLbVVDMJlWnp0XtVJrJnSIS3P qrQofeRfGiTg4oID6JE5G7Ki/KmJ4U1KFTinTmw+a0uGXI2JsQ+PMUQ4+s9yFroWktgs4bzfeCD AUcnknh3tOfCa6A== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-9-f5f5e8075da0@google.com> Subject: [PATCH v5 09/10] rust: list: support heterogeneous lists From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Support linked lists that can hold many different structs at once. This is generally done using trait objects. The main challenge is figuring what the struct is given only a pointer to the ListLinks. We do this by storing a pointer to the struct next to the ListLinks field. The container_of operation will then just read that pointer. When the type is a trait object, that pointer will be a fat pointer whose metadata is a vtable that tells you what kind of struct it is. Heterogeneous lists are heavily used by Rust Binder. There are a lot of so-called todo lists containing various events that need to be delivered to userspace next time userspace calls into the driver. And there are quite a few different todo item types: incoming transaction, changes to refcounts, death notifications, and more. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 47 +++++++++++- rust/kernel/list/impl_list_item_mod.rs | 131 +++++++++++++++++++++++++++++= ++++ 2 files changed, 177 insertions(+), 1 deletion(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 904cfa454dff..8946c6c92521 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -12,7 +12,9 @@ use core::ptr; =20 mod impl_list_item_mod; -pub use self::impl_list_item_mod::{impl_has_list_links, impl_list_item, Ha= sListLinks}; +pub use self::impl_list_item_mod::{ + impl_has_list_links, impl_has_list_links_self_ptr, impl_list_item, Has= ListLinks, HasSelfPtr, +}; =20 mod arc; pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; @@ -183,6 +185,49 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *mu= t Self { } } =20 +/// Similar to [`ListLinks`], but also contains a pointer to the full valu= e. +/// +/// This type can be used instead of [`ListLinks`] to support lists with t= rait objects. +#[repr(C)] +pub struct ListLinksSelfPtr { + /// The `ListLinks` field inside this value. + /// + /// This is public so that it can be used with `impl_has_list_links!`. + pub inner: ListLinks, + // UnsafeCell is not enough here because we use `Opaque::uninit` as a = dummy value, and + // `ptr::null()` doesn't work for `T: ?Sized`. + self_ptr: Opaque<*const T>, +} + +// SAFETY: The fields of a ListLinksSelfPtr can be moved across thread bou= ndaries. +unsafe impl Send for ListLinksSelfPtr {} +// SAFETY: The type is opaque so immutable references to a ListLinksSelfPt= r are useless. Therefore, +// it's okay to have immutable access to a ListLinks from several threads = at once. +// +// Note that `inner` being a public field does not prevent this type from = being opaque, since +// `inner` is a opaque type. +unsafe impl Sync for ListLinksSelfPtr {} + +impl ListLinksSelfPtr { + /// The offset from the [`ListLinks`] to the self pointer field. + pub const LIST_LINKS_SELF_PTR_OFFSET: usize =3D core::mem::offset_of!(= Self, self_ptr); + + /// Creates a new initializer for this type. + pub fn new() -> impl PinInit { + // INVARIANT: Pin-init initializers can't be used on an existing `= Arc`, so this value will + // not be constructed in an `Arc` that already has a `ListArc`. + Self { + inner: ListLinks { + inner: Opaque::new(ListLinksFields { + prev: ptr::null_mut(), + next: ptr::null_mut(), + }), + }, + self_ptr: Opaque::uninit(), + } + } +} + impl, const ID: u64> List { /// Creates a new empty list. pub const fn new() -> Self { diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl= _list_item_mod.rs index 1bcb14774aeb..a0438537cee1 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -68,6 +68,49 @@ unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $cr= ate::list::ListLinks$(<$ } pub use impl_has_list_links; =20 +/// Declares that the `ListLinks` field in this struct is inside a `Li= stLinksSelfPtr`. +/// +/// # Safety +/// +/// The `ListLinks` field of this struct at the offset `HasListLinks::OFFSET` must be +/// inside a `ListLinksSelfPtr`. +pub unsafe trait HasSelfPtr +where + Self: HasListLinks, +{ +} + +/// Implements the [`HasListLinks`] and [`HasSelfPtr`] traits for the give= n type. +#[macro_export] +macro_rules! impl_has_list_links_self_ptr { + ($(impl$({$($implarg:tt)*})? + HasSelfPtr<$item_type:ty $(, $id:tt)?> + for $self:ident $(<$($selfarg:ty),*>)? + { self.$field:ident } + )*) =3D> {$( + // SAFETY: The implementation of `raw_get_list_links` only compile= s if the field has the + // right type. + unsafe impl$(<$($implarg)*>)? $crate::list::HasSelfPtr<$item_type = $(, $id)?> for + $self $(<$($selfarg),*>)? + {} + + unsafe impl$(<$($implarg)*>)? $crate::list::HasListLinks$(<$id>)? = for + $self $(<$($selfarg),*>)? + { + const OFFSET: usize =3D ::core::mem::offset_of!(Self, $field) = as usize; + + #[inline] + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::l= ist::ListLinks$(<$id>)? { + // SAFETY: The caller promises that the pointer is not dan= gling. + let ptr: *mut $crate::list::ListLinksSelfPtr<$item_type $(= , $id)?> =3D + unsafe { ::core::ptr::addr_of_mut!((*ptr).$field) }; + ptr.cast() + } + } + )*}; +} +pub use impl_has_list_links_self_ptr; + /// Implements the [`ListItem`] trait for the given type. /// /// Requires that the type implements [`HasListLinks`]. Use the [`impl_has= _list_links!`] macro to @@ -139,5 +182,93 @@ unsafe fn post_remove(me: *mut $crate::list::ListLinks= <$num>) -> *const Self { } } )*}; + + ( + $(impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + using ListLinksSelfPtr; + })* + ) =3D> {$( + // SAFETY: See GUARANTEES comment on each method. + unsafe impl$(<$($generics)*>)? $crate::list::ListItem<$num> for $t= { + // GUARANTEES: + // This implementation of `ListItem` will not give out exclusi= ve access to the same + // `ListLinks` several times because calls to `prepare_to_inse= rt` and `post_remove` + // must alternate and exclusive access is given up when `post_= remove` is called. + // + // Other invocations of `impl_list_item!` also cannot give out= exclusive access to the + // same `ListLinks` because you can only implement `ListItem` = once for each value of + // `ID`, and the `ListLinks` fields only work with the specifi= ed `ID`. + unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::l= ist::ListLinks<$num> { + // SAFETY: The caller promises that `me` points at a valid= value of type `Self`. + let links_field =3D unsafe { >::view_links(me) }; + + let spoff =3D $crate::list::ListLinksSelfPtr::= ::LIST_LINKS_SELF_PTR_OFFSET; + // Goes via the offset as the field is private. + // + // SAFETY: The constant is equal to `offset_of!(ListLinksS= elfPtr, self_ptr)`, so + // the pointer stays in bounds of the allocation. + let self_ptr =3D unsafe { (links_field as *const u8).add(s= poff) } + as *const $crate::types::Opaque<*const Self>; + let cell_inner =3D $crate::types::Opaque::raw_get(self_ptr= ); + + // SAFETY: This value is not accessed in any other places = than `prepare_to_insert`, + // `post_remove`, or `view_value`. By the safety requireme= nts of those methods, + // none of these three methods may be called in parallel w= ith this call to + // `prepare_to_insert`, so this write will not race with a= ny other access to the + // value. + unsafe { ::core::ptr::write(cell_inner, me) }; + + links_field + } + + // GUARANTEES: + // * This returns the same pointer as `prepare_to_insert` beca= use `prepare_to_insert` + // returns the return value of `view_links`. + // * By the type invariants of `ListLinks`, the `ListLinks` ha= s two null pointers when + // this value is not in a list. + unsafe fn view_links(me: *const Self) -> *mut $crate::list::Li= stLinks<$num> { + // SAFETY: The caller promises that `me` points at a valid= value of type `Self`. + unsafe { >::raw_get_list_links(= me.cast_mut()) } + } + + // This function is also used as the implementation of `post_r= emove`, so the caller + // may choose to satisfy the safety requirements of `post_remo= ve` instead of the safety + // requirements for `view_value`. + // + // GUARANTEES: (always) + // * This returns the same pointer as the one passed to the mo= st recent call to + // `prepare_to_insert` since that call wrote that pointer to= this location. The value + // is only modified in `prepare_to_insert`, so it has not be= en modified since the + // most recent call. + // + // GUARANTEES: (only when using the `view_value` safety requir= ements) + // * The pointer remains valid until the next call to `post_re= move` because the caller + // of the most recent call to `prepare_to_insert` promised t= o retain ownership of the + // `ListArc` containing `Self` until the next call to `post_= remove`. The value cannot + // be destroyed while a `ListArc` reference exists. + unsafe fn view_value(links_field: *mut $crate::list::ListLinks= <$num>) -> *const Self { + let spoff =3D $crate::list::ListLinksSelfPtr::= ::LIST_LINKS_SELF_PTR_OFFSET; + // SAFETY: The constant is equal to `offset_of!(ListLinksS= elfPtr, self_ptr)`, so + // the pointer stays in bounds of the allocation. + let self_ptr =3D unsafe { (links_field as *const u8).add(s= poff) } + as *const ::core::cell::UnsafeCell<*const Self>; + let cell_inner =3D ::core::cell::UnsafeCell::raw_get(self_= ptr); + // SAFETY: This is not a data race, because the only funct= ion that writes to this + // value is `prepare_to_insert`, but by the safety require= ments the + // `prepare_to_insert` method may not be called in paralle= l with `view_value` or + // `post_remove`. + unsafe { ::core::ptr::read(cell_inner) } + } + + // GUARANTEES: + // The first guarantee of `view_value` is exactly what `post_r= emove` guarantees. + unsafe fn post_remove(me: *mut $crate::list::ListLinks<$num>) = -> *const Self { + // SAFETY: This specific implementation of `view_value` al= lows the caller to + // promise the safety requirements of `post_remove` instea= d of the safety + // requirements for `view_value`. + unsafe { >::view_valu= e(me) } + } + } + )*}; } pub use impl_list_item; --=20 2.46.0.76.ge559c4bf1a-goog From nobody Thu Sep 19 02:02:50 2024 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 66528149C6E for ; Wed, 14 Aug 2024 08:06:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622785; cv=none; b=U3hS2xNoCuVo2rg4fkCVHF/i0Qm4DPoOtD4wyq3DeKSjd/03SyeP5UvF1o2SJg5FNkAMxVaqmVwQTKf8a/OzK8dQ+sFFMTwEm3QvEncf7E5OdQp53yaWUdBLrAapDPxvli6gCqg7YjPNsssKlY6Xa5TtazKGdF2XjDkL5CYaL5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723622785; c=relaxed/simple; bh=23htzdPkrIBuzSbwmb2DpGXd+teL3V6vJsnxeIFBMn0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GaYZ78WQXX5uTQoI4XId0+Pb5cG16rT83xD+RxSo6b9+KUR/4ktMDIki2SWWY7cEQE21TO9DiGRcGjrmvqcgO4oeiW9IPueAifMcy/GKSZ00KT5TXTW2H2/DyQRLjQKSmBivH+xqPyJlrb76ogV5x83ouWssJ9/tAImWgg4XYOw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CsEG1bub; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CsEG1bub" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3688010b3bfso4255062f8f.3 for ; Wed, 14 Aug 2024 01:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723622782; x=1724227582; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G+5F6LU/OcA7Gz9zYCaBXloVPCpdqosC0u282L4bOXQ=; b=CsEG1bublEo1bjEs7WT4oK1nYFhwIQaImeoqu6spRZBmiNvENT/NBTCzKXGbGlmlb8 wUlg3TD+Ss3OEQoM+t2VPmEXD4pTOFxNGiiABZKsyjj0AYBsmnCPDIkiPYEATE1wUWJx Qeazveol5v3A8X6zU98lk2ogfckk1ZTVsy33Iu2Q61rjERBYIxC7BErucB9HRC29JvVc VKNL2VFJPQxTd0vsYjRdx0N2vjcL+RwKODhakU4iwA92db5Nfx8Lu5hyd63tXgQOWNRG D/ur1cE7+syFkvccwQUah8f3VWKTI8RXGOKbJkpPUvE2FNYgZ5MJ/P+FFvv6rXfa5Mr0 N9uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723622782; x=1724227582; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G+5F6LU/OcA7Gz9zYCaBXloVPCpdqosC0u282L4bOXQ=; b=mv1AgeC9EXsRKEsR0TfWO5k8B5vd3aDmEkyRKOV/hWdRl51s7n8ZRMV5bOzK7SEn5Y GR8RLvKRuDQLN5/f7LMqRKJAwLB45cG93t0WIvfFxQyl2RlfFaRDVW/G1Q3apcuPDjoW rvC9dOos75K/0brFXi8eoyRvEpgHC90E/ChiWc9Jt8QccKB6DfRTAA/+KRea0ejnOM5q 5a6RjdFr/HJfCu4S5b9eOvZe0zEvM/L6QWN8JLQdMxuwcgoqxa+xoLeIbAIOFrwvjDzS 84F4CkkQ7CSjYSEOnGKsCFHVmDe9GhSp7NNo0EXPtFsDxaqLmK8M+LOw7Sb10erkJ7Zd /pYQ== X-Forwarded-Encrypted: i=1; AJvYcCWlqicMVDo1o2sk2+Q0d48LRA7gjENR0FSWaUGnfB9jKxIB4gCdAaA7CuSrBunmYORFsfp4K8Ic3NbvUVU=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5Lq8T4mZFxEayRrYmQw50Zuy6e4JNNv21huVkTWmXW4s9M+EZ o2JZy7Gmc8qBj5mhFlxpqNyOyTUul+NuYkhaZC6yb3xWm91et/s9I0Y/R23JVaTUHCfHr236iDS ZEVswd+7SEjE1nA== X-Google-Smtp-Source: AGHT+IG4+uzs9JFoN8ApOz1EGDBkax7+lwA1i+qa4gBdfEeEUziZccMcI64ZxfWnhlBLnsVsnpcXULhDkh4UkjM= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:adf:eb01:0:b0:367:96b9:d06f with SMTP id ffacd0b85a97d-37177828450mr4127f8f.11.1723622781382; Wed, 14 Aug 2024 01:06:21 -0700 (PDT) Date: Wed, 14 Aug 2024 08:05:29 +0000 In-Reply-To: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240814-linked-list-v5-0-f5f5e8075da0@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5361; i=aliceryhl@google.com; h=from:subject:message-id; bh=23htzdPkrIBuzSbwmb2DpGXd+teL3V6vJsnxeIFBMn0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmvGVh4B7ecG/0WoAj9SNsZ3AW95MJ+LAZH4Xw+ vx3fBmV8J+JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZrxlYQAKCRAEWL7uWMY5 Rj3uD/46bS2kk5jHziMtHpjUpKC2bjCl21bUHG373S8vxIE7H6O66RoyceGwjifV8eR8UPKyobI Jki1fSlFhS8ZTh9AUlVYCgsqJ027XiWqJJJQYwYwa/dS/ocBpqo/AVGxrV/NdhaMaTV+Xa3U2sH krtuUk4l0MuCQ2oFeqSjHbc0QiGkaogGeXSC4VluG6ajqqci92McNgVkCsolgpV643VoSFGqS43 iD5Tqp3l07B8DgS2OVte0Bu1WINvFkoscJKSN/RhEkqqSTCuFGNfQ4whE1BfYvL2sXXtDxyLMrr UnW+XWCALEU3uBTqy7CEYXmKUxSUc43I9j999kt/fP5Ed6IzUUAB4e+Tq79gmK8GPQMQE/zKsUT 3O9K4+8hlR63A0lAptwajlfXOFShK/o1lEKb0sJ3HHOYaTVW6sFVB7gmO2cmTztuZsQ5xYOPsIA iKV+jgQD/qBwzhX7q40RYhFHaZUj7zcQflw5aevYB8OVH2CWPD6sL4QClxtlVs1bEypXeDPr4QY yifwiu1SvfR3XUX2YjnGQl5vFGDBbXJVuN777gPb8UVFeO3NLu7xyOI71Q/MzMSDC/JKUJMUJqD PrnSyIyr4byQ+5w7pVFaXO1cMxfdXAWNh+y2HgPMwzu9chRUibgPXwu+90PUf3YUzOm/SaKYtFQ hFPDjShEvHF9HOA== X-Mailer: b4 0.13.0 Message-ID: <20240814-linked-list-v5-10-f5f5e8075da0@google.com> Subject: [PATCH v5 10/10] rust: list: add ListArcField From: Alice Ryhl To: Miguel Ojeda , Andrew Morton Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Marco Elver , Coly Li , Paolo Abeni , Pierre Gondois , Ingo Molnar , Jakub Kicinski , Wei Yang , Matthew Wilcox , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Kees Cook Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable One way to explain what `ListArc` does is that it controls exclusive access to the prev/next pointer field in a refcounted object. The feature of having a special reference to a refcounted object with exclusive access to specific fields is useful for other things, so provide a general utility for that. This is used by Rust Binder to keep track of which processes have a reference to a given node. This involves an object for each process/node pair, that is referenced by both the process and the node. For some fields in this object, only the process's reference needs to access them (and it needs mutable access), so Binder uses a ListArc to give the process's reference exclusive access. Reviewed-by: Benno Lossin Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 3 ++ rust/kernel/list/arc_field.rs | 96 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 99 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 8946c6c92521..88fbf70bdf4a 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -19,6 +19,9 @@ mod arc; pub use self::arc::{impl_list_arc_safe, AtomicTracker, ListArc, ListArcSaf= e, TryNewListArc}; =20 +mod arc_field; +pub use self::arc_field::{define_list_arc_field_getter, ListArcField}; + /// A linked list. /// /// All elements in this linked list will be [`ListArc`] references to the= value. Since a value can diff --git a/rust/kernel/list/arc_field.rs b/rust/kernel/list/arc_field.rs new file mode 100644 index 000000000000..2330f673427a --- /dev/null +++ b/rust/kernel/list/arc_field.rs @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! A field that is exclusively owned by a [`ListArc`]. +//! +//! This can be used to have reference counted struct where one of the ref= erence counted pointers +//! has exclusive access to a field of the struct. +//! +//! [`ListArc`]: crate::list::ListArc + +use core::cell::UnsafeCell; + +/// A field owned by a specific [`ListArc`]. +/// +/// [`ListArc`]: crate::list::ListArc +pub struct ListArcField { + value: UnsafeCell, +} + +// SAFETY: If the inner type is thread-safe, then it's also okay for `List= Arc` to be thread-safe. +unsafe impl Send for ListArcField {} +// SAFETY: If the inner type is thread-safe, then it's also okay for `List= Arc` to be thread-safe. +unsafe impl Sync for ListArcField {} + +impl ListArcField { + /// Creates a new `ListArcField`. + pub fn new(value: T) -> Self { + Self { + value: UnsafeCell::new(value), + } + } + + /// Access the value when we have exclusive access to the `ListArcFiel= d`. + /// + /// This allows access to the field using an `UniqueArc` instead of a = `ListArc`. + pub fn get_mut(&mut self) -> &mut T { + self.value.get_mut() + } + + /// Unsafely assert that you have shared access to the `ListArc` for t= his field. + /// + /// # Safety + /// + /// The caller must have shared access to the `ListArc` containing= the struct with this + /// field for the duration of the returned reference. + pub unsafe fn assert_ref(&self) -> &T { + // SAFETY: The caller has shared access to the `ListArc`, so they = also have shared access + // to this field. + unsafe { &*self.value.get() } + } + + /// Unsafely assert that you have mutable access to the `ListArc` for = this field. + /// + /// # Safety + /// + /// The caller must have mutable access to the `ListArc` containin= g the struct with this + /// field for the duration of the returned reference. + #[allow(clippy::mut_from_ref)] + pub unsafe fn assert_mut(&self) -> &mut T { + // SAFETY: The caller has exclusive access to the `ListArc`, so th= ey also have exclusive + // access to this field. + unsafe { &mut *self.value.get() } + } +} + +/// Defines getters for a [`ListArcField`]. +#[macro_export] +macro_rules! define_list_arc_field_getter { + ($pub:vis fn $name:ident(&self $(<$id:tt>)?) -> &$typ:ty { $field:iden= t } + $($rest:tt)* + ) =3D> { + $pub fn $name<'a>(self: &'a $crate::list::ListArc)= -> &'a $typ { + let field =3D &(&**self).$field; + // SAFETY: We have a shared reference to the `ListArc`. + unsafe { $crate::list::ListArcField::<$typ $(, $id)?>::assert_= ref(field) } + } + + $crate::list::define_list_arc_field_getter!($($rest)*); + }; + + ($pub:vis fn $name:ident(&mut self $(<$id:tt>)?) -> &mut $typ:ty { $fi= eld:ident } + $($rest:tt)* + ) =3D> { + $pub fn $name<'a>(self: &'a mut $crate::list::ListArc) -> &'a mut $typ { + let field =3D &(&**self).$field; + // SAFETY: We have a mutable reference to the `ListArc`. + unsafe { $crate::list::ListArcField::<$typ $(, $id)?>::assert_= mut(field) } + } + + $crate::list::define_list_arc_field_getter!($($rest)*); + }; + + () =3D> {}; +} +pub use define_list_arc_field_getter; --=20 2.46.0.76.ge559c4bf1a-goog