188459642SOmar Sandoval /* 288459642SOmar Sandoval * Copyright (C) 2016 Facebook 388459642SOmar Sandoval * Copyright (C) 2013-2014 Jens Axboe 488459642SOmar Sandoval * 588459642SOmar Sandoval * This program is free software; you can redistribute it and/or 688459642SOmar Sandoval * modify it under the terms of the GNU General Public 788459642SOmar Sandoval * License v2 as published by the Free Software Foundation. 888459642SOmar Sandoval * 988459642SOmar Sandoval * This program is distributed in the hope that it will be useful, 1088459642SOmar Sandoval * but WITHOUT ANY WARRANTY; without even the implied warranty of 1188459642SOmar Sandoval * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1288459642SOmar Sandoval * General Public License for more details. 1388459642SOmar Sandoval * 1488459642SOmar Sandoval * You should have received a copy of the GNU General Public License 1588459642SOmar Sandoval * along with this program. If not, see <https://www.gnu.org/licenses/>. 1688459642SOmar Sandoval */ 1788459642SOmar Sandoval 18*af8601adSIngo Molnar #include <linux/sched.h> 1998d95416SOmar Sandoval #include <linux/random.h> 2088459642SOmar Sandoval #include <linux/sbitmap.h> 2124af1ccfSOmar Sandoval #include <linux/seq_file.h> 2288459642SOmar Sandoval 2388459642SOmar Sandoval int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, 2488459642SOmar Sandoval gfp_t flags, int node) 2588459642SOmar Sandoval { 2688459642SOmar Sandoval unsigned int bits_per_word; 2788459642SOmar Sandoval unsigned int i; 2888459642SOmar Sandoval 2988459642SOmar Sandoval if (shift < 0) { 3088459642SOmar Sandoval shift = ilog2(BITS_PER_LONG); 3188459642SOmar Sandoval /* 3288459642SOmar Sandoval * If the bitmap is small, shrink the number of bits per word so 3388459642SOmar Sandoval * we spread over a few cachelines, at least. If less than 4 3488459642SOmar Sandoval * bits, just forget about it, it's not going to work optimally 3588459642SOmar Sandoval * anyway. 3688459642SOmar Sandoval */ 3788459642SOmar Sandoval if (depth >= 4) { 3888459642SOmar Sandoval while ((4U << shift) > depth) 3988459642SOmar Sandoval shift--; 4088459642SOmar Sandoval } 4188459642SOmar Sandoval } 4288459642SOmar Sandoval bits_per_word = 1U << shift; 4388459642SOmar Sandoval if (bits_per_word > BITS_PER_LONG) 4488459642SOmar Sandoval return -EINVAL; 4588459642SOmar Sandoval 4688459642SOmar Sandoval sb->shift = shift; 4788459642SOmar Sandoval sb->depth = depth; 4888459642SOmar Sandoval sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); 4988459642SOmar Sandoval 5088459642SOmar Sandoval if (depth == 0) { 5188459642SOmar Sandoval sb->map = NULL; 5288459642SOmar Sandoval return 0; 5388459642SOmar Sandoval } 5488459642SOmar Sandoval 5588459642SOmar Sandoval sb->map = kzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node); 5688459642SOmar Sandoval if (!sb->map) 5788459642SOmar Sandoval return -ENOMEM; 5888459642SOmar Sandoval 5988459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 6088459642SOmar Sandoval sb->map[i].depth = min(depth, bits_per_word); 6188459642SOmar Sandoval depth -= sb->map[i].depth; 6288459642SOmar Sandoval } 6388459642SOmar Sandoval return 0; 6488459642SOmar Sandoval } 6588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_init_node); 6688459642SOmar Sandoval 6788459642SOmar Sandoval void sbitmap_resize(struct sbitmap *sb, unsigned int depth) 6888459642SOmar Sandoval { 6988459642SOmar Sandoval unsigned int bits_per_word = 1U << sb->shift; 7088459642SOmar Sandoval unsigned int i; 7188459642SOmar Sandoval 7288459642SOmar Sandoval sb->depth = depth; 7388459642SOmar Sandoval sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); 7488459642SOmar Sandoval 7588459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 7688459642SOmar Sandoval sb->map[i].depth = min(depth, bits_per_word); 7788459642SOmar Sandoval depth -= sb->map[i].depth; 7888459642SOmar Sandoval } 7988459642SOmar Sandoval } 8088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_resize); 8188459642SOmar Sandoval 8288459642SOmar Sandoval static int __sbitmap_get_word(struct sbitmap_word *word, unsigned int hint, 8388459642SOmar Sandoval bool wrap) 8488459642SOmar Sandoval { 8588459642SOmar Sandoval unsigned int orig_hint = hint; 8688459642SOmar Sandoval int nr; 8788459642SOmar Sandoval 8888459642SOmar Sandoval while (1) { 8988459642SOmar Sandoval nr = find_next_zero_bit(&word->word, word->depth, hint); 9088459642SOmar Sandoval if (unlikely(nr >= word->depth)) { 9188459642SOmar Sandoval /* 9288459642SOmar Sandoval * We started with an offset, and we didn't reset the 9388459642SOmar Sandoval * offset to 0 in a failure case, so start from 0 to 9488459642SOmar Sandoval * exhaust the map. 9588459642SOmar Sandoval */ 9688459642SOmar Sandoval if (orig_hint && hint && wrap) { 9788459642SOmar Sandoval hint = orig_hint = 0; 9888459642SOmar Sandoval continue; 9988459642SOmar Sandoval } 10088459642SOmar Sandoval return -1; 10188459642SOmar Sandoval } 10288459642SOmar Sandoval 10388459642SOmar Sandoval if (!test_and_set_bit(nr, &word->word)) 10488459642SOmar Sandoval break; 10588459642SOmar Sandoval 10688459642SOmar Sandoval hint = nr + 1; 10788459642SOmar Sandoval if (hint >= word->depth - 1) 10888459642SOmar Sandoval hint = 0; 10988459642SOmar Sandoval } 11088459642SOmar Sandoval 11188459642SOmar Sandoval return nr; 11288459642SOmar Sandoval } 11388459642SOmar Sandoval 11488459642SOmar Sandoval int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) 11588459642SOmar Sandoval { 11688459642SOmar Sandoval unsigned int i, index; 11788459642SOmar Sandoval int nr = -1; 11888459642SOmar Sandoval 11988459642SOmar Sandoval index = SB_NR_TO_INDEX(sb, alloc_hint); 12088459642SOmar Sandoval 12188459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 12288459642SOmar Sandoval nr = __sbitmap_get_word(&sb->map[index], 12388459642SOmar Sandoval SB_NR_TO_BIT(sb, alloc_hint), 12488459642SOmar Sandoval !round_robin); 12588459642SOmar Sandoval if (nr != -1) { 12688459642SOmar Sandoval nr += index << sb->shift; 12788459642SOmar Sandoval break; 12888459642SOmar Sandoval } 12988459642SOmar Sandoval 13088459642SOmar Sandoval /* Jump to next index. */ 13188459642SOmar Sandoval index++; 13288459642SOmar Sandoval alloc_hint = index << sb->shift; 13388459642SOmar Sandoval 13488459642SOmar Sandoval if (index >= sb->map_nr) { 13588459642SOmar Sandoval index = 0; 13688459642SOmar Sandoval alloc_hint = 0; 13788459642SOmar Sandoval } 13888459642SOmar Sandoval } 13988459642SOmar Sandoval 14088459642SOmar Sandoval return nr; 14188459642SOmar Sandoval } 14288459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_get); 14388459642SOmar Sandoval 14488459642SOmar Sandoval bool sbitmap_any_bit_set(const struct sbitmap *sb) 14588459642SOmar Sandoval { 14688459642SOmar Sandoval unsigned int i; 14788459642SOmar Sandoval 14888459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 14988459642SOmar Sandoval if (sb->map[i].word) 15088459642SOmar Sandoval return true; 15188459642SOmar Sandoval } 15288459642SOmar Sandoval return false; 15388459642SOmar Sandoval } 15488459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); 15588459642SOmar Sandoval 15688459642SOmar Sandoval bool sbitmap_any_bit_clear(const struct sbitmap *sb) 15788459642SOmar Sandoval { 15888459642SOmar Sandoval unsigned int i; 15988459642SOmar Sandoval 16088459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 16188459642SOmar Sandoval const struct sbitmap_word *word = &sb->map[i]; 16288459642SOmar Sandoval unsigned long ret; 16388459642SOmar Sandoval 16488459642SOmar Sandoval ret = find_first_zero_bit(&word->word, word->depth); 16588459642SOmar Sandoval if (ret < word->depth) 16688459642SOmar Sandoval return true; 16788459642SOmar Sandoval } 16888459642SOmar Sandoval return false; 16988459642SOmar Sandoval } 17088459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear); 17188459642SOmar Sandoval 17288459642SOmar Sandoval unsigned int sbitmap_weight(const struct sbitmap *sb) 17388459642SOmar Sandoval { 17460658e0dSColin Ian King unsigned int i, weight = 0; 17588459642SOmar Sandoval 17688459642SOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 17788459642SOmar Sandoval const struct sbitmap_word *word = &sb->map[i]; 17888459642SOmar Sandoval 17988459642SOmar Sandoval weight += bitmap_weight(&word->word, word->depth); 18088459642SOmar Sandoval } 18188459642SOmar Sandoval return weight; 18288459642SOmar Sandoval } 18388459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_weight); 18488459642SOmar Sandoval 18524af1ccfSOmar Sandoval void sbitmap_show(struct sbitmap *sb, struct seq_file *m) 18624af1ccfSOmar Sandoval { 18724af1ccfSOmar Sandoval seq_printf(m, "depth=%u\n", sb->depth); 18824af1ccfSOmar Sandoval seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); 18924af1ccfSOmar Sandoval seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); 19024af1ccfSOmar Sandoval seq_printf(m, "map_nr=%u\n", sb->map_nr); 19124af1ccfSOmar Sandoval } 19224af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_show); 19324af1ccfSOmar Sandoval 19424af1ccfSOmar Sandoval static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) 19524af1ccfSOmar Sandoval { 19624af1ccfSOmar Sandoval if ((offset & 0xf) == 0) { 19724af1ccfSOmar Sandoval if (offset != 0) 19824af1ccfSOmar Sandoval seq_putc(m, '\n'); 19924af1ccfSOmar Sandoval seq_printf(m, "%08x:", offset); 20024af1ccfSOmar Sandoval } 20124af1ccfSOmar Sandoval if ((offset & 0x1) == 0) 20224af1ccfSOmar Sandoval seq_putc(m, ' '); 20324af1ccfSOmar Sandoval seq_printf(m, "%02x", byte); 20424af1ccfSOmar Sandoval } 20524af1ccfSOmar Sandoval 20624af1ccfSOmar Sandoval void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) 20724af1ccfSOmar Sandoval { 20824af1ccfSOmar Sandoval u8 byte = 0; 20924af1ccfSOmar Sandoval unsigned int byte_bits = 0; 21024af1ccfSOmar Sandoval unsigned int offset = 0; 21124af1ccfSOmar Sandoval int i; 21224af1ccfSOmar Sandoval 21324af1ccfSOmar Sandoval for (i = 0; i < sb->map_nr; i++) { 21424af1ccfSOmar Sandoval unsigned long word = READ_ONCE(sb->map[i].word); 21524af1ccfSOmar Sandoval unsigned int word_bits = READ_ONCE(sb->map[i].depth); 21624af1ccfSOmar Sandoval 21724af1ccfSOmar Sandoval while (word_bits > 0) { 21824af1ccfSOmar Sandoval unsigned int bits = min(8 - byte_bits, word_bits); 21924af1ccfSOmar Sandoval 22024af1ccfSOmar Sandoval byte |= (word & (BIT(bits) - 1)) << byte_bits; 22124af1ccfSOmar Sandoval byte_bits += bits; 22224af1ccfSOmar Sandoval if (byte_bits == 8) { 22324af1ccfSOmar Sandoval emit_byte(m, offset, byte); 22424af1ccfSOmar Sandoval byte = 0; 22524af1ccfSOmar Sandoval byte_bits = 0; 22624af1ccfSOmar Sandoval offset++; 22724af1ccfSOmar Sandoval } 22824af1ccfSOmar Sandoval word >>= bits; 22924af1ccfSOmar Sandoval word_bits -= bits; 23024af1ccfSOmar Sandoval } 23124af1ccfSOmar Sandoval } 23224af1ccfSOmar Sandoval if (byte_bits) { 23324af1ccfSOmar Sandoval emit_byte(m, offset, byte); 23424af1ccfSOmar Sandoval offset++; 23524af1ccfSOmar Sandoval } 23624af1ccfSOmar Sandoval if (offset) 23724af1ccfSOmar Sandoval seq_putc(m, '\n'); 23824af1ccfSOmar Sandoval } 23924af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); 24024af1ccfSOmar Sandoval 24188459642SOmar Sandoval static unsigned int sbq_calc_wake_batch(unsigned int depth) 24288459642SOmar Sandoval { 24388459642SOmar Sandoval unsigned int wake_batch; 24488459642SOmar Sandoval 24588459642SOmar Sandoval /* 24688459642SOmar Sandoval * For each batch, we wake up one queue. We need to make sure that our 24788459642SOmar Sandoval * batch size is small enough that the full depth of the bitmap is 24888459642SOmar Sandoval * enough to wake up all of the queues. 24988459642SOmar Sandoval */ 25088459642SOmar Sandoval wake_batch = SBQ_WAKE_BATCH; 25188459642SOmar Sandoval if (wake_batch > depth / SBQ_WAIT_QUEUES) 25288459642SOmar Sandoval wake_batch = max(1U, depth / SBQ_WAIT_QUEUES); 25388459642SOmar Sandoval 25488459642SOmar Sandoval return wake_batch; 25588459642SOmar Sandoval } 25688459642SOmar Sandoval 25788459642SOmar Sandoval int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, 258f4a644dbSOmar Sandoval int shift, bool round_robin, gfp_t flags, int node) 25988459642SOmar Sandoval { 26088459642SOmar Sandoval int ret; 26188459642SOmar Sandoval int i; 26288459642SOmar Sandoval 26388459642SOmar Sandoval ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node); 26488459642SOmar Sandoval if (ret) 26588459642SOmar Sandoval return ret; 26688459642SOmar Sandoval 26740aabb67SOmar Sandoval sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags); 26840aabb67SOmar Sandoval if (!sbq->alloc_hint) { 26940aabb67SOmar Sandoval sbitmap_free(&sbq->sb); 27040aabb67SOmar Sandoval return -ENOMEM; 27140aabb67SOmar Sandoval } 27240aabb67SOmar Sandoval 27398d95416SOmar Sandoval if (depth && !round_robin) { 27498d95416SOmar Sandoval for_each_possible_cpu(i) 27598d95416SOmar Sandoval *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth; 27698d95416SOmar Sandoval } 27798d95416SOmar Sandoval 27888459642SOmar Sandoval sbq->wake_batch = sbq_calc_wake_batch(depth); 27988459642SOmar Sandoval atomic_set(&sbq->wake_index, 0); 28088459642SOmar Sandoval 28148e28166SOmar Sandoval sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); 28288459642SOmar Sandoval if (!sbq->ws) { 28340aabb67SOmar Sandoval free_percpu(sbq->alloc_hint); 28488459642SOmar Sandoval sbitmap_free(&sbq->sb); 28588459642SOmar Sandoval return -ENOMEM; 28688459642SOmar Sandoval } 28788459642SOmar Sandoval 28888459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 28988459642SOmar Sandoval init_waitqueue_head(&sbq->ws[i].wait); 29088459642SOmar Sandoval atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch); 29188459642SOmar Sandoval } 292f4a644dbSOmar Sandoval 293f4a644dbSOmar Sandoval sbq->round_robin = round_robin; 29488459642SOmar Sandoval return 0; 29588459642SOmar Sandoval } 29688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); 29788459642SOmar Sandoval 29888459642SOmar Sandoval void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) 29988459642SOmar Sandoval { 3006c0ca7aeSOmar Sandoval unsigned int wake_batch = sbq_calc_wake_batch(depth); 3016c0ca7aeSOmar Sandoval int i; 3026c0ca7aeSOmar Sandoval 3036c0ca7aeSOmar Sandoval if (sbq->wake_batch != wake_batch) { 3046c0ca7aeSOmar Sandoval WRITE_ONCE(sbq->wake_batch, wake_batch); 3056c0ca7aeSOmar Sandoval /* 3066c0ca7aeSOmar Sandoval * Pairs with the memory barrier in sbq_wake_up() to ensure that 3076c0ca7aeSOmar Sandoval * the batch size is updated before the wait counts. 3086c0ca7aeSOmar Sandoval */ 3096c0ca7aeSOmar Sandoval smp_mb__before_atomic(); 3106c0ca7aeSOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) 3116c0ca7aeSOmar Sandoval atomic_set(&sbq->ws[i].wait_cnt, 1); 3126c0ca7aeSOmar Sandoval } 31388459642SOmar Sandoval sbitmap_resize(&sbq->sb, depth); 31488459642SOmar Sandoval } 31588459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_resize); 31688459642SOmar Sandoval 317f4a644dbSOmar Sandoval int __sbitmap_queue_get(struct sbitmap_queue *sbq) 31840aabb67SOmar Sandoval { 31905fd095dSOmar Sandoval unsigned int hint, depth; 32040aabb67SOmar Sandoval int nr; 32140aabb67SOmar Sandoval 32240aabb67SOmar Sandoval hint = this_cpu_read(*sbq->alloc_hint); 32305fd095dSOmar Sandoval depth = READ_ONCE(sbq->sb.depth); 32405fd095dSOmar Sandoval if (unlikely(hint >= depth)) { 32505fd095dSOmar Sandoval hint = depth ? prandom_u32() % depth : 0; 32605fd095dSOmar Sandoval this_cpu_write(*sbq->alloc_hint, hint); 32705fd095dSOmar Sandoval } 328f4a644dbSOmar Sandoval nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin); 32940aabb67SOmar Sandoval 33040aabb67SOmar Sandoval if (nr == -1) { 33140aabb67SOmar Sandoval /* If the map is full, a hint won't do us much good. */ 33240aabb67SOmar Sandoval this_cpu_write(*sbq->alloc_hint, 0); 333f4a644dbSOmar Sandoval } else if (nr == hint || unlikely(sbq->round_robin)) { 33440aabb67SOmar Sandoval /* Only update the hint if we used it. */ 33540aabb67SOmar Sandoval hint = nr + 1; 33605fd095dSOmar Sandoval if (hint >= depth - 1) 33740aabb67SOmar Sandoval hint = 0; 33840aabb67SOmar Sandoval this_cpu_write(*sbq->alloc_hint, hint); 33940aabb67SOmar Sandoval } 34040aabb67SOmar Sandoval 34140aabb67SOmar Sandoval return nr; 34240aabb67SOmar Sandoval } 34340aabb67SOmar Sandoval EXPORT_SYMBOL_GPL(__sbitmap_queue_get); 34440aabb67SOmar Sandoval 34588459642SOmar Sandoval static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) 34688459642SOmar Sandoval { 34788459642SOmar Sandoval int i, wake_index; 34888459642SOmar Sandoval 34988459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 35088459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 35188459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 35288459642SOmar Sandoval 35388459642SOmar Sandoval if (waitqueue_active(&ws->wait)) { 35488459642SOmar Sandoval int o = atomic_read(&sbq->wake_index); 35588459642SOmar Sandoval 35688459642SOmar Sandoval if (wake_index != o) 35788459642SOmar Sandoval atomic_cmpxchg(&sbq->wake_index, o, wake_index); 35888459642SOmar Sandoval return ws; 35988459642SOmar Sandoval } 36088459642SOmar Sandoval 36188459642SOmar Sandoval wake_index = sbq_index_inc(wake_index); 36288459642SOmar Sandoval } 36388459642SOmar Sandoval 36488459642SOmar Sandoval return NULL; 36588459642SOmar Sandoval } 36688459642SOmar Sandoval 36788459642SOmar Sandoval static void sbq_wake_up(struct sbitmap_queue *sbq) 36888459642SOmar Sandoval { 36988459642SOmar Sandoval struct sbq_wait_state *ws; 3706c0ca7aeSOmar Sandoval unsigned int wake_batch; 37188459642SOmar Sandoval int wait_cnt; 37288459642SOmar Sandoval 373f66227deSOmar Sandoval /* 374f66227deSOmar Sandoval * Pairs with the memory barrier in set_current_state() to ensure the 375f66227deSOmar Sandoval * proper ordering of clear_bit()/waitqueue_active() in the waker and 376f66227deSOmar Sandoval * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See 377f66227deSOmar Sandoval * the comment on waitqueue_active(). This is __after_atomic because we 378f66227deSOmar Sandoval * just did clear_bit() in the caller. 379f66227deSOmar Sandoval */ 380f66227deSOmar Sandoval smp_mb__after_atomic(); 38188459642SOmar Sandoval 38288459642SOmar Sandoval ws = sbq_wake_ptr(sbq); 38388459642SOmar Sandoval if (!ws) 38488459642SOmar Sandoval return; 38588459642SOmar Sandoval 38688459642SOmar Sandoval wait_cnt = atomic_dec_return(&ws->wait_cnt); 3876c0ca7aeSOmar Sandoval if (wait_cnt <= 0) { 3886c0ca7aeSOmar Sandoval wake_batch = READ_ONCE(sbq->wake_batch); 3896c0ca7aeSOmar Sandoval /* 3906c0ca7aeSOmar Sandoval * Pairs with the memory barrier in sbitmap_queue_resize() to 3916c0ca7aeSOmar Sandoval * ensure that we see the batch size update before the wait 3926c0ca7aeSOmar Sandoval * count is reset. 3936c0ca7aeSOmar Sandoval */ 3946c0ca7aeSOmar Sandoval smp_mb__before_atomic(); 3956c0ca7aeSOmar Sandoval /* 3966c0ca7aeSOmar Sandoval * If there are concurrent callers to sbq_wake_up(), the last 3976c0ca7aeSOmar Sandoval * one to decrement the wait count below zero will bump it back 3986c0ca7aeSOmar Sandoval * up. If there is a concurrent resize, the count reset will 3996c0ca7aeSOmar Sandoval * either cause the cmpxchg to fail or overwrite after the 4006c0ca7aeSOmar Sandoval * cmpxchg. 4016c0ca7aeSOmar Sandoval */ 4026c0ca7aeSOmar Sandoval atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch); 40388459642SOmar Sandoval sbq_index_atomic_inc(&sbq->wake_index); 40488459642SOmar Sandoval wake_up(&ws->wait); 40588459642SOmar Sandoval } 40688459642SOmar Sandoval } 40788459642SOmar Sandoval 40840aabb67SOmar Sandoval void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, 409f4a644dbSOmar Sandoval unsigned int cpu) 41088459642SOmar Sandoval { 41188459642SOmar Sandoval sbitmap_clear_bit(&sbq->sb, nr); 41288459642SOmar Sandoval sbq_wake_up(sbq); 4135c64a8dfSOmar Sandoval if (likely(!sbq->round_robin && nr < sbq->sb.depth)) 41440aabb67SOmar Sandoval *per_cpu_ptr(sbq->alloc_hint, cpu) = nr; 41588459642SOmar Sandoval } 41688459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_clear); 41788459642SOmar Sandoval 41888459642SOmar Sandoval void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) 41988459642SOmar Sandoval { 42088459642SOmar Sandoval int i, wake_index; 42188459642SOmar Sandoval 42288459642SOmar Sandoval /* 423f66227deSOmar Sandoval * Pairs with the memory barrier in set_current_state() like in 424f66227deSOmar Sandoval * sbq_wake_up(). 42588459642SOmar Sandoval */ 42688459642SOmar Sandoval smp_mb(); 42788459642SOmar Sandoval wake_index = atomic_read(&sbq->wake_index); 42888459642SOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 42988459642SOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[wake_index]; 43088459642SOmar Sandoval 43188459642SOmar Sandoval if (waitqueue_active(&ws->wait)) 43288459642SOmar Sandoval wake_up(&ws->wait); 43388459642SOmar Sandoval 43488459642SOmar Sandoval wake_index = sbq_index_inc(wake_index); 43588459642SOmar Sandoval } 43688459642SOmar Sandoval } 43788459642SOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); 43824af1ccfSOmar Sandoval 43924af1ccfSOmar Sandoval void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) 44024af1ccfSOmar Sandoval { 44124af1ccfSOmar Sandoval bool first; 44224af1ccfSOmar Sandoval int i; 44324af1ccfSOmar Sandoval 44424af1ccfSOmar Sandoval sbitmap_show(&sbq->sb, m); 44524af1ccfSOmar Sandoval 44624af1ccfSOmar Sandoval seq_puts(m, "alloc_hint={"); 44724af1ccfSOmar Sandoval first = true; 44824af1ccfSOmar Sandoval for_each_possible_cpu(i) { 44924af1ccfSOmar Sandoval if (!first) 45024af1ccfSOmar Sandoval seq_puts(m, ", "); 45124af1ccfSOmar Sandoval first = false; 45224af1ccfSOmar Sandoval seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i)); 45324af1ccfSOmar Sandoval } 45424af1ccfSOmar Sandoval seq_puts(m, "}\n"); 45524af1ccfSOmar Sandoval 45624af1ccfSOmar Sandoval seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); 45724af1ccfSOmar Sandoval seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); 45824af1ccfSOmar Sandoval 45924af1ccfSOmar Sandoval seq_puts(m, "ws={\n"); 46024af1ccfSOmar Sandoval for (i = 0; i < SBQ_WAIT_QUEUES; i++) { 46124af1ccfSOmar Sandoval struct sbq_wait_state *ws = &sbq->ws[i]; 46224af1ccfSOmar Sandoval 46324af1ccfSOmar Sandoval seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n", 46424af1ccfSOmar Sandoval atomic_read(&ws->wait_cnt), 46524af1ccfSOmar Sandoval waitqueue_active(&ws->wait) ? "active" : "inactive"); 46624af1ccfSOmar Sandoval } 46724af1ccfSOmar Sandoval seq_puts(m, "}\n"); 46824af1ccfSOmar Sandoval 46924af1ccfSOmar Sandoval seq_printf(m, "round_robin=%d\n", sbq->round_robin); 47024af1ccfSOmar Sandoval } 47124af1ccfSOmar Sandoval EXPORT_SYMBOL_GPL(sbitmap_queue_show); 472