Skip to content

Commit 4fe8f84

Browse files
sjp38gregkh
authored andcommitted
mm/damon/core: implement damon_kdamond_pid()
commit 4262c53 upstream. Patch series "mm/damon: hide kdamond and kdamond_lock from API callers". 'kdamond' and 'kdamond_lock' fields initially exposed to DAMON API callers for flexible synchronization and use cases. As DAMON API became somewhat complicated compared to the early days, Keeping those exposed could only encourage the API callers to invent more creative but complicated and difficult-to-debug use cases. Fortunately DAMON API callers didn't invent that many creative use cases. There exist only two use cases of 'kdamond' and 'kdamond_lock'. Finding whether the kdamond is actively running, and getting the pid of the kdamond. For the first use case, a dedicated API function, namely 'damon_is_running()' is provided, and all DAMON API callers are using the function for the use case. Hence only the second use case is where the fields are directly being used by DAMON API callers. To prevent future invention of complicated and erroneous use cases of the fields, hide the fields from the API callers. For that, provide new dedicated DAMON API functions for the remaining use case, namely damon_kdamond_pid(), migrate DAMON API callers to use the new function, and mark the fields as private fields. This patch (of 5): '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. Link: https://lkml.kernel.org/r/20260115152047.68415-1-sj@kernel.org Link: https://lkml.kernel.org/r/20260115152047.68415-2-sj@kernel.org Signed-off-by: SeongJae Park <sj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent cdf55cb commit 4fe8f84

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

include/linux/damon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ static inline unsigned int damon_max_nr_accesses(const struct damon_attrs *attrs
778778

779779
int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
780780
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
781+
int damon_kdamond_pid(struct damon_ctx *ctx);
781782

782783
int damon_set_region_biggest_system_ram_default(struct damon_target *t,
783784
unsigned long *start, unsigned long *end);

mm/damon/core.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,23 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
11631163
return err;
11641164
}
11651165

1166+
/**
1167+
* damon_kdamond_pid() - Return pid of a given DAMON context's worker thread.
1168+
* @ctx: The DAMON context of the question.
1169+
*
1170+
* Return: pid if @ctx is running, negative error code otherwise.
1171+
*/
1172+
int damon_kdamond_pid(struct damon_ctx *ctx)
1173+
{
1174+
int pid = -EINVAL;
1175+
1176+
mutex_lock(&ctx->kdamond_lock);
1177+
if (ctx->kdamond)
1178+
pid = ctx->kdamond->pid;
1179+
mutex_unlock(&ctx->kdamond_lock);
1180+
return pid;
1181+
}
1182+
11661183
/*
11671184
* Reset the aggregated monitoring results ('nr_accesses' of each region).
11681185
*/

0 commit comments

Comments
 (0)