From nobody Sun Feb 8 04:11:15 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 72328251790 for ; Thu, 1 May 2025 22:47:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139670; cv=none; b=Jeyc4UxTnz4V08KtLW3ge39PXlI/4GloWu3krFk0xhJ3o0Q05fyME7mBpYIgokF7i5zjFU95QkaO9TIXgvfci70O0epB7Cem/63HBA5vP1kir4ol2tU3Q+lowTj2JEe6MQPUCJGbsQKl95Zter105RCCePtARzgatQaxuVXCb+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139670; c=relaxed/simple; bh=JbEd4yVGkc4CGLLwMtsUZunY8qOMQArWopLk0wZkvOI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TtjwMYHe3OrKIRrx4/fwfNCEdqyo/uDCwgyqYT2Q86gy8Lg+Z/nONEW71TC242OxnB6yEIBf4YKEiQOtHeBX3c1eN/AkoiQCrcAvSycjxonUk8jirE5lqiZApTkvhslahni5Sy+S/y3iC9dIFy4uBqdUj2eCD2WI5tFVoncLflQ= 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=QWaTUBI4; arc=none smtp.client-ip=209.85.210.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="QWaTUBI4" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7394792f83cso1181950b3a.3 for ; Thu, 01 May 2025 15:47:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746139667; x=1746744467; 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=w44KCo9lpA5CgPVgm2HVbQSg5eZSvHUWTxcca4PJz2I=; b=QWaTUBI40VapxdSFugblR0rqXds4ltyW4b6WS4cLWNv3vShBXzQjRImZ9iHLM/5GvA NC4zmEUcWlKGm1rGw6RWICgNFTHBrSC9IdX/sI6ixrMsO2rCAhmf+PCETqzqXDEpSnvn DxcbMXxIj1vGnZDBBZgRSCsnO0TJ0EF7b73WdKk79ogn1ibTjAQvv8wDl922uAE7Lme+ L986mOoEcrJWFACBoKINMb0k09jR9wiJsDD3PTLnSEZ3/e9jWiSyZwaDoFsNuED/7jgh bGjVJ9NrRWyqri0cT/kVFeGBImVu/hhSGAlbKcTlhjkiQuVA3g32qvnqhnGh/SVahvLp Ab4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746139667; x=1746744467; 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=w44KCo9lpA5CgPVgm2HVbQSg5eZSvHUWTxcca4PJz2I=; b=amIiw1lcpkTP9t+tOdZ33az/NTtNuFglDJebdKEdEwsTPc+Iy/dqKnC3kuESJG/wdM EdYA3GKAWCWq00fQWLzSjr1BAemwRprNuT5NK8FWsSnDhn3U+Umzfd1CAdpwFOasub4G S85HYTn855fquqq7mspcjEJVxDxWtMMW2GBj45AB3rP8JycURYUE6H2u/GgKYPl0WhlR LjIW6Lxq3whsuaCTq9Vd+cTiaNqRSFSHPSr6vv7VnkAN2vCYvjPv+b1bxwJGaM/uCRnp TpVIr/4a26xcbUsB34lP9nQmC3/qNMXEAQ9ql8RR2Ue/I6YGIY3wsMQlANz7x3C3MhgI Sj0g== X-Gm-Message-State: AOJu0YzSqOGA9SLep5F8JQI1blac5Ah+juZm1122qSY69Yx7JGSXuSio SjLoyDTYLLsiH62FDXhHIUwE5N81QrljfO57o8PdsWCWGt2QTPNcuaL5oX22PvSOkUooXYyDQR0 +tonSiQ== X-Google-Smtp-Source: AGHT+IGlVa2Bg4kQzTXqXrUtJZZg3o01lqCHz4aOmRSyKYFZMXefdnfozoziz1aewpPKP4pxqQiT2a9yT57m X-Received: from pgbcz7.prod.google.com ([2002:a05:6a02:2307:b0:b0b:301e:8e96]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:394f:b0:1f5:7b6f:f8e8 with SMTP id adf61e73a8af0-20cde36d531mr991871637.6.1746139666691; Thu, 01 May 2025 15:47:46 -0700 (PDT) Date: Thu, 01 May 2025 22:47:41 +0000 In-Reply-To: <20250501-debugfs-rust-v3-0-850869fab672@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250501-debugfs-rust-v3-0-850869fab672@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746139663; l=7385; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=JbEd4yVGkc4CGLLwMtsUZunY8qOMQArWopLk0wZkvOI=; b=AQJYJpVCc5ljzQ9PEPNKo50FEdhNQQ74R4AtsT0cANrJVwYSmxn7/WQtCFRE6M2tDFvw+OqP1 6Jx0HAB9yTtCPeDIhKlVC640Z65Flm/b6EzeipyXYoguP9JHww8qSpv X-Mailer: b4 0.14.2 Message-ID: <20250501-debugfs-rust-v3-1-850869fab672@google.com> Subject: [PATCH v3 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 | 155 ++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/lib.rs | 1 + 4 files changed, 158 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..b589c2d9a8d169bd66e98d28942= 61784e427230e --- /dev/null +++ b/rust/kernel/debugfs.rs @@ -0,0 +1,155 @@ +// 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::mem::ManuallyDrop; +use core::ops::Deref; + +/// 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(#[cfg(CONFIG_DEBUG_FS)] *mut bindings::dentry); + +// SAFETY: Dir is just a `dentry` under the hood, which the API promises c= an be transferred +// between threads. +unsafe impl Send for Dir {} + +// 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 {} + +impl Dir { + /// Create a new directory in DebugFS at the root. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// { + /// let parent =3D Dir::new(c_str!("parent")); + /// // The path "parent" exists in DebugFS here. + /// } + /// // It does not exist here. + /// ``` + pub fn new(name: &CStr) -> Self { + Self::create(name, None) + } + + /// Create a DebugFS subdirectory. + /// + /// This returns a [`SubDir`], 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::from`]. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// { + /// let parent =3D Dir::new(c_str!("parent")); + /// // The path "parent" exists in DebugFS here. + /// { + /// let child =3D parent.subdir(c_str!("child")); + /// // The path "parent/child" exists in DebugFS here. + /// } + /// // The path "parent/child" still exists. + /// { + /// let child2 =3D Dir::from(parent.subdir(c_str!("child2"))); + /// // The path "parent/child2" exists in DebugFS here. + /// } + /// // The path "parent/child2" is gone. + /// } + /// // None of the paths exist here. + /// ``` + pub fn subdir(&self, name: &CStr) -> SubDir { + SubDir::new(Self::create(name, Some(self))) + } + + /// 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<&Self>) -> 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. + // * `debugfs_create_dir` either returns an error code or a legal = `dentry` pointer, + // so we can call `Self::from_ptr`. + unsafe { Self::from_ptr(bindings::debugfs_create_dir(name.as_char_= ptr(), parent_ptr)) } + } + + #[cfg(not(CONFIG_DEBUG_FS))] + fn create(_name: &CStr, _parent: Option<&Self>) -> 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(ptr: *mut bindings::dentry) -> Self { + Self(ptr) + } + + /// Returns the pointer representation of the DebugFS directory. + /// + /// 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 `ENODEV`. + #[cfg(CONFIG_DEBUG_FS)] + fn as_ptr(&self) -> *mut bindings::dentry { + self.0 + } +} + +impl Drop for Dir { + fn drop(&mut self) { + // SAFETY: `debugfs_remove` can take `NULL`, error values, and leg= al DebugFS dentries. + // `as_ptr` guarantees that the pointer is of this form. + #[cfg(CONFIG_DEBUG_FS)] + unsafe { + bindings::debugfs_remove(self.as_ptr()) + } + } +} + +/// Handle to a DebugFS directory that will stay alive after leaving scope. +#[repr(transparent)] +pub struct SubDir(ManuallyDrop); + +impl Deref for SubDir { + type Target =3D Dir; + fn deref(&self) -> &Dir { + &self.0 + } +} + +impl From for Dir { + fn from(subdir: SubDir) -> Self { + ManuallyDrop::into_inner(subdir.0) + } +} + +impl SubDir { + fn new(dir: Dir) -> Self { + Self(ManuallyDrop::new(dir)) + } +} 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 Sun Feb 8 04:11:15 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 0FB391EA7FF for ; Thu, 1 May 2025 22:47:48 +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=1746139670; cv=none; b=Kz2knxlmf70EvpUGEYRorWeKpNm/DivLNmyJBKHcMMSHuoYUyGy9TMEYueTDcE1xBvfyYdmohEPC/qQXhwgGReamRXVfTSW/+5h0mOUF+txsVLsfJ8L7lUOI/OPvEZXXWsOsV4+mGtvlD1mvZJ/R5VBmXZHBATmKl7IObSsBtuk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139670; c=relaxed/simple; bh=vGtX626ufCW0kxKZSGGr1DsjP6wp5nwALWioEPKyoDM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ccF1aAtpUDuvqpTSsjRL5FE3FqKhCL9nmthC/E7tVjMyv0gD3F8AMa8gSsf6k9TzT2ONJqPhxbAL0Gklxiz6bVcNOj4GGkfuZdxeiT3mSzbktOHVVC2jxEdW89M2fNe+2I1Y+suvmsdtWFtaTfaa5WkRcF6F0UqohyL5hq3UNWM= 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=LqHr5sVi; 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="LqHr5sVi" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff64898e2aso1894991a91.1 for ; Thu, 01 May 2025 15:47:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746139668; x=1746744468; 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=C38GHycutHTGIWUmaIQeGI20h3XTl61MbEqtw6bDUhs=; b=LqHr5sViKkc8XdCVfMZ/qfNmNqyufxfI4D0ul1Pjt6zP+ORg7jnFcYGCl/x2FxdirC kDy9+FK8mxqEcYj1DlQQ1kb1dJnF+ynAz7Kx4glFDBxGPiNCd1jTzIzpMMRX0jOPSU7b GhJE5pX9Igysc3Cgq8KZ+akwF24NmBc0s2Sp+/tuuQlRGdfzD2abpURNQtt/DzxVKP3L th3nFWVBWaJazaruZOwHYKn/SraKT+l1QI5u9zr2LCGBgjoHXFZlj5iMuCZMOLeBYEC8 Ht2MbpMkGMcK7ks00E28JwspzXd6FPbQUmQXIoGdIrJW/lP+IzphwGt/O+YamGS10X9W ycFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746139668; x=1746744468; 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=C38GHycutHTGIWUmaIQeGI20h3XTl61MbEqtw6bDUhs=; b=dm0t1rzCtVf7d9hlMH7H2eQlNqH52aMP2IHArLZ8LaPvedr6fC0GY2o4OlI4JGPqOd GiCP5D1mlXScrGQ7fXStOO0Vzjw6+JyHvl2n0d5YRh0OaybRL11DYaguuYFI1LSc/ZCc Kr8DJfaZGS+6aTeRspKPqjkmPXfsV9EY7+eqkuMALd4KdWiGe5sEgyHqqjydfolT0cKb DOAPctSRIgp4cs0zVmmSBZk1Sbi5rUscMX1aBAYmyRAqs++WFqAl1lzJSMCJFR/sqqSZ yzu6drC+7m+WOrdiNkAFv6iY0J6Aelp5mzeNaCZTJ46vNuYaZsbjoIbD8Z2wpoiJS1+L jhJA== X-Gm-Message-State: AOJu0Yza2/drbdSZr5iB6Foj63UkP8t/9sLGFj4K/V2rjPf80mcPGMBb kAUGWLiArYrcbLMjRwLrRX32+qrwFFOUHBNS4toZYRXWVaZEZA2ycM5Rr+ruwj0u39vW01gHFDe kykyWqw== X-Google-Smtp-Source: AGHT+IEHhAjLNh4B5uuSy96gpwkNJdmG0NJVHyV4NzmklUFQ0ooeDa27MV9sjJQh/AhLvjGleYqTMwlo7A3y X-Received: from pjbqn16.prod.google.com ([2002:a17:90b:3d50:b0:2f9:dc36:b11]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:35c2:b0:2fa:603e:905c with SMTP id 98e67ed59e1d1-30a42e4ec2bmr6434727a91.2.1746139668389; Thu, 01 May 2025 15:47:48 -0700 (PDT) Date: Thu, 01 May 2025 22:47:42 +0000 In-Reply-To: <20250501-debugfs-rust-v3-0-850869fab672@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250501-debugfs-rust-v3-0-850869fab672@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746139663; l=6574; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=vGtX626ufCW0kxKZSGGr1DsjP6wp5nwALWioEPKyoDM=; b=U+ouHWH+j8aDuCE2NAbl2aFiHOzXeoikFWOS1YbGeOihe8jY5adamc/Zb4JfweIvqZN58P5x4 f4He64v6TqzBLY4dcobtiGUtFVXDcEZ+WkRM7GNNkqdi3HCkPBdyp0y X-Mailer: b4 0.14.2 Message-ID: <20250501-debugfs-rust-v3-2-850869fab672@google.com> Subject: [PATCH v3 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 | 134 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 134 insertions(+) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index b589c2d9a8d169bd66e98d2894261784e427230e..ef69ae8f550a9fe6b0afc1c51be= baad2fc087811 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::mem::ManuallyDrop; use core::ops::Deref; =20 @@ -118,6 +119,48 @@ unsafe fn from_ptr(ptr: *mut bindings::dentry) -> Self= { fn as_ptr(&self) -> *mut bindings::dentry { self.0 } + + /// 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(&self, name: &CStr, data: &'st= atic T) -> File { + // 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. + // * debugfs_create_file_full either returns an error code or a le= gal dentry pointer, so + // `Self::from_ptr` is safe to call here. + #[cfg(CONFIG_DEBUG_FS)] + let dir =3D unsafe { + Self::from_ptr(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 dir =3D { + // Mark parameters used + let (_, _) =3D (name, data); + Self() + }; + File(ManuallyDrop::new(dir)) + } } =20 impl Drop for Dir { @@ -153,3 +196,94 @@ fn new(dir: Dir) -> Self { Self(ManuallyDrop::new(dir)) } } +/// Handle to a DebugFS file. +#[repr(transparent)] +pub struct File(ManuallyDrop); + +impl File { + /// 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. + /// } + /// // "foo/bar" still exists. + /// { + /// let file =3D dir.display_file(c_str!("baz"), &0); + /// // "foo/baz" is created. + /// file.remove(); + /// // "foo/baz" is gone. + /// } + pub fn remove(self) { + drop(ManuallyDrop::into_inner(self.0)) + } +} + +#[cfg(CONFIG_DEBUG_FS)] +mod helpers { + use crate::seq_file::SeqFile; + use crate::seq_print; + use core::fmt::Display; + + /// 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, this pointer is live, points to= a value of type `T`, and + // is not being mutated. + let data =3D unsafe { &*((*seq).private 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 Sun Feb 8 04:11:15 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 8F3C2265CD6 for ; Thu, 1 May 2025 22:47:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139672; cv=none; b=DO3qD4fYLCczHFx0PaIIhhSfydcBo513/8d5KWEeNUTjJNP3A+xDU6a5sCiV5ExKRmoMQGF6n/B3NQKTBbIQQh4xNXhUbxByt31M3SY7fuJH46JksZruDDqm9kifrOkdmXF17ri0S2hFRPV9UcpwAEk3Pfw+MaTnek7XFGSG89U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139672; c=relaxed/simple; bh=HbnP5HAr/7GrvIZ/usUBfS0jEKFbS6C4taoCnpgU62M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XKOCx+uVVpCyVT7tp6VjPaF7DrKj7j+Fmb8zymcClxG2hopM9UPEi5yQrlNwVe5qizOICyYVN7eFhaXQSZuadh5dvTfAG+DC16vfxPrglI6+5Ou8Pn1okfdsbzA302gb/+q736fot8ZuDC4QoYAd6kpfWp1OZjssS2Qd+AAnQLU= 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=l2v97HjN; arc=none smtp.client-ip=209.85.210.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="l2v97HjN" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-73c09e99069so1611193b3a.0 for ; Thu, 01 May 2025 15:47:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746139670; x=1746744470; 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=QvSZKcf5Ez4NdQxzj0zDxtHsG3SSXzKb6tYz9Hk+k2s=; b=l2v97HjNkg1OY7WuM7mO56VE1xMMbEb+Ltm7o4tZkEkBJKvPGkUAsAKzukxPMZKMv3 Djvks725XsXIxPmLTnFk//9W+XD7buVgiGBuX6TREGTZdAY3dlquBX8SMQqbgQm4l5kg RhKltHUA1I4zfexp6QaGCtPQBSlUDnbPqnMGGzOoBXikStDwdo6lKpQ57vZTvcwrDahf wBzSWNZWSTqbWEmckJUr3mtbRCa01jhQ725kQRD4G50ChjbIZS/hUm4m/24XIYBF+aIt gzKuOLfPijKxWIhK42D9FBhfqJl5bV9yWDcWPOqk+dX6abA4CI+FX5DCoJAGwCgctFH4 xv6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746139670; x=1746744470; 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=QvSZKcf5Ez4NdQxzj0zDxtHsG3SSXzKb6tYz9Hk+k2s=; b=e4+JtsvhrYEZ1YQ7g10IPVYi4TrWegZVsspVdworzzD7Qc30Yo7Fwe5UI3B+W4yBrY hIYYmmGuHkBL9F8AhUjE+1HI2IB7P12DGtnARX01tLDaojQ2TNYDTj8PWAwM2MzUUvwy DT2pM7vEqLAdK154JY1cZ8HFGSEsHlLhw4AS7HLG+J3CnvFAcd4mhjZxxONcikhoGKMu PSO0lW0zlUOr2cwmTWfQHRtUk/+rp2eSj5kjCeycxR3oyfNRs4VaJnuumBRKjEBqB2FO LRyLEDwEou3/9TyqX0IU1txBiU6KlNNKNR02kTESK3GJqp19FVpO4exDv/dpaBfVWeie 5tTw== X-Gm-Message-State: AOJu0Yxgc1ASplK4+7bHCEyQb9kKtjlMq1eYWACqQ04yrCzg3EKGgZ51 VYTe3qtWSfNM4OfcdNkcTj0Tp4Lzc6+wkHGyl5skqa1N+Bs5FA3PGqH4hrxEQSu88qwXVGugo8D C81R0LQ== X-Google-Smtp-Source: AGHT+IEPkIEce4lfj55Uh62mCtxMLmgEn7c5rBVaAwAF/1wZO8ZRtpT8zH0RVHOjC6G6T2zH+9JNs5/yLg4Q X-Received: from pggj20.prod.google.com ([2002:a63:cf14:0:b0:b13:7874:b1b3]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3514:b0:203:bb3b:5f1d with SMTP id adf61e73a8af0-20cde46dc23mr968514637.6.1746139670023; Thu, 01 May 2025 15:47:50 -0700 (PDT) Date: Thu, 01 May 2025 22:47:43 +0000 In-Reply-To: <20250501-debugfs-rust-v3-0-850869fab672@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250501-debugfs-rust-v3-0-850869fab672@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746139663; l=6626; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=HbnP5HAr/7GrvIZ/usUBfS0jEKFbS6C4taoCnpgU62M=; b=u9TN62e2FvLUjy7e71xyNxi3VK9E1i80eBawwFQs2oOwRS4zHRI8D+/UJYvZJSkeoHVuQ6tj7 U0bDOHcgntcBwgDsIU3Ak1/hrqkMsT3alE+uXQ0neeT67GincPxwKkP X-Mailer: b4 0.14.2 Message-ID: <20250501-debugfs-rust-v3-3-850869fab672@google.com> Subject: [PATCH v3 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 | 118 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index ef69ae8f550a9fe6b0afc1c51bebaad2fc087811..9689e21cb24903e9f5a21fb93e6= 652adc5cafbdc 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::mem::ManuallyDrop; use core::ops::Deref; @@ -133,6 +134,48 @@ fn as_ptr(&self) -> *mut bindings::dentry { /// // "my_debugfs_dir/foo" now contains the number 200. /// ``` pub fn display_file(&self, name: &CStr, data: &'st= atic T) -> File { + // 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) -> fmt::Result>( + &self, + name: &CStr, + data: &'static T, + f: &'static F, + ) -> File { + // 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(&self, name: &CStr, dat= a: *const T) -> File { // 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. @@ -161,6 +204,32 @@ pub fn display_file(&self, name: &= CStr, data: &'static T) -> }; File(ManuallyDrop::new(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) -> fmt::R= esult>( + &self, + name: &CStr, + data: &T, + f: &'static F, + ) -> File { + #[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 Drop for Dir { @@ -228,7 +297,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; =20 /// Implements `open` for `file_operations` via `single_open` to fill = out a `seq_file`. /// @@ -283,6 +354,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 Sun Feb 8 04:11:15 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 77826267738 for ; Thu, 1 May 2025 22:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139674; cv=none; b=AsbmXyvP1noJOyC+ubNS4yCDaEGSmKi7fP1KMuldQy1xw/u5NQfWCK3S2Q/q5T1aQhSXUpCJXn6NJMqlr6E2CXH9aa123WhaSHwP2F4QA0IvC58BityDQb76Iqtvr2POzsOkjwkphxgXUiacs4BZfosiDFXF5EUuLrtVnE2L68c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746139674; c=relaxed/simple; bh=Kux678it5JwswESl98vCNed292y211bNittJAL641JM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b2mHL1yb60A+bEjOP9JkyLdmvESF8hVni3Dk81twPBFmsnpz14s9f+dBz69SN6/WKU+FLPxTcTLsLXZ3wFNjrK+jlAT61x7AaXQJRkv/zSmE9oLTV2ACmyQxb2s1Kzoo+JgGj/oZBZPvQajqzLLnw4ZtmqzFJv1Uu4JhTi++rOs= 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=TODPacFG; arc=none smtp.client-ip=209.85.214.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="TODPacFG" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-224364f2492so13125315ad.3 for ; Thu, 01 May 2025 15:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746139672; x=1746744472; 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=wmtC1RujjIPLNELPkpAAOYKIfd0scSadrJC8Lm1a8Ww=; b=TODPacFG0JQuEGODYTt4K2Al76KvyoT7DVRvW5BlzX1DiKM5Wqlnt/19khdet1Lt+J UaCiUTw9wmY5OzYo8ic4X1/RrZuYIbDmHsB1qW+qPGz4lfsqHJr1mcmvTcJJ9KlLo1WO Ay9aDiMx82ArVltfK0uP7bSb07+LVYRjNxclw1JDZesCfpUBlHsJZNeDoa1DLpQzBjvc bGuvh9Uw8zIZsXnzUwk2G2PsX3O2g+uD5uMKLsskg6MYPS9IoBmYzXvzDmIU8ck/QjMx oxTjoPkXjIA8k6WoY4FZACbH9NPq7y5Jxfb2ZAwmLTscuQ6mQ4g/oqBTNcvKnFjWMoPL +EFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746139672; x=1746744472; 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=wmtC1RujjIPLNELPkpAAOYKIfd0scSadrJC8Lm1a8Ww=; b=te16P73hOUcuGyrb4eoKZEcJR6tp5+rIMGEdk5z+1Fp6AoNOLNd4KDwPtakif3tYGe CFiord0GYu/4hYkZ1+5BwZCK4xqruTze0DijFJgfI7oXmv52vczpjxXkf1Di1lQDEqi+ w9HWre+ncTzHV6p4LJ+yv/9H85IbSIeuUzZIzX0ACnp6W/zXjSyMOgc/wyaHMObPYzvv Td83xuH00D0X1kaEFFjAQZNlx7U1osSC93OG8C3pXU/wzVeExFDvdSzywtCx6TauJycS nDcc/k70fSSS43x7/JcB8rfvInkKXcOpEgzmWurvpWWRq5WYa1LnMwz7I3ew2Dati4Rw jSSA== X-Gm-Message-State: AOJu0Ywnf7sFCwjMpq690lJ93VTdknKIuJHCF2kUN0Myh0MYBC3w/w33 8RIor0zexSmC5mLumQFbQunVpaIKutj8ELOsJp4gsOoBCX1v2YNEBocjZlapJta7AlPFwHu1YVK xIiUSsg== X-Google-Smtp-Source: AGHT+IGRSoPIUWL5vu0fwQaTzDohYuzNK1EdjKDNo2MIULwo4EUZv4IW6G+QuI2GRpfgFpdbKPcqjGSXRgo/ X-Received: from plbkt6.prod.google.com ([2002:a17:903:886:b0:223:4fa3:bcbd]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:198c:b0:224:2175:b0cd with SMTP id d9443c01a7336-22e1032d8e5mr9648405ad.26.1746139671837; Thu, 01 May 2025 15:47:51 -0700 (PDT) Date: Thu, 01 May 2025 22:47:44 +0000 In-Reply-To: <20250501-debugfs-rust-v3-0-850869fab672@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250501-debugfs-rust-v3-0-850869fab672@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746139663; l=4627; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=Kux678it5JwswESl98vCNed292y211bNittJAL641JM=; b=2KCYhVvZke5bDHRUAfSbO1GTxTBP/DKHmRyy2lmaF0oiqMwDU7IbaIY4t1EP3O99TO+EcbgKT GOG7N053QKyAhjUQmQh57lN+t8cUOqjSmYTLdbqx8gKAj5zlU4Ns9lz X-Mailer: b4 0.14.2 Message-ID: <20250501-debugfs-rust-v3-4-850869fab672@google.com> Subject: [PATCH v3 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 | 54 ++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 67 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..b22255667f3bb91b10555ac8b91= be850cd5f172e --- /dev/null +++ b/samples/rust/rust_debugfs.rs @@ -0,0 +1,54 @@ +// 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 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 exists. + let sub =3D debugfs.subdir(c_str!("subdir")); + + // Create a single file in the subdirectory called "example" that = will read from the + // `EXAMPLE` atomic variable. + sub.fmt_file(c_str!("example"), &EXAMPLE, &|example, f| { + writeln!(f, "Reading atomic: {}", example.load(Ordering::Relax= ed)) + }); + // Now, "sample_debugfs/subdir/example" will print "Reading atomic= : 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