mm/damon: add mTHP collapse and split actions
Subsystems (3)
v2 cover mm/damon: add mTHP collapse and split actions
Resend of v2 with the RFC tag restored (v1 was RFC PATCH, so v2 should be RFC PATCH v2).
This resend also includes fixes for issues identified during review of the earlier mis-sent PATCH v2 thread: uninitialized memory, TOCTOU races, BUILD_BUG guards, missing sysfs action name registration, and stack allocation overflow. The series has been re-tested on aarch64 (anonymous and file-backed THP split) and is checkpatch clean.
v1: https://lore.kernel.org/linux-mm/[email protected]/
Changes since v1
- Rename DAMOS_MTHP_SPLIT -> DAMOS_SPLIT for naming consistency with the existing actions (per SJ's review).
- Drop the per-scheme hot_threshold field. Hotness policy does not belong in the kernel; target selection now lives in user space and is expressed to DAMOS via the address filter (per SJ's review).
- Drop the v1 SPE debugfs patch entirely. debugfs is not the right interface for a feature, and the SPE profiler belongs in user space (see "User-space target selection" below). v2 is kernel mechanism
only: 5 patches.
- Decouple T1 (a lab observation) from T2 (the production issue), and correct the architecture claim: ptep_test_and_clear_young() skips the TLB flush on both x86_64 and arm64, so the blind spot is architecture-independent rather than arm64-only.
- Terminology: avoid "stale TLB". A valid TLB entry is doing its job; the point is only that it lets the CPU satisfy a translation without a page-table walk, so the Accessed bit cleared by DAMON is not re-set.
Background
Two effects degrade DAMON's PTE-Accessed-bit (AF) signal once THP is in play. Both are described here as motivation only; this series does not change the AF monitoring path.
T2 -- PMD-granularity inflation (production issue)
A 2MB THP is tracked by a single PMD-level Accessed bit. One access to any 4KB sub-page sets the AF for the whole 2MB, so DAMON reports the entire THP as hot and cannot distinguish a genuinely hot 2MB region from a 2MB region with a single hot 4KB page. Cold memory hides inside "hot" THPs, and access-driven pageout/migration becomes coarse.
This is the workload that drove the work: Sangfor's Kunpeng 920 KVM hosts running Oracle. ARM SPE sampling of that workload shows 94.6% of THPs have fewer than 10% of their sub-pages actually accessed.
T1 -- TLB-reach blind spot (lab observation)
When the working set fits within L2 TLB reach (measured at 2048 entries x 2MB = 4GB on Kunpeng 920; no public data available), the CPU satisfies translations entirely from the TLB, preventing translation table walks. Because ptep_test_and_clear_young() does not flush
the TLB, valid TLB entries continue to satisfy translations and the AF that DAMON cleared is never re-set, so DAMON sees nr_accesses=0 for memory that is in fact hot, and no scheme triggers. This reproduces in the lab with small workloads; it is not something we have seen reported from production, where working sets exceed TLB reach.
What this series adds
Rather than change AF monitoring, this series adds two order-aware DAMOS actions so a policy layer can act at mTHP granularity:
- DAMOS_COLLAPSE + target_order (patches 1-3): collapse small folios up to a chosen mTHP order. Patch 1 adds the target_order field and its sysfs file; patch 2 exports a khugepaged helper (damon_collapse_folio_range()); patch 3 wires the vaddr handler.
- DAMOS_SPLIT + target_order (patches 4-5): split large folios down to a chosen mTHP order via split_folio_to_order(), for both anonymous and file-backed (tmpfs/shmem) folios.
The two are complementary, not competing:
THP=never + DAMOS_COLLAPSE: start at 4KB, grow hot regions up.
THP=always + DAMOS_SPLIT: start at 2MB, shrink cold regions down.
This dual-path design aligns with ideas discussed with Asier Gutierrez; we plan to unify our mTHP automation and evaluation roadmaps under this standard DAMOS_SPLIT action.
A deployment can pick either baseline, or run both, and let DAMOS manage the placement. THP is still wanted for the hot working set (fewer TLB misses, shallower walks); the goal is not "no THP" but "THP where it is hot, small pages where it is cold."
User-space target selection
The decision of *which* regions to collapse or split is left to user space and fed to DAMOS through the existing DAMOS address filter (DAMOS_FILTER_TYPE_ADDR) -- the interface suggested during v1 review. The kernel provides the mechanism; user space provides the policy, consistent with the perf/BPF "kernel samples, user space decides" model and with the DAMON-X direction.
Because the AF signal is unreliable at PMD granularity (T1/T2), the scheme is run with min_nr_accesses=0 so it does not gate on access count, and the address filter selects targets. min_nr_accesses=0 is also what unblocks the T1 case, where nr_accesses is pinned at 0.
Why not just turn khugepaged off? You can, but khugepaged is global and usually left enabled because other workloads rely on it; it cannot be disabled per region. DAMOS_COLLAPSE gives per-region, access-pattern-driven collapse -- a more precise, targeted complement to khugepaged's global scan, not a replacement for it. To handle the runtime race where khugepaged might aggressively re-collapse what DAMOS_SPLIT just split, we are evaluating a precise VMA-level handshake or back-off mechanism to prevent ping-pong effects in mixed environments.
Two user-space data sources produce the candidate address ranges:
1. ARM SPE (ARMv8.2+): perf record (SPE) -> per-2MB hot-fraction histogram -> PA->VA via /proc/<pid>/pagemap -> sparse-THP VA ranges. SPE reads physical addresses from the CPU pipeline, bypassing the TLB and page tables, so it is immune to T1 and T2.
2. smaps fallback (no SPE): scan /proc/<pid>/smaps for THP-backed VMAs and treat the 2MB-aligned ranges as split candidates.
The SPE profiler stays in user space deliberately: the SPE PMU is a single-consumer resource, so a kernel consumer would lock out user-space perf and tooling (x86 PEBS / AMD IBS have the same property). Keeping it in user space avoids that and keeps the metric source pluggable, in line with DAMON-X. This is why v2 drops the v1 SPE debugfs patch.
Testing
Tested on aarch64 with this series applied to 7.1.0-rc5, THP=always, using a DAMOS_SPLIT scheme (target_order=2, min_nr_accesses=0) and a single DAMOS address filter selecting one 2MB-aligned range:
- Anonymous THP: the filter splits exactly that one THP -- sz_applied=2MB and AnonHugePages drops by 2MB, the rest of the 256MB mapping untouched.
- File-backed THP (tmpfs/shmem mounted huge=always): the same setup splits exactly one 2MB shmem THP -- sz_applied=2MB and ShmemPmdMapped drops by 2MB. This confirms split_folio_to_order() works for shmem folios (the KVM-guest-on-THP-tmpfs case).
- The address filter is what bounds the action: sz_tried covers the whole ~2GB monitored region while sz_applied is exactly the 2MB the filter selected.
- A smaps-based path (for hosts without SPE) enumerates THP-backed ranges and splits all THP in the target workload.
- checkpatch clean on all 5 patches.
Test scripts and SPE-to-DAMON pipeline tools:
https://github.com/lianux-mm/damon_spe/tree/v2
Lian Wang (5):
mm/damon: add target_order field for DAMOS_COLLAPSE mm/khugepaged: add damon_collapse_folio_range() for external callers mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler mm/damon: introduce DAMOS_SPLIT action
mm/damon/vaddr: implement DAMOS_SPLIT handler
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)
--
2.50.1 (Apple Git-155)
RFC PATCH v2 1/5 mm/damon: add target_order field for DAMOS_COLLAPSE
DAMOS_COLLAPSE currently collapses into PMD-size THP only. Add a target_order field to express per-order mTHP collapse intent. Zero means system default (PMD order, same as current behavior). Valid values are 0 and 2..HPAGE_PMD_ORDER.
Wire up the sysfs interface: a per-scheme rw file "target_order". Validate at store time that the value is in range, and warn at scheme creation time if DAMOS_COLLAPSE is used with an unsupported non-PMD order, resetting to 0.
The actual mTHP application via the khugepaged wrapper will be added in subsequent patches.
--- include/linux/damon.h | 5 ++++ mm/damon/core.c | 2 ++ mm/damon/sysfs-schemes.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+)
include/linux/damon.h +5 -0
@@ -572,6 +572,11 @@ struct damos_migrate_dests {struct damos {struct damos_access_pattern pattern;enum damos_action action; +/* +* @target_order: target order for mTHP actions (DAMOS_COLLAPSE). +* 0 means system default (PMD order). Valid: 0, 2..HPAGE_PMD_ORDER. +*/ +unsigned int target_order;unsigned long apply_interval_us;/* private: internal use only */ /*
mm/damon/core.c +2 -0
@@ -579,6 +579,7 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern, scheme->migrate_dests = (struct damos_migrate_dests ){}; scheme->target_nid = target_nid; + scheme->target_order =0 ;return scheme; }
@@ -1278,6 +1279,7 @@ static int damos_commit(struct damos *dst, struct damos *src) dst->wmarks = src->wmarks; dst->target_nid = src->target_nid; + dst->target_order = src->target_order; err =damos_commit_dests (&dst->migrate_dests, &src->migrate_dests);if (err)
mm/damon/sysfs-schemes.c +61 -0
108 lines not on this page · raw message
[email protected]
AI reviewRe: [RESEND RFC PATCH v2 1/5] mm/damon: add target_order field for DAMOS_COLLAPSE
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Double-read of `sysfs_scheme->action` allows a concurrent sysfs write to bypass the…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Double-read of sysfs_scheme->action allows a concurrent sysfs write to bypass the target_order validation. --
commit 8f068b9d900dccbf38f0673e711831fdf8cce5dc
Author: Lian Wang <[email protected]>
mm/damon: add target_order field for DAMOS_COLLAPSE
This commit adds a target_order field to the DAMOS scheme structure to support per-order mTHP collapse operations. It also exposes this new parameter via sysfs with basic validation.
Quoted 4 lines
mm/damon/sysfs-schemes.c +0
-0
[ ... ]
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3058
Quoted 8 lines
mm/damon/sysfs-schemes.c +4
-0@@ -3010,6 +3052,25 @@ static struct damos *damon_sysfs_mk_scheme(if (!scheme)return NULL ; + target_order = sysfs_scheme->target_order; + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (sysfs_scheme->action == DAMOS_COLLAPSE &&
[Severity: Medium]
Is there a time-of-check to time-of-use (TOCTOU) race condition here?
The scheme action is read earlier and passed into damon_new_scheme(), which saves it to the newly allocated scheme:
scheme = damon_new_scheme(&pattern, sysfs_scheme->action, ...);
Then it is read again here directly from sysfs_scheme->action for the target_order validation.
If a concurrent sysfs write modifies the action parameter after damon_new_scheme() but before this validation, could it bypass the check and allow the scheme to be created with an invalid target_order?
Would it be safer to check the snapshotted scheme->action instead?
Quoted 18 lines
mm/damon/sysfs-schemes.c +15
-0+ target_order != 0 && + target_order != HPAGE_PMD_ORDER) { +pr_warn ("DAMON collapse: target_order %u not supported, only PMD order (%u) is available. Use 0 or %u.\n" , + target_order, +HPAGE_PMD_ORDER ,HPAGE_PMD_ORDER ); + target_order =0 ; + } +#else +if (sysfs_scheme->action ==DAMOS_COLLAPSE && target_order !=0 ) { +pr_warn ("DAMON collapse: target_order not supported without THP. Use 0.\n" ); + target_order =0 ; + } +#endif + scheme->target_order = target_order; + err =damos_sysfs_add_quota_score (sysfs_quotas->goals, &scheme->quota);if (err) {damon_destroy_scheme (scheme);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SJ Park
maintainerRe: [RESEND RFC PATCH v2 1/5] mm/damon: add target_order field for DAMOS_COLLAPSE
In reply to Lian WangQuoted 6 lines · On Thu, 2 Jul 2026 17:46:29 +0800 Lian Wang <[email protected]> wrote:
DAMOS_COLLAPSEcurrently collapses into PMD-size THP only. Add atarget_orderfield to express per-order mTHP collapse intent. Zero means system default (PMD order, same as current behavior). Valid values are 0 and 2..HPAGE_PMD_ORDER.Wire up the sysfs interface: a per-scheme rw file "
target_order".
Unless there is a name conflict, let's just call it "order".
Quoted 3 lines
Validate at store time that the value is in range, and warn at scheme creation time if
DAMOS_COLLAPSEis used with an unsupported non-PMD order, resetting to 0.
I'm working [1] on centralize the user parameter validations into core layer. It is fine to have this for now. But once the core layer validation is merged into mm-new, let's use it for the validation. That is, just let the user writes invalidate target order. But eventual attempt to use that via damon_start() or damon_commit() will fail.
Quoted 8 lines
The actual mTHP application via the khugepaged wrapper will be added in subsequent patches.
Co-developed-by Kunwu Chan <[email protected]>Signed-off-by Kunwu Chan <[email protected]>Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>
Having two Signed-off-by: looks odd. If you want to give a credit to your employer, you can add that to your signature, like:
That's a de-facto standard in mm community.
Quoted 11 lines
--- include/linux/damon.h | 5 ++++ mm/damon/core.c | 2 ++ mm/damon/sysfs-schemes.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+)include/linux/damon.h +0
-0
include/linux/damon.h struct damos_migrate_dests { · line 580
Quoted 9 lines
include/linux/damon.h +5
-0@@ -572,6 +572,11 @@ struct damos_migrate_dests {struct damos {struct damos_access_pattern pattern;enum damos_action action; +/* +* @target_order: target order for mTHP actions (DAMOS_COLLAPSE). +* 0 means system default (PMD order). Valid: 0, 2..HPAGE_PMD_ORDER. +*/ +unsigned int target_order;unsigned long apply_interval_us;
Let's rename to 'order'. And put it in the union having 'target_nid' and 'migrate_dests'.
Quoted 37 lines
include/linux/damon.h +0
-0/* private: internal use only */ /* mm/damon/core.c +2
-0@@ -579,6 +579,7 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern, scheme->migrate_dests = (struct damos_migrate_dests ){}; scheme->target_nid = target_nid; + scheme->target_order =0 ;return scheme; }@@ -1278,6 +1279,7 @@ static int damos_commit(struct damos *dst, struct damos *src) dst->wmarks = src->wmarks; dst->target_nid = src->target_nid; + dst->target_order = src->target_order; err =damos_commit_dests (&dst->migrate_dests, &src->migrate_dests);if (err)mm/damon/sysfs-schemes.c +2
-0@@ -6,7 +6,9 @@*/ #include <linux/slab.h> +#include <linux/mm.h> #include <linux/numa.h> +#include <linux/huge_mm.h> #include "sysfs-common.h"
mm/damon/sysfs-schemes.c struct damon_sysfs_scheme { · line 2262
Quoted 4 lines
mm/damon/sysfs-schemes.c +1
-0@@ -2257,6 +2259,7 @@ struct damon_sysfs_scheme {struct damon_sysfs_stats *stats;struct damon_sysfs_scheme_regions *tried_regions;int target_nid; +unsigned int target_order;
Let's rename to 'order' if there is no problem. Same for below.
Quoted 33 lines
mm/damon/sysfs-schemes.c +19
-0struct damos_sysfs_dests *dests; };@@ -2323,6 +2326,7 @@ static struct damon_sysfs_scheme *damon_sysfs_scheme_alloc( scheme->action = action; scheme->apply_interval_us = apply_interval_us; scheme->target_nid =NUMA_NO_NODE ; + scheme->target_order =0 ;return scheme; }@@ -2642,6 +2646,39 @@ static ssize_t target_nid_store(struct kobject *kobj,return err ? err : count; } +static ssize_t target_order_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +struct damon_sysfs_scheme *scheme =container_of (kobj, + struct damon_sysfs_scheme, kobj); + +return sysfs_emit (buf,"%u\n" , scheme->target_order); +} + +static ssize_t target_order_store (struct kobject *kobj, +struct kobj_attribute *attr,const char *buf,size_t count) +{ +struct damon_sysfs_scheme *scheme =container_of (kobj, + struct damon_sysfs_scheme, kobj); +unsigned int val; +int err; + + err =kstrtouint (buf,0 , &val);
mm/damon/sysfs-schemes.c static ssize_t target_nid_store(struct kobject *kobj, · line 2676
Quoted 10 lines
mm/damon/sysfs-schemes.c +10
-0+if (err) +return err; + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (val !=0 && (val <2 || val >HPAGE_PMD_ORDER )) +return -EINVAL ; +#else +if (val !=0 ) +return -EINVAL ; +#endif
As I mentioned above, this should eventually removed in lieu of core layer parameters validation.
Quoted 36 lines
mm/damon/sysfs-schemes.c +11
-0+ + scheme->target_order = val; +return count; +} +static void damon_sysfs_scheme_release (struct kobject *kobj) {kfree (container_of(kobj ,struct damon_sysfs_scheme ,kobj ));@@ -2656,10 +2693,14 @@ static struct kobj_attribute damon_sysfs_scheme_apply_interval_us_attr =static struct kobj_attribute damon_sysfs_scheme_target_nid_attr =__ATTR_RW_MODE (target_nid,0600 ); +static struct kobj_attribute damon_sysfs_scheme_target_order_attr = +__ATTR_RW_MODE (target_order,0600 ); +static struct attribute *damon_sysfs_scheme_attrs[] = { &damon_sysfs_scheme_action_attr.attr, &damon_sysfs_scheme_apply_interval_us_attr.attr, &damon_sysfs_scheme_target_nid_attr.attr, + &damon_sysfs_scheme_target_order_attr.attr,NULL , };ATTRIBUTE_GROUPS (damon_sysfs_scheme);@@ -2970,6 +3011,7 @@ static struct damos *damon_sysfs_mk_scheme(struct damon_sysfs_weights *sysfs_weights = sysfs_quotas->weights;struct damon_sysfs_watermarks *sysfs_wmarks = sysfs_scheme->watermarks;struct damos *scheme; +unsigned int target_order;int err;struct damos_access_pattern pattern = {@@ -3005,6 +3047,25 @@ static struct damos *damon_sysfs_mk_scheme(if (!scheme)return NULL ; + target_order = sysfs_scheme->target_order;
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3060
Quoted 10 lines
mm/damon/sysfs-schemes.c +10
-0+ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +if (sysfs_scheme->action ==DAMOS_COLLAPSE && + target_order !=0 && + target_order !=HPAGE_PMD_ORDER ) { +pr_warn ("DAMON collapse: target_order %u not supported, only PMD order (%u) is available. Use 0 or %u.\n" , + target_order, +HPAGE_PMD_ORDER ,HPAGE_PMD_ORDER ); + target_order =0 ; + }
Ditto. This validation should eventually be removed.
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3071
Quoted 11 lines
mm/damon/sysfs-schemes.c +8
-0+#else +if (sysfs_scheme->action ==DAMOS_COLLAPSE && target_order !=0 ) { +pr_warn ("DAMON collapse: target_order not supported without THP. Use 0.\n" ); + target_order =0 ; + } +#endif + scheme->target_order = target_order; + err =damos_sysfs_add_quota_score (sysfs_quotas->goals, &scheme->quota);if (err) {damon_destroy_scheme (scheme);
Overall, looks good.
[1] https://lore.kerneel.org/[email protected]
Thanks,
SJ
[...]
RFC PATCH v2 2/5 mm/khugepaged: add damon_collapse_folio_range() for external callers
In reply to Lian WangExport a thin wrapper around collapse_huge_page() that allows external subsystems such as DAMON to trigger THP collapse on a target address range.
Currently restricted to PMD order (HPAGE_PMD_ORDER), since collapse_huge_page() does not yet support arbitrary mTHP orders. The restriction can be relaxed when khugepaged gains mTHP support.
The caller must hold a reference to @mm. Do not hold mmap lock:collapse_huge_page() acquires mmap_read_lock for validation, releases it, then acquires mmap_write_lock for the actual collapse. Holding an outer mmap_read_lock would cause a self-deadlock when the same thread attempts the inner mmap_write_lock.
--- include/linux/khugepaged.h | 9 ++++++++ mm/khugepaged.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
include/linux/khugepaged.h +9 -0
@@ -20,6 +20,9 @@ extern bool current_is_khugepaged(void);void collapse_pte_mapped_thp (struct mm_struct *mm,unsigned long addr,bool install_pmd); +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order); +static inline void khugepaged_fork (struct mm_struct *mm,struct mm_struct *oldmm) {if (mm_flags_test (MMF_VM_HUGEPAGE , oldmm))
@@ -47,6 +50,12 @@ static inline void collapse_pte_mapped_thp(struct mm_struct *mm, { } +static inline int damon_collapse_folio_range (struct mm_struct *mm, +unsigned long start_addr,unsigned int target_order) +{ +return -EINVAL ; +} +static inline void khugepaged_min_free_kbytes_update (void ) { }
mm/khugepaged.c +46 -0
49 lines not on this page · raw message
[email protected]
AI reviewRe: [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Uninitialized memory in `struct collapse_control` causes undefined behavior and random NUMA node…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Uninitialized memory in struct collapse_control causes undefined behavior and random NUMA node constraints during THP allocation.
- [High] Calling lru_add_drain_all() for every PMD creates severe system-wide latency spikes and IPI storms.
- [High] Directly calling collapse_huge_page() bypasses PTE pre-scanning and swap-in, causing unconditional THP allocations and breaking file-backed THP support. --
commit 03af0641474bb6262efed77c042c17e3c94d4f27
Author: Lian Wang <[email protected]>
mm/khugepaged: add damon_collapse_folio_range() for external callers
Adds a thin wrapper around collapse_huge_page() to allow external subsystems like DAMON to trigger THP collapse on a target address range.
Quoted 4 lines
mm/khugepaged.c +0
-0
[ ... ]
Quoted 26 lines
mm/khugepaged.c +22
-0@@ -3272,3 +3272,49 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,return thps == ((hend - hstart) >>HPAGE_PMD_SHIFT ) ?0 :madvise_collapse_errno (last_fail); } + +/** +* damon_collapse_folio_range() - Collapse base pages in range into a THP +* @mm: mm_struct of the target process +* @start_addr: start address (must be order-aligned) +* @target_order: page order of the collapse result (currently only +* HPAGE_PMD_ORDER is supported) +* +* Thin wrapper around collapse_huge_page() for external callers such as +* DAMON. The caller must hold a reference to @mm. Do not hold mmap +* lock: collapse_huge_page() acquires mmap_read_lock for validation, +* releases it, then acquires mmap_write_lock for the collapse. Holding +* an outer mmap_read_lock would self-deadlock. +* +* Return: 0 on success, -EINVAL on bad arguments, negative error from +* madvise_collapse_errno() otherwise. +*/ +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order) +{ +struct collapse_control *cc; +enum scan_result result;
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3306
Quoted 10 lines
mm/khugepaged.c +10
-0+ +if (target_order !=HPAGE_PMD_ORDER ) { +pr_warn_once ("%s: only PMD order (%u) is supported, got %u\n" , + __func__,HPAGE_PMD_ORDER , target_order); +return -EINVAL ; + } +if (start_addr & ((PAGE_SIZE << target_order) -1 )) +return -EINVAL ; + + cc =kmalloc_obj (*cc);
[Severity: High]
Does this leave the cc->node_load array and cc->alloc_nmask nodemask uninitialized?
Since this code bypasses collapse_scan_pmd() which typically zeros these fields, collapse_huge_page() will call alloc_charge_folio() and then collapse_find_target_node().
In collapse_find_target_node(), the code reads cc->node_load[nid] to find the maximum value and sets bits in cc->alloc_nmask. Passing a garbage nodemask to __folio_alloc() could cause undefined behavior and random NUMA node constraints during allocation.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3312
Quoted 6 lines
mm/khugepaged.c +6
-0+if (!cc) +return -ENOMEM ; + cc->is_khugepaged = false; + cc->progress =0 ; + +lru_add_drain_all ();
[Severity: High]
Can this cause system-wide latency spikes and IPI storms?
Because damon_collapse_folio_range() is called for every 2MB chunk by DAMON iteration logic, unconditionally invoking the heavy cross-CPU lru_add_drain_all() here means it will execute for every single PMD in the region.
Unlike madvise_collapse() which hoists this drain out of its scanning loop, placing it inside the per-PMD API might severely impact system performance.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3314
Quoted 2 lines
mm/khugepaged.c +2
-0+ + result =collapse_huge_page (mm, start_addr,1 ,0 , cc, target_order);
[Severity: High]
Does this direct call bypass PTE pre-scanning and swap-in logic?
By skipping collapse_scan_pmd() and directly invoking collapse_huge_page(), this forces an unconditional allocation via alloc_charge_folio() for every chunk evaluated, before even checking if the PMD is collapsible.
Also, passing 0 for the unmapped parameter skips the swap-in step, meaning any swapped-out pages in the target region will cause the isolation phase to abort.
Furthermore, hugepage_vma_revalidate() inside collapse_huge_page() hardcodes expect_anon=true, so this path will silently fail for any file-backed memory.
Quoted 6 lines
mm/khugepaged.c +6
-0+kfree (cc); +if (result ==SCAN_SUCCEED || result ==SCAN_PMD_MAPPED ) +return 0 ; +return madvise_collapse_errno (result); +} +EXPORT_SYMBOL_GPL (damon_collapse_folio_range);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
In reply to Lian Wang(+cc missing people again)
Sorry but we're not going to accept anything that exports THP logic like this at all.
And a damon wrapper in core mm code is just a non-starter, so you really need to rethink your approach.
I think SJ already commented on this in your v1 from what I can see? I'd listen to his advice on this :)
Quoted 14 lines · Lian Wang
Export a thin wrapper around
collapse_huge_page()that allows external subsystems such as DAMON to trigger THP collapse on a target address range.Currently restricted to PMD order (
HPAGE_PMD_ORDER), sincecollapse_huge_page()does not yet support arbitrary mTHP orders. The restriction can be relaxed when khugepaged gains mTHP support.The caller must hold a reference to @mm. Do not hold mmap lock:
collapse_huge_page()acquiresmmap_read_lockfor validation, releases it, then acquiresmmap_write_lockfor the actual collapse. Holding an outermmap_read_lockwould cause a self-deadlock when the same thread attempts the innermmap_write_lock.
Quoted 4 lines
Co-developed-by Kunwu Chan <[email protected]>Signed-off-by Kunwu Chan <[email protected]>Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>
This patch is exporting internal mm logic without proper safeguards so it's just not something we're going to accept, sorry.
(Also not sure it's correct to have multiple S-o-b for the same person (unless re-tagging a backport or something)? I'm not sure though)
Quoted 10 lines
--- include/linux/khugepaged.h | 9 ++++++++ mm/khugepaged.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)include/linux/khugepaged.h +0
-0
include/linux/khugepaged.h extern bool current_is_khugepaged(void); · line 24
Quoted 5 lines
include/linux/khugepaged.h +2
-0@@ -20,6 +20,9 @@ extern bool current_is_khugepaged(void);void collapse_pte_mapped_thp (struct mm_struct *mm,unsigned long addr,bool install_pmd); +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order);
No thanks. We're not putting damon wrappers into core code. This is breaking the abstraction and letting arbitrary users invoke internal mm logic.
Plus you're literally exporting this so it can be abused by drivers.
Quoted 25 lines
include/linux/khugepaged.h +7
-0+static inline void khugepaged_fork (struct mm_struct *mm,struct mm_struct *oldmm) {if (mm_flags_test (MMF_VM_HUGEPAGE , oldmm))@@ -47,6 +50,12 @@ static inline void collapse_pte_mapped_thp(struct mm_struct *mm, { } +static inline int damon_collapse_folio_range (struct mm_struct *mm, +unsigned long start_addr,unsigned int target_order) +{ +return -EINVAL ; +} +static inline void khugepaged_min_free_kbytes_update (void ) { }mm/khugepaged.c +0
-0@@ -3272,3 +3272,49 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,return thps == ((hend - hstart) >>HPAGE_PMD_SHIFT ) ?0 :madvise_collapse_errno (last_fail); }
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3284
Quoted 10 lines
mm/khugepaged.c +10
-0+ +/** +* damon_collapse_folio_range() - Collapse base pages in range into a THP +* @mm: mm_struct of the target process +* @start_addr: start address (must be order-aligned) +* @target_order: page order of the collapse result (currently only +* HPAGE_PMD_ORDER is supported) +* +* Thin wrapper around collapse_huge_page() for external callers such as +* DAMON. The caller must hold a reference to @mm. Do not hold mmap
This is really fragile and bug bait.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3285
Quoted 1 line
mm/khugepaged.c +1
-0+* lock: collapse_huge_page() acquires mmap_read_lock for validation,
This is just gross, you're now collapsing based on an outdated concept of what the current VMA state is...
You're also losing literally everything that madvise_collapse() does.
AND you're overriding THP limitations like max_ptes_none, which is horrible...
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3287
Quoted 2 lines
mm/khugepaged.c +2
-0+* releases it, then acquires mmap_write_lock for the collapse. Holding +* an outer mmap_read_lock would self-deadlock.
This is a sign the interface is wrong!
Quoted 15 lines
mm/khugepaged.c +15
-0+* +* Return: 0 on success, -EINVAL on bad arguments, negative error from +* madvise_collapse_errno() otherwise. +*/ +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order) +{ +struct collapse_control *cc; +enum scan_result result; + +if (target_order !=HPAGE_PMD_ORDER ) { +pr_warn_once ("%s: only PMD order (%u) is supported, got %u\n" , + __func__,HPAGE_PMD_ORDER , target_order); +return -EINVAL ; + }
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3312
Quoted 10 lines
mm/khugepaged.c +10
-0+if (start_addr & ((PAGE_SIZE << target_order) -1 )) +return -EINVAL ; + + cc =kmalloc_obj (*cc); +if (!cc) +return -ENOMEM ; + cc->is_khugepaged = false; + cc->progress =0 ; + +lru_add_drain_all ();
This is quite literally a copy/paste from madvise_collapse(). No no no :) code duplication like this is also unacceptable.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3320
Quoted 8 lines
mm/khugepaged.c +8
-0+ + result =collapse_huge_page (mm, start_addr,1 ,0 , cc, target_order); +kfree (cc); +if (result ==SCAN_SUCCEED || result ==SCAN_PMD_MAPPED ) +return 0 ; +return madvise_collapse_errno (result); +} +EXPORT_SYMBOL_GPL (damon_collapse_folio_range);
We _definitely_ cannot have internal mm logic _exported_.
Yeah sorry you need to rethink this.
Thanks, Lorenzo
SJ Park
maintainerRe: [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
Quoted 1 line · On Thu, 2 Jul 2026 12:07:01 +0100 Lorenzo Stoakes <[email protected]> wrote:
(+cc missing people again)
Thank you for adding the recipients, and review, Lorenzo!
Quoted 9 lines
Sorry but we're not going to accept anything that exports THP logic like this at all.
And a damon wrapper in core mm code is just a non-starter, so you really need to rethink your approach.
I think SJ already commented on this in your v1 from what I can see? I'd listen to his advice on this :)
Lorenzo is right. Not disrupting the world outside of mm/damon/ is the first principle of DAMON development. Sometimes we may have to make some changes outside of mm/damon/, but we MUST make it not disruptive, small, and perfectly aligned with the developers of the area with respects.
Quoted 21 lines · Lian Wang
Quoted 14 lines
Export a thin wrapper around
collapse_huge_page()that allows external subsystems such as DAMON to trigger THP collapse on a target address range.Currently restricted to PMD order (
HPAGE_PMD_ORDER), sincecollapse_huge_page()does not yet support arbitrary mTHP orders. The restriction can be relaxed when khugepaged gains mTHP support.The caller must hold a reference to @mm. Do not hold mmap lock:
collapse_huge_page()acquiresmmap_read_lockfor validation, releases it, then acquiresmmap_write_lockfor the actual collapse. Holding an outermmap_read_lockwould cause a self-deadlock when the same thread attempts the innermmap_write_lock.Quoted 4 lines
Co-developed-by Kunwu Chan <[email protected]>Signed-off-by Kunwu Chan <[email protected]>Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>
Quoted 5 lines
This patch is exporting internal mm logic without proper safeguards so it's just not something we're going to accept, sorry.
(Also not sure it's correct to have multiple S-o-b for the same person (unless re-tagging a backport or something)? I'm not sure though)
I agree to Lorenzo.
As I replied [1] to patch 1, if you just want to give a credit to your employer, you could add your employer name on the single tag, like "Lian Wang (processssmission) <[email protected]>".
If it is for ensure the replies goes to not only your private inbox but also corporate email inbox, you can simply Cc: your corporate email address.
Quoted 14 lines
Quoted 14 lines
--- include/linux/khugepaged.h | 9 ++++++++ mm/khugepaged.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)include/linux/khugepaged.h +0
-0@@ -20,6 +20,9 @@ extern bool current_is_khugepaged(void);void collapse_pte_mapped_thp (struct mm_struct *mm,unsigned long addr,bool install_pmd);
Quoted 5 lines
include/linux/khugepaged.h extern bool current_is_khugepaged(void); · line 24
Quoted 2 lines
include/linux/khugepaged.h +2
-0+int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order);No thanks. We're not putting damon wrappers into core code. This is breaking the abstraction and letting arbitrary users invoke internal mm logic.
I agree. First of all, having something with 'damon_' prefix in khugepaged.h makes no sense. If we have to expose something, maybe mm/internal.h could be considered in my opinion. But for only DAMON usage, include/linux/khugepaged.h is definitely wrong.
Quoted 2 lines
Plus you're literally exporting this so it can be abused by drivers.
I was raising my eyebrows here. I show this patch adds EXPORT_SYMBOL_GPL() at the end. Lorenzo is completely right. I believe Lian added that only by a mistake.
Quoted 73 lines
Quoted 26 lines
include/linux/khugepaged.h +7
-0+static inline void khugepaged_fork (struct mm_struct *mm,struct mm_struct *oldmm) {if (mm_flags_test (MMF_VM_HUGEPAGE , oldmm))@@ -47,6 +50,12 @@ static inline void collapse_pte_mapped_thp(struct mm_struct *mm, { } +static inline int damon_collapse_folio_range (struct mm_struct *mm, +unsigned long start_addr,unsigned int target_order) +{ +return -EINVAL ; +} +static inline void khugepaged_min_free_kbytes_update (void ) { }mm/khugepaged.c +0
-0@@ -3272,3 +3272,49 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,return thps == ((hend - hstart) >>HPAGE_PMD_SHIFT ) ?0 :madvise_collapse_errno (last_fail); }mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3284
Quoted 10 lines
mm/khugepaged.c +10
-0+ +/** +* damon_collapse_folio_range() - Collapse base pages in range into a THP +* @mm: mm_struct of the target process +* @start_addr: start address (must be order-aligned) +* @target_order: page order of the collapse result (currently only +* HPAGE_PMD_ORDER is supported) +* +* Thin wrapper around collapse_huge_page() for external callers such as +* DAMON. The caller must hold a reference to @mm. Do not hold mmap This is really fragile and bug bait.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3285
Quoted 1 line
mm/khugepaged.c +1
-0+* lock: collapse_huge_page() acquires mmap_read_lock for validation, This is just gross, you're now collapsing based on an outdated concept of what the current VMA state is...
You're also losing literally everything that
madvise_collapse()does.AND you're overriding THP limitations like
max_ptes_none, which is horrible...mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3287
Quoted 2 lines
mm/khugepaged.c +2
-0+* releases it, then acquires mmap_write_lock for the collapse. Holding +* an outer mmap_read_lock would self-deadlock. This is a sign the interface is wrong!
Quoted 20 lines
mm/khugepaged.c +20
-0+* +* Return: 0 on success, -EINVAL on bad arguments, negative error from +* madvise_collapse_errno() otherwise. +*/ +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order) +{ +struct collapse_control *cc; +enum scan_result result; + +if (target_order !=HPAGE_PMD_ORDER ) { +pr_warn_once ("%s: only PMD order (%u) is supported, got %u\n" , + __func__,HPAGE_PMD_ORDER , target_order); +return -EINVAL ; + } +if (start_addr & ((PAGE_SIZE << target_order) -1 )) +return -EINVAL ; + + cc =kmalloc_obj (*cc); +if (!cc)
Quoted 8 lines
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3312
Quoted 5 lines
mm/khugepaged.c +5
-0+return -ENOMEM ; + cc->is_khugepaged = false; + cc->progress =0 ; + +lru_add_drain_all ();This is quite literally a copy/paste from
madvise_collapse(). No no no :) code duplication like this is also unacceptable.
Thank you for kind review, Lorenzo.
I believe Lorenzo's concerns are valid and should all be addressed before dropping RFC tag of this patch. Also, if you "have to" post the next revision before addressing those to get reviews of changes on other parts of this series, let's make sure the fact is very clearly described. In the way, we can avoid wasting time of reviewers. For example, you could add a note like below at the beginning of the next revision of this patch.
"""
NOTE to THP reviewers: This patch is not yet addressing problems that are found on the previous revision. This version is posted for review of only other parts in the series. Please ignore this patch for now.
"""
Quoted 11 lines
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3320
Quoted 9 lines
mm/khugepaged.c +8
-0+ + result =collapse_huge_page (mm, start_addr,1 ,0 , cc, target_order); +kfree (cc); +if (result ==SCAN_SUCCEED || result ==SCAN_PMD_MAPPED ) +return 0 ; +return madvise_collapse_errno (result); +} +EXPORT_SYMBOL_GPL (damon_collapse_folio_range);We _definitely_ cannot have internal mm logic _exported_.
I completely agree. Please drop this from the next version.
Quoted 2 lines
Yeah sorry you need to rethink this.
I wonder if we could extend madvise_collapse() for mTHP and use it.
I initially thought this is a silly idea, but 'Documentation/admin-guide/mm/transhuge.rst' is saying "currently", madvise_collapse only supports collapsing to PMD-sized THPs and does not attemp mTHP collapse. It reads to me like supporting mTHP from MADV_COLLAPSE is a possible TODO. And it makes sense to me. Users who do MADV_COLLAPSE may want it to at least have a best-effort mTHP. The interface of madvise_collapse() is already receiving arbitrary address range, so fit with DAMON's purpose.
If supporting mTHP with MADV_THP is not a real TODO but just a silly idea that better not to be implemented, I wonder if we can split out the core of madvise_collapse(), extend it for mTHP, and expose it on mm/internal.h.
I concern if the above idea makes call depth unnecessarily deep, though. A shallower approach would be renaming madvise_collapse() to somewhat general (I fail at making an example, but whatever without maddvise_ prefix) and extending it for mTHP support.
Off the topic of this patch, but I find 'madvise_collapse()' is declared in include/linux/huge_mm.h, while it is being used only inside mm/. Would it make sense to move the declaration to mm/internal.h?
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ
[...]
SJ Park
maintainerRe: [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
Quoted 13 lines · On Thu, 02 Jul 2026 12:43:59 -0700 SJ Park <[email protected]> wrote:
Quoted 1 line · On Thu, 2 Jul 2026 12:07:01 +0100 Lorenzo Stoakes <[email protected]> wrote:
(+cc missing people again)
Thank you for adding the recipients, and review, Lorenzo!
Quoted 7 lines
Sorry but we're not going to accept anything that exports THP logic like this at all.
And a damon wrapper in core mm code is just a non-starter, so you really need to rethink your approach.
Quoted 7 lines
Quoted 2 lines
I think SJ already commented on this in your v1 from what I can see? I'd listen to his advice on this :)
Lorenzo is right. Not disrupting the world outside of
mm/damon/is the first principle of DAMON development. Sometimes we may have to make some changes outside ofmm/damon/, but we MUST make it not disruptive, small, and perfectly aligned with the developers of the area with respects.
The best option is just not doing this. And that might be the case.
we already have pmd level DAMOS_COLLAPSE. I find mTHP-supporting DAMOS_SPLIT can be implemented without any changes on mm/damon/ external world. If it is true and there is no objection at doing that, mTHP collapse may not really necessary. That is, the users could collapse in pmd level first, and then split in desired mTHP level to accomplish their goal. I think that works for common use cases, too.
It would be suboptimal to collapse in pmd level first and then split. But the efficiency is unclear. I don't want to disrupt others for unclear gains. We can upstream split part first, measure the efficiency, and revisit mTHP collapse if it turns out to be really needed.
What do you think, Lian?
Thanks,
SJ
[...]
Re: [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
In reply to Lorenzo StoakesHi Lorenzo,
Quoted 152 lines · Lorenzo Stoakes
(+cc missing people again)
Sorry but we're not going to accept anything that exports THP logic like this at all.
And a damon wrapper in core mm code is just a non-starter, so you really need to rethink your approach.
I think SJ already commented on this in your v1 from what I can see? I'd listen to his advice on this :)
Quoted 14 lines · Lian Wang
Export a thin wrapper around
collapse_huge_page()that allows external subsystems such as DAMON to trigger THP collapse on a target address range.Currently restricted to PMD order (
HPAGE_PMD_ORDER), sincecollapse_huge_page()does not yet support arbitrary mTHP orders. The restriction can be relaxed when khugepaged gains mTHP support.The caller must hold a reference to @mm. Do not hold mmap lock:
collapse_huge_page()acquiresmmap_read_lockfor validation, releases it, then acquiresmmap_write_lockfor the actual collapse. Holding an outermmap_read_lockwould cause a self-deadlock when the same thread attempts the innermmap_write_lock.Quoted 4 lines
Co-developed-by Kunwu Chan <[email protected]>Signed-off-by Kunwu Chan <[email protected]>Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>This patch is exporting internal mm logic without proper safeguards so it's just not something we're going to accept, sorry.
(Also not sure it's correct to have multiple S-o-b for the same person (unless re-tagging a backport or something)? I'm not sure though)
Quoted 10 lines
---
include/linux/khugepaged.h| 9 ++++++++mm/khugepaged.c| 46 ++++++++++++++++++++++++++++++++++++++2 files changed, 55 insertions(+)include/linux/khugepaged.h +0
-0include/linux/khugepaged.h extern bool current_is_khugepaged(void); · line 24
Quoted 5 lines
include/linux/khugepaged.h +2
-0@@ -20,6 +20,9 @@ extern bool current_is_khugepaged(void);void collapse_pte_mapped_thp (struct mm_struct *mm,unsigned long addr,bool install_pmd); +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order);No thanks. We're not putting damon wrappers into core code. This is breaking the abstraction and letting arbitrary users invoke internal mm logic.
Plus you're literally exporting this so it can be abused by drivers.
Quoted 25 lines
include/linux/khugepaged.h +7
-0+static inline void khugepaged_fork (struct mm_struct *mm,struct mm_struct *oldmm) {if (mm_flags_test (MMF_VM_HUGEPAGE , oldmm))@@ -47,6 +50,12 @@ static inline void collapse_pte_mapped_thp(struct mm_struct *mm, { } +static inline int damon_collapse_folio_range (struct mm_struct *mm, +unsigned long start_addr,unsigned int target_order) +{ +return -EINVAL ; +} +static inline void khugepaged_min_free_kbytes_update (void ) { }mm/khugepaged.c +0
-0@@ -3272,3 +3272,49 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,return thps == ((hend - hstart) >>HPAGE_PMD_SHIFT ) ?0 :madvise_collapse_errno (last_fail); }mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3284
Quoted 10 lines
mm/khugepaged.c +10
-0+ +/** +* damon_collapse_folio_range() - Collapse base pages in range into a THP +* @mm: mm_struct of the target process +* @start_addr: start address (must be order-aligned) +* @target_order: page order of the collapse result (currently only +* HPAGE_PMD_ORDER is supported) +* +* Thin wrapper around collapse_huge_page() for external callers such as +* DAMON. The caller must hold a reference to @mm. Do not hold mmap This is really fragile and bug bait.
mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3285
Quoted 1 line
mm/khugepaged.c +1
-0+* lock: collapse_huge_page() acquires mmap_read_lock for validation, This is just gross, you're now collapsing based on an outdated concept of what the current VMA state is...
You're also losing literally everything that
madvise_collapse()does.AND you're overriding THP limitations like
max_ptes_none, which is horrible...mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3287
Quoted 2 lines
mm/khugepaged.c +2
-0+* releases it, then acquires mmap_write_lock for the collapse. Holding +* an outer mmap_read_lock would self-deadlock. This is a sign the interface is wrong!
Quoted 15 lines
mm/khugepaged.c +15
-0+* +* Return: 0 on success, -EINVAL on bad arguments, negative error from +* madvise_collapse_errno() otherwise. +*/ +int damon_collapse_folio_range (struct mm_struct *mm,unsigned long start_addr, +unsigned int target_order) +{ +struct collapse_control *cc; +enum scan_result result; + +if (target_order !=HPAGE_PMD_ORDER ) { +pr_warn_once ("%s: only PMD order (%u) is supported, got %u\n" , + __func__,HPAGE_PMD_ORDER , target_order); +return -EINVAL ; + }mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3312
Quoted 10 lines
mm/khugepaged.c +10
-0+if (start_addr & ((PAGE_SIZE << target_order) -1 )) +return -EINVAL ; + + cc =kmalloc_obj (*cc); +if (!cc) +return -ENOMEM ; + cc->is_khugepaged = false; + cc->progress =0 ; + +lru_add_drain_all ();This is quite literally a copy/paste from
madvise_collapse(). No no no :) code duplication like this is also unacceptable.mm/khugepaged.c int madvise_collapse(struct vm_area_struct *vma, unsigned long start, · line 3320
Quoted 8 lines
mm/khugepaged.c +8
-0+ + result =collapse_huge_page (mm, start_addr,1 ,0 , cc, target_order); +kfree (cc); +if (result ==SCAN_SUCCEED || result ==SCAN_PMD_MAPPED ) +return 0 ; +return madvise_collapse_errno (result); +} +EXPORT_SYMBOL_GPL (damon_collapse_folio_range);We _definitely_ cannot have internal mm logic _exported_.
Quoted 2 lines
Yeah sorry you need to rethink this.
Thank you for the incredibly candid and sharp review. You are completely right, and I apologize for my rashness.
My initial motivation was purely focused on solving a pressing production issue we encountered, but in my eagerness, I rushed into the implementation without respecting the structural boundaries of the core MM code. Bypassing the proper mmap_lock state checks, duplicating the madvise validation logic, and blindly exporting internal MM machinery via EXPORT_SYMBOL_GPL creates fragile bug bait. It was reckless of me, and I feel I have let down the valuable time and enthusiasm you and SJ invested in reviewing this work.
I will completely withdraw this design, step back, and fundamentally rethink how to achieve this functionality gracefully within proper subsystem boundaries.
Please give me some time to restructure this into a safe and elegant design. In the meantime, I want to channel this energy into doing something genuinely useful for you and the community. I will take on the development of the patch pre-flight check bot we discussed to automate the verification of CC lists based on scripts/get_maintainer.pl. I hope this tool can help save your valuable time and prevent other newcomers from repeating my exact blunders.
Thank you again for your patience and for steering me back onto the right path.
Best regards,
Wang Lian
Quoted 1 line
Thanks, Lorenzo
RFC PATCH v2 3/5 mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler
In reply to Lian WangWhen target_order is set (non-zero), the DAMOS_COLLAPSE handler now calls damon_collapse_folio_range() to collapse pages into the requested mTHP size, iterating over the target region in order-aligned chunks. When target_order is 0 (default), the existing madvise(MADV_COLLAPSE) path is used, preserving backwards compatibility.
Region boundaries are expanded outward to the covering aligned range (ALIGN_DOWN start, ALIGN end) so that collapse works even after kdamond_split_regions reduces region sizes below the chunk size. collapse_huge_page() internally validates VMA bounds, so expanding beyond the original region is safe.
No external mmap lock is held: collapse_huge_page() acquires mmap_read_lock internally for validation, releases it, then acquires mmap_write_lock for the actual collapse. Holding an outer mmap_read_lock would cause a self-deadlock when the same thread attempts the inner mmap_write_lock.
--- mm/damon/vaddr.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
mm/damon/vaddr.c +48 -0
@@ -14,6 +14,7 @@#include <linux/page_idle.h> #include <linux/pagewalk.h> #include <linux/sched/mm.h> +#include <linux/khugepaged.h> #include "../internal.h" #include "ops-common.h"
@@ -899,6 +900,50 @@ static unsigned long damos_va_stat(struct damon_target *target,return 0 ; } +static unsigned long damos_va_collapse (struct damon_target *target, +struct damon_region *r,struct damos *s, +unsigned long *sz_filter_passed) +{ +unsigned long addr, end, chunk_sz; +unsigned long last_chunk =ULONG_MAX ; +unsigned int target_order = s->target_order; +unsigned long applied =0 ; +struct mm_struct *mm; +int ret; + +if (target_order <2 || target_order >HPAGE_PMD_ORDER ) +return 0 ; + + chunk_sz =PAGE_SIZE << target_order; + addr =ALIGN_DOWN (r->ar.start, chunk_sz); + end =ALIGN (r->ar.end, chunk_sz); +if (end < addr) +return 0 ; + + mm =damon_get_mm (target); +if (!mm) +return 0 ; + +while (addr < end) { +if (addr + chunk_sz < addr) +break ; +if (addr == last_chunk) + goto next; + last_chunk = addr; + + ret =damon_collapse_folio_range (mm, addr, target_order); +if (!ret) + applied += chunk_sz; + *sz_filter_passed += chunk_sz; +next: + addr += chunk_sz; +cond_resched (); + } + +mmput (mm); +return applied; +} +static unsigned long damon_va_apply_scheme (struct damon_ctx *ctx,struct damon_target *t,struct damon_region *r,struct damos *scheme,unsigned long *sz_filter_passed)
@@ -922,6 +967,9 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, madv_action =MADV_NOHUGEPAGE ;break ;case DAMOS_COLLAPSE : +if (scheme->target_order) +return damos_va_collapse (t, r, scheme, + sz_filter_passed); madv_action =MADV_COLLAPSE ;break ;case DAMOS_MIGRATE_HOT :
[email protected]
AI reviewRe: [RESEND RFC PATCH v2 3/5] mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] damos_va_collapse() accepts mTHP target orders but calls damon_collapse_folio_range() which…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] damos_va_collapse() accepts mTHP target orders but calls damon_collapse_folio_range() which strictly rejects them.
- [High] Calling damon_collapse_folio_range() in a per-chunk loop triggers severe global lock contention and IPI storms via lru_add_drain_all().
- [High] damos_va_split() skips entire PMD-sized chunks when encountering unmapped pages or small folios, preventing valid mTHPs from being split.
- [Medium] damos_va_collapse() fails to deduplicate overlapping chunk collapses across adjacent DAMON regions, artificially inflating statistics. --
commit bbcfac46aaf7ca5b18e45240fde04cb5fffba9c1
Author: Lian Wang <[email protected]>
Subject: mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler
This commit implements the DAMOS_COLLAPSE action for mTHP by iterating over target regions in order-aligned chunks. It expands region boundaries to the covering aligned range and calls damon_collapse_folio_range() without holding an external mmap lock to avoid self-deadlocks.
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 904
Quoted 13 lines
mm/damon/vaddr.c +6
-0@@ -895,6 +896,50 @@ static unsigned long damos_va_stat(struct damon_target *target,return 0 ; } +static unsigned long damos_va_collapse (struct damon_target *target, +struct damon_region *r,struct damos *s, +unsigned long *sz_filter_passed) +{ +unsigned long addr, end, chunk_sz; +unsigned long last_chunk =ULONG_MAX ;
[Severity: Medium]
Does this code fail to deduplicate overlapping chunk collapses across adjacent DAMON regions?
When kdamond splits regions into smaller sizes that fall into the same aligned chunk, damos_va_collapse() is called for each region. Because last_chunk is initialized locally to ULONG_MAX on every call, it does not persist across calls, which artificially inflates the DAMON statistics (sz_filter_passed and applied) and prematurely exhausts quotas.
Additionally, because addr is strictly incremented by chunk_sz within the loop, the addr == last_chunk check appears to be dead code:
while (addr < end) {
...
if (addr == last_chunk)
goto next;
last_chunk = addr;
...
addr += chunk_sz;
}Quoted 16 lines
mm/damon/vaddr.c +16
-0+unsigned int target_order = s->target_order; +unsigned long applied =0 ; +struct mm_struct *mm; +int ret; + +if (target_order <2 || target_order >HPAGE_PMD_ORDER ) +return 0 ; + + chunk_sz =PAGE_SIZE << target_order; + addr =ALIGN_DOWN (r->ar.start, chunk_sz); + end =ALIGN (r->ar.end, chunk_sz); +if (end < addr) +return 0 ; + + mm =damon_get_mm (target); +if (!mm)
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 934
Quoted 10 lines
mm/damon/vaddr.c +10
-0+return 0 ; + +while (addr < end) { +if (addr + chunk_sz < addr) +break ; +if (addr == last_chunk) + goto next; + last_chunk = addr; + + ret =damon_collapse_folio_range (mm, addr, target_order);
[Severity: High]
Does calling damon_collapse_folio_range() with mTHP orders fail silently?
While this loop accepts target_order > 0 and < HPAGE_PMD_ORDER, damon_collapse_folio_range() explicitly rejects any order that is not PMD_ORDER:
mm/khugepaged.c:damon_collapse_folio_range() {
if (target_order != HPAGE_PMD_ORDER) {
pr_warn_once("... only PMD order ... is supported ...");
return -EINVAL;
}
}This means attempts to collapse regions into mTHP sizes will fail, while still incrementing sz_filter_passed.
[Severity: High]
Can this tight loop cause severe global lock contention and IPI storms?
damos_va_collapse() iterates over a target memory region in chunk_sz increments, calling damon_collapse_folio_range() sequentially for every chunk. However, the wrapper damon_collapse_folio_range() unconditionally executes lru_add_drain_all():
mm/khugepaged.c:damon_collapse_folio_range() {
...
lru_add_drain_all();
...
}For a multi-GB VMA region, this places a global synchronization point (acquiring lru_add_drain_mutex and sending IPIs to all online CPUs) inside a tight loop hundreds or thousands of times, which can cause system-wide performance degradation, latency spikes, and potential RCU lockups.
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 945
Quoted 11 lines
mm/damon/vaddr.c +11
-0+if (!ret) + applied += chunk_sz; + *sz_filter_passed += chunk_sz; +next: + addr += chunk_sz; +cond_resched (); + } + +mmput (mm); +return applied; +}
[ ... ]
I also noticed an issue in the related damos_va_split() function:
[Severity: High]
Does damos_va_split() skip entire PMD-sized chunks when encountering unmapped pages or small folios?
In damos_va_split(), if an unmapped page is encountered, the code jumps to the unlock label:
folio = folio_walk_start(&fw, vma, addr, 0); if (!folio) goto unlock;
And at the unlock label, addr is advanced by chunk_sz (typically 2MB) instead of PAGE_SIZE:
unlock:
*sz_filter_passed += chunk_sz; addr += chunk_sz;
A similar advancement occurs in the else block when a small folio is encountered:
} else {
folio_walk_end(&fw, vma);
*sz_filter_passed += chunk_sz;
addr += chunk_sz;
}By advancing addr by 2MB upon encountering a single unmapped or small page, this prevents valid mTHPs in the remainder of the chunk from being split.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
SJ Park
maintainerRe: [RESEND RFC PATCH v2 3/5] mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler
In reply to Lian WangQuoted 45 lines · On Thu, 2 Jul 2026 17:46:31 +0800 Lian Wang <[email protected]> wrote:
When
target_orderis set (non-zero), theDAMOS_COLLAPSEhandler now callsdamon_collapse_folio_range()to collapse pages into the requested mTHP size, iterating over the target region in order-aligned chunks. Whentarget_orderis 0 (default), the existingmadvise(MADV_COLLAPSE)path is used, preserving backwards compatibility.Region boundaries are expanded outward to the covering aligned range (
ALIGN_DOWNstart, ALIGN end) so that collapse works even afterkdamond_split_regionsreduces region sizes below the chunk size.collapse_huge_page()internally validates VMA bounds, so expanding beyond the original region is safe.No external mmap lock is held:
collapse_huge_page()acquiresmmap_read_lockinternally for validation, releases it, then acquiresmmap_write_lockfor the actual collapse. Holding an outermmap_read_lockwould cause a self-deadlock when the same thread attempts the innermmap_write_lock.Co-developed-by Kunwu Chan <[email protected]>Signed-off-by Kunwu Chan <[email protected]>Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>--- mm/damon/vaddr.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/damon/vaddr.c +4
-0@@ -14,6 +14,7 @@#include <linux/page_idle.h> #include <linux/pagewalk.h> #include <linux/sched/mm.h> +#include <linux/khugepaged.h> #include "../internal.h" #include "ops-common.h" @@ -899,6 +900,50 @@ static unsigned long damos_va_stat(struct damon_target *target,return 0 ; } +static unsigned long damos_va_collapse (struct damon_target *target, +struct damon_region *r,struct damos *s, +unsigned long *sz_filter_passed)
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 915
Quoted 10 lines
mm/damon/vaddr.c +10
-0+{ +unsigned long addr, end, chunk_sz; +unsigned long last_chunk =ULONG_MAX ; +unsigned int target_order = s->target_order; +unsigned long applied =0 ; +struct mm_struct *mm; +int ret; + +if (target_order <2 || target_order >HPAGE_PMD_ORDER ) +return 0 ;
I think this validation should be done by the caller, or whoever before this function is called. Let's remove this.
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 919
Quoted 4 lines
mm/damon/vaddr.c +4
-0+ + chunk_sz =PAGE_SIZE << target_order; + addr =ALIGN_DOWN (r->ar.start, chunk_sz); + end =ALIGN (r->ar.end, chunk_sz);
How about ALIGN() for addr and ALIGN_DOWN() for end? I show madvise_collapse() is doing so and being consistent to that is maybe a good idea to less confusing users.
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 921
Quoted 2 lines
mm/damon/vaddr.c +2
-0+if (end < addr) +return 0 ;
Let's early-return for end == addr case, too.
Quoted 6 lines
mm/damon/vaddr.c +6
-0+ + mm =damon_get_mm (target); +if (!mm) +return 0 ; + +while (addr < end) {
mm/damon/vaddr.c static unsigned long damos_va_stat(struct damon_target *target, · line 937
Quoted 10 lines
mm/damon/vaddr.c +10
-0+if (addr + chunk_sz < addr) +break ; +if (addr == last_chunk) + goto next; + last_chunk = addr; + + ret =damon_collapse_folio_range (mm, addr, target_order); +if (!ret) + applied += chunk_sz; + *sz_filter_passed += chunk_sz;
Shouldn't this call damos_va_filter_out() before damos_collapse_folio_range()?
Quoted 13 lines
mm/damon/vaddr.c +9
-0+next: + addr += chunk_sz; +cond_resched (); + } + +mmput (mm); +return applied; +} +static unsigned long damon_va_apply_scheme (struct damon_ctx *ctx,struct damon_target *t,struct damon_region *r,struct damos *scheme,unsigned long *sz_filter_passed)
mm/damon/vaddr.c static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, · line 972
Quoted 6 lines
mm/damon/vaddr.c +3
-0@@ -922,6 +967,9 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, madv_action =MADV_NOHUGEPAGE ;break ;case DAMOS_COLLAPSE : +if (scheme->target_order) +return damos_va_collapse (t, r, scheme, + sz_filter_passed);
Let's use two tabs indentation on the second line of parameters, instead of aligning things with the mix of tabs and spaces.
mm/damon/vaddr.c static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, · line 975
Quoted 3 lines
mm/damon/vaddr.c +0
-0madv_action =MADV_COLLAPSE ;break ;case DAMOS_MIGRATE_HOT :
Thanks,
SJ
RFC PATCH v2 4/5 mm/damon: introduce DAMOS_SPLIT action
In reply to Lian WangAdd DAMOS_SPLIT to the damos_action enum for splitting large folios into smaller mTHP-order folios. Add a target_order field to struct damos to specify the desired split order.
Expose the action as "split" through the DAMON sysfs interface with target_order validation (must be 2..HPAGE_PMD_ORDER-1).
--- include/linux/damon.h | 9 +++++++-- mm/damon/sysfs-schemes.c | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-)
include/linux/damon.h +7 -2
@@ -121,6 +121,7 @@ struct damon_target {* @DAMOS_HUGEPAGE: Call ``madvise()`` for the region with MADV_HUGEPAGE. * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE. * @DAMOS_COLLAPSE: Call ``madvise()`` for the region with MADV_COLLAPSE. +* @DAMOS_SPLIT: Split large folios to the target mTHP order. * @DAMOS_LRU_PRIO: Prioritize the region on its LRU lists. * @DAMOS_LRU_DEPRIO: Deprioritize the region on its LRU lists. * @DAMOS_MIGRATE_HOT: Migrate the regions prioritizing warmer regions.
@@ -141,6 +142,7 @@ enum damos_action {DAMOS_HUGEPAGE ,DAMOS_NOHUGEPAGE ,DAMOS_COLLAPSE , +DAMOS_SPLIT ,DAMOS_LRU_PRIO ,DAMOS_LRU_DEPRIO , DAMOS_MIGRATE_HOT,
@@ -573,8 +575,11 @@ struct damos {struct damos_access_pattern pattern;enum damos_action action;/* -* @target_order: target order for mTHP actions (DAMOS_COLLAPSE). -* 0 means system default (PMD order). Valid: 0, 2..HPAGE_PMD_ORDER. +* @target_order: target mTHP order for DAMOS_COLLAPSE and +* DAMOS_SPLIT. For COLLAPSE, 0 means PMD order default, +* valid values: 0, 2..HPAGE_PMD_ORDER. For SPLIT, +* valid values: 2..HPAGE_PMD_ORDER-1; 0 and HPAGE_PMD_ORDER +* are rejected at scheme creation time (defaulting to 2). */ unsigned int target_order;unsigned long apply_interval_us;
mm/damon/sysfs-schemes.c +16 -0
@@ -2293,6 +2293,10 @@ static struct damos_sysfs_action_name damos_sysfs_action_names[] = { .action = DAMOS_COLLAPSE, .name = "collapse", }, + { + .action = DAMOS_SPLIT, + .name = "split", + }, { .action = DAMOS_LRU_PRIO, .name = "lru_prio",
@@ -3058,11 +3062,23 @@ static struct damos *damon_sysfs_mk_scheme( HPAGE_PMD_ORDER, HPAGE_PMD_ORDER); target_order = 0; } +if (sysfs_scheme->action ==DAMOS_SPLIT && + (target_order ==0 || + target_order >=HPAGE_PMD_ORDER )) { +pr_warn ("DAMON split: target_order %u invalid, need 2..%u. Defaulting to 2.\n" , + target_order, +HPAGE_PMD_ORDER -1 ); + target_order =2 ; + }#else if (sysfs_scheme->action ==DAMOS_COLLAPSE && target_order !=0 ) {pr_warn ("DAMON collapse: target_order not supported without THP. Use 0.\n" ); target_order =0 ; } +if (sysfs_scheme->action ==DAMOS_SPLIT ) { +pr_warn ("DAMON split: not supported without THP.\n" ); + target_order =2 ; + }#endif scheme->target_order = target_order;
[email protected]
AI reviewRe: [RESEND RFC PATCH v2 4/5] mm/damon: introduce DAMOS_SPLIT action
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] `damos_va_collapse` and `damos_va_split` redundantly process memory and corrupt accounting quotas…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] damos_va_collapse and damos_va_split redundantly process memory and corrupt accounting quotas, while split skips memory chunks.
- [Medium] damos_va_collapse and damos_va_split silently ignore DAMON operations layer filters.
- [Low] Unconditional use of HPAGE_PMD_ORDER breaks the build on architectures without huge page support.
- [Low] Implicit default target_order for DAMOS_SPLIT triggers a kernel warning on scheme creation.
--
Note: This report is provided as a free-form summary due to recitation restrictions preventing the inline quotation of the original patch code.
Commit: bad86cd869e24f6ecf3b06b8afd727d7561ea5eb
Author: Lian Wang <[email protected]>
Subject: mm/damon: introduce DAMOS_SPLIT action
This commit adds a new DAMOS_SPLIT action to the DAMON operations layer. It allows splitting large folios into smaller mTHP-order folios based on a target_order provided via the sysfs interface.
[Severity: Low]
Could the unconditional use of HPAGE_PMD_ORDER in damos_va_collapse and damos_va_split in mm/damon/vaddr.c break the build on architectures without huge page support? On architectures where CONFIG_PGTABLE_HAS_HUGE_LEAVES is not defined, HPAGE_PMD_ORDER evaluates to a macro containing a BUILD_BUG, which might cause a compilation failure since vaddr.c does not depend on CONFIG_TRANSPARENT_HUGEPAGE.
[Severity: High]
Does damos_va_collapse redundantly process memory and inflate the accounting statistics? It uses a local variable last_chunk to prevent duplicate processing, but because the function is called per-region, last_chunk is reinitialized to ULONG_MAX each time, causing it to repeatedly process the same chunks.
Additionally, in damos_va_split, if a small folio is encountered or the address is unmapped, does the address iterator incorrectly skip memory? It appears to increment the address by the full chunk_sz (2MB) instead of the folio size or PAGE_SIZE, which would skip the remaining folios in the PMD block.
[Severity: Medium]
Do damos_va_collapse and damos_va_split ignore DAMON operations layer filters? Unlike other actions that evaluate damos_va_filter_out on each folio, these functions appear to unconditionally increment the sz_filter_passed statistic. This might falsely report to the DAMON core that the memory passed user-configured filters like anon/file or age filters.
[Severity: Low]
Will creating a sysfs DAMON scheme for the split action trigger unnecessary kernel log spam if the target_order is left at its default? In damon_sysfs_mk_scheme in mm/damon/sysfs-schemes.c, the target_order is zero-initialized. The validation logic treats 0 as invalid for DAMOS_SPLIT, defaults it to 2, and emits a pr_warn, which might generate a warning every time such a scheme is committed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
SJ Park
maintainerRe: [RESEND RFC PATCH v2 4/5] mm/damon: introduce DAMOS_SPLIT action
In reply to Lian WangQuoted 3 lines · On Thu, 2 Jul 2026 17:46:32 +0800 Lian Wang <[email protected]> wrote:
Add
DAMOS_SPLITto thedamos_actionenum for splitting large folios into smaller mTHP-order folios. Add atarget_orderfield to struct damos to specify the desired split order.
This commit is not adding but reusing the field.
Quoted 6 lines
Expose the action as "split" through the DAMON sysfs interface with
target_ordervalidation (must be 2..HPAGE_PMD_ORDER-1).Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>
Let's have only one S-o-b: line if there is no reason to have two.
Quoted 28 lines
--- include/linux/damon.h | 9 +++++++-- mm/damon/sysfs-schemes.c | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-)include/linux/damon.h +2
-0@@ -121,6 +121,7 @@ struct damon_target {* @DAMOS_HUGEPAGE: Call ``madvise()`` for the region with MADV_HUGEPAGE. * @DAMOS_NOHUGEPAGE: Call ``madvise()`` for the region with MADV_NOHUGEPAGE. * @DAMOS_COLLAPSE: Call ``madvise()`` for the region with MADV_COLLAPSE. +* @DAMOS_SPLIT: Split large folios to the target mTHP order. * @DAMOS_LRU_PRIO: Prioritize the region on its LRU lists. * @DAMOS_LRU_DEPRIO: Deprioritize the region on its LRU lists. * @DAMOS_MIGRATE_HOT: Migrate the regions prioritizing warmer regions. @@ -141,6 +142,7 @@ enum damos_action {DAMOS_HUGEPAGE ,DAMOS_NOHUGEPAGE ,DAMOS_COLLAPSE , +DAMOS_SPLIT ,DAMOS_LRU_PRIO ,DAMOS_LRU_DEPRIO , DAMOS_MIGRATE_HOT,@@ -573,8 +575,11 @@ struct damos {struct damos_access_pattern pattern;enum damos_action action;
include/linux/damon.h struct damos { · line 584
Quoted 10 lines
include/linux/damon.h +5
-2/* -* @target_order: target order for mTHP actions (DAMOS_COLLAPSE). -* 0 means system default (PMD order). Valid: 0, 2..HPAGE_PMD_ORDER. +* @target_order: target mTHP order for DAMOS_COLLAPSE and +* DAMOS_SPLIT. For COLLAPSE, 0 means PMD order default, +* valid values: 0, 2..HPAGE_PMD_ORDER. For SPLIT, +* valid values: 2..HPAGE_PMD_ORDER-1; 0 and HPAGE_PMD_ORDER +* are rejected at scheme creation time (defaulting to 2). */ unsigned int target_order;
As I commented to an earlier patch, let's use the union.
Quoted 18 lines
include/linux/damon.h +0
-0unsigned long apply_interval_us;mm/damon/sysfs-schemes.c +4
-0@@ -2293,6 +2293,10 @@ static struct damos_sysfs_action_name damos_sysfs_action_names[] = { .action = DAMOS_COLLAPSE, .name = "collapse", }, + { + .action = DAMOS_SPLIT, + .name = "split", + }, { .action = DAMOS_LRU_PRIO, .name = "lru_prio",@@ -3058,11 +3062,23 @@ static struct damos *damon_sysfs_mk_scheme( HPAGE_PMD_ORDER, HPAGE_PMD_ORDER);
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3072
Quoted 10 lines
mm/damon/sysfs-schemes.c +8
-0target_order =0 ; } +if (sysfs_scheme->action ==DAMOS_SPLIT && + (target_order ==0 || + target_order >=HPAGE_PMD_ORDER )) { +pr_warn ("DAMON split: target_order %u invalid, need 2..%u. Defaulting to 2.\n" , + target_order, +HPAGE_PMD_ORDER -1 ); + target_order =2 ; + }
As I also commented to an earlier patch, this validation will eventually dropped.
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3080
Quoted 8 lines
mm/damon/sysfs-schemes.c +3
-0#else if (sysfs_scheme->action ==DAMOS_COLLAPSE && target_order !=0 ) {pr_warn ("DAMON collapse: target_order not supported without THP. Use 0.\n" ); target_order =0 ; } +if (sysfs_scheme->action ==DAMOS_SPLIT ) { +pr_warn ("DAMON split: not supported without THP.\n" ); + target_order =2 ;
Why set target_order here?
mm/damon/sysfs-schemes.c static struct damos *damon_sysfs_mk_scheme( · line 3083
Quoted 3 lines
mm/damon/sysfs-schemes.c +1
-0+ }#endif scheme->target_order = target_order;
Thanks,
SJ
RFC PATCH v2 5/5 mm/damon/vaddr: implement DAMOS_SPLIT handler
In reply to Lian WangImplement the vaddr operations layer handler for DAMOS_SPLIT. For each folio in the target region that is larger than the scheme's target_order, split it via split_folio_to_order().
This supports both anonymous and file-backed (e.g. tmpfs/shmem) folios, covering KVM guest memory backed by THP tmpfs.
--- mm/damon/vaddr.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
mm/damon/vaddr.c +80 -0
@@ -944,6 +944,83 @@ static unsigned long damos_va_collapse(struct damon_target *target,return applied; } +static unsigned long damos_va_split (struct damon_target *target, +struct damon_region *r,struct damos *s, +unsigned long *sz_filter_passed) +{ +unsigned long addr, end, chunk_sz; +unsigned int target_order = s->target_order; +unsigned long applied =0 ; +struct mm_struct *mm; +struct vm_area_struct *vma; +struct folio *folio; +struct folio_walk fw; + + mm =damon_get_mm (target); +if (!mm) +return 0 ; + + chunk_sz =PAGE_SIZE <<HPAGE_PMD_ORDER ; + addr =ALIGN_DOWN (r->ar.start, chunk_sz); + end =ALIGN (r->ar.end, chunk_sz); +if (end < addr) + goto out_mmput; + +while (addr < end) { +unsigned long folio_sz; + +if (addr + chunk_sz < addr) +break ; + +mmap_read_lock (mm); + vma =find_vma (mm, addr); + +if (!vma || addr < vma->vm_start || + vma->vm_flags & (VM_HUGETLB |VM_MIXEDMAP )) + goto unlock; + + folio =folio_walk_start (&fw, vma, addr,0 ); +if (!folio) + goto unlock; + + folio_sz =PAGE_SIZE <<folio_order (folio); + +if (folio_order (folio) > target_order) { +if (!folio_trylock (folio)) { +folio_walk_end (&fw, vma); + goto unlock; + } +folio_get (folio); +folio_walk_end (&fw, vma); + +if (!split_folio_to_order (folio, target_order)) + applied += folio_sz; + +folio_unlock (folio); +folio_put (folio); + *sz_filter_passed += folio_sz; + addr += folio_sz; + }else { +folio_walk_end (&fw, vma); + *sz_filter_passed += chunk_sz; + addr += chunk_sz; + } +mmap_read_unlock (mm); +cond_resched (); +continue ; + +unlock: + *sz_filter_passed += chunk_sz; + addr += chunk_sz; +mmap_read_unlock (mm); +cond_resched (); + } + +out_mmput: +mmput (mm); +return applied; +} +static unsigned long damon_va_apply_scheme (struct damon_ctx *ctx,struct damon_target *t,struct damon_region *r,struct damos *scheme,unsigned long *sz_filter_passed)
@@ -977,6 +1054,9 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,return damos_va_migrate (t, r, scheme, sz_filter_passed);case DAMOS_STAT :return damos_va_stat (t, r, scheme, sz_filter_passed); +case DAMOS_SPLIT : +return damos_va_split (t, r, scheme, + sz_filter_passed);default :/* * DAMOS actions that are not yet supported by 'vaddr'.
[email protected]
AI reviewRe: [RESEND RFC PATCH v2 5/5] mm/damon/vaddr: implement DAMOS_SPLIT handler
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incorrect virtual address advancement skips valid memory regions, overshoots unaligned folios, and…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incorrect virtual address advancement skips valid memory regions, overshoots unaligned folios, and corrupts DAMOS statistics.
- [High] Missing validation for driver-allocated compound pages before calling split_folio_to_order().
--
commit 8064bd2b9a44cd475dbdec8fba8f8de5e1726797
Author: Lian Wang <[email protected]>
mm/damon/vaddr: implement DAMOS_SPLIT handler
Implement the vaddr operations layer handler for DAMOS_SPLIT. For each folio in the target region that is larger than the scheme's target_order, split it via split_folio_to_order().
This supports both anonymous and file-backed (e.g. tmpfs/shmem) folios, covering KVM guest memory backed by THP tmpfs.
Quoted 4 lines
mm/damon/vaddr.c +0
-0
[ ... ]
Quoted 5 lines
mm/damon/vaddr.c +1
-0@@ -940,6 +940,83 @@ static unsigned long damos_va_collapse(struct damon_target *target,return applied; } +static unsigned long damos_va_split(struct damon_target *target,
[ ... ]
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 996
Quoted 11 lines
mm/damon/vaddr.c +11
-0+ folio_sz =PAGE_SIZE <<folio_order (folio); + +if (folio_order (folio) > target_order) { +if (!folio_trylock (folio)) { +folio_walk_end (&fw, vma); + goto unlock; + } +folio_get (folio); +folio_walk_end (&fw, vma); + +if (!split_folio_to_order (folio, target_order))
[Severity: High]
Can this call to split_folio_to_order() trigger a kernel panic if the folio is a driver-allocated compound page?
folio_walk_start() can return driver-allocated compound pages mapped into normal VMAs. These pages will have a folio_order() greater than zero, but they lack the expected LRU, anonymous, or pagecache metadata.
Should there be a check here to ensure the folio is splittable (for example, verifying it is an LRU page) before attempting to split it?
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 1002
Quoted 6 lines
mm/damon/vaddr.c +6
-0+ applied += folio_sz; + +folio_unlock (folio); +folio_put (folio); + *sz_filter_passed += folio_sz; + addr += folio_sz;
[Severity: High]
Could this advancement logic overshoot the folio boundary?
If the iterator starts in the middle of a large folio, advancing addr by the unadjusted folio_sz might push the address past the actual end of the folio.
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 1006
Quoted 4 lines
mm/damon/vaddr.c +4
-0+ }else { +folio_walk_end (&fw, vma); + *sz_filter_passed += chunk_sz; + addr += chunk_sz;
[Severity: High]
Does advancing by chunk_sz here accidentally skip all subsequent memory within the 2MB chunk?
If folio_walk_start() returns an order-0 folio or any folio smaller than target_order, the else branch advances addr by chunk_sz (2MB). This appears to skip over any remaining folios that might exist in the rest of the chunk.
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 1014
Quoted 8 lines
mm/damon/vaddr.c +8
-0+ } +mmap_read_unlock (mm); +cond_resched (); +continue ; + +unlock: + *sz_filter_passed += chunk_sz; + addr += chunk_sz;
[Severity: High]
Does advancing by chunk_sz in this error path inflate the DAMOS statistics and skip over remaining memory?
If a VMA check fails, or if folio_trylock() fails above, the loop jumps to unlock and adds chunk_sz (2MB) to both sz_filter_passed and addr. This seems like it would bypass the rest of the memory in the chunk and report incorrect statistics for the filtered size.
Quoted 8 lines
mm/damon/vaddr.c +8
-0+mmap_read_unlock (mm); +cond_resched (); + } + +out_mmput: +mmput (mm); +return applied; +}
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
SJ Park
maintainerRe: [RESEND RFC PATCH v2 5/5] mm/damon/vaddr: implement DAMOS_SPLIT handler
In reply to Lian WangQuoted 9 lines · On Thu, 2 Jul 2026 17:46:33 +0800 Lian Wang <[email protected]> wrote:
Implement the vaddr operations layer handler for
DAMOS_SPLIT. For each folio in the target region that is larger than the scheme'starget_order, split it viasplit_folio_to_order().This supports both anonymous and file-backed (e.g. tmpfs/shmem) folios, covering KVM guest memory backed by THP tmpfs.
Signed-off-by Lian Wang <[email protected]>Signed-off-by Lian Wang <[email protected]>
Let's use single S-o-b: if it works.
Quoted 21 lines
--- mm/damon/vaddr.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)mm/damon/vaddr.c +9
-0@@ -944,6 +944,83 @@ static unsigned long damos_va_collapse(struct damon_target *target,return applied; } +static unsigned long damos_va_split (struct damon_target *target, +struct damon_region *r,struct damos *s, +unsigned long *sz_filter_passed) +{ +unsigned long addr, end, chunk_sz; +unsigned int target_order = s->target_order; +unsigned long applied =0 ; +struct mm_struct *mm; +struct vm_area_struct *vma;
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 965
Quoted 10 lines
mm/damon/vaddr.c +10
-0+struct folio *folio; +struct folio_walk fw; + + mm =damon_get_mm (target); +if (!mm) +return 0 ; + + chunk_sz =PAGE_SIZE <<HPAGE_PMD_ORDER ; + addr =ALIGN_DOWN (r->ar.start, chunk_sz); + end =ALIGN (r->ar.end, chunk_sz);
Like I commented to collapse part, would it make more sense to ALIGN() addr and ALIGN_DOWN() end?
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 969
Quoted 4 lines
mm/damon/vaddr.c +4
-0+if (end < addr) + goto out_mmput; + +while (addr < end) {
Above 'if (end < addr)' seems unnecessary, as while() here is covering it?
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 973
Quoted 4 lines
mm/damon/vaddr.c +4
-0+unsigned long folio_sz; + +if (addr + chunk_sz < addr) +break ;
What's the purpose here? Preventing overflow? How the overflow can happen, and what is the consequence?
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 982
Quoted 9 lines
mm/damon/vaddr.c +9
-0+ +mmap_read_lock (mm); + vma =find_vma (mm, addr); + +if (!vma || addr < vma->vm_start || + vma->vm_flags & (VM_HUGETLB |VM_MIXEDMAP )) + goto unlock; + + folio =folio_walk_start (&fw, vma, addr,0 );
Why we need this? Can't we use the simpler pattern like that on damos_va_stat_pmd_entry()?
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 986
Quoted 4 lines
mm/damon/vaddr.c +4
-0+if (!folio) + goto unlock; + + folio_sz =PAGE_SIZE <<folio_order (folio);
You can use folio_size().
Quoted 5 lines
mm/damon/vaddr.c +5
-0+ +if (folio_order (folio) > target_order) { +if (!folio_trylock (folio)) { +folio_walk_end (&fw, vma); + goto unlock;
mm/damon/vaddr.c static unsigned long damos_va_collapse(struct damon_target *target, · line 1001
Quoted 10 lines
mm/damon/vaddr.c +10
-0+ } +folio_get (folio); +folio_walk_end (&fw, vma); + +if (!split_folio_to_order (folio, target_order)) + applied += folio_sz; + +folio_unlock (folio); +folio_put (folio); + *sz_filter_passed += folio_sz;
Shoudn't this code calls damos_va_filter_out() before split_folio_to_order()?
Quoted 26 lines
mm/damon/vaddr.c +22
-0+ addr += folio_sz; + }else { +folio_walk_end (&fw, vma); + *sz_filter_passed += chunk_sz; + addr += chunk_sz; + } +mmap_read_unlock (mm); +cond_resched (); +continue ; + +unlock: + *sz_filter_passed += chunk_sz; + addr += chunk_sz; +mmap_read_unlock (mm); +cond_resched (); + } + +out_mmput: +mmput (mm); +return applied; +} +static unsigned long damon_va_apply_scheme (struct damon_ctx *ctx,struct damon_target *t,struct damon_region *r,struct damos *scheme,unsigned long *sz_filter_passed)
mm/damon/vaddr.c static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, · line 1059
Quoted 6 lines
mm/damon/vaddr.c +3
-0@@ -977,6 +1054,9 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,return damos_va_migrate (t, r, scheme, sz_filter_passed);case DAMOS_STAT :return damos_va_stat (t, r, scheme, sz_filter_passed); +case DAMOS_SPLIT : +return damos_va_split (t, r, scheme, + sz_filter_passed);
Let's keep the order same to that of damos_action.
mm/damon/vaddr.c static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx, · line 1062
Quoted 3 lines
mm/damon/vaddr.c +0
-0default :/* * DAMOS actions that are not yet supported by 'vaddr'.
Thanks,
SJ
+cc all those you missed.
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Quoted 7 lines · Lian Wang
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)
You are doing damon changes, and that belongs to SJ, sure.
But you're also changing core THP code? Please ensure you cc- THP people because without our approval this cannot be merged:
$ scripts/get_maintainer.pl [email protected] SJ Park <[email protected]> (maintainer:DAMON) Andrew Morton <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) David Hildenbrand <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lorenzo Stoakes <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Zi Yan <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Baolin Wang <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) "Liam R. Howlett" <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Nico Pache <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Ryan Roberts <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Dev Jain <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Barry Song <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lance Yang <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) [email protected] (open list:DAMON) [email protected] (open list:DAMON) [email protected] (open list)
Quoted 4 lines
--
2.50.1 (Apple Git-155)
Thanks, Lorenzo
SJ Park
maintainerQuoted 1 line · On Thu, 2 Jul 2026 11:23:55 +0100 Lorenzo Stoakes <[email protected]> wrote:
+cc all those you missed.
Thank you for doing this, Lorenzo.
Quoted 3 lines
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Good idea. I will also consider implementing this kind of checks to to my lzy tool box [1] or hkml [2].
Quoted 10 lines · Lian Wang
Quoted 7 lines
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)
Quoted 4 lines
You are doing damon changes, and that belongs to SJ, sure.
But you're also changing core THP code? Please ensure you cc- THP people because without our approval this cannot be merged:
Thank you for calling out this, Lorenzo.
Lian, please do as Lorenzo kindly asked, from the next revision. You don't need to add those recipients to all the patches if you worry their inbox volumes. But do ensure adding them to at least patches that modifies khugepaged.h and khugepaged.c, and the cover letter.
If it is cumbersome, consider using 'hkml patch format' [3]. It does that (run get_maintainer.pl and add recipients to each patch and the coverletter) for its users.
[1] https://github.com/sjp38/lazybox
[2] https://github.com/sjp38/hackermail
[3] https://github.com/sjp38/hackermail/blob/master/USAGE.md#formatting-patches
Thanks,
SJ
[...]
SJ Park
maintainerQuoted 4 lines · On Thu, 2 Jul 2026 09:52:34 -0700 SJ Park <[email protected]> wrote:
Quoted 1 line · On Thu, 2 Jul 2026 11:23:55 +0100 Lorenzo Stoakes <[email protected]> wrote:
+cc all those you missed.
Quoted 8 lines
Thank you for doing this, Lorenzo.
Quoted 3 lines
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Good idea. I will also consider implementing this kind of checks to to my lzy tool box [1] or hkml [2].
I implemented [1] the check logic on hkml. Many things to fix and improve in future, but seems working at least for this series. You can use it like below:
$ hkml patch check --check_recipients only ./0002-mm-khugepaged-add-damon-collapse-folio-range-for-external-callers.patch
MISSING RECIPIENTS for [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers
- Andrew Morton <[email protected]>
- David Hildenbrand <[email protected]>
- Lorenzo Stoakes <[email protected]>
- Zi Yan <[email protected]>
- Baolin Wang <[email protected]>
- "Liam R. Howlett" <[email protected]>
- Nico Pache <[email protected]>
- Ryan Roberts <[email protected]>
- Dev Jain <[email protected]>
- Barry Song <[email protected]>
- Lance Yang <[email protected]>
- Usama Arif <[email protected]>
- [email protected]
$ hkml patch check --check_recipients only ./0003-mm-damon-vaddr-implement-mthp-aware-damos-collapse-handler.patch
MISSING RECIPIENTS for [RESEND RFC PATCH v2 3/5] mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler
- Andrew Morton <[email protected]>
- [email protected]It is also integrated with the interactive mails list [2], so that you can run it without manually downloading the patch files.
FWIW, 'hkml patch format' automatically adds recipients based on get_maintainer.pl, so this check is unnecessary for hkml users. But this will help me reviewing patches that not formatted with hkml or other similar scripts.
hkml has a feature [3] to monitor new mails. Extending it to run this new check and send mails for check failures could be one of the ways to implement the bot. I'm not planning to do that by myself, though. I will run the check only for patches that look interesting to me, for now.
[1] https://github.com/sjp38/hackermail/commit/9ca6fa1f71edc7a219edeb41d4c7f91
[2] https://github.com/sjp38/hackermail/blob/master/USAGE.md#interactive-viewer
[3] https://github.com/sjp38/hackermail/blob/master/USAGE.md#monitoring-mails
Thanks,
SJ
[...]
Quoted 56 lines · SJ Park
Quoted 4 lines · On Thu, 2 Jul 2026 09:52:34 -0700 SJ Park <[email protected]> wrote:
Quoted 1 line · On Thu, 2 Jul 2026 11:23:55 +0100 Lorenzo Stoakes <[email protected]> wrote:
+cc all those you missed.
Quoted 8 lines
Thank you for doing this, Lorenzo.
Quoted 3 lines
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Good idea. I will also consider implementing this kind of checks to to my lzy tool box [1] or hkml [2].
I implemented [1] the check logic on hkml. Many things to fix and improve in future, but seems working at least for this series. You can use it like below:
$ hkml patch check --check_recipients only ./0002-mm-khugepaged-add-damon-collapse-folio-range-for-external-callers.patch MISSING RECIPIENTS for [RESEND RFC PATCH v2 2/5] mm/khugepaged: add damon_collapse_folio_range() for external callers - Andrew Morton <[email protected]> - David Hildenbrand <[email protected]> - Lorenzo Stoakes <[email protected]> - Zi Yan <[email protected]> - Baolin Wang <[email protected]> - "Liam R. Howlett" <[email protected]> - Nico Pache <[email protected]> - Ryan Roberts <[email protected]> - Dev Jain <[email protected]> - Barry Song <[email protected]> - Lance Yang <[email protected]> - Usama Arif <[email protected]> - [email protected] $ hkml patch check --check_recipients only ./0003-mm-damon-vaddr-implement-mthp-aware-damos-collapse-handler.patch MISSING RECIPIENTS for [RESEND RFC PATCH v2 3/5] mm/damon/vaddr: implement mTHP-aware DAMOS_COLLAPSE handler - Andrew Morton <[email protected]> - [email protected]It is also integrated with the interactive mails list [2], so that you can run it without manually downloading the patch files.
FWIW, 'hkml patch format' automatically adds recipients based on get_maintainer.pl, so this check is unnecessary for hkml users. But this will help me reviewing patches that not formatted with hkml or other similar scripts.
hkml has a feature [3] to monitor new mails. Extending it to run this new check and send mails for check failures could be one of the ways to implement the bot. I'm not planning to do that by myself, though. I will run the check only for patches that look interesting to me, for now.
[1] https://github.com/sjp38/hackermail/commit/9ca6fa1f71edc7a219edeb41d4c7f91
[2] https://github.com/sjp38/hackermail/blob/master/USAGE.md#interactive-viewer
[3] https://github.com/sjp38/hackermail/blob/master/USAGE.md#monitoring-mails
Quoted 4 lines
Thanks,
SJ[...]
Nice, thanks SJ! That's cool :)
Cheers, Lorenzo
Hi,Lorenzo
Quoted 21 lines · Lorenzo Stoakes
+cc all those you missed.
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Quoted 7 lines · Lian Wang
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)You are doing damon changes, and that belongs to SJ, sure.
But you're also changing core THP code? Please ensure you cc- THP people because without our approval this cannot be merged:
Quoted 22 lines
$ scripts/get_maintainer.pl [email protected] <mailto:[email protected]> SJ Park <[email protected] <mailto:[email protected]>> (maintainer:DAMON) Andrew Morton <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) David Hildenbrand <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lorenzo Stoakes <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Zi Yan <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Baolin Wang <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) "Liam R. Howlett" <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Nico Pache <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Ryan Roberts <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Dev Jain <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Barry Song <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lance Yang <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) [email protected] <mailto:[email protected]> (open list:DAMON) [email protected] <mailto:[email protected]> (open list:DAMON) [email protected] <mailto:[email protected]> (open list)Quoted 4 lines
--
2.50.1 (Apple Git-155)
Thank you for pointing this out again, and I deeply appreciate all the tedious work you do as a maintainer to keep the mailing lists aligned.
As a newcomer trying to transition into complex feature development, I must admit my initial design is far from perfect. I leveraged AI tools to assist with certain implementation paths, and although I extensively self-reviewed, tested, and reasoned through every single line of code, your and SJ's feedback clearly shows there are still major architectural flaws. Just like my very first kernel patch—which was strictly reviewed and guided by you and David H. —I know that your sharp feedback is exactly what will help me grow as a kernel engineer.
I will completely step back and radically rethink the design of this series, especially regarding the cross-subsystem encapsulation and locking hazards.
As for that bot you mentioned getting tired of running manually—let me write it for you. An imperfect newcomer who just made these exact mistakes knows precisely where the traps are. I can craft a pre-flight patch check script or a CI bot that automatically validates the get_maintainer.pl output against the patch file diffstat before any email is dispatched, throwing a loud warning if core MM files are touched but their respective maintainers are missing from the CC line.
What do you think? I would love to build this tool to save your and other maintainers' valuable time in the future.
Thanks again for steering me in the right direction!
Best regards,
Wang Lian
Quoted 1 line
Thanks, Lorenzo
SJ Park
maintainerQuoted 57 lines · On Fri, 3 Jul 2026 09:10:51 +0800 wang lian <[email protected]> wrote:
Hi,Lorenzo
Quoted 21 lines · Lorenzo Stoakes
+cc all those you missed.
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Quoted 7 lines · Lian Wang
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)You are doing damon changes, and that belongs to SJ, sure.
But you're also changing core THP code? Please ensure you cc- THP people because without our approval this cannot be merged:
Quoted 22 lines
$ scripts/get_maintainer.pl [email protected] <mailto:[email protected]> SJ Park <[email protected] <mailto:[email protected]>> (maintainer:DAMON) Andrew Morton <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) David Hildenbrand <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lorenzo Stoakes <[email protected] <mailto:[email protected]>> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Zi Yan <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Baolin Wang <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) "Liam R. Howlett" <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Nico Pache <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Ryan Roberts <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Dev Jain <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Barry Song <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lance Yang <[email protected] <mailto:[email protected]>> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) [email protected] <mailto:[email protected]> (open list:DAMON) [email protected] <mailto:[email protected]> (open list:DAMON) [email protected] <mailto:[email protected]> (open list)Quoted 4 lines
--
2.50.1 (Apple Git-155)Thank you for pointing this out again, and I deeply appreciate all the tedious work you do as a maintainer to keep the mailing lists aligned.
As a newcomer trying to transition into complex feature development, I must admit my initial design is far from perfect. I leveraged AI tools to assist with certain implementation paths, and although I extensively self-reviewed, tested, and reasoned through every single line of code, your and SJ's feedback clearly shows there are still major architectural flaws. Just like my very first kernel patch—which was strictly reviewed and guided by you and David H. —I know that your sharp feedback is exactly what will help me grow as a kernel engineer.
Quoted 2 lines
I will completely step back and radically rethink the design of this series, especially regarding the cross-subsystem encapsulation and locking hazards.
No worries, take your time :)
Quoted 11 lines
As for that bot you mentioned getting tired of running manually—let me write it for you. An imperfect newcomer who just made these exact mistakes knows precisely where the traps are. I can craft a pre-flight patch check script or a CI bot that automatically validates the
get_maintainer.ploutput against the patch file diffstat before any email is dispatched, throwing a loud warning if core MM files are touched but their respective maintainers are missing from the CC line.What do you think? I would love to build this tool to save your and other maintainers' valuable time in the future.
Sounds great! hkml already has all building blocks for doing the check, so I'm planning to develop such checks in hkml, but only for manual runs. I ain't develop and run a bot, since I don't have that much compute resource. Maybe you could save your time by reusing the hkml feature on your bot.
Thanks,
SJ
[...]
Hi Lorenzo,
Quoted 21 lines · Lorenzo Stoakes
+cc all those you missed.
I really need to write a bot to do this, because I'm getting a little tired of pointing this out :))
Quoted 7 lines · Lian Wang
include/linux/damon.h | 10 +++ include/linux/khugepaged.h | 9 +++ mm/damon/core.c | 2 + mm/damon/sysfs-schemes.c | 77 ++++++++++++++++++++++ mm/damon/vaddr.c | 128 +++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 46 +++++++++++++ 6 files changed, 272 insertions(+)You are doing damon changes, and that belongs to SJ, sure.
But you're also changing core THP code? Please ensure you cc- THP people because without our approval this cannot be merged:
Quoted 22 lines
$ scripts/get_maintainer.pl [email protected] SJ Park <[email protected]> (maintainer:DAMON) Andrew Morton <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) David Hildenbrand <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lorenzo Stoakes <[email protected]> (maintainer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Zi Yan <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Baolin Wang <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) "Liam R. Howlett" <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Nico Pache <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Ryan Roberts <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Dev Jain <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Barry Song <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) Lance Yang <[email protected]> (reviewer:MEMORY MANAGEMENT - THP (TRANSPARENT HUGE PAGE)) [email protected] (open list:DAMON) [email protected] (open list:DAMON) [email protected] (open list)Quoted 4 lines
--
2.50.1 (Apple Git-155)
Thank you for pointing this out again, and I deeply appreciate all the tedious work you do as a maintainer to keep the mailing lists aligned.
As a newcomer trying to transition into complex feature development, I must admit my initial design is far from perfect. I leveraged AI tools to assist with certain implementation paths, and although I extensively self-reviewed, tested, and reasoned through every single line of code, your and SJ's feedback clearly shows there are still major architectural flaws. Just like my very first kernel patch -- which was strictly reviewed and guided by you and David H. -- I know that your sharp feedback is exactly what will help me grow as a kernel engineer.
I will completely step back and radically rethink the design of this series, especially regarding the cross-subsystem encapsulation and locking hazards.
As for that bot you mentioned getting tired of running manually -- let me write it for you. An imperfect newcomer who just made these exact mistakes knows precisely where the traps are. I can craft a pre-flight patch check script or a CI bot that automatically validates the get_maintainer.pl output against the patch file diffstat before any email is dispatched, throwing a loud warning if core MM files are touched but their respective maintainers are missing from the CC line.
What do you think? I would love to build this tool to save your and other maintainers' valuable time in the future.
Thanks again for steering me in the right direction!
Best regards,
Wang Lian
Quoted 1 line
Thanks, Lorenzo
SJ Park
maintainerQuoted 10 lines · On Thu, 2 Jul 2026 17:46:28 +0800 Lian Wang <[email protected]> wrote:
Resend of v2 with the RFC tag restored (v1 was RFC PATCH, so v2 should be RFC PATCH v2).
This resend also includes fixes for issues identified during review of the earlier mis-sent PATCH v2 thread: uninitialized memory, TOCTOU races,
BUILD_BUGguards, missing sysfs action name registration, and stack allocation overflow. The series has been re-tested on aarch64 (anonymous and file-backed THP split) and is checkpatch clean.
Let's call it 'RFC v1'.
Quoted 2 lines
Changes since v1
Ditto.
Quoted 10 lines
- Rename
DAMOS_MTHP_SPLIT->DAMOS_SPLITfor naming consistency with the existing actions (per SJ's review).
- Drop the per-schemehot_thresholdfield. Hotness policy does not belong in the kernel; target selection now lives in user space and is expressed to DAMOS via the address filter (per SJ's review).
- Drop the v1 SPE debugfs patch entirely. debugfs is not the right interface for a feature, and the SPE profiler belongs in user space (see "User-space target selection" below). v2 is kernel mechanism
only: 5 patches.
Quoted 8 lines
- Decouple T1 (a lab observation) from T2 (the production issue), and correct the architecture claim:
ptep_test_and_clear_young()skips the TLB flush on bothx86_64and arm64, so the blind spot is architecture-independent rather than arm64-only.
- Terminology: avoid "stale TLB". A valid TLB entry is doing its job; the point is only that it lets the CPU satisfy a translation without a page-table walk, so the Accessed bit cleared by DAMON is not re-set.
Thank you for detailed changelog. This is helpful for reviewers.
Quoted 8 lines
Background
Two effects degrade DAMON's PTE-Accessed-bit (AF) signal once THP is in play. Both are described here as motivation only; this series does not change the AF monitoring path.
T2 -- PMD-granularity inflation (production issue)
I think it is better to call this T1, for readers.
Quoted 11 lines
A 2MB THP is tracked by a single PMD-level Accessed bit. One access to any 4KB sub-page sets the AF for the whole 2MB, so DAMON reports the entire THP as hot and cannot distinguish a genuinely hot 2MB region from a 2MB region with a single hot 4KB page. Cold memory hides inside "hot" THPs, and access-driven pageout/migration becomes coarse.
This is the workload that drove the work: Sangfor's Kunpeng 920 KVM hosts running Oracle. ARM SPE sampling of that workload shows 94.6% of THPs have fewer than 10% of their sub-pages actually accessed.
Cool finding, thank you for sharing. What DB workloads were running there? Real production workload? Or, synthetic benchmarks?
On the first read, I was wondering how you did ARM SPE sampling. After reading this mail to the end, I now understand you use perf. Briefly mentioning that here would be nice. E.g., "ARM SPE sampling of that worklaod using perf shows ..."
Quoted 2 lines
T1 -- TLB-reach blind spot (lab observation)
I think it is better to call this T2, for readers.
Quoted 6 lines
When the working set fits within L2 TLB reach (measured at 2048 entries x 2MB = 4GB on Kunpeng 920; no public data available), the CPU satisfies translations entirely from the TLB, preventing translation table walks. Because
ptep_test_and_clear_young()does not flush
Wrapping text for the max columns is nice. But let's not wrap it early when there are spaces. That could reduce space, and even carbon emissions from people who want to read this nice cover letter after printing out on a paper.
Quoted 10 lines
the TLB, valid TLB entries continue to satisfy translations and the AF that DAMON cleared is never re-set, so DAMON sees nr_accesses=0 for memory that is in fact hot, and no scheme triggers. This reproduces in the lab with small workloads; it is not something we have seen reported from production, where working sets exceed TLB reach.
What this series adds
Rather than change AF monitoring, this series adds two order-aware DAMOS actions so a policy layer can act at mTHP granularity:
The background explained rooms to improve in DAMON's THP access "monitoring". And this patch series is proposing adding new DAMOS actions for THP "handling". Those are two unrelated things.
I really appreciate sharing your findings with the background, but as those are not related to the proposal, I think it is better to be shared in a different way.
I understand you are proposing this change because you know DAMON's hugepages monitoring is imperfect, but still useful enough to get some benefits. If there were some findings that made you to think so, that could be good background.
Also, you may have a reason to believe it is a good idea to use larger mTHP for hot pages, and smaller mTHP for cold pages. If so, and the description of the reason is not trivial, that could be good materials to add on background.
Quoted 2 lines
-
DAMOS_COLLAPSE+target_order(patches 1-3): collapse small folios
This reads like you are introducing a new DAMOS action. You indeed mentioned "this series adds two order-aware DAMOS actions". That's not completely wrong in a sense, but more technically speaking you are adding a new mode of DAMOS_COLLAPSE. I'd recommend rephrasing to "extend" DAMOS_COLLAPSE..
Quoted 3 lines
up to a chosen mTHP order. Patch 1 adds the
target_orderfield and its sysfs file; patch 2 exports a khugepaged helper (damon_collapse_folio_range());
So patch 2 modifies khugepaged? As Lorenzo mentioned on the other reply, that change should also be reviewed by THP developers on MAINTAINERS file. Please ensure adding THP developers to the recipients list of the patch and this cover letter.
The patch adds damon_collapse_folio_range() to khugepaged.h. I understand DAMON is the only user for now, and therefore you are adding damon_ prefix to the name. Not necesasrily DAMON is the only user forever. And having damon_ prefix in a land outside of DAMON feels weird. To be consistent with other functions like collapse_pte_mapped_thp(), I'd suggest dropping the prefix from the name.
Quoted 11 lines
patch 3 wires the vaddr handler.
-
DAMOS_SPLIT+target_order(patches 4-5): split large folios down to a chosen mTHP order viasplit_folio_to_order(), for both anonymous and file-backed (tmpfs/shmem) folios.The two are complementary, not competing:
THP=never +
DAMOS_COLLAPSE: start at 4KB, grow hot regions up.
THP=always +DAMOS_SPLIT: start at 2MB, shrink cold regions down.
Quoted 8 lines
This dual-path design aligns with ideas discussed with Asier Gutierrez; we plan to unify our mTHP automation and evaluation roadmaps under this standard
DAMOS_SPLITaction.A deployment can pick either baseline, or run both, and let DAMOS manage the placement. THP is still wanted for the hot working set (fewer TLB misses, shallower walks); the goal is not "no THP" but "THP where it is hot, small pages where it is cold."
I think this is a good idea. Could you further elaborate what benefit users can get from this in more detail, though? Off the top of my head, I can expect the benefits would be 1) less TLB miss from hot data, and 2) less mTHP allocation failures from cold data occupying phsically contiguous memory. But
you might showing even more benefits. Anyway I think those are better to be
widely known by our kernel users. Some of those may better to be put on the background section.
Quoted 10 lines
User-space target selection
The decision of *which* regions to collapse or split is left to user space and fed to DAMOS through the existing DAMOS address filter (
DAMOS_FILTER_TYPE_ADDR) -- the interface suggested during v1 review. The kernel provides the mechanism; user space provides the policy, consistent with the perf/BPF "kernel samples, user space decides" model and with the DAMON-X direction.
Quoted 4 lines
Because the AF signal is unreliable at PMD granularity (T1/T2), the scheme is run with min_nr_accesses=0 so it does not gate on access count, and the address filter selects targets. min_nr_accesses=0 is also what unblocks the T1 case, where
nr_accessesis pinned at 0.
Oh, so you are saying DAMON's huge pages monitoring is too problematic to use as-is, for your use case. That's completely fair. And that explains what you really want to do. But this whole pictur is better to be described earlier than your changes proposal.
From the beginning, explain why using larger mTHP for hot pages and smaller mTHP for cold pages are good idea. After that, explain how DAMON can be extended for doing that. Then, you can further explain your T1 and T2 findings that explain why DAMON-only appraoch is not feasible, and how user-space target selection can overcome it.
Also, I understand DAMON-only approach is not optimum or just useless for your aimed use case. But, is it completely useless for every possible use case? I think it might still provide some benefit in some use cases. Could you pleae clarify this point more in detail? If you have data showing how useless DAMON-only appraoch is, and how user space approach improves, it would be awesome.
Quoted 10 lines
Why not just turn khugepaged off? You can, but khugepaged is global and usually left enabled because other workloads rely on it; it cannot be disabled per region.
DAMOS_COLLAPSEgives per-region, access-pattern-driven collapse -- a more precise, targeted complement to khugepaged's global scan, not a replacement for it. To handle the runtime race where khugepaged might aggressively re-collapse whatDAMOS_SPLITjust split, we are evaluating a precise VMA-level handshake or back-off mechanism to prevent ping-pong effects in mixed environments.
Good reasoning. However, khugepaged can be turned off per process, using prctl(). How about turning khugepaged off for the process you want to use DAMOS_COLLAPSE/SPLIT for?
Quoted 8 lines
Two user-space data sources produce the candidate address ranges:
1. ARM SPE (ARMv8.2+): perf record (SPE) -> per-2MB hot-fraction histogram ->
PA->VAvia /proc/<pid>/pagemap -> sparse-THP VA ranges. SPE reads physical addresses from the CPU pipeline, bypassing the TLB and page tables, so it is immune to T1 and T2.
Quoted 8 lines
2. smaps fallback (no SPE): scan /proc/<pid>/smaps for THP-backed VMAs and treat the 2MB-aligned ranges as split candidates.
The SPE profiler stays in user space deliberately: the SPE PMU is a single-consumer resource, so a kernel consumer would lock out user-space perf and tooling (x86 PEBS / AMD IBS have the same property). Keeping it in user space avoids that and keeps the metric source pluggable, in line with DAMON-X.
Maybe you are mentioning the perf events based DAMON, not DAMON-X.
And I understand you plan to extend DAMON to use ARM SPE, on top of the perf events based DAMON as a future work. As I mentioned before, I think that makes perfect sense and I'm aligned. Maybe this paragraph can bit reworded to make it more clear, though.
Quoted 9 lines
This is why v2 drops the v1
SPE debugfs patch.Testing
Tested on aarch64 with this series applied to 7.1.0-rc5, THP=always, using a
DAMOS_SPLITscheme (target_order=2, min_nr_accesses=0) and a single DAMOS address filter selecting one 2MB-aligned range:
Quoted 13 lines
- Anonymous THP: the filter splits exactly that one THP -- sz_applied=2MB and AnonHugePages drops by 2MB, the rest of the 256MB mapping untouched.
- File-backed THP (tmpfs/shmem mounted huge=always): the same setup splits exactly one 2MB shmem THP -- sz_applied=2MB and ShmemPmdMapped drops by 2MB. This confirmssplit_folio_to_order()works for shmem folios (the KVM-guest-on-THP-tmpfs case).
- The address filter is what bounds the action:sz_triedcovers the whole ~2GB monitored region whilesz_appliedis exactly the 2MB the filter selected.
- A smaps-based path (for hosts without SPE) enumerates THP-backed ranges and splits all THP in the target workload.
- checkpatch clean on all 5 patches.
So, you tested only split part, for functionality. Do you have plans to further test collapse part, and performance?
Quoted 3 lines
Test scripts and SPE-to-DAMON pipeline tools:
https://github.com/lianux-mm/damon_spe/tree/v2
Thank you for sharing the code!
So, I find rooms to improve on this cover letter for the readability and clarity of the idea. But as I mentioned before, I like the overall idea of this series.
Thanks,
SJ
[...]
SJ Park
maintainerKeeping full original mail, so that Lian can answer all comments in one reply.
Quoted 106 lines · On Thu, 2 Jul 2026 11:35:51 -0700 SJ Park <[email protected]> wrote:
Quoted 10 lines · On Thu, 2 Jul 2026 17:46:28 +0800 Lian Wang <[email protected]> wrote:
Resend of v2 with the RFC tag restored (v1 was RFC PATCH, so v2 should be RFC PATCH v2).
This resend also includes fixes for issues identified during review of the earlier mis-sent PATCH v2 thread: uninitialized memory, TOCTOU races,
BUILD_BUGguards, missing sysfs action name registration, and stack allocation overflow. The series has been re-tested on aarch64 (anonymous and file-backed THP split) and is checkpatch clean.Let's call it 'RFC v1'.
Quoted 2 lines
Changes since v1
Ditto.
Quoted 10 lines
- Rename
DAMOS_MTHP_SPLIT->DAMOS_SPLITfor naming consistency with the existing actions (per SJ's review).
- Drop the per-schemehot_thresholdfield. Hotness policy does not belong in the kernel; target selection now lives in user space and is expressed to DAMOS via the address filter (per SJ's review).
- Drop the v1 SPE debugfs patch entirely. debugfs is not the right interface for a feature, and the SPE profiler belongs in user space (see "User-space target selection" below). v2 is kernel mechanism
only: 5 patches.Quoted 8 lines
- Decouple T1 (a lab observation) from T2 (the production issue), and correct the architecture claim:
ptep_test_and_clear_young()skips the TLB flush on bothx86_64and arm64, so the blind spot is architecture-independent rather than arm64-only.
- Terminology: avoid "stale TLB". A valid TLB entry is doing its job; the point is only that it lets the CPU satisfy a translation without a page-table walk, so the Accessed bit cleared by DAMON is not re-set.Thank you for detailed changelog. This is helpful for reviewers.
Quoted 8 lines
Background
Two effects degrade DAMON's PTE-Accessed-bit (AF) signal once THP is in play. Both are described here as motivation only; this series does not change the AF monitoring path.
T2 -- PMD-granularity inflation (production issue)
I think it is better to call this T1, for readers.
Quoted 11 lines
A 2MB THP is tracked by a single PMD-level Accessed bit. One access to any 4KB sub-page sets the AF for the whole 2MB, so DAMON reports the entire THP as hot and cannot distinguish a genuinely hot 2MB region from a 2MB region with a single hot 4KB page. Cold memory hides inside "hot" THPs, and access-driven pageout/migration becomes coarse.
This is the workload that drove the work: Sangfor's Kunpeng 920 KVM hosts running Oracle. ARM SPE sampling of that workload shows 94.6% of THPs have fewer than 10% of their sub-pages actually accessed.
Cool finding, thank you for sharing. What DB workloads were running there? Real production workload? Or, synthetic benchmarks?
On the first read, I was wondering how you did ARM SPE sampling. After reading this mail to the end, I now understand you use perf. Briefly mentioning that here would be nice. E.g., "ARM SPE sampling of that worklaod using perf shows ..."
Quoted 2 lines
T1 -- TLB-reach blind spot (lab observation)
I think it is better to call this T2, for readers.
Quoted 6 lines
When the working set fits within L2 TLB reach (measured at 2048 entries x 2MB = 4GB on Kunpeng 920; no public data available), the CPU satisfies translations entirely from the TLB, preventing translation table walks. Because
ptep_test_and_clear_young()does not flushWrapping text for the max columns is nice. But let's not wrap it early when there are spaces. That could reduce space, and even carbon emissions from people who want to read this nice cover letter after printing out on a paper.
Quoted 10 lines
the TLB, valid TLB entries continue to satisfy translations and the AF that DAMON cleared is never re-set, so DAMON sees nr_accesses=0 for memory that is in fact hot, and no scheme triggers. This reproduces in the lab with small workloads; it is not something we have seen reported from production, where working sets exceed TLB reach.
What this series adds
Rather than change AF monitoring, this series adds two order-aware DAMOS actions so a policy layer can act at mTHP granularity:
The background explained rooms to improve in DAMON's THP access "monitoring". And this patch series is proposing adding new DAMOS actions for THP "handling". Those are two unrelated things.
I really appreciate sharing your findings with the background, but as those are not related to the proposal, I think it is better to be shared in a different way.
Quoted 8 lines
I understand you are proposing this change because you know DAMON's hugepages monitoring is imperfect, but still useful enough to get some benefits. If there were some findings that made you to think so, that could be good background.
Also, you may have a reason to believe it is a good idea to use larger mTHP for hot pages, and smaller mTHP for cold pages. If so, and the description of the reason is not trivial, that could be good materials to add on background.
Now I doubt if we really need two new DAMOS actions. What happens if user asks DAMOS_COLLAPSE of a target order for region that currently being backed by an mTHP of an order that is larger than the newly asked one? If we just ignore the case, DAMOS_SPLIT will really nneeded. But maybe we can just split the large folio into the newly requested order mTHPs. In this scenario, DAMOS_SPLIT is not needed?
Quoted 162 lines
Quoted 3 lines
-
DAMOS_COLLAPSE+target_order(patches 1-3): collapse small foliosThis reads like you are introducing a new DAMOS action. You indeed mentioned "this series adds two order-aware DAMOS actions". That's not completely wrong in a sense, but more technically speaking you are adding a new mode of
DAMOS_COLLAPSE. I'd recommend rephrasing to "extend"DAMOS_COLLAPSE..Quoted 3 lines
up to a chosen mTHP order. Patch 1 adds the
target_orderfield and its sysfs file; patch 2 exports a khugepaged helper (damon_collapse_folio_range());So patch 2 modifies khugepaged? As Lorenzo mentioned on the other reply, that change should also be reviewed by THP developers on
MAINTAINERSfile. Please ensure adding THP developers to the recipients list of the patch and this cover letter.The patch adds
damon_collapse_folio_range()tokhugepaged.h. I understand DAMON is the only user for now, and therefore you are addingdamon_prefix to the name. Not necesasrily DAMON is the only user forever. And havingdamon_prefix in a land outside of DAMON feels weird. To be consistent with other functions likecollapse_pte_mapped_thp(), I'd suggest dropping the prefix from the name.Quoted 11 lines
patch 3 wires the vaddr handler.
-
DAMOS_SPLIT+target_order(patches 4-5): split large folios down to a chosen mTHP order viasplit_folio_to_order(), for both anonymous and file-backed (tmpfs/shmem) folios.The two are complementary, not competing:
THP=never +
DAMOS_COLLAPSE: start at 4KB, grow hot regions up.
THP=always +DAMOS_SPLIT: start at 2MB, shrink cold regions down.Quoted 8 lines
This dual-path design aligns with ideas discussed with Asier Gutierrez; we plan to unify our mTHP automation and evaluation roadmaps under this standard
DAMOS_SPLITaction.A deployment can pick either baseline, or run both, and let DAMOS manage the placement. THP is still wanted for the hot working set (fewer TLB misses, shallower walks); the goal is not "no THP" but "THP where it is hot, small pages where it is cold."
I think this is a good idea. Could you further elaborate what benefit users can get from this in more detail, though? Off the top of my head, I can expect the benefits would be 1) less TLB miss from hot data, and 2) less mTHP allocation failures from cold data occupying phsically contiguous memory. But
you might showing even more benefits. Anyway I think those are better to be
widely known by our kernel users. Some of those may better to be put on the background section.Quoted 10 lines
User-space target selection
The decision of *which* regions to collapse or split is left to user space and fed to DAMOS through the existing DAMOS address filter (
DAMOS_FILTER_TYPE_ADDR) -- the interface suggested during v1 review. The kernel provides the mechanism; user space provides the policy, consistent with the perf/BPF "kernel samples, user space decides" model and with the DAMON-X direction.Quoted 4 lines
Because the AF signal is unreliable at PMD granularity (T1/T2), the scheme is run with min_nr_accesses=0 so it does not gate on access count, and the address filter selects targets. min_nr_accesses=0 is also what unblocks the T1 case, where
nr_accessesis pinned at 0.Oh, so you are saying DAMON's huge pages monitoring is too problematic to use as-is, for your use case. That's completely fair. And that explains what you really want to do. But this whole pictur is better to be described earlier than your changes proposal.
From the beginning, explain why using larger mTHP for hot pages and smaller mTHP for cold pages are good idea. After that, explain how DAMON can be extended for doing that. Then, you can further explain your T1 and T2 findings that explain why DAMON-only appraoch is not feasible, and how user-space target selection can overcome it.
Also, I understand DAMON-only approach is not optimum or just useless for your aimed use case. But, is it completely useless for every possible use case? I think it might still provide some benefit in some use cases. Could you pleae clarify this point more in detail? If you have data showing how useless DAMON-only appraoch is, and how user space approach improves, it would be awesome.
Quoted 10 lines
Why not just turn khugepaged off? You can, but khugepaged is global and usually left enabled because other workloads rely on it; it cannot be disabled per region.
DAMOS_COLLAPSEgives per-region, access-pattern-driven collapse -- a more precise, targeted complement to khugepaged's global scan, not a replacement for it. To handle the runtime race where khugepaged might aggressively re-collapse whatDAMOS_SPLITjust split, we are evaluating a precise VMA-level handshake or back-off mechanism to prevent ping-pong effects in mixed environments.Good reasoning. However, khugepaged can be turned off per process, using
prctl(). How about turning khugepaged off for the process you want to use DAMOS_COLLAPSE/SPLIT for?Quoted 8 lines
Two user-space data sources produce the candidate address ranges:
1. ARM SPE (ARMv8.2+): perf record (SPE) -> per-2MB hot-fraction histogram ->
PA->VAvia /proc/<pid>/pagemap -> sparse-THP VA ranges. SPE reads physical addresses from the CPU pipeline, bypassing the TLB and page tables, so it is immune to T1 and T2.Quoted 8 lines
2. smaps fallback (no SPE): scan /proc/<pid>/smaps for THP-backed VMAs and treat the 2MB-aligned ranges as split candidates.
The SPE profiler stays in user space deliberately: the SPE PMU is a single-consumer resource, so a kernel consumer would lock out user-space perf and tooling (x86 PEBS / AMD IBS have the same property). Keeping it in user space avoids that and keeps the metric source pluggable, in line with DAMON-X.
Maybe you are mentioning the perf events based DAMON, not DAMON-X.
And I understand you plan to extend DAMON to use ARM SPE, on top of the perf events based DAMON as a future work. As I mentioned before, I think that makes perfect sense and I'm aligned. Maybe this paragraph can bit reworded to make it more clear, though.
Quoted 9 lines
This is why v2 drops the v1
SPE debugfs patch.Testing
Tested on aarch64 with this series applied to 7.1.0-rc5, THP=always, using a
DAMOS_SPLITscheme (target_order=2, min_nr_accesses=0) and a single DAMOS address filter selecting one 2MB-aligned range:Quoted 13 lines
- Anonymous THP: the filter splits exactly that one THP -- sz_applied=2MB and AnonHugePages drops by 2MB, the rest of the 256MB mapping untouched.
- File-backed THP (tmpfs/shmem mounted huge=always): the same setup splits exactly one 2MB shmem THP -- sz_applied=2MB and ShmemPmdMapped drops by 2MB. This confirmssplit_folio_to_order()works for shmem folios (the KVM-guest-on-THP-tmpfs case).
- The address filter is what bounds the action:sz_triedcovers the whole ~2GB monitored region whilesz_appliedis exactly the 2MB the filter selected.
- A smaps-based path (for hosts without SPE) enumerates THP-backed ranges and splits all THP in the target workload.
- checkpatch clean on all 5 patches.So, you tested only split part, for functionality. Do you have plans to further test collapse part, and performance?
Quoted 3 lines
Test scripts and SPE-to-DAMON pipeline tools:
https://github.com/lianux-mm/damon_spe/tree/v2Thank you for sharing the code!
So, I find rooms to improve on this cover letter for the readability and clarity of the idea. But as I mentioned before, I like the overall idea of this series.
Quoted 4 lines
Thanks,
SJ[...]
Sent using hkml (https://github.com/sjp38/hackermail)