Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5157657pxj; Wed, 9 Jun 2021 10:25:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwhfZXYG0xATEHSKyjmx0m56BLCc4Z+t0TzbHm1WPw0NuSLc3RRAqFgGSh2mpmdYDqikyJP X-Received: by 2002:a17:906:c314:: with SMTP id s20mr876940ejz.355.1623259520743; Wed, 09 Jun 2021 10:25:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623259520; cv=none; d=google.com; s=arc-20160816; b=mFih/t3GsyF9d7w2uevIDHNXJKPa9BkUInkTRVc/uYBLE11a0l1lVu9XwuMyBZiJHP S7/jB7cMG8diZctpn2fIxY2smGJgWiSMKjZtBzqzpDeWWVedgQFs5nArh7E8w2YxcTSS 4iH9MhQuStpUK9cmRwd1YgZzmmK7/aT9wuK3eZ33VdPflwMoZ+HP8xjfSz+VhxfeimWk uXz2Ua38OaHwMaFtpOArNGImXiyvRzbdRvfC8UH49mMbvNyxcQJjYK4Qs5lbSbVQZQj0 ilah8smRQsOIEPJI7Nb3E7pZOllzZaATLbxwnI68uz7wIBjwv7w7LLEn4f+0JyCVG3xG sqrw== 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=Q5gMWEv9tC8/vo4nEZxaNdZR95vKmKnlaLF90wl0oFU=; b=PH3wQ/ff4JtyZgx3MHvMGrq15sNBil27Jpdb10TrYI7/sG8x9odMiuvpqmeobrT1b8 Tq2suR8T7AnA7TuGHM/EkQA+O0JJnipuhVaTTRUD34QOAHkYSOXMzC5eaD+LvNV04oOi TVFCVRk7OcLO0gnkgAz9XA5FYEdBNdqUPHjnCbObomdeaf2SEmerBTp8wCSFQ7d22asu hJmwfv7yLRtpgPHo6OLy31FdsgkuyK0PdBJA3SByP2r/hpSRPnl9VpSHWVWoV7juWBuf 0W0cfEKRwZTnhZHoXD+kzMqPyva0rpEPwGXrVzLHWVeHYsd/cUdysKgEglln82zAN6uY +/rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=fF+rSzdr; 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 r18si208440edd.158.2021.06.09.10.24.55; Wed, 09 Jun 2021 10:25:20 -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=fF+rSzdr; 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 S233315AbhFIMTQ (ORCPT + 99 others); Wed, 9 Jun 2021 08:19:16 -0400 Received: from mail-pj1-f45.google.com ([209.85.216.45]:35757 "EHLO mail-pj1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239743AbhFIMTK (ORCPT ); Wed, 9 Jun 2021 08:19:10 -0400 Received: by mail-pj1-f45.google.com with SMTP id fy24-20020a17090b0218b029016c5a59021fso1298199pjb.0 for ; Wed, 09 Jun 2021 05:17:16 -0700 (PDT) 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=Q5gMWEv9tC8/vo4nEZxaNdZR95vKmKnlaLF90wl0oFU=; b=fF+rSzdrOYcR9ijjFVFrOhei1ZRjgv4y5N87LenL7irAqVNjAsEvLUOZbVHDTOBl27 5GyM0gAB28wmHolfQQszATK2NLf0pC1NXbWBfc4O2tHsS5xwlTnj4pVa5e2cyPTow1Mi t750zb5lZNj4wElyK/c4dZJ0KZ406xm1z0oCfs6yn7A3LcrvojwLivhUbr+RT2meDfV6 WdPy+xrxr7qLkGmoP4Ry12kceUi99PdX8EZlpknB36edvksdwxJ8We63qW2Arq2eCeTl 2yZDbJiOrJU1Y2s9z4oM2CaR63D8ZK8+lR0FmeG8mAvgtTEfLiXm13+vG3J8R5SNq7To ZSUg== 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=Q5gMWEv9tC8/vo4nEZxaNdZR95vKmKnlaLF90wl0oFU=; b=CVtOkQE3Bhw334oZH4QYQi8lvF7e7rtNTfEq2Liw7bmjcjR4oNW1e/TjdqNl4uiQTz obmfeyZd4RJtKS7R9cALbs9vnDAcaZHyhCVQWu8QM8h37vrsi3JbSL4+CIIxkvunyNwn o7oRaD2cmCDVuqUpC9Ixe2iC5/enaMlDXGZcmpJpBC9x8tkhaFfauriM3PUUm2+HaMv5 s6ya/hupFC9Km99WGp3jeRtv9dqx+HMu5FumVFyprfTZlTC5s/asvaIxFyN7YY2NC1QT 6GTm9GBMGJov+3fDMkktHz2lLR+6/HkGStv4KMBQES9ybmVxC9zEpFfoMJ/L4o7PNi5G nUsg== X-Gm-Message-State: AOAM531H6MId7k7dAs8+auFEaCb+uDa/p3soxBbSwEXbMaCcpb8ltaDi QvduKEmhYzh3bY1Wi1ADNr5dXA== X-Received: by 2002:a17:902:d305:b029:10d:c8a3:657f with SMTP id b5-20020a170902d305b029010dc8a3657fmr4756404plc.0.1623240976435; Wed, 09 Jun 2021 05:16:16 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id h16sm13689224pfk.119.2021.06.09.05.16.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Jun 2021 05:16:16 -0700 (PDT) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com, david@redhat.com, chenhuang5@huawei.com, bodeddub@amazon.com, corbet@lwn.net Cc: duanxiongchun@bytedance.com, fam.zheng@bytedance.com, zhengqi.arch@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Muchun Song Subject: [PATCH 2/5] mm: hugetlb: introduce helpers to preallocate page tables from bootmem allocator Date: Wed, 9 Jun 2021 20:13:07 +0800 Message-Id: <20210609121310.62229-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20210609121310.62229-1-songmuchun@bytedance.com> References: <20210609121310.62229-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If we want to split the huge PMD of vmemmap pages associated with each gigantic page allocated from bootmem allocator, we should pre-allocate the page tables from bootmem allocator. In this patch, we introduce some helpers to preallocate page tables for gigantic pages. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 3 +++ mm/hugetlb_vmemmap.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 13 ++++++++++ 3 files changed, 79 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 03ca83db0a3e..c27a299c4211 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -622,6 +622,9 @@ struct hstate { struct huge_bootmem_page { struct list_head list; struct hstate *hstate; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + pte_t *vmemmap_pte; +#endif }; int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 628e2752714f..6f3a47b4ebd3 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -171,6 +171,7 @@ #define pr_fmt(fmt) "HugeTLB: " fmt #include +#include #include #include "hugetlb_vmemmap.h" @@ -263,6 +264,68 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct list_head *pgtables) return -ENOMEM; } +unsigned long __init gigantic_vmemmap_pgtable_prealloc(void) +{ + struct huge_bootmem_page *m, *tmp; + unsigned long nr_free = 0; + + list_for_each_entry_safe(m, tmp, &huge_boot_pages, list) { + struct hstate *h = m->hstate; + unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); + unsigned long size; + + if (!nr) + continue; + + size = nr << PAGE_SHIFT; + m->vmemmap_pte = memblock_alloc_try_nid(size, PAGE_SIZE, 0, + MEMBLOCK_ALLOC_ACCESSIBLE, + NUMA_NO_NODE); + if (!m->vmemmap_pte) { + nr_free++; + list_del(&m->list); + memblock_free_early(__pa(m), huge_page_size(h)); + } + } + + return nr_free; +} + +void __init gigantic_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *head) +{ + struct hstate *h = m->hstate; + unsigned long pte = (unsigned long)m->vmemmap_pte; + unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); + + if (!nr) + return; + + /* + * If we had gigantic hugepages allocated at boot time, we need + * to restore the 'stolen' pages to totalram_pages in order to + * fix confusing memory reports from free(1) and another + * side-effects, like CommitLimit going negative. + */ + adjust_managed_page_count(head, nr); + + /* + * Use the huge page lru list to temporarily store the preallocated + * pages. The preallocated pages are used and the list is emptied + * before the huge page is put into use. When the huge page is put + * into use by prep_new_huge_page() the list will be reinitialized. + */ + INIT_LIST_HEAD(&head->lru); + + while (nr--) { + struct page *pte_page = virt_to_page(pte); + + __ClearPageReserved(pte_page); + list_add(&pte_page->lru, &head->lru); + pte += PAGE_SIZE; + } +} + /* * Previously discarded vmemmap pages will be allocated and remapping * after this function returns zero. diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 306e15519da1..f6170720f183 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -16,6 +16,9 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head); void hugetlb_vmemmap_init(struct hstate *h); int vmemmap_pgtable_prealloc(struct hstate *h, struct list_head *pgtables); void vmemmap_pgtable_free(struct list_head *pgtables); +unsigned long gigantic_vmemmap_pgtable_prealloc(void); +void gigantic_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *head); /* * How many vmemmap pages associated with a HugeTLB page that can be freed @@ -45,6 +48,16 @@ static inline void vmemmap_pgtable_free(struct list_head *pgtables) { } +static inline unsigned long gigantic_vmemmap_pgtable_prealloc(void) +{ + return 0; +} + +static inline void gigantic_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *head) +{ +} + static inline void hugetlb_vmemmap_init(struct hstate *h) { } -- 2.11.0