Received: by 2002:ab2:7a55:0:b0:1f4:4a7d:290d with SMTP id u21csp132872lqp; Thu, 4 Apr 2024 08:50:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUiwCW8wXvsD+P9mt2MxWQj/O1Aq2jA4HHqqnxaLWQwL3npiumGH24qg+JO2Np+giHlqVmpqb8Koc4AKg0J9ov+k01ZrotpFuliGetRCA== X-Google-Smtp-Source: AGHT+IFpoQHbj/X5okakXhnXHxpWc0AAeTEEkAOqLq5nFvupsU5gFnhorFYkBzFzWtVpmAZmL1hD X-Received: by 2002:a17:906:7116:b0:a47:5231:754f with SMTP id x22-20020a170906711600b00a475231754fmr73312ejj.7.1712245855051; Thu, 04 Apr 2024 08:50:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712245855; cv=pass; d=google.com; s=arc-20160816; b=UNN0itB7m/UhBXjQXxzHm5tFhaIbslZxT6cHgVS2bEis98/uXaMQwmSlbwcAKangox 3eFLdvqEHEy+v7EzQPvk0JejymOSNWeNkkZFrlXWjcCJ1TrLCDSnN1R6vfLyRAztw0v0 p6v9SMYgVTs9s97hEt5od8+PSA0/LoZbkej1KN0AOh5cRVhdhPCqH/cLsyNGLbJExKBs vITe3jDaz3QzWm62TMMeOKhoZ0GGhpihrGQvPd6TgojO1T4sU9L78HYcaPaIMVmdIlR9 a0AuP7WjGR6kqCiQmOJIhLvc8HFtXTjVkcig6cMwuo6Ps4VPEcJ1q7DncZ91pp0J3ag/ cJ1g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=b94fePUkCB3u0VzYfcwy26wl06e1l/qHAvF9anETlCY=; fh=1eN+strsLnX8cuvE+uTff7wY1KiEAf1jFO97IDCVf3U=; b=B1KLzLum6tREzMQVcNMr074Qz/AaiAabSTgdQMrRruUQOWPSTA5aiDUR7Dl8yzEvFv 9DTS/biUfGmzNqYmfk56bgvpYkj/fznZdyNUKsAzVTcujuvW2RVLv5Nsk3NYMG5mdl/L JYfTJuk/d/SfJzAHTIF8mFf8dlkozldMg505XMjLkzSXxSB/I9oMRivm8dPovg4IBZyD altkBDhDE28N2lf+wuXfC7zecPEvPLI+hCeiY3U7GeCWc/hlvCUTyOQOfKKsG9SUw3sS lVNkILUmt7yQE+gG+vxcresIGeLnyCXflaKylH/A12S9uNQxkMg9uRBE4NtV4TO2ifdd e0AQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="D9goF/Wq"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131764-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id f20-20020a1709062c5400b00a51963c0410si256808ejh.611.2024.04.04.08.50.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 08:50:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-131764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b="D9goF/Wq"; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-131764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-131764-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9AE931F21D0B for ; Thu, 4 Apr 2024 15:50:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74A9512AAD6; Thu, 4 Apr 2024 15:48:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="D9goF/Wq" Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20BA31292E5; Thu, 4 Apr 2024 15:48:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712245682; cv=none; b=WUUhmpZUexqHPfUpMonm1haXo72nqRHUKbRBY0AwrujkWbNfn4yCVpmVpMxSuNvBtNynksJuvPse6XoPjOwae7zqGHXKLOUPjZup3/XHyn192MDRWEBBIGslyyJ/ym0D4D8X9+1kQLyTtkXF4g+6Mz1ocjuvrC1mThW195jHsNM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712245682; c=relaxed/simple; bh=LP0uZSuTig8XQMIPTRba8MduWopWSkwtnn04svCVZOM=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ssPVM7zaiyOrtajpa1uir0nyIOUgPbq0pcN/XA92kfF4T2/R1yztfe1V61zDh1MXNbvyMXEvVCySbbEEWumHaPfMLtV3Aa9W1lWf5QrShwHBziN95h6pMQUDqiR390B0A7wyGWRV5lSJYC100MJ35yWHv7aC5P+0ZSQWFJCeSy4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=D9goF/Wq; arc=none smtp.client-ip=185.70.40.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1712245678; x=1712504878; bh=b94fePUkCB3u0VzYfcwy26wl06e1l/qHAvF9anETlCY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=D9goF/Wqu2/yGD93JT0jylmtc2ePnclvzANZkdbFrKB+CuMNy4a5B0FTzMsuOUrJa CBmGLE7eZClpgPiHT3fKLR+hVH3H4Qz9GYK0VPaYgg8Etx+NNcJrXmMFfm8ND3xV7m BQbcDPA/oKG60qcTt0ZgbzZfAq6bf5tICJfpH9e8VAG7aLmr7hYp5OYraQl+m+vgNj /SaJmN0PfRPyR0agEFbZG+SyclipoFEQM6qHxcL+XPFu+wT1DBWgY+/LX6CoB5zEa/ m4T5IgeadPaTvbAOkzXOjlKmn9amIlQplwul1MM4nTVo6z8+Zzy+Jm0Q55gh+vYsVo u3xaNZgOXlgTw== Date: Thu, 04 Apr 2024 15:47:49 +0000 To: Alice Ryhl , Miguel Ojeda , Andrew Morton From: Benno Lossin Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Marco Elver , Kees Cook , 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 Subject: Re: [PATCH 9/9] rust: list: add ListArcField Message-ID: <86ac172a-4ad5-4cc5-844e-7b928d0765bd@proton.me> In-Reply-To: <20240402-linked-list-v1-9-b1c59ba7ae3b@google.com> References: <20240402-linked-list-v1-0-b1c59ba7ae3b@google.com> <20240402-linked-list-v1-9-b1c59ba7ae3b@google.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 02.04.24 14:17, Alice Ryhl wrote: > 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. >=20 > 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. This pattern screams "field projection" :) >=20 > Signed-off-by: Alice Ryhl > --- > rust/kernel/list.rs | 3 ++ > rust/kernel/list/arc_field.rs | 94 +++++++++++++++++++++++++++++++++++++= ++++++ > 2 files changed, 97 insertions(+) >=20 > diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs > index 68d03b100863..a59b35b67e9b 100644 > --- a/rust/kernel/list.rs > +++ b/rust/kernel/list.rs > @@ -23,6 +23,9 @@ > impl_list_arc_safe, AtomicListArcTracker, ListArc, ListArcSafe, TryN= ewListArc, > }; >=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 t= he value. Since a value can > diff --git a/rust/kernel/list/arc_field.rs b/rust/kernel/list/arc_field.r= s > new file mode 100644 > index 000000000000..936fd97bc5ac > --- /dev/null > +++ b/rust/kernel/list/arc_field.rs > @@ -0,0 +1,94 @@ > +// 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 r= eference 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`. Missing doc link. > +pub struct ListArcField { > + value: UnsafeCell, > +} > + > +// SAFETY: If the inner type is thread-safe, then it's also okay for `Li= stArc` to be thread-safe. > +unsafe impl Send for ListArcField = {} > +// SAFETY: If the inner type is thread-safe, then it's also okay for `Li= stArc` 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 `ListArcFi= eld`. > + /// > + /// 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= this field. > + /// > + /// # Safety > + /// > + /// The caller must have shared access to the `ListArc` containi= ng 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 the= y also have shared access > + // to this field. > + unsafe { &*self.value.get() } > + } > + > + /// Unsafely assert that you have mutable access to the `ListArc` fo= r this field. > + /// > + /// # Safety > + /// > + /// The caller must have mutable access to the `ListArc` contain= ing 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 = they also have exclusive > + // access to this field. > + unsafe { &mut *self.value.get() } > + } > +} > + > +/// Defines. Missing docs. --=20 Cheers, Benno > +#[macro_export] > +macro_rules! define_list_arc_field_getter { > + ($pub:vis fn $name:ident(&self $(<$id:tt>)?) -> &$typ:ty { $field:id= ent } > + $($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)?>::asser= t_ref(field) } > + } > + > + $crate::list::define_list_arc_field_getter!($($rest)*); > + }; > + > + ($pub:vis fn $name:ident(&mut self $(<$id:tt>)?) -> &mut $typ:ty { $= field: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)?>::asser= t_mut(field) } > + } > + > + $crate::list::define_list_arc_field_getter!($($rest)*); > + }; > + > + () =3D> {}; > +} > +pub use define_list_arc_field_getter; >=20 > -- > 2.44.0.478.gd926399ef9-goog >=20