linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Asier Gutierrez <gutierrez.asier@huawei-partners.com>
To: <gutierrez.asier@huawei-partners.com>, <sj@kernel.org>,
	<akpm@linux-foundation.org>, <damon@lists.linux.dev>,
	<linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
	<wangkefeng.wang@huawei.com>, <artem.kuzin@huawei.com>,
	<stepanov.anatoly@huawei.com>
Subject: [RFC PATCH v1] mm: improve call_controls_lock
Date: Mon, 29 Dec 2025 14:55:32 +0000	[thread overview]
Message-ID: <20251229145533.2437293-1-gutierrez.asier@huawei-partners.com> (raw)

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




             reply	other threads:[~2025-12-29 14:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-29 14:55 Asier Gutierrez [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251229145533.2437293-1-gutierrez.asier@huawei-partners.com \
    --to=gutierrez.asier@huawei-partners.com \
    --cc=akpm@linux-foundation.org \
    --cc=artem.kuzin@huawei.com \
    --cc=damon@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=sj@kernel.org \
    --cc=stepanov.anatoly@huawei.com \
    --cc=wangkefeng.wang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox