xref: /openbmc/linux/lib/sbitmap.c (revision c854ab57)
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 
1034ace53f1SOmar Sandoval 		if (!test_and_set_bit_lock(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 
273a3275539SOmar Sandoval static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq,
274a3275539SOmar Sandoval 					unsigned int depth)
27588459642SOmar Sandoval {
27688459642SOmar Sandoval 	unsigned int wake_batch;
277a3275539SOmar Sandoval 	unsigned int shallow_depth;
27888459642SOmar Sandoval 
27988459642SOmar Sandoval 	/*
28088459642SOmar Sandoval 	 * For each batch, we wake up one queue. We need to make sure that our
281a3275539SOmar Sandoval 	 * batch size is small enough that the full depth of the bitmap,
282a3275539SOmar Sandoval 	 * potentially limited by a shallow depth, is enough to wake up all of
283a3275539SOmar Sandoval 	 * the queues.
284a3275539SOmar Sandoval 	 *
285a3275539SOmar Sandoval 	 * Each full word of the bitmap has bits_per_word bits, and there might
286a3275539SOmar Sandoval 	 * be a partial word. There are depth / bits_per_word full words and
287a3275539SOmar Sandoval 	 * depth % bits_per_word bits left over. In bitwise arithmetic:
288a3275539SOmar Sandoval 	 *
289a3275539SOmar Sandoval 	 * bits_per_word = 1 << shift
290a3275539SOmar Sandoval 	 * depth / bits_per_word = depth >> shift
291a3275539SOmar Sandoval 	 * depth % bits_per_word = depth & ((1 << shift) - 1)
292a3275539SOmar Sandoval 	 *
293a3275539SOmar Sandoval 	 * Each word can be limited to sbq->min_shallow_depth bits.
29488459642SOmar Sandoval 	 */
295a3275539SOmar Sandoval 	shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth);
296a3275539SOmar Sandoval 	depth = ((depth >> sbq->sb.shift) * shallow_depth +
297a3275539SOmar Sandoval 		 min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth));
298a3275539SOmar Sandoval 	wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1,
299a3275539SOmar Sandoval 			     SBQ_WAKE_BATCH);
30088459642SOmar Sandoval 
30188459642SOmar Sandoval 	return wake_batch;
30288459642SOmar Sandoval }
30388459642SOmar Sandoval 
30488459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
305f4a644dbSOmar Sandoval 			    int shift, bool round_robin, gfp_t flags, int node)
30688459642SOmar Sandoval {
30788459642SOmar Sandoval 	int ret;
30888459642SOmar Sandoval 	int i;
30988459642SOmar Sandoval 
31088459642SOmar Sandoval 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
31188459642SOmar Sandoval 	if (ret)
31288459642SOmar Sandoval 		return ret;
31388459642SOmar Sandoval 
31440aabb67SOmar Sandoval 	sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
31540aabb67SOmar Sandoval 	if (!sbq->alloc_hint) {
31640aabb67SOmar Sandoval 		sbitmap_free(&sbq->sb);
31740aabb67SOmar Sandoval 		return -ENOMEM;
31840aabb67SOmar Sandoval 	}
31940aabb67SOmar Sandoval 
32098d95416SOmar Sandoval 	if (depth && !round_robin) {
32198d95416SOmar Sandoval 		for_each_possible_cpu(i)
32298d95416SOmar Sandoval 			*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
32398d95416SOmar Sandoval 	}
32498d95416SOmar Sandoval 
325a3275539SOmar Sandoval 	sbq->min_shallow_depth = UINT_MAX;
326a3275539SOmar Sandoval 	sbq->wake_batch = sbq_calc_wake_batch(sbq, depth);
32788459642SOmar Sandoval 	atomic_set(&sbq->wake_index, 0);
32888459642SOmar Sandoval 
32948e28166SOmar Sandoval 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
33088459642SOmar Sandoval 	if (!sbq->ws) {
33140aabb67SOmar Sandoval 		free_percpu(sbq->alloc_hint);
33288459642SOmar Sandoval 		sbitmap_free(&sbq->sb);
33388459642SOmar Sandoval 		return -ENOMEM;
33488459642SOmar Sandoval 	}
33588459642SOmar Sandoval 
33688459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
33788459642SOmar Sandoval 		init_waitqueue_head(&sbq->ws[i].wait);
33888459642SOmar Sandoval 		atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
33988459642SOmar Sandoval 	}
340f4a644dbSOmar Sandoval 
341f4a644dbSOmar Sandoval 	sbq->round_robin = round_robin;
34288459642SOmar Sandoval 	return 0;
34388459642SOmar Sandoval }
34488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
34588459642SOmar Sandoval 
346a3275539SOmar Sandoval static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq,
347a3275539SOmar Sandoval 					    unsigned int depth)
34888459642SOmar Sandoval {
349a3275539SOmar Sandoval 	unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth);
3506c0ca7aeSOmar Sandoval 	int i;
3516c0ca7aeSOmar Sandoval 
3526c0ca7aeSOmar Sandoval 	if (sbq->wake_batch != wake_batch) {
3536c0ca7aeSOmar Sandoval 		WRITE_ONCE(sbq->wake_batch, wake_batch);
3546c0ca7aeSOmar Sandoval 		/*
3556c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbq_wake_up() to ensure that
3566c0ca7aeSOmar Sandoval 		 * the batch size is updated before the wait counts.
3576c0ca7aeSOmar Sandoval 		 */
3586c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
3596c0ca7aeSOmar Sandoval 		for (i = 0; i < SBQ_WAIT_QUEUES; i++)
3606c0ca7aeSOmar Sandoval 			atomic_set(&sbq->ws[i].wait_cnt, 1);
3616c0ca7aeSOmar Sandoval 	}
362a3275539SOmar Sandoval }
363a3275539SOmar Sandoval 
364a3275539SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
365a3275539SOmar Sandoval {
366a3275539SOmar Sandoval 	sbitmap_queue_update_wake_batch(sbq, depth);
36788459642SOmar Sandoval 	sbitmap_resize(&sbq->sb, depth);
36888459642SOmar Sandoval }
36988459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
37088459642SOmar Sandoval 
371f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq)
37240aabb67SOmar Sandoval {
37305fd095dSOmar Sandoval 	unsigned int hint, depth;
37440aabb67SOmar Sandoval 	int nr;
37540aabb67SOmar Sandoval 
37640aabb67SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
37705fd095dSOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
37805fd095dSOmar Sandoval 	if (unlikely(hint >= depth)) {
37905fd095dSOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
38005fd095dSOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
38105fd095dSOmar Sandoval 	}
382f4a644dbSOmar Sandoval 	nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
38340aabb67SOmar Sandoval 
38440aabb67SOmar Sandoval 	if (nr == -1) {
38540aabb67SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
38640aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
387f4a644dbSOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
38840aabb67SOmar Sandoval 		/* Only update the hint if we used it. */
38940aabb67SOmar Sandoval 		hint = nr + 1;
39005fd095dSOmar Sandoval 		if (hint >= depth - 1)
39140aabb67SOmar Sandoval 			hint = 0;
39240aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
39340aabb67SOmar Sandoval 	}
39440aabb67SOmar Sandoval 
39540aabb67SOmar Sandoval 	return nr;
39640aabb67SOmar Sandoval }
39740aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
39840aabb67SOmar Sandoval 
399c05e6673SOmar Sandoval int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
400c05e6673SOmar Sandoval 				unsigned int shallow_depth)
401c05e6673SOmar Sandoval {
402c05e6673SOmar Sandoval 	unsigned int hint, depth;
403c05e6673SOmar Sandoval 	int nr;
404c05e6673SOmar Sandoval 
40561445b56SOmar Sandoval 	WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth);
40661445b56SOmar Sandoval 
407c05e6673SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
408c05e6673SOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
409c05e6673SOmar Sandoval 	if (unlikely(hint >= depth)) {
410c05e6673SOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
411c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
412c05e6673SOmar Sandoval 	}
413c05e6673SOmar Sandoval 	nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth);
414c05e6673SOmar Sandoval 
415c05e6673SOmar Sandoval 	if (nr == -1) {
416c05e6673SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
417c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
418c05e6673SOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
419c05e6673SOmar Sandoval 		/* Only update the hint if we used it. */
420c05e6673SOmar Sandoval 		hint = nr + 1;
421c05e6673SOmar Sandoval 		if (hint >= depth - 1)
422c05e6673SOmar Sandoval 			hint = 0;
423c05e6673SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
424c05e6673SOmar Sandoval 	}
425c05e6673SOmar Sandoval 
426c05e6673SOmar Sandoval 	return nr;
427c05e6673SOmar Sandoval }
428c05e6673SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow);
429c05e6673SOmar Sandoval 
430a3275539SOmar Sandoval void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
431a3275539SOmar Sandoval 				     unsigned int min_shallow_depth)
432a3275539SOmar Sandoval {
433a3275539SOmar Sandoval 	sbq->min_shallow_depth = min_shallow_depth;
434a3275539SOmar Sandoval 	sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth);
435a3275539SOmar Sandoval }
436a3275539SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth);
437a3275539SOmar Sandoval 
43888459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
43988459642SOmar Sandoval {
44088459642SOmar Sandoval 	int i, wake_index;
44188459642SOmar Sandoval 
44288459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
44388459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
44488459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
44588459642SOmar Sandoval 
44688459642SOmar Sandoval 		if (waitqueue_active(&ws->wait)) {
44788459642SOmar Sandoval 			int o = atomic_read(&sbq->wake_index);
44888459642SOmar Sandoval 
44988459642SOmar Sandoval 			if (wake_index != o)
45088459642SOmar Sandoval 				atomic_cmpxchg(&sbq->wake_index, o, wake_index);
45188459642SOmar Sandoval 			return ws;
45288459642SOmar Sandoval 		}
45388459642SOmar Sandoval 
45488459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
45588459642SOmar Sandoval 	}
45688459642SOmar Sandoval 
45788459642SOmar Sandoval 	return NULL;
45888459642SOmar Sandoval }
45988459642SOmar Sandoval 
460c854ab57SJens Axboe static bool __sbq_wake_up(struct sbitmap_queue *sbq)
46188459642SOmar Sandoval {
46288459642SOmar Sandoval 	struct sbq_wait_state *ws;
4636c0ca7aeSOmar Sandoval 	unsigned int wake_batch;
46488459642SOmar Sandoval 	int wait_cnt;
46588459642SOmar Sandoval 
466f66227deSOmar Sandoval 	/*
467f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() to ensure the
468f66227deSOmar Sandoval 	 * proper ordering of clear_bit()/waitqueue_active() in the waker and
4694ace53f1SOmar Sandoval 	 * test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the
4704ace53f1SOmar Sandoval 	 * waiter. See the comment on waitqueue_active(). This is __after_atomic
4714ace53f1SOmar Sandoval 	 * because we just did clear_bit_unlock() in the caller.
472f66227deSOmar Sandoval 	 */
473f66227deSOmar Sandoval 	smp_mb__after_atomic();
47488459642SOmar Sandoval 
47588459642SOmar Sandoval 	ws = sbq_wake_ptr(sbq);
47688459642SOmar Sandoval 	if (!ws)
477c854ab57SJens Axboe 		return false;
47888459642SOmar Sandoval 
47988459642SOmar Sandoval 	wait_cnt = atomic_dec_return(&ws->wait_cnt);
4806c0ca7aeSOmar Sandoval 	if (wait_cnt <= 0) {
481c854ab57SJens Axboe 		int ret;
482c854ab57SJens Axboe 
4836c0ca7aeSOmar Sandoval 		wake_batch = READ_ONCE(sbq->wake_batch);
484c854ab57SJens Axboe 
4856c0ca7aeSOmar Sandoval 		/*
4866c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbitmap_queue_resize() to
4876c0ca7aeSOmar Sandoval 		 * ensure that we see the batch size update before the wait
4886c0ca7aeSOmar Sandoval 		 * count is reset.
4896c0ca7aeSOmar Sandoval 		 */
4906c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
491c854ab57SJens Axboe 
4926c0ca7aeSOmar Sandoval 		/*
493c854ab57SJens Axboe 		 * For concurrent callers of this, the one that failed the
494c854ab57SJens Axboe 		 * atomic_cmpxhcg() race should call this function again
495c854ab57SJens Axboe 		 * to wakeup a new batch on a different 'ws'.
4966c0ca7aeSOmar Sandoval 		 */
497c854ab57SJens Axboe 		ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch);
498c854ab57SJens Axboe 		if (ret == wait_cnt) {
49988459642SOmar Sandoval 			sbq_index_atomic_inc(&sbq->wake_index);
5004e5dff41SJens Axboe 			wake_up_nr(&ws->wait, wake_batch);
501c854ab57SJens Axboe 			return false;
50288459642SOmar Sandoval 		}
503c854ab57SJens Axboe 
504c854ab57SJens Axboe 		return true;
505c854ab57SJens Axboe 	}
506c854ab57SJens Axboe 
507c854ab57SJens Axboe 	return false;
508c854ab57SJens Axboe }
509c854ab57SJens Axboe 
510c854ab57SJens Axboe static void sbq_wake_up(struct sbitmap_queue *sbq)
511c854ab57SJens Axboe {
512c854ab57SJens Axboe 	while (__sbq_wake_up(sbq))
513c854ab57SJens Axboe 		;
51488459642SOmar Sandoval }
51588459642SOmar Sandoval 
51640aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
517f4a644dbSOmar Sandoval 			 unsigned int cpu)
51888459642SOmar Sandoval {
5194ace53f1SOmar Sandoval 	sbitmap_clear_bit_unlock(&sbq->sb, nr);
52088459642SOmar Sandoval 	sbq_wake_up(sbq);
5215c64a8dfSOmar Sandoval 	if (likely(!sbq->round_robin && nr < sbq->sb.depth))
52240aabb67SOmar Sandoval 		*per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
52388459642SOmar Sandoval }
52488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
52588459642SOmar Sandoval 
52688459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
52788459642SOmar Sandoval {
52888459642SOmar Sandoval 	int i, wake_index;
52988459642SOmar Sandoval 
53088459642SOmar Sandoval 	/*
531f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() like in
532f66227deSOmar Sandoval 	 * sbq_wake_up().
53388459642SOmar Sandoval 	 */
53488459642SOmar Sandoval 	smp_mb();
53588459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
53688459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
53788459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
53888459642SOmar Sandoval 
53988459642SOmar Sandoval 		if (waitqueue_active(&ws->wait))
54088459642SOmar Sandoval 			wake_up(&ws->wait);
54188459642SOmar Sandoval 
54288459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
54388459642SOmar Sandoval 	}
54488459642SOmar Sandoval }
54588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
54624af1ccfSOmar Sandoval 
54724af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
54824af1ccfSOmar Sandoval {
54924af1ccfSOmar Sandoval 	bool first;
55024af1ccfSOmar Sandoval 	int i;
55124af1ccfSOmar Sandoval 
55224af1ccfSOmar Sandoval 	sbitmap_show(&sbq->sb, m);
55324af1ccfSOmar Sandoval 
55424af1ccfSOmar Sandoval 	seq_puts(m, "alloc_hint={");
55524af1ccfSOmar Sandoval 	first = true;
55624af1ccfSOmar Sandoval 	for_each_possible_cpu(i) {
55724af1ccfSOmar Sandoval 		if (!first)
55824af1ccfSOmar Sandoval 			seq_puts(m, ", ");
55924af1ccfSOmar Sandoval 		first = false;
56024af1ccfSOmar Sandoval 		seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
56124af1ccfSOmar Sandoval 	}
56224af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
56324af1ccfSOmar Sandoval 
56424af1ccfSOmar Sandoval 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
56524af1ccfSOmar Sandoval 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
56624af1ccfSOmar Sandoval 
56724af1ccfSOmar Sandoval 	seq_puts(m, "ws={\n");
56824af1ccfSOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
56924af1ccfSOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[i];
57024af1ccfSOmar Sandoval 
57124af1ccfSOmar Sandoval 		seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
57224af1ccfSOmar Sandoval 			   atomic_read(&ws->wait_cnt),
57324af1ccfSOmar Sandoval 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
57424af1ccfSOmar Sandoval 	}
57524af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
57624af1ccfSOmar Sandoval 
57724af1ccfSOmar Sandoval 	seq_printf(m, "round_robin=%d\n", sbq->round_robin);
578a3275539SOmar Sandoval 	seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth);
57924af1ccfSOmar Sandoval }
58024af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show);
581