damon_set_target_pid() overwrites t->pid with a fresh find_get_pid() reference without putting the old one, so calling it twice on the same target leaks a struct pid. On failure it also clears t->pid while the old reference leaks. No in-tree caller hits this today, since everyone calls it once on a freshly created target. But as a general API it shouldn't rely on that. Fix it by putting the old pid back before installing the new one, and leaving t->pid untouched when find_get_pid() fails. Fixes: ee5a5b0a2813 ("mm/damon/core: introduce damon_set_target_pid()") Signed-off-by: Enze Li --- mm/damon/core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 6dfa31cd2207..c04c9656193e 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -872,9 +872,15 @@ void damon_add_target(struct damon_ctx *ctx, struct damon_target *t) */ int damon_set_target_pid(struct damon_target *t, int pid) { - t->pid = find_get_pid(pid); - if (!t->pid) + struct pid *npid = find_get_pid(pid); + + if (!npid) return -EINVAL; + + if (t->pid) + put_pid(t->pid); + t->pid = npid; + return 0; } base-commit: 43e4338eed2ded03cb7ef3f0652eb703ca32af44 -- 2.53.0