[PATCH] sched/core: use try_cmpxchg() in mm_cid_pcpu_unset()

Uros Bizjak posted 1 patch 2 months, 2 weeks ago
There is a newer version of this series
kernel/sched/sched.h | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
[PATCH] sched/core: use try_cmpxchg() in mm_cid_pcpu_unset()
Posted by Uros Bizjak 2 months, 2 weeks ago
Use try_cmpxchg() instead of cmpxchg (*ptr, old, new) == old in
mm_cid_pcpu_unset(). x86 CMPXCHG instruction returns success in
ZF flag, so this change saves a compare after cmpxchg.

Also, try_cmpxchg() implicitly assigns old *ptr value to "old" when
cmpxchg fails, enabling substantial source code simplifications.

No functional change intended.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Vincent Guittot <vincent.guittot@linaro.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ben Segall <bsegall@google.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Valentin Schneider <vschneid@redhat.com>
---
 kernel/sched/sched.h | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 83e3aa917142..42006e8525c9 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3647,21 +3647,16 @@ static inline void mm_cid_put_lazy(struct task_struct *t)
 static inline int mm_cid_pcpu_unset(struct mm_struct *mm)
 {
 	struct mm_cid __percpu *pcpu_cid = mm->pcpu_cid;
-	int cid, res;
+	int cid;
 
 	lockdep_assert_irqs_disabled();
 	cid = __this_cpu_read(pcpu_cid->cid);
-	for (;;) {
+	do {
 		if (mm_cid_is_unset(cid))
 			return MM_CID_UNSET;
-		/*
-		 * Attempt transition from valid or lazy-put to unset.
-		 */
-		res = cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, cid, MM_CID_UNSET);
-		if (res == cid)
-			break;
-		cid = res;
-	}
+	/* Attempt transition from valid or lazy-put to unset. */
+	} while (!try_cmpxchg(&this_cpu_ptr(pcpu_cid)->cid, &cid, MM_CID_UNSET));
+
 	return cid;
 }
 
-- 
2.50.1