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: process damon_call_control requests on a local list

kdamond_call() handles damon_call() requests on the ->call_controls list
of damon_ctx, which is shared with damon_call() callers. To protect the
list from concurrent accesses while letting the callback function
independent of the call_controls_lock, the function does complicated
locking operations. For each damon_call_control object on the list, the
function removes the control object from the list under locking, invoke
the callback of the control object without locking, and then puts the
control object back to the list if needed, under locking. It is
complicated, and can contend the locks more frequently with other DAMON
API caller threads as the number of concurrent callback requests
increases. Contention overhead is not a big deal, but the increased race
opportunity can make headaches.

Simplify the locking sequence by moving all damon_call_control objects
from the shared list to a local list at once under the single lock
protection, processing the callback requests without locking, and adding
back repeat mode controls to the shared list again at once again, again
under the single lock protection. This change makes the number of locking
in kdamond_call() be always two, regardless of the number of the queued
requests.

Link: https://lkml.kernel.org/r/20260117175256.82826-5-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

SeongJae Park and committed by
Andrew Morton
ebc4734a 69714a74

+22 -39
+22 -39
mm/damon/core.c
··· 2649 2649 */ 2650 2650 static void kdamond_call(struct damon_ctx *ctx, bool cancel) 2651 2651 { 2652 - struct damon_call_control *control; 2653 - LIST_HEAD(repeat_controls); 2654 - int ret = 0; 2652 + struct damon_call_control *control, *next; 2653 + LIST_HEAD(controls); 2655 2654 2656 - while (true) { 2657 - mutex_lock(&ctx->call_controls_lock); 2658 - control = list_first_entry_or_null(&ctx->call_controls, 2659 - struct damon_call_control, list); 2660 - mutex_unlock(&ctx->call_controls_lock); 2661 - if (!control) 2662 - break; 2663 - if (cancel) { 2664 - control->canceled = true; 2665 - } else { 2666 - ret = control->fn(control->data); 2667 - control->return_code = ret; 2668 - } 2669 - mutex_lock(&ctx->call_controls_lock); 2670 - list_del(&control->list); 2671 - mutex_unlock(&ctx->call_controls_lock); 2672 - if (!control->repeat) { 2673 - complete(&control->completion); 2674 - } else if (control->canceled && control->dealloc_on_cancel) { 2675 - kfree(control); 2676 - continue; 2677 - } else { 2678 - list_add(&control->list, &repeat_controls); 2679 - } 2680 - } 2681 - while (true) { 2682 - control = list_first_entry_or_null(&repeat_controls, 2683 - struct damon_call_control, list); 2684 - if (!control) 2685 - break; 2686 - /* Unlink from the repeate_controls list. */ 2687 - list_del(&control->list); 2655 + mutex_lock(&ctx->call_controls_lock); 2656 + list_splice_tail_init(&ctx->call_controls, &controls); 2657 + mutex_unlock(&ctx->call_controls_lock); 2658 + 2659 + list_for_each_entry_safe(control, next, &controls, list) { 2660 + if (!control->repeat || cancel) 2661 + list_del(&control->list); 2662 + 2688 2663 if (cancel) 2689 - continue; 2690 - mutex_lock(&ctx->call_controls_lock); 2691 - list_add(&control->list, &ctx->call_controls); 2692 - mutex_unlock(&ctx->call_controls_lock); 2664 + control->canceled = true; 2665 + else 2666 + control->return_code = control->fn(control->data); 2667 + 2668 + if (!control->repeat) 2669 + complete(&control->completion); 2670 + else if (control->canceled && control->dealloc_on_cancel) 2671 + kfree(control); 2693 2672 } 2673 + 2674 + mutex_lock(&ctx->call_controls_lock); 2675 + list_splice_tail(&controls, &ctx->call_controls); 2676 + mutex_unlock(&ctx->call_controls_lock); 2694 2677 } 2695 2678 2696 2679 /* Returns negative error code if it's not activated but should return */