10fc479b1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 288459642SOmar Sandoval /* 388459642SOmar Sandoval * Copyright (C) 2016 Facebook 488459642SOmar Sandoval * Copyright (C) 2013-2014 Jens Axboe 588459642SOmar Sandoval */ 688459642SOmar Sandoval 7af8601adSIngo Molnar #include <linux/sched.h> 898d95416SOmar Sandoval #include <linux/random.h> 988459642SOmar Sandoval #include <linux/sbitmap.h> 1024af1ccfSOmar Sandoval #include <linux/seq_file.h> 1188459642SOmar Sandoval 12c548e62bSMing Lei static int init_alloc_hint(struct sbitmap *sb, gfp_t flags) 13bf2c4282SMing Lei { 14c548e62bSMing Lei unsigned depth = sb->depth; 15bf2c4282SMing Lei 16c548e62bSMing Lei sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags); 17c548e62bSMing Lei if (!sb->alloc_hint) 18bf2c4282SMing Lei return -ENOMEM; 19bf2c4282SMing Lei 20c548e62bSMing Lei if (depth && !sb->round_robin) { 21bf2c4282SMing Lei int i; 22bf2c4282SMing Lei 23bf2c4282SMing Lei for_each_possible_cpu(i) 24c548e62bSMing Lei *per_cpu_ptr(sb->alloc_hint, i) = prandom_u32() % depth; 25bf2c4282SMing Lei } 26bf2c4282SMing Lei return 0; 27bf2c4282SMing Lei } 28bf2c4282SMing Lei 29c548e62bSMing Lei static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb, 30bf2c4282SMing Lei unsigned int depth) 31bf2c4282SMing Lei { 32bf2c4282SMing Lei unsigned hint; 33bf2c4282SMing Lei 34c548e62bSMing Lei hint = this_cpu_read(*sb->alloc_hint); 35bf2c4282SMing Lei if (unlikely(hint >= depth)) { 36bf2c4282SMing Lei hint = depth ? prandom_u32() % depth : 0; 37c548e62bSMing Lei this_cpu_write(*sb->alloc_hint, hint); 38bf2c4282SMing Lei } 39bf2c4282SMing Lei 40bf2c4282SMing Lei return hint; 41bf2c4282SMing Lei } 42bf2c4282SMing Lei 43c548e62bSMing Lei static inline void update_alloc_hint_after_get(struct sbitmap *sb, 44bf2c4282SMing Lei unsigned int depth, 45bf2c4282SMing Lei unsigned int hint, 46bf2c4282SMing Lei unsigned int nr) 47bf2c4282SMing Lei { 48bf2c4282SMing Lei if (nr == -1) { 49bf2c4282SMing Lei /* If the map is full, a hint won't do us much good. */ 50c548e62bSMing Lei this_cpu_write(*sb->alloc_hint, 0); 51c548e62bSMing Lei } else if (nr == hint || unlikely(sb->round_robin)) { 52bf2c4282SMing Lei /* Only update the hint if we used it. */ 53bf2c4282SMing Lei hint = nr + 1; 54bf2c4282SMing Lei if (hint >= depth - 1) 55bf2c4282SMing Lei hint = 0; 56c548e62bSMing Lei this_cpu_write(*sb->alloc_hint, hint); 57bf2c4282SMing Lei } 58bf2c4282SMing Lei } 59bf2c4282SMing Lei 60b2dbff1bSJens Axboe /* 61b2dbff1bSJens Axboe * See if we have deferred clears that we can batch move 62b2dbff1bSJens Axboe */ 63b78beea0SPavel Begunkov static inline bool sbitmap_deferred_clear(struct sbitmap_word *map) 64b2dbff1bSJens Axboe { 65c3250c8dSPavel Begunkov unsigned long mask; 66b2dbff1bSJens Axboe 67661d4f55SPavel Begunkov if (!READ_ONCE(map->cleared)) 68661d4f55SPavel Begunkov return false; 69b2dbff1bSJens Axboe 70b2dbff1bSJens Axboe /* 71b2dbff1bSJens Axboe * First get a stable cleared mask, setting the old mask to 0. 72b2dbff1bSJens Axboe */ 73b78beea0SPavel Begunkov mask = xchg(&map->cleared, 0); 74b2dbff1bSJens Axboe 75b2dbff1bSJens Axboe /* 76b2dbff1bSJens Axboe * Now clear the masked bits in our free word 77b2dbff1bSJens Axboe */ 78c3250c8dSPavel Begunkov atomic_long_andnot(mask, (atomic_long_t *)&map->word); 79c3250c8dSPavel Begunkov BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word)); 80661d4f55SPavel Begunkov return true; 81b2dbff1bSJens Axboe } 82b2dbff1bSJens Axboe 8388459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, 84c548e62bSMing Lei gfp_t flags, int node, bool round_robin, 85c548e62bSMing Lei bool alloc_hint) 8688459642SOmar Sandoval { 8788459642SOmar Sandoval unsigned int bits_per_word; 8888459642SOmar Sandoval 892d13b1eaSMing Lei if (shift < 0) 902d13b1eaSMing Lei shift = sbitmap_calculate_shift(depth); 912d13b1eaSMing Lei 9288459642SOmar Sandoval bits_per_word = 1U << shift; 9388459642SOmar Sandoval if (bits_per_word > BITS_PER_LONG) 9488459642SOmar Sandoval return -EINVAL; 9588459642SOmar Sandoval 9688459642SOmar Sandoval sb->shift = shift; 9788459642SOmar Sandoval sb->depth = depth; 9888459642SOmar Sandoval sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); 99efe1f3a1SMing Lei sb->round_robin = round_robin; 10088459642SOmar Sandoval 10188459642SOmar Sandoval if (depth == 0) { 10288459642SOmar Sandoval sb->map = NULL; 10388459642SOmar Sandoval return 0; 10488459642SOmar Sandoval } 10588459642SOmar Sandoval 106c548e62bSMing Lei if (alloc_hint) { 107c548e62bSMing Lei if (init_alloc_hint(sb, flags)) 10888459642SOmar Sandoval return -ENOMEM; 109c548e62bSMing Lei } else { 110c548e62bSMing Lei sb->alloc_hint = NULL; 111c548e62bSMing Lei } 112c548e62bSMing Lei 113863a66cdSMing Lei sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node); 114c548e62bSMing Lei if (!sb->map) { 115c548e62bSMing Lei free_percpu(sb->alloc_hint); 116c548e62bSMing Lei return -ENOMEM; 117c548e62bSMing Lei } 11888459642SOmar Sandoval 11988459642SOmar Sandoval return 0; 12088459642SOmar Sandoval } 12188459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_init_node); 12288459642SOmar Sandoval 12388459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth) 12488459642SOmar Sandoval { 12588459642SOmar Sandoval unsigned int bits_per_word = 1U << sb->shift; 12688459642SOmar Sandoval unsigned int i; 12788459642SOmar Sandoval 128b2dbff1bSJens Axboe for (i = 0; i < sb->map_nr; i++) 129b78beea0SPavel Begunkov sbitmap_deferred_clear(&sb->map[i]); 130b2dbff1bSJens Axboe 13188459642SOmar Sandoval sb->depth = depth; 13288459642SOmar Sandoval sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); 13388459642SOmar Sandoval } 13488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_resize); 13588459642SOmar Sandoval 136c05e6673SOmar Sandoval static int __sbitmap_get_word(unsigned long *word, unsigned long depth, 137c05e6673SOmar Sandoval unsigned int hint, bool wrap) 13888459642SOmar Sandoval { 13988459642SOmar Sandoval int nr; 14088459642SOmar Sandoval 1410eff1f1aSPavel Begunkov /* don't wrap if starting from 0 */ 1420eff1f1aSPavel Begunkov wrap = wrap && hint; 1430eff1f1aSPavel Begunkov 14488459642SOmar Sandoval while (1) { 145c05e6673SOmar Sandoval nr = find_next_zero_bit(word, depth, hint); 146c05e6673SOmar Sandoval if (unlikely(nr >= depth)) { 14788459642SOmar Sandoval /* 14888459642SOmar Sandoval * We started with an offset, and we didn't reset the 14988459642SOmar Sandoval * offset to 0 in a failure case, so start from 0 to 15088459642SOmar Sandoval * exhaust the map. 15188459642SOmar Sandoval */ 1520eff1f1aSPavel Begunkov if (hint && wrap) { 1530eff1f1aSPavel Begunkov hint = 0; 15488459642SOmar Sandoval continue; 15588459642SOmar Sandoval } 15688459642SOmar Sandoval return -1; 15788459642SOmar Sandoval } 15888459642SOmar Sandoval 1594ace53f1SOmar Sandoval if (!test_and_set_bit_lock(nr, word)) 16088459642SOmar Sandoval break; 16188459642SOmar Sandoval 16288459642SOmar Sandoval hint = nr + 1; 163c05e6673SOmar Sandoval if (hint >= depth - 1) 16488459642SOmar Sandoval hint = 0; 16588459642SOmar Sandoval } 16688459642SOmar Sandoval 16788459642SOmar Sandoval return nr; 16888459642SOmar Sandoval } 16988459642SOmar Sandoval 170ea86ea2cSJens Axboe static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index, 171efe1f3a1SMing Lei unsigned int alloc_hint) 172ea86ea2cSJens Axboe { 173b78beea0SPavel Begunkov struct sbitmap_word *map = &sb->map[index]; 174ea86ea2cSJens Axboe int nr; 175ea86ea2cSJens Axboe 176ea86ea2cSJens Axboe do { 1773301bc53SMing Lei nr = __sbitmap_get_word(&map->word, __map_depth(sb, index), 1783301bc53SMing Lei alloc_hint, !sb->round_robin); 179ea86ea2cSJens Axboe if (nr != -1) 180ea86ea2cSJens Axboe break; 181b78beea0SPavel Begunkov if (!sbitmap_deferred_clear(map)) 182ea86ea2cSJens Axboe break; 183ea86ea2cSJens Axboe } while (1); 184ea86ea2cSJens Axboe 185ea86ea2cSJens Axboe return nr; 186ea86ea2cSJens Axboe } 187ea86ea2cSJens Axboe 188c548e62bSMing Lei static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint) 18988459642SOmar Sandoval { 19088459642SOmar Sandoval unsigned int i, index; 19188459642SOmar Sandoval int nr = -1; 19288459642SOmar Sandoval 19388459642SOmar Sandoval index = SB_NR_TO_INDEX(sb, alloc_hint); 19488459642SOmar Sandoval 19527fae429SJens Axboe /* 19627fae429SJens Axboe * Unless we're doing round robin tag allocation, just use the 19727fae429SJens Axboe * alloc_hint to find the right word index. No point in looping 19827fae429SJens Axboe * twice in find_next_zero_bit() for that case. 19927fae429SJens Axboe */ 200efe1f3a1SMing Lei if (sb->round_robin) 20127fae429SJens Axboe alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); 20227fae429SJens Axboe else 20327fae429SJens Axboe alloc_hint = 0; 20427fae429SJens Axboe 20588459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 206efe1f3a1SMing Lei nr = sbitmap_find_bit_in_index(sb, index, alloc_hint); 20788459642SOmar Sandoval if (nr != -1) { 20888459642SOmar Sandoval nr += index << sb->shift; 20988459642SOmar Sandoval break; 21088459642SOmar Sandoval } 21188459642SOmar Sandoval 21288459642SOmar Sandoval /* Jump to next index. */ 21388459642SOmar Sandoval alloc_hint = 0; 21427fae429SJens Axboe if (++index >= sb->map_nr) 21527fae429SJens Axboe index = 0; 21688459642SOmar Sandoval } 21788459642SOmar Sandoval 21888459642SOmar Sandoval return nr; 21988459642SOmar Sandoval } 220c548e62bSMing Lei 221c548e62bSMing Lei int sbitmap_get(struct sbitmap *sb) 222c548e62bSMing Lei { 223c548e62bSMing Lei int nr; 224c548e62bSMing Lei unsigned int hint, depth; 225c548e62bSMing Lei 226c548e62bSMing Lei if (WARN_ON_ONCE(unlikely(!sb->alloc_hint))) 227c548e62bSMing Lei return -1; 228c548e62bSMing Lei 229c548e62bSMing Lei depth = READ_ONCE(sb->depth); 230c548e62bSMing Lei hint = update_alloc_hint_before_get(sb, depth); 231c548e62bSMing Lei nr = __sbitmap_get(sb, hint); 232c548e62bSMing Lei update_alloc_hint_after_get(sb, depth, hint, nr); 233c548e62bSMing Lei 234c548e62bSMing Lei return nr; 235c548e62bSMing Lei } 23688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get); 23788459642SOmar Sandoval 238c548e62bSMing Lei static int __sbitmap_get_shallow(struct sbitmap *sb, 239c548e62bSMing Lei unsigned int alloc_hint, 240c05e6673SOmar Sandoval unsigned long shallow_depth) 241c05e6673SOmar Sandoval { 242c05e6673SOmar Sandoval unsigned int i, index; 243c05e6673SOmar Sandoval int nr = -1; 244c05e6673SOmar Sandoval 245c05e6673SOmar Sandoval index = SB_NR_TO_INDEX(sb, alloc_hint); 246c05e6673SOmar Sandoval 247c05e6673SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 248b2dbff1bSJens Axboe again: 249c05e6673SOmar Sandoval nr = __sbitmap_get_word(&sb->map[index].word, 2503301bc53SMing Lei min_t(unsigned int, 2513301bc53SMing Lei __map_depth(sb, index), 2523301bc53SMing Lei shallow_depth), 253c05e6673SOmar Sandoval SB_NR_TO_BIT(sb, alloc_hint), true); 254c05e6673SOmar Sandoval if (nr != -1) { 255c05e6673SOmar Sandoval nr += index << sb->shift; 256c05e6673SOmar Sandoval break; 257c05e6673SOmar Sandoval } 258c05e6673SOmar Sandoval 259b78beea0SPavel Begunkov if (sbitmap_deferred_clear(&sb->map[index])) 260b2dbff1bSJens Axboe goto again; 261b2dbff1bSJens Axboe 262c05e6673SOmar Sandoval /* Jump to next index. */ 263c05e6673SOmar Sandoval index++; 264c05e6673SOmar Sandoval alloc_hint = index << sb->shift; 265c05e6673SOmar Sandoval 266c05e6673SOmar Sandoval if (index >= sb->map_nr) { 267c05e6673SOmar Sandoval index = 0; 268c05e6673SOmar Sandoval alloc_hint = 0; 269c05e6673SOmar Sandoval } 270c05e6673SOmar Sandoval } 271c05e6673SOmar Sandoval 272c05e6673SOmar Sandoval return nr; 273c05e6673SOmar Sandoval } 274c548e62bSMing Lei 275c548e62bSMing Lei int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth) 276c548e62bSMing Lei { 277c548e62bSMing Lei int nr; 278c548e62bSMing Lei unsigned int hint, depth; 279c548e62bSMing Lei 280c548e62bSMing Lei if (WARN_ON_ONCE(unlikely(!sb->alloc_hint))) 281c548e62bSMing Lei return -1; 282c548e62bSMing Lei 283c548e62bSMing Lei depth = READ_ONCE(sb->depth); 284c548e62bSMing Lei hint = update_alloc_hint_before_get(sb, depth); 285c548e62bSMing Lei nr = __sbitmap_get_shallow(sb, hint, shallow_depth); 286c548e62bSMing Lei update_alloc_hint_after_get(sb, depth, hint, nr); 287c548e62bSMing Lei 288c548e62bSMing Lei return nr; 289c548e62bSMing Lei } 290c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get_shallow); 291c05e6673SOmar Sandoval 29288459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb) 29388459642SOmar Sandoval { 29488459642SOmar Sandoval unsigned int i; 29588459642SOmar Sandoval 29688459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 297b2dbff1bSJens Axboe if (sb->map[i].word & ~sb->map[i].cleared) 29888459642SOmar Sandoval return true; 29988459642SOmar Sandoval } 30088459642SOmar Sandoval return false; 30188459642SOmar Sandoval } 30288459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); 30388459642SOmar Sandoval 304ea86ea2cSJens Axboe static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set) 30588459642SOmar Sandoval { 30660658e0dSColin Ian King unsigned int i, weight = 0; 30788459642SOmar Sandoval 30888459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 30988459642SOmar Sandoval const struct sbitmap_word *word = &sb->map[i]; 3103301bc53SMing Lei unsigned int word_depth = __map_depth(sb, i); 31188459642SOmar Sandoval 312ea86ea2cSJens Axboe if (set) 3133301bc53SMing Lei weight += bitmap_weight(&word->word, word_depth); 314ea86ea2cSJens Axboe else 3153301bc53SMing Lei weight += bitmap_weight(&word->cleared, word_depth); 31688459642SOmar Sandoval } 31788459642SOmar Sandoval return weight; 31888459642SOmar Sandoval } 319ea86ea2cSJens Axboe 320ea86ea2cSJens Axboe static unsigned int sbitmap_cleared(const struct sbitmap *sb) 321ea86ea2cSJens Axboe { 322ea86ea2cSJens Axboe return __sbitmap_weight(sb, false); 323ea86ea2cSJens Axboe } 32488459642SOmar Sandoval 325cbb9950bSMing Lei unsigned int sbitmap_weight(const struct sbitmap *sb) 326cbb9950bSMing Lei { 327cbb9950bSMing Lei return __sbitmap_weight(sb, true) - sbitmap_cleared(sb); 328cbb9950bSMing Lei } 329cbb9950bSMing Lei EXPORT_SYMBOL_GPL(sbitmap_weight); 330cbb9950bSMing Lei 33124af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m) 33224af1ccfSOmar Sandoval { 33324af1ccfSOmar Sandoval seq_printf(m, "depth=%u\n", sb->depth); 334cbb9950bSMing Lei seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); 335ea86ea2cSJens Axboe seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb)); 33624af1ccfSOmar Sandoval seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); 33724af1ccfSOmar Sandoval seq_printf(m, "map_nr=%u\n", sb->map_nr); 33824af1ccfSOmar Sandoval } 33924af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show); 34024af1ccfSOmar Sandoval 34124af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) 34224af1ccfSOmar Sandoval { 34324af1ccfSOmar Sandoval if ((offset & 0xf) == 0) { 34424af1ccfSOmar Sandoval if (offset != 0) 34524af1ccfSOmar Sandoval seq_putc(m, '\n'); 34624af1ccfSOmar Sandoval seq_printf(m, "%08x:", offset); 34724af1ccfSOmar Sandoval } 34824af1ccfSOmar Sandoval if ((offset & 0x1) == 0) 34924af1ccfSOmar Sandoval seq_putc(m, ' '); 35024af1ccfSOmar Sandoval seq_printf(m, "%02x", byte); 35124af1ccfSOmar Sandoval } 35224af1ccfSOmar Sandoval 35324af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) 35424af1ccfSOmar Sandoval { 35524af1ccfSOmar Sandoval u8 byte = 0; 35624af1ccfSOmar Sandoval unsigned int byte_bits = 0; 35724af1ccfSOmar Sandoval unsigned int offset = 0; 35824af1ccfSOmar Sandoval int i; 35924af1ccfSOmar Sandoval 36024af1ccfSOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 36124af1ccfSOmar Sandoval unsigned long word = READ_ONCE(sb->map[i].word); 3626bf0eb55SJohn Garry unsigned long cleared = READ_ONCE(sb->map[i].cleared); 3633301bc53SMing Lei unsigned int word_bits = __map_depth(sb, i); 36424af1ccfSOmar Sandoval 3656bf0eb55SJohn Garry word &= ~cleared; 3666bf0eb55SJohn Garry 36724af1ccfSOmar Sandoval while (word_bits > 0) { 36824af1ccfSOmar Sandoval unsigned int bits = min(8 - byte_bits, word_bits); 36924af1ccfSOmar Sandoval 37024af1ccfSOmar Sandoval byte |= (word & (BIT(bits) - 1)) << byte_bits; 37124af1ccfSOmar Sandoval byte_bits += bits; 37224af1ccfSOmar Sandoval if (byte_bits == 8) { 37324af1ccfSOmar Sandoval emit_byte(m, offset, byte); 37424af1ccfSOmar Sandoval byte = 0; 37524af1ccfSOmar Sandoval byte_bits = 0; 37624af1ccfSOmar Sandoval offset++; 37724af1ccfSOmar Sandoval } 37824af1ccfSOmar Sandoval word >>= bits; 37924af1ccfSOmar Sandoval word_bits -= bits; 38024af1ccfSOmar Sandoval } 38124af1ccfSOmar Sandoval } 38224af1ccfSOmar Sandoval if (byte_bits) { 38324af1ccfSOmar Sandoval emit_byte(m, offset, byte); 38424af1ccfSOmar Sandoval offset++; 38524af1ccfSOmar Sandoval } 38624af1ccfSOmar Sandoval if (offset) 38724af1ccfSOmar Sandoval seq_putc(m, '\n'); 38824af1ccfSOmar Sandoval } 38924af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); 39024af1ccfSOmar Sandoval 391a3275539SOmar Sandoval static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq, 392a3275539SOmar Sandoval unsigned int depth) 39388459642SOmar Sandoval { 39488459642SOmar Sandoval unsigned int wake_batch; 395a3275539SOmar Sandoval unsigned int shallow_depth; 39688459642SOmar Sandoval 39788459642SOmar Sandoval /* 39888459642SOmar Sandoval * For each batch, we wake up one queue. We need to make sure that our 399a3275539SOmar Sandoval * batch size is small enough that the full depth of the bitmap, 400a3275539SOmar Sandoval * potentially limited by a shallow depth, is enough to wake up all of 401a3275539SOmar Sandoval * the queues. 402a3275539SOmar Sandoval * 403a3275539SOmar Sandoval * Each full word of the bitmap has bits_per_word bits, and there might 404a3275539SOmar Sandoval * be a partial word. There are depth / bits_per_word full words and 405a3275539SOmar Sandoval * depth % bits_per_word bits left over. In bitwise arithmetic: 406a3275539SOmar Sandoval * 407a3275539SOmar Sandoval * bits_per_word = 1 << shift 408a3275539SOmar Sandoval * depth / bits_per_word = depth >> shift 409a3275539SOmar Sandoval * depth % bits_per_word = depth & ((1 << shift) - 1) 410a3275539SOmar Sandoval * 411a3275539SOmar Sandoval * Each word can be limited to sbq->min_shallow_depth bits. 41288459642SOmar Sandoval */ 413a3275539SOmar Sandoval shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth); 414a3275539SOmar Sandoval depth = ((depth >> sbq->sb.shift) * shallow_depth + 415a3275539SOmar Sandoval min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth)); 416a3275539SOmar Sandoval wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1, 417a3275539SOmar Sandoval SBQ_WAKE_BATCH); 41888459642SOmar Sandoval 41988459642SOmar Sandoval return wake_batch; 42088459642SOmar Sandoval } 42188459642SOmar Sandoval 42288459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 423f4a644dbSOmar Sandoval int shift, bool round_robin, gfp_t flags, int node) 42488459642SOmar Sandoval { 42588459642SOmar Sandoval int ret; 42688459642SOmar Sandoval int i; 42788459642SOmar Sandoval 428efe1f3a1SMing Lei ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node, 429c548e62bSMing Lei round_robin, true); 43088459642SOmar Sandoval if (ret) 43188459642SOmar Sandoval return ret; 43288459642SOmar Sandoval 433a3275539SOmar Sandoval sbq->min_shallow_depth = UINT_MAX; 434a3275539SOmar Sandoval sbq->wake_batch = sbq_calc_wake_batch(sbq, depth); 43588459642SOmar Sandoval atomic_set(&sbq->wake_index, 0); 4365d2ee712SJens Axboe atomic_set(&sbq->ws_active, 0); 43788459642SOmar Sandoval 43848e28166SOmar Sandoval sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); 43988459642SOmar Sandoval if (!sbq->ws) { 44088459642SOmar Sandoval sbitmap_free(&sbq->sb); 44188459642SOmar Sandoval return -ENOMEM; 44288459642SOmar Sandoval } 44388459642SOmar Sandoval 44488459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 44588459642SOmar Sandoval init_waitqueue_head(&sbq->ws[i].wait); 44688459642SOmar Sandoval atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch); 44788459642SOmar Sandoval } 448f4a644dbSOmar Sandoval 44988459642SOmar Sandoval return 0; 45088459642SOmar Sandoval } 45188459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); 45288459642SOmar Sandoval 453180dccb0SLaibin Qiu static inline void __sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq, 454180dccb0SLaibin Qiu unsigned int wake_batch) 45588459642SOmar Sandoval { 4566c0ca7aeSOmar Sandoval int i; 4576c0ca7aeSOmar Sandoval 4586c0ca7aeSOmar Sandoval if (sbq->wake_batch != wake_batch) { 4596c0ca7aeSOmar Sandoval WRITE_ONCE(sbq->wake_batch, wake_batch); 4606c0ca7aeSOmar Sandoval /* 461e6fc4649SMing Lei * Pairs with the memory barrier in sbitmap_queue_wake_up() 462e6fc4649SMing Lei * to ensure that the batch size is updated before the wait 463e6fc4649SMing Lei * counts. 4646c0ca7aeSOmar Sandoval */ 465a0934fd2SAndrea Parri smp_mb(); 4666c0ca7aeSOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) 4676c0ca7aeSOmar Sandoval atomic_set(&sbq->ws[i].wait_cnt, 1); 4686c0ca7aeSOmar Sandoval } 469a3275539SOmar Sandoval } 470a3275539SOmar Sandoval 471180dccb0SLaibin Qiu static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq, 472180dccb0SLaibin Qiu unsigned int depth) 473180dccb0SLaibin Qiu { 474180dccb0SLaibin Qiu unsigned int wake_batch; 475180dccb0SLaibin Qiu 476180dccb0SLaibin Qiu wake_batch = sbq_calc_wake_batch(sbq, depth); 477180dccb0SLaibin Qiu __sbitmap_queue_update_wake_batch(sbq, wake_batch); 478180dccb0SLaibin Qiu } 479180dccb0SLaibin Qiu 480180dccb0SLaibin Qiu void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq, 481180dccb0SLaibin Qiu unsigned int users) 482180dccb0SLaibin Qiu { 483180dccb0SLaibin Qiu unsigned int wake_batch; 48410825410SLaibin Qiu unsigned int min_batch; 48510825410SLaibin Qiu unsigned int depth = (sbq->sb.depth + users - 1) / users; 486180dccb0SLaibin Qiu 48710825410SLaibin Qiu min_batch = sbq->sb.depth >= (4 * SBQ_WAIT_QUEUES) ? 4 : 1; 48810825410SLaibin Qiu 48910825410SLaibin Qiu wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES, 49010825410SLaibin Qiu min_batch, SBQ_WAKE_BATCH); 491180dccb0SLaibin Qiu __sbitmap_queue_update_wake_batch(sbq, wake_batch); 492180dccb0SLaibin Qiu } 493180dccb0SLaibin Qiu EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch); 494180dccb0SLaibin Qiu 495a3275539SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) 496a3275539SOmar Sandoval { 497a3275539SOmar Sandoval sbitmap_queue_update_wake_batch(sbq, depth); 49888459642SOmar Sandoval sbitmap_resize(&sbq->sb, depth); 49988459642SOmar Sandoval } 50088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize); 50188459642SOmar Sandoval 502f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq) 50340aabb67SOmar Sandoval { 504c548e62bSMing Lei return sbitmap_get(&sbq->sb); 50540aabb67SOmar Sandoval } 50640aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get); 50740aabb67SOmar Sandoval 5089672b0d4SJens Axboe unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, 5099672b0d4SJens Axboe unsigned int *offset) 5109672b0d4SJens Axboe { 5119672b0d4SJens Axboe struct sbitmap *sb = &sbq->sb; 5129672b0d4SJens Axboe unsigned int hint, depth; 5139672b0d4SJens Axboe unsigned long index, nr; 5149672b0d4SJens Axboe int i; 5159672b0d4SJens Axboe 5169672b0d4SJens Axboe if (unlikely(sb->round_robin)) 5179672b0d4SJens Axboe return 0; 5189672b0d4SJens Axboe 5199672b0d4SJens Axboe depth = READ_ONCE(sb->depth); 5209672b0d4SJens Axboe hint = update_alloc_hint_before_get(sb, depth); 5219672b0d4SJens Axboe 5229672b0d4SJens Axboe index = SB_NR_TO_INDEX(sb, hint); 5239672b0d4SJens Axboe 5249672b0d4SJens Axboe for (i = 0; i < sb->map_nr; i++) { 5259672b0d4SJens Axboe struct sbitmap_word *map = &sb->map[index]; 5269672b0d4SJens Axboe unsigned long get_mask; 5273301bc53SMing Lei unsigned int map_depth = __map_depth(sb, index); 5289672b0d4SJens Axboe 5299672b0d4SJens Axboe sbitmap_deferred_clear(map); 5303301bc53SMing Lei if (map->word == (1UL << (map_depth - 1)) - 1) 531fbb564a5Swuchi goto next; 5329672b0d4SJens Axboe 5333301bc53SMing Lei nr = find_first_zero_bit(&map->word, map_depth); 5343301bc53SMing Lei if (nr + nr_tags <= map_depth) { 5359672b0d4SJens Axboe atomic_long_t *ptr = (atomic_long_t *) &map->word; 536*c35227d4SUros Bizjak unsigned long val; 5379672b0d4SJens Axboe 538ddbfc34fSLiu Song get_mask = ((1UL << nr_tags) - 1) << nr; 5399672b0d4SJens Axboe val = READ_ONCE(map->word); 540*c35227d4SUros Bizjak do { 541fbb564a5Swuchi if ((val & ~get_mask) != val) 542fbb564a5Swuchi goto next; 543*c35227d4SUros Bizjak } while (!atomic_long_try_cmpxchg(ptr, &val, 544*c35227d4SUros Bizjak get_mask | val)); 545*c35227d4SUros Bizjak get_mask = (get_mask & ~val) >> nr; 5469672b0d4SJens Axboe if (get_mask) { 5479672b0d4SJens Axboe *offset = nr + (index << sb->shift); 5489672b0d4SJens Axboe update_alloc_hint_after_get(sb, depth, hint, 549ddbfc34fSLiu Song *offset + nr_tags - 1); 5509672b0d4SJens Axboe return get_mask; 5519672b0d4SJens Axboe } 5529672b0d4SJens Axboe } 553fbb564a5Swuchi next: 5549672b0d4SJens Axboe /* Jump to next index. */ 5559672b0d4SJens Axboe if (++index >= sb->map_nr) 5569672b0d4SJens Axboe index = 0; 5579672b0d4SJens Axboe } 5589672b0d4SJens Axboe 5599672b0d4SJens Axboe return 0; 5609672b0d4SJens Axboe } 5619672b0d4SJens Axboe 5623f607293SJohn Garry int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 563c05e6673SOmar Sandoval unsigned int shallow_depth) 564c05e6673SOmar Sandoval { 56561445b56SOmar Sandoval WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth); 56661445b56SOmar Sandoval 567c548e62bSMing Lei return sbitmap_get_shallow(&sbq->sb, shallow_depth); 568c05e6673SOmar Sandoval } 5693f607293SJohn Garry EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow); 570c05e6673SOmar Sandoval 571a3275539SOmar Sandoval void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, 572a3275539SOmar Sandoval unsigned int min_shallow_depth) 573a3275539SOmar Sandoval { 574a3275539SOmar Sandoval sbq->min_shallow_depth = min_shallow_depth; 575a3275539SOmar Sandoval sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth); 576a3275539SOmar Sandoval } 577a3275539SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); 578a3275539SOmar Sandoval 57988459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) 58088459642SOmar Sandoval { 58188459642SOmar Sandoval int i, wake_index; 58288459642SOmar Sandoval 5835d2ee712SJens Axboe if (!atomic_read(&sbq->ws_active)) 5845d2ee712SJens Axboe return NULL; 5855d2ee712SJens Axboe 58688459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 58788459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 58888459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 58988459642SOmar Sandoval 59088459642SOmar Sandoval if (waitqueue_active(&ws->wait)) { 59141723288SPavel Begunkov if (wake_index != atomic_read(&sbq->wake_index)) 59241723288SPavel Begunkov atomic_set(&sbq->wake_index, wake_index); 59388459642SOmar Sandoval return ws; 59488459642SOmar Sandoval } 59588459642SOmar Sandoval 59688459642SOmar Sandoval wake_index = sbq_index_inc(wake_index); 59788459642SOmar Sandoval } 59888459642SOmar Sandoval 59988459642SOmar Sandoval return NULL; 60088459642SOmar Sandoval } 60188459642SOmar Sandoval 602bce1b56cSJens Axboe static bool __sbq_wake_up(struct sbitmap_queue *sbq) 60388459642SOmar Sandoval { 60488459642SOmar Sandoval struct sbq_wait_state *ws; 605bce1b56cSJens Axboe unsigned int wake_batch; 606bce1b56cSJens Axboe int wait_cnt; 60748c03331SJan Kara bool ret; 60888459642SOmar Sandoval 60988459642SOmar Sandoval ws = sbq_wake_ptr(sbq); 610bce1b56cSJens Axboe if (!ws) 611c854ab57SJens Axboe return false; 61288459642SOmar Sandoval 613bce1b56cSJens Axboe wait_cnt = atomic_dec_return(&ws->wait_cnt); 614040b83fcSYu Kuai /* 615040b83fcSYu Kuai * For concurrent callers of this, callers should call this function 616040b83fcSYu Kuai * again to wakeup a new batch on a different 'ws'. 617040b83fcSYu Kuai */ 61848c03331SJan Kara if (wait_cnt < 0) 619040b83fcSYu Kuai return true; 620040b83fcSYu Kuai 62148c03331SJan Kara /* 62248c03331SJan Kara * If we decremented queue without waiters, retry to avoid lost 62348c03331SJan Kara * wakeups. 62448c03331SJan Kara */ 625040b83fcSYu Kuai if (wait_cnt > 0) 62648c03331SJan Kara return !waitqueue_active(&ws->wait); 627c854ab57SJens Axboe 62848c03331SJan Kara /* 62948c03331SJan Kara * When wait_cnt == 0, we have to be particularly careful as we are 63048c03331SJan Kara * responsible to reset wait_cnt regardless whether we've actually 63148c03331SJan Kara * woken up anybody. But in case we didn't wakeup anybody, we still 63248c03331SJan Kara * need to retry. 63348c03331SJan Kara */ 63448c03331SJan Kara ret = !waitqueue_active(&ws->wait); 635bce1b56cSJens Axboe wake_batch = READ_ONCE(sbq->wake_batch); 636bce1b56cSJens Axboe 6376c0ca7aeSOmar Sandoval /* 638040b83fcSYu Kuai * Wake up first in case that concurrent callers decrease wait_cnt 639040b83fcSYu Kuai * while waitqueue is empty. 640040b83fcSYu Kuai */ 641bce1b56cSJens Axboe wake_up_nr(&ws->wait, wake_batch); 642040b83fcSYu Kuai 643040b83fcSYu Kuai /* 6446c0ca7aeSOmar Sandoval * Pairs with the memory barrier in sbitmap_queue_resize() to 6456c0ca7aeSOmar Sandoval * ensure that we see the batch size update before the wait 6466c0ca7aeSOmar Sandoval * count is reset. 647040b83fcSYu Kuai * 648040b83fcSYu Kuai * Also pairs with the implicit barrier between decrementing wait_cnt 649040b83fcSYu Kuai * and checking for waitqueue_active() to make sure waitqueue_active() 650040b83fcSYu Kuai * sees result of the wakeup if atomic_dec_return() has seen the result 651040b83fcSYu Kuai * of atomic_set(). 6526c0ca7aeSOmar Sandoval */ 6536c0ca7aeSOmar Sandoval smp_mb__before_atomic(); 654c854ab57SJens Axboe 6556c0ca7aeSOmar Sandoval /* 656040b83fcSYu Kuai * Increase wake_index before updating wait_cnt, otherwise concurrent 657040b83fcSYu Kuai * callers can see valid wait_cnt in old waitqueue, which can cause 658040b83fcSYu Kuai * invalid wakeup on the old waitqueue. 6596c0ca7aeSOmar Sandoval */ 66088459642SOmar Sandoval sbq_index_atomic_inc(&sbq->wake_index); 661040b83fcSYu Kuai atomic_set(&ws->wait_cnt, wake_batch); 662c854ab57SJens Axboe 66348c03331SJan Kara return ret; 664c854ab57SJens Axboe } 665c854ab57SJens Axboe 666bce1b56cSJens Axboe void sbitmap_queue_wake_up(struct sbitmap_queue *sbq) 667c854ab57SJens Axboe { 668bce1b56cSJens Axboe while (__sbq_wake_up(sbq)) 669c854ab57SJens Axboe ; 67088459642SOmar Sandoval } 671bce1b56cSJens Axboe EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up); 67288459642SOmar Sandoval 6731aec5e4aSJens Axboe static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag) 6741aec5e4aSJens Axboe { 6751aec5e4aSJens Axboe if (likely(!sb->round_robin && tag < sb->depth)) 6769f8b93a7SJens Axboe data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag); 6771aec5e4aSJens Axboe } 6781aec5e4aSJens Axboe 6791aec5e4aSJens Axboe void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset, 6801aec5e4aSJens Axboe int *tags, int nr_tags) 6811aec5e4aSJens Axboe { 6821aec5e4aSJens Axboe struct sbitmap *sb = &sbq->sb; 6831aec5e4aSJens Axboe unsigned long *addr = NULL; 6841aec5e4aSJens Axboe unsigned long mask = 0; 6851aec5e4aSJens Axboe int i; 6861aec5e4aSJens Axboe 6871aec5e4aSJens Axboe smp_mb__before_atomic(); 6881aec5e4aSJens Axboe for (i = 0; i < nr_tags; i++) { 6891aec5e4aSJens Axboe const int tag = tags[i] - offset; 6901aec5e4aSJens Axboe unsigned long *this_addr; 6911aec5e4aSJens Axboe 6921aec5e4aSJens Axboe /* since we're clearing a batch, skip the deferred map */ 6931aec5e4aSJens Axboe this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word; 6941aec5e4aSJens Axboe if (!addr) { 6951aec5e4aSJens Axboe addr = this_addr; 6961aec5e4aSJens Axboe } else if (addr != this_addr) { 6971aec5e4aSJens Axboe atomic_long_andnot(mask, (atomic_long_t *) addr); 6981aec5e4aSJens Axboe mask = 0; 6991aec5e4aSJens Axboe addr = this_addr; 7001aec5e4aSJens Axboe } 7011aec5e4aSJens Axboe mask |= (1UL << SB_NR_TO_BIT(sb, tag)); 7021aec5e4aSJens Axboe } 7031aec5e4aSJens Axboe 7041aec5e4aSJens Axboe if (mask) 7051aec5e4aSJens Axboe atomic_long_andnot(mask, (atomic_long_t *) addr); 7061aec5e4aSJens Axboe 7071aec5e4aSJens Axboe smp_mb__after_atomic(); 708bce1b56cSJens Axboe sbitmap_queue_wake_up(sbq); 7091aec5e4aSJens Axboe sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(), 7101aec5e4aSJens Axboe tags[nr_tags - 1] - offset); 7111aec5e4aSJens Axboe } 7121aec5e4aSJens Axboe 71340aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 714f4a644dbSOmar Sandoval unsigned int cpu) 71588459642SOmar Sandoval { 716e6d1fa58SMing Lei /* 717e6d1fa58SMing Lei * Once the clear bit is set, the bit may be allocated out. 718e6d1fa58SMing Lei * 7199dbbc3b9SZhen Lei * Orders READ/WRITE on the associated instance(such as request 720e6d1fa58SMing Lei * of blk_mq) by this bit for avoiding race with re-allocation, 721e6d1fa58SMing Lei * and its pair is the memory barrier implied in __sbitmap_get_word. 722e6d1fa58SMing Lei * 723e6d1fa58SMing Lei * One invariant is that the clear bit has to be zero when the bit 724e6d1fa58SMing Lei * is in use. 725e6d1fa58SMing Lei */ 726e6d1fa58SMing Lei smp_mb__before_atomic(); 727ea86ea2cSJens Axboe sbitmap_deferred_clear_bit(&sbq->sb, nr); 728ea86ea2cSJens Axboe 729e6fc4649SMing Lei /* 730e6fc4649SMing Lei * Pairs with the memory barrier in set_current_state() to ensure the 731e6fc4649SMing Lei * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker 732e6fc4649SMing Lei * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the 733e6fc4649SMing Lei * waiter. See the comment on waitqueue_active(). 734e6fc4649SMing Lei */ 735e6fc4649SMing Lei smp_mb__after_atomic(); 736bce1b56cSJens Axboe sbitmap_queue_wake_up(sbq); 7371aec5e4aSJens Axboe sbitmap_update_cpu_hint(&sbq->sb, cpu, nr); 73888459642SOmar Sandoval } 73988459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear); 74088459642SOmar Sandoval 74188459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) 74288459642SOmar Sandoval { 74388459642SOmar Sandoval int i, wake_index; 74488459642SOmar Sandoval 74588459642SOmar Sandoval /* 746f66227deSOmar Sandoval * Pairs with the memory barrier in set_current_state() like in 747e6fc4649SMing Lei * sbitmap_queue_wake_up(). 74888459642SOmar Sandoval */ 74988459642SOmar Sandoval smp_mb(); 75088459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 75188459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 75288459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 75388459642SOmar Sandoval 75488459642SOmar Sandoval if (waitqueue_active(&ws->wait)) 75588459642SOmar Sandoval wake_up(&ws->wait); 75688459642SOmar Sandoval 75788459642SOmar Sandoval wake_index = sbq_index_inc(wake_index); 75888459642SOmar Sandoval } 75988459642SOmar Sandoval } 76088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); 76124af1ccfSOmar Sandoval 76224af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) 76324af1ccfSOmar Sandoval { 76424af1ccfSOmar Sandoval bool first; 76524af1ccfSOmar Sandoval int i; 76624af1ccfSOmar Sandoval 76724af1ccfSOmar Sandoval sbitmap_show(&sbq->sb, m); 76824af1ccfSOmar Sandoval 76924af1ccfSOmar Sandoval seq_puts(m, "alloc_hint={"); 77024af1ccfSOmar Sandoval first = true; 77124af1ccfSOmar Sandoval for_each_possible_cpu(i) { 77224af1ccfSOmar Sandoval if (!first) 77324af1ccfSOmar Sandoval seq_puts(m, ", "); 77424af1ccfSOmar Sandoval first = false; 775c548e62bSMing Lei seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i)); 77624af1ccfSOmar Sandoval } 77724af1ccfSOmar Sandoval seq_puts(m, "}\n"); 77824af1ccfSOmar Sandoval 77924af1ccfSOmar Sandoval seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); 78024af1ccfSOmar Sandoval seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); 7815d2ee712SJens Axboe seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active)); 78224af1ccfSOmar Sandoval 78324af1ccfSOmar Sandoval seq_puts(m, "ws={\n"); 78424af1ccfSOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 78524af1ccfSOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[i]; 78624af1ccfSOmar Sandoval 78724af1ccfSOmar Sandoval seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n", 78824af1ccfSOmar Sandoval atomic_read(&ws->wait_cnt), 78924af1ccfSOmar Sandoval waitqueue_active(&ws->wait) ? "active" : "inactive"); 79024af1ccfSOmar Sandoval } 79124af1ccfSOmar Sandoval seq_puts(m, "}\n"); 79224af1ccfSOmar Sandoval 793efe1f3a1SMing Lei seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin); 794a3275539SOmar Sandoval seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); 79524af1ccfSOmar Sandoval } 79624af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show); 7975d2ee712SJens Axboe 7989f6b7ef6SJens Axboe void sbitmap_add_wait_queue(struct sbitmap_queue *sbq, 7999f6b7ef6SJens Axboe struct sbq_wait_state *ws, 8009f6b7ef6SJens Axboe struct sbq_wait *sbq_wait) 8019f6b7ef6SJens Axboe { 8029f6b7ef6SJens Axboe if (!sbq_wait->sbq) { 8039f6b7ef6SJens Axboe sbq_wait->sbq = sbq; 8049f6b7ef6SJens Axboe atomic_inc(&sbq->ws_active); 8059f6b7ef6SJens Axboe add_wait_queue(&ws->wait, &sbq_wait->wait); 8069f6b7ef6SJens Axboe } 807df034c93SDavid Jeffery } 8089f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue); 8099f6b7ef6SJens Axboe 8109f6b7ef6SJens Axboe void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait) 8119f6b7ef6SJens Axboe { 8129f6b7ef6SJens Axboe list_del_init(&sbq_wait->wait.entry); 8139f6b7ef6SJens Axboe if (sbq_wait->sbq) { 8149f6b7ef6SJens Axboe atomic_dec(&sbq_wait->sbq->ws_active); 8159f6b7ef6SJens Axboe sbq_wait->sbq = NULL; 8169f6b7ef6SJens Axboe } 8179f6b7ef6SJens Axboe } 8189f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue); 8199f6b7ef6SJens Axboe 8205d2ee712SJens Axboe void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq, 8215d2ee712SJens Axboe struct sbq_wait_state *ws, 8225d2ee712SJens Axboe struct sbq_wait *sbq_wait, int state) 8235d2ee712SJens Axboe { 8249f6b7ef6SJens Axboe if (!sbq_wait->sbq) { 8255d2ee712SJens Axboe atomic_inc(&sbq->ws_active); 8269f6b7ef6SJens Axboe sbq_wait->sbq = sbq; 8275d2ee712SJens Axboe } 8285d2ee712SJens Axboe prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state); 8295d2ee712SJens Axboe } 8305d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait); 8315d2ee712SJens Axboe 8325d2ee712SJens Axboe void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws, 8335d2ee712SJens Axboe struct sbq_wait *sbq_wait) 8345d2ee712SJens Axboe { 8355d2ee712SJens Axboe finish_wait(&ws->wait, &sbq_wait->wait); 8369f6b7ef6SJens Axboe if (sbq_wait->sbq) { 8375d2ee712SJens Axboe atomic_dec(&sbq->ws_active); 8389f6b7ef6SJens Axboe sbq_wait->sbq = NULL; 8395d2ee712SJens Axboe } 8405d2ee712SJens Axboe } 8415d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_finish_wait); 842