xref: /openbmc/linux/lib/sbitmap.c (revision 24af1ccf)
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 
1898d95416SOmar Sandoval #include <linux/random.h>
1988459642SOmar Sandoval #include <linux/sbitmap.h>
2024af1ccfSOmar Sandoval #include <linux/seq_file.h>
2188459642SOmar Sandoval 
2288459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
2388459642SOmar Sandoval 		      gfp_t flags, int node)
2488459642SOmar Sandoval {
2588459642SOmar Sandoval 	unsigned int bits_per_word;
2688459642SOmar Sandoval 	unsigned int i;
2788459642SOmar Sandoval 
2888459642SOmar Sandoval 	if (shift < 0) {
2988459642SOmar Sandoval 		shift = ilog2(BITS_PER_LONG);
3088459642SOmar Sandoval 		/*
3188459642SOmar Sandoval 		 * If the bitmap is small, shrink the number of bits per word so
3288459642SOmar Sandoval 		 * we spread over a few cachelines, at least. If less than 4
3388459642SOmar Sandoval 		 * bits, just forget about it, it's not going to work optimally
3488459642SOmar Sandoval 		 * anyway.
3588459642SOmar Sandoval 		 */
3688459642SOmar Sandoval 		if (depth >= 4) {
3788459642SOmar Sandoval 			while ((4U << shift) > depth)
3888459642SOmar Sandoval 				shift--;
3988459642SOmar Sandoval 		}
4088459642SOmar Sandoval 	}
4188459642SOmar Sandoval 	bits_per_word = 1U << shift;
4288459642SOmar Sandoval 	if (bits_per_word > BITS_PER_LONG)
4388459642SOmar Sandoval 		return -EINVAL;
4488459642SOmar Sandoval 
4588459642SOmar Sandoval 	sb->shift = shift;
4688459642SOmar Sandoval 	sb->depth = depth;
4788459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
4888459642SOmar Sandoval 
4988459642SOmar Sandoval 	if (depth == 0) {
5088459642SOmar Sandoval 		sb->map = NULL;
5188459642SOmar Sandoval 		return 0;
5288459642SOmar Sandoval 	}
5388459642SOmar Sandoval 
5488459642SOmar Sandoval 	sb->map = kzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
5588459642SOmar Sandoval 	if (!sb->map)
5688459642SOmar Sandoval 		return -ENOMEM;
5788459642SOmar Sandoval 
5888459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
5988459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
6088459642SOmar Sandoval 		depth -= sb->map[i].depth;
6188459642SOmar Sandoval 	}
6288459642SOmar Sandoval 	return 0;
6388459642SOmar Sandoval }
6488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_init_node);
6588459642SOmar Sandoval 
6688459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth)
6788459642SOmar Sandoval {
6888459642SOmar Sandoval 	unsigned int bits_per_word = 1U << sb->shift;
6988459642SOmar Sandoval 	unsigned int i;
7088459642SOmar Sandoval 
7188459642SOmar Sandoval 	sb->depth = depth;
7288459642SOmar Sandoval 	sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word);
7388459642SOmar Sandoval 
7488459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
7588459642SOmar Sandoval 		sb->map[i].depth = min(depth, bits_per_word);
7688459642SOmar Sandoval 		depth -= sb->map[i].depth;
7788459642SOmar Sandoval 	}
7888459642SOmar Sandoval }
7988459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_resize);
8088459642SOmar Sandoval 
8188459642SOmar Sandoval static int __sbitmap_get_word(struct sbitmap_word *word, unsigned int hint,
8288459642SOmar Sandoval 			      bool wrap)
8388459642SOmar Sandoval {
8488459642SOmar Sandoval 	unsigned int orig_hint = hint;
8588459642SOmar Sandoval 	int nr;
8688459642SOmar Sandoval 
8788459642SOmar Sandoval 	while (1) {
8888459642SOmar Sandoval 		nr = find_next_zero_bit(&word->word, word->depth, hint);
8988459642SOmar Sandoval 		if (unlikely(nr >= word->depth)) {
9088459642SOmar Sandoval 			/*
9188459642SOmar Sandoval 			 * We started with an offset, and we didn't reset the
9288459642SOmar Sandoval 			 * offset to 0 in a failure case, so start from 0 to
9388459642SOmar Sandoval 			 * exhaust the map.
9488459642SOmar Sandoval 			 */
9588459642SOmar Sandoval 			if (orig_hint && hint && wrap) {
9688459642SOmar Sandoval 				hint = orig_hint = 0;
9788459642SOmar Sandoval 				continue;
9888459642SOmar Sandoval 			}
9988459642SOmar Sandoval 			return -1;
10088459642SOmar Sandoval 		}
10188459642SOmar Sandoval 
10288459642SOmar Sandoval 		if (!test_and_set_bit(nr, &word->word))
10388459642SOmar Sandoval 			break;
10488459642SOmar Sandoval 
10588459642SOmar Sandoval 		hint = nr + 1;
10688459642SOmar Sandoval 		if (hint >= word->depth - 1)
10788459642SOmar Sandoval 			hint = 0;
10888459642SOmar Sandoval 	}
10988459642SOmar Sandoval 
11088459642SOmar Sandoval 	return nr;
11188459642SOmar Sandoval }
11288459642SOmar Sandoval 
11388459642SOmar Sandoval int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin)
11488459642SOmar Sandoval {
11588459642SOmar Sandoval 	unsigned int i, index;
11688459642SOmar Sandoval 	int nr = -1;
11788459642SOmar Sandoval 
11888459642SOmar Sandoval 	index = SB_NR_TO_INDEX(sb, alloc_hint);
11988459642SOmar Sandoval 
12088459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
12188459642SOmar Sandoval 		nr = __sbitmap_get_word(&sb->map[index],
12288459642SOmar Sandoval 					SB_NR_TO_BIT(sb, alloc_hint),
12388459642SOmar Sandoval 					!round_robin);
12488459642SOmar Sandoval 		if (nr != -1) {
12588459642SOmar Sandoval 			nr += index << sb->shift;
12688459642SOmar Sandoval 			break;
12788459642SOmar Sandoval 		}
12888459642SOmar Sandoval 
12988459642SOmar Sandoval 		/* Jump to next index. */
13088459642SOmar Sandoval 		index++;
13188459642SOmar Sandoval 		alloc_hint = index << sb->shift;
13288459642SOmar Sandoval 
13388459642SOmar Sandoval 		if (index >= sb->map_nr) {
13488459642SOmar Sandoval 			index = 0;
13588459642SOmar Sandoval 			alloc_hint = 0;
13688459642SOmar Sandoval 		}
13788459642SOmar Sandoval 	}
13888459642SOmar Sandoval 
13988459642SOmar Sandoval 	return nr;
14088459642SOmar Sandoval }
14188459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get);
14288459642SOmar Sandoval 
14388459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb)
14488459642SOmar Sandoval {
14588459642SOmar Sandoval 	unsigned int i;
14688459642SOmar Sandoval 
14788459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
14888459642SOmar Sandoval 		if (sb->map[i].word)
14988459642SOmar Sandoval 			return true;
15088459642SOmar Sandoval 	}
15188459642SOmar Sandoval 	return false;
15288459642SOmar Sandoval }
15388459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set);
15488459642SOmar Sandoval 
15588459642SOmar Sandoval bool sbitmap_any_bit_clear(const struct sbitmap *sb)
15688459642SOmar Sandoval {
15788459642SOmar Sandoval 	unsigned int i;
15888459642SOmar Sandoval 
15988459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
16088459642SOmar Sandoval 		const struct sbitmap_word *word = &sb->map[i];
16188459642SOmar Sandoval 		unsigned long ret;
16288459642SOmar Sandoval 
16388459642SOmar Sandoval 		ret = find_first_zero_bit(&word->word, word->depth);
16488459642SOmar Sandoval 		if (ret < word->depth)
16588459642SOmar Sandoval 			return true;
16688459642SOmar Sandoval 	}
16788459642SOmar Sandoval 	return false;
16888459642SOmar Sandoval }
16988459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear);
17088459642SOmar Sandoval 
17188459642SOmar Sandoval unsigned int sbitmap_weight(const struct sbitmap *sb)
17288459642SOmar Sandoval {
17360658e0dSColin Ian King 	unsigned int i, weight = 0;
17488459642SOmar Sandoval 
17588459642SOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
17688459642SOmar Sandoval 		const struct sbitmap_word *word = &sb->map[i];
17788459642SOmar Sandoval 
17888459642SOmar Sandoval 		weight += bitmap_weight(&word->word, word->depth);
17988459642SOmar Sandoval 	}
18088459642SOmar Sandoval 	return weight;
18188459642SOmar Sandoval }
18288459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_weight);
18388459642SOmar Sandoval 
18424af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m)
18524af1ccfSOmar Sandoval {
18624af1ccfSOmar Sandoval 	seq_printf(m, "depth=%u\n", sb->depth);
18724af1ccfSOmar Sandoval 	seq_printf(m, "busy=%u\n", sbitmap_weight(sb));
18824af1ccfSOmar Sandoval 	seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift);
18924af1ccfSOmar Sandoval 	seq_printf(m, "map_nr=%u\n", sb->map_nr);
19024af1ccfSOmar Sandoval }
19124af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show);
19224af1ccfSOmar Sandoval 
19324af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte)
19424af1ccfSOmar Sandoval {
19524af1ccfSOmar Sandoval 	if ((offset & 0xf) == 0) {
19624af1ccfSOmar Sandoval 		if (offset != 0)
19724af1ccfSOmar Sandoval 			seq_putc(m, '\n');
19824af1ccfSOmar Sandoval 		seq_printf(m, "%08x:", offset);
19924af1ccfSOmar Sandoval 	}
20024af1ccfSOmar Sandoval 	if ((offset & 0x1) == 0)
20124af1ccfSOmar Sandoval 		seq_putc(m, ' ');
20224af1ccfSOmar Sandoval 	seq_printf(m, "%02x", byte);
20324af1ccfSOmar Sandoval }
20424af1ccfSOmar Sandoval 
20524af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m)
20624af1ccfSOmar Sandoval {
20724af1ccfSOmar Sandoval 	u8 byte = 0;
20824af1ccfSOmar Sandoval 	unsigned int byte_bits = 0;
20924af1ccfSOmar Sandoval 	unsigned int offset = 0;
21024af1ccfSOmar Sandoval 	int i;
21124af1ccfSOmar Sandoval 
21224af1ccfSOmar Sandoval 	for (i = 0; i < sb->map_nr; i++) {
21324af1ccfSOmar Sandoval 		unsigned long word = READ_ONCE(sb->map[i].word);
21424af1ccfSOmar Sandoval 		unsigned int word_bits = READ_ONCE(sb->map[i].depth);
21524af1ccfSOmar Sandoval 
21624af1ccfSOmar Sandoval 		while (word_bits > 0) {
21724af1ccfSOmar Sandoval 			unsigned int bits = min(8 - byte_bits, word_bits);
21824af1ccfSOmar Sandoval 
21924af1ccfSOmar Sandoval 			byte |= (word & (BIT(bits) - 1)) << byte_bits;
22024af1ccfSOmar Sandoval 			byte_bits += bits;
22124af1ccfSOmar Sandoval 			if (byte_bits == 8) {
22224af1ccfSOmar Sandoval 				emit_byte(m, offset, byte);
22324af1ccfSOmar Sandoval 				byte = 0;
22424af1ccfSOmar Sandoval 				byte_bits = 0;
22524af1ccfSOmar Sandoval 				offset++;
22624af1ccfSOmar Sandoval 			}
22724af1ccfSOmar Sandoval 			word >>= bits;
22824af1ccfSOmar Sandoval 			word_bits -= bits;
22924af1ccfSOmar Sandoval 		}
23024af1ccfSOmar Sandoval 	}
23124af1ccfSOmar Sandoval 	if (byte_bits) {
23224af1ccfSOmar Sandoval 		emit_byte(m, offset, byte);
23324af1ccfSOmar Sandoval 		offset++;
23424af1ccfSOmar Sandoval 	}
23524af1ccfSOmar Sandoval 	if (offset)
23624af1ccfSOmar Sandoval 		seq_putc(m, '\n');
23724af1ccfSOmar Sandoval }
23824af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show);
23924af1ccfSOmar Sandoval 
24088459642SOmar Sandoval static unsigned int sbq_calc_wake_batch(unsigned int depth)
24188459642SOmar Sandoval {
24288459642SOmar Sandoval 	unsigned int wake_batch;
24388459642SOmar Sandoval 
24488459642SOmar Sandoval 	/*
24588459642SOmar Sandoval 	 * For each batch, we wake up one queue. We need to make sure that our
24688459642SOmar Sandoval 	 * batch size is small enough that the full depth of the bitmap is
24788459642SOmar Sandoval 	 * enough to wake up all of the queues.
24888459642SOmar Sandoval 	 */
24988459642SOmar Sandoval 	wake_batch = SBQ_WAKE_BATCH;
25088459642SOmar Sandoval 	if (wake_batch > depth / SBQ_WAIT_QUEUES)
25188459642SOmar Sandoval 		wake_batch = max(1U, depth / SBQ_WAIT_QUEUES);
25288459642SOmar Sandoval 
25388459642SOmar Sandoval 	return wake_batch;
25488459642SOmar Sandoval }
25588459642SOmar Sandoval 
25688459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
257f4a644dbSOmar Sandoval 			    int shift, bool round_robin, gfp_t flags, int node)
25888459642SOmar Sandoval {
25988459642SOmar Sandoval 	int ret;
26088459642SOmar Sandoval 	int i;
26188459642SOmar Sandoval 
26288459642SOmar Sandoval 	ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node);
26388459642SOmar Sandoval 	if (ret)
26488459642SOmar Sandoval 		return ret;
26588459642SOmar Sandoval 
26640aabb67SOmar Sandoval 	sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags);
26740aabb67SOmar Sandoval 	if (!sbq->alloc_hint) {
26840aabb67SOmar Sandoval 		sbitmap_free(&sbq->sb);
26940aabb67SOmar Sandoval 		return -ENOMEM;
27040aabb67SOmar Sandoval 	}
27140aabb67SOmar Sandoval 
27298d95416SOmar Sandoval 	if (depth && !round_robin) {
27398d95416SOmar Sandoval 		for_each_possible_cpu(i)
27498d95416SOmar Sandoval 			*per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth;
27598d95416SOmar Sandoval 	}
27698d95416SOmar Sandoval 
27788459642SOmar Sandoval 	sbq->wake_batch = sbq_calc_wake_batch(depth);
27888459642SOmar Sandoval 	atomic_set(&sbq->wake_index, 0);
27988459642SOmar Sandoval 
28048e28166SOmar Sandoval 	sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node);
28188459642SOmar Sandoval 	if (!sbq->ws) {
28240aabb67SOmar Sandoval 		free_percpu(sbq->alloc_hint);
28388459642SOmar Sandoval 		sbitmap_free(&sbq->sb);
28488459642SOmar Sandoval 		return -ENOMEM;
28588459642SOmar Sandoval 	}
28688459642SOmar Sandoval 
28788459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
28888459642SOmar Sandoval 		init_waitqueue_head(&sbq->ws[i].wait);
28988459642SOmar Sandoval 		atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
29088459642SOmar Sandoval 	}
291f4a644dbSOmar Sandoval 
292f4a644dbSOmar Sandoval 	sbq->round_robin = round_robin;
29388459642SOmar Sandoval 	return 0;
29488459642SOmar Sandoval }
29588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node);
29688459642SOmar Sandoval 
29788459642SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth)
29888459642SOmar Sandoval {
2996c0ca7aeSOmar Sandoval 	unsigned int wake_batch = sbq_calc_wake_batch(depth);
3006c0ca7aeSOmar Sandoval 	int i;
3016c0ca7aeSOmar Sandoval 
3026c0ca7aeSOmar Sandoval 	if (sbq->wake_batch != wake_batch) {
3036c0ca7aeSOmar Sandoval 		WRITE_ONCE(sbq->wake_batch, wake_batch);
3046c0ca7aeSOmar Sandoval 		/*
3056c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbq_wake_up() to ensure that
3066c0ca7aeSOmar Sandoval 		 * the batch size is updated before the wait counts.
3076c0ca7aeSOmar Sandoval 		 */
3086c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
3096c0ca7aeSOmar Sandoval 		for (i = 0; i < SBQ_WAIT_QUEUES; i++)
3106c0ca7aeSOmar Sandoval 			atomic_set(&sbq->ws[i].wait_cnt, 1);
3116c0ca7aeSOmar Sandoval 	}
31288459642SOmar Sandoval 	sbitmap_resize(&sbq->sb, depth);
31388459642SOmar Sandoval }
31488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize);
31588459642SOmar Sandoval 
316f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq)
31740aabb67SOmar Sandoval {
31805fd095dSOmar Sandoval 	unsigned int hint, depth;
31940aabb67SOmar Sandoval 	int nr;
32040aabb67SOmar Sandoval 
32140aabb67SOmar Sandoval 	hint = this_cpu_read(*sbq->alloc_hint);
32205fd095dSOmar Sandoval 	depth = READ_ONCE(sbq->sb.depth);
32305fd095dSOmar Sandoval 	if (unlikely(hint >= depth)) {
32405fd095dSOmar Sandoval 		hint = depth ? prandom_u32() % depth : 0;
32505fd095dSOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
32605fd095dSOmar Sandoval 	}
327f4a644dbSOmar Sandoval 	nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin);
32840aabb67SOmar Sandoval 
32940aabb67SOmar Sandoval 	if (nr == -1) {
33040aabb67SOmar Sandoval 		/* If the map is full, a hint won't do us much good. */
33140aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, 0);
332f4a644dbSOmar Sandoval 	} else if (nr == hint || unlikely(sbq->round_robin)) {
33340aabb67SOmar Sandoval 		/* Only update the hint if we used it. */
33440aabb67SOmar Sandoval 		hint = nr + 1;
33505fd095dSOmar Sandoval 		if (hint >= depth - 1)
33640aabb67SOmar Sandoval 			hint = 0;
33740aabb67SOmar Sandoval 		this_cpu_write(*sbq->alloc_hint, hint);
33840aabb67SOmar Sandoval 	}
33940aabb67SOmar Sandoval 
34040aabb67SOmar Sandoval 	return nr;
34140aabb67SOmar Sandoval }
34240aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get);
34340aabb67SOmar Sandoval 
34488459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
34588459642SOmar Sandoval {
34688459642SOmar Sandoval 	int i, wake_index;
34788459642SOmar Sandoval 
34888459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
34988459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
35088459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
35188459642SOmar Sandoval 
35288459642SOmar Sandoval 		if (waitqueue_active(&ws->wait)) {
35388459642SOmar Sandoval 			int o = atomic_read(&sbq->wake_index);
35488459642SOmar Sandoval 
35588459642SOmar Sandoval 			if (wake_index != o)
35688459642SOmar Sandoval 				atomic_cmpxchg(&sbq->wake_index, o, wake_index);
35788459642SOmar Sandoval 			return ws;
35888459642SOmar Sandoval 		}
35988459642SOmar Sandoval 
36088459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
36188459642SOmar Sandoval 	}
36288459642SOmar Sandoval 
36388459642SOmar Sandoval 	return NULL;
36488459642SOmar Sandoval }
36588459642SOmar Sandoval 
36688459642SOmar Sandoval static void sbq_wake_up(struct sbitmap_queue *sbq)
36788459642SOmar Sandoval {
36888459642SOmar Sandoval 	struct sbq_wait_state *ws;
3696c0ca7aeSOmar Sandoval 	unsigned int wake_batch;
37088459642SOmar Sandoval 	int wait_cnt;
37188459642SOmar Sandoval 
372f66227deSOmar Sandoval 	/*
373f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() to ensure the
374f66227deSOmar Sandoval 	 * proper ordering of clear_bit()/waitqueue_active() in the waker and
375f66227deSOmar Sandoval 	 * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See
376f66227deSOmar Sandoval 	 * the comment on waitqueue_active(). This is __after_atomic because we
377f66227deSOmar Sandoval 	 * just did clear_bit() in the caller.
378f66227deSOmar Sandoval 	 */
379f66227deSOmar Sandoval 	smp_mb__after_atomic();
38088459642SOmar Sandoval 
38188459642SOmar Sandoval 	ws = sbq_wake_ptr(sbq);
38288459642SOmar Sandoval 	if (!ws)
38388459642SOmar Sandoval 		return;
38488459642SOmar Sandoval 
38588459642SOmar Sandoval 	wait_cnt = atomic_dec_return(&ws->wait_cnt);
3866c0ca7aeSOmar Sandoval 	if (wait_cnt <= 0) {
3876c0ca7aeSOmar Sandoval 		wake_batch = READ_ONCE(sbq->wake_batch);
3886c0ca7aeSOmar Sandoval 		/*
3896c0ca7aeSOmar Sandoval 		 * Pairs with the memory barrier in sbitmap_queue_resize() to
3906c0ca7aeSOmar Sandoval 		 * ensure that we see the batch size update before the wait
3916c0ca7aeSOmar Sandoval 		 * count is reset.
3926c0ca7aeSOmar Sandoval 		 */
3936c0ca7aeSOmar Sandoval 		smp_mb__before_atomic();
3946c0ca7aeSOmar Sandoval 		/*
3956c0ca7aeSOmar Sandoval 		 * If there are concurrent callers to sbq_wake_up(), the last
3966c0ca7aeSOmar Sandoval 		 * one to decrement the wait count below zero will bump it back
3976c0ca7aeSOmar Sandoval 		 * up. If there is a concurrent resize, the count reset will
3986c0ca7aeSOmar Sandoval 		 * either cause the cmpxchg to fail or overwrite after the
3996c0ca7aeSOmar Sandoval 		 * cmpxchg.
4006c0ca7aeSOmar Sandoval 		 */
4016c0ca7aeSOmar Sandoval 		atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch);
40288459642SOmar Sandoval 		sbq_index_atomic_inc(&sbq->wake_index);
40388459642SOmar Sandoval 		wake_up(&ws->wait);
40488459642SOmar Sandoval 	}
40588459642SOmar Sandoval }
40688459642SOmar Sandoval 
40740aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
408f4a644dbSOmar Sandoval 			 unsigned int cpu)
40988459642SOmar Sandoval {
41088459642SOmar Sandoval 	sbitmap_clear_bit(&sbq->sb, nr);
41188459642SOmar Sandoval 	sbq_wake_up(sbq);
4125c64a8dfSOmar Sandoval 	if (likely(!sbq->round_robin && nr < sbq->sb.depth))
41340aabb67SOmar Sandoval 		*per_cpu_ptr(sbq->alloc_hint, cpu) = nr;
41488459642SOmar Sandoval }
41588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
41688459642SOmar Sandoval 
41788459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq)
41888459642SOmar Sandoval {
41988459642SOmar Sandoval 	int i, wake_index;
42088459642SOmar Sandoval 
42188459642SOmar Sandoval 	/*
422f66227deSOmar Sandoval 	 * Pairs with the memory barrier in set_current_state() like in
423f66227deSOmar Sandoval 	 * sbq_wake_up().
42488459642SOmar Sandoval 	 */
42588459642SOmar Sandoval 	smp_mb();
42688459642SOmar Sandoval 	wake_index = atomic_read(&sbq->wake_index);
42788459642SOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
42888459642SOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[wake_index];
42988459642SOmar Sandoval 
43088459642SOmar Sandoval 		if (waitqueue_active(&ws->wait))
43188459642SOmar Sandoval 			wake_up(&ws->wait);
43288459642SOmar Sandoval 
43388459642SOmar Sandoval 		wake_index = sbq_index_inc(wake_index);
43488459642SOmar Sandoval 	}
43588459642SOmar Sandoval }
43688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all);
43724af1ccfSOmar Sandoval 
43824af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
43924af1ccfSOmar Sandoval {
44024af1ccfSOmar Sandoval 	bool first;
44124af1ccfSOmar Sandoval 	int i;
44224af1ccfSOmar Sandoval 
44324af1ccfSOmar Sandoval 	sbitmap_show(&sbq->sb, m);
44424af1ccfSOmar Sandoval 
44524af1ccfSOmar Sandoval 	seq_puts(m, "alloc_hint={");
44624af1ccfSOmar Sandoval 	first = true;
44724af1ccfSOmar Sandoval 	for_each_possible_cpu(i) {
44824af1ccfSOmar Sandoval 		if (!first)
44924af1ccfSOmar Sandoval 			seq_puts(m, ", ");
45024af1ccfSOmar Sandoval 		first = false;
45124af1ccfSOmar Sandoval 		seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i));
45224af1ccfSOmar Sandoval 	}
45324af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
45424af1ccfSOmar Sandoval 
45524af1ccfSOmar Sandoval 	seq_printf(m, "wake_batch=%u\n", sbq->wake_batch);
45624af1ccfSOmar Sandoval 	seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index));
45724af1ccfSOmar Sandoval 
45824af1ccfSOmar Sandoval 	seq_puts(m, "ws={\n");
45924af1ccfSOmar Sandoval 	for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
46024af1ccfSOmar Sandoval 		struct sbq_wait_state *ws = &sbq->ws[i];
46124af1ccfSOmar Sandoval 
46224af1ccfSOmar Sandoval 		seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
46324af1ccfSOmar Sandoval 			   atomic_read(&ws->wait_cnt),
46424af1ccfSOmar Sandoval 			   waitqueue_active(&ws->wait) ? "active" : "inactive");
46524af1ccfSOmar Sandoval 	}
46624af1ccfSOmar Sandoval 	seq_puts(m, "}\n");
46724af1ccfSOmar Sandoval 
46824af1ccfSOmar Sandoval 	seq_printf(m, "round_robin=%d\n", sbq->round_robin);
46924af1ccfSOmar Sandoval }
47024af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show);
471