[PATCH 5.19 101/101] damon/sysfs: fix possible memleak on damon_sysfs_add_target

From: Greg Kroah-Hartman
Date: Mon Oct 03 2022 - 03:27:39 EST


From: Levi Yun <ppbuk5246@xxxxxxxxx>

commit 1c8e2349f2d033f634d046063b704b2ca6c46972 upstream.

When damon_sysfs_add_target couldn't find proper task, New allocated
damon_target structure isn't registered yet, So, it's impossible to free
new allocated one by damon_sysfs_destroy_targets.

By calling damon_add_target as soon as allocating new target, Fix this
possible memory leak.

Link: https://lkml.kernel.org/r/20220926160611.48536-1-sj@xxxxxxxxxx
Fixes: a61ea561c871 ("mm/damon/sysfs: link DAMON for virtual address spaces monitoring")
Signed-off-by: Levi Yun <ppbuk5246@xxxxxxxxx>
Signed-off-by: SeongJae Park <sj@xxxxxxxxxx>
Reviewed-by: SeongJae Park <sj@xxxxxxxxxx>
Cc: <stable@xxxxxxxxxxxxxxx> [5.17.x]
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
mm/damon/sysfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -2181,13 +2181,13 @@ static int damon_sysfs_add_target(struct

if (!t)
return -ENOMEM;
+ damon_add_target(ctx, t);
if (ctx->ops.id == DAMON_OPS_VADDR ||
ctx->ops.id == DAMON_OPS_FVADDR) {
t->pid = find_get_pid(sys_target->pid);
if (!t->pid)
goto destroy_targets_out;
}
- damon_add_target(ctx, t);
err = damon_sysfs_set_regions(t, sys_target->regions);
if (err)
goto destroy_targets_out;