188459642SOmar Sandoval /* 288459642SOmar Sandoval * Fast and scalable bitmaps. 388459642SOmar Sandoval * 488459642SOmar Sandoval * Copyright (C) 2016 Facebook 588459642SOmar Sandoval * Copyright (C) 2013-2014 Jens Axboe 688459642SOmar Sandoval * 788459642SOmar Sandoval * This program is free software; you can redistribute it and/or 888459642SOmar Sandoval * modify it under the terms of the GNU General Public 988459642SOmar Sandoval * License v2 as published by the Free Software Foundation. 1088459642SOmar Sandoval * 1188459642SOmar Sandoval * This program is distributed in the hope that it will be useful, 1288459642SOmar Sandoval * but WITHOUT ANY WARRANTY; without even the implied warranty of 1388459642SOmar Sandoval * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1488459642SOmar Sandoval * General Public License for more details. 1588459642SOmar Sandoval * 1688459642SOmar Sandoval * You should have received a copy of the GNU General Public License 1788459642SOmar Sandoval * along with this program. If not, see <https://www.gnu.org/licenses/>. 1888459642SOmar Sandoval */ 1988459642SOmar Sandoval 2088459642SOmar Sandoval #ifndef __LINUX_SCALE_BITMAP_H 2188459642SOmar Sandoval #define __LINUX_SCALE_BITMAP_H 2288459642SOmar Sandoval 2388459642SOmar Sandoval #include <linux/kernel.h> 2488459642SOmar Sandoval #include <linux/slab.h> 2588459642SOmar Sandoval 2614b470b5SArnd Bergmann struct seq_file; 2714b470b5SArnd Bergmann 2888459642SOmar Sandoval /** 2988459642SOmar Sandoval * struct sbitmap_word - Word in a &struct sbitmap. 3088459642SOmar Sandoval */ 3188459642SOmar Sandoval struct sbitmap_word { 3288459642SOmar Sandoval /** 33*ea86ea2cSJens Axboe * @depth: Number of bits being used in @word/@cleared 3488459642SOmar Sandoval */ 3588459642SOmar Sandoval unsigned long depth; 36*ea86ea2cSJens Axboe 37*ea86ea2cSJens Axboe /** 38*ea86ea2cSJens Axboe * @word: word holding free bits 39*ea86ea2cSJens Axboe */ 40*ea86ea2cSJens Axboe unsigned long word ____cacheline_aligned_in_smp; 41*ea86ea2cSJens Axboe 42*ea86ea2cSJens Axboe /** 43*ea86ea2cSJens Axboe * @cleared: word holding cleared bits 44*ea86ea2cSJens Axboe */ 45*ea86ea2cSJens Axboe unsigned long cleared ____cacheline_aligned_in_smp; 46*ea86ea2cSJens Axboe 47*ea86ea2cSJens Axboe /** 48*ea86ea2cSJens Axboe * @swap_lock: Held while swapping word <-> cleared 49*ea86ea2cSJens Axboe */ 50*ea86ea2cSJens Axboe spinlock_t swap_lock; 5188459642SOmar Sandoval } ____cacheline_aligned_in_smp; 5288459642SOmar Sandoval 5388459642SOmar Sandoval /** 5488459642SOmar Sandoval * struct sbitmap - Scalable bitmap. 5588459642SOmar Sandoval * 5688459642SOmar Sandoval * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This 5788459642SOmar Sandoval * trades off higher memory usage for better scalability. 5888459642SOmar Sandoval */ 5988459642SOmar Sandoval struct sbitmap { 6088459642SOmar Sandoval /** 6188459642SOmar Sandoval * @depth: Number of bits used in the whole bitmap. 6288459642SOmar Sandoval */ 6388459642SOmar Sandoval unsigned int depth; 6488459642SOmar Sandoval 6588459642SOmar Sandoval /** 6688459642SOmar Sandoval * @shift: log2(number of bits used per word) 6788459642SOmar Sandoval */ 6888459642SOmar Sandoval unsigned int shift; 6988459642SOmar Sandoval 7088459642SOmar Sandoval /** 7188459642SOmar Sandoval * @map_nr: Number of words (cachelines) being used for the bitmap. 7288459642SOmar Sandoval */ 7388459642SOmar Sandoval unsigned int map_nr; 7488459642SOmar Sandoval 7588459642SOmar Sandoval /** 7688459642SOmar Sandoval * @map: Allocated bitmap. 7788459642SOmar Sandoval */ 7888459642SOmar Sandoval struct sbitmap_word *map; 7988459642SOmar Sandoval }; 8088459642SOmar Sandoval 8188459642SOmar Sandoval #define SBQ_WAIT_QUEUES 8 8288459642SOmar Sandoval #define SBQ_WAKE_BATCH 8 8388459642SOmar Sandoval 8488459642SOmar Sandoval /** 8588459642SOmar Sandoval * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue. 8688459642SOmar Sandoval */ 8788459642SOmar Sandoval struct sbq_wait_state { 8888459642SOmar Sandoval /** 8988459642SOmar Sandoval * @wait_cnt: Number of frees remaining before we wake up. 9088459642SOmar Sandoval */ 9188459642SOmar Sandoval atomic_t wait_cnt; 9288459642SOmar Sandoval 9388459642SOmar Sandoval /** 9488459642SOmar Sandoval * @wait: Wait queue. 9588459642SOmar Sandoval */ 9688459642SOmar Sandoval wait_queue_head_t wait; 9788459642SOmar Sandoval } ____cacheline_aligned_in_smp; 9888459642SOmar Sandoval 9988459642SOmar Sandoval /** 10088459642SOmar Sandoval * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free 10188459642SOmar Sandoval * bits. 10288459642SOmar Sandoval * 10388459642SOmar Sandoval * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to 10488459642SOmar Sandoval * avoid contention on the wait queue spinlock. This ensures that we don't hit a 10588459642SOmar Sandoval * scalability wall when we run out of free bits and have to start putting tasks 10688459642SOmar Sandoval * to sleep. 10788459642SOmar Sandoval */ 10888459642SOmar Sandoval struct sbitmap_queue { 10988459642SOmar Sandoval /** 11088459642SOmar Sandoval * @sb: Scalable bitmap. 11188459642SOmar Sandoval */ 11288459642SOmar Sandoval struct sbitmap sb; 11388459642SOmar Sandoval 11440aabb67SOmar Sandoval /* 11540aabb67SOmar Sandoval * @alloc_hint: Cache of last successfully allocated or freed bit. 11640aabb67SOmar Sandoval * 11740aabb67SOmar Sandoval * This is per-cpu, which allows multiple users to stick to different 11840aabb67SOmar Sandoval * cachelines until the map is exhausted. 11940aabb67SOmar Sandoval */ 12040aabb67SOmar Sandoval unsigned int __percpu *alloc_hint; 12140aabb67SOmar Sandoval 12288459642SOmar Sandoval /** 12388459642SOmar Sandoval * @wake_batch: Number of bits which must be freed before we wake up any 12488459642SOmar Sandoval * waiters. 12588459642SOmar Sandoval */ 12688459642SOmar Sandoval unsigned int wake_batch; 12788459642SOmar Sandoval 12888459642SOmar Sandoval /** 12988459642SOmar Sandoval * @wake_index: Next wait queue in @ws to wake up. 13088459642SOmar Sandoval */ 13188459642SOmar Sandoval atomic_t wake_index; 13288459642SOmar Sandoval 13388459642SOmar Sandoval /** 13488459642SOmar Sandoval * @ws: Wait queues. 13588459642SOmar Sandoval */ 13688459642SOmar Sandoval struct sbq_wait_state *ws; 137f4a644dbSOmar Sandoval 138f4a644dbSOmar Sandoval /** 139f4a644dbSOmar Sandoval * @round_robin: Allocate bits in strict round-robin order. 140f4a644dbSOmar Sandoval */ 141f4a644dbSOmar Sandoval bool round_robin; 142a3275539SOmar Sandoval 143a3275539SOmar Sandoval /** 144a3275539SOmar Sandoval * @min_shallow_depth: The minimum shallow depth which may be passed to 145a3275539SOmar Sandoval * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow(). 146a3275539SOmar Sandoval */ 147a3275539SOmar Sandoval unsigned int min_shallow_depth; 14888459642SOmar Sandoval }; 14988459642SOmar Sandoval 15088459642SOmar Sandoval /** 15188459642SOmar Sandoval * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node. 15288459642SOmar Sandoval * @sb: Bitmap to initialize. 15388459642SOmar Sandoval * @depth: Number of bits to allocate. 15488459642SOmar Sandoval * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if 15588459642SOmar Sandoval * given, a good default is chosen. 15688459642SOmar Sandoval * @flags: Allocation flags. 15788459642SOmar Sandoval * @node: Memory node to allocate on. 15888459642SOmar Sandoval * 15988459642SOmar Sandoval * Return: Zero on success or negative errno on failure. 16088459642SOmar Sandoval */ 16188459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, 16288459642SOmar Sandoval gfp_t flags, int node); 16388459642SOmar Sandoval 16488459642SOmar Sandoval /** 16588459642SOmar Sandoval * sbitmap_free() - Free memory used by a &struct sbitmap. 16688459642SOmar Sandoval * @sb: Bitmap to free. 16788459642SOmar Sandoval */ 16888459642SOmar Sandoval static inline void sbitmap_free(struct sbitmap *sb) 16988459642SOmar Sandoval { 17088459642SOmar Sandoval kfree(sb->map); 17188459642SOmar Sandoval sb->map = NULL; 17288459642SOmar Sandoval } 17388459642SOmar Sandoval 17488459642SOmar Sandoval /** 17588459642SOmar Sandoval * sbitmap_resize() - Resize a &struct sbitmap. 17688459642SOmar Sandoval * @sb: Bitmap to resize. 17788459642SOmar Sandoval * @depth: New number of bits to resize to. 17888459642SOmar Sandoval * 17988459642SOmar Sandoval * Doesn't reallocate anything. It's up to the caller to ensure that the new 18088459642SOmar Sandoval * depth doesn't exceed the depth that the sb was initialized with. 18188459642SOmar Sandoval */ 18288459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth); 18388459642SOmar Sandoval 18488459642SOmar Sandoval /** 18588459642SOmar Sandoval * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap. 18688459642SOmar Sandoval * @sb: Bitmap to allocate from. 18788459642SOmar Sandoval * @alloc_hint: Hint for where to start searching for a free bit. 18888459642SOmar Sandoval * @round_robin: If true, be stricter about allocation order; always allocate 18988459642SOmar Sandoval * starting from the last allocated bit. This is less efficient 19088459642SOmar Sandoval * than the default behavior (false). 19188459642SOmar Sandoval * 1924ace53f1SOmar Sandoval * This operation provides acquire barrier semantics if it succeeds. 1934ace53f1SOmar Sandoval * 19488459642SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 19588459642SOmar Sandoval */ 19688459642SOmar Sandoval int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin); 19788459642SOmar Sandoval 19888459642SOmar Sandoval /** 199c05e6673SOmar Sandoval * sbitmap_get_shallow() - Try to allocate a free bit from a &struct sbitmap, 200c05e6673SOmar Sandoval * limiting the depth used from each word. 201c05e6673SOmar Sandoval * @sb: Bitmap to allocate from. 202c05e6673SOmar Sandoval * @alloc_hint: Hint for where to start searching for a free bit. 203c05e6673SOmar Sandoval * @shallow_depth: The maximum number of bits to allocate from a single word. 204c05e6673SOmar Sandoval * 205c05e6673SOmar Sandoval * This rather specific operation allows for having multiple users with 206c05e6673SOmar Sandoval * different allocation limits. E.g., there can be a high-priority class that 207c05e6673SOmar Sandoval * uses sbitmap_get() and a low-priority class that uses sbitmap_get_shallow() 208c05e6673SOmar Sandoval * with a @shallow_depth of (1 << (@sb->shift - 1)). Then, the low-priority 209c05e6673SOmar Sandoval * class can only allocate half of the total bits in the bitmap, preventing it 210c05e6673SOmar Sandoval * from starving out the high-priority class. 211c05e6673SOmar Sandoval * 212c05e6673SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 213c05e6673SOmar Sandoval */ 214c05e6673SOmar Sandoval int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint, 215c05e6673SOmar Sandoval unsigned long shallow_depth); 216c05e6673SOmar Sandoval 217c05e6673SOmar Sandoval /** 21888459642SOmar Sandoval * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap. 21988459642SOmar Sandoval * @sb: Bitmap to check. 22088459642SOmar Sandoval * 22188459642SOmar Sandoval * Return: true if any bit in the bitmap is set, false otherwise. 22288459642SOmar Sandoval */ 22388459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb); 22488459642SOmar Sandoval 22588459642SOmar Sandoval /** 22688459642SOmar Sandoval * sbitmap_any_bit_clear() - Check for an unset bit in a &struct 22788459642SOmar Sandoval * sbitmap. 22888459642SOmar Sandoval * @sb: Bitmap to check. 22988459642SOmar Sandoval * 23088459642SOmar Sandoval * Return: true if any bit in the bitmap is clear, false otherwise. 23188459642SOmar Sandoval */ 23288459642SOmar Sandoval bool sbitmap_any_bit_clear(const struct sbitmap *sb); 23388459642SOmar Sandoval 2347930d0a0SMing Lei #define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift) 2357930d0a0SMing Lei #define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U)) 2367930d0a0SMing Lei 23788459642SOmar Sandoval typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *); 23888459642SOmar Sandoval 23988459642SOmar Sandoval /** 2407930d0a0SMing Lei * __sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap. 2417930d0a0SMing Lei * @start: Where to start the iteration. 24288459642SOmar Sandoval * @sb: Bitmap to iterate over. 24388459642SOmar Sandoval * @fn: Callback. Should return true to continue or false to break early. 24488459642SOmar Sandoval * @data: Pointer to pass to callback. 24588459642SOmar Sandoval * 24688459642SOmar Sandoval * This is inline even though it's non-trivial so that the function calls to the 24788459642SOmar Sandoval * callback will hopefully get optimized away. 24888459642SOmar Sandoval */ 2497930d0a0SMing Lei static inline void __sbitmap_for_each_set(struct sbitmap *sb, 2507930d0a0SMing Lei unsigned int start, 2517930d0a0SMing Lei sb_for_each_fn fn, void *data) 25288459642SOmar Sandoval { 2537930d0a0SMing Lei unsigned int index; 2547930d0a0SMing Lei unsigned int nr; 2557930d0a0SMing Lei unsigned int scanned = 0; 25688459642SOmar Sandoval 2577930d0a0SMing Lei if (start >= sb->depth) 2587930d0a0SMing Lei start = 0; 2597930d0a0SMing Lei index = SB_NR_TO_INDEX(sb, start); 2607930d0a0SMing Lei nr = SB_NR_TO_BIT(sb, start); 26188459642SOmar Sandoval 2627930d0a0SMing Lei while (scanned < sb->depth) { 2637930d0a0SMing Lei struct sbitmap_word *word = &sb->map[index]; 2647930d0a0SMing Lei unsigned int depth = min_t(unsigned int, word->depth - nr, 2657930d0a0SMing Lei sb->depth - scanned); 2667930d0a0SMing Lei 2677930d0a0SMing Lei scanned += depth; 26888459642SOmar Sandoval if (!word->word) 2697930d0a0SMing Lei goto next; 27088459642SOmar Sandoval 2717930d0a0SMing Lei /* 2727930d0a0SMing Lei * On the first iteration of the outer loop, we need to add the 2737930d0a0SMing Lei * bit offset back to the size of the word for find_next_bit(). 2747930d0a0SMing Lei * On all other iterations, nr is zero, so this is a noop. 2757930d0a0SMing Lei */ 2767930d0a0SMing Lei depth += nr; 27788459642SOmar Sandoval while (1) { 2787930d0a0SMing Lei nr = find_next_bit(&word->word, depth, nr); 2797930d0a0SMing Lei if (nr >= depth) 28088459642SOmar Sandoval break; 2817930d0a0SMing Lei if (!fn(sb, (index << sb->shift) + nr, data)) 28288459642SOmar Sandoval return; 28388459642SOmar Sandoval 28488459642SOmar Sandoval nr++; 28588459642SOmar Sandoval } 2867930d0a0SMing Lei next: 2877930d0a0SMing Lei nr = 0; 2887930d0a0SMing Lei if (++index >= sb->map_nr) 2897930d0a0SMing Lei index = 0; 29088459642SOmar Sandoval } 29188459642SOmar Sandoval } 29288459642SOmar Sandoval 2937930d0a0SMing Lei /** 2947930d0a0SMing Lei * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap. 2957930d0a0SMing Lei * @sb: Bitmap to iterate over. 2967930d0a0SMing Lei * @fn: Callback. Should return true to continue or false to break early. 2977930d0a0SMing Lei * @data: Pointer to pass to callback. 2987930d0a0SMing Lei */ 2997930d0a0SMing Lei static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn, 3007930d0a0SMing Lei void *data) 3017930d0a0SMing Lei { 3027930d0a0SMing Lei __sbitmap_for_each_set(sb, 0, fn, data); 3037930d0a0SMing Lei } 30488459642SOmar Sandoval 30588459642SOmar Sandoval static inline unsigned long *__sbitmap_word(struct sbitmap *sb, 30688459642SOmar Sandoval unsigned int bitnr) 30788459642SOmar Sandoval { 30888459642SOmar Sandoval return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word; 30988459642SOmar Sandoval } 31088459642SOmar Sandoval 31188459642SOmar Sandoval /* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */ 31288459642SOmar Sandoval 31388459642SOmar Sandoval static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr) 31488459642SOmar Sandoval { 31588459642SOmar Sandoval set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 31688459642SOmar Sandoval } 31788459642SOmar Sandoval 31888459642SOmar Sandoval static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr) 31988459642SOmar Sandoval { 32088459642SOmar Sandoval clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 32188459642SOmar Sandoval } 32288459642SOmar Sandoval 323*ea86ea2cSJens Axboe /* 324*ea86ea2cSJens Axboe * This one is special, since it doesn't actually clear the bit, rather it 325*ea86ea2cSJens Axboe * sets the corresponding bit in the ->cleared mask instead. Paired with 326*ea86ea2cSJens Axboe * the caller doing sbitmap_batch_clear() if a given index is full, which 327*ea86ea2cSJens Axboe * will clear the previously freed entries in the corresponding ->word. 328*ea86ea2cSJens Axboe */ 329*ea86ea2cSJens Axboe static inline void sbitmap_deferred_clear_bit(struct sbitmap *sb, unsigned int bitnr) 330*ea86ea2cSJens Axboe { 331*ea86ea2cSJens Axboe unsigned long *addr = &sb->map[SB_NR_TO_INDEX(sb, bitnr)].cleared; 332*ea86ea2cSJens Axboe 333*ea86ea2cSJens Axboe set_bit(SB_NR_TO_BIT(sb, bitnr), addr); 334*ea86ea2cSJens Axboe } 335*ea86ea2cSJens Axboe 3364ace53f1SOmar Sandoval static inline void sbitmap_clear_bit_unlock(struct sbitmap *sb, 3374ace53f1SOmar Sandoval unsigned int bitnr) 3384ace53f1SOmar Sandoval { 3394ace53f1SOmar Sandoval clear_bit_unlock(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 3404ace53f1SOmar Sandoval } 3414ace53f1SOmar Sandoval 34288459642SOmar Sandoval static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr) 34388459642SOmar Sandoval { 34488459642SOmar Sandoval return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr)); 34588459642SOmar Sandoval } 34688459642SOmar Sandoval 34788459642SOmar Sandoval /** 34824af1ccfSOmar Sandoval * sbitmap_show() - Dump &struct sbitmap information to a &struct seq_file. 34924af1ccfSOmar Sandoval * @sb: Bitmap to show. 35024af1ccfSOmar Sandoval * @m: struct seq_file to write to. 35124af1ccfSOmar Sandoval * 35224af1ccfSOmar Sandoval * This is intended for debugging. The format may change at any time. 35324af1ccfSOmar Sandoval */ 35424af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m); 35524af1ccfSOmar Sandoval 35624af1ccfSOmar Sandoval /** 35724af1ccfSOmar Sandoval * sbitmap_bitmap_show() - Write a hex dump of a &struct sbitmap to a &struct 35824af1ccfSOmar Sandoval * seq_file. 35924af1ccfSOmar Sandoval * @sb: Bitmap to show. 36024af1ccfSOmar Sandoval * @m: struct seq_file to write to. 36124af1ccfSOmar Sandoval * 36224af1ccfSOmar Sandoval * This is intended for debugging. The output isn't guaranteed to be internally 36324af1ccfSOmar Sandoval * consistent. 36424af1ccfSOmar Sandoval */ 36524af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m); 36624af1ccfSOmar Sandoval 36724af1ccfSOmar Sandoval /** 36888459642SOmar Sandoval * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific 36988459642SOmar Sandoval * memory node. 37088459642SOmar Sandoval * @sbq: Bitmap queue to initialize. 37188459642SOmar Sandoval * @depth: See sbitmap_init_node(). 37288459642SOmar Sandoval * @shift: See sbitmap_init_node(). 373f4a644dbSOmar Sandoval * @round_robin: See sbitmap_get(). 37488459642SOmar Sandoval * @flags: Allocation flags. 37588459642SOmar Sandoval * @node: Memory node to allocate on. 37688459642SOmar Sandoval * 37788459642SOmar Sandoval * Return: Zero on success or negative errno on failure. 37888459642SOmar Sandoval */ 37988459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 380f4a644dbSOmar Sandoval int shift, bool round_robin, gfp_t flags, int node); 38188459642SOmar Sandoval 38288459642SOmar Sandoval /** 38388459642SOmar Sandoval * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue. 38488459642SOmar Sandoval * 38588459642SOmar Sandoval * @sbq: Bitmap queue to free. 38688459642SOmar Sandoval */ 38788459642SOmar Sandoval static inline void sbitmap_queue_free(struct sbitmap_queue *sbq) 38888459642SOmar Sandoval { 38988459642SOmar Sandoval kfree(sbq->ws); 39040aabb67SOmar Sandoval free_percpu(sbq->alloc_hint); 39188459642SOmar Sandoval sbitmap_free(&sbq->sb); 39288459642SOmar Sandoval } 39388459642SOmar Sandoval 39488459642SOmar Sandoval /** 39588459642SOmar Sandoval * sbitmap_queue_resize() - Resize a &struct sbitmap_queue. 39688459642SOmar Sandoval * @sbq: Bitmap queue to resize. 39788459642SOmar Sandoval * @depth: New number of bits to resize to. 39888459642SOmar Sandoval * 39988459642SOmar Sandoval * Like sbitmap_resize(), this doesn't reallocate anything. It has to do 40088459642SOmar Sandoval * some extra work on the &struct sbitmap_queue, so it's not safe to just 40188459642SOmar Sandoval * resize the underlying &struct sbitmap. 40288459642SOmar Sandoval */ 40388459642SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth); 40488459642SOmar Sandoval 40588459642SOmar Sandoval /** 40640aabb67SOmar Sandoval * __sbitmap_queue_get() - Try to allocate a free bit from a &struct 40740aabb67SOmar Sandoval * sbitmap_queue with preemption already disabled. 40840aabb67SOmar Sandoval * @sbq: Bitmap queue to allocate from. 40940aabb67SOmar Sandoval * 41040aabb67SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 41140aabb67SOmar Sandoval */ 412f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq); 41340aabb67SOmar Sandoval 41440aabb67SOmar Sandoval /** 415c05e6673SOmar Sandoval * __sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct 416c05e6673SOmar Sandoval * sbitmap_queue, limiting the depth used from each word, with preemption 417c05e6673SOmar Sandoval * already disabled. 418c05e6673SOmar Sandoval * @sbq: Bitmap queue to allocate from. 419c05e6673SOmar Sandoval * @shallow_depth: The maximum number of bits to allocate from a single word. 420c05e6673SOmar Sandoval * See sbitmap_get_shallow(). 421c05e6673SOmar Sandoval * 422a3275539SOmar Sandoval * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after 423a3275539SOmar Sandoval * initializing @sbq. 424a3275539SOmar Sandoval * 425c05e6673SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 426c05e6673SOmar Sandoval */ 427c05e6673SOmar Sandoval int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 428c05e6673SOmar Sandoval unsigned int shallow_depth); 429c05e6673SOmar Sandoval 430c05e6673SOmar Sandoval /** 43140aabb67SOmar Sandoval * sbitmap_queue_get() - Try to allocate a free bit from a &struct 43240aabb67SOmar Sandoval * sbitmap_queue. 43340aabb67SOmar Sandoval * @sbq: Bitmap queue to allocate from. 43440aabb67SOmar Sandoval * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to 43540aabb67SOmar Sandoval * sbitmap_queue_clear()). 43640aabb67SOmar Sandoval * 43740aabb67SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 43840aabb67SOmar Sandoval */ 439f4a644dbSOmar Sandoval static inline int sbitmap_queue_get(struct sbitmap_queue *sbq, 44040aabb67SOmar Sandoval unsigned int *cpu) 44140aabb67SOmar Sandoval { 44240aabb67SOmar Sandoval int nr; 44340aabb67SOmar Sandoval 44440aabb67SOmar Sandoval *cpu = get_cpu(); 445f4a644dbSOmar Sandoval nr = __sbitmap_queue_get(sbq); 44640aabb67SOmar Sandoval put_cpu(); 44740aabb67SOmar Sandoval return nr; 44840aabb67SOmar Sandoval } 44940aabb67SOmar Sandoval 45040aabb67SOmar Sandoval /** 451c05e6673SOmar Sandoval * sbitmap_queue_get_shallow() - Try to allocate a free bit from a &struct 452c05e6673SOmar Sandoval * sbitmap_queue, limiting the depth used from each word. 453c05e6673SOmar Sandoval * @sbq: Bitmap queue to allocate from. 454c05e6673SOmar Sandoval * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to 455c05e6673SOmar Sandoval * sbitmap_queue_clear()). 456c05e6673SOmar Sandoval * @shallow_depth: The maximum number of bits to allocate from a single word. 457c05e6673SOmar Sandoval * See sbitmap_get_shallow(). 458c05e6673SOmar Sandoval * 459a3275539SOmar Sandoval * If you call this, make sure to call sbitmap_queue_min_shallow_depth() after 460a3275539SOmar Sandoval * initializing @sbq. 461a3275539SOmar Sandoval * 462c05e6673SOmar Sandoval * Return: Non-negative allocated bit number if successful, -1 otherwise. 463c05e6673SOmar Sandoval */ 464c05e6673SOmar Sandoval static inline int sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, 465c05e6673SOmar Sandoval unsigned int *cpu, 466c05e6673SOmar Sandoval unsigned int shallow_depth) 467c05e6673SOmar Sandoval { 468c05e6673SOmar Sandoval int nr; 469c05e6673SOmar Sandoval 470c05e6673SOmar Sandoval *cpu = get_cpu(); 471c05e6673SOmar Sandoval nr = __sbitmap_queue_get_shallow(sbq, shallow_depth); 472c05e6673SOmar Sandoval put_cpu(); 473c05e6673SOmar Sandoval return nr; 474c05e6673SOmar Sandoval } 475c05e6673SOmar Sandoval 476c05e6673SOmar Sandoval /** 477a3275539SOmar Sandoval * sbitmap_queue_min_shallow_depth() - Inform a &struct sbitmap_queue of the 478a3275539SOmar Sandoval * minimum shallow depth that will be used. 479a3275539SOmar Sandoval * @sbq: Bitmap queue in question. 480a3275539SOmar Sandoval * @min_shallow_depth: The minimum shallow depth that will be passed to 481a3275539SOmar Sandoval * sbitmap_queue_get_shallow() or __sbitmap_queue_get_shallow(). 482a3275539SOmar Sandoval * 483a3275539SOmar Sandoval * sbitmap_queue_clear() batches wakeups as an optimization. The batch size 484a3275539SOmar Sandoval * depends on the depth of the bitmap. Since the shallow allocation functions 485a3275539SOmar Sandoval * effectively operate with a different depth, the shallow depth must be taken 486a3275539SOmar Sandoval * into account when calculating the batch size. This function must be called 487a3275539SOmar Sandoval * with the minimum shallow depth that will be used. Failure to do so can result 488a3275539SOmar Sandoval * in missed wakeups. 489a3275539SOmar Sandoval */ 490a3275539SOmar Sandoval void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, 491a3275539SOmar Sandoval unsigned int min_shallow_depth); 492a3275539SOmar Sandoval 493a3275539SOmar Sandoval /** 49488459642SOmar Sandoval * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a 49588459642SOmar Sandoval * &struct sbitmap_queue. 49688459642SOmar Sandoval * @sbq: Bitmap to free from. 49788459642SOmar Sandoval * @nr: Bit number to free. 49840aabb67SOmar Sandoval * @cpu: CPU the bit was allocated on. 49988459642SOmar Sandoval */ 50040aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 501f4a644dbSOmar Sandoval unsigned int cpu); 50288459642SOmar Sandoval 50388459642SOmar Sandoval static inline int sbq_index_inc(int index) 50488459642SOmar Sandoval { 50588459642SOmar Sandoval return (index + 1) & (SBQ_WAIT_QUEUES - 1); 50688459642SOmar Sandoval } 50788459642SOmar Sandoval 50888459642SOmar Sandoval static inline void sbq_index_atomic_inc(atomic_t *index) 50988459642SOmar Sandoval { 51088459642SOmar Sandoval int old = atomic_read(index); 51188459642SOmar Sandoval int new = sbq_index_inc(old); 51288459642SOmar Sandoval atomic_cmpxchg(index, old, new); 51388459642SOmar Sandoval } 51488459642SOmar Sandoval 51588459642SOmar Sandoval /** 51688459642SOmar Sandoval * sbq_wait_ptr() - Get the next wait queue to use for a &struct 51788459642SOmar Sandoval * sbitmap_queue. 51888459642SOmar Sandoval * @sbq: Bitmap queue to wait on. 51988459642SOmar Sandoval * @wait_index: A counter per "user" of @sbq. 52088459642SOmar Sandoval */ 52188459642SOmar Sandoval static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq, 52288459642SOmar Sandoval atomic_t *wait_index) 52388459642SOmar Sandoval { 52488459642SOmar Sandoval struct sbq_wait_state *ws; 52588459642SOmar Sandoval 52688459642SOmar Sandoval ws = &sbq->ws[atomic_read(wait_index)]; 52788459642SOmar Sandoval sbq_index_atomic_inc(wait_index); 52888459642SOmar Sandoval return ws; 52988459642SOmar Sandoval } 53088459642SOmar Sandoval 53188459642SOmar Sandoval /** 53288459642SOmar Sandoval * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct 53388459642SOmar Sandoval * sbitmap_queue. 53488459642SOmar Sandoval * @sbq: Bitmap queue to wake up. 53588459642SOmar Sandoval */ 53688459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq); 53788459642SOmar Sandoval 53824af1ccfSOmar Sandoval /** 539e6fc4649SMing Lei * sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue 540e6fc4649SMing Lei * on a &struct sbitmap_queue. 541e6fc4649SMing Lei * @sbq: Bitmap queue to wake up. 542e6fc4649SMing Lei */ 543e6fc4649SMing Lei void sbitmap_queue_wake_up(struct sbitmap_queue *sbq); 544e6fc4649SMing Lei 545e6fc4649SMing Lei /** 54624af1ccfSOmar Sandoval * sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct 54724af1ccfSOmar Sandoval * seq_file. 54824af1ccfSOmar Sandoval * @sbq: Bitmap queue to show. 54924af1ccfSOmar Sandoval * @m: struct seq_file to write to. 55024af1ccfSOmar Sandoval * 55124af1ccfSOmar Sandoval * This is intended for debugging. The format may change at any time. 55224af1ccfSOmar Sandoval */ 55324af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m); 55424af1ccfSOmar Sandoval 55588459642SOmar Sandoval #endif /* __LINUX_SCALE_BITMAP_H */ 556