blob: e188d4fbebd86b1409c285f856d41a10461d02da [file] [log] [blame]
Thomas Gleixner0fc479b2019-05-29 16:57:42 -07001// SPDX-License-Identifier: GPL-2.0-only
Omar Sandoval88459642016-09-17 08:38:44 -06002/*
3 * Copyright (C) 2016 Facebook
4 * Copyright (C) 2013-2014 Jens Axboe
Omar Sandoval88459642016-09-17 08:38:44 -06005 */
6
Ingo Molnaraf8601a2017-02-03 09:57:00 +01007#include <linux/sched.h>
Omar Sandoval98d95412016-09-17 01:28:25 -07008#include <linux/random.h>
Omar Sandoval88459642016-09-17 08:38:44 -06009#include <linux/sbitmap.h>
Omar Sandoval24af1ccf2017-01-25 14:32:13 -080010#include <linux/seq_file.h>
Omar Sandoval88459642016-09-17 08:38:44 -060011
Ming Leic548e622021-01-22 10:33:08 +080012static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
Ming Leibf2c4282021-01-22 10:33:07 +080013{
Ming Leic548e622021-01-22 10:33:08 +080014 unsigned depth = sb->depth;
Ming Leibf2c4282021-01-22 10:33:07 +080015
Ming Leic548e622021-01-22 10:33:08 +080016 sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
17 if (!sb->alloc_hint)
Ming Leibf2c4282021-01-22 10:33:07 +080018 return -ENOMEM;
19
Ming Leic548e622021-01-22 10:33:08 +080020 if (depth && !sb->round_robin) {
Ming Leibf2c4282021-01-22 10:33:07 +080021 int i;
22
23 for_each_possible_cpu(i)
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -060024 *per_cpu_ptr(sb->alloc_hint, i) = get_random_u32_below(depth);
Ming Leibf2c4282021-01-22 10:33:07 +080025 }
Ming Leibf2c4282021-01-22 10:33:07 +080026 return 0;
27}
28
Ming Leic548e622021-01-22 10:33:08 +080029static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,
Ming Leibf2c4282021-01-22 10:33:07 +080030 unsigned int depth)
31{
32 unsigned hint;
33
Ming Leic548e622021-01-22 10:33:08 +080034 hint = this_cpu_read(*sb->alloc_hint);
Ming Leibf2c4282021-01-22 10:33:07 +080035 if (unlikely(hint >= depth)) {
Jason A. Donenfeld8032bf12022-10-09 20:44:02 -060036 hint = depth ? get_random_u32_below(depth) : 0;
Ming Leic548e622021-01-22 10:33:08 +080037 this_cpu_write(*sb->alloc_hint, hint);
Ming Leibf2c4282021-01-22 10:33:07 +080038 }
39
40 return hint;
41}
42
Ming Leic548e622021-01-22 10:33:08 +080043static inline void update_alloc_hint_after_get(struct sbitmap *sb,
Ming Leibf2c4282021-01-22 10:33:07 +080044 unsigned int depth,
45 unsigned int hint,
46 unsigned int nr)
47{
48 if (nr == -1) {
49 /* If the map is full, a hint won't do us much good. */
Ming Leic548e622021-01-22 10:33:08 +080050 this_cpu_write(*sb->alloc_hint, 0);
51 } else if (nr == hint || unlikely(sb->round_robin)) {
Ming Leibf2c4282021-01-22 10:33:07 +080052 /* Only update the hint if we used it. */
53 hint = nr + 1;
54 if (hint >= depth - 1)
55 hint = 0;
Ming Leic548e622021-01-22 10:33:08 +080056 this_cpu_write(*sb->alloc_hint, hint);
Ming Leibf2c4282021-01-22 10:33:07 +080057 }
58}
59
Yang Yang783d6782024-07-16 16:26:27 +080060/**
61 * sbitmap_spinlock() - Get the spinlock corresponding to sbitmap_work.
62 * @map: The memory address of the index sbitmap_work area.
63 * @map_nr: Number of words (cachelines) being used for the bitmap.
64 * @index: The index of the spinlock.
65 *
66 * this lock: serializes simultaneous updates of ->word and ->cleared.
67 * sbitmap_spinlock is for checking on ->cleared and updating on both
68 * ->cleared and ->word need to be done atomically.
69 * Prevent the following situations from causing block:
70 * Configuration for sbq:
71 * depth=64, wake_batch=6, shift=6, map_nr=1
72 *
73 * 1. There are 64 requests in progress:
74 * map->word = 0xFFFFFFFFFFFFFFFF
75 * 2. After all the 64 requests complete, and no more requests come:
76 * map->word = 0xFFFFFFFFFFFFFFFF, map->cleared = 0xFFFFFFFFFFFFFFFF
77 * 3. Now two tasks try to allocate requests:
78 * T1: T2:
79 * __blk_mq_get_tag .
80 * __sbitmap_queue_get .
81 * sbitmap_get .
82 * sbitmap_find_bit .
83 * sbitmap_find_bit_in_word .
84 * sbitmap_get_word -> nr=-1 __blk_mq_get_tag
85 * sbitmap_deferred_clear __sbitmap_queue_get
86 * //map->cleared=0xFFFFFFFFFFFFFFFF sbitmap_find_bit
87 * if (!READ_ONCE(map->cleared)) sbitmap_find_bit_in_word
88 * return false; __sbitmap_get_word -> nr=-1
89 * mask = xchg(&map->cleared, 0) sbitmap_deferred_clear
90 * atomic_long_andnot() // map->cleared=0
91 * if (!(map->cleared))
92 * return false;
93 *
94 * // map->cleared is cleared by T1
95 * // T2 fail to acquire the tag
96 *
97 * 4. T2 is the sole tag waiter. When T1 puts the tag, T2 cannot be woken
98 * up due to the wake_batch being set at 6. If no more requests come, T1
99 * will wait here indefinitely.
100 *
101 * Return: Returns the spinlock corresponding to index.
102 */
103static spinlock_t *sbitmap_spinlock(struct sbitmap_word *map,
104 unsigned int map_nr, unsigned int index)
105{
106 spinlock_t *base_lock = (spinlock_t *)(&map[map_nr - index]);
107
108 BUG_ON(((unsigned long)base_lock % __alignof__(spinlock_t)));
109 return &base_lock[index];
110}
111
Jens Axboeb2dbff12018-12-11 18:39:41 -0700112/*
113 * See if we have deferred clears that we can batch move
114 */
Yang Yang378c3572024-07-16 16:26:27 +0800115static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,
Yang Yang783d6782024-07-16 16:26:27 +0800116 unsigned int depth, unsigned int alloc_hint, bool wrap,
117 unsigned int map_nr, unsigned int index)
Jens Axboeb2dbff12018-12-11 18:39:41 -0700118{
Yang Yang378c3572024-07-16 16:26:27 +0800119 unsigned long mask, word_mask;
Yang Yang783d6782024-07-16 16:26:27 +0800120 spinlock_t *swap_lock = sbitmap_spinlock(map, map_nr, index);
Jens Axboeb2dbff12018-12-11 18:39:41 -0700121
Yang Yang783d6782024-07-16 16:26:27 +0800122 guard(spinlock_irqsave)(swap_lock);
Yang Yang378c3572024-07-16 16:26:27 +0800123
124 if (!map->cleared) {
125 if (depth == 0)
126 return false;
127
128 word_mask = (~0UL) >> (BITS_PER_LONG - depth);
129 /*
130 * The current behavior is to always retry after moving
131 * ->cleared to word, and we change it to retry in case
132 * of any free bits. To avoid an infinite loop, we need
133 * to take wrap & alloc_hint into account, otherwise a
134 * soft lockup may occur.
135 */
136 if (!wrap && alloc_hint)
137 word_mask &= ~((1UL << alloc_hint) - 1);
138
139 return (READ_ONCE(map->word) & word_mask) != word_mask;
140 }
Jens Axboeb2dbff12018-12-11 18:39:41 -0700141
142 /*
143 * First get a stable cleared mask, setting the old mask to 0.
144 */
Pavel Begunkovb78beea2020-11-22 15:35:45 +0000145 mask = xchg(&map->cleared, 0);
Jens Axboeb2dbff12018-12-11 18:39:41 -0700146
147 /*
148 * Now clear the masked bits in our free word
149 */
Pavel Begunkovc3250c82020-11-22 15:35:47 +0000150 atomic_long_andnot(mask, (atomic_long_t *)&map->word);
151 BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word));
Pavel Begunkov661d4f52020-11-22 15:35:46 +0000152 return true;
Jens Axboeb2dbff12018-12-11 18:39:41 -0700153}
154
Omar Sandoval88459642016-09-17 08:38:44 -0600155int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
Ming Leic548e622021-01-22 10:33:08 +0800156 gfp_t flags, int node, bool round_robin,
157 bool alloc_hint)
Omar Sandoval88459642016-09-17 08:38:44 -0600158{
159 unsigned int bits_per_word;
Yang Yang378c3572024-07-16 16:26:27 +0800160 int i;
Omar Sandoval88459642016-09-17 08:38:44 -0600161
Ming Lei2d13b1e2021-01-22 10:33:10 +0800162 if (shift < 0)
163 shift = sbitmap_calculate_shift(depth);
164
Omar Sandoval88459642016-09-17 08:38:44 -0600165 bits_per_word = 1U << shift;
166 if (bits_per_word > BITS_PER_LONG)
167 return -EINVAL;
168
169 sb->shift = shift;
170 sb->depth = depth;
171 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
Ming Leiefe1f3a2021-01-22 10:33:06 +0800172 sb->round_robin = round_robin;
Omar Sandoval88459642016-09-17 08:38:44 -0600173
174 if (depth == 0) {
175 sb->map = NULL;
176 return 0;
177 }
178
Ming Leic548e622021-01-22 10:33:08 +0800179 if (alloc_hint) {
180 if (init_alloc_hint(sb, flags))
181 return -ENOMEM;
182 } else {
183 sb->alloc_hint = NULL;
184 }
185
Yang Yang783d6782024-07-16 16:26:27 +0800186 /* Due to 72d04bdcf3f7 ("sbitmap: fix io hung due to race on sbitmap_word
187 * ::cleared") directly adding spinlock_t swap_1ock to struct sbitmap_word
188 * in sbitmap.h, KMI was damaged. In order to achieve functionality without
189 * damaging KMI, we can only apply for a block of memory with a size of
190 * map_nr * (sizeof (* sb ->map)+sizeof(spinlock_t)) to ensure that each
191 * struct sbitmap-word receives protection from spinlock.
192 * The actual memory distribution used is as follows:
193 * ----------------------
194 * struct sbitmap_word[0]
195 * ......................
196 * struct sbitmap_word[n]
197 * -----------------------
198 * spinlock_t swap_lock[0]
199 * .......................
200 * spinlock_t swap_lock[n]
201 * ----------------------
202 * sbitmap_word[0] corresponds to swap_lock[0], and sbitmap_word[n]
203 * corresponds to swap_lock[n], and so on
204 */
205 sb->map = kvzalloc_node(sb->map_nr * (sizeof(*sb->map) + sizeof(spinlock_t)), flags, node);
Ming Leic548e622021-01-22 10:33:08 +0800206 if (!sb->map) {
207 free_percpu(sb->alloc_hint);
Omar Sandoval88459642016-09-17 08:38:44 -0600208 return -ENOMEM;
Ming Leic548e622021-01-22 10:33:08 +0800209 }
Omar Sandoval88459642016-09-17 08:38:44 -0600210
Yang Yang783d6782024-07-16 16:26:27 +0800211 for (i = 0; i < sb->map_nr; i++) {
212 spinlock_t *swap_lock = sbitmap_spinlock(&sb->map[i], sb->map_nr, i);
213
214 spin_lock_init(swap_lock);
215 }
Yang Yang378c3572024-07-16 16:26:27 +0800216
Omar Sandoval88459642016-09-17 08:38:44 -0600217 return 0;
218}
219EXPORT_SYMBOL_GPL(sbitmap_init_node);
220
221void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
222{
223 unsigned int bits_per_word = 1U << sb->shift;
224 unsigned int i;
225
Jens Axboeb2dbff12018-12-11 18:39:41 -0700226 for (i = 0; i < sb->map_nr; i++)
Yang Yang783d6782024-07-16 16:26:27 +0800227 sbitmap_deferred_clear(&sb->map[i], 0, 0, 0, sb->map_nr, i);
Jens Axboeb2dbff12018-12-11 18:39:41 -0700228
Omar Sandoval88459642016-09-17 08:38:44 -0600229 sb->depth = depth;
230 sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
Omar Sandoval88459642016-09-17 08:38:44 -0600231}
232EXPORT_SYMBOL_GPL(sbitmap_resize);
233
Omar Sandovalc05e6672017-04-14 00:59:58 -0700234static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
235 unsigned int hint, bool wrap)
Omar Sandoval88459642016-09-17 08:38:44 -0600236{
Omar Sandoval88459642016-09-17 08:38:44 -0600237 int nr;
238
Pavel Begunkov0eff1f12020-11-22 15:35:48 +0000239 /* don't wrap if starting from 0 */
240 wrap = wrap && hint;
241
Omar Sandoval88459642016-09-17 08:38:44 -0600242 while (1) {
Omar Sandovalc05e6672017-04-14 00:59:58 -0700243 nr = find_next_zero_bit(word, depth, hint);
244 if (unlikely(nr >= depth)) {
Omar Sandoval88459642016-09-17 08:38:44 -0600245 /*
246 * We started with an offset, and we didn't reset the
247 * offset to 0 in a failure case, so start from 0 to
248 * exhaust the map.
249 */
Pavel Begunkov0eff1f12020-11-22 15:35:48 +0000250 if (hint && wrap) {
251 hint = 0;
Omar Sandoval88459642016-09-17 08:38:44 -0600252 continue;
253 }
254 return -1;
255 }
256
Omar Sandoval4ace53f2018-02-27 16:56:43 -0800257 if (!test_and_set_bit_lock(nr, word))
Omar Sandoval88459642016-09-17 08:38:44 -0600258 break;
259
260 hint = nr + 1;
Omar Sandovalc05e6672017-04-14 00:59:58 -0700261 if (hint >= depth - 1)
Omar Sandoval88459642016-09-17 08:38:44 -0600262 hint = 0;
263 }
264
265 return nr;
266}
267
Kemeng Shi08470a92023-01-17 04:50:57 +0800268static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
269 unsigned int depth,
270 unsigned int alloc_hint,
Yang Yang783d6782024-07-16 16:26:27 +0800271 bool wrap,
272 unsigned int map_nr,
273 unsigned int index)
Jens Axboeea86ea22018-11-30 13:18:06 -0700274{
275 int nr;
276
277 do {
Kemeng Shi08470a92023-01-17 04:50:57 +0800278 nr = __sbitmap_get_word(&map->word, depth,
279 alloc_hint, wrap);
Jens Axboeea86ea22018-11-30 13:18:06 -0700280 if (nr != -1)
281 break;
Yang Yang783d6782024-07-16 16:26:27 +0800282 if (!sbitmap_deferred_clear(map, depth, alloc_hint, wrap, map_nr, index))
Jens Axboeea86ea22018-11-30 13:18:06 -0700283 break;
284 } while (1);
285
286 return nr;
287}
288
Kemeng Shi678418c2023-01-17 04:50:58 +0800289static int sbitmap_find_bit(struct sbitmap *sb,
290 unsigned int depth,
291 unsigned int index,
292 unsigned int alloc_hint,
293 bool wrap)
294{
295 unsigned int i;
296 int nr = -1;
297
298 for (i = 0; i < sb->map_nr; i++) {
299 nr = sbitmap_find_bit_in_word(&sb->map[index],
300 min_t(unsigned int,
301 __map_depth(sb, index),
302 depth),
Yang Yang783d6782024-07-16 16:26:27 +0800303 alloc_hint, wrap,
304 sb->map_nr, index);
Kemeng Shi678418c2023-01-17 04:50:58 +0800305
306 if (nr != -1) {
307 nr += index << sb->shift;
308 break;
309 }
310
311 /* Jump to next index. */
312 alloc_hint = 0;
313 if (++index >= sb->map_nr)
314 index = 0;
315 }
316
317 return nr;
318}
319
Ming Leic548e622021-01-22 10:33:08 +0800320static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
Omar Sandoval88459642016-09-17 08:38:44 -0600321{
Kemeng Shi678418c2023-01-17 04:50:58 +0800322 unsigned int index;
Omar Sandoval88459642016-09-17 08:38:44 -0600323
324 index = SB_NR_TO_INDEX(sb, alloc_hint);
325
Jens Axboe27fae422018-11-29 12:35:16 -0700326 /*
327 * Unless we're doing round robin tag allocation, just use the
328 * alloc_hint to find the right word index. No point in looping
329 * twice in find_next_zero_bit() for that case.
330 */
Ming Leiefe1f3a2021-01-22 10:33:06 +0800331 if (sb->round_robin)
Jens Axboe27fae422018-11-29 12:35:16 -0700332 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
333 else
334 alloc_hint = 0;
335
Kemeng Shi678418c2023-01-17 04:50:58 +0800336 return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint,
337 !sb->round_robin);
Omar Sandoval88459642016-09-17 08:38:44 -0600338}
Ming Leic548e622021-01-22 10:33:08 +0800339
340int sbitmap_get(struct sbitmap *sb)
341{
342 int nr;
343 unsigned int hint, depth;
344
345 if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
346 return -1;
347
348 depth = READ_ONCE(sb->depth);
349 hint = update_alloc_hint_before_get(sb, depth);
350 nr = __sbitmap_get(sb, hint);
351 update_alloc_hint_after_get(sb, depth, hint, nr);
352
353 return nr;
354}
Omar Sandoval88459642016-09-17 08:38:44 -0600355EXPORT_SYMBOL_GPL(sbitmap_get);
356
Ming Leic548e622021-01-22 10:33:08 +0800357static int __sbitmap_get_shallow(struct sbitmap *sb,
358 unsigned int alloc_hint,
359 unsigned long shallow_depth)
Omar Sandovalc05e6672017-04-14 00:59:58 -0700360{
Kemeng Shi678418c2023-01-17 04:50:58 +0800361 unsigned int index;
Omar Sandovalc05e6672017-04-14 00:59:58 -0700362
363 index = SB_NR_TO_INDEX(sb, alloc_hint);
Kemeng Shif1591a82023-01-17 04:50:55 +0800364 alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
Omar Sandovalc05e6672017-04-14 00:59:58 -0700365
Kemeng Shi678418c2023-01-17 04:50:58 +0800366 return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true);
Omar Sandovalc05e6672017-04-14 00:59:58 -0700367}
Ming Leic548e622021-01-22 10:33:08 +0800368
369int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
370{
371 int nr;
372 unsigned int hint, depth;
373
374 if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
375 return -1;
376
377 depth = READ_ONCE(sb->depth);
378 hint = update_alloc_hint_before_get(sb, depth);
379 nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
380 update_alloc_hint_after_get(sb, depth, hint, nr);
381
382 return nr;
383}
Omar Sandovalc05e6672017-04-14 00:59:58 -0700384EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
385
Omar Sandoval88459642016-09-17 08:38:44 -0600386bool sbitmap_any_bit_set(const struct sbitmap *sb)
387{
388 unsigned int i;
389
390 for (i = 0; i < sb->map_nr; i++) {
Jens Axboeb2dbff12018-12-11 18:39:41 -0700391 if (sb->map[i].word & ~sb->map[i].cleared)
Omar Sandoval88459642016-09-17 08:38:44 -0600392 return true;
393 }
394 return false;
395}
396EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
397
Jens Axboeea86ea22018-11-30 13:18:06 -0700398static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
Omar Sandoval88459642016-09-17 08:38:44 -0600399{
Colin Ian King60658e02016-09-19 14:34:08 +0100400 unsigned int i, weight = 0;
Omar Sandoval88459642016-09-17 08:38:44 -0600401
402 for (i = 0; i < sb->map_nr; i++) {
403 const struct sbitmap_word *word = &sb->map[i];
Ming Lei3301bc52022-01-10 15:29:45 +0800404 unsigned int word_depth = __map_depth(sb, i);
Omar Sandoval88459642016-09-17 08:38:44 -0600405
Jens Axboeea86ea22018-11-30 13:18:06 -0700406 if (set)
Ming Lei3301bc52022-01-10 15:29:45 +0800407 weight += bitmap_weight(&word->word, word_depth);
Jens Axboeea86ea22018-11-30 13:18:06 -0700408 else
Ming Lei3301bc52022-01-10 15:29:45 +0800409 weight += bitmap_weight(&word->cleared, word_depth);
Omar Sandoval88459642016-09-17 08:38:44 -0600410 }
411 return weight;
412}
Jens Axboeea86ea22018-11-30 13:18:06 -0700413
Jens Axboeea86ea22018-11-30 13:18:06 -0700414static unsigned int sbitmap_cleared(const struct sbitmap *sb)
415{
416 return __sbitmap_weight(sb, false);
417}
Omar Sandoval88459642016-09-17 08:38:44 -0600418
Ming Leicbb99502021-01-22 10:33:09 +0800419unsigned int sbitmap_weight(const struct sbitmap *sb)
420{
421 return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);
422}
423EXPORT_SYMBOL_GPL(sbitmap_weight);
424
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800425void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
426{
427 seq_printf(m, "depth=%u\n", sb->depth);
Ming Leicbb99502021-01-22 10:33:09 +0800428 seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
Jens Axboeea86ea22018-11-30 13:18:06 -0700429 seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800430 seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
431 seq_printf(m, "map_nr=%u\n", sb->map_nr);
432}
433EXPORT_SYMBOL_GPL(sbitmap_show);
434
435static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
436{
437 if ((offset & 0xf) == 0) {
438 if (offset != 0)
439 seq_putc(m, '\n');
440 seq_printf(m, "%08x:", offset);
441 }
442 if ((offset & 0x1) == 0)
443 seq_putc(m, ' ');
444 seq_printf(m, "%02x", byte);
445}
446
447void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
448{
449 u8 byte = 0;
450 unsigned int byte_bits = 0;
451 unsigned int offset = 0;
452 int i;
453
454 for (i = 0; i < sb->map_nr; i++) {
455 unsigned long word = READ_ONCE(sb->map[i].word);
John Garry6bf0eb52020-07-01 16:06:25 +0800456 unsigned long cleared = READ_ONCE(sb->map[i].cleared);
Ming Lei3301bc52022-01-10 15:29:45 +0800457 unsigned int word_bits = __map_depth(sb, i);
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800458
John Garry6bf0eb52020-07-01 16:06:25 +0800459 word &= ~cleared;
460
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800461 while (word_bits > 0) {
462 unsigned int bits = min(8 - byte_bits, word_bits);
463
464 byte |= (word & (BIT(bits) - 1)) << byte_bits;
465 byte_bits += bits;
466 if (byte_bits == 8) {
467 emit_byte(m, offset, byte);
468 byte = 0;
469 byte_bits = 0;
470 offset++;
471 }
472 word >>= bits;
473 word_bits -= bits;
474 }
475 }
476 if (byte_bits) {
477 emit_byte(m, offset, byte);
478 offset++;
479 }
480 if (offset)
481 seq_putc(m, '\n');
482}
483EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
484
Omar Sandovala3275532018-05-09 17:16:31 -0700485static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
486 unsigned int depth)
Omar Sandoval88459642016-09-17 08:38:44 -0600487{
488 unsigned int wake_batch;
Omar Sandovala3275532018-05-09 17:16:31 -0700489 unsigned int shallow_depth;
Omar Sandoval88459642016-09-17 08:38:44 -0600490
491 /*
492 * For each batch, we wake up one queue. We need to make sure that our
Omar Sandovala3275532018-05-09 17:16:31 -0700493 * batch size is small enough that the full depth of the bitmap,
494 * potentially limited by a shallow depth, is enough to wake up all of
495 * the queues.
496 *
497 * Each full word of the bitmap has bits_per_word bits, and there might
498 * be a partial word. There are depth / bits_per_word full words and
499 * depth % bits_per_word bits left over. In bitwise arithmetic:
500 *
501 * bits_per_word = 1 << shift
502 * depth / bits_per_word = depth >> shift
503 * depth % bits_per_word = depth & ((1 << shift) - 1)
504 *
505 * Each word can be limited to sbq->min_shallow_depth bits.
Omar Sandoval88459642016-09-17 08:38:44 -0600506 */
Omar Sandovala3275532018-05-09 17:16:31 -0700507 shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
508 depth = ((depth >> sbq->sb.shift) * shallow_depth +
509 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
510 wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
511 SBQ_WAKE_BATCH);
Omar Sandoval88459642016-09-17 08:38:44 -0600512
513 return wake_batch;
514}
515
516int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700517 int shift, bool round_robin, gfp_t flags, int node)
Omar Sandoval88459642016-09-17 08:38:44 -0600518{
519 int ret;
520 int i;
521
Ming Leiefe1f3a2021-01-22 10:33:06 +0800522 ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
Ming Leic548e622021-01-22 10:33:08 +0800523 round_robin, true);
Omar Sandoval88459642016-09-17 08:38:44 -0600524 if (ret)
525 return ret;
526
Omar Sandovala3275532018-05-09 17:16:31 -0700527 sbq->min_shallow_depth = UINT_MAX;
528 sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
Omar Sandoval88459642016-09-17 08:38:44 -0600529 atomic_set(&sbq->wake_index, 0);
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700530 atomic_set(&sbq->ws_active, 0);
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400531 atomic_set(&sbq->completion_cnt, 0);
532 atomic_set(&sbq->wakeup_cnt, 0);
Omar Sandoval88459642016-09-17 08:38:44 -0600533
Omar Sandoval48e28162016-09-17 01:28:22 -0700534 sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
Omar Sandoval88459642016-09-17 08:38:44 -0600535 if (!sbq->ws) {
536 sbitmap_free(&sbq->sb);
537 return -ENOMEM;
538 }
539
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400540 for (i = 0; i < SBQ_WAIT_QUEUES; i++)
Omar Sandoval88459642016-09-17 08:38:44 -0600541 init_waitqueue_head(&sbq->ws[i].wait);
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700542
Omar Sandoval88459642016-09-17 08:38:44 -0600543 return 0;
544}
545EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
546
Laibin Qiu180dccb2022-01-13 10:55:36 +0800547static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
548 unsigned int depth)
549{
550 unsigned int wake_batch;
551
552 wake_batch = sbq_calc_wake_batch(sbq, depth);
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400553 if (sbq->wake_batch != wake_batch)
554 WRITE_ONCE(sbq->wake_batch, wake_batch);
Laibin Qiu180dccb2022-01-13 10:55:36 +0800555}
556
557void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,
558 unsigned int users)
559{
560 unsigned int wake_batch;
Laibin Qiu108254102022-01-27 18:00:47 +0800561 unsigned int depth = (sbq->sb.depth + users - 1) / users;
Laibin Qiu180dccb2022-01-13 10:55:36 +0800562
Laibin Qiu108254102022-01-27 18:00:47 +0800563 wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,
Kemeng Shib5fcf782023-01-17 04:50:59 +0800564 1, SBQ_WAKE_BATCH);
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400565
566 WRITE_ONCE(sbq->wake_batch, wake_batch);
Laibin Qiu180dccb2022-01-13 10:55:36 +0800567}
568EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);
569
Omar Sandovala3275532018-05-09 17:16:31 -0700570void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
571{
572 sbitmap_queue_update_wake_batch(sbq, depth);
Omar Sandoval88459642016-09-17 08:38:44 -0600573 sbitmap_resize(&sbq->sb, depth);
574}
575EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
576
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700577int __sbitmap_queue_get(struct sbitmap_queue *sbq)
Omar Sandoval40aabb62016-09-17 01:28:23 -0700578{
Ming Leic548e622021-01-22 10:33:08 +0800579 return sbitmap_get(&sbq->sb);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700580}
581EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
582
Jens Axboe9672b0d2021-10-09 13:02:23 -0600583unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
584 unsigned int *offset)
585{
586 struct sbitmap *sb = &sbq->sb;
587 unsigned int hint, depth;
588 unsigned long index, nr;
589 int i;
590
591 if (unlikely(sb->round_robin))
592 return 0;
593
594 depth = READ_ONCE(sb->depth);
595 hint = update_alloc_hint_before_get(sb, depth);
596
597 index = SB_NR_TO_INDEX(sb, hint);
598
599 for (i = 0; i < sb->map_nr; i++) {
600 struct sbitmap_word *map = &sb->map[index];
601 unsigned long get_mask;
Ming Lei3301bc52022-01-10 15:29:45 +0800602 unsigned int map_depth = __map_depth(sb, index);
linke lia3e4c8f2024-04-26 18:34:44 +0800603 unsigned long val;
Jens Axboe9672b0d2021-10-09 13:02:23 -0600604
Yang Yang783d6782024-07-16 16:26:27 +0800605 sbitmap_deferred_clear(map, 0, 0, 0, sb->map_nr, index);
linke lia3e4c8f2024-04-26 18:34:44 +0800606 val = READ_ONCE(map->word);
607 if (val == (1UL << (map_depth - 1)) - 1)
wuchifbb564a2022-06-05 22:58:35 +0800608 goto next;
Jens Axboe9672b0d2021-10-09 13:02:23 -0600609
linke lia3e4c8f2024-04-26 18:34:44 +0800610 nr = find_first_zero_bit(&val, map_depth);
Ming Lei3301bc52022-01-10 15:29:45 +0800611 if (nr + nr_tags <= map_depth) {
Jens Axboe9672b0d2021-10-09 13:02:23 -0600612 atomic_long_t *ptr = (atomic_long_t *) &map->word;
Jens Axboe9672b0d2021-10-09 13:02:23 -0600613
Liu Songddbfc342022-08-26 11:14:13 +0800614 get_mask = ((1UL << nr_tags) - 1) << nr;
Kemeng Shi903e86f2023-01-17 04:50:56 +0800615 while (!atomic_long_try_cmpxchg(ptr, &val,
616 get_mask | val))
617 ;
Uros Bizjakc35227d2022-09-08 17:12:00 +0200618 get_mask = (get_mask & ~val) >> nr;
Jens Axboe9672b0d2021-10-09 13:02:23 -0600619 if (get_mask) {
620 *offset = nr + (index << sb->shift);
621 update_alloc_hint_after_get(sb, depth, hint,
Liu Songddbfc342022-08-26 11:14:13 +0800622 *offset + nr_tags - 1);
Jens Axboe9672b0d2021-10-09 13:02:23 -0600623 return get_mask;
624 }
625 }
wuchifbb564a2022-06-05 22:58:35 +0800626next:
Jens Axboe9672b0d2021-10-09 13:02:23 -0600627 /* Jump to next index. */
628 if (++index >= sb->map_nr)
629 index = 0;
630 }
631
632 return 0;
633}
634
John Garry3f607292022-02-08 20:07:04 +0800635int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
636 unsigned int shallow_depth)
Omar Sandovalc05e6672017-04-14 00:59:58 -0700637{
Omar Sandoval61445b562018-05-09 17:29:24 -0700638 WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
639
Ming Leic548e622021-01-22 10:33:08 +0800640 return sbitmap_get_shallow(&sbq->sb, shallow_depth);
Omar Sandovalc05e6672017-04-14 00:59:58 -0700641}
John Garry3f607292022-02-08 20:07:04 +0800642EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow);
Omar Sandovalc05e6672017-04-14 00:59:58 -0700643
Omar Sandovala3275532018-05-09 17:16:31 -0700644void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
645 unsigned int min_shallow_depth)
646{
647 sbq->min_shallow_depth = min_shallow_depth;
648 sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
649}
650EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
651
Gabriel Krisman Bertazi26edb302022-11-15 17:45:53 -0500652static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
Omar Sandoval88459642016-09-17 08:38:44 -0600653{
David Jeffery10639732023-07-21 17:57:15 +0800654 int i, wake_index, woken;
Omar Sandoval88459642016-09-17 08:38:44 -0600655
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700656 if (!atomic_read(&sbq->ws_active))
Gabriel Krisman Bertazi26edb302022-11-15 17:45:53 -0500657 return;
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700658
Omar Sandoval88459642016-09-17 08:38:44 -0600659 wake_index = atomic_read(&sbq->wake_index);
660 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
661 struct sbq_wait_state *ws = &sbq->ws[wake_index];
662
Gabriel Krisman Bertazi976570b2022-11-15 17:45:51 -0500663 /*
664 * Advance the index before checking the current queue.
665 * It improves fairness, by ensuring the queue doesn't
666 * need to be fully emptied before trying to wake up
667 * from the next one.
668 */
Omar Sandoval88459642016-09-17 08:38:44 -0600669 wake_index = sbq_index_inc(wake_index);
Gabriel Krisman Bertazi976570b2022-11-15 17:45:51 -0500670
David Jeffery10639732023-07-21 17:57:15 +0800671 if (waitqueue_active(&ws->wait)) {
672 woken = wake_up_nr(&ws->wait, nr);
673 if (woken == nr)
674 break;
675 nr -= woken;
676 }
Omar Sandoval88459642016-09-17 08:38:44 -0600677 }
678
Gabriel Krisman Bertazi26edb302022-11-15 17:45:53 -0500679 if (wake_index != atomic_read(&sbq->wake_index))
680 atomic_set(&sbq->wake_index, wake_index);
Jens Axboec854ab572018-05-14 12:17:31 -0600681}
682
Keith Busch4acb8342022-09-09 11:40:22 -0700683void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
Jens Axboec854ab572018-05-14 12:17:31 -0600684{
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400685 unsigned int wake_batch = READ_ONCE(sbq->wake_batch);
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400686 unsigned int wakeups;
687
688 if (!atomic_read(&sbq->ws_active))
689 return;
690
691 atomic_add(nr, &sbq->completion_cnt);
692 wakeups = atomic_read(&sbq->wakeup_cnt);
693
694 do {
695 if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
696 return;
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400697 } while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
698 &wakeups, wakeups + wake_batch));
699
Gabriel Krisman Bertazi26edb302022-11-15 17:45:53 -0500700 __sbitmap_queue_wake_up(sbq, wake_batch);
Omar Sandoval88459642016-09-17 08:38:44 -0600701}
Jens Axboebce1b562022-09-04 06:39:25 -0600702EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
Omar Sandoval88459642016-09-17 08:38:44 -0600703
Jens Axboe1aec5e42021-10-08 05:44:23 -0600704static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
705{
706 if (likely(!sb->round_robin && tag < sb->depth))
Jens Axboe9f8b93a2021-10-25 10:45:01 -0600707 data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
Jens Axboe1aec5e42021-10-08 05:44:23 -0600708}
709
710void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
711 int *tags, int nr_tags)
712{
713 struct sbitmap *sb = &sbq->sb;
714 unsigned long *addr = NULL;
715 unsigned long mask = 0;
716 int i;
717
718 smp_mb__before_atomic();
719 for (i = 0; i < nr_tags; i++) {
720 const int tag = tags[i] - offset;
721 unsigned long *this_addr;
722
723 /* since we're clearing a batch, skip the deferred map */
724 this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word;
725 if (!addr) {
726 addr = this_addr;
727 } else if (addr != this_addr) {
728 atomic_long_andnot(mask, (atomic_long_t *) addr);
729 mask = 0;
730 addr = this_addr;
731 }
732 mask |= (1UL << SB_NR_TO_BIT(sb, tag));
733 }
734
735 if (mask)
736 atomic_long_andnot(mask, (atomic_long_t *) addr);
737
738 smp_mb__after_atomic();
Keith Busch4acb8342022-09-09 11:40:22 -0700739 sbitmap_queue_wake_up(sbq, nr_tags);
Jens Axboe1aec5e42021-10-08 05:44:23 -0600740 sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
741 tags[nr_tags - 1] - offset);
742}
743
Omar Sandoval40aabb62016-09-17 01:28:23 -0700744void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
Omar Sandovalf4a644d2016-09-17 01:28:24 -0700745 unsigned int cpu)
Omar Sandoval88459642016-09-17 08:38:44 -0600746{
Ming Leie6d1fa52019-03-22 09:13:51 +0800747 /*
748 * Once the clear bit is set, the bit may be allocated out.
749 *
Zhen Lei9dbbc3b2021-07-07 18:07:31 -0700750 * Orders READ/WRITE on the associated instance(such as request
Ming Leie6d1fa52019-03-22 09:13:51 +0800751 * of blk_mq) by this bit for avoiding race with re-allocation,
752 * and its pair is the memory barrier implied in __sbitmap_get_word.
753 *
754 * One invariant is that the clear bit has to be zero when the bit
755 * is in use.
756 */
757 smp_mb__before_atomic();
Jens Axboeea86ea22018-11-30 13:18:06 -0700758 sbitmap_deferred_clear_bit(&sbq->sb, nr);
759
Ming Leie6fc4642018-05-24 11:00:39 -0600760 /*
761 * Pairs with the memory barrier in set_current_state() to ensure the
762 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
763 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
764 * waiter. See the comment on waitqueue_active().
765 */
766 smp_mb__after_atomic();
Keith Busch4acb8342022-09-09 11:40:22 -0700767 sbitmap_queue_wake_up(sbq, 1);
Jens Axboe1aec5e42021-10-08 05:44:23 -0600768 sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
Omar Sandoval88459642016-09-17 08:38:44 -0600769}
770EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
771
772void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
773{
774 int i, wake_index;
775
776 /*
Omar Sandovalf66227d2017-01-18 11:55:21 -0800777 * Pairs with the memory barrier in set_current_state() like in
Ming Leie6fc4642018-05-24 11:00:39 -0600778 * sbitmap_queue_wake_up().
Omar Sandoval88459642016-09-17 08:38:44 -0600779 */
780 smp_mb();
781 wake_index = atomic_read(&sbq->wake_index);
782 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
783 struct sbq_wait_state *ws = &sbq->ws[wake_index];
784
785 if (waitqueue_active(&ws->wait))
786 wake_up(&ws->wait);
787
788 wake_index = sbq_index_inc(wake_index);
789 }
790}
791EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800792
793void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
794{
795 bool first;
796 int i;
797
798 sbitmap_show(&sbq->sb, m);
799
800 seq_puts(m, "alloc_hint={");
801 first = true;
802 for_each_possible_cpu(i) {
803 if (!first)
804 seq_puts(m, ", ");
805 first = false;
Ming Leic548e622021-01-22 10:33:08 +0800806 seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i));
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800807 }
808 seq_puts(m, "}\n");
809
810 seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
811 seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700812 seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800813
814 seq_puts(m, "ws={\n");
815 for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
816 struct sbq_wait_state *ws = &sbq->ws[i];
Gabriel Krisman Bertazi4f8126b2022-11-05 19:10:55 -0400817 seq_printf(m, "\t{.wait=%s},\n",
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800818 waitqueue_active(&ws->wait) ? "active" : "inactive");
819 }
820 seq_puts(m, "}\n");
821
Ming Leiefe1f3a2021-01-22 10:33:06 +0800822 seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
Omar Sandovala3275532018-05-09 17:16:31 -0700823 seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
Omar Sandoval24af1ccf2017-01-25 14:32:13 -0800824}
825EXPORT_SYMBOL_GPL(sbitmap_queue_show);
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700826
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700827void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
828 struct sbq_wait_state *ws,
829 struct sbq_wait *sbq_wait)
830{
831 if (!sbq_wait->sbq) {
832 sbq_wait->sbq = sbq;
833 atomic_inc(&sbq->ws_active);
David Jefferydf034c92019-12-17 11:00:24 -0500834 add_wait_queue(&ws->wait, &sbq_wait->wait);
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700835 }
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700836}
837EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
838
839void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
840{
841 list_del_init(&sbq_wait->wait.entry);
842 if (sbq_wait->sbq) {
843 atomic_dec(&sbq_wait->sbq->ws_active);
844 sbq_wait->sbq = NULL;
845 }
846}
847EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
848
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700849void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
850 struct sbq_wait_state *ws,
851 struct sbq_wait *sbq_wait, int state)
852{
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700853 if (!sbq_wait->sbq) {
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700854 atomic_inc(&sbq->ws_active);
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700855 sbq_wait->sbq = sbq;
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700856 }
857 prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
858}
859EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
860
861void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
862 struct sbq_wait *sbq_wait)
863{
864 finish_wait(&ws->wait, &sbq_wait->wait);
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700865 if (sbq_wait->sbq) {
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700866 atomic_dec(&sbq->ws_active);
Jens Axboe9f6b7ef2018-12-20 08:49:00 -0700867 sbq_wait->sbq = NULL;
Jens Axboe5d2ee7122018-11-29 17:36:41 -0700868 }
869}
870EXPORT_SYMBOL_GPL(sbitmap_finish_wait);