diff options
Diffstat (limited to 'lib/sbitmap.c')
| -rw-r--r-- | lib/sbitmap.c | 139 |
1 files changed, 131 insertions, 8 deletions
diff --git a/lib/sbitmap.c b/lib/sbitmap.c index 2cecf05c82fd..55e11c4b2f3b 100644 --- a/lib/sbitmap.c +++ b/lib/sbitmap.c | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | #include <linux/random.h> | 18 | #include <linux/random.h> |
| 19 | #include <linux/sbitmap.h> | 19 | #include <linux/sbitmap.h> |
| 20 | #include <linux/seq_file.h> | ||
| 20 | 21 | ||
| 21 | int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, | 22 | int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift, |
| 22 | gfp_t flags, int node) | 23 | gfp_t flags, int node) |
| @@ -180,6 +181,62 @@ unsigned int sbitmap_weight(const struct sbitmap *sb) | |||
| 180 | } | 181 | } |
| 181 | EXPORT_SYMBOL_GPL(sbitmap_weight); | 182 | EXPORT_SYMBOL_GPL(sbitmap_weight); |
| 182 | 183 | ||
| 184 | void sbitmap_show(struct sbitmap *sb, struct seq_file *m) | ||
| 185 | { | ||
| 186 | seq_printf(m, "depth=%u\n", sb->depth); | ||
| 187 | seq_printf(m, "busy=%u\n", sbitmap_weight(sb)); | ||
| 188 | seq_printf(m, "bits_per_word=%u\n", 1U << sb->shift); | ||
| 189 | seq_printf(m, "map_nr=%u\n", sb->map_nr); | ||
| 190 | } | ||
| 191 | EXPORT_SYMBOL_GPL(sbitmap_show); | ||
| 192 | |||
| 193 | static inline void emit_byte(struct seq_file *m, unsigned int offset, u8 byte) | ||
| 194 | { | ||
| 195 | if ((offset & 0xf) == 0) { | ||
| 196 | if (offset != 0) | ||
| 197 | seq_putc(m, '\n'); | ||
| 198 | seq_printf(m, "%08x:", offset); | ||
| 199 | } | ||
| 200 | if ((offset & 0x1) == 0) | ||
| 201 | seq_putc(m, ' '); | ||
| 202 | seq_printf(m, "%02x", byte); | ||
| 203 | } | ||
| 204 | |||
| 205 | void sbitmap_bitmap_show(struct sbitmap *sb, struct seq_file *m) | ||
| 206 | { | ||
| 207 | u8 byte = 0; | ||
| 208 | unsigned int byte_bits = 0; | ||
| 209 | unsigned int offset = 0; | ||
| 210 | int i; | ||
| 211 | |||
| 212 | for (i = 0; i < sb->map_nr; i++) { | ||
| 213 | unsigned long word = READ_ONCE(sb->map[i].word); | ||
| 214 | unsigned int word_bits = READ_ONCE(sb->map[i].depth); | ||
| 215 | |||
| 216 | while (word_bits > 0) { | ||
| 217 | unsigned int bits = min(8 - byte_bits, word_bits); | ||
| 218 | |||
| 219 | byte |= (word & (BIT(bits) - 1)) << byte_bits; | ||
| 220 | byte_bits += bits; | ||
| 221 | if (byte_bits == 8) { | ||
| 222 | emit_byte(m, offset, byte); | ||
| 223 | byte = 0; | ||
| 224 | byte_bits = 0; | ||
| 225 | offset++; | ||
| 226 | } | ||
| 227 | word >>= bits; | ||
| 228 | word_bits -= bits; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | if (byte_bits) { | ||
| 232 | emit_byte(m, offset, byte); | ||
| 233 | offset++; | ||
| 234 | } | ||
| 235 | if (offset) | ||
| 236 | seq_putc(m, '\n'); | ||
| 237 | } | ||
| 238 | EXPORT_SYMBOL_GPL(sbitmap_bitmap_show); | ||
| 239 | |||
| 183 | static unsigned int sbq_calc_wake_batch(unsigned int depth) | 240 | static unsigned int sbq_calc_wake_batch(unsigned int depth) |
| 184 | { | 241 | { |
| 185 | unsigned int wake_batch; | 242 | unsigned int wake_batch; |
| @@ -239,7 +296,19 @@ EXPORT_SYMBOL_GPL(sbitmap_queue_init_node); | |||
| 239 | 296 | ||
| 240 | void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) | 297 | void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth) |
| 241 | { | 298 | { |
| 242 | sbq->wake_batch = sbq_calc_wake_batch(depth); | 299 | unsigned int wake_batch = sbq_calc_wake_batch(depth); |
| 300 | int i; | ||
| 301 | |||
| 302 | if (sbq->wake_batch != wake_batch) { | ||
| 303 | WRITE_ONCE(sbq->wake_batch, wake_batch); | ||
| 304 | /* | ||
| 305 | * Pairs with the memory barrier in sbq_wake_up() to ensure that | ||
| 306 | * the batch size is updated before the wait counts. | ||
| 307 | */ | ||
| 308 | smp_mb__before_atomic(); | ||
| 309 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) | ||
| 310 | atomic_set(&sbq->ws[i].wait_cnt, 1); | ||
| 311 | } | ||
| 243 | sbitmap_resize(&sbq->sb, depth); | 312 | sbitmap_resize(&sbq->sb, depth); |
| 244 | } | 313 | } |
| 245 | EXPORT_SYMBOL_GPL(sbitmap_queue_resize); | 314 | EXPORT_SYMBOL_GPL(sbitmap_queue_resize); |
| @@ -297,20 +366,39 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) | |||
| 297 | static void sbq_wake_up(struct sbitmap_queue *sbq) | 366 | static void sbq_wake_up(struct sbitmap_queue *sbq) |
| 298 | { | 367 | { |
| 299 | struct sbq_wait_state *ws; | 368 | struct sbq_wait_state *ws; |
| 369 | unsigned int wake_batch; | ||
| 300 | int wait_cnt; | 370 | int wait_cnt; |
| 301 | 371 | ||
| 302 | /* Ensure that the wait list checks occur after clear_bit(). */ | 372 | /* |
| 303 | smp_mb(); | 373 | * Pairs with the memory barrier in set_current_state() to ensure the |
| 374 | * proper ordering of clear_bit()/waitqueue_active() in the waker and | ||
| 375 | * test_and_set_bit()/prepare_to_wait()/finish_wait() in the waiter. See | ||
| 376 | * the comment on waitqueue_active(). This is __after_atomic because we | ||
| 377 | * just did clear_bit() in the caller. | ||
| 378 | */ | ||
| 379 | smp_mb__after_atomic(); | ||
| 304 | 380 | ||
| 305 | ws = sbq_wake_ptr(sbq); | 381 | ws = sbq_wake_ptr(sbq); |
| 306 | if (!ws) | 382 | if (!ws) |
| 307 | return; | 383 | return; |
| 308 | 384 | ||
| 309 | wait_cnt = atomic_dec_return(&ws->wait_cnt); | 385 | wait_cnt = atomic_dec_return(&ws->wait_cnt); |
| 310 | if (unlikely(wait_cnt < 0)) | 386 | if (wait_cnt <= 0) { |
| 311 | wait_cnt = atomic_inc_return(&ws->wait_cnt); | 387 | wake_batch = READ_ONCE(sbq->wake_batch); |
| 312 | if (wait_cnt == 0) { | 388 | /* |
| 313 | atomic_add(sbq->wake_batch, &ws->wait_cnt); | 389 | * Pairs with the memory barrier in sbitmap_queue_resize() to |
| 390 | * ensure that we see the batch size update before the wait | ||
| 391 | * count is reset. | ||
| 392 | */ | ||
| 393 | smp_mb__before_atomic(); | ||
| 394 | /* | ||
| 395 | * If there are concurrent callers to sbq_wake_up(), the last | ||
| 396 | * one to decrement the wait count below zero will bump it back | ||
| 397 | * up. If there is a concurrent resize, the count reset will | ||
| 398 | * either cause the cmpxchg to fail or overwrite after the | ||
| 399 | * cmpxchg. | ||
| 400 | */ | ||
| 401 | atomic_cmpxchg(&ws->wait_cnt, wait_cnt, wait_cnt + wake_batch); | ||
| 314 | sbq_index_atomic_inc(&sbq->wake_index); | 402 | sbq_index_atomic_inc(&sbq->wake_index); |
| 315 | wake_up(&ws->wait); | 403 | wake_up(&ws->wait); |
| 316 | } | 404 | } |
| @@ -331,7 +419,8 @@ void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) | |||
| 331 | int i, wake_index; | 419 | int i, wake_index; |
| 332 | 420 | ||
| 333 | /* | 421 | /* |
| 334 | * Make sure all changes prior to this are visible from other CPUs. | 422 | * Pairs with the memory barrier in set_current_state() like in |
| 423 | * sbq_wake_up(). | ||
| 335 | */ | 424 | */ |
| 336 | smp_mb(); | 425 | smp_mb(); |
| 337 | wake_index = atomic_read(&sbq->wake_index); | 426 | wake_index = atomic_read(&sbq->wake_index); |
| @@ -345,3 +434,37 @@ void sbitmap_queue_wake_all(struct sbitmap_queue *sbq) | |||
| 345 | } | 434 | } |
| 346 | } | 435 | } |
| 347 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); | 436 | EXPORT_SYMBOL_GPL(sbitmap_queue_wake_all); |
| 437 | |||
| 438 | void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m) | ||
| 439 | { | ||
| 440 | bool first; | ||
| 441 | int i; | ||
| 442 | |||
| 443 | sbitmap_show(&sbq->sb, m); | ||
| 444 | |||
| 445 | seq_puts(m, "alloc_hint={"); | ||
| 446 | first = true; | ||
| 447 | for_each_possible_cpu(i) { | ||
| 448 | if (!first) | ||
| 449 | seq_puts(m, ", "); | ||
| 450 | first = false; | ||
| 451 | seq_printf(m, "%u", *per_cpu_ptr(sbq->alloc_hint, i)); | ||
| 452 | } | ||
| 453 | seq_puts(m, "}\n"); | ||
| 454 | |||
| 455 | seq_printf(m, "wake_batch=%u\n", sbq->wake_batch); | ||
| 456 | seq_printf(m, "wake_index=%d\n", atomic_read(&sbq->wake_index)); | ||
| 457 | |||
| 458 | seq_puts(m, "ws={\n"); | ||
| 459 | for (i = 0; i < SBQ_WAIT_QUEUES; i++) { | ||
| 460 | struct sbq_wait_state *ws = &sbq->ws[i]; | ||
| 461 | |||
| 462 | seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n", | ||
| 463 | atomic_read(&ws->wait_cnt), | ||
| 464 | waitqueue_active(&ws->wait) ? "active" : "inactive"); | ||
| 465 | } | ||
| 466 | seq_puts(m, "}\n"); | ||
| 467 | |||
| 468 | seq_printf(m, "round_robin=%d\n", sbq->round_robin); | ||
| 469 | } | ||
| 470 | EXPORT_SYMBOL_GPL(sbitmap_queue_show); | ||
