[PATCH v2 8/8] samples: rust: debugfs_scoped: add example for blobs

Danilo Krummrich
2025-10-20T22:28:13Z

Extend the rust_debugfs_scoped sample to demonstrate how to export a
large binary object through a ScopedDir.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
samples/rust/rust_debugfs_scoped.rs | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/samples/rust/rust_debugfs_scoped.rs b/samples/rust/rust_debugfs_scoped.rs
index b0c4e76b123e..c80312cf168d 100644
--- a/samples/rust/rust_debugfs_scoped.rs
+++ b/samples/rust/rust_debugfs_scoped.rs
@@ -9,6 +9,7 @@
use core::sync::atomic::AtomicUsize;
use kernel::debugfs::{Dir, Scope};
use kernel::prelude::*;
+use kernel::sizes::*;
use kernel::sync::Mutex;
use kernel::{c_str, new_mutex, str::CString};

@@ -66,18 +67,22 @@ fn create_file_write(
GFP_KERNEL,
)?;
}
+ let blob = KBox::pin_init(new_mutex!([0x42; SZ_4K]), GFP_KERNEL)?;

let scope = KBox::pin_init(
- mod_data
- .device_dir
- .scope(DeviceData { name, nums }, &file_name, |dev_data, dir| {
+ mod_data.device_dir.scope(
+ DeviceData { name, nums, blob },
+ &file_name,
+ |dev_data, dir| {
for (idx, val) in dev_data.nums.iter().enumerate() {
let Ok(name) = CString::try_from_fmt(fmt!("{idx}")) else {
return;
};
dir.read_write_file(&name, val);
}
- }),
+ dir.read_write_binary_file(c_str!("blob"), &dev_data.blob);
+ },
+ ),
GFP_KERNEL,
)?;
(*mod_data.devices.lock()).push(scope, GFP_KERNEL)?;
@@ -110,6 +115,7 @@ fn init(device_dir: Dir) -> impl PinInit<Self> {
struct DeviceData {
name: CString,
nums: KVec<AtomicUsize>,
+ blob: Pin<KBox<Mutex<[u8; SZ_4K]>>>,
}

fn init_control(base_dir: &Dir, dyn_dirs: Dir) -> impl PinInit<Scope<ModuleData>> + '_ {
--
2.51.0