xref: /linux/lib/sbitmap.c (revision 2988dfed8a5dc752921a5790b81c06e781af51ce)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2016 Facebook
4  * Copyright (C) 2013-2014 Jens Axboe
5  */
6 
7 #include <linux/sched.h>
8 #include <linux/random.h>
9 #include <linux/sbitmap.h>
10 #include <linux/seq_file.h>
11 
init_alloc_hint(struct sbitmap * sb,gfp_t flags)12 static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
13 {
14 	unsigned depth = sb->depth;
15 
16 	sb->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
17 	if (!sb->alloc_hint)
18 		return -ENOMEM;
19 
20 	if (depth && !sb->round_robin) {
21 		int i;
22 
23 		for_each_possible_cpu(i)
24 			*per_cpu_ptr(sb->alloc_hint, i) = get_random_u32_below(depth);
25 	}
26 	return 0;
27 }
28 
update_alloc_hint_before_get(struct sbitmap * sb,unsigned int depth)29 static inline unsigned update_alloc_hint_before_get(struct sbitmap *sb,
30 						    unsigned int depth)
31 {
32 	unsigned hint;
33 
34 	hint = this_cpu_read(*sb->alloc_hint);
35 	if (unlikely(hint >= depth)) {
36 		hint = depth ? get_random_u32_below(depth) : 0;
37 		this_cpu_write(*sb->alloc_hint, hint);
38 	}
39 
40 	return hint;
41 }
42 
update_alloc_hint_after_get(struct sbitmap * sb,unsigned int depth,unsigned int hint,unsigned int nr)43 static inline void update_alloc_hint_after_get(struct sbitmap *sb,
44 					       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. */
50 		this_cpu_write(*sb->alloc_hint, 0);
51 	} else if (nr == hint || unlikely(sb->round_robin)) {
52 		/* Only update the hint if we used it. */
53 		hint = nr + 1;
54 		if (hint >= depth - 1)
55 			hint = 0;
56 		this_cpu_write(*sb->alloc_hint, hint);
57 	}
58 }
59 
60 /*
61  * See if we have deferred clears that we can batch move
62  */
sbitmap_deferred_clear(struct sbitmap_word * map,unsigned int depth,unsigned int alloc_hint,bool wrap)63 static inline bool sbitmap_deferred_clear(struct sbitmap_word *map,
64 		unsigned int depth, unsigned int alloc_hint, bool wrap)
65 {
66 	unsigned long mask, word_mask;
67 
68 	guard(raw_spinlock_irqsave)(&map->swap_lock);
69 
70 	if (!map->cleared) {
71 		if (depth == 0)
72 			return false;
73 
74 		word_mask = (~0UL) >> (BITS_PER_LONG - depth);
75 		/*
76 		 * The current behavior is to always retry after moving
77 		 * ->cleared to word, and we change it to retry in case
78 		 * of any free bits. To avoid an infinite loop, we need
79 		 * to take wrap & alloc_hint into account, otherwise a
80 		 * soft lockup may occur.
81 		 */
82 		if (!wrap && alloc_hint)
83 			word_mask &= ~((1UL << alloc_hint) - 1);
84 
85 		return (READ_ONCE(map->word) & word_mask) != word_mask;
86 	}
87 
88 	/*
89 	 * First get a stable cleared mask, setting the old mask to 0.
90 	 */
91 	mask = xchg(&map->cleared, 0);
92 
93 	/*
94 	 * Now clear the masked bits in our free word
95 	 */
96 	atomic_long_andnot(mask, (atomic_long_t *)&map->word);
97 	BUILD_BUG_ON(sizeof(atomic_long_t) != sizeof(map->word));
98 	return true;
99 }
100 
sbitmap_init_node(struct sbitmap * sb,unsigned int depth,int shift,gfp_t flags,int node,bool round_robin,bool alloc_hint)101 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
102 		      gfp_t flags, int node, bool round_robin,
103 		      bool alloc_hint)
104 {
105 	unsigned int bits_per_word;
106 	int i;
107 
108 	if (shift < 0)
109 		shift = sbitmap_calculate_shift(depth);
110 
111 	bits_per_word = 1U << shift;
112 	if (bits_per_word > BITS_PER_LONG)
113 		return -EINVAL;
114 
115 	sb->shift = shift;
116 	sb->depth = depth;
117 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
118 	sb->round_robin = round_robin;
119 
120 	if (depth == 0) {
121 		sb->map = NULL;
122 		return 0;
123 	}
124 
125 	if (alloc_hint) {
126 		if (init_alloc_hint(sb, flags))
127 			return -ENOMEM;
128 	} else {
129 		sb->alloc_hint = NULL;
130 	}
131 
132 	sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
133 	if (!sb->map) {
134 		free_percpu(sb->alloc_hint);
135 		return -ENOMEM;
136 	}
137 
138 	for (i = 0; i < sb->map_nr; i++)
139 		raw_spin_lock_init(&sb->map[i].swap_lock);
140 
141 	return 0;
142 }
143 EXPORT_SYMBOL_GPL(sbitmap_init_node);
144 
sbitmap_resize(struct sbitmap * sb,unsigned int depth)145 void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
146 {
147 	unsigned int bits_per_word = 1U << sb->shift;
148 	unsigned int i;
149 
150 	for (i = 0; i < sb->map_nr; i++)
151 		sbitmap_deferred_clear(&sb->map[i], 0, 0, 0);
152 
153 	sb->depth = depth;
154 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
155 }
156 EXPORT_SYMBOL_GPL(sbitmap_resize);
157 
__sbitmap_get_word(unsigned long * word,unsigned long depth,unsigned int hint,bool wrap)158 static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
159 			      unsigned int hint, bool wrap)
160 {
161 	int nr;
162 
163 	/* don't wrap if starting from 0 */
164 	wrap = wrap && hint;
165 
166 	while (1) {
167 		nr = find_next_zero_bit(word, depth, hint);
168 		if (unlikely(nr >= depth)) {
169 			/*
170 			 * We started with an offset, and we didn't reset the
171 			 * offset to 0 in a failure case, so start from 0 to
172 			 * exhaust the map.
173 			 */
174 			if (hint && wrap) {
175 				hint = 0;
176 				continue;
177 			}
178 			return -1;
179 		}
180 
181 		if (!test_and_set_bit_lock(nr, word))
182 			break;
183 
184 		hint = nr + 1;
185 		if (hint >= depth - 1)
186 			hint = 0;
187 	}
188 
189 	return nr;
190 }
191 
sbitmap_find_bit_in_word(struct sbitmap_word * map,unsigned int depth,unsigned int alloc_hint,bool wrap)192 static int sbitmap_find_bit_in_word(struct sbitmap_word *map,
193 				    unsigned int depth,
194 				    unsigned int alloc_hint,
195 				    bool wrap)
196 {
197 	int nr;
198 
199 	do {
200 		nr = __sbitmap_get_word(&map->word, depth,
201 					alloc_hint, wrap);
202 		if (nr != -1)
203 			break;
204 		if (!sbitmap_deferred_clear(map, depth, alloc_hint, wrap))
205 			break;
206 	} while (1);
207 
208 	return nr;
209 }
210 
__map_depth_with_shallow(const struct sbitmap * sb,int index,unsigned int shallow_depth)211 static unsigned int __map_depth_with_shallow(const struct sbitmap *sb,
212 					     int index,
213 					     unsigned int shallow_depth)
214 {
215 	u64 shallow_word_depth;
216 	unsigned int word_depth, reminder;
217 
218 	word_depth = __map_depth(sb, index);
219 	if (shallow_depth >= sb->depth)
220 		return word_depth;
221 
222 	shallow_word_depth = word_depth * shallow_depth;
223 	reminder = do_div(shallow_word_depth, sb->depth);
224 
225 	if (reminder >= (index + 1) * word_depth)
226 		shallow_word_depth++;
227 
228 	return (unsigned int)shallow_word_depth;
229 }
230 
sbitmap_find_bit(struct sbitmap * sb,unsigned int shallow_depth,unsigned int index,unsigned int alloc_hint,bool wrap)231 static int sbitmap_find_bit(struct sbitmap *sb,
232 			    unsigned int shallow_depth,
233 			    unsigned int index,
234 			    unsigned int alloc_hint,
235 			    bool wrap)
236 {
237 	unsigned int i;
238 	int nr = -1;
239 
240 	for (i = 0; i < sb->map_nr; i++) {
241 		unsigned int depth = __map_depth_with_shallow(sb, index,
242 							      shallow_depth);
243 
244 		if (depth)
245 			nr = sbitmap_find_bit_in_word(&sb->map[index], depth,
246 						      alloc_hint, wrap);
247 		if (nr != -1) {
248 			nr += index << sb->shift;
249 			break;
250 		}
251 
252 		/* Jump to next index. */
253 		alloc_hint = 0;
254 		if (++index >= sb->map_nr)
255 			index = 0;
256 	}
257 
258 	return nr;
259 }
260 
__sbitmap_get(struct sbitmap * sb,unsigned int alloc_hint)261 static int __sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint)
262 {
263 	unsigned int index;
264 
265 	index = SB_NR_TO_INDEX(sb, alloc_hint);
266 
267 	/*
268 	 * Unless we're doing round robin tag allocation, just use the
269 	 * alloc_hint to find the right word index. No point in looping
270 	 * twice in find_next_zero_bit() for that case.
271 	 */
272 	if (sb->round_robin)
273 		alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
274 	else
275 		alloc_hint = 0;
276 
277 	return sbitmap_find_bit(sb, UINT_MAX, index, alloc_hint,
278 				!sb->round_robin);
279 }
280 
sbitmap_get(struct sbitmap * sb)281 int sbitmap_get(struct sbitmap *sb)
282 {
283 	int nr;
284 	unsigned int hint, depth;
285 
286 	if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
287 		return -1;
288 
289 	depth = READ_ONCE(sb->depth);
290 	hint = update_alloc_hint_before_get(sb, depth);
291 	nr = __sbitmap_get(sb, hint);
292 	update_alloc_hint_after_get(sb, depth, hint, nr);
293 
294 	return nr;
295 }
296 EXPORT_SYMBOL_GPL(sbitmap_get);
297 
__sbitmap_get_shallow(struct sbitmap * sb,unsigned int alloc_hint,unsigned long shallow_depth)298 static int __sbitmap_get_shallow(struct sbitmap *sb,
299 				 unsigned int alloc_hint,
300 				 unsigned long shallow_depth)
301 {
302 	unsigned int index;
303 
304 	index = SB_NR_TO_INDEX(sb, alloc_hint);
305 	alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
306 
307 	return sbitmap_find_bit(sb, shallow_depth, index, alloc_hint, true);
308 }
309 
310 /**
311  * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
312  * limiting the depth used from each word.
313  * @sb: Bitmap to allocate from.
314  * @shallow_depth: The maximum number of bits to allocate from the bitmap.
315  *
316  * This rather specific operation allows for having multiple users with
317  * different allocation limits. E.g., there can be a high-priority class that
318  * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
319  * with a @shallow_depth of (sb->depth >> 1). Then, the low-priority
320  * class can only allocate half of the total bits in the bitmap, preventing it
321  * from starving out the high-priority class.
322  *
323  * Return: Non-negative allocated bit number if successful, -1 otherwise.
324  */
sbitmap_get_shallow(struct sbitmap * sb,unsigned long shallow_depth)325 static int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
326 {
327 	int nr;
328 	unsigned int hint, depth;
329 
330 	if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
331 		return -1;
332 
333 	depth = READ_ONCE(sb->depth);
334 	hint = update_alloc_hint_before_get(sb, depth);
335 	nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
336 	update_alloc_hint_after_get(sb, depth, hint, nr);
337 
338 	return nr;
339 }
340 
sbitmap_any_bit_set(const struct sbitmap * sb)341 bool sbitmap_any_bit_set(const struct sbitmap *sb)
342 {
343 	unsigned int i;
344 
345 	for (i = 0; i < sb->map_nr; i++) {
346 		if (sb->map[i].word & ~sb->map[i].cleared)
347 			return true;
348 	}
349 	return false;
350 }
351 EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
352 
__sbitmap_weight(const struct sbitmap * sb,bool set)353 static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
354 {
355 	unsigned int i, weight = 0;
356 
357 	for (i = 0; i < sb->map_nr; i++) {
358 		const struct sbitmap_word *word = &sb->map[i];
359 		unsigned int word_depth = __map_depth(sb, i);
360 
361 		if (set)
362 			weight += bitmap_weight(&word->word, word_depth);
363 		else
364 			weight += bitmap_weight(&word->cleared, word_depth);
365 	}
366 	return weight;
367 }
368 
sbitmap_cleared(const struct sbitmap * sb)369 static unsigned int sbitmap_cleared(const struct sbitmap *sb)
370 {
371 	return __sbitmap_weight(sb, false);
372 }
373 
sbitmap_weight(const struct sbitmap * sb)374 unsigned int sbitmap_weight(const struct sbitmap *sb)
375 {
376 	return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);
377 }
378 EXPORT_SYMBOL_GPL(sbitmap_weight);
379 
sbitmap_show(struct sbitmap * sb,struct seq_file * m)380 void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
381 {
382 	seq_printf(m, "depth=%u\n", sb->depth);
383 	seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
384 	seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
385 	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
386 	seq_printf(m, "map_nr=%u\n", sb->map_nr);
387 }
388 EXPORT_SYMBOL_GPL(sbitmap_show);
389 
emit_byte(struct seq_file * m,unsigned int offset,u8 byte)390 static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
391 {
392 	if ((offset & 0xf) == 0) {
393 		if (offset != 0)
394 			seq_putc(m, '\n');
395 		seq_printf(m, "%08x:", offset);
396 	}
397 	if ((offset & 0x1) == 0)
398 		seq_putc(m, ' ');
399 	seq_printf(m, "%02x", byte);
400 }
401 
sbitmap_bitmap_show(struct sbitmap * sb,struct seq_file * m)402 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
403 {
404 	u8 byte = 0;
405 	unsigned int byte_bits = 0;
406 	unsigned int offset = 0;
407 	int i;
408 
409 	for (i = 0; i < sb->map_nr; i++) {
410 		unsigned long word = READ_ONCE(sb->map[i].word);
411 		unsigned long cleared = READ_ONCE(sb->map[i].cleared);
412 		unsigned int word_bits = __map_depth(sb, i);
413 
414 		word &= ~cleared;
415 
416 		while (word_bits > 0) {
417 			unsigned int bits = min(8 - byte_bits, word_bits);
418 
419 			byte |= (word & (BIT(bits) - 1)) << byte_bits;
420 			byte_bits += bits;
421 			if (byte_bits == 8) {
422 				emit_byte(m, offset, byte);
423 				byte = 0;
424 				byte_bits = 0;
425 				offset++;
426 			}
427 			word >>= bits;
428 			word_bits -= bits;
429 		}
430 	}
431 	if (byte_bits) {
432 		emit_byte(m, offset, byte);
433 		offset++;
434 	}
435 	if (offset)
436 		seq_putc(m, '\n');
437 }
438 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
439 
sbq_calc_wake_batch(struct sbitmap_queue * sbq,unsigned int depth)440 static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
441 					unsigned int depth)
442 {
443 	return clamp_t(unsigned int,
444 		       min(depth, sbq->min_shallow_depth) / SBQ_WAIT_QUEUES,
445 		       1, SBQ_WAKE_BATCH);
446 }
447 
sbitmap_queue_init_node(struct sbitmap_queue * sbq,unsigned int depth,int shift,bool round_robin,gfp_t flags,int node)448 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
449 			    int shift, bool round_robin, gfp_t flags, int node)
450 {
451 	int ret;
452 	int i;
453 
454 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
455 				round_robin, true);
456 	if (ret)
457 		return ret;
458 
459 	sbq->min_shallow_depth = UINT_MAX;
460 	sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
461 	atomic_set(&sbq->wake_index, 0);
462 	atomic_set(&sbq->ws_active, 0);
463 	atomic_set(&sbq->completion_cnt, 0);
464 	atomic_set(&sbq->wakeup_cnt, 0);
465 
466 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
467 	if (!sbq->ws) {
468 		sbitmap_free(&sbq->sb);
469 		return -ENOMEM;
470 	}
471 
472 	for (i = 0; i < SBQ_WAIT_QUEUES; i++)
473 		init_waitqueue_head(&sbq->ws[i].wait);
474 
475 	return 0;
476 }
477 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
478 
sbitmap_queue_update_wake_batch(struct sbitmap_queue * sbq,unsigned int depth)479 static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
480 					    unsigned int depth)
481 {
482 	unsigned int wake_batch;
483 
484 	wake_batch = sbq_calc_wake_batch(sbq, depth);
485 	if (sbq->wake_batch != wake_batch)
486 		WRITE_ONCE(sbq->wake_batch, wake_batch);
487 }
488 
sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue * sbq,unsigned int users)489 void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,
490 					    unsigned int users)
491 {
492 	unsigned int wake_batch;
493 	unsigned int depth = (sbq->sb.depth + users - 1) / users;
494 
495 	wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,
496 			1, SBQ_WAKE_BATCH);
497 
498 	WRITE_ONCE(sbq->wake_batch, wake_batch);
499 }
500 EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);
501 
sbitmap_queue_resize(struct sbitmap_queue * sbq,unsigned int depth)502 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
503 {
504 	sbitmap_queue_update_wake_batch(sbq, depth);
505 	sbitmap_resize(&sbq->sb, depth);
506 }
507 EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
508 
__sbitmap_queue_get(struct sbitmap_queue * sbq)509 int __sbitmap_queue_get(struct sbitmap_queue *sbq)
510 {
511 	return sbitmap_get(&sbq->sb);
512 }
513 EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
514 
__sbitmap_queue_get_batch(struct sbitmap_queue * sbq,int nr_tags,unsigned int * offset)515 unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
516 					unsigned int *offset)
517 {
518 	struct sbitmap *sb = &sbq->sb;
519 	unsigned int hint, depth;
520 	unsigned long index, nr;
521 	int i;
522 
523 	if (unlikely(sb->round_robin))
524 		return 0;
525 
526 	depth = READ_ONCE(sb->depth);
527 	hint = update_alloc_hint_before_get(sb, depth);
528 
529 	index = SB_NR_TO_INDEX(sb, hint);
530 
531 	for (i = 0; i < sb->map_nr; i++) {
532 		struct sbitmap_word *map = &sb->map[index];
533 		unsigned long get_mask;
534 		unsigned int map_depth = __map_depth(sb, index);
535 		unsigned long val;
536 
537 		sbitmap_deferred_clear(map, 0, 0, 0);
538 		val = READ_ONCE(map->word);
539 		if (val == (1UL << (map_depth - 1)) - 1)
540 			goto next;
541 
542 		nr = find_first_zero_bit(&val, map_depth);
543 		if (nr + nr_tags <= map_depth) {
544 			atomic_long_t *ptr = (atomic_long_t *) &map->word;
545 
546 			get_mask = ((1UL << nr_tags) - 1) << nr;
547 			while (!atomic_long_try_cmpxchg(ptr, &val,
548 							  get_mask | val))
549 				;
550 			get_mask = (get_mask & ~val) >> nr;
551 			if (get_mask) {
552 				*offset = nr + (index << sb->shift);
553 				update_alloc_hint_after_get(sb, depth, hint,
554 							*offset + nr_tags - 1);
555 				return get_mask;
556 			}
557 		}
558 next:
559 		/* Jump to next index. */
560 		if (++index >= sb->map_nr)
561 			index = 0;
562 	}
563 
564 	return 0;
565 }
566 
sbitmap_queue_get_shallow(struct sbitmap_queue * sbq,unsigned int shallow_depth)567 int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
568 			      unsigned int shallow_depth)
569 {
570 	WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
571 
572 	return sbitmap_get_shallow(&sbq->sb, shallow_depth);
573 }
574 EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow);
575 
sbitmap_queue_min_shallow_depth(struct sbitmap_queue * sbq,unsigned int min_shallow_depth)576 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
577 				     unsigned int min_shallow_depth)
578 {
579 	sbq->min_shallow_depth = min_shallow_depth;
580 	sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
581 }
582 EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
583 
__sbitmap_queue_wake_up(struct sbitmap_queue * sbq,int nr)584 static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
585 {
586 	int i, wake_index, woken;
587 
588 	if (!atomic_read(&sbq->ws_active))
589 		return;
590 
591 	wake_index = atomic_read(&sbq->wake_index);
592 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
593 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
594 
595 		/*
596 		 * Advance the index before checking the current queue.
597 		 * It improves fairness, by ensuring the queue doesn't
598 		 * need to be fully emptied before trying to wake up
599 		 * from the next one.
600 		 */
601 		wake_index = sbq_index_inc(wake_index);
602 
603 		if (waitqueue_active(&ws->wait)) {
604 			woken = wake_up_nr(&ws->wait, nr);
605 			if (woken == nr)
606 				break;
607 			nr -= woken;
608 		}
609 	}
610 
611 	if (wake_index != atomic_read(&sbq->wake_index))
612 		atomic_set(&sbq->wake_index, wake_index);
613 }
614 
sbitmap_queue_wake_up(struct sbitmap_queue * sbq,int nr)615 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
616 {
617 	unsigned int wake_batch = READ_ONCE(sbq->wake_batch);
618 	unsigned int wakeups;
619 
620 	if (!atomic_read(&sbq->ws_active))
621 		return;
622 
623 	atomic_add(nr, &sbq->completion_cnt);
624 	wakeups = atomic_read(&sbq->wakeup_cnt);
625 
626 	do {
627 		if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
628 			return;
629 	} while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
630 				     &wakeups, wakeups + wake_batch));
631 
632 	__sbitmap_queue_wake_up(sbq, wake_batch);
633 }
634 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
635 
sbitmap_update_cpu_hint(struct sbitmap * sb,int cpu,int tag)636 static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
637 {
638 	if (likely(!sb->round_robin && tag < sb->depth))
639 		data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
640 }
641 
sbitmap_queue_clear_batch(struct sbitmap_queue * sbq,int offset,int * tags,int nr_tags)642 void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
643 				int *tags, int nr_tags)
644 {
645 	struct sbitmap *sb = &sbq->sb;
646 	unsigned long *addr = NULL;
647 	unsigned long mask = 0;
648 	int i;
649 
650 	smp_mb__before_atomic();
651 	for (i = 0; i < nr_tags; i++) {
652 		const int tag = tags[i] - offset;
653 		unsigned long *this_addr;
654 
655 		/* since we're clearing a batch, skip the deferred map */
656 		this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word;
657 		if (!addr) {
658 			addr = this_addr;
659 		} else if (addr != this_addr) {
660 			atomic_long_andnot(mask, (atomic_long_t *) addr);
661 			mask = 0;
662 			addr = this_addr;
663 		}
664 		mask |= (1UL << SB_NR_TO_BIT(sb, tag));
665 	}
666 
667 	if (mask)
668 		atomic_long_andnot(mask, (atomic_long_t *) addr);
669 
670 	smp_mb__after_atomic();
671 	sbitmap_queue_wake_up(sbq, nr_tags);
672 	sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
673 					tags[nr_tags - 1] - offset);
674 }
675 
sbitmap_queue_clear(struct sbitmap_queue * sbq,unsigned int nr,unsigned int cpu)676 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
677 			 unsigned int cpu)
678 {
679 	/*
680 	 * Once the clear bit is set, the bit may be allocated out.
681 	 *
682 	 * Orders READ/WRITE on the associated instance(such as request
683 	 * of blk_mq) by this bit for avoiding race with re-allocation,
684 	 * and its pair is the memory barrier implied in __sbitmap_get_word.
685 	 *
686 	 * One invariant is that the clear bit has to be zero when the bit
687 	 * is in use.
688 	 */
689 	smp_mb__before_atomic();
690 	sbitmap_deferred_clear_bit(&sbq->sb, nr);
691 
692 	/*
693 	 * Pairs with the memory barrier in set_current_state() to ensure the
694 	 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
695 	 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
696 	 * waiter. See the comment on waitqueue_active().
697 	 */
698 	smp_mb__after_atomic();
699 	sbitmap_queue_wake_up(sbq, 1);
700 	sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
701 }
702 EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
703 
sbitmap_queue_wake_all(struct sbitmap_queue * sbq)704 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
705 {
706 	int i, wake_index;
707 
708 	/*
709 	 * Pairs with the memory barrier in set_current_state() like in
710 	 * sbitmap_queue_wake_up().
711 	 */
712 	smp_mb();
713 	wake_index = atomic_read(&sbq->wake_index);
714 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
715 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
716 
717 		if (waitqueue_active(&ws->wait))
718 			wake_up(&ws->wait);
719 
720 		wake_index = sbq_index_inc(wake_index);
721 	}
722 }
723 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
724 
sbitmap_queue_show(struct sbitmap_queue * sbq,struct seq_file * m)725 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
726 {
727 	bool first;
728 	int i;
729 
730 	sbitmap_show(&sbq->sb, m);
731 
732 	seq_puts(m, "alloc_hint={");
733 	first = true;
734 	for_each_possible_cpu(i) {
735 		if (!first)
736 			seq_puts(m, ", ");
737 		first = false;
738 		seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i));
739 	}
740 	seq_puts(m, "}\n");
741 
742 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
743 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
744 	seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
745 
746 	seq_puts(m, "ws={\n");
747 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
748 		struct sbq_wait_state *ws = &sbq->ws[i];
749 		seq_printf(m, "\t{.wait=%s},\n",
750 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
751 	}
752 	seq_puts(m, "}\n");
753 
754 	seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
755 	seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
756 }
757 EXPORT_SYMBOL_GPL(sbitmap_queue_show);
758 
sbitmap_add_wait_queue(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait)759 void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
760 			    struct sbq_wait_state *ws,
761 			    struct sbq_wait *sbq_wait)
762 {
763 	if (!sbq_wait->sbq) {
764 		sbq_wait->sbq = sbq;
765 		atomic_inc(&sbq->ws_active);
766 		add_wait_queue(&ws->wait, &sbq_wait->wait);
767 	}
768 }
769 EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
770 
sbitmap_del_wait_queue(struct sbq_wait * sbq_wait)771 void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
772 {
773 	list_del_init(&sbq_wait->wait.entry);
774 	if (sbq_wait->sbq) {
775 		atomic_dec(&sbq_wait->sbq->ws_active);
776 		sbq_wait->sbq = NULL;
777 	}
778 }
779 EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
780 
sbitmap_prepare_to_wait(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait,int state)781 void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
782 			     struct sbq_wait_state *ws,
783 			     struct sbq_wait *sbq_wait, int state)
784 {
785 	if (!sbq_wait->sbq) {
786 		atomic_inc(&sbq->ws_active);
787 		sbq_wait->sbq = sbq;
788 	}
789 	prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
790 }
791 EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
792 
sbitmap_finish_wait(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait)793 void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
794 			 struct sbq_wait *sbq_wait)
795 {
796 	finish_wait(&ws->wait, &sbq_wait->wait);
797 	if (sbq_wait->sbq) {
798 		atomic_dec(&sbq->ws_active);
799 		sbq_wait->sbq = NULL;
800 	}
801 }
802 EXPORT_SYMBOL_GPL(sbitmap_finish_wait);
803