scftorture.c 20 KB

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