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