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.8 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 1F6E5C4361B for ; Thu, 17 Dec 2020 03:20:02 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 72FC123731 for ; Thu, 17 Dec 2020 03:20:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 72FC123731 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 98BB76B0036; Wed, 16 Dec 2020 22:20:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9157B6B005D; Wed, 16 Dec 2020 22:20:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 804A36B0068; Wed, 16 Dec 2020 22:20:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0247.hostedemail.com [216.40.44.247]) by kanga.kvack.org (Postfix) with ESMTP id 6575F6B0036 for ; Wed, 16 Dec 2020 22:20:00 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 264D4181AEF07 for ; Thu, 17 Dec 2020 03:20:00 +0000 (UTC) X-FDA: 77601320160.04.teeth14_440318927431 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id E2B118008B28 for ; Thu, 17 Dec 2020 03:19:59 +0000 (UTC) X-HE-Tag: teeth14_440318927431 X-Filterd-Recvd-Size: 9350 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 03:19:59 +0000 (UTC) Received: by mail-pg1-f182.google.com with SMTP id n7so19394314pgg.2 for ; Wed, 16 Dec 2020 19:19:58 -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=gbs7EgWZM52eCkELY86Hx3Zy59sL0MGXRI9ZDzydt2c=; b=DjbHeqrJr1G09b1tq/4/bGl9ph/QpMnxYiVt/4LUikyVkDynO0Z+2OztDUd8jKixY+ Kjj+U6omCQFXxlfIyKkqy6hd/h2uJp6nU7+Xn2BXnlFSvE0bhqIJ5ojY52b+JZ4Zm0DU N/u2hmCHQ/k6VYjzZC1eQG3H4ePsUALhLZ6n+rian726xsArim630rX4iJfPntTj0ZXL btZaJndxj/OLEl2+0Dn18H/gvmVq0GOsMzzqv2QaTRC8OhJSG1xZ4yXBr61XkEo/2ZvP 4r+vJdF1oYQO7WiPkVdRuynOFCHEk/67kIobjm4rfscCONh3xpui46H5urXVcKEi4qhj 8XEg== 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=gbs7EgWZM52eCkELY86Hx3Zy59sL0MGXRI9ZDzydt2c=; b=ZJKUxOlgor7CZ1p7sXirS+Kx5i1QfJQtSgPV4ir3jqjTg9OZwjdxkoeYoB4mjlee9f EHfGRVBDM2uyVaQM2VsL0MCjvYk4AbFyRE86bBB7tCky5wAWUPlYfc48hPwB8/HukWaE 6hvKUBz0GsQ7R9//sWLkcHikBytXOygP2PxWBguz9tPMC1EciLRkocPvKSzdfBtR1jsv sROMsqh5IRZZae4aqUw1Kd8dQrWRIi4nc1M0pDb9yMGzlMI2ZO77ZM0Axi8f0M3D9xMY HYLQ+q0Wd1+MpmdRvd1RCiHwgkK6fEzAXuSGHwpUCbkBIv1idya9NyBz9BnHfDdo/IhI +mIA== X-Gm-Message-State: AOAM530qiIEqCjoHP+k2on9n5w+rQe/Qs0delEc4ZCzMUFRUrMi5DUgH McfojW+XT4uKQfR/Qd9o5Zrt7FUgJgOHt0r+B6FZxg== X-Google-Smtp-Source: ABdhPJy/YaJPtY/hqJbcS/FGGKfDszH2srApx17B+zdeojgzKxeVpXHtauVm3SP3eS9HwXVXuS1t+NctH625G9fRFWY= X-Received: by 2002:a63:50a:: with SMTP id 10mr7737721pgf.273.1608175197760; Wed, 16 Dec 2020 19:19:57 -0800 (PST) MIME-Version: 1.0 References: <20201213154534.54826-1-songmuchun@bytedance.com> <20201213154534.54826-5-songmuchun@bytedance.com> <6b555fb8-6fd5-049a-49c1-4dc8a3f66766@oracle.com> In-Reply-To: <6b555fb8-6fd5-049a-49c1-4dc8a3f66766@oracle.com> From: Muchun Song Date: Thu, 17 Dec 2020 11:19:21 +0800 Message-ID: Subject: Re: [External] Re: [PATCH v9 04/11] mm/hugetlb: Defer freeing of HugeTLB pages To: Mike Kravetz Cc: Jonathan Corbet , 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 , Oscar Salvador , Michal Hocko , "Song Bao Hua (Barry Song)" , David Hildenbrand , 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 Thu, Dec 17, 2020 at 7:48 AM Mike Kravetz wrote: > > On 12/13/20 7:45 AM, Muchun Song wrote: > > In the subsequent patch, we will allocate the vmemmap pages when free > > HugeTLB pages. But update_and_free_page() is called from a non-task > > context(and hold hugetlb_lock), so we can defer the actual freeing in > > a workqueue to prevent use GFP_ATOMIC to allocate the vmemmap pages. > > > > Signed-off-by: Muchun Song > > It is unfortunate we need to add this complexitty, but I can not think > of another way. One small comment (no required change) below. > > Reviewed-by: Mike Kravetz Thank you. > > > --- > > mm/hugetlb.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++---- > > mm/hugetlb_vmemmap.c | 12 -------- > > mm/hugetlb_vmemmap.h | 17 ++++++++++++ > > 3 files changed, 88 insertions(+), 18 deletions(-) > > > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > index 140135fc8113..0ff9b90e524f 100644 > > --- a/mm/hugetlb.c > > +++ b/mm/hugetlb.c > > @@ -1292,15 +1292,76 @@ static inline void destroy_compound_gigantic_page(struct page *page, > > unsigned int order) { } > > #endif > > > > -static void update_and_free_page(struct hstate *h, struct page *page) > > +static void __free_hugepage(struct hstate *h, struct page *page); > > + > > +/* > > + * As update_and_free_page() is be called from a non-task context(and hold > > + * hugetlb_lock), we can defer the actual freeing in a workqueue to prevent > > + * use GFP_ATOMIC to allocate a lot of vmemmap pages. > > + * > > + * update_hpage_vmemmap_workfn() locklessly retrieves the linked list of > > + * pages to be freed and frees them one-by-one. As the page->mapping pointer > > + * is going to be cleared in update_hpage_vmemmap_workfn() anyway, it is > > + * reused as the llist_node structure of a lockless linked list of huge > > + * pages to be freed. > > + */ > > +static LLIST_HEAD(hpage_update_freelist); > > + > > +static void update_hpage_vmemmap_workfn(struct work_struct *work) > > { > > - int i; > > + struct llist_node *node; > > + struct page *page; > > + > > + node = llist_del_all(&hpage_update_freelist); > > > > + while (node) { > > + page = container_of((struct address_space **)node, > > + struct page, mapping); > > + node = node->next; > > + page->mapping = NULL; > > + __free_hugepage(page_hstate(page), page); > > + > > + cond_resched(); > > + } > > +} > > +static DECLARE_WORK(hpage_update_work, update_hpage_vmemmap_workfn); > > + > > +static inline void __update_and_free_page(struct hstate *h, struct page *page) > > +{ > > + /* No need to allocate vmemmap pages */ > > + if (!free_vmemmap_pages_per_hpage(h)) { > > + __free_hugepage(h, page); > > + return; > > + } > > + > > + /* > > + * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap > > + * pages. > > + * > > + * Only call schedule_work() if hpage_update_freelist is previously > > + * empty. Otherwise, schedule_work() had been called but the workfn > > + * hasn't retrieved the list yet. > > + */ > > + if (llist_add((struct llist_node *)&page->mapping, > > + &hpage_update_freelist)) > > + schedule_work(&hpage_update_work); > > +} > > + > > +static void update_and_free_page(struct hstate *h, struct page *page) > > +{ > > if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) > > return; > > > > h->nr_huge_pages--; > > h->nr_huge_pages_node[page_to_nid(page)]--; > > + > > + __update_and_free_page(h, page); > > +} > > + > > +static void __free_hugepage(struct hstate *h, struct page *page) > > +{ > > + int i; > > + > > Can we add a comment here saying that this is where the call to allocate > vmemmmap pages will be inserted in a later patch. Such a comment would > help a bit to understand the restructuring of the code. OK. Will do. Thanks. > > -- > Mike Kravetz > > > for (i = 0; i < pages_per_huge_page(h); i++) { > > page[i].flags &= ~(1 << PG_locked | 1 << PG_error | > > 1 << PG_referenced | 1 << PG_dirty | > > @@ -1313,13 +1374,17 @@ static void update_and_free_page(struct hstate *h, struct page *page) > > set_page_refcounted(page); > > if (hstate_is_gigantic(h)) { > > /* > > - * Temporarily drop the hugetlb_lock, because > > - * we might block in free_gigantic_page(). > > + * Temporarily drop the hugetlb_lock only when this type of > > + * HugeTLB page does not support vmemmap optimization (which > > + * contex do not hold the hugetlb_lock), because we might block > > + * in free_gigantic_page(). > > */ > > - spin_unlock(&hugetlb_lock); > > + if (!free_vmemmap_pages_per_hpage(h)) > > + spin_unlock(&hugetlb_lock); > > destroy_compound_gigantic_page(page, huge_page_order(h)); > > free_gigantic_page(page, huge_page_order(h)); > > - spin_lock(&hugetlb_lock); > > + if (!free_vmemmap_pages_per_hpage(h)) > > + spin_lock(&hugetlb_lock); > > } else { > > __free_pages(page, huge_page_order(h)); > > } -- Yours, Muchun