From nobody Mon Apr 6 18:23:10 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 618D93559F5 for ; Wed, 18 Mar 2026 20:40:21 +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=1773866421; cv=none; b=sqh8XG3JAondC7uiD8fuYvzJlPtssDeKdxUbUVjHLdoyimUVLUoD0cYFEOFEQMe4tP7DI0427E5Czvdm9mEQYPwyuRkHMh305nmV1NwJgbWFYksbVD5QM7gqgINVkSRLsX1lCh8IiGE+KoiZJdTNqraEWJfKA13NPEITUAXTTRI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866421; c=relaxed/simple; bh=tBNKEPKePH5rEEJcTRad3eBiHZXAi6VWZ/qpvNX9iPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HyUZ70W20L1dFgXrZhmgC6/ML0dcYOnFKLx6HFIvhiQ8uwkOqrO8qdalaAa4J4qwJa/jKupjcznGQ+e8EuvfRvcBrztHskWFjeyfc3iwosowF09/htxEzOE9XPryqFcS+Xo99SbyPDdC7zzn+2KPyzzTi4wvnoo2o+YL3LD8VJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DxUHWIwf; 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="DxUHWIwf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3C76C19421; Wed, 18 Mar 2026 20:40:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866421; bh=tBNKEPKePH5rEEJcTRad3eBiHZXAi6VWZ/qpvNX9iPk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DxUHWIwfNCja3hhGITdGYGgMfnGvvhVMGmciAOR/AP3E9eTKa/Jt1sibs6P5qkpJu nae2V11wRFGLg5fWk8LVf5OcEWRVgWQb1TmrTqePSLHh521a//R5Pm7VmDlCcerWKF r+sfCgei15flhHfFC5LdWpzEesuiI8YdeFyNwiwQp9/XXHPL63LuTzEUpsbhEOOn14 YXQEnqQpHxge/smqA6tixVDENxuecMZ7dQ10HOi56YxLT1a5N2LpsEz3qjXsHS/gwh Bh/DiQp58C7YWKmdEp1s9NjA6L+7A1CLV2vko/qFGtqFUUpx+JyjzYuZoWquy4jacF +FVuOlQqP6rUg== 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 8/8] mm/huge_memory: deduplicate zap_huge_pmd() further by tracking state Date: Wed, 18 Mar 2026 20:39:30 +0000 Message-ID: <102016790a04d115dff734765caa25be25771405.1773865827.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 bcc74b0172fa..f6caa6d35659 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 flush_needed =3D false; bool needs_deposit =3D false; + bool is_present =3D false; bool ret =3D true; spinlock_t *ptl; pmd_t orig_pmd; @@ -2450,6 +2451,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)) @@ -2459,17 +2461,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 { @@ -2485,27 +2485,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 ret; } --=20 2.53.0