From nobody Sat Feb 7 22:01:45 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 3815A229B29 for ; Wed, 30 Apr 2025 23:32:07 +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=1746055929; cv=none; b=MZyDlvtFITe6IYJiqtSVyWItRndu10iz5CZg8HcqcTKJET6fiDw1Qin+xUwHp2luz/QJ38ay7vM9BcdrZekrIv6mHdFzhQUDoyVkoJqfcviOFQDhov9URaYYlSwbI8DjCBn/IQ2VmutkIyNaAVH7exOJP7SboO3xq63R1iYfzAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746055929; c=relaxed/simple; bh=yPlSgrrz4IXdvGcnIh4cW6PR57JStxkzPpJQO5+RMzU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d16GWn9W7BqSTHVO/nIUoTevQ275gYidN7WVedoDucIryJl1GZixRlDHIoZjsdVrB89TTZheL/ps5Eqi52ODLylmZngp/irXKrXBsKvykxQxi8/9bGZzAuqxdOXns8xwpIN5k3k2gZRcK5J+DAjTCA6ZL6vZSDypjggqG8WVO/M= 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=eYBLdS+M; 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="eYBLdS+M" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30a2a2c99f6so594820a91.2 for ; Wed, 30 Apr 2025 16:32:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746055927; x=1746660727; 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=Dhx6v/+VUn2kb10b8iM44dLEQVytCie/Z3XMWWbZPuY=; b=eYBLdS+MEEQcRMi4ooNQwyFkpJVsq37ex/gJjcvYheA1NcpfB+qHbVzmY6X43fDqby iZUNvEsCJgkffnqx5t4XGMbgPQ1w6ka/GlzU3szMkDCgqj174yW7skUUnGrvICiBDhuT hzbceE8Fez1aiF1MCvMjgPMmg2/xXSXt+zfM0d9SyKDKO08iSBXKZoJKMAvcc0wKlIDU fWh+o9h8L+yzFo4r9xdJugXZtW3Y9VSCM2l1C6x2TIwbkc55Sg3SyAY4HXZK0MXD4qPe w2/F3cO+jnAX9nEl3edvRk/gq1zN55DO/yGCJ79GynWeQsh7xhG6UhvVfIcnP9z17jCv sWzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746055927; x=1746660727; 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=Dhx6v/+VUn2kb10b8iM44dLEQVytCie/Z3XMWWbZPuY=; b=hf/Rf2RBGJLkyTx90NPE6xFwGZMrOfuupcJh816vN7YCJ7aMDznfX6LSX7lMWyit05 3iG/0B/8p15HTSIUCBobuLRupsHCGEWkCItg35mI/YB9O/GCIMlSW12ucNkF01C6DtCN sCmam3w+KXgjvMdSt3qAKIsxiADT+Q8OuCHbVM4aF4hc9RthXWBWfW3/3cMjJqwUkTnJ bXhjX2nyqaz72aWZmsc3I9H9sB+4fmlWtHh2bUsl88fHWoQ9Y2Vsdc9oaIvMhfCu8dx2 E/AStS+zVNvlNJwCAwbJqTEmjYm7W09AFDVMFf7kKlvw1ycCf571tkOQxHbYTR1FVPs+ MWog== X-Gm-Message-State: AOJu0Yz514iZLUaSGJ3LtD//ilyIpB2K0PnAev3lD19ut9p4FUNx2cX+ dQZn407ZKpCMAARYV4Cl0NTwxH1m/edRnT5aXCaZx9nxs7K0hlv2KEv63k7dfWaSUyFMkKrXwA7 NWbiDkw== X-Google-Smtp-Source: AGHT+IGmun35lYu+juO8WNDrBAweocBeo9jksRENwv/DORWF0CR/Z0hH+/xYXvi/6POmN51ZaEpqgsNhnM3x X-Received: from pjbeu4.prod.google.com ([2002:a17:90a:f944:b0:301:1bf5:2f07]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:56cc:b0:30a:20d6:ed35 with SMTP id 98e67ed59e1d1-30a41ec6b80mr811328a91.23.1746055927519; Wed, 30 Apr 2025 16:32:07 -0700 (PDT) Date: Wed, 30 Apr 2025 23:31:56 +0000 In-Reply-To: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746055923; l=6660; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=yPlSgrrz4IXdvGcnIh4cW6PR57JStxkzPpJQO5+RMzU=; b=0PGZHBdNMf+sWawAXN8TN9TepRTOl4EO1hWo4wLpcAdshElgeyYwhinazyZAq3sbDqbGngqiP OYsZXtupkK0AQzs+aj+UplNkqgdBH+siGWkzSR1yVIPprSL17cOm2s8 X-Mailer: b4 0.14.2 Message-ID: <20250430-debugfs-rust-v2-1-2e8d3985812b@google.com> Subject: [PATCH v2 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 directory handle leaves scope, it will be cleaned up. This can be suppressed by calling `.keep()` on it. Signed-off-by: Matthew Maurer --- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/debugfs.rs | 135 ++++++++++++++++++++++++++++++++++++= ++++ rust/kernel/lib.rs | 1 + 4 files changed, 138 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..b533ab21aaa775d4e3f33caf89e= 2d67ef85592f8 --- /dev/null +++ b/rust/kernel/debugfs.rs @@ -0,0 +1,135 @@ +// 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; + +/// Handle to a DebugFS directory. +// INVARIANT: The wrapped pointer will always be NULL, an error, or an own= ed DebugFS `dentry` +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")); + /// // parent exists in DebugFS here. + /// } + /// // It does not exist here. + /// ``` + pub fn new(name: &CStr) -> Self { + Self::create(name, None) + } + + /// Create a DebugFS subdirectory. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// { + /// let parent =3D Dir::new(c_str!("parent")); + /// // parent exists in DebugFS here. + /// let child =3D parent.subdir(c_str!("child")); + /// // parent/child exists in DebugFS here. + /// } + /// // Neither exist here. + /// ``` + pub fn subdir(&self, name: &CStr) -> Self { + 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 null terminated string that lives a= cross the call, by + // invariants of `&CStr` + // * If parent is None, parent accepts null pointers to mean creat= e at root + // * If parent is Some, parent accepts live dentry debugfs pointers + // * `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 tra= nsfer 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. + /// + /// # Invariant + /// + /// The value returned from this function will 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 + } + + /// Allow the handle to go out of scope without removing the directory. + /// + /// Equivalent to `core::mem::forget`, but with a more semantically me= aningful name. + /// + /// # Examples + /// + /// ``` + /// # use kernel::c_str; + /// # use kernel::debugfs::Dir; + /// { + /// let parent =3D Dir::new(c_str!("parent")); + /// parent.subdir(c_str!("child")).keep(); + /// // We have no handle to the child, but it will not be deleted d= irectly. + /// } + /// // When parent is deleted, it will still be deleted. + /// ``` + pub fn keep(self) { + core::mem::forget(self) + } +} + +impl Drop for Dir { + fn drop(&mut self) { + // SAFETY: `debugfs_remove` can take NULL, error values, and legal= DebugFS dentries. + // `as_ptr` guarantees that the pointer is of this form. + #[cfg(CONFIG_DEBUG_FS)] + 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 22:01:45 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 42B17214232 for ; Wed, 30 Apr 2025 23:32:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746055932; cv=none; b=FeDxblBQXL3Vga/kH+JkZHFM0UER/69uIGYIDcDmlo4zPvhrWdabJ54Kg03SX5BIA4OKpkuaPQK7+N25fOqIWp66OlA8UfdDUb2i2ykeJwq80i/KEE76kIDxC+mUPJPOALp0cHKsFwuAihe9uT5oqFggVO+iDSCcJYk289G1Rtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746055932; c=relaxed/simple; bh=K3ANi7xRlSj5yHHCVsAYvdNWi4HCri0Umkny65ip/+s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FkXn5PcfxCaohWrUHFRG19oPhGE720s5J3dkeMM/rxUSPEeKj/t08CJFm2z3H34TDkhni9ipbKaddaOR9xw27VUr+C/2zC2zf62JFwHI+FO9C72y+eH2dvVhF1jU2RL+ZNeJrEJjuoEz5CSqOLp0sSjhuFAnODESW/j/9nsdzm0= 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=jBi07hiL; arc=none smtp.client-ip=209.85.215.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="jBi07hiL" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b115383fcecso225129a12.1 for ; Wed, 30 Apr 2025 16:32:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746055929; x=1746660729; 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=W2PaGNarEJSxlEaGNeHssoqREdq53cRaBB8TVSLbGvg=; b=jBi07hiLqmy+RnSw92lvBxVwcH1L3NX3pgU1yH+oey9CY0B/VNNHCCfbSHg3l3biQP t3UojTSSSLF79sGmdpqgXzpN0MueqfWVkjFga5ohrqOj9PaSzs0D+CIQkT0wF1v6l2Nz P6sD+FAHdZEU7EWzNU5SPtkPZWrHTMqmCND4h1j8KkjyG/rQA+B9ono9ktB0XFhJW99a kyVczyCHWW6xsTYutFJe2l/TlGeQhf8vcA8mVx+Xcn0T04ohb+1oQjcxh1QUTOSRvGA9 T2DMH6qLy5qOVzypOqzJ7Ppbf5R1l0xQPaGgORWBotbnL/9VOklJpRa8DzNRicZz6er5 X2yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746055929; x=1746660729; 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=W2PaGNarEJSxlEaGNeHssoqREdq53cRaBB8TVSLbGvg=; b=pkB617bRt18xmKee16nGN+jXzK9xKT6736wqcx71EGaOBs5RdSx/k5h70btgR/Az3C YVcKSZeo4nPw3+uSXQ1mVzkTapmFLaBG9uMwkhjf2hrq5ijf+i82qiUVfxOsiFbYn4XT eoLOn6JIhEAn9xJHMS2nTyqzHsNgwFLBwvYzPHz3yXI7qPgSUyeecYN9TmC3nZmLJELr FrcpT8qEaZv3cDDNir8sY5xy09BbFCr0q9staVrzIqlTHPW2E0FXL2pRLhcJhcGJfjv+ fEPDXfXZ/XuF7QInS032d/TmnuPZl+PQIYoU0Gysp5eQc3ss2vnmqfKCilkej6frpnc0 c0gA== X-Gm-Message-State: AOJu0YwtOyhamK0037flb11/4tS+0MGWx60JAUqy6CZqV7bKBtObSG1u rDCkxPoZYT6EEiYWKwOVuPz309TrXGIpEEsusBw8DWBimoxxrwA68HETMbIxRw2zIgvcrPeawjg 6Mz2c3g== X-Google-Smtp-Source: AGHT+IFXnTJSYIgkAmYiFhWawzHDclbmYW4vocFGHfbUMvt00HY5mWdcj77NAjHsOUZw3S7ps85/EhVjj2ES X-Received: from pjbsw7.prod.google.com ([2002:a17:90b:2c87:b0:309:da3b:15d1]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3a48:b0:2ff:6788:cc67 with SMTP id 98e67ed59e1d1-30a4337e5d4mr362658a91.34.1746055929125; Wed, 30 Apr 2025 16:32:09 -0700 (PDT) Date: Wed, 30 Apr 2025 23:31:57 +0000 In-Reply-To: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746055923; l=5710; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=K3ANi7xRlSj5yHHCVsAYvdNWi4HCri0Umkny65ip/+s=; b=H932J1Iteny0GaOUFT89EwS069TMIC7KPthw1db2MoFRYGx7QsQ+M/c7GEfTIPyLnjbLtTP1b /BLqVsNh+F3AVJfYfntAOqLPjjENnpUYE/NAfJKI4lz1H1rMDoy4yN/ X-Mailer: b4 0.14.2 Message-ID: <20250430-debugfs-rust-v2-2-2e8d3985812b@google.com> Subject: [PATCH v2 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 | 102 +++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 102 insertions(+) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index b533ab21aaa775d4e3f33caf89e2d67ef85592f8..87de94da3b27c2a399bb377afd4= 7280f65208d41 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; =20 /// Handle to a DebugFS directory. // INVARIANT: The wrapped pointer will always be NULL, an error, or an own= ed DebugFS `dentry` @@ -121,6 +122,47 @@ fn as_ptr(&self) -> *mut bindings::dentry { pub fn keep(self) { core::mem::forget(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).keep(); + /// // "my_debugfs_dir/foo" now contains the number 200. + /// ``` + pub fn display_file(&self, name: &CStr, data: &'st= atic T) -> Self { + // 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)] + 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))] + { + // Mark parameters used + let (_, _) =3D (name, data); + Self() + } + } } =20 impl Drop for Dir { @@ -133,3 +175,63 @@ fn drop(&mut self) { } } } + +#[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 which wi= ll 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 po= inter 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 seq_f= ile, 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 22:01:45 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 38DF722A4EF for ; Wed, 30 Apr 2025 23:32:11 +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=1746055933; cv=none; b=I38VE+Ge4YQgtzPo0zM9AygCBOoLrv/+cXoLVzRxewM+5LLimbiW/7Yg3dqwl+kq/RRSoeW7i3TglNli49jKja+Z+v+FkSCxVgB6iGEz65jw1EUZVnZifquMr8egR0Ul2mtFQr0yE/qYmkct+Aw8CqZtCx1/WF7wYMfOV8p3oqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746055933; c=relaxed/simple; bh=qzcAKC5z+jfCNwA4KzH4cSbQhEndXssauIghnf8+6Iw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s3hfONMXmmaDydeATPd4sRQe7qvPX82WUTnUZozD7ql/dOAIEEZlmVAJ1Gd7bIq9LVtS9DMQ0fxxjCuI8Hh6Ur9jWKsZqqU9AtPm/gZYQC9BfDzOyFS6hq+yi8knyif/2ZPs0A0Iapslj+DXQQMs490gEItrTNbp4cLYj88hdqM= 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=Gp+3aqkC; 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="Gp+3aqkC" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af8f28f85a7so215613a12.2 for ; Wed, 30 Apr 2025 16:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746055930; x=1746660730; 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=5080AaWuHxY/+zOzJ0GujJhdmP9WodPLpLxIiRjde1k=; b=Gp+3aqkC9JSfAvNTZcipa07IRiR2v953NNvuVeX3z0LKEfMgyTz241a7FxsOcpJVuN w5IG2LtzDzC6bvOiHS/P3waWhJbVUXkGfwE2BNMr4+IeWM3zk6RQuIJyNpSdMsnLN+t0 y+OvTWAPKuknNLyWs47FDdWoAXL3wRIvHNzMzaDzp3Fl3DoOPBuWmGb3nH5mLAhiHczf QCFxswjcf/qAU371+PIVy8UY2OKtaVpl0OK7IoxE+36xX++w+0mgh/WVcWc5WgvimCdE K0TomsNPj54tTR9MW1tvEnPz1tzGfSrkJAbs7rgQruz7r10UXlNcSHaFiaGvTIqWke3o r9IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746055930; x=1746660730; 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=5080AaWuHxY/+zOzJ0GujJhdmP9WodPLpLxIiRjde1k=; b=vb8PnL8zlFZVL1fTTo3kPfBYzhJMgJ48flgOYptM0VtXnjlxBzt8Opn/O6V2FtkVVs I5J0HFNSNvO4Cey0zDwY7bC5zURUd3i966CTPZ0ZGOY9pn7wsKn0hDaYYDoaTKa2Lq3m Kzo9cUqbUO09VnHBjD210f8zKRbScmttEUDA9g7u23nS6zzR+pxixHLO5snklg6OJ7ar mkcaTkKexAQyVCONAjoI6vz8yTKKXDjEexvXbTlG5Z+cz+i09qtjU2C4sZDB0UTwo6zg bRTvWzDx0QjqHEWRy7Hsp0SXQJjBJGS/SMZ5uC4QcS7OBuKqR8A1sEGvwOiXYp7JKb9T wEYg== X-Gm-Message-State: AOJu0Yy0L6Z1Mzw4d+8EVe8VcWIfeM4GB1JXefJA2TwlMimVacZE6aof +U4Q0k5zcK9UcVig6IKaHoUy9VY4MHC9QphC/nGrj+kYnKgDvnwQoQh1t9l9MSutZoGLUEr0Hco yNlIi9w== X-Google-Smtp-Source: AGHT+IElgHFwEamhzSuEWWDqRNvDc8mU6IwdF7N8R9tOi4vLjCZKi6WUSh+RMkJJbUhBrb1Jw3Yo32RnfcMo X-Received: from pfoc24.prod.google.com ([2002:aa7:8818:0:b0:740:3d9e:4ca4]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9992:b0:203:bb3b:5f1e with SMTP id adf61e73a8af0-20ba6a20256mr1575665637.1.1746055930537; Wed, 30 Apr 2025 16:32:10 -0700 (PDT) Date: Wed, 30 Apr 2025 23:31:58 +0000 In-Reply-To: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746055923; l=6267; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=qzcAKC5z+jfCNwA4KzH4cSbQhEndXssauIghnf8+6Iw=; b=BLUTi5wgUiCtHSwywOnXg7KwtoWkKYwPHNnn1kBJkmDnSNGjVUhs/H0jmIrBAmfcN8j69iqTe 6tMnI1IVqOaA3z54jzttXFC6Wep2R8JNFk02WWa8jQNYibUMq2L4bMl X-Mailer: b4 0.14.2 Message-ID: <20250430-debugfs-rust-v2-3-2e8d3985812b@google.com> Subject: [PATCH v2 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 | 110 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs index 87de94da3b27c2a399bb377afd47280f65208d41..2935c7ffbfaf460fff5b5f1ffc7= 68f803c2da345 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; =20 /// Handle to a DebugFS directory. @@ -136,6 +137,47 @@ pub fn keep(self) { /// // "my_debugfs_dir/foo" now contains the number 200. /// ``` pub fn display_file(&self, name: &CStr, data: &'st= atic T) -> Self { + // 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, + ) -> Self { + // 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) -> Self { // 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 @@ -163,6 +205,32 @@ pub fn display_file(&self, name: &= CStr, data: &'static T) -> Self() } } + + /// 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, + ) -> Self { + #[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 { @@ -180,7 +248,9 @@ fn drop(&mut 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` /// @@ -231,6 +301,44 @@ pub(crate) trait DisplayFile: Display + Sized { } =20 impl DisplayFile for T {} + + // INVARIANT: F is inhabited + #[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) + } + } + + /// # 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 22:01:45 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 BFA5B22B8A2 for ; Wed, 30 Apr 2025 23:32:12 +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=1746055934; cv=none; b=ZIp4Sln0uFTjgKxPnpWjlNbY+psC3HLKJQUZs4wKKr1wof705Yl2gaK8tUItx9Jb0tSR20g0kTTeoE5VtMzxeeO8/5YW6kBOYQEv4tIL/JXSNCtOYsh58rJBP6/hh4EviGG52JlejRV4/RDgd7A3F8JEHV6Ew8EXK5COMYQjSdk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746055934; c=relaxed/simple; bh=0r3nnpv1r4BIBg/qPhAuVRymuwLZn71zdPHeXEpfV10=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JQ3Q3QmmHuPpBnFW2IvYTidokGgGQ2M1yCK+M5L278cftJJsYlNsz2mR8Meu+w0b0XmOKEeeSkY0zM+2GLNKPUZxcbuBEHIF2vbSsc4uhinm7d5YofBYGrZSbmnvbLNNYeJHOCjMSEpiSelnQDdNQO85tpq3kyT+MKYehMFRNjg= 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=jiFmfyKf; 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="jiFmfyKf" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-224191d9228so4626025ad.3 for ; Wed, 30 Apr 2025 16:32:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746055932; x=1746660732; 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=fjhzEGURkcn9UbWg5KxOT29i9wmd1rs0Wo265/N8Qzc=; b=jiFmfyKfOdFHFo3rl40BAW92XUmsbZMS1xu1dYmfGDkrhF0ofspycnsXtvbPylxf4R /6fhZLdeai1VOD33BrzYWQoWaFPgTHtZSnpv9HrTjVJZhulHvYRIuZAnnCPRV3WlIZe2 Zwa2vuZbcMyZieq/9tTTvVCSkiFIryLESi25LduPLw6QDPIvVCnBlFx8d+d9UCm/o53Y mV6L2Vv3/NaEuY0ac43IDZ2QOyqKFo6gdkuZJSu0IrUi1Wt6yE+3IRwNOdRZTceAao5f 9mKZMDun5Pv7TSlLgNpr0khJFXfT2v5zE0ypla9P5IzxrMpBTNpfRTs9AhPRpUx1Jso9 43wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746055932; x=1746660732; 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=fjhzEGURkcn9UbWg5KxOT29i9wmd1rs0Wo265/N8Qzc=; b=rH14erVmsZEvSdlha/vsIxnsgg032cjWMIZr8f9XJp0JNVng5ZrvgkVFAzSsNH9/l6 gLyDKhKbbpDZ1acPTH2UDoM07HsxIJxBk3himhYb3dwEWKd3oUxPar/7K3j1j5Oy/uda ylgVMwR9+G0+jdHSOBH+j98aYZm8jpaEruTW/tup6Fpo6grFVGmSamhoSGwt6wqzYPqZ 0TmtDJzD18dddMVl1IHI4IoSGXYEgM4LjWi+wV+gJkDSqVrqjuYoprRNZVlFzJ9MihwE BcgpI+bISjZMe6Q+e3gbZRlJTXqb8wI3QTwMlyIkq/piU/PK8ofGcySSOI0qWIBS1Yx5 r2aw== X-Gm-Message-State: AOJu0Yx4qgDx8Qcz7H155DS1mE5xujpV7D4Beg+WG/PbBEhl4cvFOMaf dc3+fjyBLLJreQaIi+N4Q9kjBkyiN8FXxJTKkNtIMFUEo/n7Y1yZiC25c1gb3uaWak7LKYwFOu0 rd14zCw== X-Google-Smtp-Source: AGHT+IE8feJ0hYlz4K66uJTpWFrcCx5lvXCT8nMGqCSSGEuNtR5RCbSNQcRmo9E607OGsVe8FQHuOlLyawFU X-Received: from plps2.prod.google.com ([2002:a17:902:9882:b0:229:2f8a:d4ba]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c94b:b0:224:f12:3734 with SMTP id d9443c01a7336-22e0843cc0emr5975785ad.30.1746055931999; Wed, 30 Apr 2025 16:32:11 -0700 (PDT) Date: Wed, 30 Apr 2025 23:31:59 +0000 In-Reply-To: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250430-debugfs-rust-v2-0-2e8d3985812b@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1746055923; l=3391; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=0r3nnpv1r4BIBg/qPhAuVRymuwLZn71zdPHeXEpfV10=; b=I6kD3xrVQb/NIjm0rtd0YHTEZJB+Y6zLAf79Jd53bVqEhNtlEq1IOqQJoGfMacyXm98jjfZQP 4rGLnLbqW5MBrzivSatV3K8WWOxEH0V7HrzQuArPHxphFGAkX/CfSDV X-Mailer: b4 0.14.2 Message-ID: <20250430-debugfs-rust-v2-4-2e8d3985812b@google.com> Subject: [PATCH v2 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 | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 50 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..ce3e1a997bf314f63026446f5a6= d2a00579c602a --- /dev/null +++ b/samples/rust/rust_debugfs.rs @@ -0,0 +1,37 @@ +// 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 { + _debugfs: Dir, +} + +static EXAMPLE: AtomicU32 =3D AtomicU32::new(8); + +impl kernel::Module for RustDebugFs { + fn init(_this: &'static ThisModule) -> Result { + let debugfs =3D Dir::new(c_str!("sample_debugfs")); + debugfs + .fmt_file(c_str!("example"), &EXAMPLE, &|example, f| { + writeln!(f, "Reading atomic: {}", example.load(Ordering::R= elaxed)) + }) + .keep(); + EXAMPLE.store(10, Ordering::Relaxed); + Ok(Self { _debugfs: debugfs }) + } +} --=20 2.49.0.906.g1f30a19c02-goog