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 << 216 << 217 // Definitions for RCU Tasks ref scale testing 210 // Definitions for RCU Tasks ref scale testing: Empty read markers. 218 // These definitions also work for RCU Rude re 211 // These definitions also work for RCU Rude readers. 219 static void rcu_tasks_ref_scale_read_section(c 212 static void rcu_tasks_ref_scale_read_section(const int nloops) 220 { 213 { 221 int i; 214 int i; 222 215 223 for (i = nloops; i >= 0; i--) 216 for (i = nloops; i >= 0; i--) 224 continue; 217 continue; 225 } 218 } 226 219 227 static void rcu_tasks_ref_scale_delay_section( 220 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl) 228 { 221 { 229 int i; 222 int i; 230 223 231 for (i = nloops; i >= 0; i--) 224 for (i = nloops; i >= 0; i--) 232 un_delay(udl, ndl); 225 un_delay(udl, ndl); 233 } 226 } 234 227 235 static const struct ref_scale_ops rcu_tasks_op !! 228 static struct ref_scale_ops rcu_tasks_ops = { 236 .init = rcu_sync_scale_init, 229 .init = rcu_sync_scale_init, 237 .readsection = rcu_tasks_ref_scale_ 230 .readsection = rcu_tasks_ref_scale_read_section, 238 .delaysection = rcu_tasks_ref_scale_ 231 .delaysection = rcu_tasks_ref_scale_delay_section, 239 .name = "rcu-tasks" 232 .name = "rcu-tasks" 240 }; 233 }; 241 234 242 #define RCU_TASKS_OPS &rcu_tasks_ops, << 243 << 244 #else // #ifdef CONFIG_TASKS_RCU << 245 << 246 #define RCU_TASKS_OPS << 247 << 248 #endif // #else // #ifdef CONFIG_TASKS_RCU << 249 << 250 #ifdef CONFIG_TASKS_TRACE_RCU << 251 << 252 // Definitions for RCU Tasks Trace ref scale t 235 // Definitions for RCU Tasks Trace ref scale testing. 253 static void rcu_trace_ref_scale_read_section(c 236 static void rcu_trace_ref_scale_read_section(const int nloops) 254 { 237 { 255 int i; 238 int i; 256 239 257 for (i = nloops; i >= 0; i--) { 240 for (i = nloops; i >= 0; i--) { 258 rcu_read_lock_trace(); 241 rcu_read_lock_trace(); 259 rcu_read_unlock_trace(); 242 rcu_read_unlock_trace(); 260 } 243 } 261 } 244 } 262 245 263 static void rcu_trace_ref_scale_delay_section( 246 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl) 264 { 247 { 265 int i; 248 int i; 266 249 267 for (i = nloops; i >= 0; i--) { 250 for (i = nloops; i >= 0; i--) { 268 rcu_read_lock_trace(); 251 rcu_read_lock_trace(); 269 un_delay(udl, ndl); 252 un_delay(udl, ndl); 270 rcu_read_unlock_trace(); 253 rcu_read_unlock_trace(); 271 } 254 } 272 } 255 } 273 256 274 static const struct ref_scale_ops rcu_trace_op !! 257 static struct ref_scale_ops rcu_trace_ops = { 275 .init = rcu_sync_scale_init, 258 .init = rcu_sync_scale_init, 276 .readsection = rcu_trace_ref_scale_ 259 .readsection = rcu_trace_ref_scale_read_section, 277 .delaysection = rcu_trace_ref_scale_ 260 .delaysection = rcu_trace_ref_scale_delay_section, 278 .name = "rcu-trace" 261 .name = "rcu-trace" 279 }; 262 }; 280 263 281 #define RCU_TRACE_OPS &rcu_trace_ops, << 282 << 283 #else // #ifdef CONFIG_TASKS_TRACE_RCU << 284 << 285 #define RCU_TRACE_OPS << 286 << 287 #endif // #else // #ifdef CONFIG_TASKS_TRACE_R << 288 << 289 // Definitions for reference count 264 // Definitions for reference count 290 static atomic_t refcnt; 265 static atomic_t refcnt; 291 266 292 static void ref_refcnt_section(const int nloop 267 static void ref_refcnt_section(const int nloops) 293 { 268 { 294 int i; 269 int i; 295 270 296 for (i = nloops; i >= 0; i--) { 271 for (i = nloops; i >= 0; i--) { 297 atomic_inc(&refcnt); 272 atomic_inc(&refcnt); 298 atomic_dec(&refcnt); 273 atomic_dec(&refcnt); 299 } 274 } 300 } 275 } 301 276 302 static void ref_refcnt_delay_section(const int 277 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl) 303 { 278 { 304 int i; 279 int i; 305 280 306 for (i = nloops; i >= 0; i--) { 281 for (i = nloops; i >= 0; i--) { 307 atomic_inc(&refcnt); 282 atomic_inc(&refcnt); 308 un_delay(udl, ndl); 283 un_delay(udl, ndl); 309 atomic_dec(&refcnt); 284 atomic_dec(&refcnt); 310 } 285 } 311 } 286 } 312 287 313 static const struct ref_scale_ops refcnt_ops = !! 288 static struct ref_scale_ops refcnt_ops = { 314 .init = rcu_sync_scale_init, 289 .init = rcu_sync_scale_init, 315 .readsection = ref_refcnt_section, 290 .readsection = ref_refcnt_section, 316 .delaysection = ref_refcnt_delay_sec 291 .delaysection = ref_refcnt_delay_section, 317 .name = "refcnt" 292 .name = "refcnt" 318 }; 293 }; 319 294 320 // Definitions for rwlock 295 // Definitions for rwlock 321 static rwlock_t test_rwlock; 296 static rwlock_t test_rwlock; 322 297 323 static bool ref_rwlock_init(void) !! 298 static void ref_rwlock_init(void) 324 { 299 { 325 rwlock_init(&test_rwlock); 300 rwlock_init(&test_rwlock); 326 return true; << 327 } 301 } 328 302 329 static void ref_rwlock_section(const int nloop 303 static void ref_rwlock_section(const int nloops) 330 { 304 { 331 int i; 305 int i; 332 306 333 for (i = nloops; i >= 0; i--) { 307 for (i = nloops; i >= 0; i--) { 334 read_lock(&test_rwlock); 308 read_lock(&test_rwlock); 335 read_unlock(&test_rwlock); 309 read_unlock(&test_rwlock); 336 } 310 } 337 } 311 } 338 312 339 static void ref_rwlock_delay_section(const int 313 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl) 340 { 314 { 341 int i; 315 int i; 342 316 343 for (i = nloops; i >= 0; i--) { 317 for (i = nloops; i >= 0; i--) { 344 read_lock(&test_rwlock); 318 read_lock(&test_rwlock); 345 un_delay(udl, ndl); 319 un_delay(udl, ndl); 346 read_unlock(&test_rwlock); 320 read_unlock(&test_rwlock); 347 } 321 } 348 } 322 } 349 323 350 static const struct ref_scale_ops rwlock_ops = !! 324 static struct ref_scale_ops rwlock_ops = { 351 .init = ref_rwlock_init, 325 .init = ref_rwlock_init, 352 .readsection = ref_rwlock_section, 326 .readsection = ref_rwlock_section, 353 .delaysection = ref_rwlock_delay_sec 327 .delaysection = ref_rwlock_delay_section, 354 .name = "rwlock" 328 .name = "rwlock" 355 }; 329 }; 356 330 357 // Definitions for rwsem 331 // Definitions for rwsem 358 static struct rw_semaphore test_rwsem; 332 static struct rw_semaphore test_rwsem; 359 333 360 static bool ref_rwsem_init(void) !! 334 static void ref_rwsem_init(void) 361 { 335 { 362 init_rwsem(&test_rwsem); 336 init_rwsem(&test_rwsem); 363 return true; << 364 } 337 } 365 338 366 static void ref_rwsem_section(const int nloops 339 static void ref_rwsem_section(const int nloops) 367 { 340 { 368 int i; 341 int i; 369 342 370 for (i = nloops; i >= 0; i--) { 343 for (i = nloops; i >= 0; i--) { 371 down_read(&test_rwsem); 344 down_read(&test_rwsem); 372 up_read(&test_rwsem); 345 up_read(&test_rwsem); 373 } 346 } 374 } 347 } 375 348 376 static void ref_rwsem_delay_section(const int 349 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl) 377 { 350 { 378 int i; 351 int i; 379 352 380 for (i = nloops; i >= 0; i--) { 353 for (i = nloops; i >= 0; i--) { 381 down_read(&test_rwsem); 354 down_read(&test_rwsem); 382 un_delay(udl, ndl); 355 un_delay(udl, ndl); 383 up_read(&test_rwsem); 356 up_read(&test_rwsem); 384 } 357 } 385 } 358 } 386 359 387 static const struct ref_scale_ops rwsem_ops = !! 360 static struct ref_scale_ops rwsem_ops = { 388 .init = ref_rwsem_init, 361 .init = ref_rwsem_init, 389 .readsection = ref_rwsem_section, 362 .readsection = ref_rwsem_section, 390 .delaysection = ref_rwsem_delay_sect 363 .delaysection = ref_rwsem_delay_section, 391 .name = "rwsem" 364 .name = "rwsem" 392 }; 365 }; 393 366 394 // Definitions for global spinlock 367 // Definitions for global spinlock 395 static DEFINE_RAW_SPINLOCK(test_lock); !! 368 static DEFINE_SPINLOCK(test_lock); 396 369 397 static void ref_lock_section(const int nloops) 370 static void ref_lock_section(const int nloops) 398 { 371 { 399 int i; 372 int i; 400 373 401 preempt_disable(); 374 preempt_disable(); 402 for (i = nloops; i >= 0; i--) { 375 for (i = nloops; i >= 0; i--) { 403 raw_spin_lock(&test_lock); !! 376 spin_lock(&test_lock); 404 raw_spin_unlock(&test_lock); !! 377 spin_unlock(&test_lock); 405 } 378 } 406 preempt_enable(); 379 preempt_enable(); 407 } 380 } 408 381 409 static void ref_lock_delay_section(const int n 382 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl) 410 { 383 { 411 int i; 384 int i; 412 385 413 preempt_disable(); 386 preempt_disable(); 414 for (i = nloops; i >= 0; i--) { 387 for (i = nloops; i >= 0; i--) { 415 raw_spin_lock(&test_lock); !! 388 spin_lock(&test_lock); 416 un_delay(udl, ndl); 389 un_delay(udl, ndl); 417 raw_spin_unlock(&test_lock); !! 390 spin_unlock(&test_lock); 418 } 391 } 419 preempt_enable(); 392 preempt_enable(); 420 } 393 } 421 394 422 static const struct ref_scale_ops lock_ops = { !! 395 static struct ref_scale_ops lock_ops = { 423 .readsection = ref_lock_section, 396 .readsection = ref_lock_section, 424 .delaysection = ref_lock_delay_secti 397 .delaysection = ref_lock_delay_section, 425 .name = "lock" 398 .name = "lock" 426 }; 399 }; 427 400 428 // Definitions for global irq-save spinlock 401 // Definitions for global irq-save spinlock 429 402 430 static void ref_lock_irq_section(const int nlo 403 static void ref_lock_irq_section(const int nloops) 431 { 404 { 432 unsigned long flags; 405 unsigned long flags; 433 int i; 406 int i; 434 407 435 preempt_disable(); 408 preempt_disable(); 436 for (i = nloops; i >= 0; i--) { 409 for (i = nloops; i >= 0; i--) { 437 raw_spin_lock_irqsave(&test_lo !! 410 spin_lock_irqsave(&test_lock, flags); 438 raw_spin_unlock_irqrestore(&te !! 411 spin_unlock_irqrestore(&test_lock, flags); 439 } 412 } 440 preempt_enable(); 413 preempt_enable(); 441 } 414 } 442 415 443 static void ref_lock_irq_delay_section(const i 416 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl) 444 { 417 { 445 unsigned long flags; 418 unsigned long flags; 446 int i; 419 int i; 447 420 448 preempt_disable(); 421 preempt_disable(); 449 for (i = nloops; i >= 0; i--) { 422 for (i = nloops; i >= 0; i--) { 450 raw_spin_lock_irqsave(&test_lo !! 423 spin_lock_irqsave(&test_lock, flags); 451 un_delay(udl, ndl); 424 un_delay(udl, ndl); 452 raw_spin_unlock_irqrestore(&te !! 425 spin_unlock_irqrestore(&test_lock, flags); 453 } 426 } 454 preempt_enable(); 427 preempt_enable(); 455 } 428 } 456 429 457 static const struct ref_scale_ops lock_irq_ops !! 430 static struct ref_scale_ops lock_irq_ops = { 458 .readsection = ref_lock_irq_section 431 .readsection = ref_lock_irq_section, 459 .delaysection = ref_lock_irq_delay_s 432 .delaysection = ref_lock_irq_delay_section, 460 .name = "lock-irq" 433 .name = "lock-irq" 461 }; 434 }; 462 435 463 // Definitions acquire-release. 436 // Definitions acquire-release. 464 static DEFINE_PER_CPU(unsigned long, test_acqr 437 static DEFINE_PER_CPU(unsigned long, test_acqrel); 465 438 466 static void ref_acqrel_section(const int nloop 439 static void ref_acqrel_section(const int nloops) 467 { 440 { 468 unsigned long x; 441 unsigned long x; 469 int i; 442 int i; 470 443 471 preempt_disable(); 444 preempt_disable(); 472 for (i = nloops; i >= 0; i--) { 445 for (i = nloops; i >= 0; i--) { 473 x = smp_load_acquire(this_cpu_ 446 x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); 474 smp_store_release(this_cpu_ptr 447 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); 475 } 448 } 476 preempt_enable(); 449 preempt_enable(); 477 } 450 } 478 451 479 static void ref_acqrel_delay_section(const int 452 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl) 480 { 453 { 481 unsigned long x; 454 unsigned long x; 482 int i; 455 int i; 483 456 484 preempt_disable(); 457 preempt_disable(); 485 for (i = nloops; i >= 0; i--) { 458 for (i = nloops; i >= 0; i--) { 486 x = smp_load_acquire(this_cpu_ 459 x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); 487 un_delay(udl, ndl); 460 un_delay(udl, ndl); 488 smp_store_release(this_cpu_ptr 461 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); 489 } 462 } 490 preempt_enable(); 463 preempt_enable(); 491 } 464 } 492 465 493 static const struct ref_scale_ops acqrel_ops = !! 466 static struct ref_scale_ops acqrel_ops = { 494 .readsection = ref_acqrel_section, 467 .readsection = ref_acqrel_section, 495 .delaysection = ref_acqrel_delay_sec 468 .delaysection = ref_acqrel_delay_section, 496 .name = "acqrel" 469 .name = "acqrel" 497 }; 470 }; 498 471 499 static volatile u64 stopopts; 472 static volatile u64 stopopts; 500 473 501 static void ref_clock_section(const int nloops 474 static void ref_clock_section(const int nloops) 502 { 475 { 503 u64 x = 0; 476 u64 x = 0; 504 int i; 477 int i; 505 478 506 preempt_disable(); 479 preempt_disable(); 507 for (i = nloops; i >= 0; i--) 480 for (i = nloops; i >= 0; i--) 508 x += ktime_get_real_fast_ns(); 481 x += ktime_get_real_fast_ns(); 509 preempt_enable(); 482 preempt_enable(); 510 stopopts = x; 483 stopopts = x; 511 } 484 } 512 485 513 static void ref_clock_delay_section(const int 486 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl) 514 { 487 { 515 u64 x = 0; 488 u64 x = 0; 516 int i; 489 int i; 517 490 518 preempt_disable(); 491 preempt_disable(); 519 for (i = nloops; i >= 0; i--) { 492 for (i = nloops; i >= 0; i--) { 520 x += ktime_get_real_fast_ns(); 493 x += ktime_get_real_fast_ns(); 521 un_delay(udl, ndl); 494 un_delay(udl, ndl); 522 } 495 } 523 preempt_enable(); 496 preempt_enable(); 524 stopopts = x; 497 stopopts = x; 525 } 498 } 526 499 527 static const struct ref_scale_ops clock_ops = !! 500 static struct ref_scale_ops clock_ops = { 528 .readsection = ref_clock_section, 501 .readsection = ref_clock_section, 529 .delaysection = ref_clock_delay_sect 502 .delaysection = ref_clock_delay_section, 530 .name = "clock" 503 .name = "clock" 531 }; 504 }; 532 505 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) 506 static void rcu_scale_one_reader(void) 797 { 507 { 798 if (readdelay <= 0) 508 if (readdelay <= 0) 799 cur_ops->readsection(loops); 509 cur_ops->readsection(loops); 800 else 510 else 801 cur_ops->delaysection(loops, r 511 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000); 802 } 512 } 803 513 804 // Reader kthread. Repeatedly does empty RCU 514 // Reader kthread. Repeatedly does empty RCU read-side 805 // critical section, minimizing update-side in 515 // critical section, minimizing update-side interference. 806 static int 516 static int 807 ref_scale_reader(void *arg) 517 ref_scale_reader(void *arg) 808 { 518 { 809 unsigned long flags; 519 unsigned long flags; 810 long me = (long)arg; 520 long me = (long)arg; 811 struct reader_task *rt = &(reader_task 521 struct reader_task *rt = &(reader_tasks[me]); 812 u64 start; 522 u64 start; 813 s64 duration; 523 s64 duration; 814 524 815 VERBOSE_SCALEOUT_BATCH("ref_scale_read 525 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me); 816 WARN_ON_ONCE(set_cpus_allowed_ptr(curr 526 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids))); 817 set_user_nice(current, MAX_NICE); 527 set_user_nice(current, MAX_NICE); 818 atomic_inc(&n_init); 528 atomic_inc(&n_init); 819 if (holdoff) 529 if (holdoff) 820 schedule_timeout_interruptible 530 schedule_timeout_interruptible(holdoff * HZ); 821 repeat: 531 repeat: 822 VERBOSE_SCALEOUT_BATCH("ref_scale_read 532 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id()); 823 533 824 // Wait for signal that this reader ca 534 // Wait for signal that this reader can start. 825 wait_event(rt->wq, (atomic_read(&nread 535 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) || 826 torture_must_stop() 536 torture_must_stop()); 827 537 828 if (torture_must_stop()) 538 if (torture_must_stop()) 829 goto end; 539 goto end; 830 540 831 // Make sure that the CPU is affinitiz 541 // Make sure that the CPU is affinitized appropriately during testing. 832 WARN_ON_ONCE(raw_smp_processor_id() != 542 WARN_ON_ONCE(raw_smp_processor_id() != me); 833 543 834 WRITE_ONCE(rt->start_reader, 0); 544 WRITE_ONCE(rt->start_reader, 0); 835 if (!atomic_dec_return(&n_started)) 545 if (!atomic_dec_return(&n_started)) 836 while (atomic_read_acquire(&n_ 546 while (atomic_read_acquire(&n_started)) 837 cpu_relax(); 547 cpu_relax(); 838 548 839 VERBOSE_SCALEOUT_BATCH("ref_scale_read 549 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx); 840 550 841 551 842 // To reduce noise, do an initial cach 552 // To reduce noise, do an initial cache-warming invocation, check 843 // in, and then keep warming until eve 553 // in, and then keep warming until everyone has checked in. 844 rcu_scale_one_reader(); 554 rcu_scale_one_reader(); 845 if (!atomic_dec_return(&n_warmedup)) 555 if (!atomic_dec_return(&n_warmedup)) 846 while (atomic_read_acquire(&n_ 556 while (atomic_read_acquire(&n_warmedup)) 847 rcu_scale_one_reader() 557 rcu_scale_one_reader(); 848 // Also keep interrupts disabled. Thi 558 // Also keep interrupts disabled. This also has the effect 849 // of preventing entries into slow pat 559 // of preventing entries into slow path for rcu_read_unlock(). 850 local_irq_save(flags); 560 local_irq_save(flags); 851 start = ktime_get_mono_fast_ns(); 561 start = ktime_get_mono_fast_ns(); 852 562 853 rcu_scale_one_reader(); 563 rcu_scale_one_reader(); 854 564 855 duration = ktime_get_mono_fast_ns() - 565 duration = ktime_get_mono_fast_ns() - start; 856 local_irq_restore(flags); 566 local_irq_restore(flags); 857 567 858 rt->last_duration_ns = WARN_ON_ONCE(du 568 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration; 859 // To reduce runtime-skew noise, do ma 569 // To reduce runtime-skew noise, do maintain-load invocations until 860 // everyone is done. 570 // everyone is done. 861 if (!atomic_dec_return(&n_cooleddown)) 571 if (!atomic_dec_return(&n_cooleddown)) 862 while (atomic_read_acquire(&n_ 572 while (atomic_read_acquire(&n_cooleddown)) 863 rcu_scale_one_reader() 573 rcu_scale_one_reader(); 864 574 865 if (atomic_dec_and_test(&nreaders_exp) 575 if (atomic_dec_and_test(&nreaders_exp)) 866 wake_up(&main_wq); 576 wake_up(&main_wq); 867 577 868 VERBOSE_SCALEOUT_BATCH("ref_scale_read 578 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)", 869 me, exp_idx, a 579 me, exp_idx, atomic_read(&nreaders_exp)); 870 580 871 if (!torture_must_stop()) 581 if (!torture_must_stop()) 872 goto repeat; 582 goto repeat; 873 end: 583 end: 874 torture_kthread_stopping("ref_scale_re 584 torture_kthread_stopping("ref_scale_reader"); 875 return 0; 585 return 0; 876 } 586 } 877 587 878 static void reset_readers(void) 588 static void reset_readers(void) 879 { 589 { 880 int i; 590 int i; 881 struct reader_task *rt; 591 struct reader_task *rt; 882 592 883 for (i = 0; i < nreaders; i++) { 593 for (i = 0; i < nreaders; i++) { 884 rt = &(reader_tasks[i]); 594 rt = &(reader_tasks[i]); 885 595 886 rt->last_duration_ns = 0; 596 rt->last_duration_ns = 0; 887 } 597 } 888 } 598 } 889 599 890 // Print the results of each reader and return 600 // Print the results of each reader and return the sum of all their durations. 891 static u64 process_durations(int n) 601 static u64 process_durations(int n) 892 { 602 { 893 int i; 603 int i; 894 struct reader_task *rt; 604 struct reader_task *rt; 895 struct seq_buf s; !! 605 char buf1[64]; 896 char *buf; 606 char *buf; 897 u64 sum = 0; 607 u64 sum = 0; 898 608 899 buf = kmalloc(800 + 64, GFP_KERNEL); 609 buf = kmalloc(800 + 64, GFP_KERNEL); 900 if (!buf) 610 if (!buf) 901 return 0; 611 return 0; 902 seq_buf_init(&s, buf, 800 + 64); !! 612 buf[0] = 0; 903 !! 613 sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", 904 seq_buf_printf(&s, "Experiment #%d (Fo !! 614 exp_idx); 905 exp_idx); << 906 615 907 for (i = 0; i < n && !torture_must_sto 616 for (i = 0; i < n && !torture_must_stop(); i++) { 908 rt = &(reader_tasks[i]); 617 rt = &(reader_tasks[i]); >> 618 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); 909 619 910 if (i % 5 == 0) 620 if (i % 5 == 0) 911 seq_buf_putc(&s, '\n') !! 621 strcat(buf, "\n"); 912 !! 622 if (strlen(buf) >= 800) { 913 if (seq_buf_used(&s) >= 800) { !! 623 pr_alert("%s", buf); 914 pr_alert("%s", seq_buf !! 624 buf[0] = 0; 915 seq_buf_clear(&s); << 916 } 625 } 917 !! 626 strcat(buf, buf1); 918 seq_buf_printf(&s, "%d: %llu\t << 919 627 920 sum += rt->last_duration_ns; 628 sum += rt->last_duration_ns; 921 } 629 } 922 pr_alert("%s\n", seq_buf_str(&s)); !! 630 pr_alert("%s\n", buf); 923 631 924 kfree(buf); 632 kfree(buf); 925 return sum; 633 return sum; 926 } 634 } 927 635 928 // The main_func is the main orchestrator, it 636 // The main_func is the main orchestrator, it performs a bunch of 929 // experiments. For every experiment, it orde 637 // experiments. For every experiment, it orders all the readers 930 // involved to start and waits for them to fin 638 // involved to start and waits for them to finish the experiment. It 931 // then reads their timestamps and starts the 639 // then reads their timestamps and starts the next experiment. Each 932 // experiment progresses from 1 concurrent rea 640 // experiment progresses from 1 concurrent reader to N of them at which 933 // point all the timestamps are printed. 641 // point all the timestamps are printed. 934 static int main_func(void *arg) 642 static int main_func(void *arg) 935 { 643 { 936 int exp, r; 644 int exp, r; 937 char buf1[64]; 645 char buf1[64]; 938 char *buf; 646 char *buf; 939 u64 *result_avg; 647 u64 *result_avg; 940 648 941 set_cpus_allowed_ptr(current, cpumask_ 649 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids)); 942 set_user_nice(current, MAX_NICE); 650 set_user_nice(current, MAX_NICE); 943 651 944 VERBOSE_SCALEOUT("main_func task start 652 VERBOSE_SCALEOUT("main_func task started"); 945 result_avg = kzalloc(nruns * sizeof(*r 653 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL); 946 buf = kzalloc(800 + 64, GFP_KERNEL); 654 buf = kzalloc(800 + 64, GFP_KERNEL); 947 if (!result_avg || !buf) { 655 if (!result_avg || !buf) { 948 SCALEOUT_ERRSTRING("out of mem 656 SCALEOUT_ERRSTRING("out of memory"); 949 goto oom_exit; 657 goto oom_exit; 950 } 658 } 951 if (holdoff) 659 if (holdoff) 952 schedule_timeout_interruptible 660 schedule_timeout_interruptible(holdoff * HZ); 953 661 954 // Wait for all threads to start. 662 // Wait for all threads to start. 955 atomic_inc(&n_init); 663 atomic_inc(&n_init); 956 while (atomic_read(&n_init) < nreaders 664 while (atomic_read(&n_init) < nreaders + 1) 957 schedule_timeout_uninterruptib 665 schedule_timeout_uninterruptible(1); 958 666 959 // Start exp readers up per experiment 667 // Start exp readers up per experiment 960 for (exp = 0; exp < nruns && !torture_ 668 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) { 961 if (torture_must_stop()) 669 if (torture_must_stop()) 962 goto end; 670 goto end; 963 671 964 reset_readers(); 672 reset_readers(); 965 atomic_set(&nreaders_exp, nrea 673 atomic_set(&nreaders_exp, nreaders); 966 atomic_set(&n_started, nreader 674 atomic_set(&n_started, nreaders); 967 atomic_set(&n_warmedup, nreade 675 atomic_set(&n_warmedup, nreaders); 968 atomic_set(&n_cooleddown, nrea 676 atomic_set(&n_cooleddown, nreaders); 969 677 970 exp_idx = exp; 678 exp_idx = exp; 971 679 972 for (r = 0; r < nreaders; r++) 680 for (r = 0; r < nreaders; r++) { 973 smp_store_release(&rea 681 smp_store_release(&reader_tasks[r].start_reader, 1); 974 wake_up(&reader_tasks[ 682 wake_up(&reader_tasks[r].wq); 975 } 683 } 976 684 977 VERBOSE_SCALEOUT("main_func: e 685 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers", 978 nreaders); 686 nreaders); 979 687 980 wait_event(main_wq, 688 wait_event(main_wq, 981 !atomic_read(&nread 689 !atomic_read(&nreaders_exp) || torture_must_stop()); 982 690 983 VERBOSE_SCALEOUT("main_func: e 691 VERBOSE_SCALEOUT("main_func: experiment ended"); 984 692 985 if (torture_must_stop()) 693 if (torture_must_stop()) 986 goto end; 694 goto end; 987 695 988 result_avg[exp] = div_u64(1000 696 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops); 989 } 697 } 990 698 991 // Print the average of all experiment 699 // Print the average of all experiments 992 SCALEOUT("END OF TEST. Calculating ave 700 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n"); 993 701 994 pr_alert("Runs\tTime(ns)\n"); 702 pr_alert("Runs\tTime(ns)\n"); 995 for (exp = 0; exp < nruns; exp++) { 703 for (exp = 0; exp < nruns; exp++) { 996 u64 avg; 704 u64 avg; 997 u32 rem; 705 u32 rem; 998 706 999 avg = div_u64_rem(result_avg[e 707 avg = div_u64_rem(result_avg[exp], 1000, &rem); 1000 sprintf(buf1, "%d\t%llu.%03u\ 708 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem); 1001 strcat(buf, buf1); 709 strcat(buf, buf1); 1002 if (strlen(buf) >= 800) { 710 if (strlen(buf) >= 800) { 1003 pr_alert("%s", buf); 711 pr_alert("%s", buf); 1004 buf[0] = 0; 712 buf[0] = 0; 1005 } 713 } 1006 } 714 } 1007 715 1008 pr_alert("%s", buf); 716 pr_alert("%s", buf); 1009 717 1010 oom_exit: 718 oom_exit: 1011 // This will shutdown everything incl 719 // This will shutdown everything including us. 1012 if (shutdown) { 720 if (shutdown) { 1013 shutdown_start = 1; 721 shutdown_start = 1; 1014 wake_up(&shutdown_wq); 722 wake_up(&shutdown_wq); 1015 } 723 } 1016 724 1017 // Wait for torture to stop us 725 // Wait for torture to stop us 1018 while (!torture_must_stop()) 726 while (!torture_must_stop()) 1019 schedule_timeout_uninterrupti 727 schedule_timeout_uninterruptible(1); 1020 728 1021 end: 729 end: 1022 torture_kthread_stopping("main_func") 730 torture_kthread_stopping("main_func"); 1023 kfree(result_avg); 731 kfree(result_avg); 1024 kfree(buf); 732 kfree(buf); 1025 return 0; 733 return 0; 1026 } 734 } 1027 735 1028 static void 736 static void 1029 ref_scale_print_module_parms(const struct ref !! 737 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag) 1030 { 738 { 1031 pr_alert("%s" SCALE_FLAG 739 pr_alert("%s" SCALE_FLAG 1032 "--- %s: verbose=%d verbose !! 740 "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag, 1033 verbose, verbose_batched, sh !! 741 verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay); 1034 } 742 } 1035 743 1036 static void 744 static void 1037 ref_scale_cleanup(void) 745 ref_scale_cleanup(void) 1038 { 746 { 1039 int i; 747 int i; 1040 748 1041 if (torture_cleanup_begin()) 749 if (torture_cleanup_begin()) 1042 return; 750 return; 1043 751 1044 if (!cur_ops) { 752 if (!cur_ops) { 1045 torture_cleanup_end(); 753 torture_cleanup_end(); 1046 return; 754 return; 1047 } 755 } 1048 756 1049 if (reader_tasks) { 757 if (reader_tasks) { 1050 for (i = 0; i < nreaders; i++ 758 for (i = 0; i < nreaders; i++) 1051 torture_stop_kthread( 759 torture_stop_kthread("ref_scale_reader", 1052 760 reader_tasks[i].task); 1053 } 761 } 1054 kfree(reader_tasks); 762 kfree(reader_tasks); 1055 763 1056 torture_stop_kthread("main_task", mai 764 torture_stop_kthread("main_task", main_task); 1057 kfree(main_task); 765 kfree(main_task); 1058 766 1059 // Do scale-type-specific cleanup ope 767 // Do scale-type-specific cleanup operations. 1060 if (cur_ops->cleanup != NULL) 768 if (cur_ops->cleanup != NULL) 1061 cur_ops->cleanup(); 769 cur_ops->cleanup(); 1062 770 1063 torture_cleanup_end(); 771 torture_cleanup_end(); 1064 } 772 } 1065 773 1066 // Shutdown kthread. Just waits to be awaken 774 // Shutdown kthread. Just waits to be awakened, then shuts down system. 1067 static int 775 static int 1068 ref_scale_shutdown(void *arg) 776 ref_scale_shutdown(void *arg) 1069 { 777 { 1070 wait_event_idle(shutdown_wq, shutdown !! 778 wait_event(shutdown_wq, shutdown_start); 1071 779 1072 smp_mb(); // Wake before output. 780 smp_mb(); // Wake before output. 1073 ref_scale_cleanup(); 781 ref_scale_cleanup(); 1074 kernel_power_off(); 782 kernel_power_off(); 1075 783 1076 return -EINVAL; 784 return -EINVAL; 1077 } 785 } 1078 786 1079 static int __init 787 static int __init 1080 ref_scale_init(void) 788 ref_scale_init(void) 1081 { 789 { 1082 long i; 790 long i; 1083 int firsterr = 0; 791 int firsterr = 0; 1084 static const struct ref_scale_ops *sc !! 792 static struct ref_scale_ops *scale_ops[] = { 1085 &rcu_ops, &srcu_ops, RCU_TRAC !! 793 &rcu_ops, &srcu_ops, &rcu_trace_ops, &rcu_tasks_ops, &refcnt_ops, &rwlock_ops, 1086 &rwsem_ops, &lock_ops, &lock_ !! 794 &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops, 1087 &typesafe_ref_ops, &typesafe_ << 1088 }; 795 }; 1089 796 1090 if (!torture_init_begin(scale_type, v 797 if (!torture_init_begin(scale_type, verbose)) 1091 return -EBUSY; 798 return -EBUSY; 1092 799 1093 for (i = 0; i < ARRAY_SIZE(scale_ops) 800 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) { 1094 cur_ops = scale_ops[i]; 801 cur_ops = scale_ops[i]; 1095 if (strcmp(scale_type, cur_op 802 if (strcmp(scale_type, cur_ops->name) == 0) 1096 break; 803 break; 1097 } 804 } 1098 if (i == ARRAY_SIZE(scale_ops)) { 805 if (i == ARRAY_SIZE(scale_ops)) { 1099 pr_alert("rcu-scale: invalid 806 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type); 1100 pr_alert("rcu-scale types:"); 807 pr_alert("rcu-scale types:"); 1101 for (i = 0; i < ARRAY_SIZE(sc 808 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) 1102 pr_cont(" %s", scale_ 809 pr_cont(" %s", scale_ops[i]->name); 1103 pr_cont("\n"); 810 pr_cont("\n"); 1104 firsterr = -EINVAL; 811 firsterr = -EINVAL; 1105 cur_ops = NULL; 812 cur_ops = NULL; 1106 goto unwind; 813 goto unwind; 1107 } 814 } 1108 if (cur_ops->init) 815 if (cur_ops->init) 1109 if (!cur_ops->init()) { !! 816 cur_ops->init(); 1110 firsterr = -EUCLEAN; << 1111 goto unwind; << 1112 } << 1113 817 1114 ref_scale_print_module_parms(cur_ops, 818 ref_scale_print_module_parms(cur_ops, "Start of test"); 1115 819 1116 // Shutdown task 820 // Shutdown task 1117 if (shutdown) { 821 if (shutdown) { 1118 init_waitqueue_head(&shutdown 822 init_waitqueue_head(&shutdown_wq); 1119 firsterr = torture_create_kth 823 firsterr = torture_create_kthread(ref_scale_shutdown, NULL, 1120 824 shutdown_task); 1121 if (torture_init_error(firste 825 if (torture_init_error(firsterr)) 1122 goto unwind; 826 goto unwind; 1123 schedule_timeout_uninterrupti 827 schedule_timeout_uninterruptible(1); 1124 } 828 } 1125 829 1126 // Reader tasks (default to ~75% of o 830 // Reader tasks (default to ~75% of online CPUs). 1127 if (nreaders < 0) 831 if (nreaders < 0) 1128 nreaders = (num_online_cpus() 832 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2); 1129 if (WARN_ONCE(loops <= 0, "%s: loops 833 if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops)) 1130 loops = 1; 834 loops = 1; 1131 if (WARN_ONCE(nreaders <= 0, "%s: nre 835 if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders)) 1132 nreaders = 1; 836 nreaders = 1; 1133 if (WARN_ONCE(nruns <= 0, "%s: nruns 837 if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns)) 1134 nruns = 1; 838 nruns = 1; 1135 reader_tasks = kcalloc(nreaders, size 839 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]), 1136 GFP_KERNEL); 840 GFP_KERNEL); 1137 if (!reader_tasks) { 841 if (!reader_tasks) { 1138 SCALEOUT_ERRSTRING("out of me 842 SCALEOUT_ERRSTRING("out of memory"); 1139 firsterr = -ENOMEM; 843 firsterr = -ENOMEM; 1140 goto unwind; 844 goto unwind; 1141 } 845 } 1142 846 1143 VERBOSE_SCALEOUT("Starting %d reader 847 VERBOSE_SCALEOUT("Starting %d reader threads", nreaders); 1144 848 1145 for (i = 0; i < nreaders; i++) { 849 for (i = 0; i < nreaders; i++) { 1146 init_waitqueue_head(&reader_t << 1147 firsterr = torture_create_kth 850 firsterr = torture_create_kthread(ref_scale_reader, (void *)i, 1148 851 reader_tasks[i].task); 1149 if (torture_init_error(firste 852 if (torture_init_error(firsterr)) 1150 goto unwind; 853 goto unwind; >> 854 >> 855 init_waitqueue_head(&(reader_tasks[i].wq)); 1151 } 856 } 1152 857 1153 // Main Task 858 // Main Task 1154 init_waitqueue_head(&main_wq); 859 init_waitqueue_head(&main_wq); 1155 firsterr = torture_create_kthread(mai 860 firsterr = torture_create_kthread(main_func, NULL, main_task); 1156 if (torture_init_error(firsterr)) 861 if (torture_init_error(firsterr)) 1157 goto unwind; 862 goto unwind; 1158 863 1159 torture_init_end(); 864 torture_init_end(); 1160 return 0; 865 return 0; 1161 866 1162 unwind: 867 unwind: 1163 torture_init_end(); 868 torture_init_end(); 1164 ref_scale_cleanup(); 869 ref_scale_cleanup(); 1165 if (shutdown) { 870 if (shutdown) { 1166 WARN_ON(!IS_MODULE(CONFIG_RCU 871 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST)); 1167 kernel_power_off(); 872 kernel_power_off(); 1168 } 873 } 1169 return firsterr; 874 return firsterr; 1170 } 875 } 1171 876 1172 module_init(ref_scale_init); 877 module_init(ref_scale_init); 1173 module_exit(ref_scale_cleanup); 878 module_exit(ref_scale_cleanup); 1174 879
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.