From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (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 2500D47606E for ; Tue, 4 Aug 2026 19:25:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785871531; cv=none; b=f34a+PVdx/8bK3Lfyb760YtAPp7SN88G94KOx3VuE7r6XO35qDA2/2gNofRTdHkwVnWtewUAih3lKBdRGnMflmuOdcEtriJ+TckXudY9VOHwZUXKeP18kWkgyQtFrRCuLCh/DShhPrTrs1SQFbahuOgmjctxry4ySRh2jNZdFpQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785871531; c=relaxed/simple; bh=2G6xuOd+lEIt8bVMlUD0kGAehyrpR7NcNcsyLUWXgv8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ujn/TCDFpDSWkKuJcLT9ikD/OM9r5AUS2hnR+PvIFVdAfOFQ2GNVgcwF0mpMRE2B0Bj1l/sFLWzXdWfMpBGOsJy7cl575j+QizMCo6uYIzamywjTEQCFvJmIpvOnOeAgx+/dQPYYq3YNqnrH5bimkdhi4XVLLxanC3Beg2rKs84= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rCWUb/f0; arc=none smtp.client-ip=91.218.175.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="rCWUb/f0" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1785871527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dVme6Lrk8eLALEVFcm4lB1EGIT/woVs12FA3T5p62Gk=; b=rCWUb/f0kE/YDFFqxsBddT28dRPKcKcZqwxdVfULUqxAts5R4l83/1wdpxno73X/EOlDRK oSfjTrBWMELU/htvG4Y4VYUGu0G2B2GxotUs5qW9VbSlXmdCwyG1OA2Z2oc7HsIBlGnszN EBN+yVvT6QPBVYbjB3l3TP4f6wYEZhA= From: "Nico Pache (Red Hat)" Date: Tue, 04 Aug 2026 13:25:00 -0600 Subject: [PATCH v3 2/7] mm/khugepaged: extract young page check into pte_is_referenced() helper 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-Transfer-Encoding: 7bit Message-Id: <20260804-khugepaged_pte_refactor-v3-2-0364cad642a0@linux.dev> References: <20260804-khugepaged_pte_refactor-v3-0-0364cad642a0@linux.dev> In-Reply-To: <20260804-khugepaged_pte_refactor-v3-0-0364cad642a0@linux.dev> To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jonathan Corbet , Shuah Khan , "Nico Pache (Red Hat)" X-Migadu-Flow: FLOW_OUT This change deduplicates the "is this PTE/folio referenced enough to be considered for a collapse" condition that was repeated in both __collapse_huge_page_isolate() and collapse_scan_pmd(), extracting it into a single inline helper function. Also move the comment and use it as the function header. While we are at it, updated the comment to clarify that a young pte is a recently accessed one. Acked-by: Usama Arif Signed-off-by: Nico Pache (Red Hat) --- mm/khugepaged.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 1e26ea97381a..9b15c1740943 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -672,6 +672,23 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, } } +/* + * pte_is_referenced() - Check for enough referenced PTEs to justify collapsing + * + * If collapse was initiated by khugepaged, check that the page has been + * recently accessed (young pte) to justify collapsing the page. + * + * Return: true if the page has been recently accessed. + */ +static inline bool pte_is_referenced(struct collapse_control *cc, pte_t pteval, + struct folio *folio, struct vm_area_struct *vma, unsigned long addr) +{ + return cc->is_khugepaged && + (pte_young(pteval) || folio_test_young(folio) || + folio_test_referenced(folio) || + mmu_notifier_test_young(vma->vm_mm, addr)); +} + static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, unsigned long start_addr, pte_t *pte, struct collapse_control *cc, unsigned int order, struct list_head *compound_pagelist) @@ -810,14 +827,7 @@ static enum scan_result __collapse_huge_page_isolate(struct vm_area_struct *vma, if (folio_test_large(folio)) list_add_tail(&folio->lru, compound_pagelist); next: - /* - * If collapse was initiated by khugepaged, check that there is - * enough young pte to justify collapsing the page - */ - if (cc->is_khugepaged && - (pte_young(pteval) || folio_test_young(folio) || - folio_test_referenced(folio) || - mmu_notifier_test_young(vma->vm_mm, addr))) + if (pte_is_referenced(cc, pteval, folio, vma, addr)) referenced++; } @@ -1766,14 +1776,7 @@ static enum scan_result collapse_scan_pmd(struct mm_struct *mm, goto out_unmap; } - /* - * If collapse was initiated by khugepaged, check that there is - * enough young pte to justify collapsing the page - */ - if (cc->is_khugepaged && - (pte_young(pteval) || folio_test_young(folio) || - folio_test_referenced(folio) || - mmu_notifier_test_young(vma->vm_mm, addr))) + if (pte_is_referenced(cc, pteval, folio, vma, addr)) referenced++; } if (cc->is_khugepaged && -- 2.55.0