xref: /linux/include/linux/sbitmap.h (revision 975ef7ff81bb000af6e6c8e63e81f89f3468dcf7)
1 /*
2  * Fast and scalable bitmaps.
3  *
4  * Copyright (C) 2016 Facebook
5  * Copyright (C) 2013-2014 Jens Axboe
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License v2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __LINUX_SCALE_BITMAP_H
21 #define __LINUX_SCALE_BITMAP_H
22 
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 
26 /**
27  * struct sbitmap_word - Word in a &struct sbitmap.
28  */
29 struct sbitmap_word {
30 	/**
31 	 * @word: The bitmap word itself.
32 	 */
33 	unsigned long word;
34 
35 	/**
36 	 * @depth: Number of bits being used in @word.
37 	 */
38 	unsigned long depth;
39 } ____cacheline_aligned_in_smp;
40 
41 /**
42  * struct sbitmap - Scalable bitmap.
43  *
44  * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
45  * trades off higher memory usage for better scalability.
46  */
47 struct sbitmap {
48 	/**
49 	 * @depth: Number of bits used in the whole bitmap.
50 	 */
51 	unsigned int depth;
52 
53 	/**
54 	 * @shift: log2(number of bits used per word)
55 	 */
56 	unsigned int shift;
57 
58 	/**
59 	 * @map_nr: Number of words (cachelines) being used for the bitmap.
60 	 */
61 	unsigned int map_nr;
62 
63 	/**
64 	 * @map: Allocated bitmap.
65 	 */
66 	struct sbitmap_word *map;
67 };
68 
69 #define SBQ_WAIT_QUEUES 8
70 #define SBQ_WAKE_BATCH 8
71 
72 /**
73  * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
74  */
75 struct sbq_wait_state {
76 	/**
77 	 * @wait_cnt: Number of frees remaining before we wake up.
78 	 */
79 	atomic_t wait_cnt;
80 
81 	/**
82 	 * @wait: Wait queue.
83 	 */
84 	wait_queue_head_t wait;
85 } ____cacheline_aligned_in_smp;
86 
87 /**
88  * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
89  * bits.
90  *
91  * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to
92  * avoid contention on the wait queue spinlock. This ensures that we don't hit a
93  * scalability wall when we run out of free bits and have to start putting tasks
94  * to sleep.
95  */
96 struct sbitmap_queue {
97 	/**
98 	 * @sb: Scalable bitmap.
99 	 */
100 	struct sbitmap sb;
101 
102 	/*
103 	 * @alloc_hint: Cache of last successfully allocated or freed bit.
104 	 *
105 	 * This is per-cpu, which allows multiple users to stick to different
106 	 * cachelines until the map is exhausted.
107 	 */
108 	unsigned int __percpu *alloc_hint;
109 
110 	/**
111 	 * @wake_batch: Number of bits which must be freed before we wake up any
112 	 * waiters.
113 	 */
114 	unsigned int wake_batch;
115 
116 	/**
117 	 * @wake_index: Next wait queue in @ws to wake up.
118 	 */
119 	atomic_t wake_index;
120 
121 	/**
122 	 * @ws: Wait queues.
123 	 */
124 	struct sbq_wait_state *ws;
125 
126 	/**
127 	 * @round_robin: Allocate bits in strict round-robin order.
128 	 */
129 	bool round_robin;
130 
131 	/**
132 	 * @min_shallow_depth: The minimum shallow depth which may be passed to
133 	 * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
134 	 */
135 	unsigned int min_shallow_depth;
136 };
137 
138 /**
139  * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
140  * @sb: Bitmap to initialize.
141  * @depth: Number of bits to allocate.
142  * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
143  *         given, a good default is chosen.
144  * @flags: Allocation flags.
145  * @node: Memory node to allocate on.
146  *
147  * Return: Zero on success or negative errno on failure.
148  */
149 int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
150 		      gfp_t flags, int node);
151 
152 /**
153  * sbitmap_free() - Free memory used by a &struct sbitmap.
154  * @sb: Bitmap to free.
155  */
156 static inline void sbitmap_free(struct sbitmap *sb)
157 {
158 	kfree(sb->map);
159 	sb->map = NULL;
160 }
161 
162 /**
163  * sbitmap_resize() - Resize a &struct sbitmap.
164  * @sb: Bitmap to resize.
165  * @depth: New number of bits to resize to.
166  *
167  * Doesn't reallocate anything. It's up to the caller to ensure that the new
168  * depth doesn't exceed the depth that the sb was initialized with.
169  */
170 void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
171 
172 /**
173  * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
174  * @sb: Bitmap to allocate from.
175  * @alloc_hint: Hint for where to start searching for a free bit.
176  * @round_robin: If true, be stricter about allocation order; always allocate
177  *               starting from the last allocated bit. This is less efficient
178  *               than the default behavior (false).
179  *
180  * This operation provides acquire barrier semantics if it succeeds.
181  *
182  * Return: Non-negative allocated bit number if successful, -1 otherwise.
183  */
184 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
185 
186 /**
187  * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap,
188  * limiting the depth used from each word.
189  * @sb: Bitmap to allocate from.
190  * @alloc_hint: Hint for where to start searching for a free bit.
191  * @shallow_depth: The maximum number of bits to allocate from a single word.
192  *
193  * This rather specific operation allows for having multiple users with
194  * different allocation limits. E.g., there can be a high-priority class that
195  * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow()
196  * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority
197  * class can only allocate half of the total bits in the bitmap, preventing it
198  * from starving out the high-priority class.
199  *
200  * Return: Non-negative allocated bit number if successful, -1 otherwise.
201  */
202 int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint,
203 			unsigned long shallow_depth);
204 
205 /**
206  * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
207  * @sb: Bitmap to check.
208  *
209  * Return: true if any bit in the bitmap is set, false otherwise.
210  */
211 bool sbitmap_any_bit_set(const struct sbitmap *sb);
212 
213 /**
214  * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
215  * sbitmap.
216  * @sb: Bitmap to check.
217  *
218  * Return: true if any bit in the bitmap is clear, false otherwise.
219  */
220 bool sbitmap_any_bit_clear(const struct sbitmap *sb);
221 
222 #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
223 #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
224 
225 typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
226 
227 /**
228  * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
229  * @start: Where to start the iteration.
230  * @sb: Bitmap to iterate over.
231  * @fn: Callback. Should return true to continue or false to break early.
232  * @data: Pointer to pass to callback.
233  *
234  * This is inline even though it's non-trivial so that the function calls to the
235  * callback will hopefully get optimized away.
236  */
237 static inline void __sbitmap_for_each_set(struct sbitmap *sb,
238 					  unsigned int start,
239 					  sb_for_each_fn fn, void *data)
240 {
241 	unsigned int index;
242 	unsigned int nr;
243 	unsigned int scanned = 0;
244 
245 	if (start >= sb->depth)
246 		start = 0;
247 	index = SB_NR_TO_INDEX(sb, start);
248 	nr = SB_NR_TO_BIT(sb, start);
249 
250 	while (scanned < sb->depth) {
251 		struct sbitmap_word *word = &sb->map[index];
252 		unsigned int depth = min_t(unsigned int, word->depth - nr,
253 					   sb->depth - scanned);
254 
255 		scanned += depth;
256 		if (!word->word)
257 			goto next;
258 
259 		/*
260 		 * On the first iteration of the outer loop, we need to add the
261 		 * bit offset back to the size of the word for find_next_bit().
262 		 * On all other iterations, nr is zero, so this is a noop.
263 		 */
264 		depth += nr;
265 		while (1) {
266 			nr = find_next_bit(&word->word, depth, nr);
267 			if (nr >= depth)
268 				break;
269 			if (!fn(sb, (index << sb->shift) + nr, data))
270 				return;
271 
272 			nr++;
273 		}
274 next:
275 		nr = 0;
276 		if (++index >= sb->map_nr)
277 			index = 0;
278 	}
279 }
280 
281 /**
282  * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
283  * @sb: Bitmap to iterate over.
284  * @fn: Callback. Should return true to continue or false to break early.
285  * @data: Pointer to pass to callback.
286  */
287 static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
288 					void *data)
289 {
290 	__sbitmap_for_each_set(sb, 0, fn, data);
291 }
292 
293 static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
294 					    unsigned int bitnr)
295 {
296 	return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
297 }
298 
299 /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
300 
301 static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
302 {
303 	set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
304 }
305 
306 static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
307 {
308 	clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
309 }
310 
311 static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb,
312 					    unsigned int bitnr)
313 {
314 	clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
315 }
316 
317 static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
318 {
319 	return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
320 }
321 
322 unsigned int sbitmap_weight(const struct sbitmap *sb);
323 
324 /**
325  * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file.
326  * @sb: Bitmap to show.
327  * @m: struct seq_file to write to.
328  *
329  * This is intended for debugging. The format may change at any time.
330  */
331 void sbitmap_show(struct sbitmap *sb, struct seq_file *m);
332 
333 /**
334  * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct
335  * seq_file.
336  * @sb: Bitmap to show.
337  * @m: struct seq_file to write to.
338  *
339  * This is intended for debugging. The output isn't guaranteed to be internally
340  * consistent.
341  */
342 void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m);
343 
344 /**
345  * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
346  * memory node.
347  * @sbq: Bitmap queue to initialize.
348  * @depth: See sbitmap_init_node().
349  * @shift: See sbitmap_init_node().
350  * @round_robin: See sbitmap_get().
351  * @flags: Allocation flags.
352  * @node: Memory node to allocate on.
353  *
354  * Return: Zero on success or negative errno on failure.
355  */
356 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
357 			    int shift, bool round_robin, gfp_t flags, int node);
358 
359 /**
360  * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
361  *
362  * @sbq: Bitmap queue to free.
363  */
364 static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
365 {
366 	kfree(sbq->ws);
367 	free_percpu(sbq->alloc_hint);
368 	sbitmap_free(&sbq->sb);
369 }
370 
371 /**
372  * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
373  * @sbq: Bitmap queue to resize.
374  * @depth: New number of bits to resize to.
375  *
376  * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
377  * some extra work on the &struct sbitmap_queue, so it's not safe to just
378  * resize the underlying &struct sbitmap.
379  */
380 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
381 
382 /**
383  * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
384  * sbitmap_queue with preemption already disabled.
385  * @sbq: Bitmap queue to allocate from.
386  *
387  * Return: Non-negative allocated bit number if successful, -1 otherwise.
388  */
389 int __sbitmap_queue_get(struct sbitmap_queue *sbq);
390 
391 /**
392  * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
393  * sbitmap_queue, limiting the depth used from each word, with preemption
394  * already disabled.
395  * @sbq: Bitmap queue to allocate from.
396  * @shallow_depth: The maximum number of bits to allocate from a single word.
397  * See sbitmap_get_shallow().
398  *
399  * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
400  * initializing @sbq.
401  *
402  * Return: Non-negative allocated bit number if successful, -1 otherwise.
403  */
404 int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
405 				unsigned int shallow_depth);
406 
407 /**
408  * sbitmap_queue_get() - Try to allocate a free bit from a &struct
409  * sbitmap_queue.
410  * @sbq: Bitmap queue to allocate from.
411  * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
412  *       sbitmap_queue_clear()).
413  *
414  * Return: Non-negative allocated bit number if successful, -1 otherwise.
415  */
416 static inline int sbitmap_queue_get(struct sbitmap_queue *sbq,
417 				    unsigned int *cpu)
418 {
419 	int nr;
420 
421 	*cpu = get_cpu();
422 	nr = __sbitmap_queue_get(sbq);
423 	put_cpu();
424 	return nr;
425 }
426 
427 /**
428  * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct
429  * sbitmap_queue, limiting the depth used from each word.
430  * @sbq: Bitmap queue to allocate from.
431  * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
432  *       sbitmap_queue_clear()).
433  * @shallow_depth: The maximum number of bits to allocate from a single word.
434  * See sbitmap_get_shallow().
435  *
436  * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after
437  * initializing @sbq.
438  *
439  * Return: Non-negative allocated bit number if successful, -1 otherwise.
440  */
441 static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq,
442 					    unsigned int *cpu,
443 					    unsigned int shallow_depth)
444 {
445 	int nr;
446 
447 	*cpu = get_cpu();
448 	nr = __sbitmap_queue_get_shallow(sbq, shallow_depth);
449 	put_cpu();
450 	return nr;
451 }
452 
453 /**
454  * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the
455  * minimum shallow depth that will be used.
456  * @sbq: Bitmap queue in question.
457  * @min_shallow_depth: The minimum shallow depth that will be passed to
458  * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow().
459  *
460  * sbitmap_queue_clear() batches wakeups as an optimization. The batch size
461  * depends on the depth of the bitmap. Since the shallow allocation functions
462  * effectively operate with a different depth, the shallow depth must be taken
463  * into account when calculating the batch size. This function must be called
464  * with the minimum shallow depth that will be used. Failure to do so can result
465  * in missed wakeups.
466  */
467 void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq,
468 				     unsigned int min_shallow_depth);
469 
470 /**
471  * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
472  * &struct sbitmap_queue.
473  * @sbq: Bitmap to free from.
474  * @nr: Bit number to free.
475  * @cpu: CPU the bit was allocated on.
476  */
477 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
478 			 unsigned int cpu);
479 
480 static inline int sbq_index_inc(int index)
481 {
482 	return (index + 1) & (SBQ_WAIT_QUEUES - 1);
483 }
484 
485 static inline void sbq_index_atomic_inc(atomic_t *index)
486 {
487 	int old = atomic_read(index);
488 	int new = sbq_index_inc(old);
489 	atomic_cmpxchg(index, old, new);
490 }
491 
492 /**
493  * sbq_wait_ptr() - Get the next wait queue to use for a &struct
494  * sbitmap_queue.
495  * @sbq: Bitmap queue to wait on.
496  * @wait_index: A counter per "user" of @sbq.
497  */
498 static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
499 						  atomic_t *wait_index)
500 {
501 	struct sbq_wait_state *ws;
502 
503 	ws = &sbq->ws[atomic_read(wait_index)];
504 	sbq_index_atomic_inc(wait_index);
505 	return ws;
506 }
507 
508 /**
509  * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
510  * sbitmap_queue.
511  * @sbq: Bitmap queue to wake up.
512  */
513 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
514 
515 /**
516  * sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue
517  * on a &struct sbitmap_queue.
518  * @sbq: Bitmap queue to wake up.
519  */
520 void sbitmap_queue_wake_up(struct sbitmap_queue *sbq);
521 
522 /**
523  * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct
524  * seq_file.
525  * @sbq: Bitmap queue to show.
526  * @m: struct seq_file to write to.
527  *
528  * This is intended for debugging. The format may change at any time.
529  */
530 void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m);
531 
532 #endif /* __LINUX_SCALE_BITMAP_H */
533