From nobody Mon Apr 6 13:30:37 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 3A7C23D47BA for ; Thu, 19 Mar 2026 13:00:42 +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=1773925243; cv=none; b=PO6skK7ZE/GscicPksXUQwZMWPc7V7WvKRH5CxfxIHIghfwaslbxpWHjWIgAPeVJS8g4YgSI1Sxn+4MHtBjgNOobfo2onuvLI5SjSEgG2ugTxDSInVoyOyIBqLh19cARsA3sucZ9jjEv5NHcBI7O8gAq268GtOaj8+O0ExCBKpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773925243; c=relaxed/simple; bh=jwAcjIvj94lIaFqSDU5rGJUwe2UvHWTByimGr9pyvys=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vc8Yzb6RZi/97w4GwOJDiKWakuGPZKVdZplZO/yVH/ZOrlz5dwdLe3k76QEKuek69hgW8N1KTRE4xOYP9GpVxxnZrhiADrdwVBwWHGJfhjg+j1uaLniNh964Zf6V42pgF+f6/Pc+bB5h/cq31ePk5YGiHjiQGlDqMQ3EeKSOosM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nkialUqi; 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="nkialUqi" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E223C2BCAF; Thu, 19 Mar 2026 13:00:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773925242; bh=jwAcjIvj94lIaFqSDU5rGJUwe2UvHWTByimGr9pyvys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nkialUqirVob79VJcXVODpVto2tm0ZpXaRVx2iJtHiGVZ9MOVVMaxvmeFqBDxo7a5 FvQmMrYASCI/q9JwLy2KsJH+UDaCTyu2OO/wy5a0Ouy9ZPD0F5+GF9LybkX3ifvCjf k5swhZPcrmvw9Q7rdVgFgdVjv0pZUMnX96G96lj9+hd3xUZjictASE6ENJbrMJ/mwC qy5/5wGs5oVqpLx/U1YUiige/3UllKiHQRvOGACxLyT1bfLhzKKWAwkiScuqJWmFzd G2t1N0wO17Paz8QplizaFaZox+jE2IXxkKsCzgEPW/ZbBlrsmkUzh7rjsEOE7N6vMZ gcVUBIOFrTwJA== 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 , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 8/9] mm/huge_memory: deduplicate zap_huge_pmd() further by tracking state Date: Thu, 19 Mar 2026 13:00:14 +0000 Message-ID: <440f68edcb597c28918d89b0be279d498561c89a.1773924928.git.ljs@kernel.org> 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" The flush_needed boolean is really tracking whether a PMD entry is present, so use it that way directly and rename it to is_present. Deduplicate the folio_remove_rmap_pmd() and folio map count warning between present and device private by tracking where we need to remove the rmap. We can also remove the comment about using flush_needed to track whether a PMD entry is present as it's now irrelevant. Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index c4e00c645e58..22715027e56c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2430,9 +2430,10 @@ static inline void zap_deposited_table(struct mm_str= uct *mm, pmd_t *pmd) bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr) { + bool needs_remove_rmap =3D false; struct folio *folio =3D NULL; bool needs_deposit =3D false; - bool flush_needed =3D false; + bool is_present =3D false; spinlock_t *ptl; pmd_t orig_pmd; =20 @@ -2449,6 +2450,7 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, */ orig_pmd =3D pmdp_huge_get_and_clear_full(vma, addr, pmd, tlb->fullmm); + arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); if (vma_is_special_huge(vma)) @@ -2458,17 +2460,15 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, goto out; } =20 - if (pmd_present(orig_pmd)) { + is_present =3D pmd_present(orig_pmd); + if (is_present) { folio =3D pmd_folio(orig_pmd); - - flush_needed =3D true; - folio_remove_rmap_pmd(folio, &folio->page, vma); - WARN_ON_ONCE(folio_mapcount(folio) < 0); + needs_remove_rmap =3D true; } else if (pmd_is_valid_softleaf(orig_pmd)) { const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 folio =3D softleaf_to_folio(entry); - + needs_remove_rmap =3D folio_is_device_private(folio); if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); } else { @@ -2483,27 +2483,25 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, add_mm_counter(tlb->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 (flush_needed && pmd_young(orig_pmd) && + if (is_present && pmd_young(orig_pmd) && likely(vma_has_recency(vma))) folio_mark_accessed(folio); } =20 - if (folio_is_device_private(folio)) { + if (needs_remove_rmap) { folio_remove_rmap_pmd(folio, &folio->page, vma); WARN_ON_ONCE(folio_mapcount(folio) < 0); - folio_put(folio); } =20 out: if (arch_needs_pgtable_deposit() || needs_deposit) zap_deposited_table(tlb->mm, pmd); =20 + if (needs_remove_rmap && !is_present) + folio_put(folio); + spin_unlock(ptl); - if (flush_needed) + if (is_present) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); return true; } --=20 2.53.0