From nobody Thu Apr 2 17:15:14 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 80CDD1519AC for ; Thu, 12 Feb 2026 02:19:05 +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=1770862746; cv=none; b=T7SPUi1+AZmoTAzgFmY0E7ofNcRPnj9TfGkgLXjJjUM+Jy17PJ55kHZo0xV5SeaToGwAho6cQ7myMgxs5j+Qvx+y10AWpkWedDwdmgKIuOkL4iODjU2mgsjYMIPNIEniYfKJrGCYFHZAEbqQK7wJ9AGyEMjcK7WdWor7tOEM6ek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770862746; c=relaxed/simple; bh=pkNMzY5/RJWsZ+8aZtVcUHmPea4kRrSXlmVURAAjLTU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BG2rvSg18GPxpWTCknj+qBC+Ybvu+53j8qYs6CEmCe3yr4PgSug7rZNlmEo/DrxocMrqA1itiR3AsMNQPDvGogTP9zRiUVLg644QO9wr89pl0JPfKtGcBf4/nbdAikxUo1sWRl/+qXaWANCCtJWFCsGvXNiq2FO3fAZAFHcegMk= 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=JyNtvOBa; 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="JyNtvOBa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770862744; 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=Q6U/FrtkRP7LOssZfEv12dprTABfhdV/gGW8Q/dm8jw=; b=JyNtvOBamMNSIan7itdAqnN5YdgCUgMTvJMGk0abFvBEI3xIuvzscKcLInCYrXVMmkeU2L sI8/59u5gNeip4ILtUqCVjBS0EG2oabTmPfX89QyplXCiAMfXcCLq0n3ehCp/qVyPLezGC 9DWVPKJ7coSxYOWwlKuRSs5eRiAp5zs= 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-450-nagl1iivM9eipOFExciPKg-1; Wed, 11 Feb 2026 21:19:03 -0500 X-MC-Unique: nagl1iivM9eipOFExciPKg-1 X-Mimecast-MFC-AGG-ID: nagl1iivM9eipOFExciPKg_1770862742 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 3FD891955F12; Thu, 12 Feb 2026 02:19:02 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.91]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 84D601956056; Thu, 12 Feb 2026 02:18:51 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, npache@redhat.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH mm-unstable v1 1/5] mm: consolidate anonymous folio PTE mapping into helpers Date: Wed, 11 Feb 2026 19:18:31 -0700 Message-ID: <20260212021835.17755-2-npache@redhat.com> In-Reply-To: <20260212021835.17755-1-npache@redhat.com> References: <20260212021835.17755-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.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" The anonymous page fault handler in do_anonymous_page() open-codes the sequence to map a newly allocated anonymous folio at the PTE level: - construct the PTE entry - add rmap - add to LRU - set the PTEs - update the MMU cache. Introduce a two helpers to consolidate this duplicated logic, mirroring the existing map_anon_folio_pmd_nopf() pattern for PMD-level mappings: map_anon_folio_pte_nopf(): constructs the PTE entry, takes folio references, adds anon rmap and LRU. This function also handles the uffd_wp that can occur in the pf variant. map_anon_folio_pte_pf(): extends the nopf variant to handle MM_ANONPAGES counter updates, and mTHP fault allocation statistics for the page fault path. The zero-page read path in do_anonymous_page() is also untangled from the shared setpte label, since it does not allocate a folio and should not share the same mapping sequence as the write path. Make nr_pages =3D 1 rather than relying on the variable. This makes it more clear that we are operating on the zero page only. This refactoring will also help reduce code duplication between mm/memory.c and mm/khugepaged.c, and provides a clean API for PTE-level anonymous folio mapping that can be reused by future callers. Signed-off-by: Nico Pache Acked-by: Pedro Falcato --- include/linux/mm.h | 4 ++++ mm/memory.c | 56 ++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index f8a8fd47399c..c3aa1f51e020 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4916,4 +4916,8 @@ static inline bool snapshot_page_is_faithful(const st= ruct page_snapshot *ps) =20 void snapshot_page(struct page_snapshot *ps, const struct page *page); =20 +void map_anon_folio_pte_nopf(struct folio *folio, pte_t *pte, + struct vm_area_struct *vma, unsigned long addr, + bool uffd_wp); + #endif /* _LINUX_MM_H */ diff --git a/mm/memory.c b/mm/memory.c index 8c19af97f0a0..61c2277c9d9f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5211,6 +5211,35 @@ static struct folio *alloc_anon_folio(struct vm_faul= t *vmf) return folio_prealloc(vma->vm_mm, vma, vmf->address, true); } =20 + +void map_anon_folio_pte_nopf(struct folio *folio, pte_t *pte, + struct vm_area_struct *vma, unsigned long addr, + bool uffd_wp) +{ + pte_t entry =3D folio_mk_pte(folio, vma->vm_page_prot); + unsigned int nr_pages =3D folio_nr_pages(folio); + + entry =3D maybe_mkwrite(pte_mkdirty(entry), vma); + if (uffd_wp) + entry =3D pte_mkuffd_wp(entry); + + folio_ref_add(folio, nr_pages - 1); + folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE); + folio_add_lru_vma(folio, vma); + set_ptes(vma->vm_mm, addr, pte, entry, nr_pages); + update_mmu_cache_range(NULL, vma, addr, pte, nr_pages); +} + +static void map_anon_folio_pte_pf(struct folio *folio, pte_t *pte, + struct vm_area_struct *vma, unsigned long addr, + unsigned int nr_pages, bool uffd_wp) +{ + map_anon_folio_pte_nopf(folio, pte, vma, addr, uffd_wp); + add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); + count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_FAULT_ALLOC); +} + + /* * We enter with non-exclusive mmap_lock (to exclude vma changes, * but allow concurrent faults), and pte mapped but not yet locked. @@ -5257,7 +5286,13 @@ static vm_fault_t do_anonymous_page(struct vm_fault = *vmf) pte_unmap_unlock(vmf->pte, vmf->ptl); return handle_userfault(vmf, VM_UFFD_MISSING); } - goto setpte; + if (vmf_orig_pte_uffd_wp(vmf)) + entry =3D pte_mkuffd_wp(entry); + set_pte_at(vma->vm_mm, addr, vmf->pte, entry); + + /* No need to invalidate - it was non-present before */ + update_mmu_cache_range(vmf, vma, addr, vmf->pte, /*nr_pages=3D*/ 1); + goto unlock; } =20 /* Allocate our own private page. */ @@ -5281,11 +5316,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault = *vmf) */ __folio_mark_uptodate(folio); =20 - entry =3D folio_mk_pte(folio, vma->vm_page_prot); - entry =3D pte_sw_mkyoung(entry); - if (vma->vm_flags & VM_WRITE) - entry =3D pte_mkwrite(pte_mkdirty(entry), vma); - vmf->pte =3D pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl); if (!vmf->pte) goto release; @@ -5307,19 +5337,7 @@ static vm_fault_t do_anonymous_page(struct vm_fault = *vmf) folio_put(folio); return handle_userfault(vmf, VM_UFFD_MISSING); } - - folio_ref_add(folio, nr_pages - 1); - add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages); - count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_FAULT_ALLOC); - folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE); - folio_add_lru_vma(folio, vma); -setpte: - if (vmf_orig_pte_uffd_wp(vmf)) - entry =3D pte_mkuffd_wp(entry); - set_ptes(vma->vm_mm, addr, vmf->pte, entry, nr_pages); - - /* No need to invalidate - it was non-present before */ - update_mmu_cache_range(vmf, vma, addr, vmf->pte, nr_pages); + map_anon_folio_pte_pf(folio, vmf->pte, vma, addr, nr_pages, vmf_orig_pte_= uffd_wp(vmf)); unlock: if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); --=20 2.53.0 From nobody Thu Apr 2 17:15:14 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 A342A1F4C8C for ; Thu, 12 Feb 2026 02:19:18 +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=1770862759; cv=none; b=IQRuuWOecHUYCyRW6NIiZkOrk5eQ0AyVlLKHNi8FS0Rp6oAhq5rTKrBq0U/BtUhJFalVQ8W4WhFAzvqYH2adM/ebWKaY4Qo5qfK8apTuIdbQzszNIHZ7CoxJRs2HoIcdk5meGsG1JPD5T5NuRatDGvkOuzwQQtgo0CIlUuHijGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770862759; c=relaxed/simple; bh=rW64OleDM6H1vQ+Tvju5x4ZV6UNoMoBmgOwyiJUV9OM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EVgQ8HMFwuuGYFad1Ues6GBcWogdDuUP60bNl/zcr/AB2p1mxCdDYKyHmJF/X0VZLRo5uuVByKg2iouqG42vUs7ClErazbawoYomnbUe/L8u18dyIn7EaNugSjBjLDjpk4pdbvWh6gAqYxweoxipEdrxw6i2vjEspB8MX1cuXug= 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=QGA8zger; 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="QGA8zger" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770862757; 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=OMwzVZ6Pva3OZZvdMPb7Ru0uN6OFYrizNjTFcvvE/ds=; b=QGA8zgerd3YU9yM+0jo7Ha4W9HSj9yd/5cMFysNqs8Gb/Yedk1Oqxx10Y3FhxL/Vlqf8ev yKhmSSbzUYTAj0g6N5FPcjoXCr3yqBvC2Wx8uBRvQ6gU5smgdPjiuktwKmGkOzxHK5sdQs WjiSY+olwcwDaENoOjw+MrxJPKkCepA= 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-66-6vJplVlWNkmOQ2GM6PEGAg-1; Wed, 11 Feb 2026 21:19:13 -0500 X-MC-Unique: 6vJplVlWNkmOQ2GM6PEGAg-1 X-Mimecast-MFC-AGG-ID: 6vJplVlWNkmOQ2GM6PEGAg_1770862752 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 BF1F818005AD; Thu, 12 Feb 2026 02:19:12 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.91]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8D8B91956053; Thu, 12 Feb 2026 02:19:02 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, npache@redhat.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH mm-unstable v1 2/5] mm: introduce is_pmd_order helper Date: Wed, 11 Feb 2026 19:18:32 -0700 Message-ID: <20260212021835.17755-3-npache@redhat.com> In-Reply-To: <20260212021835.17755-1-npache@redhat.com> References: <20260212021835.17755-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.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" In order to add mTHP support to khugepaged, we will often be checking if a given order is (or is not) a PMD order. Some places in the kernel already use this check, so lets create a simple helper function to keep the code clean and readable. Reviewed-by: Lorenzo Stoakes Suggested-by: Lorenzo Stoakes Signed-off-by: Nico Pache arches# pick b3ace8be204f # mm/khugepaged: rename hpage_collapse_* to co= llapse_* Acked-by: David Hildenbrand (Arm) Reviewed-by: Barry Song Reviewed-by: Lance Yang Reviewed-by: Pedro Falcato Reviewed-by: Wei Yang Reviewed-by: Zi Yan --- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 2 +- mm/khugepaged.c | 4 ++-- mm/mempolicy.c | 2 +- mm/page_alloc.c | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index a4d9f964dfde..bd7f0e1d8094 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -771,6 +771,11 @@ static inline bool pmd_is_huge(pmd_t pmd) } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 +static inline bool is_pmd_order(unsigned int order) +{ + return order =3D=3D HPAGE_PMD_ORDER; +} + static inline int split_folio_to_list_to_order(struct folio *folio, struct list_head *list, int new_order) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 44ff8a648afd..5eae85818635 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4097,7 +4097,7 @@ static int __folio_split(struct folio *folio, unsigne= d int new_order, i_mmap_unlock_read(mapping); out: xas_destroy(&xas); - if (old_order =3D=3D HPAGE_PMD_ORDER) + if (is_pmd_order(old_order)) count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED); count_mthp_stat(old_order, !ret ? MTHP_STAT_SPLIT : MTHP_STAT_SPLIT_FAILE= D); return ret; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index fa1e57fd2c46..c362b3b2e08a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2000,7 +2000,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, * we locked the first folio, then a THP might be there already. * This will be discovered on the first iteration. */ - if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && + if (is_pmd_order(folio_order(folio)) && folio->index =3D=3D start) { /* Maybe PMD-mapped */ result =3D SCAN_PTE_MAPPED_HUGEPAGE; @@ -2329,7 +2329,7 @@ static enum scan_result hpage_collapse_scan_file(stru= ct mm_struct *mm, unsigned continue; } =20 - if (folio_order(folio) =3D=3D HPAGE_PMD_ORDER && + if (is_pmd_order(folio_order(folio)) && folio->index =3D=3D start) { /* Maybe PMD-mapped */ result =3D SCAN_PTE_MAPPED_HUGEPAGE; diff --git a/mm/mempolicy.c b/mm/mempolicy.c index dbd48502ac24..3802e52b01fc 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2450,7 +2450,7 @@ static struct page *alloc_pages_mpol(gfp_t gfp, unsig= ned int order, =20 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && /* filter "hugepage" allocation, unless from alloc_pages() */ - order =3D=3D HPAGE_PMD_ORDER && ilx !=3D NO_INTERLEAVE_INDEX) { + is_pmd_order(order) && ilx !=3D NO_INTERLEAVE_INDEX) { /* * For hugepage allocation and non-interleave policy which * allows the current node (or other explicitly preferred diff --git a/mm/page_alloc.c b/mm/page_alloc.c index c2e96ac35636..2acf22f97ae5 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -719,7 +719,7 @@ static inline bool pcp_allowed_order(unsigned int order) if (order <=3D PAGE_ALLOC_COSTLY_ORDER) return true; #ifdef CONFIG_TRANSPARENT_HUGEPAGE - if (order =3D=3D HPAGE_PMD_ORDER) + if (is_pmd_order(order)) return true; #endif return false; --=20 2.53.0 From nobody Thu Apr 2 17:15:14 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 2CAC92147F9 for ; Thu, 12 Feb 2026 02:19: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=1770862769; cv=none; b=QsETHYW687tskTuLBNijqwx41Ay4jjzfa7/F/B0/kYk8KbPyDI/aXfL7V/t6FwT06rfcxoE7KzZ6NrI+/lJOC7Ot3DNalE42V4NqbGdBtoFPO3OYWslddmIY7rYthkeN1GLfkX00F63+1USf4eXCyfpeTIkVKr3Lh7cHf9snTHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770862769; c=relaxed/simple; bh=/j/u/tlqHWLAPOn9nx/w5IBBuhZbBvWbiyWUpmtOsUY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YYd7/g99KnAtpuipbTfC68IevW36vC4WaxNUn3MTKyVnO0diIfSpSoA0JKQxyKT6uBJhqROcGnXtXqUNVzIGxvDKFyhwodrDuZF2gftgGq86gVbr2e86uy+jse/5f8eDX7CO0l03k0OpdEWMJEEKh98F1t0Ua1BMGwYzYlIUns0= 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=K26QN8UF; 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="K26QN8UF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770862767; 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=BjtR0F+sNTw1I9L2+TY8dc3ujo+lVzKZ7PJHcSSnNDA=; b=K26QN8UFrhDrQDaRFgcc2o6M+TPGGRKeCx7pQl+YwKxPFpIVuPkw5X2XukySxAIP/hfC8c tV5FfUqXGsjcIchXLhCUvYoh5owCmmK8lMg6fykfFrLarX01JOhp7KLeb/0R7wYn04UQpw I0nh2JY1P9xEzm/8djCksSCZH1IQki0= Received: from mx-prod-mc-08.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-214-cOnx6XVFNbyLbnIWZHOlBQ-1; Wed, 11 Feb 2026 21:19:23 -0500 X-MC-Unique: cOnx6XVFNbyLbnIWZHOlBQ-1 X-Mimecast-MFC-AGG-ID: cOnx6XVFNbyLbnIWZHOlBQ_1770862762 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9D9EF1800367; Thu, 12 Feb 2026 02:19:22 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.91]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0EDA01956056; Thu, 12 Feb 2026 02:19:12 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, npache@redhat.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH mm-unstable v1 3/5] mm/khugepaged: define COLLAPSE_MAX_PTES_LIMIT as HPAGE_PMD_NR - 1 Date: Wed, 11 Feb 2026 19:18:33 -0700 Message-ID: <20260212021835.17755-4-npache@redhat.com> In-Reply-To: <20260212021835.17755-1-npache@redhat.com> References: <20260212021835.17755-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.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" The value (HPAGE_PMD_NR - 1) is used often in the khugepaged code to signify the limit of the max_ptes_* values. Add a define for this to increase code readability and reuse. Signed-off-by: Nico Pache Acked-by: Pedro Falcato Reviewed-by: Zi Yan --- mm/khugepaged.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index c362b3b2e08a..3dcce6018f20 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -85,6 +85,7 @@ static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait); * * Note that these are only respected if collapse was initiated by khugepa= ged. */ +#define COLLAPSE_MAX_PTES_LIMIT (HPAGE_PMD_NR - 1) unsigned int khugepaged_max_ptes_none __read_mostly; static unsigned int khugepaged_max_ptes_swap __read_mostly; static unsigned int khugepaged_max_ptes_shared __read_mostly; @@ -252,7 +253,7 @@ static ssize_t max_ptes_none_store(struct kobject *kobj, unsigned long max_ptes_none; =20 err =3D kstrtoul(buf, 10, &max_ptes_none); - if (err || max_ptes_none > HPAGE_PMD_NR - 1) + if (err || max_ptes_none > COLLAPSE_MAX_PTES_LIMIT) return -EINVAL; =20 khugepaged_max_ptes_none =3D max_ptes_none; @@ -277,7 +278,7 @@ static ssize_t max_ptes_swap_store(struct kobject *kobj, unsigned long max_ptes_swap; =20 err =3D kstrtoul(buf, 10, &max_ptes_swap); - if (err || max_ptes_swap > HPAGE_PMD_NR - 1) + if (err || max_ptes_swap > COLLAPSE_MAX_PTES_LIMIT) return -EINVAL; =20 khugepaged_max_ptes_swap =3D max_ptes_swap; @@ -303,7 +304,7 @@ static ssize_t max_ptes_shared_store(struct kobject *ko= bj, unsigned long max_ptes_shared; =20 err =3D kstrtoul(buf, 10, &max_ptes_shared); - if (err || max_ptes_shared > HPAGE_PMD_NR - 1) + if (err || max_ptes_shared > COLLAPSE_MAX_PTES_LIMIT) return -EINVAL; =20 khugepaged_max_ptes_shared =3D max_ptes_shared; @@ -384,7 +385,7 @@ int __init khugepaged_init(void) return -ENOMEM; =20 khugepaged_pages_to_scan =3D HPAGE_PMD_NR * 8; - khugepaged_max_ptes_none =3D HPAGE_PMD_NR - 1; + khugepaged_max_ptes_none =3D COLLAPSE_MAX_PTES_LIMIT; khugepaged_max_ptes_swap =3D HPAGE_PMD_NR / 8; khugepaged_max_ptes_shared =3D HPAGE_PMD_NR / 2; =20 @@ -1869,7 +1870,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, bool is_shmem =3D shmem_file(file); =20 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); - VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); + VM_BUG_ON(start & (COLLAPSE_MAX_PTES_LIMIT)); =20 result =3D alloc_charge_folio(&new_folio, mm, cc); if (result !=3D SCAN_SUCCEED) @@ -2209,7 +2210,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, * unwritten page. */ folio_mark_uptodate(new_folio); - folio_ref_add(new_folio, HPAGE_PMD_NR - 1); + folio_ref_add(new_folio, COLLAPSE_MAX_PTES_LIMIT); =20 if (is_shmem) folio_mark_dirty(new_folio); @@ -2303,7 +2304,7 @@ static enum scan_result hpage_collapse_scan_file(stru= ct mm_struct *mm, unsigned memset(cc->node_load, 0, sizeof(cc->node_load)); nodes_clear(cc->alloc_nmask); rcu_read_lock(); - xas_for_each(&xas, folio, start + HPAGE_PMD_NR - 1) { + xas_for_each(&xas, folio, start + COLLAPSE_MAX_PTES_LIMIT) { if (xas_retry(&xas, folio)) continue; =20 --=20 2.53.0 From nobody Thu Apr 2 17:15:14 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 00CDC42A96 for ; Thu, 12 Feb 2026 02:23:50 +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=1770863032; cv=none; b=GontHQ5FDaMHyTDqiJMwdk1LfupEzgCZuk1sK/cnyqTWEcqe+qAxQ+O+rnAxPt+BGGpdjL4XnYJEfuSGD0DtY9ccJI3h9OHbrOpySw0Aw/xJbIIlZ3XHssBDKSHWAoHTbj5gkYuKeb89qp1yQRijIfoBpvCgE8+nLLhoKNr1sfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770863032; c=relaxed/simple; bh=T6BErkpwsoH2GMiooOyXkPAcTNfhGoNkwDiSa9cXV40=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QkMsjBePkTh3OL8bjLhn7cV3QrydYmteAE6IGMCs5QwgKs7f9TGAkzAGapOycIB4Sg8FWSWXCEbRyNQGBQaYrwB4I1qS6cyzxtoBd99ghyomNiETID6z6BEq8ilm5iYfH9lKx3ltUD7MKetXpy3N8cHrACk1LQdeACPduIPDG0s= 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=QsjJTbwQ; 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="QsjJTbwQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770863030; 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=397xlPpjUxFvY954VRGPkHg8dIT/MsS+yMoqJGr/bMM=; b=QsjJTbwQ536DR3pVqC1mv37NAJo7OXp1t71YGcPTLZDoXRW4/E09wFqDkpDmBrHFFlnjJi UeRf6RN/dj3OPy/mRbqd4BY/l2npeYUFPkIFcb5srMo/msJpul5ctahfy0GZ5MXk6s0T/h 4sxTsEiTgdPSM82u2qEsCKL9lIZO7ds= 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-81-Vd976550OfaHGN9nXINbNw-1; Wed, 11 Feb 2026 21:23:48 -0500 X-MC-Unique: Vd976550OfaHGN9nXINbNw-1 X-Mimecast-MFC-AGG-ID: Vd976550OfaHGN9nXINbNw_1770863027 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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 B84E318005AD; Thu, 12 Feb 2026 02:23:47 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.91]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 566751800464; Thu, 12 Feb 2026 02:23:38 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, npache@redhat.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH mm-unstable v1 4/5] mm/khugepaged: rename hpage_collapse_* to collapse_* Date: Wed, 11 Feb 2026 19:23:28 -0700 Message-ID: <20260212022329.18892-1-npache@redhat.com> In-Reply-To: <20260212021835.17755-1-npache@redhat.com> References: <20260212021835.17755-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.93 Content-Type: text/plain; charset="utf-8" The hpage_collapse functions describe functions used by madvise_collapse and khugepaged. remove the unnecessary hpage prefix to shorten the function name. Reviewed-by: Wei Yang Reviewed-by: Lance Yang Reviewed-by: Liam R. Howlett Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Reviewed-by: Lorenzo Stoakes Signed-off-by: Nico Pache Acked-by: David Hildenbrand (Arm) --- mm/khugepaged.c | 62 ++++++++++++++++++++++++------------------------- mm/mremap.c | 2 +- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 3dcce6018f20..fa41480f6948 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -397,14 +397,14 @@ void __init khugepaged_destroy(void) kmem_cache_destroy(mm_slot_cache); } =20 -static inline int hpage_collapse_test_exit(struct mm_struct *mm) +static inline int collapse_test_exit(struct mm_struct *mm) { return atomic_read(&mm->mm_users) =3D=3D 0; } =20 -static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm) +static inline int collapse_test_exit_or_disable(struct mm_struct *mm) { - return hpage_collapse_test_exit(mm) || + return collapse_test_exit(mm) || mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm); } =20 @@ -438,7 +438,7 @@ void __khugepaged_enter(struct mm_struct *mm) int wakeup; =20 /* __khugepaged_exit() must not run from under us */ - VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm); + VM_BUG_ON_MM(collapse_test_exit(mm), mm); if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) return; =20 @@ -492,7 +492,7 @@ void __khugepaged_exit(struct mm_struct *mm) } else if (slot) { /* * This is required to serialize against - * hpage_collapse_test_exit() (which is guaranteed to run + * collapse_test_exit() (which is guaranteed to run * under mmap sem read mode). Stop here (after we return all * pagetables will be destroyed) until khugepaged has finished * working on the pagetables under the mmap_lock. @@ -581,7 +581,7 @@ static enum scan_result __collapse_huge_page_isolate(st= ruct vm_area_struct *vma, folio =3D page_folio(page); VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio); =20 - /* See hpage_collapse_scan_pmd(). */ + /* See collapse_scan_pmd(). */ if (folio_maybe_mapped_shared(folio)) { ++shared; if (cc->is_khugepaged && @@ -832,7 +832,7 @@ static struct collapse_control khugepaged_collapse_cont= rol =3D { .is_khugepaged =3D true, }; =20 -static bool hpage_collapse_scan_abort(int nid, struct collapse_control *cc) +static bool collapse_scan_abort(int nid, struct collapse_control *cc) { int i; =20 @@ -867,7 +867,7 @@ static inline gfp_t alloc_hugepage_khugepaged_gfpmask(v= oid) } =20 #ifdef CONFIG_NUMA -static int hpage_collapse_find_target_node(struct collapse_control *cc) +static int collapse_find_target_node(struct collapse_control *cc) { int nid, target_node =3D 0, max_value =3D 0; =20 @@ -886,7 +886,7 @@ static int hpage_collapse_find_target_node(struct colla= pse_control *cc) return target_node; } #else -static int hpage_collapse_find_target_node(struct collapse_control *cc) +static int collapse_find_target_node(struct collapse_control *cc) { return 0; } @@ -905,7 +905,7 @@ static enum scan_result hugepage_vma_revalidate(struct = mm_struct *mm, unsigned l enum tva_type type =3D cc->is_khugepaged ? TVA_KHUGEPAGED : TVA_FORCED_COLLAPSE; =20 - if (unlikely(hpage_collapse_test_exit_or_disable(mm))) + if (unlikely(collapse_test_exit_or_disable(mm))) return SCAN_ANY_PROCESS; =20 *vmap =3D vma =3D find_vma(mm, address); @@ -976,7 +976,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 hpage_collapse_scan_pmd believes it is worthwhile. + * Only done if khugepaged_scan_pmd believes it is worthwhile. * * Called and returns without pte mapped or spinlocks held. * Returns result: if not SCAN_SUCCEED, mmap_lock has been released. @@ -1062,7 +1062,7 @@ static enum scan_result alloc_charge_folio(struct fol= io **foliop, struct mm_stru { gfp_t gfp =3D (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() : GFP_TRANSHUGE); - int node =3D hpage_collapse_find_target_node(cc); + int node =3D collapse_find_target_node(cc); struct folio *folio; =20 folio =3D __folio_alloc(gfp, HPAGE_PMD_ORDER, node, &cc->alloc_nmask); @@ -1240,7 +1240,7 @@ static enum scan_result collapse_huge_page(struct mm_= struct *mm, unsigned long a return result; } =20 -static enum scan_result hpage_collapse_scan_pmd(struct mm_struct *mm, +static enum scan_result collapse_scan_pmd(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long start_addr, bool *mmap_locked, struct collapse_control *cc) { @@ -1350,7 +1350,7 @@ static enum scan_result hpage_collapse_scan_pmd(struc= t mm_struct *mm, * hit record. */ node =3D folio_nid(folio); - if (hpage_collapse_scan_abort(node, cc)) { + if (collapse_scan_abort(node, cc)) { result =3D SCAN_SCAN_ABORT; goto out_unmap; } @@ -1416,7 +1416,7 @@ static void collect_mm_slot(struct mm_slot *slot) =20 lockdep_assert_held(&khugepaged_mm_lock); =20 - if (hpage_collapse_test_exit(mm)) { + if (collapse_test_exit(mm)) { /* free mm_slot */ hash_del(&slot->hash); list_del(&slot->mm_node); @@ -1771,7 +1771,7 @@ static void retract_page_tables(struct address_space = *mapping, pgoff_t pgoff) if (find_pmd_or_thp_or_none(mm, addr, &pmd) !=3D SCAN_SUCCEED) continue; =20 - if (hpage_collapse_test_exit(mm)) + if (collapse_test_exit(mm)) continue; =20 if (!file_backed_vma_is_retractable(vma)) @@ -2289,7 +2289,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, return result; } =20 -static enum scan_result hpage_collapse_scan_file(struct mm_struct *mm, uns= igned long addr, +static enum scan_result collapse_scan_file(struct mm_struct *mm, unsigned = long addr, struct file *file, pgoff_t start, struct collapse_control *cc) { struct folio *folio =3D NULL; @@ -2345,7 +2345,7 @@ static enum scan_result hpage_collapse_scan_file(stru= ct mm_struct *mm, unsigned } =20 node =3D folio_nid(folio); - if (hpage_collapse_scan_abort(node, cc)) { + if (collapse_scan_abort(node, cc)) { result =3D SCAN_SCAN_ABORT; folio_put(folio); break; @@ -2395,7 +2395,7 @@ static enum scan_result hpage_collapse_scan_file(stru= ct mm_struct *mm, unsigned return result; } =20 -static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_= result *result, +static unsigned int collapse_scan_mm_slot(unsigned int pages, enum scan_re= sult *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock) __acquires(&khugepaged_mm_lock) @@ -2430,7 +2430,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, enum scan_result goto breakouterloop_mmap_lock; =20 progress++; - if (unlikely(hpage_collapse_test_exit_or_disable(mm))) + if (unlikely(collapse_test_exit_or_disable(mm))) goto breakouterloop; =20 vma_iter_init(&vmi, mm, khugepaged_scan.address); @@ -2438,7 +2438,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, enum scan_result unsigned long hstart, hend; =20 cond_resched(); - if (unlikely(hpage_collapse_test_exit_or_disable(mm))) { + if (unlikely(collapse_test_exit_or_disable(mm))) { progress++; break; } @@ -2460,7 +2460,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, enum scan_result bool mmap_locked =3D true; =20 cond_resched(); - if (unlikely(hpage_collapse_test_exit_or_disable(mm))) + if (unlikely(collapse_test_exit_or_disable(mm))) goto breakouterloop; =20 VM_BUG_ON(khugepaged_scan.address < hstart || @@ -2473,12 +2473,12 @@ static unsigned int khugepaged_scan_mm_slot(unsigne= d int pages, enum scan_result =20 mmap_read_unlock(mm); mmap_locked =3D false; - *result =3D hpage_collapse_scan_file(mm, + *result =3D collapse_scan_file(mm, khugepaged_scan.address, file, pgoff, cc); fput(file); if (*result =3D=3D SCAN_PTE_MAPPED_HUGEPAGE) { mmap_read_lock(mm); - if (hpage_collapse_test_exit_or_disable(mm)) + if (collapse_test_exit_or_disable(mm)) goto breakouterloop; *result =3D try_collapse_pte_mapped_thp(mm, khugepaged_scan.address, false); @@ -2487,7 +2487,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, enum scan_result mmap_read_unlock(mm); } } else { - *result =3D hpage_collapse_scan_pmd(mm, vma, + *result =3D collapse_scan_pmd(mm, vma, khugepaged_scan.address, &mmap_locked, cc); } =20 @@ -2520,7 +2520,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, enum scan_result * Release the current mm_slot if this mm is about to die, or * if we scanned all vmas of this mm. */ - if (hpage_collapse_test_exit(mm) || !vma) { + if (collapse_test_exit(mm) || !vma) { /* * Make sure that if mm_users is reaching zero while * khugepaged runs here, khugepaged_exit will find @@ -2571,8 +2571,8 @@ static void khugepaged_do_scan(struct collapse_contro= l *cc) pass_through_head++; if (khugepaged_has_work() && pass_through_head < 2) - progress +=3D khugepaged_scan_mm_slot(pages - progress, - &result, cc); + progress +=3D collapse_scan_mm_slot(pages - progress, + &result, cc); else progress =3D pages; spin_unlock(&khugepaged_mm_lock); @@ -2816,8 +2816,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsi= gned long start, mmap_read_unlock(mm); mmap_locked =3D false; *lock_dropped =3D true; - result =3D hpage_collapse_scan_file(mm, addr, file, pgoff, - cc); + result =3D collapse_scan_file(mm, addr, file, pgoff, cc); =20 if (result =3D=3D SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb && mapping_can_writeback(file->f_mapping)) { @@ -2831,8 +2830,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsi= gned long start, } fput(file); } else { - result =3D hpage_collapse_scan_pmd(mm, vma, addr, - &mmap_locked, cc); + result =3D collapse_scan_pmd(mm, vma, addr, &mmap_locked, cc); } if (!mmap_locked) *lock_dropped =3D true; diff --git a/mm/mremap.c b/mm/mremap.c index 2be876a70cc0..eb222af91c2d 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -244,7 +244,7 @@ static int move_ptes(struct pagetable_move_control *pmc, goto out; } /* - * Now new_pte is none, so hpage_collapse_scan_file() path can not find + * Now new_pte is none, so collapse_scan_file() path can not find * this by traversing file->f_mapping, so there is no concurrency with * retract_page_tables(). In addition, we already hold the exclusive * mmap_lock, so this new_pte page is stable, so there is no need to get --=20 2.53.0 From nobody Thu Apr 2 17:15:14 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 35D1C21C9FD for ; Thu, 12 Feb 2026 02:25:29 +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=1770863131; cv=none; b=hCvqfECwd/8Yon/45ReIiBQJhqYpMEOHh+XhQQKygdM777oWu02f5qWts4uBNmFenH+saHbP3lGVamcycLQVOwAAuRxMs6S75EDyAQcxTb96gQ4pDYyPkTvzuOTXa7E8FWxwr5mmqjBwFFr+MeEfqMPUMF8Fv35bQtT9IIM5bx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770863131; c=relaxed/simple; bh=SwPCO594e2z+UrDy4eBRaZiBdQi8A77pbV/NKHS5qKQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B8p4HdQgZa0stqMTzbPuOHkrThKrx/MZxg/xkuDw62qK1N052kUUOjip1EGsVvUkAu5dD2BQfUh8FBp/CpnCSCvEpUWggfIhUr5dK4s7B1a3uIBkXge7Uf5Z64JkZT47pICvKqipx4WREYoOl410oRGGG2S6kkulrcHvNI5Z3HI= 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=DwbslE1I; 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="DwbslE1I" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770863129; 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=4xykWdQM5vJ3oEKsCmEuM4BEcEsG5qojr85PS8jMpA0=; b=DwbslE1I/dSvSVE8u7R4rUybAO75g1OiZBGSI/OWDxtS3DdkciB2sVKEV/S63zMQj+s6JZ jAsv6RnrhdNFzS4Is5tubiTJQ1US9wKC3cT/OElKefQ/rMT/PGJcAj1YW+Sxn6849ra+UP Jj7nUZUU9hGVy1ssAPw0xB+Q78qu2DQ= 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-679-bc-JOtfSPI64Pqxd3fMi5A-1; Wed, 11 Feb 2026 21:25:25 -0500 X-MC-Unique: bc-JOtfSPI64Pqxd3fMi5A-1 X-Mimecast-MFC-AGG-ID: bc-JOtfSPI64Pqxd3fMi5A_1770863124 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 67B1B1955F1D; Thu, 12 Feb 2026 02:25:24 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.80.91]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 68BE9300DDA4; Thu, 12 Feb 2026 02:25:14 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: aarcange@redhat.com, akpm@linux-foundation.org, anshuman.khandual@arm.com, apopple@nvidia.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, byungchul@sk.com, catalin.marinas@arm.com, cl@gentwo.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jackmanb@google.com, jack@suse.cz, jannh@google.com, jglisse@google.com, joshua.hahnjy@gmail.com, kas@kernel.org, lance.yang@linux.dev, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, mathieu.desnoyers@efficios.com, matthew.brost@intel.com, mhiramat@kernel.org, mhocko@suse.com, npache@redhat.com, peterx@redhat.com, pfalcato@suse.de, rakie.kim@sk.com, raquini@redhat.com, rdunlap@infradead.org, richard.weiyang@gmail.com, rientjes@google.com, rostedt@goodmis.org, rppt@kernel.org, ryan.roberts@arm.com, shivankg@amd.com, sunnanyong@huawei.com, surenb@google.com, thomas.hellstrom@linux.intel.com, tiwai@suse.de, usamaarif642@gmail.com, vbabka@suse.cz, vishal.moola@gmail.com, wangkefeng.wang@huawei.com, will@kernel.org, willy@infradead.org, yang@os.amperecomputing.com, ying.huang@linux.alibaba.com, ziy@nvidia.com, zokeefe@google.com Subject: [PATCH mm-unstable v1 5/5] mm/khugepaged: unify khugepaged and madv_collapse with collapse_single_pmd() Date: Wed, 11 Feb 2026 19:25:12 -0700 Message-ID: <20260212022512.19076-1-npache@redhat.com> In-Reply-To: <20260212021835.17755-1-npache@redhat.com> References: <20260212021835.17755-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.4 Content-Type: text/plain; charset="utf-8" The khugepaged daemon and madvise_collapse have two different implementations that do almost the same thing. Create collapse_single_pmd to increase code reuse and create an entry point to these two users. Refactor madvise_collapse and collapse_scan_mm_slot to use the new collapse_single_pmd function. This introduces a minor behavioral change that is most likely an undiscovered bug. The current implementation of khugepaged tests collapse_test_exit_or_disable before calling collapse_pte_mapped_thp, but we weren't doing it in the madvise_collapse case. By unifying these two callers madvise_collapse now also performs this check. We also modify the return value to be SCAN_ANY_PROCESS which properly indicates that this process is no longer valid to operate on. We also guard the khugepaged_pages_collapsed variable to ensure its only incremented for khugepaged. Reviewed-by: Lorenzo Stoakes Signed-off-by: Nico Pache Acked-by: Pedro Falcato Reviewed-by: Zi Yan --- mm/khugepaged.c | 121 ++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 55 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index fa41480f6948..0839a781bedd 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2395,6 +2395,62 @@ static enum scan_result collapse_scan_file(struct mm= _struct *mm, unsigned long a return result; } =20 +/* + * Try to collapse a single PMD starting at a PMD aligned addr, and return + * the results. + */ +static enum scan_result collapse_single_pmd(unsigned long addr, + struct vm_area_struct *vma, bool *mmap_locked, + struct collapse_control *cc) +{ + struct mm_struct *mm =3D vma->vm_mm; + enum scan_result result; + struct file *file; + pgoff_t pgoff; + + if (vma_is_anonymous(vma)) { + result =3D collapse_scan_pmd(mm, vma, addr, mmap_locked, cc); + goto end; + } + + file =3D get_file(vma->vm_file); + pgoff =3D linear_page_index(vma, addr); + + mmap_read_unlock(mm); + *mmap_locked =3D false; + result =3D collapse_scan_file(mm, addr, file, pgoff, cc); + + if (!cc->is_khugepaged && result =3D=3D SCAN_PAGE_DIRTY_OR_WRITEBACK && + mapping_can_writeback(file->f_mapping)) { + const loff_t lstart =3D (loff_t)pgoff << PAGE_SHIFT; + const loff_t lend =3D lstart + HPAGE_PMD_SIZE - 1; + + filemap_write_and_wait_range(file->f_mapping, lstart, lend); + } + fput(file); + + if (result !=3D SCAN_PTE_MAPPED_HUGEPAGE) + goto end; + + mmap_read_lock(mm); + *mmap_locked =3D true; + if (collapse_test_exit_or_disable(mm)) { + mmap_read_unlock(mm); + *mmap_locked =3D false; + return SCAN_ANY_PROCESS; + } + result =3D try_collapse_pte_mapped_thp(mm, addr, !cc->is_khugepaged); + if (result =3D=3D SCAN_PMD_MAPPED) + result =3D SCAN_SUCCEED; + mmap_read_unlock(mm); + *mmap_locked =3D false; + +end: + if (cc->is_khugepaged && result =3D=3D SCAN_SUCCEED) + ++khugepaged_pages_collapsed; + return result; +} + static unsigned int collapse_scan_mm_slot(unsigned int pages, enum scan_re= sult *result, struct collapse_control *cc) __releases(&khugepaged_mm_lock) @@ -2466,34 +2522,9 @@ static unsigned int collapse_scan_mm_slot(unsigned i= nt pages, enum scan_result * VM_BUG_ON(khugepaged_scan.address < hstart || khugepaged_scan.address + HPAGE_PMD_SIZE > hend); - if (!vma_is_anonymous(vma)) { - struct file *file =3D get_file(vma->vm_file); - pgoff_t pgoff =3D linear_page_index(vma, - khugepaged_scan.address); - - mmap_read_unlock(mm); - mmap_locked =3D false; - *result =3D collapse_scan_file(mm, - khugepaged_scan.address, file, pgoff, cc); - fput(file); - if (*result =3D=3D SCAN_PTE_MAPPED_HUGEPAGE) { - mmap_read_lock(mm); - if (collapse_test_exit_or_disable(mm)) - goto breakouterloop; - *result =3D try_collapse_pte_mapped_thp(mm, - khugepaged_scan.address, false); - if (*result =3D=3D SCAN_PMD_MAPPED) - *result =3D SCAN_SUCCEED; - mmap_read_unlock(mm); - } - } else { - *result =3D collapse_scan_pmd(mm, vma, - khugepaged_scan.address, &mmap_locked, cc); - } - - if (*result =3D=3D SCAN_SUCCEED) - ++khugepaged_pages_collapsed; =20 + *result =3D collapse_single_pmd(khugepaged_scan.address, + vma, &mmap_locked, cc); /* move to next address */ khugepaged_scan.address +=3D HPAGE_PMD_SIZE; progress +=3D HPAGE_PMD_NR; @@ -2799,6 +2830,7 @@ int madvise_collapse(struct vm_area_struct *vma, unsi= gned long start, cond_resched(); mmap_read_lock(mm); mmap_locked =3D true; + *lock_dropped =3D true; result =3D hugepage_vma_revalidate(mm, addr, false, &vma, cc); if (result !=3D SCAN_SUCCEED) { @@ -2809,46 +2841,25 @@ int madvise_collapse(struct vm_area_struct *vma, un= signed long start, hend =3D min(hend, vma->vm_end & HPAGE_PMD_MASK); } mmap_assert_locked(mm); - if (!vma_is_anonymous(vma)) { - struct file *file =3D get_file(vma->vm_file); - pgoff_t pgoff =3D linear_page_index(vma, addr); =20 - mmap_read_unlock(mm); - mmap_locked =3D false; - *lock_dropped =3D true; - result =3D collapse_scan_file(mm, addr, file, pgoff, cc); - - if (result =3D=3D SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb && - mapping_can_writeback(file->f_mapping)) { - loff_t lstart =3D (loff_t)pgoff << PAGE_SHIFT; - loff_t lend =3D lstart + HPAGE_PMD_SIZE - 1; + result =3D collapse_single_pmd(addr, vma, &mmap_locked, cc); =20 - filemap_write_and_wait_range(file->f_mapping, lstart, lend); - triggered_wb =3D true; - fput(file); - goto retry; - } - fput(file); - } else { - result =3D collapse_scan_pmd(mm, vma, addr, &mmap_locked, cc); - } if (!mmap_locked) *lock_dropped =3D true; =20 -handle_result: + if (result =3D=3D SCAN_PAGE_DIRTY_OR_WRITEBACK && !triggered_wb) { + triggered_wb =3D true; + goto retry; + } + switch (result) { case SCAN_SUCCEED: case SCAN_PMD_MAPPED: ++thps; break; - case SCAN_PTE_MAPPED_HUGEPAGE: - BUG_ON(mmap_locked); - mmap_read_lock(mm); - result =3D try_collapse_pte_mapped_thp(mm, addr, true); - mmap_read_unlock(mm); - goto handle_result; /* Whitelisted set of results where continuing OK */ case SCAN_NO_PTE_TABLE: + case SCAN_PTE_MAPPED_HUGEPAGE: case SCAN_PTE_NON_PRESENT: case SCAN_PTE_UFFD_WP: case SCAN_LACK_REFERENCED_PAGE: --=20 2.53.0