Skip to content

Commit c35227d

Browse files
ubizjakaxboe
authored andcommitted
sbitmap: Use atomic_long_try_cmpxchg in __sbitmap_queue_get_batch
Use atomic_long_try_cmpxchg instead of atomic_long_cmpxchg (*ptr, old, new) == old in __sbitmap_queue_get_batch. 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_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails, enabling further code simplifications, e.g. an extra memory read can be avoided in the loop. No functional change intended. Cc: Jens Axboe <[email protected]> Signed-off-by: Uros Bizjak <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 1de7c3c commit c35227d

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/sbitmap.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,16 @@ unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
533533
nr = find_first_zero_bit(&map->word, map_depth);
534534
if (nr + nr_tags <= map_depth) {
535535
atomic_long_t *ptr = (atomic_long_t *) &map->word;
536-
unsigned long val, ret;
536+
unsigned long val;
537537

538538
get_mask = ((1UL << nr_tags) - 1) << nr;
539+
val = READ_ONCE(map->word);
539540
do {
540-
val = READ_ONCE(map->word);
541541
if ((val & ~get_mask) != val)
542542
goto next;
543-
ret = atomic_long_cmpxchg(ptr, val, get_mask | val);
544-
} while (ret != val);
545-
get_mask = (get_mask & ~ret) >> nr;
543+
} while (!atomic_long_try_cmpxchg(ptr, &val,
544+
get_mask | val));
545+
get_mask = (get_mask & ~val) >> nr;
546546
if (get_mask) {
547547
*offset = nr + (index << sb->shift);
548548
update_alloc_hint_after_get(sb, depth, hint,

0 commit comments

Comments
 (0)