Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Facebook |
| 3 | * Copyright (C) 2013-2014 Jens Axboe |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public |
| 7 | * License v2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Ingo Molnar | af8601a | 2017-02-03 09:57:00 +0100 | [diff] [blame] | 18 | #include <linux/sched.h> |
Omar Sandoval | 98d9541 | 2016-09-17 01:28:25 -0700 | [diff] [blame] | 19 | #include <linux/random.h> |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 20 | #include <linux/sbitmap.h> |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 22 | |
| 23 | int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, |
| 24 | gfp_t flags, int node) |
| 25 | { |
| 26 | unsigned int bits_per_word; |
| 27 | unsigned int i; |
| 28 | |
| 29 | if (shift < 0) { |
| 30 | shift = ilog2(BITS_PER_LONG); |
| 31 | /* |
| 32 | * If the bitmap is small, shrink the number of bits per word so |
| 33 | * we spread over a few cachelines, at least. If less than 4 |
| 34 | * bits, just forget about it, it's not going to work optimally |
| 35 | * anyway. |
| 36 | */ |
| 37 | if (depth >= 4) { |
| 38 | while ((4U << shift) > depth) |
| 39 | shift--; |
| 40 | } |
| 41 | } |
| 42 | bits_per_word = 1U << shift; |
| 43 | if (bits_per_word > BITS_PER_LONG) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | sb->shift = shift; |
| 47 | sb->depth = depth; |
| 48 | sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); |
| 49 | |
| 50 | if (depth == 0) { |
| 51 | sb->map = NULL; |
| 52 | return 0; |
| 53 | } |
| 54 | |
Kees Cook | 590b5b7 | 2018-06-12 14:04:20 -0700 | [diff] [blame] | 55 | sb->map = kcalloc_node(sb->map_nr, sizeof(*sb->map), flags, node); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 56 | if (!sb->map) |
| 57 | return -ENOMEM; |
| 58 | |
| 59 | for (i = 0; i < sb->map_nr; i++) { |
| 60 | sb->map[i].depth = min(depth, bits_per_word); |
| 61 | depth -= sb->map[i].depth; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(sbitmap_init_node); |
| 66 | |
| 67 | void sbitmap_resize(struct sbitmap *sb, unsigned int depth) |
| 68 | { |
| 69 | unsigned int bits_per_word = 1U << sb->shift; |
| 70 | unsigned int i; |
| 71 | |
| 72 | sb->depth = depth; |
| 73 | sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); |
| 74 | |
| 75 | for (i = 0; i < sb->map_nr; i++) { |
| 76 | sb->map[i].depth = min(depth, bits_per_word); |
| 77 | depth -= sb->map[i].depth; |
| 78 | } |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(sbitmap_resize); |
| 81 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 82 | static int __sbitmap_get_word(unsigned long *word, unsigned long depth, |
| 83 | unsigned int hint, bool wrap) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 84 | { |
| 85 | unsigned int orig_hint = hint; |
| 86 | int nr; |
| 87 | |
| 88 | while (1) { |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 89 | nr = find_next_zero_bit(word, depth, hint); |
| 90 | if (unlikely(nr >= depth)) { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 91 | /* |
| 92 | * We started with an offset, and we didn't reset the |
| 93 | * offset to 0 in a failure case, so start from 0 to |
| 94 | * exhaust the map. |
| 95 | */ |
| 96 | if (orig_hint && hint && wrap) { |
| 97 | hint = orig_hint = 0; |
| 98 | continue; |
| 99 | } |
| 100 | return -1; |
| 101 | } |
| 102 | |
Omar Sandoval | 4ace53f | 2018-02-27 16:56:43 -0800 | [diff] [blame] | 103 | if (!test_and_set_bit_lock(nr, word)) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | hint = nr + 1; |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 107 | if (hint >= depth - 1) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 108 | hint = 0; |
| 109 | } |
| 110 | |
| 111 | return nr; |
| 112 | } |
| 113 | |
| 114 | int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) |
| 115 | { |
| 116 | unsigned int i, index; |
| 117 | int nr = -1; |
| 118 | |
| 119 | index = SB_NR_TO_INDEX(sb, alloc_hint); |
| 120 | |
Jens Axboe | 27fae42 | 2018-11-29 12:35:16 -0700 | [diff] [blame^] | 121 | /* |
| 122 | * Unless we're doing round robin tag allocation, just use the |
| 123 | * alloc_hint to find the right word index. No point in looping |
| 124 | * twice in find_next_zero_bit() for that case. |
| 125 | */ |
| 126 | if (round_robin) |
| 127 | alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); |
| 128 | else |
| 129 | alloc_hint = 0; |
| 130 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 131 | for (i = 0; i < sb->map_nr; i++) { |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 132 | nr = __sbitmap_get_word(&sb->map[index].word, |
Jens Axboe | 27fae42 | 2018-11-29 12:35:16 -0700 | [diff] [blame^] | 133 | sb->map[index].depth, alloc_hint, |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 134 | !round_robin); |
| 135 | if (nr != -1) { |
| 136 | nr += index << sb->shift; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | /* Jump to next index. */ |
Jens Axboe | 27fae42 | 2018-11-29 12:35:16 -0700 | [diff] [blame^] | 141 | alloc_hint = 0; |
| 142 | if (++index >= sb->map_nr) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 143 | index = 0; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return nr; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(sbitmap_get); |
| 149 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 150 | int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint, |
| 151 | unsigned long shallow_depth) |
| 152 | { |
| 153 | unsigned int i, index; |
| 154 | int nr = -1; |
| 155 | |
| 156 | index = SB_NR_TO_INDEX(sb, alloc_hint); |
| 157 | |
| 158 | for (i = 0; i < sb->map_nr; i++) { |
| 159 | nr = __sbitmap_get_word(&sb->map[index].word, |
| 160 | min(sb->map[index].depth, shallow_depth), |
| 161 | SB_NR_TO_BIT(sb, alloc_hint), true); |
| 162 | if (nr != -1) { |
| 163 | nr += index << sb->shift; |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | /* Jump to next index. */ |
| 168 | index++; |
| 169 | alloc_hint = index << sb->shift; |
| 170 | |
| 171 | if (index >= sb->map_nr) { |
| 172 | index = 0; |
| 173 | alloc_hint = 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return nr; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(sbitmap_get_shallow); |
| 180 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 181 | bool sbitmap_any_bit_set(const struct sbitmap *sb) |
| 182 | { |
| 183 | unsigned int i; |
| 184 | |
| 185 | for (i = 0; i < sb->map_nr; i++) { |
| 186 | if (sb->map[i].word) |
| 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); |
| 192 | |
| 193 | bool sbitmap_any_bit_clear(const struct sbitmap *sb) |
| 194 | { |
| 195 | unsigned int i; |
| 196 | |
| 197 | for (i = 0; i < sb->map_nr; i++) { |
| 198 | const struct sbitmap_word *word = &sb->map[i]; |
| 199 | unsigned long ret; |
| 200 | |
| 201 | ret = find_first_zero_bit(&word->word, word->depth); |
| 202 | if (ret < word->depth) |
| 203 | return true; |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear); |
| 208 | |
| 209 | unsigned int sbitmap_weight(const struct sbitmap *sb) |
| 210 | { |
Colin Ian King | 60658e0 | 2016-09-19 14:34:08 +0100 | [diff] [blame] | 211 | unsigned int i, weight = 0; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 212 | |
| 213 | for (i = 0; i < sb->map_nr; i++) { |
| 214 | const struct sbitmap_word *word = &sb->map[i]; |
| 215 | |
| 216 | weight += bitmap_weight(&word->word, word->depth); |
| 217 | } |
| 218 | return weight; |
| 219 | } |
| 220 | EXPORT_SYMBOL_GPL(sbitmap_weight); |
| 221 | |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 222 | void sbitmap_show(struct sbitmap *sb, struct seq_file *m) |
| 223 | { |
| 224 | seq_printf(m, "depth=%u\n", sb->depth); |
| 225 | seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); |
| 226 | seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); |
| 227 | seq_printf(m, "map_nr=%u\n", sb->map_nr); |
| 228 | } |
| 229 | EXPORT_SYMBOL_GPL(sbitmap_show); |
| 230 | |
| 231 | static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) |
| 232 | { |
| 233 | if ((offset & 0xf) == 0) { |
| 234 | if (offset != 0) |
| 235 | seq_putc(m, '\n'); |
| 236 | seq_printf(m, "%08x:", offset); |
| 237 | } |
| 238 | if ((offset & 0x1) == 0) |
| 239 | seq_putc(m, ' '); |
| 240 | seq_printf(m, "%02x", byte); |
| 241 | } |
| 242 | |
| 243 | void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) |
| 244 | { |
| 245 | u8 byte = 0; |
| 246 | unsigned int byte_bits = 0; |
| 247 | unsigned int offset = 0; |
| 248 | int i; |
| 249 | |
| 250 | for (i = 0; i < sb->map_nr; i++) { |
| 251 | unsigned long word = READ_ONCE(sb->map[i].word); |
| 252 | unsigned int word_bits = READ_ONCE(sb->map[i].depth); |
| 253 | |
| 254 | while (word_bits > 0) { |
| 255 | unsigned int bits = min(8 - byte_bits, word_bits); |
| 256 | |
| 257 | byte |= (word & (BIT(bits) - 1)) << byte_bits; |
| 258 | byte_bits += bits; |
| 259 | if (byte_bits == 8) { |
| 260 | emit_byte(m, offset, byte); |
| 261 | byte = 0; |
| 262 | byte_bits = 0; |
| 263 | offset++; |
| 264 | } |
| 265 | word >>= bits; |
| 266 | word_bits -= bits; |
| 267 | } |
| 268 | } |
| 269 | if (byte_bits) { |
| 270 | emit_byte(m, offset, byte); |
| 271 | offset++; |
| 272 | } |
| 273 | if (offset) |
| 274 | seq_putc(m, '\n'); |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); |
| 277 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 278 | static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq, |
| 279 | unsigned int depth) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 280 | { |
| 281 | unsigned int wake_batch; |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 282 | unsigned int shallow_depth; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * For each batch, we wake up one queue. We need to make sure that our |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 286 | * batch size is small enough that the full depth of the bitmap, |
| 287 | * potentially limited by a shallow depth, is enough to wake up all of |
| 288 | * the queues. |
| 289 | * |
| 290 | * Each full word of the bitmap has bits_per_word bits, and there might |
| 291 | * be a partial word. There are depth / bits_per_word full words and |
| 292 | * depth % bits_per_word bits left over. In bitwise arithmetic: |
| 293 | * |
| 294 | * bits_per_word = 1 << shift |
| 295 | * depth / bits_per_word = depth >> shift |
| 296 | * depth % bits_per_word = depth & ((1 << shift) - 1) |
| 297 | * |
| 298 | * Each word can be limited to sbq->min_shallow_depth bits. |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 299 | */ |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 300 | shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth); |
| 301 | depth = ((depth >> sbq->sb.shift) * shallow_depth + |
| 302 | min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth)); |
| 303 | wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1, |
| 304 | SBQ_WAKE_BATCH); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 305 | |
| 306 | return wake_batch; |
| 307 | } |
| 308 | |
| 309 | int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 310 | int shift, bool round_robin, gfp_t flags, int node) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 311 | { |
| 312 | int ret; |
| 313 | int i; |
| 314 | |
| 315 | ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node); |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 319 | sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags); |
| 320 | if (!sbq->alloc_hint) { |
| 321 | sbitmap_free(&sbq->sb); |
| 322 | return -ENOMEM; |
| 323 | } |
| 324 | |
Omar Sandoval | 98d9541 | 2016-09-17 01:28:25 -0700 | [diff] [blame] | 325 | if (depth && !round_robin) { |
| 326 | for_each_possible_cpu(i) |
| 327 | *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth; |
| 328 | } |
| 329 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 330 | sbq->min_shallow_depth = UINT_MAX; |
| 331 | sbq->wake_batch = sbq_calc_wake_batch(sbq, depth); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 332 | atomic_set(&sbq->wake_index, 0); |
| 333 | |
Omar Sandoval | 48e2816 | 2016-09-17 01:28:22 -0700 | [diff] [blame] | 334 | sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 335 | if (!sbq->ws) { |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 336 | free_percpu(sbq->alloc_hint); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 337 | sbitmap_free(&sbq->sb); |
| 338 | return -ENOMEM; |
| 339 | } |
| 340 | |
| 341 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 342 | init_waitqueue_head(&sbq->ws[i].wait); |
| 343 | atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch); |
| 344 | } |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 345 | |
| 346 | sbq->round_robin = round_robin; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); |
| 350 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 351 | static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq, |
| 352 | unsigned int depth) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 353 | { |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 354 | unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth); |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 355 | int i; |
| 356 | |
| 357 | if (sbq->wake_batch != wake_batch) { |
| 358 | WRITE_ONCE(sbq->wake_batch, wake_batch); |
| 359 | /* |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 360 | * Pairs with the memory barrier in sbitmap_queue_wake_up() |
| 361 | * to ensure that the batch size is updated before the wait |
| 362 | * counts. |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 363 | */ |
| 364 | smp_mb__before_atomic(); |
| 365 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) |
| 366 | atomic_set(&sbq->ws[i].wait_cnt, 1); |
| 367 | } |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) |
| 371 | { |
| 372 | sbitmap_queue_update_wake_batch(sbq, depth); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 373 | sbitmap_resize(&sbq->sb, depth); |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(sbitmap_queue_resize); |
| 376 | |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 377 | int __sbitmap_queue_get(struct sbitmap_queue *sbq) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 378 | { |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 379 | unsigned int hint, depth; |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 380 | int nr; |
| 381 | |
| 382 | hint = this_cpu_read(*sbq->alloc_hint); |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 383 | depth = READ_ONCE(sbq->sb.depth); |
| 384 | if (unlikely(hint >= depth)) { |
| 385 | hint = depth ? prandom_u32() % depth : 0; |
| 386 | this_cpu_write(*sbq->alloc_hint, hint); |
| 387 | } |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 388 | nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin); |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 389 | |
| 390 | if (nr == -1) { |
| 391 | /* If the map is full, a hint won't do us much good. */ |
| 392 | this_cpu_write(*sbq->alloc_hint, 0); |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 393 | } else if (nr == hint || unlikely(sbq->round_robin)) { |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 394 | /* Only update the hint if we used it. */ |
| 395 | hint = nr + 1; |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 396 | if (hint >= depth - 1) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 397 | hint = 0; |
| 398 | this_cpu_write(*sbq->alloc_hint, hint); |
| 399 | } |
| 400 | |
| 401 | return nr; |
| 402 | } |
| 403 | EXPORT_SYMBOL_GPL(__sbitmap_queue_get); |
| 404 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 405 | int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, |
| 406 | unsigned int shallow_depth) |
| 407 | { |
| 408 | unsigned int hint, depth; |
| 409 | int nr; |
| 410 | |
Omar Sandoval | 61445b56 | 2018-05-09 17:29:24 -0700 | [diff] [blame] | 411 | WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth); |
| 412 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 413 | hint = this_cpu_read(*sbq->alloc_hint); |
| 414 | depth = READ_ONCE(sbq->sb.depth); |
| 415 | if (unlikely(hint >= depth)) { |
| 416 | hint = depth ? prandom_u32() % depth : 0; |
| 417 | this_cpu_write(*sbq->alloc_hint, hint); |
| 418 | } |
| 419 | nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth); |
| 420 | |
| 421 | if (nr == -1) { |
| 422 | /* If the map is full, a hint won't do us much good. */ |
| 423 | this_cpu_write(*sbq->alloc_hint, 0); |
| 424 | } else if (nr == hint || unlikely(sbq->round_robin)) { |
| 425 | /* Only update the hint if we used it. */ |
| 426 | hint = nr + 1; |
| 427 | if (hint >= depth - 1) |
| 428 | hint = 0; |
| 429 | this_cpu_write(*sbq->alloc_hint, hint); |
| 430 | } |
| 431 | |
| 432 | return nr; |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow); |
| 435 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 436 | void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, |
| 437 | unsigned int min_shallow_depth) |
| 438 | { |
| 439 | sbq->min_shallow_depth = min_shallow_depth; |
| 440 | sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth); |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); |
| 443 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 444 | static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) |
| 445 | { |
| 446 | int i, wake_index; |
| 447 | |
| 448 | wake_index = atomic_read(&sbq->wake_index); |
| 449 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 450 | struct sbq_wait_state *ws = &sbq->ws[wake_index]; |
| 451 | |
| 452 | if (waitqueue_active(&ws->wait)) { |
| 453 | int o = atomic_read(&sbq->wake_index); |
| 454 | |
| 455 | if (wake_index != o) |
| 456 | atomic_cmpxchg(&sbq->wake_index, o, wake_index); |
| 457 | return ws; |
| 458 | } |
| 459 | |
| 460 | wake_index = sbq_index_inc(wake_index); |
| 461 | } |
| 462 | |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 466 | static bool __sbq_wake_up(struct sbitmap_queue *sbq) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 467 | { |
| 468 | struct sbq_wait_state *ws; |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 469 | unsigned int wake_batch; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 470 | int wait_cnt; |
| 471 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 472 | ws = sbq_wake_ptr(sbq); |
| 473 | if (!ws) |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 474 | return false; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 475 | |
| 476 | wait_cnt = atomic_dec_return(&ws->wait_cnt); |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 477 | if (wait_cnt <= 0) { |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 478 | int ret; |
| 479 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 480 | wake_batch = READ_ONCE(sbq->wake_batch); |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 481 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 482 | /* |
| 483 | * Pairs with the memory barrier in sbitmap_queue_resize() to |
| 484 | * ensure that we see the batch size update before the wait |
| 485 | * count is reset. |
| 486 | */ |
| 487 | smp_mb__before_atomic(); |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 488 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 489 | /* |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 490 | * For concurrent callers of this, the one that failed the |
| 491 | * atomic_cmpxhcg() race should call this function again |
| 492 | * to wakeup a new batch on a different 'ws'. |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 493 | */ |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 494 | ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch); |
| 495 | if (ret == wait_cnt) { |
| 496 | sbq_index_atomic_inc(&sbq->wake_index); |
| 497 | wake_up_nr(&ws->wait, wake_batch); |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | return true; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 502 | } |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 507 | void sbitmap_queue_wake_up(struct sbitmap_queue *sbq) |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 508 | { |
| 509 | while (__sbq_wake_up(sbq)) |
| 510 | ; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 511 | } |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 512 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 513 | |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 514 | void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 515 | unsigned int cpu) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 516 | { |
Omar Sandoval | 4ace53f | 2018-02-27 16:56:43 -0800 | [diff] [blame] | 517 | sbitmap_clear_bit_unlock(&sbq->sb, nr); |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 518 | /* |
| 519 | * Pairs with the memory barrier in set_current_state() to ensure the |
| 520 | * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker |
| 521 | * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the |
| 522 | * waiter. See the comment on waitqueue_active(). |
| 523 | */ |
| 524 | smp_mb__after_atomic(); |
| 525 | sbitmap_queue_wake_up(sbq); |
| 526 | |
Omar Sandoval | 5c64a8d | 2016-09-17 12:20:54 -0700 | [diff] [blame] | 527 | if (likely(!sbq->round_robin && nr < sbq->sb.depth)) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 528 | *per_cpu_ptr(sbq->alloc_hint, cpu) = nr; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 529 | } |
| 530 | EXPORT_SYMBOL_GPL(sbitmap_queue_clear); |
| 531 | |
| 532 | void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) |
| 533 | { |
| 534 | int i, wake_index; |
| 535 | |
| 536 | /* |
Omar Sandoval | f66227d | 2017-01-18 11:55:21 -0800 | [diff] [blame] | 537 | * Pairs with the memory barrier in set_current_state() like in |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 538 | * sbitmap_queue_wake_up(). |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 539 | */ |
| 540 | smp_mb(); |
| 541 | wake_index = atomic_read(&sbq->wake_index); |
| 542 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 543 | struct sbq_wait_state *ws = &sbq->ws[wake_index]; |
| 544 | |
| 545 | if (waitqueue_active(&ws->wait)) |
| 546 | wake_up(&ws->wait); |
| 547 | |
| 548 | wake_index = sbq_index_inc(wake_index); |
| 549 | } |
| 550 | } |
| 551 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 552 | |
| 553 | void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) |
| 554 | { |
| 555 | bool first; |
| 556 | int i; |
| 557 | |
| 558 | sbitmap_show(&sbq->sb, m); |
| 559 | |
| 560 | seq_puts(m, "alloc_hint={"); |
| 561 | first = true; |
| 562 | for_each_possible_cpu(i) { |
| 563 | if (!first) |
| 564 | seq_puts(m, ", "); |
| 565 | first = false; |
| 566 | seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i)); |
| 567 | } |
| 568 | seq_puts(m, "}\n"); |
| 569 | |
| 570 | seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); |
| 571 | seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); |
| 572 | |
| 573 | seq_puts(m, "ws={\n"); |
| 574 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 575 | struct sbq_wait_state *ws = &sbq->ws[i]; |
| 576 | |
| 577 | seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n", |
| 578 | atomic_read(&ws->wait_cnt), |
| 579 | waitqueue_active(&ws->wait) ? "active" : "inactive"); |
| 580 | } |
| 581 | seq_puts(m, "}\n"); |
| 582 | |
| 583 | seq_printf(m, "round_robin=%d\n", sbq->round_robin); |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 584 | seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 585 | } |
| 586 | EXPORT_SYMBOL_GPL(sbitmap_queue_show); |