From nobody Sat Jul 25 18:06:05 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1B2F936C5B3 for ; Wed, 15 Jul 2026 03:00:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084423; cv=none; b=fprTR7pyt2iI3GJLwUo+1nRGidoYcmT/d0LFGHhPqlEqpywNedRYYI5tBrHsatXxX49shZskW1iCYPUhH7vI1XMCGsdEB9cXYqmnFu71PQvQaDlsIo+3yHYcksoBUTDe5iR2PByI4tnU4VYGUnargTOUy+RwdkoP8Fi3ZcromnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084423; c=relaxed/simple; bh=H1iJBFK0h4/BZcz0yT2Xb5OFnVYv8bJkTQekiV8mjmY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H6qKNES/e8y7NR2j3owBtheIqL2UwiWuNCO1NqjNYm8Hnmw/z6XonjGjNv4mqWD6kyrFf/mpXcv7BZvRFjSRTO3xhlPSu+tVjeu3mJmRlPHn0K5LPQUzY6zXnEUDiec1djrNfQOSo0xfMvH5rDIBm6vrhcJwAuFlIEw6oUI/Ppo= 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=CdTc62sa; arc=none smtp.client-ip=170.10.133.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="CdTc62sa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084420; 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=r2S1pF402fEwqnXDZiEv0A+qeJQZJmSklciMUBTEkZ4=; b=CdTc62saKwztBRFn/EOCN+dRy0T/Rf9I0Cwyfh9ppERTsZ+bBUgI3oEHB1BeUgcfNj4bch dac6gifBJPW3XvZndesbiZuc1zZeYU3pVMwr1BETa44yJ4CFJtPTYA5pysE9u2cpueXDFL kBEDXI3ANY2Y6mf8oWTDQXpLlv9cBh0= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-DaWvWqfgNIqxwO1J7L8avA-1; Tue, 14 Jul 2026 23:00:17 -0400 X-MC-Unique: DaWvWqfgNIqxwO1J7L8avA-1 X-Mimecast-MFC-AGG-ID: DaWvWqfgNIqxwO1J7L8avA_1784084414 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6D8BC1956052; Wed, 15 Jul 2026 03:00:12 +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 ED26818005B7; Wed, 15 Jul 2026 03:00:00 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , Baolin Wang , Usama Arif , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "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 1/7] mm/khugepaged: refactor per-scan state clearing into collapse_control_init_scan() Date: Tue, 14 Jul 2026 20:59:30 -0600 Message-ID: <20260715025941.1571316-2-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Extract the repeated clearing of node_load, alloc_nmask, and mthp_present_ptes into a helper to reduce duplication in collapse_scan_pmd() and collapse_scan_file(). Althought file scans do not current use the bitmap, they will in the future, and clearing it now is harmless. Reviewed-by: Baolin Wang Acked-by: Usama Arif Signed-off-by: Nico Pache --- mm/khugepaged.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 55157567dc4c..6ec0812210b6 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -629,6 +629,13 @@ void __khugepaged_exit(struct mm_struct *mm) } } =20 +static void collapse_control_init_scan(struct collapse_control *cc) +{ + memset(cc->node_load, 0, sizeof(cc->node_load)); + nodes_clear(cc->alloc_nmask); + bitmap_zero(cc->mthp_present_ptes, MAX_PTRS_PER_PTE); +} + static void release_pte_folio(struct folio *folio) { node_stat_mod_folio(folio, @@ -1617,9 +1624,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, goto out; } =20 - bitmap_zero(cc->mthp_present_ptes, MAX_PTRS_PER_PTE); - memset(cc->node_load, 0, sizeof(cc->node_load)); - nodes_clear(cc->alloc_nmask); + collapse_control_init_scan(cc); =20 enabled_orders =3D collapse_possible_orders(vma, vma->vm_flags, tva_flags= ); =20 @@ -2691,8 +2696,7 @@ static enum scan_result collapse_scan_file(struct mm_= struct *mm, =20 present =3D 0; swap =3D 0; - memset(cc->node_load, 0, sizeof(cc->node_load)); - nodes_clear(cc->alloc_nmask); + collapse_control_init_scan(cc); rcu_read_lock(); xas_for_each(&xas, folio, start + HPAGE_PMD_NR - 1) { if (xas_retry(&xas, folio)) --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 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 7502E217659 for ; Wed, 15 Jul 2026 03:00:36 +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=1784084437; cv=none; b=KPk5uFPloCqxhfrpyfp0PLK5CQnKpL0hTIO9sa32Nzm8E9shBkT1md8KSxdHTo58nbL1euJDmJ8uIOA6j9qAl0q+sTUDwmcPQq+jpnsVOUeSuEqCGe4uegbW8Iqbq5XMesUOHVJc0zreH+asUhcrKRkHRZmduwpSrTxtIdnfuWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084437; c=relaxed/simple; bh=XWlReEmJiYrUXfHc4WOrhRSXzFJCLA91JygqVdrx91w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aKN3bIqdYhVNiokkynC/ODx950/IMK562BkmforB43cxscY7vNcU5HclqHuekwK33Anqjfg0Ks6GOUQzEMiW4wQY/sf+P4d/cb+xE9M9ksCp5GpvqjZNI8aedOXcIxRz6jAob7wiCfGuE1sj71GeXoTjqor9r4AWpJIa5vDeCaU= 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=FA0WbQWA; 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="FA0WbQWA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084435; 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=FA0WbQWAxOCkl5b/5CBkJlIQPBBtmTbJ/6erKfxbepO/oduS4+nSinP3uSa38wo7byYSpE JZMwMiR82CTr5pU7JtrP2u7xcSkhPqVHwCHNOksxCfwTY7muF/QeSn0Z3CuTLg/WBYON6y OL2Iz1tsZT18H/dndWphmlV0yCeZqok= 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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" 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, } } =20 +/* + * 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(s= truct 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++; } =20 @@ -1766,14 +1776,7 @@ static enum scan_result collapse_scan_pmd(struct mm_= struct *mm, goto out_unmap; } =20 - /* - * 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 && --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 CD0F22E8B9B for ; Wed, 15 Jul 2026 03:00:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084449; cv=none; b=H1Biq4VLaMIEw5dqb7zxCUvNC5Od+PpI2zxY6BWA3trwXjjPBJJNQm1H1k/8OCsNd/5xyBtfZNJ1j7/0AjCDJGAOoZ1a0gWms0ue4ZI1ndiglqVxGfPp/JYU34XaGiMrpYQ5fID5L+OIuuGR2Z32IEV+KqFHhBym7EYiM46j1Ao= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084449; c=relaxed/simple; bh=KNyBXRp2XfsPB9KVTckBxEueI9znAQwXbhU5Asq5cUo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hCJ7Ozktvm402j5c8JxKCCSi4wyzLM9rOXjnH5XRSvKZvPsnm22muvHAjHqKwXn4n0UDH8Gff0pRjoqkTE0loKnHg1e0JhGU9cuB3jyvruOfeCCki1dPty7Ua+KKdAoOqjenSzbiuIVN7qelSUzotuGb+4jCgC5jzOSFuF91Dqs= 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=BA0vNDRy; arc=none smtp.client-ip=170.10.133.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="BA0vNDRy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084445; 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=iPhmQGvxVsdgikGQPOW7y5soGlfMNgvozbvH3hbHJ10=; b=BA0vNDRyJwBJPLJm/a+FbRG87rADry077NCMZBLqktH10eXGCTFGtX5UieIoUrYNAeJxEr 4OGnJtSNfrbs5mjSFlhQm3GUj2ER6MnN7XYEdS4qwIKxzc7HICoTM/Hj6Q/7+EhmouVsH3 ctDEio84PnQQGh5wjdcecG69K3h2lqY= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-282-kbESWq2OMgy90tgGrgby2Q-1; Tue, 14 Jul 2026 23:00:40 -0400 X-MC-Unique: kbESWq2OMgy90tgGrgby2Q-1 X-Mimecast-MFC-AGG-ID: kbESWq2OMgy90tgGrgby2Q_1784084437 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 273AA19560B2; Wed, 15 Jul 2026 03:00:37 +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 A581A180028B; Wed, 15 Jul 2026 03:00:25 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , Baolin Wang , Usama Arif , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "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 3/7] mm/khugepaged: introduce a count_collapse_event() helper Date: Tue, 14 Jul 2026 20:59:32 -0600 Message-ID: <20260715025941.1571316-4-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Provide a simple helper function to help reduce a often used, and duplicate pattern across the khugepaged code. When collapsing to a PMD we need to record a vm_event and the mTHP_stat event. When doing mTHP collapse we only update the mTHP stat. Reviewed-by: Baolin Wang Acked-by: Usama Arif Signed-off-by: Nico Pache --- mm/khugepaged.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index e92e2b928f17..f65bbe2051b3 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -689,6 +689,14 @@ static inline bool collapse_is_referenced(struct colla= pse_control *cc, pte_t pte mmu_notifier_test_young(vma->vm_mm, addr)); } =20 +static void count_collapse_event(unsigned int order, enum vm_event_item vm= _event, + enum mthp_stat_item mthp_event) +{ + if (is_pmd_order(order)) + count_vm_event(vm_event); + count_mthp_stat(order, mthp_event); +} + 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) @@ -709,9 +717,8 @@ static enum scan_result __collapse_huge_page_isolate(st= ruct vm_area_struct *vma, if (pte_none_or_zero(pteval)) { if (++none_or_zero > max_ptes_none) { result =3D SCAN_EXCEED_NONE_PTE; - if (is_pmd_order(order)) - count_vm_event(THP_SCAN_EXCEED_NONE_PTE); - count_mthp_stat(order, MTHP_STAT_COLLAPSE_EXCEED_NONE); + count_collapse_event(order, THP_SCAN_EXCEED_NONE_PTE, + MTHP_STAT_COLLAPSE_EXCEED_NONE); goto out; } continue; @@ -753,9 +760,8 @@ static enum scan_result __collapse_huge_page_isolate(st= ruct vm_area_struct *vma, */ if (++shared > max_ptes_shared) { result =3D SCAN_EXCEED_SHARED_PTE; - if (is_pmd_order(order)) - count_vm_event(THP_SCAN_EXCEED_SHARED_PTE); - count_mthp_stat(order, MTHP_STAT_COLLAPSE_EXCEED_SHARED); + count_collapse_event(order, THP_SCAN_EXCEED_SHARED_PTE, + MTHP_STAT_COLLAPSE_EXCEED_SHARED); goto out; } } @@ -1264,15 +1270,12 @@ static enum scan_result alloc_charge_folio(struct f= olio **foliop, struct mm_stru folio =3D __folio_alloc(gfp, order, node, &cc->alloc_nmask); if (!folio) { *foliop =3D NULL; - if (is_pmd_order(order)) - count_vm_event(THP_COLLAPSE_ALLOC_FAILED); - count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC_FAILED); + count_collapse_event(order, THP_COLLAPSE_ALLOC_FAILED, + MTHP_STAT_COLLAPSE_ALLOC_FAILED); return SCAN_ALLOC_HUGE_PAGE_FAIL; } =20 - if (is_pmd_order(order)) - count_vm_event(THP_COLLAPSE_ALLOC); - count_mthp_stat(order, MTHP_STAT_COLLAPSE_ALLOC); + count_collapse_event(order, THP_COLLAPSE_ALLOC, MTHP_STAT_COLLAPSE_ALLOC); =20 if (unlikely(mem_cgroup_charge(folio, mm, gfp))) { folio_put(folio); @@ -1662,8 +1665,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, if (pte_none_or_zero(pteval)) { if (++none_or_zero > max_ptes_none) { result =3D SCAN_EXCEED_NONE_PTE; - count_vm_event(THP_SCAN_EXCEED_NONE_PTE); - count_mthp_stat(HPAGE_PMD_ORDER, + count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_NONE_PTE, MTHP_STAT_COLLAPSE_EXCEED_NONE); goto out_unmap; } @@ -1672,8 +1674,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, if (!pte_present(pteval)) { if (++unmapped > max_ptes_swap) { result =3D SCAN_EXCEED_SWAP_PTE; - count_vm_event(THP_SCAN_EXCEED_SWAP_PTE); - count_mthp_stat(HPAGE_PMD_ORDER, + count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE, MTHP_STAT_COLLAPSE_EXCEED_SWAP); goto out_unmap; } @@ -1731,8 +1732,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, if (folio_maybe_mapped_shared(folio)) { if (++shared > max_ptes_shared) { result =3D SCAN_EXCEED_SHARED_PTE; - count_vm_event(THP_SCAN_EXCEED_SHARED_PTE); - count_mthp_stat(HPAGE_PMD_ORDER, + count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SHARED_PTE, MTHP_STAT_COLLAPSE_EXCEED_SHARED); goto out_unmap; } --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 653C8288B8 for ; Wed, 15 Jul 2026 03:01:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084462; cv=none; b=M65rJ8xH17YWbxvlPZAzWWvN5esDMZ7eN/Zdm3z1PlFIN+D9J2lByg1XCAJnXp4h04jEolDU43YD9rV9/s9lSpmv91+Vv+FfDdVcsiKpvgM9z1aPHaByakKPBl+C9vP/UnKdy0UT+PSKGspry/rH5Jxs8zy6WLFt4Vm8u9b3dDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084462; c=relaxed/simple; bh=Vzr3a51OVpzNKWf/Krbv6y3xEH3/ChJITFawP2Nx2hs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pLrgkHLiTbMhHOPABul9XgihNWThZmCGQHf49sGEXs/tGTmaPz5LTrycJkbSV33qjz8s8VKBKnb6tNlE5WIwIZV5OguCmKrBoVOcWGGHtJWYiPorJ8uOXRnPUNKDAxPAaA3qsdoyKhOQFRXwknm9J7uEi/hWWCcA91c6T8b2ggE= 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=OBFkSE0A; arc=none smtp.client-ip=170.10.133.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="OBFkSE0A" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084460; 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=9uqOn1R4+r9lMeuwYoLpR5vlJPP1JnN5hGqw9wV+8Ww=; b=OBFkSE0A88U1DxzkLniI6o0LLtOAsX7SQx2Plpm1KV6jjXshn0lg6O6Pdad3m/1eqwhKPg x9p57J8w++JAVsnITD1/Wpo3bEpTeGLQBmUTyfzwkKKLbrvPBwRNSC+MPBIzeYMmyZ88JI GQDRIk1MctYssR2k9bUxGM1eIVZqWE0= 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-686-RLZNDMXpO7-tX4auukvGpQ-1; Tue, 14 Jul 2026 23:00:52 -0400 X-MC-Unique: RLZNDMXpO7-tX4auukvGpQ-1 X-Mimecast-MFC-AGG-ID: RLZNDMXpO7-tX4auukvGpQ_1784084449 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 AC3C3180034C; Wed, 15 Jul 2026 03:00:49 +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 DEBAB180028B; Wed, 15 Jul 2026 03:00:38 +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 4/7] mm/khugepaged: fix outdated comments Date: Tue, 14 Jul 2026 20:59:33 -0600 Message-ID: <20260715025941.1571316-5-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" Fix comment in collapse_scan_pmd() that still described the old folio_mapcount() > folio_ref_count() check and a "512" false-positive scenario. The code now uses folio_expected_ref_count() !=3D folio_ref_count= () which doesn't suffer from the same limitation. Fix comment in collapse_huge_page() that referenced ptep_clear_flush, when the code actually uses pmdp_collapse_flush. Fix comment in __collapse_huge_page_swapin() that referenced the old function name khugepaged_scan_pmd, now collapse_scan_pmd. Also clean up some simple typos and stale terminology (mmap_sem -> mmap_lock, PG_lock -> folio lock, page -> folio, grammar). We also clarify a comment regarding where the max_ptes_none check is deferred to in mthp_collapse() from the original collapse_scan_pmd check. Acked-by: Usama Arif Assisted-by: Cursor(claude-sonnet-4):4.6 Signed-off-by: Nico Pache --- mm/khugepaged.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index f65bbe2051b3..d785468ffb96 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -620,7 +620,7 @@ void __khugepaged_exit(struct mm_struct *mm) /* * This is required to serialize against * collapse_test_exit() (which is guaranteed to run - * under mmap sem read mode). Stop here (after we return all + * under mmap_lock read mode). Stop here (after we return all * pagetables will be destroyed) until khugepaged has finished * working on the pagetables under the mmap_lock. */ @@ -789,7 +789,7 @@ static enum scan_result __collapse_huge_page_isolate(st= ruct vm_area_struct *vma, =20 /* * We can do it before folio_isolate_lru because the - * folio can't be freed from under us. NOTE: PG_lock + * folio can't be freed from under us. NOTE: folio lock * is needed to serialize against split_huge_page * when invoked from the VM. */ @@ -816,7 +816,7 @@ static enum scan_result __collapse_huge_page_isolate(st= ruct vm_area_struct *vma, } =20 /* - * Isolate the page to avoid collapsing an hugepage + * Isolate the folio to avoid collapsing a hugepage * currently in use by the VM. */ if (!folio_isolate_lru(folio)) { @@ -1101,7 +1101,7 @@ static enum scan_result hugepage_vma_revalidate(struc= t mm_struct *mm, unsigned l return SCAN_VMA_CHECK; /* * Anon VMA expected, the address may be unmapped then - * remapped to file after khugepaged reaquired the mmap_lock. + * remapped to file after khugepaged reacquired the mmap_lock. * * thp_vma_allowable_orders may return true for qualified file * vmas. @@ -1159,7 +1159,7 @@ static enum scan_result check_pmd_still_valid(struct = mm_struct *mm, =20 /* * Bring missing pages in from swap, to complete THP collapse. - * Only done if khugepaged_scan_pmd believes it is worthwhile. + * Only done if collapse_scan_pmd believes it is worthwhile. * * For mTHP orders the function bails on the first swap entry, because * faulting pages back in during collapse could re-populate PTEs that @@ -1351,8 +1351,8 @@ static enum scan_result collapse_huge_page(struct mm_= struct *mm, unsigned long s mmap_read_unlock(mm); /* * Prevent all access to pagetables with the exception of - * gup_fast later handled by the ptep_clear_flush and the VM - * handled by the anon_vma lock + PG_lock. + * gup_fast later handled by the pmdp_collapse_flush and the VM + * handled by the anon_vma lock + folio lock. * * UFFDIO_MOVE is prevented to race as well thanks to the * mmap_lock. @@ -1643,7 +1643,8 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, =20 /* * If PMD is the only enabled order, enforce max_ptes_none, otherwise - * scan all pages to populate the bitmap for mTHP collapse. + * scan all pages to populate the bitmap for mTHP collapse. The bitmap + * is then checked again in mthp_collapse() for each attempted order. */ if (enabled_orders !=3D BIT(HPAGE_PMD_ORDER)) max_ptes_none =3D KHUGEPAGED_MAX_PTES_LIMIT; @@ -1764,12 +1765,9 @@ static enum scan_result collapse_scan_pmd(struct mm_= struct *mm, /* * Check if the page has any GUP (or other external) pins. * - * Here the check may be racy: - * it may see folio_mapcount() > folio_ref_count(). - * But such case is ephemeral we could always retry collapse - * later. However it may report false positive if the page - * has excessive GUP pins (i.e. 512). Anyway the same check - * will be done again later the risk seems low. + * Here the check is racy, but such case is ephemeral and + * we could always retry collapse later. Anyway the same + * check will be done again later the risk seems low. */ if (folio_expected_ref_count(folio) !=3D folio_ref_count(folio)) { result =3D SCAN_PAGE_COUNT; --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 1131326159E for ; Wed, 15 Jul 2026 03:01:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084470; cv=none; b=ow95K5GfcdGHfYqBzQGZJ93+ffb/iPZe7kx3N6Jhckt0VusYrqf7j1piWhddClQqcxrHWhvDq6Rcehcp3Hjb/Zr9DH8TNZH/gOA20Lqaji8ebiKJjPa4CiNTGdm6wGu11adQ/XCw2xkAfREV4utIv4GLBXKT2GZ/AXTW56w6o2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084470; c=relaxed/simple; bh=9yywHY8+UGrFdZ5+hsf2lH1Bju/TS8QU01qrnhJwBrw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e234t4hZT89E02U3hGHz2q0KrBir/KxLBvzjMe1gJsu12X9ge+mMGbGJNnNNoIydwEzJLM/w/m6hnoqB0FL4KZ2lOJO1j1gynwayKRw/p+ot891xTL7pqZSmUEI8OpsR73XOB4eyvr4BOdNwx/l8tK8gtvzDtR07S2cF7+TJMF0= 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=FPBl19WK; arc=none smtp.client-ip=170.10.133.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="FPBl19WK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084464; 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=u9NeLOAHalnwve7oIbZyE7s1kBR3uoq+RYEBdfdmZFo=; b=FPBl19WKjoDPvqg5QJtl+YmnVAHrkUZhWkp9HfQ93tN/hDMvCNtmZOfK5eSPVNjEa9IjkN Z2jQ6SV+UspzTXZKjAFu20YbAsFx0z5cCbXlUVo5yxJNEnF4/PLknqzD4GoW/qaur1LJ72 yOERUiFxKwETlTEau7WfdFlb2TcyM5w= 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-312-LRRND7mMPyKkeWf9L7zlfA-1; Tue, 14 Jul 2026 23:01:02 -0400 X-MC-Unique: LRRND7mMPyKkeWf9L7zlfA-1 X-Mimecast-MFC-AGG-ID: LRRND7mMPyKkeWf9L7zlfA_1784084460 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 302EE180034C; Wed, 15 Jul 2026 03:01:00 +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 36D10180028B; Wed, 15 Jul 2026 03:00:50 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , David Hildenbrand , Andrew Morton , 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 Subject: [PATCH v2 5/7] mm/khugepaged: Refactor the PTE state checks into a helper Date: Tue, 14 Jul 2026 20:59:34 -0600 Message-ID: <20260715025941.1571316-6-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" For anonymous collapse, the collapse_scan_pmd() and __collapse_huge_page_isolate() functions share a large portion of their logic. These functions both check the state of the PTEs and verify the following: - max_pte_* values are not exceeded - uffd is not active - lazyfree properties - non-anonymous Merge these checks into a helper collapse_check_pte() to reduce code duplication. We also add a helper struct for this function called pte_check_context which allows us to pass the required parameters in a clean and elegant manner. A helper function is also introduced pte_check_fail() to provide a clean interface to set the pte_check_context failure results and return PTE_CHECK_FAIL state. This helps reduce code duplications across the new collapse_check_pte function. Two slight modifications are done to the original functionality. We now warn (instead of crash) if the anon test fails, and we leverage the vm_normal_folio function instead of page->folio, this should be functionally equivalent. No other functional changes intended. This patch is heavily based off work done by Lance Yang, but modified to deal with conflicts and feedback received during the review cycle [1]. [1] https://lore.kernel.org/linux-mm/20251008043748.45554-1-lance.yang@linu= x.dev/ Suggested-by: David Hildenbrand Signed-off-by: Nico Pache --- mm/khugepaged.c | 298 +++++++++++++++++++++++++----------------------- 1 file changed, 157 insertions(+), 141 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index d785468ffb96..655dbc90535a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -64,6 +64,12 @@ enum scan_result { SCAN_PAGE_DIRTY_OR_WRITEBACK, }; =20 +enum pte_check_result { + PTE_CHECK_SUCCEED, + PTE_CHECK_FAIL, + PTE_CHECK_CONTINUE, +}; + #define CREATE_TRACE_POINTS #include =20 @@ -118,6 +124,20 @@ struct collapse_control { DECLARE_BITMAP(mthp_present_ptes, MAX_PTRS_PER_PTE); }; =20 +struct pte_check_context { + struct collapse_control *cc; + struct vm_area_struct *vma; + unsigned int order; + struct folio *folio; + int none_or_zero; + int shared; + int unmapped; + enum scan_result result; + unsigned int max_ptes_none; + unsigned int max_ptes_swap; + unsigned int max_ptes_shared; +}; + /** * struct khugepaged_scan - cursor for scanning * @mm_head: the head of the mm list to scan @@ -697,74 +717,131 @@ static void count_collapse_event(unsigned int order,= enum vm_event_item vm_event count_mthp_stat(order, mthp_event); } =20 +/* + * pte_check_fail() - A simple helper to set the pte_check_context result = and + * return PTE_CHECK_FAIL. + */ +static enum pte_check_result pte_check_fail(struct pte_check_context *ctx, + enum scan_result result) +{ + ctx->result =3D result; + return PTE_CHECK_FAIL; +} + +/* + * collapse_check_pte() - Check if a PTE is suitable for collapse + * + * Check if a PTE is suitable for collapse based on the following criteria: + * - max_pte_* values are not exceeded + * - uffd is not active + * - lazyfree properties are not present + * - only anonymous pages are present + * + * a helper struct pte_check_context is used to pass and store relevant + * information between the collapse_check_pte() function and the caller. + * + * Return: PTE_CHECK_SUCCEED if the PTE is suitable for collapse, + * PTE_CHECK_FAIL if the PTE is not suitable for collapse, + * PTE_CHECK_CONTINUE if the scan should continue to check the nex= t PTE. + */ +static enum pte_check_result collapse_check_pte(pte_t pteval, + unsigned long addr, struct pte_check_context *ctx) +{ + if (pte_none_or_zero(pteval)) { + if (++ctx->none_or_zero > ctx->max_ptes_none) { + count_collapse_event(ctx->order, THP_SCAN_EXCEED_NONE_PTE, + MTHP_STAT_COLLAPSE_EXCEED_NONE); + return pte_check_fail(ctx, SCAN_EXCEED_NONE_PTE); + } + return PTE_CHECK_CONTINUE; + } + if (!pte_present(pteval)) { + if (ctx->unmapped =3D=3D -1) + return pte_check_fail(ctx, SCAN_PTE_NON_PRESENT); + if (++ctx->unmapped > ctx->max_ptes_swap) { + count_collapse_event(ctx->order, THP_SCAN_EXCEED_SWAP_PTE, + MTHP_STAT_COLLAPSE_EXCEED_SWAP); + return pte_check_fail(ctx, SCAN_EXCEED_SWAP_PTE); + } + if (pte_swp_uffd_any(pteval)) + return pte_check_fail(ctx, SCAN_PTE_UFFD); + return PTE_CHECK_CONTINUE; + } + /* + * Don't collapse if any of the small PTEs are armed with uffd + * write protection. Marking the new huge pmd as write protected + * could bring userfault messages that fall outside of the + * registered range. + */ + if (pte_uffd(pteval)) + return pte_check_fail(ctx, SCAN_PTE_UFFD); + + ctx->folio =3D vm_normal_folio(ctx->vma, addr, pteval); + if (unlikely(!ctx->folio) || unlikely(folio_is_zone_device(ctx->folio))) + return pte_check_fail(ctx, SCAN_PAGE_NULL); + + /* + * If the vma has the VM_DROPPABLE flag, the collapse will + * preserve the lazyfree property without needing to skip. + */ + if (ctx->cc->is_khugepaged && !(ctx->vma->vm_flags & VM_DROPPABLE) && + folio_test_lazyfree(ctx->folio) && !pte_dirty(pteval)) + return pte_check_fail(ctx, SCAN_PAGE_LAZYFREE); + + if (!folio_test_anon(ctx->folio)) { + VM_WARN_ON_FOLIO(!folio_test_anon(ctx->folio), ctx->folio); + return pte_check_fail(ctx, SCAN_PAGE_ANON); + } + + if (folio_maybe_mapped_shared(ctx->folio)) { + /* + * TODO: Support shared pages without leading to further + * mTHP collapses. Currently bringing in new pages via + * shared may cause a future higher order collapse on a + * rescan of the same range. + */ + if (++ctx->shared > ctx->max_ptes_shared) { + count_collapse_event(ctx->order, THP_SCAN_EXCEED_SHARED_PTE, + MTHP_STAT_COLLAPSE_EXCEED_SHARED); + return pte_check_fail(ctx, SCAN_EXCEED_SHARED_PTE); + } + } + + return PTE_CHECK_SUCCEED; +} + 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) { - const unsigned int max_ptes_none =3D collapse_max_ptes_none(cc, vma, orde= r); - const unsigned int max_ptes_shared =3D collapse_max_ptes_shared(cc, order= ); const unsigned long nr_pages =3D 1UL << order; - struct page *page =3D NULL; struct folio *folio =3D NULL; unsigned long addr =3D start_addr; - pte_t *_pte; - int none_or_zero =3D 0, shared =3D 0, referenced =3D 0; + pte_t *_pte, pteval; + int referenced =3D 0; enum scan_result result =3D SCAN_FAIL; + enum pte_check_result pte_check; + struct pte_check_context ctx =3D { + .cc =3D cc, + .vma =3D vma, + .order =3D order, + .unmapped =3D -1, /* don't check swap PTEs */ + .max_ptes_none =3D collapse_max_ptes_none(cc, vma, order), + .max_ptes_shared =3D collapse_max_ptes_shared(cc, order), + }; =20 for (_pte =3D pte; _pte < pte + nr_pages; _pte++, addr +=3D PAGE_SIZE) { - pte_t pteval =3D ptep_get(_pte); - if (pte_none_or_zero(pteval)) { - if (++none_or_zero > max_ptes_none) { - result =3D SCAN_EXCEED_NONE_PTE; - count_collapse_event(order, THP_SCAN_EXCEED_NONE_PTE, - MTHP_STAT_COLLAPSE_EXCEED_NONE); - goto out; - } - continue; - } - if (!pte_present(pteval)) { - result =3D SCAN_PTE_NON_PRESENT; - goto out; - } - if (pte_uffd(pteval)) { - result =3D SCAN_PTE_UFFD; - goto out; - } - page =3D vm_normal_page(vma, addr, pteval); - if (unlikely(!page) || unlikely(is_zone_device_page(page))) { - result =3D SCAN_PAGE_NULL; - goto out; - } - - folio =3D page_folio(page); - VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio); - - /* - * If the vma has the VM_DROPPABLE flag, the collapse will - * preserve the lazyfree property without needing to skip. - */ - if (cc->is_khugepaged && !(vma->vm_flags & VM_DROPPABLE) && - folio_test_lazyfree(folio) && !pte_dirty(pteval)) { - result =3D SCAN_PAGE_LAZYFREE; + pteval =3D ptep_get(_pte); + pte_check =3D collapse_check_pte(pteval, addr, &ctx); + folio =3D ctx.folio; + if (pte_check =3D=3D PTE_CHECK_FAIL) { + result =3D ctx.result; goto out; } + if (pte_check =3D=3D PTE_CHECK_CONTINUE) + continue; =20 - /* See collapse_scan_pmd(). */ - if (folio_maybe_mapped_shared(folio)) { - /* - * TODO: Support shared pages without leading to further - * mTHP collapses. Currently bringing in new pages via - * shared may cause a future higher order collapse on a - * rescan of the same range. - */ - if (++shared > max_ptes_shared) { - result =3D SCAN_EXCEED_SHARED_PTE; - count_collapse_event(order, THP_SCAN_EXCEED_SHARED_PTE, - MTHP_STAT_COLLAPSE_EXCEED_SHARED); - goto out; - } - } /* * TODO: In some cases of partially-mapped folios, we'd actually * want to collapse. @@ -841,13 +918,13 @@ static enum scan_result __collapse_huge_page_isolate(= struct vm_area_struct *vma, result =3D SCAN_LACK_REFERENCED_PAGE; } else { result =3D SCAN_SUCCEED; - trace_mm_collapse_huge_page_isolate(folio, none_or_zero, + trace_mm_collapse_huge_page_isolate(folio, ctx.none_or_zero, referenced, result, order); return result; } out: release_pte_pages(pte, _pte, compound_pagelist); - trace_mm_collapse_huge_page_isolate(folio, none_or_zero, + trace_mm_collapse_huge_page_isolate(folio, ctx.none_or_zero, referenced, result, order); return result; } @@ -1459,7 +1536,7 @@ static enum scan_result collapse_huge_page(struct mm_= struct *mm, unsigned long s spin_lock_nested(pte_ptl, SINGLE_DEPTH_NESTING); pmd_populate(mm, pmd, pmd_pgtable(_pmd)); map_anon_folio_pte_nopf(folio, pte, vma, start_addr, - /*uffd_wp=3D*/ false); + /*uffd=3D*/ false); if (pte_ptl !=3D pmd_ptl) spin_unlock(pte_ptl); } @@ -1613,24 +1690,30 @@ static enum scan_result collapse_scan_pmd(struct mm= _struct *mm, struct vm_area_struct *vma, unsigned long start_addr, bool *lock_dropped, struct collapse_control *cc) { - const unsigned int max_ptes_shared =3D collapse_max_ptes_shared(cc, HPAGE= _PMD_ORDER); - const unsigned int max_ptes_swap =3D collapse_max_ptes_swap(cc, HPAGE_PMD= _ORDER); - unsigned int max_ptes_none =3D collapse_max_ptes_none(cc, vma, HPAGE_PMD_= ORDER); enum tva_type tva_flags =3D cc->is_khugepaged ? TVA_KHUGEPAGED : TVA_FORC= ED_COLLAPSE; pmd_t *pmd; pte_t *pte, *_pte, pteval; int i; - int none_or_zero =3D 0, shared =3D 0, referenced =3D 0; - enum scan_result result =3D SCAN_FAIL; - struct page *page =3D NULL; struct folio *folio =3D NULL; + int referenced =3D 0; + enum scan_result result =3D SCAN_FAIL; unsigned long addr; unsigned long enabled_orders; spinlock_t *ptl; - int node =3D NUMA_NO_NODE, unmapped =3D 0; + int node =3D NUMA_NO_NODE; + enum pte_check_result pte_check; =20 VM_BUG_ON(start_addr & ~HPAGE_PMD_MASK); =20 + struct pte_check_context ctx =3D { + .cc =3D cc, + .vma =3D vma, + .order =3D HPAGE_PMD_ORDER, + .max_ptes_none =3D collapse_max_ptes_none(cc, vma, HPAGE_PMD_ORDER), + .max_ptes_swap =3D collapse_max_ptes_swap(cc, HPAGE_PMD_ORDER), + .max_ptes_shared =3D collapse_max_ptes_shared(cc, HPAGE_PMD_ORDER), + }; + result =3D find_pmd_or_thp_or_none(mm, start_addr, &pmd); if (result !=3D SCAN_SUCCEED) { cc->progress++; @@ -1647,7 +1730,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, * is then checked again in mthp_collapse() for each attempted order. */ if (enabled_orders !=3D BIT(HPAGE_PMD_ORDER)) - max_ptes_none =3D KHUGEPAGED_MAX_PTES_LIMIT; + ctx.max_ptes_none =3D KHUGEPAGED_MAX_PTES_LIMIT; =20 pte =3D pte_offset_map_lock(mm, pmd, start_addr, &ptl); if (!pte) { @@ -1663,81 +1746,14 @@ static enum scan_result collapse_scan_pmd(struct mm= _struct *mm, =20 cc->progress++; =20 - if (pte_none_or_zero(pteval)) { - if (++none_or_zero > max_ptes_none) { - result =3D SCAN_EXCEED_NONE_PTE; - count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_NONE_PTE, - MTHP_STAT_COLLAPSE_EXCEED_NONE); - goto out_unmap; - } - continue; - } - if (!pte_present(pteval)) { - if (++unmapped > max_ptes_swap) { - result =3D SCAN_EXCEED_SWAP_PTE; - count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SWAP_PTE, - MTHP_STAT_COLLAPSE_EXCEED_SWAP); - goto out_unmap; - } - /* - * Always be strict with uffd-wp - * enabled swap entries. Please see - * comment below for pte_uffd(). - */ - if (pte_swp_uffd_any(pteval)) { - result =3D SCAN_PTE_UFFD; - goto out_unmap; - } - continue; - } - if (pte_uffd(pteval)) { - /* - * Don't collapse the page if any of the small - * PTEs are armed with uffd write protection. - * Here we can also mark the new huge pmd as - * write protected if any of the small ones is - * marked but that could bring unknown - * userfault messages that falls outside of - * the registered range. So, just be simple. - */ - result =3D SCAN_PTE_UFFD; - goto out_unmap; - } - - page =3D vm_normal_page(vma, addr, pteval); - if (unlikely(!page) || unlikely(is_zone_device_page(page))) { - result =3D SCAN_PAGE_NULL; - goto out_unmap; - } - folio =3D page_folio(page); - - /* - * If the vma has the VM_DROPPABLE flag, the collapse will - * preserve the lazyfree property without needing to skip. - */ - if (cc->is_khugepaged && !(vma->vm_flags & VM_DROPPABLE) && - folio_test_lazyfree(folio) && !pte_dirty(pteval)) { - result =3D SCAN_PAGE_LAZYFREE; - goto out_unmap; - } - - if (!folio_test_anon(folio)) { - result =3D SCAN_PAGE_ANON; + pte_check =3D collapse_check_pte(pteval, addr, &ctx); + if (pte_check =3D=3D PTE_CHECK_FAIL) { + result =3D ctx.result; goto out_unmap; } - - /* - * We treat a single page as shared if any part of the THP - * is shared. - */ - if (folio_maybe_mapped_shared(folio)) { - if (++shared > max_ptes_shared) { - result =3D SCAN_EXCEED_SHARED_PTE; - count_collapse_event(HPAGE_PMD_ORDER, THP_SCAN_EXCEED_SHARED_PTE, - MTHP_STAT_COLLAPSE_EXCEED_SHARED); - goto out_unmap; - } - } + if (pte_check =3D=3D PTE_CHECK_CONTINUE) + continue; + folio =3D ctx.folio; =20 /* Set bit for occupied pages */ __set_bit(i, cc->mthp_present_ptes); @@ -1779,7 +1795,7 @@ static enum scan_result collapse_scan_pmd(struct mm_s= truct *mm, } if (cc->is_khugepaged && (!referenced || - (unmapped && referenced < HPAGE_PMD_NR / 2))) { + (ctx.unmapped && referenced < HPAGE_PMD_NR / 2))) { result =3D SCAN_LACK_REFERENCED_PAGE; } else { result =3D SCAN_SUCCEED; @@ -1790,13 +1806,13 @@ static enum scan_result collapse_scan_pmd(struct mm= _struct *mm, /* collapse_huge_page expects the lock to be dropped before calling */ mmap_read_unlock(mm); result =3D mthp_collapse(mm, start_addr, referenced, - unmapped, cc, enabled_orders); + ctx.unmapped, cc, enabled_orders); /* mmap_lock was released above, set lock_dropped */ *lock_dropped =3D true; } out: trace_mm_khugepaged_scan_pmd(mm, folio, referenced, - none_or_zero, result, unmapped); + ctx.none_or_zero, result, ctx.unmapped); return result; } =20 --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 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 3E7B523507C for ; Wed, 15 Jul 2026 03:01:17 +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=1784084479; cv=none; b=Rho0jbuDLCprwjR4vzMDt2K5oIqKqo8OMEB81D5nGGn7HXm96/9ShFAPYNq46+FDmo2y+fP750HogWi6CCqvim7M3TaGc5L8Zh/uKRjRW72xz5ixVYQmJFFnrO6ZAeDZDXjkMshB4AImQDrM+C8vIGuDjx4gr/MEAjW0fd1n1+A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084479; c=relaxed/simple; bh=4luQiXsCuwsclFGFRc3xlBCF+QI7Qfyz35QdJyJm34U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GvP6AS8cg6x3qvLqE8dkOnk+s2otY2X8kFX9y1tFtU29NcUblWZ60OXkYkYwHBo3rt8dFIQPK/eaVzMCJc5xuCX0DGnbFyh37FO3X1Z7pSFiOBLrZWQnSTpbtiRKsHuL99jShKPHbJCK1oBNqoDzFLrgKrql1bpSZAmCeu6uizo= 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=Akai4DeF; 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="Akai4DeF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084477; 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=rkmNJj3p+KyDaYOMlbC6g4u6+fH9bsqVff+cIbv9v0Q=; b=Akai4DeFXeSpYH/Q9MGb4O2umRd/gB6dkIEiUGYSU7Iv08pjVTN2Q7nAr0eky8Ioe+wb5M OyHCaupYxrqmX9NOvaMmE7huQBz0I9BRgZ9SYIpKWQRBD9IwWW6tSLEEd5ZGUMy5sefAF9 28kKvzu9qNDRTCAB+Pcx0VzNpr4dKEQ= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-253-Bshq_oCoPjeSmfnmViBaSw-1; Tue, 14 Jul 2026 23:01:12 -0400 X-MC-Unique: Bshq_oCoPjeSmfnmViBaSw-1 X-Mimecast-MFC-AGG-ID: Bshq_oCoPjeSmfnmViBaSw_1784084469 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9B6BC195606B; Wed, 15 Jul 2026 03:01:09 +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 96D481800677; Wed, 15 Jul 2026 03:01:01 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , David Hildenbrand , Andrew Morton , 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 Subject: [PATCH v2 6/7] mm/khugepaged: unmap pte before releasing vma write lock Date: Tue, 14 Jul 2026 20:59:35 -0600 Message-ID: <20260715025941.1571316-7-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" We are currently dropping the anon_vma write lock before unmapping the PTE. Although this is safe, due to us still holding the mmap_write_lock, its safer and less confusing to switch the order of these two operations. Suggested-by: David Hildenbrand Signed-off-by: Nico Pache --- mm/khugepaged.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 655dbc90535a..40125dcb4de9 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1546,10 +1546,10 @@ static enum scan_result collapse_huge_page(struct m= m_struct *mm, unsigned long s =20 result =3D SCAN_SUCCEED; out_up_write: - if (anon_vma_locked) - anon_vma_unlock_write(vma->anon_vma); if (pte) pte_unmap(pte); + if (anon_vma_locked) + anon_vma_unlock_write(vma->anon_vma); mmap_write_unlock(mm); out_nolock: if (folio) --=20 2.54.0 From nobody Sat Jul 25 18:06:05 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 94C6923507C for ; Wed, 15 Jul 2026 03:01:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084488; cv=none; b=uqELHbvxc5i9dEEXUstIw7yztIal5giy9xwmsmNBxTzHiPQOgcDiIu8Nd/Tcc0yTU1ajZxvZpWqvTNvsPb0nFRjKObQ7KcTC01GzwWWTLz4twv+Im5xbeyNyHSEApoDlgwOAifjglgN4GLsxw6t1Y6AEJKGTc2EjQBTNAQJQnCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784084488; c=relaxed/simple; bh=pIVoiqjlNVCk10yLQulMwW5KkPxiVNnoMH41RZkdLiQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Jt4FWVrazb6YAcF5Oxl0TiGgUmk7uEkM+vpPmqDabhGLLFdMtyRUS2tZJWc73659jDSof8Ocp3myMvyqDwvD53TSkD9Qf02ZKL60m/8oC03kDuaKbBiCvQXvKtByjV8CRL1VpRLaNnIZVhb7m9onf/5mswyhY1FIcR8M3qzwQNI= 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=T++4/XGT; arc=none smtp.client-ip=170.10.133.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="T++4/XGT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784084486; 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=5TDsdIa8Xg5/7TFb8E//3LgWq7maer0xFr2rl4UMK3c=; b=T++4/XGTWR2CdZ8clvWQ//MqDQmJs0MGwuyTNMBMz5WH1vjEthvzX2RXKe2CTwX5ICFRXP s7iUlUQRXuC1hyTprF6fRUILzX65ng0KuC/soMM+0EMk0SSV/MqXZhEfAqTYAIOcgvoCjE l6MiAOSurQYp7NBaeVLjTSMEZUn7chI= 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-668-DN0lcp4kP0eGz8Mp95imLg-1; Tue, 14 Jul 2026 23:01:23 -0400 X-MC-Unique: DN0lcp4kP0eGz8Mp95imLg-1 X-Mimecast-MFC-AGG-ID: DN0lcp4kP0eGz8Mp95imLg_1784084480 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 5BE501800361; Wed, 15 Jul 2026 03:01:20 +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 807BB180028B; Wed, 15 Jul 2026 03:01:10 +0000 (UTC) From: Nico Pache To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Nico Pache , Baolin Wang , Lorenzo Stoakes , Andrew Morton , David Hildenbrand , Zi Yan , "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 Subject: [PATCH v2 7/7] mm: Documentation: clarify where the mTHP stats live Date: Tue, 14 Jul 2026 20:59:36 -0600 Message-ID: <20260715025941.1571316-8-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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" The note about khugepaged counters references /proc/vmstat for the PMD case, but never mentions where the mTHPs stats can be found (i.e.: /sys/kernel/mm/transparent_hugepage/hugepages-kB/stats/) Add a small addition to this section for clarity. Also fix a missing period while we are at it. Reviewed-by: Baolin Wang Suggested-by: Lorenzo Stoakes Signed-off-by: Nico Pache --- Documentation/admin-guide/mm/transhuge.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 16f37135ed80..b187d618452f 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -224,7 +224,7 @@ khugepaged will be automatically started when any THP s= ize is enabled (either of the per-size anon control or the top-level control are set to "always" or "madvise"), and it'll be automatically shutdown when all THP sizes are disabled (when both the per-size anon control and the -top-level control are "never") +top-level control are "never"). =20 process THP controls -------------------- @@ -301,7 +301,9 @@ being replaced by a PMD mapping, or (2) physical pages = replaced by one hugepage of various sizes (PMD-sized or mTHP). Each may happen independent= ly, or together, depending on the type of memory and the failures that occur. As such, this value should be interpreted roughly as a sign of progress, -and counters in /proc/vmstat consulted for more accurate accounting):: +and counters in /proc/vmstat consulted for more accurate accounting. +Per-order mTHP collapse statistics are also available under +/sys/kernel/mm/transparent_hugepage/hugepages-kB/stats/):: =20 /sys/kernel/mm/transparent_hugepage/khugepaged/pages_collapsed =20 --=20 2.54.0