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 X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,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 5E8E5C433E3 for ; Fri, 24 Jul 2020 13:57:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 070F820656 for ; Fri, 24 Jul 2020 13:57:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="YgmoZBvc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 070F820656 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 70D666B002A; Fri, 24 Jul 2020 09:57:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BCFE6B002B; Fri, 24 Jul 2020 09:57:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AC176B002C; Fri, 24 Jul 2020 09:57:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0226.hostedemail.com [216.40.44.226]) by kanga.kvack.org (Postfix) with ESMTP id 460D76B002A for ; Fri, 24 Jul 2020 09:57:08 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D09798248047 for ; Fri, 24 Jul 2020 13:57:07 +0000 (UTC) X-FDA: 77073120894.07.pies92_600714426f47 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin07.hostedemail.com (Postfix) with ESMTP id 932F018211CC9 for ; Fri, 24 Jul 2020 13:57:07 +0000 (UTC) X-HE-Tag: pies92_600714426f47 X-Filterd-Recvd-Size: 6555 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Fri, 24 Jul 2020 13:57:06 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id x8so4450339plm.10 for ; Fri, 24 Jul 2020 06:57:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WjjQN8ks2b2RtZrHKrhZ4oEn8Kn+yd36Wb2kzIQgMkY=; b=YgmoZBvckAEgAfwuMocjyBcu/IyVoL401Htncdj0K2+rx5zBGYGcvyzaGczG8krg/0 p4DtZvSOqMxV8FTKsCnG26DeO7Q1aaVr9TOQxoOPsbwFfovT7VaR5i+8QqqMaf+24iZL PFQ2cYFNJGML0CJYc8jjo2HxQvVz68FfAXpjmEarg01w4ugluOZOVslKxbxrZIuPxtJg qScHsb3HJZn1tLjuudk9yzaROEHAHMx3CtJih4lCmRngUKEMfbmYhipwBHH3TxC1SYhJ nChcLFEoixyOiSTzxicNB16E3OaWM0FIbspW6HxufhSiaZvgNccG8yRma2I9142ZFH7u wkWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WjjQN8ks2b2RtZrHKrhZ4oEn8Kn+yd36Wb2kzIQgMkY=; b=FtNtMCKo3i+Ip8rOJYBrx6gQXCM0vU5CFRAm5xAg/2GMjkfS5AuMO3ISJYYUEkYBj8 ZP/J69q12xd3HEOr0Y8tDQ726GXTUxdJNj0FxsN38/H0mjOGi64TL/aVvKEIYt9mEh5J y+VlrGiJFyomCLormgjSLxnbbn6LciBCEzyI+RPLrJC3OWaj66eh8iqqoK6jMaHUyu1a DUdr2PeAm/1nx/w6XIfjJ0A4Ue6U/vSKkSaXCH8tQxi8+Z8gKbASJc97mnk+6h6t1ghF HGdwld0qfIM/T0/NUf40Jcqw4YndlEu+UQlP0CJR5oHAeERwf59p2dDg04gUvW7wsrqj UILg== X-Gm-Message-State: AOAM531EnRRYmtFoSHWYQJSoR8U884pXHhogpJCjS5r78N+MWpPZm5mU fTYXuAgwmbJngyPpJCRBp2Xqhhib+rUkqyZWRjTBIA== X-Google-Smtp-Source: ABdhPJwKzbMeNgQpLCc7PUfVVarPU4j6FpeRmmFMAfQwxGwOv0V5jGVpgNu2UtLa274ITps4QrCYk27NbvAsvN5WC7o= X-Received: by 2002:a17:90b:4005:: with SMTP id ie5mr5615501pjb.147.1595599025576; Fri, 24 Jul 2020 06:57:05 -0700 (PDT) MIME-Version: 1.0 References: <20200724100306.33457-1-songmuchun@bytedance.com> <20200724113415.GG4061@dhcp22.suse.cz> In-Reply-To: <20200724113415.GG4061@dhcp22.suse.cz> From: Muchun Song Date: Fri, 24 Jul 2020 21:56:29 +0800 Message-ID: Subject: Re: [Phishing Risk] [External] Re: [PATCH v2] mm/hugetlb: add mempolicy check in the reservation routine To: Michal Hocko Cc: mike.kravetz@oracle.com, Andrew Morton , David Rientjes , mgorman@suse.de, walken@google.com, Linux Memory Management List , LKML , Jianchao Guo Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 932F018211CC9 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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, Jul 24, 2020 at 7:34 PM Michal Hocko wrote: > > 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. An unsigned long can satisfy 64 nodes. So I think that nodemask is using little stack memory. Right? > -- > Michal Hocko > SUSE Labs -- Yours, Muchun