From patchwork Thu Jul 23 07:44:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11680377 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D57DE138C for ; Thu, 23 Jul 2020 07:45:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A118120714 for ; Thu, 23 Jul 2020 07:45:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="k2QQsu/o" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A118120714 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A02FC6B0002; Thu, 23 Jul 2020 03:45:11 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9B2066B0005; Thu, 23 Jul 2020 03:45:11 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8A2A26B0006; Thu, 23 Jul 2020 03:45:11 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 717546B0002 for ; Thu, 23 Jul 2020 03:45:11 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E3A21824805A for ; Thu, 23 Jul 2020 07:45:10 +0000 (UTC) X-FDA: 77068554780.22.nose55_5e16ef526f3d Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin22.hostedemail.com (Postfix) with ESMTP id B3C611808CDCE for ; Thu, 23 Jul 2020 07:45:10 +0000 (UTC) X-Spam-Summary: 1,0,0,8d91260f6f45344e,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1311:1314:1345:1437:1515:1535:1542:1711:1730:1747:1777:1792:2196:2199:2393:2553:2559:2562:2693:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:4250:4385:5007:6119:6261:6653:7903:10004:11026:11658:11914:12043:12291:12296:12297:12438:12517:12519:12555:12683:12895:13161:13221:13229:13894:14093:14181:14394:14721:21080:21444:21451:21627:21939:21990:30054:30056:30070:30090,0,RBL:209.85.216.67:@bytedance.com:.lbl8.mailshell.net-62.2.0.100 66.100.201.201;04yf4myr45ncmupushnsy5s1ecdzoopzu1mei93nbfqpmyauy887mcxnhyjp91b.7i3tynbzk7ybkukriqceaoj7iy47ahmacrfr3wwodir5888germffeb8qpcypop.q-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: nose55_5e16ef526f3d X-Filterd-Recvd-Size: 5430 Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Thu, 23 Jul 2020 07:45:09 +0000 (UTC) Received: by mail-pj1-f67.google.com with SMTP id md7so2669407pjb.1 for ; Thu, 23 Jul 2020 00:45:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GlrEuOnc8m9qfFKEmGd35c8KAaqdG3oG7G+fWZk2PVw=; b=k2QQsu/oPzg9h9QS226YCvxxGTqjMuGzTSIL9OemCJtcKgtr9y6fHZwRO3mA1Njiym rrPygf6i1qZhelPIDyNsKq8n/4FLglc0Laf0Bqwa1Cb7h2l0h/ov++9bfLrAA9vt2rr4 QI04xlSoXpNO/Zy3X59z7dEdTz8Cr3KfRIaJ4/Ua+Z1jJqWJulgQsAGrlThol6DcuHXx G1IGmSOBt/IAYEp+LlEwOvCveMliiIK//w5r0NhygI1uNoeGpmLwBbjtmxzDZIrVUHTW UFqG5oEQS81yjJNXsXmogre95nI8lsWdgmBYVqanG/XAc2i/8TSlZgYgNmHlY9I8BAzU J3aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=GlrEuOnc8m9qfFKEmGd35c8KAaqdG3oG7G+fWZk2PVw=; b=DYIvC5ihUNSYBu7wvHYiIROzx8A1c6I0WN5CU4HWPI4UvDdsdq6jE+MTMmARcztUXn Xqf6kcg9phu7jB2QltrX8mVZYPRJalYk6Yq3LYujIktEthBUvRu2cKk74fn9XhufFeGH qs6Ga29qQlH3qhwJycd/dUwoGCYsn32wbrUIAml4FQHZRPSEaLkCwMg5BBd/wUPhsz3u qDsMttUfj53jDQCnOS8S9KlyKRWZEBKYT64W3zvJKQMRl4MGe7OILjs0zLZjs+YTKfaW DMmGDu/L6VQpVkFUOkkwJi4QNjjO9t5YiewP8YXzBz1xFOt8xEPiDodrmjVuJfL/3OSM ng/w== X-Gm-Message-State: AOAM532xYKoVF66th3eLKPJAFODhn4XLIeGXO6Ie5bWFvhIwKS4t6IHp DMrLE77UHy9aQ8VyO/bnKoAPPA== X-Google-Smtp-Source: ABdhPJzrTrP1vmoeo8u6usfXOm0nqCvSjUfCxJNhmcLL3AoexdxLEKQYKZHRjnbd2vt3RjE6v9ejMw== X-Received: by 2002:a17:90a:362e:: with SMTP id s43mr3076100pjb.217.1595490307281; Thu, 23 Jul 2020 00:45:07 -0700 (PDT) Received: from Smcdef-MBP.local.net ([103.136.220.69]) by smtp.gmail.com with ESMTPSA id 7sm1908904pgw.85.2020.07.23.00.45.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Jul 2020 00:45:06 -0700 (PDT) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Jianchao Guo Subject: [PATCH] mm/hugetlb: add mempolicy check in the reservation routine Date: Thu, 23 Jul 2020 15:44:17 +0800 Message-Id: <20200723074417.89467-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: B3C611808CDCE X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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: 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 With this patch applied, the mmap will fail in the step 3) and throw "mmap: Cannot allocate memory". Reported-by: Jianchao Guo Signed-off-by: Muchun Song --- mm/hugetlb.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 589c330df4db..e946f41b4dcb 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3463,12 +3463,36 @@ static int __init default_hugepagesz_setup(char *s) } __setup("default_hugepagesz=", default_hugepagesz_setup); -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; +} + +static unsigned int allowed_mems_nr(unsigned int *array) { int node; unsigned int nr = 0; + nodemask_t *mempolicy_allowed, *mems_allowed, nodemask; + + mempolicy_allowed = mempolicy_current_bind_nodemask(); + if (mempolicy_allowed) { + nodes_and(nodemask, cpuset_current_mems_allowed, + *mempolicy_allowed); + mems_allowed = &nodemask; + } else { + mems_allowed = &cpuset_current_mems_allowed; + } - for_each_node_mask(node, cpuset_current_mems_allowed) + for_each_node_mask(node, *mems_allowed) nr += array[node]; return nr; @@ -3653,7 +3677,7 @@ static int hugetlb_acct_memory(struct hstate *h, long delta) if (gather_surplus_pages(h, delta) < 0) goto out; - if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { + if (delta > allowed_mems_nr(h->free_huge_pages_node)) { return_unused_surplus_pages(h, delta); goto out; }