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 */ 29struct 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 */ 47struct 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 */ 75struct 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 */ 96struct 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/** 133 * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node. 134 * @sb: Bitmap to initialize. 135 * @depth: Number of bits to allocate. 136 * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if 137 * given, a good default is chosen. 138 * @flags: Allocation flags. 139 * @node: Memory node to allocate on. 140 * 141 * Return: Zero on success or negative errno on failure. 142 */ 143int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, 144 gfp_t flags, int node); 145 146/** 147 * sbitmap_free() - Free memory used by a &struct sbitmap. 148 * @sb: Bitmap to free. 149 */ 150static inline void sbitmap_free(struct sbitmap *sb) 151{ 152 kfree(sb->map); 153 sb->map = NULL; 154} 155 156/** 157 * sbitmap_resize() - Resize a &struct sbitmap. 158 * @sb: Bitmap to resize. 159 * @depth: New number of bits to resize to. 160 * 161 * Doesn't reallocate anything. It's up to the caller to ensure that the new 162 * depth doesn't exceed the depth that the sb was initialized with. 163 */ 164void sbitmap_resize(struct sbitmap *sb, unsigned int depth); 165 166/** 167 * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap. 168 * @sb: Bitmap to allocate from. 169 * @alloc_hint: Hint for where to start searching for a free bit. 170 * @round_robin: If true, be stricter about allocation order; always allocate 171 * starting from the last allocated bit. This is less efficient 172 * than the default behavior (false). 173 * 174 * Return: Non-negative allocated bit number if successful, -1 otherwise. 175 */ 176int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin); 177 178/** 179 * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap, 180 * limiting the depth used from each word. 181 * @sb: Bitmap to allocate from. 182 * @alloc_hint: Hint for where to start searching for a free bit. 183 * @shallow_depth: The maximum number of bits to allocate from a single word. 184 * 185 * This rather specific operation allows for having multiple users with 186 * different allocation limits. E.g., there can be a high-priority class that 187 * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow() 188 * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority 189 * class can only allocate half of the total bits in the bitmap, preventing it 190 * from starving out the high-priority class. 191 * 192 * Return: Non-negative allocated bit number if successful, -1 otherwise. 193 */ 194int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint, 195 unsigned long shallow_depth); 196 197/** 198 * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap. 199 * @sb: Bitmap to check. 200 * 201 * Return: true if any bit in the bitmap is set, false otherwise. 202 */ 203bool sbitmap_any_bit_set(const struct sbitmap *sb); 204 205/** 206 * sbitmap_any_bit_clear() - Check for an unset bit in a &struct 207 * sbitmap. 208 * @sb: Bitmap to check. 209 * 210 * Return: true if any bit in the bitmap is clear, false otherwise. 211 */ 212bool sbitmap_any_bit_clear(const struct sbitmap *sb); 213 214#define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift) 215#define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U)) 216 217typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *); 218 219/** 220 * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap. 221 * @start: Where to start the iteration. 222 * @sb: Bitmap to iterate over. 223 * @fn: Callback. Should return true to continue or false to break early. 224 * @data: Pointer to pass to callback. 225 * 226 * This is inline even though it's non-trivial so that the function calls to the 227 * callback will hopefully get optimized away. 228 */ 229static inline void __sbitmap_for_each_set(struct sbitmap *sb, 230 unsigned int start, 231 sb_for_each_fn fn, void *data) 232{ 233 unsigned int index; 234 unsigned int nr; 235 unsigned int scanned = 0; 236 237 if (start >= sb->depth) 238 start = 0; 239 index = SB_NR_TO_INDEX(sb, start); 240 nr = SB_NR_TO_BIT(sb, start); 241 242 while (scanned < sb->depth) { 243 struct sbitmap_word *word = &sb->map[index]; 244 unsigned int depth = min_t(unsigned int, word->depth - nr, 245 sb->depth - scanned); 246 247 scanned += depth; 248 if (!word->word) 249 goto next; 250 251 /* 252 * On the first iteration of the outer loop, we need to add the 253 * bit offset back to the size of the word for find_next_bit(). 254 * On all other iterations, nr is zero, so this is a noop. 255 */ 256 depth += nr; 257 while (1) { 258 nr = find_next_bit(&word->word, depth, nr); 259 if (nr >= depth) 260 break; 261 if (!fn(sb, (index << sb->shift) + nr, data)) 262 return; 263 264 nr++; 265 } 266next: 267 nr = 0; 268 if (++index >= sb->map_nr) 269 index = 0; 270 } 271} 272 273/** 274 * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap. 275 * @sb: Bitmap to iterate over. 276 * @fn: Callback. Should return true to continue or false to break early. 277 * @data: Pointer to pass to callback. 278 */ 279static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn, 280 void *data) 281{ 282 __sbitmap_for_each_set(sb, 0, fn, data); 283} 284 285static inline unsigned long *__sbitmap_word(struct sbitmap *sb, 286 unsigned int bitnr) 287{ 288 return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word; 289} 290 291/* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */ 292 293static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr) 294{ 295 set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 296} 297 298static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr) 299{ 300 clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 301} 302 303static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr) 304{ 305 return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 306} 307 308unsigned int sbitmap_weight(const struct sbitmap *sb); 309 310/** 311 * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file. 312 * @sb: Bitmap to show. 313 * @m: struct seq_file to write to. 314 * 315 * This is intended for debugging. The format may change at any time. 316 */ 317void sbitmap_show(struct sbitmap *sb, struct seq_file *m); 318 319/** 320 * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct 321 * seq_file. 322 * @sb: Bitmap to show. 323 * @m: struct seq_file to write to. 324 * 325 * This is intended for debugging. The output isn't guaranteed to be internally 326 * consistent. 327 */ 328void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m); 329 330/** 331 * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific 332 * memory node. 333 * @sbq: Bitmap queue to initialize. 334 * @depth: See sbitmap_init_node(). 335 * @shift: See sbitmap_init_node(). 336 * @round_robin: See sbitmap_get(). 337 * @flags: Allocation flags. 338 * @node: Memory node to allocate on. 339 * 340 * Return: Zero on success or negative errno on failure. 341 */ 342int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 343 int shift, bool round_robin, gfp_t flags, int node); 344 345/** 346 * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue. 347 * 348 * @sbq: Bitmap queue to free. 349 */ 350static inline void sbitmap_queue_free(struct sbitmap_queue *sbq) 351{ 352 kfree(sbq->ws); 353 free_percpu(sbq->alloc_hint); 354 sbitmap_free(&sbq->sb); 355} 356 357/** 358 * sbitmap_queue_resize() - Resize a &struct sbitmap_queue. 359 * @sbq: Bitmap queue to resize. 360 * @depth: New number of bits to resize to. 361 * 362 * Like sbitmap_resize(), this doesn't reallocate anything. It has to do 363 * some extra work on the &struct sbitmap_queue, so it's not safe to just 364 * resize the underlying &struct sbitmap. 365 */ 366void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth); 367 368/** 369 * __sbitmap_queue_get() - Try to allocate a free bit from a &struct 370 * sbitmap_queue with preemption already disabled. 371 * @sbq: Bitmap queue to allocate from. 372 * 373 * Return: Non-negative allocated bit number if successful, -1 otherwise. 374 */ 375int __sbitmap_queue_get(struct sbitmap_queue *sbq); 376 377/** 378 * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct 379 * sbitmap_queue, limiting the depth used from each word, with preemption 380 * already disabled. 381 * @sbq: Bitmap queue to allocate from. 382 * @shallow_depth: The maximum number of bits to allocate from a single word. 383 * See sbitmap_get_shallow(). 384 * 385 * Return: Non-negative allocated bit number if successful, -1 otherwise. 386 */ 387int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 388 unsigned int shallow_depth); 389 390/** 391 * sbitmap_queue_get() - Try to allocate a free bit from a &struct 392 * sbitmap_queue. 393 * @sbq: Bitmap queue to allocate from. 394 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to 395 * sbitmap_queue_clear()). 396 * 397 * Return: Non-negative allocated bit number if successful, -1 otherwise. 398 */ 399static inline int sbitmap_queue_get(struct sbitmap_queue *sbq, 400 unsigned int *cpu) 401{ 402 int nr; 403 404 *cpu = get_cpu(); 405 nr = __sbitmap_queue_get(sbq); 406 put_cpu(); 407 return nr; 408} 409 410/** 411 * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct 412 * sbitmap_queue, limiting the depth used from each word. 413 * @sbq: Bitmap queue to allocate from. 414 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to 415 * sbitmap_queue_clear()). 416 * @shallow_depth: The maximum number of bits to allocate from a single word. 417 * See sbitmap_get_shallow(). 418 * 419 * Return: Non-negative allocated bit number if successful, -1 otherwise. 420 */ 421static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 422 unsigned int *cpu, 423 unsigned int shallow_depth) 424{ 425 int nr; 426 427 *cpu = get_cpu(); 428 nr = __sbitmap_queue_get_shallow(sbq, shallow_depth); 429 put_cpu(); 430 return nr; 431} 432 433/** 434 * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a 435 * &struct sbitmap_queue. 436 * @sbq: Bitmap to free from. 437 * @nr: Bit number to free. 438 * @cpu: CPU the bit was allocated on. 439 */ 440void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 441 unsigned int cpu); 442 443static inline int sbq_index_inc(int index) 444{ 445 return (index + 1) & (SBQ_WAIT_QUEUES - 1); 446} 447 448static inline void sbq_index_atomic_inc(atomic_t *index) 449{ 450 int old = atomic_read(index); 451 int new = sbq_index_inc(old); 452 atomic_cmpxchg(index, old, new); 453} 454 455/** 456 * sbq_wait_ptr() - Get the next wait queue to use for a &struct 457 * sbitmap_queue. 458 * @sbq: Bitmap queue to wait on. 459 * @wait_index: A counter per "user" of @sbq. 460 */ 461static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq, 462 atomic_t *wait_index) 463{ 464 struct sbq_wait_state *ws; 465 466 ws = &sbq->ws[atomic_read(wait_index)]; 467 sbq_index_atomic_inc(wait_index); 468 return ws; 469} 470 471/** 472 * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct 473 * sbitmap_queue. 474 * @sbq: Bitmap queue to wake up. 475 */ 476void sbitmap_queue_wake_all(struct sbitmap_queue *sbq); 477 478/** 479 * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct 480 * seq_file. 481 * @sbq: Bitmap queue to show. 482 * @m: struct seq_file to write to. 483 * 484 * This is intended for debugging. The format may change at any time. 485 */ 486void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m); 487 488#endif /* __LINUX_SCALE_BITMAP_H */ 489