scftorture.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Torture test for smp_call_function() and friends.
  4. //
  5. // Copyright (C) Facebook, 2020.
  6. //
  7. // Author: Paul E. McKenney <[email protected]>
  8. #define pr_fmt(fmt) fmt
  9. #include <linux/atomic.h>
  10. #include <linux/bitops.h>
  11. #include <linux/completion.h>
  12. #include <linux/cpu.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kthread.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/notifier.h>
  23. #include <linux/percpu.h>
  24. #include <linux/rcupdate.h>
  25. #include <linux/rcupdate_trace.h>
  26. #include <linux/reboot.h>
  27. #include <linux/sched.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/smp.h>
  30. #include <linux/stat.h>
  31. #include <linux/srcu.h>
  32. #include <linux/slab.h>
  33. #include <linux/torture.h>
  34. #include <linux/types.h>
  35. #define SCFTORT_STRING "scftorture"
  36. #define SCFTORT_FLAG SCFTORT_STRING ": "
  37. #define VERBOSE_SCFTORTOUT(s, x...) \
  38. do { if (verbose) pr_alert(SCFTORT_FLAG s "\n", ## x); } while (0)
  39. #define SCFTORTOUT_ERRSTRING(s, x...) pr_alert(SCFTORT_FLAG "!!! " s "\n", ## x)
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Paul E. McKenney <[email protected]>");
  42. // Wait until there are multiple CPUs before starting test.
  43. torture_param(int, holdoff, IS_BUILTIN(CONFIG_SCF_TORTURE_TEST) ? 10 : 0,
  44. "Holdoff time before test start (s)");
  45. torture_param(int, longwait, 0, "Include ridiculously long waits? (seconds)");
  46. torture_param(int, nthreads, -1, "# threads, defaults to -1 for all CPUs.");
  47. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  48. torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (s), 0=disable");
  49. torture_param(int, shutdown_secs, 0, "Shutdown time (ms), <= zero to disable.");
  50. torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s.");
  51. torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
  52. torture_param(bool, use_cpus_read_lock, 0, "Use cpus_read_lock() to exclude CPU hotplug.");
  53. torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
  54. torture_param(int, weight_resched, -1, "Testing weight for resched_cpu() operations.");
  55. torture_param(int, weight_single, -1, "Testing weight for single-CPU no-wait operations.");
  56. torture_param(int, weight_single_rpc, -1, "Testing weight for single-CPU RPC operations.");
  57. torture_param(int, weight_single_wait, -1, "Testing weight for single-CPU operations.");
  58. torture_param(int, weight_many, -1, "Testing weight for multi-CPU no-wait operations.");
  59. torture_param(int, weight_many_wait, -1, "Testing weight for multi-CPU operations.");
  60. torture_param(int, weight_all, -1, "Testing weight for all-CPU no-wait operations.");
  61. torture_param(int, weight_all_wait, -1, "Testing weight for all-CPU operations.");
  62. char *torture_type = "";
  63. #ifdef MODULE
  64. # define SCFTORT_SHUTDOWN 0
  65. #else
  66. # define SCFTORT_SHUTDOWN 1
  67. #endif
  68. torture_param(bool, shutdown, SCFTORT_SHUTDOWN, "Shutdown at end of torture test.");
  69. struct scf_statistics {
  70. struct task_struct *task;
  71. int cpu;
  72. long long n_resched;
  73. long long n_single;
  74. long long n_single_ofl;
  75. long long n_single_rpc;
  76. long long n_single_rpc_ofl;
  77. long long n_single_wait;
  78. long long n_single_wait_ofl;
  79. long long n_many;
  80. long long n_many_wait;
  81. long long n_all;
  82. long long n_all_wait;
  83. };
  84. static struct scf_statistics *scf_stats_p;
  85. static struct task_struct *scf_torture_stats_task;
  86. static DEFINE_PER_CPU(long long, scf_invoked_count);
  87. // Data for random primitive selection
  88. #define SCF_PRIM_RESCHED 0
  89. #define SCF_PRIM_SINGLE 1
  90. #define SCF_PRIM_SINGLE_RPC 2
  91. #define SCF_PRIM_MANY 3
  92. #define SCF_PRIM_ALL 4
  93. #define SCF_NPRIMS 8 // Need wait and no-wait versions of each,
  94. // except for SCF_PRIM_RESCHED and
  95. // SCF_PRIM_SINGLE_RPC.
  96. static char *scf_prim_name[] = {
  97. "resched_cpu",
  98. "smp_call_function_single",
  99. "smp_call_function_single_rpc",
  100. "smp_call_function_many",
  101. "smp_call_function",
  102. };
  103. struct scf_selector {
  104. unsigned long scfs_weight;
  105. int scfs_prim;
  106. bool scfs_wait;
  107. };
  108. static struct scf_selector scf_sel_array[SCF_NPRIMS];
  109. static int scf_sel_array_len;
  110. static unsigned long scf_sel_totweight;
  111. // Communicate between caller and handler.
  112. struct scf_check {
  113. bool scfc_in;
  114. bool scfc_out;
  115. int scfc_cpu; // -1 for not _single().
  116. bool scfc_wait;
  117. bool scfc_rpc;
  118. struct completion scfc_completion;
  119. };
  120. // Use to wait for all threads to start.
  121. static atomic_t n_started;
  122. static atomic_t n_errs;
  123. static atomic_t n_mb_in_errs;
  124. static atomic_t n_mb_out_errs;
  125. static atomic_t n_alloc_errs;
  126. static bool scfdone;
  127. static char *bangstr = "";
  128. static DEFINE_TORTURE_RANDOM_PERCPU(scf_torture_rand);
  129. extern void resched_cpu(int cpu); // An alternative IPI vector.
  130. // Print torture statistics. Caller must ensure serialization.
  131. static void scf_torture_stats_print(void)
  132. {
  133. int cpu;
  134. int i;
  135. long long invoked_count = 0;
  136. bool isdone = READ_ONCE(scfdone);
  137. struct scf_statistics scfs = {};
  138. for_each_possible_cpu(cpu)
  139. invoked_count += data_race(per_cpu(scf_invoked_count, cpu));
  140. for (i = 0; i < nthreads; i++) {
  141. scfs.n_resched += scf_stats_p[i].n_resched;
  142. scfs.n_single += scf_stats_p[i].n_single;
  143. scfs.n_single_ofl += scf_stats_p[i].n_single_ofl;
  144. scfs.n_single_rpc += scf_stats_p[i].n_single_rpc;
  145. scfs.n_single_wait += scf_stats_p[i].n_single_wait;
  146. scfs.n_single_wait_ofl += scf_stats_p[i].n_single_wait_ofl;
  147. scfs.n_many += scf_stats_p[i].n_many;
  148. scfs.n_many_wait += scf_stats_p[i].n_many_wait;
  149. scfs.n_all += scf_stats_p[i].n_all;
  150. scfs.n_all_wait += scf_stats_p[i].n_all_wait;
  151. }
  152. if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) ||
  153. atomic_read(&n_mb_out_errs) ||
  154. (!IS_ENABLED(CONFIG_KASAN) && atomic_read(&n_alloc_errs)))
  155. bangstr = "!!! ";
  156. pr_alert("%s %sscf_invoked_count %s: %lld resched: %lld single: %lld/%lld single_ofl: %lld/%lld single_rpc: %lld single_rpc_ofl: %lld many: %lld/%lld all: %lld/%lld ",
  157. SCFTORT_FLAG, bangstr, isdone ? "VER" : "ver", invoked_count, scfs.n_resched,
  158. scfs.n_single, scfs.n_single_wait, scfs.n_single_ofl, scfs.n_single_wait_ofl,
  159. scfs.n_single_rpc, scfs.n_single_rpc_ofl,
  160. scfs.n_many, scfs.n_many_wait, scfs.n_all, scfs.n_all_wait);
  161. torture_onoff_stats();
  162. pr_cont("ste: %d stnmie: %d stnmoe: %d staf: %d\n", atomic_read(&n_errs),
  163. atomic_read(&n_mb_in_errs), atomic_read(&n_mb_out_errs),
  164. atomic_read(&n_alloc_errs));
  165. }
  166. // Periodically prints torture statistics, if periodic statistics printing
  167. // was specified via the stat_interval module parameter.
  168. static int
  169. scf_torture_stats(void *arg)
  170. {
  171. VERBOSE_TOROUT_STRING("scf_torture_stats task started");
  172. do {
  173. schedule_timeout_interruptible(stat_interval * HZ);
  174. scf_torture_stats_print();
  175. torture_shutdown_absorb("scf_torture_stats");
  176. } while (!torture_must_stop());
  177. torture_kthread_stopping("scf_torture_stats");
  178. return 0;
  179. }
  180. // Add a primitive to the scf_sel_array[].
  181. static void scf_sel_add(unsigned long weight, int prim, bool wait)
  182. {
  183. struct scf_selector *scfsp = &scf_sel_array[scf_sel_array_len];
  184. // If no weight, if array would overflow, if computing three-place
  185. // percentages would overflow, or if the scf_prim_name[] array would
  186. // overflow, don't bother. In the last three two cases, complain.
  187. if (!weight ||
  188. WARN_ON_ONCE(scf_sel_array_len >= ARRAY_SIZE(scf_sel_array)) ||
  189. WARN_ON_ONCE(0 - 100000 * weight <= 100000 * scf_sel_totweight) ||
  190. WARN_ON_ONCE(prim >= ARRAY_SIZE(scf_prim_name)))
  191. return;
  192. scf_sel_totweight += weight;
  193. scfsp->scfs_weight = scf_sel_totweight;
  194. scfsp->scfs_prim = prim;
  195. scfsp->scfs_wait = wait;
  196. scf_sel_array_len++;
  197. }
  198. // Dump out weighting percentages for scf_prim_name[] array.
  199. static void scf_sel_dump(void)
  200. {
  201. int i;
  202. unsigned long oldw = 0;
  203. struct scf_selector *scfsp;
  204. unsigned long w;
  205. for (i = 0; i < scf_sel_array_len; i++) {
  206. scfsp = &scf_sel_array[i];
  207. w = (scfsp->scfs_weight - oldw) * 100000 / scf_sel_totweight;
  208. pr_info("%s: %3lu.%03lu %s(%s)\n", __func__, w / 1000, w % 1000,
  209. scf_prim_name[scfsp->scfs_prim],
  210. scfsp->scfs_wait ? "wait" : "nowait");
  211. oldw = scfsp->scfs_weight;
  212. }
  213. }
  214. // Randomly pick a primitive and wait/nowait, based on weightings.
  215. static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp)
  216. {
  217. int i;
  218. unsigned long w = torture_random(trsp) % (scf_sel_totweight + 1);
  219. for (i = 0; i < scf_sel_array_len; i++)
  220. if (scf_sel_array[i].scfs_weight >= w)
  221. return &scf_sel_array[i];
  222. WARN_ON_ONCE(1);
  223. return &scf_sel_array[0];
  224. }
  225. // Update statistics and occasionally burn up mass quantities of CPU time,
  226. // if told to do so via scftorture.longwait. Otherwise, occasionally burn
  227. // a little bit.
  228. static void scf_handler(void *scfc_in)
  229. {
  230. int i;
  231. int j;
  232. unsigned long r = torture_random(this_cpu_ptr(&scf_torture_rand));
  233. struct scf_check *scfcp = scfc_in;
  234. if (likely(scfcp)) {
  235. WRITE_ONCE(scfcp->scfc_out, false); // For multiple receivers.
  236. if (WARN_ON_ONCE(unlikely(!READ_ONCE(scfcp->scfc_in))))
  237. atomic_inc(&n_mb_in_errs);
  238. }
  239. this_cpu_inc(scf_invoked_count);
  240. if (longwait <= 0) {
  241. if (!(r & 0xffc0)) {
  242. udelay(r & 0x3f);
  243. goto out;
  244. }
  245. }
  246. if (r & 0xfff)
  247. goto out;
  248. r = (r >> 12);
  249. if (longwait <= 0) {
  250. udelay((r & 0xff) + 1);
  251. goto out;
  252. }
  253. r = r % longwait + 1;
  254. for (i = 0; i < r; i++) {
  255. for (j = 0; j < 1000; j++) {
  256. udelay(1000);
  257. cpu_relax();
  258. }
  259. }
  260. out:
  261. if (unlikely(!scfcp))
  262. return;
  263. if (scfcp->scfc_wait) {
  264. WRITE_ONCE(scfcp->scfc_out, true);
  265. if (scfcp->scfc_rpc)
  266. complete(&scfcp->scfc_completion);
  267. } else {
  268. kfree(scfcp);
  269. }
  270. }
  271. // As above, but check for correct CPU.
  272. static void scf_handler_1(void *scfc_in)
  273. {
  274. struct scf_check *scfcp = scfc_in;
  275. if (likely(scfcp) && WARN_ONCE(smp_processor_id() != scfcp->scfc_cpu, "%s: Wanted CPU %d got CPU %d\n", __func__, scfcp->scfc_cpu, smp_processor_id())) {
  276. atomic_inc(&n_errs);
  277. }
  278. scf_handler(scfcp);
  279. }
  280. // Randomly do an smp_call_function*() invocation.
  281. static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_random_state *trsp)
  282. {
  283. uintptr_t cpu;
  284. int ret = 0;
  285. struct scf_check *scfcp = NULL;
  286. struct scf_selector *scfsp = scf_sel_rand(trsp);
  287. if (use_cpus_read_lock)
  288. cpus_read_lock();
  289. else
  290. preempt_disable();
  291. if (scfsp->scfs_prim == SCF_PRIM_SINGLE || scfsp->scfs_wait) {
  292. scfcp = kmalloc(sizeof(*scfcp), GFP_ATOMIC);
  293. if (!scfcp) {
  294. WARN_ON_ONCE(!IS_ENABLED(CONFIG_KASAN));
  295. atomic_inc(&n_alloc_errs);
  296. } else {
  297. scfcp->scfc_cpu = -1;
  298. scfcp->scfc_wait = scfsp->scfs_wait;
  299. scfcp->scfc_out = false;
  300. scfcp->scfc_rpc = false;
  301. }
  302. }
  303. switch (scfsp->scfs_prim) {
  304. case SCF_PRIM_RESCHED:
  305. if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST)) {
  306. cpu = torture_random(trsp) % nr_cpu_ids;
  307. scfp->n_resched++;
  308. resched_cpu(cpu);
  309. this_cpu_inc(scf_invoked_count);
  310. }
  311. break;
  312. case SCF_PRIM_SINGLE:
  313. cpu = torture_random(trsp) % nr_cpu_ids;
  314. if (scfsp->scfs_wait)
  315. scfp->n_single_wait++;
  316. else
  317. scfp->n_single++;
  318. if (scfcp) {
  319. scfcp->scfc_cpu = cpu;
  320. barrier(); // Prevent race-reduction compiler optimizations.
  321. scfcp->scfc_in = true;
  322. }
  323. ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, scfsp->scfs_wait);
  324. if (ret) {
  325. if (scfsp->scfs_wait)
  326. scfp->n_single_wait_ofl++;
  327. else
  328. scfp->n_single_ofl++;
  329. kfree(scfcp);
  330. scfcp = NULL;
  331. }
  332. break;
  333. case SCF_PRIM_SINGLE_RPC:
  334. if (!scfcp)
  335. break;
  336. cpu = torture_random(trsp) % nr_cpu_ids;
  337. scfp->n_single_rpc++;
  338. scfcp->scfc_cpu = cpu;
  339. scfcp->scfc_wait = true;
  340. init_completion(&scfcp->scfc_completion);
  341. scfcp->scfc_rpc = true;
  342. barrier(); // Prevent race-reduction compiler optimizations.
  343. scfcp->scfc_in = true;
  344. ret = smp_call_function_single(cpu, scf_handler_1, (void *)scfcp, 0);
  345. if (!ret) {
  346. if (use_cpus_read_lock)
  347. cpus_read_unlock();
  348. else
  349. preempt_enable();
  350. wait_for_completion(&scfcp->scfc_completion);
  351. if (use_cpus_read_lock)
  352. cpus_read_lock();
  353. else
  354. preempt_disable();
  355. } else {
  356. scfp->n_single_rpc_ofl++;
  357. kfree(scfcp);
  358. scfcp = NULL;
  359. }
  360. break;
  361. case SCF_PRIM_MANY:
  362. if (scfsp->scfs_wait)
  363. scfp->n_many_wait++;
  364. else
  365. scfp->n_many++;
  366. if (scfcp) {
  367. barrier(); // Prevent race-reduction compiler optimizations.
  368. scfcp->scfc_in = true;
  369. }
  370. smp_call_function_many(cpu_online_mask, scf_handler, scfcp, scfsp->scfs_wait);
  371. break;
  372. case SCF_PRIM_ALL:
  373. if (scfsp->scfs_wait)
  374. scfp->n_all_wait++;
  375. else
  376. scfp->n_all++;
  377. if (scfcp) {
  378. barrier(); // Prevent race-reduction compiler optimizations.
  379. scfcp->scfc_in = true;
  380. }
  381. smp_call_function(scf_handler, scfcp, scfsp->scfs_wait);
  382. break;
  383. default:
  384. WARN_ON_ONCE(1);
  385. if (scfcp)
  386. scfcp->scfc_out = true;
  387. }
  388. if (scfcp && scfsp->scfs_wait) {
  389. if (WARN_ON_ONCE((num_online_cpus() > 1 || scfsp->scfs_prim == SCF_PRIM_SINGLE) &&
  390. !scfcp->scfc_out)) {
  391. pr_warn("%s: Memory-ordering failure, scfs_prim: %d.\n", __func__, scfsp->scfs_prim);
  392. atomic_inc(&n_mb_out_errs); // Leak rather than trash!
  393. } else {
  394. kfree(scfcp);
  395. }
  396. barrier(); // Prevent race-reduction compiler optimizations.
  397. }
  398. if (use_cpus_read_lock)
  399. cpus_read_unlock();
  400. else
  401. preempt_enable();
  402. if (!(torture_random(trsp) & 0xfff))
  403. schedule_timeout_uninterruptible(1);
  404. }
  405. // SCF test kthread. Repeatedly does calls to members of the
  406. // smp_call_function() family of functions.
  407. static int scftorture_invoker(void *arg)
  408. {
  409. int cpu;
  410. int curcpu;
  411. DEFINE_TORTURE_RANDOM(rand);
  412. struct scf_statistics *scfp = (struct scf_statistics *)arg;
  413. bool was_offline = false;
  414. VERBOSE_SCFTORTOUT("scftorture_invoker %d: task started", scfp->cpu);
  415. cpu = scfp->cpu % nr_cpu_ids;
  416. WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(cpu)));
  417. set_user_nice(current, MAX_NICE);
  418. if (holdoff)
  419. schedule_timeout_interruptible(holdoff * HZ);
  420. VERBOSE_SCFTORTOUT("scftorture_invoker %d: Waiting for all SCF torturers from cpu %d", scfp->cpu, raw_smp_processor_id());
  421. // Make sure that the CPU is affinitized appropriately during testing.
  422. curcpu = raw_smp_processor_id();
  423. WARN_ONCE(curcpu != scfp->cpu % nr_cpu_ids,
  424. "%s: Wanted CPU %d, running on %d, nr_cpu_ids = %d\n",
  425. __func__, scfp->cpu, curcpu, nr_cpu_ids);
  426. if (!atomic_dec_return(&n_started))
  427. while (atomic_read_acquire(&n_started)) {
  428. if (torture_must_stop()) {
  429. VERBOSE_SCFTORTOUT("scftorture_invoker %d ended before starting", scfp->cpu);
  430. goto end;
  431. }
  432. schedule_timeout_uninterruptible(1);
  433. }
  434. VERBOSE_SCFTORTOUT("scftorture_invoker %d started", scfp->cpu);
  435. do {
  436. scftorture_invoke_one(scfp, &rand);
  437. while (cpu_is_offline(cpu) && !torture_must_stop()) {
  438. schedule_timeout_interruptible(HZ / 5);
  439. was_offline = true;
  440. }
  441. if (was_offline) {
  442. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  443. was_offline = false;
  444. }
  445. cond_resched();
  446. stutter_wait("scftorture_invoker");
  447. } while (!torture_must_stop());
  448. VERBOSE_SCFTORTOUT("scftorture_invoker %d ended", scfp->cpu);
  449. end:
  450. torture_kthread_stopping("scftorture_invoker");
  451. return 0;
  452. }
  453. static void
  454. scftorture_print_module_parms(const char *tag)
  455. {
  456. pr_alert(SCFTORT_FLAG
  457. "--- %s: verbose=%d holdoff=%d longwait=%d nthreads=%d onoff_holdoff=%d onoff_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d use_cpus_read_lock=%d, weight_resched=%d, weight_single=%d, weight_single_rpc=%d, weight_single_wait=%d, weight_many=%d, weight_many_wait=%d, weight_all=%d, weight_all_wait=%d\n", tag,
  458. verbose, holdoff, longwait, nthreads, onoff_holdoff, onoff_interval, shutdown, stat_interval, stutter, use_cpus_read_lock, weight_resched, weight_single, weight_single_rpc, weight_single_wait, weight_many, weight_many_wait, weight_all, weight_all_wait);
  459. }
  460. static void scf_cleanup_handler(void *unused)
  461. {
  462. }
  463. static void scf_torture_cleanup(void)
  464. {
  465. int i;
  466. if (torture_cleanup_begin())
  467. return;
  468. WRITE_ONCE(scfdone, true);
  469. if (nthreads && scf_stats_p)
  470. for (i = 0; i < nthreads; i++)
  471. torture_stop_kthread("scftorture_invoker", scf_stats_p[i].task);
  472. else
  473. goto end;
  474. smp_call_function(scf_cleanup_handler, NULL, 0);
  475. torture_stop_kthread(scf_torture_stats, scf_torture_stats_task);
  476. scf_torture_stats_print(); // -After- the stats thread is stopped!
  477. kfree(scf_stats_p); // -After- the last stats print has completed!
  478. scf_stats_p = NULL;
  479. if (atomic_read(&n_errs) || atomic_read(&n_mb_in_errs) || atomic_read(&n_mb_out_errs))
  480. scftorture_print_module_parms("End of test: FAILURE");
  481. else if (torture_onoff_failures())
  482. scftorture_print_module_parms("End of test: LOCK_HOTPLUG");
  483. else
  484. scftorture_print_module_parms("End of test: SUCCESS");
  485. end:
  486. torture_cleanup_end();
  487. }
  488. static int __init scf_torture_init(void)
  489. {
  490. long i;
  491. int firsterr = 0;
  492. unsigned long weight_resched1 = weight_resched;
  493. unsigned long weight_single1 = weight_single;
  494. unsigned long weight_single_rpc1 = weight_single_rpc;
  495. unsigned long weight_single_wait1 = weight_single_wait;
  496. unsigned long weight_many1 = weight_many;
  497. unsigned long weight_many_wait1 = weight_many_wait;
  498. unsigned long weight_all1 = weight_all;
  499. unsigned long weight_all_wait1 = weight_all_wait;
  500. if (!torture_init_begin(SCFTORT_STRING, verbose))
  501. return -EBUSY;
  502. scftorture_print_module_parms("Start of test");
  503. if (weight_resched <= 0 &&
  504. weight_single <= 0 && weight_single_rpc <= 0 && weight_single_wait <= 0 &&
  505. weight_many <= 0 && weight_many_wait <= 0 &&
  506. weight_all <= 0 && weight_all_wait <= 0) {
  507. weight_resched1 = weight_resched == 0 ? 0 : 2 * nr_cpu_ids;
  508. weight_single1 = weight_single == 0 ? 0 : 2 * nr_cpu_ids;
  509. weight_single_rpc1 = weight_single_rpc == 0 ? 0 : 2 * nr_cpu_ids;
  510. weight_single_wait1 = weight_single_wait == 0 ? 0 : 2 * nr_cpu_ids;
  511. weight_many1 = weight_many == 0 ? 0 : 2;
  512. weight_many_wait1 = weight_many_wait == 0 ? 0 : 2;
  513. weight_all1 = weight_all == 0 ? 0 : 1;
  514. weight_all_wait1 = weight_all_wait == 0 ? 0 : 1;
  515. } else {
  516. if (weight_resched == -1)
  517. weight_resched1 = 0;
  518. if (weight_single == -1)
  519. weight_single1 = 0;
  520. if (weight_single_rpc == -1)
  521. weight_single_rpc1 = 0;
  522. if (weight_single_wait == -1)
  523. weight_single_wait1 = 0;
  524. if (weight_many == -1)
  525. weight_many1 = 0;
  526. if (weight_many_wait == -1)
  527. weight_many_wait1 = 0;
  528. if (weight_all == -1)
  529. weight_all1 = 0;
  530. if (weight_all_wait == -1)
  531. weight_all_wait1 = 0;
  532. }
  533. if (weight_resched1 == 0 && weight_single1 == 0 && weight_single_rpc1 == 0 &&
  534. weight_single_wait1 == 0 && weight_many1 == 0 && weight_many_wait1 == 0 &&
  535. weight_all1 == 0 && weight_all_wait1 == 0) {
  536. SCFTORTOUT_ERRSTRING("all zero weights makes no sense");
  537. firsterr = -EINVAL;
  538. goto unwind;
  539. }
  540. if (IS_BUILTIN(CONFIG_SCF_TORTURE_TEST))
  541. scf_sel_add(weight_resched1, SCF_PRIM_RESCHED, false);
  542. else if (weight_resched1)
  543. SCFTORTOUT_ERRSTRING("built as module, weight_resched ignored");
  544. scf_sel_add(weight_single1, SCF_PRIM_SINGLE, false);
  545. scf_sel_add(weight_single_rpc1, SCF_PRIM_SINGLE_RPC, true);
  546. scf_sel_add(weight_single_wait1, SCF_PRIM_SINGLE, true);
  547. scf_sel_add(weight_many1, SCF_PRIM_MANY, false);
  548. scf_sel_add(weight_many_wait1, SCF_PRIM_MANY, true);
  549. scf_sel_add(weight_all1, SCF_PRIM_ALL, false);
  550. scf_sel_add(weight_all_wait1, SCF_PRIM_ALL, true);
  551. scf_sel_dump();
  552. if (onoff_interval > 0) {
  553. firsterr = torture_onoff_init(onoff_holdoff * HZ, onoff_interval, NULL);
  554. if (torture_init_error(firsterr))
  555. goto unwind;
  556. }
  557. if (shutdown_secs > 0) {
  558. firsterr = torture_shutdown_init(shutdown_secs, scf_torture_cleanup);
  559. if (torture_init_error(firsterr))
  560. goto unwind;
  561. }
  562. if (stutter > 0) {
  563. firsterr = torture_stutter_init(stutter, stutter);
  564. if (torture_init_error(firsterr))
  565. goto unwind;
  566. }
  567. // Worker tasks invoking smp_call_function().
  568. if (nthreads < 0)
  569. nthreads = num_online_cpus();
  570. scf_stats_p = kcalloc(nthreads, sizeof(scf_stats_p[0]), GFP_KERNEL);
  571. if (!scf_stats_p) {
  572. SCFTORTOUT_ERRSTRING("out of memory");
  573. firsterr = -ENOMEM;
  574. goto unwind;
  575. }
  576. VERBOSE_SCFTORTOUT("Starting %d smp_call_function() threads", nthreads);
  577. atomic_set(&n_started, nthreads);
  578. for (i = 0; i < nthreads; i++) {
  579. scf_stats_p[i].cpu = i;
  580. firsterr = torture_create_kthread(scftorture_invoker, (void *)&scf_stats_p[i],
  581. scf_stats_p[i].task);
  582. if (torture_init_error(firsterr))
  583. goto unwind;
  584. }
  585. if (stat_interval > 0) {
  586. firsterr = torture_create_kthread(scf_torture_stats, NULL, scf_torture_stats_task);
  587. if (torture_init_error(firsterr))
  588. goto unwind;
  589. }
  590. torture_init_end();
  591. return 0;
  592. unwind:
  593. torture_init_end();
  594. scf_torture_cleanup();
  595. if (shutdown_secs) {
  596. WARN_ON(!IS_MODULE(CONFIG_SCF_TORTURE_TEST));
  597. kernel_power_off();
  598. }
  599. return firsterr;
  600. }
  601. module_init(scf_torture_init);
  602. module_exit(scf_torture_cleanup);