Re: [PATCH] mm: mempolicy: fix the absence of the last bit of nodemask

From: Michal Hocko
Date: Mon Oct 14 2019 - 05:12:47 EST

[Cc Christopher - th initial emails is]

On Sat 12-10-19 20:19:48, Pan Zhang wrote:
> When I want to use set_mempolicy to get the memory from each node on the numa machine,
> and the MPOL_INTERLEAVE flag seems to achieve this goal.
> However, during the test, it was found that the use result of node was unbalanced.
> The memory was allocated evenly from the nodes except the last node,
> which obviously did not match the expectations.
> You can test as follows:
> 1. Create a file that needs to be mmap ped:
> dd if=/dev/zero of=./test count=1024 bs=1M

This will already poppulate the page cache and if it fits into memory
(which seems to be the case in your example output) then your mmap later
will not allocate any new memory.

I suspect that using numactl --interleave 0,1 dd if=/dev/zero of=./test count=1024 bs=1M

will produce an output much closer to your expectation. Right?

> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 4ae967b..a23509f 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -1328,9 +1328,11 @@ static int get_nodes(nodemask_t *nodes, const unsigned long __user *nmask,
> unsigned long nlongs;
> unsigned long endmask;
> - --maxnode;
> nodes_clear(*nodes);
> - if (maxnode == 0 || !nmask)
> + /*
> + * If the user specified only one node, no need to set nodemask
> + */
> + if (maxnode - 1 == 0 || !nmask)
> return 0;
> if (maxnode > PAGE_SIZE*BITS_PER_BYTE)
> return -EINVAL;

I am afraid this is a wrong fix. It is really hard to grasp the code but my
understanding is that the caller is supposed to provide maxnode larger
than than the nodemask. So if you want 2 nodes then maxnode should be 3.
Have a look at the libnuma (which is a reference implementation)

static void setpol(int policy, struct bitmask *bmp)
if (set_mempolicy(policy, bmp->maskp, bmp->size + 1) < 0)

The semantic is quite awkward but it is that way for years.
Michal Hocko