From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 EE07723507C for ; Wed, 15 Jul 2026 03:00:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084434; cv=none; b=IX99e2CleAp115Nt9TR8V9vadxpfjjQPmy3Q1qzb6JGezUypqAFGT1jgBZvPbClF097hfN9rQJW2SboXcS36q8Gp8DHX36g9rWpnMM6u+9oOeexM/fkDhS1pP/qvxWh6CHXOn5rPQDcZNnKii163jsJudDLzaRLqCyxfi3dzYf4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084434; c=relaxed/simple; bh=XWlReEmJiYrUXfHc4WOrhRSXzFJCLA91JygqVdrx91w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TBzIrV+ij1yZEvwv/+fqrqnAlLc2igGnfjd8qg37V2EIV2wss1gDs/MMW5QOUhucF+m+AXL0SQ4wZpkEoojOmNz2e63XCJVtT65ONOpwhzC6ladypCjNKB+KUN0CUfDTnBQKV5lbWH+dPFHJ44qQqJpBGI50KzQeqNm5gSLBBEg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cT0hLFLN; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cT0hLFLN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DGUz7PpR4WMeHkdZ7b/XP0zPTbF8uLKzTNlgGjEbA7o=; b=cT0hLFLNWjUZFd1aI0YTjCW/oZc/clhVKwVe6RQOj7U4kXVHxtihFVfVr6JCmbY/Kqhjlo wPQ/f5mGygB7ozkorjuFV9m4qSvLoyZG6Rh3scjSYWAPqImh05iPsVJXz6CXqyAeNbmeJn MCqtAOl7ykc4fkUrDN/QXCsvSqWxlb0= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-471-9urISJdoNiCa7PnOmp61Aw-1; Tue, 14 Jul 2026 23:00:27 -0400 X-MC-Unique: 9urISJdoNiCa7PnOmp61Aw-1 X-Mimecast-MFC-AGG-ID: 9urISJdoNiCa7PnOmp61Aw_1784084425 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E73771800473; Wed, 15 Jul 2026 03:00:24 +0000 (UTC) Received: from p1.redhat.com (unknown [10.22.76.5]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 99A04180028B; Wed, 15 Jul 2026 03:00:13 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , Usama Arif , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jonathan Corbet , Shuah Khan Subject: [PATCH v2 2/7] mm/khugepaged: extract young page check into collapse_is_referenced() helper Date: Tue, 14 Jul 2026 20:59:31 -0600 Message-ID: <20260715025941.1571316-3-npache@redhat.com> In-Reply-To: <20260715025941.1571316-1-npache@redhat.com> References: <20260715025941.1571316-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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 --- mm/khugepaged.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6ec0812210b6..e92e2b928f17 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -672,6 +672,23 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, } } +/* + * collapse_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 collapse_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 (collapse_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 (collapse_is_referenced(cc, pteval, folio, vma, addr)) referenced++; } if (cc->is_khugepaged && -- 2.54.0