• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
sbitmap_get_shallow(struct sbitmap * sb,unsigned long shallow_depth)310 int sbitmap_get_shallow(struct sbitmap *sb, unsigned long shallow_depth)
311 {
312 	int nr;
313 	unsigned int hint, depth;
314 
315 	if (WARN_ON_ONCE(unlikely(!sb->alloc_hint)))
316 		return -1;
317 
318 	depth = READ_ONCE(sb->depth);
319 	hint = update_alloc_hint_before_get(sb, depth);
320 	nr = __sbitmap_get_shallow(sb, hint, shallow_depth);
321 	update_alloc_hint_after_get(sb, depth, hint, nr);
322 
323 	return nr;
324 }
325 EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
326 
sbitmap_any_bit_set(const struct sbitmap * sb)327 bool sbitmap_any_bit_set(const struct sbitmap *sb)
328 {
329 	unsigned int i;
330 
331 	for (i = 0; i < sb->map_nr; i++) {
332 		if (sb->map[i].word & ~sb->map[i].cleared)
333 			return true;
334 	}
335 	return false;
336 }
337 EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
338 
__sbitmap_weight(const struct sbitmap * sb,bool set)339 static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
340 {
341 	unsigned int i, weight = 0;
342 
343 	for (i = 0; i < sb->map_nr; i++) {
344 		const struct sbitmap_word *word = &sb->map[i];
345 		unsigned int word_depth = __map_depth(sb, i);
346 
347 		if (set)
348 			weight += bitmap_weight(&word->word, word_depth);
349 		else
350 			weight += bitmap_weight(&word->cleared, word_depth);
351 	}
352 	return weight;
353 }
354 
sbitmap_cleared(const struct sbitmap * sb)355 static unsigned int sbitmap_cleared(const struct sbitmap *sb)
356 {
357 	return __sbitmap_weight(sb, false);
358 }
359 
sbitmap_weight(const struct sbitmap * sb)360 unsigned int sbitmap_weight(const struct sbitmap *sb)
361 {
362 	return __sbitmap_weight(sb, true) - sbitmap_cleared(sb);
363 }
364 EXPORT_SYMBOL_GPL(sbitmap_weight);
365 
sbitmap_show(struct sbitmap * sb,struct seq_file * m)366 void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
367 {
368 	seq_printf(m, "depth=%u\n", sb->depth);
369 	seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
370 	seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
371 	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
372 	seq_printf(m, "map_nr=%u\n", sb->map_nr);
373 }
374 EXPORT_SYMBOL_GPL(sbitmap_show);
375 
emit_byte(struct seq_file * m,unsigned int offset,u8 byte)376 static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
377 {
378 	if ((offset & 0xf) == 0) {
379 		if (offset != 0)
380 			seq_putc(m, '\n');
381 		seq_printf(m, "%08x:", offset);
382 	}
383 	if ((offset & 0x1) == 0)
384 		seq_putc(m, ' ');
385 	seq_printf(m, "%02x", byte);
386 }
387 
sbitmap_bitmap_show(struct sbitmap * sb,struct seq_file * m)388 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
389 {
390 	u8 byte = 0;
391 	unsigned int byte_bits = 0;
392 	unsigned int offset = 0;
393 	int i;
394 
395 	for (i = 0; i < sb->map_nr; i++) {
396 		unsigned long word = READ_ONCE(sb->map[i].word);
397 		unsigned long cleared = READ_ONCE(sb->map[i].cleared);
398 		unsigned int word_bits = __map_depth(sb, i);
399 
400 		word &= ~cleared;
401 
402 		while (word_bits > 0) {
403 			unsigned int bits = min(8 - byte_bits, word_bits);
404 
405 			byte |= (word & (BIT(bits) - 1)) << byte_bits;
406 			byte_bits += bits;
407 			if (byte_bits == 8) {
408 				emit_byte(m, offset, byte);
409 				byte = 0;
410 				byte_bits = 0;
411 				offset++;
412 			}
413 			word >>= bits;
414 			word_bits -= bits;
415 		}
416 	}
417 	if (byte_bits) {
418 		emit_byte(m, offset, byte);
419 		offset++;
420 	}
421 	if (offset)
422 		seq_putc(m, '\n');
423 }
424 EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
425 
sbq_calc_wake_batch(struct sbitmap_queue * sbq,unsigned int depth)426 static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
427 					unsigned int depth)
428 {
429 	return clamp_t(unsigned int,
430 		       min(depth, sbq->min_shallow_depth) / SBQ_WAIT_QUEUES,
431 		       1, SBQ_WAKE_BATCH);
432 }
433 
sbitmap_queue_init_node(struct sbitmap_queue * sbq,unsigned int depth,int shift,bool round_robin,gfp_t flags,int node)434 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
435 			    int shift, bool round_robin, gfp_t flags, int node)
436 {
437 	int ret;
438 	int i;
439 
440 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node,
441 				round_robin, true);
442 	if (ret)
443 		return ret;
444 
445 	sbq->min_shallow_depth = UINT_MAX;
446 	sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
447 	atomic_set(&sbq->wake_index, 0);
448 	atomic_set(&sbq->ws_active, 0);
449 	atomic_set(&sbq->completion_cnt, 0);
450 	atomic_set(&sbq->wakeup_cnt, 0);
451 
452 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
453 	if (!sbq->ws) {
454 		sbitmap_free(&sbq->sb);
455 		return -ENOMEM;
456 	}
457 
458 	for (i = 0; i < SBQ_WAIT_QUEUES; i++)
459 		init_waitqueue_head(&sbq->ws[i].wait);
460 
461 	return 0;
462 }
463 EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
464 
sbitmap_queue_update_wake_batch(struct sbitmap_queue * sbq,unsigned int depth)465 static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
466 					    unsigned int depth)
467 {
468 	unsigned int wake_batch;
469 
470 	wake_batch = sbq_calc_wake_batch(sbq, depth);
471 	if (sbq->wake_batch != wake_batch)
472 		WRITE_ONCE(sbq->wake_batch, wake_batch);
473 }
474 
sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue * sbq,unsigned int users)475 void sbitmap_queue_recalculate_wake_batch(struct sbitmap_queue *sbq,
476 					    unsigned int users)
477 {
478 	unsigned int wake_batch;
479 	unsigned int depth = (sbq->sb.depth + users - 1) / users;
480 
481 	wake_batch = clamp_val(depth / SBQ_WAIT_QUEUES,
482 			1, SBQ_WAKE_BATCH);
483 
484 	WRITE_ONCE(sbq->wake_batch, wake_batch);
485 }
486 EXPORT_SYMBOL_GPL(sbitmap_queue_recalculate_wake_batch);
487 
sbitmap_queue_resize(struct sbitmap_queue * sbq,unsigned int depth)488 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
489 {
490 	sbitmap_queue_update_wake_batch(sbq, depth);
491 	sbitmap_resize(&sbq->sb, depth);
492 }
493 EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
494 
__sbitmap_queue_get(struct sbitmap_queue * sbq)495 int __sbitmap_queue_get(struct sbitmap_queue *sbq)
496 {
497 	return sbitmap_get(&sbq->sb);
498 }
499 EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
500 
__sbitmap_queue_get_batch(struct sbitmap_queue * sbq,int nr_tags,unsigned int * offset)501 unsigned long __sbitmap_queue_get_batch(struct sbitmap_queue *sbq, int nr_tags,
502 					unsigned int *offset)
503 {
504 	struct sbitmap *sb = &sbq->sb;
505 	unsigned int hint, depth;
506 	unsigned long index, nr;
507 	int i;
508 
509 	if (unlikely(sb->round_robin))
510 		return 0;
511 
512 	depth = READ_ONCE(sb->depth);
513 	hint = update_alloc_hint_before_get(sb, depth);
514 
515 	index = SB_NR_TO_INDEX(sb, hint);
516 
517 	for (i = 0; i < sb->map_nr; i++) {
518 		struct sbitmap_word *map = &sb->map[index];
519 		unsigned long get_mask;
520 		unsigned int map_depth = __map_depth(sb, index);
521 		unsigned long val;
522 
523 		sbitmap_deferred_clear(map, 0, 0, 0);
524 		val = READ_ONCE(map->word);
525 		if (val == (1UL << (map_depth - 1)) - 1)
526 			goto next;
527 
528 		nr = find_first_zero_bit(&val, map_depth);
529 		if (nr + nr_tags <= map_depth) {
530 			atomic_long_t *ptr = (atomic_long_t *) &map->word;
531 
532 			get_mask = ((1UL << nr_tags) - 1) << nr;
533 			while (!atomic_long_try_cmpxchg(ptr, &val,
534 							  get_mask | val))
535 				;
536 			get_mask = (get_mask & ~val) >> nr;
537 			if (get_mask) {
538 				*offset = nr + (index << sb->shift);
539 				update_alloc_hint_after_get(sb, depth, hint,
540 							*offset + nr_tags - 1);
541 				return get_mask;
542 			}
543 		}
544 next:
545 		/* Jump to next index. */
546 		if (++index >= sb->map_nr)
547 			index = 0;
548 	}
549 
550 	return 0;
551 }
552 
sbitmap_queue_get_shallow(struct sbitmap_queue * sbq,unsigned int shallow_depth)553 int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
554 			      unsigned int shallow_depth)
555 {
556 	WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
557 
558 	return sbitmap_get_shallow(&sbq->sb, shallow_depth);
559 }
560 EXPORT_SYMBOL_GPL(sbitmap_queue_get_shallow);
561 
sbitmap_queue_min_shallow_depth(struct sbitmap_queue * sbq,unsigned int min_shallow_depth)562 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
563 				     unsigned int min_shallow_depth)
564 {
565 	sbq->min_shallow_depth = min_shallow_depth;
566 	sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
567 }
568 EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
569 
__sbitmap_queue_wake_up(struct sbitmap_queue * sbq,int nr)570 static void __sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
571 {
572 	int i, wake_index, woken;
573 
574 	if (!atomic_read(&sbq->ws_active))
575 		return;
576 
577 	wake_index = atomic_read(&sbq->wake_index);
578 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
579 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
580 
581 		/*
582 		 * Advance the index before checking the current queue.
583 		 * It improves fairness, by ensuring the queue doesn't
584 		 * need to be fully emptied before trying to wake up
585 		 * from the next one.
586 		 */
587 		wake_index = sbq_index_inc(wake_index);
588 
589 		if (waitqueue_active(&ws->wait)) {
590 			woken = wake_up_nr(&ws->wait, nr);
591 			if (woken == nr)
592 				break;
593 			nr -= woken;
594 		}
595 	}
596 
597 	if (wake_index != atomic_read(&sbq->wake_index))
598 		atomic_set(&sbq->wake_index, wake_index);
599 }
600 
sbitmap_queue_wake_up(struct sbitmap_queue * sbq,int nr)601 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
602 {
603 	unsigned int wake_batch = READ_ONCE(sbq->wake_batch);
604 	unsigned int wakeups;
605 
606 	if (!atomic_read(&sbq->ws_active))
607 		return;
608 
609 	atomic_add(nr, &sbq->completion_cnt);
610 	wakeups = atomic_read(&sbq->wakeup_cnt);
611 
612 	do {
613 		if (atomic_read(&sbq->completion_cnt) - wakeups < wake_batch)
614 			return;
615 	} while (!atomic_try_cmpxchg(&sbq->wakeup_cnt,
616 				     &wakeups, wakeups + wake_batch));
617 
618 	__sbitmap_queue_wake_up(sbq, wake_batch);
619 }
620 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
621 
sbitmap_update_cpu_hint(struct sbitmap * sb,int cpu,int tag)622 static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
623 {
624 	if (likely(!sb->round_robin && tag < sb->depth))
625 		data_race(*per_cpu_ptr(sb->alloc_hint, cpu) = tag);
626 }
627 
sbitmap_queue_clear_batch(struct sbitmap_queue * sbq,int offset,int * tags,int nr_tags)628 void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
629 				int *tags, int nr_tags)
630 {
631 	struct sbitmap *sb = &sbq->sb;
632 	unsigned long *addr = NULL;
633 	unsigned long mask = 0;
634 	int i;
635 
636 	smp_mb__before_atomic();
637 	for (i = 0; i < nr_tags; i++) {
638 		const int tag = tags[i] - offset;
639 		unsigned long *this_addr;
640 
641 		/* since we're clearing a batch, skip the deferred map */
642 		this_addr = &sb->map[SB_NR_TO_INDEX(sb, tag)].word;
643 		if (!addr) {
644 			addr = this_addr;
645 		} else if (addr != this_addr) {
646 			atomic_long_andnot(mask, (atomic_long_t *) addr);
647 			mask = 0;
648 			addr = this_addr;
649 		}
650 		mask |= (1UL << SB_NR_TO_BIT(sb, tag));
651 	}
652 
653 	if (mask)
654 		atomic_long_andnot(mask, (atomic_long_t *) addr);
655 
656 	smp_mb__after_atomic();
657 	sbitmap_queue_wake_up(sbq, nr_tags);
658 	sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
659 					tags[nr_tags - 1] - offset);
660 }
661 
sbitmap_queue_clear(struct sbitmap_queue * sbq,unsigned int nr,unsigned int cpu)662 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
663 			 unsigned int cpu)
664 {
665 	/*
666 	 * Once the clear bit is set, the bit may be allocated out.
667 	 *
668 	 * Orders READ/WRITE on the associated instance(such as request
669 	 * of blk_mq) by this bit for avoiding race with re-allocation,
670 	 * and its pair is the memory barrier implied in __sbitmap_get_word.
671 	 *
672 	 * One invariant is that the clear bit has to be zero when the bit
673 	 * is in use.
674 	 */
675 	smp_mb__before_atomic();
676 	sbitmap_deferred_clear_bit(&sbq->sb, nr);
677 
678 	/*
679 	 * Pairs with the memory barrier in set_current_state() to ensure the
680 	 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
681 	 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
682 	 * waiter. See the comment on waitqueue_active().
683 	 */
684 	smp_mb__after_atomic();
685 	sbitmap_queue_wake_up(sbq, 1);
686 	sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
687 }
688 EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
689 
sbitmap_queue_wake_all(struct sbitmap_queue * sbq)690 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
691 {
692 	int i, wake_index;
693 
694 	/*
695 	 * Pairs with the memory barrier in set_current_state() like in
696 	 * sbitmap_queue_wake_up().
697 	 */
698 	smp_mb();
699 	wake_index = atomic_read(&sbq->wake_index);
700 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
701 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
702 
703 		if (waitqueue_active(&ws->wait))
704 			wake_up(&ws->wait);
705 
706 		wake_index = sbq_index_inc(wake_index);
707 	}
708 }
709 EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
710 
sbitmap_queue_show(struct sbitmap_queue * sbq,struct seq_file * m)711 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
712 {
713 	bool first;
714 	int i;
715 
716 	sbitmap_show(&sbq->sb, m);
717 
718 	seq_puts(m, "alloc_hint={");
719 	first = true;
720 	for_each_possible_cpu(i) {
721 		if (!first)
722 			seq_puts(m, ", ");
723 		first = false;
724 		seq_printf(m, "%u", *per_cpu_ptr(sbq->sb.alloc_hint, i));
725 	}
726 	seq_puts(m, "}\n");
727 
728 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
729 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
730 	seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
731 
732 	seq_puts(m, "ws={\n");
733 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
734 		struct sbq_wait_state *ws = &sbq->ws[i];
735 		seq_printf(m, "\t{.wait=%s},\n",
736 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
737 	}
738 	seq_puts(m, "}\n");
739 
740 	seq_printf(m, "round_robin=%d\n", sbq->sb.round_robin);
741 	seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
742 }
743 EXPORT_SYMBOL_GPL(sbitmap_queue_show);
744 
sbitmap_add_wait_queue(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait)745 void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
746 			    struct sbq_wait_state *ws,
747 			    struct sbq_wait *sbq_wait)
748 {
749 	if (!sbq_wait->sbq) {
750 		sbq_wait->sbq = sbq;
751 		atomic_inc(&sbq->ws_active);
752 		add_wait_queue(&ws->wait, &sbq_wait->wait);
753 	}
754 }
755 EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
756 
sbitmap_del_wait_queue(struct sbq_wait * sbq_wait)757 void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
758 {
759 	list_del_init(&sbq_wait->wait.entry);
760 	if (sbq_wait->sbq) {
761 		atomic_dec(&sbq_wait->sbq->ws_active);
762 		sbq_wait->sbq = NULL;
763 	}
764 }
765 EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
766 
sbitmap_prepare_to_wait(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait,int state)767 void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
768 			     struct sbq_wait_state *ws,
769 			     struct sbq_wait *sbq_wait, int state)
770 {
771 	if (!sbq_wait->sbq) {
772 		atomic_inc(&sbq->ws_active);
773 		sbq_wait->sbq = sbq;
774 	}
775 	prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
776 }
777 EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
778 
sbitmap_finish_wait(struct sbitmap_queue * sbq,struct sbq_wait_state * ws,struct sbq_wait * sbq_wait)779 void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
780 			 struct sbq_wait *sbq_wait)
781 {
782 	finish_wait(&ws->wait, &sbq_wait->wait);
783 	if (sbq_wait->sbq) {
784 		atomic_dec(&sbq->ws_active);
785 		sbq_wait->sbq = NULL;
786 	}
787 }
788 EXPORT_SYMBOL_GPL(sbitmap_finish_wait);
789