Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1393955pxb; Sun, 17 Jan 2021 07:19:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJzFUyDy0d5eCqT4Rtu+pXnItp3dEFqRv4RZmPMniBETC0gLakUmG+XhI3KjJzoUabw6NKmn X-Received: by 2002:a50:d491:: with SMTP id s17mr16147141edi.169.1610896744094; Sun, 17 Jan 2021 07:19:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610896744; cv=none; d=google.com; s=arc-20160816; b=0ynDmZduYg/lvSURRWFM59zErsUzRc9G0rztZuO/xOCYEdJIVhWt1DZ7v7YtAWZh1F kfa2ERkQTHoZockjfQKSETAPpqCoS1j3INnmoeAg6jyA/xuP+qIdkOIsXgvZ0Pfwogj2 vJl4eEcUWo8by+BowMKIOmYFdPYXHbagsKIOIwMpLUSdcU8yV5k/upr1WNa7HNjhFmgl HecjvWLa7Ng3AD+5x6Qb5sEtMb7oZiQ8OonQ+MYUEbmUppmBUgO8wK4T4bOjCcenGzzV z6fQIoaEE9jdvg6rnDCQ7Rj+rl8zoHdVPiQbZkyJ/FQEsrs6sM9uAlkP5Rpbw0bA5O35 8ugg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eriJkkL7fLLxATd22osz6+JQx60d8cBmS+HC2zuMsVY=; b=bf9y/LEFRb8iTAhpe7XyxKB8i4OfqI+123G2UMvWM1CDt0v4KZBoTc/PJUP/XDK2Q1 4I5o198rxsOwoiPG9TPt8Qw9a51MOGoWwZafOXpYm6EFmLj8enTSwpWk0UHsFqQY2SdJ VN61PB05yVz6Q4D6eegEb49mgbFDGjcyPPH97QgOCor1GHSm3biXLlXVjQD3bUg9VJJx 7vKs8VOZLkbWdNhBZuY5Gl6wY0Fslz1TOMxCHvBG2XjU8YSMiKHDbLFW8O3mGlg9pjGL sX+gj5OPqhBalIh3eNzvScjSU+sN5HPkuwIeq3yd4imlROzpCY90TcJ/MxQ5/V7GTIPE tt6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=momL6MFk; 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 jp25si343761ejb.519.2021.01.17.07.18.40; Sun, 17 Jan 2021 07:19:04 -0800 (PST) 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=momL6MFk; 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 S1729414AbhAQPQO (ORCPT + 99 others); Sun, 17 Jan 2021 10:16:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729389AbhAQPPG (ORCPT ); Sun, 17 Jan 2021 10:15:06 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A12ABC0613D6 for ; Sun, 17 Jan 2021 07:14:26 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id j12so1520806pfj.12 for ; Sun, 17 Jan 2021 07:14:26 -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=eriJkkL7fLLxATd22osz6+JQx60d8cBmS+HC2zuMsVY=; b=momL6MFk49e8UpFPXPurDl4ejJIdmDtiX6SKLsBAOdKpCQLeIwaGBPMaHEr2ULRj7N vP1IpCXFXvlB4XP1rR+0FA/G1imHRm8fAzrT+/bM0yGySjgr5EDhLd4PNdPu7MYC2ATX FtjqCU4STC7bpU/9vgT2J5FhsiEz2+yzrJXRPI80BdS//LyCOWepWeGSqKkXnN6mQnJw j5KHYeNfV1pCEmuephfUWYcY4+HlScp2zLO2ztjj44O06dTL7I+BR6uJmjLIrKXrYznK 13w5/jV6XfeneoDN6gpgGc8X2a5284C+9N2/2OxkNqyAwVFUZjnsrbMBW26rpMlTRpko eF6Q== 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=eriJkkL7fLLxATd22osz6+JQx60d8cBmS+HC2zuMsVY=; b=FJSsjlVCbAuPMF9UfRWEGIMn1dlCFh8GBX//8fstRVCjKmFScYji2j6cT/y1yGWEtE 82ZslJfOU9Yl5wg3CYzRVGeB6Jm2i6C188m84MkRk7xgpQ/lq6U1J5h9F+rEh3ACCimD ZhijkQ+dja9t8TFU84alZ6nFGqXPx15Wd17BQjTZcyvcOH4+USSE0rc+XPd4k7PFdEro uIs5eq39Q8rYscNqnS+cH2NVkllIeJ54WOMpc63Qc1mXsb5nEw1dhx91nsMnnK/a3p2Q Z5qhaR+KAwd1z/tN3VEkxHbqptaCcQV+1pN4YZqxLR3QawSGdpyCTYAqQoK1p08dS+KI VBEA== X-Gm-Message-State: AOAM531H6gW/LXyRaSVyWninqEXA8a3cpWMm8I0+2lmTU6sSeQsac6EC uHQiDdr8zDWTEmrU/Xxs7HYk/A== X-Received: by 2002:a63:3549:: with SMTP id c70mr22007739pga.361.1610896466134; Sun, 17 Jan 2021 07:14:26 -0800 (PST) Received: from localhost.bytedance.net ([139.177.225.247]) by smtp.gmail.com with ESMTPSA id i22sm9247915pjv.35.2021.01.17.07.14.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Jan 2021 07:14:25 -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 v13 05/12] mm: hugetlb: allocate the vmemmap pages associated with each HugeTLB page Date: Sun, 17 Jan 2021 23:10:46 +0800 Message-Id: <20210117151053.24600-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210117151053.24600-1-songmuchun@bytedance.com> References: <20210117151053.24600-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we free a HugeTLB page to the buddy allocator, we should allocate the 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 | 2 ++ mm/hugetlb.c | 2 ++ mm/hugetlb_vmemmap.c | 15 ++++++++++ mm/hugetlb_vmemmap.h | 5 ++++ mm/sparse-vmemmap.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 100 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f928994ed273..16b55d13b0ab 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3007,6 +3007,8 @@ static inline void print_vma_addr(char *prefix, unsigned long rip) void vmemmap_remap_free(unsigned long start, unsigned long end, unsigned long reuse); +void vmemmap_remap_alloc(unsigned long start, unsigned long end, + unsigned long reuse); 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 c165186ec2cf..d11c32fcdb38 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1326,6 +1326,8 @@ static void update_hpage_vmemmap_workfn(struct work_struct *work) page->mapping = NULL; h = page_hstate(page); + alloc_huge_page_vmemmap(h, page); + spin_lock(&hugetlb_lock); __free_hugepage(h, page); spin_unlock(&hugetlb_lock); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 19f1898aaede..6108ae80314f 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -183,6 +183,21 @@ static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h) return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT; } +void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) +{ + unsigned long vmemmap_addr = (unsigned long)head; + unsigned long vmemmap_end, vmemmap_reuse; + + if (!free_vmemmap_pages_per_hpage(h)) + return; + + vmemmap_addr += RESERVE_VMEMMAP_SIZE; + vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h); + vmemmap_reuse = vmemmap_addr - PAGE_SIZE; + + vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse); +} + void free_huge_page_vmemmap(struct hstate *h, struct page *head) { unsigned long vmemmap_addr = (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 #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); /* @@ -25,6 +26,10 @@ static inline unsigned int free_vmemmap_pages_per_hpage(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 ce4be1fa93c2..3b146d5949f3 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -29,6 +29,7 @@ #include #include #include +#include #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; }; +/* 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) @@ -228,6 +234,75 @@ void vmemmap_remap_free(unsigned long start, unsigned long end, free_vmemmap_page_list(&vmemmap_pages); } +static void vmemmap_restore_pte(pte_t *pte, unsigned long addr, + struct vmemmap_remap_walk *walk) +{ + pgprot_t pgprot = PAGE_KERNEL; + struct page *page; + void *to; + + BUG_ON(pte_page(*pte) != walk->reuse_page); + + page = list_first_entry(walk->vmemmap_pages, struct page, lru); + list_del(&page->lru); + to = 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; + + for (addr = start; addr < end; addr += PAGE_SIZE) { + struct page *page; + int nid = page_to_nid((const void *)addr); + +retry: + page = 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, end) + * to the page which is from the @vmemmap_pages + * respectively. + * @start: start address of the vmemmap virtual address range. + * @end: end address of the vmemmap virtual address range. + * @reuse: reuse address. + */ +void vmemmap_remap_alloc(unsigned long start, unsigned long end, + unsigned long reuse) +{ + LIST_HEAD(vmemmap_pages); + struct vmemmap_remap_walk walk = { + .remap_pte = vmemmap_restore_pte, + .reuse_addr = reuse, + .vmemmap_pages = &vmemmap_pages, + }; + + might_sleep(); + + /* See the comment in the vmemmap_remap_free(). */ + BUG_ON(start - reuse != PAGE_SIZE); + + alloc_vmemmap_page_list(&vmemmap_pages, start, end); + vmemmap_remap_range(reuse, 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. -- 2.11.0