From nobody Sat Feb 7 23:12:14 2026 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 3BF561EEA47 for ; Fri, 2 May 2025 19:49:47 +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=1746215389; cv=none; b=h5TXivDunANF8r130Ln9qRWf+Gjsknk4v5RqqCMbqYpT9aK5A7I3f0ZKbqjLUMQ+SiYJ+Fon83nDnjDlVjjT21nSUqNwQy+tYugm2q+8giVR0VgYfBpucBQWw/MnGdp6ZydlxEoq6hacqAWcnzLduJ8x5dCHGgoNFs4pe+uip+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215389; c=relaxed/simple; bh=EHBhfCwaB/PnpKByIIKrHGgj32DfG0VS+hMiv2azs7Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=j7xcar7YEMqODEVpJXIJT25z295TEvVF2j2kAsPw/80GqgBAFG3+cJo8SIKnGK61TpLeaIDa5q+nXkSEVO9lr/DtYMaaTfi2qUcvGEOeaEpIancU1+eStxwB0SLmf1WG6JWHDlRspeOCJiS/JI8ASM/0VWcMCimgtK9HrXGmLfM= 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=FjA//ZqG; 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="FjA//ZqG" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3085f5855c4so2338045a91.1 for ; Fri, 02 May 2025 12:49:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746215387; x=1746820187; 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=GsTDMr/FRX+ryl4d/GP3feYD00SUaTvz4M5O3cBtWhE=; b=FjA//ZqGqSYOtFL/UCG/wODp4Tr6mrGHTN/TQmVmSGZnqfvUgc5nJmrr75pvmwgl98 zs3GVuS0Edt1ebRMfeVrcVXZPx/QybHb7vggVNU66j774xMS9oViG5nGMaQ/CfxN7T1r t7Kb9A56j+C5JheqwvVDjwWWMAMWRcQusiNH2mjBs5sLBt8wWquudKreGKINaGMb9LIv gU/fkEeINCwMrrLEzNtuCyOI49KmJBOjnBlH759lo5DWQ8WlTKNWVAcUyDeOY9FLnTxW ET4PzigKY7VsJSDZnGqYTeKKlqqIwNjEl6LQ36+AI/y+5KlMS+PUNMW4MkmgzkefacbC qakQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746215387; x=1746820187; 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=GsTDMr/FRX+ryl4d/GP3feYD00SUaTvz4M5O3cBtWhE=; b=A+/g9hlbmH0Gq5aTqSiOTKq8LSLL6dOoyNHFNoX1DYXhuR2wZXwinzxGUglKzMvwDs ubLrvkCkN4zZkySAckGILlbyiCqHCkR7ZV2GGA04WUr3Xe0cSA/rmdO4LHwRupxPk2PN eJPFDUb/7akk0S0Eei3w5qeInQx8p+EusXfFY9C2C0UhWSQz4SZ2QXjzZ7mFDBrng0Jq 3N3gQ+YxsGoy/Nu5PKZ+4WzAHXzxGSSrMpqBje7XZF9A/UL0Ogr5ljUtExKrZqQ5eUnZ 12tIWmhLpuJDuVjNDX+CMBh/OqaAKoHvXzq6DtAYc3z2m9LGjbh0eDFTHD8BKScRXq3M dgEA== X-Gm-Message-State: AOJu0YybxymKk3vsHTFbjZI1I6OVJPQoY00O2y/z6aLMvrMzfZ3Tu2vA uP4opgLktAhJzBJ+1J6u9B1SQ6uL7X9IHe1YBYhZO8/mvzDLI/KwfGd8uvgQMbfIJ1TQkJ7YLWH nAaPezw== X-Google-Smtp-Source: AGHT+IFr5KNNHP4xPoSKk/lUdp6dvgVBFZdQPVsIclv4/vIvl/QoBnVbfpDue/W09ra4J8bsX86pvUKjL55U X-Received: from pjp6.prod.google.com ([2002:a17:90b:55c6:b0:2f8:4024:b59a]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:57e7:b0:30a:214c:24c9 with SMTP id 98e67ed59e1d1-30a5adf9577mr681765a91.3.1746215387472; Fri, 02 May 2025 12:49:47 -0700 (PDT) Date: Fri, 02 May 2025 19:49:30 +0000 In-Reply-To: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746215384; l=7773; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=EHBhfCwaB/PnpKByIIKrHGgj32DfG0VS+hMiv2azs7Q=; b=yIYL/whoKwzDfKcDmWIHRAgn6Na22JDKKT0b1CxDWcKXcpURN+jcPF/Or45klFrbjmuuJJMA0 46lBzUW4g4uCLOYpBbFd/n2GhPlvWrYiWn2LbUxj5u7/TIG6Asr0mjl X-Mailer: b4 0.14.2 Message-ID: <20250502-debugfs-rust-v4-1-788a9c6c2e77@google.com> Subject: [PATCH v4 1/4] rust: debugfs: Bind DebugFS directory creation From: Matthew Maurer To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Sami Tolvanen , Timur Tabi Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Support creating DebugFS directories and subdirectories. Similar to the original DebugFS API, errors are hidden. By default, when a root directory handle leaves scope, it will be cleaned up. Subdirectories will by default persist until their root directory is cleaned up, but can be converted into a root directory if a scoped lifecycle is desired. Signed-off-by: Matthew Maurer --- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/debugfs.rs | 159 ++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/lib.rs | 1 + 4 files changed, 162 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 906881b6c5cb6ff743e13b251873b89138c69a1c..a3b835e427b083a4ddd690d9e77= 39851f0af47ae 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -7271,6 +7271,7 @@ F: include/linux/kobj* F: include/linux/property.h F: include/linux/sysfs.h F: lib/kobj* +F: rust/kernel/debugfs.rs F: rust/kernel/device.rs F: rust/kernel/device_id.rs F: rust/kernel/devres.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 8a2add69e5d66d1c2ebed9d2c950380e61c48842..787f928467faabd02a7f3cf0413= 78fac856c4f89 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs new file mode 100644 index 0000000000000000000000000000000000000000..41ac1711e9c0e66de1a434217c3= 63176f806f434 --- /dev/null +++ b/rust/kernel/debugfs.rs @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Google LLC. + +//! DebugFS Abstraction +//! +//! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h) + +use crate::str::CStr; +use core::marker::PhantomData; + +/// Owning handle to a DebugFS directory. +/// +/// This directory will be cleaned up when it goes out of scope. +/// +/// # Invariants +/// +/// The wrapped pointer will always be `NULL`, an error, or an owned Debug= FS `dentry`. +#[repr(transparent)] +pub struct Dir<'a, const KEEP: bool =3D false> { + #[cfg(CONFIG_DEBUG_FS)] + dir: *mut bindings::dentry, + // We need to be outlived by our parent, if they exist, but we don't a= ctually need to be able + // to access the data. + _phantom: PhantomData<&'a Dir<'a, true>>, +} + +// SAFETY: [`Dir`] is just a `dentry` under the hood, which the API promis= es can be transferred +// between threads. +unsafe impl Send for Dir<'_, KEEP> {} + +// SAFETY: All the native functions we re-export use interior locking, and= the contents of the +// struct are opaque to Rust. +unsafe impl Sync for Dir<'_, KEEP> {} + +impl<'a, const KEEP: bool> Dir<'a, KEEP> { + /// Create a new directory in DebugFS. If `parent` is [`None`], it wil= l be created at the root. + #[cfg(CONFIG_DEBUG_FS)] + fn create(name: &CStr, parent: Option<&Dir<'a= , PARENT_KEEP>>) -> Self { + let parent_ptr =3D match parent { + Some(parent) =3D> parent.as_ptr(), + None =3D> core::ptr::null_mut(), + }; + // SAFETY: + // * `name` argument points to a NUL-terminated string that lives = across the call, by + // invariants of `&CStr`. + // * If `parent` is `None`, `parent` accepts null pointers to mean= create at root. + // * If `parent` is `Some`, `parent` accepts live dentry debugfs p= ointers. + // so we can call `Self::from_ptr`. + let dir =3D unsafe { bindings::debugfs_create_dir(name.as_char_ptr= (), parent_ptr) }; + + // SAFETY: `debugfs_create_dir` either returns an error code or a = legal `dentry` pointer, + unsafe { Self::from_ptr(dir) } + } + + #[cfg(not(CONFIG_DEBUG_FS))] + fn create( + _name: &CStr, + _parent: Option<&Dir<'a, PARENT_KEEP>>, + ) -> Self { + Self() + } + + /// Constructs a new DebugFS [`Dir`] from the underlying pointer. + /// + /// # Safety + /// + /// The pointer must either be an error code, `NULL`, or represent a t= ransfer of ownership of a + /// live DebugFS directory. + #[cfg(CONFIG_DEBUG_FS)] + unsafe fn from_ptr(dir: *mut bindings::dentry) -> Self { + Self { + dir, + _phantom: PhantomData, + } + } + + /// Returns the pointer representation of the DebugFS directory. + /// + /// # Guarantees + /// + /// Due to the type invariant, the value returned from this function w= ill always be an error + /// code, NUL, or a live DebugFS directory. + // If this function is ever needed with `not(CONFIG_DEBUG_FS)`, hardco= de it to return + // `ERR_PTR(ENODEV)`. + #[cfg(CONFIG_DEBUG_FS)] + fn as_ptr(&self) -> *mut bindings::dentry { + self.dir + } + + /// Create a DebugFS subdirectory. + /// + /// This returns a [`Dir<'_, true>`], which will not be automatically = cleaned up when it + /// leaves scope. + /// To convert this to a handle governing the lifetime of the director= y, use [`Dir::owning`]. + /// + /// Regardless of conversion, subdirectory handles cannot outlive the = directory handle they + /// were created from. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let parent =3D Dir::new(c_str!("parent")); + /// let child =3D parent.subdir(c_str!("child")); + /// ``` + pub fn subdir<'b>(&'b self, name: &CStr) -> Dir<'b, true> { + Dir::create(name, Some(self)) + } +} + +impl<'a> Dir<'a, false> { + /// Create a new directory in DebugFS at the root. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let debugfs =3D Dir::new(c_str!("parent")); + /// ``` + pub fn new(name: &CStr) -> Self { + Dir::create::(name, None) + } +} + +impl<'a> Dir<'a, true> { + /// Upgrade a non-owning directory to one which will be removed on dro= p. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let debugfs =3D Dir::new(c_str!("parent")); + /// let subdir =3D debugfs.subdir(c_str!("child")); + /// // If subdir were dropped, the directory would not be removed. + /// let owned_subdir =3D subdir.owning(); + /// // If owned_subdir is dropped, "child" will be removed. + /// ``` + pub fn owning(self) -> Dir<'a, false> { + Dir { + dir: self.dir, + _phantom: self._phantom, + } + } +} + +impl Drop for Dir<'_, KEEP> { + fn drop(&mut self) { + #[cfg(CONFIG_DEBUG_FS)] + if !KEEP { + // SAFETY: `debugfs_remove` can take `NULL`, error values, and= legal DebugFS dentries. + // `as_ptr` guarantees that the pointer is of this form. + unsafe { bindings::debugfs_remove(self.as_ptr()) } + } + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index c3762e80b314316b4b0cee3bfd9442f8f0510b91..86f6055b828d5f711578293d891= 6a517f2436977 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -45,6 +45,7 @@ #[doc(hidden)] pub mod build_assert; pub mod cred; +pub mod debugfs; pub mod device; pub mod device_id; pub mod devres; --=20 2.49.0.906.g1f30a19c02-goog From nobody Sat Feb 7 23:12:14 2026 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 C29BE1F4C8C for ; Fri, 2 May 2025 19:49:49 +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=1746215391; cv=none; b=VNyun3DiL8P9FpW+QOyhPgZjHtZ/OTBBXRizCd6jf2w9sfymO/ukjxcBji3rej0hRqppABLGyNm2lJxYBwOxu/3F04GyvQVtQgeEsDStWO3xaRaR1xcgBzSJwtrxGQ11+9+Z75VGozK+AxK+54R6ztsEnKE2ff5izSLlWNbDPHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215391; c=relaxed/simple; bh=5WvGXldG5bcGQ6en9bVq55b0OrTvTyjsMF6SZd8Z0Bg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rgeH33iaXCOHnbFWbCuLWfnUrekmpTHf8t1d0vPdb5rC9swkb9nQN//KISIi63HVNzdkydlQOwxsnbZ8bDitV2/lOCSxYDlGXqlva2N8UvxnyR2alUN8a5yzwgxtR9DTJYQHF4F09dSrCxBBVjb5/Hn9NZ1z19NBhmG3kV/b25o= 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=QQepJyYo; 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="QQepJyYo" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-224192ff68bso21501565ad.1 for ; Fri, 02 May 2025 12:49:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746215389; x=1746820189; 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=LE24yftjPky7Y2IO1C0R+PH9HBG2ii3K+MN0duXsHJs=; b=QQepJyYoRuIA1naIpEltkpN1y9n76XNV8pGtj7V+kj4hHcMI7dPp8zFGa9Od16TQl7 huC8zmD0NtQz/VhwYuGo3Bu1vDf4iaXj1zm1gehYgPtcQi3X6DVFJxEhRZusn1zwplcV usEPLufja9f7oVoYCe7q2lf1qypHBm71pWt/srdKaDSQL2o96Y7pwUlzsQezByqOQG5C IySuQde1WhpTHJTf7/iFpcYHL58YBC8e4Sz/bvxmINBC4Kq35/ervFoNXrilJptXBHhn B5Q6zxK8uUnwSFeOqxv21n140elbcM34UcV1NJqT0YKz4NuHrIUezb5/EE5Gpf9XTLAz aTEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746215389; x=1746820189; 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=LE24yftjPky7Y2IO1C0R+PH9HBG2ii3K+MN0duXsHJs=; b=wZqN10Lj9lsubmF0tZ4Fob9gz/oGSfVtnf+f9+3+oTzJ4oZnvVCgHQQPaDkdjlTjv/ +YeKyBKxuz5NLsBMvNFOiPIAbd9yNMZmEWKDwV85BqOkPqaSp5DGCqsFoMqULION8jpq GDxNhpmDkqstUD9ZT70WhaLE/63D8g9aP2snKG2/4ZvSAajgw85t7ldqI7En8VDuhJp7 c1hniPPAAOZ8NALEKUw5xTbPNc2yZo/C+VSu1OW5WtG9HMoUpu5VNXElD28NIHzeOW9U WVXMJXYQ4AkmAOTSFf0nlrFd77IwBGOkIdMPWF4I/kw7AUSfi3w32CQrSrAijdxjUBV4 b5WQ== X-Gm-Message-State: AOJu0Yzz0YRRNsElmEzI2uCKgNP0c6kHYNS0gZjpVDV8oQ/wywdF7Bk1 4g4sSMseW8ABv1AkqwJKi1eUukl14d3dBeexUVH/E6mmKAkO3zVpEnh799Qk2guyDT0jeZXrJgD KBnFGzQ== X-Google-Smtp-Source: AGHT+IHkpua1Vghb+Jy8opdP02Q/1cQHwzORd+5iSClvFlwjf1VuPwqdumtC+R0Xu3VGwgvOukYgWBW6XEFp X-Received: from plhs15.prod.google.com ([2002:a17:903:320f:b0:21f:b748:a1c8]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1109:b0:223:4d7e:e52c with SMTP id d9443c01a7336-22e18b864damr5930405ad.5.1746215389085; Fri, 02 May 2025 12:49:49 -0700 (PDT) Date: Fri, 02 May 2025 19:49:31 +0000 In-Reply-To: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746215384; l=6716; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=5WvGXldG5bcGQ6en9bVq55b0OrTvTyjsMF6SZd8Z0Bg=; b=xNFl7XVnitDzw+Q68ShzQxJezdhBNjD8bPK6UkGs1VR4IZP3d4bykIm39W5i/pH3LTK6qYTAl ixqrmmeI2myBjbEK1+2ymiW8Xc9zMNnohxAHuZPlRIM9D/LFzZCuS5C X-Mailer: b4 0.14.2 Message-ID: <20250502-debugfs-rust-v4-2-788a9c6c2e77@google.com> Subject: [PATCH v4 2/4] rust: debugfs: Bind file creation for long-lived Display From: Matthew Maurer To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Sami Tolvanen , Timur Tabi Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Allows creation of files for references that live forever and lack metadata through the `Display` implementation. The reference must live forever because we do not have a maximum lifetime for the file we are creating. The `Display` implementation is used because `seq_printf` needs to route through `%pA`, which in turn routes through Arguments. A more generic API is provided later in the series, implemented in terms of this one. Signed-off-by: Matthew Maurer --- rust/kernel/debugfs.rs | 139 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 139 insertions(+) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index 41ac1711e9c0e66de1a434217c363176f806f434..21b116abad864d303f11cc515fe= 6f86ce5d51cbf 100644 --- a/rust/kernel/debugfs.rs +++ b/rust/kernel/debugfs.rs @@ -7,6 +7,7 @@ //! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h) =20 use crate::str::CStr; +use core::fmt::Display; use core::marker::PhantomData; =20 /// Owning handle to a DebugFS directory. @@ -108,6 +109,57 @@ fn as_ptr(&self) -> *mut bindings::dentry { pub fn subdir<'b>(&'b self, name: &CStr) -> Dir<'b, true> { Dir::create(name, Some(self)) } + + /// Create a file in a DebugFS directory with the provided name, and c= ontents from invoking + /// [`Display::fmt`] on the provided reference. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let dir =3D Dir::new(c_str!("my_debugfs_dir")); + /// dir.display_file(c_str!("foo"), &200); + /// // "my_debugfs_dir/foo" now contains the number 200. + /// ``` + pub fn display_file<'b, T: Display + Sized>( + &'a self, + name: &CStr, + data: &'static T, + ) -> File<'b> { + // 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. + // * `vtable` is all stock `seq_file` implementations except for `= open`. + // `open`'s only requirement beyond what is provided to all open= functions is that the + // inode's data pointer must point to a `T` that will outlive it= , which we know because + // we have a static reference. + #[cfg(CONFIG_DEBUG_FS)] + let ptr =3D unsafe { + bindings::debugfs_create_file_full( + name.as_char_ptr(), + 0o444, + self.as_ptr(), + data as *const _ as *mut _, + core::ptr::null(), + &::VTABLE, + ) + }; + + #[cfg(not(CONFIG_DEBUG_FS))] + let ptr =3D { + // Mark parameters used + let (_, _) =3D (name, data); + ERR_PTR(ENODEV) + }; + + // SAFETY: `debugfs_create_file_full` either returns an error code= or a legal + // dentry pointer, and without `CONFIG_DEBUGFS` we return an error= pointer, so + // `Dir::from_ptr` is safe to call here. + let dir =3D unsafe { Dir::from_ptr(ptr) }; + + File(dir) + } } =20 impl<'a> Dir<'a, false> { @@ -157,3 +209,90 @@ fn drop(&mut self) { } } } +/// Handle to a DebugFS file. +#[repr(transparent)] +pub struct File<'a>(Dir<'a, true>); + +impl<'a> File<'a> { + /// Remove the file from DebugFS. + /// + /// # Examples + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let dir =3D Dir::new(c_str!("foo")); + /// let file =3D dir.display_file(c_str!("bar"), &0); + /// // "foo/bar" is created. + /// file.remove() + /// // "foo/bar" is removed" + pub fn remove(self) { + drop(self.0.owning()) + } +} + +#[cfg(CONFIG_DEBUG_FS)] +mod helpers { + use crate::seq_file::SeqFile; + use crate::seq_print; + use core::fmt::Display; + use core::ptr::addr_of; + + /// Implements `open` for `file_operations` via `single_open` to fill = out a `seq_file`. + /// + /// # Safety + /// + /// * `inode`'s private pointer must point to a value of type `T` whic= h will outlive the `inode` + /// and will not be mutated during this call. + /// * `file` must point to a live, not-yet-initialized file object. + pub(crate) unsafe extern "C" fn display_open( + inode: *mut bindings::inode, + file: *mut bindings::file, + ) -> i32 { + // SAFETY: + // * `file` is acceptable by caller precondition. + // * `print_act` will be called on a `seq_file` with private data = set to the third argument, + // so we meet its safety requirements. + // * The `data` pointer passed in the third argument is a valid `T= ` pointer that outlives + // this call by caller preconditions. + unsafe { bindings::single_open(file, Some(display_act::), (*ino= de).i_private) } + } + + /// Prints private data stashed in a seq_file to that seq file. + /// + /// # Safety + /// + /// `seq` must point to a live `seq_file` whose private data is a live= pointer to a `T` which is + /// not being mutated. + pub(crate) unsafe extern "C" fn display_act( + seq: *mut bindings::seq_file, + _: *mut core::ffi::c_void, + ) -> i32 { + // SAFETY: By caller precondition, seq points to a live seq_file. + let private_addr =3D unsafe { addr_of!((*seq).private) }; + // SAFETY: By caller precondition, this pointer is live, points to= a value of type `T`, and + // is not being mutated. + let data =3D unsafe { &*(*private_addr as *mut T) }; + // SAFETY: By caller precondition, `seq_file` points to a live `se= q_file`, so we can lift + // it. + let seq_file =3D unsafe { SeqFile::from_raw(seq) }; + seq_print!(seq_file, "{}", data); + 0 + } + + // Work around lack of generic const items. + pub(crate) trait DisplayFile: Display + Sized { + const VTABLE: bindings::file_operations =3D bindings::file_operati= ons { + read: Some(bindings::seq_read), + llseek: Some(bindings::seq_lseek), + release: Some(bindings::single_release), + open: Some(display_open:: as _), + // SAFETY: `file_operations` supports zeroes in all fields. + ..unsafe { core::mem::zeroed() } + }; + } + + impl DisplayFile for T {} +} + +#[cfg(CONFIG_DEBUG_FS)] +use helpers::*; --=20 2.49.0.906.g1f30a19c02-goog From nobody Sat Feb 7 23:12:14 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.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 5E2671F7060 for ; Fri, 2 May 2025 19:49:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215393; cv=none; b=P7+QhHHcGsudjyzWDDPZhP7PHmYRMl2ST/WSNN7MGOWNgoRNk+Gyg1qoF693ziOWBtk6uYVBkibzgbrOyu4IFoit6WDRAGYup+1/wW1iLFKKW5l7+ngHwx/JNOyhzsWMSoeaLKMu4+Axt/MGEI/pkBo1nk4TqKcYO8L8BTviCdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215393; c=relaxed/simple; bh=JhApeNpm6KRKmDZL064E5Lc9ddDprNYbokO6XuBFr20=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cXv7srYrlESb4i5WiYPL40+VARz/+csmrZDRMuuLfuFBORHZXXfEwYhkJjtkNcxj5OGBAe+L5C2YqQx8OBGh5k3wmis5xL136gAIybEj48mqH68+MlK98Yp0XOUbeUeW7zpwuUe49Hm4ASh696rF7K8b3VUu3c+1fILb6ZdzZyw= 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=0IOK7RgY; arc=none smtp.client-ip=209.85.216.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--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="0IOK7RgY" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30872785c3cso3613110a91.1 for ; Fri, 02 May 2025 12:49:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746215391; x=1746820191; 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=iI6/+R49BrXGz9ZHCjQKdNUfmCZ6Bd/PYP3WfSK4eew=; b=0IOK7RgY+Qr6sCzBkgjFsqquhyTlWUq3baQYbT/mMGW2CZhWJ4t33DoXF+awKahmgY ww7VCdmKjkbnnk3aqxy0ZGCFGy3uaSRJl5BHkRiU3hkrccXevtLx8JjiGArKQjDDEEGl Plj+uPKoz9GAgiXkW5d5IOdcUxy1321KjtGxPEmVdKVaBMA09+IrA36/dyBnJwAf6s9O CQ6PwpPXDeCyBZgAIYGglSkj13mZ1jxh7ccCH3ErCnBSwRXyW84TV0O4K0mM8+o//+3l 8tdQMpXQVJ5M9zP/Nc9HLG81GLiiLC+X1cl9AIbZgatIqPADq+ELomDEzhsnjbwZMZ5w ov2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746215391; x=1746820191; 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=iI6/+R49BrXGz9ZHCjQKdNUfmCZ6Bd/PYP3WfSK4eew=; b=TE/OoY01HXvEh1qts+xeyUKmg71/bqllJpUIS99qcViq/fNRlVb3SUeBB42OOqZKRY eLlaOQ343aoIVWxs6xnnRjdR4hCysXvmrgAgFNRPaMDLd44RIE9+oGFxtB7kVub1Qqvn scpEllLw7bcCaz0DoYmL5F5xszKgN6XTB2xXvSQ/N9n1JdCmr2I79fIoRWBrVPw/coKE Z4lDXn+3qIK8CKTX7yvSkjfuuFvuiv2wOz56xe4pYGqWaITX0t6unrGy3hhlvSDBl7iX yJoSMf4sKEBBJOPG0eCInQFrC2aOaAQuT4wEoq228QhAdF0Z40ctTQ0nQKYPcv8t/p1s +p5g== X-Gm-Message-State: AOJu0YyB7rbh0ZH0nzSwCq/F5KRg3KVtf8ipQy1ZpUgFBfNysA8hZfSU fptzQ1dcQpq5xpBbks6jb78R4XEeIUedLAVdYasQi2NhxrxcsKdQ1bF7iTIx6Lb1hmkxXiEf1gb hbEq64w== X-Google-Smtp-Source: AGHT+IHTc7hZUu50VWfyrKHUUfkiL73Zvx1DsxEHHlEKNWDd0bBmcIuaBBUDVwG5blJVXkp7l2oKvJ8RZx23 X-Received: from pjbsq6.prod.google.com ([2002:a17:90b:5306:b0:2fa:2891:e310]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2ccd:b0:2ff:7c2d:6ff3 with SMTP id 98e67ed59e1d1-30a5aed74b2mr751065a91.35.1746215390696; Fri, 02 May 2025 12:49:50 -0700 (PDT) Date: Fri, 02 May 2025 19:49:32 +0000 In-Reply-To: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746215384; l=6651; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=JhApeNpm6KRKmDZL064E5Lc9ddDprNYbokO6XuBFr20=; b=i6MEteXCHFzqGFc9pgXr5/46UZJsVrF/7VEewzeYl9g8c+Ydi7wnJU9Xz8DXmJsIf/6ECr3gW m54roob78oLCLs1MY8gAb9nZomtj/5N86TpmTV+jQ3qZ0Vvv3tczok8 X-Mailer: b4 0.14.2 Message-ID: <20250502-debugfs-rust-v4-3-788a9c6c2e77@google.com> Subject: [PATCH v4 3/4] rust: debugfs: Support format hooks From: Matthew Maurer To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Sami Tolvanen , Timur Tabi Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Rather than always using Display, allow hooking arbitrary functions to arbitrary files. Display technically has the expressiveness to do this, but requires a new type be declared for every different way to render things, which can be very clumsy. Signed-off-by: Matthew Maurer --- rust/kernel/debugfs.rs | 124 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index 21b116abad864d303f11cc515fe6f86ce5d51cbf..cfdff63c10517f1aebf757c9652= 89a49fed6ae85 100644 --- a/rust/kernel/debugfs.rs +++ b/rust/kernel/debugfs.rs @@ -7,6 +7,7 @@ //! C header: [`include/linux/debugfs.h`](srctree/include/linux/debugfs.h) =20 use crate::str::CStr; +use core::fmt; use core::fmt::Display; use core::marker::PhantomData; =20 @@ -123,9 +124,55 @@ pub fn subdir<'b>(&'b self, name: &CStr) -> Dir<'b, tr= ue> { /// // "my_debugfs_dir/foo" now contains the number 200. /// ``` pub fn display_file<'b, T: Display + Sized>( - &'a self, + &'b self, name: &CStr, data: &'static T, + ) -> File<'b> { + // SAFETY: As `data` lives for the static lifetime, it outlives th= e file. + unsafe { self.display_file_raw(name, data) } + } + + /// 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 core::sync::atomic::{AtomicU32, Ordering}; + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// let dir =3D Dir::new(c_str!("foo")); + /// static MY_ATOMIC: AtomicU32 =3D AtomicU32::new(3); + /// let file =3D dir.fmt_file(c_str!("bar"), &MY_ATOMIC, &|val, f| { + /// let out =3D val.load(Ordering::Relaxed); + /// writeln!(f, "{out:#010x}") + /// }); + /// MY_ATOMIC.store(10, Ordering::Relaxed); + /// ``` + pub fn fmt_file<'b, T, F: Fn(&T, &mut fmt::Formatter<'_>) -> fmt::Resu= lt>( + &'b self, + name: &CStr, + data: &'static T, + f: &'static F, + ) -> File<'b> { + // SAFETY: As `data` lives for the static lifetime, it outlives th= e file + unsafe { self.fmt_file_raw(name, data, f) } + } + + /// Creates a DebugFS file backed by the display implementation of the= provided pointer. + /// + /// # Safety + /// + /// The pointee of `data` must outlive the accessibility of the `Dir` = returned by this function. + /// 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> { // SAFETY: // * `name` is a NUL-terminated C string, living across the call, = by `CStr` invariant. @@ -160,6 +207,32 @@ pub fn display_file<'b, T: Display + Sized>( =20 File(dir) } + + /// Create a file in a DebugFS directory with the provided name, and c= ontents from invoking the + /// fomatter on the attached data. + /// + /// The attached function must be a ZST, and will cause a compilation = error if it is not. + /// + /// # 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, + name: &CStr, + data: &T, + f: &'static F, + ) -> File<'b> { + #[cfg(CONFIG_DEBUG_FS)] + let data_adapted =3D FormatAdapter::new(data, f); + #[cfg(not(CONFIG_DEBUG_FS))] + let data_adapted =3D { + // Mark used + let (_, _) =3D (data, f); + &0 + }; + // SAFETY: data outlives the file's accessibility, so data_adapted= does too + unsafe { self.display_file_raw(name, data_adapted) } + } } =20 impl<'a> Dir<'a, false> { @@ -234,7 +307,9 @@ pub fn remove(self) { mod helpers { use crate::seq_file::SeqFile; use crate::seq_print; - use core::fmt::Display; + use core::fmt; + use core::fmt::{Display, Formatter}; + use core::marker::PhantomData; use core::ptr::addr_of; =20 /// Implements `open` for `file_operations` via `single_open` to fill = out a `seq_file`. @@ -292,6 +367,51 @@ pub(crate) trait DisplayFile: Display + Sized { } =20 impl DisplayFile for T {} + + /// Adapter to implement `Display` via a callback with the same repres= entation as `T`. + /// + /// # Invariants + /// + /// If an instance for `FormatAdapter<_, F>` is constructed, `F` is in= habited. + #[repr(transparent)] + pub(crate) struct FormatAdapter { + inner: T, + _formatter: PhantomData, + } + + impl FormatAdapter { + pub(crate) fn new<'a>(inner: &'a T, _f: &'static F) -> &'a Self { + // SAFETY: FormatAdapater is a repr(transparent) wrapper aroun= d T, so + // casting a reference is legal + // INVARIANT: We were passed a reference to F, so it is inhabi= ted. + unsafe { core::mem::transmute(inner) } + } + } + + impl Display for FormatAdapter + where + F: Fn(&T, &mut Formatter<'_>) -> fmt::Result + 'static, + { + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { + // SAFETY: FormatAdapter<_, F> can only be constructed if F is= inhabited + let f: &F =3D unsafe { materialize_zst_fmt() }; + f(&self.inner, fmt) + } + } + + /// For types with a unique value, produce a static reference to it. + /// + /// # Safety + /// + /// The caller asserts that F is inhabited + unsafe fn materialize_zst_fmt() -> &'static F { + const { assert!(core::mem::size_of::() =3D=3D 0) }; + let zst_dangle: core::ptr::NonNull =3D core::ptr::NonNull::dang= ling(); + // SAFETY: While the pointer is dangling, it is a dangling pointer= to a ZST, based on the + // assertion above. The type is also inhabited, by the caller's as= sertion. This means + // we can materialize it. + unsafe { zst_dangle.as_ref() } + } } =20 #[cfg(CONFIG_DEBUG_FS)] --=20 2.49.0.906.g1f30a19c02-goog From nobody Sat Feb 7 23:12:14 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 F370D1EB189 for ; Fri, 2 May 2025 19:49:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215394; cv=none; b=hJcap3MUF0UGxEo0STM75exLhTYtkbv3FRhIIW+i4IJf9DVfuuxRQ3eAyOzuo2fVH7Nxf8nF3imb5YUE0OmuD89TDSmPU7aRBvJnWK001H9iKLmwwGpKxwms126rvtFtUrDpSXxQT1/IeSkE4FvJE70qRoU43SRkjAPx+HB1m3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746215394; c=relaxed/simple; bh=xZyMIcm7Emay+xFbC5MyxDcgqVO90WS4L99HXwW+6Hk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NoZdpX8fuVAt7OgMnGrNhvffBtyas31UztcpxkL7YZpUYAso1hCT+klDvERG9kb6SPtbO2zAD6urYbnKT7ZyQpcsYhSB1uwJ/3RVG5OQviCrVLzaLccE0TtRbohPtulsQJPxKm23ScUgGj7tJGeDwoteNGxWPO5DwvGQEwxMVSU= 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=awqCuMAx; arc=none smtp.client-ip=209.85.215.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--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="awqCuMAx" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b0e0c573531so1627996a12.3 for ; Fri, 02 May 2025 12:49:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746215392; x=1746820192; 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=KrPFlgS27l600n2Kx8nhkVj8oNRd5F87R7Vo3kXSj00=; b=awqCuMAxQoxCxGwnUal89a0oqpmzwMOIrpVdo/V/6almKVMcreIlLHJxAY25a54icW h2FjLoiCPhwcYuerksd8mi6jYs4Ats92vXUZnjpwEiHGm8Zuqy7JwMMmzN5KFAp6ccPc 2lMSS7mgzielnbpl9EkXuqVYQUr0J8hy0fLO2qOPZLqxPVvfXrLlnrJzddm8dDTPXLHv LxC8Xy5DjDeS58/tioh30cwWe5T7osHcChxhvAgQxqjN9oOzKVIdaNLOB0ONYv/2e625 p/EBtJ+nSBYIpTu96MKI1WlwSyXHVqq4/VnaoNVZySOeThK3sfu8KyKt35a/ZqPVKcQC sQmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746215392; x=1746820192; 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=KrPFlgS27l600n2Kx8nhkVj8oNRd5F87R7Vo3kXSj00=; b=it9bW2PLBpoo85c0afeCQaObMssiQZxn4anQdSeE10wwuK+mcaawehNLg1CVJpKHmG KSADhyQ8ESujLk8zGmxxgN9QSFcVjyWDstNungrl+yLdVLjkOF0vYLPnpofldrFRDh/7 abpjOOLp/SjKd4pxXsf++ggwK27wC16rDvqmz8AoZTI/WWJf+nI5y+gzKnkJUHTiVL79 yZpWkicUqvMyiWaSqbwnIgp3/t5Vv0VuKPJIZmzynVnbWPEdwvLp38IoFkmrPFaAQDNU M6fXJeoc36JZw82F6mPO0wXQTO4+NjNAbdF2pCyrvPxa5+414x2RkSsI5uf1x1JoL4RH 0wVw== X-Gm-Message-State: AOJu0YwGlfMrNq4yxquMD6X5UUVbfhoZeVDJwRJK3R+jNWyoSitnsMZd wKh5RSbolDe6VJPA6EpN0i9NaITuNRz/I8FTG2Aci44oNEMSrRhxfiBomJD3f5Nb8122s6LwGiq BDUldHQ== X-Google-Smtp-Source: AGHT+IHXzAQ0WN3eqj8p8jAhysAFEED88RC2eQV6j4qy4qcb8ln2EPYngNWWfRwpth67Jppnp+Djkykw9B7Y X-Received: from pjx5.prod.google.com ([2002:a17:90b:5685:b0:308:7499:3dfc]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5252:b0:305:2d68:8d39 with SMTP id 98e67ed59e1d1-30a5ae1afa1mr914287a91.12.1746215392233; Fri, 02 May 2025 12:49:52 -0700 (PDT) Date: Fri, 02 May 2025 19:49:33 +0000 In-Reply-To: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250502-debugfs-rust-v4-0-788a9c6c2e77@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746215384; l=4692; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=xZyMIcm7Emay+xFbC5MyxDcgqVO90WS4L99HXwW+6Hk=; b=EE7UxGSeqBLFuYi+yvie2LinkeCeYpZpxuYWPqyO8OCcsvPCrI1h9hI1BIOR9p84mNRtiHFW+ Le+23ZPbyJ0DBcgy29twB0lxwDruxd62NKpCltJwh4iTSgQvt2KjuMs X-Mailer: b4 0.14.2 Message-ID: <20250502-debugfs-rust-v4-4-788a9c6c2e77@google.com> Subject: [PATCH v4 4/4] rust: samples: Add debugfs sample From: Matthew Maurer To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" , Sami Tolvanen , Timur Tabi Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Provides an example of using the Rust DebugFS bindings. Signed-off-by: Matthew Maurer --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 +++++++++ samples/rust/Makefile | 1 + samples/rust/rust_debugfs.rs | 56 ++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 69 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index a3b835e427b083a4ddd690d9e7739851f0af47ae..426bcdac025134e20911de8e2cf= 5c9efb0591814 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -7278,6 +7278,7 @@ F: rust/kernel/devres.rs F: rust/kernel/driver.rs F: rust/kernel/faux.rs F: rust/kernel/platform.rs +F: samples/rust/rust_debugfs.rs F: samples/rust/rust_driver_platform.rs F: samples/rust/rust_driver_faux.rs =20 diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index 43cb72d72631bb2d6e06185e1d88778edff6ee13..6c42ed73f842cda26256039e691= 7bb443738d3f1 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -51,6 +51,17 @@ config SAMPLE_RUST_DMA =20 If unsure, say N. =20 +config SAMPLE_RUST_DEBUGFS + tristate "DebugFS Test Driver" + depends on DEBUG_FS + help + This option builds the Rust DebugFS Test driver sample. + + To compile this as a module, choose M here: + the module will be called rust_debugfs. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_PCI tristate "PCI Driver" depends on PCI diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 6a466afd2a21eba84a3b7b2be29f25dce44e9053..b1fc4677ed53fcf7d0f5a3dbf32= 2f65851bc1784 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -4,6 +4,7 @@ ccflags-y +=3D -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) +=3D rust_minimal.o obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) +=3D rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) +=3D rust_print.o +obj-$(CONFIG_SAMPLE_RUST_DEBUGFS) +=3D rust_debugfs.o obj-$(CONFIG_SAMPLE_RUST_DMA) +=3D rust_dma.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) +=3D rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) +=3D rust_driver_platform.o diff --git a/samples/rust/rust_debugfs.rs b/samples/rust/rust_debugfs.rs new file mode 100644 index 0000000000000000000000000000000000000000..2b1119b7281fd15109b542e6853= d4206c2c80afc --- /dev/null +++ b/samples/rust/rust_debugfs.rs @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2025 Google LLC. + +//! Sample DebugFS exporting module + +use core::sync::atomic::{AtomicU32, Ordering}; +use kernel::c_str; +use kernel::debugfs::Dir; +use kernel::prelude::*; + +module! { + type: RustDebugFs, + name: "rust_debugfs", + authors: ["Matthew Maurer"], + description: "Rust DebugFS usage sample", + license: "GPL", +} + +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>, +} + +static EXAMPLE: AtomicU32 =3D AtomicU32::new(8); + +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")); + + { + // Create a subdirectory, so "sample_debugfs/subdir" now exist= s. + let sub =3D debugfs.subdir(c_str!("subdir")); + + // Create a single file in the subdirectory called "example" t= hat will read from the + // `EXAMPLE` atomic variable. + sub.fmt_file(c_str!("example"), &EXAMPLE, &|example, f| { + writeln!(f, "Reading atomic: {}", example.load(Ordering::R= elaxed)) + }); + // Now, "sample_debugfs/subdir/example" will print "Reading at= omic: 8\n" when read. + } + + // Change the value in the variable displayed by the file. This is= intended to demonstrate + // that the module can continue to change the value used by the fi= le. + EXAMPLE.store(10, Ordering::Relaxed); + // Now, "sample_debugfs/subdir/example" will print "Reading atomic= : 10\n" when read. + + // 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 + // continue to exist until the module is unloaded. + Ok(Self { _debugfs: debugfs }) + } +} --=20 2.49.0.906.g1f30a19c02-goog