From patchwork Wed Jun 9 12:13:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12309977 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.6 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,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 E1297C48BCF for ; Wed, 9 Jun 2021 12:16:20 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5A15D613CA for ; Wed, 9 Jun 2021 12:16:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A15D613CA 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 EC5DA6B0070; Wed, 9 Jun 2021 08:16:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E76026B0071; Wed, 9 Jun 2021 08:16:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA2086B0072; Wed, 9 Jun 2021 08:16:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0241.hostedemail.com [216.40.44.241]) by kanga.kvack.org (Postfix) with ESMTP id 968646B0070 for ; Wed, 9 Jun 2021 08:16:18 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 33E678249980 for ; Wed, 9 Jun 2021 12:16:18 +0000 (UTC) X-FDA: 78234082836.31.D3E3746 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf07.hostedemail.com (Postfix) with ESMTP id 0A2CCA00025C for ; Wed, 9 Jun 2021 12:16:12 +0000 (UTC) Received: by mail-pj1-f49.google.com with SMTP id k5so1236857pjj.1 for ; Wed, 09 Jun 2021 05:16:17 -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=ZCMmkrpSDlJYlwHr+JtF9Ez+zvWBTuWXkSvJF5oJDewxVAg3YZoUhqjZ6Z5/yWUrFm iF3et39fp2gjEUyZCB9YY6Vk89SJDj0qTfKYR58qoO49DgsmLVVc46SxkYL79kCMrAjV ChrPSA6q3cnyTX0ANFrf1VK5cA0Z5Slq4mrN9Isi8Jvkg91TNGnD6NOrEyGmDJKyNora FamK2101QjILPMStq27HiHXC78zKTfYie6vQoWgxF7vo5nMOLYd2l8yKrb86mo76nMfP DN2SuhYVJwxWZ6wGmE5wQJwc/QYPPu0+n955cGZNntd+/xaEG76PZDFXw7HDl2leXH8F C/3Q== X-Gm-Message-State: AOAM531HB253FRDlsZoWfVVIc15HLmGicJnGZWfYmluF2byMJpPoXP4G HeC/F31f+zYYw4XiNYBP1zqsUw== X-Google-Smtp-Source: ABdhPJzOgyiAQiBObGnuElieU/iq/wW8CUGGkfOtZNVadqwy30FNgVSA44A84vH2Tc9oftskRcB8zw== 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 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=fF+rSzdr; spf=pass (imf07.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0A2CCA00025C X-Stat-Signature: feqd3t9erw6n5bqmdawfsuswotrz7mac X-HE-Tag: 1623240972-866022 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: 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) { }