public inbox for linux-mm@kvack.org
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: SeongJae Park <sj@kernel.org>,
	damon@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Subject: [PATCH 0/3] mm/damon/core: make passed_sample_intervals comparisons overflow-safe
Date: Sat,  7 Mar 2026 11:49:11 -0800	[thread overview]
Message-ID: <20260307194915.203169-1-sj@kernel.org> (raw)

DAMON accounts time using its own jiffies-like time counter, namely
damon_ctx->passed_sample_intervals.  The counter is incremented on each
iteration of kdamond_fn() main loop, which sleeps at least one sample
interval.  Hence the name is like that.

DAMON has time-periodic operations including monitoring results
aggregation and DAMOS action application.  DAMON sets the next time to
do each of such operations in the passed_sample_intervals unit.  And it
does the operation when the counter becomes the same to or larger than
the pre-set values, and update the next time for the operation.  Note
that the operation is done not only when the values exactly match but
also when the time is passed, because the values can be updated for
online-committed DAMON parameters.

The counter is 'unsigned long' type, and the comparison is done using
normal comparison operators.  It is not safe from overflows.  This can
cause rare and limited but odd situations.

Let's suppose there is an operation that should be executed every 20
sampling intervals, and the passed_sample_intervals value for next
execution of the operation is ULONG_MAX - 3.  Once the
passed_sample_intervals reaches ULONG_MAX - 3, the operation will be
executed, and the next time value for doing the operation becomes 17
(ULONG_MAX - 3 + 20), since overflow happens.  In the next iteration of
the kdamond_fn() main loop, passed_sample_intervals is larger than the
next operation time value, so the operation will be executed again.  It
will continue executing the operation for each iteration, until the
passed_sample_intervals also overflows.

Note that this will not be common and problematic in the real world.
The sampling interval, which takes for each passed_sample_intervals
increment, is 5 ms by default.  And it is usually [auto-]tuned for
hundreds of milliseconds.  That means it takes about 248 days or 4,971
days to have the overflow on 32 bit machines when the sampling interval
is 5 ms and 100 ms, respectively (1<<32 * sampling_interval_in_seconds /
3600 / 24).  On 64 bit machines, the numbers become 2924712086.77536 and
58494241735.5072 years.  So the real user impact is negligible.  But
still this is better to be fixed as long as the fix is simple and
efficient.

Fix this by simply replacing the overflow-unsafe native comparison
operators with the existing overflow-safe time comparison helpers.

The first patch only cleans up the next DAMOS action application time
setup for consistency and reduced code.  The second and the third
patches update DAMOS action application time setup and rest,
respectively.

SeongJae Park (3):
  mm/damon/core: remove damos_set_next_apply_sis() duplicates
  mm/damon/core: use time_before() for next_apply_sis
  mm/damon/core: use time_after_eq() in kdamond_fn()

 mm/damon/core.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)


base-commit: 4ef41fd88e37fcdc1751edd9bc57403dba2742a3
-- 
2.47.3


             reply	other threads:[~2026-03-07 19:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-07 19:49 SeongJae Park [this message]
2026-03-07 19:49 ` [PATCH 1/3] mm/damon/core: remove damos_set_next_apply_sis() duplicates SeongJae Park
2026-03-07 19:49 ` [PATCH 2/3] mm/damon/core: use time_before() for next_apply_sis SeongJae Park
2026-03-07 19:49 ` [PATCH 3/3] mm/damon/core: use time_after_eq() in kdamond_fn() 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=20260307194915.203169-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=damon@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.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