locktorture.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Module-based torture test facility for locking
  4. *
  5. * Copyright (C) IBM Corporation, 2014
  6. *
  7. * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
  8. * Davidlohr Bueso <dave@stgolabs.net>
  9. * Based on kernel/rcu/torture.c.
  10. */
  11. #define pr_fmt(fmt) fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/kthread.h>
  15. #include <linux/sched/rt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/smp.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/sched.h>
  22. #include <uapi/linux/sched/types.h>
  23. #include <linux/rtmutex.h>
  24. #include <linux/atomic.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/torture.h>
  29. #include <linux/reboot.h>
  30. MODULE_DESCRIPTION("torture test facility for locking");
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
  33. torture_param(int, acq_writer_lim, 0, "Write_acquisition time limit (jiffies).");
  34. torture_param(int, call_rcu_chains, 0, "Self-propagate call_rcu() chains during test (0=disable).");
  35. torture_param(int, long_hold, 100, "Do occasional long hold of lock (ms), 0=disable");
  36. torture_param(int, nested_locks, 0, "Number of nested locks (max = 8)");
  37. torture_param(int, nreaders_stress, -1, "Number of read-locking stress-test threads");
  38. torture_param(int, nwriters_stress, -1, "Number of write-locking stress-test threads");
  39. torture_param(int, onoff_holdoff, 0, "Time after boot before CPU hotplugs (s)");
  40. torture_param(int, onoff_interval, 0, "Time between CPU hotplugs (s), 0=disable");
  41. torture_param(int, rt_boost, 2,
  42. "Do periodic rt-boost. 0=Disable, 1=Only for rt_mutex, 2=For all lock types.");
  43. torture_param(int, rt_boost_factor, 50, "A factor determining how often rt-boost happens.");
  44. torture_param(int, shuffle_interval, 3, "Number of jiffies between shuffles, 0=disable");
  45. torture_param(int, shutdown_secs, 0, "Shutdown time (j), <= zero to disable.");
  46. torture_param(int, stat_interval, 60, "Number of seconds between stats printk()s");
  47. torture_param(int, stutter, 5, "Number of jiffies to run/halt test, 0=disable");
  48. torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
  49. torture_param(int, writer_fifo, 0, "Run writers at sched_set_fifo() priority");
  50. /* Going much higher trips "BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!" errors */
  51. #define MAX_NESTED_LOCKS 8
  52. static char *torture_type = IS_ENABLED(CONFIG_PREEMPT_RT) ? "raw_spin_lock" : "spin_lock";
  53. module_param(torture_type, charp, 0444);
  54. MODULE_PARM_DESC(torture_type,
  55. "Type of lock to torture (spin_lock, spin_lock_irq, mutex_lock, ...)");
  56. static cpumask_var_t bind_readers; // Bind the readers to the specified set of CPUs.
  57. static cpumask_var_t bind_writers; // Bind the writers to the specified set of CPUs.
  58. // Parse a cpumask kernel parameter. If there are more users later on,
  59. // this might need to got to a more central location.
  60. static int param_set_cpumask(const char *val, const struct kernel_param *kp)
  61. {
  62. cpumask_var_t *cm_bind = kp->arg;
  63. int ret;
  64. char *s;
  65. if (!alloc_cpumask_var(cm_bind, GFP_KERNEL)) {
  66. s = "Out of memory";
  67. ret = -ENOMEM;
  68. goto out_err;
  69. }
  70. ret = cpulist_parse(val, *cm_bind);
  71. if (!ret)
  72. return ret;
  73. s = "Bad CPU range";
  74. out_err:
  75. pr_warn("%s: %s, all CPUs set\n", kp->name, s);
  76. cpumask_setall(*cm_bind);
  77. return ret;
  78. }
  79. // Output a cpumask kernel parameter.
  80. static int param_get_cpumask(char *buffer, const struct kernel_param *kp)
  81. {
  82. cpumask_var_t *cm_bind = kp->arg;
  83. return sprintf(buffer, "%*pbl", cpumask_pr_args(*cm_bind));
  84. }
  85. static bool cpumask_nonempty(cpumask_var_t mask)
  86. {
  87. return cpumask_available(mask) && !cpumask_empty(mask);
  88. }
  89. static const struct kernel_param_ops lt_bind_ops = {
  90. .set = param_set_cpumask,
  91. .get = param_get_cpumask,
  92. };
  93. module_param_cb(bind_readers, &lt_bind_ops, &bind_readers, 0644);
  94. module_param_cb(bind_writers, &lt_bind_ops, &bind_writers, 0644);
  95. long torture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask);
  96. static struct task_struct *stats_task;
  97. static struct task_struct **writer_tasks;
  98. static struct task_struct **reader_tasks;
  99. static bool lock_is_write_held;
  100. static atomic_t lock_is_read_held;
  101. static unsigned long last_lock_release;
  102. struct lock_stress_stats {
  103. long n_lock_fail;
  104. long n_lock_acquired;
  105. };
  106. struct call_rcu_chain {
  107. struct rcu_head crc_rh;
  108. bool crc_stop;
  109. };
  110. struct call_rcu_chain *call_rcu_chain_list;
  111. /* Forward reference. */
  112. static void lock_torture_cleanup(void);
  113. /*
  114. * Operations vector for selecting different types of tests.
  115. */
  116. struct lock_torture_ops {
  117. void (*init)(void);
  118. void (*exit)(void);
  119. int (*nested_lock)(int tid, u32 lockset);
  120. int (*writelock)(int tid);
  121. void (*write_delay)(struct torture_random_state *trsp);
  122. void (*task_boost)(struct torture_random_state *trsp);
  123. void (*writeunlock)(int tid);
  124. void (*nested_unlock)(int tid, u32 lockset);
  125. int (*readlock)(int tid);
  126. void (*read_delay)(struct torture_random_state *trsp);
  127. void (*readunlock)(int tid);
  128. unsigned long flags; /* for irq spinlocks */
  129. const char *name;
  130. };
  131. struct lock_torture_cxt {
  132. int nrealwriters_stress;
  133. int nrealreaders_stress;
  134. bool debug_lock;
  135. bool init_called;
  136. atomic_t n_lock_torture_errors;
  137. struct lock_torture_ops *cur_ops;
  138. struct lock_stress_stats *lwsa; /* writer statistics */
  139. struct lock_stress_stats *lrsa; /* reader statistics */
  140. };
  141. static struct lock_torture_cxt cxt = { 0, 0, false, false,
  142. ATOMIC_INIT(0),
  143. NULL, NULL};
  144. /*
  145. * Definitions for lock torture testing.
  146. */
  147. static int torture_lock_busted_write_lock(int tid __maybe_unused)
  148. {
  149. return 0; /* BUGGY, do not use in real life!!! */
  150. }
  151. static void torture_lock_busted_write_delay(struct torture_random_state *trsp)
  152. {
  153. /* We want a long delay occasionally to force massive contention. */
  154. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
  155. mdelay(long_hold);
  156. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  157. torture_preempt_schedule(); /* Allow test to be preempted. */
  158. }
  159. static void torture_lock_busted_write_unlock(int tid __maybe_unused)
  160. {
  161. /* BUGGY, do not use in real life!!! */
  162. }
  163. static void __torture_rt_boost(struct torture_random_state *trsp)
  164. {
  165. const unsigned int factor = rt_boost_factor;
  166. if (!rt_task(current)) {
  167. /*
  168. * Boost priority once every rt_boost_factor operations. When
  169. * the task tries to take the lock, the rtmutex it will account
  170. * for the new priority, and do any corresponding pi-dance.
  171. */
  172. if (trsp && !(torture_random(trsp) %
  173. (cxt.nrealwriters_stress * factor))) {
  174. sched_set_fifo(current);
  175. } else /* common case, do nothing */
  176. return;
  177. } else {
  178. /*
  179. * The task will remain boosted for another 10 * rt_boost_factor
  180. * operations, then restored back to its original prio, and so
  181. * forth.
  182. *
  183. * When @trsp is nil, we want to force-reset the task for
  184. * stopping the kthread.
  185. */
  186. if (!trsp || !(torture_random(trsp) %
  187. (cxt.nrealwriters_stress * factor * 2))) {
  188. sched_set_normal(current, 0);
  189. } else /* common case, do nothing */
  190. return;
  191. }
  192. }
  193. static void torture_rt_boost(struct torture_random_state *trsp)
  194. {
  195. if (rt_boost != 2)
  196. return;
  197. __torture_rt_boost(trsp);
  198. }
  199. static struct lock_torture_ops lock_busted_ops = {
  200. .writelock = torture_lock_busted_write_lock,
  201. .write_delay = torture_lock_busted_write_delay,
  202. .task_boost = torture_rt_boost,
  203. .writeunlock = torture_lock_busted_write_unlock,
  204. .readlock = NULL,
  205. .read_delay = NULL,
  206. .readunlock = NULL,
  207. .name = "lock_busted"
  208. };
  209. static DEFINE_SPINLOCK(torture_spinlock);
  210. static int torture_spin_lock_write_lock(int tid __maybe_unused)
  211. __acquires(torture_spinlock)
  212. {
  213. spin_lock(&torture_spinlock);
  214. return 0;
  215. }
  216. static void torture_spin_lock_write_delay(struct torture_random_state *trsp)
  217. {
  218. const unsigned long shortdelay_us = 2;
  219. unsigned long j;
  220. /* We want a short delay mostly to emulate likely code, and
  221. * we want a long delay occasionally to force massive contention.
  222. */
  223. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold))) {
  224. j = jiffies;
  225. mdelay(long_hold);
  226. pr_alert("%s: delay = %lu jiffies.\n", __func__, jiffies - j);
  227. }
  228. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 200 * shortdelay_us)))
  229. udelay(shortdelay_us);
  230. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  231. torture_preempt_schedule(); /* Allow test to be preempted. */
  232. }
  233. static void torture_spin_lock_write_unlock(int tid __maybe_unused)
  234. __releases(torture_spinlock)
  235. {
  236. spin_unlock(&torture_spinlock);
  237. }
  238. static struct lock_torture_ops spin_lock_ops = {
  239. .writelock = torture_spin_lock_write_lock,
  240. .write_delay = torture_spin_lock_write_delay,
  241. .task_boost = torture_rt_boost,
  242. .writeunlock = torture_spin_lock_write_unlock,
  243. .readlock = NULL,
  244. .read_delay = NULL,
  245. .readunlock = NULL,
  246. .name = "spin_lock"
  247. };
  248. static int torture_spin_lock_write_lock_irq(int tid __maybe_unused)
  249. __acquires(torture_spinlock)
  250. {
  251. unsigned long flags;
  252. spin_lock_irqsave(&torture_spinlock, flags);
  253. cxt.cur_ops->flags = flags;
  254. return 0;
  255. }
  256. static void torture_lock_spin_write_unlock_irq(int tid __maybe_unused)
  257. __releases(torture_spinlock)
  258. {
  259. spin_unlock_irqrestore(&torture_spinlock, cxt.cur_ops->flags);
  260. }
  261. static struct lock_torture_ops spin_lock_irq_ops = {
  262. .writelock = torture_spin_lock_write_lock_irq,
  263. .write_delay = torture_spin_lock_write_delay,
  264. .task_boost = torture_rt_boost,
  265. .writeunlock = torture_lock_spin_write_unlock_irq,
  266. .readlock = NULL,
  267. .read_delay = NULL,
  268. .readunlock = NULL,
  269. .name = "spin_lock_irq"
  270. };
  271. static DEFINE_RAW_SPINLOCK(torture_raw_spinlock);
  272. static int torture_raw_spin_lock_write_lock(int tid __maybe_unused)
  273. __acquires(torture_raw_spinlock)
  274. {
  275. raw_spin_lock(&torture_raw_spinlock);
  276. return 0;
  277. }
  278. static void torture_raw_spin_lock_write_unlock(int tid __maybe_unused)
  279. __releases(torture_raw_spinlock)
  280. {
  281. raw_spin_unlock(&torture_raw_spinlock);
  282. }
  283. static struct lock_torture_ops raw_spin_lock_ops = {
  284. .writelock = torture_raw_spin_lock_write_lock,
  285. .write_delay = torture_spin_lock_write_delay,
  286. .task_boost = torture_rt_boost,
  287. .writeunlock = torture_raw_spin_lock_write_unlock,
  288. .readlock = NULL,
  289. .read_delay = NULL,
  290. .readunlock = NULL,
  291. .name = "raw_spin_lock"
  292. };
  293. static int torture_raw_spin_lock_write_lock_irq(int tid __maybe_unused)
  294. __acquires(torture_raw_spinlock)
  295. {
  296. unsigned long flags;
  297. raw_spin_lock_irqsave(&torture_raw_spinlock, flags);
  298. cxt.cur_ops->flags = flags;
  299. return 0;
  300. }
  301. static void torture_raw_spin_lock_write_unlock_irq(int tid __maybe_unused)
  302. __releases(torture_raw_spinlock)
  303. {
  304. raw_spin_unlock_irqrestore(&torture_raw_spinlock, cxt.cur_ops->flags);
  305. }
  306. static struct lock_torture_ops raw_spin_lock_irq_ops = {
  307. .writelock = torture_raw_spin_lock_write_lock_irq,
  308. .write_delay = torture_spin_lock_write_delay,
  309. .task_boost = torture_rt_boost,
  310. .writeunlock = torture_raw_spin_lock_write_unlock_irq,
  311. .readlock = NULL,
  312. .read_delay = NULL,
  313. .readunlock = NULL,
  314. .name = "raw_spin_lock_irq"
  315. };
  316. static DEFINE_RWLOCK(torture_rwlock);
  317. static int torture_rwlock_write_lock(int tid __maybe_unused)
  318. __acquires(torture_rwlock)
  319. {
  320. write_lock(&torture_rwlock);
  321. return 0;
  322. }
  323. static void torture_rwlock_write_delay(struct torture_random_state *trsp)
  324. {
  325. const unsigned long shortdelay_us = 2;
  326. /* We want a short delay mostly to emulate likely code, and
  327. * we want a long delay occasionally to force massive contention.
  328. */
  329. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
  330. mdelay(long_hold);
  331. else
  332. udelay(shortdelay_us);
  333. }
  334. static void torture_rwlock_write_unlock(int tid __maybe_unused)
  335. __releases(torture_rwlock)
  336. {
  337. write_unlock(&torture_rwlock);
  338. }
  339. static int torture_rwlock_read_lock(int tid __maybe_unused)
  340. __acquires(torture_rwlock)
  341. {
  342. read_lock(&torture_rwlock);
  343. return 0;
  344. }
  345. static void torture_rwlock_read_delay(struct torture_random_state *trsp)
  346. {
  347. const unsigned long shortdelay_us = 10;
  348. /* We want a short delay mostly to emulate likely code, and
  349. * we want a long delay occasionally to force massive contention.
  350. */
  351. if (long_hold && !(torture_random(trsp) % (cxt.nrealreaders_stress * 2000 * long_hold)))
  352. mdelay(long_hold);
  353. else
  354. udelay(shortdelay_us);
  355. }
  356. static void torture_rwlock_read_unlock(int tid __maybe_unused)
  357. __releases(torture_rwlock)
  358. {
  359. read_unlock(&torture_rwlock);
  360. }
  361. static struct lock_torture_ops rw_lock_ops = {
  362. .writelock = torture_rwlock_write_lock,
  363. .write_delay = torture_rwlock_write_delay,
  364. .task_boost = torture_rt_boost,
  365. .writeunlock = torture_rwlock_write_unlock,
  366. .readlock = torture_rwlock_read_lock,
  367. .read_delay = torture_rwlock_read_delay,
  368. .readunlock = torture_rwlock_read_unlock,
  369. .name = "rw_lock"
  370. };
  371. static int torture_rwlock_write_lock_irq(int tid __maybe_unused)
  372. __acquires(torture_rwlock)
  373. {
  374. unsigned long flags;
  375. write_lock_irqsave(&torture_rwlock, flags);
  376. cxt.cur_ops->flags = flags;
  377. return 0;
  378. }
  379. static void torture_rwlock_write_unlock_irq(int tid __maybe_unused)
  380. __releases(torture_rwlock)
  381. {
  382. write_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
  383. }
  384. static int torture_rwlock_read_lock_irq(int tid __maybe_unused)
  385. __acquires(torture_rwlock)
  386. {
  387. unsigned long flags;
  388. read_lock_irqsave(&torture_rwlock, flags);
  389. cxt.cur_ops->flags = flags;
  390. return 0;
  391. }
  392. static void torture_rwlock_read_unlock_irq(int tid __maybe_unused)
  393. __releases(torture_rwlock)
  394. {
  395. read_unlock_irqrestore(&torture_rwlock, cxt.cur_ops->flags);
  396. }
  397. static struct lock_torture_ops rw_lock_irq_ops = {
  398. .writelock = torture_rwlock_write_lock_irq,
  399. .write_delay = torture_rwlock_write_delay,
  400. .task_boost = torture_rt_boost,
  401. .writeunlock = torture_rwlock_write_unlock_irq,
  402. .readlock = torture_rwlock_read_lock_irq,
  403. .read_delay = torture_rwlock_read_delay,
  404. .readunlock = torture_rwlock_read_unlock_irq,
  405. .name = "rw_lock_irq"
  406. };
  407. static DEFINE_MUTEX(torture_mutex);
  408. static struct mutex torture_nested_mutexes[MAX_NESTED_LOCKS];
  409. static struct lock_class_key nested_mutex_keys[MAX_NESTED_LOCKS];
  410. static void torture_mutex_init(void)
  411. {
  412. int i;
  413. for (i = 0; i < MAX_NESTED_LOCKS; i++)
  414. __mutex_init(&torture_nested_mutexes[i], __func__,
  415. &nested_mutex_keys[i]);
  416. }
  417. static int torture_mutex_nested_lock(int tid __maybe_unused,
  418. u32 lockset)
  419. {
  420. int i;
  421. for (i = 0; i < nested_locks; i++)
  422. if (lockset & (1 << i))
  423. mutex_lock(&torture_nested_mutexes[i]);
  424. return 0;
  425. }
  426. static int torture_mutex_lock(int tid __maybe_unused)
  427. __acquires(torture_mutex)
  428. {
  429. mutex_lock(&torture_mutex);
  430. return 0;
  431. }
  432. static void torture_mutex_delay(struct torture_random_state *trsp)
  433. {
  434. /* We want a long delay occasionally to force massive contention. */
  435. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
  436. mdelay(long_hold * 5);
  437. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  438. torture_preempt_schedule(); /* Allow test to be preempted. */
  439. }
  440. static void torture_mutex_unlock(int tid __maybe_unused)
  441. __releases(torture_mutex)
  442. {
  443. mutex_unlock(&torture_mutex);
  444. }
  445. static void torture_mutex_nested_unlock(int tid __maybe_unused,
  446. u32 lockset)
  447. {
  448. int i;
  449. for (i = nested_locks - 1; i >= 0; i--)
  450. if (lockset & (1 << i))
  451. mutex_unlock(&torture_nested_mutexes[i]);
  452. }
  453. static struct lock_torture_ops mutex_lock_ops = {
  454. .init = torture_mutex_init,
  455. .nested_lock = torture_mutex_nested_lock,
  456. .writelock = torture_mutex_lock,
  457. .write_delay = torture_mutex_delay,
  458. .task_boost = torture_rt_boost,
  459. .writeunlock = torture_mutex_unlock,
  460. .nested_unlock = torture_mutex_nested_unlock,
  461. .readlock = NULL,
  462. .read_delay = NULL,
  463. .readunlock = NULL,
  464. .name = "mutex_lock"
  465. };
  466. #include <linux/ww_mutex.h>
  467. /*
  468. * The torture ww_mutexes should belong to the same lock class as
  469. * torture_ww_class to avoid lockdep problem. The ww_mutex_init()
  470. * function is called for initialization to ensure that.
  471. */
  472. static DEFINE_WD_CLASS(torture_ww_class);
  473. static struct ww_mutex torture_ww_mutex_0, torture_ww_mutex_1, torture_ww_mutex_2;
  474. static struct ww_acquire_ctx *ww_acquire_ctxs;
  475. static void torture_ww_mutex_init(void)
  476. {
  477. ww_mutex_init(&torture_ww_mutex_0, &torture_ww_class);
  478. ww_mutex_init(&torture_ww_mutex_1, &torture_ww_class);
  479. ww_mutex_init(&torture_ww_mutex_2, &torture_ww_class);
  480. ww_acquire_ctxs = kmalloc_array(cxt.nrealwriters_stress,
  481. sizeof(*ww_acquire_ctxs),
  482. GFP_KERNEL);
  483. if (!ww_acquire_ctxs)
  484. VERBOSE_TOROUT_STRING("ww_acquire_ctx: Out of memory");
  485. }
  486. static void torture_ww_mutex_exit(void)
  487. {
  488. kfree(ww_acquire_ctxs);
  489. }
  490. static int torture_ww_mutex_lock(int tid)
  491. __acquires(torture_ww_mutex_0)
  492. __acquires(torture_ww_mutex_1)
  493. __acquires(torture_ww_mutex_2)
  494. {
  495. LIST_HEAD(list);
  496. struct reorder_lock {
  497. struct list_head link;
  498. struct ww_mutex *lock;
  499. } locks[3], *ll, *ln;
  500. struct ww_acquire_ctx *ctx = &ww_acquire_ctxs[tid];
  501. locks[0].lock = &torture_ww_mutex_0;
  502. list_add(&locks[0].link, &list);
  503. locks[1].lock = &torture_ww_mutex_1;
  504. list_add(&locks[1].link, &list);
  505. locks[2].lock = &torture_ww_mutex_2;
  506. list_add(&locks[2].link, &list);
  507. ww_acquire_init(ctx, &torture_ww_class);
  508. list_for_each_entry(ll, &list, link) {
  509. int err;
  510. err = ww_mutex_lock(ll->lock, ctx);
  511. if (!err)
  512. continue;
  513. ln = ll;
  514. list_for_each_entry_continue_reverse(ln, &list, link)
  515. ww_mutex_unlock(ln->lock);
  516. if (err != -EDEADLK)
  517. return err;
  518. ww_mutex_lock_slow(ll->lock, ctx);
  519. list_move(&ll->link, &list);
  520. }
  521. return 0;
  522. }
  523. static void torture_ww_mutex_unlock(int tid)
  524. __releases(torture_ww_mutex_0)
  525. __releases(torture_ww_mutex_1)
  526. __releases(torture_ww_mutex_2)
  527. {
  528. struct ww_acquire_ctx *ctx = &ww_acquire_ctxs[tid];
  529. ww_mutex_unlock(&torture_ww_mutex_0);
  530. ww_mutex_unlock(&torture_ww_mutex_1);
  531. ww_mutex_unlock(&torture_ww_mutex_2);
  532. ww_acquire_fini(ctx);
  533. }
  534. static struct lock_torture_ops ww_mutex_lock_ops = {
  535. .init = torture_ww_mutex_init,
  536. .exit = torture_ww_mutex_exit,
  537. .writelock = torture_ww_mutex_lock,
  538. .write_delay = torture_mutex_delay,
  539. .task_boost = torture_rt_boost,
  540. .writeunlock = torture_ww_mutex_unlock,
  541. .readlock = NULL,
  542. .read_delay = NULL,
  543. .readunlock = NULL,
  544. .name = "ww_mutex_lock"
  545. };
  546. #ifdef CONFIG_RT_MUTEXES
  547. static DEFINE_RT_MUTEX(torture_rtmutex);
  548. static struct rt_mutex torture_nested_rtmutexes[MAX_NESTED_LOCKS];
  549. static struct lock_class_key nested_rtmutex_keys[MAX_NESTED_LOCKS];
  550. static void torture_rtmutex_init(void)
  551. {
  552. int i;
  553. for (i = 0; i < MAX_NESTED_LOCKS; i++)
  554. __rt_mutex_init(&torture_nested_rtmutexes[i], __func__,
  555. &nested_rtmutex_keys[i]);
  556. }
  557. static int torture_rtmutex_nested_lock(int tid __maybe_unused,
  558. u32 lockset)
  559. {
  560. int i;
  561. for (i = 0; i < nested_locks; i++)
  562. if (lockset & (1 << i))
  563. rt_mutex_lock(&torture_nested_rtmutexes[i]);
  564. return 0;
  565. }
  566. static int torture_rtmutex_lock(int tid __maybe_unused)
  567. __acquires(torture_rtmutex)
  568. {
  569. rt_mutex_lock(&torture_rtmutex);
  570. return 0;
  571. }
  572. static void torture_rtmutex_delay(struct torture_random_state *trsp)
  573. {
  574. const unsigned long shortdelay_us = 2;
  575. /*
  576. * We want a short delay mostly to emulate likely code, and
  577. * we want a long delay occasionally to force massive contention.
  578. */
  579. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
  580. mdelay(long_hold);
  581. if (!(torture_random(trsp) %
  582. (cxt.nrealwriters_stress * 200 * shortdelay_us)))
  583. udelay(shortdelay_us);
  584. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  585. torture_preempt_schedule(); /* Allow test to be preempted. */
  586. }
  587. static void torture_rtmutex_unlock(int tid __maybe_unused)
  588. __releases(torture_rtmutex)
  589. {
  590. rt_mutex_unlock(&torture_rtmutex);
  591. }
  592. static void torture_rt_boost_rtmutex(struct torture_random_state *trsp)
  593. {
  594. if (!rt_boost)
  595. return;
  596. __torture_rt_boost(trsp);
  597. }
  598. static void torture_rtmutex_nested_unlock(int tid __maybe_unused,
  599. u32 lockset)
  600. {
  601. int i;
  602. for (i = nested_locks - 1; i >= 0; i--)
  603. if (lockset & (1 << i))
  604. rt_mutex_unlock(&torture_nested_rtmutexes[i]);
  605. }
  606. static struct lock_torture_ops rtmutex_lock_ops = {
  607. .init = torture_rtmutex_init,
  608. .nested_lock = torture_rtmutex_nested_lock,
  609. .writelock = torture_rtmutex_lock,
  610. .write_delay = torture_rtmutex_delay,
  611. .task_boost = torture_rt_boost_rtmutex,
  612. .writeunlock = torture_rtmutex_unlock,
  613. .nested_unlock = torture_rtmutex_nested_unlock,
  614. .readlock = NULL,
  615. .read_delay = NULL,
  616. .readunlock = NULL,
  617. .name = "rtmutex_lock"
  618. };
  619. #endif
  620. static DECLARE_RWSEM(torture_rwsem);
  621. static int torture_rwsem_down_write(int tid __maybe_unused)
  622. __acquires(torture_rwsem)
  623. {
  624. down_write(&torture_rwsem);
  625. return 0;
  626. }
  627. static void torture_rwsem_write_delay(struct torture_random_state *trsp)
  628. {
  629. /* We want a long delay occasionally to force massive contention. */
  630. if (long_hold && !(torture_random(trsp) % (cxt.nrealwriters_stress * 2000 * long_hold)))
  631. mdelay(long_hold * 10);
  632. if (!(torture_random(trsp) % (cxt.nrealwriters_stress * 20000)))
  633. torture_preempt_schedule(); /* Allow test to be preempted. */
  634. }
  635. static void torture_rwsem_up_write(int tid __maybe_unused)
  636. __releases(torture_rwsem)
  637. {
  638. up_write(&torture_rwsem);
  639. }
  640. static int torture_rwsem_down_read(int tid __maybe_unused)
  641. __acquires(torture_rwsem)
  642. {
  643. down_read(&torture_rwsem);
  644. return 0;
  645. }
  646. static void torture_rwsem_read_delay(struct torture_random_state *trsp)
  647. {
  648. /* We want a long delay occasionally to force massive contention. */
  649. if (long_hold && !(torture_random(trsp) % (cxt.nrealreaders_stress * 2000 * long_hold)))
  650. mdelay(long_hold * 2);
  651. else
  652. mdelay(long_hold / 2);
  653. if (!(torture_random(trsp) % (cxt.nrealreaders_stress * 20000)))
  654. torture_preempt_schedule(); /* Allow test to be preempted. */
  655. }
  656. static void torture_rwsem_up_read(int tid __maybe_unused)
  657. __releases(torture_rwsem)
  658. {
  659. up_read(&torture_rwsem);
  660. }
  661. static struct lock_torture_ops rwsem_lock_ops = {
  662. .writelock = torture_rwsem_down_write,
  663. .write_delay = torture_rwsem_write_delay,
  664. .task_boost = torture_rt_boost,
  665. .writeunlock = torture_rwsem_up_write,
  666. .readlock = torture_rwsem_down_read,
  667. .read_delay = torture_rwsem_read_delay,
  668. .readunlock = torture_rwsem_up_read,
  669. .name = "rwsem_lock"
  670. };
  671. #include <linux/percpu-rwsem.h>
  672. static struct percpu_rw_semaphore pcpu_rwsem;
  673. static void torture_percpu_rwsem_init(void)
  674. {
  675. BUG_ON(percpu_init_rwsem(&pcpu_rwsem));
  676. }
  677. static void torture_percpu_rwsem_exit(void)
  678. {
  679. percpu_free_rwsem(&pcpu_rwsem);
  680. }
  681. static int torture_percpu_rwsem_down_write(int tid __maybe_unused)
  682. __acquires(pcpu_rwsem)
  683. {
  684. percpu_down_write(&pcpu_rwsem);
  685. return 0;
  686. }
  687. static void torture_percpu_rwsem_up_write(int tid __maybe_unused)
  688. __releases(pcpu_rwsem)
  689. {
  690. percpu_up_write(&pcpu_rwsem);
  691. }
  692. static int torture_percpu_rwsem_down_read(int tid __maybe_unused)
  693. __acquires(pcpu_rwsem)
  694. {
  695. percpu_down_read(&pcpu_rwsem);
  696. return 0;
  697. }
  698. static void torture_percpu_rwsem_up_read(int tid __maybe_unused)
  699. __releases(pcpu_rwsem)
  700. {
  701. percpu_up_read(&pcpu_rwsem);
  702. }
  703. static struct lock_torture_ops percpu_rwsem_lock_ops = {
  704. .init = torture_percpu_rwsem_init,
  705. .exit = torture_percpu_rwsem_exit,
  706. .writelock = torture_percpu_rwsem_down_write,
  707. .write_delay = torture_rwsem_write_delay,
  708. .task_boost = torture_rt_boost,
  709. .writeunlock = torture_percpu_rwsem_up_write,
  710. .readlock = torture_percpu_rwsem_down_read,
  711. .read_delay = torture_rwsem_read_delay,
  712. .readunlock = torture_percpu_rwsem_up_read,
  713. .name = "percpu_rwsem_lock"
  714. };
  715. /*
  716. * Lock torture writer kthread. Repeatedly acquires and releases
  717. * the lock, checking for duplicate acquisitions.
  718. */
  719. static int lock_torture_writer(void *arg)
  720. {
  721. unsigned long j;
  722. unsigned long j1;
  723. u32 lockset_mask;
  724. struct lock_stress_stats *lwsp = arg;
  725. DEFINE_TORTURE_RANDOM(rand);
  726. bool skip_main_lock;
  727. int tid = lwsp - cxt.lwsa;
  728. VERBOSE_TOROUT_STRING("lock_torture_writer task started");
  729. if (!rt_task(current))
  730. set_user_nice(current, MAX_NICE);
  731. do {
  732. if ((torture_random(&rand) & 0xfffff) == 0)
  733. schedule_timeout_uninterruptible(1);
  734. lockset_mask = torture_random(&rand);
  735. /*
  736. * When using nested_locks, we want to occasionally
  737. * skip the main lock so we can avoid always serializing
  738. * the lock chains on that central lock. By skipping the
  739. * main lock occasionally, we can create different
  740. * contention patterns (allowing for multiple disjoint
  741. * blocked trees)
  742. */
  743. skip_main_lock = (nested_locks &&
  744. !(torture_random(&rand) % 100));
  745. cxt.cur_ops->task_boost(&rand);
  746. if (cxt.cur_ops->nested_lock)
  747. cxt.cur_ops->nested_lock(tid, lockset_mask);
  748. if (!skip_main_lock) {
  749. if (acq_writer_lim > 0)
  750. j = jiffies;
  751. cxt.cur_ops->writelock(tid);
  752. if (WARN_ON_ONCE(lock_is_write_held))
  753. lwsp->n_lock_fail++;
  754. lock_is_write_held = true;
  755. if (WARN_ON_ONCE(atomic_read(&lock_is_read_held)))
  756. lwsp->n_lock_fail++; /* rare, but... */
  757. if (acq_writer_lim > 0) {
  758. j1 = jiffies;
  759. WARN_ONCE(time_after(j1, j + acq_writer_lim),
  760. "%s: Lock acquisition took %lu jiffies.\n",
  761. __func__, j1 - j);
  762. }
  763. lwsp->n_lock_acquired++;
  764. cxt.cur_ops->write_delay(&rand);
  765. lock_is_write_held = false;
  766. WRITE_ONCE(last_lock_release, jiffies);
  767. cxt.cur_ops->writeunlock(tid);
  768. }
  769. if (cxt.cur_ops->nested_unlock)
  770. cxt.cur_ops->nested_unlock(tid, lockset_mask);
  771. stutter_wait("lock_torture_writer");
  772. } while (!torture_must_stop());
  773. cxt.cur_ops->task_boost(NULL); /* reset prio */
  774. torture_kthread_stopping("lock_torture_writer");
  775. return 0;
  776. }
  777. /*
  778. * Lock torture reader kthread. Repeatedly acquires and releases
  779. * the reader lock.
  780. */
  781. static int lock_torture_reader(void *arg)
  782. {
  783. struct lock_stress_stats *lrsp = arg;
  784. int tid = lrsp - cxt.lrsa;
  785. DEFINE_TORTURE_RANDOM(rand);
  786. VERBOSE_TOROUT_STRING("lock_torture_reader task started");
  787. set_user_nice(current, MAX_NICE);
  788. do {
  789. if ((torture_random(&rand) & 0xfffff) == 0)
  790. schedule_timeout_uninterruptible(1);
  791. cxt.cur_ops->readlock(tid);
  792. atomic_inc(&lock_is_read_held);
  793. if (WARN_ON_ONCE(lock_is_write_held))
  794. lrsp->n_lock_fail++; /* rare, but... */
  795. lrsp->n_lock_acquired++;
  796. cxt.cur_ops->read_delay(&rand);
  797. atomic_dec(&lock_is_read_held);
  798. cxt.cur_ops->readunlock(tid);
  799. stutter_wait("lock_torture_reader");
  800. } while (!torture_must_stop());
  801. torture_kthread_stopping("lock_torture_reader");
  802. return 0;
  803. }
  804. /*
  805. * Create an lock-torture-statistics message in the specified buffer.
  806. */
  807. static void __torture_print_stats(char *page,
  808. struct lock_stress_stats *statp, bool write)
  809. {
  810. long cur;
  811. bool fail = false;
  812. int i, n_stress;
  813. long max = 0, min = statp ? data_race(statp[0].n_lock_acquired) : 0;
  814. long long sum = 0;
  815. n_stress = write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress;
  816. for (i = 0; i < n_stress; i++) {
  817. if (data_race(statp[i].n_lock_fail))
  818. fail = true;
  819. cur = data_race(statp[i].n_lock_acquired);
  820. sum += cur;
  821. if (max < cur)
  822. max = cur;
  823. if (min > cur)
  824. min = cur;
  825. }
  826. page += sprintf(page,
  827. "%s: Total: %lld Max/Min: %ld/%ld %s Fail: %d %s\n",
  828. write ? "Writes" : "Reads ",
  829. sum, max, min,
  830. !onoff_interval && max / 2 > min ? "???" : "",
  831. fail, fail ? "!!!" : "");
  832. if (fail)
  833. atomic_inc(&cxt.n_lock_torture_errors);
  834. }
  835. /*
  836. * Print torture statistics. Caller must ensure that there is only one
  837. * call to this function at a given time!!! This is normally accomplished
  838. * by relying on the module system to only have one copy of the module
  839. * loaded, and then by giving the lock_torture_stats kthread full control
  840. * (or the init/cleanup functions when lock_torture_stats thread is not
  841. * running).
  842. */
  843. static void lock_torture_stats_print(void)
  844. {
  845. int size = cxt.nrealwriters_stress * 200 + 8192;
  846. char *buf;
  847. if (cxt.cur_ops->readlock)
  848. size += cxt.nrealreaders_stress * 200 + 8192;
  849. buf = kmalloc(size, GFP_KERNEL);
  850. if (!buf) {
  851. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  852. size);
  853. return;
  854. }
  855. __torture_print_stats(buf, cxt.lwsa, true);
  856. pr_alert("%s", buf);
  857. kfree(buf);
  858. if (cxt.cur_ops->readlock) {
  859. buf = kmalloc(size, GFP_KERNEL);
  860. if (!buf) {
  861. pr_err("lock_torture_stats_print: Out of memory, need: %d",
  862. size);
  863. return;
  864. }
  865. __torture_print_stats(buf, cxt.lrsa, false);
  866. pr_alert("%s", buf);
  867. kfree(buf);
  868. }
  869. }
  870. /*
  871. * Periodically prints torture statistics, if periodic statistics printing
  872. * was specified via the stat_interval module parameter.
  873. *
  874. * No need to worry about fullstop here, since this one doesn't reference
  875. * volatile state or register callbacks.
  876. */
  877. static int lock_torture_stats(void *arg)
  878. {
  879. VERBOSE_TOROUT_STRING("lock_torture_stats task started");
  880. do {
  881. schedule_timeout_interruptible(stat_interval * HZ);
  882. lock_torture_stats_print();
  883. torture_shutdown_absorb("lock_torture_stats");
  884. } while (!torture_must_stop());
  885. torture_kthread_stopping("lock_torture_stats");
  886. return 0;
  887. }
  888. static inline void
  889. lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
  890. const char *tag)
  891. {
  892. static cpumask_t cpumask_all;
  893. cpumask_t *rcmp = cpumask_nonempty(bind_readers) ? bind_readers : &cpumask_all;
  894. cpumask_t *wcmp = cpumask_nonempty(bind_writers) ? bind_writers : &cpumask_all;
  895. cpumask_setall(&cpumask_all);
  896. pr_alert("%s" TORTURE_FLAG
  897. "--- %s%s: acq_writer_lim=%d bind_readers=%*pbl bind_writers=%*pbl call_rcu_chains=%d long_hold=%d nested_locks=%d nreaders_stress=%d nwriters_stress=%d onoff_holdoff=%d onoff_interval=%d rt_boost=%d rt_boost_factor=%d shuffle_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d verbose=%d writer_fifo=%d\n",
  898. torture_type, tag, cxt.debug_lock ? " [debug]": "",
  899. acq_writer_lim, cpumask_pr_args(rcmp), cpumask_pr_args(wcmp),
  900. call_rcu_chains, long_hold, nested_locks, cxt.nrealreaders_stress,
  901. cxt.nrealwriters_stress, onoff_holdoff, onoff_interval, rt_boost,
  902. rt_boost_factor, shuffle_interval, shutdown_secs, stat_interval, stutter,
  903. verbose, writer_fifo);
  904. }
  905. // If requested, maintain call_rcu() chains to keep a grace period always
  906. // in flight. These increase the probability of getting an RCU CPU stall
  907. // warning and associated diagnostics when a locking primitive stalls.
  908. static void call_rcu_chain_cb(struct rcu_head *rhp)
  909. {
  910. struct call_rcu_chain *crcp = container_of(rhp, struct call_rcu_chain, crc_rh);
  911. if (!smp_load_acquire(&crcp->crc_stop)) {
  912. (void)start_poll_synchronize_rcu(); // Start one grace period...
  913. call_rcu(&crcp->crc_rh, call_rcu_chain_cb); // ... and later start another.
  914. }
  915. }
  916. // Start the requested number of call_rcu() chains.
  917. static int call_rcu_chain_init(void)
  918. {
  919. int i;
  920. if (call_rcu_chains <= 0)
  921. return 0;
  922. call_rcu_chain_list = kcalloc(call_rcu_chains, sizeof(*call_rcu_chain_list), GFP_KERNEL);
  923. if (!call_rcu_chain_list)
  924. return -ENOMEM;
  925. for (i = 0; i < call_rcu_chains; i++) {
  926. call_rcu_chain_list[i].crc_stop = false;
  927. call_rcu(&call_rcu_chain_list[i].crc_rh, call_rcu_chain_cb);
  928. }
  929. return 0;
  930. }
  931. // Stop all of the call_rcu() chains.
  932. static void call_rcu_chain_cleanup(void)
  933. {
  934. int i;
  935. if (!call_rcu_chain_list)
  936. return;
  937. for (i = 0; i < call_rcu_chains; i++)
  938. smp_store_release(&call_rcu_chain_list[i].crc_stop, true);
  939. rcu_barrier();
  940. kfree(call_rcu_chain_list);
  941. call_rcu_chain_list = NULL;
  942. }
  943. static void lock_torture_cleanup(void)
  944. {
  945. int i;
  946. if (torture_cleanup_begin())
  947. return;
  948. /*
  949. * Indicates early cleanup, meaning that the test has not run,
  950. * such as when passing bogus args when loading the module.
  951. * However cxt->cur_ops.init() may have been invoked, so beside
  952. * perform the underlying torture-specific cleanups, cur_ops.exit()
  953. * will be invoked if needed.
  954. */
  955. if (!cxt.lwsa && !cxt.lrsa)
  956. goto end;
  957. if (writer_tasks) {
  958. for (i = 0; i < cxt.nrealwriters_stress; i++)
  959. torture_stop_kthread(lock_torture_writer, writer_tasks[i]);
  960. kfree(writer_tasks);
  961. writer_tasks = NULL;
  962. }
  963. if (reader_tasks) {
  964. for (i = 0; i < cxt.nrealreaders_stress; i++)
  965. torture_stop_kthread(lock_torture_reader,
  966. reader_tasks[i]);
  967. kfree(reader_tasks);
  968. reader_tasks = NULL;
  969. }
  970. torture_stop_kthread(lock_torture_stats, stats_task);
  971. lock_torture_stats_print(); /* -After- the stats thread is stopped! */
  972. if (atomic_read(&cxt.n_lock_torture_errors))
  973. lock_torture_print_module_parms(cxt.cur_ops,
  974. "End of test: FAILURE");
  975. else if (torture_onoff_failures())
  976. lock_torture_print_module_parms(cxt.cur_ops,
  977. "End of test: LOCK_HOTPLUG");
  978. else
  979. lock_torture_print_module_parms(cxt.cur_ops,
  980. "End of test: SUCCESS");
  981. kfree(cxt.lwsa);
  982. cxt.lwsa = NULL;
  983. kfree(cxt.lrsa);
  984. cxt.lrsa = NULL;
  985. call_rcu_chain_cleanup();
  986. end:
  987. if (cxt.init_called) {
  988. if (cxt.cur_ops->exit)
  989. cxt.cur_ops->exit();
  990. cxt.init_called = false;
  991. }
  992. torture_cleanup_end();
  993. }
  994. static int __init lock_torture_init(void)
  995. {
  996. int i, j;
  997. int firsterr = 0;
  998. static struct lock_torture_ops *torture_ops[] = {
  999. &lock_busted_ops,
  1000. &spin_lock_ops, &spin_lock_irq_ops,
  1001. &raw_spin_lock_ops, &raw_spin_lock_irq_ops,
  1002. &rw_lock_ops, &rw_lock_irq_ops,
  1003. &mutex_lock_ops,
  1004. &ww_mutex_lock_ops,
  1005. #ifdef CONFIG_RT_MUTEXES
  1006. &rtmutex_lock_ops,
  1007. #endif
  1008. &rwsem_lock_ops,
  1009. &percpu_rwsem_lock_ops,
  1010. };
  1011. if (!torture_init_begin(torture_type, verbose))
  1012. return -EBUSY;
  1013. /* Process args and tell the world that the torturer is on the job. */
  1014. for (i = 0; i < ARRAY_SIZE(torture_ops); i++) {
  1015. cxt.cur_ops = torture_ops[i];
  1016. if (strcmp(torture_type, cxt.cur_ops->name) == 0)
  1017. break;
  1018. }
  1019. if (i == ARRAY_SIZE(torture_ops)) {
  1020. pr_alert("lock-torture: invalid torture type: \"%s\"\n",
  1021. torture_type);
  1022. pr_alert("lock-torture types:");
  1023. for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
  1024. pr_alert(" %s", torture_ops[i]->name);
  1025. pr_alert("\n");
  1026. firsterr = -EINVAL;
  1027. goto unwind;
  1028. }
  1029. if (nwriters_stress == 0 &&
  1030. (!cxt.cur_ops->readlock || nreaders_stress == 0)) {
  1031. pr_alert("lock-torture: must run at least one locking thread\n");
  1032. firsterr = -EINVAL;
  1033. goto unwind;
  1034. }
  1035. if (nwriters_stress >= 0)
  1036. cxt.nrealwriters_stress = nwriters_stress;
  1037. else
  1038. cxt.nrealwriters_stress = 2 * num_online_cpus();
  1039. if (cxt.cur_ops->init) {
  1040. cxt.cur_ops->init();
  1041. cxt.init_called = true;
  1042. }
  1043. #ifdef CONFIG_DEBUG_MUTEXES
  1044. if (str_has_prefix(torture_type, "mutex"))
  1045. cxt.debug_lock = true;
  1046. #endif
  1047. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1048. if (str_has_prefix(torture_type, "rtmutex"))
  1049. cxt.debug_lock = true;
  1050. #endif
  1051. #ifdef CONFIG_DEBUG_SPINLOCK
  1052. if ((str_has_prefix(torture_type, "spin")) ||
  1053. (str_has_prefix(torture_type, "rw_lock")))
  1054. cxt.debug_lock = true;
  1055. #endif
  1056. /* Initialize the statistics so that each run gets its own numbers. */
  1057. if (nwriters_stress) {
  1058. lock_is_write_held = false;
  1059. cxt.lwsa = kmalloc_array(cxt.nrealwriters_stress,
  1060. sizeof(*cxt.lwsa),
  1061. GFP_KERNEL);
  1062. if (cxt.lwsa == NULL) {
  1063. VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory");
  1064. firsterr = -ENOMEM;
  1065. goto unwind;
  1066. }
  1067. for (i = 0; i < cxt.nrealwriters_stress; i++) {
  1068. cxt.lwsa[i].n_lock_fail = 0;
  1069. cxt.lwsa[i].n_lock_acquired = 0;
  1070. }
  1071. }
  1072. if (cxt.cur_ops->readlock) {
  1073. if (nreaders_stress >= 0)
  1074. cxt.nrealreaders_stress = nreaders_stress;
  1075. else {
  1076. /*
  1077. * By default distribute evenly the number of
  1078. * readers and writers. We still run the same number
  1079. * of threads as the writer-only locks default.
  1080. */
  1081. if (nwriters_stress < 0) /* user doesn't care */
  1082. cxt.nrealwriters_stress = num_online_cpus();
  1083. cxt.nrealreaders_stress = cxt.nrealwriters_stress;
  1084. }
  1085. if (nreaders_stress) {
  1086. cxt.lrsa = kmalloc_array(cxt.nrealreaders_stress,
  1087. sizeof(*cxt.lrsa),
  1088. GFP_KERNEL);
  1089. if (cxt.lrsa == NULL) {
  1090. VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
  1091. firsterr = -ENOMEM;
  1092. kfree(cxt.lwsa);
  1093. cxt.lwsa = NULL;
  1094. goto unwind;
  1095. }
  1096. for (i = 0; i < cxt.nrealreaders_stress; i++) {
  1097. cxt.lrsa[i].n_lock_fail = 0;
  1098. cxt.lrsa[i].n_lock_acquired = 0;
  1099. }
  1100. }
  1101. }
  1102. firsterr = call_rcu_chain_init();
  1103. if (torture_init_error(firsterr))
  1104. goto unwind;
  1105. lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
  1106. /* Prepare torture context. */
  1107. if (onoff_interval > 0) {
  1108. firsterr = torture_onoff_init(onoff_holdoff * HZ,
  1109. onoff_interval * HZ, NULL);
  1110. if (torture_init_error(firsterr))
  1111. goto unwind;
  1112. }
  1113. if (shuffle_interval > 0) {
  1114. firsterr = torture_shuffle_init(shuffle_interval);
  1115. if (torture_init_error(firsterr))
  1116. goto unwind;
  1117. }
  1118. if (shutdown_secs > 0) {
  1119. firsterr = torture_shutdown_init(shutdown_secs,
  1120. lock_torture_cleanup);
  1121. if (torture_init_error(firsterr))
  1122. goto unwind;
  1123. }
  1124. if (stutter > 0) {
  1125. firsterr = torture_stutter_init(stutter, stutter);
  1126. if (torture_init_error(firsterr))
  1127. goto unwind;
  1128. }
  1129. if (nwriters_stress) {
  1130. writer_tasks = kcalloc(cxt.nrealwriters_stress,
  1131. sizeof(writer_tasks[0]),
  1132. GFP_KERNEL);
  1133. if (writer_tasks == NULL) {
  1134. TOROUT_ERRSTRING("writer_tasks: Out of memory");
  1135. firsterr = -ENOMEM;
  1136. goto unwind;
  1137. }
  1138. }
  1139. /* cap nested_locks to MAX_NESTED_LOCKS */
  1140. if (nested_locks > MAX_NESTED_LOCKS)
  1141. nested_locks = MAX_NESTED_LOCKS;
  1142. if (cxt.cur_ops->readlock) {
  1143. reader_tasks = kcalloc(cxt.nrealreaders_stress,
  1144. sizeof(reader_tasks[0]),
  1145. GFP_KERNEL);
  1146. if (reader_tasks == NULL) {
  1147. TOROUT_ERRSTRING("reader_tasks: Out of memory");
  1148. kfree(writer_tasks);
  1149. writer_tasks = NULL;
  1150. firsterr = -ENOMEM;
  1151. goto unwind;
  1152. }
  1153. }
  1154. /*
  1155. * Create the kthreads and start torturing (oh, those poor little locks).
  1156. *
  1157. * TODO: Note that we interleave writers with readers, giving writers a
  1158. * slight advantage, by creating its kthread first. This can be modified
  1159. * for very specific needs, or even let the user choose the policy, if
  1160. * ever wanted.
  1161. */
  1162. for (i = 0, j = 0; i < cxt.nrealwriters_stress ||
  1163. j < cxt.nrealreaders_stress; i++, j++) {
  1164. if (i >= cxt.nrealwriters_stress)
  1165. goto create_reader;
  1166. /* Create writer. */
  1167. firsterr = torture_create_kthread_cb(lock_torture_writer, &cxt.lwsa[i],
  1168. writer_tasks[i],
  1169. writer_fifo ? sched_set_fifo : NULL);
  1170. if (torture_init_error(firsterr))
  1171. goto unwind;
  1172. if (cpumask_nonempty(bind_writers))
  1173. torture_sched_setaffinity(writer_tasks[i]->pid, bind_writers);
  1174. create_reader:
  1175. if (cxt.cur_ops->readlock == NULL || (j >= cxt.nrealreaders_stress))
  1176. continue;
  1177. /* Create reader. */
  1178. firsterr = torture_create_kthread(lock_torture_reader, &cxt.lrsa[j],
  1179. reader_tasks[j]);
  1180. if (torture_init_error(firsterr))
  1181. goto unwind;
  1182. if (cpumask_nonempty(bind_readers))
  1183. torture_sched_setaffinity(reader_tasks[j]->pid, bind_readers);
  1184. }
  1185. if (stat_interval > 0) {
  1186. firsterr = torture_create_kthread(lock_torture_stats, NULL,
  1187. stats_task);
  1188. if (torture_init_error(firsterr))
  1189. goto unwind;
  1190. }
  1191. torture_init_end();
  1192. return 0;
  1193. unwind:
  1194. torture_init_end();
  1195. lock_torture_cleanup();
  1196. if (shutdown_secs) {
  1197. WARN_ON(!IS_MODULE(CONFIG_LOCK_TORTURE_TEST));
  1198. kernel_power_off();
  1199. }
  1200. return firsterr;
  1201. }
  1202. module_init(lock_torture_init);
  1203. module_exit(lock_torture_cleanup);