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/[email protected] Signed-off-by: Uros Bizjak <[email protected]> Reviewed-by: Alexey Gladkov <[email protected]> Cc: Sebastian Andrzej Siewior <[email protected]> Cc: "Paul E. McKenney" <[email protected]> Cc: Alexey Gladkov <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: MengEn Sun <[email protected]> Cc: "Thomas Weißschuh" <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
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)