Andrey Ryabinin
ee6f34215c
mm: mempolicy: fix THP allocations escaping mempolicy restrictions
...
commit 338635340669d5b317c7e8dcf4fff4a0f3651d87 upstream.
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 ac5b2c18911f
("mm: thp: relax __GFP_THISNODE for MADV_HUGEPAGE mappings").
The bug disappeared later in the commit 89c83fb539f9 ("mm, thp:
consolidate THP gfp handling into alloc_hugepage_direct_gfpmask") and
reappeared again in slightly different form in the commit 76e654cc91bb
("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: ac5b2c18911f ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-12-29 12:26:06 +01:00
..
2021-01-27 11:55:23 +01:00
2021-12-14 11:32:37 +01:00
2019-09-04 07:42:01 -04:00
2020-07-10 13:54:00 -07:00
2020-08-12 10:57:57 -07:00
2020-09-01 09:19:43 +02:00
2021-03-04 11:38:38 +01:00
2021-07-14 16:56:13 +02:00
2020-10-13 18:38:29 -07:00
2020-10-13 18:38:32 -07:00
2020-01-31 10:30:38 -08:00
2020-10-13 18:38:29 -07:00
2021-02-10 09:29:21 +01:00
2020-06-09 09:39:14 -07:00
2020-08-14 19:56:56 -07:00
2020-10-18 09:27:09 -07:00
2021-07-14 16:55:42 +02:00
2020-10-16 11:11:18 -07:00
2021-09-18 13:40:36 +02:00
2021-07-25 14:36:18 +02:00
2021-03-30 14:31:54 +02:00
2021-11-26 10:39:21 +01:00
2020-10-16 11:11:16 -07:00
2020-12-30 11:53:54 +01:00
2021-06-30 08:47:27 -04:00
2020-08-07 11:33:26 -07:00
2020-12-06 10:19:07 -08:00
2020-06-14 01:57:21 +09:00
2021-11-06 14:10:09 +01:00
2020-10-13 18:38:27 -07:00
2021-05-19 10:13:07 +02:00
2020-12-06 10:19:07 -08:00
2020-08-12 10:57:59 -07:00
2021-03-17 17:06:37 +01:00
2020-10-13 18:38:27 -07:00
2020-04-02 09:35:29 -07:00
2021-12-17 10:14:42 +01:00
2021-11-18 14:04:29 +01:00
2019-09-24 15:54:08 -07:00
2021-09-22 12:27:59 +02:00
2021-12-29 12:26:05 +01:00
2021-07-25 14:36:18 +02:00
2021-12-29 12:26:06 +01:00
2020-10-13 18:38:34 -07:00
2021-03-04 11:38:21 +01:00
2021-06-30 08:47:26 -04:00
2020-10-13 18:38:29 -07:00
2020-09-19 13:13:38 -07:00
2020-08-07 11:33:26 -07:00
2020-12-06 10:19:07 -08:00
2020-06-09 09:39:14 -07:00
2021-03-30 14:32:06 +02:00
2020-09-04 12:46:07 +01:00
2020-08-07 11:33:27 -07:00
2020-06-09 09:39:14 -07:00
2020-10-18 09:27:10 -07:00
2021-11-18 14:04:30 +01:00
2021-09-12 08:58:26 +02:00
2020-10-13 18:38:30 -07:00
2020-04-07 10:43:40 -07:00
2020-06-08 11:05:55 -07:00
2021-03-07 12:34:15 +01:00
2020-10-16 11:11:19 -07:00
2020-10-16 11:11:19 -07:00
2020-10-16 11:11:17 -07:00
2020-10-16 11:11:19 -07:00
2020-06-09 09:39:13 -07:00
2021-06-30 08:47:29 -04:00
2021-01-12 20:18:22 +01:00
2020-06-09 09:39:14 -07:00
2021-04-14 08:42:03 +02:00
2020-08-12 10:57:55 -07:00
2021-04-14 08:42:03 +02:00
2020-08-12 10:57:55 -07:00
2021-04-14 08:42:03 +02:00
2020-08-07 11:33:26 -07:00
2021-06-30 08:47:26 -04:00
2021-01-19 18:27:21 +01:00
2021-04-21 13:00:57 +02:00
2020-10-17 13:49:08 -06:00
2021-06-30 08:47:27 -04:00
2020-08-21 09:52:53 -07:00
2021-07-25 14:36:20 +02:00
2020-10-16 11:11:19 -07:00
2020-08-07 11:33:29 -07:00
2021-06-23 14:42:54 +02:00
2021-05-14 09:50:45 +02:00
2021-11-26 10:39:19 +01:00
2020-08-07 11:33:24 -07:00
2021-10-27 09:56:53 +02:00
2020-08-07 11:33:27 -07:00
2021-05-14 09:50:45 +02:00
2020-06-03 20:09:48 -07:00
2020-10-13 18:38:30 -07:00
2020-10-16 11:11:19 -07:00
2020-10-13 18:38:33 -07:00
2021-06-23 14:42:53 +02:00
2021-06-30 08:47:27 -04:00
2020-08-12 10:57:58 -07:00
2020-08-12 10:57:55 -07:00
2021-10-06 15:56:03 +02:00
2020-06-10 19:14:18 -07:00
2021-01-19 18:27:21 +01:00
2020-04-02 09:35:31 -07:00
2021-09-18 13:40:36 +02:00
2020-10-16 11:11:17 -07:00
2020-10-20 14:39:37 -07:00
2021-07-14 16:56:51 +02:00
2020-10-13 18:38:34 -07:00
2020-08-12 10:57:58 -07:00
2021-11-18 14:04:26 +01:00
2020-04-07 10:43:41 -07:00