From patchwork Wed Dec 8 16:53:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Ryabinin X-Patchwork-Id: 12664801 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DC9DC433EF for ; Wed, 8 Dec 2021 16:53:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9658A6B0072; Wed, 8 Dec 2021 11:52:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EED06B0073; Wed, 8 Dec 2021 11:52:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 768ED6B0074; Wed, 8 Dec 2021 11:52:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0114.hostedemail.com [216.40.44.114]) by kanga.kvack.org (Postfix) with ESMTP id 6698D6B0072 for ; Wed, 8 Dec 2021 11:52:55 -0500 (EST) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 240C474D09 for ; Wed, 8 Dec 2021 16:52:45 +0000 (UTC) X-FDA: 78895221090.03.6D4F92E Received: from forwardcorp1j.mail.yandex.net (forwardcorp1j.mail.yandex.net [5.45.199.163]) by imf04.hostedemail.com (Postfix) with ESMTP id AB50940005 for ; Wed, 8 Dec 2021 16:52:43 +0000 (UTC) Received: from myt5-23f0be3aa648.qloud-c.yandex.net (myt5-23f0be3aa648.qloud-c.yandex.net [IPv6:2a02:6b8:c12:3e29:0:640:23f0:be3a]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 04DC62E19DF; Wed, 8 Dec 2021 19:52:41 +0300 (MSK) Received: from myt5-70c90f7d6d7d.qloud-c.yandex.net (myt5-70c90f7d6d7d.qloud-c.yandex.net [2a02:6b8:c12:3e2c:0:640:70c9:f7d]) by myt5-23f0be3aa648.qloud-c.yandex.net (mxbackcorp/Yandex) with ESMTP id x8NnySuGmu-qeL83aS8; Wed, 08 Dec 2021 19:52:40 +0300 List-Unsubscribe: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.com; s=default; t=1638982360; bh=uWDV/C7qHJTbeUDXiMYY7w20fkxR4a9fgoLjBp1RhWc=; h=Message-Id:Date:Subject:To:From:Cc; b=Nvhxd7o4cD1BI/1i+K7EeQaNno0Y3xmhJC0YQ9iSfSXcHQT6WLHgNavHEndgCZhwo 2vnb9jFI6ZmO8umbPEDepEyM9MFAnUv2mZgmob2zCqTVDpxise6PUiYlymQ877V+5x 7iZrC2mKvQRZcpnn3oEHbP0oqWJRiwGRxFKa/6Ms= Received: from dellarbn.yandex.net (dynamic-red3.dhcp.yndx.net [2a02:6b8:0:107:3e85:844d:5b1d:60a]) by myt5-70c90f7d6d7d.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id 0aLko5JcgA-qePCic9n; Wed, 08 Dec 2021 19:52:40 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) X-Yandex-Fwd: 2 From: Andrey Ryabinin To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yc-core@yandex-team.ru, Andrey Ryabinin , stable@vger.kernel.org, Andrea Arcangeli , Michal Hocko , Mel Gorman , David Rientjes Subject: [PATCH] mm: mempolicy: fix THP allocations escaping mempolicy restrictions Date: Wed, 8 Dec 2021 19:53:43 +0300 Message-Id: <20211208165343.22349-1-arbn@yandex-team.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: AB50940005 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=yandex-team.com header.s=default header.b=Nvhxd7o4; spf=pass (imf04.hostedemail.com: domain of arbn@yandex-team.com designates 5.45.199.163 as permitted sender) smtp.mailfrom=arbn@yandex-team.com; dmarc=pass (policy=none) header.from=yandex-team.com X-Rspamd-Server: rspam04 X-Stat-Signature: a93a18fainn47f36tytp8g588wu4hytn X-HE-Tag: 1638982363-669840 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 #include #include #include #include #include #include #include #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 Fixes: ac5b2c18911f ("mm: thp: relax __GFP_THISNODE for MADV_HUGEPAGE mappings") Signed-off-by: Andrey Ryabinin Cc: Cc: Andrea Arcangeli Cc: Michal Hocko Cc: Mel Gorman Cc: David Rientjes Acked-by: Michal Hocko Acked-by: Mel Gorman Acked-by: David Rientjes --- mm/mempolicy.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 10e9c87260ed..f6248affaf38 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2140,8 +2140,7 @@ struct page *alloc_pages_vma(gfp_t gfp, int order, struct vm_area_struct *vma, * memory with both reclaim and compact as well. */ if (!page && (gfp & __GFP_DIRECT_RECLAIM)) - page = __alloc_pages_node(hpage_node, - gfp, order); + page = __alloc_pages(gfp, order, hpage_node, nmask); goto out; }