Skip to content

Commit 58b4fba

Browse files
ubizjakakpm00
authored andcommitted
ucount: use atomic_long_try_cmpxchg() in atomic_long_inc_below()
Use atomic_long_try_cmpxchg() instead of atomic_long_cmpxchg (*ptr, old, new) == old in atomic_long_inc_below(). 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, atomic_long_try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails, enabling further code simplifications. No functional change intended. Link: https://lkml.kernel.org/r/20250721174610.28361-2-ubizjak@gmail.com Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Reviewed-by: Alexey Gladkov <legion@kernel.org> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: "Paul E. McKenney" <paulmck@kernel.org> Cc: Alexey Gladkov <legion@kernel.org> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: MengEn Sun <mengensun@tencent.com> Cc: "Thomas Weißschuh" <linux@weissschuh.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent f8cd919 commit 58b4fba

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

kernel/ucount.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,14 @@ void put_ucounts(struct ucounts *ucounts)
201201

202202
static inline bool atomic_long_inc_below(atomic_long_t *v, long u)
203203
{
204-
long c, old;
205-
c = atomic_long_read(v);
206-
for (;;) {
204+
long c = atomic_long_read(v);
205+
206+
do {
207207
if (unlikely(c >= u))
208208
return false;
209-
old = atomic_long_cmpxchg(v, c, c+1);
210-
if (likely(old == c))
211-
return true;
212-
c = old;
213-
}
209+
} while (!atomic_long_try_cmpxchg(v, &c, c+1));
210+
211+
return true;
214212
}
215213

216214
struct ucounts *inc_ucount(struct user_namespace *ns, kuid_t uid,

0 commit comments

Comments
 (0)