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=-13.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham 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 069E8C433DB for ; Sat, 6 Feb 2021 08:02:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4626664E51 for ; Sat, 6 Feb 2021 08:02:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4626664E51 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 823846B0005; Sat, 6 Feb 2021 03:02:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D5C56B006C; Sat, 6 Feb 2021 03:02:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C3D76B006E; Sat, 6 Feb 2021 03:02:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0036.hostedemail.com [216.40.44.36]) by kanga.kvack.org (Postfix) with ESMTP id 57BC56B0005 for ; Sat, 6 Feb 2021 03:02:38 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 214B73634 for ; Sat, 6 Feb 2021 08:02:38 +0000 (UTC) X-FDA: 77787101196.14.clam17_240f594275eb Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 006F71822987B for ; Sat, 6 Feb 2021 08:02:37 +0000 (UTC) X-HE-Tag: clam17_240f594275eb X-Filterd-Recvd-Size: 9588 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf44.hostedemail.com (Postfix) with ESMTP for ; Sat, 6 Feb 2021 08:02:37 +0000 (UTC) Received: by mail-pf1-f181.google.com with SMTP id w18so5899686pfu.9 for ; Sat, 06 Feb 2021 00:02:36 -0800 (PST) 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=+4ngpioykdaswjtQ50G9wJ1z4lzpWogRACPFYV04cNw=; b=LBNEqzXrKkilK+S+ibcAuad7DFuUukoIsNHlF4p6chdboba20kOYSIu5QwpWxQH2nx OEZ31k5B9EyIvppcoINkh65RGhM7KBuvGESxDNDPNcDy8qRL1GZSlNF3RNZxFN0AnqUf jzbSD2vCfbkyhYFr5nkOMwKC0n0demDWQv75PFSvstDHGmhOPtrCQS+pEBQgHzwMMIDD iEWt2VE5hj7kFYYsIZ8F/GmiFVudtGC6EsUVV6VXChW3/RROvXmn3nLPPl5uCcAFABgh Tpkct590AMCu9aD10Gqv5FCbNGgWxb8naqpM8gwbWdvZXQKbyJaYoWvFgocaCAfo0mQz HdTg== 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=+4ngpioykdaswjtQ50G9wJ1z4lzpWogRACPFYV04cNw=; b=R1lmJIImrSWb/iqzWZIK4EzT22Tgv45YYUyp3Wr67Vb7gCURSZF/xPQvVAWrQXeyqd pkUGC0YIk+rcjpQGHwR3eNNK1afiYqae3/k054huzp6hROwPcCkwQwbOaoevLYY4l++Q pr1Xo4IhdLPDdsoslWWQAYr8ANifQTNNWq4equjMyEhudAnv7oWVZadyW7hm7MaChOzf +Nmt/KrqnqwaPUtD09IvuBIf4UvUo+qbwgE31rhQz7MPgixYpLXzV28kpcfUhtQVZoYD 0RiMOQFhbcjQMl1VQHXm8DmsXd+0CSYZly/RD3MZ3wQhrl7tD+Cu+jjCTYITRAJVdMwj vYBQ== X-Gm-Message-State: AOAM531hqn72JBjlWRFysu76pxrh1yeU/Ru0FfjhSU04Rg5ZSH55eUQD Q7dRb4fw89uqzI5b+YAOFGECjleEHAAhkOxvRa/4Mg== X-Google-Smtp-Source: ABdhPJzVyVF5yNIljrr7V2pJ5nxH8aJhsD1UM3EyuHS7WsAUOEosBeeTy2sHnzGqAIU6QNuHnp8ZM3ux8Bx7YWuBCDE= X-Received: by 2002:aa7:9790:0:b029:1d8:263e:cc9b with SMTP id o16-20020aa797900000b02901d8263ecc9bmr6239477pfp.2.1612598555875; Sat, 06 Feb 2021 00:02:35 -0800 (PST) MIME-Version: 1.0 References: <20210204035043.36609-1-songmuchun@bytedance.com> <20210204035043.36609-5-songmuchun@bytedance.com> <20210205115351.GA16428@linux> In-Reply-To: <20210205115351.GA16428@linux> From: Muchun Song Date: Sat, 6 Feb 2021 16:01:58 +0800 Message-ID: Subject: Re: [External] Re: [PATCH v14 4/8] mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page To: Oscar Salvador Cc: Jonathan Corbet , Mike Kravetz , Thomas Gleixner , mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, Peter Zijlstra , viro@zeniv.linux.org.uk, Andrew Morton , paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, Randy Dunlap , oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, Mina Almasry , David Rientjes , Matthew Wilcox , Michal Hocko , "Song Bao Hua (Barry Song)" , David Hildenbrand , =?UTF-8?B?SE9SSUdVQ0hJIE5BT1lBKOWggOWPoyDnm7TkuZ8p?= , Xiongchun duan , linux-doc@vger.kernel.org, LKML , Linux Memory Management List , linux-fsdevel Content-Type: text/plain; charset="UTF-8" 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, Feb 5, 2021 at 7:54 PM Oscar Salvador wrote: > > On Thu, Feb 04, 2021 at 11:50:39AM +0800, Muchun Song wrote: > > When we free a HugeTLB page to the buddy allocator, we should allocate the > > vmemmap pages associated with it. But we may cannot allocate vmemmap pages > > when the system is under memory pressure, in this case, we just refuse to > > free the HugeTLB page instead of looping forever trying to allocate the > > pages. > > > > Signed-off-by: Muchun Song > > [...] > > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > index 4cfca27c6d32..5518283aa667 100644 > > --- a/mm/hugetlb.c > > +++ b/mm/hugetlb.c > > @@ -1397,16 +1397,26 @@ static void __free_huge_page(struct page *page) > > h->resv_huge_pages++; > > > > if (HPageTemporary(page)) { > > - list_del(&page->lru); > > ClearHPageTemporary(page); > > + > > + if (alloc_huge_page_vmemmap(h, page, GFP_ATOMIC)) { > > + h->surplus_huge_pages++; > > + h->surplus_huge_pages_node[nid]++; > > + goto enqueue; > > + } > > + list_del(&page->lru); > > update_and_free_page(h, page); > > } else if (h->surplus_huge_pages_node[nid]) { > > + if (alloc_huge_page_vmemmap(h, page, GFP_ATOMIC)) > > + goto enqueue; > > + > > /* remove the page from active list */ > > list_del(&page->lru); > > update_and_free_page(h, page); > > h->surplus_huge_pages--; > > h->surplus_huge_pages_node[nid]--; > > } else { > > +enqueue: > > arch_clear_hugepage_flags(page); > > enqueue_huge_page(h, page); > > Ok, we just keep them in the pool in case we fail to allocate. > > > > diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c > > index ddd872ab6180..0bd6b8d7282d 100644 > > --- a/mm/hugetlb_vmemmap.c > > +++ b/mm/hugetlb_vmemmap.c > > @@ -169,6 +169,8 @@ > > * (last) level. So this type of HugeTLB page can be optimized only when its > > * size of the struct page structs is greater than 2 pages. > > [...] > > > +int alloc_huge_page_vmemmap(struct hstate *h, struct page *head, gfp_t gfp_mask) > > +{ > > + int ret; > > + unsigned long vmemmap_addr = (unsigned long)head; > > + unsigned long vmemmap_end, vmemmap_reuse; > > + > > + if (!free_vmemmap_pages_per_hpage(h)) > > + return 0; > > + > > + vmemmap_addr += RESERVE_VMEMMAP_SIZE; > > + vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h); > > + vmemmap_reuse = vmemmap_addr - PAGE_SIZE; > > + > > + /* > > + * The pages which the vmemmap virtual address range [@vmemmap_addr, > > + * @vmemmap_end) are mapped to are freed to the buddy allocator, and > > + * the range is mapped to the page which @vmemmap_reuse is mapped to. > > + * When a HugeTLB page is freed to the buddy allocator, previously > > + * discarded vmemmap pages must be allocated and remapping. > > + */ > > + ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse, > > + gfp_mask | __GFP_NOWARN | __GFP_THISNODE); > > Why don't you set all the GFP flags here? Originally, I wanted to let the caller know the GFP flag which they used. But setting all the GFP flags here also makes sense to me. And we can remove the @gfp_mask parameter of the alloc_huge_page_vmemmap. It is simple. > > vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse, GFP_ATOMIC| > __GFP_NOWARN | __GFP_THISNODE) ? I will use this. > > > diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c > > index 50c1dc00b686..277eb43aebd5 100644 > > --- a/mm/sparse-vmemmap.c > > +++ b/mm/sparse-vmemmap.c > > [...] > > > +static int alloc_vmemmap_page_list(unsigned long start, unsigned long end, > > + gfp_t gfp_mask, struct list_head *list) > > I think it would make more sense for this function to get the nid and the > nr_pages to allocate directly. Just like alloc_pages(), right? If so, make sense to me. > > > +{ > > + unsigned long addr; > > + int nid = page_to_nid((const void *)start); > > Uh, that void is a bit ugly. page_to_nid(struct page *)start). > Do not need the const either. OK. Will do. Thanks. > > > + struct page *page, *next; > > + > > + for (addr = start; addr < end; addr += PAGE_SIZE) { > > + page = alloc_pages_node(nid, gfp_mask, 0); > > + if (!page) > > + goto out; > > + list_add_tail(&page->lru, list); > > + } > > and replace this by while(--nr_pages) etc. OK. Will do. > > I did not really go in depth, but looks good to me, and much more simply > overall. Yeah. The series only has 8 patches now. It is simpler. > > The only thing I am not sure about is the use of GFP_ATOMIC. > It has been raised before than when we are close to OOM, the user might want > to try to free up some memory by dissolving free_huge_pages, and so we might > want to dip in the reserves. > > Given the fact that we are prepared to fail, and that we do not retry, I would > rather use GFP_KERNEL than to have X pages atomically allocated and then realize > we need to drop them on the ground because we cannot go further at some point. > I think those reserves would be better off used by someone else in that > situation. > > But this is just my thoughs, and given the fact that there seems to be a consensus > of susing GFP_ATOMIC. > > -- > Oscar Salvador > SUSE L3