* [RFC PATCH v1] mm: improve call_controls_lock
@ 2025-12-29 14:55 Asier Gutierrez
2025-12-29 15:22 ` SeongJae Park
0 siblings, 1 reply; 9+ messages in thread
From: Asier Gutierrez @ 2025-12-29 14:55 UTC (permalink / raw)
To: gutierrez.asier, sj, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
This is a minor patch set for a call_controls_lock synchronization improvement.
Spinlocks are faster than mutexes, even when the mutex takes the fast
path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
Initial benchmarking shows the following results
# bpftrace -e 'kprobe:kdamond_call { @start[tid] = nsecs; }
kretprobe:kdamond_call /@start[tid]/
{ @ns[comm] = hist(nsecs - @start[tid]); delete(@start[tid]); }'
@ns[kdamond.0]:
[16K, 32K) 22 |@@@@@@@@@@@@ |
[32K, 64K) 90 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[64K, 128K) 0 | |
[128K, 256K) 1 | |
@ns[kdamond.0]:
[16K, 32K) 19 |@@@@@@@@ |
[32K, 64K) 118 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[64K, 128K) 0 | |
[128K, 256K) 1 |
Signed-off-by: Asier Gutierrez <gutierrez.asier@huawei-partners.com>
---
include/linux/damon.h | 2 +-
mm/damon/core.c | 17 ++++++++---------
2 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index 3813373a9200..43665e63a498 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -801,7 +801,7 @@ struct damon_ctx {
/* lists of &struct damon_call_control */
struct list_head call_controls;
- struct mutex call_controls_lock;
+ spinlock_t call_controls_lock;
struct damos_walk_control *walk_control;
struct mutex walk_control_lock;
diff --git a/mm/damon/core.c b/mm/damon/core.c
index f9fc0375890a..a929bdf3bc7b 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -539,7 +539,6 @@ struct damon_ctx *damon_new_ctx(void)
mutex_init(&ctx->kdamond_lock);
INIT_LIST_HEAD(&ctx->call_controls);
- mutex_init(&ctx->call_controls_lock);
mutex_init(&ctx->walk_control_lock);
ctx->attrs.min_nr_regions = 10;
@@ -1457,9 +1456,9 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)
control->canceled = false;
INIT_LIST_HEAD(&control->list);
- mutex_lock(&ctx->call_controls_lock);
+ spin_lock(&ctx->call_controls_lock);
list_add_tail(&control->list, &ctx->call_controls);
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
if (!damon_is_running(ctx))
return -EINVAL;
if (control->repeat)
@@ -2549,10 +2548,10 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)
int ret = 0;
while (true) {
- mutex_lock(&ctx->call_controls_lock);
+ spin_lock(&ctx->call_controls_lock);
control = list_first_entry_or_null(&ctx->call_controls,
struct damon_call_control, list);
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
if (!control)
break;
if (cancel) {
@@ -2561,9 +2560,9 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)
ret = control->fn(control->data);
control->return_code = ret;
}
- mutex_lock(&ctx->call_controls_lock);
+ spin_lock(&ctx->call_controls_lock);
list_del(&control->list);
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
if (!control->repeat) {
complete(&control->completion);
} else if (control->canceled && control->dealloc_on_cancel) {
@@ -2577,9 +2576,9 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel)
struct damon_call_control, list);
if (!control || cancel)
return;
- mutex_lock(&ctx->call_controls_lock);
+ spin_lock(&ctx->call_controls_lock);
list_add_tail(&control->list, &ctx->call_controls);
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
}
/* Returns negative error code if it's not activated but should return */
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-29 14:55 [RFC PATCH v1] mm: improve call_controls_lock Asier Gutierrez
@ 2025-12-29 15:22 ` SeongJae Park
2025-12-30 9:02 ` Gutierrez Asier
2025-12-31 2:15 ` JaeJoon Jung
0 siblings, 2 replies; 9+ messages in thread
From: SeongJae Park @ 2025-12-29 15:22 UTC (permalink / raw)
To: Asier Gutierrez
Cc: SeongJae Park, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
Hello Asier,
Thank you for sending this patch!
On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> This is a minor patch set for a call_controls_lock synchronization improvement.
Please break description lines to not exceed 75 characters per line.
>
> Spinlocks are faster than mutexes, even when the mutex takes the fast
> path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
But call_controls_lock is not being used on performance critical part.
Actually, most of DAMON code is not performance critical. I really appreciate
your patch, but I have to say I don't think this change is really needed now.
Please let me know if I'm missing something.
>
> Initial benchmarking shows the following results
>
>
> # bpftrace -e 'kprobe:kdamond_call { @start[tid] = nsecs; }
Commit log shouldn't start with '#'. Please consider indenting the above
command and below outputs of it.
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-29 15:22 ` SeongJae Park
@ 2025-12-30 9:02 ` Gutierrez Asier
2025-12-31 5:01 ` SeongJae Park
2025-12-31 2:15 ` JaeJoon Jung
1 sibling, 1 reply; 9+ messages in thread
From: Gutierrez Asier @ 2025-12-30 9:02 UTC (permalink / raw)
To: SeongJae Park
Cc: akpm, damon, linux-mm, linux-kernel, wangkefeng.wang,
artem.kuzin, stepanov.anatoly
On 12/29/2025 6:22 PM, SeongJae Park wrote:
> Hello Asier,
Hi SeongJae,
>
> Thank you for sending this patch!
>
> On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
>
>> This is a minor patch set for a call_controls_lock synchronization improvement.
>
> Please break description lines to not exceed 75 characters per line.
>
>>
>> Spinlocks are faster than mutexes, even when the mutex takes the fast
>> path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
>
> But call_controls_lock is not being used on performance critical part.
> Actually, most of DAMON code is not performance critical. I really appreciate
> your patch, but I have to say I don't think this change is really needed now.
> Please let me know if I'm missing something.
I was just reviewing the code and I noticed this. Yes, you are right, the performance
is not critical.
>>
>> Initial benchmarking shows the following results
>>
>>
>> # bpftrace -e 'kprobe:kdamond_call { @start[tid] = nsecs; }
>
> Commit log shouldn't start with '#'. Please consider indenting the above
> command and below outputs of it.
>
>
> Thanks,
> SJ
>
> [...]
>
Thanks a lot for the review!
--
Asier Gutierrez
Huawei
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-30 9:02 ` Gutierrez Asier
@ 2025-12-31 5:01 ` SeongJae Park
0 siblings, 0 replies; 9+ messages in thread
From: SeongJae Park @ 2025-12-31 5:01 UTC (permalink / raw)
To: Gutierrez Asier
Cc: SeongJae Park, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Tue, 30 Dec 2025 12:02:31 +0300 Gutierrez Asier <gutierrez.asier@huawei-partners.com> wrote:
>
>
> On 12/29/2025 6:22 PM, SeongJae Park wrote:
> > Hello Asier,
>
> Hi SeongJae,
>
> >
> > Thank you for sending this patch!
> >
> > On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> >
> >> This is a minor patch set for a call_controls_lock synchronization improvement.
> >
> > Please break description lines to not exceed 75 characters per line.
> >
> >>
> >> Spinlocks are faster than mutexes, even when the mutex takes the fast
> >> path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
> >
> > But call_controls_lock is not being used on performance critical part.
> > Actually, most of DAMON code is not performance critical. I really appreciate
> > your patch, but I have to say I don't think this change is really needed now.
> > Please let me know if I'm missing something.
>
> I was just reviewing the code and I noticed this. Yes, you are right, the performance
> is not critical.
Thank you for flexibly accepting my humble opinion.
>
> >>
> >> Initial benchmarking shows the following results
> >>
> >>
> >> # bpftrace -e 'kprobe:kdamond_call { @start[tid] = nsecs; }
> >
> > Commit log shouldn't start with '#'. Please consider indenting the above
> > command and below outputs of it.
> >
> >
> > Thanks,
> > SJ
> >
> > [...]
> >
>
> Thanks a lot for the review!
My pleasure! Looking forward to your next patch :)
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-29 15:22 ` SeongJae Park
2025-12-30 9:02 ` Gutierrez Asier
@ 2025-12-31 2:15 ` JaeJoon Jung
2025-12-31 4:59 ` SeongJae Park
1 sibling, 1 reply; 9+ messages in thread
From: JaeJoon Jung @ 2025-12-31 2:15 UTC (permalink / raw)
To: SeongJae Park
Cc: Asier Gutierrez, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Tue, 30 Dec 2025 at 00:23, SeongJae Park <sj@kernel.org> wrote:
>
> Hello Asier,
>
>
> Thank you for sending this patch!
>
> On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
>
> > This is a minor patch set for a call_controls_lock synchronization improvement.
>
> Please break description lines to not exceed 75 characters per line.
>
> >
> > Spinlocks are faster than mutexes, even when the mutex takes the fast
> > path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
>
> But call_controls_lock is not being used on performance critical part.
> Actually, most of DAMON code is not performance critical. I really appreciate
> your patch, but I have to say I don't think this change is really needed now.
> Please let me know if I'm missing something.
Paradoxically, when it comes to locking, spin_lock is better than
mutex_lock
because "most of DAMON code is not performance critical."
DAMON code only accesses the ctx belonging to kdamond itself. For
example:
kdamond.0 --> ctx.0
kdamond.1 --> ctx.1
kdamond.2 --> ctx.2
kdamond.# --> ctx.#
There is no cross-approach as shown below:
kdamond.0 --> ctx.1
kdamond.1 --> ctx.2
kdamond.2 --> ctx.0
Only the data belonging to kdamond needs to be resolved for concurrent access.
most DAMON code needs to lock/unlock briefly when add/del linked
lists,
so spin_lock is effective. If you handle it with a mutex, it becomes
more
complicated because the rescheduling occurs as a context switch occurs
inside the kernel. Moreover, since the call_controls_lock that is
currently
being raised as a problem only occurs in two places, the kdamon_call()
loop
and the damon_call() function, it is effective to handle it with a
spin_lock
as shown below.
@@ -1502,14 +1501,15 @@ int damon_call(struct damon_ctx *ctx, struct
damon_call_control *control)
control->canceled = false;
INIT_LIST_HEAD(&control->list);
- mutex_lock(&ctx->call_controls_lock);
+ spin_lock(&ctx->call_controls_lock);
+ /* damon_is_running */
if (ctx->kdamond) {
list_add_tail(&control->list, &ctx->call_controls);
} else {
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
return -EINVAL;
}
- mutex_unlock(&ctx->call_controls_lock);
+ spin_unlock(&ctx->call_controls_lock);
if (control->repeat)
return 0;
>
> >
> > Initial benchmarking shows the following results
> >
> >
> > # bpftrace -e 'kprobe:kdamond_call { @start[tid] = nsecs; }
>
> Commit log shouldn't start with '#'. Please consider indenting the above
> command and below outputs of it.
>
>
> Thanks,
> SJ
>
> [...]
>
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-31 2:15 ` JaeJoon Jung
@ 2025-12-31 4:59 ` SeongJae Park
2025-12-31 6:10 ` JaeJoon Jung
0 siblings, 1 reply; 9+ messages in thread
From: SeongJae Park @ 2025-12-31 4:59 UTC (permalink / raw)
To: JaeJoon Jung
Cc: SeongJae Park, Asier Gutierrez, akpm, damon, linux-mm,
linux-kernel, wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Wed, 31 Dec 2025 11:15:00 +0900 JaeJoon Jung <rgbi3307@gmail.com> wrote:
> On Tue, 30 Dec 2025 at 00:23, SeongJae Park <sj@kernel.org> wrote:
> >
> > Hello Asier,
> >
> >
> > Thank you for sending this patch!
> >
> > On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> >
> > > This is a minor patch set for a call_controls_lock synchronization improvement.
> >
> > Please break description lines to not exceed 75 characters per line.
> >
> > >
> > > Spinlocks are faster than mutexes, even when the mutex takes the fast
> > > path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
> >
> > But call_controls_lock is not being used on performance critical part.
> > Actually, most of DAMON code is not performance critical. I really appreciate
> > your patch, but I have to say I don't think this change is really needed now.
> > Please let me know if I'm missing something.
>
> Paradoxically, when it comes to locking, spin_lock is better than
> mutex_lock
> because "most of DAMON code is not performance critical."
>
> DAMON code only accesses the ctx belonging to kdamond itself. For
> example:
> kdamond.0 --> ctx.0
> kdamond.1 --> ctx.1
> kdamond.2 --> ctx.2
> kdamond.# --> ctx.#
>
> There is no cross-approach as shown below:
> kdamond.0 --> ctx.1
> kdamond.1 --> ctx.2
> kdamond.2 --> ctx.0
>
> Only the data belonging to kdamond needs to be resolved for concurrent access.
> most DAMON code needs to lock/unlock briefly when add/del linked
> lists,
> so spin_lock is effective.
I don't disagree this. Both spinlock and mutex effectively work for DAMON's
locking usages.
> If you handle it with a mutex, it becomes
> more
> complicated because the rescheduling occurs as a context switch occurs
> inside the kernel.
Can you please elaborate what kind of complexities you are saying about?
Adding some examples would be nice.
> Moreover, since the call_controls_lock that is
> currently
> being raised as a problem only occurs in two places, the kdamon_call()
> loop
> and the damon_call() function, it is effective to handle it with a
> spin_lock
> as shown below.
>
> @@ -1502,14 +1501,15 @@ int damon_call(struct damon_ctx *ctx, struct
> damon_call_control *control)
> control->canceled = false;
> INIT_LIST_HEAD(&control->list);
>
> - mutex_lock(&ctx->call_controls_lock);
> + spin_lock(&ctx->call_controls_lock);
> + /* damon_is_running */
> if (ctx->kdamond) {
> list_add_tail(&control->list, &ctx->call_controls);
> } else {
> - mutex_unlock(&ctx->call_controls_lock);
> + spin_unlock(&ctx->call_controls_lock);
> return -EINVAL;
> }
> - mutex_unlock(&ctx->call_controls_lock);
> + spin_unlock(&ctx->call_controls_lock);
>
> if (control->repeat)
> return 0;
Are you saying the above diff can fix the damon_call() use-after-free bug [1]?
Can you please elaborate why you think so?
[1] https://lore.kernel.org/20251231012315.75835-1-sj@kernel.org
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-31 4:59 ` SeongJae Park
@ 2025-12-31 6:10 ` JaeJoon Jung
2025-12-31 7:51 ` JaeJoon Jung
2025-12-31 15:32 ` SeongJae Park
0 siblings, 2 replies; 9+ messages in thread
From: JaeJoon Jung @ 2025-12-31 6:10 UTC (permalink / raw)
To: SeongJae Park
Cc: Asier Gutierrez, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Wed, 31 Dec 2025 at 13:59, SeongJae Park <sj@kernel.org> wrote:
>
> On Wed, 31 Dec 2025 11:15:00 +0900 JaeJoon Jung <rgbi3307@gmail.com> wrote:
>
> > On Tue, 30 Dec 2025 at 00:23, SeongJae Park <sj@kernel.org> wrote:
> > >
> > > Hello Asier,
> > >
> > >
> > > Thank you for sending this patch!
> > >
> > > On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> > >
> > > > This is a minor patch set for a call_controls_lock synchronization improvement.
> > >
> > > Please break description lines to not exceed 75 characters per line.
> > >
> > > >
> > > > Spinlocks are faster than mutexes, even when the mutex takes the fast
> > > > path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
> > >
> > > But call_controls_lock is not being used on performance critical part.
> > > Actually, most of DAMON code is not performance critical. I really appreciate
> > > your patch, but I have to say I don't think this change is really needed now.
> > > Please let me know if I'm missing something.
> >
> > Paradoxically, when it comes to locking, spin_lock is better than
> > mutex_lock
> > because "most of DAMON code is not performance critical."
> >
> > DAMON code only accesses the ctx belonging to kdamond itself. For
> > example:
> > kdamond.0 --> ctx.0
> > kdamond.1 --> ctx.1
> > kdamond.2 --> ctx.2
> > kdamond.# --> ctx.#
> >
> > There is no cross-approach as shown below:
> > kdamond.0 --> ctx.1
> > kdamond.1 --> ctx.2
> > kdamond.2 --> ctx.0
> >
> > Only the data belonging to kdamond needs to be resolved for concurrent access.
> > most DAMON code needs to lock/unlock briefly when add/del linked
> > lists,
> > so spin_lock is effective.
>
> I don't disagree this. Both spinlock and mutex effectively work for DAMON's
> locking usages.
>
> > If you handle it with a mutex, it becomes
> > more
> > complicated because the rescheduling occurs as a context switch occurs
> > inside the kernel.
>
> Can you please elaborate what kind of complexities you are saying about?
> Adding some examples would be nice.
>
> > Moreover, since the call_controls_lock that is
> > currently
> > being raised as a problem only occurs in two places, the kdamon_call()
> > loop
> > and the damon_call() function, it is effective to handle it with a
> > spin_lock
> > as shown below.
> >
> > @@ -1502,14 +1501,15 @@ int damon_call(struct damon_ctx *ctx, struct
> > damon_call_control *control)
> > control->canceled = false;
> > INIT_LIST_HEAD(&control->list);
> >
> > - mutex_lock(&ctx->call_controls_lock);
> > + spin_lock(&ctx->call_controls_lock);
> > + /* damon_is_running */
> > if (ctx->kdamond) {
> > list_add_tail(&control->list, &ctx->call_controls);
> > } else {
> > - mutex_unlock(&ctx->call_controls_lock);
> > + spin_unlock(&ctx->call_controls_lock);
> > return -EINVAL;
> > }
> > - mutex_unlock(&ctx->call_controls_lock);
> > + spin_unlock(&ctx->call_controls_lock);
> >
> > if (control->repeat)
> > return 0;
>
> Are you saying the above diff can fix the damon_call() use-after-free bug [1]?
> Can you please elaborate why you think so?
>
> [1] https://lore.kernel.org/20251231012315.75835-1-sj@kernel.org
>
The above code works fine with spin_lock. However, when booting the kernel,
the spin_lock call trace from damon_call() is output as follows:
If you have any experience with the following, please share it.
[ 0.834450] Call Trace:
[ 0.834456] [<ffffffff8001b376>] dump_backtrace+0x1c/0x24
[ 0.834471] [<ffffffff800024e0>] show_stack+0x28/0x34
[ 0.834480] [<ffffffff80014f4c>] dump_stack_lvl+0x48/0x66
[ 0.834493] [<ffffffff80014f7e>] dump_stack+0x14/0x1c
[ 0.834503] [<ffffffff800032c6>] spin_dump+0x62/0x6e
[ 0.834511] [<ffffffff80087376>] do_raw_spin_lock+0xd0/0x128
[ 0.834523] [<ffffffff80de9378>] _raw_spin_lock+0x1a/0x22
[ 0.834538] [<ffffffff80255c0c>] damon_call+0x38/0x100
[ 0.834548] [<ffffffff8025f022>] damon_stat_start+0x10e/0x168
[ 0.834558] [<ffffffff80e21ab4>] damon_stat_init+0x2a/0x44
[ 0.834568] [<ffffffff800157c0>] do_one_initcall+0x40/0x202
[ 0.834579] [<ffffffff80e015f6>] kernel_init_freeable+0x1fc/0x27e
[ 0.834588] [<ffffffff80de0a9e>] kernel_init+0x1e/0x13c
[ 0.834599] [<ffffffff8001716a>] ret_from_fork_kernel+0x10/0xf8
[ 0.834607] [<ffffffff80deab22>] ret_from_fork_kernel_asm+0x16/0x18
[ 0.943407] NFS: Registering the id_resolver key type
[ 0.948996] Key type id_resolver registered
[ 0.953614] Key type id_legacy registered
Thanks,
JaeJoon
>
> Thanks,
> SJ
>
> [...]
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-31 6:10 ` JaeJoon Jung
@ 2025-12-31 7:51 ` JaeJoon Jung
2025-12-31 15:32 ` SeongJae Park
1 sibling, 0 replies; 9+ messages in thread
From: JaeJoon Jung @ 2025-12-31 7:51 UTC (permalink / raw)
To: SeongJae Park
Cc: Asier Gutierrez, akpm, damon, linux-mm, linux-kernel,
wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Wed, 31 Dec 2025 at 15:10, JaeJoon Jung <rgbi3307@gmail.com> wrote:
>
> On Wed, 31 Dec 2025 at 13:59, SeongJae Park <sj@kernel.org> wrote:
> >
> > On Wed, 31 Dec 2025 11:15:00 +0900 JaeJoon Jung <rgbi3307@gmail.com> wrote:
> >
> > > On Tue, 30 Dec 2025 at 00:23, SeongJae Park <sj@kernel.org> wrote:
> > > >
> > > > Hello Asier,
> > > >
> > > >
> > > > Thank you for sending this patch!
> > > >
> > > > On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> > > >
> > > > > This is a minor patch set for a call_controls_lock synchronization improvement.
> > > >
> > > > Please break description lines to not exceed 75 characters per line.
> > > >
> > > > >
> > > > > Spinlocks are faster than mutexes, even when the mutex takes the fast
> > > > > path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
> > > >
> > > > But call_controls_lock is not being used on performance critical part.
> > > > Actually, most of DAMON code is not performance critical. I really appreciate
> > > > your patch, but I have to say I don't think this change is really needed now.
> > > > Please let me know if I'm missing something.
> > >
> > > Paradoxically, when it comes to locking, spin_lock is better than
> > > mutex_lock
> > > because "most of DAMON code is not performance critical."
> > >
> > > DAMON code only accesses the ctx belonging to kdamond itself. For
> > > example:
> > > kdamond.0 --> ctx.0
> > > kdamond.1 --> ctx.1
> > > kdamond.2 --> ctx.2
> > > kdamond.# --> ctx.#
> > >
> > > There is no cross-approach as shown below:
> > > kdamond.0 --> ctx.1
> > > kdamond.1 --> ctx.2
> > > kdamond.2 --> ctx.0
> > >
> > > Only the data belonging to kdamond needs to be resolved for concurrent access.
> > > most DAMON code needs to lock/unlock briefly when add/del linked
> > > lists,
> > > so spin_lock is effective.
> >
> > I don't disagree this. Both spinlock and mutex effectively work for DAMON's
> > locking usages.
> >
> > > If you handle it with a mutex, it becomes
> > > more
> > > complicated because the rescheduling occurs as a context switch occurs
> > > inside the kernel.
> >
> > Can you please elaborate what kind of complexities you are saying about?
> > Adding some examples would be nice.
> >
> > > Moreover, since the call_controls_lock that is
> > > currently
> > > being raised as a problem only occurs in two places, the kdamon_call()
> > > loop
> > > and the damon_call() function, it is effective to handle it with a
> > > spin_lock
> > > as shown below.
> > >
> > > @@ -1502,14 +1501,15 @@ int damon_call(struct damon_ctx *ctx, struct
> > > damon_call_control *control)
> > > control->canceled = false;
> > > INIT_LIST_HEAD(&control->list);
> > >
> > > - mutex_lock(&ctx->call_controls_lock);
> > > + spin_lock(&ctx->call_controls_lock);
> > > + /* damon_is_running */
> > > if (ctx->kdamond) {
> > > list_add_tail(&control->list, &ctx->call_controls);
> > > } else {
> > > - mutex_unlock(&ctx->call_controls_lock);
> > > + spin_unlock(&ctx->call_controls_lock);
> > > return -EINVAL;
> > > }
> > > - mutex_unlock(&ctx->call_controls_lock);
> > > + spin_unlock(&ctx->call_controls_lock);
> > >
> > > if (control->repeat)
> > > return 0;
> >
> > Are you saying the above diff can fix the damon_call() use-after-free bug [1]?
> > Can you please elaborate why you think so?
> >
> > [1] https://lore.kernel.org/20251231012315.75835-1-sj@kernel.org
> >
>
> The above code works fine with spin_lock. However, when booting the kernel,
> the spin_lock call trace from damon_call() is output as follows:
> If you have any experience with the following, please share it.
>
> [ 0.834450] Call Trace:
> [ 0.834456] [<ffffffff8001b376>] dump_backtrace+0x1c/0x24
> [ 0.834471] [<ffffffff800024e0>] show_stack+0x28/0x34
> [ 0.834480] [<ffffffff80014f4c>] dump_stack_lvl+0x48/0x66
> [ 0.834493] [<ffffffff80014f7e>] dump_stack+0x14/0x1c
> [ 0.834503] [<ffffffff800032c6>] spin_dump+0x62/0x6e
> [ 0.834511] [<ffffffff80087376>] do_raw_spin_lock+0xd0/0x128
> [ 0.834523] [<ffffffff80de9378>] _raw_spin_lock+0x1a/0x22
> [ 0.834538] [<ffffffff80255c0c>] damon_call+0x38/0x100
> [ 0.834548] [<ffffffff8025f022>] damon_stat_start+0x10e/0x168
> [ 0.834558] [<ffffffff80e21ab4>] damon_stat_init+0x2a/0x44
> [ 0.834568] [<ffffffff800157c0>] do_one_initcall+0x40/0x202
> [ 0.834579] [<ffffffff80e015f6>] kernel_init_freeable+0x1fc/0x27e
> [ 0.834588] [<ffffffff80de0a9e>] kernel_init+0x1e/0x13c
> [ 0.834599] [<ffffffff8001716a>] ret_from_fork_kernel+0x10/0xf8
> [ 0.834607] [<ffffffff80deab22>] ret_from_fork_kernel_asm+0x16/0x18
> [ 0.943407] NFS: Registering the id_resolver key type
> [ 0.948996] Key type id_resolver registered
> [ 0.953614] Key type id_legacy registered
The above occurred because spin_lock_init() was not performed. The problem
is that spin_lock_init() was not added while deleting mutex_init().
Please refer to the contents below.
@@ -539,6 +539,7 @@ struct damon_ctx *damon_new_ctx(void)
mutex_init(&ctx->kdamond_lock);
INIT_LIST_HEAD(&ctx->call_controls);
- mutex_init(&ctx->call_controls_lock);
+ spin_lock_init(&ctx->call_controls_lock);
mutex_init(&ctx->walk_control_lock);
ctx->attrs.min_nr_regions = 10;
>
> Thanks,
> JaeJoon
>
> >
> > Thanks,
> > SJ
> >
> > [...]
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [RFC PATCH v1] mm: improve call_controls_lock
2025-12-31 6:10 ` JaeJoon Jung
2025-12-31 7:51 ` JaeJoon Jung
@ 2025-12-31 15:32 ` SeongJae Park
1 sibling, 0 replies; 9+ messages in thread
From: SeongJae Park @ 2025-12-31 15:32 UTC (permalink / raw)
To: JaeJoon Jung
Cc: SeongJae Park, Asier Gutierrez, akpm, damon, linux-mm,
linux-kernel, wangkefeng.wang, artem.kuzin, stepanov.anatoly
On Wed, 31 Dec 2025 15:10:12 +0900 JaeJoon Jung <rgbi3307@gmail.com> wrote:
> On Wed, 31 Dec 2025 at 13:59, SeongJae Park <sj@kernel.org> wrote:
> >
> > On Wed, 31 Dec 2025 11:15:00 +0900 JaeJoon Jung <rgbi3307@gmail.com> wrote:
> >
> > > On Tue, 30 Dec 2025 at 00:23, SeongJae Park <sj@kernel.org> wrote:
> > > >
> > > > Hello Asier,
> > > >
> > > >
> > > > Thank you for sending this patch!
> > > >
> > > > On Mon, 29 Dec 2025 14:55:32 +0000 Asier Gutierrez <gutierrez.asier@huawei-partners.com> wrote:
> > > >
> > > > > This is a minor patch set for a call_controls_lock synchronization improvement.
> > > >
> > > > Please break description lines to not exceed 75 characters per line.
> > > >
> > > > >
> > > > > Spinlocks are faster than mutexes, even when the mutex takes the fast
> > > > > path. Hence, this patch replaces the mutex call_controls_lock with a spinlock.
> > > >
> > > > But call_controls_lock is not being used on performance critical part.
> > > > Actually, most of DAMON code is not performance critical. I really appreciate
> > > > your patch, but I have to say I don't think this change is really needed now.
> > > > Please let me know if I'm missing something.
> > >
> > > Paradoxically, when it comes to locking, spin_lock is better than
> > > mutex_lock
> > > because "most of DAMON code is not performance critical."
> > >
> > > DAMON code only accesses the ctx belonging to kdamond itself. For
> > > example:
> > > kdamond.0 --> ctx.0
> > > kdamond.1 --> ctx.1
> > > kdamond.2 --> ctx.2
> > > kdamond.# --> ctx.#
> > >
> > > There is no cross-approach as shown below:
> > > kdamond.0 --> ctx.1
> > > kdamond.1 --> ctx.2
> > > kdamond.2 --> ctx.0
> > >
> > > Only the data belonging to kdamond needs to be resolved for concurrent access.
> > > most DAMON code needs to lock/unlock briefly when add/del linked
> > > lists,
> > > so spin_lock is effective.
> >
> > I don't disagree this. Both spinlock and mutex effectively work for DAMON's
> > locking usages.
> >
> > > If you handle it with a mutex, it becomes
> > > more
> > > complicated because the rescheduling occurs as a context switch occurs
> > > inside the kernel.
> >
> > Can you please elaborate what kind of complexities you are saying about?
> > Adding some examples would be nice.
> >
> > > Moreover, since the call_controls_lock that is
> > > currently
> > > being raised as a problem only occurs in two places, the kdamon_call()
> > > loop
> > > and the damon_call() function, it is effective to handle it with a
> > > spin_lock
> > > as shown below.
> > >
> > > @@ -1502,14 +1501,15 @@ int damon_call(struct damon_ctx *ctx, struct
> > > damon_call_control *control)
> > > control->canceled = false;
> > > INIT_LIST_HEAD(&control->list);
> > >
> > > - mutex_lock(&ctx->call_controls_lock);
> > > + spin_lock(&ctx->call_controls_lock);
> > > + /* damon_is_running */
> > > if (ctx->kdamond) {
> > > list_add_tail(&control->list, &ctx->call_controls);
> > > } else {
> > > - mutex_unlock(&ctx->call_controls_lock);
> > > + spin_unlock(&ctx->call_controls_lock);
> > > return -EINVAL;
> > > }
> > > - mutex_unlock(&ctx->call_controls_lock);
> > > + spin_unlock(&ctx->call_controls_lock);
> > >
> > > if (control->repeat)
> > > return 0;
> >
> > Are you saying the above diff can fix the damon_call() use-after-free bug [1]?
> > Can you please elaborate why you think so?
> >
> > [1] https://lore.kernel.org/20251231012315.75835-1-sj@kernel.org
> >
>
> The above code works fine with spin_lock. However, when booting the kernel,
> the spin_lock call trace from damon_call() is output as follows:
> If you have any experience with the following, please share it.
Can you please reply to my questions above, first?
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2025-12-31 15:32 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-29 14:55 [RFC PATCH v1] mm: improve call_controls_lock Asier Gutierrez
2025-12-29 15:22 ` SeongJae Park
2025-12-30 9:02 ` Gutierrez Asier
2025-12-31 5:01 ` SeongJae Park
2025-12-31 2:15 ` JaeJoon Jung
2025-12-31 4:59 ` SeongJae Park
2025-12-31 6:10 ` JaeJoon Jung
2025-12-31 7:51 ` JaeJoon Jung
2025-12-31 15:32 ` SeongJae Park
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox