From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796137694; b=maqqjXg3BgXuSIqfkeFd7ekr6mwj0brMq4qgU9bhHrb7gU3B+AQ96Fn14wLFTZEx7efN jGhFFKwlwSbcsohcvwhqrFh0t+/xvs2nNliTbMT2R3RILKkgdiNakytmol4cBghdPt6O2 58v8+PI1OyqjMSCdtzAkD35VCBTrqySjcdM8JUb7yyB8uEAqQdmQm1HjeW5lLLRthOM8F 8C0cSscga5RrjYktef6w+CEBYbIGFoedfWHV/+OHFUxT0IIslMB4bG7d2OMDCntcdltkn sHJhDUUiT+i4g0LuB6GoqF7epIRh5rtPnlTS+hoSPOXsAvAb2lCDhHb6K3OUjSZXPUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137694; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=NZirZQOliwfdI/VA4qoueHQKoRLI+p/Iu1qiZ1NHEX0=; b=dE4W8snTV0V7VcoYNbHskow2qGDZ+bQadOW8oniWfVcew5quz18P4pqQt4lC5CaJe/Ua rxDOHOzN7TwG6E3BHP83GKEhBCGuO+6vn0ekHlzOFUNktv674bFsNkjtvzVE8i/AYZg7L nybS8cigC5BEETMhdc4w3HyQkYQHsdTVeYZGFlDH6JuSFbwcqnU2Zvo24VUuvrpwJ3gCz CKngKrL7IExtePRFTbAEwio4M8YuNqxIJ4zd4oFQt/V+bzi64NxB7PLnPwxph7MbM+nD7 b48UVV7HsKoCjqy6XUCgVckbEudq9CPxK90kuRjwXL9gevyo3rNkL4MDyiaSnBr/zjg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PMxptQOp; arc=none smtp.remote-ip=209.85.216.72 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E1C9606A33F for ; Tue, 1 Dec 2026 15:08:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E1C9606A33F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PMxptQOp Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-3b41e8c1ac0so4669140a91.2 for ; Tue, 01 Dec 2026 07:08:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796137693; x=1796742493; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NZirZQOliwfdI/VA4qoueHQKoRLI+p/Iu1qiZ1NHEX0=; b=PMxptQOpoxcwPztrHyOFuIqtD6JucGujsCZoxLhUT0djRHjcC0Nn0VgVJoMj9mCcZb E0BAkj623iUqJ05HOBmMYBaVeRzZ6xcZXXD0siP5uA1sAjuwp2cIy+0VlKZUa3jGNrWS n8pAT59HqPY15qEEJBzah7mrvhVZo0+56df4+zK6O85ooHlAzF7pp4qSGDQBRBmhvZoq IrWI3HOiPVH5SCxSkseQKMA2OU3yu3VQfDtpneBKn7Hab5IO/OJqn3+HUvTDRXqq//dc I3s0jneVdvrqmcjy+ZtuCZntzHze8kl6jvcExgGeE18L7w0uWqVi4rzV0ItYmmyXwbr7 wX2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796137693; x=1796742493; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NZirZQOliwfdI/VA4qoueHQKoRLI+p/Iu1qiZ1NHEX0=; b=H7NxQ+ngI9tOz0J42fiTu6Ac+01m1zStA9EuzL09pW549W+yrp/tt1bimdUMblvIEi aVBWVgJMMFmVO8Auj4iPLq6zWKly4RNxV8YfdmjiKTacm5/z6pEeDPlAfVWKidDtzVYv 5wVdppIZbYOigfcU4KINcAdA5CBqd9raW+2edNLw7V45VJ2VRu0VziFWVilvyjHvC/cU Xb9/LEzQ0A9Xx8j0ud9tdPxdQAckn2efUK+nGkOsb+aUOcmCdtNifFzHhhEghyxVBAoE 2yN+PO1rP8iKWyyoFllUNs4M0pkwtLB1N0Wi27jAvfhziolOCjT77KZbK4FuRMdXRfqU OYIA== X-Forwarded-Encrypted: i=1; AL+hErHcdd+dRAVnmnJx87GDbeOOiQti/XFwU2B2u7sN/5yGvMxGHnbYnMh+lWDFOluETPxlWKNpKOlmkzxr1rk=@vger.kernel.org X-Gm-Message-State: APuU4Z8/Q1UqAHQfeVCjh0ETlJfOjeJzJ8P7ZtnYVBNq1ZIMgdpiWldC RJQEuDA2s9q6M0KEpT33hB7a8PvLhz7V4tLCkY2qck2BK4WF2aURvjbql4CPHGk2i25jshNWBg0 UfVEKyefZ9eQG1PNDIIc/bA== X-Received: from pjbmg4.prod.google.com ([2002:a17:90b:3704:b0:3b4:1351:6107]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b52:b0:3b3:b9e4:e3f5 with SMTP id 98e67ed59e1d1-3b41c92a524mr36695543a91.30.1796137693033; Tue, 01 Dec 2026 07:08:13 -0800 (PST) Date: Tue, 1 Dec 2026 15:07:48 +0000 In-Reply-To: <20261201150801.2239894-1-jthoughton@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201150801.2239894-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261201150801.2239894-7-jthoughton@google.com> Subject: [PATCH v4 06/18] hugetlb: Optimize bootmem folios that were not pre-HVOed From: James Houghton To: Will Deacon , Catalin Marinas , Muchun Song , Oscar Salvador , Andrew Morton Cc: Kefeng Wang , Nikos Nikoleris , Linu Cherian , Mark Rutland , David Hildenbrand , Ryan Roberts , Nanyong Sun , Yu Zhao , Frank van der Linden , David Rientjes , James Houghton , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Boot-time gigantic folios are expected to be pre-HVOed through the section-based vmemmap optimization, so the bulk optimization pass for bootmem folios was dropped. However, arch_supports_vmemmap_remap() may not yet return true when bootmem folios are allocated (e.g. on arm64, where support depends on a system-wide CPU capability), while it does return true by the time the bootmem folios are gathered. Such architectures disable the section-based vmemmap optimization in their vmemmap population path (see section_vmemmap_optimization_disable()), so the folios are not pre-HVOed and would never be optimized. Optimize such folios when gathering bootmem folios. Their tail struct pages have all been initialized by memmap_init_compound_page(), as they were not pre-HVOed. Pre-HVOed folios are skipped by vmemmap_should_optimize_folio(). Also restore the early return in hugetlb_vmemmap_optimize_folios() when no folio needs to be optimized, so that systems where all bootmem folios were optimized through the section-based optimization (or where HVO is disabled) do not take vmemmap_remap_rwsem for write and flush the TLB twice for nothing. Signed-off-by: James Houghton --- mm/hugetlb.c | 7 +++++++ mm/hugetlb_vmemmap.c | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6a4af46ddded..54c1dd955a29 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3364,6 +3364,13 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, unsigned long flags; struct folio *folio, *tmp_f; + /* + * Some architectures do not support section-based vmemmap optimization, + * so their bootmem folios should be optimized here with folio-based + * optimization. + */ + hugetlb_vmemmap_optimize_folios(h, folio_list); + list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { /* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave(&hugetlb_lock, flags); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 8ec80f450123..df145776aa6b 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -610,6 +610,7 @@ static int hugetlb_vmemmap_pteify_folio(const struct hstate *h, struct folio *fo void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list) { struct folio *folio; + unsigned long nr_to_optimize = 0; LIST_HEAD(vmemmap_pages); unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH; @@ -618,7 +619,14 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l down_write(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { - int ret = hugetlb_vmemmap_pteify_folio(h, folio, flags); + int ret; + + if (!vmemmap_should_optimize_folio(h, folio)) + continue; + + nr_to_optimize++; + + ret = hugetlb_vmemmap_pteify_folio(h, folio, flags); /* * Splitting the PMD requires allocating a page, thus let's fail @@ -630,6 +638,11 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l break; } + if (!nr_to_optimize) { + up_write(&vmemmap_remap_rwsem); + return; + } + flush_tlb_all(); downgrade_write(&vmemmap_remap_rwsem); -- 2.56.1.839.g22a8290db9-goog