Skip to content

Commit 4364282

Browse files
sjp38torvalds
authored andcommitted
mm/damon/core: move damon_set_targets() into dbgfs
damon_set_targets() function is defined in the core for general use cases, but called from only dbgfs. Also, because the function is for general use cases, dbgfs does additional handling of pid type target id case. To make the situation simpler, this commit moves the function into dbgfs and makes it to do the pid type case handling on its own. Link: https://lkml.kernel.org/r/20211230100723.2238-4-sj@kernel.org Signed-off-by: SeongJae Park <sj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8041c87 commit 4364282

5 files changed

Lines changed: 52 additions & 54 deletions

File tree

include/linux/damon.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ unsigned int damon_nr_regions(struct damon_target *t);
484484

485485
struct damon_ctx *damon_new_ctx(void);
486486
void damon_destroy_ctx(struct damon_ctx *ctx);
487-
int damon_set_targets(struct damon_ctx *ctx,
488-
unsigned long *ids, ssize_t nr_ids);
489487
int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int,
490488
unsigned long aggr_int, unsigned long primitive_upd_int,
491489
unsigned long min_nr_reg, unsigned long max_nr_reg);

mm/damon/core-test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ static void damon_test_aggregate(struct kunit *test)
8686
struct damon_region *r;
8787
int it, ir;
8888

89-
damon_set_targets(ctx, target_ids, 3);
89+
for (it = 0; it < 3; it++) {
90+
t = damon_new_target(target_ids[it]);
91+
damon_add_target(ctx, t);
92+
}
9093

9194
it = 0;
9295
damon_for_each_target(t, ctx) {

mm/damon/core.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -245,38 +245,6 @@ void damon_destroy_ctx(struct damon_ctx *ctx)
245245
kfree(ctx);
246246
}
247247

248-
/**
249-
* damon_set_targets() - Set monitoring targets.
250-
* @ctx: monitoring context
251-
* @ids: array of target ids
252-
* @nr_ids: number of entries in @ids
253-
*
254-
* This function should not be called while the kdamond is running.
255-
*
256-
* Return: 0 on success, negative error code otherwise.
257-
*/
258-
int damon_set_targets(struct damon_ctx *ctx,
259-
unsigned long *ids, ssize_t nr_ids)
260-
{
261-
ssize_t i;
262-
struct damon_target *t, *next;
263-
264-
damon_destroy_targets(ctx);
265-
266-
for (i = 0; i < nr_ids; i++) {
267-
t = damon_new_target(ids[i]);
268-
if (!t) {
269-
/* The caller should do cleanup of the ids itself */
270-
damon_for_each_target_safe(t, next, ctx)
271-
damon_destroy_target(t);
272-
return -ENOMEM;
273-
}
274-
damon_add_target(ctx, t);
275-
}
276-
277-
return 0;
278-
}
279-
280248
/**
281249
* damon_set_attrs() - Set attributes for the monitoring.
282250
* @ctx: monitoring context

mm/damon/dbgfs-test.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,23 @@ static void damon_dbgfs_test_set_targets(struct kunit *test)
8686
ctx->primitive.target_valid = NULL;
8787
ctx->primitive.cleanup = NULL;
8888

89-
damon_set_targets(ctx, ids, 3);
89+
dbgfs_set_targets(ctx, ids, 3);
9090
sprint_target_ids(ctx, buf, 64);
9191
KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2 3\n");
9292

93-
damon_set_targets(ctx, NULL, 0);
93+
dbgfs_set_targets(ctx, NULL, 0);
9494
sprint_target_ids(ctx, buf, 64);
9595
KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
9696

97-
damon_set_targets(ctx, (unsigned long []){1, 2}, 2);
97+
dbgfs_set_targets(ctx, (unsigned long []){1, 2}, 2);
9898
sprint_target_ids(ctx, buf, 64);
9999
KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2\n");
100100

101-
damon_set_targets(ctx, (unsigned long []){2}, 1);
101+
dbgfs_set_targets(ctx, (unsigned long []){2}, 1);
102102
sprint_target_ids(ctx, buf, 64);
103103
KUNIT_EXPECT_STREQ(test, (char *)buf, "2\n");
104104

105-
damon_set_targets(ctx, NULL, 0);
105+
dbgfs_set_targets(ctx, NULL, 0);
106106
sprint_target_ids(ctx, buf, 64);
107107
KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
108108

@@ -130,7 +130,7 @@ static void damon_dbgfs_test_set_init_regions(struct kunit *test)
130130
int i, rc;
131131
char buf[256];
132132

133-
damon_set_targets(ctx, ids, 3);
133+
dbgfs_set_targets(ctx, ids, 3);
134134

135135
/* Put valid inputs and check the results */
136136
for (i = 0; i < ARRAY_SIZE(valid_inputs); i++) {
@@ -158,7 +158,7 @@ static void damon_dbgfs_test_set_init_regions(struct kunit *test)
158158
KUNIT_EXPECT_STREQ(test, (char *)buf, "");
159159
}
160160

161-
damon_set_targets(ctx, NULL, 0);
161+
dbgfs_set_targets(ctx, NULL, 0);
162162
damon_destroy_ctx(ctx);
163163
}
164164

mm/damon/dbgfs.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,48 @@ static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
358358
put_pid((struct pid *)ids[i]);
359359
}
360360

361+
/*
362+
* dbgfs_set_targets() - Set monitoring targets.
363+
* @ctx: monitoring context
364+
* @ids: array of target ids
365+
* @nr_ids: number of entries in @ids
366+
*
367+
* This function should not be called while the kdamond is running.
368+
*
369+
* Return: 0 on success, negative error code otherwise.
370+
*/
371+
static int dbgfs_set_targets(struct damon_ctx *ctx,
372+
unsigned long *ids, ssize_t nr_ids)
373+
{
374+
ssize_t i;
375+
struct damon_target *t, *next;
376+
377+
damon_for_each_target_safe(t, next, ctx) {
378+
if (targetid_is_pid(ctx))
379+
put_pid((struct pid *)t->id);
380+
damon_destroy_target(t);
381+
}
382+
383+
for (i = 0; i < nr_ids; i++) {
384+
t = damon_new_target(ids[i]);
385+
if (!t) {
386+
/* The caller should do cleanup of the ids itself */
387+
damon_for_each_target_safe(t, next, ctx)
388+
damon_destroy_target(t);
389+
if (targetid_is_pid(ctx))
390+
dbgfs_put_pids(ids, nr_ids);
391+
return -ENOMEM;
392+
}
393+
damon_add_target(ctx, t);
394+
}
395+
396+
return 0;
397+
}
398+
361399
static ssize_t dbgfs_target_ids_write(struct file *file,
362400
const char __user *buf, size_t count, loff_t *ppos)
363401
{
364402
struct damon_ctx *ctx = file->private_data;
365-
struct damon_target *t, *next_t;
366403
bool id_is_pid = true;
367404
char *kbuf;
368405
unsigned long *targets;
@@ -407,25 +444,17 @@ static ssize_t dbgfs_target_ids_write(struct file *file,
407444
}
408445

409446
/* remove previously set targets */
410-
damon_for_each_target_safe(t, next_t, ctx) {
411-
if (targetid_is_pid(ctx))
412-
put_pid((struct pid *)t->id);
413-
damon_destroy_target(t);
414-
}
447+
dbgfs_set_targets(ctx, NULL, 0);
415448

416449
/* Configure the context for the address space type */
417450
if (id_is_pid)
418451
damon_va_set_primitives(ctx);
419452
else
420453
damon_pa_set_primitives(ctx);
421454

422-
ret = damon_set_targets(ctx, targets, nr_targets);
423-
if (ret) {
424-
if (id_is_pid)
425-
dbgfs_put_pids(targets, nr_targets);
426-
} else {
455+
ret = dbgfs_set_targets(ctx, targets, nr_targets);
456+
if (!ret)
427457
ret = count;
428-
}
429458

430459
unlock_out:
431460
mutex_unlock(&ctx->kdamond_lock);

0 commit comments

Comments
 (0)