From nobody Sat Apr 4 04:39:38 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43D533E3152 for ; Fri, 20 Mar 2026 18:08:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774030087; cv=none; b=PfZIqhIf9Uk3tthcWXxxZU6VDZ/k3uoPM5KrsDAObPM6N68zzVQav5U6DHEfqjaeiMkaawMmXD2c1EHdwMytAOUyhdmIt4UnEx0Kd1gHhqwzEuCMPaTjiL9Pq8McZfvJNPqO08KbDeGJoDc9Sh0a2S+199ZC/+6Na5r2GUEUlJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774030087; c=relaxed/simple; bh=6P2QVEZVXVar1bmzgldHuZ7a+MnKysvamdRBwGT7peM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=niYR7U9jdQ1I4oqLhF6I07dWqAcI0Zgufl3ef6UHrggWEYZB9GtHV5mFCdT/SaVs99YpXcktxiPygvQjQiD56ZPBMuVDMEwcmOi0uRO7vW01MZkJ6ftT+Ui4OMEm5dtRUqJHNoYLO08udmtGI72N68Y4n8aRbhLvNMmw9VsELM0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a68hAJ1i; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a68hAJ1i" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5E3AC19425; Fri, 20 Mar 2026 18:08:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774030086; bh=6P2QVEZVXVar1bmzgldHuZ7a+MnKysvamdRBwGT7peM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a68hAJ1iGqH/rdOm9hYDiCHCh8oHUxOEaobz/g6HDcmOJMC9tpFKDjNUCaxMvitz7 Y3InS9H/twbtaW9AREXURS2pEjKmH6WK5wJWsDJoNGS0cEtgT1kctX9T5aQWZC/pjO KaYXQ2KePnvfxMjBmRvx2Llacg4PJVRnhYcrGXzkq9exheni6GiTG8Dcbc7pEDlk9R Lqmr2Kifo7s4iB28ELQwP3m2RggAkrl01KOON70VqbrVm+QKdnTcVzIEKUVsg/Dei2 DTPSHnMVPx0Smc7arXu8ioY6C46nFk6ONrS6/O41gw4kLvmMl/0yQHPFUhRAWKh4l4 E6fXVkX9r/Rkg== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kiryl Shutsemau , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 12/13] mm/huge_memory: add and use normal_or_softleaf_folio_pmd() Date: Fri, 20 Mar 2026 18:07:29 +0000 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now we have pmd_to_softleaf_folio() available to us which also raises a CONFIG_DEBUG_VM warning if unexpectedly an invalid softleaf entry, we can now abstract folio handling altogether. vm_normal_folio() deals with the huge zero page (which is present), as well as PFN map/mixed map mappings in both cases returning NULL. Otherwise, we try to obtain the softleaf folio. This makes the logic far easier to comprehend and has it use the standard vm_normal_folio_pmd() path for decoding of present entries. Finally, we have to update the flushing logic to only do so if a folio is established. This patch also makes the 'is_present' value more accurate - because PFN map, mixed map and zero huge pages are present, just not present and 'normal'. Signed-off-by: Lorenzo Stoakes (Oracle) Reviewed-by: Suren Baghdasaryan --- mm/huge_memory.c | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 9ddf38d68406..5831966391bd 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2342,10 +2342,6 @@ static void zap_huge_pmd_folio(struct mm_struct *mm,= struct vm_area_struct *vma, add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR); =20 - /* - * Use flush_needed to indicate whether the PMD entry - * is present, instead of checking pmd_present() again. - */ if (is_present && pmd_young(pmdval) && likely(vma_has_recency(vma))) folio_mark_accessed(folio); @@ -2356,6 +2352,17 @@ static void zap_huge_pmd_folio(struct mm_struct *mm,= struct vm_area_struct *vma, folio_put(folio); } =20 +static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *v= ma, + unsigned long addr, pmd_t pmdval, bool is_present) +{ + if (is_present) + return vm_normal_folio_pmd(vma, addr, pmdval); + + if (!thp_migration_supported()) + WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); + return pmd_to_softleaf_folio(pmdval); +} + /** * zap_huge_pmd - Zap a huge THP which is of PMD size. * @tlb: The MMU gather TLB state associated with the operation. @@ -2390,36 +2397,20 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, tlb->fullmm); arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); - if (vma_is_special_huge(vma)) - goto out; - if (is_huge_zero_pmd(orig_pmd)) { - if (!vma_is_dax(vma)) - has_deposit =3D true; - goto out; - } =20 - if (pmd_present(orig_pmd)) { - folio =3D pmd_folio(orig_pmd); - is_present =3D true; - } else if (pmd_is_valid_softleaf(orig_pmd)) { - const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); + is_present =3D pmd_present(orig_pmd); + folio =3D normal_or_softleaf_folio_pmd(vma, addr, orig_pmd, is_present); + if (folio) + zap_huge_pmd_folio(mm, vma, orig_pmd, folio, is_present, + &has_deposit); + else if (is_huge_zero_pmd(orig_pmd)) + has_deposit =3D !vma_is_dax(vma); =20 - folio =3D softleaf_to_folio(entry); - if (!thp_migration_supported()) - WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); - } else { - WARN_ON_ONCE(true); - goto out; - } - - zap_huge_pmd_folio(mm, vma, orig_pmd, folio, is_present, &has_deposit); - -out: if (has_deposit) zap_deposited_table(mm, pmd); =20 spin_unlock(ptl); - if (is_present) + if (is_present && folio) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); return true; } --=20 2.53.0