mm/damon: introduce damon_rand_fast() for per-ctx PRNG
Subsystems (2)
PATCH 1/2 mm/damon: introduce damon_rand_fast() for per-ctx PRNG
From: Jiayuan Chen <[email protected]>
damon_rand() on the sampling_addr hot path calls get_random_u32_below(), which takes a local_lock_irqsave() around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with large nr_regions (20k+), this shows up as a large fraction of kdamond CPU
time: the lock_acquire / local_lock pair plus __get_random_u32_below() dominate perf profiles.
Introduce damon_rand_fast(), which uses a lockless lfsr113 generator (struct rnd_state) held per damon_ctx and seeded from get_random_u64() in damon_new_ctx(). kdamond is the sole consumer of a given ctx, so no synchronization is required. Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a 64-bit division; residual bias is bounded by span / 2^32, negligible for statistical sampling.
The new helper is intended for the sampling-address hot path only. damon_rand() is kept for call sites that run outside the kdamond loop and/or have no ctx available (damon_split_regions_of(), kunit tests).
Convert the two hot callers:
- __damon_pa_prepare_access_check()
- __damon_va_prepare_access_check()
lfsr113 is a linear PRNG and MUST NOT be used for anything security-sensitive. DAMON's sampling_addr is not exposed to userspace and is only consumed as a probe point for PTE accessed-bit sampling, so a non-cryptographic PRNG is appropriate here.
Tested with paddr monitoring and max_nr_regions=20000: kdamond CPU usage reduced from ~72% to ~50% of one core.
--- include/linux/damon.h | 28 ++++++++++++++++++++++++++++ mm/damon/core.c | 2 ++ mm/damon/paddr.c | 10 +++++----- mm/damon/vaddr.c | 9 +++++---- 4 files changed, 40 insertions(+), 9 deletions(-)
include/linux/damon.h +28 -0
@@ -10,6 +10,7 @@#include <linux/memcontrol.h> #include <linux/mutex.h> +#include <linux/prandom.h> #include <linux/time64.h> #include <linux/types.h> #include <linux/random.h>
@@ -843,8 +844,35 @@ struct damon_ctx {struct list_head adaptive_targets;struct list_head schemes; + +/* +* Per-ctx lockless PRNG state for damon_rand_fast(). Seeded from +* get_random_u64() in damon_new_ctx(). Owned exclusively by the +* kdamond thread of this ctx, so no locking is required. +*/ +struct rnd_state rnd_state; }; +/* +* damon_rand_fast - per-ctx PRNG variant of damon_rand() for hot paths. +* +* Uses the lockless lfsr113 state kept in @ctx->rnd_state. Safe because +* kdamond is the single consumer of a given ctx, so no synchronization +* is required. Quality is sufficient for statistical sampling; do NOT +* use for any security-sensitive randomness. +* +* Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a division; +* bias is bounded by span / 2^32, negligible for DAMON. +*/ +static inline unsigned long damon_rand_fast (struct damon_ctx *ctx, +unsigned long l,unsigned long r) +{ +u32 rnd =prandom_u32_state (&ctx->rnd_state); +u32 span = (u32)(r - l); + +return l + (unsigned long )(((u64 )rnd * span) >>32 ); +} +static inline struct damon_region *damon_next_region (struct damon_region *r) { return container_of(r->list.next, struct damon_region, list);
mm/damon/core.c +2 -0
@@ -607,6 +607,8 @@ struct damon_ctx *damon_new_ctx(void)INIT_LIST_HEAD (&ctx->adaptive_targets);INIT_LIST_HEAD (&ctx->schemes); +prandom_seed_state (&ctx->rnd_state,get_random_u64 ()); +return ctx; }
mm/damon/paddr.c +5 -5
@@ -48,12 +48,12 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); } -static void __damon_pa_prepare_access_check (struct damon_region *r, -unsigned long addr_unit) +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_region *r) { - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end); -damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, addr_unit)); +damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, ctx->addr_unit)); }static void damon_pa_prepare_access_checks (struct damon_ctx *ctx)
@@ -63,7 +63,7 @@ static void damon_pa_prepare_access_checks(struct damon_ctx *ctx)damon_for_each_target (t, ctx) {damon_for_each_region (r, t) -__damon_pa_prepare_access_check (r, ctx->addr_unit); +__damon_pa_prepare_access_check (ctx, r); } }
mm/damon/vaddr.c +5 -4
@@ -332,10 +332,11 @@ static void damon_va_mkold(struct mm_struct *mm, unsigned long addr)* Functions for the access checking of the regions */ -static void __damon_va_prepare_access_check (struct mm_struct *mm, -struct damon_region *r) +static void __damon_va_prepare_access_check (struct damon_ctx *ctx, +struct mm_struct *mm, +struct damon_region *r) { - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end);damon_va_mkold (mm, r->sampling_addr); }
@@ -351,7 +352,7 @@ static void damon_va_prepare_access_checks(struct damon_ctx *ctx)if (!mm)continue ;damon_for_each_region (r, t) -__damon_va_prepare_access_check (mm, r); +__damon_va_prepare_access_check (ctx, mm, r);mmput (mm); } }
Signature · 1 line
2.43.0
PATCH 2/2 mm/damon/paddr: prefetch struct page of the next region
From: Jiayuan Chen <[email protected]>
In paddr mode with large nr_regions (20k+), damon_get_folio() dominates kdamond CPU time. perf annotate shows ~98% of its samples on a single
load: reading page->compound_head for page_folio(). DAMON samples a random PFN per region, so the access pattern scatters across the vmemmap with no locality that hardware prefetchers can exploit; every compound_head load misses to DRAM (~250 cycles).
Issue a software prefetch for the next region's struct page one iteration before it is needed. In __damon_pa_prepare_access_check(), the next region's sampling_addr is picked eagerly (one iteration ahead of where its mkold will run) so the prefetched line is usable, and the first region of each epoch is handled with a one-off branch since it has no predecessor to have pre-picked its addr. damon_pa_check_accesses() just prefetches based on sampling_addr already populated by the preceding prepare pass.
Two details worth calling out:
- prefetchw() rather than prefetch(): compound_head (+0x8) and
_refcount (+0x34) share a 64B cacheline. The subsequent
folio_try_get() / folio_put() atomics write _refcount, so bringing
the line in exclusive state avoids a later S->M coherence upgrade. - pfn_to_page() rather than pfn_to_online_page(): on CONFIG_SPARSEMEM_VMEMMAP the former is pure arithmetic (vmemmap + pfn), while the latter walks the mem_section table. The mem_section lookup itself incurs a DRAM miss for random PFNs, which would serialize what is supposed to be a non-blocking hint - an earlier attempt that used pfn_to_online_page() saw the prefetch path's internal stall dominate perf (~91% skid on the converge point after the call). Prefetching an unmapped vmemmap entry is
safe: the hint is dropped without faulting.
Concurrency: list_next_entry() and &list == &head comparisons are safe without locking because kdamond is the sole mutator of the region list of its ctx; external threads must go through damon_call() which defers execution to the same kdamond thread between sampling iterations (see documentation on struct damon_ctx and damon_call()).
Tested with paddr monitoring, max_nr_regions=20000, and stress-ng-vm consuming ~90% of memory across 8 workers: kdamond CPU further reduced from ~50% to ~40% of one core on top of the earlier damon_rand_fast() change.
--- mm/damon/paddr.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-)
mm/damon/paddr.c +28 -3
@@ -10,6 +10,7 @@#include <linux/mmu_notifier.h> #include <linux/page_idle.h> #include <linux/pagemap.h> +#include <linux/prefetch.h> #include <linux/rmap.h> #include <linux/swap.h> #include <linux/memory-tiers.h>
@@ -48,10 +49,29 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); } +static void damon_pa_prefetch_page (unsigned long sampling_addr, +unsigned long addr_unit) +{ +phys_addr_t paddr =damon_pa_phys_addr (sampling_addr, addr_unit); + +prefetchw (pfn_to_page (PHYS_PFN (paddr))); +} +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_target *t,struct damon_region *r) { - r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end); +struct damon_region *next =list_next_entry (r, list); + +/* First region has no predecessor to have pre-picked its addr. */ +if (r->list.prev == &t->regions_list) + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end); + +if (&next->list != &t->regions_list) { + next->sampling_addr =damon_rand_fast (ctx, next->ar.start, + next->ar.end); +damon_pa_prefetch_page (next->sampling_addr, ctx->addr_unit); + }damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, ctx->addr_unit)); }
@@ -63,7 +83,7 @@ static void damon_pa_prepare_access_checks(struct damon_ctx *ctx)damon_for_each_target (t, ctx) {damon_for_each_region (r, t) -__damon_pa_prepare_access_check (ctx, r); +__damon_pa_prepare_access_check (ctx, t, r); } }
@@ -106,11 +126,16 @@ static void __damon_pa_check_access(struct damon_region *r,static unsigned int damon_pa_check_accesses (struct damon_ctx *ctx) {struct damon_target *t; -struct damon_region *r; +struct damon_region *r, *next;unsigned int max_nr_accesses =0 ;damon_for_each_target (t, ctx) {damon_for_each_region (r, t) { + next =list_next_entry (r, list); +if (&next->list != &t->regions_list) +damon_pa_prefetch_page (next->sampling_addr, + ctx->addr_unit); +__damon_pa_check_access ( r, &ctx->attrs, ctx->addr_unit); max_nr_accesses =max (r->nr_accesses, max_nr_accesses);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/2] mm/damon/paddr: prefetch struct page of the next region
Sashiko AI review found 1 potential issue(s): - [High] Unsafe use of `pfn_to_page()` on unvalidated PFNs causes kernel panic on SPARSEMEM configurations without VMEMMAP. --
Sashiko AI review found 1 potential issue(s):
- [High] Unsafe use of pfn_to_page() on unvalidated PFNs causes kernel panic on SPARSEMEM configurations without VMEMMAP. --
commit 8abf0dd953e91eace9153afe00585b922bbe2751
Author: Jiayuan Chen <[email protected]>
mm/damon/paddr: prefetch struct page of the next region
This commit introduces software prefetching for the next region's struct page in DAMON's physical address mode to reduce cache misses. It uses pfn_to_page() instead of pfn_to_online_page() to avoid the serialization costs of looking up the memory section, expecting prefetchw() to safely drop hints for unmapped addresses.
Quoted 9 lines
-
pfn_to_page()rather thanpfn_to_online_page(): onCONFIG_SPARSEMEM_VMEMMAPthe former is pure arithmetic (vmemmap + pfn), while the latter walks themem_sectiontable. Themem_sectionlookup itself incurs a DRAM miss for random PFNs, which would serialize what is supposed to be a non-blocking hint - an earlier attempt that usedpfn_to_online_page()saw the prefetch path's internal stall dominate perf (~91% skid on the converge point after the call). Prefetching an unmapped vmemmap entry is
safe: the hint is dropped without faulting.
Does this logic hold for systems using CONFIG_SPARSEMEM without VMEMMAP?
While it is true that pfn_to_page() is pure arithmetic on VMEMMAP, under classic SPARSEMEM it requires a section lookup to evaluate.
Quoted 4 lines
mm/damon/paddr.c +0
-0
[ ... ]
mm/damon/paddr.c static void damon_pa_mkold(phys_addr_t paddr) · line 57
Quoted 10 lines
mm/damon/paddr.c +6
-0@@ -48,10 +49,29 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); } +static void damon_pa_prefetch_page (unsigned long sampling_addr, +unsigned long addr_unit) +{ +phys_addr_t paddr =damon_pa_phys_addr (sampling_addr, addr_unit); + +prefetchw (pfn_to_page (PHYS_PFN (paddr)));
Can this cause a kernel panic on CONFIG_SPARSEMEM configurations without VMEMMAP?
In DAMON paddr mode, target regions can span physical memory holes. Since damon_rand_fast() selects a random physical address, it might select an unpopulated area.
If the random PFN falls into an unallocated section, __pfn_to_section() will be invalid. Attempting to read the section_mem_map from it will lead to an immediate NULL pointer dereference in C code before the prefetchw instruction is ever reached.
Quoted 2 lines
mm/damon/paddr.c +2
-0+} +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
SeongJae Park
maintainerRe: [PATCH 2/2] mm/damon/paddr: prefetch struct page of the next region
In reply to Jiayuan ChenQuoted 25 lines · On Thu, 23 Apr 2026 20:23:37 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
In paddr mode with large
nr_regions(20k+),damon_get_folio()dominates kdamond CPU time. perf annotate shows ~98% of its samples on a single
load: readingpage->compound_headforpage_folio(). DAMON samples a random PFN per region, so the access pattern scatters across the vmemmap with no locality that hardware prefetchers can exploit; everycompound_headload misses to DRAM (~250 cycles).Issue a software prefetch for the next region's struct page one iteration before it is needed. In
__damon_pa_prepare_access_check(), the next region'ssampling_addris picked eagerly (one iteration ahead of where its mkold will run) so the prefetched line is usable, and the first region of each epoch is handled with a one-off branch since it has no predecessor to have pre-picked its addr.damon_pa_check_accesses()just prefetches based onsampling_addralready populated by the preceding prepare pass.Two details worth calling out:
-
prefetchw()rather thanprefetch():compound_head(+0x8) and_refcount (+0x34) share a 64B cacheline. The subsequent folio_try_get() / folio_put() atomics write _refcount, so bringing the line in exclusive state avoids a later S->M coherence upgrade.
Quoted 9 lines
-
pfn_to_page()rather thanpfn_to_online_page(): onCONFIG_SPARSEMEM_VMEMMAPthe former is pure arithmetic (vmemmap + pfn), while the latter walks themem_sectiontable. Themem_sectionlookup itself incurs a DRAM miss for random PFNs, which would serialize what is supposed to be a non-blocking hint - an earlier attempt that usedpfn_to_online_page()saw the prefetch path's internal stall dominate perf (~91% skid on the converge point after the call). Prefetching an unmapped vmemmap entry is
safe: the hint is dropped without faulting.
Sashiko raises [1] a concern about this. Could you please reply?
Quoted 11 lines
Concurrency:
list_next_entry()and &list == &head comparisons are safe without locking because kdamond is the sole mutator of the region list of its ctx; external threads must go throughdamon_call()which defers execution to the same kdamond thread between sampling iterations (see documentation on structdamon_ctxanddamon_call()).Tested with paddr monitoring, max_nr_regions=20000, and stress-ng-vm consuming ~90% of memory across 8 workers: kdamond CPU further reduced from ~50% to ~40% of one core on top of the earlier
damon_rand_fast()change.
This patch feels bit complicated. I'm not sure if the performance gain is really big enough to compensate the added complexity. As I also asked to the previous patch, could you please share more details about your use case?
I will review the code in detail after the above high level question and concern are addressed.
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ
[...]
[email protected]
AI reviewSashiko AI review found 1 potential issue(s): - [High] damon_rand_fast() truncates the sampling range to 32 bits, preventing DAMON from monitoring the upper parts of memory regions larger than 4GB. --
Sashiko AI review found 1 potential issue(s):
- [High] damon_rand_fast() truncates the sampling range to 32 bits, preventing DAMON from monitoring the upper parts of memory regions larger than 4GB. --
commit 094f8506b34920335d2088d576411d78378a8a58
Author: Jiayuan Chen <[email protected]>
mm/damon: introduce damon_rand_fast() for per-ctx PRNG
This commit introduces a lockless, per-ctx PRNG function for the DAMON sampling address hot path. It replaces the heavier get_random_u32_below() call to reduce kdamond CPU usage on workloads with many regions.
Quoted 4 lines
include/linux/damon.h +0
-0
[ ... ]
Quoted 1 line
include/linux/damon.h +0
-0@@ -843,8 +844,35 @@ struct damon_ctx {
[ ... ]
include/linux/damon.h struct damon_ctx { · line 871
Quoted 5 lines
include/linux/damon.h +5
-0+static inline unsigned long damon_rand_fast (struct damon_ctx *ctx, +unsigned long l,unsigned long r) +{ +u32 rnd =prandom_u32_state (&ctx->rnd_state); +u32 span = (u32)(r - l);
Does this cast truncate the memory region span to 32 bits, preventing the sampling of addresses beyond the first 4GB of a region?
If a region is larger than 4GB (which can occur with large anonymous mappings or physical memory ranges), the explicit cast to u32 will wrap around. For example, a 5GB region would result in a span of 1GB, and an exactly 4GB region would result in a span of 0.
This would cause the PRNG to only sample addresses within that truncated span, leaving the rest of the memory region unmonitored. While the previous damon_rand() implementation had a similar limitation by passing an unsigned long to get_random_u32_below(), could this be updated to correctly handle 64-bit region spans?
Quoted 3 lines
include/linux/damon.h +3
-0+ +return l + (unsigned long )(((u64 )rnd * span) >>32 ); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
SeongJae Park
maintainerHello Jiayuan,
Thank you for sharing this patch with us!
Quoted 8 lines · On Thu, 23 Apr 2026 20:23:36 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
damon_rand()on thesampling_addrhot path callsget_random_u32_below(), which takes alocal_lock_irqsave()around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with largenr_regions(20k+), this shows up as a large fraction of kdamond CPU
time: thelock_acquire/local_lockpair plus__get_random_u32_below()dominate perf profiles.
Could you please share more details about the use case? I'm particularly curious how you ended up setting 'nr_regiions' that high, while the upper limit of nr_regions is set to 1,0000 by default.
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Quoted 68 lines
Introduce
damon_rand_fast(), which uses a lockless lfsr113 generator (structrnd_state) held perdamon_ctxand seeded fromget_random_u64()indamon_new_ctx(). kdamond is the sole consumer of a given ctx, so no synchronization is required. Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a 64-bit division; residual bias is bounded by span / 2^32, negligible for statistical sampling.The new helper is intended for the sampling-address hot path only.
damon_rand()is kept for call sites that run outside the kdamond loop and/or have no ctx available (damon_split_regions_of(), kunit tests).Convert the two hot callers:
-
__damon_pa_prepare_access_check()
-__damon_va_prepare_access_check()lfsr113 is a linear PRNG and MUST NOT be used for anything security-sensitive. DAMON's
sampling_addris not exposed to userspace and is only consumed as a probe point for PTE accessed-bit sampling, so a non-cryptographic PRNG is appropriate here.Tested with paddr monitoring and max_nr_regions=20000: kdamond CPU usage reduced from ~72% to ~50% of one core.
Cc Jiayuan Chen <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>--- include/linux/damon.h | 28 ++++++++++++++++++++++++++++ mm/damon/core.c | 2 ++ mm/damon/paddr.c | 10 +++++----- mm/damon/vaddr.c | 9 +++++---- 4 files changed, 40 insertions(+), 9 deletions(-)include/linux/damon.h +17
-0@@ -10,6 +10,7 @@#include <linux/memcontrol.h> #include <linux/mutex.h> +#include <linux/prandom.h> #include <linux/time64.h> #include <linux/types.h> #include <linux/random.h> @@ -843,8 +844,35 @@ struct damon_ctx {struct list_head adaptive_targets;struct list_head schemes; + +/* +* Per-ctx lockless PRNG state for damon_rand_fast(). Seeded from +* get_random_u64() in damon_new_ctx(). Owned exclusively by the +* kdamond thread of this ctx, so no locking is required. +*/ +struct rnd_state rnd_state; }; +/* +* damon_rand_fast - per-ctx PRNG variant of damon_rand() for hot paths. +* +* Uses the lockless lfsr113 state kept in @ctx->rnd_state. Safe because +* kdamond is the single consumer of a given ctx, so no synchronization +* is required. Quality is sufficient for statistical sampling; do NOT +* use for any security-sensitive randomness. +* +* Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a division;
include/linux/damon.h struct damon_ctx { · line 874
Quoted 10 lines
include/linux/damon.h +10
-0+* bias is bounded by span / 2^32, negligible for DAMON. +*/ +static inline unsigned long damon_rand_fast (struct damon_ctx *ctx, +unsigned long l,unsigned long r) +{ +u32 rnd =prandom_u32_state (&ctx->rnd_state); +u32 span = (u32)(r - l); + +return l + (unsigned long )(((u64 )rnd * span) >>32 ); +}
As Sashiko pointed out [2], we may better to return 'unsigned long' from this function. Can this algorithm be extended for that?
Quoted 22 lines
include/linux/damon.h +1
-0+static inline struct damon_region *damon_next_region (struct damon_region *r) { return container_of(r->list.next, struct damon_region, list);mm/damon/core.c +2
-0@@ -607,6 +607,8 @@ struct damon_ctx *damon_new_ctx(void)INIT_LIST_HEAD (&ctx->adaptive_targets);INIT_LIST_HEAD (&ctx->schemes); +prandom_seed_state (&ctx->rnd_state,get_random_u64 ()); +return ctx; }mm/damon/paddr.c +0
-0
mm/damon/paddr.c static void damon_pa_mkold(phys_addr_t paddr) · line 52
Quoted 7 lines
mm/damon/paddr.c +2
-2@@ -48,12 +48,12 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); } -static void __damon_pa_prepare_access_check (struct damon_region *r, -unsigned long addr_unit) +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_region *r)
Let's keep 'r' on the first line, and update the second line without indent change.
Quoted 23 lines
mm/damon/paddr.c +3
-3{ - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end); -damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, addr_unit)); +damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, ctx->addr_unit)); }static void damon_pa_prepare_access_checks (struct damon_ctx *ctx)@@ -63,7 +63,7 @@ static void damon_pa_prepare_access_checks(struct damon_ctx *ctx)damon_for_each_target (t, ctx) {damon_for_each_region (r, t) -__damon_pa_prepare_access_check (r, ctx->addr_unit); +__damon_pa_prepare_access_check (ctx, r); } }mm/damon/vaddr.c +0
-0
mm/damon/vaddr.c static void damon_va_mkold(struct mm_struct *mm, unsigned long addr) · line 337
Quoted 8 lines
mm/damon/vaddr.c +3
-2@@ -332,10 +332,11 @@ static void damon_va_mkold(struct mm_struct *mm, unsigned long addr)* Functions for the access checking of the regions */ -static void __damon_va_prepare_access_check (struct mm_struct *mm, -struct damon_region *r) +static void __damon_va_prepare_access_check (struct damon_ctx *ctx, +struct mm_struct *mm, +struct damon_region *r)
Let's keep the first line and the the indentation as were, and add 'ctx' argument to the end.
Quoted 19 lines
mm/damon/vaddr.c +2
-2{ - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end);damon_va_mkold (mm, r->sampling_addr); }@@ -351,7 +352,7 @@ static void damon_va_prepare_access_checks(struct damon_ctx *ctx)if (!mm)continue ;damon_for_each_region (r, t) -__damon_va_prepare_access_check (mm, r); +__damon_va_prepare_access_check (ctx, mm, r);mmput (mm); } }Signature · 1 line
2.43.0
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lore.kernel.org/[email protected]
Thanks,
SJ
Hello SJ,
Thank you for the review.
Quoted 15 lines · SeongJae Park
Hello Jiayuan,
Thank you for sharing this patch with us!
Quoted 8 lines · On Thu, 23 Apr 2026 20:23:36 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
damon_rand()on thesampling_addrhot path callsget_random_u32_below(), which takes alocal_lock_irqsave()around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with largenr_regions(20k+), this shows up as a large fraction of kdamond CPU
time: thelock_acquire/local_lockpair plus__get_random_u32_below()dominate perf profiles.
Quoted 3 lines
Could you please share more details about the use case? I'm particularly curious how you ended up setting '
nr_regiions' that high, while the upper limit ofnr_regionsis set to 1,0000 by default.
We use DAMON paddr on a 2 TiB host for per-cgroup hot/cold page classification. Target cgroups can be as small as 1-2% of total memory (tens of GiB).
With the default max_nr_regions=1000, each region covers ~2 GiB on average, which is often larger than the entire target cgroup. Adaptive split cannot carve out cgroup-homogeneous regions in that
regime: each region's nr_accesses averages the (small) cgroup fraction with the surrounding non-cgroup pages, and the cgroup's access signal gets washed out.
Raising max_nr_regions to 10k-20k gives the adaptive split enough spatial resolution that cgroup-majority regions can form at cgroup boundaries (allocations have enough physical locality in practice for this to work -- THP, per-node allocation, etc.). At that region count, damon_rand() starts showing up at the top of kdamond profiles, which is what motivated this patch.
Quoted 8 lines
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Thanks for pointing to intervals auto-tuning - we do use it. But it
trades sampling frequency against monitoring overhead; it cannot change the spatial resolution. With N=1000 regions on a 2 TiB host, a 20 GiB cgroup cannot be resolved no matter how we tune sample_us / aggr_us, because the region boundary itself averages cgroup and
non-cgroup pages together.
So raising max_nr_regions and making the per-region overhead cheap are complementary to interval auto-tuning, not redundant with it.
Quoted 71 lines
Quoted 71 lines
Introduce
damon_rand_fast(), which uses a lockless lfsr113 generator (structrnd_state) held perdamon_ctxand seeded fromget_random_u64()indamon_new_ctx(). kdamond is the sole consumer of a given ctx, so no synchronization is required. Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a 64-bit division; residual bias is bounded by span / 2^32, negligible for statistical sampling.The new helper is intended for the sampling-address hot path only.
damon_rand()is kept for call sites that run outside the kdamond loop and/or have no ctx available (damon_split_regions_of(), kunit tests).Convert the two hot callers:
-
__damon_pa_prepare_access_check()
-__damon_va_prepare_access_check()lfsr113 is a linear PRNG and MUST NOT be used for anything security-sensitive. DAMON's
sampling_addris not exposed to userspace and is only consumed as a probe point for PTE accessed-bit sampling, so a non-cryptographic PRNG is appropriate here.Tested with paddr monitoring and max_nr_regions=20000: kdamond CPU usage reduced from ~72% to ~50% of one core.
Cc Jiayuan Chen <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>--- include/linux/damon.h | 28 ++++++++++++++++++++++++++++ mm/damon/core.c | 2 ++ mm/damon/paddr.c | 10 +++++----- mm/damon/vaddr.c | 9 +++++---- 4 files changed, 40 insertions(+), 9 deletions(-)include/linux/damon.h +21
-0@@ -10,6 +10,7 @@#include <linux/memcontrol.h> #include <linux/mutex.h> +#include <linux/prandom.h> #include <linux/time64.h> #include <linux/types.h> #include <linux/random.h> @@ -843,8 +844,35 @@ struct damon_ctx {struct list_head adaptive_targets;struct list_head schemes; + +/* +* Per-ctx lockless PRNG state for damon_rand_fast(). Seeded from +* get_random_u64() in damon_new_ctx(). Owned exclusively by the +* kdamond thread of this ctx, so no locking is required. +*/ +struct rnd_state rnd_state; }; +/* +* damon_rand_fast - per-ctx PRNG variant of damon_rand() for hot paths. +* +* Uses the lockless lfsr113 state kept in @ctx->rnd_state. Safe because +* kdamond is the single consumer of a given ctx, so no synchronization +* is required. Quality is sufficient for statistical sampling; do NOT +* use for any security-sensitive randomness. +* +* Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a division; +* bias is bounded by span / 2^32, negligible for DAMON. +*/ +static inline unsigned long damon_rand_fast (struct damon_ctx *ctx, +unsigned long l,unsigned long r)
Quoted 8 lines
include/linux/damon.h struct damon_ctx { · line 874
Quoted 6 lines
include/linux/damon.h +6
-0+{ +u32 rnd =prandom_u32_state (&ctx->rnd_state); +u32 span = (u32)(r - l); + +return l + (unsigned long )(((u64 )rnd * span) >>32 ); +}As Sashiko pointed out [2], we may better to return 'unsigned long' from this function. Can this algorithm be extended for that?
Sashiko is correct that (u32)(r - l) truncates spans greater than 4 GiB.
This is a pre-existing limitation of damon_rand() itself, which passes r - l to get_random_u32_below() (a u32 parameter) and truncates the same way. My patch makes that truncation explicit but does not introduce a new bug.
We can restrict the region size when config or just allow spans greater than 4G.
static inline unsigned long damon_rand_fast(struct damon_ctx *ctx, unsigned long l, unsigned long r)
{
unsigned long span = r - l;
u64 rnd;
if (likely(span <= U32_MAX)) {
rnd = prandom_u32_state(&ctx->rnd_state); return l + (unsigned long)((rnd * span) >> 32); }
rnd = ((u64)prandom_u32_state(&ctx->rnd_state) << 32) | prandom_u32_state(&ctx->rnd_state);
// same as 'l + (unsigned long)((rnd * span) >> 32);' return l + mul_u64_u64_shr(rnd, span, 64);
}
Quoted 25 lines
Quoted 25 lines
include/linux/damon.h +1
-0+static inline struct damon_region *damon_next_region (struct damon_region *r) { return container_of(r->list.next, struct damon_region, list);mm/damon/core.c +2
-0@@ -607,6 +607,8 @@ struct damon_ctx *damon_new_ctx(void)INIT_LIST_HEAD (&ctx->adaptive_targets);INIT_LIST_HEAD (&ctx->schemes); +prandom_seed_state (&ctx->rnd_state,get_random_u64 ()); +return ctx; }mm/damon/paddr.c +0
-0@@ -48,12 +48,12 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); }
Quoted 7 lines
mm/damon/paddr.c static void damon_pa_mkold(phys_addr_t paddr) · line 52
Quoted 4 lines
mm/damon/paddr.c +2
-2-static void __damon_pa_prepare_access_check (struct damon_region *r, -unsigned long addr_unit) +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_region *r)Let's keep 'r' on the first line, and update the second line without indent change.
Thanks
Quoted 59 lines
Quoted 23 lines
mm/damon/paddr.c +3
-3{ - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end); -damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, addr_unit)); +damon_pa_mkold (damon_pa_phys_addr (r->sampling_addr, ctx->addr_unit)); }static void damon_pa_prepare_access_checks (struct damon_ctx *ctx)@@ -63,7 +63,7 @@ static void damon_pa_prepare_access_checks(struct damon_ctx *ctx)damon_for_each_target (t, ctx) {damon_for_each_region (r, t) -__damon_pa_prepare_access_check (r, ctx->addr_unit); +__damon_pa_prepare_access_check (ctx, r); } }mm/damon/vaddr.c +0
-0mm/damon/vaddr.c static void damon_va_mkold(struct mm_struct *mm, unsigned long addr) · line 337
Quoted 8 lines
mm/damon/vaddr.c +3
-2@@ -332,10 +332,11 @@ static void damon_va_mkold(struct mm_struct *mm, unsigned long addr)* Functions for the access checking of the regions */ -static void __damon_va_prepare_access_check (struct mm_struct *mm, -struct damon_region *r) +static void __damon_va_prepare_access_check (struct damon_ctx *ctx, +struct mm_struct *mm, +struct damon_region *r)Let's keep the first line and the the indentation as were, and add 'ctx' argument to the end.
Quoted 19 lines
mm/damon/vaddr.c +2
-2{ - r->sampling_addr =damon_rand (r->ar.start, r->ar.end); + r->sampling_addr =damon_rand_fast (ctx, r->ar.start, r->ar.end);damon_va_mkold (mm, r->sampling_addr); }@@ -351,7 +352,7 @@ static void damon_va_prepare_access_checks(struct damon_ctx *ctx)if (!mm)continue ;damon_for_each_region (r, t) -__damon_va_prepare_access_check (mm, r); +__damon_va_prepare_access_check (ctx, mm, r);mmput (mm); } }Signature · 1 line
2.43.0[1] https://lkml.kernel.org/r/[email protected]
[2] https://lore.kernel.org/[email protected]Thanks,
SJ
SeongJae Park
maintainerQuoted 20 lines · On Fri, 24 Apr 2026 10:29:58 +0800 Jiayuan Chen <[email protected]> wrote:
Hello SJ,
Thank you for the review.
Quoted 15 lines · SeongJae Park
Hello Jiayuan,
Thank you for sharing this patch with us!
Quoted 8 lines · On Thu, 23 Apr 2026 20:23:36 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
damon_rand()on thesampling_addrhot path callsget_random_u32_below(), which takes alocal_lock_irqsave()around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with largenr_regions(20k+), this shows up as a large fraction of kdamond CPU
time: thelock_acquire/local_lockpair plus__get_random_u32_below()dominate perf profiles.
Quoted 7 lines
Quoted 3 lines
Could you please share more details about the use case? I'm particularly curious how you ended up setting '
nr_regiions' that high, while the upper limit ofnr_regionsis set to 1,0000 by default.We use DAMON paddr on a 2 TiB host for per-cgroup hot/cold page classification. Target cgroups can be as small as 1-2% of total memory (tens of GiB).
Thank you for sharing this detail!
Could you further share me what do you do with the hot/cold information? We found some people want to use DAMON for proactive cold pages reclamation but they watned to use it for only file-backed pages, and therefore developed page level DAMOS filter. In a similar way, if we know your detailed use case, we might be able to serve you better by finding not well advertised features, or developing a new features.
Quoted 7 lines
With the default max_nr_regions=1000, each region covers ~2 GiB on average, which is often larger than the entire target cgroup. Adaptive split cannot carve out cgroup-homogeneous regions in that
regime: each region'snr_accessesaverages the (small) cgroup fraction with the surrounding non-cgroup pages, and the cgroup's access signal gets washed out.
But, pages of cgroups would be scattered around on the physical address space. So even if DAMON finds a hot or cold region on physical address space that having a size smaller than a cgroup's memory, it is hard to say if it is for a specific cgroup. How do you know if the region is for which cgroup? Do you have a way to make sure pages of same cgroup are gathered on the physical address space? If not, I'm not sure how ensuring size of region to be equal or smaller than each cgroup size helps you.
We are supporting page level monitoring [1] for such cgroup-aware use case. Are you using it? But again, if you use it, I'm not sure why you need to ensure DAMON region size smaller than the cgroup size.
Fyi, I'm also working [2] on making the cgroup-aware monitoring much more lightweight.
Quoted 7 lines
Raising
max_nr_regionsto 10k-20k gives the adaptive split enough spatial resolution that cgroup-majority regions can form at cgroup boundaries (allocations have enough physical locality in practice for this to work -- THP, per-node allocation, etc.). At that region count,damon_rand()starts showing up at the top of kdamond profiles, which is what motivated this patch.
Thank you for sharing how this patch has developed. I'm still curious if there is yet more ways to make DAMON better for your use case. But this patch makes sense in general to me.
Quoted 19 lines
Quoted 10 lines
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Thanks for pointing to intervals auto-tuning - we do use it. But it
trades sampling frequency against monitoring overhead; it cannot change the spatial resolution. With N=1000 regions on a 2 TiB host, a 20 GiB cgroup cannot be resolved no matter how we tune
sample_us/aggr_us, because the region boundary itself averages cgroup and
Quoted 4 lines
non-cgroup pages together.
So raising
max_nr_regionsand making the per-region overhead cheap are complementary to interval auto-tuning, not redundant with it.
Makes sense. I'm still not sure how it helps for cgroup. But, if you do want it and if it helps you, you are of course ok to use it in your way, and I will help you. :)
Quoted 81 lines
Quoted 72 lines
Quoted 72 lines
Introduce
damon_rand_fast(), which uses a lockless lfsr113 generator (structrnd_state) held perdamon_ctxand seeded fromget_random_u64()indamon_new_ctx(). kdamond is the sole consumer of a given ctx, so no synchronization is required. Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a 64-bit division; residual bias is bounded by span / 2^32, negligible for statistical sampling.The new helper is intended for the sampling-address hot path only.
damon_rand()is kept for call sites that run outside the kdamond loop and/or have no ctx available (damon_split_regions_of(), kunit tests).Convert the two hot callers:
-
__damon_pa_prepare_access_check()
-__damon_va_prepare_access_check()lfsr113 is a linear PRNG and MUST NOT be used for anything security-sensitive. DAMON's
sampling_addris not exposed to userspace and is only consumed as a probe point for PTE accessed-bit sampling, so a non-cryptographic PRNG is appropriate here.Tested with paddr monitoring and max_nr_regions=20000: kdamond CPU usage reduced from ~72% to ~50% of one core.
Cc Jiayuan Chen <[email protected]>Signed-off-by Jiayuan Chen <[email protected]>--- include/linux/damon.h | 28 ++++++++++++++++++++++++++++ mm/damon/core.c | 2 ++ mm/damon/paddr.c | 10 +++++----- mm/damon/vaddr.c | 9 +++++---- 4 files changed, 40 insertions(+), 9 deletions(-)include/linux/damon.h +21
-0@@ -10,6 +10,7 @@#include <linux/memcontrol.h> #include <linux/mutex.h> +#include <linux/prandom.h> #include <linux/time64.h> #include <linux/types.h> #include <linux/random.h> @@ -843,8 +844,35 @@ struct damon_ctx {struct list_head adaptive_targets;struct list_head schemes; + +/* +* Per-ctx lockless PRNG state for damon_rand_fast(). Seeded from +* get_random_u64() in damon_new_ctx(). Owned exclusively by the +* kdamond thread of this ctx, so no locking is required. +*/ +struct rnd_state rnd_state; }; +/* +* damon_rand_fast - per-ctx PRNG variant of damon_rand() for hot paths. +* +* Uses the lockless lfsr113 state kept in @ctx->rnd_state. Safe because +* kdamond is the single consumer of a given ctx, so no synchronization +* is required. Quality is sufficient for statistical sampling; do NOT +* use for any security-sensitive randomness. +* +* Range mapping uses Lemire's (u64)rnd * span >> 32 to avoid a division; +* bias is bounded by span / 2^32, negligible for DAMON. +*/ +static inline unsigned long damon_rand_fast (struct damon_ctx *ctx, +unsigned long l,unsigned long r)Quoted 8 lines
include/linux/damon.h struct damon_ctx { · line 874
Quoted 6 lines
include/linux/damon.h +6
-0+{ +u32 rnd =prandom_u32_state (&ctx->rnd_state); +u32 span = (u32)(r - l); + +return l + (unsigned long )(((u64 )rnd * span) >>32 ); +}As Sashiko pointed out [2], we may better to return 'unsigned long' from this function. Can this algorithm be extended for that?
Quoted 6 lines
Sashiko is correct that (u32)(r - l) truncates spans greater than 4 GiB.
This is a pre-existing limitation of
damon_rand()itself, which passes r - l toget_random_u32_below()(a u32 parameter) and truncates the same way. My patch makes that truncation explicit but does not introduce a new bug.
You are 100% correct and I agree. Thank you for making this clear. I should also mentioned and underlined this in my previous reply, but I forgot it.
So I think this patch is ok to be merged as is (after addressing my nit trivial comments about coding styles), but we may still want to fix it in future. So I was wondering if such future fix for this new, shiny and efficient function is easy.
Quoted 11 lines
We can restrict the region size when config or just allow spans greater than 4G.
static inline unsigned long damon_rand_fast(struct
damon_ctx*ctx,
unsigned long l, unsigned long r)
{
unsigned long span = r - l;
u64 rnd;if (
likely(span <= U32_MAX)) {
I think we could drop 'likely' and hope compiler or hardware branch prediction to be smart enough.
Quoted 8 lines
rnd =
prandom_u32_state(&ctx->rnd_state); return l + (unsigned long)((rnd * span) >> 32); }
rnd = ((u64)prandom_u32_state(&ctx->rnd_state) << 32) |prandom_u32_state(&ctx->rnd_state);// same as 'l + (unsigned long)((rnd * span) >> 32);' return l +
mul_u64_u64_shr(rnd, span, 64);
Makes sense to me.
Quoted 28 lines
}
Quoted 25 lines
Quoted 25 lines
include/linux/damon.h +1
-0+static inline struct damon_region *damon_next_region (struct damon_region *r) { return container_of(r->list.next, struct damon_region, list);mm/damon/core.c +2
-0@@ -607,6 +607,8 @@ struct damon_ctx *damon_new_ctx(void)INIT_LIST_HEAD (&ctx->adaptive_targets);INIT_LIST_HEAD (&ctx->schemes); +prandom_seed_state (&ctx->rnd_state,get_random_u64 ()); +return ctx; }mm/damon/paddr.c +0
-0@@ -48,12 +48,12 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); }
Quoted 8 lines
Quoted 7 lines
mm/damon/paddr.c static void damon_pa_mkold(phys_addr_t paddr) · line 52
Quoted 4 lines
mm/damon/paddr.c +2
-2-static void __damon_pa_prepare_access_check (struct damon_region *r, -unsigned long addr_unit) +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_region *r)Let's keep 'r' on the first line, and update the second line without indent change.
Thanks
You're welcome :)
So I'm still unsure if this is the only single and best way for your use case. I'd like to further discuss your use case and find more way to help it if you are also interested. So, I'd like to continue the conversation.
Regardless of it, though, I think this patch is good enough to be merged for general use case, though it require above trivial style changes. I wouldn't strongly request you to solve the >4GiB region issue together, but it would be nice to be added from the beginning. It's up to your choice.
I'm also waiting for your answer to the second patch of this series. I'm still bit skeptical about it, though. To my simple brain, it feels introducing too much complexity. I feel like making this damon_rand() optimization patch merged first and keep discussing your use case and the second patch later might be a path forward. If you agree, please feel free to post a new version of this patch.
[1] https://github.com/damonitor/damo/blob/next/USAGE.md#damo-report-access-page-level-properties-based-access-monitoring
[2] https://lore.kernel.org/linux-mm/[email protected]/
Thanks,
SJ
[...]
Quoted 28 lines · SeongJae Park
Quoted 20 lines · On Fri, 24 Apr 2026 10:29:58 +0800 Jiayuan Chen <[email protected]> wrote:
Hello SJ,
Thank you for the review.
Quoted 15 lines · SeongJae Park
Hello Jiayuan,
Thank you for sharing this patch with us!
Quoted 8 lines · On Thu, 23 Apr 2026 20:23:36 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
damon_rand()on thesampling_addrhot path callsget_random_u32_below(), which takes alocal_lock_irqsave()around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with largenr_regions(20k+), this shows up as a large fraction of kdamond CPU
time: thelock_acquire/local_lockpair plus__get_random_u32_below()dominate perf profiles.Quoted 6 lines
Quoted 3 lines
Could you please share more details about the use case? I'm particularly curious how you ended up setting '
nr_regiions' that high, while the upper limit ofnr_regionsis set to 1,0000 by default.We use DAMON paddr on a 2 TiB host for per-cgroup hot/cold page classification. Target cgroups can be as small as 1-2% of total memory (tens of GiB).
Quoted 9 lines
Thank you for sharing this detail!
Could you further share me what do you do with the hot/cold information? We found some people want to use DAMON for proactive cold pages reclamation but they watned to use it for only file-backed pages, and therefore developed page level DAMOS filter. In a similar way, if we know your detailed use case, we might be able to serve you better by finding not well advertised features, or developing a new features.
Hello SJ,
Thanks.
Use case: mixed-workload Kubernetes host (latency-sensitive + batch pods on the same machine). We need continuous per-pod cold/hot signal for two things -- k8s scheduling decisions (which pod can give up memory under host pressure) and sizing the eventual reclamation through memory.reclaim. Both anon and file pages matter (swap is configured); reclamation itself goes through mainline. What we add is the per-pod cold attribution feeding the scheduler.
DAMON_RECLAIM alone doesn't fit -- it is reclaim-only with aggregate stats, while we need continuous per-pod visibility even when reclamation isn't firing.
Let me re-frame the region-size point, since I don't think I explained it well last time. We are NOT trying to make region size match cgroup size; you're right that pages are scattered and that mapping doesn't work. The high nr_regions is so that DAMON's adaptive split has enough resolution to identify cold sub-areas of physical memory at all -- regardless of cgroup. With ~2 GiB default regions on a 2 TiB host, a small pod's pages are averaged with thousands of non-pod pages in the same region, and the region never reaches nr_accesses=0 even when the pod is genuinely idle. The cold signal is gone before any cgroup attribution happens. Cgroup attribution itself is done at sample granularity (folio_memcg per sampled page), not at region granularity -- the regions just need to be fine enough that there *is* a cold signal to attribute.
Quoted 14 lines
Quoted 6 lines
With the default max_nr_regions=1000, each region covers ~2 GiB on average, which is often larger than the entire target cgroup. Adaptive split cannot carve out cgroup-homogeneous regions in that
regime: each region'snr_accessesaverages the (small) cgroup fraction with the surrounding non-cgroup pages, and the cgroup's access signal gets washed out.But, pages of cgroups would be scattered around on the physical address space. So even if DAMON finds a hot or cold region on physical address space that having a size smaller than a cgroup's memory, it is hard to say if it is for a specific cgroup. How do you know if the region is for which cgroup? Do you have a way to make sure pages of same cgroup are gathered on the physical address space? If not, I'm not sure how ensuring size of region to be equal or smaller than each cgroup size helps you.
Quoted 3 lines
We are supporting page level monitoring [1] for such cgroup-aware use case. Are you using it? But again, if you use it, I'm not sure why you need to ensure DAMON region size smaller than the cgroup size.
This is also why DAMOS memcg filters do not bypass the nr_regions constraint -- the scheme's access-pattern matcher operates on the already-averaged region, so by the time any filter sees pages the signal is already lost. Whatever attribution mechanism we use afterwards (custom callback, memcg filter, page-level reporting), the region count needs to be high enough first.
Quoted 2 lines
Fyi, I'm also working [2] on making the cgroup-aware monitoring much more lightweight.
Looking forward to it -- hopefully it'll cover our case. 😁
Quoted 35 lines
Quoted 6 lines
Raising
max_nr_regionsto 10k-20k gives the adaptive split enough spatial resolution that cgroup-majority regions can form at cgroup boundaries (allocations have enough physical locality in practice for this to work -- THP, per-node allocation, etc.). At that region count,damon_rand()starts showing up at the top of kdamond profiles, which is what motivated this patch.Thank you for sharing how this patch has developed. I'm still curious if there is yet more ways to make DAMON better for your use case. But this patch makes sense in general to me.
Quoted 17 lines
Quoted 9 lines
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Thanks for pointing to intervals auto-tuning - we do use it. But it
trades sampling frequency against monitoring overhead; it cannot change the spatial resolution. With N=1000 regions on a 2 TiB host, a 20 GiB cgroup cannot be resolved no matter how we tune
sample_us/aggr_us, because the region boundary itself averages cgroup andQuoted 4 lines
non-cgroup pages together.
So raising
max_nr_regionsand making the per-region overhead cheap are complementary to interval auto-tuning, not redundant with it.Makes sense. I'm still not sure how it helps for cgroup. But, if you do want it and if it helps you, you are of course ok to use it in your way, and I will help you. :)
[...]
Quoted 11 lines
Quoted 6 lines
Sashiko is correct that (u32)(r - l) truncates spans greater than 4 GiB.
This is a pre-existing limitation of
damon_rand()itself, which passes r - l toget_random_u32_below()(a u32 parameter) and truncates the same way. My patch makes that truncation explicit but does not introduce a new bug.You are 100% correct and I agree. Thank you for making this clear. I should also mentioned and underlined this in my previous reply, but I forgot it.
So I think this patch is ok to be merged as is (after addressing my nit trivial comments about coding styles), but we may still want to fix it in future. So I
damon_rand() is now only called by damon_split_regions_of() with the constant range (1, 10). may by we can rename it to damon_rand_u32() to make the u32 constraint explicit in the API name; that closes out the truncation concern at the legacy helper without needing a separate series.
Quoted 2 lines
was wondering if such future fix for this new, shiny and efficient function is easy.
Will do.
v2 is a single patch with the style fixes (drop 'likely', 'r' on the first line for paddr.c, ctx at the end for
vaddr.c) and 64-bit handling:
static inline unsigned long damon_rand_fast(struct damon_ctx *ctx, unsigned long l, unsigned long r)
{
unsigned long span = r - l;
u64 rnd;
if (span <= U32_MAX) {
rnd = prandom_u32_state(&ctx->rnd_state); return l + (unsigned long)((rnd * span) >> 32); }
rnd = ((u64)prandom_u32_state(&ctx->rnd_state) << 32) | prandom_u32_state(&ctx->rnd_state); return l + mul_u64_u64_shr(rnd, span, 64);
}
I will also drop Prefetch patch.
Quoted 86 lines
Quoted 10 lines
We can restrict the region size when config or just allow spans greater than 4G.
static inline unsigned long damon_rand_fast(struct
damon_ctx*ctx,
unsigned long l, unsigned long r)
{
unsigned long span = r - l;
u64 rnd;if (likely(span <= U32_MAX)) {I think we could drop 'likely' and hope compiler or hardware branch prediction to be smart enough.
Quoted 8 lines
rnd = prandom_u32_state(&ctx->rnd_state); return l + (unsigned long)((rnd * span) >> 32); } rnd = ((u64)prandom_u32_state(&ctx->rnd_state) << 32) | prandom_u32_state(&ctx->rnd_state); // same as 'l + (unsigned long)((rnd * span) >> 32);' return l + mul_u64_u64_shr(rnd, span, 64);Makes sense to me.
Quoted 28 lines
}
Quoted 25 lines
Quoted 25 lines
include/linux/damon.h +1
-0+static inline struct damon_region *damon_next_region (struct damon_region *r) { return container_of(r->list.next, struct damon_region, list);mm/damon/core.c +2
-0@@ -607,6 +607,8 @@ struct damon_ctx *damon_new_ctx(void)INIT_LIST_HEAD (&ctx->adaptive_targets);INIT_LIST_HEAD (&ctx->schemes); +prandom_seed_state (&ctx->rnd_state,get_random_u64 ()); +return ctx; }mm/damon/paddr.c +0
-0@@ -48,12 +48,12 @@ static void damon_pa_mkold(phys_addr_t paddr)folio_put (folio); }Quoted 8 lines
Quoted 7 lines
mm/damon/paddr.c static void damon_pa_mkold(phys_addr_t paddr) · line 52
Quoted 4 lines
mm/damon/paddr.c +2
-2-static void __damon_pa_prepare_access_check (struct damon_region *r, -unsigned long addr_unit) +static void __damon_pa_prepare_access_check (struct damon_ctx *ctx, +struct damon_region *r)Let's keep 'r' on the first line, and update the second line without indent change.
Thanks
You're welcome :)
So I'm still unsure if this is the only single and best way for your use case. I'd like to further discuss your use case and find more way to help it if you are also interested. So, I'd like to continue the conversation.
Regardless of it, though, I think this patch is good enough to be merged for general use case, though it require above trivial style changes. I wouldn't strongly request you to solve the >4GiB region issue together, but it would be nice to be added from the beginning. It's up to your choice.
I'm also waiting for your answer to the second patch of this series. I'm still bit skeptical about it, though. To my simple brain, it feels introducing too much complexity. I feel like making this
damon_rand()optimization patch merged first and keep discussing your use case and the second patch later might be a path forward. If you agree, please feel free to post a new version of this patch.[1] https://github.com/damonitor/damo/blob/next/USAGE.md#damo-report-access-page-level-properties-based-access-monitoring
[2] https://lore.kernel.org/linux-mm/[email protected]/Thanks,
SJ[...]
SeongJae Park
maintainerQuoted 44 lines · On Sat, 25 Apr 2026 11:36:02 +0800 Jiayuan Chen <[email protected]> wrote:
Quoted 28 lines · SeongJae Park
Quoted 20 lines · On Fri, 24 Apr 2026 10:29:58 +0800 Jiayuan Chen <[email protected]> wrote:
Hello SJ,
Thank you for the review.
Quoted 15 lines · SeongJae Park
Hello Jiayuan,
Thank you for sharing this patch with us!
Quoted 8 lines · On Thu, 23 Apr 2026 20:23:36 +0800 Jiayuan Chen <[email protected]> wrote:
From: Jiayuan Chen <[email protected]>
damon_rand()on thesampling_addrhot path callsget_random_u32_below(), which takes alocal_lock_irqsave()around a per-CPU batched entropy pool and periodically refills it with ChaCha20. On workloads with largenr_regions(20k+), this shows up as a large fraction of kdamond CPU
time: thelock_acquire/local_lockpair plus__get_random_u32_below()dominate perf profiles.Quoted 6 lines
Quoted 3 lines
Could you please share more details about the use case? I'm particularly curious how you ended up setting '
nr_regiions' that high, while the upper limit ofnr_regionsis set to 1,0000 by default.We use DAMON paddr on a 2 TiB host for per-cgroup hot/cold page classification. Target cgroups can be as small as 1-2% of total memory (tens of GiB).
Quoted 9 lines
Thank you for sharing this detail!
Could you further share me what do you do with the hot/cold information? We found some people want to use DAMON for proactive cold pages reclamation but they watned to use it for only file-backed pages, and therefore developed page level DAMOS filter. In a similar way, if we know your detailed use case, we might be able to serve you better by finding not well advertised features, or developing a new features.
Hello SJ,
Thanks.
Quoted 8 lines
Use case: mixed-workload Kubernetes host (latency-sensitive + batch pods on the same machine). We need continuous per-pod cold/hot signal for two things -- k8s scheduling decisions (which pod can give up memory under host pressure) and sizing the eventual reclamation through memory.reclaim. Both anon and file pages matter (swap is configured); reclamation itself goes through mainline. What we add is the per-pod cold attribution feeding the scheduler.
Thank you for sharing this detail!
Quoted 11 lines
DAMON_RECLAIMalone doesn't fit -- it is reclaim-only with aggregate stats, while we need continuous per-pod visibility even when reclamation isn't firing.Let me re-frame the region-size point, since I don't think I explained it well last time. We are NOT trying to make region size match cgroup size; you're right that pages are scattered and that mapping doesn't work. The high
nr_regionsis so that DAMON's adaptive split has enough resolution to identify cold sub-areas of physical memory at all -- regardless of cgroup.
Thank you, this helps me betetr understand your concern.
Quoted 5 lines
With ~2 GiB
default regions on a 2 TiB host, a small pod's pages are averaged with thousands of non-pod pages in the same region, and the region never reaches nr_accesses=0 even when the pod is genuinely idle.
But, the adaptive regions adjustment mechanism dynamically change size of regions, down to 4 KiB. If the small pod's page is really cold while its surrounding pages are not, DAMON should down-size the region to capture only the page and show you nr_accesses=0.
Quoted 5 lines
The cold signal is gone before any cgroup attribution happens. Cgroup attribution itself is done at sample granularity (
folio_memcgper sampled page), not at region granularity -- the regions just need to be fine enough that there *is* a cold signal to attribute.
Could you please share more details about what is the cgroup attribution, and how it is done? I guess that is the way to map DAMON's monitoring regions to each cgroup to determine if each cgroup is hot or cold. I'm unsure how it is really be done.
Quoted 18 lines
Quoted 15 lines
Quoted 7 lines
With the default max_nr_regions=1000, each region covers ~2 GiB on average, which is often larger than the entire target cgroup. Adaptive split cannot carve out cgroup-homogeneous regions in that
regime: each region'snr_accessesaverages the (small) cgroup fraction with the surrounding non-cgroup pages, and the cgroup's access signal gets washed out.But, pages of cgroups would be scattered around on the physical address space. So even if DAMON finds a hot or cold region on physical address space that having a size smaller than a cgroup's memory, it is hard to say if it is for a specific cgroup. How do you know if the region is for which cgroup? Do you have a way to make sure pages of same cgroup are gathered on the physical address space? If not, I'm not sure how ensuring size of region to be equal or smaller than each cgroup size helps you.
Quoted 3 lines
We are supporting page level monitoring [1] for such cgroup-aware use case. Are you using it? But again, if you use it, I'm not sure why you need to ensure DAMON region size smaller than the cgroup size.
Quoted 7 lines
This is also why DAMOS memcg filters do not bypass the
nr_regionsconstraint -- the scheme's access-pattern matcher operates on the already-averaged region, so by the time any filter sees pages the signal is already lost. Whatever attribution mechanism we use afterwards (custom callback, memcg filter, page-level reporting), the region count needs to be high enough first.
But, I'm not understanding why the regions adjustment mechanism cannot work here. Your answer to my above question will be helpful.
Quoted 5 lines
Quoted 2 lines
Fyi, I'm also working [2] on making the cgroup-aware monitoring much more lightweight.
Looking forward to it -- hopefully it'll cover our case. 😁
I hope so, too! :)
Quoted 47 lines
Quoted 37 lines
Quoted 8 lines
Raising
max_nr_regionsto 10k-20k gives the adaptive split enough spatial resolution that cgroup-majority regions can form at cgroup boundaries (allocations have enough physical locality in practice for this to work -- THP, per-node allocation, etc.). At that region count,damon_rand()starts showing up at the top of kdamond profiles, which is what motivated this patch.Thank you for sharing how this patch has developed. I'm still curious if there is yet more ways to make DAMON better for your use case. But this patch makes sense in general to me.
Quoted 17 lines
Quoted 9 lines
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Thanks for pointing to intervals auto-tuning - we do use it. But it
trades sampling frequency against monitoring overhead; it cannot change the spatial resolution. With N=1000 regions on a 2 TiB host, a 20 GiB cgroup cannot be resolved no matter how we tune
sample_us/aggr_us, because the region boundary itself averages cgroup andQuoted 4 lines
non-cgroup pages together.
So raising
max_nr_regionsand making the per-region overhead cheap are complementary to interval auto-tuning, not redundant with it.Makes sense. I'm still not sure how it helps for cgroup. But, if you do want it and if it helps you, you are of course ok to use it in your way, and I will help you. :)
[...]
Quoted 9 lines
Quoted 6 lines
Sashiko is correct that (u32)(r - l) truncates spans greater than 4 GiB.
This is a pre-existing limitation of
damon_rand()itself, which passes r - l toget_random_u32_below()(a u32 parameter) and truncates the same way. My patch makes that truncation explicit but does not introduce a new bug.You are 100% correct and I agree. Thank you for making this clear. I should also mentioned and underlined this in my previous reply, but I forgot it.
Quoted 7 lines
Quoted 2 lines
So I think this patch is ok to be merged as is (after addressing my nit trivial comments about coding styles), but we may still want to fix it in future. So I
damon_rand()is now only called bydamon_split_regions_of()with the constant range (1, 10). may by we can rename it todamon_rand_u32()to make the u32 constraint explicit in the API name; that closes out the truncation concern at the legacy helper without needing a separate series.
Good point. I'm wondering if we have a reason to keep using damon_rand() at all. I find no such reason. If you also find no real reason, how about simply removing existing damon_rand() and renaming damon_rand_fast() to damon_rand()?
Quoted 26 lines
Quoted 2 lines
was wondering if such future fix for this new, shiny and efficient function is easy.
Will do.
v2 is a single patch with the style fixes (drop 'likely', 'r' on the first line for
paddr.c, ctx at the end for
vaddr.c) and 64-bit handling:static inline unsigned long
damon_rand_fast(struct damon_ctx *ctx, unsigned long l, unsigned long r)
{
unsigned long span = r - l;
u64 rnd;if (span <=
U32_MAX) {
rnd =prandom_u32_state(&ctx->rnd_state); return l + (unsigned long)((rnd * span) >> 32); }
rnd = ((u64)prandom_u32_state(&ctx->rnd_state) << 32) |prandom_u32_state(&ctx->rnd_state); return l +mul_u64_u64_shr(rnd, span, 64);
}
Quoted 1 line
I will also drop Prefetch patch.
Sounds good, looking forward to v2!
Thanks,
SJ
[...]
Hello SJ
Thanks.
Quoted 3 lines · SeongJae Park
Quoted 1 line · On Sat, 25 Apr 2026 11:36:02 +0800 Jiayuan Chen <[email protected]> wrote:
On 4/24/26 11:11 PM, SeongJae Park wrote:
[...]
Quoted 10 lines
Quoted 5 lines
With ~2 GiB
default regions on a 2 TiB host, a small pod's pages are averaged with thousands of non-pod pages in the same region, and the region never reaches nr_accesses=0 even when the pod is genuinely idle.But, the adaptive regions adjustment mechanism dynamically change size of regions, down to 4 KiB. If the small pod's page is really cold while its surrounding pages are not, DAMON should down-size the region to capture only the page and show you nr_accesses=0.
Looking at kdamond_split_regions() / kdamond_merge_regions():
the per-region floor is 4 KiB, but the *total* count is hard-capped at max_nr_regions, and split itself is blind -- it picks the cut position via damon_rand(1, 10) without looking at access patterns. What keeps a split in place is the next merge cycle finding the two halves have visibly different nr_accesses; if a small cgroup's signal is averaged with surrounding host pages on both sides of the random cut, the halves look identical and merge folds them back. For a 1% cgroup on a 2 TiB host with max_nr_regions=1000, the split-merge loop never converges to cgroup-aligned regions -- random cuts almost always land in "99% host + 1% cgroup" mixtures. Raising the cap gives the random splitter enough attempts that some cuts happen to land on physically-clustered cgroup pages (THP, NUMA-local allocations) and stick. That's why the cap matters in practice, not the 4 KiB floor.
Quoted 9 lines
Quoted 5 lines
The cold signal is gone before any cgroup attribution happens. Cgroup attribution itself is done at sample granularity (
folio_memcgper sampled page), not at region granularity -- the regions just need to be fine enough that there *is* a cold signal to attribute.Could you please share more details about what is the cgroup attribution, and how it is done? I guess that is the way to map DAMON's monitoring regions to each cgroup to determine if each cgroup is hot or cold. I'm unsure how it is really be done.
We sample physical pages within DAMON regions, look up folio_memcg() per sampled page to find the owning memcg, and accumulate cold bytes per memcg. Userspace reads the per-cgroup result and sizes memory.reclaim per pod. Conceptually similar to the page-level monitoring you pointed me at -- we'll evaluate whether [1] / [2] can replace this path.
Quoted 79 lines
Quoted 17 lines
Quoted 14 lines
Quoted 6 lines
With the default max_nr_regions=1000, each region covers ~2 GiB on average, which is often larger than the entire target cgroup. Adaptive split cannot carve out cgroup-homogeneous regions in that
regime: each region'snr_accessesaverages the (small) cgroup fraction with the surrounding non-cgroup pages, and the cgroup's access signal gets washed out.But, pages of cgroups would be scattered around on the physical address space. So even if DAMON finds a hot or cold region on physical address space that having a size smaller than a cgroup's memory, it is hard to say if it is for a specific cgroup. How do you know if the region is for which cgroup? Do you have a way to make sure pages of same cgroup are gathered on the physical address space? If not, I'm not sure how ensuring size of region to be equal or smaller than each cgroup size helps you.
Quoted 3 lines
We are supporting page level monitoring [1] for such cgroup-aware use case. Are you using it? But again, if you use it, I'm not sure why you need to ensure DAMON region size smaller than the cgroup size.
Quoted 7 lines
This is also why DAMOS memcg filters do not bypass the
nr_regionsconstraint -- the scheme's access-pattern matcher operates on the already-averaged region, so by the time any filter sees pages the signal is already lost. Whatever attribution mechanism we use afterwards (custom callback, memcg filter, page-level reporting), the region count needs to be high enough first.But, I'm not understanding why the regions adjustment mechanism cannot work here. Your answer to my above question will be helpful.
Quoted 4 lines
Quoted 2 lines
Fyi, I'm also working [2] on making the cgroup-aware monitoring much more lightweight.
Looking forward to it -- hopefully it'll cover our case. 😁
I hope so, too! :)
Quoted 46 lines
Quoted 34 lines
Quoted 7 lines
Raising
max_nr_regionsto 10k-20k gives the adaptive split enough spatial resolution that cgroup-majority regions can form at cgroup boundaries (allocations have enough physical locality in practice for this to work -- THP, per-node allocation, etc.). At that region count,damon_rand()starts showing up at the top of kdamond profiles, which is what motivated this patch.Thank you for sharing how this patch has developed. I'm still curious if there is yet more ways to make DAMON better for your use case. But this patch makes sense in general to me.
Quoted 15 lines
Quoted 8 lines
I know some people worry if the limit is too low and it could result in poor monitoring accuracy. Therefore we developed [1] monitoring intervals auto-tuning. From multiple tests on real environment it showed somewhat convincing results, and therefore I nowadays suggest DAMON users to try that if they didn't try.
I'm bit concerned if this is over-engineering. It would be helpful to know if it is, if you could share the more detailed use case.
Thanks for pointing to intervals auto-tuning - we do use it. But it
trades sampling frequency against monitoring overhead; it cannot change the spatial resolution. With N=1000 regions on a 2 TiB host, a 20 GiB cgroup cannot be resolved no matter how we tune
sample_us/aggr_us, because the region boundary itself averages cgroup andQuoted 4 lines
non-cgroup pages together.
So raising
max_nr_regionsand making the per-region overhead cheap are complementary to interval auto-tuning, not redundant with it.Makes sense. I'm still not sure how it helps for cgroup. But, if you do want it and if it helps you, you are of course ok to use it in your way, and I will help you. :)
[...]
Quoted 11 lines
Quoted 6 lines
Sashiko is correct that (u32)(r - l) truncates spans greater than 4 GiB.
This is a pre-existing limitation of
damon_rand()itself, which passes r - l toget_random_u32_below()(a u32 parameter) and truncates the same way. My patch makes that truncation explicit but does not introduce a new bug.You are 100% correct and I agree. Thank you for making this clear. I should also mentioned and underlined this in my previous reply, but I forgot it.
So I think this patch is ok to be merged as is (after addressing my nit trivial comments about coding styles), but we may still want to fix it in future. So I
Quoted 9 lines
Quoted 5 lines
damon_rand()is now only called bydamon_split_regions_of()with the constant range (1, 10). may by we can rename it todamon_rand_u32()to make the u32 constraint explicit in the API name; that closes out the truncation concern at the legacy helper without needing a separate series.Good point. I'm wondering if we have a reason to keep using
damon_rand()at all. I find no such reason. If you also find no real reason, how about simply removing existingdamon_rand()and renamingdamon_rand_fast()todamon_rand()?
Good idea. v2 will remove the legacy helper, rename damon_rand_fast() to damon_rand(), and plumb ctx into damon_split_regions_of() for the new signature.
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
was wondering if such future fix for this new, shiny and efficient function is easy.
[...]
Thanks!
SeongJae Park
maintainerQuoted 21 lines · On Sun, 26 Apr 2026 13:50:42 +0800 Jiayuan Chen <[email protected]> wrote:
Hello SJ
Thanks.
Quoted 3 lines · SeongJae Park
Quoted 1 line · On Sat, 25 Apr 2026 11:36:02 +0800 Jiayuan Chen <[email protected]> wrote:
On 4/24/26 11:11 PM, SeongJae Park wrote:
[...]
Quoted 10 lines
Quoted 5 lines
With ~2 GiB
default regions on a 2 TiB host, a small pod's pages are averaged with thousands of non-pod pages in the same region, and the region never reaches nr_accesses=0 even when the pod is genuinely idle.But, the adaptive regions adjustment mechanism dynamically change size of regions, down to 4 KiB. If the small pod's page is really cold while its surrounding pages are not, DAMON should down-size the region to capture only the page and show you nr_accesses=0.
Quoted 16 lines
Looking at
kdamond_split_regions()/kdamond_merge_regions():
the per-region floor is 4 KiB, but the *total* count is hard-capped atmax_nr_regions, and split itself is blind -- it picks the cut position viadamon_rand(1, 10)without looking at access patterns. What keeps a split in place is the next merge cycle finding the two halves have visibly differentnr_accesses; if a small cgroup's signal is averaged with surrounding host pages on both sides of the random cut, the halves look identical and merge folds them back. For a 1% cgroup on a 2 TiB host with max_nr_regions=1000, the split-merge loop never converges to cgroup-aligned regions -- random cuts almost always land in "99% host + 1% cgroup" mixtures. Raising the cap gives the random splitter enough attempts that some cuts happen to land on physically-clustered cgroup pages (THP, NUMA-local allocations) and stick. That's why the cap matters in practice, not the 4 KiB floor.
Theoretically, you are rigt. But the size of the impact would depend on the workload.
In my previous testing on 1 TiB memory machine that was running a real world workload, DAMON was able to find 4 KiB cold pages.
Is this somewhat observed on your products?
Quoted 12 lines
Quoted 10 lines
Quoted 6 lines
The cold signal is gone before any cgroup attribution happens. Cgroup attribution itself is done at sample granularity (
folio_memcgper sampled page), not at region granularity -- the regions just need to be fine enough that there *is* a cold signal to attribute.Could you please share more details about what is the cgroup attribution, and how it is done? I guess that is the way to map DAMON's monitoring regions to each cgroup to determine if each cgroup is hot or cold. I'm unsure how it is really be done.
Quoted 6 lines
We sample physical pages within DAMON regions, look up
folio_memcg()per sampled page to find the owning memcg, and accumulate cold bytes per memcg. Userspace reads the per-cgroup result and sizes memory.reclaim per pod. Conceptually similar to the page-level monitoring you pointed me at -- we'll evaluate whether [1] / [2] can replace this path.
Thank you for sharing this. That all make sense. I also hope the existing and planned cgroup-aware monitoring will help your use case!
FYI, I removed [1] and [2] in my previous reply, so pasting those here again, for other readers of this mail.
[1] https://lkml.kernel.org/r/[email protected]
[2] https://lore.kernel.org/[email protected]
[...]
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
So I think this patch is ok to be merged as is (after addressing my nit trivial comments about coding styles), but we may still want to fix it in future. So I
damon_rand()is now only called bydamon_split_regions_of()with the constant range (1, 10). may by we can rename it todamon_rand_u32()to make the u32 constraint explicit in the API name; that closes out the truncation concern at the legacy helper without needing a separate series.
Quoted 8 lines
Quoted 4 lines
Good point. I'm wondering if we have a reason to keep using
damon_rand()at all. I find no such reason. If you also find no real reason, how about simply removing existingdamon_rand()and renamingdamon_rand_fast()todamon_rand()?Good idea. v2 will remove the legacy helper, rename
damon_rand_fast()todamon_rand(), and plumb ctx intodamon_split_regions_of()for the new signature.
Sounds good, looking forward to v2!
Thanks,
SJ
[...]