From mboxrd@z Thu Jan 1 00:00:00 1970 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 EDD5D20C490 for ; Mon, 30 Mar 2026 09:48:07 +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=1774864088; cv=none; b=J6tqpeGPYax9ezNcPiwJZCY5zbluYddwdbEe5wy/wqdLs7RHiVHhGhQFpGrywSV79fxRBds+hnvz9fiVuJt4+b9ni5ZiGr7LzQlzAz33Bv9BZW6yMoEIvmppfQMuBRSmWecZpaLzNTChiM74ubNbaql/5U87RnaoWT9+LFRsSAw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774864088; c=relaxed/simple; bh=L7TKWn2Q+ImhzxbGenUlYPf7P2gJHg98JdI2bl3bESY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Q9bNQXg3DR7qdqfBRibvOF5fUPW/J9+egcvF0Czrc9XgtkKeOZCDa57y3cWci9zrpeOiwCEttHbYQKyd7p/KMSUgX0MjJ8HTvZciQz/dRObpAZrrtkI1CsIe4ovJMGy9HTpPyk84CqPPz6WjHRnGZnzYduHWrOYF1AC584zdviw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OMReKoLU; 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="OMReKoLU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8AD2C4CEF7; Mon, 30 Mar 2026 09:48:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774864087; bh=L7TKWn2Q+ImhzxbGenUlYPf7P2gJHg98JdI2bl3bESY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=OMReKoLUngDh1GvdLGC2cMwqAfgTJSZlKrJ2vhmNvrtwgynF43AQNXWWTBYG86brb wfwvLo8+0+TnB5qHCt1ArY2icGvw64fU9M2THJJfev8FL3J1cGfvhIVr2e1XIV7rzn Y1Ltz3syL1ZWE8n+sWIiyre0sa6L4CoQ3RmfM6ovO19RBpFDUL4V/xWxY5bMNSYKYd 2HZNASNSb8dh6TUMUA2Iy3RZOizWVlbZMt/czBp7VfC2DaskMsv/rN+nPbouofs+IK ND2cT2TT/KVdoDEE+MrbACTJKeNywx3wCsP8u38VPv5c7TKi1uRqvk9eEBoYbIocEB BP0UhrqPucwfw== Date: Mon, 30 Mar 2026 10:48:01 +0100 From: "Lorenzo Stoakes (Oracle)" To: Suren Baghdasaryan Cc: Andrew Morton , David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Michal Hocko , Kiryl Shutsemau , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 12/13] mm/huge_memory: add and use normal_or_softleaf_folio_pmd() Message-ID: <6ad6c2a6-463f-4e2c-824c-1c9cd02a6355@lucifer.local> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Sat, Mar 28, 2026 at 12:45:08PM -0700, Suren Baghdasaryan wrote: > On Fri, Mar 20, 2026 at 11:08 AM Lorenzo Stoakes (Oracle) > wrote: > > > > 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 Thanks! > > One clarifying question below but I think I know the answer, just want > to double-check. > > > --- > > 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); > > > > - /* > > - * 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); > > } > > > > +static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *vma, > > + 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 = true; > > - goto out; > > - } > > > > - if (pmd_present(orig_pmd)) { > > - folio = pmd_folio(orig_pmd); > > - is_present = true; > > - } else if (pmd_is_valid_softleaf(orig_pmd)) { > > - const softleaf_t entry = softleaf_from_pmd(orig_pmd); > > + is_present = pmd_present(orig_pmd); > > nit: With this you don't need to initialize is_present anymore when > you define it. Ah I think that's sorted out later. > > > + folio = 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 = !vma_is_dax(vma); > > > > - folio = 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); > > > > spin_unlock(ptl); > > - if (is_present) > > + if (is_present && folio) > > In which case would you have a valid folio and !is_present? Is that > the softleaf case? Yes, as pmd_present() will return false for a softleaf entry. This is an easy way of figuring that out, kind of expressed a bit differently 'it's present so maybe we have to do a TLB operation, but make sure we have a folio to operate on'. > > > tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); > > return true; > > } > > -- > > 2.53.0 > > Cheers, Lorenzo