rcuscale.c 32 KB

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