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