Re: [PATCH v4 5/9] rust: percpu: introduce a rust API for dynamic per-CPU variables

From: Andreas Hindborg

Date: Fri Nov 14 2025 - 10:24:30 EST


"Mitchell Levy" <levymitchell0@xxxxxxxxx> writes:

> Dynamically allocated per-CPU variables are core to many of the
> use-cases of per-CPU variables (e.g., ref counting). Add support for
> them using the core `PerCpuPtr<T>` primitive, implementing the
> `PerCpu<T>` trait.
>
> Co-developed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: Mitchell Levy <levymitchell0@xxxxxxxxx>
> ---
> rust/helpers/percpu.c | 10 ++++
> rust/kernel/percpu.rs | 30 ++++++++--
> rust/kernel/percpu/dynamic.rs | 130 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 166 insertions(+), 4 deletions(-)
>
> diff --git a/rust/helpers/percpu.c b/rust/helpers/percpu.c
> index a091389f730f..35656333dfae 100644

<cut>

> diff --git a/rust/kernel/percpu/dynamic.rs b/rust/kernel/percpu/dynamic.rs
> new file mode 100644
> index 000000000000..1863f31a2817
> --- /dev/null
> +++ b/rust/kernel/percpu/dynamic.rs
> @@ -0,0 +1,130 @@
> +// SPDX-License-Identifier: GPL-2.0
> +//! Dynamically allocated per-CPU variables.
> +
> +use super::*;
> +
> +use crate::alloc::Flags;
> +use crate::bindings::{alloc_percpu, free_percpu};
> +use crate::cpumask::Cpumask;
> +use crate::prelude::*;
> +use crate::sync::Arc;
> +use core::mem::{align_of, size_of, MaybeUninit};
> +
> +/// Represents a dynamic allocation of a per-CPU variable via `alloc_percpu`. Calls `free_percpu`
> +/// when dropped.
> +///
> +/// # Contents
> +/// Note that the allocated memory need not be initialized, and this type does not track when/if
> +/// the memory location on any particular CPU has been initialized. This means that it cannot tell
> +/// whether it should drop the *contents* of the allocation when it is dropped. It is up to the
> +/// user to do this via something like [`core::ptr::drop_in_place`].
> +pub struct PerCpuAllocation<T>(PerCpuPtr<T>);
> +
> +impl<T: Zeroable> PerCpuAllocation<T> {
> + /// Dynamically allocates a space in the per-CPU area suitably sized and aligned to hold a `T`,
> + /// initially filled with the zero value for `T`.
> + ///
> + /// Returns [`None`] under the same circumstances the C function `alloc_percpu` returns `NULL`.
> + pub fn new_zero() -> Option<PerCpuAllocation<T>> {
> + let ptr: *mut MaybeUninit<T> =
> + // SAFETY: No preconditions to call `alloc_percpu`; `MaybeUninit<T>` is
> + // `#[repr(transparent)]`, so we can cast a `*mut T` to it.
> + unsafe { alloc_percpu(size_of::<T>(), align_of::<T>()) }.cast();
> + if ptr.is_null() {
> + return None;
> + }
> +
> + // alloc_percpu returns zero'ed memory
> + Some(Self(PerCpuPtr::new(ptr)))
> + }
> +}
> +
> +impl<T> PerCpuAllocation<T> {
> + /// Makes a per-CPU allocation sized and aligned to hold a `T`.
> + ///
> + /// Returns [`None`] under the same circumstances the C function `alloc_percpu` returns `NULL`.
> + pub fn new_uninit() -> Option<PerCpuAllocation<T>> {
> + let ptr: *mut MaybeUninit<T> =
> + // SAFETY: No preconditions to call `alloc_percpu`; `MaybeUninit<T>` is
> + // `#[repr(transparent)]`, so we can cast a `*mut T` to it.
> + unsafe { alloc_percpu(size_of::<T>(), align_of::<T>()) }.cast();
> + if ptr.is_null() {
> + return None;
> + }
> +
> + Some(Self(PerCpuPtr::new(ptr)))
> + }
> +}
> +
> +impl<T> Drop for PerCpuAllocation<T> {
> + fn drop(&mut self) {
> + // SAFETY: self.0.0 was returned by alloc_percpu, and so was a valid pointer into
> + // the percpu area, and has remained valid by the invariants of PerCpuAllocation<T>.
> + unsafe { free_percpu(self.0 .0.cast()) }
> + }
> +}
> +
> +/// Holds a dynamically-allocated per-CPU variable.

Can we place an example here? It was a bit difficult for me to figure
out how to use this from browsing the documentation. Perhaps we can lift
some of the sample out and add it as a documentation example here?


Best regards,
Andreas Hindborg