rcuscale.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <[email protected]>
  8. */
  9. #define pr_fmt(fmt) fmt
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/mm.h>
  14. #include <linux/module.h>
  15. #include <linux/kthread.h>
  16. #include <linux/err.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/smp.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/sched.h>
  22. #include <uapi/linux/sched/types.h>
  23. #include <linux/atomic.h>
  24. #include <linux/bitops.h>
  25. #include <linux/completion.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/percpu.h>
  28. #include <linux/notifier.h>
  29. #include <linux/reboot.h>
  30. #include <linux/freezer.h>
  31. #include <linux/cpu.h>
  32. #include <linux/delay.h>
  33. #include <linux/stat.h>
  34. #include <linux/srcu.h>
  35. #include <linux/slab.h>
  36. #include <asm/byteorder.h>
  37. #include <linux/torture.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/rcupdate_trace.h>
  40. #include "rcu.h"
  41. MODULE_LICENSE("GPL");
  42. MODULE_AUTHOR("Paul E. McKenney <[email protected]>");
  43. #define SCALE_FLAG "-scale:"
  44. #define SCALEOUT_STRING(s) \
  45. pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s)
  46. #define VERBOSE_SCALEOUT_STRING(s) \
  47. do { if (verbose) pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s); } while (0)
  48. #define SCALEOUT_ERRSTRING(s) \
  49. pr_alert("%s" SCALE_FLAG "!!! %s\n", scale_type, s)
  50. /*
  51. * The intended use cases for the nreaders and nwriters module parameters
  52. * are as follows:
  53. *
  54. * 1. Specify only the nr_cpus kernel boot parameter. This will
  55. * set both nreaders and nwriters to the value specified by
  56. * nr_cpus for a mixed reader/writer test.
  57. *
  58. * 2. Specify the nr_cpus kernel boot parameter, but set
  59. * rcuscale.nreaders to zero. This will set nwriters to the
  60. * value specified by nr_cpus for an update-only test.
  61. *
  62. * 3. Specify the nr_cpus kernel boot parameter, but set
  63. * rcuscale.nwriters to zero. This will set nreaders to the
  64. * value specified by nr_cpus for a read-only test.
  65. *
  66. * Various other use cases may of course be specified.
  67. *
  68. * Note that this test's readers are intended only as a test load for
  69. * the writers. The reader scalability statistics will be overly
  70. * pessimistic due to the per-critical-section interrupt disabling,
  71. * test-end checks, and the pair of calls through pointers.
  72. */
  73. #ifdef MODULE
  74. # define RCUSCALE_SHUTDOWN 0
  75. #else
  76. # define RCUSCALE_SHUTDOWN 1
  77. #endif
  78. torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
  79. torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
  80. torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
  81. torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
  82. torture_param(int, nreaders, -1, "Number of RCU reader threads");
  83. torture_param(int, nwriters, -1, "Number of RCU updater threads");
  84. torture_param(bool, shutdown, RCUSCALE_SHUTDOWN,
  85. "Shutdown at end of scalability tests.");
  86. torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
  87. torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
  88. torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() scale test?");
  89. torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate.");
  90. static char *scale_type = "rcu";
  91. module_param(scale_type, charp, 0444);
  92. MODULE_PARM_DESC(scale_type, "Type of RCU to scalability-test (rcu, srcu, ...)");
  93. static int nrealreaders;
  94. static int nrealwriters;
  95. static struct task_struct **writer_tasks;
  96. static struct task_struct **reader_tasks;
  97. static struct task_struct *shutdown_task;
  98. static u64 **writer_durations;
  99. static int *writer_n_durations;
  100. static atomic_t n_rcu_scale_reader_started;
  101. static atomic_t n_rcu_scale_writer_started;
  102. static atomic_t n_rcu_scale_writer_finished;
  103. static wait_queue_head_t shutdown_wq;
  104. static u64 t_rcu_scale_writer_started;
  105. static u64 t_rcu_scale_writer_finished;
  106. static unsigned long b_rcu_gp_test_started;
  107. static unsigned long b_rcu_gp_test_finished;
  108. static DEFINE_PER_CPU(atomic_t, n_async_inflight);
  109. #define MAX_MEAS 10000
  110. #define MIN_MEAS 100
  111. /*
  112. * Operations vector for selecting different types of tests.
  113. */
  114. struct rcu_scale_ops {
  115. int ptype;
  116. void (*init)(void);
  117. void (*cleanup)(void);
  118. int (*readlock)(void);
  119. void (*readunlock)(int idx);
  120. unsigned long (*get_gp_seq)(void);
  121. unsigned long (*gp_diff)(unsigned long new, unsigned long old);
  122. unsigned long (*exp_completed)(void);
  123. void (*async)(struct rcu_head *head, rcu_callback_t func);
  124. void (*gp_barrier)(void);
  125. void (*sync)(void);
  126. void (*exp_sync)(void);
  127. const char *name;
  128. };
  129. static struct rcu_scale_ops *cur_ops;
  130. /*
  131. * Definitions for rcu scalability testing.
  132. */
  133. static int rcu_scale_read_lock(void) __acquires(RCU)
  134. {
  135. rcu_read_lock();
  136. return 0;
  137. }
  138. static void rcu_scale_read_unlock(int idx) __releases(RCU)
  139. {
  140. rcu_read_unlock();
  141. }
  142. static unsigned long __maybe_unused rcu_no_completed(void)
  143. {
  144. return 0;
  145. }
  146. static void rcu_sync_scale_init(void)
  147. {
  148. }
  149. static struct rcu_scale_ops rcu_ops = {
  150. .ptype = RCU_FLAVOR,
  151. .init = rcu_sync_scale_init,
  152. .readlock = rcu_scale_read_lock,
  153. .readunlock = rcu_scale_read_unlock,
  154. .get_gp_seq = rcu_get_gp_seq,
  155. .gp_diff = rcu_seq_diff,
  156. .exp_completed = rcu_exp_batches_completed,
  157. .async = call_rcu_hurry,
  158. .gp_barrier = rcu_barrier,
  159. .sync = synchronize_rcu,
  160. .exp_sync = synchronize_rcu_expedited,
  161. .name = "rcu"
  162. };
  163. /*
  164. * Definitions for srcu scalability testing.
  165. */
  166. DEFINE_STATIC_SRCU(srcu_ctl_scale);
  167. static struct srcu_struct *srcu_ctlp = &srcu_ctl_scale;
  168. static int srcu_scale_read_lock(void) __acquires(srcu_ctlp)
  169. {
  170. return srcu_read_lock(srcu_ctlp);
  171. }
  172. static void srcu_scale_read_unlock(int idx) __releases(srcu_ctlp)
  173. {
  174. srcu_read_unlock(srcu_ctlp, idx);
  175. }
  176. static unsigned long srcu_scale_completed(void)
  177. {
  178. return srcu_batches_completed(srcu_ctlp);
  179. }
  180. static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
  181. {
  182. call_srcu(srcu_ctlp, head, func);
  183. }
  184. static void srcu_rcu_barrier(void)
  185. {
  186. srcu_barrier(srcu_ctlp);
  187. }
  188. static void srcu_scale_synchronize(void)
  189. {
  190. synchronize_srcu(srcu_ctlp);
  191. }
  192. static void srcu_scale_synchronize_expedited(void)
  193. {
  194. synchronize_srcu_expedited(srcu_ctlp);
  195. }
  196. static struct rcu_scale_ops srcu_ops = {
  197. .ptype = SRCU_FLAVOR,
  198. .init = rcu_sync_scale_init,
  199. .readlock = srcu_scale_read_lock,
  200. .readunlock = srcu_scale_read_unlock,
  201. .get_gp_seq = srcu_scale_completed,
  202. .gp_diff = rcu_seq_diff,
  203. .exp_completed = srcu_scale_completed,
  204. .async = srcu_call_rcu,
  205. .gp_barrier = srcu_rcu_barrier,
  206. .sync = srcu_scale_synchronize,
  207. .exp_sync = srcu_scale_synchronize_expedited,
  208. .name = "srcu"
  209. };
  210. static struct srcu_struct srcud;
  211. static void srcu_sync_scale_init(void)
  212. {
  213. srcu_ctlp = &srcud;
  214. init_srcu_struct(srcu_ctlp);
  215. }
  216. static void srcu_sync_scale_cleanup(void)
  217. {
  218. cleanup_srcu_struct(srcu_ctlp);
  219. }
  220. static struct rcu_scale_ops srcud_ops = {
  221. .ptype = SRCU_FLAVOR,
  222. .init = srcu_sync_scale_init,
  223. .cleanup = srcu_sync_scale_cleanup,
  224. .readlock = srcu_scale_read_lock,
  225. .readunlock = srcu_scale_read_unlock,
  226. .get_gp_seq = srcu_scale_completed,
  227. .gp_diff = rcu_seq_diff,
  228. .exp_completed = srcu_scale_completed,
  229. .async = srcu_call_rcu,
  230. .gp_barrier = srcu_rcu_barrier,
  231. .sync = srcu_scale_synchronize,
  232. .exp_sync = srcu_scale_synchronize_expedited,
  233. .name = "srcud"
  234. };
  235. #ifdef CONFIG_TASKS_RCU
  236. /*
  237. * Definitions for RCU-tasks scalability testing.
  238. */
  239. static int tasks_scale_read_lock(void)
  240. {
  241. return 0;
  242. }
  243. static void tasks_scale_read_unlock(int idx)
  244. {
  245. }
  246. static struct rcu_scale_ops tasks_ops = {
  247. .ptype = RCU_TASKS_FLAVOR,
  248. .init = rcu_sync_scale_init,
  249. .readlock = tasks_scale_read_lock,
  250. .readunlock = tasks_scale_read_unlock,
  251. .get_gp_seq = rcu_no_completed,
  252. .gp_diff = rcu_seq_diff,
  253. .async = call_rcu_tasks,
  254. .gp_barrier = rcu_barrier_tasks,
  255. .sync = synchronize_rcu_tasks,
  256. .exp_sync = synchronize_rcu_tasks,
  257. .name = "tasks"
  258. };
  259. #define TASKS_OPS &tasks_ops,
  260. #else // #ifdef CONFIG_TASKS_RCU
  261. #define TASKS_OPS
  262. #endif // #else // #ifdef CONFIG_TASKS_RCU
  263. #ifdef CONFIG_TASKS_TRACE_RCU
  264. /*
  265. * Definitions for RCU-tasks-trace scalability testing.
  266. */
  267. static int tasks_trace_scale_read_lock(void)
  268. {
  269. rcu_read_lock_trace();
  270. return 0;
  271. }
  272. static void tasks_trace_scale_read_unlock(int idx)
  273. {
  274. rcu_read_unlock_trace();
  275. }
  276. static struct rcu_scale_ops tasks_tracing_ops = {
  277. .ptype = RCU_TASKS_FLAVOR,
  278. .init = rcu_sync_scale_init,
  279. .readlock = tasks_trace_scale_read_lock,
  280. .readunlock = tasks_trace_scale_read_unlock,
  281. .get_gp_seq = rcu_no_completed,
  282. .gp_diff = rcu_seq_diff,
  283. .async = call_rcu_tasks_trace,
  284. .gp_barrier = rcu_barrier_tasks_trace,
  285. .sync = synchronize_rcu_tasks_trace,
  286. .exp_sync = synchronize_rcu_tasks_trace,
  287. .name = "tasks-tracing"
  288. };
  289. #define TASKS_TRACING_OPS &tasks_tracing_ops,
  290. #else // #ifdef CONFIG_TASKS_TRACE_RCU
  291. #define TASKS_TRACING_OPS
  292. #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
  293. static unsigned long rcuscale_seq_diff(unsigned long new, unsigned long old)
  294. {
  295. if (!cur_ops->gp_diff)
  296. return new - old;
  297. return cur_ops->gp_diff(new, old);
  298. }
  299. /*
  300. * If scalability tests complete, wait for shutdown to commence.
  301. */
  302. static void rcu_scale_wait_shutdown(void)
  303. {
  304. cond_resched_tasks_rcu_qs();
  305. if (atomic_read(&n_rcu_scale_writer_finished) < nrealwriters)
  306. return;
  307. while (!torture_must_stop())
  308. schedule_timeout_uninterruptible(1);
  309. }
  310. /*
  311. * RCU scalability reader kthread. Repeatedly does empty RCU read-side
  312. * critical section, minimizing update-side interference. However, the
  313. * point of this test is not to evaluate reader scalability, but instead
  314. * to serve as a test load for update-side scalability testing.
  315. */
  316. static int
  317. rcu_scale_reader(void *arg)
  318. {
  319. unsigned long flags;
  320. int idx;
  321. long me = (long)arg;
  322. VERBOSE_SCALEOUT_STRING("rcu_scale_reader task started");
  323. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  324. set_user_nice(current, MAX_NICE);
  325. atomic_inc(&n_rcu_scale_reader_started);
  326. do {
  327. local_irq_save(flags);
  328. idx = cur_ops->readlock();
  329. cur_ops->readunlock(idx);
  330. local_irq_restore(flags);
  331. rcu_scale_wait_shutdown();
  332. } while (!torture_must_stop());
  333. torture_kthread_stopping("rcu_scale_reader");
  334. return 0;
  335. }
  336. /*
  337. * Callback function for asynchronous grace periods from rcu_scale_writer().
  338. */
  339. static void rcu_scale_async_cb(struct rcu_head *rhp)
  340. {
  341. atomic_dec(this_cpu_ptr(&n_async_inflight));
  342. kfree(rhp);
  343. }
  344. /*
  345. * RCU scale writer kthread. Repeatedly does a grace period.
  346. */
  347. static int
  348. rcu_scale_writer(void *arg)
  349. {
  350. int i = 0;
  351. int i_max;
  352. long me = (long)arg;
  353. struct rcu_head *rhp = NULL;
  354. bool started = false, done = false, alldone = false;
  355. u64 t;
  356. u64 *wdp;
  357. u64 *wdpp = writer_durations[me];
  358. VERBOSE_SCALEOUT_STRING("rcu_scale_writer task started");
  359. WARN_ON(!wdpp);
  360. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  361. current->flags |= PF_NO_SETAFFINITY;
  362. sched_set_fifo_low(current);
  363. if (holdoff)
  364. schedule_timeout_idle(holdoff * HZ);
  365. /*
  366. * Wait until rcu_end_inkernel_boot() is called for normal GP tests
  367. * so that RCU is not always expedited for normal GP tests.
  368. * The system_state test is approximate, but works well in practice.
  369. */
  370. while (!gp_exp && system_state != SYSTEM_RUNNING)
  371. schedule_timeout_uninterruptible(1);
  372. t = ktime_get_mono_fast_ns();
  373. if (atomic_inc_return(&n_rcu_scale_writer_started) >= nrealwriters) {
  374. t_rcu_scale_writer_started = t;
  375. if (gp_exp) {
  376. b_rcu_gp_test_started =
  377. cur_ops->exp_completed() / 2;
  378. } else {
  379. b_rcu_gp_test_started = cur_ops->get_gp_seq();
  380. }
  381. }
  382. do {
  383. if (writer_holdoff)
  384. udelay(writer_holdoff);
  385. wdp = &wdpp[i];
  386. *wdp = ktime_get_mono_fast_ns();
  387. if (gp_async) {
  388. retry:
  389. if (!rhp)
  390. rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
  391. if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
  392. atomic_inc(this_cpu_ptr(&n_async_inflight));
  393. cur_ops->async(rhp, rcu_scale_async_cb);
  394. rhp = NULL;
  395. } else if (!kthread_should_stop()) {
  396. cur_ops->gp_barrier();
  397. goto retry;
  398. } else {
  399. kfree(rhp); /* Because we are stopping. */
  400. }
  401. } else if (gp_exp) {
  402. cur_ops->exp_sync();
  403. } else {
  404. cur_ops->sync();
  405. }
  406. t = ktime_get_mono_fast_ns();
  407. *wdp = t - *wdp;
  408. i_max = i;
  409. if (!started &&
  410. atomic_read(&n_rcu_scale_writer_started) >= nrealwriters)
  411. started = true;
  412. if (!done && i >= MIN_MEAS) {
  413. done = true;
  414. sched_set_normal(current, 0);
  415. pr_alert("%s%s rcu_scale_writer %ld has %d measurements\n",
  416. scale_type, SCALE_FLAG, me, MIN_MEAS);
  417. if (atomic_inc_return(&n_rcu_scale_writer_finished) >=
  418. nrealwriters) {
  419. schedule_timeout_interruptible(10);
  420. rcu_ftrace_dump(DUMP_ALL);
  421. SCALEOUT_STRING("Test complete");
  422. t_rcu_scale_writer_finished = t;
  423. if (gp_exp) {
  424. b_rcu_gp_test_finished =
  425. cur_ops->exp_completed() / 2;
  426. } else {
  427. b_rcu_gp_test_finished =
  428. cur_ops->get_gp_seq();
  429. }
  430. if (shutdown) {
  431. smp_mb(); /* Assign before wake. */
  432. wake_up(&shutdown_wq);
  433. }
  434. }
  435. }
  436. if (done && !alldone &&
  437. atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters)
  438. alldone = true;
  439. if (started && !alldone && i < MAX_MEAS - 1)
  440. i++;
  441. rcu_scale_wait_shutdown();
  442. } while (!torture_must_stop());
  443. if (gp_async) {
  444. cur_ops->gp_barrier();
  445. }
  446. writer_n_durations[me] = i_max + 1;
  447. torture_kthread_stopping("rcu_scale_writer");
  448. return 0;
  449. }
  450. static void
  451. rcu_scale_print_module_parms(struct rcu_scale_ops *cur_ops, const char *tag)
  452. {
  453. pr_alert("%s" SCALE_FLAG
  454. "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
  455. scale_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
  456. }
  457. /*
  458. * Return the number if non-negative. If -1, the number of CPUs.
  459. * If less than -1, that much less than the number of CPUs, but
  460. * at least one.
  461. */
  462. static int compute_real(int n)
  463. {
  464. int nr;
  465. if (n >= 0) {
  466. nr = n;
  467. } else {
  468. nr = num_online_cpus() + 1 + n;
  469. if (nr <= 0)
  470. nr = 1;
  471. }
  472. return nr;
  473. }
  474. /*
  475. * kfree_rcu() scalability tests: Start a kfree_rcu() loop on all CPUs for number
  476. * of iterations and measure total time and number of GP for all iterations to complete.
  477. */
  478. torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu().");
  479. torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration.");
  480. torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees.");
  481. torture_param(bool, kfree_rcu_test_double, false, "Do we run a kfree_rcu() double-argument scale test?");
  482. torture_param(bool, kfree_rcu_test_single, false, "Do we run a kfree_rcu() single-argument scale test?");
  483. static struct task_struct **kfree_reader_tasks;
  484. static int kfree_nrealthreads;
  485. static atomic_t n_kfree_scale_thread_started;
  486. static atomic_t n_kfree_scale_thread_ended;
  487. struct kfree_obj {
  488. char kfree_obj[8];
  489. struct rcu_head rh;
  490. };
  491. static int
  492. kfree_scale_thread(void *arg)
  493. {
  494. int i, loop = 0;
  495. long me = (long)arg;
  496. struct kfree_obj *alloc_ptr;
  497. u64 start_time, end_time;
  498. long long mem_begin, mem_during = 0;
  499. bool kfree_rcu_test_both;
  500. DEFINE_TORTURE_RANDOM(tr);
  501. VERBOSE_SCALEOUT_STRING("kfree_scale_thread task started");
  502. set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
  503. set_user_nice(current, MAX_NICE);
  504. kfree_rcu_test_both = (kfree_rcu_test_single == kfree_rcu_test_double);
  505. start_time = ktime_get_mono_fast_ns();
  506. if (atomic_inc_return(&n_kfree_scale_thread_started) >= kfree_nrealthreads) {
  507. if (gp_exp)
  508. b_rcu_gp_test_started = cur_ops->exp_completed() / 2;
  509. else
  510. b_rcu_gp_test_started = cur_ops->get_gp_seq();
  511. }
  512. do {
  513. if (!mem_during) {
  514. mem_during = mem_begin = si_mem_available();
  515. } else if (loop % (kfree_loops / 4) == 0) {
  516. mem_during = (mem_during + si_mem_available()) / 2;
  517. }
  518. for (i = 0; i < kfree_alloc_num; i++) {
  519. alloc_ptr = kmalloc(kfree_mult * sizeof(struct kfree_obj), GFP_KERNEL);
  520. if (!alloc_ptr)
  521. return -ENOMEM;
  522. // By default kfree_rcu_test_single and kfree_rcu_test_double are
  523. // initialized to false. If both have the same value (false or true)
  524. // both are randomly tested, otherwise only the one with value true
  525. // is tested.
  526. if ((kfree_rcu_test_single && !kfree_rcu_test_double) ||
  527. (kfree_rcu_test_both && torture_random(&tr) & 0x800))
  528. kfree_rcu(alloc_ptr);
  529. else
  530. kfree_rcu(alloc_ptr, rh);
  531. }
  532. cond_resched();
  533. } while (!torture_must_stop() && ++loop < kfree_loops);
  534. if (atomic_inc_return(&n_kfree_scale_thread_ended) >= kfree_nrealthreads) {
  535. end_time = ktime_get_mono_fast_ns();
  536. if (gp_exp)
  537. b_rcu_gp_test_finished = cur_ops->exp_completed() / 2;
  538. else
  539. b_rcu_gp_test_finished = cur_ops->get_gp_seq();
  540. pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
  541. (unsigned long long)(end_time - start_time), kfree_loops,
  542. rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
  543. (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
  544. if (shutdown) {
  545. smp_mb(); /* Assign before wake. */
  546. wake_up(&shutdown_wq);
  547. }
  548. }
  549. torture_kthread_stopping("kfree_scale_thread");
  550. return 0;
  551. }
  552. static void
  553. kfree_scale_cleanup(void)
  554. {
  555. int i;
  556. if (torture_cleanup_begin())
  557. return;
  558. if (kfree_reader_tasks) {
  559. for (i = 0; i < kfree_nrealthreads; i++)
  560. torture_stop_kthread(kfree_scale_thread,
  561. kfree_reader_tasks[i]);
  562. kfree(kfree_reader_tasks);
  563. }
  564. torture_cleanup_end();
  565. }
  566. /*
  567. * shutdown kthread. Just waits to be awakened, then shuts down system.
  568. */
  569. static int
  570. kfree_scale_shutdown(void *arg)
  571. {
  572. wait_event_idle(shutdown_wq,
  573. atomic_read(&n_kfree_scale_thread_ended) >= kfree_nrealthreads);
  574. smp_mb(); /* Wake before output. */
  575. kfree_scale_cleanup();
  576. kernel_power_off();
  577. return -EINVAL;
  578. }
  579. static int __init
  580. kfree_scale_init(void)
  581. {
  582. long i;
  583. int firsterr = 0;
  584. kfree_nrealthreads = compute_real(kfree_nthreads);
  585. /* Start up the kthreads. */
  586. if (shutdown) {
  587. init_waitqueue_head(&shutdown_wq);
  588. firsterr = torture_create_kthread(kfree_scale_shutdown, NULL,
  589. shutdown_task);
  590. if (torture_init_error(firsterr))
  591. goto unwind;
  592. schedule_timeout_uninterruptible(1);
  593. }
  594. pr_alert("kfree object size=%zu\n", kfree_mult * sizeof(struct kfree_obj));
  595. kfree_reader_tasks = kcalloc(kfree_nrealthreads, sizeof(kfree_reader_tasks[0]),
  596. GFP_KERNEL);
  597. if (kfree_reader_tasks == NULL) {
  598. firsterr = -ENOMEM;
  599. goto unwind;
  600. }
  601. for (i = 0; i < kfree_nrealthreads; i++) {
  602. firsterr = torture_create_kthread(kfree_scale_thread, (void *)i,
  603. kfree_reader_tasks[i]);
  604. if (torture_init_error(firsterr))
  605. goto unwind;
  606. }
  607. while (atomic_read(&n_kfree_scale_thread_started) < kfree_nrealthreads)
  608. schedule_timeout_uninterruptible(1);
  609. torture_init_end();
  610. return 0;
  611. unwind:
  612. torture_init_end();
  613. kfree_scale_cleanup();
  614. return firsterr;
  615. }
  616. static void
  617. rcu_scale_cleanup(void)
  618. {
  619. int i;
  620. int j;
  621. int ngps = 0;
  622. u64 *wdp;
  623. u64 *wdpp;
  624. /*
  625. * Would like warning at start, but everything is expedited
  626. * during the mid-boot phase, so have to wait till the end.
  627. */
  628. if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
  629. SCALEOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
  630. if (rcu_gp_is_normal() && gp_exp)
  631. SCALEOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
  632. if (gp_exp && gp_async)
  633. SCALEOUT_ERRSTRING("No expedited async GPs, so went with async!");
  634. if (kfree_rcu_test) {
  635. kfree_scale_cleanup();
  636. return;
  637. }
  638. if (torture_cleanup_begin())
  639. return;
  640. if (!cur_ops) {
  641. torture_cleanup_end();
  642. return;
  643. }
  644. if (reader_tasks) {
  645. for (i = 0; i < nrealreaders; i++)
  646. torture_stop_kthread(rcu_scale_reader,
  647. reader_tasks[i]);
  648. kfree(reader_tasks);
  649. }
  650. if (writer_tasks) {
  651. for (i = 0; i < nrealwriters; i++) {
  652. torture_stop_kthread(rcu_scale_writer,
  653. writer_tasks[i]);
  654. if (!writer_n_durations)
  655. continue;
  656. j = writer_n_durations[i];
  657. pr_alert("%s%s writer %d gps: %d\n",
  658. scale_type, SCALE_FLAG, i, j);
  659. ngps += j;
  660. }
  661. pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
  662. scale_type, SCALE_FLAG,
  663. t_rcu_scale_writer_started, t_rcu_scale_writer_finished,
  664. t_rcu_scale_writer_finished -
  665. t_rcu_scale_writer_started,
  666. ngps,
  667. rcuscale_seq_diff(b_rcu_gp_test_finished,
  668. b_rcu_gp_test_started));
  669. for (i = 0; i < nrealwriters; i++) {
  670. if (!writer_durations)
  671. break;
  672. if (!writer_n_durations)
  673. continue;
  674. wdpp = writer_durations[i];
  675. if (!wdpp)
  676. continue;
  677. for (j = 0; j < writer_n_durations[i]; j++) {
  678. wdp = &wdpp[j];
  679. pr_alert("%s%s %4d writer-duration: %5d %llu\n",
  680. scale_type, SCALE_FLAG,
  681. i, j, *wdp);
  682. if (j % 100 == 0)
  683. schedule_timeout_uninterruptible(1);
  684. }
  685. kfree(writer_durations[i]);
  686. }
  687. kfree(writer_tasks);
  688. kfree(writer_durations);
  689. kfree(writer_n_durations);
  690. }
  691. /* Do torture-type-specific cleanup operations. */
  692. if (cur_ops->cleanup != NULL)
  693. cur_ops->cleanup();
  694. torture_cleanup_end();
  695. }
  696. /*
  697. * RCU scalability shutdown kthread. Just waits to be awakened, then shuts
  698. * down system.
  699. */
  700. static int
  701. rcu_scale_shutdown(void *arg)
  702. {
  703. wait_event_idle(shutdown_wq, atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters);
  704. smp_mb(); /* Wake before output. */
  705. rcu_scale_cleanup();
  706. kernel_power_off();
  707. return -EINVAL;
  708. }
  709. static int __init
  710. rcu_scale_init(void)
  711. {
  712. long i;
  713. int firsterr = 0;
  714. static struct rcu_scale_ops *scale_ops[] = {
  715. &rcu_ops, &srcu_ops, &srcud_ops, TASKS_OPS TASKS_TRACING_OPS
  716. };
  717. if (!torture_init_begin(scale_type, verbose))
  718. return -EBUSY;
  719. /* Process args and announce that the scalability'er is on the job. */
  720. for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
  721. cur_ops = scale_ops[i];
  722. if (strcmp(scale_type, cur_ops->name) == 0)
  723. break;
  724. }
  725. if (i == ARRAY_SIZE(scale_ops)) {
  726. pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
  727. pr_alert("rcu-scale types:");
  728. for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
  729. pr_cont(" %s", scale_ops[i]->name);
  730. pr_cont("\n");
  731. firsterr = -EINVAL;
  732. cur_ops = NULL;
  733. goto unwind;
  734. }
  735. if (cur_ops->init)
  736. cur_ops->init();
  737. if (kfree_rcu_test)
  738. return kfree_scale_init();
  739. nrealwriters = compute_real(nwriters);
  740. nrealreaders = compute_real(nreaders);
  741. atomic_set(&n_rcu_scale_reader_started, 0);
  742. atomic_set(&n_rcu_scale_writer_started, 0);
  743. atomic_set(&n_rcu_scale_writer_finished, 0);
  744. rcu_scale_print_module_parms(cur_ops, "Start of test");
  745. /* Start up the kthreads. */
  746. if (shutdown) {
  747. init_waitqueue_head(&shutdown_wq);
  748. firsterr = torture_create_kthread(rcu_scale_shutdown, NULL,
  749. shutdown_task);
  750. if (torture_init_error(firsterr))
  751. goto unwind;
  752. schedule_timeout_uninterruptible(1);
  753. }
  754. reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
  755. GFP_KERNEL);
  756. if (reader_tasks == NULL) {
  757. SCALEOUT_ERRSTRING("out of memory");
  758. firsterr = -ENOMEM;
  759. goto unwind;
  760. }
  761. for (i = 0; i < nrealreaders; i++) {
  762. firsterr = torture_create_kthread(rcu_scale_reader, (void *)i,
  763. reader_tasks[i]);
  764. if (torture_init_error(firsterr))
  765. goto unwind;
  766. }
  767. while (atomic_read(&n_rcu_scale_reader_started) < nrealreaders)
  768. schedule_timeout_uninterruptible(1);
  769. writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
  770. GFP_KERNEL);
  771. writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
  772. GFP_KERNEL);
  773. writer_n_durations =
  774. kcalloc(nrealwriters, sizeof(*writer_n_durations),
  775. GFP_KERNEL);
  776. if (!writer_tasks || !writer_durations || !writer_n_durations) {
  777. SCALEOUT_ERRSTRING("out of memory");
  778. firsterr = -ENOMEM;
  779. goto unwind;
  780. }
  781. for (i = 0; i < nrealwriters; i++) {
  782. writer_durations[i] =
  783. kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
  784. GFP_KERNEL);
  785. if (!writer_durations[i]) {
  786. firsterr = -ENOMEM;
  787. goto unwind;
  788. }
  789. firsterr = torture_create_kthread(rcu_scale_writer, (void *)i,
  790. writer_tasks[i]);
  791. if (torture_init_error(firsterr))
  792. goto unwind;
  793. }
  794. torture_init_end();
  795. return 0;
  796. unwind:
  797. torture_init_end();
  798. rcu_scale_cleanup();
  799. if (shutdown) {
  800. WARN_ON(!IS_MODULE(CONFIG_RCU_SCALE_TEST));
  801. kernel_power_off();
  802. }
  803. return firsterr;
  804. }
  805. module_init(rcu_scale_init);
  806. module_exit(rcu_scale_cleanup);