Blame view
kernel/locking/test-ww_mutex.c
12.3 KB
d6cd1e9b9 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
f2a5fec17 locking/ww_mutex:... |
2 3 |
/* * Module-based API test facility for ww_mutexes |
f2a5fec17 locking/ww_mutex:... |
4 5 6 7 8 |
*/ #include <linux/kernel.h> #include <linux/completion.h> |
2a0c11282 locking/ww_mutex:... |
9 |
#include <linux/delay.h> |
f2a5fec17 locking/ww_mutex:... |
10 11 |
#include <linux/kthread.h> #include <linux/module.h> |
2a0c11282 locking/ww_mutex:... |
12 |
#include <linux/random.h> |
d1b42b800 locking/ww_mutex:... |
13 |
#include <linux/slab.h> |
f2a5fec17 locking/ww_mutex:... |
14 |
#include <linux/ww_mutex.h> |
08295b3b5 locking: Implemen... |
15 |
static DEFINE_WD_CLASS(ww_class); |
d1b42b800 locking/ww_mutex:... |
16 |
struct workqueue_struct *wq; |
f2a5fec17 locking/ww_mutex:... |
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
struct test_mutex { struct work_struct work; struct ww_mutex mutex; struct completion ready, go, done; unsigned int flags; }; #define TEST_MTX_SPIN BIT(0) #define TEST_MTX_TRY BIT(1) #define TEST_MTX_CTX BIT(2) #define __TEST_MTX_LAST BIT(3) static void test_mutex_work(struct work_struct *work) { struct test_mutex *mtx = container_of(work, typeof(*mtx), work); complete(&mtx->ready); wait_for_completion(&mtx->go); if (mtx->flags & TEST_MTX_TRY) { while (!ww_mutex_trylock(&mtx->mutex)) |
2b232e0c3 locking/ww_mutex:... |
39 |
cond_resched(); |
f2a5fec17 locking/ww_mutex:... |
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
} else { ww_mutex_lock(&mtx->mutex, NULL); } complete(&mtx->done); ww_mutex_unlock(&mtx->mutex); } static int __test_mutex(unsigned int flags) { #define TIMEOUT (HZ / 16) struct test_mutex mtx; struct ww_acquire_ctx ctx; int ret; ww_mutex_init(&mtx.mutex, &ww_class); ww_acquire_init(&ctx, &ww_class); INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); init_completion(&mtx.ready); init_completion(&mtx.go); init_completion(&mtx.done); mtx.flags = flags; schedule_work(&mtx.work); wait_for_completion(&mtx.ready); ww_mutex_lock(&mtx.mutex, (flags & TEST_MTX_CTX) ? &ctx : NULL); complete(&mtx.go); if (flags & TEST_MTX_SPIN) { unsigned long timeout = jiffies + TIMEOUT; ret = 0; do { if (completion_done(&mtx.done)) { ret = -EINVAL; break; } |
2b232e0c3 locking/ww_mutex:... |
77 |
cond_resched(); |
f2a5fec17 locking/ww_mutex:... |
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
} while (time_before(jiffies, timeout)); } else { ret = wait_for_completion_timeout(&mtx.done, TIMEOUT); } ww_mutex_unlock(&mtx.mutex); ww_acquire_fini(&ctx); if (ret) { pr_err("%s(flags=%x): mutual exclusion failure ", __func__, flags); ret = -EINVAL; } flush_work(&mtx.work); destroy_work_on_stack(&mtx.work); return ret; #undef TIMEOUT } static int test_mutex(void) { int ret; int i; for (i = 0; i < __TEST_MTX_LAST; i++) { ret = __test_mutex(i); if (ret) return ret; } return 0; } |
c22fb3807 locking/ww_mutex:... |
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
static int test_aa(void) { struct ww_mutex mutex; struct ww_acquire_ctx ctx; int ret; ww_mutex_init(&mutex, &ww_class); ww_acquire_init(&ctx, &ww_class); ww_mutex_lock(&mutex, &ctx); if (ww_mutex_trylock(&mutex)) { pr_err("%s: trylocked itself! ", __func__); ww_mutex_unlock(&mutex); ret = -EINVAL; goto out; } ret = ww_mutex_lock(&mutex, &ctx); if (ret != -EALREADY) { pr_err("%s: missed deadlock for recursing, ret=%d ", __func__, ret); if (!ret) ww_mutex_unlock(&mutex); ret = -EINVAL; goto out; } ret = 0; out: ww_mutex_unlock(&mutex); ww_acquire_fini(&ctx); return ret; } |
70207686e locking/ww_mutex:... |
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
struct test_abba { struct work_struct work; struct ww_mutex a_mutex; struct ww_mutex b_mutex; struct completion a_ready; struct completion b_ready; bool resolve; int result; }; static void test_abba_work(struct work_struct *work) { struct test_abba *abba = container_of(work, typeof(*abba), work); struct ww_acquire_ctx ctx; int err; ww_acquire_init(&ctx, &ww_class); ww_mutex_lock(&abba->b_mutex, &ctx); complete(&abba->b_ready); wait_for_completion(&abba->a_ready); err = ww_mutex_lock(&abba->a_mutex, &ctx); if (abba->resolve && err == -EDEADLK) { ww_mutex_unlock(&abba->b_mutex); ww_mutex_lock_slow(&abba->a_mutex, &ctx); err = ww_mutex_lock(&abba->b_mutex, &ctx); } if (!err) ww_mutex_unlock(&abba->a_mutex); ww_mutex_unlock(&abba->b_mutex); ww_acquire_fini(&ctx); abba->result = err; } static int test_abba(bool resolve) { struct test_abba abba; struct ww_acquire_ctx ctx; int err, ret; ww_mutex_init(&abba.a_mutex, &ww_class); ww_mutex_init(&abba.b_mutex, &ww_class); INIT_WORK_ONSTACK(&abba.work, test_abba_work); init_completion(&abba.a_ready); init_completion(&abba.b_ready); abba.resolve = resolve; schedule_work(&abba.work); ww_acquire_init(&ctx, &ww_class); ww_mutex_lock(&abba.a_mutex, &ctx); complete(&abba.a_ready); wait_for_completion(&abba.b_ready); err = ww_mutex_lock(&abba.b_mutex, &ctx); if (resolve && err == -EDEADLK) { ww_mutex_unlock(&abba.a_mutex); ww_mutex_lock_slow(&abba.b_mutex, &ctx); err = ww_mutex_lock(&abba.a_mutex, &ctx); } if (!err) ww_mutex_unlock(&abba.b_mutex); ww_mutex_unlock(&abba.a_mutex); ww_acquire_fini(&ctx); flush_work(&abba.work); destroy_work_on_stack(&abba.work); ret = 0; if (resolve) { if (err || abba.result) { pr_err("%s: failed to resolve ABBA deadlock, A err=%d, B err=%d ", __func__, err, abba.result); ret = -EINVAL; } } else { if (err != -EDEADLK && abba.result != -EDEADLK) { pr_err("%s: missed ABBA deadlock, A err=%d, B err=%d ", __func__, err, abba.result); ret = -EINVAL; } } return ret; } |
d1b42b800 locking/ww_mutex:... |
238 239 240 241 242 243 244 245 246 247 248 249 250 |
struct test_cycle { struct work_struct work; struct ww_mutex a_mutex; struct ww_mutex *b_mutex; struct completion *a_signal; struct completion b_signal; int result; }; static void test_cycle_work(struct work_struct *work) { struct test_cycle *cycle = container_of(work, typeof(*cycle), work); struct ww_acquire_ctx ctx; |
e4a02ed2a locking/ww_mutex:... |
251 |
int err, erra = 0; |
d1b42b800 locking/ww_mutex:... |
252 253 254 255 256 257 258 259 260 |
ww_acquire_init(&ctx, &ww_class); ww_mutex_lock(&cycle->a_mutex, &ctx); complete(cycle->a_signal); wait_for_completion(&cycle->b_signal); err = ww_mutex_lock(cycle->b_mutex, &ctx); if (err == -EDEADLK) { |
e4a02ed2a locking/ww_mutex:... |
261 |
err = 0; |
d1b42b800 locking/ww_mutex:... |
262 263 |
ww_mutex_unlock(&cycle->a_mutex); ww_mutex_lock_slow(cycle->b_mutex, &ctx); |
e4a02ed2a locking/ww_mutex:... |
264 |
erra = ww_mutex_lock(&cycle->a_mutex, &ctx); |
d1b42b800 locking/ww_mutex:... |
265 266 267 268 |
} if (!err) ww_mutex_unlock(cycle->b_mutex); |
e4a02ed2a locking/ww_mutex:... |
269 270 |
if (!erra) ww_mutex_unlock(&cycle->a_mutex); |
d1b42b800 locking/ww_mutex:... |
271 |
ww_acquire_fini(&ctx); |
e4a02ed2a locking/ww_mutex:... |
272 |
cycle->result = err ?: erra; |
d1b42b800 locking/ww_mutex:... |
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
} static int __test_cycle(unsigned int nthreads) { struct test_cycle *cycles; unsigned int n, last = nthreads - 1; int ret; cycles = kmalloc_array(nthreads, sizeof(*cycles), GFP_KERNEL); if (!cycles) return -ENOMEM; for (n = 0; n < nthreads; n++) { struct test_cycle *cycle = &cycles[n]; ww_mutex_init(&cycle->a_mutex, &ww_class); if (n == last) cycle->b_mutex = &cycles[0].a_mutex; else cycle->b_mutex = &cycles[n + 1].a_mutex; if (n == 0) cycle->a_signal = &cycles[last].b_signal; else cycle->a_signal = &cycles[n - 1].b_signal; init_completion(&cycle->b_signal); INIT_WORK(&cycle->work, test_cycle_work); cycle->result = 0; } for (n = 0; n < nthreads; n++) queue_work(wq, &cycles[n].work); flush_workqueue(wq); ret = 0; for (n = 0; n < nthreads; n++) { struct test_cycle *cycle = &cycles[n]; if (!cycle->result) continue; |
0b405c65a locking/ww_mutex:... |
315 316 |
pr_err("cyclic deadlock not resolved, ret[%d/%d] = %d ", |
d1b42b800 locking/ww_mutex:... |
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
n, nthreads, cycle->result); ret = -EINVAL; break; } for (n = 0; n < nthreads; n++) ww_mutex_destroy(&cycles[n].a_mutex); kfree(cycles); return ret; } static int test_cycle(unsigned int ncpus) { unsigned int n; int ret; for (n = 2; n <= ncpus + 1; n++) { ret = __test_cycle(n); if (ret) return ret; } return 0; } |
2a0c11282 locking/ww_mutex:... |
341 342 343 |
struct stress { struct work_struct work; struct ww_mutex *locks; |
57dd924e5 locking/ww-mutex:... |
344 |
unsigned long timeout; |
2a0c11282 locking/ww_mutex:... |
345 |
int nlocks; |
2a0c11282 locking/ww_mutex:... |
346 347 348 349 350 351 |
}; static int *get_random_order(int count) { int *order; int n, r, tmp; |
0ee931c4e mm: treewide: rem... |
352 |
order = kmalloc_array(count, sizeof(*order), GFP_KERNEL); |
2a0c11282 locking/ww_mutex:... |
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
if (!order) return order; for (n = 0; n < count; n++) order[n] = n; for (n = count - 1; n > 1; n--) { r = get_random_int() % (n + 1); if (r != n) { tmp = order[n]; order[n] = order[r]; order[r] = tmp; } } return order; } static void dummy_load(struct stress *stress) { usleep_range(1000, 2000); } static void stress_inorder_work(struct work_struct *work) { struct stress *stress = container_of(work, typeof(*stress), work); const int nlocks = stress->nlocks; struct ww_mutex *locks = stress->locks; struct ww_acquire_ctx ctx; int *order; order = get_random_order(nlocks); if (!order) return; |
2a0c11282 locking/ww_mutex:... |
387 388 389 |
do { int contended = -1; int n, err; |
bf7b3ac2e locking/ww_mutex:... |
390 |
ww_acquire_init(&ctx, &ww_class); |
2a0c11282 locking/ww_mutex:... |
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
retry: err = 0; for (n = 0; n < nlocks; n++) { if (n == contended) continue; err = ww_mutex_lock(&locks[order[n]], &ctx); if (err < 0) break; } if (!err) dummy_load(stress); if (contended > n) ww_mutex_unlock(&locks[order[contended]]); contended = n; while (n--) ww_mutex_unlock(&locks[order[n]]); if (err == -EDEADLK) { ww_mutex_lock_slow(&locks[order[contended]], &ctx); goto retry; } if (err) { pr_err_once("stress (%s) failed with %d ", __func__, err); break; } |
2a0c11282 locking/ww_mutex:... |
421 |
|
bf7b3ac2e locking/ww_mutex:... |
422 |
ww_acquire_fini(&ctx); |
57dd924e5 locking/ww-mutex:... |
423 |
} while (!time_after(jiffies, stress->timeout)); |
2a0c11282 locking/ww_mutex:... |
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
kfree(order); kfree(stress); } struct reorder_lock { struct list_head link; struct ww_mutex *lock; }; static void stress_reorder_work(struct work_struct *work) { struct stress *stress = container_of(work, typeof(*stress), work); LIST_HEAD(locks); struct ww_acquire_ctx ctx; struct reorder_lock *ll, *ln; int *order; int n, err; order = get_random_order(stress->nlocks); if (!order) return; for (n = 0; n < stress->nlocks; n++) { ll = kmalloc(sizeof(*ll), GFP_KERNEL); if (!ll) goto out; ll->lock = &stress->locks[order[n]]; list_add(&ll->link, &locks); } kfree(order); order = NULL; |
2a0c11282 locking/ww_mutex:... |
457 |
do { |
bf7b3ac2e locking/ww_mutex:... |
458 |
ww_acquire_init(&ctx, &ww_class); |
2a0c11282 locking/ww_mutex:... |
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
list_for_each_entry(ll, &locks, link) { err = ww_mutex_lock(ll->lock, &ctx); if (!err) continue; ln = ll; list_for_each_entry_continue_reverse(ln, &locks, link) ww_mutex_unlock(ln->lock); if (err != -EDEADLK) { pr_err_once("stress (%s) failed with %d ", __func__, err); break; } ww_mutex_lock_slow(ll->lock, &ctx); list_move(&ll->link, &locks); /* restarts iteration */ } dummy_load(stress); list_for_each_entry(ll, &locks, link) ww_mutex_unlock(ll->lock); |
2a0c11282 locking/ww_mutex:... |
482 |
|
bf7b3ac2e locking/ww_mutex:... |
483 |
ww_acquire_fini(&ctx); |
57dd924e5 locking/ww-mutex:... |
484 |
} while (!time_after(jiffies, stress->timeout)); |
2a0c11282 locking/ww_mutex:... |
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
out: list_for_each_entry_safe(ll, ln, &locks, link) kfree(ll); kfree(order); kfree(stress); } static void stress_one_work(struct work_struct *work) { struct stress *stress = container_of(work, typeof(*stress), work); const int nlocks = stress->nlocks; struct ww_mutex *lock = stress->locks + (get_random_int() % nlocks); int err; do { err = ww_mutex_lock(lock, NULL); if (!err) { dummy_load(stress); ww_mutex_unlock(lock); } else { pr_err_once("stress (%s) failed with %d ", __func__, err); break; } |
57dd924e5 locking/ww-mutex:... |
511 |
} while (!time_after(jiffies, stress->timeout)); |
2a0c11282 locking/ww_mutex:... |
512 513 514 515 516 517 518 519 |
kfree(stress); } #define STRESS_INORDER BIT(0) #define STRESS_REORDER BIT(1) #define STRESS_ONE BIT(2) #define STRESS_ALL (STRESS_INORDER | STRESS_REORDER | STRESS_ONE) |
57dd924e5 locking/ww-mutex:... |
520 |
static int stress(int nlocks, int nthreads, unsigned int flags) |
2a0c11282 locking/ww_mutex:... |
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
{ struct ww_mutex *locks; int n; locks = kmalloc_array(nlocks, sizeof(*locks), GFP_KERNEL); if (!locks) return -ENOMEM; for (n = 0; n < nlocks; n++) ww_mutex_init(&locks[n], &ww_class); for (n = 0; nthreads; n++) { struct stress *stress; void (*fn)(struct work_struct *work); fn = NULL; switch (n & 3) { case 0: if (flags & STRESS_INORDER) fn = stress_inorder_work; break; case 1: if (flags & STRESS_REORDER) fn = stress_reorder_work; break; case 2: if (flags & STRESS_ONE) fn = stress_one_work; break; } if (!fn) continue; stress = kmalloc(sizeof(*stress), GFP_KERNEL); if (!stress) break; INIT_WORK(&stress->work, fn); stress->locks = locks; stress->nlocks = nlocks; |
57dd924e5 locking/ww-mutex:... |
562 |
stress->timeout = jiffies + 2*HZ; |
2a0c11282 locking/ww_mutex:... |
563 564 565 566 567 568 569 570 571 572 573 574 575 |
queue_work(wq, &stress->work); nthreads--; } flush_workqueue(wq); for (n = 0; n < nlocks; n++) ww_mutex_destroy(&locks[n]); kfree(locks); return 0; } |
f2a5fec17 locking/ww_mutex:... |
576 577 |
static int __init test_ww_mutex_init(void) { |
d1b42b800 locking/ww_mutex:... |
578 |
int ncpus = num_online_cpus(); |
f2a5fec17 locking/ww_mutex:... |
579 |
int ret; |
d1b42b800 locking/ww_mutex:... |
580 581 582 |
wq = alloc_workqueue("test-ww_mutex", WQ_UNBOUND, 0); if (!wq) return -ENOMEM; |
f2a5fec17 locking/ww_mutex:... |
583 584 585 |
ret = test_mutex(); if (ret) return ret; |
c22fb3807 locking/ww_mutex:... |
586 587 588 |
ret = test_aa(); if (ret) return ret; |
70207686e locking/ww_mutex:... |
589 590 591 592 593 594 595 |
ret = test_abba(false); if (ret) return ret; ret = test_abba(true); if (ret) return ret; |
d1b42b800 locking/ww_mutex:... |
596 597 598 |
ret = test_cycle(ncpus); if (ret) return ret; |
57dd924e5 locking/ww-mutex:... |
599 |
ret = stress(16, 2*ncpus, STRESS_INORDER); |
2a0c11282 locking/ww_mutex:... |
600 601 |
if (ret) return ret; |
57dd924e5 locking/ww-mutex:... |
602 |
ret = stress(16, 2*ncpus, STRESS_REORDER); |
2a0c11282 locking/ww_mutex:... |
603 604 |
if (ret) return ret; |
57dd924e5 locking/ww-mutex:... |
605 |
ret = stress(4095, hweight32(STRESS_ALL)*ncpus, STRESS_ALL); |
2a0c11282 locking/ww_mutex:... |
606 607 |
if (ret) return ret; |
f2a5fec17 locking/ww_mutex:... |
608 609 610 611 612 |
return 0; } static void __exit test_ww_mutex_exit(void) { |
d1b42b800 locking/ww_mutex:... |
613 |
destroy_workqueue(wq); |
f2a5fec17 locking/ww_mutex:... |
614 615 616 617 618 619 620 |
} module_init(test_ww_mutex_init); module_exit(test_ww_mutex_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Intel Corporation"); |