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 /** 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 */ 143 int 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 */ 150 static 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 */ 164 void 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 */ 176 int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin); 177 178 /** 179 * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap. 180 * @sb: Bitmap to check. 181 * 182 * Return: true if any bit in the bitmap is set, false otherwise. 183 */ 184 bool sbitmap_any_bit_set(const struct sbitmap *sb); 185 186 /** 187 * sbitmap_any_bit_clear() - Check for an unset bit in a &struct 188 * sbitmap. 189 * @sb: Bitmap to check. 190 * 191 * Return: true if any bit in the bitmap is clear, false otherwise. 192 */ 193 bool sbitmap_any_bit_clear(const struct sbitmap *sb); 194 195 typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *); 196 197 /** 198 * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap. 199 * @sb: Bitmap to iterate over. 200 * @fn: Callback. Should return true to continue or false to break early. 201 * @data: Pointer to pass to callback. 202 * 203 * This is inline even though it's non-trivial so that the function calls to the 204 * callback will hopefully get optimized away. 205 */ 206 static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn, 207 void *data) 208 { 209 unsigned int i; 210 211 for (i = 0; i < sb->map_nr; i++) { 212 struct sbitmap_word *word = &sb->map[i]; 213 unsigned int off, nr; 214 215 if (!word->word) 216 continue; 217 218 nr = 0; 219 off = i << sb->shift; 220 while (1) { 221 nr = find_next_bit(&word->word, word->depth, nr); 222 if (nr >= word->depth) 223 break; 224 225 if (!fn(sb, off + nr, data)) 226 return; 227 228 nr++; 229 } 230 } 231 } 232 233 #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift) 234 #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U)) 235 236 static inline unsigned long *__sbitmap_word(struct sbitmap *sb, 237 unsigned int bitnr) 238 { 239 return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word; 240 } 241 242 /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */ 243 244 static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr) 245 { 246 set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 247 } 248 249 static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr) 250 { 251 clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 252 } 253 254 static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr) 255 { 256 return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 257 } 258 259 unsigned int sbitmap_weight(const struct sbitmap *sb); 260 261 /** 262 * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific 263 * memory node. 264 * @sbq: Bitmap queue to initialize. 265 * @depth: See sbitmap_init_node(). 266 * @shift: See sbitmap_init_node(). 267 * @round_robin: See sbitmap_get(). 268 * @flags: Allocation flags. 269 * @node: Memory node to allocate on. 270 * 271 * Return: Zero on success or negative errno on failure. 272 */ 273 int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 274 int shift, bool round_robin, gfp_t flags, int node); 275 276 /** 277 * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue. 278 * 279 * @sbq: Bitmap queue to free. 280 */ 281 static inline void sbitmap_queue_free(struct sbitmap_queue *sbq) 282 { 283 kfree(sbq->ws); 284 free_percpu(sbq->alloc_hint); 285 sbitmap_free(&sbq->sb); 286 } 287 288 /** 289 * sbitmap_queue_resize() - Resize a &struct sbitmap_queue. 290 * @sbq: Bitmap queue to resize. 291 * @depth: New number of bits to resize to. 292 * 293 * Like sbitmap_resize(), this doesn't reallocate anything. It has to do 294 * some extra work on the &struct sbitmap_queue, so it's not safe to just 295 * resize the underlying &struct sbitmap. 296 */ 297 void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth); 298 299 /** 300 * __sbitmap_queue_get() - Try to allocate a free bit from a &struct 301 * sbitmap_queue with preemption already disabled. 302 * @sbq: Bitmap queue to allocate from. 303 * 304 * Return: Non-negative allocated bit number if successful, -1 otherwise. 305 */ 306 int __sbitmap_queue_get(struct sbitmap_queue *sbq); 307 308 /** 309 * sbitmap_queue_get() - Try to allocate a free bit from a &struct 310 * sbitmap_queue. 311 * @sbq: Bitmap queue to allocate from. 312 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to 313 * sbitmap_queue_clear()). 314 * 315 * Return: Non-negative allocated bit number if successful, -1 otherwise. 316 */ 317 static inline int sbitmap_queue_get(struct sbitmap_queue *sbq, 318 unsigned int *cpu) 319 { 320 int nr; 321 322 *cpu = get_cpu(); 323 nr = __sbitmap_queue_get(sbq); 324 put_cpu(); 325 return nr; 326 } 327 328 /** 329 * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a 330 * &struct sbitmap_queue. 331 * @sbq: Bitmap to free from. 332 * @nr: Bit number to free. 333 * @cpu: CPU the bit was allocated on. 334 */ 335 void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 336 unsigned int cpu); 337 338 static inline int sbq_index_inc(int index) 339 { 340 return (index + 1) & (SBQ_WAIT_QUEUES - 1); 341 } 342 343 static inline void sbq_index_atomic_inc(atomic_t *index) 344 { 345 int old = atomic_read(index); 346 int new = sbq_index_inc(old); 347 atomic_cmpxchg(index, old, new); 348 } 349 350 /** 351 * sbq_wait_ptr() - Get the next wait queue to use for a &struct 352 * sbitmap_queue. 353 * @sbq: Bitmap queue to wait on. 354 * @wait_index: A counter per "user" of @sbq. 355 */ 356 static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq, 357 atomic_t *wait_index) 358 { 359 struct sbq_wait_state *ws; 360 361 ws = &sbq->ws[atomic_read(wait_index)]; 362 sbq_index_atomic_inc(wait_index); 363 return ws; 364 } 365 366 /** 367 * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct 368 * sbitmap_queue. 369 * @sbq: Bitmap queue to wake up. 370 */ 371 void sbitmap_queue_wake_all(struct sbitmap_queue *sbq); 372 373 #endif /* __LINUX_SCALE_BITMAP_H */ 374