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