From nobody Tue Dec 16 07:33:12 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 0044129899D for ; Tue, 6 May 2025 01:04:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746493459; cv=none; b=oba1cFZFHXmz1QgmnQpuGngwzEZSJhFJPSzy5LlcIDrprM013wzwqWII/dWiLjSMaslSdzo9LW8/s+IexJZGCowS6H1FIdyfT1lS0aefKy0zPVKrrClpWsgFg263nHwLf9mblgGBDQ1tGsTIy1YESSnbGC8czzWlE2eVOvROFBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746493459; c=relaxed/simple; bh=NAIRHl9jRwApoLdgJlgRFmMt3m5CcZE9so5qmj5XRdo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=C1Y6HYq/OPjUgkBvEgLW24SxT16dPbgLhY18GO/YWrgJEuEqpOqPMNHjFHlFaru2WTpLrLcZOfrqQOWcjoAAzYRd39BqOYqyZiMHA3cGRpaUTgzSKY6OMgoxnOD8KAozjLzVKEJzRRicguRhfiYrpEO4DxoqfW+zM8782zZsEp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tWNxcKGy; arc=none smtp.client-ip=209.85.214.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--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tWNxcKGy" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2242ce15cc3so47725315ad.1 for ; Mon, 05 May 2025 18:04:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746493457; x=1747098257; 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=YNg/75q6BfSe8D1KXAmFCz3H50TWhsSoQGs55Qs/RH8=; b=tWNxcKGymebuJEDsLT9E+wdS39VmAQ0p33b1d9a/RCzEynXpwhzbG27ZwjX0IOxGah SD04ZeYYTpPVz38PfPvxN4EkW/npmvsubXDtd204cbH2J+9mkAfC+H4Fi50bT8pWYMhW jpSTh1DYYxma5Yu3FtZ0+u7fV9KTUko5ZMTcS8iddj4MjDBvO7OqHdIRTDE3Lo/C+Eux jIfVvPDqf3GG10vb1xFsYKacQm6+1+6/dc75nOACZGUka3WMiNEUeRWsuM7whIPp5fF6 Pk1CHyU47luFL0qS7/Va1ikTLNkMmJbyTOJlw0EaGlxBCACKAqSlqPKNkBvb9gmvyG55 JKRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746493457; x=1747098257; 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=YNg/75q6BfSe8D1KXAmFCz3H50TWhsSoQGs55Qs/RH8=; b=Xvgz1Q917lpS33IQLt1yyus31LGHBnGu/EdqmtK7f055Z1BTQepddd0YU4Go7rQL0v QQ6LAWZrBzaxMthYE/rywig9FnWrDIur8XutEtkxZ69zeRMVEleqXjtBCWMMyAkLdJdd YLvClvDRVUaLskFzWpNNl2jGlOGRytZmTlJE94fkEu9EoJsuS198mQMUe3lMbA+3YvNJ pF+TMCm5IkCqrO7PuTUFgiNCNiUbzJy+8la9lKGDrC8B6nXhFgpr/Jl100bcWxBJ5HT3 PQSrDHGdzdkqfyWFUzUZdm+J1URSWaAMYa0VLIUxnAgUu5FgSgJVN0NsySxKPzjY81mr T8nQ== X-Forwarded-Encrypted: i=1; AJvYcCVGOp0Tbqti3AjBo69HTZ7CzZhvV+7LgthN3ZD66+Y0AV2RFORzf7L/gSA0qIknqWKuaabB4XhOjHyZ8sI=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1MEUzT0izJ56FhAvIHdrP9jtXR2NoAne/kG7izKJkSD5uHZc9 22IcqNvj77ozUphKjlQWu7fqdlChb2xGc+xZFLQWCIPwWHeJZDkkX4flg//zsLmuloOUYzKbDFP U5vns2A== X-Google-Smtp-Source: AGHT+IF6woG3grc42jD9/hM5kbZtDO4mWv1QeZMCoSy1twB/zymf2W6+Y2yIWZd/t0aJHDcjkf0blutiyAVD X-Received: from plso8.prod.google.com ([2002:a17:902:bcc8:b0:220:dc21:7a39]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d402:b0:22e:4509:cb8a with SMTP id d9443c01a7336-22e4509e0bbmr943085ad.21.1746493456998; Mon, 05 May 2025 18:04:16 -0700 (PDT) Date: Tue, 06 May 2025 01:04:13 +0000 In-Reply-To: <20250506-debugfs-rust-attach-v2-0-c6f88be3890a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250506-debugfs-rust-attach-v2-0-c6f88be3890a@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746493453; l=11182; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=NAIRHl9jRwApoLdgJlgRFmMt3m5CcZE9so5qmj5XRdo=; b=zWBWiJ3hiBmYSlpNujsN1jzH4rN0ip5oVQa4UNc5k39QLFJFcavaLbxOIMCWd1ndl/lTH3g4i KhK1E72hWX0BYe7Ur04/zZ6SGpeHZCDerAaRKM7y2mAsCOOuNHBKJmG X-Mailer: b4 0.14.2 Message-ID: <20250506-debugfs-rust-attach-v2-1-c6f88be3890a@google.com> Subject: [PATCH WIP v2 1/2] rust: debugfs: Add interface to build debugfs off pinned objects From: Matthew Maurer To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Sami Tolvanen , Timur Tabi Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Previously, we could only expose `'static` references to DebugFS because we don't know how long the file could live. This provides a way to package data alongside an owning directory to guarantee that it will live long enough to be accessed by the DebugFS files, while still allowing a dynamic lifecycle. Signed-off-by: Matthew Maurer --- rust/kernel/debugfs.rs | 218 +++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 208 insertions(+), 10 deletions(-) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index e2f5960bb87f24607780b3f4e67039e379f3bda6..560a2b68c7d28371ae11bc90efd= 15e7ada17ff77 100644 --- a/rust/kernel/debugfs.rs +++ b/rust/kernel/debugfs.rs @@ -5,10 +5,15 @@ //! //! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h) =20 +use crate::prelude::PinInit; use crate::str::CStr; use core::fmt; use core::fmt::Display; -use core::marker::PhantomData; +use core::marker::{PhantomData, PhantomPinned}; +use core::mem::ManuallyDrop; +use core::ops::Deref; +use core::pin::Pin; +use pin_init::pin_data; =20 /// Owning handle to a DebugFS entry. /// @@ -80,6 +85,19 @@ fn new() -> Self { fn as_ptr(&self) -> *mut bindings::dentry { self.entry } + + /// Rescopes the entry based on a borrow. + /// + /// # Safety + /// + /// Caller promises they will not drop the wrapped entry. + unsafe fn borrow<'b>(&'b self) -> ManuallyDrop> { + ManuallyDrop::new(Entry { + #[cfg(CONFIG_DEBUG_FS)] + entry: self.entry, + _phantom: PhantomData, + }) + } } =20 impl Drop for Entry<'_> { @@ -186,6 +204,8 @@ pub fn fmt_file<'b, T, F: Fn(&T, &mut fmt::Formatter<'_= >) -> fmt::Result>( f: &'static F, ) -> File<'b> { // SAFETY: As `data` lives for the static lifetime, it outlives th= e file + // As we return `File<'b>`, and we have a borrow to a directory wi= th lifetime 'b, we have a + // lower bound of `'b` on the directory. unsafe { self.fmt_file_raw(name, data, f) } } =20 @@ -197,11 +217,10 @@ pub fn fmt_file<'b, T, F: Fn(&T, &mut fmt::Formatter<= '_>) -> fmt::Result>( /// This means that before `data` may become invalid, either: /// * The refcount must go to zero. /// * The file must be rendered inaccessible, e.g. via `debugfs_remove= `. - unsafe fn display_file_raw<'b, T: Display + Sized>( - &'b self, - name: &CStr, - data: *const T, - ) -> File<'b> { + /// * If `self` may take longer than `'a` to be destroyed, the caller = is responsible for + /// shortening the lifetime of the return value to a lower bound for= the lifetime of that + /// object. + unsafe fn display_file_raw(&self, name: &CStr, dat= a: *const T) -> File<'a> { // SAFETY: // * `name` is a NUL-terminated C string, living across the call, = by `CStr` invariant. // * `parent` is a live `dentry` since we have a reference to it. @@ -243,13 +262,16 @@ unsafe fn display_file_raw<'b, T: Display + Sized>( /// /// # Safety /// - /// `data` must outlive the resulting file's accessibility - unsafe fn fmt_file_raw<'b, T, F: Fn(&T, &mut fmt::Formatter<'_>) -> fm= t::Result>( - &'b self, + /// * `data` must outlive the resulting file's accessibility + /// * If `self` may take longer than `'a` to be destroyed, the caller = is responsible for + /// shortening the lifetime of the return value to a lower bound for= the lifetime of that + /// object. + unsafe fn fmt_file_raw) -> fmt::R= esult>( + &self, name: &CStr, data: &T, f: &'static F, - ) -> File<'b> { + ) -> File<'a> { #[cfg(CONFIG_DEBUG_FS)] let data_adapted =3D FormatAdapter::new(data, f); #[cfg(not(CONFIG_DEBUG_FS))] @@ -280,6 +302,182 @@ pub fn new(name: &CStr) -> Self { #[repr(transparent)] pub struct File<'a>(Entry<'a>); =20 +/// A DebugFS directory combined with a backing store for data to implemen= t it. +#[pin_data] +pub struct Values { + dir: Dir<'static>, + // The order here is load-bearing - `dir` must be dropped before `back= ing`, as files under + // `dir` may point into `backing`. + #[pin] + backing: T, + // Since the files present under our directory may point into `backing= `, we are `!Unpin`. + #[pin] + _pin: PhantomPinned, +} + +impl Deref for Values { + type Target =3D T; + fn deref(&self) -> &T { + &self.backing + } +} + +impl Values { + /// Attach backing data to a DebugFS directory. + /// + /// Attached data will be available inside [`Values::build`] to use wh= en defining files in + /// the provided directory. When this object is dropped, it will remov= e the provided directory + /// before destroying the attached data. + /// + /// # Example + /// + /// ``` + /// # use kernel::{c_str, new_mutex}; + /// # use kernel::debugfs::{Dir, Values}; + /// let dir =3D Dir::new(c_str!("foo")); + /// let debugfs =3D KBox::pin_init(Values::attach(new_mutex!(0), dir),= GFP_KERNEL)?; + /// // Files can be put inside `debugfs` which reference the construct= ed mutex. + /// # Ok::<(), Error>(()) + /// ``` + pub fn attach(backing: impl PinInit, dir: Dir<'static>) -> impl Pin= Init { + pin_init::pin_init! { Self { + dir: dir, + backing <- backing, + _pin: PhantomPinned, + }} + } + + /// Runs a function which can create files from the backing data. + /// + /// # Example + /// + /// ``` + /// # use kernel::{c_str, new_mutex}; + /// # use kernel::debugfs::{Dir, Values}; + /// let dir =3D Dir::new(c_str!("foo")); + /// let debugfs =3D KBox::pin_init(Values::attach(new_mutex!(0), dir),= GFP_KERNEL)?; + /// debugfs.as_ref().build(|mutex, dir| { + /// // Can access `BoundDir` methods on `dir` here, with lifetime un= ified to the + /// // lifetime of `mutex` + /// }); + /// # Ok::<(), Error>(()) + /// ``` + pub fn build FnOnce(&'b T, BoundDir<'b>) -> U>(self: Pin= <&Self>, f: F) -> U { + // SAFETY: The `BoundDir` here is only being provided as a univers= ally quantified argument + // to a function, so in the body, it will only be available in an = existentially quantified + // context. This means that the function is legal to execute again= s the true lifetime of + // the directory, even if we don't know exactly what that is. + f(&self.backing, unsafe { BoundDir::new(&self.dir) }) + } +} + +/// A `Dir`, bound to the lifetime for which it will exist. Unlike `&'a Di= r`, this has an invariant +/// lifetime - it cannot be shortened or lengthened. +/// +/// # Invariants +/// +/// * `BoundDir`'s lifetime must match the actual lifetime the directory l= ives for. In practice, +/// this means that a `BoundDir` may only be used in an existentially qu= antified context. +/// * `dir` will never be promoted to an owning reference (e.g. via callin= g `Dir::owning`) +#[repr(transparent)] +pub struct BoundDir<'a> { + dir: ManuallyDrop>, + _invariant: PhantomData &'a ()>, +} + +impl<'a> BoundDir<'a> { + /// Create a new bound directory. + /// + /// # Safety + /// + /// `'a` is being used in a context where it is existentially quantifi= ed. + unsafe fn new(dir: &'a Dir<'_>) -> Self { + // SAFETY: We will keep the return wrapped in ManuallyDrop, so we = will not drop it. + let entry =3D unsafe { dir.0.borrow() }; + // Rewrap it into a directory + let dir =3D ManuallyDrop::new(Dir(ManuallyDrop::into_inner(entry))= ); + Self { + dir, + _invariant: PhantomData, + } + } + + /// Create a DebugFS subdirectory. + /// + /// This will produce another [`BoundDir`], scoped to the lifetime of = the parent [`BoundDir`]. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::{Dir, Values}; + /// let parent =3D Dir::new(c_str!("parent")); + /// let values =3D KBox::pin_init(Values::attach(0, parent), GFP_KERNE= L)?; + /// values.as_ref().build(|value, builder| { + /// builder.subdir(c_str!("child")); + /// }); + /// # Ok::<(), Error>(()) + /// ``` + pub fn subdir<'b>(&'b self, name: &CStr) -> BoundDir<'b> { + BoundDir { + dir: ManuallyDrop::new(Dir::create(name, Some(&self.dir))), + _invariant: PhantomData, + } + } + + /// Create a file in a DebugFS directory with the provided name, and c= ontents from invoking `f` + /// on the provided reference. + /// + /// `f` must be a function item or a non-capturing closure, or this wi= ll fail to compile. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{c_str, new_mutex}; + /// # use kernel::debugfs::{Dir, Values}; + /// let parent =3D Dir::new(c_str!("parent")); + /// let values =3D KBox::pin_init(Values::attach(new_mutex!(0), parent= ), GFP_KERNEL)?; + /// values.as_ref().build(|value, builder| { + /// builder.fmt_file(c_str!("child"), value, &|value, f| { + /// writeln!(f, "Reading a mutex: {}", *value.lock()) + /// }); + /// }); + /// # Ok::<(), Error>(()) + /// ``` + pub fn fmt_file) -> fmt::Result>( + &self, + name: &CStr, + data: &'a T, + f: &'static F, + ) -> File<'a> { + // SAFETY: As `data` lives for the same lifetime as our `BoundDir`= lifetime, which is + // instantiated as the actual lifetime of the directory, it lives = long enough. + // We don't need to shorten the file handle because `BoundDir`'s l= ifetime parameter is both + // a lower *and* upper bound. + unsafe { self.dir.fmt_file_raw(name, data, f) } + } + + /// Create a file in a DebugFS directory with the provided name, and c= ontents from invoking + /// [`Display::fmt`] on the provided reference with a trailing newline. + /// + /// # Examples + /// + /// ``` + /// # use kernel::{c_str, new_mutex}; + /// # use kernel::debugfs::{Dir, Values}; + /// let parent =3D Dir::new(c_str!("parent")); + /// let values =3D KBox::pin_init(Values::attach((1, 2), parent), GFP_= KERNEL)?; + /// values.as_ref().build(|value, builder| { + /// builder.display_file(c_str!("child_0"), &value.0); + /// builder.display_file(c_str!("child_1"), &value.1); + /// }); + /// # Ok::<(), Error>(()) + /// ``` + pub fn display_file(&self, name: &CStr, data: &'a T) -> Fi= le<'a> { + self.fmt_file(name, data, &|data, f| writeln!(f, "{}", data)) + } +} + #[cfg(CONFIG_DEBUG_FS)] mod helpers { use crate::seq_file::SeqFile; --=20 2.49.0.967.g6a0df3ecc3-goog From nobody Tue Dec 16 07:33:12 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86F512989A5 for ; Tue, 6 May 2025 01:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746493461; cv=none; b=rdBfYlXU7yRy4VZUG9XnD9Z5YuP5rtIrUOcBMrZ6qroF6On/4yHpmr/bMuauspZU+ExcAD19RiDOUVXwLdFNOAdxh7pibqqwV7icuv0FheJ04togNrO0k3YK7i4mS+ETq/qLG/UzWwf61Xm4nZYW25g14xNn9Ljhi83ZYgt9Ck0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746493461; c=relaxed/simple; bh=/k3Yo3khleRb+uxwsSODt3lvyFcZ63bMldnep7BhLE8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HGNkET8/4Rv1ayvZ6NL1JwxuWcqbkSGw15M963UdTmp5VlFdB9lys1NMG6MtplUyXVi4ymXA7AlDnRmeTvHSgwoMK9cHEuNu28eywsIb0XKNJvYX5ZWdT7eLBhdHfhZk3Y1EvzpFcublEbnd3BZPSCvEFvUvwig4fgUZSpox0K0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=z2OL1KhP; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="z2OL1KhP" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-309c6e43a9aso7554735a91.2 for ; Mon, 05 May 2025 18:04:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746493459; x=1747098259; 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=T8/F7HTaR1lenYv3bo9DPTN5D8KoLav+c1ChwtLfsdY=; b=z2OL1KhPrDx80f+1EzE6n1bGXnJxtn07RG6551YpXzLkwUZwy4oWlb+sV3TdKuBJ2V p36Mc8h02N7KovbbNxojEi0yRxBAAxw8KnkcXG3etQ9ezsnHUtTTf9gtpZ3XEm6rhWC4 aZtMf2OdOrL+eXm/K9380eStClgmJkNg7TX5hkQ6+mGHVQe8xKWJLsJKYPlYPdeAd0iw eKeCJ1a9lfS75SseSf6MKiJn76rgo7nQ8Oin2b1lFFsPKVP5ACMQuSq/ZlWkxqUDKZZT epKpy6x0IN6mKr2dnSgwn89+QCPKuPnJPXiFF0whDZyIzEB2tDheup3G7SNoADp7Eivi oRtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746493459; x=1747098259; 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=T8/F7HTaR1lenYv3bo9DPTN5D8KoLav+c1ChwtLfsdY=; b=NpqOXfebSSNGloHWw173x8ZdB43z3KoS9W/p2hIExHadD20FLz5zl7GNhv7DrW6rOd 38lqlt2+0hgDfYc224ghR8/y4Bd7uOe2FoMmITPW3WKLgFusDRJ9q05KvMoig8L5Darr W90ov5zy1PfTu1L4rFLgBd8LWw8vBadVO9GqSZV7616kShKp1+HzFu8Na/lou8XKWiTh IcS6oBzkyH28JbpuuLCEOBXt9XhRl8uCVvvt3bMKRn8In1NUh/oI44OtyOaRr9BZRuKb U98SAh4j4VQoh8CAYhqBNMBZTcK0dQ2/WhjpUw22UA3vZm1wIZvN3e6W+y3BnYxF4CtV ewUQ== X-Forwarded-Encrypted: i=1; AJvYcCUPZhgICupPQl4kwAfJ7WrC/91U4vQ1dsH2289vaHin9GhdOYktEkzgJcfjiZ01L9+Z4MqIkgbMcHaflGo=@vger.kernel.org X-Gm-Message-State: AOJu0Yyv6YjD3CpGZnZg4n2KnIlFTQfHoZolNElczh2gt+M9ftuTY0fN RvRueFZoJr9TR+jwE9JJ1/0VdCjyqCzqUEnRcNdOYgAXHqb2V7q9ShcXyp4M9Y9dXRuqdk03gqW n2TbO0Q== X-Google-Smtp-Source: AGHT+IHgCB4fVw2Q+BCHF9mjG2qX9mTc3bFJdne7hHQKkSQzvpo0cj2msg14MbGPMOt6w+ocQVkp0Nu6Jimf X-Received: from pjbtb7.prod.google.com ([2002:a17:90b:53c7:b0:301:2679:9d9]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:56c8:b0:2ee:8ea0:6b9c with SMTP id 98e67ed59e1d1-30a7c0806e9mr2600361a91.12.1746493458765; Mon, 05 May 2025 18:04:18 -0700 (PDT) Date: Tue, 06 May 2025 01:04:14 +0000 In-Reply-To: <20250506-debugfs-rust-attach-v2-0-c6f88be3890a@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250506-debugfs-rust-attach-v2-0-c6f88be3890a@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746493453; l=6273; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=/k3Yo3khleRb+uxwsSODt3lvyFcZ63bMldnep7BhLE8=; b=SCAhhh32aNFPz7cmDQI4C3sCPKGhapQWcPJVBsZsIU8GMTMEXvfsAyPFbxDsscRgcSw/LU+F5 c2ytLqN7pIQDKauKD6eB5tfJrsfjzMQDWKo3KQoXIHQzjSAPgRZOPY5 X-Mailer: b4 0.14.2 Message-ID: <20250506-debugfs-rust-attach-v2-2-c6f88be3890a@google.com> Subject: [PATCH WIP v2 2/2] rust: debugfs: Extend sample to use attached data From: Matthew Maurer To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Sami Tolvanen , Timur Tabi Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Demonstrates how to attach data/handles needed for implementing DebugFS file to a directory. Signed-off-by: Matthew Maurer --- samples/rust/rust_debugfs.rs | 110 +++++++++++++++++++++++++++++++++++++++= ++-- 1 file changed, 106 insertions(+), 4 deletions(-) diff --git a/samples/rust/rust_debugfs.rs b/samples/rust/rust_debugfs.rs index a4b17c8241330b2f6caf8f17a5b2366138de6ced..efaca1b140f710fcc7d0467e9b0= 0e02a69c2cf55 100644 --- a/samples/rust/rust_debugfs.rs +++ b/samples/rust/rust_debugfs.rs @@ -4,11 +4,15 @@ =20 //! Sample DebugFS exporting module =20 +use core::fmt; +use core::fmt::{Display, Formatter}; use core::mem::{forget, ManuallyDrop}; use core::sync::atomic::{AtomicU32, Ordering}; use kernel::c_str; -use kernel::debugfs::Dir; +use kernel::debugfs::{BoundDir, Dir, Values}; +use kernel::new_mutex; use kernel::prelude::*; +use kernel::sync::{Arc, Mutex}; =20 module! { type: RustDebugFs, @@ -21,7 +25,89 @@ struct RustDebugFs { // As we only hold this for drop effect (to remove the directory) we h= ave a leading underscore // to indicate to the compiler that we don't expect to use this field = directly. - _debugfs: Dir<'static>, + _debugfs: Pin>>, +} + +struct Composite { + major: u32, + minor: u32, +} + +struct Record { + name: &'static CStr, + size: usize, + stride: usize, +} + +struct Backing { + simple: u32, + composite: Composite, + custom: u32, + many: KVec, + atomic: AtomicU32, + locked: Arc>, +} + +impl Display for Composite { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}.{}", self.major, self.minor) + } +} + +impl Backing { + fn new() -> Result { + let mut many =3D KVec::new(); + many.push( + Record { + name: c_str!("foo"), + size: 1, + stride: 2, + }, + GFP_KERNEL, + )?; + many.push( + Record { + name: c_str!("bar"), + size: 3, + stride: 4, + }, + GFP_KERNEL, + )?; + Ok(Self { + simple: 10, + composite: Composite { major: 1, minor: 2 }, + custom: 37, + many, + atomic: AtomicU32::new(7), + locked: Arc::pin_init(new_mutex!(0), GFP_KERNEL)?, + }) + } + + fn build<'a>(&'a self, root: BoundDir<'a>) { + // Just prints out the number in the field + forget(root.display_file(c_str!("simple"), &self.simple)); + // Uses the custom display implementation to print major.minor + forget(root.display_file(c_str!("composite"), &self.composite)); + // Uses the custom hook print the number in 0-padded hex with some= decorations. + forget(root.fmt_file(c_str!("custom"), &self.custom, &|custom, f| { + writeln!(f, "Foo! {:#010x} Bar!", custom) + })); + // Creates a directory for every record in the list, named the nam= e of the item, with files + // for each attribute. + for record in self.many.iter() { + let dir =3D ManuallyDrop::new(root.subdir(record.name)); + forget(dir.display_file(c_str!("size"), &record.size)); + forget(dir.display_file(c_str!("stride"), &record.stride)); + } + // Access the attached atomic via `.load()` + forget(root.fmt_file(c_str!("atomic"), &self.atomic, &|atomic, f| { + writeln!(f, "{}", atomic.load(Ordering::Relaxed)) + })); + // Access the attached mutex-guarded data via `.lock()` + forget(root.fmt_file(c_str!("locked"), &self.locked, &|locked, f| { + writeln!(f, "{}", *locked.lock()) + })); + } } =20 static EXAMPLE: AtomicU32 =3D AtomicU32::new(8); @@ -29,13 +115,13 @@ struct RustDebugFs { impl kernel::Module for RustDebugFs { fn init(_this: &'static ThisModule) -> Result { // Create a debugfs directory in the root of the filesystem called= "sample_debugfs". - let debugfs =3D Dir::new(c_str!("sample_debugfs")); + let root =3D Dir::new(c_str!("sample_debugfs")); =20 { // Create a subdirectory, so "sample_debugfs/subdir" now exist= s. // We wrap it in `ManuallyDrop` so that the subdirectory is no= t automatically discarded // at the end of the scope - it will be cleaned up when `debug= fs` is. - let sub =3D ManuallyDrop::new(debugfs.subdir(c_str!("subdir"))= ); + let sub =3D ManuallyDrop::new(root.subdir(c_str!("subdir"))); =20 // Create a single file in the subdirectory called "example" t= hat will read from the // `EXAMPLE` atomic variable. @@ -51,6 +137,22 @@ fn init(_this: &'static ThisModule) -> Result { EXAMPLE.store(10, Ordering::Relaxed); // Now, "sample_debugfs/subdir/example" will print "Reading atomic= : 10\n" when read. =20 + // We can also attach data scoped to our root directory + let backing =3D Backing::new()?; + // Grab a refcount pointing to the locked data + let locked =3D backing.locked.clone(); + let debugfs =3D KBox::pin_init(Values::attach(backing, root), GFP_= KERNEL)?; + + // Once it's attached, we can invoke `Backing::build` to create th= e relevant files: + debugfs.as_ref().build(Backing::build); + + // We can still access read-only references the contents of the at= tached values. If the + // values allow interior mutability, like atomics, this lets us st= ill change them: + debugfs.as_ref().atomic.store(8, Ordering::Relaxed); + + // If we attached refcounted data, we can use an external handle t= o access it + *locked.lock() =3D 42; + // Save the root debugfs directory we created to our module object= . It will be // automatically cleaned up when our module is unloaded because dr= opping the module object // will drop the `Dir` handle. The base directory, the subdirector= y, and the file will all --=20 2.49.0.967.g6a0df3ecc3-goog