refscale.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Scalability test comparing RCU vs other mechanisms
  4. // for acquiring references on objects.
  5. //
  6. // Copyright (C) Google, 2020.
  7. //
  8. // Author: Joel Fernandes <joel@joelfernandes.org>
  9. #define pr_fmt(fmt) fmt
  10. #include <linux/atomic.h>
  11. #include <linux/bitops.h>
  12. #include <linux/completion.h>
  13. #include <linux/cpu.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kthread.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/notifier.h>
  24. #include <linux/percpu.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/rcupdate_trace.h>
  27. #include <linux/reboot.h>
  28. #include <linux/sched.h>
  29. #include <linux/seq_buf.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/smp.h>
  32. #include <linux/stat.h>
  33. #include <linux/srcu.h>
  34. #include <linux/slab.h>
  35. #include <linux/torture.h>
  36. #include <linux/types.h>
  37. #include "rcu.h"
  38. #define SCALE_FLAG "-ref-scale: "
  39. #define SCALEOUT(s, x...) \
  40. pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
  41. #define VERBOSE_SCALEOUT(s, x...) \
  42. do { \
  43. if (verbose) \
  44. pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
  45. } while (0)
  46. static atomic_t verbose_batch_ctr;
  47. #define VERBOSE_SCALEOUT_BATCH(s, x...) \
  48. do { \
  49. if (verbose && \
  50. (verbose_batched <= 0 || \
  51. !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \
  52. schedule_timeout_uninterruptible(1); \
  53. pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
  54. } \
  55. } while (0)
  56. #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x)
  57. MODULE_DESCRIPTION("Scalability test for object reference mechanisms");
  58. MODULE_LICENSE("GPL");
  59. MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
  60. static char *scale_type = "rcu";
  61. module_param(scale_type, charp, 0444);
  62. MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
  63. torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
  64. torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
  65. // Wait until there are multiple CPUs before starting test.
  66. torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
  67. "Holdoff time before test start (s)");
  68. // Number of typesafe_lookup structures, that is, the degree of concurrency.
  69. torture_param(long, lookup_instances, 0, "Number of typesafe_lookup structures.");
  70. // Number of loops per experiment, all readers execute operations concurrently.
  71. torture_param(int, loops, 10000, "Number of loops per experiment.");
  72. // Number of readers, with -1 defaulting to about 75% of the CPUs.
  73. torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
  74. // Number of runs.
  75. torture_param(int, nruns, 30, "Number of experiments to run.");
  76. // Reader delay in nanoseconds, 0 for no delay.
  77. torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
  78. #ifdef MODULE
  79. # define REFSCALE_SHUTDOWN 0
  80. #else
  81. # define REFSCALE_SHUTDOWN 1
  82. #endif
  83. torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
  84. "Shutdown at end of scalability tests.");
  85. struct reader_task {
  86. struct task_struct *task;
  87. int start_reader;
  88. wait_queue_head_t wq;
  89. u64 last_duration_ns;
  90. };
  91. static struct task_struct *shutdown_task;
  92. static wait_queue_head_t shutdown_wq;
  93. static struct task_struct *main_task;
  94. static wait_queue_head_t main_wq;
  95. static int shutdown_start;
  96. static struct reader_task *reader_tasks;
  97. // Number of readers that are part of the current experiment.
  98. static atomic_t nreaders_exp;
  99. // Use to wait for all threads to start.
  100. static atomic_t n_init;
  101. static atomic_t n_started;
  102. static atomic_t n_warmedup;
  103. static atomic_t n_cooleddown;
  104. // Track which experiment is currently running.
  105. static int exp_idx;
  106. // Operations vector for selecting different types of tests.
  107. struct ref_scale_ops {
  108. bool (*init)(void);
  109. void (*cleanup)(void);
  110. void (*readsection)(const int nloops);
  111. void (*delaysection)(const int nloops, const int udl, const int ndl);
  112. const char *name;
  113. };
  114. static const struct ref_scale_ops *cur_ops;
  115. static void un_delay(const int udl, const int ndl)
  116. {
  117. if (udl)
  118. udelay(udl);
  119. if (ndl)
  120. ndelay(ndl);
  121. }
  122. static void ref_rcu_read_section(const int nloops)
  123. {
  124. int i;
  125. for (i = nloops; i >= 0; i--) {
  126. rcu_read_lock();
  127. rcu_read_unlock();
  128. }
  129. }
  130. static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
  131. {
  132. int i;
  133. for (i = nloops; i >= 0; i--) {
  134. rcu_read_lock();
  135. un_delay(udl, ndl);
  136. rcu_read_unlock();
  137. }
  138. }
  139. static bool rcu_sync_scale_init(void)
  140. {
  141. return true;
  142. }
  143. static const struct ref_scale_ops rcu_ops = {
  144. .init = rcu_sync_scale_init,
  145. .readsection = ref_rcu_read_section,
  146. .delaysection = ref_rcu_delay_section,
  147. .name = "rcu"
  148. };
  149. // Definitions for SRCU ref scale testing.
  150. DEFINE_STATIC_SRCU(srcu_refctl_scale);
  151. static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
  152. static void srcu_ref_scale_read_section(const int nloops)
  153. {
  154. int i;
  155. int idx;
  156. for (i = nloops; i >= 0; i--) {
  157. idx = srcu_read_lock(srcu_ctlp);
  158. srcu_read_unlock(srcu_ctlp, idx);
  159. }
  160. }
  161. static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
  162. {
  163. int i;
  164. int idx;
  165. for (i = nloops; i >= 0; i--) {
  166. idx = srcu_read_lock(srcu_ctlp);
  167. un_delay(udl, ndl);
  168. srcu_read_unlock(srcu_ctlp, idx);
  169. }
  170. }
  171. static const struct ref_scale_ops srcu_ops = {
  172. .init = rcu_sync_scale_init,
  173. .readsection = srcu_ref_scale_read_section,
  174. .delaysection = srcu_ref_scale_delay_section,
  175. .name = "srcu"
  176. };
  177. #ifdef CONFIG_TASKS_RCU
  178. // Definitions for RCU Tasks ref scale testing: Empty read markers.
  179. // These definitions also work for RCU Rude readers.
  180. static void rcu_tasks_ref_scale_read_section(const int nloops)
  181. {
  182. int i;
  183. for (i = nloops; i >= 0; i--)
  184. continue;
  185. }
  186. static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
  187. {
  188. int i;
  189. for (i = nloops; i >= 0; i--)
  190. un_delay(udl, ndl);
  191. }
  192. static const struct ref_scale_ops rcu_tasks_ops = {
  193. .init = rcu_sync_scale_init,
  194. .readsection = rcu_tasks_ref_scale_read_section,
  195. .delaysection = rcu_tasks_ref_scale_delay_section,
  196. .name = "rcu-tasks"
  197. };
  198. #define RCU_TASKS_OPS &rcu_tasks_ops,
  199. #else // #ifdef CONFIG_TASKS_RCU
  200. #define RCU_TASKS_OPS
  201. #endif // #else // #ifdef CONFIG_TASKS_RCU
  202. #ifdef CONFIG_TASKS_TRACE_RCU
  203. // Definitions for RCU Tasks Trace ref scale testing.
  204. static void rcu_trace_ref_scale_read_section(const int nloops)
  205. {
  206. int i;
  207. for (i = nloops; i >= 0; i--) {
  208. rcu_read_lock_trace();
  209. rcu_read_unlock_trace();
  210. }
  211. }
  212. static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
  213. {
  214. int i;
  215. for (i = nloops; i >= 0; i--) {
  216. rcu_read_lock_trace();
  217. un_delay(udl, ndl);
  218. rcu_read_unlock_trace();
  219. }
  220. }
  221. static const struct ref_scale_ops rcu_trace_ops = {
  222. .init = rcu_sync_scale_init,
  223. .readsection = rcu_trace_ref_scale_read_section,
  224. .delaysection = rcu_trace_ref_scale_delay_section,
  225. .name = "rcu-trace"
  226. };
  227. #define RCU_TRACE_OPS &rcu_trace_ops,
  228. #else // #ifdef CONFIG_TASKS_TRACE_RCU
  229. #define RCU_TRACE_OPS
  230. #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
  231. // Definitions for reference count
  232. static atomic_t refcnt;
  233. static void ref_refcnt_section(const int nloops)
  234. {
  235. int i;
  236. for (i = nloops; i >= 0; i--) {
  237. atomic_inc(&refcnt);
  238. atomic_dec(&refcnt);
  239. }
  240. }
  241. static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
  242. {
  243. int i;
  244. for (i = nloops; i >= 0; i--) {
  245. atomic_inc(&refcnt);
  246. un_delay(udl, ndl);
  247. atomic_dec(&refcnt);
  248. }
  249. }
  250. static const struct ref_scale_ops refcnt_ops = {
  251. .init = rcu_sync_scale_init,
  252. .readsection = ref_refcnt_section,
  253. .delaysection = ref_refcnt_delay_section,
  254. .name = "refcnt"
  255. };
  256. // Definitions for rwlock
  257. static rwlock_t test_rwlock;
  258. static bool ref_rwlock_init(void)
  259. {
  260. rwlock_init(&test_rwlock);
  261. return true;
  262. }
  263. static void ref_rwlock_section(const int nloops)
  264. {
  265. int i;
  266. for (i = nloops; i >= 0; i--) {
  267. read_lock(&test_rwlock);
  268. read_unlock(&test_rwlock);
  269. }
  270. }
  271. static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
  272. {
  273. int i;
  274. for (i = nloops; i >= 0; i--) {
  275. read_lock(&test_rwlock);
  276. un_delay(udl, ndl);
  277. read_unlock(&test_rwlock);
  278. }
  279. }
  280. static const struct ref_scale_ops rwlock_ops = {
  281. .init = ref_rwlock_init,
  282. .readsection = ref_rwlock_section,
  283. .delaysection = ref_rwlock_delay_section,
  284. .name = "rwlock"
  285. };
  286. // Definitions for rwsem
  287. static struct rw_semaphore test_rwsem;
  288. static bool ref_rwsem_init(void)
  289. {
  290. init_rwsem(&test_rwsem);
  291. return true;
  292. }
  293. static void ref_rwsem_section(const int nloops)
  294. {
  295. int i;
  296. for (i = nloops; i >= 0; i--) {
  297. down_read(&test_rwsem);
  298. up_read(&test_rwsem);
  299. }
  300. }
  301. static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
  302. {
  303. int i;
  304. for (i = nloops; i >= 0; i--) {
  305. down_read(&test_rwsem);
  306. un_delay(udl, ndl);
  307. up_read(&test_rwsem);
  308. }
  309. }
  310. static const struct ref_scale_ops rwsem_ops = {
  311. .init = ref_rwsem_init,
  312. .readsection = ref_rwsem_section,
  313. .delaysection = ref_rwsem_delay_section,
  314. .name = "rwsem"
  315. };
  316. // Definitions for global spinlock
  317. static DEFINE_RAW_SPINLOCK(test_lock);
  318. static void ref_lock_section(const int nloops)
  319. {
  320. int i;
  321. preempt_disable();
  322. for (i = nloops; i >= 0; i--) {
  323. raw_spin_lock(&test_lock);
  324. raw_spin_unlock(&test_lock);
  325. }
  326. preempt_enable();
  327. }
  328. static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
  329. {
  330. int i;
  331. preempt_disable();
  332. for (i = nloops; i >= 0; i--) {
  333. raw_spin_lock(&test_lock);
  334. un_delay(udl, ndl);
  335. raw_spin_unlock(&test_lock);
  336. }
  337. preempt_enable();
  338. }
  339. static const struct ref_scale_ops lock_ops = {
  340. .readsection = ref_lock_section,
  341. .delaysection = ref_lock_delay_section,
  342. .name = "lock"
  343. };
  344. // Definitions for global irq-save spinlock
  345. static void ref_lock_irq_section(const int nloops)
  346. {
  347. unsigned long flags;
  348. int i;
  349. preempt_disable();
  350. for (i = nloops; i >= 0; i--) {
  351. raw_spin_lock_irqsave(&test_lock, flags);
  352. raw_spin_unlock_irqrestore(&test_lock, flags);
  353. }
  354. preempt_enable();
  355. }
  356. static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
  357. {
  358. unsigned long flags;
  359. int i;
  360. preempt_disable();
  361. for (i = nloops; i >= 0; i--) {
  362. raw_spin_lock_irqsave(&test_lock, flags);
  363. un_delay(udl, ndl);
  364. raw_spin_unlock_irqrestore(&test_lock, flags);
  365. }
  366. preempt_enable();
  367. }
  368. static const struct ref_scale_ops lock_irq_ops = {
  369. .readsection = ref_lock_irq_section,
  370. .delaysection = ref_lock_irq_delay_section,
  371. .name = "lock-irq"
  372. };
  373. // Definitions acquire-release.
  374. static DEFINE_PER_CPU(unsigned long, test_acqrel);
  375. static void ref_acqrel_section(const int nloops)
  376. {
  377. unsigned long x;
  378. int i;
  379. preempt_disable();
  380. for (i = nloops; i >= 0; i--) {
  381. x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
  382. smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
  383. }
  384. preempt_enable();
  385. }
  386. static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
  387. {
  388. unsigned long x;
  389. int i;
  390. preempt_disable();
  391. for (i = nloops; i >= 0; i--) {
  392. x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
  393. un_delay(udl, ndl);
  394. smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
  395. }
  396. preempt_enable();
  397. }
  398. static const struct ref_scale_ops acqrel_ops = {
  399. .readsection = ref_acqrel_section,
  400. .delaysection = ref_acqrel_delay_section,
  401. .name = "acqrel"
  402. };
  403. static volatile u64 stopopts;
  404. static void ref_clock_section(const int nloops)
  405. {
  406. u64 x = 0;
  407. int i;
  408. preempt_disable();
  409. for (i = nloops; i >= 0; i--)
  410. x += ktime_get_real_fast_ns();
  411. preempt_enable();
  412. stopopts = x;
  413. }
  414. static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
  415. {
  416. u64 x = 0;
  417. int i;
  418. preempt_disable();
  419. for (i = nloops; i >= 0; i--) {
  420. x += ktime_get_real_fast_ns();
  421. un_delay(udl, ndl);
  422. }
  423. preempt_enable();
  424. stopopts = x;
  425. }
  426. static const struct ref_scale_ops clock_ops = {
  427. .readsection = ref_clock_section,
  428. .delaysection = ref_clock_delay_section,
  429. .name = "clock"
  430. };
  431. static void ref_jiffies_section(const int nloops)
  432. {
  433. u64 x = 0;
  434. int i;
  435. preempt_disable();
  436. for (i = nloops; i >= 0; i--)
  437. x += jiffies;
  438. preempt_enable();
  439. stopopts = x;
  440. }
  441. static void ref_jiffies_delay_section(const int nloops, const int udl, const int ndl)
  442. {
  443. u64 x = 0;
  444. int i;
  445. preempt_disable();
  446. for (i = nloops; i >= 0; i--) {
  447. x += jiffies;
  448. un_delay(udl, ndl);
  449. }
  450. preempt_enable();
  451. stopopts = x;
  452. }
  453. static const struct ref_scale_ops jiffies_ops = {
  454. .readsection = ref_jiffies_section,
  455. .delaysection = ref_jiffies_delay_section,
  456. .name = "jiffies"
  457. };
  458. ////////////////////////////////////////////////////////////////////////
  459. //
  460. // Methods leveraging SLAB_TYPESAFE_BY_RCU.
  461. //
  462. // Item to look up in a typesafe manner. Array of pointers to these.
  463. struct refscale_typesafe {
  464. atomic_t rts_refctr; // Used by all flavors
  465. spinlock_t rts_lock;
  466. seqlock_t rts_seqlock;
  467. unsigned int a;
  468. unsigned int b;
  469. };
  470. static struct kmem_cache *typesafe_kmem_cachep;
  471. static struct refscale_typesafe **rtsarray;
  472. static long rtsarray_size;
  473. static DEFINE_TORTURE_RANDOM_PERCPU(refscale_rand);
  474. static bool (*rts_acquire)(struct refscale_typesafe *rtsp, unsigned int *start);
  475. static bool (*rts_release)(struct refscale_typesafe *rtsp, unsigned int start);
  476. // Conditionally acquire an explicit in-structure reference count.
  477. static bool typesafe_ref_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
  478. {
  479. return atomic_inc_not_zero(&rtsp->rts_refctr);
  480. }
  481. // Unconditionally release an explicit in-structure reference count.
  482. static bool typesafe_ref_release(struct refscale_typesafe *rtsp, unsigned int start)
  483. {
  484. if (!atomic_dec_return(&rtsp->rts_refctr)) {
  485. WRITE_ONCE(rtsp->a, rtsp->a + 1);
  486. kmem_cache_free(typesafe_kmem_cachep, rtsp);
  487. }
  488. return true;
  489. }
  490. // Unconditionally acquire an explicit in-structure spinlock.
  491. static bool typesafe_lock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
  492. {
  493. spin_lock(&rtsp->rts_lock);
  494. return true;
  495. }
  496. // Unconditionally release an explicit in-structure spinlock.
  497. static bool typesafe_lock_release(struct refscale_typesafe *rtsp, unsigned int start)
  498. {
  499. spin_unlock(&rtsp->rts_lock);
  500. return true;
  501. }
  502. // Unconditionally acquire an explicit in-structure sequence lock.
  503. static bool typesafe_seqlock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
  504. {
  505. *start = read_seqbegin(&rtsp->rts_seqlock);
  506. return true;
  507. }
  508. // Conditionally release an explicit in-structure sequence lock. Return
  509. // true if this release was successful, that is, if no retry is required.
  510. static bool typesafe_seqlock_release(struct refscale_typesafe *rtsp, unsigned int start)
  511. {
  512. return !read_seqretry(&rtsp->rts_seqlock, start);
  513. }
  514. // Do a read-side critical section with the specified delay in
  515. // microseconds and nanoseconds inserted so as to increase probability
  516. // of failure.
  517. static void typesafe_delay_section(const int nloops, const int udl, const int ndl)
  518. {
  519. unsigned int a;
  520. unsigned int b;
  521. int i;
  522. long idx;
  523. struct refscale_typesafe *rtsp;
  524. unsigned int start;
  525. for (i = nloops; i >= 0; i--) {
  526. preempt_disable();
  527. idx = torture_random(this_cpu_ptr(&refscale_rand)) % rtsarray_size;
  528. preempt_enable();
  529. retry:
  530. rcu_read_lock();
  531. rtsp = rcu_dereference(rtsarray[idx]);
  532. a = READ_ONCE(rtsp->a);
  533. if (!rts_acquire(rtsp, &start)) {
  534. rcu_read_unlock();
  535. goto retry;
  536. }
  537. if (a != READ_ONCE(rtsp->a)) {
  538. (void)rts_release(rtsp, start);
  539. rcu_read_unlock();
  540. goto retry;
  541. }
  542. un_delay(udl, ndl);
  543. b = READ_ONCE(rtsp->a);
  544. // Remember, seqlock read-side release can fail.
  545. if (!rts_release(rtsp, start)) {
  546. rcu_read_unlock();
  547. goto retry;
  548. }
  549. WARN_ONCE(a != b, "Re-read of ->a changed from %u to %u.\n", a, b);
  550. b = rtsp->b;
  551. rcu_read_unlock();
  552. WARN_ON_ONCE(a * a != b);
  553. }
  554. }
  555. // Because the acquisition and release methods are expensive, there
  556. // is no point in optimizing away the un_delay() function's two checks.
  557. // Thus simply define typesafe_read_section() as a simple wrapper around
  558. // typesafe_delay_section().
  559. static void typesafe_read_section(const int nloops)
  560. {
  561. typesafe_delay_section(nloops, 0, 0);
  562. }
  563. // Allocate and initialize one refscale_typesafe structure.
  564. static struct refscale_typesafe *typesafe_alloc_one(void)
  565. {
  566. struct refscale_typesafe *rtsp;
  567. rtsp = kmem_cache_alloc(typesafe_kmem_cachep, GFP_KERNEL);
  568. if (!rtsp)
  569. return NULL;
  570. atomic_set(&rtsp->rts_refctr, 1);
  571. WRITE_ONCE(rtsp->a, rtsp->a + 1);
  572. WRITE_ONCE(rtsp->b, rtsp->a * rtsp->a);
  573. return rtsp;
  574. }
  575. // Slab-allocator constructor for refscale_typesafe structures created
  576. // out of a new slab of system memory.
  577. static void refscale_typesafe_ctor(void *rtsp_in)
  578. {
  579. struct refscale_typesafe *rtsp = rtsp_in;
  580. spin_lock_init(&rtsp->rts_lock);
  581. seqlock_init(&rtsp->rts_seqlock);
  582. preempt_disable();
  583. rtsp->a = torture_random(this_cpu_ptr(&refscale_rand));
  584. preempt_enable();
  585. }
  586. static const struct ref_scale_ops typesafe_ref_ops;
  587. static const struct ref_scale_ops typesafe_lock_ops;
  588. static const struct ref_scale_ops typesafe_seqlock_ops;
  589. // Initialize for a typesafe test.
  590. static bool typesafe_init(void)
  591. {
  592. long idx;
  593. long si = lookup_instances;
  594. typesafe_kmem_cachep = kmem_cache_create("refscale_typesafe",
  595. sizeof(struct refscale_typesafe), sizeof(void *),
  596. SLAB_TYPESAFE_BY_RCU, refscale_typesafe_ctor);
  597. if (!typesafe_kmem_cachep)
  598. return false;
  599. if (si < 0)
  600. si = -si * nr_cpu_ids;
  601. else if (si == 0)
  602. si = nr_cpu_ids;
  603. rtsarray_size = si;
  604. rtsarray = kcalloc(si, sizeof(*rtsarray), GFP_KERNEL);
  605. if (!rtsarray)
  606. return false;
  607. for (idx = 0; idx < rtsarray_size; idx++) {
  608. rtsarray[idx] = typesafe_alloc_one();
  609. if (!rtsarray[idx])
  610. return false;
  611. }
  612. if (cur_ops == &typesafe_ref_ops) {
  613. rts_acquire = typesafe_ref_acquire;
  614. rts_release = typesafe_ref_release;
  615. } else if (cur_ops == &typesafe_lock_ops) {
  616. rts_acquire = typesafe_lock_acquire;
  617. rts_release = typesafe_lock_release;
  618. } else if (cur_ops == &typesafe_seqlock_ops) {
  619. rts_acquire = typesafe_seqlock_acquire;
  620. rts_release = typesafe_seqlock_release;
  621. } else {
  622. WARN_ON_ONCE(1);
  623. return false;
  624. }
  625. return true;
  626. }
  627. // Clean up after a typesafe test.
  628. static void typesafe_cleanup(void)
  629. {
  630. long idx;
  631. if (rtsarray) {
  632. for (idx = 0; idx < rtsarray_size; idx++)
  633. kmem_cache_free(typesafe_kmem_cachep, rtsarray[idx]);
  634. kfree(rtsarray);
  635. rtsarray = NULL;
  636. rtsarray_size = 0;
  637. }
  638. kmem_cache_destroy(typesafe_kmem_cachep);
  639. typesafe_kmem_cachep = NULL;
  640. rts_acquire = NULL;
  641. rts_release = NULL;
  642. }
  643. // The typesafe_init() function distinguishes these structures by address.
  644. static const struct ref_scale_ops typesafe_ref_ops = {
  645. .init = typesafe_init,
  646. .cleanup = typesafe_cleanup,
  647. .readsection = typesafe_read_section,
  648. .delaysection = typesafe_delay_section,
  649. .name = "typesafe_ref"
  650. };
  651. static const struct ref_scale_ops typesafe_lock_ops = {
  652. .init = typesafe_init,
  653. .cleanup = typesafe_cleanup,
  654. .readsection = typesafe_read_section,
  655. .delaysection = typesafe_delay_section,
  656. .name = "typesafe_lock"
  657. };
  658. static const struct ref_scale_ops typesafe_seqlock_ops = {
  659. .init = typesafe_init,
  660. .cleanup = typesafe_cleanup,
  661. .readsection = typesafe_read_section,
  662. .delaysection = typesafe_delay_section,
  663. .name = "typesafe_seqlock"
  664. };
  665. static void rcu_scale_one_reader(void)
  666. {
  667. if (readdelay <= 0)
  668. cur_ops->readsection(loops);
  669. else
  670. cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
  671. }
  672. // Reader kthread. Repeatedly does empty RCU read-side
  673. // critical section, minimizing update-side interference.
  674. static int
  675. ref_scale_reader(void *arg)
  676. {
  677. unsigned long flags;
  678. long me = (long)arg;
  679. struct reader_task *rt = &(reader_tasks[me]);
  680. u64 start;
  681. s64 duration;
  682. VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
  683. WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
  684. set_user_nice(current, MAX_NICE);
  685. atomic_inc(&n_init);
  686. if (holdoff)
  687. schedule_timeout_interruptible(holdoff * HZ);
  688. repeat:
  689. VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
  690. // Wait for signal that this reader can start.
  691. wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
  692. torture_must_stop());
  693. if (torture_must_stop())
  694. goto end;
  695. // Make sure that the CPU is affinitized appropriately during testing.
  696. WARN_ON_ONCE(raw_smp_processor_id() != me);
  697. WRITE_ONCE(rt->start_reader, 0);
  698. if (!atomic_dec_return(&n_started))
  699. while (atomic_read_acquire(&n_started))
  700. cpu_relax();
  701. VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
  702. // To reduce noise, do an initial cache-warming invocation, check
  703. // in, and then keep warming until everyone has checked in.
  704. rcu_scale_one_reader();
  705. if (!atomic_dec_return(&n_warmedup))
  706. while (atomic_read_acquire(&n_warmedup))
  707. rcu_scale_one_reader();
  708. // Also keep interrupts disabled. This also has the effect
  709. // of preventing entries into slow path for rcu_read_unlock().
  710. local_irq_save(flags);
  711. start = ktime_get_mono_fast_ns();
  712. rcu_scale_one_reader();
  713. duration = ktime_get_mono_fast_ns() - start;
  714. local_irq_restore(flags);
  715. rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
  716. // To reduce runtime-skew noise, do maintain-load invocations until
  717. // everyone is done.
  718. if (!atomic_dec_return(&n_cooleddown))
  719. while (atomic_read_acquire(&n_cooleddown))
  720. rcu_scale_one_reader();
  721. if (atomic_dec_and_test(&nreaders_exp))
  722. wake_up(&main_wq);
  723. VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
  724. me, exp_idx, atomic_read(&nreaders_exp));
  725. if (!torture_must_stop())
  726. goto repeat;
  727. end:
  728. torture_kthread_stopping("ref_scale_reader");
  729. return 0;
  730. }
  731. static void reset_readers(void)
  732. {
  733. int i;
  734. struct reader_task *rt;
  735. for (i = 0; i < nreaders; i++) {
  736. rt = &(reader_tasks[i]);
  737. rt->last_duration_ns = 0;
  738. }
  739. }
  740. // Print the results of each reader and return the sum of all their durations.
  741. static u64 process_durations(int n)
  742. {
  743. int i;
  744. struct reader_task *rt;
  745. struct seq_buf s;
  746. char *buf;
  747. u64 sum = 0;
  748. buf = kmalloc(800 + 64, GFP_KERNEL);
  749. if (!buf)
  750. return 0;
  751. seq_buf_init(&s, buf, 800 + 64);
  752. seq_buf_printf(&s, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
  753. exp_idx);
  754. for (i = 0; i < n && !torture_must_stop(); i++) {
  755. rt = &(reader_tasks[i]);
  756. if (i % 5 == 0)
  757. seq_buf_putc(&s, '\n');
  758. if (seq_buf_used(&s) >= 800) {
  759. pr_alert("%s", seq_buf_str(&s));
  760. seq_buf_clear(&s);
  761. }
  762. seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns);
  763. sum += rt->last_duration_ns;
  764. }
  765. pr_alert("%s\n", seq_buf_str(&s));
  766. kfree(buf);
  767. return sum;
  768. }
  769. // The main_func is the main orchestrator, it performs a bunch of
  770. // experiments. For every experiment, it orders all the readers
  771. // involved to start and waits for them to finish the experiment. It
  772. // then reads their timestamps and starts the next experiment. Each
  773. // experiment progresses from 1 concurrent reader to N of them at which
  774. // point all the timestamps are printed.
  775. static int main_func(void *arg)
  776. {
  777. int exp, r;
  778. char buf1[64];
  779. char *buf;
  780. u64 *result_avg;
  781. set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
  782. set_user_nice(current, MAX_NICE);
  783. VERBOSE_SCALEOUT("main_func task started");
  784. result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
  785. buf = kzalloc(800 + 64, GFP_KERNEL);
  786. if (!result_avg || !buf) {
  787. SCALEOUT_ERRSTRING("out of memory");
  788. goto oom_exit;
  789. }
  790. if (holdoff)
  791. schedule_timeout_interruptible(holdoff * HZ);
  792. // Wait for all threads to start.
  793. atomic_inc(&n_init);
  794. while (atomic_read(&n_init) < nreaders + 1)
  795. schedule_timeout_uninterruptible(1);
  796. // Start exp readers up per experiment
  797. for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
  798. if (torture_must_stop())
  799. goto end;
  800. reset_readers();
  801. atomic_set(&nreaders_exp, nreaders);
  802. atomic_set(&n_started, nreaders);
  803. atomic_set(&n_warmedup, nreaders);
  804. atomic_set(&n_cooleddown, nreaders);
  805. exp_idx = exp;
  806. for (r = 0; r < nreaders; r++) {
  807. smp_store_release(&reader_tasks[r].start_reader, 1);
  808. wake_up(&reader_tasks[r].wq);
  809. }
  810. VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
  811. nreaders);
  812. wait_event(main_wq,
  813. !atomic_read(&nreaders_exp) || torture_must_stop());
  814. VERBOSE_SCALEOUT("main_func: experiment ended");
  815. if (torture_must_stop())
  816. goto end;
  817. result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
  818. }
  819. // Print the average of all experiments
  820. SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
  821. pr_alert("Runs\tTime(ns)\n");
  822. for (exp = 0; exp < nruns; exp++) {
  823. u64 avg;
  824. u32 rem;
  825. avg = div_u64_rem(result_avg[exp], 1000, &rem);
  826. sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
  827. strcat(buf, buf1);
  828. if (strlen(buf) >= 800) {
  829. pr_alert("%s", buf);
  830. buf[0] = 0;
  831. }
  832. }
  833. pr_alert("%s", buf);
  834. oom_exit:
  835. // This will shutdown everything including us.
  836. if (shutdown) {
  837. shutdown_start = 1;
  838. wake_up(&shutdown_wq);
  839. }
  840. // Wait for torture to stop us
  841. while (!torture_must_stop())
  842. schedule_timeout_uninterruptible(1);
  843. end:
  844. torture_kthread_stopping("main_func");
  845. kfree(result_avg);
  846. kfree(buf);
  847. return 0;
  848. }
  849. static void
  850. ref_scale_print_module_parms(const struct ref_scale_ops *cur_ops, const char *tag)
  851. {
  852. pr_alert("%s" SCALE_FLAG
  853. "--- %s: verbose=%d verbose_batched=%d shutdown=%d holdoff=%d lookup_instances=%ld loops=%d nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
  854. verbose, verbose_batched, shutdown, holdoff, lookup_instances, loops, nreaders, nruns, readdelay);
  855. }
  856. static void
  857. ref_scale_cleanup(void)
  858. {
  859. int i;
  860. if (torture_cleanup_begin())
  861. return;
  862. if (!cur_ops) {
  863. torture_cleanup_end();
  864. return;
  865. }
  866. if (reader_tasks) {
  867. for (i = 0; i < nreaders; i++)
  868. torture_stop_kthread("ref_scale_reader",
  869. reader_tasks[i].task);
  870. }
  871. kfree(reader_tasks);
  872. torture_stop_kthread("main_task", main_task);
  873. kfree(main_task);
  874. // Do scale-type-specific cleanup operations.
  875. if (cur_ops->cleanup != NULL)
  876. cur_ops->cleanup();
  877. torture_cleanup_end();
  878. }
  879. // Shutdown kthread. Just waits to be awakened, then shuts down system.
  880. static int
  881. ref_scale_shutdown(void *arg)
  882. {
  883. wait_event_idle(shutdown_wq, shutdown_start);
  884. smp_mb(); // Wake before output.
  885. ref_scale_cleanup();
  886. kernel_power_off();
  887. return -EINVAL;
  888. }
  889. static int __init
  890. ref_scale_init(void)
  891. {
  892. long i;
  893. int firsterr = 0;
  894. static const struct ref_scale_ops *scale_ops[] = {
  895. &rcu_ops, &srcu_ops, RCU_TRACE_OPS RCU_TASKS_OPS &refcnt_ops, &rwlock_ops,
  896. &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops, &jiffies_ops,
  897. &typesafe_ref_ops, &typesafe_lock_ops, &typesafe_seqlock_ops,
  898. };
  899. if (!torture_init_begin(scale_type, verbose))
  900. return -EBUSY;
  901. for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
  902. cur_ops = scale_ops[i];
  903. if (strcmp(scale_type, cur_ops->name) == 0)
  904. break;
  905. }
  906. if (i == ARRAY_SIZE(scale_ops)) {
  907. pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
  908. pr_alert("rcu-scale types:");
  909. for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
  910. pr_cont(" %s", scale_ops[i]->name);
  911. pr_cont("\n");
  912. firsterr = -EINVAL;
  913. cur_ops = NULL;
  914. goto unwind;
  915. }
  916. if (cur_ops->init)
  917. if (!cur_ops->init()) {
  918. firsterr = -EUCLEAN;
  919. goto unwind;
  920. }
  921. ref_scale_print_module_parms(cur_ops, "Start of test");
  922. // Shutdown task
  923. if (shutdown) {
  924. init_waitqueue_head(&shutdown_wq);
  925. firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
  926. shutdown_task);
  927. if (torture_init_error(firsterr))
  928. goto unwind;
  929. schedule_timeout_uninterruptible(1);
  930. }
  931. // Reader tasks (default to ~75% of online CPUs).
  932. if (nreaders < 0)
  933. nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
  934. if (WARN_ONCE(loops <= 0, "%s: loops = %d, adjusted to 1\n", __func__, loops))
  935. loops = 1;
  936. if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
  937. nreaders = 1;
  938. if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
  939. nruns = 1;
  940. if (WARN_ONCE(loops > INT_MAX / nreaders,
  941. "%s: nreaders * loops will overflow, adjusted loops to %d",
  942. __func__, INT_MAX / nreaders))
  943. loops = INT_MAX / nreaders;
  944. reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
  945. GFP_KERNEL);
  946. if (!reader_tasks) {
  947. SCALEOUT_ERRSTRING("out of memory");
  948. firsterr = -ENOMEM;
  949. goto unwind;
  950. }
  951. VERBOSE_SCALEOUT("Starting %d reader threads", nreaders);
  952. for (i = 0; i < nreaders; i++) {
  953. init_waitqueue_head(&reader_tasks[i].wq);
  954. firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
  955. reader_tasks[i].task);
  956. if (torture_init_error(firsterr))
  957. goto unwind;
  958. }
  959. // Main Task
  960. init_waitqueue_head(&main_wq);
  961. firsterr = torture_create_kthread(main_func, NULL, main_task);
  962. if (torture_init_error(firsterr))
  963. goto unwind;
  964. torture_init_end();
  965. return 0;
  966. unwind:
  967. torture_init_end();
  968. ref_scale_cleanup();
  969. if (shutdown) {
  970. WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
  971. kernel_power_off();
  972. }
  973. return firsterr;
  974. }
  975. module_init(ref_scale_init);
  976. module_exit(ref_scale_cleanup);