linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gregory Price <gourry@gourry.net>
To: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
Cc: akpm@linux-foundation.org, david@redhat.com,
	joshua.hahnjy@gmail.com, ziy@nvidia.com,
	ying.huang@linux.alibaba.com, rakie.kim@sk.com, byungchul@sk.com,
	apopple@nvidia.com, linux-mm@kvack.org
Subject: Re: [PATCH] mm/mempolicy: prevent the risk of division by 0
Date: Mon, 8 Sep 2025 17:25:26 -0400	[thread overview]
Message-ID: <aL9Jxs3r423xhat3@gourry-fedora-PF4VCD3F> (raw)
In-Reply-To: <aL8cjd1mIAamBzXo@gourry-fedora-PF4VCD3F>

On Mon, Sep 08, 2025 at 02:12:29PM -0400, Gregory Price wrote:
> On Sun, Sep 07, 2025 at 09:08:29AM -0700, Chelsy Ratnawat wrote:
> > If no bits are set in the policy's node mask, then nodes will be 0.
> > This patch adds a check if nodes == 0 before dividing.
> > 
> > Signed-off-by: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
> > ---
> >  mm/mempolicy.c | 2 ++
> >  1 file changed, 2 insertions(+)
> > 
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index eb83cff7db8c..faacc604fc16 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -2530,6 +2530,8 @@ static unsigned long alloc_pages_bulk_interleave(gfp_t gfp,
> >  	unsigned long total_allocated = 0;
> >  
> >  	nodes = nodes_weight(pol->nodes);
> > +	if (nodes == 0)
> > +		return 0;
> 
> 3 second look at this code would tell you the only way this can happen
> is a mempolicy where MPOL_INTERLEAVE is set and the nodemask is empty
>

after some more thought... there is actually a race condition here that
is non-obvious

in some places we take a cpusets cookie to prevent pol->nodes from ever
showing up empty while iterating over the policy nodes.

Excerpt from: alloc_pages_bulk_weighted_interleave

```
        /* read the nodes onto the stack, retry if done during rebind */
        do {
                cpuset_mems_cookie = read_mems_allowed_begin();
                nnodes = read_once_policy_nodemask(pol, &nodes);
        } while (read_mems_allowed_retry(cpuset_mems_cookie));

        /* if the nodemask has become invalid, we cannot do anything */
        if (!nnodes)
                return 0;
```

So this actually can happen - otherwise I wouldn't have bothered with
the stack read in this code chunk anyway.

So now i've argued with and against myself, i think this patch
as-written is not sufficient - and in fact there's a very subtle bug in
the normal interleave that isn't present in the weighted interleave
because we take a copy of the nodes.

So mea culpa for the snap read and response.  But could you please
take a look at the weighted interleave code and model the fix off
of that?

~Gregory


      reply	other threads:[~2025-09-08 21:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-07 16:08 Chelsy Ratnawat
2025-09-07 23:53 ` Joshua Hahn
2025-09-08  3:48   ` Huang, Ying
2025-09-08  7:50     ` David Hildenbrand
2025-09-08 18:12 ` Gregory Price
2025-09-08 21:25   ` Gregory Price [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aL9Jxs3r423xhat3@gourry-fedora-PF4VCD3F \
    --to=gourry@gourry.net \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=byungchul@sk.com \
    --cc=chelsyratnawat2001@gmail.com \
    --cc=david@redhat.com \
    --cc=joshua.hahnjy@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=rakie.kim@sk.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox