Skip to content

Commit b4b11b5

Browse files
ubizjakebiederm
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. Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Reviewed-by: Alexey Gladkov <legion@kernel.org> Link: https://lkml.kernel.org/r/20221017144049.3404-3-ubizjak@gmail.com Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
1 parent 295227b commit b4b11b5

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

kernel/ucount.c

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

215215
static inline bool atomic_long_inc_below(atomic_long_t *v, long u)
216216
{
217-
long c, old;
218-
c = atomic_long_read(v);
219-
for (;;) {
217+
long c = atomic_long_read(v);
218+
219+
do {
220220
if (unlikely(c >= u))
221221
return false;
222-
old = atomic_long_cmpxchg(v, c, c+1);
223-
if (likely(old == c))
224-
return true;
225-
c = old;
226-
}
222+
} while (!atomic_long_try_cmpxchg(v, &c, c+1));
223+
224+
return true;
227225
}
228226

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

0 commit comments

Comments
 (0)