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) 248032bf12SJason A. Donenfeld *per_cpu_ptr(sb->alloc_hint, i) = get_random_u32_below(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)) { 368032bf12SJason A. Donenfeld hint = depth ? get_random_u32_below(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 17008470a98SKemeng Shi static int sbitmap_find_bit_in_word(struct sbitmap_word *map, 17108470a98SKemeng Shi unsigned int depth, 17208470a98SKemeng Shi unsigned int alloc_hint, 17308470a98SKemeng Shi bool wrap) 174ea86ea2cSJens Axboe { 175ea86ea2cSJens Axboe int nr; 176ea86ea2cSJens Axboe 177ea86ea2cSJens Axboe do { 17808470a98SKemeng Shi nr = __sbitmap_get_word(&map->word, depth, 17908470a98SKemeng Shi alloc_hint, wrap); 180ea86ea2cSJens Axboe if (nr != -1) 181ea86ea2cSJens Axboe break; 182b78beea0SPavel Begunkov if (!sbitmap_deferred_clear(map)) 183ea86ea2cSJens Axboe break; 184ea86ea2cSJens Axboe } while (1); 185ea86ea2cSJens Axboe 186ea86ea2cSJens Axboe return nr; 187ea86ea2cSJens Axboe } 188ea86ea2cSJens Axboe 189*678418c6SKemeng Shi static int sbitmap_find_bit(struct sbitmap *sb, 190*678418c6SKemeng Shi unsigned int depth, 191*678418c6SKemeng Shi unsigned int index, 192*678418c6SKemeng Shi unsigned int alloc_hint, 193*678418c6SKemeng Shi bool wrap) 194*678418c6SKemeng Shi { 195*678418c6SKemeng Shi unsigned int i; 196*678418c6SKemeng Shi int nr = -1; 197*678418c6SKemeng Shi 198*678418c6SKemeng Shi for (i = 0; i < sb->map_nr; i++) { 199*678418c6SKemeng Shi nr = sbitmap_find_bit_in_word(&sb->map[index], 200*678418c6SKemeng Shi min_t(unsigned int, 201*678418c6SKemeng Shi __map_depth(sb, index), 202*678418c6SKemeng Shi depth), 203*678418c6SKemeng Shi alloc_hint, wrap); 204*678418c6SKemeng Shi 205*678418c6SKemeng Shi if (nr != -1) { 206*678418c6SKemeng Shi nr += index << sb->shift; 207*678418c6SKemeng Shi break; 208*678418c6SKemeng Shi } 209*678418c6SKemeng Shi 210*678418c6SKemeng Shi /* Jump to next index. */ 211*678418c6SKemeng Shi alloc_hint = 0; 212*678418c6SKemeng Shi if (++index >= sb->map_nr) 213*678418c6SKemeng Shi index = 0; 214*678418c6SKemeng Shi } 215*678418c6SKemeng Shi 216*678418c6SKemeng Shi return nr; 217*678418c6SKemeng Shi } 218*678418c6SKemeng Shi 219c548e62bSMing Lei static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint) 22088459642SOmar Sandoval { 221*678418c6SKemeng Shi unsigned int index; 22288459642SOmar Sandoval 22388459642SOmar Sandoval index = SB_NR_TO_INDEX(sb, alloc_hint); 22488459642SOmar Sandoval 22527fae429SJens Axboe /* 22627fae429SJens Axboe * Unless we're doing round robin tag allocation, just use the 22727fae429SJens Axboe * alloc_hint to find the right word index. No point in looping 22827fae429SJens Axboe * twice in find_next_zero_bit() for that case. 22927fae429SJens Axboe */ 230efe1f3a1SMing Lei if (sb->round_robin) 23127fae429SJens Axboe alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); 23227fae429SJens Axboe else 23327fae429SJens Axboe alloc_hint = 0; 23427fae429SJens Axboe 235*678418c6SKemeng Shi return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint, 236*678418c6SKemeng Shi !sb->round_robin); 23788459642SOmar Sandoval } 238c548e62bSMing Lei 239c548e62bSMing Lei int sbitmap_get(struct sbitmap *sb) 240c548e62bSMing Lei { 241c548e62bSMing Lei int nr; 242c548e62bSMing Lei unsigned int hint, depth; 243c548e62bSMing Lei 244c548e62bSMing Lei if (WARN_ON_ONCE(unlikely(!sb->alloc_hint))) 245c548e62bSMing Lei return -1; 246c548e62bSMing Lei 247c548e62bSMing Lei depth = READ_ONCE(sb->depth); 248c548e62bSMing Lei hint = update_alloc_hint_before_get(sb, depth); 249c548e62bSMing Lei nr = __sbitmap_get(sb, hint); 250c548e62bSMing Lei update_alloc_hint_after_get(sb, depth, hint, nr); 251c548e62bSMing Lei 252c548e62bSMing Lei return nr; 253c548e62bSMing Lei } 25488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get); 25588459642SOmar Sandoval 256c548e62bSMing Lei static int __sbitmap_get_shallow(struct sbitmap *sb, 257c548e62bSMing Lei unsigned int alloc_hint, 258c05e6673SOmar Sandoval unsigned long shallow_depth) 259c05e6673SOmar Sandoval { 260*678418c6SKemeng Shi unsigned int index; 261c05e6673SOmar Sandoval 262c05e6673SOmar Sandoval index = SB_NR_TO_INDEX(sb, alloc_hint); 263f1591a8bSKemeng Shi alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); 264c05e6673SOmar Sandoval 265*678418c6SKemeng Shi return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true); 266c05e6673SOmar Sandoval } 267c548e62bSMing Lei 268c548e62bSMing Lei int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth) 269c548e62bSMing Lei { 270c548e62bSMing Lei int nr; 271c548e62bSMing Lei unsigned int hint, depth; 272c548e62bSMing Lei 273c548e62bSMing Lei if (WARN_ON_ONCE(unlikely(!sb->alloc_hint))) 274c548e62bSMing Lei return -1; 275c548e62bSMing Lei 276c548e62bSMing Lei depth = READ_ONCE(sb->depth); 277c548e62bSMing Lei hint = update_alloc_hint_before_get(sb, depth); 278c548e62bSMing Lei nr = __sbitmap_get_shallow(sb, hint, shallow_depth); 279c548e62bSMing Lei update_alloc_hint_after_get(sb, depth, hint, nr); 280c548e62bSMing Lei 281c548e62bSMing Lei return nr; 282c548e62bSMing Lei } 283c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get_shallow); 284c05e6673SOmar Sandoval 28588459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb) 28688459642SOmar Sandoval { 28788459642SOmar Sandoval unsigned int i; 28888459642SOmar Sandoval 28988459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 290b2dbff1bSJens Axboe if (sb->map[i].word & ~sb->map[i].cleared) 29188459642SOmar Sandoval return true; 29288459642SOmar Sandoval } 29388459642SOmar Sandoval return false; 29488459642SOmar Sandoval } 29588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); 29688459642SOmar Sandoval 297ea86ea2cSJens Axboe static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set) 29888459642SOmar Sandoval { 29960658e0dSColin Ian King unsigned int i, weight = 0; 30088459642SOmar Sandoval 30188459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 30288459642SOmar Sandoval const struct sbitmap_word *word = &sb->map[i]; 3033301bc53SMing Lei unsigned int word_depth = __map_depth(sb, i); 30488459642SOmar Sandoval 305ea86ea2cSJens Axboe if (set) 3063301bc53SMing Lei weight += bitmap_weight(&word->word, word_depth); 307ea86ea2cSJens Axboe else 3083301bc53SMing Lei weight += bitmap_weight(&word->cleared, word_depth); 30988459642SOmar Sandoval } 31088459642SOmar Sandoval return weight; 31188459642SOmar Sandoval } 312ea86ea2cSJens Axboe 313ea86ea2cSJens Axboe static unsigned int sbitmap_cleared(const struct sbitmap *sb) 314ea86ea2cSJens Axboe { 315ea86ea2cSJens Axboe return __sbitmap_weight(sb, false); 316ea86ea2cSJens Axboe } 31788459642SOmar Sandoval 318cbb9950bSMing Lei unsigned int sbitmap_weight(const struct sbitmap *sb) 319cbb9950bSMing Lei { 320cbb9950bSMing Lei return __sbitmap_weight(sb, true) - sbitmap_cleared(sb); 321cbb9950bSMing Lei } 322cbb9950bSMing Lei EXPORT_SYMBOL_GPL(sbitmap_weight); 323cbb9950bSMing Lei 32424af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m) 32524af1ccfSOmar Sandoval { 32624af1ccfSOmar Sandoval seq_printf(m, "depth=%u\n", sb->depth); 327cbb9950bSMing Lei seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); 328ea86ea2cSJens Axboe seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb)); 32924af1ccfSOmar Sandoval seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); 33024af1ccfSOmar Sandoval seq_printf(m, "map_nr=%u\n", sb->map_nr); 33124af1ccfSOmar Sandoval } 33224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show); 33324af1ccfSOmar Sandoval 33424af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) 33524af1ccfSOmar Sandoval { 33624af1ccfSOmar Sandoval if ((offset & 0xf) == 0) { 33724af1ccfSOmar Sandoval if (offset != 0) 33824af1ccfSOmar Sandoval seq_putc(m, '\n'); 33924af1ccfSOmar Sandoval seq_printf(m, "%08x:", offset); 34024af1ccfSOmar Sandoval } 34124af1ccfSOmar Sandoval if ((offset & 0x1) == 0) 34224af1ccfSOmar Sandoval seq_putc(m, ' '); 34324af1ccfSOmar Sandoval seq_printf(m, "%02x", byte); 34424af1ccfSOmar Sandoval } 34524af1ccfSOmar Sandoval 34624af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) 34724af1ccfSOmar Sandoval { 34824af1ccfSOmar Sandoval u8 byte = 0; 34924af1ccfSOmar Sandoval unsigned int byte_bits = 0; 35024af1ccfSOmar Sandoval unsigned int offset = 0; 35124af1ccfSOmar Sandoval int i; 35224af1ccfSOmar Sandoval 35324af1ccfSOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 35424af1ccfSOmar Sandoval unsigned long word = READ_ONCE(sb->map[i].word); 3556bf0eb55SJohn Garry unsigned long cleared = READ_ONCE(sb->map[i].cleared); 3563301bc53SMing Lei unsigned int word_bits = __map_depth(sb, i); 35724af1ccfSOmar Sandoval 3586bf0eb55SJohn Garry word &= ~cleared; 3596bf0eb55SJohn Garry 36024af1ccfSOmar Sandoval while (word_bits > 0) { 36124af1ccfSOmar Sandoval unsigned int bits = min(8 - byte_bits, word_bits); 36224af1ccfSOmar Sandoval 36324af1ccfSOmar Sandoval byte |= (word & (BIT(bits) - 1)) << byte_bits; 36424af1ccfSOmar Sandoval byte_bits += bits; 36524af1ccfSOmar Sandoval if (byte_bits == 8) { 36624af1ccfSOmar Sandoval emit_byte(m, offset, byte); 36724af1ccfSOmar Sandoval byte = 0; 36824af1ccfSOmar Sandoval byte_bits = 0; 36924af1ccfSOmar Sandoval offset++; 37024af1ccfSOmar Sandoval } 37124af1ccfSOmar Sandoval word >>= bits; 37224af1ccfSOmar Sandoval word_bits -= bits; 37324af1ccfSOmar Sandoval } 37424af1ccfSOmar Sandoval } 37524af1ccfSOmar Sandoval if (byte_bits) { 37624af1ccfSOmar Sandoval emit_byte(m, offset, byte); 37724af1ccfSOmar Sandoval offset++; 37824af1ccfSOmar Sandoval } 37924af1ccfSOmar Sandoval if (offset) 38024af1ccfSOmar Sandoval seq_putc(m, '\n'); 38124af1ccfSOmar Sandoval } 38224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); 38324af1ccfSOmar Sandoval 384a3275539SOmar Sandoval static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq, 385a3275539SOmar Sandoval unsigned int depth) 38688459642SOmar Sandoval { 38788459642SOmar Sandoval unsigned int wake_batch; 388a3275539SOmar Sandoval unsigned int shallow_depth; 38988459642SOmar Sandoval 39088459642SOmar Sandoval /* 39188459642SOmar Sandoval * For each batch, we wake up one queue. We need to make sure that our 392a3275539SOmar Sandoval * batch size is small enough that the full depth of the bitmap, 393a3275539SOmar Sandoval * potentially limited by a shallow depth, is enough to wake up all of 394a3275539SOmar Sandoval * the queues. 395a3275539SOmar Sandoval * 396a3275539SOmar Sandoval * Each full word of the bitmap has bits_per_word bits, and there might 397a3275539SOmar Sandoval * be a partial word. There are depth / bits_per_word full words and 398a3275539SOmar Sandoval * depth % bits_per_word bits left over. In bitwise arithmetic: 399a3275539SOmar Sandoval * 400a3275539SOmar Sandoval * bits_per_word = 1 << shift 401a3275539SOmar Sandoval * depth / bits_per_word = depth >> shift 402a3275539SOmar Sandoval * depth % bits_per_word = depth & ((1 << shift) - 1) 403a3275539SOmar Sandoval * 404a3275539SOmar Sandoval * Each word can be limited to sbq->min_shallow_depth bits. 40588459642SOmar Sandoval */ 406a3275539SOmar Sandoval shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth); 407a3275539SOmar Sandoval depth = ((depth >> sbq->sb.shift) * shallow_depth + 408a3275539SOmar Sandoval min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth)); 409a3275539SOmar Sandoval wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1, 410a3275539SOmar Sandoval SBQ_WAKE_BATCH); 41188459642SOmar Sandoval 41288459642SOmar Sandoval return wake_batch; 41388459642SOmar Sandoval } 41488459642SOmar Sandoval 41588459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 416f4a644dbSOmar Sandoval int shift, bool round_robin, gfp_t flags, int node) 41788459642SOmar Sandoval { 41888459642SOmar Sandoval int ret; 41988459642SOmar Sandoval int i; 42088459642SOmar Sandoval 421efe1f3a1SMing Lei ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node, 422c548e62bSMing Lei round_robin, true); 42388459642SOmar Sandoval if (ret) 42488459642SOmar Sandoval return ret; 42588459642SOmar Sandoval 426a3275539SOmar Sandoval sbq->min_shallow_depth = UINT_MAX; 427a3275539SOmar Sandoval sbq->wake_batch = sbq_calc_wake_batch(sbq, depth); 42888459642SOmar Sandoval atomic_set(&sbq->wake_index, 0); 4295d2ee712SJens Axboe atomic_set(&sbq->ws_active, 0); 4304f8126bbSGabriel Krisman Bertazi atomic_set(&sbq->completion_cnt, 0); 4314f8126bbSGabriel Krisman Bertazi atomic_set(&sbq->wakeup_cnt, 0); 43288459642SOmar Sandoval 43348e28166SOmar Sandoval sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); 43488459642SOmar Sandoval if (!sbq->ws) { 43588459642SOmar Sandoval sbitmap_free(&sbq->sb); 43688459642SOmar Sandoval return -ENOMEM; 43788459642SOmar Sandoval } 43888459642SOmar Sandoval 4394f8126bbSGabriel Krisman Bertazi for (i = 0; i < SBQ_WAIT_QUEUES; i++) 44088459642SOmar Sandoval init_waitqueue_head(&sbq->ws[i].wait); 441f4a644dbSOmar Sandoval 44288459642SOmar Sandoval return 0; 44388459642SOmar Sandoval } 44488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); 44588459642SOmar Sandoval 446180dccb0SLaibin Qiu static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq, 447180dccb0SLaibin Qiu unsigned int depth) 448180dccb0SLaibin Qiu { 449180dccb0SLaibin Qiu unsigned int wake_batch; 450180dccb0SLaibin Qiu 451180dccb0SLaibin Qiu wake_batch = sbq_calc_wake_batch(sbq, depth); 4524f8126bbSGabriel Krisman Bertazi if (sbq->wake_batch != wake_batch) 4534f8126bbSGabriel Krisman Bertazi WRITE_ONCE(sbq->wake_batch, wake_batch); 454180dccb0SLaibin Qiu } 455180dccb0SLaibin Qiu 456180dccb0SLaibin Qiu void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq, 457180dccb0SLaibin Qiu unsigned int users) 458180dccb0SLaibin Qiu { 459180dccb0SLaibin Qiu unsigned int wake_batch; 46010825410SLaibin Qiu unsigned int min_batch; 46110825410SLaibin Qiu unsigned int depth = (sbq->sb.depth + users - 1) / users; 462180dccb0SLaibin Qiu 46310825410SLaibin Qiu min_batch = sbq->sb.depth >= (4 * SBQ_WAIT_QUEUES) ? 4 : 1; 46410825410SLaibin Qiu 46510825410SLaibin Qiu wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES, 46610825410SLaibin Qiu min_batch, SBQ_WAKE_BATCH); 4674f8126bbSGabriel Krisman Bertazi 4684f8126bbSGabriel Krisman Bertazi WRITE_ONCE(sbq->wake_batch, wake_batch); 469180dccb0SLaibin Qiu } 470180dccb0SLaibin Qiu EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch); 471180dccb0SLaibin Qiu 472a3275539SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) 473a3275539SOmar Sandoval { 474a3275539SOmar Sandoval sbitmap_queue_update_wake_batch(sbq, depth); 47588459642SOmar Sandoval sbitmap_resize(&sbq->sb, depth); 47688459642SOmar Sandoval } 47788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize); 47888459642SOmar Sandoval 479f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq) 48040aabb67SOmar Sandoval { 481c548e62bSMing Lei return sbitmap_get(&sbq->sb); 48240aabb67SOmar Sandoval } 48340aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get); 48440aabb67SOmar Sandoval 4859672b0d4SJens Axboe unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags, 4869672b0d4SJens Axboe unsigned int *offset) 4879672b0d4SJens Axboe { 4889672b0d4SJens Axboe struct sbitmap *sb = &sbq->sb; 4899672b0d4SJens Axboe unsigned int hint, depth; 4909672b0d4SJens Axboe unsigned long index, nr; 4919672b0d4SJens Axboe int i; 4929672b0d4SJens Axboe 4939672b0d4SJens Axboe if (unlikely(sb->round_robin)) 4949672b0d4SJens Axboe return 0; 4959672b0d4SJens Axboe 4969672b0d4SJens Axboe depth = READ_ONCE(sb->depth); 4979672b0d4SJens Axboe hint = update_alloc_hint_before_get(sb, depth); 4989672b0d4SJens Axboe 4999672b0d4SJens Axboe index = SB_NR_TO_INDEX(sb, hint); 5009672b0d4SJens Axboe 5019672b0d4SJens Axboe for (i = 0; i < sb->map_nr; i++) { 5029672b0d4SJens Axboe struct sbitmap_word *map = &sb->map[index]; 5039672b0d4SJens Axboe unsigned long get_mask; 5043301bc53SMing Lei unsigned int map_depth = __map_depth(sb, index); 5059672b0d4SJens Axboe 5069672b0d4SJens Axboe sbitmap_deferred_clear(map); 5073301bc53SMing Lei if (map->word == (1UL << (map_depth - 1)) - 1) 508fbb564a5Swuchi goto next; 5099672b0d4SJens Axboe 5103301bc53SMing Lei nr = find_first_zero_bit(&map->word, map_depth); 5113301bc53SMing Lei if (nr + nr_tags <= map_depth) { 5129672b0d4SJens Axboe atomic_long_t *ptr = (atomic_long_t *) &map->word; 513c35227d4SUros Bizjak unsigned long val; 5149672b0d4SJens Axboe 515ddbfc34fSLiu Song get_mask = ((1UL << nr_tags) - 1) << nr; 5169672b0d4SJens Axboe val = READ_ONCE(map->word); 517903e86f3SKemeng Shi while (!atomic_long_try_cmpxchg(ptr, &val, 518903e86f3SKemeng Shi get_mask | val)) 519903e86f3SKemeng Shi ; 520c35227d4SUros Bizjak get_mask = (get_mask & ~val) >> nr; 5219672b0d4SJens Axboe if (get_mask) { 5229672b0d4SJens Axboe *offset = nr + (index << sb->shift); 5239672b0d4SJens Axboe update_alloc_hint_after_get(sb, depth, hint, 524ddbfc34fSLiu Song *offset + nr_tags - 1); 5259672b0d4SJens Axboe return get_mask; 5269672b0d4SJens Axboe } 5279672b0d4SJens Axboe } 528fbb564a5Swuchi next: 5299672b0d4SJens Axboe /* Jump to next index. */ 5309672b0d4SJens Axboe if (++index >= sb->map_nr) 5319672b0d4SJens Axboe index = 0; 5329672b0d4SJens Axboe } 5339672b0d4SJens Axboe 5349672b0d4SJens Axboe return 0; 5359672b0d4SJens Axboe } 5369672b0d4SJens Axboe 5373f607293SJohn Garry int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 538c05e6673SOmar Sandoval unsigned int shallow_depth) 539c05e6673SOmar Sandoval { 54061445b56SOmar Sandoval WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth); 54161445b56SOmar Sandoval 542c548e62bSMing Lei return sbitmap_get_shallow(&sbq->sb, shallow_depth); 543c05e6673SOmar Sandoval } 5443f607293SJohn Garry EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow); 545c05e6673SOmar Sandoval 546a3275539SOmar Sandoval void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, 547a3275539SOmar Sandoval unsigned int min_shallow_depth) 548a3275539SOmar Sandoval { 549a3275539SOmar Sandoval sbq->min_shallow_depth = min_shallow_depth; 550a3275539SOmar Sandoval sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth); 551a3275539SOmar Sandoval } 552a3275539SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); 553a3275539SOmar Sandoval 55426edb30dSGabriel Krisman Bertazi static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) 55588459642SOmar Sandoval { 55688459642SOmar Sandoval int i, wake_index; 55788459642SOmar Sandoval 5585d2ee712SJens Axboe if (!atomic_read(&sbq->ws_active)) 55926edb30dSGabriel Krisman Bertazi return; 5605d2ee712SJens Axboe 56188459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 56288459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 56388459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 56488459642SOmar Sandoval 565976570b4SGabriel Krisman Bertazi /* 566976570b4SGabriel Krisman Bertazi * Advance the index before checking the current queue. 567976570b4SGabriel Krisman Bertazi * It improves fairness, by ensuring the queue doesn't 568976570b4SGabriel Krisman Bertazi * need to be fully emptied before trying to wake up 569976570b4SGabriel Krisman Bertazi * from the next one. 570976570b4SGabriel Krisman Bertazi */ 571976570b4SGabriel Krisman Bertazi wake_index = sbq_index_inc(wake_index); 572976570b4SGabriel Krisman Bertazi 57326edb30dSGabriel Krisman Bertazi /* 57426edb30dSGabriel Krisman Bertazi * It is sufficient to wake up at least one waiter to 57526edb30dSGabriel Krisman Bertazi * guarantee forward progress. 57626edb30dSGabriel Krisman Bertazi */ 57726edb30dSGabriel Krisman Bertazi if (waitqueue_active(&ws->wait) && 57826edb30dSGabriel Krisman Bertazi wake_up_nr(&ws->wait, nr)) 57926edb30dSGabriel Krisman Bertazi break; 58088459642SOmar Sandoval } 58188459642SOmar Sandoval 58288459642SOmar Sandoval if (wake_index != atomic_read(&sbq->wake_index)) 58388459642SOmar Sandoval atomic_set(&sbq->wake_index, wake_index); 584c854ab57SJens Axboe } 585c854ab57SJens Axboe 5864acb8341SKeith Busch void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr) 587c854ab57SJens Axboe { 5884f8126bbSGabriel Krisman Bertazi unsigned int wake_batch = READ_ONCE(sbq->wake_batch); 5894f8126bbSGabriel Krisman Bertazi unsigned int wakeups; 5904f8126bbSGabriel Krisman Bertazi 5914f8126bbSGabriel Krisman Bertazi if (!atomic_read(&sbq->ws_active)) 5924f8126bbSGabriel Krisman Bertazi return; 5934f8126bbSGabriel Krisman Bertazi 5944f8126bbSGabriel Krisman Bertazi atomic_add(nr, &sbq->completion_cnt); 5954f8126bbSGabriel Krisman Bertazi wakeups = atomic_read(&sbq->wakeup_cnt); 5964f8126bbSGabriel Krisman Bertazi 5974f8126bbSGabriel Krisman Bertazi do { 5984f8126bbSGabriel Krisman Bertazi if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch) 5994f8126bbSGabriel Krisman Bertazi return; 6004f8126bbSGabriel Krisman Bertazi } while (!atomic_try_cmpxchg(&sbq->wakeup_cnt, 6014f8126bbSGabriel Krisman Bertazi &wakeups, wakeups + wake_batch)); 6024f8126bbSGabriel Krisman Bertazi 60326edb30dSGabriel Krisman Bertazi __sbitmap_queue_wake_up(sbq, wake_batch); 60488459642SOmar Sandoval } 605bce1b56cSJens Axboe EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up); 60688459642SOmar Sandoval 6071aec5e4aSJens Axboe static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag) 6081aec5e4aSJens Axboe { 6091aec5e4aSJens Axboe if (likely(!sb->round_robin && tag < sb->depth)) 6109f8b93a7SJens Axboe data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag); 6111aec5e4aSJens Axboe } 6121aec5e4aSJens Axboe 6131aec5e4aSJens Axboe void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset, 6141aec5e4aSJens Axboe int *tags, int nr_tags) 6151aec5e4aSJens Axboe { 6161aec5e4aSJens Axboe struct sbitmap *sb = &sbq->sb; 6171aec5e4aSJens Axboe unsigned long *addr = NULL; 6181aec5e4aSJens Axboe unsigned long mask = 0; 6191aec5e4aSJens Axboe int i; 6201aec5e4aSJens Axboe 6211aec5e4aSJens Axboe smp_mb__before_atomic(); 6221aec5e4aSJens Axboe for (i = 0; i < nr_tags; i++) { 6231aec5e4aSJens Axboe const int tag = tags[i] - offset; 6241aec5e4aSJens Axboe unsigned long *this_addr; 6251aec5e4aSJens Axboe 6261aec5e4aSJens Axboe /* since we're clearing a batch, skip the deferred map */ 6271aec5e4aSJens Axboe this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word; 6281aec5e4aSJens Axboe if (!addr) { 6291aec5e4aSJens Axboe addr = this_addr; 6301aec5e4aSJens Axboe } else if (addr != this_addr) { 6311aec5e4aSJens Axboe atomic_long_andnot(mask, (atomic_long_t *) addr); 6321aec5e4aSJens Axboe mask = 0; 6331aec5e4aSJens Axboe addr = this_addr; 6341aec5e4aSJens Axboe } 6351aec5e4aSJens Axboe mask |= (1UL << SB_NR_TO_BIT(sb, tag)); 6361aec5e4aSJens Axboe } 6371aec5e4aSJens Axboe 6381aec5e4aSJens Axboe if (mask) 6391aec5e4aSJens Axboe atomic_long_andnot(mask, (atomic_long_t *) addr); 6401aec5e4aSJens Axboe 6411aec5e4aSJens Axboe smp_mb__after_atomic(); 6424acb8341SKeith Busch sbitmap_queue_wake_up(sbq, nr_tags); 6431aec5e4aSJens Axboe sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(), 6441aec5e4aSJens Axboe tags[nr_tags - 1] - offset); 6451aec5e4aSJens Axboe } 6461aec5e4aSJens Axboe 64740aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 648f4a644dbSOmar Sandoval unsigned int cpu) 64988459642SOmar Sandoval { 650e6d1fa58SMing Lei /* 651e6d1fa58SMing Lei * Once the clear bit is set, the bit may be allocated out. 652e6d1fa58SMing Lei * 6539dbbc3b9SZhen Lei * Orders READ/WRITE on the associated instance(such as request 654e6d1fa58SMing Lei * of blk_mq) by this bit for avoiding race with re-allocation, 655e6d1fa58SMing Lei * and its pair is the memory barrier implied in __sbitmap_get_word. 656e6d1fa58SMing Lei * 657e6d1fa58SMing Lei * One invariant is that the clear bit has to be zero when the bit 658e6d1fa58SMing Lei * is in use. 659e6d1fa58SMing Lei */ 660e6d1fa58SMing Lei smp_mb__before_atomic(); 661ea86ea2cSJens Axboe sbitmap_deferred_clear_bit(&sbq->sb, nr); 662ea86ea2cSJens Axboe 663e6fc4649SMing Lei /* 664e6fc4649SMing Lei * Pairs with the memory barrier in set_current_state() to ensure the 665e6fc4649SMing Lei * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker 666e6fc4649SMing Lei * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the 667e6fc4649SMing Lei * waiter. See the comment on waitqueue_active(). 668e6fc4649SMing Lei */ 669e6fc4649SMing Lei smp_mb__after_atomic(); 6704acb8341SKeith Busch sbitmap_queue_wake_up(sbq, 1); 6711aec5e4aSJens Axboe sbitmap_update_cpu_hint(&sbq->sb, cpu, nr); 67288459642SOmar Sandoval } 67388459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear); 67488459642SOmar Sandoval 67588459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) 67688459642SOmar Sandoval { 67788459642SOmar Sandoval int i, wake_index; 67888459642SOmar Sandoval 67988459642SOmar Sandoval /* 680f66227deSOmar Sandoval * Pairs with the memory barrier in set_current_state() like in 681e6fc4649SMing Lei * sbitmap_queue_wake_up(). 68288459642SOmar Sandoval */ 68388459642SOmar Sandoval smp_mb(); 68488459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 68588459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 68688459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 68788459642SOmar Sandoval 68888459642SOmar Sandoval if (waitqueue_active(&ws->wait)) 68988459642SOmar Sandoval wake_up(&ws->wait); 69088459642SOmar Sandoval 69188459642SOmar Sandoval wake_index = sbq_index_inc(wake_index); 69288459642SOmar Sandoval } 69388459642SOmar Sandoval } 69488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); 69524af1ccfSOmar Sandoval 69624af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) 69724af1ccfSOmar Sandoval { 69824af1ccfSOmar Sandoval bool first; 69924af1ccfSOmar Sandoval int i; 70024af1ccfSOmar Sandoval 70124af1ccfSOmar Sandoval sbitmap_show(&sbq->sb, m); 70224af1ccfSOmar Sandoval 70324af1ccfSOmar Sandoval seq_puts(m, "alloc_hint={"); 70424af1ccfSOmar Sandoval first = true; 70524af1ccfSOmar Sandoval for_each_possible_cpu(i) { 70624af1ccfSOmar Sandoval if (!first) 70724af1ccfSOmar Sandoval seq_puts(m, ", "); 70824af1ccfSOmar Sandoval first = false; 709c548e62bSMing Lei seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i)); 71024af1ccfSOmar Sandoval } 71124af1ccfSOmar Sandoval seq_puts(m, "}\n"); 71224af1ccfSOmar Sandoval 71324af1ccfSOmar Sandoval seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); 71424af1ccfSOmar Sandoval seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); 7155d2ee712SJens Axboe seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active)); 71624af1ccfSOmar Sandoval 71724af1ccfSOmar Sandoval seq_puts(m, "ws={\n"); 71824af1ccfSOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 71924af1ccfSOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[i]; 7204f8126bbSGabriel Krisman Bertazi seq_printf(m, "\t{.wait=%s},\n", 72124af1ccfSOmar Sandoval waitqueue_active(&ws->wait) ? "active" : "inactive"); 72224af1ccfSOmar Sandoval } 72324af1ccfSOmar Sandoval seq_puts(m, "}\n"); 72424af1ccfSOmar Sandoval 725efe1f3a1SMing Lei seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin); 726a3275539SOmar Sandoval seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); 72724af1ccfSOmar Sandoval } 72824af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show); 7295d2ee712SJens Axboe 7309f6b7ef6SJens Axboe void sbitmap_add_wait_queue(struct sbitmap_queue *sbq, 7319f6b7ef6SJens Axboe struct sbq_wait_state *ws, 7329f6b7ef6SJens Axboe struct sbq_wait *sbq_wait) 7339f6b7ef6SJens Axboe { 7349f6b7ef6SJens Axboe if (!sbq_wait->sbq) { 7359f6b7ef6SJens Axboe sbq_wait->sbq = sbq; 7369f6b7ef6SJens Axboe atomic_inc(&sbq->ws_active); 7379f6b7ef6SJens Axboe add_wait_queue(&ws->wait, &sbq_wait->wait); 7389f6b7ef6SJens Axboe } 739df034c93SDavid Jeffery } 7409f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue); 7419f6b7ef6SJens Axboe 7429f6b7ef6SJens Axboe void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait) 7439f6b7ef6SJens Axboe { 7449f6b7ef6SJens Axboe list_del_init(&sbq_wait->wait.entry); 7459f6b7ef6SJens Axboe if (sbq_wait->sbq) { 7469f6b7ef6SJens Axboe atomic_dec(&sbq_wait->sbq->ws_active); 7479f6b7ef6SJens Axboe sbq_wait->sbq = NULL; 7489f6b7ef6SJens Axboe } 7499f6b7ef6SJens Axboe } 7509f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue); 7519f6b7ef6SJens Axboe 7525d2ee712SJens Axboe void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq, 7535d2ee712SJens Axboe struct sbq_wait_state *ws, 7545d2ee712SJens Axboe struct sbq_wait *sbq_wait, int state) 7555d2ee712SJens Axboe { 7569f6b7ef6SJens Axboe if (!sbq_wait->sbq) { 7575d2ee712SJens Axboe atomic_inc(&sbq->ws_active); 7589f6b7ef6SJens Axboe sbq_wait->sbq = sbq; 7595d2ee712SJens Axboe } 7605d2ee712SJens Axboe prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state); 7615d2ee712SJens Axboe } 7625d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait); 7635d2ee712SJens Axboe 7645d2ee712SJens Axboe void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws, 7655d2ee712SJens Axboe struct sbq_wait *sbq_wait) 7665d2ee712SJens Axboe { 7675d2ee712SJens Axboe finish_wait(&ws->wait, &sbq_wait->wait); 7689f6b7ef6SJens Axboe if (sbq_wait->sbq) { 7695d2ee712SJens Axboe atomic_dec(&sbq->ws_active); 7709f6b7ef6SJens Axboe sbq_wait->sbq = NULL; 7715d2ee712SJens Axboe } 7725d2ee712SJens Axboe } 7735d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_finish_wait); 774