Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=BAYES_00,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DCFAC433E3 for ; Fri, 24 Jul 2020 11:34:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2CEBE206E3 for ; Fri, 24 Jul 2020 11:34:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2CEBE206E3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6A5718D0023; Fri, 24 Jul 2020 07:34:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 655788D0013; Fri, 24 Jul 2020 07:34:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56C798D0023; Fri, 24 Jul 2020 07:34:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0156.hostedemail.com [216.40.44.156]) by kanga.kvack.org (Postfix) with ESMTP id 412FA8D0013 for ; Fri, 24 Jul 2020 07:34:20 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DC07918031066 for ; Fri, 24 Jul 2020 11:34:19 +0000 (UTC) X-FDA: 77072761038.16.touch50_0217db626f47 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin16.hostedemail.com (Postfix) with ESMTP id AF2C21004ADEB for ; Fri, 24 Jul 2020 11:34:19 +0000 (UTC) X-HE-Tag: touch50_0217db626f47 X-Filterd-Recvd-Size: 5513 Received: from mail-ed1-f68.google.com (mail-ed1-f68.google.com [209.85.208.68]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Fri, 24 Jul 2020 11:34:19 +0000 (UTC) Received: by mail-ed1-f68.google.com with SMTP id bm28so6823007edb.2 for ; Fri, 24 Jul 2020 04:34:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=M110HA4nMMKBLYGy/DI8jTdSDD0jpS2wTCc48wWnBmY=; b=gz7Ja4ibY55Uz9QDRXTWea6Y5G3KhZos965OIfUI9Fnvk9/JITeBV3TxQgwfP3ceNL jEAg0uFc7IvpE/mBkwPYaCnr42afiqQRCKRpx7R6ABfkOtSx90kQy1QNSsEg1HWGW2wT F+2Xy6QIEJ9qdHskJ0jqqoqorUjyrmsvcJ5K9L2ONv2iK4uhJfw1BG52oyMk3wMeL1MN MJubfYIvd7ZcuuiO5EwpwTQOFMyZafKMy+/06PpYGOp/uAZ62YQW0WitwX3H+ioik5mp 2P+X6DuANi6aZyRm+kSZBmvGJ2efCHdrnsb0DjN+TTwCRxMr2gzAGfoYi3sMMKRIflT+ TXnA== X-Gm-Message-State: AOAM532hxqUeKVYRczSsbSkOiCQ0UyI60YHbPlDYM81rQWxHmJfq2Xs7 tMzvnSehjNvUjKSuVCkJ9Do= X-Google-Smtp-Source: ABdhPJz4nnh7QTvkeAbcVkZTca3Hx6YW1ikFfwsIPl+KbdGmUHOgsbm6KIfTaIaKeQqwCU6W2POzhQ== X-Received: by 2002:a05:6402:2d7:: with SMTP id b23mr8635662edx.145.1595590458024; Fri, 24 Jul 2020 04:34:18 -0700 (PDT) Received: from localhost (ip-37-188-169-187.eurotel.cz. [37.188.169.187]) by smtp.gmail.com with ESMTPSA id q21sm496212ejr.75.2020.07.24.04.34.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2020 04:34:17 -0700 (PDT) Date: Fri, 24 Jul 2020 13:34:15 +0200 From: Michal Hocko To: Muchun Song Cc: mike.kravetz@oracle.com, akpm@linux-foundation.org, rientjes@google.com, mgorman@suse.de, walken@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jianchao Guo Subject: Re: [PATCH v2] mm/hugetlb: add mempolicy check in the reservation routine Message-ID: <20200724113415.GG4061@dhcp22.suse.cz> References: <20200724100306.33457-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200724100306.33457-1-songmuchun@bytedance.com> X-Rspamd-Queue-Id: AF2C21004ADEB X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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 Fri 24-07-20 18:03:06, Muchun Song wrote: > 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; > + } I believe you can simplify this and use a similar pattern as the page allocator. Something like for_each_node_mask(node, mpol_allowed) { if (node_isset(node, &cpuset_current_mems_allowed)) nr += array[node]; } There shouldn't be any need to allocate a potentially large nodemask on the stack. -- Michal Hocko SUSE Labs