[PATCH] samples: rust: debugfs: fix excessive stack use

Gary Guo posted 1 patch 1 week, 2 days ago
samples/rust/rust_debugfs_scoped.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] samples: rust: debugfs: fix excessive stack use
Posted by Gary Guo 1 week, 2 days ago
From: Gary Guo <gary@garyguo.net>

The current implementation creates a 4K array and move it into the box.
Klint reports that this causes excesssive stack usage:

warning: stack size of `create_file_write` is 4472 bytes, exceeds the 2048-byte limit
  --> samples/rust/rust_debugfs_scoped.rs:54:1
   |
54 | / fn create_file_write(
55 | |     mod_data: &ModuleData,
56 | |     reader: &mut kernel::uaccess::UserSliceReader,
57 | | ) -> Result {
   | |___________^
   |
   = note: the stack size is inferred from instruction `sub $0x1178,%rsp` at .text+2205

Use pin-init to create the array in-place instead.

Fixes: f656279afde1 ("samples: rust: debugfs_scoped: add example for blobs")
Signed-off-by: Gary Guo <gary@garyguo.net>
---
 samples/rust/rust_debugfs_scoped.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/samples/rust/rust_debugfs_scoped.rs b/samples/rust/rust_debugfs_scoped.rs
index 6a575a15a2c2..33a4a0865f5b 100644
--- a/samples/rust/rust_debugfs_scoped.rs
+++ b/samples/rust/rust_debugfs_scoped.rs
@@ -75,7 +75,7 @@ fn create_file_write(
             GFP_KERNEL,
         )?;
     }
-    let blob = KBox::pin_init(new_mutex!([0x42; SZ_4K]), GFP_KERNEL)?;
+    let blob = KBox::pin_init(new_mutex!(pin_init::init_array_from_fn(|_| 0x42)), GFP_KERNEL)?;
 
     let scope = KBox::pin_init(
         mod_data.device_dir.scope(

base-commit: b8809969e1d7a591e0f49dd464a5d04b3cf02ab1
-- 
2.54.0
Re: [PATCH] samples: rust: debugfs: fix excessive stack use
Posted by Danilo Krummrich 4 days, 15 hours ago
On Thu, 16 Jul 2026 15:41:43 +0100, Gary Guo wrote:
> [PATCH] samples: rust: debugfs: fix excessive stack use

Applied, thanks!

  Branch: driver-core-testing
  Tree:   git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] samples: rust: debugfs: fix excessive stack use
      commit: f5256ad60651

      [ Make the patch rustfmtcheck complient. - Danilo ]

The patch will appear in the next linux-next integration (typically within 24
hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to
driver-core-next after validation.
Re: [PATCH] samples: rust: debugfs: fix excessive stack use
Posted by kernel test robot 1 week, 1 day ago
Hi Gary,

kernel test robot noticed the following build errors:

[auto build test ERROR on b8809969e1d7a591e0f49dd464a5d04b3cf02ab1]

url:    https://github.com/intel-lab-lkp/linux/commits/Gary-Guo/samples-rust-debugfs-fix-excessive-stack-use/20260717-053803
base:   b8809969e1d7a591e0f49dd464a5d04b3cf02ab1
patch link:    https://lore.kernel.org/r/20260716144144.3665719-1-gary%40kernel.org
patch subject: [PATCH] samples: rust: debugfs: fix excessive stack use
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260717/202607170734.2N6biRuj-lkp@intel.com/config)
compiler: clang version 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1)
rustc: rustc 1.96.0 (ac68faa20 2026-05-25)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260717/202607170734.2N6biRuj-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202607170734.2N6biRuj-lkp@intel.com/

All errors (new ones prefixed by >>):

   PATH=/opt/cross/clang-22/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
   INFO PATH=/opt/cross/rustc-1.96.0-bindgen-0.72.1/cargo/bin:/opt/cross/clang-22/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
   /usr/bin/timeout -k 100 12h /usr/bin/make KCFLAGS=\ -fno-crash-diagnostics\ -Wno-error=return-type\ -Wreturn-type\ -funsigned-char\ -Wundef\ -falign-functions=64 W=1 --keep-going LLVM=1 -j384 -C source O=/kbuild/obj/consumer/x86_64-rhel-9.4-rust ARCH=x86_64 SHELL=/bin/bash rustfmtcheck 
   make: Entering directory '/kbuild/src'
   make[1]: Entering directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust'
   Diff in rust/kernel/cpufreq.rs:792:
        }
    
        /// Driver's `adjust_perf` callback.
   -    fn adjust_perf(_policy: &mut Policy, _min_perf: usize, _target_perf: usize,
   -                   _max_perf: usize, _capacity: usize) {
   +    fn adjust_perf(
   +        _policy: &mut Policy,
   +        _min_perf: usize,
   +        _target_perf: usize,
   +        _max_perf: usize,
   +        _capacity: usize,
   +    ) {
            build_error!(VTABLE_DEFAULT_ERROR)
        }
    
   Diff in rust/kernel/cpufreq.rs:792:
        }
    
        /// Driver's `adjust_perf` callback.
   -    fn adjust_perf(_policy: &mut Policy, _min_perf: usize, _target_perf: usize,
   -                   _max_perf: usize, _capacity: usize) {
   +    fn adjust_perf(
   +        _policy: &mut Policy,
   +        _min_perf: usize,
   +        _target_perf: usize,
   +        _max_perf: usize,
   +        _capacity: usize,
   +    ) {
            build_error!(VTABLE_DEFAULT_ERROR)
        }
    
>> Diff in samples/rust/rust_debugfs_scoped.rs:75:
                GFP_KERNEL,
            )?;
        }
   -    let blob = KBox::pin_init(new_mutex!(pin_init::init_array_from_fn(|_| 0x42)), GFP_KERNEL)?;
   +    let blob = KBox::pin_init(
   +        new_mutex!(pin_init::init_array_from_fn(|_| 0x42)),
   +        GFP_KERNEL,
   +    )?;
    
        let scope = KBox::pin_init(
            mod_data.device_dir.scope(
   make[2]: *** [Makefile:2029: rustfmt] Error 123
   make[2]: Target 'rustfmtcheck' not remade because of errors.
   make[1]: Leaving directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust'
   make[1]: *** [Makefile:248: __sub-make] Error 2
   make[1]: Target 'rustfmtcheck' not remade because of errors.
   make: *** [Makefile:248: __sub-make] Error 2
   make: Target 'rustfmtcheck' not remade because of errors.
   make: Leaving directory '/kbuild/src'

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] samples: rust: debugfs: fix excessive stack use
Posted by Alexandre Courbot 1 week, 1 day ago
On Thu Jul 16, 2026 at 7:41 AM PDT, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> The current implementation creates a 4K array and move it into the box.
> Klint reports that this causes excesssive stack usage:
>
> warning: stack size of `create_file_write` is 4472 bytes, exceeds the 2048-byte limit
>   --> samples/rust/rust_debugfs_scoped.rs:54:1
>    |
> 54 | / fn create_file_write(
> 55 | |     mod_data: &ModuleData,
> 56 | |     reader: &mut kernel::uaccess::UserSliceReader,
> 57 | | ) -> Result {
>    | |___________^
>    |
>    = note: the stack size is inferred from instruction `sub $0x1178,%rsp` at .text+2205
>
> Use pin-init to create the array in-place instead.
>
> Fixes: f656279afde1 ("samples: rust: debugfs_scoped: add example for blobs")
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
>  samples/rust/rust_debugfs_scoped.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/samples/rust/rust_debugfs_scoped.rs b/samples/rust/rust_debugfs_scoped.rs
> index 6a575a15a2c2..33a4a0865f5b 100644
> --- a/samples/rust/rust_debugfs_scoped.rs
> +++ b/samples/rust/rust_debugfs_scoped.rs
> @@ -75,7 +75,7 @@ fn create_file_write(
>              GFP_KERNEL,
>          )?;
>      }
> -    let blob = KBox::pin_init(new_mutex!([0x42; SZ_4K]), GFP_KERNEL)?;
> +    let blob = KBox::pin_init(new_mutex!(pin_init::init_array_from_fn(|_| 0x42)), GFP_KERNEL)?;

Since this is a sample, do we want to specify the size explicitly as it
is arguably more readable in this case?

Regardless,

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH] samples: rust: debugfs: fix excessive stack use
Posted by Gary Guo 1 week, 1 day ago
On Thu Jul 16, 2026 at 9:33 PM BST, Alexandre Courbot wrote:
> On Thu Jul 16, 2026 at 7:41 AM PDT, Gary Guo wrote:
>> From: Gary Guo <gary@garyguo.net>
>>
>> The current implementation creates a 4K array and move it into the box.
>> Klint reports that this causes excesssive stack usage:
>>
>> warning: stack size of `create_file_write` is 4472 bytes, exceeds the 2048-byte limit
>>   --> samples/rust/rust_debugfs_scoped.rs:54:1
>>    |
>> 54 | / fn create_file_write(
>> 55 | |     mod_data: &ModuleData,
>> 56 | |     reader: &mut kernel::uaccess::UserSliceReader,
>> 57 | | ) -> Result {
>>    | |___________^
>>    |
>>    = note: the stack size is inferred from instruction `sub $0x1178,%rsp` at .text+2205
>>
>> Use pin-init to create the array in-place instead.
>>
>> Fixes: f656279afde1 ("samples: rust: debugfs_scoped: add example for blobs")
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>> ---
>>  samples/rust/rust_debugfs_scoped.rs | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/samples/rust/rust_debugfs_scoped.rs b/samples/rust/rust_debugfs_scoped.rs
>> index 6a575a15a2c2..33a4a0865f5b 100644
>> --- a/samples/rust/rust_debugfs_scoped.rs
>> +++ b/samples/rust/rust_debugfs_scoped.rs
>> @@ -75,7 +75,7 @@ fn create_file_write(
>>              GFP_KERNEL,
>>          )?;
>>      }
>> -    let blob = KBox::pin_init(new_mutex!([0x42; SZ_4K]), GFP_KERNEL)?;
>> +    let blob = KBox::pin_init(new_mutex!(pin_init::init_array_from_fn(|_| 0x42)), GFP_KERNEL)?;
>
> Since this is a sample, do we want to specify the size explicitly as it
> is arguably more readable in this case?

It's not easy to specify size with `init_array_from_fn` because you need to
specify additionally Init, T and E, so it'll become

    init_array_from_fn::<_, SZ_4K, _, _>

which is ugly.

Best,
Gary