linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/damon/sysfs: fix refresh_ms control overwriting on multi-kdamonds usages
@ 2025-09-08 20:15 SeongJae Park
  2025-09-08 20:15 ` [PATCH 1/2] mm/damon/core: introduce damon_call_control->dealloc_on_cancel SeongJae Park
  2025-09-08 20:15 ` [PATCH 2/2] mm/damon/sysfs: use dynamically allocated repeat mode damon_call_control SeongJae Park
  0 siblings, 2 replies; 3+ messages in thread
From: SeongJae Park @ 2025-09-08 20:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, Yunjeong Mun, damon, kernel-team, linux-kernel, linux-mm

Automatic esssential DAMON/DAMOS status update feature of DAMON sysfs
interface (refresh_ms) is broken [1] for multiple DAMON contexts
(kdamonds) use case, since it uses a global single damon_call_control
object for all created DAMON contexts.  The fields of the object,
particularly the list field is over-written for the contexts and it
makes unexpected results including user-space hangup and kernel crashes
[2].  Fix it by extending damon_call_control for the use case and
updating the usage on DAMON sysfs interface to use per-context
dynamically allocated damon_call_control object.

[1] https://lore.kernel.org/20250904011738.930-1-yunjeong.mun@sk.com
[2] https://lore.kernel.org/20250905035411.39501-1-sj@kernel.org

SeongJae Park (2):
  mm/damon/core: introduce damon_call_control->dealloc_on_cancel
  mm/damon/sysfs: use dynamically allocated repeat mode
    damon_call_control

 include/linux/damon.h |  2 ++
 mm/damon/core.c       |  8 ++++++--
 mm/damon/sysfs.c      | 23 +++++++++++++++--------
 3 files changed, 23 insertions(+), 10 deletions(-)


base-commit: c6680f5947fa5ff95bc881f2c4e36443478c8829
-- 
2.39.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] mm/damon/core: introduce damon_call_control->dealloc_on_cancel
  2025-09-08 20:15 [PATCH 0/2] mm/damon/sysfs: fix refresh_ms control overwriting on multi-kdamonds usages SeongJae Park
@ 2025-09-08 20:15 ` SeongJae Park
  2025-09-08 20:15 ` [PATCH 2/2] mm/damon/sysfs: use dynamically allocated repeat mode damon_call_control SeongJae Park
  1 sibling, 0 replies; 3+ messages in thread
From: SeongJae Park @ 2025-09-08 20:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, Yunjeong Mun, damon, kernel-team, linux-kernel, linux-mm

When damon_call_control->repeat is set, damon_call() is executed
asynchronously, and eventually be canceled when kdamond finishes.  If
the damon_call_control object is dynamically allocated, hence, finding
the place to deallocate the object is difficult.  Introduce a new
damon_call_control field, namely dealloc_on_cancel, to ask the kdamond
deallocates those dynamically allocated objects when those are canceled.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 include/linux/damon.h | 2 ++
 mm/damon/core.c       | 8 ++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/include/linux/damon.h b/include/linux/damon.h
index ec8716292c09..aa7381be388c 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -636,6 +636,7 @@ struct damon_operations {
  * @data:		Data that will be passed to @fn.
  * @repeat:		Repeat invocations.
  * @return_code:	Return code from @fn invocation.
+ * @dealloc_on_cancel:	De-allocate when canceled.
  *
  * Control damon_call(), which requests specific kdamond to invoke a given
  * function.  Refer to damon_call() for more details.
@@ -645,6 +646,7 @@ struct damon_call_control {
 	void *data;
 	bool repeat;
 	int return_code;
+	bool dealloc_on_cancel;
 /* private: internal use only */
 	/* informs if the kdamond finished handling of the request */
 	struct completion completion;
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 7aeb3f24aae8..be5942435d78 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2510,10 +2510,14 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)
 		mutex_lock(&ctx->call_controls_lock);
 		list_del(&control->list);
 		mutex_unlock(&ctx->call_controls_lock);
-		if (!control->repeat)
+		if (!control->repeat) {
 			complete(&control->completion);
-		else
+		} else if (control->canceled && control->dealloc_on_cancel) {
+			kfree(control);
+			continue;
+		} else {
 			list_add(&control->list, &repeat_controls);
+		}
 	}
 	control = list_first_entry_or_null(&repeat_controls,
 			struct damon_call_control, list);
-- 
2.39.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] mm/damon/sysfs: use dynamically allocated repeat mode damon_call_control
  2025-09-08 20:15 [PATCH 0/2] mm/damon/sysfs: fix refresh_ms control overwriting on multi-kdamonds usages SeongJae Park
  2025-09-08 20:15 ` [PATCH 1/2] mm/damon/core: introduce damon_call_control->dealloc_on_cancel SeongJae Park
@ 2025-09-08 20:15 ` SeongJae Park
  1 sibling, 0 replies; 3+ messages in thread
From: SeongJae Park @ 2025-09-08 20:15 UTC (permalink / raw)
  To: Andrew Morton
  Cc: SeongJae Park, Yunjeong Mun, damon, kernel-team, linux-kernel, linux-mm

DAMON sysfs interface is using a single global repeat mode
damon_call_control variable for refresh_ms handling, for all DAMON
contexts.  As a result, when there are more than one context, the single
global damon_call_control is unexpectedly over-written (corrupted).
Particularly the ->link field is overwritten by the multiple contexts
and this can cause a user hangup, and/or a kernel crash.  Fix it by
using dynamically allocated damon_call_control object per DAMON context.

Fixes: d809a7c64ba8 ("mm/damon/sysfs: implement refresh_ms file internal work") # v6.17-rc1
Reported-by: Yunjeong Mun <yunjeong.mun@sk.com>
Closes: https://lore.kernel.org/20250904011738.930-1-yunjeong.mun@sk.com
Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/sysfs.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 6625fb718195..fe4e73d0ebbb 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1569,14 +1569,10 @@ static int damon_sysfs_repeat_call_fn(void *data)
 	return 0;
 }
 
-static struct damon_call_control damon_sysfs_repeat_call_control = {
-	.fn = damon_sysfs_repeat_call_fn,
-	.repeat = true,
-};
-
 static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
 {
 	struct damon_ctx *ctx;
+	struct damon_call_control *repeat_call_control;
 	int err;
 
 	if (damon_sysfs_kdamond_running(kdamond))
@@ -1589,18 +1585,29 @@ static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond)
 		damon_destroy_ctx(kdamond->damon_ctx);
 	kdamond->damon_ctx = NULL;
 
+	repeat_call_control = kmalloc(sizeof(*repeat_call_control),
+			GFP_KERNEL);
+	if (!repeat_call_control)
+		return -ENOMEM;
+
 	ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]);
-	if (IS_ERR(ctx))
+	if (IS_ERR(ctx)) {
+		kfree(repeat_call_control);
 		return PTR_ERR(ctx);
+	}
 	err = damon_start(&ctx, 1, false);
 	if (err) {
+		kfree(repeat_call_control);
 		damon_destroy_ctx(ctx);
 		return err;
 	}
 	kdamond->damon_ctx = ctx;
 
-	damon_sysfs_repeat_call_control.data = kdamond;
-	damon_call(ctx, &damon_sysfs_repeat_call_control);
+	repeat_call_control->fn = damon_sysfs_repeat_call_fn;
+	repeat_call_control->data = kdamond;
+	repeat_call_control->repeat = true;
+	repeat_call_control->dealloc_on_cancel = true;
+	damon_call(ctx, repeat_call_control);
 	return err;
 }
 
-- 
2.39.5


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-08 20:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-08 20:15 [PATCH 0/2] mm/damon/sysfs: fix refresh_ms control overwriting on multi-kdamonds usages SeongJae Park
2025-09-08 20:15 ` [PATCH 1/2] mm/damon/core: introduce damon_call_control->dealloc_on_cancel SeongJae Park
2025-09-08 20:15 ` [PATCH 2/2] mm/damon/sysfs: use dynamically allocated repeat mode damon_call_control SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox