Re: [PATCH 04/11] mm/mempolicy: preserve full-width userspace nodemasks
From: Yury Norov
Date: Tue Dec 01 2026 - 16:05:52 EST
On Tue, Dec 01, 2026 at 11:10:42AM +0100, David Hildenbrand (Arm) wrote:
> On 11/28/26 00:01, Yury Norov wrote:
> > This series switches nodemask API to runtime nr_node_ids.
> >
> > Userspace mempolicy masks can contain static node IDs or relative
> > indices above nr_node_ids. They must retain their full MAX_NUMNODES
> > width when the nodemask helpers switch to runtime bounds.
> >
> > For example, with MAX_NUMNODES=1024, nr_node_ids=2 and allowed
> > memory nodes {0,1},
> >
> > set_mempolicy(MPOL_BIND | MPOL_F_RELATIVE_NODES, ...)
> >
> > can specify a mask containing only bit 3. This is a relative
> > index: folding it modulo the two allowed nodes maps it to node 1.
> >
> > A runtime-bounded emptiness check would reject this valid mask,
> > and a runtime-bounded fold would discard its only set bit.
> >
> > Use underscored nodemask API with an explicit MAX_NUMNODES bound where
> > needed.
> >
> > Signed-off-by: Yury Norov <ynorov@xxxxxxxxxx>
> > ---
> > mm/mempolicy.c | 29 +++++++++++++++++++----------
> > 1 file changed, 19 insertions(+), 10 deletions(-)
> >
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index 67e258c62527..ed8b20b27753 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -396,7 +396,10 @@ static void mpol_relative_nodemask(nodemask_t *ret, const nodemask_t *orig,
> > const nodemask_t *rel)
> > {
> > nodemask_t tmp;
> > - nodes_fold(tmp, *orig, nodes_weight(*rel));
> > +
> > + /* Relative indices can exceed the runtime node ID limit. */
> > + __nodes_fold(&tmp, orig, nodes_weight(*rel), MAX_NUMNODES);
>
> This is a bit shaky. It means that we can silently introduce new issues by using
> the "obvious" helper.
That's why the comment is there. Using double-underscored alternative
for some helper is never obvious IMO.
> Maybe we should just have a set of helpers for these cases
>
> __nodes_fold(&tmp, orig, nodes_weight(*rel), MAX_NUMNODES);
> __nodes_empty(nodes, MAX_NUMNODES
> __nodes_clear(nodes, MAX_NUMNODES)
> ...
>
> that embed in their name what they should be used for (I assume, whenever we are
> interacting with user-space)?
There's 6 direct users of the underscored API in the kernel, all in
mm/mempolicy.c. Let's maybe introduce something like
mm_usernodes_fold()
in mempolicy.h? If there's more users, we can always move the helpers
to the system-wide header. LMK what do you think?