From nobody Sat Apr 4 04:48:20 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 C19833DF002 for ; Fri, 20 Mar 2026 18:08:00 +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=1774030080; cv=none; b=oNvyoD4gOiopQ7tijsYPOedCtys9oE9KO4NnolHpULwTNnTjIXcRr5vWrzHewYfb+fiR/z9GTQOMdlnsJVryefP7aukEa+D8ieYG34jKG0hM6TmZnnKh21zrghI8vNcR4nQ2vbIyEFanA9w6uUT0LUO5ys9Jp/l2o+Imh/X4ckM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774030080; c=relaxed/simple; bh=tXuLxXbV6sBaJ9fh/guXYRO+hrWj8nkDANbCEmM4U+w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JejjLGBO6Hej5pHktYMAfTyCuiRNjFXDOwALgv11EGk2fGSpl048qs0wwhMENC7NE/xnak+M9ADx4gJ8EyguikuKketKHyvPyOi4HFYcaLq0WkqoOQMLITwulO05y/7+v35rTYcHD99jhnvAWs5+O0FM4kuGbEr5CT0nSUm4qQQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OBaSXJ+l; 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="OBaSXJ+l" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 442A2C4CEF7; Fri, 20 Mar 2026 18:08:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774030080; bh=tXuLxXbV6sBaJ9fh/guXYRO+hrWj8nkDANbCEmM4U+w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OBaSXJ+lJmXJ2EJ0++u5PATPuo9ha9Q1I7CKvIXwGZiX4vqIqCAyPCTt0oDVQGKPf Qbh9z6yJo6V7Wf/VaoawJTTmioIRd4Pc81c613eqpP+NYqpL64ucKucKPjXXSe1XyD WdNP0NTcuAwlAYPUS2xZfzE5SZcd1cwvBr1EIVrAYMQedcz+aV3QQK0qs1OekIdkeC 5ewxNG1LsWXiU8cQuuoOInQKyGUO3Gm4c/4Thi4qfWL89xW/1tw9OL0z0WaqSlg5ih 8K7SSuu7kjt69FpMVRDik/KC+MmMgXvHaizS+TRZ9eiKtvcCvtauTV1RctFfq9wuMT juI+UuXSImyBg== 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 10/13] mm/huge_memory: separate out the folio part of zap_huge_pmd() Date: Fri, 20 Mar 2026 18:07:27 +0000 Message-ID: <6c4db67952f5529da4db102a6149b9050b5dda4e.1774029655.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" Place the part of the logic that manipulates counters and possibly updates the accessed bit of the folio into its own function to make zap_huge_pmd() more readable. Also rename flush_needed to is_present as we only require a flush for present entries. Additionally add comments as to why we're doing what we're doing with respect to softleaf entries. This also lays the ground for further refactoring. Signed-off-by: Lorenzo Stoakes (Oracle) Reviewed-by: Baolin Wang Reviewed-by: Suren Baghdasaryan --- mm/huge_memory.c | 61 +++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 673d0c4734ad..9ddf38d68406 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2325,6 +2325,37 @@ static inline void zap_deposited_table(struct mm_str= uct *mm, pmd_t *pmd) mm_dec_nr_ptes(mm); } =20 +static void zap_huge_pmd_folio(struct mm_struct *mm, struct vm_area_struct= *vma, + pmd_t pmdval, struct folio *folio, bool is_present, + bool *has_deposit) +{ + const bool is_device_private =3D folio_is_device_private(folio); + + /* Present and device private folios are rmappable. */ + if (is_present || is_device_private) + folio_remove_rmap_pmd(folio, &folio->page, vma); + + if (folio_test_anon(folio)) { + *has_deposit =3D true; + add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); + } else { + add_mm_counter(mm, mm_counter_file(folio), + -HPAGE_PMD_NR); + + /* + * 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); + } + + /* Device private folios are pinned. */ + if (is_device_private) + folio_put(folio); +} + /** * zap_huge_pmd - Zap a huge THP which is of PMD size. * @tlb: The MMU gather TLB state associated with the operation. @@ -2340,7 +2371,7 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, bool has_deposit =3D arch_needs_pgtable_deposit(); struct mm_struct *mm =3D tlb->mm; struct folio *folio =3D NULL; - bool flush_needed =3D false; + bool is_present =3D false; spinlock_t *ptl; pmd_t orig_pmd; =20 @@ -2369,14 +2400,11 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, =20 if (pmd_present(orig_pmd)) { folio =3D pmd_folio(orig_pmd); - - flush_needed =3D true; - folio_remove_rmap_pmd(folio, &folio->page, vma); + is_present =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); - if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); } else { @@ -2384,33 +2412,14 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, goto out; } =20 - if (folio_test_anon(folio)) { - has_deposit =3D true; - add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR); - } else { - add_mm_counter(mm, mm_counter_file(folio), - -HPAGE_PMD_NR); - - /* - * Use flush_needed to indicate whether the PMD entry - * is present, instead of checking pmd_present() again. - */ - if (flush_needed && pmd_young(orig_pmd) && - likely(vma_has_recency(vma))) - folio_mark_accessed(folio); - } - - if (folio_is_device_private(folio)) { - folio_remove_rmap_pmd(folio, &folio->page, vma); - folio_put(folio); - } + zap_huge_pmd_folio(mm, vma, orig_pmd, folio, is_present, &has_deposit); =20 out: if (has_deposit) zap_deposited_table(mm, pmd); =20 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