xref: /linux/lib/sbitmap.c (revision 6bf0eb5504529cdf50a24135e4c4442a093c06d6)
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 
12b2dbff1bSJens Axboe /*
13b2dbff1bSJens Axboe  * See if we have deferred clears that we can batch move
14b2dbff1bSJens Axboe  */
15b2dbff1bSJens Axboe static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index)
16b2dbff1bSJens Axboe {
17b2dbff1bSJens Axboe 	unsigned long mask, val;
18b2dbff1bSJens Axboe 	bool ret = false;
19fe76fc6aSMing Lei 	unsigned long flags;
20b2dbff1bSJens Axboe 
21fe76fc6aSMing Lei 	spin_lock_irqsave(&sb->map[index].swap_lock, flags);
22b2dbff1bSJens Axboe 
23b2dbff1bSJens Axboe 	if (!sb->map[index].cleared)
24b2dbff1bSJens Axboe 		goto out_unlock;
25b2dbff1bSJens Axboe 
26b2dbff1bSJens Axboe 	/*
27b2dbff1bSJens Axboe 	 * First get a stable cleared mask, setting the old mask to 0.
28b2dbff1bSJens Axboe 	 */
2941723288SPavel Begunkov 	mask = xchg(&sb->map[index].cleared, 0);
30b2dbff1bSJens Axboe 
31b2dbff1bSJens Axboe 	/*
32b2dbff1bSJens Axboe 	 * Now clear the masked bits in our free word
33b2dbff1bSJens Axboe 	 */
34b2dbff1bSJens Axboe 	do {
35b2dbff1bSJens Axboe 		val = sb->map[index].word;
36b2dbff1bSJens Axboe 	} while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val);
37b2dbff1bSJens Axboe 
38b2dbff1bSJens Axboe 	ret = true;
39b2dbff1bSJens Axboe out_unlock:
40fe76fc6aSMing Lei 	spin_unlock_irqrestore(&sb->map[index].swap_lock, flags);
41b2dbff1bSJens Axboe 	return ret;
42b2dbff1bSJens Axboe }
43b2dbff1bSJens Axboe 
4488459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
4588459642SOmar Sandoval 		      gfp_t flags, int node)
4688459642SOmar Sandoval {
4788459642SOmar Sandoval 	unsigned int bits_per_word;
4888459642SOmar Sandoval 	unsigned int i;
4988459642SOmar Sandoval 
5088459642SOmar Sandoval 	if (shift < 0) {
5188459642SOmar Sandoval 		shift = ilog2(BITS_PER_LONG);
5288459642SOmar Sandoval 		/*
5388459642SOmar Sandoval 		 * If the bitmap is small, shrink the number of bits per word so
5488459642SOmar Sandoval 		 * we spread over a few cachelines, at least. If less than 4
5588459642SOmar Sandoval 		 * bits, just forget about it, it's not going to work optimally
5688459642SOmar Sandoval 		 * anyway.
5788459642SOmar Sandoval 		 */
5888459642SOmar Sandoval 		if (depth >= 4) {
5988459642SOmar Sandoval 			while ((4U << shift) > depth)
6088459642SOmar Sandoval 				shift--;
6188459642SOmar Sandoval 		}
6288459642SOmar Sandoval 	}
6388459642SOmar Sandoval 	bits_per_word = 1U << shift;
6488459642SOmar Sandoval 	if (bits_per_word > BITS_PER_LONG)
6588459642SOmar Sandoval 		return -EINVAL;
6688459642SOmar Sandoval 
6788459642SOmar Sandoval 	sb->shift = shift;
6888459642SOmar Sandoval 	sb->depth = depth;
6988459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
7088459642SOmar Sandoval 
7188459642SOmar Sandoval 	if (depth == 0) {
7288459642SOmar Sandoval 		sb->map = NULL;
7388459642SOmar Sandoval 		return 0;
7488459642SOmar Sandoval 	}
7588459642SOmar Sandoval 
76590b5b7dSKees Cook 	sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node);
7788459642SOmar Sandoval 	if (!sb->map)
7888459642SOmar Sandoval 		return -ENOMEM;
7988459642SOmar Sandoval 
8088459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
8188459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
8288459642SOmar Sandoval 		depth -= sb->map[i].depth;
83ea86ea2cSJens Axboe 		spin_lock_init(&sb->map[i].swap_lock);
8488459642SOmar Sandoval 	}
8588459642SOmar Sandoval 	return 0;
8688459642SOmar Sandoval }
8788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_init_node);
8888459642SOmar Sandoval 
8988459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
9088459642SOmar Sandoval {
9188459642SOmar Sandoval 	unsigned int bits_per_word = 1U << sb->shift;
9288459642SOmar Sandoval 	unsigned int i;
9388459642SOmar Sandoval 
94b2dbff1bSJens Axboe 	for (i = 0; i < sb->map_nr; i++)
95b2dbff1bSJens Axboe 		sbitmap_deferred_clear(sb, i);
96b2dbff1bSJens Axboe 
9788459642SOmar Sandoval 	sb->depth = depth;
9888459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
9988459642SOmar Sandoval 
10088459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
10188459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
10288459642SOmar Sandoval 		depth -= sb->map[i].depth;
10388459642SOmar Sandoval 	}
10488459642SOmar Sandoval }
10588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_resize);
10688459642SOmar Sandoval 
107c05e6673SOmar Sandoval static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
108c05e6673SOmar Sandoval 			      unsigned int hint, bool wrap)
10988459642SOmar Sandoval {
11088459642SOmar Sandoval 	unsigned int orig_hint = hint;
11188459642SOmar Sandoval 	int nr;
11288459642SOmar Sandoval 
11388459642SOmar Sandoval 	while (1) {
114c05e6673SOmar Sandoval 		nr = find_next_zero_bit(word, depth, hint);
115c05e6673SOmar Sandoval 		if (unlikely(nr >= depth)) {
11688459642SOmar Sandoval 			/*
11788459642SOmar Sandoval 			 * We started with an offset, and we didn't reset the
11888459642SOmar Sandoval 			 * offset to 0 in a failure case, so start from 0 to
11988459642SOmar Sandoval 			 * exhaust the map.
12088459642SOmar Sandoval 			 */
12188459642SOmar Sandoval 			if (orig_hint && hint && wrap) {
12288459642SOmar Sandoval 				hint = orig_hint = 0;
12388459642SOmar Sandoval 				continue;
12488459642SOmar Sandoval 			}
12588459642SOmar Sandoval 			return -1;
12688459642SOmar Sandoval 		}
12788459642SOmar Sandoval 
1284ace53f1SOmar Sandoval 		if (!test_and_set_bit_lock(nr, word))
12988459642SOmar Sandoval 			break;
13088459642SOmar Sandoval 
13188459642SOmar Sandoval 		hint = nr + 1;
132c05e6673SOmar Sandoval 		if (hint >= depth - 1)
13388459642SOmar Sandoval 			hint = 0;
13488459642SOmar Sandoval 	}
13588459642SOmar Sandoval 
13688459642SOmar Sandoval 	return nr;
13788459642SOmar Sandoval }
13888459642SOmar Sandoval 
139ea86ea2cSJens Axboe static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index,
140ea86ea2cSJens Axboe 				     unsigned int alloc_hint, bool round_robin)
141ea86ea2cSJens Axboe {
142ea86ea2cSJens Axboe 	int nr;
143ea86ea2cSJens Axboe 
144ea86ea2cSJens Axboe 	do {
145ea86ea2cSJens Axboe 		nr = __sbitmap_get_word(&sb->map[index].word,
146ea86ea2cSJens Axboe 					sb->map[index].depth, alloc_hint,
147ea86ea2cSJens Axboe 					!round_robin);
148ea86ea2cSJens Axboe 		if (nr != -1)
149ea86ea2cSJens Axboe 			break;
150ea86ea2cSJens Axboe 		if (!sbitmap_deferred_clear(sb, index))
151ea86ea2cSJens Axboe 			break;
152ea86ea2cSJens Axboe 	} while (1);
153ea86ea2cSJens Axboe 
154ea86ea2cSJens Axboe 	return nr;
155ea86ea2cSJens Axboe }
156ea86ea2cSJens Axboe 
15788459642SOmar Sandoval int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
15888459642SOmar Sandoval {
15988459642SOmar Sandoval 	unsigned int i, index;
16088459642SOmar Sandoval 	int nr = -1;
16188459642SOmar Sandoval 
16288459642SOmar Sandoval 	index = SB_NR_TO_INDEX(sb, alloc_hint);
16388459642SOmar Sandoval 
16427fae429SJens Axboe 	/*
16527fae429SJens Axboe 	 * Unless we're doing round robin tag allocation, just use the
16627fae429SJens Axboe 	 * alloc_hint to find the right word index. No point in looping
16727fae429SJens Axboe 	 * twice in find_next_zero_bit() for that case.
16827fae429SJens Axboe 	 */
16927fae429SJens Axboe 	if (round_robin)
17027fae429SJens Axboe 		alloc_hint = SB_NR_TO_BIT(sb, alloc_hint);
17127fae429SJens Axboe 	else
17227fae429SJens Axboe 		alloc_hint = 0;
17327fae429SJens Axboe 
17488459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
175ea86ea2cSJens Axboe 		nr = sbitmap_find_bit_in_index(sb, index, alloc_hint,
176ea86ea2cSJens Axboe 						round_robin);
17788459642SOmar Sandoval 		if (nr != -1) {
17888459642SOmar Sandoval 			nr += index << sb->shift;
17988459642SOmar Sandoval 			break;
18088459642SOmar Sandoval 		}
18188459642SOmar Sandoval 
18288459642SOmar Sandoval 		/* Jump to next index. */
18388459642SOmar Sandoval 		alloc_hint = 0;
18427fae429SJens Axboe 		if (++index >= sb->map_nr)
18527fae429SJens Axboe 			index = 0;
18688459642SOmar Sandoval 	}
18788459642SOmar Sandoval 
18888459642SOmar Sandoval 	return nr;
18988459642SOmar Sandoval }
19088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get);
19188459642SOmar Sandoval 
192c05e6673SOmar Sandoval int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
193c05e6673SOmar Sandoval 			unsigned long shallow_depth)
194c05e6673SOmar Sandoval {
195c05e6673SOmar Sandoval 	unsigned int i, index;
196c05e6673SOmar Sandoval 	int nr = -1;
197c05e6673SOmar Sandoval 
198c05e6673SOmar Sandoval 	index = SB_NR_TO_INDEX(sb, alloc_hint);
199c05e6673SOmar Sandoval 
200c05e6673SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
201b2dbff1bSJens Axboe again:
202c05e6673SOmar Sandoval 		nr = __sbitmap_get_word(&sb->map[index].word,
203c05e6673SOmar Sandoval 					min(sb->map[index].depth, shallow_depth),
204c05e6673SOmar Sandoval 					SB_NR_TO_BIT(sb, alloc_hint), true);
205c05e6673SOmar Sandoval 		if (nr != -1) {
206c05e6673SOmar Sandoval 			nr += index << sb->shift;
207c05e6673SOmar Sandoval 			break;
208c05e6673SOmar Sandoval 		}
209c05e6673SOmar Sandoval 
210b2dbff1bSJens Axboe 		if (sbitmap_deferred_clear(sb, index))
211b2dbff1bSJens Axboe 			goto again;
212b2dbff1bSJens Axboe 
213c05e6673SOmar Sandoval 		/* Jump to next index. */
214c05e6673SOmar Sandoval 		index++;
215c05e6673SOmar Sandoval 		alloc_hint = index << sb->shift;
216c05e6673SOmar Sandoval 
217c05e6673SOmar Sandoval 		if (index >= sb->map_nr) {
218c05e6673SOmar Sandoval 			index = 0;
219c05e6673SOmar Sandoval 			alloc_hint = 0;
220c05e6673SOmar Sandoval 		}
221c05e6673SOmar Sandoval 	}
222c05e6673SOmar Sandoval 
223c05e6673SOmar Sandoval 	return nr;
224c05e6673SOmar Sandoval }
225c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
226c05e6673SOmar Sandoval 
22788459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb)
22888459642SOmar Sandoval {
22988459642SOmar Sandoval 	unsigned int i;
23088459642SOmar Sandoval 
23188459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
232b2dbff1bSJens Axboe 		if (sb->map[i].word & ~sb->map[i].cleared)
23388459642SOmar Sandoval 			return true;
23488459642SOmar Sandoval 	}
23588459642SOmar Sandoval 	return false;
23688459642SOmar Sandoval }
23788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
23888459642SOmar Sandoval 
239ea86ea2cSJens Axboe static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set)
24088459642SOmar Sandoval {
24160658e0dSColin Ian King 	unsigned int i, weight = 0;
24288459642SOmar Sandoval 
24388459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
24488459642SOmar Sandoval 		const struct sbitmap_word *word = &sb->map[i];
24588459642SOmar Sandoval 
246ea86ea2cSJens Axboe 		if (set)
24788459642SOmar Sandoval 			weight += bitmap_weight(&word->word, word->depth);
248ea86ea2cSJens Axboe 		else
249ea86ea2cSJens Axboe 			weight += bitmap_weight(&word->cleared, word->depth);
25088459642SOmar Sandoval 	}
25188459642SOmar Sandoval 	return weight;
25288459642SOmar Sandoval }
253ea86ea2cSJens Axboe 
254ea86ea2cSJens Axboe static unsigned int sbitmap_weight(const struct sbitmap *sb)
255ea86ea2cSJens Axboe {
256ea86ea2cSJens Axboe 	return __sbitmap_weight(sb, true);
257ea86ea2cSJens Axboe }
258ea86ea2cSJens Axboe 
259ea86ea2cSJens Axboe static unsigned int sbitmap_cleared(const struct sbitmap *sb)
260ea86ea2cSJens Axboe {
261ea86ea2cSJens Axboe 	return __sbitmap_weight(sb, false);
262ea86ea2cSJens Axboe }
26388459642SOmar Sandoval 
26424af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
26524af1ccfSOmar Sandoval {
26624af1ccfSOmar Sandoval 	seq_printf(m, "depth=%u\n", sb->depth);
267ea86ea2cSJens Axboe 	seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb));
268ea86ea2cSJens Axboe 	seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb));
26924af1ccfSOmar Sandoval 	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
27024af1ccfSOmar Sandoval 	seq_printf(m, "map_nr=%u\n", sb->map_nr);
27124af1ccfSOmar Sandoval }
27224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show);
27324af1ccfSOmar Sandoval 
27424af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
27524af1ccfSOmar Sandoval {
27624af1ccfSOmar Sandoval 	if ((offset & 0xf) == 0) {
27724af1ccfSOmar Sandoval 		if (offset != 0)
27824af1ccfSOmar Sandoval 			seq_putc(m, '\n');
27924af1ccfSOmar Sandoval 		seq_printf(m, "%08x:", offset);
28024af1ccfSOmar Sandoval 	}
28124af1ccfSOmar Sandoval 	if ((offset & 0x1) == 0)
28224af1ccfSOmar Sandoval 		seq_putc(m, ' ');
28324af1ccfSOmar Sandoval 	seq_printf(m, "%02x", byte);
28424af1ccfSOmar Sandoval }
28524af1ccfSOmar Sandoval 
28624af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
28724af1ccfSOmar Sandoval {
28824af1ccfSOmar Sandoval 	u8 byte = 0;
28924af1ccfSOmar Sandoval 	unsigned int byte_bits = 0;
29024af1ccfSOmar Sandoval 	unsigned int offset = 0;
29124af1ccfSOmar Sandoval 	int i;
29224af1ccfSOmar Sandoval 
29324af1ccfSOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
29424af1ccfSOmar Sandoval 		unsigned long word = READ_ONCE(sb->map[i].word);
295*6bf0eb55SJohn Garry 		unsigned long cleared = READ_ONCE(sb->map[i].cleared);
29624af1ccfSOmar Sandoval 		unsigned int word_bits = READ_ONCE(sb->map[i].depth);
29724af1ccfSOmar Sandoval 
298*6bf0eb55SJohn Garry 		word &= ~cleared;
299*6bf0eb55SJohn Garry 
30024af1ccfSOmar Sandoval 		while (word_bits > 0) {
30124af1ccfSOmar Sandoval 			unsigned int bits = min(8 - byte_bits, word_bits);
30224af1ccfSOmar Sandoval 
30324af1ccfSOmar Sandoval 			byte |= (word & (BIT(bits) - 1)) << byte_bits;
30424af1ccfSOmar Sandoval 			byte_bits += bits;
30524af1ccfSOmar Sandoval 			if (byte_bits == 8) {
30624af1ccfSOmar Sandoval 				emit_byte(m, offset, byte);
30724af1ccfSOmar Sandoval 				byte = 0;
30824af1ccfSOmar Sandoval 				byte_bits = 0;
30924af1ccfSOmar Sandoval 				offset++;
31024af1ccfSOmar Sandoval 			}
31124af1ccfSOmar Sandoval 			word >>= bits;
31224af1ccfSOmar Sandoval 			word_bits -= bits;
31324af1ccfSOmar Sandoval 		}
31424af1ccfSOmar Sandoval 	}
31524af1ccfSOmar Sandoval 	if (byte_bits) {
31624af1ccfSOmar Sandoval 		emit_byte(m, offset, byte);
31724af1ccfSOmar Sandoval 		offset++;
31824af1ccfSOmar Sandoval 	}
31924af1ccfSOmar Sandoval 	if (offset)
32024af1ccfSOmar Sandoval 		seq_putc(m, '\n');
32124af1ccfSOmar Sandoval }
32224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
32324af1ccfSOmar Sandoval 
324a3275539SOmar Sandoval static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
325a3275539SOmar Sandoval 					unsigned int depth)
32688459642SOmar Sandoval {
32788459642SOmar Sandoval 	unsigned int wake_batch;
328a3275539SOmar Sandoval 	unsigned int shallow_depth;
32988459642SOmar Sandoval 
33088459642SOmar Sandoval 	/*
33188459642SOmar Sandoval 	 * For each batch, we wake up one queue. We need to make sure that our
332a3275539SOmar Sandoval 	 * batch size is small enough that the full depth of the bitmap,
333a3275539SOmar Sandoval 	 * potentially limited by a shallow depth, is enough to wake up all of
334a3275539SOmar Sandoval 	 * the queues.
335a3275539SOmar Sandoval 	 *
336a3275539SOmar Sandoval 	 * Each full word of the bitmap has bits_per_word bits, and there might
337a3275539SOmar Sandoval 	 * be a partial word. There are depth / bits_per_word full words and
338a3275539SOmar Sandoval 	 * depth % bits_per_word bits left over. In bitwise arithmetic:
339a3275539SOmar Sandoval 	 *
340a3275539SOmar Sandoval 	 * bits_per_word = 1 << shift
341a3275539SOmar Sandoval 	 * depth / bits_per_word = depth >> shift
342a3275539SOmar Sandoval 	 * depth % bits_per_word = depth & ((1 << shift) - 1)
343a3275539SOmar Sandoval 	 *
344a3275539SOmar Sandoval 	 * Each word can be limited to sbq->min_shallow_depth bits.
34588459642SOmar Sandoval 	 */
346a3275539SOmar Sandoval 	shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
347a3275539SOmar Sandoval 	depth = ((depth >> sbq->sb.shift) * shallow_depth +
348a3275539SOmar Sandoval 		 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
349a3275539SOmar Sandoval 	wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
350a3275539SOmar Sandoval 			     SBQ_WAKE_BATCH);
35188459642SOmar Sandoval 
35288459642SOmar Sandoval 	return wake_batch;
35388459642SOmar Sandoval }
35488459642SOmar Sandoval 
35588459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
356f4a644dbSOmar Sandoval 			    int shift, bool round_robin, gfp_t flags, int node)
35788459642SOmar Sandoval {
35888459642SOmar Sandoval 	int ret;
35988459642SOmar Sandoval 	int i;
36088459642SOmar Sandoval 
36188459642SOmar Sandoval 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
36288459642SOmar Sandoval 	if (ret)
36388459642SOmar Sandoval 		return ret;
36488459642SOmar Sandoval 
36540aabb67SOmar Sandoval 	sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
36640aabb67SOmar Sandoval 	if (!sbq->alloc_hint) {
36740aabb67SOmar Sandoval 		sbitmap_free(&sbq->sb);
36840aabb67SOmar Sandoval 		return -ENOMEM;
36940aabb67SOmar Sandoval 	}
37040aabb67SOmar Sandoval 
37198d95416SOmar Sandoval 	if (depth && !round_robin) {
37298d95416SOmar Sandoval 		for_each_possible_cpu(i)
37398d95416SOmar Sandoval 			*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
37498d95416SOmar Sandoval 	}
37598d95416SOmar Sandoval 
376a3275539SOmar Sandoval 	sbq->min_shallow_depth = UINT_MAX;
377a3275539SOmar Sandoval 	sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
37888459642SOmar Sandoval 	atomic_set(&sbq->wake_index, 0);
3795d2ee712SJens Axboe 	atomic_set(&sbq->ws_active, 0);
38088459642SOmar Sandoval 
38148e28166SOmar Sandoval 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
38288459642SOmar Sandoval 	if (!sbq->ws) {
38340aabb67SOmar Sandoval 		free_percpu(sbq->alloc_hint);
38488459642SOmar Sandoval 		sbitmap_free(&sbq->sb);
38588459642SOmar Sandoval 		return -ENOMEM;
38688459642SOmar Sandoval 	}
38788459642SOmar Sandoval 
38888459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
38988459642SOmar Sandoval 		init_waitqueue_head(&sbq->ws[i].wait);
39088459642SOmar Sandoval 		atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
39188459642SOmar Sandoval 	}
392f4a644dbSOmar Sandoval 
393f4a644dbSOmar Sandoval 	sbq->round_robin = round_robin;
39488459642SOmar Sandoval 	return 0;
39588459642SOmar Sandoval }
39688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
39788459642SOmar Sandoval 
398a3275539SOmar Sandoval static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
399a3275539SOmar Sandoval 					    unsigned int depth)
40088459642SOmar Sandoval {
401a3275539SOmar Sandoval 	unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
4026c0ca7aeSOmar Sandoval 	int i;
4036c0ca7aeSOmar Sandoval 
4046c0ca7aeSOmar Sandoval 	if (sbq->wake_batch != wake_batch) {
4056c0ca7aeSOmar Sandoval 		WRITE_ONCE(sbq->wake_batch, wake_batch);
4066c0ca7aeSOmar Sandoval 		/*
407e6fc4649SMing Lei 		 * Pairs with the memory barrier in sbitmap_queue_wake_up()
408e6fc4649SMing Lei 		 * to ensure that the batch size is updated before the wait
409e6fc4649SMing Lei 		 * counts.
4106c0ca7aeSOmar Sandoval 		 */
411a0934fd2SAndrea Parri 		smp_mb();
4126c0ca7aeSOmar Sandoval 		for (i = 0; i < SBQ_WAIT_QUEUES; i++)
4136c0ca7aeSOmar Sandoval 			atomic_set(&sbq->ws[i].wait_cnt, 1);
4146c0ca7aeSOmar Sandoval 	}
415a3275539SOmar Sandoval }
416a3275539SOmar Sandoval 
417a3275539SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
418a3275539SOmar Sandoval {
419a3275539SOmar Sandoval 	sbitmap_queue_update_wake_batch(sbq, depth);
42088459642SOmar Sandoval 	sbitmap_resize(&sbq->sb, depth);
42188459642SOmar Sandoval }
42288459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
42388459642SOmar Sandoval 
424f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq)
42540aabb67SOmar Sandoval {
42605fd095dSOmar Sandoval 	unsigned int hint, depth;
42740aabb67SOmar Sandoval 	int nr;
42840aabb67SOmar Sandoval 
42940aabb67SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
43005fd095dSOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
43105fd095dSOmar Sandoval 	if (unlikely(hint >= depth)) {
43205fd095dSOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
43305fd095dSOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
43405fd095dSOmar Sandoval 	}
435f4a644dbSOmar Sandoval 	nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
43640aabb67SOmar Sandoval 
43740aabb67SOmar Sandoval 	if (nr == -1) {
43840aabb67SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
43940aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
440f4a644dbSOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
44140aabb67SOmar Sandoval 		/* Only update the hint if we used it. */
44240aabb67SOmar Sandoval 		hint = nr + 1;
44305fd095dSOmar Sandoval 		if (hint >= depth - 1)
44440aabb67SOmar Sandoval 			hint = 0;
44540aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
44640aabb67SOmar Sandoval 	}
44740aabb67SOmar Sandoval 
44840aabb67SOmar Sandoval 	return nr;
44940aabb67SOmar Sandoval }
45040aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
45140aabb67SOmar Sandoval 
452c05e6673SOmar Sandoval int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
453c05e6673SOmar Sandoval 				unsigned int shallow_depth)
454c05e6673SOmar Sandoval {
455c05e6673SOmar Sandoval 	unsigned int hint, depth;
456c05e6673SOmar Sandoval 	int nr;
457c05e6673SOmar Sandoval 
45861445b56SOmar Sandoval 	WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
45961445b56SOmar Sandoval 
460c05e6673SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
461c05e6673SOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
462c05e6673SOmar Sandoval 	if (unlikely(hint >= depth)) {
463c05e6673SOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
464c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
465c05e6673SOmar Sandoval 	}
466c05e6673SOmar Sandoval 	nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
467c05e6673SOmar Sandoval 
468c05e6673SOmar Sandoval 	if (nr == -1) {
469c05e6673SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
470c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
471c05e6673SOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
472c05e6673SOmar Sandoval 		/* Only update the hint if we used it. */
473c05e6673SOmar Sandoval 		hint = nr + 1;
474c05e6673SOmar Sandoval 		if (hint >= depth - 1)
475c05e6673SOmar Sandoval 			hint = 0;
476c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
477c05e6673SOmar Sandoval 	}
478c05e6673SOmar Sandoval 
479c05e6673SOmar Sandoval 	return nr;
480c05e6673SOmar Sandoval }
481c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
482c05e6673SOmar Sandoval 
483a3275539SOmar Sandoval void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
484a3275539SOmar Sandoval 				     unsigned int min_shallow_depth)
485a3275539SOmar Sandoval {
486a3275539SOmar Sandoval 	sbq->min_shallow_depth = min_shallow_depth;
487a3275539SOmar Sandoval 	sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
488a3275539SOmar Sandoval }
489a3275539SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
490a3275539SOmar Sandoval 
49188459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
49288459642SOmar Sandoval {
49388459642SOmar Sandoval 	int i, wake_index;
49488459642SOmar Sandoval 
4955d2ee712SJens Axboe 	if (!atomic_read(&sbq->ws_active))
4965d2ee712SJens Axboe 		return NULL;
4975d2ee712SJens Axboe 
49888459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
49988459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
50088459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
50188459642SOmar Sandoval 
50288459642SOmar Sandoval 		if (waitqueue_active(&ws->wait)) {
50341723288SPavel Begunkov 			if (wake_index != atomic_read(&sbq->wake_index))
50441723288SPavel Begunkov 				atomic_set(&sbq->wake_index, wake_index);
50588459642SOmar Sandoval 			return ws;
50688459642SOmar Sandoval 		}
50788459642SOmar Sandoval 
50888459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
50988459642SOmar Sandoval 	}
51088459642SOmar Sandoval 
51188459642SOmar Sandoval 	return NULL;
51288459642SOmar Sandoval }
51388459642SOmar Sandoval 
514c854ab57SJens Axboe static bool __sbq_wake_up(struct sbitmap_queue *sbq)
51588459642SOmar Sandoval {
51688459642SOmar Sandoval 	struct sbq_wait_state *ws;
5176c0ca7aeSOmar Sandoval 	unsigned int wake_batch;
51888459642SOmar Sandoval 	int wait_cnt;
51988459642SOmar Sandoval 
52088459642SOmar Sandoval 	ws = sbq_wake_ptr(sbq);
52188459642SOmar Sandoval 	if (!ws)
522c854ab57SJens Axboe 		return false;
52388459642SOmar Sandoval 
52488459642SOmar Sandoval 	wait_cnt = atomic_dec_return(&ws->wait_cnt);
5256c0ca7aeSOmar Sandoval 	if (wait_cnt <= 0) {
526c854ab57SJens Axboe 		int ret;
527c854ab57SJens Axboe 
5286c0ca7aeSOmar Sandoval 		wake_batch = READ_ONCE(sbq->wake_batch);
529c854ab57SJens Axboe 
5306c0ca7aeSOmar Sandoval 		/*
5316c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbitmap_queue_resize() to
5326c0ca7aeSOmar Sandoval 		 * ensure that we see the batch size update before the wait
5336c0ca7aeSOmar Sandoval 		 * count is reset.
5346c0ca7aeSOmar Sandoval 		 */
5356c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
536c854ab57SJens Axboe 
5376c0ca7aeSOmar Sandoval 		/*
538c854ab57SJens Axboe 		 * For concurrent callers of this, the one that failed the
539c854ab57SJens Axboe 		 * atomic_cmpxhcg() race should call this function again
540c854ab57SJens Axboe 		 * to wakeup a new batch on a different 'ws'.
5416c0ca7aeSOmar Sandoval 		 */
542c854ab57SJens Axboe 		ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
543c854ab57SJens Axboe 		if (ret == wait_cnt) {
54488459642SOmar Sandoval 			sbq_index_atomic_inc(&sbq->wake_index);
5454e5dff41SJens Axboe 			wake_up_nr(&ws->wait, wake_batch);
546c854ab57SJens Axboe 			return false;
54788459642SOmar Sandoval 		}
548c854ab57SJens Axboe 
549c854ab57SJens Axboe 		return true;
550c854ab57SJens Axboe 	}
551c854ab57SJens Axboe 
552c854ab57SJens Axboe 	return false;
553c854ab57SJens Axboe }
554c854ab57SJens Axboe 
555e6fc4649SMing Lei void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
556c854ab57SJens Axboe {
557c854ab57SJens Axboe 	while (__sbq_wake_up(sbq))
558c854ab57SJens Axboe 		;
55988459642SOmar Sandoval }
560e6fc4649SMing Lei EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
56188459642SOmar Sandoval 
56240aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
563f4a644dbSOmar Sandoval 			 unsigned int cpu)
56488459642SOmar Sandoval {
565e6d1fa58SMing Lei 	/*
566e6d1fa58SMing Lei 	 * Once the clear bit is set, the bit may be allocated out.
567e6d1fa58SMing Lei 	 *
568e6d1fa58SMing Lei 	 * Orders READ/WRITE on the asssociated instance(such as request
569e6d1fa58SMing Lei 	 * of blk_mq) by this bit for avoiding race with re-allocation,
570e6d1fa58SMing Lei 	 * and its pair is the memory barrier implied in __sbitmap_get_word.
571e6d1fa58SMing Lei 	 *
572e6d1fa58SMing Lei 	 * One invariant is that the clear bit has to be zero when the bit
573e6d1fa58SMing Lei 	 * is in use.
574e6d1fa58SMing Lei 	 */
575e6d1fa58SMing Lei 	smp_mb__before_atomic();
576ea86ea2cSJens Axboe 	sbitmap_deferred_clear_bit(&sbq->sb, nr);
577ea86ea2cSJens Axboe 
578e6fc4649SMing Lei 	/*
579e6fc4649SMing Lei 	 * Pairs with the memory barrier in set_current_state() to ensure the
580e6fc4649SMing Lei 	 * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker
581e6fc4649SMing Lei 	 * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
582e6fc4649SMing Lei 	 * waiter. See the comment on waitqueue_active().
583e6fc4649SMing Lei 	 */
584e6fc4649SMing Lei 	smp_mb__after_atomic();
585e6fc4649SMing Lei 	sbitmap_queue_wake_up(sbq);
586e6fc4649SMing Lei 
5875c64a8dfSOmar Sandoval 	if (likely(!sbq->round_robin && nr < sbq->sb.depth))
58840aabb67SOmar Sandoval 		*per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
58988459642SOmar Sandoval }
59088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
59188459642SOmar Sandoval 
59288459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
59388459642SOmar Sandoval {
59488459642SOmar Sandoval 	int i, wake_index;
59588459642SOmar Sandoval 
59688459642SOmar Sandoval 	/*
597f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() like in
598e6fc4649SMing Lei 	 * sbitmap_queue_wake_up().
59988459642SOmar Sandoval 	 */
60088459642SOmar Sandoval 	smp_mb();
60188459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
60288459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
60388459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
60488459642SOmar Sandoval 
60588459642SOmar Sandoval 		if (waitqueue_active(&ws->wait))
60688459642SOmar Sandoval 			wake_up(&ws->wait);
60788459642SOmar Sandoval 
60888459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
60988459642SOmar Sandoval 	}
61088459642SOmar Sandoval }
61188459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
61224af1ccfSOmar Sandoval 
61324af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
61424af1ccfSOmar Sandoval {
61524af1ccfSOmar Sandoval 	bool first;
61624af1ccfSOmar Sandoval 	int i;
61724af1ccfSOmar Sandoval 
61824af1ccfSOmar Sandoval 	sbitmap_show(&sbq->sb, m);
61924af1ccfSOmar Sandoval 
62024af1ccfSOmar Sandoval 	seq_puts(m, "alloc_hint={");
62124af1ccfSOmar Sandoval 	first = true;
62224af1ccfSOmar Sandoval 	for_each_possible_cpu(i) {
62324af1ccfSOmar Sandoval 		if (!first)
62424af1ccfSOmar Sandoval 			seq_puts(m, ", ");
62524af1ccfSOmar Sandoval 		first = false;
62624af1ccfSOmar Sandoval 		seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
62724af1ccfSOmar Sandoval 	}
62824af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
62924af1ccfSOmar Sandoval 
63024af1ccfSOmar Sandoval 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
63124af1ccfSOmar Sandoval 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
6325d2ee712SJens Axboe 	seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active));
63324af1ccfSOmar Sandoval 
63424af1ccfSOmar Sandoval 	seq_puts(m, "ws={\n");
63524af1ccfSOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
63624af1ccfSOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[i];
63724af1ccfSOmar Sandoval 
63824af1ccfSOmar Sandoval 		seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
63924af1ccfSOmar Sandoval 			   atomic_read(&ws->wait_cnt),
64024af1ccfSOmar Sandoval 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
64124af1ccfSOmar Sandoval 	}
64224af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
64324af1ccfSOmar Sandoval 
64424af1ccfSOmar Sandoval 	seq_printf(m, "round_robin=%d\n", sbq->round_robin);
645a3275539SOmar Sandoval 	seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
64624af1ccfSOmar Sandoval }
64724af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show);
6485d2ee712SJens Axboe 
6499f6b7ef6SJens Axboe void sbitmap_add_wait_queue(struct sbitmap_queue *sbq,
6509f6b7ef6SJens Axboe 			    struct sbq_wait_state *ws,
6519f6b7ef6SJens Axboe 			    struct sbq_wait *sbq_wait)
6529f6b7ef6SJens Axboe {
6539f6b7ef6SJens Axboe 	if (!sbq_wait->sbq) {
6549f6b7ef6SJens Axboe 		sbq_wait->sbq = sbq;
6559f6b7ef6SJens Axboe 		atomic_inc(&sbq->ws_active);
6569f6b7ef6SJens Axboe 		add_wait_queue(&ws->wait, &sbq_wait->wait);
6579f6b7ef6SJens Axboe 	}
658df034c93SDavid Jeffery }
6599f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_add_wait_queue);
6609f6b7ef6SJens Axboe 
6619f6b7ef6SJens Axboe void sbitmap_del_wait_queue(struct sbq_wait *sbq_wait)
6629f6b7ef6SJens Axboe {
6639f6b7ef6SJens Axboe 	list_del_init(&sbq_wait->wait.entry);
6649f6b7ef6SJens Axboe 	if (sbq_wait->sbq) {
6659f6b7ef6SJens Axboe 		atomic_dec(&sbq_wait->sbq->ws_active);
6669f6b7ef6SJens Axboe 		sbq_wait->sbq = NULL;
6679f6b7ef6SJens Axboe 	}
6689f6b7ef6SJens Axboe }
6699f6b7ef6SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_del_wait_queue);
6709f6b7ef6SJens Axboe 
6715d2ee712SJens Axboe void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
6725d2ee712SJens Axboe 			     struct sbq_wait_state *ws,
6735d2ee712SJens Axboe 			     struct sbq_wait *sbq_wait, int state)
6745d2ee712SJens Axboe {
6759f6b7ef6SJens Axboe 	if (!sbq_wait->sbq) {
6765d2ee712SJens Axboe 		atomic_inc(&sbq->ws_active);
6779f6b7ef6SJens Axboe 		sbq_wait->sbq = sbq;
6785d2ee712SJens Axboe 	}
6795d2ee712SJens Axboe 	prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
6805d2ee712SJens Axboe }
6815d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait);
6825d2ee712SJens Axboe 
6835d2ee712SJens Axboe void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
6845d2ee712SJens Axboe 			 struct sbq_wait *sbq_wait)
6855d2ee712SJens Axboe {
6865d2ee712SJens Axboe 	finish_wait(&ws->wait, &sbq_wait->wait);
6879f6b7ef6SJens Axboe 	if (sbq_wait->sbq) {
6885d2ee712SJens Axboe 		atomic_dec(&sbq->ws_active);
6899f6b7ef6SJens Axboe 		sbq_wait->sbq = NULL;
6905d2ee712SJens Axboe 	}
6915d2ee712SJens Axboe }
6925d2ee712SJens Axboe EXPORT_SYMBOL_GPL(sbitmap_finish_wait);
693