xref: /openbmc/linux/lib/sbitmap.c (revision c05e6673)
188459642SOmar Sandoval /*
288459642SOmar Sandoval  * Copyright (C) 2016 Facebook
388459642SOmar Sandoval  * Copyright (C) 2013-2014 Jens Axboe
488459642SOmar Sandoval  *
588459642SOmar Sandoval  * This program is free software; you can redistribute it and/or
688459642SOmar Sandoval  * modify it under the terms of the GNU General Public
788459642SOmar Sandoval  * License v2 as published by the Free Software Foundation.
888459642SOmar Sandoval  *
988459642SOmar Sandoval  * This program is distributed in the hope that it will be useful,
1088459642SOmar Sandoval  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1188459642SOmar Sandoval  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1288459642SOmar Sandoval  * General Public License for more details.
1388459642SOmar Sandoval  *
1488459642SOmar Sandoval  * You should have received a copy of the GNU General Public License
1588459642SOmar Sandoval  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
1688459642SOmar Sandoval  */
1788459642SOmar Sandoval 
18af8601adSIngo Molnar #include <linux/sched.h>
1998d95416SOmar Sandoval #include <linux/random.h>
2088459642SOmar Sandoval #include <linux/sbitmap.h>
2124af1ccfSOmar Sandoval #include <linux/seq_file.h>
2288459642SOmar Sandoval 
2388459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
2488459642SOmar Sandoval 		      gfp_t flags, int node)
2588459642SOmar Sandoval {
2688459642SOmar Sandoval 	unsigned int bits_per_word;
2788459642SOmar Sandoval 	unsigned int i;
2888459642SOmar Sandoval 
2988459642SOmar Sandoval 	if (shift < 0) {
3088459642SOmar Sandoval 		shift = ilog2(BITS_PER_LONG);
3188459642SOmar Sandoval 		/*
3288459642SOmar Sandoval 		 * If the bitmap is small, shrink the number of bits per word so
3388459642SOmar Sandoval 		 * we spread over a few cachelines, at least. If less than 4
3488459642SOmar Sandoval 		 * bits, just forget about it, it's not going to work optimally
3588459642SOmar Sandoval 		 * anyway.
3688459642SOmar Sandoval 		 */
3788459642SOmar Sandoval 		if (depth >= 4) {
3888459642SOmar Sandoval 			while ((4U << shift) > depth)
3988459642SOmar Sandoval 				shift--;
4088459642SOmar Sandoval 		}
4188459642SOmar Sandoval 	}
4288459642SOmar Sandoval 	bits_per_word = 1U << shift;
4388459642SOmar Sandoval 	if (bits_per_word > BITS_PER_LONG)
4488459642SOmar Sandoval 		return -EINVAL;
4588459642SOmar Sandoval 
4688459642SOmar Sandoval 	sb->shift = shift;
4788459642SOmar Sandoval 	sb->depth = depth;
4888459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
4988459642SOmar Sandoval 
5088459642SOmar Sandoval 	if (depth == 0) {
5188459642SOmar Sandoval 		sb->map = NULL;
5288459642SOmar Sandoval 		return 0;
5388459642SOmar Sandoval 	}
5488459642SOmar Sandoval 
5588459642SOmar Sandoval 	sb->map = kzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
5688459642SOmar Sandoval 	if (!sb->map)
5788459642SOmar Sandoval 		return -ENOMEM;
5888459642SOmar Sandoval 
5988459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
6088459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
6188459642SOmar Sandoval 		depth -= sb->map[i].depth;
6288459642SOmar Sandoval 	}
6388459642SOmar Sandoval 	return 0;
6488459642SOmar Sandoval }
6588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_init_node);
6688459642SOmar Sandoval 
6788459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
6888459642SOmar Sandoval {
6988459642SOmar Sandoval 	unsigned int bits_per_word = 1U << sb->shift;
7088459642SOmar Sandoval 	unsigned int i;
7188459642SOmar Sandoval 
7288459642SOmar Sandoval 	sb->depth = depth;
7388459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
7488459642SOmar Sandoval 
7588459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
7688459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
7788459642SOmar Sandoval 		depth -= sb->map[i].depth;
7888459642SOmar Sandoval 	}
7988459642SOmar Sandoval }
8088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_resize);
8188459642SOmar Sandoval 
82c05e6673SOmar Sandoval static int __sbitmap_get_word(unsigned long *word, unsigned long depth,
83c05e6673SOmar Sandoval 			      unsigned int hint, bool wrap)
8488459642SOmar Sandoval {
8588459642SOmar Sandoval 	unsigned int orig_hint = hint;
8688459642SOmar Sandoval 	int nr;
8788459642SOmar Sandoval 
8888459642SOmar Sandoval 	while (1) {
89c05e6673SOmar Sandoval 		nr = find_next_zero_bit(word, depth, hint);
90c05e6673SOmar Sandoval 		if (unlikely(nr >= depth)) {
9188459642SOmar Sandoval 			/*
9288459642SOmar Sandoval 			 * We started with an offset, and we didn't reset the
9388459642SOmar Sandoval 			 * offset to 0 in a failure case, so start from 0 to
9488459642SOmar Sandoval 			 * exhaust the map.
9588459642SOmar Sandoval 			 */
9688459642SOmar Sandoval 			if (orig_hint && hint && wrap) {
9788459642SOmar Sandoval 				hint = orig_hint = 0;
9888459642SOmar Sandoval 				continue;
9988459642SOmar Sandoval 			}
10088459642SOmar Sandoval 			return -1;
10188459642SOmar Sandoval 		}
10288459642SOmar Sandoval 
103c05e6673SOmar Sandoval 		if (!test_and_set_bit(nr, word))
10488459642SOmar Sandoval 			break;
10588459642SOmar Sandoval 
10688459642SOmar Sandoval 		hint = nr + 1;
107c05e6673SOmar Sandoval 		if (hint >= depth - 1)
10888459642SOmar Sandoval 			hint = 0;
10988459642SOmar Sandoval 	}
11088459642SOmar Sandoval 
11188459642SOmar Sandoval 	return nr;
11288459642SOmar Sandoval }
11388459642SOmar Sandoval 
11488459642SOmar Sandoval int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
11588459642SOmar Sandoval {
11688459642SOmar Sandoval 	unsigned int i, index;
11788459642SOmar Sandoval 	int nr = -1;
11888459642SOmar Sandoval 
11988459642SOmar Sandoval 	index = SB_NR_TO_INDEX(sb, alloc_hint);
12088459642SOmar Sandoval 
12188459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
122c05e6673SOmar Sandoval 		nr = __sbitmap_get_word(&sb->map[index].word,
123c05e6673SOmar Sandoval 					sb->map[index].depth,
12488459642SOmar Sandoval 					SB_NR_TO_BIT(sb, alloc_hint),
12588459642SOmar Sandoval 					!round_robin);
12688459642SOmar Sandoval 		if (nr != -1) {
12788459642SOmar Sandoval 			nr += index << sb->shift;
12888459642SOmar Sandoval 			break;
12988459642SOmar Sandoval 		}
13088459642SOmar Sandoval 
13188459642SOmar Sandoval 		/* Jump to next index. */
13288459642SOmar Sandoval 		index++;
13388459642SOmar Sandoval 		alloc_hint = index << sb->shift;
13488459642SOmar Sandoval 
13588459642SOmar Sandoval 		if (index >= sb->map_nr) {
13688459642SOmar Sandoval 			index = 0;
13788459642SOmar Sandoval 			alloc_hint = 0;
13888459642SOmar Sandoval 		}
13988459642SOmar Sandoval 	}
14088459642SOmar Sandoval 
14188459642SOmar Sandoval 	return nr;
14288459642SOmar Sandoval }
14388459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get);
14488459642SOmar Sandoval 
145c05e6673SOmar Sandoval int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
146c05e6673SOmar Sandoval 			unsigned long shallow_depth)
147c05e6673SOmar Sandoval {
148c05e6673SOmar Sandoval 	unsigned int i, index;
149c05e6673SOmar Sandoval 	int nr = -1;
150c05e6673SOmar Sandoval 
151c05e6673SOmar Sandoval 	index = SB_NR_TO_INDEX(sb, alloc_hint);
152c05e6673SOmar Sandoval 
153c05e6673SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
154c05e6673SOmar Sandoval 		nr = __sbitmap_get_word(&sb->map[index].word,
155c05e6673SOmar Sandoval 					min(sb->map[index].depth, shallow_depth),
156c05e6673SOmar Sandoval 					SB_NR_TO_BIT(sb, alloc_hint), true);
157c05e6673SOmar Sandoval 		if (nr != -1) {
158c05e6673SOmar Sandoval 			nr += index << sb->shift;
159c05e6673SOmar Sandoval 			break;
160c05e6673SOmar Sandoval 		}
161c05e6673SOmar Sandoval 
162c05e6673SOmar Sandoval 		/* Jump to next index. */
163c05e6673SOmar Sandoval 		index++;
164c05e6673SOmar Sandoval 		alloc_hint = index << sb->shift;
165c05e6673SOmar Sandoval 
166c05e6673SOmar Sandoval 		if (index >= sb->map_nr) {
167c05e6673SOmar Sandoval 			index = 0;
168c05e6673SOmar Sandoval 			alloc_hint = 0;
169c05e6673SOmar Sandoval 		}
170c05e6673SOmar Sandoval 	}
171c05e6673SOmar Sandoval 
172c05e6673SOmar Sandoval 	return nr;
173c05e6673SOmar Sandoval }
174c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get_shallow);
175c05e6673SOmar Sandoval 
17688459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb)
17788459642SOmar Sandoval {
17888459642SOmar Sandoval 	unsigned int i;
17988459642SOmar Sandoval 
18088459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
18188459642SOmar Sandoval 		if (sb->map[i].word)
18288459642SOmar Sandoval 			return true;
18388459642SOmar Sandoval 	}
18488459642SOmar Sandoval 	return false;
18588459642SOmar Sandoval }
18688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
18788459642SOmar Sandoval 
18888459642SOmar Sandoval bool sbitmap_any_bit_clear(const struct sbitmap *sb)
18988459642SOmar Sandoval {
19088459642SOmar Sandoval 	unsigned int i;
19188459642SOmar Sandoval 
19288459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
19388459642SOmar Sandoval 		const struct sbitmap_word *word = &sb->map[i];
19488459642SOmar Sandoval 		unsigned long ret;
19588459642SOmar Sandoval 
19688459642SOmar Sandoval 		ret = find_first_zero_bit(&word->word, word->depth);
19788459642SOmar Sandoval 		if (ret < word->depth)
19888459642SOmar Sandoval 			return true;
19988459642SOmar Sandoval 	}
20088459642SOmar Sandoval 	return false;
20188459642SOmar Sandoval }
20288459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
20388459642SOmar Sandoval 
20488459642SOmar Sandoval unsigned int sbitmap_weight(const struct sbitmap *sb)
20588459642SOmar Sandoval {
20660658e0dSColin Ian King 	unsigned int i, weight = 0;
20788459642SOmar Sandoval 
20888459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
20988459642SOmar Sandoval 		const struct sbitmap_word *word = &sb->map[i];
21088459642SOmar Sandoval 
21188459642SOmar Sandoval 		weight += bitmap_weight(&word->word, word->depth);
21288459642SOmar Sandoval 	}
21388459642SOmar Sandoval 	return weight;
21488459642SOmar Sandoval }
21588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_weight);
21688459642SOmar Sandoval 
21724af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
21824af1ccfSOmar Sandoval {
21924af1ccfSOmar Sandoval 	seq_printf(m, "depth=%u\n", sb->depth);
22024af1ccfSOmar Sandoval 	seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
22124af1ccfSOmar Sandoval 	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
22224af1ccfSOmar Sandoval 	seq_printf(m, "map_nr=%u\n", sb->map_nr);
22324af1ccfSOmar Sandoval }
22424af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show);
22524af1ccfSOmar Sandoval 
22624af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
22724af1ccfSOmar Sandoval {
22824af1ccfSOmar Sandoval 	if ((offset & 0xf) == 0) {
22924af1ccfSOmar Sandoval 		if (offset != 0)
23024af1ccfSOmar Sandoval 			seq_putc(m, '\n');
23124af1ccfSOmar Sandoval 		seq_printf(m, "%08x:", offset);
23224af1ccfSOmar Sandoval 	}
23324af1ccfSOmar Sandoval 	if ((offset & 0x1) == 0)
23424af1ccfSOmar Sandoval 		seq_putc(m, ' ');
23524af1ccfSOmar Sandoval 	seq_printf(m, "%02x", byte);
23624af1ccfSOmar Sandoval }
23724af1ccfSOmar Sandoval 
23824af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
23924af1ccfSOmar Sandoval {
24024af1ccfSOmar Sandoval 	u8 byte = 0;
24124af1ccfSOmar Sandoval 	unsigned int byte_bits = 0;
24224af1ccfSOmar Sandoval 	unsigned int offset = 0;
24324af1ccfSOmar Sandoval 	int i;
24424af1ccfSOmar Sandoval 
24524af1ccfSOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
24624af1ccfSOmar Sandoval 		unsigned long word = READ_ONCE(sb->map[i].word);
24724af1ccfSOmar Sandoval 		unsigned int word_bits = READ_ONCE(sb->map[i].depth);
24824af1ccfSOmar Sandoval 
24924af1ccfSOmar Sandoval 		while (word_bits > 0) {
25024af1ccfSOmar Sandoval 			unsigned int bits = min(8 - byte_bits, word_bits);
25124af1ccfSOmar Sandoval 
25224af1ccfSOmar Sandoval 			byte |= (word & (BIT(bits) - 1)) << byte_bits;
25324af1ccfSOmar Sandoval 			byte_bits += bits;
25424af1ccfSOmar Sandoval 			if (byte_bits == 8) {
25524af1ccfSOmar Sandoval 				emit_byte(m, offset, byte);
25624af1ccfSOmar Sandoval 				byte = 0;
25724af1ccfSOmar Sandoval 				byte_bits = 0;
25824af1ccfSOmar Sandoval 				offset++;
25924af1ccfSOmar Sandoval 			}
26024af1ccfSOmar Sandoval 			word >>= bits;
26124af1ccfSOmar Sandoval 			word_bits -= bits;
26224af1ccfSOmar Sandoval 		}
26324af1ccfSOmar Sandoval 	}
26424af1ccfSOmar Sandoval 	if (byte_bits) {
26524af1ccfSOmar Sandoval 		emit_byte(m, offset, byte);
26624af1ccfSOmar Sandoval 		offset++;
26724af1ccfSOmar Sandoval 	}
26824af1ccfSOmar Sandoval 	if (offset)
26924af1ccfSOmar Sandoval 		seq_putc(m, '\n');
27024af1ccfSOmar Sandoval }
27124af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
27224af1ccfSOmar Sandoval 
27388459642SOmar Sandoval static unsigned int sbq_calc_wake_batch(unsigned int depth)
27488459642SOmar Sandoval {
27588459642SOmar Sandoval 	unsigned int wake_batch;
27688459642SOmar Sandoval 
27788459642SOmar Sandoval 	/*
27888459642SOmar Sandoval 	 * For each batch, we wake up one queue. We need to make sure that our
27988459642SOmar Sandoval 	 * batch size is small enough that the full depth of the bitmap is
28088459642SOmar Sandoval 	 * enough to wake up all of the queues.
28188459642SOmar Sandoval 	 */
28288459642SOmar Sandoval 	wake_batch = SBQ_WAKE_BATCH;
28388459642SOmar Sandoval 	if (wake_batch > depth / SBQ_WAIT_QUEUES)
28488459642SOmar Sandoval 		wake_batch = max(1U, depth / SBQ_WAIT_QUEUES);
28588459642SOmar Sandoval 
28688459642SOmar Sandoval 	return wake_batch;
28788459642SOmar Sandoval }
28888459642SOmar Sandoval 
28988459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
290f4a644dbSOmar Sandoval 			    int shift, bool round_robin, gfp_t flags, int node)
29188459642SOmar Sandoval {
29288459642SOmar Sandoval 	int ret;
29388459642SOmar Sandoval 	int i;
29488459642SOmar Sandoval 
29588459642SOmar Sandoval 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
29688459642SOmar Sandoval 	if (ret)
29788459642SOmar Sandoval 		return ret;
29888459642SOmar Sandoval 
29940aabb67SOmar Sandoval 	sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
30040aabb67SOmar Sandoval 	if (!sbq->alloc_hint) {
30140aabb67SOmar Sandoval 		sbitmap_free(&sbq->sb);
30240aabb67SOmar Sandoval 		return -ENOMEM;
30340aabb67SOmar Sandoval 	}
30440aabb67SOmar Sandoval 
30598d95416SOmar Sandoval 	if (depth && !round_robin) {
30698d95416SOmar Sandoval 		for_each_possible_cpu(i)
30798d95416SOmar Sandoval 			*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
30898d95416SOmar Sandoval 	}
30998d95416SOmar Sandoval 
31088459642SOmar Sandoval 	sbq->wake_batch = sbq_calc_wake_batch(depth);
31188459642SOmar Sandoval 	atomic_set(&sbq->wake_index, 0);
31288459642SOmar Sandoval 
31348e28166SOmar Sandoval 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
31488459642SOmar Sandoval 	if (!sbq->ws) {
31540aabb67SOmar Sandoval 		free_percpu(sbq->alloc_hint);
31688459642SOmar Sandoval 		sbitmap_free(&sbq->sb);
31788459642SOmar Sandoval 		return -ENOMEM;
31888459642SOmar Sandoval 	}
31988459642SOmar Sandoval 
32088459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
32188459642SOmar Sandoval 		init_waitqueue_head(&sbq->ws[i].wait);
32288459642SOmar Sandoval 		atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
32388459642SOmar Sandoval 	}
324f4a644dbSOmar Sandoval 
325f4a644dbSOmar Sandoval 	sbq->round_robin = round_robin;
32688459642SOmar Sandoval 	return 0;
32788459642SOmar Sandoval }
32888459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
32988459642SOmar Sandoval 
33088459642SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
33188459642SOmar Sandoval {
3326c0ca7aeSOmar Sandoval 	unsigned int wake_batch = sbq_calc_wake_batch(depth);
3336c0ca7aeSOmar Sandoval 	int i;
3346c0ca7aeSOmar Sandoval 
3356c0ca7aeSOmar Sandoval 	if (sbq->wake_batch != wake_batch) {
3366c0ca7aeSOmar Sandoval 		WRITE_ONCE(sbq->wake_batch, wake_batch);
3376c0ca7aeSOmar Sandoval 		/*
3386c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbq_wake_up() to ensure that
3396c0ca7aeSOmar Sandoval 		 * the batch size is updated before the wait counts.
3406c0ca7aeSOmar Sandoval 		 */
3416c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
3426c0ca7aeSOmar Sandoval 		for (i = 0; i < SBQ_WAIT_QUEUES; i++)
3436c0ca7aeSOmar Sandoval 			atomic_set(&sbq->ws[i].wait_cnt, 1);
3446c0ca7aeSOmar Sandoval 	}
34588459642SOmar Sandoval 	sbitmap_resize(&sbq->sb, depth);
34688459642SOmar Sandoval }
34788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
34888459642SOmar Sandoval 
349f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq)
35040aabb67SOmar Sandoval {
35105fd095dSOmar Sandoval 	unsigned int hint, depth;
35240aabb67SOmar Sandoval 	int nr;
35340aabb67SOmar Sandoval 
35440aabb67SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
35505fd095dSOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
35605fd095dSOmar Sandoval 	if (unlikely(hint >= depth)) {
35705fd095dSOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
35805fd095dSOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
35905fd095dSOmar Sandoval 	}
360f4a644dbSOmar Sandoval 	nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
36140aabb67SOmar Sandoval 
36240aabb67SOmar Sandoval 	if (nr == -1) {
36340aabb67SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
36440aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
365f4a644dbSOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
36640aabb67SOmar Sandoval 		/* Only update the hint if we used it. */
36740aabb67SOmar Sandoval 		hint = nr + 1;
36805fd095dSOmar Sandoval 		if (hint >= depth - 1)
36940aabb67SOmar Sandoval 			hint = 0;
37040aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
37140aabb67SOmar Sandoval 	}
37240aabb67SOmar Sandoval 
37340aabb67SOmar Sandoval 	return nr;
37440aabb67SOmar Sandoval }
37540aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
37640aabb67SOmar Sandoval 
377c05e6673SOmar Sandoval int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
378c05e6673SOmar Sandoval 				unsigned int shallow_depth)
379c05e6673SOmar Sandoval {
380c05e6673SOmar Sandoval 	unsigned int hint, depth;
381c05e6673SOmar Sandoval 	int nr;
382c05e6673SOmar Sandoval 
383c05e6673SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
384c05e6673SOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
385c05e6673SOmar Sandoval 	if (unlikely(hint >= depth)) {
386c05e6673SOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
387c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
388c05e6673SOmar Sandoval 	}
389c05e6673SOmar Sandoval 	nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
390c05e6673SOmar Sandoval 
391c05e6673SOmar Sandoval 	if (nr == -1) {
392c05e6673SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
393c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
394c05e6673SOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
395c05e6673SOmar Sandoval 		/* Only update the hint if we used it. */
396c05e6673SOmar Sandoval 		hint = nr + 1;
397c05e6673SOmar Sandoval 		if (hint >= depth - 1)
398c05e6673SOmar Sandoval 			hint = 0;
399c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
400c05e6673SOmar Sandoval 	}
401c05e6673SOmar Sandoval 
402c05e6673SOmar Sandoval 	return nr;
403c05e6673SOmar Sandoval }
404c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
405c05e6673SOmar Sandoval 
40688459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
40788459642SOmar Sandoval {
40888459642SOmar Sandoval 	int i, wake_index;
40988459642SOmar Sandoval 
41088459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
41188459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
41288459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
41388459642SOmar Sandoval 
41488459642SOmar Sandoval 		if (waitqueue_active(&ws->wait)) {
41588459642SOmar Sandoval 			int o = atomic_read(&sbq->wake_index);
41688459642SOmar Sandoval 
41788459642SOmar Sandoval 			if (wake_index != o)
41888459642SOmar Sandoval 				atomic_cmpxchg(&sbq->wake_index, o, wake_index);
41988459642SOmar Sandoval 			return ws;
42088459642SOmar Sandoval 		}
42188459642SOmar Sandoval 
42288459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
42388459642SOmar Sandoval 	}
42488459642SOmar Sandoval 
42588459642SOmar Sandoval 	return NULL;
42688459642SOmar Sandoval }
42788459642SOmar Sandoval 
42888459642SOmar Sandoval static void sbq_wake_up(struct sbitmap_queue *sbq)
42988459642SOmar Sandoval {
43088459642SOmar Sandoval 	struct sbq_wait_state *ws;
4316c0ca7aeSOmar Sandoval 	unsigned int wake_batch;
43288459642SOmar Sandoval 	int wait_cnt;
43388459642SOmar Sandoval 
434f66227deSOmar Sandoval 	/*
435f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() to ensure the
436f66227deSOmar Sandoval 	 * proper ordering of clear_bit()/waitqueue_active() in the waker and
437f66227deSOmar Sandoval 	 * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See
438f66227deSOmar Sandoval 	 * the comment on waitqueue_active(). This is __after_atomic because we
439f66227deSOmar Sandoval 	 * just did clear_bit() in the caller.
440f66227deSOmar Sandoval 	 */
441f66227deSOmar Sandoval 	smp_mb__after_atomic();
44288459642SOmar Sandoval 
44388459642SOmar Sandoval 	ws = sbq_wake_ptr(sbq);
44488459642SOmar Sandoval 	if (!ws)
44588459642SOmar Sandoval 		return;
44688459642SOmar Sandoval 
44788459642SOmar Sandoval 	wait_cnt = atomic_dec_return(&ws->wait_cnt);
4486c0ca7aeSOmar Sandoval 	if (wait_cnt <= 0) {
4496c0ca7aeSOmar Sandoval 		wake_batch = READ_ONCE(sbq->wake_batch);
4506c0ca7aeSOmar Sandoval 		/*
4516c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbitmap_queue_resize() to
4526c0ca7aeSOmar Sandoval 		 * ensure that we see the batch size update before the wait
4536c0ca7aeSOmar Sandoval 		 * count is reset.
4546c0ca7aeSOmar Sandoval 		 */
4556c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
4566c0ca7aeSOmar Sandoval 		/*
4576c0ca7aeSOmar Sandoval 		 * If there are concurrent callers to sbq_wake_up(), the last
4586c0ca7aeSOmar Sandoval 		 * one to decrement the wait count below zero will bump it back
4596c0ca7aeSOmar Sandoval 		 * up. If there is a concurrent resize, the count reset will
4606c0ca7aeSOmar Sandoval 		 * either cause the cmpxchg to fail or overwrite after the
4616c0ca7aeSOmar Sandoval 		 * cmpxchg.
4626c0ca7aeSOmar Sandoval 		 */
4636c0ca7aeSOmar Sandoval 		atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch);
46488459642SOmar Sandoval 		sbq_index_atomic_inc(&sbq->wake_index);
46588459642SOmar Sandoval 		wake_up(&ws->wait);
46688459642SOmar Sandoval 	}
46788459642SOmar Sandoval }
46888459642SOmar Sandoval 
46940aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
470f4a644dbSOmar Sandoval 			 unsigned int cpu)
47188459642SOmar Sandoval {
47288459642SOmar Sandoval 	sbitmap_clear_bit(&sbq->sb, nr);
47388459642SOmar Sandoval 	sbq_wake_up(sbq);
4745c64a8dfSOmar Sandoval 	if (likely(!sbq->round_robin && nr < sbq->sb.depth))
47540aabb67SOmar Sandoval 		*per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
47688459642SOmar Sandoval }
47788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
47888459642SOmar Sandoval 
47988459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
48088459642SOmar Sandoval {
48188459642SOmar Sandoval 	int i, wake_index;
48288459642SOmar Sandoval 
48388459642SOmar Sandoval 	/*
484f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() like in
485f66227deSOmar Sandoval 	 * sbq_wake_up().
48688459642SOmar Sandoval 	 */
48788459642SOmar Sandoval 	smp_mb();
48888459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
48988459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
49088459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
49188459642SOmar Sandoval 
49288459642SOmar Sandoval 		if (waitqueue_active(&ws->wait))
49388459642SOmar Sandoval 			wake_up(&ws->wait);
49488459642SOmar Sandoval 
49588459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
49688459642SOmar Sandoval 	}
49788459642SOmar Sandoval }
49888459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
49924af1ccfSOmar Sandoval 
50024af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
50124af1ccfSOmar Sandoval {
50224af1ccfSOmar Sandoval 	bool first;
50324af1ccfSOmar Sandoval 	int i;
50424af1ccfSOmar Sandoval 
50524af1ccfSOmar Sandoval 	sbitmap_show(&sbq->sb, m);
50624af1ccfSOmar Sandoval 
50724af1ccfSOmar Sandoval 	seq_puts(m, "alloc_hint={");
50824af1ccfSOmar Sandoval 	first = true;
50924af1ccfSOmar Sandoval 	for_each_possible_cpu(i) {
51024af1ccfSOmar Sandoval 		if (!first)
51124af1ccfSOmar Sandoval 			seq_puts(m, ", ");
51224af1ccfSOmar Sandoval 		first = false;
51324af1ccfSOmar Sandoval 		seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
51424af1ccfSOmar Sandoval 	}
51524af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
51624af1ccfSOmar Sandoval 
51724af1ccfSOmar Sandoval 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
51824af1ccfSOmar Sandoval 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
51924af1ccfSOmar Sandoval 
52024af1ccfSOmar Sandoval 	seq_puts(m, "ws={\n");
52124af1ccfSOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
52224af1ccfSOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[i];
52324af1ccfSOmar Sandoval 
52424af1ccfSOmar Sandoval 		seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
52524af1ccfSOmar Sandoval 			   atomic_read(&ws->wait_cnt),
52624af1ccfSOmar Sandoval 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
52724af1ccfSOmar Sandoval 	}
52824af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
52924af1ccfSOmar Sandoval 
53024af1ccfSOmar Sandoval 	seq_printf(m, "round_robin=%d\n", sbq->round_robin);
53124af1ccfSOmar Sandoval }
53224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show);
533