1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Scalability test comparing RCU vs other mechanisms 4 // for acquiring references on objects. 5 // 6 // Copyright (C) Google, 2020. 7 // 8 // Author: Joel Fernandes <joel@joelfernandes.org> 9 10 #define pr_fmt(fmt) fmt 11 12 #include <linux/atomic.h> 13 #include <linux/bitops.h> 14 #include <linux/completion.h> 15 #include <linux/cpu.h> 16 #include <linux/delay.h> 17 #include <linux/err.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/kthread.h> 21 #include <linux/kernel.h> 22 #include <linux/mm.h> 23 #include <linux/module.h> 24 #include <linux/moduleparam.h> 25 #include <linux/notifier.h> 26 #include <linux/percpu.h> 27 #include <linux/rcupdate.h> 28 #include <linux/rcupdate_trace.h> 29 #include <linux/reboot.h> 30 #include <linux/sched.h> 31 #include <linux/spinlock.h> 32 #include <linux/smp.h> 33 #include <linux/stat.h> 34 #include <linux/srcu.h> 35 #include <linux/slab.h> 36 #include <linux/torture.h> 37 #include <linux/types.h> 38 39 #include "rcu.h" 40 41 #define SCALE_FLAG "-ref-scale: " 42 43 #define SCALEOUT(s, x...) \ 44 pr_alert("%s" SCALE_FLAG s, scale_type, ## x) 45 46 #define VERBOSE_SCALEOUT(s, x...) \ 47 do { \ 48 if (verbose) \ 49 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \ 50 } while (0) 51 52 static atomic_t verbose_batch_ctr; 53 54 #define VERBOSE_SCALEOUT_BATCH(s, x...) \ 55 do { \ 56 if (verbose && \ 57 (verbose_batched <= 0 || \ 58 !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \ 59 schedule_timeout_uninterruptible(1); \ 60 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \ 61 } \ 62 } while (0) 63 64 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x) 65 66 MODULE_LICENSE("GPL"); 67 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>"); 68 69 static char *scale_type = "rcu"; 70 module_param(scale_type, charp, 0444); 71 MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock."); 72 73 torture_param(int, verbose, 0, "Enable verbose debugging printk()s"); 74 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s"); 75 76 // Wait until there are multiple CPUs before starting test. 77 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0, 78 "Holdoff time before test start (s)"); 79 // Number of loops per experiment, all readers execute operations concurrently. 80 torture_param(long, loops, 10000, "Number of loops per experiment."); 81 // Number of readers, with -1 defaulting to about 75% of the CPUs. 82 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs."); 83 // Number of runs. 84 torture_param(int, nruns, 30, "Number of experiments to run."); 85 // Reader delay in nanoseconds, 0 for no delay. 86 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds."); 87 88 #ifdef MODULE 89 # define REFSCALE_SHUTDOWN 0 90 #else 91 # define REFSCALE_SHUTDOWN 1 92 #endif 93 94 torture_param(bool, shutdown, REFSCALE_SHUTDOWN, 95 "Shutdown at end of scalability tests."); 96 97 struct reader_task { 98 struct task_struct *task; 99 int start_reader; 100 wait_queue_head_t wq; 101 u64 last_duration_ns; 102 }; 103 104 static struct task_struct *shutdown_task; 105 static wait_queue_head_t shutdown_wq; 106 107 static struct task_struct *main_task; 108 static wait_queue_head_t main_wq; 109 static int shutdown_start; 110 111 static struct reader_task *reader_tasks; 112 113 // Number of readers that are part of the current experiment. 114 static atomic_t nreaders_exp; 115 116 // Use to wait for all threads to start. 117 static atomic_t n_init; 118 static atomic_t n_started; 119 static atomic_t n_warmedup; 120 static atomic_t n_cooleddown; 121 122 // Track which experiment is currently running. 123 static int exp_idx; 124 125 // Operations vector for selecting different types of tests. 126 struct ref_scale_ops { 127 void (*init)(void); 128 void (*cleanup)(void); 129 void (*readsection)(const int nloops); 130 void (*delaysection)(const int nloops, const int udl, const int ndl); 131 const char *name; 132 }; 133 134 static struct ref_scale_ops *cur_ops; 135 136 static void un_delay(const int udl, const int ndl) 137 { 138 if (udl) 139 udelay(udl); 140 if (ndl) 141 ndelay(ndl); 142 } 143 144 static void ref_rcu_read_section(const int nloops) 145 { 146 int i; 147 148 for (i = nloops; i >= 0; i--) { 149 rcu_read_lock(); 150 rcu_read_unlock(); 151 } 152 } 153 154 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl) 155 { 156 int i; 157 158 for (i = nloops; i >= 0; i--) { 159 rcu_read_lock(); 160 un_delay(udl, ndl); 161 rcu_read_unlock(); 162 } 163 } 164 165 static void rcu_sync_scale_init(void) 166 { 167 } 168 169 static struct ref_scale_ops rcu_ops = { 170 .init = rcu_sync_scale_init, 171 .readsection = ref_rcu_read_section, 172 .delaysection = ref_rcu_delay_section, 173 .name = "rcu" 174 }; 175 176 // Definitions for SRCU ref scale testing. 177 DEFINE_STATIC_SRCU(srcu_refctl_scale); 178 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale; 179 180 static void srcu_ref_scale_read_section(const int nloops) 181 { 182 int i; 183 int idx; 184 185 for (i = nloops; i >= 0; i--) { 186 idx = srcu_read_lock(srcu_ctlp); 187 srcu_read_unlock(srcu_ctlp, idx); 188 } 189 } 190 191 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl) 192 { 193 int i; 194 int idx; 195 196 for (i = nloops; i >= 0; i--) { 197 idx = srcu_read_lock(srcu_ctlp); 198 un_delay(udl, ndl); 199 srcu_read_unlock(srcu_ctlp, idx); 200 } 201 } 202 203 static struct ref_scale_ops srcu_ops = { 204 .init = rcu_sync_scale_init, 205 .readsection = srcu_ref_scale_read_section, 206 .delaysection = srcu_ref_scale_delay_section, 207 .name = "srcu" 208 }; 209 210 #ifdef CONFIG_TASKS_RCU 211 212 // Definitions for RCU Tasks ref scale testing: Empty read markers. 213 // These definitions also work for RCU Rude readers. 214 static void rcu_tasks_ref_scale_read_section(const int nloops) 215 { 216 int i; 217 218 for (i = nloops; i >= 0; i--) 219 continue; 220 } 221 222 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl) 223 { 224 int i; 225 226 for (i = nloops; i >= 0; i--) 227 un_delay(udl, ndl); 228 } 229 230 static struct ref_scale_ops rcu_tasks_ops = { 231 .init = rcu_sync_scale_init, 232 .readsection = rcu_tasks_ref_scale_read_section, 233 .delaysection = rcu_tasks_ref_scale_delay_section, 234 .name = "rcu-tasks" 235 }; 236 237 #define RCU_TASKS_OPS &rcu_tasks_ops, 238 239 #else // #ifdef CONFIG_TASKS_RCU 240 241 #define RCU_TASKS_OPS 242 243 #endif // #else // #ifdef CONFIG_TASKS_RCU 244 245 // Definitions for RCU Tasks Trace ref scale testing. 246 static void rcu_trace_ref_scale_read_section(const int nloops) 247 { 248 int i; 249 250 for (i = nloops; i >= 0; i--) { 251 rcu_read_lock_trace(); 252 rcu_read_unlock_trace(); 253 } 254 } 255 256 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl) 257 { 258 int i; 259 260 for (i = nloops; i >= 0; i--) { 261 rcu_read_lock_trace(); 262 un_delay(udl, ndl); 263 rcu_read_unlock_trace(); 264 } 265 } 266 267 static struct ref_scale_ops rcu_trace_ops = { 268 .init = rcu_sync_scale_init, 269 .readsection = rcu_trace_ref_scale_read_section, 270 .delaysection = rcu_trace_ref_scale_delay_section, 271 .name = "rcu-trace" 272 }; 273 274 // Definitions for reference count 275 static atomic_t refcnt; 276 277 static void ref_refcnt_section(const int nloops) 278 { 279 int i; 280 281 for (i = nloops; i >= 0; i--) { 282 atomic_inc(&refcnt); 283 atomic_dec(&refcnt); 284 } 285 } 286 287 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl) 288 { 289 int i; 290 291 for (i = nloops; i >= 0; i--) { 292 atomic_inc(&refcnt); 293 un_delay(udl, ndl); 294 atomic_dec(&refcnt); 295 } 296 } 297 298 static struct ref_scale_ops refcnt_ops = { 299 .init = rcu_sync_scale_init, 300 .readsection = ref_refcnt_section, 301 .delaysection = ref_refcnt_delay_section, 302 .name = "refcnt" 303 }; 304 305 // Definitions for rwlock 306 static rwlock_t test_rwlock; 307 308 static void ref_rwlock_init(void) 309 { 310 rwlock_init(&test_rwlock); 311 } 312 313 static void ref_rwlock_section(const int nloops) 314 { 315 int i; 316 317 for (i = nloops; i >= 0; i--) { 318 read_lock(&test_rwlock); 319 read_unlock(&test_rwlock); 320 } 321 } 322 323 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl) 324 { 325 int i; 326 327 for (i = nloops; i >= 0; i--) { 328 read_lock(&test_rwlock); 329 un_delay(udl, ndl); 330 read_unlock(&test_rwlock); 331 } 332 } 333 334 static struct ref_scale_ops rwlock_ops = { 335 .init = ref_rwlock_init, 336 .readsection = ref_rwlock_section, 337 .delaysection = ref_rwlock_delay_section, 338 .name = "rwlock" 339 }; 340 341 // Definitions for rwsem 342 static struct rw_semaphore test_rwsem; 343 344 static void ref_rwsem_init(void) 345 { 346 init_rwsem(&test_rwsem); 347 } 348 349 static void ref_rwsem_section(const int nloops) 350 { 351 int i; 352 353 for (i = nloops; i >= 0; i--) { 354 down_read(&test_rwsem); 355 up_read(&test_rwsem); 356 } 357 } 358 359 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl) 360 { 361 int i; 362 363 for (i = nloops; i >= 0; i--) { 364 down_read(&test_rwsem); 365 un_delay(udl, ndl); 366 up_read(&test_rwsem); 367 } 368 } 369 370 static struct ref_scale_ops rwsem_ops = { 371 .init = ref_rwsem_init, 372 .readsection = ref_rwsem_section, 373 .delaysection = ref_rwsem_delay_section, 374 .name = "rwsem" 375 }; 376 377 // Definitions for global spinlock 378 static DEFINE_SPINLOCK(test_lock); 379 380 static void ref_lock_section(const int nloops) 381 { 382 int i; 383 384 preempt_disable(); 385 for (i = nloops; i >= 0; i--) { 386 spin_lock(&test_lock); 387 spin_unlock(&test_lock); 388 } 389 preempt_enable(); 390 } 391 392 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl) 393 { 394 int i; 395 396 preempt_disable(); 397 for (i = nloops; i >= 0; i--) { 398 spin_lock(&test_lock); 399 un_delay(udl, ndl); 400 spin_unlock(&test_lock); 401 } 402 preempt_enable(); 403 } 404 405 static struct ref_scale_ops lock_ops = { 406 .readsection = ref_lock_section, 407 .delaysection = ref_lock_delay_section, 408 .name = "lock" 409 }; 410 411 // Definitions for global irq-save spinlock 412 413 static void ref_lock_irq_section(const int nloops) 414 { 415 unsigned long flags; 416 int i; 417 418 preempt_disable(); 419 for (i = nloops; i >= 0; i--) { 420 spin_lock_irqsave(&test_lock, flags); 421 spin_unlock_irqrestore(&test_lock, flags); 422 } 423 preempt_enable(); 424 } 425 426 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl) 427 { 428 unsigned long flags; 429 int i; 430 431 preempt_disable(); 432 for (i = nloops; i >= 0; i--) { 433 spin_lock_irqsave(&test_lock, flags); 434 un_delay(udl, ndl); 435 spin_unlock_irqrestore(&test_lock, flags); 436 } 437 preempt_enable(); 438 } 439 440 static struct ref_scale_ops lock_irq_ops = { 441 .readsection = ref_lock_irq_section, 442 .delaysection = ref_lock_irq_delay_section, 443 .name = "lock-irq" 444 }; 445 446 // Definitions acquire-release. 447 static DEFINE_PER_CPU(unsigned long, test_acqrel); 448 449 static void ref_acqrel_section(const int nloops) 450 { 451 unsigned long x; 452 int i; 453 454 preempt_disable(); 455 for (i = nloops; i >= 0; i--) { 456 x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); 457 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); 458 } 459 preempt_enable(); 460 } 461 462 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl) 463 { 464 unsigned long x; 465 int i; 466 467 preempt_disable(); 468 for (i = nloops; i >= 0; i--) { 469 x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); 470 un_delay(udl, ndl); 471 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); 472 } 473 preempt_enable(); 474 } 475 476 static struct ref_scale_ops acqrel_ops = { 477 .readsection = ref_acqrel_section, 478 .delaysection = ref_acqrel_delay_section, 479 .name = "acqrel" 480 }; 481 482 static volatile u64 stopopts; 483 484 static void ref_clock_section(const int nloops) 485 { 486 u64 x = 0; 487 int i; 488 489 preempt_disable(); 490 for (i = nloops; i >= 0; i--) 491 x += ktime_get_real_fast_ns(); 492 preempt_enable(); 493 stopopts = x; 494 } 495 496 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl) 497 { 498 u64 x = 0; 499 int i; 500 501 preempt_disable(); 502 for (i = nloops; i >= 0; i--) { 503 x += ktime_get_real_fast_ns(); 504 un_delay(udl, ndl); 505 } 506 preempt_enable(); 507 stopopts = x; 508 } 509 510 static struct ref_scale_ops clock_ops = { 511 .readsection = ref_clock_section, 512 .delaysection = ref_clock_delay_section, 513 .name = "clock" 514 }; 515 516 static void rcu_scale_one_reader(void) 517 { 518 if (readdelay <= 0) 519 cur_ops->readsection(loops); 520 else 521 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000); 522 } 523 524 // Reader kthread. Repeatedly does empty RCU read-side 525 // critical section, minimizing update-side interference. 526 static int 527 ref_scale_reader(void *arg) 528 { 529 unsigned long flags; 530 long me = (long)arg; 531 struct reader_task *rt = &(reader_tasks[me]); 532 u64 start; 533 s64 duration; 534 535 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me); 536 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids))); 537 set_user_nice(current, MAX_NICE); 538 atomic_inc(&n_init); 539 if (holdoff) 540 schedule_timeout_interruptible(holdoff * HZ); 541 repeat: 542 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id()); 543 544 // Wait for signal that this reader can start. 545 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) || 546 torture_must_stop()); 547 548 if (torture_must_stop()) 549 goto end; 550 551 // Make sure that the CPU is affinitized appropriately during testing. 552 WARN_ON_ONCE(raw_smp_processor_id() != me); 553 554 WRITE_ONCE(rt->start_reader, 0); 555 if (!atomic_dec_return(&n_started)) 556 while (atomic_read_acquire(&n_started)) 557 cpu_relax(); 558 559 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx); 560 561 562 // To reduce noise, do an initial cache-warming invocation, check 563 // in, and then keep warming until everyone has checked in. 564 rcu_scale_one_reader(); 565 if (!atomic_dec_return(&n_warmedup)) 566 while (atomic_read_acquire(&n_warmedup)) 567 rcu_scale_one_reader(); 568 // Also keep interrupts disabled. This also has the effect 569 // of preventing entries into slow path for rcu_read_unlock(). 570 local_irq_save(flags); 571 start = ktime_get_mono_fast_ns(); 572 573 rcu_scale_one_reader(); 574 575 duration = ktime_get_mono_fast_ns() - start; 576 local_irq_restore(flags); 577 578 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration; 579 // To reduce runtime-skew noise, do maintain-load invocations until 580 // everyone is done. 581 if (!atomic_dec_return(&n_cooleddown)) 582 while (atomic_read_acquire(&n_cooleddown)) 583 rcu_scale_one_reader(); 584 585 if (atomic_dec_and_test(&nreaders_exp)) 586 wake_up(&main_wq); 587 588 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)", 589 me, exp_idx, atomic_read(&nreaders_exp)); 590 591 if (!torture_must_stop()) 592 goto repeat; 593 end: 594 torture_kthread_stopping("ref_scale_reader"); 595 return 0; 596 } 597 598 static void reset_readers(void) 599 { 600 int i; 601 struct reader_task *rt; 602 603 for (i = 0; i < nreaders; i++) { 604 rt = &(reader_tasks[i]); 605 606 rt->last_duration_ns = 0; 607 } 608 } 609 610 // Print the results of each reader and return the sum of all their durations. 611 static u64 process_durations(int n) 612 { 613 int i; 614 struct reader_task *rt; 615 char buf1[64]; 616 char *buf; 617 u64 sum = 0; 618 619 buf = kmalloc(800 + 64, GFP_KERNEL); 620 if (!buf) 621 return 0; 622 buf[0] = 0; 623 sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", 624 exp_idx); 625 626 for (i = 0; i < n && !torture_must_stop(); i++) { 627 rt = &(reader_tasks[i]); 628 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); 629 630 if (i % 5 == 0) 631 strcat(buf, "\n"); 632 if (strlen(buf) >= 800) { 633 pr_alert("%s", buf); 634 buf[0] = 0; 635 } 636 strcat(buf, buf1); 637 638 sum += rt->last_duration_ns; 639 } 640 pr_alert("%s\n", buf); 641 642 kfree(buf); 643 return sum; 644 } 645 646 // The main_func is the main orchestrator, it performs a bunch of 647 // experiments. For every experiment, it orders all the readers 648 // involved to start and waits for them to finish the experiment. It 649 // then reads their timestamps and starts the next experiment. Each 650 // experiment progresses from 1 concurrent reader to N of them at which 651 // point all the timestamps are printed. 652 static int main_func(void *arg) 653 { 654 int exp, r; 655 char buf1[64]; 656 char *buf; 657 u64 *result_avg; 658 659 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids)); 660 set_user_nice(current, MAX_NICE); 661 662 VERBOSE_SCALEOUT("main_func task started"); 663 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL); 664 buf = kzalloc(800 + 64, GFP_KERNEL); 665 if (!result_avg || !buf) { 666 SCALEOUT_ERRSTRING("out of memory"); 667 goto oom_exit; 668 } 669 if (holdoff) 670 schedule_timeout_interruptible(holdoff * HZ); 671 672 // Wait for all threads to start. 673 atomic_inc(&n_init); 674 while (atomic_read(&n_init) < nreaders + 1) 675 schedule_timeout_uninterruptible(1); 676 677 // Start exp readers up per experiment 678 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) { 679 if (torture_must_stop()) 680 goto end; 681 682 reset_readers(); 683 atomic_set(&nreaders_exp, nreaders); 684 atomic_set(&n_started, nreaders); 685 atomic_set(&n_warmedup, nreaders); 686 atomic_set(&n_cooleddown, nreaders); 687 688 exp_idx = exp; 689 690 for (r = 0; r < nreaders; r++) { 691 smp_store_release(&reader_tasks[r].start_reader, 1); 692 wake_up(&reader_tasks[r].wq); 693 } 694 695 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers", 696 nreaders); 697 698 wait_event(main_wq, 699 !atomic_read(&nreaders_exp) || torture_must_stop()); 700 701 VERBOSE_SCALEOUT("main_func: experiment ended"); 702 703 if (torture_must_stop()) 704 goto end; 705 706 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops); 707 } 708 709 // Print the average of all experiments 710 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n"); 711 712 pr_alert("Runs\tTime(ns)\n"); 713 for (exp = 0; exp < nruns; exp++) { 714 u64 avg; 715 u32 rem; 716 717 avg = div_u64_rem(result_avg[exp], 1000, &rem); 718 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem); 719 strcat(buf, buf1); 720 if (strlen(buf) >= 800) { 721 pr_alert("%s", buf); 722 buf[0] = 0; 723 } 724 } 725 726 pr_alert("%s", buf); 727 728 oom_exit: 729 // This will shutdown everything including us. 730 if (shutdown) { 731 shutdown_start = 1; 732 wake_up(&shutdown_wq); 733 } 734 735 // Wait for torture to stop us 736 while (!torture_must_stop()) 737 schedule_timeout_uninterruptible(1); 738 739 end: 740 torture_kthread_stopping("main_func"); 741 kfree(result_avg); 742 kfree(buf); 743 return 0; 744 } 745 746 static void 747 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag) 748 { 749 pr_alert("%s" SCALE_FLAG 750 "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag, 751 verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay); 752 } 753 754 static void 755 ref_scale_cleanup(void) 756 { 757 int i; 758 759 if (torture_cleanup_begin()) 760 return; 761 762 if (!cur_ops) { 763 torture_cleanup_end(); 764 return; 765 } 766 767 if (reader_tasks) { 768 for (i = 0; i < nreaders; i++) 769 torture_stop_kthread("ref_scale_reader", 770 reader_tasks[i].task); 771 } 772 kfree(reader_tasks); 773 774 torture_stop_kthread("main_task", main_task); 775 kfree(main_task); 776 777 // Do scale-type-specific cleanup operations. 778 if (cur_ops->cleanup != NULL) 779 cur_ops->cleanup(); 780 781 torture_cleanup_end(); 782 } 783 784 // Shutdown kthread. Just waits to be awakened, then shuts down system. 785 static int 786 ref_scale_shutdown(void *arg) 787 { 788 wait_event(shutdown_wq, shutdown_start); 789 790 smp_mb(); // Wake before output. 791 ref_scale_cleanup(); 792 kernel_power_off(); 793 794 return -EINVAL; 795 } 796 797 static int __init 798 ref_scale_init(void) 799 { 800 long i; 801 int firsterr = 0; 802 static struct ref_scale_ops *scale_ops[] = { 803 &rcu_ops, &srcu_ops, &rcu_trace_ops, RCU_TASKS_OPS &refcnt_ops, &rwlock_ops, 804 &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops, 805 }; 806 807 if (!torture_init_begin(scale_type, verbose)) 808 return -EBUSY; 809 810 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) { 811 cur_ops = scale_ops[i]; 812 if (strcmp(scale_type, cur_ops->name) == 0) 813 break; 814 } 815 if (i == ARRAY_SIZE(scale_ops)) { 816 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type); 817 pr_alert("rcu-scale types:"); 818 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) 819 pr_cont(" %s", scale_ops[i]->name); 820 pr_cont("\n"); 821 firsterr = -EINVAL; 822 cur_ops = NULL; 823 goto unwind; 824 } 825 if (cur_ops->init) 826 cur_ops->init(); 827 828 ref_scale_print_module_parms(cur_ops, "Start of test"); 829 830 // Shutdown task 831 if (shutdown) { 832 init_waitqueue_head(&shutdown_wq); 833 firsterr = torture_create_kthread(ref_scale_shutdown, NULL, 834 shutdown_task); 835 if (torture_init_error(firsterr)) 836 goto unwind; 837 schedule_timeout_uninterruptible(1); 838 } 839 840 // Reader tasks (default to ~75% of online CPUs). 841 if (nreaders < 0) 842 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2); 843 if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops)) 844 loops = 1; 845 if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders)) 846 nreaders = 1; 847 if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns)) 848 nruns = 1; 849 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]), 850 GFP_KERNEL); 851 if (!reader_tasks) { 852 SCALEOUT_ERRSTRING("out of memory"); 853 firsterr = -ENOMEM; 854 goto unwind; 855 } 856 857 VERBOSE_SCALEOUT("Starting %d reader threads", nreaders); 858 859 for (i = 0; i < nreaders; i++) { 860 firsterr = torture_create_kthread(ref_scale_reader, (void *)i, 861 reader_tasks[i].task); 862 if (torture_init_error(firsterr)) 863 goto unwind; 864 865 init_waitqueue_head(&(reader_tasks[i].wq)); 866 } 867 868 // Main Task 869 init_waitqueue_head(&main_wq); 870 firsterr = torture_create_kthread(main_func, NULL, main_task); 871 if (torture_init_error(firsterr)) 872 goto unwind; 873 874 torture_init_end(); 875 return 0; 876 877 unwind: 878 torture_init_end(); 879 ref_scale_cleanup(); 880 if (shutdown) { 881 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST)); 882 kernel_power_off(); 883 } 884 return firsterr; 885 } 886 887 module_init(ref_scale_init); 888 module_exit(ref_scale_cleanup); 889