1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Read-Copy Update module-based scalability-test facility 4 * 5 * Copyright (C) IBM Corporation, 2015 6 * 7 * Authors: Paul E. McKenney <paulmck@linux.ibm.com> 8 */ 9 10 #define pr_fmt(fmt) fmt 11 12 #include <linux/types.h> 13 #include <linux/kernel.h> 14 #include <linux/init.h> 15 #include <linux/mm.h> 16 #include <linux/module.h> 17 #include <linux/kthread.h> 18 #include <linux/err.h> 19 #include <linux/spinlock.h> 20 #include <linux/smp.h> 21 #include <linux/rcupdate.h> 22 #include <linux/interrupt.h> 23 #include <linux/sched.h> 24 #include <uapi/linux/sched/types.h> 25 #include <linux/atomic.h> 26 #include <linux/bitops.h> 27 #include <linux/completion.h> 28 #include <linux/moduleparam.h> 29 #include <linux/percpu.h> 30 #include <linux/notifier.h> 31 #include <linux/reboot.h> 32 #include <linux/freezer.h> 33 #include <linux/cpu.h> 34 #include <linux/delay.h> 35 #include <linux/stat.h> 36 #include <linux/srcu.h> 37 #include <linux/slab.h> 38 #include <asm/byteorder.h> 39 #include <linux/torture.h> 40 #include <linux/vmalloc.h> 41 #include <linux/rcupdate_trace.h> 42 #include <linux/sched/debug.h> 43 44 #include "rcu.h" 45 46 MODULE_DESCRIPTION("Read-Copy Update module-based scalability-test facility"); 47 MODULE_LICENSE("GPL"); 48 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>"); 49 50 #define SCALE_FLAG "-scale:" 51 #define SCALEOUT_STRING(s) \ 52 pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s) 53 #define VERBOSE_SCALEOUT_STRING(s) \ 54 do { if (verbose) pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s); } while (0) 55 #define SCALEOUT_ERRSTRING(s) \ 56 pr_alert("%s" SCALE_FLAG "!!! %s\n", scale_type, s) 57 58 /* 59 * The intended use cases for the nreaders and nwriters module parameters 60 * are as follows: 61 * 62 * 1. Specify only the nr_cpus kernel boot parameter. This will 63 * set both nreaders and nwriters to the value specified by 64 * nr_cpus for a mixed reader/writer test. 65 * 66 * 2. Specify the nr_cpus kernel boot parameter, but set 67 * rcuscale.nreaders to zero. This will set nwriters to the 68 * value specified by nr_cpus for an update-only test. 69 * 70 * 3. Specify the nr_cpus kernel boot parameter, but set 71 * rcuscale.nwriters to zero. This will set nreaders to the 72 * value specified by nr_cpus for a read-only test. 73 * 74 * Various other use cases may of course be specified. 75 * 76 * Note that this test's readers are intended only as a test load for 77 * the writers. The reader scalability statistics will be overly 78 * pessimistic due to the per-critical-section interrupt disabling, 79 * test-end checks, and the pair of calls through pointers. 80 */ 81 82 #ifdef MODULE 83 # define RCUSCALE_SHUTDOWN 0 84 #else 85 # define RCUSCALE_SHUTDOWN 1 86 #endif 87 88 torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives"); 89 torture_param(int, gp_async_max, 1000, "Max # outstanding waits per writer"); 90 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives"); 91 torture_param(int, holdoff, 10, "Holdoff time before test start (s)"); 92 torture_param(int, minruntime, 0, "Minimum run time (s)"); 93 torture_param(int, nreaders, -1, "Number of RCU reader threads"); 94 torture_param(int, nwriters, -1, "Number of RCU updater threads"); 95 torture_param(bool, shutdown, RCUSCALE_SHUTDOWN, 96 "Shutdown at end of scalability tests."); 97 torture_param(int, verbose, 1, "Enable verbose debugging printk()s"); 98 torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable"); 99 torture_param(int, writer_holdoff_jiffies, 0, "Holdoff (jiffies) between GPs, zero to disable"); 100 torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() scale test?"); 101 torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate."); 102 torture_param(int, kfree_by_call_rcu, 0, "Use call_rcu() to emulate kfree_rcu()?"); 103 104 static char *scale_type = "rcu"; 105 module_param(scale_type, charp, 0444); 106 MODULE_PARM_DESC(scale_type, "Type of RCU to scalability-test (rcu, srcu, ...)"); 107 108 // Structure definitions for custom fixed-per-task allocator. 109 struct writer_mblock { 110 struct rcu_head wmb_rh; 111 struct llist_node wmb_node; 112 struct writer_freelist *wmb_wfl; 113 }; 114 115 struct writer_freelist { 116 struct llist_head ws_lhg; 117 atomic_t ws_inflight; 118 struct llist_head ____cacheline_internodealigned_in_smp ws_lhp; 119 struct writer_mblock *ws_mblocks; 120 }; 121 122 static int nrealreaders; 123 static int nrealwriters; 124 static struct task_struct **writer_tasks; 125 static struct task_struct **reader_tasks; 126 static struct task_struct *shutdown_task; 127 128 static u64 **writer_durations; 129 static bool *writer_done; 130 static struct writer_freelist *writer_freelists; 131 static int *writer_n_durations; 132 static atomic_t n_rcu_scale_reader_started; 133 static atomic_t n_rcu_scale_writer_started; 134 static atomic_t n_rcu_scale_writer_finished; 135 static wait_queue_head_t shutdown_wq; 136 static u64 t_rcu_scale_writer_started; 137 static u64 t_rcu_scale_writer_finished; 138 static unsigned long b_rcu_gp_test_started; 139 static unsigned long b_rcu_gp_test_finished; 140 141 #define MAX_MEAS 10000 142 #define MIN_MEAS 100 143 144 /* 145 * Operations vector for selecting different types of tests. 146 */ 147 148 struct rcu_scale_ops { 149 int ptype; 150 void (*init)(void); 151 void (*cleanup)(void); 152 int (*readlock)(void); 153 void (*readunlock)(int idx); 154 unsigned long (*get_gp_seq)(void); 155 unsigned long (*gp_diff)(unsigned long new, unsigned long old); 156 unsigned long (*exp_completed)(void); 157 void (*async)(struct rcu_head *head, rcu_callback_t func); 158 void (*gp_barrier)(void); 159 void (*sync)(void); 160 void (*exp_sync)(void); 161 struct task_struct *(*rso_gp_kthread)(void); 162 void (*stats)(void); 163 const char *name; 164 }; 165 166 static struct rcu_scale_ops *cur_ops; 167 168 /* 169 * Definitions for rcu scalability testing. 170 */ 171 172 static int rcu_scale_read_lock(void) __acquires(RCU) 173 { 174 rcu_read_lock(); 175 return 0; 176 } 177 178 static void rcu_scale_read_unlock(int idx) __releases(RCU) 179 { 180 rcu_read_unlock(); 181 } 182 183 static unsigned long __maybe_unused rcu_no_completed(void) 184 { 185 return 0; 186 } 187 188 static void rcu_sync_scale_init(void) 189 { 190 } 191 192 static struct rcu_scale_ops rcu_ops = { 193 .ptype = RCU_FLAVOR, 194 .init = rcu_sync_scale_init, 195 .readlock = rcu_scale_read_lock, 196 .readunlock = rcu_scale_read_unlock, 197 .get_gp_seq = rcu_get_gp_seq, 198 .gp_diff = rcu_seq_diff, 199 .exp_completed = rcu_exp_batches_completed, 200 .async = call_rcu_hurry, 201 .gp_barrier = rcu_barrier, 202 .sync = synchronize_rcu, 203 .exp_sync = synchronize_rcu_expedited, 204 .name = "rcu" 205 }; 206 207 /* 208 * Definitions for srcu scalability testing. 209 */ 210 211 DEFINE_STATIC_SRCU(srcu_ctl_scale); 212 static struct srcu_struct *srcu_ctlp = &srcu_ctl_scale; 213 214 static int srcu_scale_read_lock(void) __acquires(srcu_ctlp) 215 { 216 return srcu_read_lock(srcu_ctlp); 217 } 218 219 static void srcu_scale_read_unlock(int idx) __releases(srcu_ctlp) 220 { 221 srcu_read_unlock(srcu_ctlp, idx); 222 } 223 224 static unsigned long srcu_scale_completed(void) 225 { 226 return srcu_batches_completed(srcu_ctlp); 227 } 228 229 static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func) 230 { 231 call_srcu(srcu_ctlp, head, func); 232 } 233 234 static void srcu_rcu_barrier(void) 235 { 236 srcu_barrier(srcu_ctlp); 237 } 238 239 static void srcu_scale_synchronize(void) 240 { 241 synchronize_srcu(srcu_ctlp); 242 } 243 244 static void srcu_scale_stats(void) 245 { 246 srcu_torture_stats_print(srcu_ctlp, scale_type, SCALE_FLAG); 247 } 248 249 static void srcu_scale_synchronize_expedited(void) 250 { 251 synchronize_srcu_expedited(srcu_ctlp); 252 } 253 254 static struct rcu_scale_ops srcu_ops = { 255 .ptype = SRCU_FLAVOR, 256 .init = rcu_sync_scale_init, 257 .readlock = srcu_scale_read_lock, 258 .readunlock = srcu_scale_read_unlock, 259 .get_gp_seq = srcu_scale_completed, 260 .gp_diff = rcu_seq_diff, 261 .exp_completed = srcu_scale_completed, 262 .async = srcu_call_rcu, 263 .gp_barrier = srcu_rcu_barrier, 264 .sync = srcu_scale_synchronize, 265 .exp_sync = srcu_scale_synchronize_expedited, 266 .stats = srcu_scale_stats, 267 .name = "srcu" 268 }; 269 270 static struct srcu_struct srcud; 271 272 static void srcu_sync_scale_init(void) 273 { 274 srcu_ctlp = &srcud; 275 init_srcu_struct(srcu_ctlp); 276 } 277 278 static void srcu_sync_scale_cleanup(void) 279 { 280 cleanup_srcu_struct(srcu_ctlp); 281 } 282 283 static struct rcu_scale_ops srcud_ops = { 284 .ptype = SRCU_FLAVOR, 285 .init = srcu_sync_scale_init, 286 .cleanup = srcu_sync_scale_cleanup, 287 .readlock = srcu_scale_read_lock, 288 .readunlock = srcu_scale_read_unlock, 289 .get_gp_seq = srcu_scale_completed, 290 .gp_diff = rcu_seq_diff, 291 .exp_completed = srcu_scale_completed, 292 .async = srcu_call_rcu, 293 .gp_barrier = srcu_rcu_barrier, 294 .sync = srcu_scale_synchronize, 295 .exp_sync = srcu_scale_synchronize_expedited, 296 .stats = srcu_scale_stats, 297 .name = "srcud" 298 }; 299 300 #ifdef CONFIG_TASKS_RCU 301 302 /* 303 * Definitions for RCU-tasks scalability testing. 304 */ 305 306 static int tasks_scale_read_lock(void) 307 { 308 return 0; 309 } 310 311 static void tasks_scale_read_unlock(int idx) 312 { 313 } 314 315 static void rcu_tasks_scale_stats(void) 316 { 317 rcu_tasks_torture_stats_print(scale_type, SCALE_FLAG); 318 } 319 320 static struct rcu_scale_ops tasks_ops = { 321 .ptype = RCU_TASKS_FLAVOR, 322 .init = rcu_sync_scale_init, 323 .readlock = tasks_scale_read_lock, 324 .readunlock = tasks_scale_read_unlock, 325 .get_gp_seq = rcu_no_completed, 326 .gp_diff = rcu_seq_diff, 327 .async = call_rcu_tasks, 328 .gp_barrier = rcu_barrier_tasks, 329 .sync = synchronize_rcu_tasks, 330 .exp_sync = synchronize_rcu_tasks, 331 .rso_gp_kthread = get_rcu_tasks_gp_kthread, 332 .stats = IS_ENABLED(CONFIG_TINY_RCU) ? NULL : rcu_tasks_scale_stats, 333 .name = "tasks" 334 }; 335 336 #define TASKS_OPS &tasks_ops, 337 338 #else // #ifdef CONFIG_TASKS_RCU 339 340 #define TASKS_OPS 341 342 #endif // #else // #ifdef CONFIG_TASKS_RCU 343 344 #ifdef CONFIG_TASKS_RUDE_RCU 345 346 /* 347 * Definitions for RCU-tasks-rude scalability testing. 348 */ 349 350 static int tasks_rude_scale_read_lock(void) 351 { 352 return 0; 353 } 354 355 static void tasks_rude_scale_read_unlock(int idx) 356 { 357 } 358 359 static void rcu_tasks_rude_scale_stats(void) 360 { 361 rcu_tasks_rude_torture_stats_print(scale_type, SCALE_FLAG); 362 } 363 364 static struct rcu_scale_ops tasks_rude_ops = { 365 .ptype = RCU_TASKS_RUDE_FLAVOR, 366 .init = rcu_sync_scale_init, 367 .readlock = tasks_rude_scale_read_lock, 368 .readunlock = tasks_rude_scale_read_unlock, 369 .get_gp_seq = rcu_no_completed, 370 .gp_diff = rcu_seq_diff, 371 .sync = synchronize_rcu_tasks_rude, 372 .exp_sync = synchronize_rcu_tasks_rude, 373 .rso_gp_kthread = get_rcu_tasks_rude_gp_kthread, 374 .stats = IS_ENABLED(CONFIG_TINY_RCU) ? NULL : rcu_tasks_rude_scale_stats, 375 .name = "tasks-rude" 376 }; 377 378 #define TASKS_RUDE_OPS &tasks_rude_ops, 379 380 #else // #ifdef CONFIG_TASKS_RUDE_RCU 381 382 #define TASKS_RUDE_OPS 383 384 #endif // #else // #ifdef CONFIG_TASKS_RUDE_RCU 385 386 #ifdef CONFIG_TASKS_TRACE_RCU 387 388 /* 389 * Definitions for RCU-tasks-trace scalability testing. 390 */ 391 392 static int tasks_trace_scale_read_lock(void) 393 { 394 rcu_read_lock_trace(); 395 return 0; 396 } 397 398 static void tasks_trace_scale_read_unlock(int idx) 399 { 400 rcu_read_unlock_trace(); 401 } 402 403 static struct rcu_scale_ops tasks_tracing_ops = { 404 .ptype = RCU_TASKS_FLAVOR, 405 .init = rcu_sync_scale_init, 406 .readlock = tasks_trace_scale_read_lock, 407 .readunlock = tasks_trace_scale_read_unlock, 408 .get_gp_seq = rcu_no_completed, 409 .gp_diff = rcu_seq_diff, 410 .async = call_rcu_tasks_trace, 411 .gp_barrier = rcu_barrier_tasks_trace, 412 .sync = synchronize_rcu_tasks_trace, 413 .exp_sync = synchronize_rcu_tasks_trace, 414 .name = "tasks-tracing" 415 }; 416 417 #define TASKS_TRACING_OPS &tasks_tracing_ops, 418 419 #else // #ifdef CONFIG_TASKS_TRACE_RCU 420 421 #define TASKS_TRACING_OPS 422 423 #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU 424 425 static unsigned long rcuscale_seq_diff(unsigned long new, unsigned long old) 426 { 427 if (!cur_ops->gp_diff) 428 return new - old; 429 return cur_ops->gp_diff(new, old); 430 } 431 432 /* 433 * If scalability tests complete, wait for shutdown to commence. 434 */ 435 static void rcu_scale_wait_shutdown(void) 436 { 437 cond_resched_tasks_rcu_qs(); 438 if (atomic_read(&n_rcu_scale_writer_finished) < nrealwriters) 439 return; 440 while (!torture_must_stop()) 441 schedule_timeout_uninterruptible(1); 442 } 443 444 /* 445 * RCU scalability reader kthread. Repeatedly does empty RCU read-side 446 * critical section, minimizing update-side interference. However, the 447 * point of this test is not to evaluate reader scalability, but instead 448 * to serve as a test load for update-side scalability testing. 449 */ 450 static int 451 rcu_scale_reader(void *arg) 452 { 453 unsigned long flags; 454 int idx; 455 long me = (long)arg; 456 457 VERBOSE_SCALEOUT_STRING("rcu_scale_reader task started"); 458 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); 459 set_user_nice(current, MAX_NICE); 460 atomic_inc(&n_rcu_scale_reader_started); 461 462 do { 463 local_irq_save(flags); 464 idx = cur_ops->readlock(); 465 cur_ops->readunlock(idx); 466 local_irq_restore(flags); 467 rcu_scale_wait_shutdown(); 468 } while (!torture_must_stop()); 469 torture_kthread_stopping("rcu_scale_reader"); 470 return 0; 471 } 472 473 /* 474 * Allocate a writer_mblock structure for the specified rcu_scale_writer 475 * task. 476 */ 477 static struct writer_mblock *rcu_scale_alloc(long me) 478 { 479 struct llist_node *llnp; 480 struct writer_freelist *wflp; 481 struct writer_mblock *wmbp; 482 483 if (WARN_ON_ONCE(!writer_freelists)) 484 return NULL; 485 wflp = &writer_freelists[me]; 486 if (llist_empty(&wflp->ws_lhp)) { 487 // ->ws_lhp is private to its rcu_scale_writer task. 488 wmbp = container_of(llist_del_all(&wflp->ws_lhg), struct writer_mblock, wmb_node); 489 wflp->ws_lhp.first = &wmbp->wmb_node; 490 } 491 llnp = llist_del_first(&wflp->ws_lhp); 492 if (!llnp) 493 return NULL; 494 return container_of(llnp, struct writer_mblock, wmb_node); 495 } 496 497 /* 498 * Free a writer_mblock structure to its rcu_scale_writer task. 499 */ 500 static void rcu_scale_free(struct writer_mblock *wmbp) 501 { 502 struct writer_freelist *wflp; 503 504 if (!wmbp) 505 return; 506 wflp = wmbp->wmb_wfl; 507 llist_add(&wmbp->wmb_node, &wflp->ws_lhg); 508 } 509 510 /* 511 * Callback function for asynchronous grace periods from rcu_scale_writer(). 512 */ 513 static void rcu_scale_async_cb(struct rcu_head *rhp) 514 { 515 struct writer_mblock *wmbp = container_of(rhp, struct writer_mblock, wmb_rh); 516 struct writer_freelist *wflp = wmbp->wmb_wfl; 517 518 atomic_dec(&wflp->ws_inflight); 519 rcu_scale_free(wmbp); 520 } 521 522 /* 523 * RCU scale writer kthread. Repeatedly does a grace period. 524 */ 525 static int 526 rcu_scale_writer(void *arg) 527 { 528 int i = 0; 529 int i_max; 530 unsigned long jdone; 531 long me = (long)arg; 532 bool selfreport = false; 533 bool started = false, done = false, alldone = false; 534 u64 t; 535 DEFINE_TORTURE_RANDOM(tr); 536 u64 *wdp; 537 u64 *wdpp = writer_durations[me]; 538 struct writer_freelist *wflp = &writer_freelists[me]; 539 struct writer_mblock *wmbp = NULL; 540 541 VERBOSE_SCALEOUT_STRING("rcu_scale_writer task started"); 542 WARN_ON(!wdpp); 543 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); 544 current->flags |= PF_NO_SETAFFINITY; 545 sched_set_fifo_low(current); 546 547 if (holdoff) 548 schedule_timeout_idle(holdoff * HZ); 549 550 /* 551 * Wait until rcu_end_inkernel_boot() is called for normal GP tests 552 * so that RCU is not always expedited for normal GP tests. 553 * The system_state test is approximate, but works well in practice. 554 */ 555 while (!gp_exp && system_state != SYSTEM_RUNNING) 556 schedule_timeout_uninterruptible(1); 557 558 t = ktime_get_mono_fast_ns(); 559 if (atomic_inc_return(&n_rcu_scale_writer_started) >= nrealwriters) { 560 t_rcu_scale_writer_started = t; 561 if (gp_exp) { 562 b_rcu_gp_test_started = 563 cur_ops->exp_completed() / 2; 564 } else { 565 b_rcu_gp_test_started = cur_ops->get_gp_seq(); 566 } 567 } 568 569 jdone = jiffies + minruntime * HZ; 570 do { 571 bool gp_succeeded = false; 572 573 if (writer_holdoff) 574 udelay(writer_holdoff); 575 if (writer_holdoff_jiffies) 576 schedule_timeout_idle(torture_random(&tr) % writer_holdoff_jiffies + 1); 577 wdp = &wdpp[i]; 578 *wdp = ktime_get_mono_fast_ns(); 579 if (gp_async && !WARN_ON_ONCE(!cur_ops->async)) { 580 if (!wmbp) 581 wmbp = rcu_scale_alloc(me); 582 if (wmbp && atomic_read(&wflp->ws_inflight) < gp_async_max) { 583 atomic_inc(&wflp->ws_inflight); 584 cur_ops->async(&wmbp->wmb_rh, rcu_scale_async_cb); 585 wmbp = NULL; 586 gp_succeeded = true; 587 } else if (!kthread_should_stop()) { 588 cur_ops->gp_barrier(); 589 } else { 590 rcu_scale_free(wmbp); /* Because we are stopping. */ 591 wmbp = NULL; 592 } 593 } else if (gp_exp) { 594 cur_ops->exp_sync(); 595 gp_succeeded = true; 596 } else { 597 cur_ops->sync(); 598 gp_succeeded = true; 599 } 600 t = ktime_get_mono_fast_ns(); 601 *wdp = t - *wdp; 602 i_max = i; 603 if (!started && 604 atomic_read(&n_rcu_scale_writer_started) >= nrealwriters) 605 started = true; 606 if (!done && i >= MIN_MEAS && time_after(jiffies, jdone)) { 607 done = true; 608 WRITE_ONCE(writer_done[me], true); 609 sched_set_normal(current, 0); 610 pr_alert("%s%s rcu_scale_writer %ld has %d measurements\n", 611 scale_type, SCALE_FLAG, me, MIN_MEAS); 612 if (atomic_inc_return(&n_rcu_scale_writer_finished) >= 613 nrealwriters) { 614 schedule_timeout_interruptible(10); 615 rcu_ftrace_dump(DUMP_ALL); 616 SCALEOUT_STRING("Test complete"); 617 t_rcu_scale_writer_finished = t; 618 if (gp_exp) { 619 b_rcu_gp_test_finished = 620 cur_ops->exp_completed() / 2; 621 } else { 622 b_rcu_gp_test_finished = 623 cur_ops->get_gp_seq(); 624 } 625 if (shutdown) { 626 smp_mb(); /* Assign before wake. */ 627 wake_up(&shutdown_wq); 628 } 629 } 630 } 631 if (done && !alldone && 632 atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters) 633 alldone = true; 634 if (done && !alldone && time_after(jiffies, jdone + HZ * 60)) { 635 static atomic_t dumped; 636 int i; 637 638 if (!atomic_xchg(&dumped, 1)) { 639 for (i = 0; i < nrealwriters; i++) { 640 if (writer_done[i]) 641 continue; 642 pr_info("%s: Task %ld flags writer %d:\n", __func__, me, i); 643 sched_show_task(writer_tasks[i]); 644 } 645 if (cur_ops->stats) 646 cur_ops->stats(); 647 } 648 } 649 if (!selfreport && time_after(jiffies, jdone + HZ * (70 + me))) { 650 pr_info("%s: Writer %ld self-report: started %d done %d/%d->%d i %d jdone %lu.\n", 651 __func__, me, started, done, writer_done[me], atomic_read(&n_rcu_scale_writer_finished), i, jiffies - jdone); 652 selfreport = true; 653 } 654 if (gp_succeeded && started && !alldone && i < MAX_MEAS - 1) 655 i++; 656 rcu_scale_wait_shutdown(); 657 } while (!torture_must_stop()); 658 if (gp_async && cur_ops->async) { 659 rcu_scale_free(wmbp); 660 cur_ops->gp_barrier(); 661 } 662 writer_n_durations[me] = i_max + 1; 663 torture_kthread_stopping("rcu_scale_writer"); 664 return 0; 665 } 666 667 static void 668 rcu_scale_print_module_parms(struct rcu_scale_ops *cur_ops, const char *tag) 669 { 670 pr_alert("%s" SCALE_FLAG 671 "--- %s: gp_async=%d gp_async_max=%d gp_exp=%d holdoff=%d minruntime=%d nreaders=%d nwriters=%d writer_holdoff=%d writer_holdoff_jiffies=%d verbose=%d shutdown=%d\n", 672 scale_type, tag, gp_async, gp_async_max, gp_exp, holdoff, minruntime, nrealreaders, nrealwriters, writer_holdoff, writer_holdoff_jiffies, verbose, shutdown); 673 } 674 675 /* 676 * Return the number if non-negative. If -1, the number of CPUs. 677 * If less than -1, that much less than the number of CPUs, but 678 * at least one. 679 */ 680 static int compute_real(int n) 681 { 682 int nr; 683 684 if (n >= 0) { 685 nr = n; 686 } else { 687 nr = num_online_cpus() + 1 + n; 688 if (nr <= 0) 689 nr = 1; 690 } 691 return nr; 692 } 693 694 /* 695 * kfree_rcu() scalability tests: Start a kfree_rcu() loop on all CPUs for number 696 * of iterations and measure total time and number of GP for all iterations to complete. 697 */ 698 699 torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu()."); 700 torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration."); 701 torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees."); 702 torture_param(bool, kfree_rcu_test_double, false, "Do we run a kfree_rcu() double-argument scale test?"); 703 torture_param(bool, kfree_rcu_test_single, false, "Do we run a kfree_rcu() single-argument scale test?"); 704 705 static struct task_struct **kfree_reader_tasks; 706 static int kfree_nrealthreads; 707 static atomic_t n_kfree_scale_thread_started; 708 static atomic_t n_kfree_scale_thread_ended; 709 static struct task_struct *kthread_tp; 710 static u64 kthread_stime; 711 712 struct kfree_obj { 713 char kfree_obj[8]; 714 struct rcu_head rh; 715 }; 716 717 /* Used if doing RCU-kfree'ing via call_rcu(). */ 718 static void kfree_call_rcu(struct rcu_head *rh) 719 { 720 struct kfree_obj *obj = container_of(rh, struct kfree_obj, rh); 721 722 kfree(obj); 723 } 724 725 static int 726 kfree_scale_thread(void *arg) 727 { 728 int i, loop = 0; 729 long me = (long)arg; 730 struct kfree_obj *alloc_ptr; 731 u64 start_time, end_time; 732 long long mem_begin, mem_during = 0; 733 bool kfree_rcu_test_both; 734 DEFINE_TORTURE_RANDOM(tr); 735 736 VERBOSE_SCALEOUT_STRING("kfree_scale_thread task started"); 737 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); 738 set_user_nice(current, MAX_NICE); 739 kfree_rcu_test_both = (kfree_rcu_test_single == kfree_rcu_test_double); 740 741 start_time = ktime_get_mono_fast_ns(); 742 743 if (atomic_inc_return(&n_kfree_scale_thread_started) >= kfree_nrealthreads) { 744 if (gp_exp) 745 b_rcu_gp_test_started = cur_ops->exp_completed() / 2; 746 else 747 b_rcu_gp_test_started = cur_ops->get_gp_seq(); 748 } 749 750 do { 751 if (!mem_during) { 752 mem_during = mem_begin = si_mem_available(); 753 } else if (loop % (kfree_loops / 4) == 0) { 754 mem_during = (mem_during + si_mem_available()) / 2; 755 } 756 757 for (i = 0; i < kfree_alloc_num; i++) { 758 alloc_ptr = kzalloc_objs(struct kfree_obj, kfree_mult, 759 GFP_KERNEL); 760 if (!alloc_ptr) 761 return -ENOMEM; 762 763 if (kfree_by_call_rcu) { 764 call_rcu(&(alloc_ptr->rh), kfree_call_rcu); 765 continue; 766 } 767 768 // By default kfree_rcu_test_single and kfree_rcu_test_double are 769 // initialized to false. If both have the same value (false or true) 770 // both are randomly tested, otherwise only the one with value true 771 // is tested. 772 if ((kfree_rcu_test_single && !kfree_rcu_test_double) || 773 (kfree_rcu_test_both && torture_random(&tr) & 0x800)) 774 kfree_rcu_mightsleep(alloc_ptr); 775 else 776 kfree_rcu(alloc_ptr, rh); 777 } 778 779 cond_resched(); 780 } while (!torture_must_stop() && ++loop < kfree_loops); 781 782 if (atomic_inc_return(&n_kfree_scale_thread_ended) >= kfree_nrealthreads) { 783 end_time = ktime_get_mono_fast_ns(); 784 785 if (gp_exp) 786 b_rcu_gp_test_finished = cur_ops->exp_completed() / 2; 787 else 788 b_rcu_gp_test_finished = cur_ops->get_gp_seq(); 789 790 pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n", 791 (unsigned long long)(end_time - start_time), kfree_loops, 792 rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started), 793 PAGES_TO_MB(mem_begin - mem_during)); 794 795 if (shutdown) { 796 smp_mb(); /* Assign before wake. */ 797 wake_up(&shutdown_wq); 798 } 799 } 800 801 torture_kthread_stopping("kfree_scale_thread"); 802 return 0; 803 } 804 805 static void 806 kfree_scale_cleanup(void) 807 { 808 int i; 809 810 if (torture_cleanup_begin()) 811 return; 812 813 if (kfree_reader_tasks) { 814 for (i = 0; i < kfree_nrealthreads; i++) 815 torture_stop_kthread(kfree_scale_thread, 816 kfree_reader_tasks[i]); 817 kfree(kfree_reader_tasks); 818 kfree_reader_tasks = NULL; 819 } 820 821 torture_cleanup_end(); 822 } 823 824 /* 825 * shutdown kthread. Just waits to be awakened, then shuts down system. 826 */ 827 static int 828 kfree_scale_shutdown(void *arg) 829 { 830 wait_event_idle(shutdown_wq, 831 atomic_read(&n_kfree_scale_thread_ended) >= kfree_nrealthreads); 832 833 smp_mb(); /* Wake before output. */ 834 835 kfree_scale_cleanup(); 836 kernel_power_off(); 837 return -EINVAL; 838 } 839 840 // Used if doing RCU-kfree'ing via call_rcu(). 841 static unsigned long jiffies_at_lazy_cb; 842 static struct rcu_head lazy_test1_rh; 843 static int rcu_lazy_test1_cb_called; 844 static void call_rcu_lazy_test1(struct rcu_head *rh) 845 { 846 jiffies_at_lazy_cb = jiffies; 847 WRITE_ONCE(rcu_lazy_test1_cb_called, 1); 848 } 849 850 static int __init 851 kfree_scale_init(void) 852 { 853 int firsterr = 0; 854 long i; 855 unsigned long jif_start; 856 unsigned long orig_jif; 857 858 pr_alert("%s" SCALE_FLAG 859 "--- kfree_rcu_test: kfree_mult=%d kfree_by_call_rcu=%d kfree_nthreads=%d kfree_alloc_num=%d kfree_loops=%d kfree_rcu_test_double=%d kfree_rcu_test_single=%d\n", 860 scale_type, kfree_mult, kfree_by_call_rcu, kfree_nthreads, kfree_alloc_num, kfree_loops, kfree_rcu_test_double, kfree_rcu_test_single); 861 862 // Also, do a quick self-test to ensure laziness is as much as 863 // expected. 864 if (kfree_by_call_rcu && !IS_ENABLED(CONFIG_RCU_LAZY)) { 865 pr_alert("CONFIG_RCU_LAZY is disabled, falling back to kfree_rcu() for delayed RCU kfree'ing\n"); 866 kfree_by_call_rcu = 0; 867 } 868 869 if (kfree_by_call_rcu) { 870 /* do a test to check the timeout. */ 871 orig_jif = rcu_get_jiffies_lazy_flush(); 872 873 rcu_set_jiffies_lazy_flush(2 * HZ); 874 rcu_barrier(); 875 876 jif_start = jiffies; 877 jiffies_at_lazy_cb = 0; 878 call_rcu(&lazy_test1_rh, call_rcu_lazy_test1); 879 880 smp_cond_load_relaxed(&rcu_lazy_test1_cb_called, VAL == 1); 881 882 rcu_set_jiffies_lazy_flush(orig_jif); 883 884 if (WARN_ON_ONCE(jiffies_at_lazy_cb - jif_start < 2 * HZ)) { 885 pr_alert("ERROR: call_rcu() CBs are not being lazy as expected!\n"); 886 firsterr = -1; 887 goto unwind; 888 } 889 890 if (WARN_ON_ONCE(jiffies_at_lazy_cb - jif_start > 3 * HZ)) { 891 pr_alert("ERROR: call_rcu() CBs are being too lazy!\n"); 892 firsterr = -1; 893 goto unwind; 894 } 895 } 896 897 kfree_nrealthreads = compute_real(kfree_nthreads); 898 /* Start up the kthreads. */ 899 if (shutdown) { 900 init_waitqueue_head(&shutdown_wq); 901 firsterr = torture_create_kthread(kfree_scale_shutdown, NULL, 902 shutdown_task); 903 if (torture_init_error(firsterr)) 904 goto unwind; 905 schedule_timeout_uninterruptible(1); 906 } 907 908 pr_alert("kfree object size=%zu, kfree_by_call_rcu=%d\n", 909 kfree_mult * sizeof(struct kfree_obj), 910 kfree_by_call_rcu); 911 912 kfree_reader_tasks = kzalloc_objs(kfree_reader_tasks[0], 913 kfree_nrealthreads, GFP_KERNEL); 914 if (kfree_reader_tasks == NULL) { 915 firsterr = -ENOMEM; 916 goto unwind; 917 } 918 919 for (i = 0; i < kfree_nrealthreads; i++) { 920 firsterr = torture_create_kthread(kfree_scale_thread, (void *)i, 921 kfree_reader_tasks[i]); 922 if (torture_init_error(firsterr)) 923 goto unwind; 924 } 925 926 while (atomic_read(&n_kfree_scale_thread_started) < kfree_nrealthreads) 927 schedule_timeout_uninterruptible(1); 928 929 torture_init_end(); 930 return 0; 931 932 unwind: 933 torture_init_end(); 934 kfree_scale_cleanup(); 935 return firsterr; 936 } 937 938 static void 939 rcu_scale_cleanup(void) 940 { 941 int i; 942 int j; 943 int ngps = 0; 944 u64 *wdp; 945 u64 *wdpp; 946 947 /* 948 * Would like warning at start, but everything is expedited 949 * during the mid-boot phase, so have to wait till the end. 950 */ 951 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp) 952 SCALEOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!"); 953 if (rcu_gp_is_normal() && gp_exp) 954 SCALEOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!"); 955 if (gp_exp && gp_async) 956 SCALEOUT_ERRSTRING("No expedited async GPs, so went with async!"); 957 958 // If built-in, just report all of the GP kthread's CPU time. 959 if (IS_BUILTIN(CONFIG_RCU_SCALE_TEST) && !kthread_tp && cur_ops->rso_gp_kthread) 960 kthread_tp = cur_ops->rso_gp_kthread(); 961 if (kthread_tp) { 962 u32 ns; 963 u64 us; 964 965 kthread_stime = kthread_tp->stime - kthread_stime; 966 us = div_u64_rem(kthread_stime, 1000, &ns); 967 pr_info("rcu_scale: Grace-period kthread CPU time: %llu.%03u us\n", us, ns); 968 show_rcu_gp_kthreads(); 969 } 970 if (kfree_rcu_test) { 971 kfree_scale_cleanup(); 972 return; 973 } 974 975 if (torture_cleanup_begin()) 976 return; 977 if (!cur_ops) { 978 torture_cleanup_end(); 979 return; 980 } 981 982 if (reader_tasks) { 983 for (i = 0; i < nrealreaders; i++) 984 torture_stop_kthread(rcu_scale_reader, 985 reader_tasks[i]); 986 kfree(reader_tasks); 987 reader_tasks = NULL; 988 } 989 990 if (writer_tasks) { 991 for (i = 0; i < nrealwriters; i++) { 992 torture_stop_kthread(rcu_scale_writer, 993 writer_tasks[i]); 994 if (!writer_n_durations) 995 continue; 996 j = writer_n_durations[i]; 997 pr_alert("%s%s writer %d gps: %d\n", 998 scale_type, SCALE_FLAG, i, j); 999 ngps += j; 1000 } 1001 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n", 1002 scale_type, SCALE_FLAG, 1003 t_rcu_scale_writer_started, t_rcu_scale_writer_finished, 1004 t_rcu_scale_writer_finished - 1005 t_rcu_scale_writer_started, 1006 ngps, 1007 rcuscale_seq_diff(b_rcu_gp_test_finished, 1008 b_rcu_gp_test_started)); 1009 for (i = 0; i < nrealwriters; i++) { 1010 if (!writer_durations) 1011 break; 1012 if (!writer_n_durations) 1013 continue; 1014 wdpp = writer_durations[i]; 1015 if (!wdpp) 1016 continue; 1017 for (j = 0; j < writer_n_durations[i]; j++) { 1018 wdp = &wdpp[j]; 1019 pr_alert("%s%s %4d writer-duration: %5d %llu\n", 1020 scale_type, SCALE_FLAG, 1021 i, j, *wdp); 1022 if (j % 100 == 0) 1023 schedule_timeout_uninterruptible(1); 1024 } 1025 kfree(writer_durations[i]); 1026 if (writer_freelists) { 1027 int ctr = 0; 1028 struct llist_node *llnp; 1029 struct writer_freelist *wflp = &writer_freelists[i]; 1030 1031 if (wflp->ws_mblocks) { 1032 llist_for_each(llnp, wflp->ws_lhg.first) 1033 ctr++; 1034 llist_for_each(llnp, wflp->ws_lhp.first) 1035 ctr++; 1036 WARN_ONCE(ctr != gp_async_max, 1037 "%s: ctr = %d gp_async_max = %d\n", 1038 __func__, ctr, gp_async_max); 1039 kfree(wflp->ws_mblocks); 1040 } 1041 } 1042 } 1043 kfree(writer_tasks); 1044 writer_tasks = NULL; 1045 kfree(writer_durations); 1046 writer_durations = NULL; 1047 kfree(writer_n_durations); 1048 writer_n_durations = NULL; 1049 kfree(writer_done); 1050 writer_done = NULL; 1051 kfree(writer_freelists); 1052 writer_freelists = NULL; 1053 } 1054 1055 /* Do torture-type-specific cleanup operations. */ 1056 if (cur_ops->cleanup != NULL) 1057 cur_ops->cleanup(); 1058 1059 torture_cleanup_end(); 1060 } 1061 1062 /* 1063 * RCU scalability shutdown kthread. Just waits to be awakened, then shuts 1064 * down system. 1065 */ 1066 static int 1067 rcu_scale_shutdown(void *arg) 1068 { 1069 wait_event_idle(shutdown_wq, atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters); 1070 smp_mb(); /* Wake before output. */ 1071 rcu_scale_cleanup(); 1072 kernel_power_off(); 1073 return -EINVAL; 1074 } 1075 1076 static int __init 1077 rcu_scale_init(void) 1078 { 1079 int firsterr = 0; 1080 long i; 1081 long j; 1082 static struct rcu_scale_ops *scale_ops[] = { 1083 &rcu_ops, &srcu_ops, &srcud_ops, TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS 1084 }; 1085 1086 if (!torture_init_begin(scale_type, verbose)) 1087 return -EBUSY; 1088 1089 /* Process args and announce that the scalability'er is on the job. */ 1090 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) { 1091 cur_ops = scale_ops[i]; 1092 if (strcmp(scale_type, cur_ops->name) == 0) 1093 break; 1094 } 1095 if (i == ARRAY_SIZE(scale_ops)) { 1096 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type); 1097 pr_alert("rcu-scale types:"); 1098 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) 1099 pr_cont(" %s", scale_ops[i]->name); 1100 pr_cont("\n"); 1101 firsterr = -EINVAL; 1102 cur_ops = NULL; 1103 goto unwind; 1104 } 1105 if (cur_ops->init) 1106 cur_ops->init(); 1107 1108 if (cur_ops->rso_gp_kthread) { 1109 kthread_tp = cur_ops->rso_gp_kthread(); 1110 if (kthread_tp) 1111 kthread_stime = kthread_tp->stime; 1112 } 1113 if (kfree_rcu_test) 1114 return kfree_scale_init(); 1115 1116 nrealwriters = compute_real(nwriters); 1117 nrealreaders = compute_real(nreaders); 1118 atomic_set(&n_rcu_scale_reader_started, 0); 1119 atomic_set(&n_rcu_scale_writer_started, 0); 1120 atomic_set(&n_rcu_scale_writer_finished, 0); 1121 rcu_scale_print_module_parms(cur_ops, "Start of test"); 1122 1123 /* Start up the kthreads. */ 1124 1125 if (shutdown) { 1126 init_waitqueue_head(&shutdown_wq); 1127 firsterr = torture_create_kthread(rcu_scale_shutdown, NULL, 1128 shutdown_task); 1129 if (torture_init_error(firsterr)) 1130 goto unwind; 1131 schedule_timeout_uninterruptible(1); 1132 } 1133 reader_tasks = kzalloc_objs(reader_tasks[0], nrealreaders, GFP_KERNEL); 1134 if (reader_tasks == NULL) { 1135 SCALEOUT_ERRSTRING("out of memory"); 1136 firsterr = -ENOMEM; 1137 goto unwind; 1138 } 1139 for (i = 0; i < nrealreaders; i++) { 1140 firsterr = torture_create_kthread(rcu_scale_reader, (void *)i, 1141 reader_tasks[i]); 1142 if (torture_init_error(firsterr)) 1143 goto unwind; 1144 } 1145 while (atomic_read(&n_rcu_scale_reader_started) < nrealreaders) 1146 schedule_timeout_uninterruptible(1); 1147 writer_tasks = kzalloc_objs(writer_tasks[0], nrealwriters, GFP_KERNEL); 1148 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations), GFP_KERNEL); 1149 writer_n_durations = kzalloc_objs(*writer_n_durations, nrealwriters, 1150 GFP_KERNEL); 1151 writer_done = kzalloc_objs(writer_done[0], nrealwriters, GFP_KERNEL); 1152 if (gp_async) { 1153 if (gp_async_max <= 0) { 1154 pr_warn("%s: gp_async_max = %d must be greater than zero.\n", 1155 __func__, gp_async_max); 1156 WARN_ON_ONCE(IS_BUILTIN(CONFIG_RCU_TORTURE_TEST)); 1157 firsterr = -EINVAL; 1158 goto unwind; 1159 } 1160 writer_freelists = kzalloc_objs(writer_freelists[0], 1161 nrealwriters, GFP_KERNEL); 1162 } 1163 if (!writer_tasks || !writer_durations || !writer_n_durations || !writer_done || 1164 (gp_async && !writer_freelists)) { 1165 SCALEOUT_ERRSTRING("out of memory"); 1166 firsterr = -ENOMEM; 1167 goto unwind; 1168 } 1169 for (i = 0; i < nrealwriters; i++) { 1170 writer_durations[i] = 1171 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]), 1172 GFP_KERNEL); 1173 if (!writer_durations[i]) { 1174 firsterr = -ENOMEM; 1175 goto unwind; 1176 } 1177 if (writer_freelists) { 1178 struct writer_freelist *wflp = &writer_freelists[i]; 1179 1180 init_llist_head(&wflp->ws_lhg); 1181 init_llist_head(&wflp->ws_lhp); 1182 wflp->ws_mblocks = kzalloc_objs(wflp->ws_mblocks[0], 1183 gp_async_max, 1184 GFP_KERNEL); 1185 if (!wflp->ws_mblocks) { 1186 firsterr = -ENOMEM; 1187 goto unwind; 1188 } 1189 for (j = 0; j < gp_async_max; j++) { 1190 struct writer_mblock *wmbp = &wflp->ws_mblocks[j]; 1191 1192 wmbp->wmb_wfl = wflp; 1193 llist_add(&wmbp->wmb_node, &wflp->ws_lhp); 1194 } 1195 } 1196 firsterr = torture_create_kthread(rcu_scale_writer, (void *)i, 1197 writer_tasks[i]); 1198 if (torture_init_error(firsterr)) 1199 goto unwind; 1200 } 1201 torture_init_end(); 1202 return 0; 1203 1204 unwind: 1205 torture_init_end(); 1206 rcu_scale_cleanup(); 1207 if (shutdown) { 1208 WARN_ON(!IS_MODULE(CONFIG_RCU_SCALE_TEST)); 1209 kernel_power_off(); 1210 } 1211 return firsterr; 1212 } 1213 1214 module_init(rcu_scale_init); 1215 module_exit(rcu_scale_cleanup); 1216