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=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT 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 91024C4361B for ; Thu, 17 Dec 2020 12:16:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1B7F3238E8 for ; Thu, 17 Dec 2020 12:16:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1B7F3238E8 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 927146B0068; Thu, 17 Dec 2020 07:16:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FFCD6B0072; Thu, 17 Dec 2020 07:16:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EC3E6B0073; Thu, 17 Dec 2020 07:16:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0141.hostedemail.com [216.40.44.141]) by kanga.kvack.org (Postfix) with ESMTP id 6976D6B0068 for ; Thu, 17 Dec 2020 07:16:29 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 24520180AD804 for ; Thu, 17 Dec 2020 12:16:29 +0000 (UTC) X-FDA: 77602672098.13.trip74_580283527434 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin13.hostedemail.com (Postfix) with ESMTP id 04DA818140B70 for ; Thu, 17 Dec 2020 12:16:29 +0000 (UTC) X-HE-Tag: trip74_580283527434 X-Filterd-Recvd-Size: 9924 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Thu, 17 Dec 2020 12:16:28 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id e2so20191850pgi.5 for ; Thu, 17 Dec 2020 04:16:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dj578UsWVM1a5aMLDM3EP67706qoyFbxAsYRyDIHJks=; b=LNBtZQUCmZmD3uyrssYOY84KUqX/8clVAGGSgDhRDr9ltbpoTR8ayWN3A8QM1QbpKS V2kQ/gp4whUbxoin7n8uXrRVtFkfd9kZV6KvTxgPegWSSzt8tgiaAmp4wOkc+uO03FnN Erb8uOTkfs1i8tg+yqKBkPSSugXygl5qsdHUEH/NwBKPlhpj4c/6rZM4/CrgMiaBC65+ pVMcDgN3rv1Q5YNDY+fCBa36+DzReATuxDKAnmeIqtvoNkdaICPXjGZkKp4YMobwJuVe Eu22Y3RdN9PsYDQJbLurn69v5Z9nmUO/Red+Zh62679uUmCYEFGe0wkQEvaFX9ASwSrs ZK2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dj578UsWVM1a5aMLDM3EP67706qoyFbxAsYRyDIHJks=; b=FOgGu+aZ6xSl1f/1rs9XCo3cxue8V7sXa1EdhgNLTQKzhCYhKIpByxF11/iG6ZI0aC VhNVhjwYpgqvEEplq/Nl3jeJYnKYdLD+4Xc4i9Ll6OnRnpgPbtxpwWubQJd4VBiXfFX8 y7KhKSZuOXK383Zi/BVWweG7sOSpo4s0+86AdKpCSJxiPXEYXrM9zhPkriXRH6ypkonU voa7XdbwbgPasHkw42aNZv5P82mOmyMzYUgqWp9lcivgYFSWfpU1w74VCFNAvlEi1Csg 1/pi9pTR4+PBPYjwPypVKNbofy7/wNOsaxFv4WRTjLLzFvgdRnPT67ACUoFwfCFUe07v YZZQ== X-Gm-Message-State: AOAM533qSw7bjjsfkrq84l2FDaSuY1vbM5HxD6sLDhrDfzcw7zeIoPwN RO8GtYGqU1ZoP7Rzuyuk5frZHQ== X-Google-Smtp-Source: ABdhPJyyzPXUFGwfxSIpjE0XjjaxadFSQYvbXxt/MqQvVJemQUqWmzotHqkrw09+XE2e6peWAVwVPA== X-Received: by 2002:a63:308:: with SMTP id 8mr21855610pgd.15.1608207387377; Thu, 17 Dec 2020 04:16:27 -0800 (PST) Received: from localhost.localdomain ([139.177.225.237]) by smtp.gmail.com with ESMTPSA id n15sm2775691pgl.31.2020.12.17.04.16.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Dec 2020 04:16:26 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com, david@redhat.com, naoya.horiguchi@nec.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v10 05/11] mm/hugetlb: Allocate the vmemmap pages associated with each HugeTLB page Date: Thu, 17 Dec 2020 20:12:57 +0800 Message-Id: <20201217121303.13386-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201217121303.13386-1-songmuchun@bytedance.com> References: <20201217121303.13386-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: When we free a HugeTLB page to the buddy allocator, we should allocate th= e vmemmap pages associated with it. We can do that in the __free_hugepage() before freeing it to buddy. Signed-off-by: Muchun Song --- include/linux/mm.h | 1 + mm/hugetlb.c | 2 ++ mm/hugetlb_vmemmap.c | 11 ++++++++ mm/hugetlb_vmemmap.h | 5 ++++ mm/sparse-vmemmap.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++= +++++- 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 0ecad1a41190..e7d022b67ee1 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3006,6 +3006,7 @@ static inline void print_vma_addr(char *prefix, uns= igned long rip) #endif =20 void vmemmap_remap_free(unsigned long start, unsigned long size); +void vmemmap_remap_alloc(unsigned long start, unsigned long size); =20 void *sparse_buffer_alloc(unsigned long size); struct page * __populate_section_memmap(unsigned long pfn, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9f35f34d3195..329f473b929e 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1365,6 +1365,8 @@ static void __free_hugepage(struct hstate *h, struc= t page *page) { int i; =20 + alloc_huge_page_vmemmap(h, page); + for (i =3D 0; i < pages_per_huge_page(h); i++) { page[i].flags &=3D ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1 << PG_dirty | diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index c4bbca270453..273816dd95b6 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -183,6 +183,17 @@ static inline unsigned long free_vmemmap_pages_size_= per_hpage(struct hstate *h) return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT; } =20 +void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) +{ + unsigned long vmemmap_addr =3D (unsigned long)head; + + if (!free_vmemmap_pages_per_hpage(h)) + return; + + vmemmap_remap_alloc(vmemmap_addr + RESERVE_VMEMMAP_SIZE, + free_vmemmap_pages_size_per_hpage(h)); +} + void free_huge_page_vmemmap(struct hstate *h, struct page *head) { unsigned long vmemmap_addr =3D (unsigned long)head; diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 01f8637adbe0..b2c8d2f11d48 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -11,6 +11,7 @@ #include =20 #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +void alloc_huge_page_vmemmap(struct hstate *h, struct page *head); void free_huge_page_vmemmap(struct hstate *h, struct page *head); =20 /* @@ -25,6 +26,10 @@ static inline unsigned int free_vmemmap_pages_per_hpag= e(struct hstate *h) return 0; } #else +static inline void alloc_huge_page_vmemmap(struct hstate *h, struct page= *head) +{ +} + static inline void free_huge_page_vmemmap(struct hstate *h, struct page = *head) { } diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 6cf2fdfb81e9..01228065b95d 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -29,6 +29,7 @@ #include #include #include +#include =20 #include #include @@ -40,7 +41,8 @@ * @remap_pte: called for each non-empty PTE (lowest-level) entry. * @reuse_page: the page which is reused for the tail vmemmap pages. * @reuse_addr: the virtual address of the @reuse_page page. - * @vmemmap_pages: the list head of the vmemmap pages that can be freed. + * @vmemmap_pages: the list head of the vmemmap pages that can be freed + * or is mapped from. */ struct vmemmap_remap_walk { void (*remap_pte)(pte_t *pte, unsigned long addr, @@ -50,6 +52,10 @@ struct vmemmap_remap_walk { struct list_head *vmemmap_pages; }; =20 +/* The gfp mask of allocating vmemmap page */ +#define GFP_VMEMMAP_PAGE \ + (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN | __GFP_THISNODE) + static void vmemmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, struct vmemmap_remap_walk *walk) @@ -207,6 +213,71 @@ void vmemmap_remap_free(unsigned long start, unsigne= d long size) free_vmemmap_page_list(&vmemmap_pages); } =20 +static void vmemmap_restore_pte(pte_t *pte, unsigned long addr, + struct vmemmap_remap_walk *walk) +{ + pgprot_t pgprot =3D PAGE_KERNEL; + struct page *page; + void *to; + + BUG_ON(pte_page(*pte) !=3D walk->reuse_page); + + page =3D list_first_entry(walk->vmemmap_pages, struct page, lru); + list_del(&page->lru); + to =3D page_to_virt(page); + copy_page(to, (void *)walk->reuse_addr); + + set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot)); +} + +static void alloc_vmemmap_page_list(struct list_head *list, + unsigned long start, unsigned long end) +{ + unsigned long addr; + int nid =3D page_to_nid((const void *)start); + + for (addr =3D start; addr < end; addr +=3D PAGE_SIZE) { + struct page *page; + +retry: + page =3D alloc_pages_node(nid, GFP_VMEMMAP_PAGE, 0); + if (unlikely(!page)) { + msleep(100); + /* + * We should retry infinitely, because we cannot + * handle allocation failures. Once we allocate + * vmemmap pages successfully, then we can free + * a HugeTLB page. + */ + goto retry; + } + list_add_tail(&page->lru, list); + } +} + +/** + * vmemmap_remap_alloc - remap the vmemmap virtual address range + * [start, start + size) to the page respectively + * which from the @vmemmap_pages + * @start: start address of the vmemmap virtual address range + * @size: size of the vmemmap virtual address range + */ +void vmemmap_remap_alloc(unsigned long start, unsigned long size) +{ + LIST_HEAD(vmemmap_pages); + unsigned long end =3D start + size; + + struct vmemmap_remap_walk walk =3D { + .remap_pte =3D vmemmap_restore_pte, + .vmemmap_pages =3D &vmemmap_pages, + }; + + might_sleep(); + + alloc_vmemmap_page_list(&vmemmap_pages, start, end); + vmemmap_remap_range(start, end, &walk); +} + /* * Allocate a block of memory to be used to back the virtual memory map * or to back the page tables that are used to create the mapping. --=20 2.11.0