Andrey Ryabinin
3386353406
mm: mempolicy: fix THP allocations escaping mempolicy restrictions
...
alloc_pages_vma() may try to allocate THP page on the local NUMA node
first:
page = __alloc_pages_node(hpage_node,
gfp | __GFP_THISNODE | __GFP_NORETRY, order);
And if the allocation fails it retries allowing remote memory:
if (!page && (gfp & __GFP_DIRECT_RECLAIM))
page = __alloc_pages_node(hpage_node,
gfp, order);
However, this retry allocation completely ignores memory policy nodemask
allowing allocation to escape restrictions.
The first appearance of this bug seems to be the commit ac5b2c1891
("mm: thp: relax __GFP_THISNODE for MADV_HUGEPAGE mappings").
The bug disappeared later in the commit 89c83fb539
("mm, thp:
consolidate THP gfp handling into alloc_hugepage_direct_gfpmask") and
reappeared again in slightly different form in the commit 76e654cc91
("mm, page_alloc: allow hugepage fallback to remote nodes when
madvised")
Fix this by passing correct nodemask to the __alloc_pages() call.
The demonstration/reproducer of the problem:
$ mount -oremount,size=4G,huge=always /dev/shm/
$ echo always > /sys/kernel/mm/transparent_hugepage/defrag
$ cat mbind_thp.c
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <numaif.h>
#define SIZE 2ULL << 30
int main(int argc, char **argv)
{
int fd;
unsigned long long i;
char *addr;
pid_t pid;
char buf[100];
unsigned long nodemask = 1;
fd = open("/dev/shm/test", O_RDWR|O_CREAT);
assert(fd > 0);
assert(ftruncate(fd, SIZE) == 0);
addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
assert(mbind(addr, SIZE, MPOL_BIND, &nodemask, 2, MPOL_MF_STRICT|MPOL_MF_MOVE)==0);
for (i = 0; i < SIZE; i+=4096) {
addr[i] = 1;
}
pid = getpid();
snprintf(buf, sizeof(buf), "grep shm /proc/%d/numa_maps", pid);
system(buf);
sleep(10000);
return 0;
}
$ gcc mbind_thp.c -o mbind_thp -lnuma
$ numactl -H
available: 2 nodes (0-1)
node 0 cpus: 0 2
node 0 size: 1918 MB
node 0 free: 1595 MB
node 1 cpus: 1 3
node 1 size: 2014 MB
node 1 free: 1731 MB
node distances:
node 0 1
0: 10 20
1: 20 10
$ rm -f /dev/shm/test; taskset -c 0 ./mbind_thp
7fd970a00000 bind:0 file=/dev/shm/test dirty=524288 active=0 N0=396800 N1=127488 kernelpagesize_kB=4
Link: https://lkml.kernel.org/r/20211208165343.22349-1-arbn@yandex-team.com
Fixes: ac5b2c1891
("mm: thp: relax __GFP_THISNODE for MADV_HUGEPAGE mappings")
Signed-off-by: Andrey Ryabinin <arbn@yandex-team.com >
Acked-by: Michal Hocko <mhocko@suse.com >
Acked-by: Mel Gorman <mgorman@techsingularity.net >
Acked-by: David Rientjes <rientjes@google.com >
Cc: Andrea Arcangeli <aarcange@redhat.com >
Cc: <stable@vger.kernel.org >
Signed-off-by: Andrew Morton <akpm@linux-foundation.org >
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org >
2021-12-25 12:20:55 -08:00
..
2021-12-10 17:10:56 -08:00
2021-11-11 09:34:35 -08:00
2021-12-25 12:20:55 -08:00
2021-12-10 17:10:56 -08:00
2021-05-07 00:26:35 -07:00
2021-09-03 09:58:14 -07:00
2021-05-05 11:27:21 -07:00
2021-05-05 11:27:24 -07:00
2021-11-06 13:30:41 -07:00
2021-05-05 11:27:24 -07:00
2021-11-06 14:08:17 -07:00
2021-11-06 13:30:33 -07:00
2021-11-06 14:08:17 -07:00
2021-06-29 10:53:52 -07:00
2021-09-08 11:50:24 -07:00
2020-10-13 18:38:29 -07:00
2021-12-10 17:10:55 -08:00
2021-10-18 07:49:41 -04:00
2021-05-05 11:27:27 -07:00
2021-05-05 11:27:26 -07:00
2021-05-05 11:27:26 -07:00
2021-11-06 14:08:17 -07:00
2021-11-25 10:13:56 -08:00
2021-09-08 18:45:52 -07:00
2021-11-01 08:47:59 -07:00
2021-11-06 13:30:39 -07:00
2021-06-30 20:47:26 -07:00
2021-06-30 20:47:25 -07:00
2021-12-10 17:10:56 -08:00
2021-09-03 09:58:15 -07:00
2021-07-08 11:48:21 -07:00
2021-11-06 14:08:17 -07:00
2021-04-30 11:20:38 -07:00
2021-04-30 11:20:39 -07:00
2021-09-08 11:50:24 -07:00
2021-12-06 12:45:09 -05:00
2020-12-15 12:13:46 -08:00
2021-11-06 14:08:17 -07:00
2021-09-08 18:45:53 -07:00
2021-10-18 07:49:39 -04:00
2021-11-06 13:30:35 -07:00
2021-08-20 11:39:25 +01:00
2021-10-14 13:29:22 +02:00
2021-09-27 09:27:30 -04:00
2021-07-01 11:06:02 -07:00
2021-11-10 11:29:30 -08:00
2021-12-10 17:10:56 -08:00
2021-11-09 10:02:48 -08:00
2021-12-03 10:58:13 -08:00
2021-11-13 12:03:03 -08:00
2021-11-06 14:08:17 -07:00
2021-12-25 12:20:55 -08:00
2021-10-18 06:17:01 -06:00
2021-09-27 09:27:31 -04:00
2021-11-11 09:34:35 -08:00
2021-01-24 14:27:16 +01:00
2021-09-27 09:27:31 -04:00
2021-04-30 11:20:42 -07:00
2021-07-23 17:43:28 -07:00
2021-11-09 10:02:48 -08:00
2021-04-16 16:10:36 -07:00
2021-03-25 09:22:55 -07:00
2020-12-15 14:48:04 -08:00
2021-11-06 13:30:36 -07:00
2021-11-06 13:30:39 -07:00
2021-04-30 11:20:37 -07:00
2021-11-06 14:08:17 -07:00
2021-11-10 16:02:08 -08:00
2021-11-06 14:08:17 -07:00
2021-04-30 11:20:38 -07:00
2021-11-06 13:30:34 -07:00
2021-09-08 11:50:24 -07:00
2021-11-01 09:19:50 -07:00
2021-11-06 13:30:40 -07:00
2021-11-11 09:34:35 -08:00
2021-04-30 11:20:36 -07:00
2021-06-29 10:53:47 -07:00
2021-06-29 10:53:47 -07:00
2021-07-01 11:06:03 -07:00
2021-11-10 21:16:52 +00:00
2021-06-29 10:53:49 -07:00
2021-07-01 17:17:24 -07:00
2021-07-04 18:30:17 +00:00
2021-06-05 20:43:15 +00:00
2021-07-04 18:30:17 +00:00
2021-11-06 13:30:41 -07:00
2021-05-07 00:26:35 -07:00
2021-06-16 09:24:42 -07:00
2021-05-05 11:27:27 -07:00
2021-04-16 16:10:37 -07:00
2021-11-06 14:08:17 -07:00
2021-11-06 14:08:17 -07:00
2021-10-28 17:18:55 -07:00
2021-11-17 10:36:35 -05:00
2021-04-16 16:10:36 -07:00
2021-05-22 15:09:07 -10:00
2021-11-06 13:30:43 -07:00
2021-11-20 10:35:54 -08:00
2021-11-20 10:35:54 -08:00
2021-11-20 10:35:54 -08:00
2021-12-10 17:10:56 -08:00
2021-11-06 13:30:36 -07:00
2021-11-06 13:30:41 -07:00
2021-12-03 10:58:13 -08:00
2021-10-18 07:49:40 -04:00
2021-11-20 10:35:54 -08:00
2021-11-06 14:08:17 -07:00
2021-11-09 10:02:48 -08:00
2021-11-13 12:03:03 -08:00
2021-11-17 10:36:35 -05:00
2021-11-06 13:30:37 -07:00
2021-11-06 13:30:40 -07:00
2021-11-09 10:11:53 -08:00
2021-11-06 13:30:42 -07:00
2021-11-09 10:11:53 -08:00
2021-07-01 11:06:03 -07:00
2021-07-01 11:06:03 -07:00
2021-05-07 00:26:35 -07:00
2021-11-06 13:30:43 -07:00
2021-06-30 20:47:31 -07:00