Re: [PATCH v4 7/9] rust: percpu: Support non-zeroable types for DynamicPerCpu

From: Andreas Hindborg

Date: Fri Nov 14 2025 - 10:18:58 EST


"Mitchell Levy" <levymitchell0@xxxxxxxxx> writes:

> Add functionality to `PerCpuPtr` to compute pointers to per-CPU variable
> slots on other CPUs. Use this facility to initialize per-CPU variables
> on all possible CPUs when a dynamic per-CPU variable is created with a
> non-zeroable type. Since `RefCell` and other `Cell`-like types fall into
> this category, `impl CheckedPerCpu` on `DynamicPerCpu` for these
> `InteriorMutable` types since they can now be used. Add examples of
> these usages to `samples/rust/rust_percpu.rs`. Add a test to ensure
> dynamic per-CPU variables properly drop their contents, done here since
> non-trivially dropped types often aren't `Zeroable`.
>
> Signed-off-by: Mitchell Levy <levymitchell0@xxxxxxxxx>
> ---
> rust/kernel/percpu/dynamic.rs | 44 +++++++++++++++++
> samples/rust/rust_percpu.rs | 109 +++++++++++++++++++++++++++++++++++++++---
> 2 files changed, 146 insertions(+), 7 deletions(-)
>
> diff --git a/rust/kernel/percpu/dynamic.rs b/rust/kernel/percpu/dynamic.rs
> index 1863f31a2817..a74c8841aeb2 100644
> --- a/rust/kernel/percpu/dynamic.rs
> +++ b/rust/kernel/percpu/dynamic.rs
> @@ -89,6 +89,36 @@ pub fn new_zero(flags: Flags) -> Option<Self> {
> }
> }
>
> +impl<T: Clone> DynamicPerCpu<T> {
> + /// Allocates a new per-CPU variable
> + ///
> + /// # Arguments
> + /// * `val` - The initial value of the per-CPU variable on all CPUs.
> + /// * `flags` - Flags used to allocate an [`Arc`] that keeps track of the underlying
> + /// [`PerCpuAllocation`].
> + pub fn new_with(val: &T, flags: Flags) -> Option<Self> {

It would be great to be able to pass a closure here that can be used to
populate the value. Then the value does not have to be `Clone`.


Best regards,
Andreas Hindborg