Skip to contentSkip to conversation

posix-timers: use atomic64_try_cmpxchg in __update_gt_cputime

PATCH Applied Uros Bizjak 2 participants 2 messages Jan 16, 2023 – Feb 6, 2023 Posix clocks and timers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 posix-timers: use atomic64_try_cmpxchg in __update_gt_cputime

Use atomic64_try_cmpxchg instead of atomic64_cmpxchg (*ptr, old, new) == old in __update_gt_cputime. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg).

Also, atomic64_try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails. There is no need to re-read the value in the loop.

No functional change intended.

Cc Thomas Gleixner <[email protected]>
---
 kernel/time/posix-cpu-timers.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
kernel/time/posix-cpu-timers.c +6 -7
index cb925e8ef9a8..2f5e9b34022c 100644
@@ -243,13 +243,12 @@ static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
  */
 static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
 {
-	u64 curr_cputime;
-retry:
-	curr_cputime = atomic64_read(cputime);
-	if (sum_cputime > curr_cputime) {
-		if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
-			goto retry;
-	}
+	u64 curr_cputime = atomic64_read(cputime);
+
+	do {
+		if (sum_cputime <= curr_cputime)
+			return;
+	} while (!atomic64_try_cmpxchg(cputime, &curr_cputime, sum_cputime));
 }
 
 static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
Signature · 1 line
2.39.0
merge bot Applied
#1 lore raw

[tip: timers/core] posix-timers: Use atomic64_try_cmpxchg() in __update_gt_cputime()

The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     915d4ad3830aa1a2dafda9b737749fb410cb9790
Gitweb:        https://git.kernel.org/tip/915d4ad3830aa1a2dafda9b737749fb410cb9790
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Mon, 16 Jan 2023 17:53:37 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 06 Feb 2023 14:22:09 +01:00

posix-timers: Use atomic64_try_cmpxchg() in __update_gt_cputime()

Use atomic64_try_cmpxchg() instead of atomic64_cmpxchg() in __update_gt_cputime(). The x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg() (and related move instruction in front of cmpxchg()).

Also, atomic64_try_cmpxchg() implicitly assigns old *ptr value to "old" when cmpxchg() fails. There is no need to re-read the value in the loop.

No functional change intended.

---
 kernel/time/posix-cpu-timers.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
kernel/time/posix-cpu-timers.c +6 -7
index cb925e8..2f5e9b3 100644
@@ -243,13 +243,12 @@ static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
  */
 static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
 {
-	u64 curr_cputime;
-retry:
-	curr_cputime = atomic64_read(cputime);
-	if (sum_cputime > curr_cputime) {
-		if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
-			goto retry;
-	}
+	u64 curr_cputime = atomic64_read(cputime);
+
+	do {
+		if (sum_cputime <= curr_cputime)
+			return;
+	} while (!atomic64_try_cmpxchg(cputime, &curr_cputime, sum_cputime));
 }
 
 static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,