[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87jyzs3ae7.fsf@t14s.mail-host-address-is-not-set>
Date: Fri, 14 Nov 2025 16:24:16 +0100
From: Andreas Hindborg <a.hindborg@...nel.org>
To: Mitchell Levy <levymitchell0@...il.com>, Miguel Ojeda
<ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng
<boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn
Roy Baron
<bjorn3_gh@...tonmail.com>, Alice Ryhl <aliceryhl@...gle.com>, Trevor
Gross <tmgross@...ch.edu>, Andrew Morton <akpm@...ux-foundation.org>,
Dennis Zhou <dennis@...nel.org>, Tejun Heo <tj@...nel.org>, Christoph
Lameter <cl@...ux.com>, Danilo Krummrich <dakr@...nel.org>, Benno Lossin
<lossin@...nel.org>, Yury Norov <yury.norov@...il.com>, Viresh Kumar
<viresh.kumar@...aro.org>
Cc: Tyler Hicks <code@...icks.com>, Allen Pais <apais@...ux.microsoft.com>,
linux-kernel@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-mm@...ck.org, Mitchell Levy <levymitchell0@...il.com>
Subject: Re: [PATCH v4 5/9] rust: percpu: introduce a rust API for dynamic
per-CPU variables
"Mitchell Levy" <levymitchell0@...il.com> 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@...il.com>
> Signed-off-by: Boqun Feng <boqun.feng@...il.com>
> Signed-off-by: Mitchell Levy <levymitchell0@...il.com>
> ---
> 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
Powered by blists - more mailing lists