Re: [PATCH] mm/hugetlb: add mempolicy check in the reservation routine

From: Michal Hocko
Date: Fri Jul 24 2020 - 03:39:48 EST


On Thu 23-07-20 15:44:17, Muchun Song wrote:
> In the reservation routine, we only check whether the cpuset meets
> the memory allocation requirements. But we ignore the mempolicy of
> MPOL_BIND case. If someone mmap hugetlb succeeds, but the subsequent
> memory allocation may fail due to mempolicy restrictions and receives
> the SIGBUS signal. This can be reproduced by the follow steps.
>
> 1) Compile the test case.
> cd tools/testing/selftests/vm/
> gcc map_hugetlb.c -o map_hugetlb
>
> 2) Pre-allocate huge pages. Suppose there are 2 numa nodes in the
> system. Each node will pre-allocate one huge page.
> echo 2 > /proc/sys/vm/nr_hugepages
>
> 3) Run test case(mmap 4MB). We receive the SIGBUS signal.
> numactl --membind=0 ./map_hugetlb 4

Cpusets and mempolicy interaction has always been a nightmare and
semantic might get really awkward in some cases. In this case I am not
really sure anybody really does soemthing like that but anyway...

[...]

> -static unsigned int cpuset_mems_nr(unsigned int *array)
> +static nodemask_t *mempolicy_current_bind_nodemask(void)
> +{
> + struct mempolicy *mpol;
> + nodemask_t *nodemask;
> +
> + mpol = get_task_policy(current);
> + if (mpol->mode == MPOL_BIND)
> + nodemask = &mpol->v.nodes;
> + else
> + nodemask = NULL;
> +
> + return nodemask;
> +}

We already have policy_nodemask which tries to do this. Is there any
reason to not reuse it?
--
Michal Hocko
SUSE Labs