Re: [PATCH 1/2] rust: Add initial cpumask abstractions

From: Alice Ryhl
Date: Tue Feb 25 2025 - 06:57:58 EST


On Tue, Feb 25, 2025 at 12:48 PM Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
>
> On 25-02-25, 10:55, Alice Ryhl wrote:
> > On Tue, Feb 25, 2025 at 10:47 AM Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
> > > + /// Creates cpumask.
> > > + #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
> > > + fn new_inner(empty: bool) -> Result<Self> {
> > > + let ptr = KBox::into_raw(KBox::new([bindings::cpumask::default(); 1], GFP_KERNEL)?);
> >
> > I don't really understand this CPUMASK_OFFSTACK logic. You seem to
> > always allocate memory, but if OFFSTACK=n, then shouldn't it be on the
> > stack ...?
>
> IIUC, the idea of the config option is to prevent stack overflow on
> systems with high number of CPUs (> 256), in which case the memory for
> the masks is allocated dynamically. Otherwise a local variable, in a
> function or a struct (which may itself get allocated dynamically) is
> fine.
>
> In the CONFIG_CPUMASK_OFFSTACK=y case, the cpumask C core does the
> allocation and the Rust code doesn't need to take care of the same.
>
> In the CONFIG_CPUMASK_OFFSTACK=n case, the allocation must be done by
> the caller (on stack or heap) and the cpumask C core will only clear
> the mask.
>
> I tried with an on-stack variable earlier but ran into problems as the
> memory is shared with the C FFI and Rust moves it unless it is pinned.

Is it a problem if a value of type struct cpumask is moved? It looks
like it is just an array of longs?

Alice