From mboxrd@z Thu Jan 1 00:00:00 1970 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 CF313C433F5 for ; Fri, 10 Dec 2021 09:52:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E76F86B0071; Fri, 10 Dec 2021 04:52:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E28046B0072; Fri, 10 Dec 2021 04:52:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D15946B0074; Fri, 10 Dec 2021 04:52:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0063.hostedemail.com [216.40.44.63]) by kanga.kvack.org (Postfix) with ESMTP id C1FBF6B0071 for ; Fri, 10 Dec 2021 04:52:39 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5E270181AF5C3 for ; Fri, 10 Dec 2021 09:52:29 +0000 (UTC) X-FDA: 78901419618.12.2285FE4 Received: from outbound-smtp17.blacknight.com (outbound-smtp17.blacknight.com [46.22.139.234]) by imf28.hostedemail.com (Postfix) with ESMTP id A27C6C000A for ; Fri, 10 Dec 2021 09:52:28 +0000 (UTC) Received: from mail.blacknight.com (pemlinmail02.blacknight.ie [81.17.254.11]) by outbound-smtp17.blacknight.com (Postfix) with ESMTPS id D460F1C3E72 for ; Fri, 10 Dec 2021 09:52:26 +0000 (GMT) Received: (qmail 28236 invoked from network); 10 Dec 2021 09:52:26 -0000 Received: from unknown (HELO techsingularity.net) (mgorman@techsingularity.net@[84.203.197.169]) by 81.17.254.9 with ESMTPSA (AES256-SHA encrypted, authenticated); 10 Dec 2021 09:52:26 -0000 Date: Fri, 10 Dec 2021 09:52:25 +0000 From: Mel Gorman To: Andrey Ryabinin Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, yc-core@yandex-team.ru, stable@vger.kernel.org, Andrea Arcangeli , Michal Hocko , David Rientjes Subject: Re: [PATCH] mm: mempolicy: fix THP allocations escaping mempolicy restrictions Message-ID: <20211210095225.GP3366@techsingularity.net> References: <20211208165343.22349-1-arbn@yandex-team.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Disposition: inline In-Reply-To: <20211208165343.22349-1-arbn@yandex-team.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Stat-Signature: w9q4t4tbjmmr9bmq99yx9jyh8ktnu9rc Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of mgorman@techsingularity.net designates 46.22.139.234 as permitted sender) smtp.mailfrom=mgorman@techsingularity.net X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A27C6C000A X-HE-Tag: 1639129948-203434 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: On Wed, Dec 08, 2021 at 07:53:43PM +0300, Andrey Ryabinin wrote: > 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: Mel Gorman -- Mel Gorman SUSE Labs