Skip to content

Commit ebb3f99

Browse files
sjp38torvalds
authored andcommitted
mm/damon/dbgfs: fix 'struct pid' leaks in 'dbgfs_target_ids_write()'
DAMON debugfs interface increases the reference counts of 'struct pid's for targets from the 'target_ids' file write callback ('dbgfs_target_ids_write()'), but decreases the counts only in DAMON monitoring termination callback ('dbgfs_before_terminate()'). Therefore, when 'target_ids' file is repeatedly written without DAMON monitoring start/termination, the reference count is not decreased and therefore memory for the 'struct pid' cannot be freed. This commit fixes this issue by decreasing the reference counts when 'target_ids' is written. Link: https://lkml.kernel.org/r/[email protected] Fixes: 4bc0595 ("mm/damon: implement a debugfs-based user space interface") Signed-off-by: SeongJae Park <[email protected]> Cc: <[email protected]> [5.15+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent f5c7329 commit ebb3f99

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mm/damon/dbgfs.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ static ssize_t dbgfs_target_ids_write(struct file *file,
353353
const char __user *buf, size_t count, loff_t *ppos)
354354
{
355355
struct damon_ctx *ctx = file->private_data;
356+
struct damon_target *t, *next_t;
356357
bool id_is_pid = true;
357358
char *kbuf, *nrs;
358359
unsigned long *targets;
@@ -397,8 +398,12 @@ static ssize_t dbgfs_target_ids_write(struct file *file,
397398
goto unlock_out;
398399
}
399400

400-
/* remove targets with previously-set primitive */
401-
damon_set_targets(ctx, NULL, 0);
401+
/* remove previously set targets */
402+
damon_for_each_target_safe(t, next_t, ctx) {
403+
if (targetid_is_pid(ctx))
404+
put_pid((struct pid *)t->id);
405+
damon_destroy_target(t);
406+
}
402407

403408
/* Configure the context for the address space type */
404409
if (id_is_pid)

0 commit comments

Comments
 (0)