Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp831379pxb; Fri, 16 Apr 2021 21:16:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFyZqpDYxltSccjeudz9+YnctkApSFYht1+om7C/2Fnk3BFHFuMFQx+Rr/AiBCmvYvsITV X-Received: by 2002:aa7:d3c6:: with SMTP id o6mr6318840edr.359.1618632967901; Fri, 16 Apr 2021 21:16:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618632967; cv=none; d=google.com; s=arc-20160816; b=or+XQ51qD/t0ASuymOm4e//t+ERK2skmPUCb3P4nm43XgK7a8aLdlVcNSDzBmSoqOE 9tMqKttRC6vETHgfO7GTJi0JDPiA5yynpqtifawvM5q+ZESG6qz2ZgBGtzHJLFcwwE19 eoTJCUJ3/0DJzzxfRxZceNaMfgSmnTr6XBMhQ1l89t+uaCmfHq0LHcZy86xTvlvSiTK2 nOm2yErBt0X0q59S6p6ld/vbgWTxyeQt0+nRMbiEzX72Dq4eaptlyC2INfwzf1fNuCk4 GXMUdxiuW/FRrY5LBIIjT7cbMoVR6MTNiQeHBXAxuplr3x+0UUXDez5W8z/IeOxv92Cu eu/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ylgfgCTB63uVvdacThJs9TcQmae/fWoutKmKR8VmUn4=; b=WnXV1JjPWFqcAQ1LcTV5hCpl+8+NQJMwITlmifogyFGgAlpxKxxPKmrHbg8zw0oPxP shRX1dvxuDrxSpeMjlC78+aww9fag1XMYxWinodNR2dufXyg5OLSclm0hrykzeWqkubH TAa5L6oOqS9ZdWLny9RqMk2mjfU6vwDJJcjZmjgTWJAoO0ohB6LUD5dt71VxuOaAyxQL RPOAg54dl7I/wGXteQYV8L1WiSChHb8t5KYBz7QnzSI0bqgWOuO7qdtBExayzKngTRrP qYw1oFglu8D/Eh5lpmIvpS544nFN/oyJDmLg2uj12y0gtSNWc5a4AnZw6ET12ppDk6jN 4xHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=y4PC6dht; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s20si6396212edu.53.2021.04.16.21.15.43; Fri, 16 Apr 2021 21:16:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=y4PC6dht; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229533AbhDQEPQ (ORCPT + 99 others); Sat, 17 Apr 2021 00:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbhDQEOt (ORCPT ); Sat, 17 Apr 2021 00:14:49 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB53EC061574 for ; Fri, 16 Apr 2021 21:13:53 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id cu16so13044821pjb.4 for ; Fri, 16 Apr 2021 21:13:53 -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=ylgfgCTB63uVvdacThJs9TcQmae/fWoutKmKR8VmUn4=; b=y4PC6dhtLnxM+GheU9KrGq+aZszXhMW31nY1EERC+Q72IGwebhRGazT63ogIYWDj9U bDAh3OQw+ccOX2q0qvSG2KEEMjFgOT4A45hkFRtshiPAth22yLZ2MF3dhsUkV5YBqEvl 4dOGTAwsLEvtH1HBM5TYHtqF7EqdsZ3Ek8cIgdkIsk0M0SGrv5+rYesR4Ol/RBZGkTPr bqq+nda5GpPH4KS5Belg9AP4J6t5ACS7h9yC4GhWf+1yzPr8RMMLCdWP+JznF/FtmnFo Oef0zZVhvFSkR9DoOF6bQ9aQykFqqsHWSvCrnJ/Cj/lnFL/OABVuwCXJ5q3/sotSBdg4 p8OA== 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=ylgfgCTB63uVvdacThJs9TcQmae/fWoutKmKR8VmUn4=; b=aQjjmfDPFpHSFir5H54/2BD3Jady5QjLjz47yz9ltTyZznmMNkfGxpFSPluUtiRl+s dqDm3COZ39Zy8dGizxdz/2YI5L5nY9pex9aOncjYm/zz09/pegCxDM3tryXMqKkjtw8l YYbq0pZiZnT75mW1ShxrQa5eYFHWzR0GsLBXKx/PJ9987JUiXzeB3KzUbZrl7J1LIIkO sRlYjwJHpsq3xE4dLpSyAJERc0UVPGsUCyPkF0/ozDSGWR5ia8espext628/7FGXmZ++ +2tJe8aWEKwE2siu8+iYANlmfkvr+ImKwjqaoCfrBG5DrQaU7Pg/hrArYeQ9CEKrLbcz r0Aw== X-Gm-Message-State: AOAM533kHvTZLyK3oL9MzTXAPgkJj8KGQva/0y7Gi65PJfKuSmZKJR8X fpR6DZ32libwG/ZStXiszwVENJzhEMNGeiEbjIyXNQ== X-Received: by 2002:a17:90a:a895:: with SMTP id h21mr13280750pjq.13.1618632833248; Fri, 16 Apr 2021 21:13:53 -0700 (PDT) MIME-Version: 1.0 References: <20210415084005.25049-1-songmuchun@bytedance.com> <20210415084005.25049-6-songmuchun@bytedance.com> <33a4f2fe-72c2-a3a1-9205-461ddde9b162@oracle.com> In-Reply-To: <33a4f2fe-72c2-a3a1-9205-461ddde9b162@oracle.com> From: Muchun Song Date: Sat, 17 Apr 2021 12:13:17 +0800 Message-ID: Subject: Re: [External] Re: [PATCH v20 5/9] mm: hugetlb: defer freeing of HugeTLB pages To: Mike Kravetz Cc: Jonathan Corbet , Thomas Gleixner , Ingo Molnar , bp@alien8.de, X86 ML , hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, Peter Zijlstra , Alexander Viro , Andrew Morton , paulmck@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 , =?UTF-8?B?SE9SSUdVQ0hJIE5BT1lBKOWggOWPoyDnm7TkuZ8p?= , Joao Martins , Xiongchun duan , fam.zheng@bytedance.com, linux-doc@vger.kernel.org, LKML , Linux Memory Management List , linux-fsdevel Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Apr 17, 2021 at 7:56 AM Mike Kravetz wrote: > > On 4/15/21 1:40 AM, Muchun Song wrote: > > In the subsequent patch, we should allocate the vmemmap pages when > > freeing a HugeTLB page. But update_and_free_page() can be called > > under any context, so we cannot use GFP_KERNEL to allocate vmemmap > > pages. However, we can defer the actual freeing in a kworker to > > prevent from using GFP_ATOMIC to allocate the vmemmap pages. > > Thanks! I knew we would need to introduce a kworker for this when I > removed the kworker previously used in free_huge_page. Yeah, but another choice is using GFP_ATOMIC to allocate vmemmap pages when we are in an atomic context. If not atomic context, just use GFP_KERNEL. In this case, we can drop kworker. > > > The __update_and_free_page() is where the call to allocate vmemmmap > > pages will be inserted. > > This patch adds the functionality required for __update_and_free_page > to potentially sleep and fail. More questions will come up in the > subsequent patch when code must deal with the failures. Right. More questions are welcome. > > > > > Signed-off-by: Muchun Song > > --- > > mm/hugetlb.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++---- > > mm/hugetlb_vmemmap.c | 12 --------- > > mm/hugetlb_vmemmap.h | 17 ++++++++++++ > > 3 files changed, 85 insertions(+), 17 deletions(-) > > > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > index 923d05e2806b..eeb8f5480170 100644 > > --- a/mm/hugetlb.c > > +++ b/mm/hugetlb.c > > @@ -1376,7 +1376,7 @@ static void remove_hugetlb_page(struct hstate *h, struct page *page, > > h->nr_huge_pages_node[nid]--; > > } > > > > -static void update_and_free_page(struct hstate *h, struct page *page) > > +static void __update_and_free_page(struct hstate *h, struct page *page) > > { > > int i; > > struct page *subpage = page; > > @@ -1399,12 +1399,73 @@ static void update_and_free_page(struct hstate *h, struct page *page) > > } > > } > > > > +/* > > + * As update_and_free_page() can be called under any context, so we cannot > > + * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the > > + * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate > > + * the vmemmap pages. > > + * > > + * free_hpage_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 free_hpage_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_freelist); > > + > > +static void free_hpage_workfn(struct work_struct *work) > > +{ > > + struct llist_node *node; > > + > > + node = llist_del_all(&hpage_freelist); > > + > > + while (node) { > > + struct page *page; > > + struct hstate *h; > > + > > + page = container_of((struct address_space **)node, > > + struct page, mapping); > > + node = node->next; > > + page->mapping = NULL; > > + h = page_hstate(page); > > The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate is going to > trigger because a previous call to remove_hugetlb_page() will > set_compound_page_dtor(page, NULL_COMPOUND_DTOR) Sorry, I did not realise that. Thanks for your reminder. > > Note how h(hstate) is grabbed before calling update_and_free_page in > existing code. > > We could potentially drop the !PageHuge(page) in page_hstate. Or, > perhaps just use 'size_to_hstate(page_size(page))' in free_hpage_workfn. I prefer not to change the behavior of page_hstate(). So I should use 'size_to_hstate(page_size(page))' directly. Thanks Mike. > -- > Mike Kravetz