blob: 5e4f1f83d38e72663d3760f46d5c0084cd3bb6c0 [file] [log] [blame]
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -08001// SPDX-License-Identifier: GPL-2.0+
Paul E. McKenney8704baa2015-12-31 18:33:22 -08002/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -07003 * Read-Copy Update module-based scalability-test facility
Paul E. McKenney8704baa2015-12-31 18:33:22 -08004 *
Paul E. McKenney8704baa2015-12-31 18:33:22 -08005 * Copyright (C) IBM Corporation, 2015
6 *
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -08007 * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
Paul E. McKenney8704baa2015-12-31 18:33:22 -08008 */
Paul E. McKenney60500032018-05-15 12:25:05 -07009
10#define pr_fmt(fmt) fmt
11
Paul E. McKenney8704baa2015-12-31 18:33:22 -080012#include <linux/types.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
Joel Fernandes (Google)12af6602019-12-19 11:22:42 -050015#include <linux/mm.h>
Paul E. McKenney8704baa2015-12-31 18:33:22 -080016#include <linux/module.h>
17#include <linux/kthread.h>
18#include <linux/err.h>
19#include <linux/spinlock.h>
20#include <linux/smp.h>
21#include <linux/rcupdate.h>
22#include <linux/interrupt.h>
23#include <linux/sched.h>
Ingo Molnarae7e81c2017-02-01 18:07:51 +010024#include <uapi/linux/sched/types.h>
Paul E. McKenney8704baa2015-12-31 18:33:22 -080025#include <linux/atomic.h>
26#include <linux/bitops.h>
27#include <linux/completion.h>
28#include <linux/moduleparam.h>
29#include <linux/percpu.h>
30#include <linux/notifier.h>
31#include <linux/reboot.h>
32#include <linux/freezer.h>
33#include <linux/cpu.h>
34#include <linux/delay.h>
35#include <linux/stat.h>
36#include <linux/srcu.h>
37#include <linux/slab.h>
38#include <asm/byteorder.h>
39#include <linux/torture.h>
40#include <linux/vmalloc.h>
Paul E. McKenney899f3172020-09-09 12:27:03 -070041#include <linux/rcupdate_trace.h>
Paul E. McKenney8704baa2015-12-31 18:33:22 -080042
Paul E. McKenney25c36322017-05-03 09:51:55 -070043#include "rcu.h"
44
Paul E. McKenney8704baa2015-12-31 18:33:22 -080045MODULE_LICENSE("GPL");
Paul E. McKenney8bf05ed2019-01-17 10:09:19 -080046MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.ibm.com>");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080047
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070048#define SCALE_FLAG "-scale:"
49#define SCALEOUT_STRING(s) \
50 pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s)
51#define VERBOSE_SCALEOUT_STRING(s) \
52 do { if (verbose) pr_alert("%s" SCALE_FLAG " %s\n", scale_type, s); } while (0)
Li Zhijian86e7ed12021-10-29 17:40:28 +080053#define SCALEOUT_ERRSTRING(s) \
54 pr_alert("%s" SCALE_FLAG "!!! %s\n", scale_type, s)
Paul E. McKenney8704baa2015-12-31 18:33:22 -080055
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080056/*
57 * The intended use cases for the nreaders and nwriters module parameters
58 * are as follows:
59 *
60 * 1. Specify only the nr_cpus kernel boot parameter. This will
61 * set both nreaders and nwriters to the value specified by
62 * nr_cpus for a mixed reader/writer test.
63 *
64 * 2. Specify the nr_cpus kernel boot parameter, but set
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070065 * rcuscale.nreaders to zero. This will set nwriters to the
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080066 * value specified by nr_cpus for an update-only test.
67 *
68 * 3. Specify the nr_cpus kernel boot parameter, but set
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070069 * rcuscale.nwriters to zero. This will set nreaders to the
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080070 * value specified by nr_cpus for a read-only test.
71 *
72 * Various other use cases may of course be specified.
Paul E. McKenney708cda32020-05-25 09:22:24 -070073 *
74 * Note that this test's readers are intended only as a test load for
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070075 * the writers. The reader scalability statistics will be overly
Paul E. McKenney708cda32020-05-25 09:22:24 -070076 * pessimistic due to the per-critical-section interrupt disabling,
77 * test-end checks, and the pair of calls through pointers.
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080078 */
79
Paul E. McKenneye838a7d2018-12-28 07:48:43 -080080#ifdef MODULE
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070081# define RCUSCALE_SHUTDOWN 0
Paul E. McKenneye838a7d2018-12-28 07:48:43 -080082#else
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070083# define RCUSCALE_SHUTDOWN 1
Paul E. McKenneye838a7d2018-12-28 07:48:43 -080084#endif
85
Paul E. McKenney881ed592017-04-17 12:47:10 -070086torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
87torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
Boqun Fengaf06d4f2016-05-25 09:25:33 +080088torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
Paul E. McKenneydf37e662016-01-30 20:56:38 -080089torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
Paul E. McKenney85ba6bf2018-02-01 19:19:04 -080090torture_param(int, nreaders, -1, "Number of RCU reader threads");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080091torture_param(int, nwriters, -1, "Number of RCU updater threads");
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070092torture_param(bool, shutdown, RCUSCALE_SHUTDOWN,
93 "Shutdown at end of scalability tests.");
Paul E. McKenney90127d62018-05-09 10:29:18 -070094torture_param(int, verbose, 1, "Enable verbose debugging printk()s");
Paul E. McKenney820687a2017-04-25 15:12:56 -070095torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070096torture_param(int, kfree_rcu_test, 0, "Do we run a kfree_rcu() scale test?");
Joel Fernandes (Google)f87dc802020-03-16 12:32:26 -040097torture_param(int, kfree_mult, 1, "Multiple of kfree_obj size to allocate.");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080098
Paul E. McKenney4e88ec42020-08-11 21:18:12 -070099static char *scale_type = "rcu";
100module_param(scale_type, charp, 0444);
101MODULE_PARM_DESC(scale_type, "Type of RCU to scalability-test (rcu, srcu, ...)");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800102
103static int nrealreaders;
104static int nrealwriters;
105static struct task_struct **writer_tasks;
106static struct task_struct **reader_tasks;
107static struct task_struct *shutdown_task;
108
109static u64 **writer_durations;
110static int *writer_n_durations;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700111static atomic_t n_rcu_scale_reader_started;
112static atomic_t n_rcu_scale_writer_started;
113static atomic_t n_rcu_scale_writer_finished;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800114static wait_queue_head_t shutdown_wq;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700115static u64 t_rcu_scale_writer_started;
116static u64 t_rcu_scale_writer_finished;
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400117static unsigned long b_rcu_gp_test_started;
118static unsigned long b_rcu_gp_test_finished;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700119static DEFINE_PER_CPU(atomic_t, n_async_inflight);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800120
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800121#define MAX_MEAS 10000
122#define MIN_MEAS 100
123
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800124/*
125 * Operations vector for selecting different types of tests.
126 */
127
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700128struct rcu_scale_ops {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800129 int ptype;
130 void (*init)(void);
131 void (*cleanup)(void);
132 int (*readlock)(void);
133 void (*readunlock)(int idx);
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700134 unsigned long (*get_gp_seq)(void);
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700135 unsigned long (*gp_diff)(unsigned long new, unsigned long old);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800136 unsigned long (*exp_completed)(void);
Paul E. McKenney881ed592017-04-17 12:47:10 -0700137 void (*async)(struct rcu_head *head, rcu_callback_t func);
138 void (*gp_barrier)(void);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800139 void (*sync)(void);
140 void (*exp_sync)(void);
141 const char *name;
142};
143
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700144static struct rcu_scale_ops *cur_ops;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800145
146/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700147 * Definitions for rcu scalability testing.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800148 */
149
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700150static int rcu_scale_read_lock(void) __acquires(RCU)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800151{
152 rcu_read_lock();
153 return 0;
154}
155
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700156static void rcu_scale_read_unlock(int idx) __releases(RCU)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800157{
158 rcu_read_unlock();
159}
160
161static unsigned long __maybe_unused rcu_no_completed(void)
162{
163 return 0;
164}
165
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700166static void rcu_sync_scale_init(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800167{
168}
169
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700170static struct rcu_scale_ops rcu_ops = {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800171 .ptype = RCU_FLAVOR,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700172 .init = rcu_sync_scale_init,
173 .readlock = rcu_scale_read_lock,
174 .readunlock = rcu_scale_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700175 .get_gp_seq = rcu_get_gp_seq,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700176 .gp_diff = rcu_seq_diff,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800177 .exp_completed = rcu_exp_batches_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700178 .async = call_rcu,
179 .gp_barrier = rcu_barrier,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800180 .sync = synchronize_rcu,
181 .exp_sync = synchronize_rcu_expedited,
182 .name = "rcu"
183};
184
185/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700186 * Definitions for srcu scalability testing.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800187 */
188
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700189DEFINE_STATIC_SRCU(srcu_ctl_scale);
190static struct srcu_struct *srcu_ctlp = &srcu_ctl_scale;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800191
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700192static int srcu_scale_read_lock(void) __acquires(srcu_ctlp)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800193{
194 return srcu_read_lock(srcu_ctlp);
195}
196
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700197static void srcu_scale_read_unlock(int idx) __releases(srcu_ctlp)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800198{
199 srcu_read_unlock(srcu_ctlp, idx);
200}
201
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700202static unsigned long srcu_scale_completed(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800203{
204 return srcu_batches_completed(srcu_ctlp);
205}
206
Paul E. McKenney881ed592017-04-17 12:47:10 -0700207static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
208{
209 call_srcu(srcu_ctlp, head, func);
210}
211
212static void srcu_rcu_barrier(void)
213{
214 srcu_barrier(srcu_ctlp);
215}
216
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700217static void srcu_scale_synchronize(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800218{
219 synchronize_srcu(srcu_ctlp);
220}
221
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700222static void srcu_scale_synchronize_expedited(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800223{
224 synchronize_srcu_expedited(srcu_ctlp);
225}
226
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700227static struct rcu_scale_ops srcu_ops = {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800228 .ptype = SRCU_FLAVOR,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700229 .init = rcu_sync_scale_init,
230 .readlock = srcu_scale_read_lock,
231 .readunlock = srcu_scale_read_unlock,
232 .get_gp_seq = srcu_scale_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700233 .gp_diff = rcu_seq_diff,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700234 .exp_completed = srcu_scale_completed,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700235 .async = srcu_call_rcu,
236 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700237 .sync = srcu_scale_synchronize,
238 .exp_sync = srcu_scale_synchronize_expedited,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800239 .name = "srcu"
240};
241
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700242static struct srcu_struct srcud;
243
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700244static void srcu_sync_scale_init(void)
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700245{
246 srcu_ctlp = &srcud;
247 init_srcu_struct(srcu_ctlp);
248}
249
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700250static void srcu_sync_scale_cleanup(void)
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700251{
252 cleanup_srcu_struct(srcu_ctlp);
253}
254
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700255static struct rcu_scale_ops srcud_ops = {
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700256 .ptype = SRCU_FLAVOR,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700257 .init = srcu_sync_scale_init,
258 .cleanup = srcu_sync_scale_cleanup,
259 .readlock = srcu_scale_read_lock,
260 .readunlock = srcu_scale_read_unlock,
261 .get_gp_seq = srcu_scale_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700262 .gp_diff = rcu_seq_diff,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700263 .exp_completed = srcu_scale_completed,
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700264 .async = srcu_call_rcu,
265 .gp_barrier = srcu_rcu_barrier,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700266 .sync = srcu_scale_synchronize,
267 .exp_sync = srcu_scale_synchronize_expedited,
Paul E. McKenneyf60cb4d2017-04-19 13:43:21 -0700268 .name = "srcud"
269};
270
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800271/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700272 * Definitions for RCU-tasks scalability testing.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800273 */
274
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700275static int tasks_scale_read_lock(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800276{
277 return 0;
278}
279
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700280static void tasks_scale_read_unlock(int idx)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800281{
282}
283
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700284static struct rcu_scale_ops tasks_ops = {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800285 .ptype = RCU_TASKS_FLAVOR,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700286 .init = rcu_sync_scale_init,
287 .readlock = tasks_scale_read_lock,
288 .readunlock = tasks_scale_read_unlock,
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700289 .get_gp_seq = rcu_no_completed,
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700290 .gp_diff = rcu_seq_diff,
Paul E. McKenney881ed592017-04-17 12:47:10 -0700291 .async = call_rcu_tasks,
292 .gp_barrier = rcu_barrier_tasks,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800293 .sync = synchronize_rcu_tasks,
294 .exp_sync = synchronize_rcu_tasks,
295 .name = "tasks"
296};
297
Paul E. McKenney899f3172020-09-09 12:27:03 -0700298/*
299 * Definitions for RCU-tasks-trace scalability testing.
300 */
301
302static int tasks_trace_scale_read_lock(void)
303{
304 rcu_read_lock_trace();
305 return 0;
306}
307
308static void tasks_trace_scale_read_unlock(int idx)
309{
310 rcu_read_unlock_trace();
311}
312
313static struct rcu_scale_ops tasks_tracing_ops = {
314 .ptype = RCU_TASKS_FLAVOR,
315 .init = rcu_sync_scale_init,
316 .readlock = tasks_trace_scale_read_lock,
317 .readunlock = tasks_trace_scale_read_unlock,
318 .get_gp_seq = rcu_no_completed,
319 .gp_diff = rcu_seq_diff,
320 .async = call_rcu_tasks_trace,
321 .gp_barrier = rcu_barrier_tasks_trace,
322 .sync = synchronize_rcu_tasks_trace,
323 .exp_sync = synchronize_rcu_tasks_trace,
324 .name = "tasks-tracing"
325};
326
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700327static unsigned long rcuscale_seq_diff(unsigned long new, unsigned long old)
Paul E. McKenneyd72193122018-05-15 15:24:41 -0700328{
329 if (!cur_ops->gp_diff)
330 return new - old;
331 return cur_ops->gp_diff(new, old);
332}
333
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800334/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700335 * If scalability tests complete, wait for shutdown to commence.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800336 */
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700337static void rcu_scale_wait_shutdown(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800338{
Paul E. McKenneycee43932018-03-02 16:35:27 -0800339 cond_resched_tasks_rcu_qs();
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700340 if (atomic_read(&n_rcu_scale_writer_finished) < nrealwriters)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800341 return;
342 while (!torture_must_stop())
343 schedule_timeout_uninterruptible(1);
344}
345
346/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700347 * RCU scalability reader kthread. Repeatedly does empty RCU read-side
348 * critical section, minimizing update-side interference. However, the
349 * point of this test is not to evaluate reader scalability, but instead
350 * to serve as a test load for update-side scalability testing.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800351 */
352static int
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700353rcu_scale_reader(void *arg)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800354{
355 unsigned long flags;
356 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800357 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800358
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700359 VERBOSE_SCALEOUT_STRING("rcu_scale_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800360 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800361 set_user_nice(current, MAX_NICE);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700362 atomic_inc(&n_rcu_scale_reader_started);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800363
364 do {
365 local_irq_save(flags);
366 idx = cur_ops->readlock();
367 cur_ops->readunlock(idx);
368 local_irq_restore(flags);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700369 rcu_scale_wait_shutdown();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800370 } while (!torture_must_stop());
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700371 torture_kthread_stopping("rcu_scale_reader");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800372 return 0;
373}
374
375/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700376 * Callback function for asynchronous grace periods from rcu_scale_writer().
Paul E. McKenney881ed592017-04-17 12:47:10 -0700377 */
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700378static void rcu_scale_async_cb(struct rcu_head *rhp)
Paul E. McKenney881ed592017-04-17 12:47:10 -0700379{
380 atomic_dec(this_cpu_ptr(&n_async_inflight));
381 kfree(rhp);
382}
383
384/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700385 * RCU scale writer kthread. Repeatedly does a grace period.
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800386 */
387static int
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700388rcu_scale_writer(void *arg)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800389{
390 int i = 0;
391 int i_max;
392 long me = (long)arg;
Paul E. McKenney881ed592017-04-17 12:47:10 -0700393 struct rcu_head *rhp = NULL;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800394 bool started = false, done = false, alldone = false;
395 u64 t;
396 u64 *wdp;
397 u64 *wdpp = writer_durations[me];
398
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700399 VERBOSE_SCALEOUT_STRING("rcu_scale_writer task started");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800400 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800401 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Peter Zijlstrab1433392020-04-21 12:09:13 +0200402 sched_set_fifo_low(current);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800403
404 if (holdoff)
405 schedule_timeout_uninterruptible(holdoff * HZ);
406
Joel Fernandes (Google)77e97522019-07-04 00:34:30 -0400407 /*
408 * Wait until rcu_end_inkernel_boot() is called for normal GP tests
409 * so that RCU is not always expedited for normal GP tests.
410 * The system_state test is approximate, but works well in practice.
411 */
412 while (!gp_exp && system_state != SYSTEM_RUNNING)
413 schedule_timeout_uninterruptible(1);
414
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800415 t = ktime_get_mono_fast_ns();
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700416 if (atomic_inc_return(&n_rcu_scale_writer_started) >= nrealwriters) {
417 t_rcu_scale_writer_started = t;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800418 if (gp_exp) {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400419 b_rcu_gp_test_started =
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800420 cur_ops->exp_completed() / 2;
421 } else {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400422 b_rcu_gp_test_started = cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800423 }
424 }
425
426 do {
Paul E. McKenney820687a2017-04-25 15:12:56 -0700427 if (writer_holdoff)
428 udelay(writer_holdoff);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800429 wdp = &wdpp[i];
430 *wdp = ktime_get_mono_fast_ns();
Paul E. McKenney881ed592017-04-17 12:47:10 -0700431 if (gp_async) {
432retry:
433 if (!rhp)
434 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
435 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700436 atomic_inc(this_cpu_ptr(&n_async_inflight));
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700437 cur_ops->async(rhp, rcu_scale_async_cb);
Paul E. McKenney881ed592017-04-17 12:47:10 -0700438 rhp = NULL;
439 } else if (!kthread_should_stop()) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700440 cur_ops->gp_barrier();
441 goto retry;
442 } else {
443 kfree(rhp); /* Because we are stopping. */
444 }
445 } else if (gp_exp) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800446 cur_ops->exp_sync();
447 } else {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800448 cur_ops->sync();
449 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800450 t = ktime_get_mono_fast_ns();
451 *wdp = t - *wdp;
452 i_max = i;
453 if (!started &&
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700454 atomic_read(&n_rcu_scale_writer_started) >= nrealwriters)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800455 started = true;
456 if (!done && i >= MIN_MEAS) {
457 done = true;
Peter Zijlstrab1433392020-04-21 12:09:13 +0200458 sched_set_normal(current, 0);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700459 pr_alert("%s%s rcu_scale_writer %ld has %d measurements\n",
460 scale_type, SCALE_FLAG, me, MIN_MEAS);
461 if (atomic_inc_return(&n_rcu_scale_writer_finished) >=
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800462 nrealwriters) {
Paul E. McKenney620316e2016-01-30 21:32:09 -0800463 schedule_timeout_interruptible(10);
Paul E. McKenneyac2bb272016-01-29 14:58:17 -0800464 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700465 SCALEOUT_STRING("Test complete");
466 t_rcu_scale_writer_finished = t;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800467 if (gp_exp) {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400468 b_rcu_gp_test_finished =
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800469 cur_ops->exp_completed() / 2;
470 } else {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400471 b_rcu_gp_test_finished =
Paul E. McKenney17ef2fe2018-04-27 11:39:34 -0700472 cur_ops->get_gp_seq();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800473 }
Artem Savkove6fb1fc2016-02-07 13:31:39 +0100474 if (shutdown) {
475 smp_mb(); /* Assign before wake. */
476 wake_up(&shutdown_wq);
477 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800478 }
479 }
480 if (done && !alldone &&
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700481 atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800482 alldone = true;
483 if (started && !alldone && i < MAX_MEAS - 1)
484 i++;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700485 rcu_scale_wait_shutdown();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800486 } while (!torture_must_stop());
Paul E. McKenney881ed592017-04-17 12:47:10 -0700487 if (gp_async) {
Paul E. McKenney881ed592017-04-17 12:47:10 -0700488 cur_ops->gp_barrier();
489 }
Jiangong.Han811192c2021-06-22 18:37:08 +0800490 writer_n_durations[me] = i_max + 1;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700491 torture_kthread_stopping("rcu_scale_writer");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800492 return 0;
493}
494
Paul E. McKenney96221792018-05-17 11:33:17 -0700495static void
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700496rcu_scale_print_module_parms(struct rcu_scale_ops *cur_ops, const char *tag)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800497{
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700498 pr_alert("%s" SCALE_FLAG
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800499 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700500 scale_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800501}
502
503static void
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700504rcu_scale_cleanup(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800505{
506 int i;
507 int j;
508 int ngps = 0;
509 u64 *wdp;
510 u64 *wdpp;
511
Paul E. McKenney96839372017-04-14 16:12:52 -0700512 /*
513 * Would like warning at start, but everything is expedited
514 * during the mid-boot phase, so have to wait till the end.
515 */
516 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
Li Zhijian86e7ed12021-10-29 17:40:28 +0800517 SCALEOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700518 if (rcu_gp_is_normal() && gp_exp)
Li Zhijian86e7ed12021-10-29 17:40:28 +0800519 SCALEOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
Paul E. McKenney881ed592017-04-17 12:47:10 -0700520 if (gp_exp && gp_async)
Li Zhijian86e7ed12021-10-29 17:40:28 +0800521 SCALEOUT_ERRSTRING("No expedited async GPs, so went with async!");
Paul E. McKenney96839372017-04-14 16:12:52 -0700522
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800523 if (torture_cleanup_begin())
524 return;
Paul E. McKenneyad092c02019-03-21 10:26:41 -0700525 if (!cur_ops) {
526 torture_cleanup_end();
527 return;
528 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800529
530 if (reader_tasks) {
531 for (i = 0; i < nrealreaders; i++)
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700532 torture_stop_kthread(rcu_scale_reader,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800533 reader_tasks[i]);
534 kfree(reader_tasks);
535 }
536
537 if (writer_tasks) {
538 for (i = 0; i < nrealwriters; i++) {
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700539 torture_stop_kthread(rcu_scale_writer,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800540 writer_tasks[i]);
541 if (!writer_n_durations)
542 continue;
543 j = writer_n_durations[i];
544 pr_alert("%s%s writer %d gps: %d\n",
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700545 scale_type, SCALE_FLAG, i, j);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800546 ngps += j;
547 }
548 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700549 scale_type, SCALE_FLAG,
550 t_rcu_scale_writer_started, t_rcu_scale_writer_finished,
551 t_rcu_scale_writer_finished -
552 t_rcu_scale_writer_started,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800553 ngps,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700554 rcuscale_seq_diff(b_rcu_gp_test_finished,
555 b_rcu_gp_test_started));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800556 for (i = 0; i < nrealwriters; i++) {
557 if (!writer_durations)
558 break;
559 if (!writer_n_durations)
560 continue;
561 wdpp = writer_durations[i];
562 if (!wdpp)
563 continue;
Jiangong.Han811192c2021-06-22 18:37:08 +0800564 for (j = 0; j < writer_n_durations[i]; j++) {
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800565 wdp = &wdpp[j];
566 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700567 scale_type, SCALE_FLAG,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800568 i, j, *wdp);
569 if (j % 100 == 0)
570 schedule_timeout_uninterruptible(1);
571 }
572 kfree(writer_durations[i]);
573 }
574 kfree(writer_tasks);
575 kfree(writer_durations);
576 kfree(writer_n_durations);
577 }
578
Paul E. McKenney620d2462018-07-07 18:25:10 -0700579 /* Do torture-type-specific cleanup operations. */
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800580 if (cur_ops->cleanup != NULL)
581 cur_ops->cleanup();
582
583 torture_cleanup_end();
584}
585
586/*
587 * Return the number if non-negative. If -1, the number of CPUs.
588 * If less than -1, that much less than the number of CPUs, but
589 * at least one.
590 */
591static int compute_real(int n)
592{
593 int nr;
594
595 if (n >= 0) {
596 nr = n;
597 } else {
598 nr = num_online_cpus() + 1 + n;
599 if (nr <= 0)
600 nr = 1;
601 }
602 return nr;
603}
604
605/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700606 * RCU scalability shutdown kthread. Just waits to be awakened, then shuts
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800607 * down system.
608 */
609static int
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700610rcu_scale_shutdown(void *arg)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800611{
Joel Fernandes (Google)7e866462020-05-25 00:36:47 -0400612 wait_event(shutdown_wq,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700613 atomic_read(&n_rcu_scale_writer_finished) >= nrealwriters);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800614 smp_mb(); /* Wake before output. */
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700615 rcu_scale_cleanup();
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800616 kernel_power_off();
617 return -EINVAL;
618}
619
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400620/*
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700621 * kfree_rcu() scalability tests: Start a kfree_rcu() loop on all CPUs for number
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400622 * of iterations and measure total time and number of GP for all iterations to complete.
623 */
624
625torture_param(int, kfree_nthreads, -1, "Number of threads running loops of kfree_rcu().");
626torture_param(int, kfree_alloc_num, 8000, "Number of allocations and frees done in an iteration.");
627torture_param(int, kfree_loops, 10, "Number of loops doing kfree_alloc_num allocations and frees.");
Uladzislau Rezki (Sony)686fe1b2021-02-17 19:51:10 +0100628torture_param(bool, kfree_rcu_test_double, false, "Do we run a kfree_rcu() double-argument scale test?");
629torture_param(bool, kfree_rcu_test_single, false, "Do we run a kfree_rcu() single-argument scale test?");
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400630
631static struct task_struct **kfree_reader_tasks;
632static int kfree_nrealthreads;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700633static atomic_t n_kfree_scale_thread_started;
634static atomic_t n_kfree_scale_thread_ended;
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400635
636struct kfree_obj {
637 char kfree_obj[8];
638 struct rcu_head rh;
639};
640
641static int
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700642kfree_scale_thread(void *arg)
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400643{
644 int i, loop = 0;
645 long me = (long)arg;
646 struct kfree_obj *alloc_ptr;
647 u64 start_time, end_time;
Joel Fernandes (Google)12af6602019-12-19 11:22:42 -0500648 long long mem_begin, mem_during = 0;
Uladzislau Rezki (Sony)686fe1b2021-02-17 19:51:10 +0100649 bool kfree_rcu_test_both;
650 DEFINE_TORTURE_RANDOM(tr);
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400651
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700652 VERBOSE_SCALEOUT_STRING("kfree_scale_thread task started");
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400653 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
654 set_user_nice(current, MAX_NICE);
Uladzislau Rezki (Sony)686fe1b2021-02-17 19:51:10 +0100655 kfree_rcu_test_both = (kfree_rcu_test_single == kfree_rcu_test_double);
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400656
657 start_time = ktime_get_mono_fast_ns();
658
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700659 if (atomic_inc_return(&n_kfree_scale_thread_started) >= kfree_nrealthreads) {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400660 if (gp_exp)
661 b_rcu_gp_test_started = cur_ops->exp_completed() / 2;
662 else
663 b_rcu_gp_test_started = cur_ops->get_gp_seq();
664 }
665
666 do {
Joel Fernandes (Google)12af6602019-12-19 11:22:42 -0500667 if (!mem_during) {
668 mem_during = mem_begin = si_mem_available();
669 } else if (loop % (kfree_loops / 4) == 0) {
670 mem_during = (mem_during + si_mem_available()) / 2;
671 }
672
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400673 for (i = 0; i < kfree_alloc_num; i++) {
Joel Fernandes (Google)f87dc802020-03-16 12:32:26 -0400674 alloc_ptr = kmalloc(kfree_mult * sizeof(struct kfree_obj), GFP_KERNEL);
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400675 if (!alloc_ptr)
676 return -ENOMEM;
677
Uladzislau Rezki (Sony)686fe1b2021-02-17 19:51:10 +0100678 // By default kfree_rcu_test_single and kfree_rcu_test_double are
679 // initialized to false. If both have the same value (false or true)
680 // both are randomly tested, otherwise only the one with value true
681 // is tested.
682 if ((kfree_rcu_test_single && !kfree_rcu_test_double) ||
683 (kfree_rcu_test_both && torture_random(&tr) & 0x800))
684 kfree_rcu(alloc_ptr);
685 else
686 kfree_rcu(alloc_ptr, rh);
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400687 }
688
689 cond_resched();
690 } while (!torture_must_stop() && ++loop < kfree_loops);
691
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700692 if (atomic_inc_return(&n_kfree_scale_thread_ended) >= kfree_nrealthreads) {
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400693 end_time = ktime_get_mono_fast_ns();
694
695 if (gp_exp)
696 b_rcu_gp_test_finished = cur_ops->exp_completed() / 2;
697 else
698 b_rcu_gp_test_finished = cur_ops->get_gp_seq();
699
Joel Fernandes (Google)12af6602019-12-19 11:22:42 -0500700 pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400701 (unsigned long long)(end_time - start_time), kfree_loops,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700702 rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
Joel Fernandes (Google)12af6602019-12-19 11:22:42 -0500703 (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
704
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400705 if (shutdown) {
706 smp_mb(); /* Assign before wake. */
707 wake_up(&shutdown_wq);
708 }
709 }
710
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700711 torture_kthread_stopping("kfree_scale_thread");
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400712 return 0;
713}
714
715static void
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700716kfree_scale_cleanup(void)
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400717{
718 int i;
719
720 if (torture_cleanup_begin())
721 return;
722
723 if (kfree_reader_tasks) {
724 for (i = 0; i < kfree_nrealthreads; i++)
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700725 torture_stop_kthread(kfree_scale_thread,
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400726 kfree_reader_tasks[i]);
727 kfree(kfree_reader_tasks);
728 }
729
730 torture_cleanup_end();
731}
732
733/*
734 * shutdown kthread. Just waits to be awakened, then shuts down system.
735 */
736static int
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700737kfree_scale_shutdown(void *arg)
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400738{
Joel Fernandes (Google)7e866462020-05-25 00:36:47 -0400739 wait_event(shutdown_wq,
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700740 atomic_read(&n_kfree_scale_thread_ended) >= kfree_nrealthreads);
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400741
742 smp_mb(); /* Wake before output. */
743
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700744 kfree_scale_cleanup();
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400745 kernel_power_off();
746 return -EINVAL;
747}
748
749static int __init
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700750kfree_scale_init(void)
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400751{
752 long i;
753 int firsterr = 0;
754
755 kfree_nrealthreads = compute_real(kfree_nthreads);
756 /* Start up the kthreads. */
757 if (shutdown) {
758 init_waitqueue_head(&shutdown_wq);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700759 firsterr = torture_create_kthread(kfree_scale_shutdown, NULL,
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400760 shutdown_task);
Paul E. McKenneyeb77abfd2021-08-05 15:58:53 -0700761 if (torture_init_error(firsterr))
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400762 goto unwind;
763 schedule_timeout_uninterruptible(1);
764 }
765
Kefeng Wangb3e2d202020-04-17 12:02:45 +0800766 pr_alert("kfree object size=%zu\n", kfree_mult * sizeof(struct kfree_obj));
Joel Fernandes (Google)f87dc802020-03-16 12:32:26 -0400767
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400768 kfree_reader_tasks = kcalloc(kfree_nrealthreads, sizeof(kfree_reader_tasks[0]),
769 GFP_KERNEL);
770 if (kfree_reader_tasks == NULL) {
771 firsterr = -ENOMEM;
772 goto unwind;
773 }
774
775 for (i = 0; i < kfree_nrealthreads; i++) {
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700776 firsterr = torture_create_kthread(kfree_scale_thread, (void *)i,
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400777 kfree_reader_tasks[i]);
Paul E. McKenneyeb77abfd2021-08-05 15:58:53 -0700778 if (torture_init_error(firsterr))
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400779 goto unwind;
780 }
781
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700782 while (atomic_read(&n_kfree_scale_thread_started) < kfree_nrealthreads)
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400783 schedule_timeout_uninterruptible(1);
784
785 torture_init_end();
786 return 0;
787
788unwind:
789 torture_init_end();
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700790 kfree_scale_cleanup();
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400791 return firsterr;
792}
793
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800794static int __init
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700795rcu_scale_init(void)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800796{
797 long i;
798 int firsterr = 0;
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700799 static struct rcu_scale_ops *scale_ops[] = {
Paul E. McKenney899f3172020-09-09 12:27:03 -0700800 &rcu_ops, &srcu_ops, &srcud_ops, &tasks_ops, &tasks_tracing_ops
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800801 };
802
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700803 if (!torture_init_begin(scale_type, verbose))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800804 return -EBUSY;
805
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700806 /* Process args and announce that the scalability'er is on the job. */
807 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
808 cur_ops = scale_ops[i];
809 if (strcmp(scale_type, cur_ops->name) == 0)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800810 break;
811 }
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700812 if (i == ARRAY_SIZE(scale_ops)) {
813 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
814 pr_alert("rcu-scale types:");
815 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
816 pr_cont(" %s", scale_ops[i]->name);
Joe Perchesa7538352018-05-14 13:27:33 -0700817 pr_cont("\n");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800818 firsterr = -EINVAL;
Paul E. McKenneyad092c02019-03-21 10:26:41 -0700819 cur_ops = NULL;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800820 goto unwind;
821 }
822 if (cur_ops->init)
823 cur_ops->init();
824
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400825 if (kfree_rcu_test)
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700826 return kfree_scale_init();
Joel Fernandes (Google)e6e78b02019-08-30 12:36:29 -0400827
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800828 nrealwriters = compute_real(nwriters);
829 nrealreaders = compute_real(nreaders);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700830 atomic_set(&n_rcu_scale_reader_started, 0);
831 atomic_set(&n_rcu_scale_writer_started, 0);
832 atomic_set(&n_rcu_scale_writer_finished, 0);
833 rcu_scale_print_module_parms(cur_ops, "Start of test");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800834
835 /* Start up the kthreads. */
836
837 if (shutdown) {
838 init_waitqueue_head(&shutdown_wq);
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700839 firsterr = torture_create_kthread(rcu_scale_shutdown, NULL,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800840 shutdown_task);
Paul E. McKenneyeb77abfd2021-08-05 15:58:53 -0700841 if (torture_init_error(firsterr))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800842 goto unwind;
843 schedule_timeout_uninterruptible(1);
844 }
845 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
846 GFP_KERNEL);
847 if (reader_tasks == NULL) {
Li Zhijian86e7ed12021-10-29 17:40:28 +0800848 SCALEOUT_ERRSTRING("out of memory");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800849 firsterr = -ENOMEM;
850 goto unwind;
851 }
852 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700853 firsterr = torture_create_kthread(rcu_scale_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800854 reader_tasks[i]);
Paul E. McKenneyeb77abfd2021-08-05 15:58:53 -0700855 if (torture_init_error(firsterr))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800856 goto unwind;
857 }
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700858 while (atomic_read(&n_rcu_scale_reader_started) < nrealreaders)
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800859 schedule_timeout_uninterruptible(1);
860 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
861 GFP_KERNEL);
862 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
863 GFP_KERNEL);
864 writer_n_durations =
865 kcalloc(nrealwriters, sizeof(*writer_n_durations),
866 GFP_KERNEL);
867 if (!writer_tasks || !writer_durations || !writer_n_durations) {
Li Zhijian86e7ed12021-10-29 17:40:28 +0800868 SCALEOUT_ERRSTRING("out of memory");
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800869 firsterr = -ENOMEM;
870 goto unwind;
871 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800872 for (i = 0; i < nrealwriters; i++) {
873 writer_durations[i] =
874 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
875 GFP_KERNEL);
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000876 if (!writer_durations[i]) {
877 firsterr = -ENOMEM;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800878 goto unwind;
Wei Yongjun05dbbfe2016-06-13 15:20:39 +0000879 }
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700880 firsterr = torture_create_kthread(rcu_scale_writer, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800881 writer_tasks[i]);
Paul E. McKenneyeb77abfd2021-08-05 15:58:53 -0700882 if (torture_init_error(firsterr))
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800883 goto unwind;
884 }
885 torture_init_end();
886 return 0;
887
888unwind:
889 torture_init_end();
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700890 rcu_scale_cleanup();
Paul E. McKenney2f2214d2020-09-17 10:30:46 -0700891 if (shutdown) {
892 WARN_ON(!IS_MODULE(CONFIG_RCU_SCALE_TEST));
893 kernel_power_off();
894 }
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800895 return firsterr;
896}
897
Paul E. McKenney4e88ec42020-08-11 21:18:12 -0700898module_init(rcu_scale_init);
899module_exit(rcu_scale_cleanup);