Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/damon/core: fix memory leak of repeat mode damon_call_control objects

A memory leak exists in the handling of repeat mode damon_call_control
objects by kdamond_call(). While damon_call() correctly allows multiple
repeat mode objects (with ->repeat set to true) to be added to the
per-context list, kdamond_call() incorrectly processes them.

The function moves all repeat mode objects from the context's list to a
temporary list (repeat_controls). However, it only moves the first object
back to the context's list for future calls, leaving the remaining objects
on the temporary list where they are abandoned and leaked.

This patch fixes the leak by ensuring all repeat mode objects are properly
re-added to the context's list.

Note that the leak is not in the real world, and therefore no user is
impacted. It is only potential for imaginaray damon_call() use cases that
do not exist in the tree for now. In more detail, the leak happens only
when the multiple repeat mode objects are assumed to be deallocated by
kdamond_call() (damon_call_control->dealloc_on_cancel is set). There is
no such damon_call() use cases at the moment.

Link: https://lkml.kernel.org/r/20251202082340.34178-1-lienze@kylinos.cn
Fixes: 43df7676e550 ("mm/damon/core: introduce repeat mode damon_call()")
Signed-off-by: Enze Li <lienze@kylinos.cn>
Reviewed-by: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Enze Li and committed by
Andrew Morton
817383b3 a03ed8f1

+13 -7
+13 -7
mm/damon/core.c
··· 2606 2606 list_add(&control->list, &repeat_controls); 2607 2607 } 2608 2608 } 2609 - control = list_first_entry_or_null(&repeat_controls, 2610 - struct damon_call_control, list); 2611 - if (!control || cancel) 2612 - return; 2613 - mutex_lock(&ctx->call_controls_lock); 2614 - list_add_tail(&control->list, &ctx->call_controls); 2615 - mutex_unlock(&ctx->call_controls_lock); 2609 + while (true) { 2610 + control = list_first_entry_or_null(&repeat_controls, 2611 + struct damon_call_control, list); 2612 + if (!control) 2613 + break; 2614 + /* Unlink from the repeate_controls list. */ 2615 + list_del(&control->list); 2616 + if (cancel) 2617 + continue; 2618 + mutex_lock(&ctx->call_controls_lock); 2619 + list_add(&control->list, &ctx->call_controls); 2620 + mutex_unlock(&ctx->call_controls_lock); 2621 + } 2616 2622 } 2617 2623 2618 2624 /* Returns negative error code if it's not activated but should return */