From nobody Mon Sep 16 19:29:06 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 CE69B14BF90 for ; Tue, 23 Jul 2024 08:22:55 +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=1721722977; cv=none; b=MOwShGDD5fN+9dw6McUCSBzuMLMiUcyjPOVWsTajtbOHMUJa/Tu1Y2nV0M51WiX90wuWi0UWaC75MVpsAhdPFmjFJxyt/3OeVzn0dhW9BuBpPVgctd6I49+yMw6EO8PNYIcOGx6HWPLi106i9XcpbplMOdy3ikgxsTLFo9av96Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722977; c=relaxed/simple; bh=B29ZNDqiZR7gkJQ9AeTvzvGahFyz/8UuN8mfE9HDzks=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HbP4vlF60Z0Z70WEWzdtdSw/LL/4yJkbzA3Fe/lNxHsw6JqpPokrELhGP4I7SokLKCVlwozdxrztuGBqN3eQNezjhtGhcvD2enNekY5jCpqNm3486DS/hRBWm+W08VVmzaxeFwMWJ339zRyFlxb8oYlYPDcliq5JP99V8ecoKMA= 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=FBSOxPWJ; 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="FBSOxPWJ" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e05f08adcacso11918293276.0 for ; Tue, 23 Jul 2024 01:22:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722975; x=1722327775; 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=lKAgE1hhMontLqvxWta5AF2oEi2tCqWpiTS25A319OY=; b=FBSOxPWJFx5gBq3UgqvBSKR4uJK8stoRKFuWKdIkIhnxdn9Ae5TzidojOuobSF1Jff H4cIhjXCp4S0TIpBqVkr9VkqjcH2Kb0QbNmbqEuW/GazIAK59wqf4VpYrbVwqdDhmaxO sXIAMKro3St1VgQqYlEZ6LJ6vOIzlAnSitlhrDMysNpI+CL7H1RWhfVcBnftEg3kSOXn 5+Hqfx7w2frETtaCkGKafOU+amQVE3ateUDNo1uAYmrfqkwhIML91omO+ObnvpO27dVT Kr4VJPdeFSKqc9YU2l1kLc6UyTg6b8LBXqLra9cxerMMqkJ0tk9Gz/Qim7eVv+/LJXWq HmVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722975; x=1722327775; 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=lKAgE1hhMontLqvxWta5AF2oEi2tCqWpiTS25A319OY=; b=onEXUa7pd61D4Qo/hgq6pbYLcHCwe/Ra8apWQBg+SkfibytNCtro5tpbbrxHIbjJlK 3ngyNohQ/bPgy5ldIUD+zv43GpWuDkH4v9z6fkrpuQ4f7YLjCzM1gfhJG4ZOZE9GNi0d cZi7Yi1AMDXu2qgGqwBPEE29Mv9s0em8wd3knJ4CThSa2aSkiZ0h0FdoDN9LPb4a+n4k 3+qnRY5eDGYGgfAYOONrTHBmJgu4Xb4R9VND0lF10z0eUsnAYY9Olc/xIOmJ/vonF5LN n8OYBwPqGgk4Rcpc9/kbHSdPxXR9qwSB/C/FDM+Tz5Ud5HuF+NUFQZIRE9QVvYLfEb9y Oapg== X-Forwarded-Encrypted: i=1; AJvYcCUDO3rZULXPa8giHoCvpyz9TdbMbHnJxvAJ0uRH5qb8btGQ85aeBdtudaNzEnn61g9H2Wx8kj2iAgsqCVixlW7Bqq2sxguU4gL3CO8L X-Gm-Message-State: AOJu0YxUwGnc/Ri9cN15+KZ3OXx6gLbQRiOT5e4bQF9duf7eazJwsaHb mOlJ99sMN9Gw296mhg3QiAHX6s6SlxeJcNnRwB+SgXTkEolNnSQxbldCOCG2VnvG4pZoURX9/wL zc1g9r+AXFr2c1A== X-Google-Smtp-Source: AGHT+IEPINv4EyyAutJzjmDpLTXjoGyuMMGsDbpKeKe2iRgOfc3gmLVck9mRZfAe69EaSfXr4Xnx3uFWG2YDSTo= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:2b0b:b0:e05:fc91:8935 with SMTP id 3f1490d57ef6-e086fe37bf7mr190993276.3.1721722974741; Tue, 23 Jul 2024 01:22:54 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:02 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4062; i=aliceryhl@google.com; h=from:subject:message-id; bh=uoexIs4rgA1q9vaVNzcAaNZWtRqNFGCqT9huoPU1gnU=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hSWfpKeIay2gf7V/WLK01LoD4w/NCj2wLF3 TM4eb/12RaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oUgAKCRAEWL7uWMY5 RrNED/9MqXg9v5Q9dDnwmwnmusgWlitLIDaV4mXddxlOJVQCHBAVC8JMkRYQ8+N22Kwiias5s5y waRuM8BojBW69i6LYstiTwfj04xykuESpeG4u8+UrVnOWnvkjHlEaQ83DIU7hxn0LaVI09SbRvA cCMAL0p6arOlt5Hk5y0vrZx3pFd2CEH1tmPM6naLLEMP0vUBuQ2j+xM8hjLSuiRGzllyPE6uqOm Onwz7yMwh96JIZOM3gUTqY0HDbG1x1Xuz5yYbgWIo48lYuQqloPTTdlY0OD2R2WQjf98W8jy3mz kVUjuq5BssP9EbwKEHUAXIuLb9Wlf4n81nHLV4pb+jgElDjwJ6MXj43VrY6vOmtp6qZi1wkYoxd OeK6boGbN30FTAxCxb2ZqVTb6603RiRgBSRRp0LqAsz0s1sYXbMe7X6VsnBgHEMMAd+FbmPwjuM N1WJGt45irvRWR23Vs4hba5uXG8UcCRtxdURQ9/5VIT+MoMhABFDlzKTBggIUr2cgpL8h5Uwy+y qXJg6hpT1CYwdlXz6EpokPbXi381LaX1OFv0wptWcCiS/v3xP+nbctzNSJJRxUbuW5kXO0eHB/s tcw4ZH4yRVwrr0As/YmdOBOnUnTUpCOQgM0ANgkGVn1Gkc46ZOefbXZ9vmUpoBuWANAlVQn/z/o hUR5RSxzsS9VyYg== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-1-89db92c7dbf4@google.com> Subject: [PATCH v3 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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.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 95E0314D2BF for ; Tue, 23 Jul 2024 08:22:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722981; cv=none; b=A2AhW0732YHTPPo0Afng4T+FjPmppOONJX7fhl05nJh27Yg56A6Gu6co0t038HFeoou630Fy4ztsqF16AxcVjWuMOVHUJA+wQop4AgEUIzE8+cfgqjUIJ2rsIFxJqD6XSNLxE8zNEuTT4UTGSvwgfyiTJQ+ByW1vlh3bx9dEV94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722981; c=relaxed/simple; bh=7QBxj3G7eNnMrpH1QwSGuLbaH+rnOpkyLTVPVID9iqk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RQ9lwj2w8CYfKFF7ACYdCIY/yz+97eQE+3bzTTaqLzfZpzm/zZBV3IysjWoPsV1Pel1gz5673M2hXzxpUZMMZukQflNLwwq/M7Q57NZf8DBeMDhr30nUfhKvP5mITOku65NqrufiBdKt4wR2geJFjgjI5a8ISrr6mtENLjuG+/E= 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=bhxu9ZOD; arc=none smtp.client-ip=209.85.208.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="bhxu9ZOD" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2ef286cf0e8so23517001fa.0 for ; Tue, 23 Jul 2024 01:22:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722978; x=1722327778; 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=hLYAKBvuWpY5x5DnpD4W5b5CYD2QiwkREiBpRLQtnks=; b=bhxu9ZOD/yzGSZK/EeKgRQNRbU2EjwrfM02JWwsS8ahHU6qNVjEEw3hPDP+5tXWDTk /SHtbtN7bpSAxpPD1wyONSu/bI14pQpRAPlaTPVMTLxO+eVX3Gu6eDA2NGRCnQmYdjq6 kBmUWYUOZBIiwLo96dvS/RDpceynw/Gl+qgq9X3TSbbVlCvALOsU3k4jXnFK81mwPDMb 4hmmkWFhu0hClu93qINaEhKvpw06WRebROidkbPpgMFL6jdpXD2Xa+I4Sh2vQ8YL2m2Q na2oaA7B2zXJ5Z3TEISKW4qhs50/1fVs1BL1LQZkuDiHyUpM+sgHNHoFQcM52wHGqZ6+ gFMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722978; x=1722327778; 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=hLYAKBvuWpY5x5DnpD4W5b5CYD2QiwkREiBpRLQtnks=; b=jPxA03hLIcPcaqIkBw6ZwbVNKLe+FAlENnJc0TNEzr/BIEP07zyGfcWQWpp6DBt92a oXEg117P5c60HuwvgHja1r6kvQNqwPRkyVh71vpdnkb03IEfI0P5dt2nQmlp5qt0OpIv RV/dxIdrbWXF1ZaY4dqbEIXxZR3TsR3Nr0J/RuLxvko5fq6VpWG5hs0WawncrxigTD9v QjtvecTWCVnXQHX7vrlkUv3wKqxxN8F0zjKLLjcZkO1j4nXsaTCOGtB2QszArZyzY83R hf8pGoaSKH847BGeJ+xk3dZuhHw/ATLiUGmVhXXukS4rHldzPskYaDE7itqv1VyhDsro yFeg== X-Forwarded-Encrypted: i=1; AJvYcCW/VuswpzRIdI5xiKwQ497UouScmHk+oSJt/yLmGReEi94jT6ziSZsTDl17fHNvbHX87j0aUle0VSSekRfXrWpGZSPzfrSoskupgiyx X-Gm-Message-State: AOJu0Yw8uqw3IBIlPHhh7ZbKe2DkjjudYnDh0rdAJ7yVd3jA7riiUt5W dDk150mJ9sQh0gvJMCjefFUisIzPwZPc3kpOeo8M7TPczyJ467dObGgckin/JTkiZ9eHS8mYymr otLaQMGEMzA4gXA== X-Google-Smtp-Source: AGHT+IEO8SpdGUGkTq9ihGkB+gkwtcZg628hiSP5ggAEz7lfi0uu/C9yBYi6hwcIZUK/XMCX8KNa6yz+Hw5hkVY= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:9611:0:b0:2ef:2217:4fce with SMTP id 38308e7fff4ca-2ef221750c3mr102991fa.8.1721722977599; Tue, 23 Jul 2024 01:22:57 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:03 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=17666; i=aliceryhl@google.com; h=from:subject:message-id; bh=7QBxj3G7eNnMrpH1QwSGuLbaH+rnOpkyLTVPVID9iqk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hS81+hrsThMQhRbzpZBXuSAvobPcGgINWoa zN0Z3KCqNqJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oUgAKCRAEWL7uWMY5 Ro/+D/9HDwwzSvrT+5Kqj84VufeAL6EjAQmE/OLhhlRMtD7OfLzlMnKSoaNzuWYM3zn8ZOBVmKS e8uWn32DXFKnLpOk+Z7vYIjCrTl1sgJJqzqc2Y0tPbP/p87+QtAOgQgFai5iUaNrd1Ier5MZ+lV nMflp/VeH5DiLQVvaVvZw352gby5y3cMSsuY6MrsUDoeLDign5Ft55Jo7XBVNBeZOEmbO02LpNZ KcX3Op2lgb98PQWv1Yd3Q43vFwytHnVCdAItaf+VeDAMft0+BSPHZ3hVBGdehEV5P9JW/OawJZu PIUU0S/o5WCgCBcMkVZPN5ZEwnQlOWfS3IfK/dw4CSwIJj0lCT+cQ/9S6SNuxHU6NBnNUlVBfHO tNMEQTFHlat91pc1z1pkQsJHPGMdosa319H8dJeZm6o4+nU7jUuiY7b+H0OD+vHdSk4C58rrE5a zR9v9EQvY+c7rTbmQjwrbVac2Kx1SCY9hmOfLIKrbu5ZqaBZ6m59iuLgksgcm8euEJrsptVDl4g 1KuwbRAv4uNqGjp4F6UD3vQR5foOgWrC1zCnJXlLNzXRBO+MDzAq0Pk+YNhPcPPXwS4SRQeB8+L qiPk3DDcNglM44Z4GMpOxAs7TeWLdLWwNVMQ+Oc4+Q6zFcqHgjorIg7ksU8Q/1g6FAfjW9RYJ3u XtXKOBCuh+hXcuw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-2-89db92c7dbf4@google.com> Subject: [PATCH v3 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. 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. Signed-off-by: Alice Ryhl Reviewed-by: Benno Lossin --- rust/kernel/lib.rs | 1 + rust/kernel/list.rs | 8 ++ rust/kernel/list/arc.rs | 348 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 357 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 5d310e79485f..662bf6ebb770 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -33,6 +33,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..3b7072e58256 --- /dev/null +++ b/rust/kernel/list/arc.rs @@ -0,0 +1,348 @@ +// 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. +/// +/// 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. +/// +/// # 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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.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 46C9814D6E7 for ; Tue, 23 Jul 2024 08:23:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722984; cv=none; b=O5pWdBADCtQv7X6RaoSwtRIZ/EgKSSg576p6Eeb8ITXOPkqVYj1HHjxhYSCtAKrkkinEoW0UpuY0BDfLxwB+/YfFDSgbTLWBS21SowRHtrVXpysOXHRVV3DmdeqQwJS3jXD6g4gKzcaGqNTdt7m+WroV86kjk2lR6vsjbbfYXUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722984; c=relaxed/simple; bh=Y1Woy0osPcWK5Kmhig4YRqu/zdNjMvINHCw3OpMj+9o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KZk1JnApSFkgprJQkcf8RTRK7RgXxeBdw6zisd//tAswlkL+KdQSKS35PmtzE6F+nrzx9rC0eTLk3qDVBo0VwEiSQW2sQgnhj+eZ4wceuWBwofg2fSdl/ylQ47ukLEUJhtVrkhIOvGNmY0BcpXnRvozf9ESWoFUfzZYflHlaxEo= 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=h69b0fUS; arc=none smtp.client-ip=209.85.167.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="h69b0fUS" Received: by mail-lf1-f73.google.com with SMTP id 2adb3069b0e04-52efdae5be6so2738038e87.3 for ; Tue, 23 Jul 2024 01:23:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722980; x=1722327780; 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=tHDHsWSYgO+1s3W4ChtXMwSAFNaQ/mfNYkx1n2oQLCo=; b=h69b0fUSvK0UCNMLMAcJAK9R8nJaquPRnKNDMSHGA3j9mm8tTqh9oPkfvehOpXvmqJ +ULivbQ5YxLhjN/KjtQ1d8wnRCji9kHTk+/QcTF9x1vQ0eJ5Z/tTToDEix92lNVrAlvF JLGBX5plyOdKm0jSplkkVyk6wlc7V8DU7jm0sNJWrDY15pfDmtVUYe8VAfNfOUYUEYmQ bmK9B4ANKsF7LPSXlGhA0PhFUXRBDGnKb2ypdmGzrjZv81nd4v0TaelyC8CcO7tthA8r 2Uap4aQ3u8ZdSct3zLdlR0oApb7IXGf8WF701YELP7j9gy/IklNsQI+iiCbd5pEADGN3 w6jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722980; x=1722327780; 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=tHDHsWSYgO+1s3W4ChtXMwSAFNaQ/mfNYkx1n2oQLCo=; b=j69us1PX6h5OoS/c+NT8D+rSDOqNw0f5Cbeyw9QxX5Wfkh5ta8fK7yyauGOrFvG0+E q1JzqH1Jl/KODvkz7vPwuKe6lRCQU0B9A8iG9X8EAlSQdSp4l8aw7ReqLbh3Z9YBd86P w8LoFF2UjMvAQWp5w5TW/6hWsRMo+KEmdNxOIpMjt3jLcw9DjkS0NzQ3Rix82vluMYOd 4b6dTSvyIQ2T32SSK6VQmKBt2p1Nz4XZXTJV2Dm4R05gDQln5gZ61rZ0rMbdgItGB8Nc 0AU9noRDQF9c5GBtmbq8qsLBTYPPtyo7iSJ5+knNx4RZYC08j7bZNF3rQR0NcmV2kPyM MAbQ== X-Forwarded-Encrypted: i=1; AJvYcCUKXLSYa1CW20yxVNroKgW4ZMuTPeh+yZHkuk5LvKQyJy5vpgZjH/DGccHEeY2aaY3zrCgQ6PhwJGtmivaQMOHbkvCBz/LmQwh0iFiR X-Gm-Message-State: AOJu0Yy/LeN75yYnYTmjZ1/HSlbk6gg3QGYyrR7Kh+nm5KrP8n1kwj4F JpQduJCNRjpo0eEUrTdlM2xXWJtbZt6EE9GhFgGwTbJSx5tSt6iTY7nyMFkIFr3h/oF9yGmjWyQ vYKQ2zlVLFStCpQ== X-Google-Smtp-Source: AGHT+IFNKKI2GTN02KrPRt5I6wTM/q05wiWB4MWiYbz7ant2rLVvn5VAbuvtigwsIHeBNgM33lyRTIMZ15/6YBI= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a19:6a16:0:b0:52d:b097:e11b with SMTP id 2adb3069b0e04-52fc4048d80mr3071e87.4.1721722980615; Tue, 23 Jul 2024 01:23:00 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:04 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=10712; i=aliceryhl@google.com; h=from:subject:message-id; bh=Y1Woy0osPcWK5Kmhig4YRqu/zdNjMvINHCw3OpMj+9o=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hTYfhI4Q2pTIs40yDSDZFa7QSD3UpCd3i1F CVTdmlMGBSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oUwAKCRAEWL7uWMY5 RlN6D/43lUX4lRCSiaJw1cbtbwxtpS6dJzfBj1Jm5DG8PNGdjnm8/pTR6aDFmZyjQv9widpD4ik OnCRrH3hps0ldhe2hy4g8GcQEHMVmJQILMdW2k9Gu6LYYpMzfDtBJDZaP/AB/nQlgKafLFvIo/p 1kmM+kKKxRpQz7VJo/HhUS3x3pwCev711Sm4SwO2EE3MOWVd6cpdBhq3rAX+Oo7/oYOKMz0kwu1 4l5Tpzw8tU2UPmwRpPn5xSZOy1OtmDJZFdGMGl20RtS0rPA8OFWnjxwXEPzU7COWJBM6pPAEWD0 GUyDAKpge+4iVZv1hwACoG6Hgatpp++Za22sYMEflGe2zmfAyUA2r4N4MLUk0Sag+dlKp9cfRks kPquhwWISbG+grDtq1SlTJB5fPC1k3c9gMnuUrKnpP1D8OJdvw82DMXzl4+m37zXKrvgESvGhfy EHP7ofuTZZ0nYu2uXBFyaqAQhl5lGQqSD0+JDLN8ynXohknXNgDDlqycvHTSLGEBcAb24HfUujK flqiuLyntQoaoS1Y9bCcONH7J1eGA+aCBIc0omtP09pltVmg/DqpzdLCF5TUp8uEtVrN5Aaowdw kUE0OJVnGviVGDtF0n0TYQmLlg+Up8mmoKMZ2OnCpYa1MubG3HJGe9iiuknRB48jGAxuytODeIl 1kVISIjaaqC8Yww== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-3-89db92c7dbf4@google.com> Subject: [PATCH v3 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. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 4 +- rust/kernel/list/arc.rs | 162 ++++++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 163 insertions(+), 3 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index fb16ea43b2ba..c5caa0f6105c 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -5,4 +5,6 @@ //! 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, AtomicListArcTracker, ListArc, ListArcSafe, TryNew= ListArc, +}; diff --git a/rust/kernel/list/arc.rs b/rust/kernel/list/arc.rs index 3b7072e58256..97bd9d52b5cf 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. @@ -47,9 +48,30 @@ 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 +/// +/// Implementers must ensure that `try_new_list_arc` does not return `true= ` if a `ListArc` already +/// exists. +pub unsafe trait TryNewListArc: ListArcSafe { + /// Attempts to convert an `Arc` into an `ListArc`. Return= s `true` if the + /// conversion was successful. + 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`]. +/// +/// The `tracked_by` strategy is usually used by deferring to a field of t= ype +/// [`AtomicListArcTracker`]. However, it is also possible to defer the tr= acking 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> { @@ -60,6 +82,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; @@ -201,6 +256,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 the= tracking now thinks that + // a `ListArc` exists, so we can create one. + 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 the= tracking now thinks that + // a `ListArc` exists, so we can create one. + 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 @@ -346,3 +447,60 @@ 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 AtomicListArcTracker { + inner: AtomicBool, + _pin: PhantomPinned, +} + +impl AtomicListArcTracker { + /// 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 AtomicListArcTracker { + 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 AtomicListArcTracker { + 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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.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 6E25514D712 for ; Tue, 23 Jul 2024 08:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722987; cv=none; b=Jt7TtczAubrPEMF11jD+a3NTw5Co5rF+R5Vq+QeEDRZoqbPtPAJSTA/MXLRDTarTxHAX5SkviHY+p+i9vho1svkt28rS9RDblKei9bByC0K2pb/lxGTJm9FU4RPTxTATwM0A0/uULDN5OvzLO7EaeRz9tVan/PX4C91G/k/bsGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722987; c=relaxed/simple; bh=UuGb6O3+CicGgR1jFURxIdmSwe0z9XIKmsa42luz5sM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qFjpK7+PmStSwPnuqADdsxWXzTKkci/Gi4YOOqqsqMXUA6+o7lmjKVI4ALxF9mimrPLSHa6l1n0aZxwOXfYgPR+zS4Melt/Kxw3hGAPfqokWoEmJGk3Z1kbsNK6s5KZ/uJrG/AjSbKH+LzNcUr/ns8XfgBVRSodVuXbRXGI7b14= 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=RIOmf+hu; arc=none smtp.client-ip=209.85.208.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="RIOmf+hu" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2ef62acc9ffso14538791fa.3 for ; Tue, 23 Jul 2024 01:23:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722983; x=1722327783; 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=NSC5xvhbZTJxyrCczhAEUBkZy+WsSQNfzVek8UHbQHo=; b=RIOmf+hu/IR6HCto2mEbMtchb3GNZOWIJikWPUPFUZLYtU6Wcfjuhhj6vX7wtxCty2 iVwFcEWLTId6S9jK0M374RpSowWV/B0lmRyXsM9XTlP5mbMLf/LcTuI+YWkiZQ7+/wFd h+x7m2cK1yM7L7P2IaUBLoOXIXWj2wOKa6gZiMUmH6P3U50hlpglk6G9+uM4k5bYCeIk 8BH4ofjY85QXuSeiM3dWAhaOTtvU7v9ODfa59uNcdiamfkDED3MDTEvPpejYfiPPwCfu pJ1qqG+Ap3pi6yWDi8EmZCYleNZTzgAFAbxT4d6fdaSzukc6FWdyQzdNHjgVwtpc6NtO VV1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722983; x=1722327783; 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=NSC5xvhbZTJxyrCczhAEUBkZy+WsSQNfzVek8UHbQHo=; b=glpsQVYevx+OHOGhg7T6qgRU2GxuvSQvJ1i5xRbYnT51kBp255QvcBPyB1NxIocQhp J0qXmhIJ92wDwNmwaBUZ1L1pfEDXzFzm5J5z6IsH/ks64j8iOW6g4A0W4asiNYTeYF3m NdyvcS0cWfhW25EJTYf1TcT+e+nCK/tqtyLCyTeBi+VGOLS0bb6Zz0bmm3QCZd9fUeVI b4b3/ELiWVQCIfmyKMXbQr4hEq3QgSAQoBaXIzstdin4ODSXbPIYlgpIgLzejpKJ2c3d Rdc1pAyCK48HHc8iUfKThOBwD6CI1HhY0+FcIcD0CVXNOhdE96FU4VQ+jcjH6mzBOM0c 5lKg== X-Forwarded-Encrypted: i=1; AJvYcCWCihwFdMhv2hbcprT/bP2ohx3FBiSq0XVo1igkiyQW7HzIP0k0hxUqEe48t/DANqYdicght90lKbo/+009TdpMF47heMXY6qCieGsu X-Gm-Message-State: AOJu0YyJcLZWYVoj5SgkRRaah6KnWCKx+L0UPk0Dlk1wcVBPa6fIfQYa 5JO7UziTkSri7F/0zgRqELYqViJah9U8BPxvSZj6QUEnHCCDi/31pNqtnARnws7AvVIiz/BST1v JOdRmbFH3ZFzHIw== X-Google-Smtp-Source: AGHT+IGFga6vKO4UbX5LF+DHLWLeuoKVJRWbTO3jngPxrxHzKVLwzO4KrhUfVJrX9g2BGGOKNoHyrsQJW94aD2A= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:97d2:0:b0:2ef:2308:a8f8 with SMTP id 38308e7fff4ca-2f01eab8f33mr28131fa.3.1721722983398; Tue, 23 Jul 2024 01:23:03 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:05 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5924; i=aliceryhl@google.com; h=from:subject:message-id; bh=UuGb6O3+CicGgR1jFURxIdmSwe0z9XIKmsa42luz5sM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hTRbkqpsFwnVQKNEr5/qhfC38vwGN6BeSEW 5jKwfwvqxmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oUwAKCRAEWL7uWMY5 RrCgEACsfXrKLDL7MQrYr53otFLMkVld6REhp0SDSelAOrupk4IW4ZpInTkdY162j3zATfaSkMo Gg1V6jwX2E+p0LcFBuavtwwgdSB1b4WyEjsOWgCdEsIswh3etWVCtwwNq02jUif3C+HIdIoF4jb OOlYjr4rV69QDUmoILa5CPwFnn8TwgiXk96d3EpTTOu96r72iU5nfAYIKiHI64XrTNJ9DMuVCre DoLvHVxVJXBXubGFLdMpK37IVdpzInWZ0czHJWEOnZRZXkjxtnPc/gM/N8jWM0aJEH9aHOSMH7C uSnMh/ZEkObEI7QyLEMIqnha06sSOhOp1oeZviTm9/plp0esI2YVHcZFcONrgnMpqZdu0s1x4BL KKDvkAwqTlgNUzMpS5R8dTcmGo2K7mqr91eUuaN76yxlizfWPYH0H80tjDG/hOsNChsDQeHn3jJ efe4AJYN2J0QgTBoNJqalj6WIFV4LrqekbIsOECRL/3TtoL9I3ef4bj2SdV7i9fwFfYSTB9ulih X3toNgR4cz+nYqZAWROJFwpBHwv7+YQsrpENK7kdNev64CHvbPVDIzLHPCKr+4GeW0Lmm9KQ/RL a5XlwuA/2WU/nfpDaMHmMplnw46TqK56RhvHxve+Nf48shf3XNu+GDklpJy+0F2c5/JKvt4Ujs5 neibA+rAP7+4qoA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-4-89db92c7dbf4@google.com> Subject: [PATCH v3 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. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 115 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index c5caa0f6105c..63e6f6a47964 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -4,7 +4,122 @@ =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, AtomicListArcTracker, ListArc, ListArcSafe, TryNew= ListArc, }; + +/// 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 { + #[allow(dead_code)] + inner: Opaque, +} + +// SAFETY: The next/prev fields of a ListLinks can be moved across thread = boundaries. +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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.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 20F6214EC4E for ; Tue, 23 Jul 2024 08:23:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722991; cv=none; b=TBtArVkLaNw8b8jc8NNchXrX2ImrI5EnEBKQniK6VypUyqflF5SrpBizJ4Gq+eQ+WegJQiNruNVzlUFOG1OiE5PX5jVf1TqEdTKEh/aFdbKhl2Hhy9CoxerKLszW68lwTdEbSmxVDRqH4Bj5NbXoOvgu1rBoz8KCdyqAw23DRaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722991; c=relaxed/simple; bh=7s+2tpsrG5RKjrFDALseGw6Co5fhy0QWgnewSA23Pbs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OgfZZcWz3EmoOapvNMctVFH/54eY/bNEGRf+vQchni6dU+xen6GDb+cyESqzDnyQL+CDVh/wf+SoFiCCJt/upUk0AZ61ln4J7oO/8MRpChckmjgSy8o8Xbqb6/kkTikhBI4wnb0jbUfeedfbCGN/NuVzrkFDV3MwTY6YpISCZk0= 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=W6tcA2aR; arc=none smtp.client-ip=209.85.208.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="W6tcA2aR" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2ef1b9a466cso31487591fa.3 for ; Tue, 23 Jul 2024 01:23:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722987; x=1722327787; 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=MPvnMxM4cw829IqByr7iX9fTi7jPKsJ659JMi5lcAVk=; b=W6tcA2aR1Ol6nPoDRJzf09TliXToKCMxJB26Czcb1OMibzzyk7S5PAjFRf67ts9H4M Lbgv0+5iajpd6BBM+oJ7tYVi8aBPWa2K+dQV5sVHldAWsJvNXLEJq00jQ+X3rZAFcNYC o2sDwel0aE1e/5WeutFE6r2hFaOjRxgNKCAvwzgK+RAd+XhaTAs7OXwHpD75diM39FxE kC2VgLFaXcrhxNEfqe76BPtrPc2zA/+sYyn2PVXVidVcVt7u65EZExErP3EtnMkz3XOz rMMKVD58TYQNGFZy8yLuFeEHY2tuMZJJJ3dG+AsQH1NqcwbHAkywz61/kyk/vO93C789 tx9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722987; x=1722327787; 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=MPvnMxM4cw829IqByr7iX9fTi7jPKsJ659JMi5lcAVk=; b=mh5GzGdCTzVhwmSbcNBfP9z63eBieiPpA8ZWi4GN2hTMJciEuCCvGd3ldPr0/ee0Dc DXHucYk90bInaA4d3QkMZr3EapxFbwYjrl28ZDR/iXl9P6PrfdMteUDwUc//3D9sacCa cLklnFV170CghmC9AmfTHnVevzrQRRx9H6vPHrCFjmTCmcoIaiOSWUYnv3+X1nDp9k+g F+uZnn/hI5ozEZrtBhz13iZNFqrBRr8Iovs8jd1j0toF+FHavl4yv5mpn5vd6v+vbp+6 bic/U1C2HAwfM+Obx+0fdKlgzLevu8EHd94wYQQUoMkWaIg4co2yb4c2FYYx/frPkCaJ hNVA== X-Forwarded-Encrypted: i=1; AJvYcCWHif/oA35/oduvqIwziBq6JsOaAS0kfmFnO8a1zAA8F9kcICi1P3vQip/GZtVvxYKUstfurSnOhRJKFHvuxv0isd8wwufipLAMP+NS X-Gm-Message-State: AOJu0YwjKe1lALxTCzAy6uOYFYaOq2eEVBiSG5N5L9UIwhtTRIbOs968 1V1SnVdofQ/BUy3/MZ+JrowreFd5xMWMsb0zGLHM2DTKx2xVev3yGKrvBszCK0C/RDhptIzD3te TG89nWpmRUwunsw== X-Google-Smtp-Source: AGHT+IH2hSq+GI7fpB845GOKVHajbqaKsEZktSCAZr0VxXAu2E0CoHmD9gPZyAlaJxcNjKWgel9HY3glRzEAGkI= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:8759:0:b0:2ef:2488:3a2 with SMTP id 38308e7fff4ca-2ef2488064cmr100851fa.3.1721722986827; Tue, 23 Jul 2024 01:23:06 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:06 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8193; i=aliceryhl@google.com; h=from:subject:message-id; bh=7s+2tpsrG5RKjrFDALseGw6Co5fhy0QWgnewSA23Pbs=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hUhvtoTALGT9zgGFLO5O+h58IEs+USgyyN/ rVQcrMCDHyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVAAKCRAEWL7uWMY5 RnlZEACOrUL7PmtfsI8t4NDnnp07TNFzNLwJ8lRiwLfij8uAHzVTgGcbSC5jIHgBdahGrSf9L6b Ery0r7C+3YME805BgFEckDVhvbUQd9Z/3PMdPXCW1DD2QIkR4NqYp8gygZJWaTilgwCbt1SiZe8 1wsvnjV4Eeheez6y/jEayTGw7/O+YXLctN51S9arAJp8q1VOViPTEbArm19b1wGg73j+El8JPGR 5gkbnFSX3kDuAof4wx9/NoUAQWlrpli9Bve7t8q433OJ5O20HPb/w4F1T20lj2Zvzny+k1Pw9p9 PSMorq0ji3L/UOTOtLmFUVjZ9463Rk7e48ILg61mXavKtl8nOoeprWx3JB1XBGB8ZM4vIeS9tyk HqHB1cfVV/XFJDGEU5o+LiQV7NXVJrCQ1+A8UXXE89/7OBB54y0qUE0GJ+EgsErBQdwT8x/4GLT U6WELpUbi916tPKtnmU2qsi7xQ/CTeKPQy35uNaLYgDtU+4J2CibEJWtMgcnfm73QFnCxqXxzeY yIN4y5zGUBX6Pzb5mH3FL1LqbuU4yu1QvZrymZfEIwHPArKjEmAbdDscZTwHNruwlnPw8CYtR1R FCnRC7aSFffCLPiPyznUf6kLAVqnDxhsluczeVyQXKhyF0k3RgOrhjnmIL3YNDyMUlpTfFuKF7A kSmOUwfyMmDu1Ng== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-5-89db92c7dbf4@google.com> Subject: [PATCH v3 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. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 3 + rust/kernel/list/impl_list_item_mod.rs | 139 +++++++++++++++++++++++++++++= ++++ 2 files changed, 142 insertions(+) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 63e6f6a47964..8fb7d2c13580 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, AtomicListArcTracker, ListArc, ListArcSafe, TryNew= ListArc, 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..9b1947371c63 --- /dev/null +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -0,0 +1,139 @@ +// 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. +/// +/// # Safety +/// +/// All values of this type must have a `ListLinks` field at the given= offset. +/// +/// The implementation 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_work!` 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 implementation of `raw_get_list_links` is not changed since= the `addr_of_mut!` macro + // is equivalent to the pointer offset operation in the trait defi= nition. + 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. +/// +/// Assumes that the type implements [`HasListLinks`]. +/// +/// [`ListItem`]: crate::list::ListItem +#[macro_export] +macro_rules! impl_list_item { + ( + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + using ListLinks; + } $($rest:tt)* + ) =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: + // 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 violates the safety requirement that `post= _remove` has not been + // called since the most recent call to `prepare_to_insert= `, but that is okay + // because the concrete implementation of `view_value` abo= ve does not rely on that + // requirement anywhere except for its second guarantee, a= nd we don't need its + // second guarantee. + unsafe { >::view_valu= e(me) } + } + } + }; +} +pub use impl_list_item; --=20 2.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.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 294D014F11E for ; Tue, 23 Jul 2024 08:23:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722994; cv=none; b=OBJPfilb331zHOrhBlkjLxtO05mCM4lywiKuiWUoapL6SAo5JNsznpyvWn+pDn3WhLyiP2e0RPCmgN0yVXGHukkLFtgQ5FXBep3zOanCwXkRZdb9hvNRTAppPUYHJz8i9y7GDzt9wfVSWoD5/17igWY/hRB+MK8lBBwlNQgpLAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722994; c=relaxed/simple; bh=0QapBrIiLmz0q51EUrjrYc/f5qhTr4+5Y3y2s7eG7CE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n3wJLf0jngzCrXLpgGBudQSqq3Isyu96d4/849BCq++zknrKaPN2ydJ8lsHZn5ugRIZuzTTw5/mkCvrc/YgmNpkiP8EujhO9Fia/EZ+7WCXkxNPAAd31QKLqqiX/AOKibE3uO1dOUDLt/0b/PWDWq0w4CG+HtxGvAcJjC3wVAbU= 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=pfkNtlUA; arc=none smtp.client-ip=209.85.167.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="pfkNtlUA" Received: by mail-lf1-f73.google.com with SMTP id 2adb3069b0e04-52f0258b020so2165745e87.0 for ; Tue, 23 Jul 2024 01:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722990; x=1722327790; 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=+2hct+lqQILsqKSncs0TW9s2v1FBaEOovWjsRT5bWBM=; b=pfkNtlUAHpA12mK6H57JAN7PCK6N8c+BvvRf9aL2+rQLO1bVl+rc/5pios5p/HHHv3 QsHSJZ+JYicdip8aH0tUzwWtHa/U4Yjg7O3BcG/kFfWkT0egneK6a6ezDjjyRl2gRJaV l0pm/twjznAbaUKrIVTy3tXfKTQ8uWxjntiTnfjTJrB0mdH1RR6OZqG9R0yC8uttOJzw DHUnrIM/oTtDjXwgYczeForxDbHD7fVNhOwlXbM8GMqDUw9n5UhtGYJEcHuIKVhLzsMZ Ktu8zxeRu90xqxBZAwk8pTan+q3wTXB6S8nt4pqmZJnFBQdMzAmCkO40AY6DKzib9an9 +xWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722990; x=1722327790; 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=+2hct+lqQILsqKSncs0TW9s2v1FBaEOovWjsRT5bWBM=; b=Bn6myKBknp9/bXTRImY2ZZV5DWavl4YZ5cWgFerxLfX3JxHoYbLLqOgEOKzPldlb2p Lf19gLG48D7qYqAol/VesZGUk4zVko81dLoPVU9ATEUFgI8DO1q/b7koqWpzFCWrc1ve e1TOjkIFPAl901GnEjQ16z5k/IpP2JbNrsxrMkLKuwRY1gW7GTQGvrTMHI/db2nFSGrr SWivmPxxeXQXsbYCFjGm5ATlVaxfj5mUsKNNLGRjaaS2/lKGN1JmTCnlCrl+P8c9rz0Q 1A7vOxCUS+OnLXNL0rM+zswa7p6gnEdLro/xkloMwp8WgFlg8R+hfeWztWKiXsnq+J2E cstw== X-Forwarded-Encrypted: i=1; AJvYcCWoLhX/b4xqGpKyaM/QAwXxHyK6eZY8T0c+jjrraor73IRPeGtBj8n47sOBiNCj9PLPGzFPqa9Ar7sQv7oURgeXTaNXYBAAkn7FPMvO X-Gm-Message-State: AOJu0YyS7n1UnDaIH/PT5EJN1sqtMwBoNHSs5sdBaZJZMSOkQdFUHEYZ mCAPmvB2fw85XLhWJv8NdqlGHbGlLoEwS/jaEIUKwIBApd8SN285wEEH432eXbT7R39T/xEnlFY 8cgteOVCwsj4h2g== X-Google-Smtp-Source: AGHT+IFIapE3r5Da6cpHSJFLvPhv2mZckCrhOCUsK6fMo7kH9aXHtFZ1HdE/gBrQ4KF4VYUWmfsG4Tk3QytFjJw= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:3f0f:b0:52e:a3fa:6e51 with SMTP id 2adb3069b0e04-52ef8dc248emr12107e87.9.1721722990283; Tue, 23 Jul 2024 01:23:10 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:07 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=19086; i=aliceryhl@google.com; h=from:subject:message-id; bh=0QapBrIiLmz0q51EUrjrYc/f5qhTr4+5Y3y2s7eG7CE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hVByQPcaOLHiU8QW8zC24J1SAx4Y6FbSlhP 7sBjHOQ7q6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVQAKCRAEWL7uWMY5 RijeD/9Bvl4IRyjcPXeK3vrqqcsuFyVx6Rnq3ZTvKdEKUvutJJN+9b1PfJI7xvsK8NJ6eDaF0ER kOpNNhlg5KPtPLT7QTuodaq/K8ZfL868h2N5VZuoPLifijoNgtzXBbsC4eaa5nYSXgMEocKAUea mLSfS9ELHcb0qV8x5k82Ayj0tALCa0dZ3LfY7ZQypLKJoyW1KLbeyBZ02iWxYyACxuchICw9Zp5 uj6P6+LeRWLVH9QE+eiLA18cYYJ9QaEgkOslb0XGXEVhcwtbPuGdU/RpaV3yEQ/Fk2590yemj2k O8Xzwc9Q5a9bCqHMcr+k354RXDALahM9eh448omdqPQkLj4ERQpjyqqlxG0WB0WnKhPJ/vawEDR 6xGTH1Pzk+kmjOtQO3fvjT5ZsMOZAGG8MT5DuBkxJ01WGKcnd2WMUUYdPcS6O5ZNCagPen5GBtb S6Ct3vLJXHK2leGvoFiYXFYsJys1YuTdLdQgNMOcGdGq5WgDfFwPNUCLn4IaAMTpnB7lUPh3igk NxtMqgoKGCOsLLLSm9CFdnMsXqVtvYCQm1qI59AXDGSlfAuAeI1xipnVqXbv3pIOW0jrgBuyNh9 cUkhKb39Ncf/BAV32qOkr19DOsoTtoGRd1O/iE4D+pV4afoouHp2YK+GF+4sDv+tbC+GJPaCw7x wDEXKf8+xslBlcw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-6-89db92c7dbf4@google.com> Subject: [PATCH v3 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. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 331 ++++++++++++++++++++++++++++++++++++++++++++= +++- rust/kernel/list/arc.rs | 6 +- 2 files changed, 332 insertions(+), 5 deletions(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index 8fb7d2c13580..d3f8e38d9ff4 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; @@ -16,7 +17,42 @@ impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, TryNew= ListArc, }; =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 /// @@ -58,7 +94,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 { /// The fields are null if and only if this item is not in a list. #[repr(transparent)] pub struct ListLinks { - #[allow(dead_code)] inner: Opaque, } =20 @@ -125,4 +160,294 @@ 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`. + // * 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_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 97bd9d52b5cf..a29bd26e49cf 100644 --- a/rust/kernel/list/arc.rs +++ b/rust/kernel/list/arc.rs @@ -124,8 +124,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 @@ -144,6 +144,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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 2E0CE14F12F for ; Tue, 23 Jul 2024 08:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722996; cv=none; b=VCbL6o3u8ouMVOUk5JT8cu1L5Odk+DxU0j9JXug1lKw+oXq7Oj0ScaqbDJjStvoRrg5GUEBaiX5QofMlmuHTwOUxRVX7W4bjWoO21+HrPH+eUAWjBGgTx9zhC0j+WBDQ1jI7evtYlSB/iL0X/uW2E1TATbArjKoeYyOnR/OtalM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722996; c=relaxed/simple; bh=yg75c+W1eHmcUh6XQTFsaiSkN/WH/Yg42MPoMls94XQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TUxfFVIIN9mOOTznpgJVpr68acxqkIkX45rZQspx2OyHwmhkAmjne8G8safJOcq3hVOvKGm9f4MxmvoR8Xz3zq3H9uyoDhsKOunLqWLrrusLx7H4q0X6dMBXo6m4ZAp8gGPEv/8FybotZkoWrtHqRA5mE9Y3hXIPu5ww5FPrWdc= 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=3h9C3zWK; arc=none smtp.client-ip=209.85.219.201 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="3h9C3zWK" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e05eae12defso9943043276.0 for ; Tue, 23 Jul 2024 01:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722993; x=1722327793; 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=tcZ7Zm5VQO7GwdFeiWI47MTbabJd70KzY1xKEHxQP9c=; b=3h9C3zWKs+WHtNIkHLGKhx6TZ0sA41x78DdtLhfcum+3HGWq2NumGb0CKBlQm6Zouv Uz26hTbSInXWVDKH7Usx6M9nzZDCb8LLIDfQnVp+iMPQNTQ1f/hDbrZ9C6llLaYjcWeX KoE4AiJ2CRvq1m1jJXPRSJrpQdbdNd0a6CXBnZy1vEOxNG6wOJaYIZ7xsFQ4msLQ4I/q 4W42p/Ft3LMggcPk8V56Tymj8dnoNKS4+vP+kuEPm7C5D4FZAtjQpreg+Ra8YVP8OR/y q7m2cRrrLUXHAaerYrTrSxPhWTFubePBR7GIoTxTKkZpe9XgY+0Vhp4PbraN7WfEsNaC gbxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722993; x=1722327793; 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=tcZ7Zm5VQO7GwdFeiWI47MTbabJd70KzY1xKEHxQP9c=; b=nk24jvmjjYgqluorTcGsZ0ecMmcSHNwEinm9cPyt1vEAWg/jY+LrtJPfOzrLSrdYXh mvoJdNIPsHRJjhK4CusJ86FuSxf3adOAPN2MZ3xfDhBQJdsYS47F6ZkmVyOvhGujylhZ L2wTHKL0R0Ow71gInRjwFKxm5bVAIjfUSqScz8zW6h7zg+/aQUw4b/KJ3XC+jNn7uHR1 jX8iZrimrt0mYkdbFyM98HQ6DU/y964/HNxlkJ0VbxyUyZmdwIL2edtiNc9EynbsDMt1 lqPu0B9WOSlfjLnX3+Y69CvZ//WnbVQc3xLN9R+68DAdjdjDXJRFV6ROgfCjyDrS67ZA 1gKQ== X-Forwarded-Encrypted: i=1; AJvYcCUj2I6/OqHyKZyqP+ynsjIDBEoPabtt+azLZWByjw48wK2jCuKAyRA0CnkjZEOOZiN+JaegHKXqOZvOnxwjC9U6qwsBBIfGjYXhXAuf X-Gm-Message-State: AOJu0YzptpRKFhr7A/U84C4czywA3iZqOuoPIwTntr7JbJvQoEm45evR 1upQnRsKo1bRVaShRe8iJlE8nVCq9orrYJj7PgnEfdQ/LDCdMn5dpfujnA28hLyhnO85BKlEJzK HJJkehSD2+MqSOA== X-Google-Smtp-Source: AGHT+IFcZBOUuldUyrZ/nC+GBhKsp/SOAJzssCONhYZVZO8dbhxu3yXfTLnyzNLBTT8kWbI+6xhmwkg9vxIdC1s= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:2b8e:b0:dfa:ff27:db9 with SMTP id 3f1490d57ef6-e08b989f5fdmr100844276.5.1721722993142; Tue, 23 Jul 2024 01:23:13 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:08 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5027; i=aliceryhl@google.com; h=from:subject:message-id; bh=yg75c+W1eHmcUh6XQTFsaiSkN/WH/Yg42MPoMls94XQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hVY5aMLZp4Cjqm6xBxmRD+EmxL3JcQWyPuC ZstqJMWmQuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVQAKCRAEWL7uWMY5 RvzjD/9maKEceIFrHCcotW18lHOJMHwE5uxCMbRD++60fWoosLMEY27NaPRda+G5QOFJGlaRQAu 55tFklAIQzLQfnnGAt30e4xagsbvhwV2oTam3zVfV/iylrxIwghHuWE1GzgIiJo4tIHDHCuj9yy k3neTOYs6SIzCX6Jfls//scpJzCfmLpku5nnKPMzQKSTDggHL1Ua1+qsfbeH0PhnfC9RvR0Ihip lAPOx/Nv1e3/+UT2RJF2H1pZV7GpWYQOK9xXTHPP2iB/T0srqej/mBtdXwD682XDIKPkDYx1QUh mc/dJyC0Yj2vSo+lSWoCpEyVQtF+GAZ3haBGbXwQIE+PBprso13FF2WnQU9fGz6BNpZSEz2yxYD DIOOLZUVQ8df8exSH38n719SoDK2LL24sR6nKaJzVjER+CvnwVX6eRpS2k3aOFnFXwBqcVdOwE4 4QTE1p0bd2Hev50lOERKSkI63XLpoNG2NRX7bhfdYiqLt3WGxU8xynuXm/Hw0fKzlQF9uic6eOD govnO6fbRKnbgjNBUFS02QHU51HvGHfiJxgkLgtx/2hh6KMNahJMDtnqzHfnhBV5dqqJi7LCywu hFfFGLYfyRC7vWUSmNo88v+d1THgQJKXadl1nmSwIEtZ4LqBSpolbj73iOYrlhJTic62faVnJiV arFAqNHIbsyZZwA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-7-89db92c7dbf4@google.com> Subject: [PATCH v3 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 d3f8e38d9ff4..e320da063c07 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 @@ -436,6 +438,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 { @@ -451,3 +464,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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 1534814F9DF for ; Tue, 23 Jul 2024 08:23:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722999; cv=none; b=aWf/Qs2+9a8nURq/9PzN9x/V0zb88bbSn1P4yJPdip2artoUoea62NdZcwIl1PXmRkUoOasw6SWCDKuHMmQIcSteWO16jGfVw8SoCZwQZwJYxDCGEgUuC6VUoVCr1Xc2BB7Mjau6+r2pwWvRwgQu3boZd2hHyTD8jWIBnJMXpqs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721722999; c=relaxed/simple; bh=DGiNcxmXnBX8XeaW9TQmBXqeb7hhSkPo8/+cGCJC2TY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=A6Y7JqRLoTC2qBUvhFGIktvjgW2Bl50Lz9F2iyzbL3saQmIa6vy15a5UYa9SV8T8hBWcUXNbQnHnKrQ5nVvFKyhk3pYD6OHboXsfFRKnX92grzedVykNx3DTt1Ok1dQg/wtxpiTi4UdTAEbkKKrV+SosbnfDWjw3EfXl883Cz6g= 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=XSLKxPPS; arc=none smtp.client-ip=209.85.219.201 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="XSLKxPPS" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e05ec8921fdso11210921276.1 for ; Tue, 23 Jul 2024 01:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721722996; x=1722327796; 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=29r0gIPhUnZ5mBg155qsbH5Qt/QtLWdfTj/Q5hfndv8=; b=XSLKxPPSWL4z77pewFKUxWmDmH2KJuQnJmGl+5H06oWMF6fVGm0/07YZi3qWPvrnX+ DXdX/+DehgsE31BmjVi8YdYzIxw+6jPoHAQNyjLeA/x++SNBppKVsu4BlhozO6/2xEen Zc6FockSsuM4uqpGRwB38XZFxJ4EavKfdqw+yQ/GH5IVd4BWc7P50bxbbtfUnFKK1afk P5VsRw9NwRZQl8q3wMPtSkMG3I8JcmOsn6h74D+2Jkgg69Nouhz7+e057IlBgFkCIOoi Yby8Bzyt/7mYwpN+CZvU1VsFwKC6lVwT4UVXzjgXhMZXn045CqQ6ltKlQKBpnSzgtyy7 w7Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721722996; x=1722327796; 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=29r0gIPhUnZ5mBg155qsbH5Qt/QtLWdfTj/Q5hfndv8=; b=DYUCh32CjrF+2z7au7SQrMyGRMXYQjJmXTL5WXi94tobFK8iu0bBL4G4sdqvpLU5zr CXb37neNrr002jpieAH9Ks1rhFbohxDVt/UM5u5PQgwUDDiwgGp7u1FWHHUQFB6NbnQU UgSj7HcCWGmRcJ6lLiC+smNg4brR46dQIdsZAqDWwBbcPLCnhHlX+9GllrbeYquRmTzE eVzAcid5wn4DJGEfP2idePGqh7P/Oc8XDATPPmx8J1h/39yoZJi5ZLtIGTNk6Aol36Ce wZfb371sycC7Tl78QfCMgY/8fNSJ9mdX5qYy9mUbBYeG3d0nvBhkVScJFMUB/ksDwIP1 HTVw== X-Forwarded-Encrypted: i=1; AJvYcCUtCBsEp9SXrAwF9isYScoZPF2zuPZGyY8xpuaU0rz1wTqZ4ru5M0bKFH3thUrM94pDkaw9KxD/gOxxJ9L3+PL5Yq1+0XW161ND6k/s X-Gm-Message-State: AOJu0YwUvapZY8Ucx68s29nJirbiBOjgDC63AxyE8El5TeCQYiEea2Gj EGfEx2pbukvq0Hk0MHOmIIYcL+5tqYUNot7AIhtzxWEJz4JcvFpYM4BUrOyykQrjOs6o/++Jagf QCKV452wGp9CCvg== X-Google-Smtp-Source: AGHT+IEORvPmcAry+mHuiowBlcK1a5jUA94sbmV2u66xPv16mNcweTtcSgMGB+a3ZAMoz2oR3mgOLF1mDnSqRBM= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a25:8007:0:b0:e08:664c:dd1f with SMTP id 3f1490d57ef6-e087045b23dmr26333276.12.1721722995934; Tue, 23 Jul 2024 01:23:15 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:09 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5335; i=aliceryhl@google.com; h=from:subject:message-id; bh=DGiNcxmXnBX8XeaW9TQmBXqeb7hhSkPo8/+cGCJC2TY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hWHg2ZCJ1/EurYZ0A7P8kK1WXe4nRKxBbRm lAcFUe9Nx6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVgAKCRAEWL7uWMY5 RrKdD/43jX1KVxMIuuvSWTNjwbZzJ8ks8rU6pLxe5r1N3qgjP9/jEYTZfn+5bp8eMJr6Ym4mned ECCI+N6Mh44A/MPNIka2JsyVK3urZNj+PAwp9yyDb9DscJUiEtozN71jrLGepAk82QSmAgP907T 3ke84jwLNV18qsTaap6XRf5Z9/XtaCAHTfbbr83vjjEkFUlqao/OnqhB6/DsLIZe0tXe3Png+nR 928INYXcNPPipzkPTDei+bt/hMSdgEMRgG/srv0oehSawgUDJgw7sscLSqHV+SHYx/u9vJ2BKk0 cNqEyNtEfmYPQjusrt/wwfkG26GRtMBz5qq+rd9sPSIJOiDB7Mh1ye3USz7GuXPeZT024eeaFzX uzDozFxNFJNHXcvlwWplHdn5SohePyEH5JcNDT/j6DAuvQdYEvUhRpg2SePyGvVN4QTQfO5kIRZ 1T0ykWHYqi3dJkHO8w6rDSxqHQ44P6BBl71WquWx5hD1qmz3iJHf10f2eyfgpDoNkiP8pVCJQcl un2oY/OTk2VUNDcrePXVzes7IT1AOnalveDgsaE97iMhcgrGQ2gE/hax3VqjkMnMCB/iveLlAST PwlY8HQntsgm42mgmpYnKrUbAIOZqC+muGjO1CXMS9Hq6ohg3O9dKExaMMURlvptGGYgZKuTPqR X2BqtcFi6RlQUpQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-8-89db92c7dbf4@google.com> Subject: [PATCH v3 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 e320da063c07..f36e17369382 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -439,6 +439,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 @@ -513,6 +527,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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 0137715099D for ; Tue, 23 Jul 2024 08:23:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723002; cv=none; b=XAF/kYHNCxJLAfXCD6xDeDQaOH+svM69ErWn8gP7S9u8YMsNTt54pC9GCxQl/QMig04OFPa+iry/plceg8W23i5jrw3lxR9HSa2utErf8ngpb9LOitoNLoyAQ2Ol6itNYLU9SLaeFsmXfqc8wHv8ka4sScQ0jjSsHlUbNXM/nAQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723002; c=relaxed/simple; bh=t2oXckmC19ETgVAHAa4uo0x7V5UDrMT6LVv4t/Ms89Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KkGcC6Ab7SKin2n7lSvqgbz1yuvmJ9fQ358YZMoIitpgaw/+GRXVrme0baQJtXFPoS+0NmO43hz+uM1HCpDsygtcH6YzYZlrutNWQi/Z2xg8KibvCdmgxhywlsdKgp0M5wIOqyR9nZ1k7+eBDmGfCASxE999WDAPNY8NKxl9WYU= 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=s3T08d98; arc=none smtp.client-ip=209.85.128.201 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="s3T08d98" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-65026e6285eso150177387b3.3 for ; Tue, 23 Jul 2024 01:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721723000; x=1722327800; 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=SR66IeXZaK15fT7q7EwWrb5J+C89ptRSyhcMWM3W7Cw=; b=s3T08d986vnP0XI0RFDoWe8IFUs/bQrFrbd1kBxiZffg6HlfmdDiLMBPF83v1gYKIo yxarvkJbbg4Sv2RUQyUHxcqrlxClMQTMt4+oVZoT4PFTV59cGr20GEOR9SQV5kwhCN6G uiJ7MGRwwiuDfGWYl43yFXgX301aodjSiR8/CBJuwQNxx9l+TDN7wNo2pfxujLlxgXnt OvOi6M+GgPmmf0R9fNCeDJGZt6LoBg/fGWovLsH8UFtBhHNMJAxTXbHxZ+kWtBaWV7Qd AII7cEwzU7YgzhB9DXPrxfnmYdY0wfkJ86gPnPdKETLWNUtuFRARlqlXKU3790WF+ezt h8WQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721723000; x=1722327800; 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=SR66IeXZaK15fT7q7EwWrb5J+C89ptRSyhcMWM3W7Cw=; b=ClXq/7vZI82XoMxsUEBbGWCEU+QDLubw5JIGJsAxZu2+tavxwd3wUBqHUkbdvsez4T GKOvlTgqtGRvHtq9PUZ8OJ76ILpv86ia9qkxvMTL77lxx6UpJkRvDwmryyUHUSt2W3Ev 20ovHuXvr0EefmoANqOK+CDD8rJCMq/E/aWm+qTUtO4LeAHVMjUCbskUgXW+COaUqhAM RmeLsEBRViuD1+cvlwUVK4iy4dLgejkRIOKg0TotZCrRcgDXOKr2p+8yV8T9TBdo4LTU uyEkRG+HAJspEJFBPmOVfDr+Ff+8fuxNfLHiRHBHldNYLajwLu/1YYQuJdGCIzzxQ+IZ /Lrg== X-Forwarded-Encrypted: i=1; AJvYcCVrlUSMh46K636zlv/R6X2WFocGt2yRMTRCMscWvbOGIHmG9P0APke/D/k4JRijScrxYZ180fuW6oITQyz5+qCCW15z6/VO+rhQ3aI3 X-Gm-Message-State: AOJu0Yww3kFXkse3Rveclmc6WD1KeR30RA6sD4bT38PDYqXmNs0LPY5X 76ZOZ6+FWlpWJaokcuDaZRekK6GNZfHb8bqF1cYH3H8YvMF73zqg9Zz9WUGpC5Bpimi1chWatDR O4QYsvCRzjgjaiQ== X-Google-Smtp-Source: AGHT+IEkucnm3KX+t4Fn5rUjU9pGGLXEIEsxSCZpG/oHFaXOMsAhblQNZYxocnc0p2MhzC3bcS3CS0UnnMwpi/A= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:fd4:b0:65c:1db1:9235 with SMTP id 00721157ae682-66e4900940amr702487b3.0.1721722999935; Tue, 23 Jul 2024 01:23:19 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:10 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=11842; i=aliceryhl@google.com; h=from:subject:message-id; bh=t2oXckmC19ETgVAHAa4uo0x7V5UDrMT6LVv4t/Ms89Y=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hXVZtQKU1bPvIXHJeNDmldhxxTmU2vo054p YTXUahkg3uJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVwAKCRAEWL7uWMY5 RhNoD/9+bAamGfXBOhmCDMLpZfriTP+vVE5Kl2Te1FExKwBPbVnVO6/GcLstEzQ9rPF+n8hQot0 7bF5ird2yhpgu8Jg0C+af5RPy8zFVOHd3EAmZmxMwpapF2JPRHTRnKvRRCrqiaHyboqbKa+w7it reP+hljsWXsUNiaPfu1jqlI8i4D5C0Mho7X49qXqUlYTA3Bbb1mW4mmCC31/0O4VwmeQdxtnBtR zZkKWiu/qaKjyHJVe1vhXjUx89c0C2P0DZ7Z6eHVF++8PUhdeCd5omg06MMbKnVsh95wxeIHRfg yRt9mwjDO3+HvNwXCiUEXxUz3KomxUksYU90CvcCG++K7mKcHYiKF9TPXAQxiyBwW848fSIQldX 6eEVbTgSyilwnjfrn7higRgPHwWttWh4Fgz1hOIw5rcUNSfz2XxK/a6Yk9i1ZcEaLhqAKoHxAj5 ejJZuinTFkgLPLKxO0Nxu5htGnYk9tAkI1wVi4+N79FdRS/m9gHa/b8DtFs75eGhFufgAJBFARU I129k0THNvAR4nPqgnCPnycUebzCMFFXLBgJv8McGx4iunZG9ogies7YF4KfGIj8dVmHumFopFJ UpCD9bpmJogTMlfwv1d2TCWA8UZly/OKBfwpMUrhNOofEtjtHMnKKHP2eUpEzsaVguq35cRZVmf MucZxGe5VlD6TWA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-9-89db92c7dbf4@google.com> Subject: [PATCH v3 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. Signed-off-by: Alice Ryhl --- rust/kernel/list.rs | 47 +++++++++++- rust/kernel/list/impl_list_item_mod.rs | 129 +++++++++++++++++++++++++++++= ++++ 2 files changed, 175 insertions(+), 1 deletion(-) diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs index f36e17369382..f61b19c145db 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -7,12 +7,16 @@ use crate::init::PinInit; use crate::sync::ArcBorrow; use crate::types::Opaque; +use core::cell::UnsafeCell; use core::iter::{DoubleEndedIterator, FusedIterator}; use core::marker::PhantomData; +use core::mem::MaybeUninit; 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::{ @@ -181,6 +185,47 @@ 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, + self_ptr: UnsafeCell>, +} + +// 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: UnsafeCell::new(MaybeUninit::zeroed()), + } + } +} + 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 9b1947371c63..9335edd8f350 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -67,6 +67,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. /// /// Assumes that the type implements [`HasListLinks`]. @@ -135,5 +178,91 @@ unsafe fn post_remove(me: *mut $crate::list::ListLinks= <$num>) -> *const Self { } } }; + + ( + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty { + using ListLinksSelfPtr; + } $($rest:tt)* + ) =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; + // 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 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: + // * 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: (when using the `view_value` safety requirement= s) + // * 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.45.2.1089.g2a221341d9-goog From nobody Mon Sep 16 19:29:06 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 B700E1509BC for ; Tue, 23 Jul 2024 08:23:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723005; cv=none; b=J9OfAmzC1zMNycwZ972sjOpJK/bN3RtMVaiVbWGI8naTUVavV5DBoQPbRwyI75vp6Me+jDs+Ejf6uSgsSI1RwZt1+mQWjMIKEeazRI9zfz1/YjoI07HkfcI6NPQULf10NW34CHARqqTwP+3SXwZkh8KKdgMKbNG99T1fAImuhxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721723005; c=relaxed/simple; bh=P4GJjcMR46AtcXwSMbkn2ALRqne3SCIpIm1LC96k388=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ppb0MlIG9UrBkNvA5STPi/IvcADrABBh8t5chUvPFRS6x79YxHaeZSPAwz4SIzh5yr1mZsXHzB5L8Bt9eM2II+RCat7Lc5gTloLhK0AEL+Ff1SUle3hDvBxJbAX8/d45kjur/teT4XCfUpjNMO6/o3xBXzfIWOff7UbX2OVzKbM= 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=2rDqSbyY; arc=none smtp.client-ip=209.85.219.201 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="2rDqSbyY" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e03623b24ddso11274148276.1 for ; Tue, 23 Jul 2024 01:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721723003; x=1722327803; 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=AhyYYDQfkjp32gHrK6fI251ZMph8ENPohnNmRiSfKnI=; b=2rDqSbyYf5AVuu/NZSLEKXJsHg1yfuaVPhQ0/TgjjygW5SeTStxntpdiAq/aBV2p0d PRORgu4/v3KAQwHt3U4N3c22EC0mlQuLSUA1XD+3voAJO1LgYyNH3PvIBp4jyDqQL1Ab 38FkUm5GlQCwvGdSWVhdiTObFTuYksSDgTYEWxTBFWLA6xtuJW3DTdInqDyxWug0B3hT csjx9JL0xHVydNzs5xHhIlJPuF/5wsLSTVgKybll2BorffsNHFUU4TLwVYydZXrdUm4T KfsmOJvAwp0ZjougSIuQeaZOGOIE0f0st7kmEGaB9F1XcwHBOLL+ZJPxd2k8QEAOrqqX Tztg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721723003; x=1722327803; 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=AhyYYDQfkjp32gHrK6fI251ZMph8ENPohnNmRiSfKnI=; b=VXUQRmkQ0FvIiOOpleMMXVrgVxZ9+bL1caJ3F9Rjp4iAfe9R4R9HARenwAxq7+9tfn HtDtWeF/DxEPwnIS4McuO1qiNYle3CCLsDgRWKAMzvVSC1Tq3YKN1GxYYqQi2b3lKReo nkmxot2lToIAHxuBmuyecsVx+yj1BXHpWmElzJgFutq1HRV8a5EAplN2WllgQlgzDiw0 udYOezJ/NBz/xcSfDgPtiPevSKGBzWhw4ZPDPpdWVGOu6JsXd70p54b3vdQees9kWR5q 51komPAQI76anPrqH1tHaNS/r4v8WK9TyqusN8jiFXvBT6o8c3/hfp+pblz+s2WFYUzu phOA== X-Forwarded-Encrypted: i=1; AJvYcCVV6ZmecXQDcvVG4OwWDG1PjhJBsnKbMQQHm5czjhUEa2S6Bv0OleaKAGDbIFX5QnXdSvWadumUQbZi7fpGDG2JFLrc9hqbSiRffMtV X-Gm-Message-State: AOJu0YzttRjaYeD/AQqT3DYTmbeNU7PEs0HRJFM/N7GzZ/Yp9RV6kZur 0HNE0J2PQpxHSh5sDT9v5aY0hLsZ0x+quaQIjKZ0SWlDG8niy7EyEYNhw06Pe59RKGZ/l8xyZ3x jN3TaK8U+RDd2pQ== X-Google-Smtp-Source: AGHT+IG6G5H67c/ash+4uWHilLseAkYsYekJmisvjTIJQ8aoyq3aBo73QFuP5o3PZ0zTuW7kBoiyNL11XTmW814= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:c06:b0:e08:6e2c:69da with SMTP id 3f1490d57ef6-e086fe5929amr33578276.1.1721723002720; Tue, 23 Jul 2024 01:23:22 -0700 (PDT) Date: Tue, 23 Jul 2024 08:22:11 +0000 In-Reply-To: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240723-linked-list-v3-0-89db92c7dbf4@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5347; i=aliceryhl@google.com; h=from:subject:message-id; bh=P4GJjcMR46AtcXwSMbkn2ALRqne3SCIpIm1LC96k388=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmn2hXo1PmAC+q0DYNGOd2yWV7fxwZJyktK3qhC coxfhbBK4eJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZp9oVwAKCRAEWL7uWMY5 RkTeEACq8mdJD5GbaeydqlsUqAWawFtAlK6l7GVNEoK8QnEmTFNaMmdXYa5vYTu8xyesOXilNjk WwgVa8HBdg5j6hx9dJLEeIpmzh3Q97hy9CQQqQyRRQRJ9Z4VpEGRJRTycjfaKzJ56HRGcWNT2rL wMUHFwTdEpeYq/GlzdWe6SL+3HpqeP0O3JODB4+e0PaB1oHcCBA10CHOvYCaI8EwJVy6ogXGMvM dsjlztvnUU4txnkXZuioHELL/b076czFo5sZdJR+Sd2G9Prn7gi0fh0YD1VIq/gHaz0PyFM9MpD sicSM6S1AKbf89y7iZuLLcp1Bgv4i1fJUmHcy3CDhnfNdDt/1Sb9N7zydNfpBLx6mZReP8zh0Xs Ocm5L8olFGQusZVhDGAEMydJxNmqFyDGpl1+zKNU1A5XsIyN0ANHdjIJjX7gYGTz/xcAYURhimf GA9kWJjBvL4/A5n8doAGG/eXyiUZI4ukSJteBEqOISMt3GIF7iQv4J58wRZOZqzQ+oo/Av66NHE 4Xl1yCYl4C2OqVLETWm71aIJTfWLuGCbEhH6UMdMMrImU6AMdvn5F7f5CzvTqVfL82XZvVvQxu/ sP7LyVpLs8+m71NyvXtnv9SNbi5U4iAu32Ka5zinQ8Z9ncC4naU6DqTx3WkjLibRy29IzovVt6q bIe6EbWfbFI6bWQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240723-linked-list-v3-10-89db92c7dbf4@google.com> Subject: [PATCH v3 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 f61b19c145db..e8031bb3a94a 100644 --- a/rust/kernel/list.rs +++ b/rust/kernel/list.rs @@ -23,6 +23,9 @@ impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, TryNew= ListArc, }; =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.45.2.1089.g2a221341d9-goog