Skip to contentSkip to conversation

mm/damon: three fixes for idle_time overflow and damos_walk() hang

PATCH New SJ Park 3 participants 8 messages Nov 23

Subsystems (3)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

cover mm/damon: three fixes for idle_time overflow and damos_walk() hang

Avoid 32-bit overflow of idle time calculation in DAMON_STAT, via patch 1 from David Carlier. Fix a damos_walk() hang that can happen under certain watermarks setup and a watermarks commit internal state overwrite issue, via patches 2 and 3 from Yifan Feng.

This is a batched reposting of separately posted and reviewed patches for being merged into the mm.git. Refer to changelog on each patch for changes that were made before this reposting.

David Carlier (1):
mm/damon/stat: avoid 32-bit overflow in idle time milliseconds

Yifan Feng (2):
mm/damon/core: fix damos_walk() hang with zero watermarks interval mm/damon/core: do not overwrite wmarks.activated in damos_commit()

 mm/damon/core.c | 13 ++++++++++++-
 mm/damon/stat.c |  4 ++--
 2 files changed, 14 insertions(+), 3 deletions(-)

base-commit: f6538c11c47408c69dc77d38fea1b2a4fca86f3e

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/3 mm/damon/stat: avoid 32-bit overflow in idle time milliseconds

From: David Carlier <[email protected]>

On 32-bit, the region idle time in microseconds is computed in a long before the division to milliseconds. It wraps once a region has been idle for about 36 minutes regardless of the intervals, so the cold end of memory_idle_ms_percentiles reports wrong values with flipped signs.

Do the multiplication in 64 bits and divide with div_s64() so the sign of recently accessed regions is kept.

Fixes e5d2585d9e85 ("mm/damon/stat: calculate and expose idle time percentiles")
Cc Aveline Noir <[email protected]>

---
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Collect R-b: from SJ.
- Rebase to the latest mm-new.

 mm/damon/stat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/stat.c +2 -2
index e44f5bf3645cc..d90a43becaddf 100644
@@ -129,8 +129,8 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)
 		accounted_bytes += region->ar.end - region->ar.start;
 		while (next_percentile <= accounted_bytes * 100 / total_sz)
 			memory_idle_ms_percentiles[next_percentile++] =
-				damon_stat_idletime(region) *
-				(long)c->attrs.aggr_interval / USEC_PER_MSEC;
+				div_s64((s64)damon_stat_idletime(region) *
+				c->attrs.aggr_interval, USEC_PER_MSEC);
 	}
 	kfree(sorted_regions);
 }
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 2/3 mm/damon/core: fix damos_walk() hang with zero watermarks interval

In reply to SJ Park

From: Yifan Feng <[email protected]>

kdamond_wait_activation() cancels damos_walk() requests while all schemes are deactivated by their watermarks. But it returns without waiting when the watermarks check interval of a deactivated scheme is zero, which is the default of the DAMON sysfs interface. The kdamond main loop then runs with all schemes deactivated.

In that state, kdamond_apply_schemes() finds no scheme to apply and returns before it calls damos_walk_complete(). The walk request is neither completed nor cancelled, so damos_walk() waits until the watermarks activate a scheme again, possibly forever.

For example, with a scheme that has the free_mem_rate metric, zero interval_us and a high watermark below the current free memory rate, writing update_schemes_tried_regions to the kdamond state file never returns and cannot be killed. The writer holds damon_sysfs_lock, so all other DAMON sysfs writes including "off" fail with -EBUSY meanwhile.

Cancel the walk request in kdamond_apply_schemes() when no scheme is activated, as kdamond_wait_activation() does for a non-zero interval.

Fixes bf0eaba0ff9c ("mm/damon/core: implement damos_walk()")
Cc <[email protected]> # 6.14.x

---
Changes from v2
- v2: https://lore.kernel.org/[email protected]
- Collect R-b: from SJ.
- Rebase to the latest mm-new.
Changes from v1
- v1: https://lore.kernel.org/r/[email protected]
- Cancel the walk when no scheme is activated, and leave has_schemes_to_apply as it is (SJ Park).

 mm/damon/core.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/damon/core.c +9 -1
index b8678a53b0a98..2748f91999240 100644
@@ -3492,9 +3492,13 @@ static noinline_for_stack void kdamond_apply_schemes(struct damon_ctx *c)
 	struct damon_target *t;
 	struct damos *s;
 	bool has_schemes_to_apply = false;
+	bool has_activated_schemes = false;
 	unsigned long max_region_sz;
 
 	damon_for_each_scheme(s, c) {
+		if (s->wmarks.activated)
+			has_activated_schemes = true;
+
 		if (time_before(c->passed_sample_intervals, s->next_apply_sis))
 			continue;
 
@@ -3506,8 +3510,12 @@ static noinline_for_stack void kdamond_apply_schemes(struct damon_ctx *c)
 		damos_adjust_quota(c, s);
 	}
 
-	if (!has_schemes_to_apply)
+	if (!has_schemes_to_apply) {
+		/* The walk cannot be completed while no scheme is activated */
+		if (!has_activated_schemes)
+			damos_walk_cancel(c);
 		return;
+	}
 
 	max_region_sz = damon_region_sz_limit(c);
 	mutex_lock(&c->walk_control_lock);
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 3/3 mm/damon/core: do not overwrite wmarks.activated in damos_commit()

In reply to SJ Park

From: Yifan Feng <[email protected]>

damos_commit() copies the whole wmarks struct from the source scheme, including the private 'activated' field. The source scheme is a new one, so the field is always true there.

As a result, a commit turns on a scheme that the watermarks turned off. If the metric is between mid and high at that time, the scheme keeps running, though it should stay off until the metric goes below mid. DAMON_RECLAIM and DAMON_LRU_SORT use the same path for commit_inputs.

Save 'activated' before the copy and restore it after. Do not restore it for DAMOS_WMARK_NONE, because nothing sets it again in that case and the scheme would stay off.

Fixes 9cb3d0b9dfce ("mm/damon/core: implement DAMON context commit function")
Cc <[email protected]> # 6.11.x

---
Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Collect R-b: from SJ.

 mm/damon/core.c | 3 +++
 1 file changed, 3 insertions(+)
mm/damon/core.c +3 -0
index 2748f91999240..0e6b361f97ea6 100644
@@ -1567,6 +1567,7 @@ static struct damos *damon_nth_scheme(int n, struct damon_ctx *ctx)
 
 static int damos_commit(struct damos *dst, struct damos *src)
 {
+	bool activated = dst->wmarks.activated;
 	int err;
 
 	dst->pattern = src->pattern;
@@ -1578,6 +1579,8 @@ static int damos_commit(struct damos *dst, struct damos *src)
 		return err;
 
 	dst->wmarks = src->wmarks;
+	if (dst->wmarks.metric != DAMOS_WMARK_NONE)
+		dst->wmarks.activated = activated;
 	dst->target_nid = src->target_nid;
 
 	err = damos_commit_dests(&dst->migrate_dests, &src->migrate_dests);
Signature · 1 line
2.47.3
maintainer tree notice
#2 lore raw

+ mm-damon-stat-avoid-32-bit-overflow-in-idle-time-milliseconds.patch added to mm-hotfixes-unstable branch

In reply to SJ Park
The patch titled Subject: mm/damon/stat: avoid 32-bit overflow in idle time milliseconds has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: mm/damon/stat: avoid 32-bit overflow in idle time milliseconds
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-damon-stat-avoid-32-bit-overflow-in-idle-time-milliseconds.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-damon-stat-avoid-32-bit-overflow-in-idle-time-milliseconds.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: David Carlier <[email protected]>
Subject: mm/damon/stat: avoid 32-bit overflow in idle time milliseconds
Date: Mon, 23 Nov 2026 07:00:46 -0800

Patch series "mm/damon: three fixes for idle_time overflow and damos_walk() hang".

Avoid 32-bit overflow of idle time calculation in DAMON_STAT, via patch 1 from David Carlier. Fix a damos_walk() hang that can happen under certain watermarks setup and a watermarks commit internal state overwrite issue, via patches 2 and 3 from Yifan Feng.

This is a batched reposting of separately posted and reviewed patches for being merged into the mm.git. Refer to changelog on each patch for changes that were made before this reposting.

This patch (of 3):

On 32-bit, the region idle time in microseconds is computed in a long before the division to milliseconds. It wraps once a region has been idle for about 36 minutes regardless of the intervals, so the cold end of memory_idle_ms_percentiles reports wrong values with flipped signs.

Do the multiplication in 64 bits and divide with div_s64() so the sign of recently accessed regions is kept.

 mm/damon/stat.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/stat.c renamed from mm/damon/stat.c~mm-damon-stat-avoid-32-bit-overflow-in-idle-time-milliseconds +2 -2
@@ -129,8 +129,8 @@ static void damon_stat_set_idletime_perc
 		accounted_bytes += region->ar.end - region->ar.start;
 		while (next_percentile <= accounted_bytes * 100 / total_sz)
 			memory_idle_ms_percentiles[next_percentile++] =
-				damon_stat_idletime(region) *
-				(long)c->attrs.aggr_interval / USEC_PER_MSEC;
+				div_s64((s64)damon_stat_idletime(region) *
+				c->attrs.aggr_interval, USEC_PER_MSEC);
 	}
 	kfree(sorted_regions);
 }

_

Patches currently in -mm which might be from [email protected] are

mm-damon-stat-avoid-32-bit-overflow-in-idle-time-milliseconds.patch

maintainer tree notice
#4 lore raw

+ mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval.patch added to mm-hotfixes-unstable branch

In reply to SJ Park
The patch titled Subject: mm/damon/core: fix damos_walk() hang with zero watermarks interval has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: mm/damon/core: fix damos_walk() hang with zero watermarks interval
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Yifan Feng <[email protected]>
Subject: mm/damon/core: fix damos_walk() hang with zero watermarks interval
Date: Mon, 23 Nov 2026 07:00:47 -0800

kdamond_wait_activation() cancels damos_walk() requests while all schemes are deactivated by their watermarks. But it returns without waiting when the watermarks check interval of a deactivated scheme is zero, which is the default of the DAMON sysfs interface. The kdamond main loop then runs with all schemes deactivated.

In that state, kdamond_apply_schemes() finds no scheme to apply and returns before it calls damos_walk_complete(). The walk request is neither completed nor cancelled, so damos_walk() waits until the watermarks activate a scheme again, possibly forever.

For example, with a scheme that has the free_mem_rate metric, zero interval_us and a high watermark below the current free memory rate, writing update_schemes_tried_regions to the kdamond state file never returns and cannot be killed. The writer holds damon_sysfs_lock, so all other DAMON sysfs writes including "off" fail with -EBUSY meanwhile.

Cancel the walk request in kdamond_apply_schemes() when no scheme is activated, as kdamond_wait_activation() does for a non-zero interval.

 mm/damon/core.c |   10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
mm/damon/core.c renamed from mm/damon/core.c~mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval +9 -1
@@ -3492,9 +3492,13 @@ static noinline_for_stack void kdamond_a
 	struct damon_target *t;
 	struct damos *s;
 	bool has_schemes_to_apply = false;
+	bool has_activated_schemes = false;
 	unsigned long max_region_sz;
 
 	damon_for_each_scheme(s, c) {
+		if (s->wmarks.activated)
+			has_activated_schemes = true;
+
 		if (time_before(c->passed_sample_intervals, s->next_apply_sis))
 			continue;
 
@@ -3506,8 +3510,12 @@ static noinline_for_stack void kdamond_a
 		damos_adjust_quota(c, s);
 	}
 
-	if (!has_schemes_to_apply)
+	if (!has_schemes_to_apply) {
+		/* The walk cannot be completed while no scheme is activated */
+		if (!has_activated_schemes)
+			damos_walk_cancel(c);
 		return;
+	}
 
 	max_region_sz = damon_region_sz_limit(c);
 	mutex_lock(&c->walk_control_lock);

_

Patches currently in -mm which might be from [email protected] are

mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval.patch mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit.patch

maintainer tree notice
#6 lore raw

+ mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit.patch added to mm-hotfixes-unstable branch

In reply to SJ Park
The patch titled Subject: mm/damon/core: do not overwrite wmarks.activated in damos_commit() has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: mm/damon/core: do not overwrite wmarks.activated in damos_commit()
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Yifan Feng <[email protected]>
Subject: mm/damon/core: do not overwrite wmarks.activated in damos_commit()
Date: Mon, 23 Nov 2026 07:00:48 -0800

damos_commit() copies the whole wmarks struct from the source scheme, including the private 'activated' field. The source scheme is a new one, so the field is always true there.

As a result, a commit turns on a scheme that the watermarks turned off. If the metric is between mid and high at that time, the scheme keeps running, though it should stay off until the metric goes below mid. DAMON_RECLAIM and DAMON_LRU_SORT use the same path for commit_inputs.

Save 'activated' before the copy and restore it after. Do not restore it for DAMOS_WMARK_NONE, because nothing sets it again in that case and the scheme would stay off.

Fixes 9cb3d0b9dfce ("mm/damon/core: implement DAMON context commit function")
Cc David Carlier <[email protected]>
Cc <[email protected]> # 6.11.x
 mm/damon/core.c |    3 +++
 1 file changed, 3 insertions(+)
mm/damon/core.c renamed from mm/damon/core.c~mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit +3 -0
@@ -1567,6 +1567,7 @@ static struct damos *damon_nth_scheme(in
 
 static int damos_commit(struct damos *dst, struct damos *src)
 {
+	bool activated = dst->wmarks.activated;
 	int err;
 
 	dst->pattern = src->pattern;
@@ -1578,6 +1579,8 @@ static int damos_commit(struct damos *ds
 		return err;
 
 	dst->wmarks = src->wmarks;
+	if (dst->wmarks.metric != DAMOS_WMARK_NONE)
+		dst->wmarks.activated = activated;
 	dst->target_nid = src->target_nid;
 
 	err = damos_commit_dests(&dst->migrate_dests, &src->migrate_dests);

_

Patches currently in -mm which might be from [email protected] are

mm-damon-core-fix-damos_walk-hang-with-zero-watermarks-interval.patch mm-damon-core-do-not-overwrite-wmarksactivated-in-damos_commit.patch