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; |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 62 | spin_lock_init(&sb->map[i].swap_lock); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(sbitmap_init_node); |
| 67 | |
| 68 | void sbitmap_resize(struct sbitmap *sb, unsigned int depth) |
| 69 | { |
| 70 | unsigned int bits_per_word = 1U << sb->shift; |
| 71 | unsigned int i; |
| 72 | |
| 73 | sb->depth = depth; |
| 74 | sb->map_nr = DIV_ROUND_UP(sb->depth, bits_per_word); |
| 75 | |
| 76 | for (i = 0; i < sb->map_nr; i++) { |
| 77 | sb->map[i].depth = min(depth, bits_per_word); |
| 78 | depth -= sb->map[i].depth; |
| 79 | } |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(sbitmap_resize); |
| 82 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 83 | static int __sbitmap_get_word(unsigned long *word, unsigned long depth, |
| 84 | unsigned int hint, bool wrap) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 85 | { |
| 86 | unsigned int orig_hint = hint; |
| 87 | int nr; |
| 88 | |
| 89 | while (1) { |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 90 | nr = find_next_zero_bit(word, depth, hint); |
| 91 | if (unlikely(nr >= depth)) { |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 92 | /* |
| 93 | * We started with an offset, and we didn't reset the |
| 94 | * offset to 0 in a failure case, so start from 0 to |
| 95 | * exhaust the map. |
| 96 | */ |
| 97 | if (orig_hint && hint && wrap) { |
| 98 | hint = orig_hint = 0; |
| 99 | continue; |
| 100 | } |
| 101 | return -1; |
| 102 | } |
| 103 | |
Omar Sandoval | 4ace53f | 2018-02-27 16:56:43 -0800 | [diff] [blame] | 104 | if (!test_and_set_bit_lock(nr, word)) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 105 | break; |
| 106 | |
| 107 | hint = nr + 1; |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 108 | if (hint >= depth - 1) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 109 | hint = 0; |
| 110 | } |
| 111 | |
| 112 | return nr; |
| 113 | } |
| 114 | |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 115 | /* |
| 116 | * See if we have deferred clears that we can batch move |
| 117 | */ |
| 118 | static inline bool sbitmap_deferred_clear(struct sbitmap *sb, int index) |
| 119 | { |
| 120 | unsigned long mask, val; |
| 121 | bool ret = false; |
| 122 | |
| 123 | spin_lock(&sb->map[index].swap_lock); |
| 124 | |
| 125 | if (!sb->map[index].cleared) |
| 126 | goto out_unlock; |
| 127 | |
| 128 | /* |
| 129 | * First get a stable cleared mask, setting the old mask to 0. |
| 130 | */ |
| 131 | do { |
| 132 | mask = sb->map[index].cleared; |
| 133 | } while (cmpxchg(&sb->map[index].cleared, mask, 0) != mask); |
| 134 | |
| 135 | /* |
| 136 | * Now clear the masked bits in our free word |
| 137 | */ |
| 138 | do { |
| 139 | val = sb->map[index].word; |
| 140 | } while (cmpxchg(&sb->map[index].word, val, val & ~mask) != val); |
| 141 | |
| 142 | ret = true; |
| 143 | out_unlock: |
| 144 | spin_unlock(&sb->map[index].swap_lock); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int sbitmap_find_bit_in_index(struct sbitmap *sb, int index, |
| 149 | unsigned int alloc_hint, bool round_robin) |
| 150 | { |
| 151 | int nr; |
| 152 | |
| 153 | do { |
| 154 | nr = __sbitmap_get_word(&sb->map[index].word, |
| 155 | sb->map[index].depth, alloc_hint, |
| 156 | !round_robin); |
| 157 | if (nr != -1) |
| 158 | break; |
| 159 | if (!sbitmap_deferred_clear(sb, index)) |
| 160 | break; |
| 161 | } while (1); |
| 162 | |
| 163 | return nr; |
| 164 | } |
| 165 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 166 | int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin) |
| 167 | { |
| 168 | unsigned int i, index; |
| 169 | int nr = -1; |
| 170 | |
| 171 | index = SB_NR_TO_INDEX(sb, alloc_hint); |
| 172 | |
Jens Axboe | 27fae42 | 2018-11-29 12:35:16 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Unless we're doing round robin tag allocation, just use the |
| 175 | * alloc_hint to find the right word index. No point in looping |
| 176 | * twice in find_next_zero_bit() for that case. |
| 177 | */ |
| 178 | if (round_robin) |
| 179 | alloc_hint = SB_NR_TO_BIT(sb, alloc_hint); |
| 180 | else |
| 181 | alloc_hint = 0; |
| 182 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 183 | for (i = 0; i < sb->map_nr; i++) { |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 184 | nr = sbitmap_find_bit_in_index(sb, index, alloc_hint, |
| 185 | round_robin); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 186 | if (nr != -1) { |
| 187 | nr += index << sb->shift; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | /* Jump to next index. */ |
Jens Axboe | 27fae42 | 2018-11-29 12:35:16 -0700 | [diff] [blame] | 192 | alloc_hint = 0; |
| 193 | if (++index >= sb->map_nr) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 194 | index = 0; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return nr; |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(sbitmap_get); |
| 200 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 201 | int sbitmap_get_shallow(struct sbitmap *sb, unsigned int alloc_hint, |
| 202 | unsigned long shallow_depth) |
| 203 | { |
| 204 | unsigned int i, index; |
| 205 | int nr = -1; |
| 206 | |
| 207 | index = SB_NR_TO_INDEX(sb, alloc_hint); |
| 208 | |
| 209 | for (i = 0; i < sb->map_nr; i++) { |
| 210 | nr = __sbitmap_get_word(&sb->map[index].word, |
| 211 | min(sb->map[index].depth, shallow_depth), |
| 212 | SB_NR_TO_BIT(sb, alloc_hint), true); |
| 213 | if (nr != -1) { |
| 214 | nr += index << sb->shift; |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | /* Jump to next index. */ |
| 219 | index++; |
| 220 | alloc_hint = index << sb->shift; |
| 221 | |
| 222 | if (index >= sb->map_nr) { |
| 223 | index = 0; |
| 224 | alloc_hint = 0; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return nr; |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(sbitmap_get_shallow); |
| 231 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 232 | bool sbitmap_any_bit_set(const struct sbitmap *sb) |
| 233 | { |
| 234 | unsigned int i; |
| 235 | |
| 236 | for (i = 0; i < sb->map_nr; i++) { |
| 237 | if (sb->map[i].word) |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | EXPORT_SYMBOL_GPL(sbitmap_any_bit_set); |
| 243 | |
| 244 | bool sbitmap_any_bit_clear(const struct sbitmap *sb) |
| 245 | { |
| 246 | unsigned int i; |
| 247 | |
| 248 | for (i = 0; i < sb->map_nr; i++) { |
| 249 | const struct sbitmap_word *word = &sb->map[i]; |
| 250 | unsigned long ret; |
| 251 | |
| 252 | ret = find_first_zero_bit(&word->word, word->depth); |
| 253 | if (ret < word->depth) |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(sbitmap_any_bit_clear); |
| 259 | |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 260 | static unsigned int __sbitmap_weight(const struct sbitmap *sb, bool set) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 261 | { |
Colin Ian King | 60658e0 | 2016-09-19 14:34:08 +0100 | [diff] [blame] | 262 | unsigned int i, weight = 0; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 263 | |
| 264 | for (i = 0; i < sb->map_nr; i++) { |
| 265 | const struct sbitmap_word *word = &sb->map[i]; |
| 266 | |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 267 | if (set) |
| 268 | weight += bitmap_weight(&word->word, word->depth); |
| 269 | else |
| 270 | weight += bitmap_weight(&word->cleared, word->depth); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 271 | } |
| 272 | return weight; |
| 273 | } |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 274 | |
| 275 | static unsigned int sbitmap_weight(const struct sbitmap *sb) |
| 276 | { |
| 277 | return __sbitmap_weight(sb, true); |
| 278 | } |
| 279 | |
| 280 | static unsigned int sbitmap_cleared(const struct sbitmap *sb) |
| 281 | { |
| 282 | return __sbitmap_weight(sb, false); |
| 283 | } |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 284 | |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 285 | void sbitmap_show(struct sbitmap *sb, struct seq_file *m) |
| 286 | { |
| 287 | seq_printf(m, "depth=%u\n", sb->depth); |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 288 | seq_printf(m, "busy=%u\n", sbitmap_weight(sb) - sbitmap_cleared(sb)); |
| 289 | seq_printf(m, "cleared=%u\n", sbitmap_cleared(sb)); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 290 | seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); |
| 291 | seq_printf(m, "map_nr=%u\n", sb->map_nr); |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(sbitmap_show); |
| 294 | |
| 295 | static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) |
| 296 | { |
| 297 | if ((offset & 0xf) == 0) { |
| 298 | if (offset != 0) |
| 299 | seq_putc(m, '\n'); |
| 300 | seq_printf(m, "%08x:", offset); |
| 301 | } |
| 302 | if ((offset & 0x1) == 0) |
| 303 | seq_putc(m, ' '); |
| 304 | seq_printf(m, "%02x", byte); |
| 305 | } |
| 306 | |
| 307 | void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) |
| 308 | { |
| 309 | u8 byte = 0; |
| 310 | unsigned int byte_bits = 0; |
| 311 | unsigned int offset = 0; |
| 312 | int i; |
| 313 | |
| 314 | for (i = 0; i < sb->map_nr; i++) { |
| 315 | unsigned long word = READ_ONCE(sb->map[i].word); |
| 316 | unsigned int word_bits = READ_ONCE(sb->map[i].depth); |
| 317 | |
| 318 | while (word_bits > 0) { |
| 319 | unsigned int bits = min(8 - byte_bits, word_bits); |
| 320 | |
| 321 | byte |= (word & (BIT(bits) - 1)) << byte_bits; |
| 322 | byte_bits += bits; |
| 323 | if (byte_bits == 8) { |
| 324 | emit_byte(m, offset, byte); |
| 325 | byte = 0; |
| 326 | byte_bits = 0; |
| 327 | offset++; |
| 328 | } |
| 329 | word >>= bits; |
| 330 | word_bits -= bits; |
| 331 | } |
| 332 | } |
| 333 | if (byte_bits) { |
| 334 | emit_byte(m, offset, byte); |
| 335 | offset++; |
| 336 | } |
| 337 | if (offset) |
| 338 | seq_putc(m, '\n'); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); |
| 341 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 342 | static unsigned int sbq_calc_wake_batch(struct sbitmap_queue *sbq, |
| 343 | unsigned int depth) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 344 | { |
| 345 | unsigned int wake_batch; |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 346 | unsigned int shallow_depth; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * 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] | 350 | * batch size is small enough that the full depth of the bitmap, |
| 351 | * potentially limited by a shallow depth, is enough to wake up all of |
| 352 | * the queues. |
| 353 | * |
| 354 | * Each full word of the bitmap has bits_per_word bits, and there might |
| 355 | * be a partial word. There are depth / bits_per_word full words and |
| 356 | * depth % bits_per_word bits left over. In bitwise arithmetic: |
| 357 | * |
| 358 | * bits_per_word = 1 << shift |
| 359 | * depth / bits_per_word = depth >> shift |
| 360 | * depth % bits_per_word = depth & ((1 << shift) - 1) |
| 361 | * |
| 362 | * Each word can be limited to sbq->min_shallow_depth bits. |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 363 | */ |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 364 | shallow_depth = min(1U << sbq->sb.shift, sbq->min_shallow_depth); |
| 365 | depth = ((depth >> sbq->sb.shift) * shallow_depth + |
| 366 | min(depth & ((1U << sbq->sb.shift) - 1), shallow_depth)); |
| 367 | wake_batch = clamp_t(unsigned int, depth / SBQ_WAIT_QUEUES, 1, |
| 368 | SBQ_WAKE_BATCH); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 369 | |
| 370 | return wake_batch; |
| 371 | } |
| 372 | |
| 373 | int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth, |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 374 | int shift, bool round_robin, gfp_t flags, int node) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 375 | { |
| 376 | int ret; |
| 377 | int i; |
| 378 | |
| 379 | ret = sbitmap_init_node(&sbq->sb, depth, shift, flags, node); |
| 380 | if (ret) |
| 381 | return ret; |
| 382 | |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 383 | sbq->alloc_hint = alloc_percpu_gfp(unsigned int, flags); |
| 384 | if (!sbq->alloc_hint) { |
| 385 | sbitmap_free(&sbq->sb); |
| 386 | return -ENOMEM; |
| 387 | } |
| 388 | |
Omar Sandoval | 98d9541 | 2016-09-17 01:28:25 -0700 | [diff] [blame] | 389 | if (depth && !round_robin) { |
| 390 | for_each_possible_cpu(i) |
| 391 | *per_cpu_ptr(sbq->alloc_hint, i) = prandom_u32() % depth; |
| 392 | } |
| 393 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 394 | sbq->min_shallow_depth = UINT_MAX; |
| 395 | sbq->wake_batch = sbq_calc_wake_batch(sbq, depth); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 396 | atomic_set(&sbq->wake_index, 0); |
Jens Axboe | 5d2ee71 | 2018-11-29 17:36:41 -0700 | [diff] [blame^] | 397 | atomic_set(&sbq->ws_active, 0); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 398 | |
Omar Sandoval | 48e2816 | 2016-09-17 01:28:22 -0700 | [diff] [blame] | 399 | sbq->ws = kzalloc_node(SBQ_WAIT_QUEUES * sizeof(*sbq->ws), flags, node); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 400 | if (!sbq->ws) { |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 401 | free_percpu(sbq->alloc_hint); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 402 | sbitmap_free(&sbq->sb); |
| 403 | return -ENOMEM; |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 407 | init_waitqueue_head(&sbq->ws[i].wait); |
| 408 | atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch); |
| 409 | } |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 410 | |
| 411 | sbq->round_robin = round_robin; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); |
| 415 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 416 | static void sbitmap_queue_update_wake_batch(struct sbitmap_queue *sbq, |
| 417 | unsigned int depth) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 418 | { |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 419 | unsigned int wake_batch = sbq_calc_wake_batch(sbq, depth); |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 420 | int i; |
| 421 | |
| 422 | if (sbq->wake_batch != wake_batch) { |
| 423 | WRITE_ONCE(sbq->wake_batch, wake_batch); |
| 424 | /* |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 425 | * Pairs with the memory barrier in sbitmap_queue_wake_up() |
| 426 | * to ensure that the batch size is updated before the wait |
| 427 | * counts. |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 428 | */ |
| 429 | smp_mb__before_atomic(); |
| 430 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) |
| 431 | atomic_set(&sbq->ws[i].wait_cnt, 1); |
| 432 | } |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) |
| 436 | { |
| 437 | sbitmap_queue_update_wake_batch(sbq, depth); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 438 | sbitmap_resize(&sbq->sb, depth); |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(sbitmap_queue_resize); |
| 441 | |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 442 | int __sbitmap_queue_get(struct sbitmap_queue *sbq) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 443 | { |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 444 | unsigned int hint, depth; |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 445 | int nr; |
| 446 | |
| 447 | hint = this_cpu_read(*sbq->alloc_hint); |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 448 | depth = READ_ONCE(sbq->sb.depth); |
| 449 | if (unlikely(hint >= depth)) { |
| 450 | hint = depth ? prandom_u32() % depth : 0; |
| 451 | this_cpu_write(*sbq->alloc_hint, hint); |
| 452 | } |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 453 | nr = sbitmap_get(&sbq->sb, hint, sbq->round_robin); |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 454 | |
| 455 | if (nr == -1) { |
| 456 | /* If the map is full, a hint won't do us much good. */ |
| 457 | this_cpu_write(*sbq->alloc_hint, 0); |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 458 | } else if (nr == hint || unlikely(sbq->round_robin)) { |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 459 | /* Only update the hint if we used it. */ |
| 460 | hint = nr + 1; |
Omar Sandoval | 05fd095 | 2016-09-17 01:28:26 -0700 | [diff] [blame] | 461 | if (hint >= depth - 1) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 462 | hint = 0; |
| 463 | this_cpu_write(*sbq->alloc_hint, hint); |
| 464 | } |
| 465 | |
| 466 | return nr; |
| 467 | } |
| 468 | EXPORT_SYMBOL_GPL(__sbitmap_queue_get); |
| 469 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 470 | int __sbitmap_queue_get_shallow(struct sbitmap_queue *sbq, |
| 471 | unsigned int shallow_depth) |
| 472 | { |
| 473 | unsigned int hint, depth; |
| 474 | int nr; |
| 475 | |
Omar Sandoval | 61445b56 | 2018-05-09 17:29:24 -0700 | [diff] [blame] | 476 | WARN_ON_ONCE(shallow_depth < sbq->min_shallow_depth); |
| 477 | |
Omar Sandoval | c05e667 | 2017-04-14 00:59:58 -0700 | [diff] [blame] | 478 | hint = this_cpu_read(*sbq->alloc_hint); |
| 479 | depth = READ_ONCE(sbq->sb.depth); |
| 480 | if (unlikely(hint >= depth)) { |
| 481 | hint = depth ? prandom_u32() % depth : 0; |
| 482 | this_cpu_write(*sbq->alloc_hint, hint); |
| 483 | } |
| 484 | nr = sbitmap_get_shallow(&sbq->sb, hint, shallow_depth); |
| 485 | |
| 486 | if (nr == -1) { |
| 487 | /* If the map is full, a hint won't do us much good. */ |
| 488 | this_cpu_write(*sbq->alloc_hint, 0); |
| 489 | } else if (nr == hint || unlikely(sbq->round_robin)) { |
| 490 | /* Only update the hint if we used it. */ |
| 491 | hint = nr + 1; |
| 492 | if (hint >= depth - 1) |
| 493 | hint = 0; |
| 494 | this_cpu_write(*sbq->alloc_hint, hint); |
| 495 | } |
| 496 | |
| 497 | return nr; |
| 498 | } |
| 499 | EXPORT_SYMBOL_GPL(__sbitmap_queue_get_shallow); |
| 500 | |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 501 | void sbitmap_queue_min_shallow_depth(struct sbitmap_queue *sbq, |
| 502 | unsigned int min_shallow_depth) |
| 503 | { |
| 504 | sbq->min_shallow_depth = min_shallow_depth; |
| 505 | sbitmap_queue_update_wake_batch(sbq, sbq->sb.depth); |
| 506 | } |
| 507 | EXPORT_SYMBOL_GPL(sbitmap_queue_min_shallow_depth); |
| 508 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 509 | static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) |
| 510 | { |
| 511 | int i, wake_index; |
| 512 | |
Jens Axboe | 5d2ee71 | 2018-11-29 17:36:41 -0700 | [diff] [blame^] | 513 | if (!atomic_read(&sbq->ws_active)) |
| 514 | return NULL; |
| 515 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 516 | wake_index = atomic_read(&sbq->wake_index); |
| 517 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 518 | struct sbq_wait_state *ws = &sbq->ws[wake_index]; |
| 519 | |
| 520 | if (waitqueue_active(&ws->wait)) { |
| 521 | int o = atomic_read(&sbq->wake_index); |
| 522 | |
| 523 | if (wake_index != o) |
| 524 | atomic_cmpxchg(&sbq->wake_index, o, wake_index); |
| 525 | return ws; |
| 526 | } |
| 527 | |
| 528 | wake_index = sbq_index_inc(wake_index); |
| 529 | } |
| 530 | |
| 531 | return NULL; |
| 532 | } |
| 533 | |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 534 | static bool __sbq_wake_up(struct sbitmap_queue *sbq) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 535 | { |
| 536 | struct sbq_wait_state *ws; |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 537 | unsigned int wake_batch; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 538 | int wait_cnt; |
| 539 | |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 540 | ws = sbq_wake_ptr(sbq); |
| 541 | if (!ws) |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 542 | return false; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 543 | |
| 544 | wait_cnt = atomic_dec_return(&ws->wait_cnt); |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 545 | if (wait_cnt <= 0) { |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 546 | int ret; |
| 547 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 548 | wake_batch = READ_ONCE(sbq->wake_batch); |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 549 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 550 | /* |
| 551 | * Pairs with the memory barrier in sbitmap_queue_resize() to |
| 552 | * ensure that we see the batch size update before the wait |
| 553 | * count is reset. |
| 554 | */ |
| 555 | smp_mb__before_atomic(); |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 556 | |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 557 | /* |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 558 | * For concurrent callers of this, the one that failed the |
| 559 | * atomic_cmpxhcg() race should call this function again |
| 560 | * to wakeup a new batch on a different 'ws'. |
Omar Sandoval | 6c0ca7a | 2017-01-18 11:55:22 -0800 | [diff] [blame] | 561 | */ |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 562 | ret = atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wake_batch); |
| 563 | if (ret == wait_cnt) { |
| 564 | sbq_index_atomic_inc(&sbq->wake_index); |
| 565 | wake_up_nr(&ws->wait, wake_batch); |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | return true; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 570 | } |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 571 | |
| 572 | return false; |
| 573 | } |
| 574 | |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 575 | void sbitmap_queue_wake_up(struct sbitmap_queue *sbq) |
Jens Axboe | c854ab5 | 2018-05-14 12:17:31 -0600 | [diff] [blame] | 576 | { |
| 577 | while (__sbq_wake_up(sbq)) |
| 578 | ; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 579 | } |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 580 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up); |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 581 | |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 582 | void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, |
Omar Sandoval | f4a644d | 2016-09-17 01:28:24 -0700 | [diff] [blame] | 583 | unsigned int cpu) |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 584 | { |
Jens Axboe | ea86ea2 | 2018-11-30 13:18:06 -0700 | [diff] [blame] | 585 | sbitmap_deferred_clear_bit(&sbq->sb, nr); |
| 586 | |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 587 | /* |
| 588 | * Pairs with the memory barrier in set_current_state() to ensure the |
| 589 | * proper ordering of clear_bit_unlock()/waitqueue_active() in the waker |
| 590 | * and test_and_set_bit_lock()/prepare_to_wait()/finish_wait() in the |
| 591 | * waiter. See the comment on waitqueue_active(). |
| 592 | */ |
| 593 | smp_mb__after_atomic(); |
| 594 | sbitmap_queue_wake_up(sbq); |
| 595 | |
Omar Sandoval | 5c64a8d | 2016-09-17 12:20:54 -0700 | [diff] [blame] | 596 | if (likely(!sbq->round_robin && nr < sbq->sb.depth)) |
Omar Sandoval | 40aabb6 | 2016-09-17 01:28:23 -0700 | [diff] [blame] | 597 | *per_cpu_ptr(sbq->alloc_hint, cpu) = nr; |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 598 | } |
| 599 | EXPORT_SYMBOL_GPL(sbitmap_queue_clear); |
| 600 | |
| 601 | void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) |
| 602 | { |
| 603 | int i, wake_index; |
| 604 | |
| 605 | /* |
Omar Sandoval | f66227d | 2017-01-18 11:55:21 -0800 | [diff] [blame] | 606 | * Pairs with the memory barrier in set_current_state() like in |
Ming Lei | e6fc464 | 2018-05-24 11:00:39 -0600 | [diff] [blame] | 607 | * sbitmap_queue_wake_up(). |
Omar Sandoval | 8845964 | 2016-09-17 08:38:44 -0600 | [diff] [blame] | 608 | */ |
| 609 | smp_mb(); |
| 610 | wake_index = atomic_read(&sbq->wake_index); |
| 611 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 612 | struct sbq_wait_state *ws = &sbq->ws[wake_index]; |
| 613 | |
| 614 | if (waitqueue_active(&ws->wait)) |
| 615 | wake_up(&ws->wait); |
| 616 | |
| 617 | wake_index = sbq_index_inc(wake_index); |
| 618 | } |
| 619 | } |
| 620 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 621 | |
| 622 | void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) |
| 623 | { |
| 624 | bool first; |
| 625 | int i; |
| 626 | |
| 627 | sbitmap_show(&sbq->sb, m); |
| 628 | |
| 629 | seq_puts(m, "alloc_hint={"); |
| 630 | first = true; |
| 631 | for_each_possible_cpu(i) { |
| 632 | if (!first) |
| 633 | seq_puts(m, ", "); |
| 634 | first = false; |
| 635 | seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i)); |
| 636 | } |
| 637 | seq_puts(m, "}\n"); |
| 638 | |
| 639 | seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); |
| 640 | seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); |
Jens Axboe | 5d2ee71 | 2018-11-29 17:36:41 -0700 | [diff] [blame^] | 641 | seq_printf(m, "ws_active=%d\n", atomic_read(&sbq->ws_active)); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 642 | |
| 643 | seq_puts(m, "ws={\n"); |
| 644 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { |
| 645 | struct sbq_wait_state *ws = &sbq->ws[i]; |
| 646 | |
| 647 | seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n", |
| 648 | atomic_read(&ws->wait_cnt), |
| 649 | waitqueue_active(&ws->wait) ? "active" : "inactive"); |
| 650 | } |
| 651 | seq_puts(m, "}\n"); |
| 652 | |
| 653 | seq_printf(m, "round_robin=%d\n", sbq->round_robin); |
Omar Sandoval | a327553 | 2018-05-09 17:16:31 -0700 | [diff] [blame] | 654 | seq_printf(m, "min_shallow_depth=%u\n", sbq->min_shallow_depth); |
Omar Sandoval | 24af1ccf | 2017-01-25 14:32:13 -0800 | [diff] [blame] | 655 | } |
| 656 | EXPORT_SYMBOL_GPL(sbitmap_queue_show); |
Jens Axboe | 5d2ee71 | 2018-11-29 17:36:41 -0700 | [diff] [blame^] | 657 | |
| 658 | void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq, |
| 659 | struct sbq_wait_state *ws, |
| 660 | struct sbq_wait *sbq_wait, int state) |
| 661 | { |
| 662 | if (!sbq_wait->accounted) { |
| 663 | atomic_inc(&sbq->ws_active); |
| 664 | sbq_wait->accounted = 1; |
| 665 | } |
| 666 | prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state); |
| 667 | } |
| 668 | EXPORT_SYMBOL_GPL(sbitmap_prepare_to_wait); |
| 669 | |
| 670 | void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws, |
| 671 | struct sbq_wait *sbq_wait) |
| 672 | { |
| 673 | finish_wait(&ws->wait, &sbq_wait->wait); |
| 674 | if (sbq_wait->accounted) { |
| 675 | atomic_dec(&sbq->ws_active); |
| 676 | sbq_wait->accounted = 0; |
| 677 | } |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(sbitmap_finish_wait); |