public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Carlier <devnexen@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>
Cc: Suren Baghdasaryan <surenb@google.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	linux-kernel@vger.kernel.org, David Carlier <devnexen@gmail.com>
Subject: [PATCH] sched/psi: Use try_cmpxchg() helpers in event and rtpoll wakeup paths
Date: Sat, 18 Apr 2026 21:42:35 +0100	[thread overview]
Message-ID: <20260418204235.9957-1-devnexen@gmail.com> (raw)

In update_triggers(), the event-generation gate uses
cmpxchg(&t->event, 0, 1) == 0 to detect a 0 -> 1 transition. On x86
this compiles to CMPXCHG followed by a redundant compare of the
returned value against the expected. try_cmpxchg() avoids the extra
compare by reading ZF directly from CMPXCHG. Convert the call site
to the try_cmpxchg() form, using a per-iteration comparand local so
the failure-path writeback does not leak across loop iterations.

In psi_rtpoll_worker(), the wait_event_interruptible() condition
uses atomic_cmpxchg(&group->rtpoll_wakeup, 1, 0) as a test-and-clear
of the wakeup flag. An inline conversion is unsafe: try_cmpxchg()
writes the observed value back into the expected-value lvalue on
failure, and wait_event_interruptible() re-evaluates the condition
many times per call, which would cause the comparand to drift and
wakeups to be lost.

Factor the test-and-clear into psi_rtpoll_consume_wakeup(), which
materializes a fresh comparand per invocation, and call it from the
wait_event_interruptible() condition. Use the fully ordered
atomic_try_cmpxchg() to preserve the original memory ordering.

No functional change.

Assisted-by: Claude:claude-opus-4-7 [tool claude-code]
Signed-off-by: David Carlier <devnexen@gmail.com>
---
 kernel/sched/psi.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index d9c9d9480a45..868833bca4d4 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -482,6 +482,7 @@ static void update_triggers(struct psi_group *group, u64 now,
 	 */
 	list_for_each_entry(t, triggers, node) {
 		u64 growth;
+		int zero = 0;
 		bool new_stall;
 
 		new_stall = aggregator_total[t->state] != total[t->state];
@@ -510,7 +511,7 @@ static void update_triggers(struct psi_group *group, u64 now,
 			continue;
 
 		/* Generate an event */
-		if (cmpxchg(&t->event, 0, 1) == 0) {
+		if (try_cmpxchg(&t->event, &zero, 1)) {
 			if (t->of)
 				kernfs_notify(t->of->kn);
 			else
@@ -735,6 +736,12 @@ static void psi_rtpoll_work(struct psi_group *group)
 	mutex_unlock(&group->rtpoll_trigger_lock);
 }
 
+static bool psi_rtpoll_consume_wakeup(struct psi_group *group)
+{
+	int wakeup = 1;
+	return atomic_try_cmpxchg(&group->rtpoll_wakeup, &wakeup, 0);
+}
+
 static int psi_rtpoll_worker(void *data)
 {
 	struct psi_group *group = (struct psi_group *)data;
@@ -743,7 +750,7 @@ static int psi_rtpoll_worker(void *data)
 
 	while (true) {
 		wait_event_interruptible(group->rtpoll_wait,
-				atomic_cmpxchg(&group->rtpoll_wakeup, 1, 0) ||
+				psi_rtpoll_consume_wakeup(group) ||
 				kthread_should_stop());
 		if (kthread_should_stop())
 			break;
-- 
2.53.0


                 reply	other threads:[~2026-04-18 20:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260418204235.9957-1-devnexen@gmail.com \
    --to=devnexen@gmail.com \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=surenb@google.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.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