From patchwork Fri Jul 24 10:03:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11682821 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 E8093722 for ; Fri, 24 Jul 2020 10:04:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B37FF20737 for ; Fri, 24 Jul 2020 10:04:35 +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="y29o8C04" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B37FF20737 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 B0DE28D0025; Fri, 24 Jul 2020 06:04:34 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id ABE048D0020; Fri, 24 Jul 2020 06:04:34 -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 9855A8D0025; Fri, 24 Jul 2020 06:04:34 -0400 (EDT) X-Original-To: linux-mm@kvack.org 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 7E80A8D0020 for ; Fri, 24 Jul 2020 06:04:34 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E9E4D802CD5D for ; Fri, 24 Jul 2020 10:04:33 +0000 (UTC) X-FDA: 77072534826.05.level08_5a133ef26f46 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id C3F1E18028E64 for ; Fri, 24 Jul 2020 10:04:33 +0000 (UTC) X-Spam-Summary: 1,0,0,989306c3d7d43aa5,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1311:1314:1345:1437:1515:1535:1543:1711:1730:1747:1777:1792:2196:2199:2393:2553:2559:2562:2693:2910:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:4117:4250:4321:4385:4605:5007:6119:6261:6653:7903:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:12986:13161:13221:13229:13894:14093:14096:14181:14394:14721:21080:21444:21451:21627:21939:21990:30054:30056:30070:30090,0,RBL:209.85.214.195:@bytedance.com:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04ygj64z8bfnthe3ysw5xogt9ka54ocfchntgrn5ce3cnr6c9ose55rtr7jygtn.zgmyey1ztz57nf9gh9z89acdacj77txo99z85trz899yjmfs765jyqrcc7zx7q8.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:24,LUA_SUMMARY:none X-HE-Tag: level08_5a133ef26f46 X-Filterd-Recvd-Size: 6622 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Fri, 24 Jul 2020 10:04:33 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id t6so4185914plo.3 for ; Fri, 24 Jul 2020 03:04:32 -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=PDq08UfcHyj6+6rEhdFsfeotLZI6cWTIuXXHWydk68A=; b=y29o8C04GZJUhOPGiSlAir+gPNlvD/rgNnU8oWAbzZbsABkpbazzs6kC4f7iYqWzGb EAWp3sEUh6RVdqR32pc5OPVqaeyrnNNqjICK1rhe8boLDk4FoRIYvzbUwyOraBN2GoIm eTBjD3gw3weUBPKa1I6ck3K2sKs/P3hES8xTGNMVZ8g446CHMLm6F99ADzMKDvg6RZj9 H44vdj2iq3UDmJDWww0pRvITeR3vdlGqeYoxQAypDjmhBYcLAGaD1mn0odB+rS6a/O5u tuFjTCOXOEXQuZw/QBx08qMX2gEnfLxTwd0hLLHwt7sAMqRLnCP1nXVLkxdNcSjdEQjT /64g== 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=PDq08UfcHyj6+6rEhdFsfeotLZI6cWTIuXXHWydk68A=; b=tAqPzA70q4LJEzqgSafTBEqimj30pzNhCt6U1vSGKZVxH3qGKKNh2jFHH5ztgTSdaU m3ULA21sTD0enVD2Ency5zEKiLEN/N1LZUTaZyV+GcNjtpSEYmKGQy/QEuEVfeADB+xp X/4jrCeVzBgAeV7wGzA03tYrjqtcdX7wAH7MQnDjtrOEMdyPWAHayXM6I3+eNC0K6jft B1m7wvPhGSklXLYfZtZK0Q86qzDsNU69JSjezi1jbubhwECfFtXeV13hDghdYW5eHEm5 6CEhnQHjaodIDlVShqyIMn00xUeYDLAOQ5Cn3Bc3sr6vaM7MC04Wxb4WrdWBMMy9CRUs akCQ== X-Gm-Message-State: AOAM532QtoqoExagGec1h4QkvQk7UOZJr7B0cZGZzsJmJqHLsJFpwQFe QZ7HYxSu6eGi6UqF2lUbveR/hw== X-Google-Smtp-Source: ABdhPJx+6H4KwZnetYtN53HnKs879xDwLlf0ryIlWuRVoZYZ5MvYAweCLwibCKAjyRmw1QGwVq4B+Q== X-Received: by 2002:a17:902:6842:: with SMTP id f2mr7810015pln.281.1595585071925; Fri, 24 Jul 2020 03:04:31 -0700 (PDT) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id h6sm5802866pfo.123.2020.07.24.03.04.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2020 03:04:31 -0700 (PDT) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org, mhocko@kernel.org Cc: rientjes@google.com, mgorman@suse.de, walken@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Jianchao Guo Subject: [PATCH v2] mm/hugetlb: add mempolicy check in the reservation routine Date: Fri, 24 Jul 2020 18:03:06 +0800 Message-Id: <20200724100306.33457-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: C3F1E18028E64 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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 --- changelog in v2: 1) Reuse policy_nodemask(). include/linux/mempolicy.h | 1 + mm/hugetlb.c | 19 ++++++++++++++++--- mm/mempolicy.c | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index ea9c15b60a96..6b9640f1c990 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -152,6 +152,7 @@ extern int huge_node(struct vm_area_struct *vma, extern bool init_nodemask_of_mempolicy(nodemask_t *mask); extern bool mempolicy_nodemask_intersects(struct task_struct *tsk, const nodemask_t *mask); +extern nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy); extern unsigned int mempolicy_slab_node(void); extern enum zone_type policy_zone; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 589c330df4db..a753fe8591b4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3463,12 +3463,25 @@ static int __init default_hugepagesz_setup(char *s) } __setup("default_hugepagesz=", default_hugepagesz_setup); -static unsigned int cpuset_mems_nr(unsigned int *array) +static unsigned int allowed_mems_nr(struct hstate *h) { int node; unsigned int nr = 0; + struct mempolicy *mpol = get_task_policy(current); + nodemask_t *mpol_allowed, *mems_allowed, nodemask; + unsigned int *array = h->free_huge_pages_node; + gfp_t gfp_mask = htlb_alloc_mask(h); + + mpol_allowed = policy_nodemask(gfp_mask, mpol); + if (mpol_allowed) { + nodes_and(nodemask, cpuset_current_mems_allowed, + *mpol_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 +3666,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)) { return_unused_surplus_pages(h, delta); goto out; } diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 93fcfc1f2fa2..fce14c3f4f38 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1873,7 +1873,7 @@ static int apply_policy_zone(struct mempolicy *policy, enum zone_type zone) * Return a nodemask representing a mempolicy for filtering nodes for * page allocation */ -static nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) +nodemask_t *policy_nodemask(gfp_t gfp, struct mempolicy *policy) { /* Lower zones don't get a nodemask applied for MPOL_BIND */ if (unlikely(policy->mode == MPOL_BIND) &&