DAMON development mailing list
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SeongJae Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [RFC PATCH 04/13] mm/damon/core: use damon_nr_accesses_mvsum() for damos region tracing
Date: Fri, 19 Jun 2026 13:17:33 -0700	[thread overview]
Message-ID: <20260619201734.144695-1-sj@kernel.org> (raw)
In-Reply-To: <20260619195140.E03221F000E9@smtp.kernel.org>

On Fri, 19 Jun 2026 19:51:40 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> 
> New issues:
> - [Medium] Unconditional evaluation of an expensive function (`damon_nr_accesses_mvsum`) as an argument to a tracepoint, causing performance overhead when tracing is disabled.
> 
> Pre-existing issues:
> - [Critical] Pre-existing vulnerability where setting aggr_interval to 0 triggers a divide-by-zero kernel panic in damon_nr_accesses_mvsum().
> --
> 
> commit a32ac0de542ee6f63cc5519f32be214de8a6347f
> Author: SeongJae Park <sj@kernel.org>
> 
> mm/damon/core: use damon_nr_accesses_mvsum() for damos region tracing
> 
> damon_nr_accesses_mvsum() returns a value same to nr_accesses_bp.  Also
> the function is more simple and therefore more tolerant to errors.
> Execution of the function would be more expensive than the simple read
> of the field, but because the function is quite simple, the overhead
> should be negligible.  Use it in the DAMON region exporting trace points
> instead of the nr_accesses_bp.
> 
> Signed-off-by: SeongJae Park <sj@kernel.org>
> 
> > diff --git a/include/trace/events/damon.h b/include/trace/events/damon.h
> > index 78388538acf44..8851727ae1627 100644
> > --- a/include/trace/events/damon.h
> > +++ b/include/trace/events/damon.h
> > @@ -78,9 +78,11 @@ TRACE_EVENT_CONDITION(damos_before_apply,
> >  
> >  	TP_PROTO(unsigned int context_idx, unsigned int scheme_idx,
> >  		unsigned int target_idx, struct damon_region *r,
> > -		unsigned int nr_regions, bool do_trace),
> > +		unsigned int nr_accesses, unsigned int nr_regions,
> > +		bool do_trace),
> 
> [ ... ]
> 
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index f45f030a72d82..a7b45b3cad508 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -2463,6 +2463,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
> >  			return;
> >  		ktime_get_coarse_ts64(&begin);
> >  		trace_damos_before_apply(cidx, sidx, tidx, r,
> > +				damon_nr_accesses_mvsum(r, c),
> >  				damon_nr_regions(t), do_trace);
> 
> [Severity: Medium]
> Will this unconditionally evaluate damon_nr_accesses_mvsum() even when
> tracing is disabled?
> 
> Because trace_damos_before_apply() is an inline function wrapper, its
> arguments are evaluated before the function is called. This means
> damon_nr_accesses_mvsum() will execute on the hot path for every valid
> region, regardless of whether the tracepoint is active. It also causes
> the function to be evaluated a second time per region, since it was
> already called in __damos_valid_target().
> 
> Would it be better to pass the DAMON context pointer c into the tracepoint
> prototype and defer the execution of damon_nr_accesses_mvsum() entirely to
> TP_fast_assign() to avoid this overhead?

Nice catch.  I will execute the function in trace_damos_before_apply_enabled()
scope of the function, like below.

'''
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2711,7 +2711,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
        struct damos *siter;            /* schemes iterator */
        unsigned int sidx = 0;
        struct damon_target *titer;     /* targets iterator */
-       unsigned int tidx = 0;
+       unsigned int tidx = 0, nr_accesses = 0;
        bool do_trace = false;

        /* get indices for trace_damos_before_apply() */
@@ -2726,6 +2726,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
                                break;
                        tidx++;
                }
+               nr_accesses = damon_nr_accesses_mvsum(r, c);
                do_trace = true;
        }

@@ -2741,8 +2742,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t,
                if (damos_core_filter_out(c, t, r, s))
                        return;
                ktime_get_coarse_ts64(&begin);
-               trace_damos_before_apply(cidx, sidx, tidx, r,
-                               damon_nr_accesses_mvsum(r, c),
+               trace_damos_before_apply(cidx, sidx, tidx, r, nr_accesses,
                                damon_nr_regions(t), do_trace);
                sz_applied = c->ops.apply_scheme(c, t, r, s,
                                &sz_ops_filter_passed);
'''

> 
> 
> [Severity: Critical]
> This is a pre-existing issue, but does damon_nr_accesses_mvsum() have a
> divide-by-zero vulnerability when userspace configures both sample_interval
> and aggr_interval to 0?
> 
> Userspace can configure DAMON via sysfs. In damon_set_attrs(), the validation
> logic accepts configurations where both intervals are 0:
> 
> mm/damon/core.c:damon_set_attrs() {
>     ...
> 	if (attrs->sample_interval > attrs->aggr_interval)
> 		return -EINVAL;
>     ...
> }
> 
> Since 0 > 0 evaluates to false, this configuration is accepted.
> 
> When damon_nr_accesses_mvsum() is subsequently called, it computes
> window_len = ctx->attrs.aggr_interval / sample_interval, which evaluates to 0.
> It then calls mult_frac(left_window, 10000, window_len), which divides
> by window_len and will cause an immediate kernel panic.

I will fix this in the next revision.


Thanks,
SJ

[...]

  reply	other threads:[~2026-06-19 20:17 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-19 19:33 [RFC PATCH 00/13] mm/damon: optimize out nr_accesses_bp SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 01/13] mm/damon: introduce damon_nr_accesses_mvsum() SeongJae Park
2026-06-19 19:44   ` sashiko-bot
2026-06-19 19:52     ` SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 02/13] mm/damon/tests/core-kunit: test damon_mvsum() SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 03/13] mm/damon/core: use damon_nr_accesses_mvsum() in __damos_valid_target() SeongJae Park
2026-06-19 19:49   ` sashiko-bot
2026-06-19 19:33 ` [RFC PATCH 04/13] mm/damon/core: use damon_nr_accesses_mvsum() for damos region tracing SeongJae Park
2026-06-19 19:51   ` sashiko-bot
2026-06-19 20:17     ` SeongJae Park [this message]
2026-06-19 19:33 ` [RFC PATCH 05/13] mm/damon/sysfs-schemes: use damon_nr_accesses_mvsum() for damo regions SeongJae Park
2026-06-19 19:47   ` sashiko-bot
2026-06-19 19:55     ` SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 06/13] mm/damon/core: remove damon_warn_fix_nr_accesses_corruption() SeongJae Park
2026-06-19 19:47   ` sashiko-bot
2026-06-19 19:56     ` SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 07/13] mm/damon/core: remove damon_verify_reset_aggregated() SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 08/13] mm/damon/core: remove damon_verify_merge_regions_of() SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 09/13] mm/damon/tests/core-kunit: remove nr_accesses_bp setup and tests SeongJae Park
2026-06-19 19:52   ` sashiko-bot
2026-06-19 20:24     ` SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 10/13] selftests/damon/drgn_dump_damon_status: do not dump nr_accesses_bp SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 11/13] mm/damon/core: remove nr_accesses_bp setups and updates SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 12/13] mm/damon/core: remove damon_moving_sum() and its unit test SeongJae Park
2026-06-19 19:33 ` [RFC PATCH 13/13] mm/damon: remove damon_region->nr_accesses_bp SeongJae Park
2026-06-19 19:49   ` sashiko-bot
2026-06-19 20:36     ` SeongJae Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260619201734.144695-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-bot@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox