Re: [PATCH 1/2] rust: Add initial cpumask abstractions
From: Viresh Kumar
Date: Tue Feb 25 2025 - 06:52:58 EST
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.
One easy way to make it work was using Box, which keeps the code
simple.
Should I do it differently ?
--
viresh