Merge tag 'nfs-for-5.11-1' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[linux-2.6-microblaze.git] / lib / sbitmap.c
index c1c8a4e..d693d92 100644 (file)
  */
 static inline bool sbitmap_deferred_clear(struct sbitmap_word *map)
 {
-       unsigned long mask, val;
-       bool ret = false;
-       unsigned long flags;
+       unsigned long mask;
 
-       spin_lock_irqsave(&map->swap_lock, flags);
-
-       if (!map->cleared)
-               goto out_unlock;
+       if (!READ_ONCE(map->cleared))
+               return false;
 
        /*
         * First get a stable cleared mask, setting the old mask to 0.
@@ -31,14 +27,9 @@ static inline bool sbitmap_deferred_clear(struct sbitmap_word *map)
        /*
         * Now clear the masked bits in our free word
         */
-       do {
-               val = map->word;
-       } while (cmpxchg(&map->word, val, val & ~mask) != val);
-
-       ret = true;
-out_unlock:
-       spin_unlock_irqrestore(&map->swap_lock, flags);
-       return ret;
+       atomic_long_andnot(mask, (atomic_long_t *)&map->word);
+       BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word));
+       return true;
 }
 
 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
@@ -80,7 +71,6 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
        for (i = 0; i < sb->map_nr; i++) {
                sb->map[i].depth = min(depth, bits_per_word);
                depth -= sb->map[i].depth;
-               spin_lock_init(&sb->map[i].swap_lock);
        }
        return 0;
 }
@@ -107,9 +97,11 @@ EXPORT_SYMBOL_GPL(sbitmap_resize);
 static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
                              unsigned int hint, bool wrap)
 {
-       unsigned int orig_hint = hint;
        int nr;
 
+       /* don't wrap if starting from 0 */
+       wrap = wrap && hint;
+
        while (1) {
                nr = find_next_zero_bit(word, depth, hint);
                if (unlikely(nr >= depth)) {
@@ -118,8 +110,8 @@ static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
                         * offset to 0 in a failure case, so start from 0 to
                         * exhaust the map.
                         */
-                       if (orig_hint && hint && wrap) {
-                               hint = orig_hint = 0;
+                       if (hint && wrap) {
+                               hint = 0;
                                continue;
                        }
                        return -1;