Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // Scalability test comparing RCU vs other mechanisms
0004 // for acquiring references on objects.
0005 //
0006 // Copyright (C) Google, 2020.
0007 //
0008 // Author: Joel Fernandes <joel@joelfernandes.org>
0009 
0010 #define pr_fmt(fmt) fmt
0011 
0012 #include <linux/atomic.h>
0013 #include <linux/bitops.h>
0014 #include <linux/completion.h>
0015 #include <linux/cpu.h>
0016 #include <linux/delay.h>
0017 #include <linux/err.h>
0018 #include <linux/init.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/kthread.h>
0021 #include <linux/kernel.h>
0022 #include <linux/mm.h>
0023 #include <linux/module.h>
0024 #include <linux/moduleparam.h>
0025 #include <linux/notifier.h>
0026 #include <linux/percpu.h>
0027 #include <linux/rcupdate.h>
0028 #include <linux/rcupdate_trace.h>
0029 #include <linux/reboot.h>
0030 #include <linux/sched.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/smp.h>
0033 #include <linux/stat.h>
0034 #include <linux/srcu.h>
0035 #include <linux/slab.h>
0036 #include <linux/torture.h>
0037 #include <linux/types.h>
0038 
0039 #include "rcu.h"
0040 
0041 #define SCALE_FLAG "-ref-scale: "
0042 
0043 #define SCALEOUT(s, x...) \
0044     pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
0045 
0046 #define VERBOSE_SCALEOUT(s, x...) \
0047     do { \
0048         if (verbose) \
0049             pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
0050     } while (0)
0051 
0052 static atomic_t verbose_batch_ctr;
0053 
0054 #define VERBOSE_SCALEOUT_BATCH(s, x...)                         \
0055 do {                                            \
0056     if (verbose &&                                  \
0057         (verbose_batched <= 0 ||                            \
0058          !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) {     \
0059         schedule_timeout_uninterruptible(1);                    \
0060         pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x);         \
0061     }                                       \
0062 } while (0)
0063 
0064 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x)
0065 
0066 MODULE_LICENSE("GPL");
0067 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
0068 
0069 static char *scale_type = "rcu";
0070 module_param(scale_type, charp, 0444);
0071 MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
0072 
0073 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
0074 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
0075 
0076 // Wait until there are multiple CPUs before starting test.
0077 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
0078           "Holdoff time before test start (s)");
0079 // Number of loops per experiment, all readers execute operations concurrently.
0080 torture_param(long, loops, 10000, "Number of loops per experiment.");
0081 // Number of readers, with -1 defaulting to about 75% of the CPUs.
0082 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
0083 // Number of runs.
0084 torture_param(int, nruns, 30, "Number of experiments to run.");
0085 // Reader delay in nanoseconds, 0 for no delay.
0086 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
0087 
0088 #ifdef MODULE
0089 # define REFSCALE_SHUTDOWN 0
0090 #else
0091 # define REFSCALE_SHUTDOWN 1
0092 #endif
0093 
0094 torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
0095           "Shutdown at end of scalability tests.");
0096 
0097 struct reader_task {
0098     struct task_struct *task;
0099     int start_reader;
0100     wait_queue_head_t wq;
0101     u64 last_duration_ns;
0102 };
0103 
0104 static struct task_struct *shutdown_task;
0105 static wait_queue_head_t shutdown_wq;
0106 
0107 static struct task_struct *main_task;
0108 static wait_queue_head_t main_wq;
0109 static int shutdown_start;
0110 
0111 static struct reader_task *reader_tasks;
0112 
0113 // Number of readers that are part of the current experiment.
0114 static atomic_t nreaders_exp;
0115 
0116 // Use to wait for all threads to start.
0117 static atomic_t n_init;
0118 static atomic_t n_started;
0119 static atomic_t n_warmedup;
0120 static atomic_t n_cooleddown;
0121 
0122 // Track which experiment is currently running.
0123 static int exp_idx;
0124 
0125 // Operations vector for selecting different types of tests.
0126 struct ref_scale_ops {
0127     void (*init)(void);
0128     void (*cleanup)(void);
0129     void (*readsection)(const int nloops);
0130     void (*delaysection)(const int nloops, const int udl, const int ndl);
0131     const char *name;
0132 };
0133 
0134 static struct ref_scale_ops *cur_ops;
0135 
0136 static void un_delay(const int udl, const int ndl)
0137 {
0138     if (udl)
0139         udelay(udl);
0140     if (ndl)
0141         ndelay(ndl);
0142 }
0143 
0144 static void ref_rcu_read_section(const int nloops)
0145 {
0146     int i;
0147 
0148     for (i = nloops; i >= 0; i--) {
0149         rcu_read_lock();
0150         rcu_read_unlock();
0151     }
0152 }
0153 
0154 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
0155 {
0156     int i;
0157 
0158     for (i = nloops; i >= 0; i--) {
0159         rcu_read_lock();
0160         un_delay(udl, ndl);
0161         rcu_read_unlock();
0162     }
0163 }
0164 
0165 static void rcu_sync_scale_init(void)
0166 {
0167 }
0168 
0169 static struct ref_scale_ops rcu_ops = {
0170     .init       = rcu_sync_scale_init,
0171     .readsection    = ref_rcu_read_section,
0172     .delaysection   = ref_rcu_delay_section,
0173     .name       = "rcu"
0174 };
0175 
0176 // Definitions for SRCU ref scale testing.
0177 DEFINE_STATIC_SRCU(srcu_refctl_scale);
0178 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
0179 
0180 static void srcu_ref_scale_read_section(const int nloops)
0181 {
0182     int i;
0183     int idx;
0184 
0185     for (i = nloops; i >= 0; i--) {
0186         idx = srcu_read_lock(srcu_ctlp);
0187         srcu_read_unlock(srcu_ctlp, idx);
0188     }
0189 }
0190 
0191 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
0192 {
0193     int i;
0194     int idx;
0195 
0196     for (i = nloops; i >= 0; i--) {
0197         idx = srcu_read_lock(srcu_ctlp);
0198         un_delay(udl, ndl);
0199         srcu_read_unlock(srcu_ctlp, idx);
0200     }
0201 }
0202 
0203 static struct ref_scale_ops srcu_ops = {
0204     .init       = rcu_sync_scale_init,
0205     .readsection    = srcu_ref_scale_read_section,
0206     .delaysection   = srcu_ref_scale_delay_section,
0207     .name       = "srcu"
0208 };
0209 
0210 #ifdef CONFIG_TASKS_RCU
0211 
0212 // Definitions for RCU Tasks ref scale testing: Empty read markers.
0213 // These definitions also work for RCU Rude readers.
0214 static void rcu_tasks_ref_scale_read_section(const int nloops)
0215 {
0216     int i;
0217 
0218     for (i = nloops; i >= 0; i--)
0219         continue;
0220 }
0221 
0222 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
0223 {
0224     int i;
0225 
0226     for (i = nloops; i >= 0; i--)
0227         un_delay(udl, ndl);
0228 }
0229 
0230 static struct ref_scale_ops rcu_tasks_ops = {
0231     .init       = rcu_sync_scale_init,
0232     .readsection    = rcu_tasks_ref_scale_read_section,
0233     .delaysection   = rcu_tasks_ref_scale_delay_section,
0234     .name       = "rcu-tasks"
0235 };
0236 
0237 #define RCU_TASKS_OPS &rcu_tasks_ops,
0238 
0239 #else // #ifdef CONFIG_TASKS_RCU
0240 
0241 #define RCU_TASKS_OPS
0242 
0243 #endif // #else // #ifdef CONFIG_TASKS_RCU
0244 
0245 #ifdef CONFIG_TASKS_TRACE_RCU
0246 
0247 // Definitions for RCU Tasks Trace ref scale testing.
0248 static void rcu_trace_ref_scale_read_section(const int nloops)
0249 {
0250     int i;
0251 
0252     for (i = nloops; i >= 0; i--) {
0253         rcu_read_lock_trace();
0254         rcu_read_unlock_trace();
0255     }
0256 }
0257 
0258 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
0259 {
0260     int i;
0261 
0262     for (i = nloops; i >= 0; i--) {
0263         rcu_read_lock_trace();
0264         un_delay(udl, ndl);
0265         rcu_read_unlock_trace();
0266     }
0267 }
0268 
0269 static struct ref_scale_ops rcu_trace_ops = {
0270     .init       = rcu_sync_scale_init,
0271     .readsection    = rcu_trace_ref_scale_read_section,
0272     .delaysection   = rcu_trace_ref_scale_delay_section,
0273     .name       = "rcu-trace"
0274 };
0275 
0276 #define RCU_TRACE_OPS &rcu_trace_ops,
0277 
0278 #else // #ifdef CONFIG_TASKS_TRACE_RCU
0279 
0280 #define RCU_TRACE_OPS
0281 
0282 #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
0283 
0284 // Definitions for reference count
0285 static atomic_t refcnt;
0286 
0287 static void ref_refcnt_section(const int nloops)
0288 {
0289     int i;
0290 
0291     for (i = nloops; i >= 0; i--) {
0292         atomic_inc(&refcnt);
0293         atomic_dec(&refcnt);
0294     }
0295 }
0296 
0297 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
0298 {
0299     int i;
0300 
0301     for (i = nloops; i >= 0; i--) {
0302         atomic_inc(&refcnt);
0303         un_delay(udl, ndl);
0304         atomic_dec(&refcnt);
0305     }
0306 }
0307 
0308 static struct ref_scale_ops refcnt_ops = {
0309     .init       = rcu_sync_scale_init,
0310     .readsection    = ref_refcnt_section,
0311     .delaysection   = ref_refcnt_delay_section,
0312     .name       = "refcnt"
0313 };
0314 
0315 // Definitions for rwlock
0316 static rwlock_t test_rwlock;
0317 
0318 static void ref_rwlock_init(void)
0319 {
0320     rwlock_init(&test_rwlock);
0321 }
0322 
0323 static void ref_rwlock_section(const int nloops)
0324 {
0325     int i;
0326 
0327     for (i = nloops; i >= 0; i--) {
0328         read_lock(&test_rwlock);
0329         read_unlock(&test_rwlock);
0330     }
0331 }
0332 
0333 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
0334 {
0335     int i;
0336 
0337     for (i = nloops; i >= 0; i--) {
0338         read_lock(&test_rwlock);
0339         un_delay(udl, ndl);
0340         read_unlock(&test_rwlock);
0341     }
0342 }
0343 
0344 static struct ref_scale_ops rwlock_ops = {
0345     .init       = ref_rwlock_init,
0346     .readsection    = ref_rwlock_section,
0347     .delaysection   = ref_rwlock_delay_section,
0348     .name       = "rwlock"
0349 };
0350 
0351 // Definitions for rwsem
0352 static struct rw_semaphore test_rwsem;
0353 
0354 static void ref_rwsem_init(void)
0355 {
0356     init_rwsem(&test_rwsem);
0357 }
0358 
0359 static void ref_rwsem_section(const int nloops)
0360 {
0361     int i;
0362 
0363     for (i = nloops; i >= 0; i--) {
0364         down_read(&test_rwsem);
0365         up_read(&test_rwsem);
0366     }
0367 }
0368 
0369 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
0370 {
0371     int i;
0372 
0373     for (i = nloops; i >= 0; i--) {
0374         down_read(&test_rwsem);
0375         un_delay(udl, ndl);
0376         up_read(&test_rwsem);
0377     }
0378 }
0379 
0380 static struct ref_scale_ops rwsem_ops = {
0381     .init       = ref_rwsem_init,
0382     .readsection    = ref_rwsem_section,
0383     .delaysection   = ref_rwsem_delay_section,
0384     .name       = "rwsem"
0385 };
0386 
0387 // Definitions for global spinlock
0388 static DEFINE_RAW_SPINLOCK(test_lock);
0389 
0390 static void ref_lock_section(const int nloops)
0391 {
0392     int i;
0393 
0394     preempt_disable();
0395     for (i = nloops; i >= 0; i--) {
0396         raw_spin_lock(&test_lock);
0397         raw_spin_unlock(&test_lock);
0398     }
0399     preempt_enable();
0400 }
0401 
0402 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
0403 {
0404     int i;
0405 
0406     preempt_disable();
0407     for (i = nloops; i >= 0; i--) {
0408         raw_spin_lock(&test_lock);
0409         un_delay(udl, ndl);
0410         raw_spin_unlock(&test_lock);
0411     }
0412     preempt_enable();
0413 }
0414 
0415 static struct ref_scale_ops lock_ops = {
0416     .readsection    = ref_lock_section,
0417     .delaysection   = ref_lock_delay_section,
0418     .name       = "lock"
0419 };
0420 
0421 // Definitions for global irq-save spinlock
0422 
0423 static void ref_lock_irq_section(const int nloops)
0424 {
0425     unsigned long flags;
0426     int i;
0427 
0428     preempt_disable();
0429     for (i = nloops; i >= 0; i--) {
0430         raw_spin_lock_irqsave(&test_lock, flags);
0431         raw_spin_unlock_irqrestore(&test_lock, flags);
0432     }
0433     preempt_enable();
0434 }
0435 
0436 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
0437 {
0438     unsigned long flags;
0439     int i;
0440 
0441     preempt_disable();
0442     for (i = nloops; i >= 0; i--) {
0443         raw_spin_lock_irqsave(&test_lock, flags);
0444         un_delay(udl, ndl);
0445         raw_spin_unlock_irqrestore(&test_lock, flags);
0446     }
0447     preempt_enable();
0448 }
0449 
0450 static struct ref_scale_ops lock_irq_ops = {
0451     .readsection    = ref_lock_irq_section,
0452     .delaysection   = ref_lock_irq_delay_section,
0453     .name       = "lock-irq"
0454 };
0455 
0456 // Definitions acquire-release.
0457 static DEFINE_PER_CPU(unsigned long, test_acqrel);
0458 
0459 static void ref_acqrel_section(const int nloops)
0460 {
0461     unsigned long x;
0462     int i;
0463 
0464     preempt_disable();
0465     for (i = nloops; i >= 0; i--) {
0466         x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
0467         smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
0468     }
0469     preempt_enable();
0470 }
0471 
0472 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
0473 {
0474     unsigned long x;
0475     int i;
0476 
0477     preempt_disable();
0478     for (i = nloops; i >= 0; i--) {
0479         x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
0480         un_delay(udl, ndl);
0481         smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
0482     }
0483     preempt_enable();
0484 }
0485 
0486 static struct ref_scale_ops acqrel_ops = {
0487     .readsection    = ref_acqrel_section,
0488     .delaysection   = ref_acqrel_delay_section,
0489     .name       = "acqrel"
0490 };
0491 
0492 static volatile u64 stopopts;
0493 
0494 static void ref_clock_section(const int nloops)
0495 {
0496     u64 x = 0;
0497     int i;
0498 
0499     preempt_disable();
0500     for (i = nloops; i >= 0; i--)
0501         x += ktime_get_real_fast_ns();
0502     preempt_enable();
0503     stopopts = x;
0504 }
0505 
0506 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
0507 {
0508     u64 x = 0;
0509     int i;
0510 
0511     preempt_disable();
0512     for (i = nloops; i >= 0; i--) {
0513         x += ktime_get_real_fast_ns();
0514         un_delay(udl, ndl);
0515     }
0516     preempt_enable();
0517     stopopts = x;
0518 }
0519 
0520 static struct ref_scale_ops clock_ops = {
0521     .readsection    = ref_clock_section,
0522     .delaysection   = ref_clock_delay_section,
0523     .name       = "clock"
0524 };
0525 
0526 static void rcu_scale_one_reader(void)
0527 {
0528     if (readdelay <= 0)
0529         cur_ops->readsection(loops);
0530     else
0531         cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
0532 }
0533 
0534 // Reader kthread.  Repeatedly does empty RCU read-side
0535 // critical section, minimizing update-side interference.
0536 static int
0537 ref_scale_reader(void *arg)
0538 {
0539     unsigned long flags;
0540     long me = (long)arg;
0541     struct reader_task *rt = &(reader_tasks[me]);
0542     u64 start;
0543     s64 duration;
0544 
0545     VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
0546     WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
0547     set_user_nice(current, MAX_NICE);
0548     atomic_inc(&n_init);
0549     if (holdoff)
0550         schedule_timeout_interruptible(holdoff * HZ);
0551 repeat:
0552     VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
0553 
0554     // Wait for signal that this reader can start.
0555     wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
0556                torture_must_stop());
0557 
0558     if (torture_must_stop())
0559         goto end;
0560 
0561     // Make sure that the CPU is affinitized appropriately during testing.
0562     WARN_ON_ONCE(raw_smp_processor_id() != me);
0563 
0564     WRITE_ONCE(rt->start_reader, 0);
0565     if (!atomic_dec_return(&n_started))
0566         while (atomic_read_acquire(&n_started))
0567             cpu_relax();
0568 
0569     VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
0570 
0571 
0572     // To reduce noise, do an initial cache-warming invocation, check
0573     // in, and then keep warming until everyone has checked in.
0574     rcu_scale_one_reader();
0575     if (!atomic_dec_return(&n_warmedup))
0576         while (atomic_read_acquire(&n_warmedup))
0577             rcu_scale_one_reader();
0578     // Also keep interrupts disabled.  This also has the effect
0579     // of preventing entries into slow path for rcu_read_unlock().
0580     local_irq_save(flags);
0581     start = ktime_get_mono_fast_ns();
0582 
0583     rcu_scale_one_reader();
0584 
0585     duration = ktime_get_mono_fast_ns() - start;
0586     local_irq_restore(flags);
0587 
0588     rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
0589     // To reduce runtime-skew noise, do maintain-load invocations until
0590     // everyone is done.
0591     if (!atomic_dec_return(&n_cooleddown))
0592         while (atomic_read_acquire(&n_cooleddown))
0593             rcu_scale_one_reader();
0594 
0595     if (atomic_dec_and_test(&nreaders_exp))
0596         wake_up(&main_wq);
0597 
0598     VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
0599                 me, exp_idx, atomic_read(&nreaders_exp));
0600 
0601     if (!torture_must_stop())
0602         goto repeat;
0603 end:
0604     torture_kthread_stopping("ref_scale_reader");
0605     return 0;
0606 }
0607 
0608 static void reset_readers(void)
0609 {
0610     int i;
0611     struct reader_task *rt;
0612 
0613     for (i = 0; i < nreaders; i++) {
0614         rt = &(reader_tasks[i]);
0615 
0616         rt->last_duration_ns = 0;
0617     }
0618 }
0619 
0620 // Print the results of each reader and return the sum of all their durations.
0621 static u64 process_durations(int n)
0622 {
0623     int i;
0624     struct reader_task *rt;
0625     char buf1[64];
0626     char *buf;
0627     u64 sum = 0;
0628 
0629     buf = kmalloc(800 + 64, GFP_KERNEL);
0630     if (!buf)
0631         return 0;
0632     buf[0] = 0;
0633     sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
0634         exp_idx);
0635 
0636     for (i = 0; i < n && !torture_must_stop(); i++) {
0637         rt = &(reader_tasks[i]);
0638         sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns);
0639 
0640         if (i % 5 == 0)
0641             strcat(buf, "\n");
0642         if (strlen(buf) >= 800) {
0643             pr_alert("%s", buf);
0644             buf[0] = 0;
0645         }
0646         strcat(buf, buf1);
0647 
0648         sum += rt->last_duration_ns;
0649     }
0650     pr_alert("%s\n", buf);
0651 
0652     kfree(buf);
0653     return sum;
0654 }
0655 
0656 // The main_func is the main orchestrator, it performs a bunch of
0657 // experiments.  For every experiment, it orders all the readers
0658 // involved to start and waits for them to finish the experiment. It
0659 // then reads their timestamps and starts the next experiment. Each
0660 // experiment progresses from 1 concurrent reader to N of them at which
0661 // point all the timestamps are printed.
0662 static int main_func(void *arg)
0663 {
0664     int exp, r;
0665     char buf1[64];
0666     char *buf;
0667     u64 *result_avg;
0668 
0669     set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
0670     set_user_nice(current, MAX_NICE);
0671 
0672     VERBOSE_SCALEOUT("main_func task started");
0673     result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
0674     buf = kzalloc(800 + 64, GFP_KERNEL);
0675     if (!result_avg || !buf) {
0676         SCALEOUT_ERRSTRING("out of memory");
0677         goto oom_exit;
0678     }
0679     if (holdoff)
0680         schedule_timeout_interruptible(holdoff * HZ);
0681 
0682     // Wait for all threads to start.
0683     atomic_inc(&n_init);
0684     while (atomic_read(&n_init) < nreaders + 1)
0685         schedule_timeout_uninterruptible(1);
0686 
0687     // Start exp readers up per experiment
0688     for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
0689         if (torture_must_stop())
0690             goto end;
0691 
0692         reset_readers();
0693         atomic_set(&nreaders_exp, nreaders);
0694         atomic_set(&n_started, nreaders);
0695         atomic_set(&n_warmedup, nreaders);
0696         atomic_set(&n_cooleddown, nreaders);
0697 
0698         exp_idx = exp;
0699 
0700         for (r = 0; r < nreaders; r++) {
0701             smp_store_release(&reader_tasks[r].start_reader, 1);
0702             wake_up(&reader_tasks[r].wq);
0703         }
0704 
0705         VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
0706                 nreaders);
0707 
0708         wait_event(main_wq,
0709                !atomic_read(&nreaders_exp) || torture_must_stop());
0710 
0711         VERBOSE_SCALEOUT("main_func: experiment ended");
0712 
0713         if (torture_must_stop())
0714             goto end;
0715 
0716         result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
0717     }
0718 
0719     // Print the average of all experiments
0720     SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
0721 
0722     pr_alert("Runs\tTime(ns)\n");
0723     for (exp = 0; exp < nruns; exp++) {
0724         u64 avg;
0725         u32 rem;
0726 
0727         avg = div_u64_rem(result_avg[exp], 1000, &rem);
0728         sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
0729         strcat(buf, buf1);
0730         if (strlen(buf) >= 800) {
0731             pr_alert("%s", buf);
0732             buf[0] = 0;
0733         }
0734     }
0735 
0736     pr_alert("%s", buf);
0737 
0738 oom_exit:
0739     // This will shutdown everything including us.
0740     if (shutdown) {
0741         shutdown_start = 1;
0742         wake_up(&shutdown_wq);
0743     }
0744 
0745     // Wait for torture to stop us
0746     while (!torture_must_stop())
0747         schedule_timeout_uninterruptible(1);
0748 
0749 end:
0750     torture_kthread_stopping("main_func");
0751     kfree(result_avg);
0752     kfree(buf);
0753     return 0;
0754 }
0755 
0756 static void
0757 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag)
0758 {
0759     pr_alert("%s" SCALE_FLAG
0760          "--- %s:  verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
0761          verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay);
0762 }
0763 
0764 static void
0765 ref_scale_cleanup(void)
0766 {
0767     int i;
0768 
0769     if (torture_cleanup_begin())
0770         return;
0771 
0772     if (!cur_ops) {
0773         torture_cleanup_end();
0774         return;
0775     }
0776 
0777     if (reader_tasks) {
0778         for (i = 0; i < nreaders; i++)
0779             torture_stop_kthread("ref_scale_reader",
0780                          reader_tasks[i].task);
0781     }
0782     kfree(reader_tasks);
0783 
0784     torture_stop_kthread("main_task", main_task);
0785     kfree(main_task);
0786 
0787     // Do scale-type-specific cleanup operations.
0788     if (cur_ops->cleanup != NULL)
0789         cur_ops->cleanup();
0790 
0791     torture_cleanup_end();
0792 }
0793 
0794 // Shutdown kthread.  Just waits to be awakened, then shuts down system.
0795 static int
0796 ref_scale_shutdown(void *arg)
0797 {
0798     wait_event(shutdown_wq, shutdown_start);
0799 
0800     smp_mb(); // Wake before output.
0801     ref_scale_cleanup();
0802     kernel_power_off();
0803 
0804     return -EINVAL;
0805 }
0806 
0807 static int __init
0808 ref_scale_init(void)
0809 {
0810     long i;
0811     int firsterr = 0;
0812     static struct ref_scale_ops *scale_ops[] = {
0813         &rcu_ops, &srcu_ops, RCU_TRACE_OPS RCU_TASKS_OPS &refcnt_ops, &rwlock_ops,
0814         &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops,
0815     };
0816 
0817     if (!torture_init_begin(scale_type, verbose))
0818         return -EBUSY;
0819 
0820     for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
0821         cur_ops = scale_ops[i];
0822         if (strcmp(scale_type, cur_ops->name) == 0)
0823             break;
0824     }
0825     if (i == ARRAY_SIZE(scale_ops)) {
0826         pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
0827         pr_alert("rcu-scale types:");
0828         for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
0829             pr_cont(" %s", scale_ops[i]->name);
0830         pr_cont("\n");
0831         firsterr = -EINVAL;
0832         cur_ops = NULL;
0833         goto unwind;
0834     }
0835     if (cur_ops->init)
0836         cur_ops->init();
0837 
0838     ref_scale_print_module_parms(cur_ops, "Start of test");
0839 
0840     // Shutdown task
0841     if (shutdown) {
0842         init_waitqueue_head(&shutdown_wq);
0843         firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
0844                           shutdown_task);
0845         if (torture_init_error(firsterr))
0846             goto unwind;
0847         schedule_timeout_uninterruptible(1);
0848     }
0849 
0850     // Reader tasks (default to ~75% of online CPUs).
0851     if (nreaders < 0)
0852         nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
0853     if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops))
0854         loops = 1;
0855     if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
0856         nreaders = 1;
0857     if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
0858         nruns = 1;
0859     reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
0860                    GFP_KERNEL);
0861     if (!reader_tasks) {
0862         SCALEOUT_ERRSTRING("out of memory");
0863         firsterr = -ENOMEM;
0864         goto unwind;
0865     }
0866 
0867     VERBOSE_SCALEOUT("Starting %d reader threads", nreaders);
0868 
0869     for (i = 0; i < nreaders; i++) {
0870         firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
0871                           reader_tasks[i].task);
0872         if (torture_init_error(firsterr))
0873             goto unwind;
0874 
0875         init_waitqueue_head(&(reader_tasks[i].wq));
0876     }
0877 
0878     // Main Task
0879     init_waitqueue_head(&main_wq);
0880     firsterr = torture_create_kthread(main_func, NULL, main_task);
0881     if (torture_init_error(firsterr))
0882         goto unwind;
0883 
0884     torture_init_end();
0885     return 0;
0886 
0887 unwind:
0888     torture_init_end();
0889     ref_scale_cleanup();
0890     if (shutdown) {
0891         WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
0892         kernel_power_off();
0893     }
0894     return firsterr;
0895 }
0896 
0897 module_init(ref_scale_init);
0898 module_exit(ref_scale_cleanup);