Re: [PATCH 24/32] mm/mempolicy: reject an empty set of allowed nodes

From: David Hildenbrand (Arm)

Date: Wed Dec 02 2026 - 10:36:46 EST


On 12/2/26 06:55, Gregory Price (Meta) wrote:
> On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
>> From: Chris Mason <mason@xxxxxxxxxx>
>>
>> Installing a mempolicy with MPOL_F_RELATIVE_NODES can trigger a division
>> by zero when racing with memory offlining. I managed to reproduce this
>> in a small VM with excessive set_mempolicy() calls while offlining
>> memory:
>>
>> Oops: divide error: 0000 [#1] SMP
>> RIP: 0010:bitmap_fold+0x43/0x60
>> Call Trace:
>> ? mpol_relative_nodemask+0x26/0x40
>> ? mpol_set_nodemask+0x43/0x90
>> ? do_set_mempolicy+0x5e/0xe0
>> ? __x64_sys_set_mempolicy+0x6c/0x80
>>
>> mpol_set_nodemask() intersects current->mems_allowed with N_MEMORY, and
>> mpol_relative_nodemask() folds the user's nodemask modulo the weight of
>> the result.
>>
>> If memory offlining just cleared the task's only allowed node in N_MEMORY,
>> but cpuset did not update mems_allowed yet, the intersection is empty and
>> nodes_fold() divides by zero.
>>
>> do_mbind() and mpol_shared_policy_init() call mpol_set_nodemask() as
>> well.
>>
>> Let's fix it by failing with -EINVAL early if there is no allowed node
>> with memory, just like ->create() would later fail for an empty nodemask.
>>
>> The same division by zero was originally reported by syzkaller for
>> mpol_rebind_mm() [1]. The discussion of Yury's fix attempt [2] also
>> identified this path in mpol_set_nodemask() [3], but only the rebind
>> path got fixed, by commit b983c5642638 ("cgroup/cpuset: rebind mm mempolicy
>> to effective_mems, not mems_allowed").
>>
>> This has low urgency, as it is extremely unlikely to hit in practice.
>>
>> Fixes: 8fe23e057172 ("mm: clear node in N_HIGH_MEMORY and stop kswapd when all memory is offlined")
>> Cc: stable@xxxxxxxxxxxxxxx
>> Reported-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>> Closes: https://lore.kernel.org/all/fe33c767-ea11-43e2-8732-f752c9c1205c@xxxxxxxxxx/ [3]
>> Link: https://lore.kernel.org/all/CA+0ovCgxbZkXa+OU8w3s84R3KNPNxxRfmsNR-udh+afQBbGNmw@xxxxxxxxxxxxxx/ [1]
>> Link: https://lore.kernel.org/all/20260528190337.878027-1-ynorov@xxxxxxxxxx/ [2]
>> Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
>> [ david: update description and comment; add reproduction details ]
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>
> fwiw also reproduced finally
>
> Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
>
> while looking at the test, I wondered whether we could fix alot of these
> issues by figuring out how do all this work *BEFORE* dropping the node
> bit (N_MEMORY) and having people check something like node_has_memory()
> instead - but this seems like it'd be a pretty big lift.

Agreed.

>
> I've been learning that node_state[] walks race bigly with hotplug in
> non-obvious ways, and I don't like it.

Hot(un)plug races with many things in unfortunate ways ... :(

But at least it's not as nasty as memory-failure (ducks and runs away).

--
Cheers,

David