* [PATCH 1/5] mm/damon/core: implement damon_kdamond_pid()
2026-01-15 15:20 [PATCH 0/5] mm/damon: hide kdamond and kdamond_lock from API callers SeongJae Park
@ 2026-01-15 15:20 ` SeongJae Park
2026-01-15 15:20 ` [PATCH 2/5] mm/damon/sysfs: use damon_kdamond_pid() SeongJae Park
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: SeongJae Park @ 2026-01-15 15:20 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, linux-kernel, linux-mm
'kdamond' and 'kdamond_lock' are directly being used by DAMON API
callers for getting the pid of the corresponding kdamond. To discourage
invention of creative but complicated and erroneous new usages of the
fields that require careful synchronization, implement a new API
function that can simply be used without the manual synchronizations.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
include/linux/damon.h | 1 +
mm/damon/core.c | 17 +++++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index 26fb8e90dff6..5b7ea7082134 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -972,6 +972,7 @@ bool damon_initialized(void);
int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
bool damon_is_running(struct damon_ctx *ctx);
+int damon_kdamond_pid(struct damon_ctx *ctx);
int damon_call(struct damon_ctx *ctx, struct damon_call_control *control);
int damos_walk(struct damon_ctx *ctx, struct damos_walk_control *control);
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 729a5f7fac94..81b998d32074 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -1442,6 +1442,23 @@ bool damon_is_running(struct damon_ctx *ctx)
return running;
}
+/**
+ * damon_kdamond_pid() - Return pid of a given DAMON context's worker thread.
+ * @ctx: The DAMON context of the question.
+ *
+ * Return: pid if @ctx is running, negative error code otherwise.
+ */
+int damon_kdamond_pid(struct damon_ctx *ctx)
+{
+ int pid = -EINVAL;
+
+ mutex_lock(&ctx->kdamond_lock);
+ if (ctx->kdamond)
+ pid = ctx->kdamond->pid;
+ mutex_unlock(&ctx->kdamond_lock);
+ return pid;
+}
+
/*
* damon_call_handle_inactive_ctx() - handle DAMON call request that added to
* an inactive context.
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/5] mm/damon/sysfs: use damon_kdamond_pid()
2026-01-15 15:20 [PATCH 0/5] mm/damon: hide kdamond and kdamond_lock from API callers SeongJae Park
2026-01-15 15:20 ` [PATCH 1/5] mm/damon/core: implement damon_kdamond_pid() SeongJae Park
@ 2026-01-15 15:20 ` SeongJae Park
2026-01-15 15:20 ` [PATCH 3/5] mm/damon/lru_sort: " SeongJae Park
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: SeongJae Park @ 2026-01-15 15:20 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, linux-kernel, linux-mm
DAMON sysfs interface directly uses damon_ctx->kdamond field with manual
synchronization using damon_ctx->kdamond_lock, to get the pid of the
kdamond. Use a new dedicated function for the purpose, namely
damon_kdamond_pid(), since that doesn't require manual and error-prone
synchronization.
Avoid use of kdamond_lock outside of the core.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/sysfs.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 95fd9375a7d8..4de25708b05a 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1819,10 +1819,9 @@ static ssize_t pid_show(struct kobject *kobj,
if (!ctx)
goto out;
- mutex_lock(&ctx->kdamond_lock);
- if (ctx->kdamond)
- pid = ctx->kdamond->pid;
- mutex_unlock(&ctx->kdamond_lock);
+ pid = damon_kdamond_pid(ctx);
+ if (pid < 0)
+ pid = -1;
out:
mutex_unlock(&damon_sysfs_lock);
return sysfs_emit(buf, "%d\n", pid);
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 3/5] mm/damon/lru_sort: use damon_kdamond_pid()
2026-01-15 15:20 [PATCH 0/5] mm/damon: hide kdamond and kdamond_lock from API callers SeongJae Park
2026-01-15 15:20 ` [PATCH 1/5] mm/damon/core: implement damon_kdamond_pid() SeongJae Park
2026-01-15 15:20 ` [PATCH 2/5] mm/damon/sysfs: use damon_kdamond_pid() SeongJae Park
@ 2026-01-15 15:20 ` SeongJae Park
2026-01-15 15:20 ` [PATCH 4/5] mm/damon/reclaim: " SeongJae Park
2026-01-15 15:20 ` [PATCH 5/5] mm/damon: hide kdamond and kdamond_lock of damon_ctx SeongJae Park
4 siblings, 0 replies; 6+ messages in thread
From: SeongJae Park @ 2026-01-15 15:20 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, linux-kernel, linux-mm
DAMON_LRU_SORT directly uses damon_ctx->kdamond field with manual
synchronization using damon_ctx->kdamond_lock, to get the pid of the
kdamond. Use a new dedicated function for the purpose, namely
damon_kdamond_pid(), since that doesn't require manual and error-prone
synchronization.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/lru_sort.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
index 8296f984b428..bedb9134d286 100644
--- a/mm/damon/lru_sort.c
+++ b/mm/damon/lru_sort.c
@@ -405,7 +405,9 @@ static int damon_lru_sort_turn(bool on)
err = damon_start(&ctx, 1, true);
if (err)
return err;
- kdamond_pid = ctx->kdamond->pid;
+ kdamond_pid = damon_kdamond_pid(ctx);
+ if (kdamond_pid < 0)
+ return kdamond_pid;
return damon_call(ctx, &call_control);
}
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/5] mm/damon/reclaim: use damon_kdamond_pid()
2026-01-15 15:20 [PATCH 0/5] mm/damon: hide kdamond and kdamond_lock from API callers SeongJae Park
` (2 preceding siblings ...)
2026-01-15 15:20 ` [PATCH 3/5] mm/damon/lru_sort: " SeongJae Park
@ 2026-01-15 15:20 ` SeongJae Park
2026-01-15 15:20 ` [PATCH 5/5] mm/damon: hide kdamond and kdamond_lock of damon_ctx SeongJae Park
4 siblings, 0 replies; 6+ messages in thread
From: SeongJae Park @ 2026-01-15 15:20 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, linux-kernel, linux-mm
DAMON_RECLAIM directly uses damon_ctx->kdamond field with manual
synchronization using damon_ctx->kdamond_lock, to get the pid of the
kdamond. Use a new dedicated function for the purpose, namely
damon_kdamond_pid(), since that doesn't require manual and error-prone
synchronization.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/reclaim.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 8463a5a5032f..55df43e241c5 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -307,7 +307,9 @@ static int damon_reclaim_turn(bool on)
err = damon_start(&ctx, 1, true);
if (err)
return err;
- kdamond_pid = ctx->kdamond->pid;
+ kdamond_pid = damon_kdamond_pid(ctx);
+ if (kdamond_pid < 0)
+ return kdamond_pid;
return damon_call(ctx, &call_control);
}
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 5/5] mm/damon: hide kdamond and kdamond_lock of damon_ctx
2026-01-15 15:20 [PATCH 0/5] mm/damon: hide kdamond and kdamond_lock from API callers SeongJae Park
` (3 preceding siblings ...)
2026-01-15 15:20 ` [PATCH 4/5] mm/damon/reclaim: " SeongJae Park
@ 2026-01-15 15:20 ` SeongJae Park
4 siblings, 0 replies; 6+ messages in thread
From: SeongJae Park @ 2026-01-15 15:20 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, linux-kernel, linux-mm
There is no DAMON API caller that directly access 'kdamond' and
'kdamond_lock' fields of 'struct damon_ctx'. Keeping those exposed
could only encourage creative but error-prone usages. Hide them from
DAMON API callers by marking those as private fields.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
include/linux/damon.h | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index 5b7ea7082134..e6930d8574d3 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -759,23 +759,20 @@ struct damon_attrs {
* of the monitoring.
*
* @attrs: Monitoring attributes for accuracy/overhead control.
- * @kdamond: Kernel thread who does the monitoring.
- * @kdamond_lock: Mutex for the synchronizations with @kdamond.
*
- * For each monitoring context, one kernel thread for the monitoring is
- * created. The pointer to the thread is stored in @kdamond.
+ * For each monitoring context, one kernel thread for the monitoring, namely
+ * kdamond, is created. The pid of kdamond can be retrieved using
+ * damon_kdamond_pid().
*
- * Once started, the monitoring thread runs until explicitly required to be
- * terminated or every monitoring target is invalid. The validity of the
- * targets is checked via the &damon_operations.target_valid of @ops. The
- * termination can also be explicitly requested by calling damon_stop().
- * The thread sets @kdamond to NULL when it terminates. Therefore, users can
- * know whether the monitoring is ongoing or terminated by reading @kdamond.
- * Reads and writes to @kdamond from outside of the monitoring thread must
- * be protected by @kdamond_lock.
+ * Once started, kdamond runs until explicitly required to be terminated or
+ * every monitoring target is invalid. The validity of the targets is checked
+ * via the &damon_operations.target_valid of @ops. The termination can also be
+ * explicitly requested by calling damon_stop(). To know if a kdamond is
+ * running, damon_is_running() can be used.
*
- * Note that the monitoring thread protects only @kdamond via @kdamond_lock.
- * Accesses to other fields must be protected by themselves.
+ * While the kdamond is running, all accesses to &struct damon_ctx from a
+ * thread other than the kdamond should be made using safe DAMON APIs,
+ * including damon_call() and damos_walk().
*
* @ops: Set of monitoring operations for given use cases.
* @addr_unit: Scale factor for core to ops address conversion.
@@ -816,10 +813,12 @@ struct damon_ctx {
struct damos_walk_control *walk_control;
struct mutex walk_control_lock;
-/* public: */
+ /* Working thread of the given DAMON context */
struct task_struct *kdamond;
+ /* Protects @kdamond field access */
struct mutex kdamond_lock;
+/* public: */
struct damon_operations ops;
unsigned long addr_unit;
unsigned long min_sz_region;
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread