0001
0002
0003
0004
0005
0006
0007
0008 #ifdef CONFIG_TASKS_RCU_GENERIC
0009 #include "rcu_segcblist.h"
0010
0011
0012
0013
0014
0015 struct rcu_tasks;
0016 typedef void (*rcu_tasks_gp_func_t)(struct rcu_tasks *rtp);
0017 typedef void (*pregp_func_t)(struct list_head *hop);
0018 typedef void (*pertask_func_t)(struct task_struct *t, struct list_head *hop);
0019 typedef void (*postscan_func_t)(struct list_head *hop);
0020 typedef void (*holdouts_func_t)(struct list_head *hop, bool ndrpt, bool *frptp);
0021 typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 struct rcu_tasks_percpu {
0037 struct rcu_segcblist cblist;
0038 raw_spinlock_t __private lock;
0039 unsigned long rtp_jiffies;
0040 unsigned long rtp_n_lock_retries;
0041 struct work_struct rtp_work;
0042 struct irq_work rtp_irq_work;
0043 struct rcu_head barrier_q_head;
0044 struct list_head rtp_blkd_tasks;
0045 int cpu;
0046 struct rcu_tasks *rtpp;
0047 };
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 struct rcu_tasks {
0083 struct rcuwait cbs_wait;
0084 raw_spinlock_t cbs_gbl_lock;
0085 struct mutex tasks_gp_mutex;
0086 int gp_state;
0087 int gp_sleep;
0088 int init_fract;
0089 unsigned long gp_jiffies;
0090 unsigned long gp_start;
0091 unsigned long tasks_gp_seq;
0092 unsigned long n_ipis;
0093 unsigned long n_ipis_fails;
0094 struct task_struct *kthread_ptr;
0095 rcu_tasks_gp_func_t gp_func;
0096 pregp_func_t pregp_func;
0097 pertask_func_t pertask_func;
0098 postscan_func_t postscan_func;
0099 holdouts_func_t holdouts_func;
0100 postgp_func_t postgp_func;
0101 call_rcu_func_t call_func;
0102 struct rcu_tasks_percpu __percpu *rtpcpu;
0103 int percpu_enqueue_shift;
0104 int percpu_enqueue_lim;
0105 int percpu_dequeue_lim;
0106 unsigned long percpu_dequeue_gpseq;
0107 struct mutex barrier_q_mutex;
0108 atomic_t barrier_q_count;
0109 struct completion barrier_q_completion;
0110 unsigned long barrier_q_seq;
0111 char *name;
0112 char *kname;
0113 };
0114
0115 static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp);
0116
0117 #define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
0118 static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = { \
0119 .lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock), \
0120 .rtp_irq_work = IRQ_WORK_INIT_HARD(call_rcu_tasks_iw_wakeup), \
0121 }; \
0122 static struct rcu_tasks rt_name = \
0123 { \
0124 .cbs_wait = __RCUWAIT_INITIALIZER(rt_name.wait), \
0125 .cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock), \
0126 .tasks_gp_mutex = __MUTEX_INITIALIZER(rt_name.tasks_gp_mutex), \
0127 .gp_func = gp, \
0128 .call_func = call, \
0129 .rtpcpu = &rt_name ## __percpu, \
0130 .name = n, \
0131 .percpu_enqueue_shift = order_base_2(CONFIG_NR_CPUS), \
0132 .percpu_enqueue_lim = 1, \
0133 .percpu_dequeue_lim = 1, \
0134 .barrier_q_mutex = __MUTEX_INITIALIZER(rt_name.barrier_q_mutex), \
0135 .barrier_q_seq = (0UL - 50UL) << RCU_SEQ_CTR_SHIFT, \
0136 .kname = #rt_name, \
0137 }
0138
0139
0140 DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
0141
0142
0143 #define RCU_TASK_IPI_DELAY (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) ? HZ / 2 : 0)
0144 static int rcu_task_ipi_delay __read_mostly = RCU_TASK_IPI_DELAY;
0145 module_param(rcu_task_ipi_delay, int, 0644);
0146
0147
0148 #define RCU_TASK_BOOT_STALL_TIMEOUT (HZ * 30)
0149 #define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
0150 static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
0151 module_param(rcu_task_stall_timeout, int, 0644);
0152 #define RCU_TASK_STALL_INFO (HZ * 10)
0153 static int rcu_task_stall_info __read_mostly = RCU_TASK_STALL_INFO;
0154 module_param(rcu_task_stall_info, int, 0644);
0155 static int rcu_task_stall_info_mult __read_mostly = 3;
0156 module_param(rcu_task_stall_info_mult, int, 0444);
0157
0158 static int rcu_task_enqueue_lim __read_mostly = -1;
0159 module_param(rcu_task_enqueue_lim, int, 0444);
0160
0161 static bool rcu_task_cb_adjust;
0162 static int rcu_task_contend_lim __read_mostly = 100;
0163 module_param(rcu_task_contend_lim, int, 0444);
0164 static int rcu_task_collapse_lim __read_mostly = 10;
0165 module_param(rcu_task_collapse_lim, int, 0444);
0166
0167
0168 #define RTGS_INIT 0
0169 #define RTGS_WAIT_WAIT_CBS 1
0170 #define RTGS_WAIT_GP 2
0171 #define RTGS_PRE_WAIT_GP 3
0172 #define RTGS_SCAN_TASKLIST 4
0173 #define RTGS_POST_SCAN_TASKLIST 5
0174 #define RTGS_WAIT_SCAN_HOLDOUTS 6
0175 #define RTGS_SCAN_HOLDOUTS 7
0176 #define RTGS_POST_GP 8
0177 #define RTGS_WAIT_READERS 9
0178 #define RTGS_INVOKE_CBS 10
0179 #define RTGS_WAIT_CBS 11
0180 #ifndef CONFIG_TINY_RCU
0181 static const char * const rcu_tasks_gp_state_names[] = {
0182 "RTGS_INIT",
0183 "RTGS_WAIT_WAIT_CBS",
0184 "RTGS_WAIT_GP",
0185 "RTGS_PRE_WAIT_GP",
0186 "RTGS_SCAN_TASKLIST",
0187 "RTGS_POST_SCAN_TASKLIST",
0188 "RTGS_WAIT_SCAN_HOLDOUTS",
0189 "RTGS_SCAN_HOLDOUTS",
0190 "RTGS_POST_GP",
0191 "RTGS_WAIT_READERS",
0192 "RTGS_INVOKE_CBS",
0193 "RTGS_WAIT_CBS",
0194 };
0195 #endif
0196
0197
0198
0199
0200
0201 static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp);
0202
0203
0204 static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
0205 {
0206 rtp->gp_state = newstate;
0207 rtp->gp_jiffies = jiffies;
0208 }
0209
0210 #ifndef CONFIG_TINY_RCU
0211
0212 static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
0213 {
0214 int i = data_race(rtp->gp_state);
0215 int j = READ_ONCE(i);
0216
0217 if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
0218 return "???";
0219 return rcu_tasks_gp_state_names[j];
0220 }
0221 #endif
0222
0223
0224
0225 static void cblist_init_generic(struct rcu_tasks *rtp)
0226 {
0227 int cpu;
0228 unsigned long flags;
0229 int lim;
0230 int shift;
0231
0232 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
0233 if (rcu_task_enqueue_lim < 0) {
0234 rcu_task_enqueue_lim = 1;
0235 rcu_task_cb_adjust = true;
0236 pr_info("%s: Setting adjustable number of callback queues.\n", __func__);
0237 } else if (rcu_task_enqueue_lim == 0) {
0238 rcu_task_enqueue_lim = 1;
0239 }
0240 lim = rcu_task_enqueue_lim;
0241
0242 if (lim > nr_cpu_ids)
0243 lim = nr_cpu_ids;
0244 shift = ilog2(nr_cpu_ids / lim);
0245 if (((nr_cpu_ids - 1) >> shift) >= lim)
0246 shift++;
0247 WRITE_ONCE(rtp->percpu_enqueue_shift, shift);
0248 WRITE_ONCE(rtp->percpu_dequeue_lim, lim);
0249 smp_store_release(&rtp->percpu_enqueue_lim, lim);
0250 for_each_possible_cpu(cpu) {
0251 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
0252
0253 WARN_ON_ONCE(!rtpcp);
0254 if (cpu)
0255 raw_spin_lock_init(&ACCESS_PRIVATE(rtpcp, lock));
0256 raw_spin_lock_rcu_node(rtpcp);
0257 if (rcu_segcblist_empty(&rtpcp->cblist))
0258 rcu_segcblist_init(&rtpcp->cblist);
0259 INIT_WORK(&rtpcp->rtp_work, rcu_tasks_invoke_cbs_wq);
0260 rtpcp->cpu = cpu;
0261 rtpcp->rtpp = rtp;
0262 if (!rtpcp->rtp_blkd_tasks.next)
0263 INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
0264 raw_spin_unlock_rcu_node(rtpcp);
0265 }
0266 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
0267 pr_info("%s: Setting shift to %d and lim to %d.\n", __func__, data_race(rtp->percpu_enqueue_shift), data_race(rtp->percpu_enqueue_lim));
0268 }
0269
0270
0271 static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp)
0272 {
0273 struct rcu_tasks *rtp;
0274 struct rcu_tasks_percpu *rtpcp = container_of(iwp, struct rcu_tasks_percpu, rtp_irq_work);
0275
0276 rtp = rtpcp->rtpp;
0277 rcuwait_wake_up(&rtp->cbs_wait);
0278 }
0279
0280
0281 static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
0282 struct rcu_tasks *rtp)
0283 {
0284 int chosen_cpu;
0285 unsigned long flags;
0286 int ideal_cpu;
0287 unsigned long j;
0288 bool needadjust = false;
0289 bool needwake;
0290 struct rcu_tasks_percpu *rtpcp;
0291
0292 rhp->next = NULL;
0293 rhp->func = func;
0294 local_irq_save(flags);
0295 rcu_read_lock();
0296 ideal_cpu = smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift);
0297 chosen_cpu = cpumask_next(ideal_cpu - 1, cpu_possible_mask);
0298 rtpcp = per_cpu_ptr(rtp->rtpcpu, chosen_cpu);
0299 if (!raw_spin_trylock_rcu_node(rtpcp)) {
0300 raw_spin_lock_rcu_node(rtpcp);
0301 j = jiffies;
0302 if (rtpcp->rtp_jiffies != j) {
0303 rtpcp->rtp_jiffies = j;
0304 rtpcp->rtp_n_lock_retries = 0;
0305 }
0306 if (rcu_task_cb_adjust && ++rtpcp->rtp_n_lock_retries > rcu_task_contend_lim &&
0307 READ_ONCE(rtp->percpu_enqueue_lim) != nr_cpu_ids)
0308 needadjust = true;
0309 }
0310 if (!rcu_segcblist_is_enabled(&rtpcp->cblist)) {
0311 raw_spin_unlock_rcu_node(rtpcp);
0312 cblist_init_generic(rtp);
0313 raw_spin_lock_rcu_node(rtpcp);
0314 }
0315 needwake = rcu_segcblist_empty(&rtpcp->cblist);
0316 rcu_segcblist_enqueue(&rtpcp->cblist, rhp);
0317 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
0318 if (unlikely(needadjust)) {
0319 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
0320 if (rtp->percpu_enqueue_lim != nr_cpu_ids) {
0321 WRITE_ONCE(rtp->percpu_enqueue_shift, 0);
0322 WRITE_ONCE(rtp->percpu_dequeue_lim, nr_cpu_ids);
0323 smp_store_release(&rtp->percpu_enqueue_lim, nr_cpu_ids);
0324 pr_info("Switching %s to per-CPU callback queuing.\n", rtp->name);
0325 }
0326 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
0327 }
0328 rcu_read_unlock();
0329
0330 if (needwake && READ_ONCE(rtp->kthread_ptr))
0331 irq_work_queue(&rtpcp->rtp_irq_work);
0332 }
0333
0334
0335 static void rcu_barrier_tasks_generic_cb(struct rcu_head *rhp)
0336 {
0337 struct rcu_tasks *rtp;
0338 struct rcu_tasks_percpu *rtpcp;
0339
0340 rtpcp = container_of(rhp, struct rcu_tasks_percpu, barrier_q_head);
0341 rtp = rtpcp->rtpp;
0342 if (atomic_dec_and_test(&rtp->barrier_q_count))
0343 complete(&rtp->barrier_q_completion);
0344 }
0345
0346
0347
0348 static void rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
0349 {
0350 int cpu;
0351 unsigned long flags;
0352 struct rcu_tasks_percpu *rtpcp;
0353 unsigned long s = rcu_seq_snap(&rtp->barrier_q_seq);
0354
0355 mutex_lock(&rtp->barrier_q_mutex);
0356 if (rcu_seq_done(&rtp->barrier_q_seq, s)) {
0357 smp_mb();
0358 mutex_unlock(&rtp->barrier_q_mutex);
0359 return;
0360 }
0361 rcu_seq_start(&rtp->barrier_q_seq);
0362 init_completion(&rtp->barrier_q_completion);
0363 atomic_set(&rtp->barrier_q_count, 2);
0364 for_each_possible_cpu(cpu) {
0365 if (cpu >= smp_load_acquire(&rtp->percpu_dequeue_lim))
0366 break;
0367 rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
0368 rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
0369 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
0370 if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
0371 atomic_inc(&rtp->barrier_q_count);
0372 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
0373 }
0374 if (atomic_sub_and_test(2, &rtp->barrier_q_count))
0375 complete(&rtp->barrier_q_completion);
0376 wait_for_completion(&rtp->barrier_q_completion);
0377 rcu_seq_end(&rtp->barrier_q_seq);
0378 mutex_unlock(&rtp->barrier_q_mutex);
0379 }
0380
0381
0382
0383 static int rcu_tasks_need_gpcb(struct rcu_tasks *rtp)
0384 {
0385 int cpu;
0386 unsigned long flags;
0387 long n;
0388 long ncbs = 0;
0389 long ncbsnz = 0;
0390 int needgpcb = 0;
0391
0392 for (cpu = 0; cpu < smp_load_acquire(&rtp->percpu_dequeue_lim); cpu++) {
0393 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
0394
0395
0396 if (!rcu_segcblist_n_cbs(&rtpcp->cblist))
0397 continue;
0398 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
0399
0400 n = rcu_segcblist_n_cbs(&rtpcp->cblist);
0401 if (n) {
0402 ncbs += n;
0403 if (cpu > 0)
0404 ncbsnz += n;
0405 }
0406 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
0407 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
0408 if (rcu_segcblist_pend_cbs(&rtpcp->cblist))
0409 needgpcb |= 0x3;
0410 if (!rcu_segcblist_empty(&rtpcp->cblist))
0411 needgpcb |= 0x1;
0412 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
0413 }
0414
0415
0416
0417
0418
0419
0420
0421
0422 if (rcu_task_cb_adjust && ncbs <= rcu_task_collapse_lim) {
0423 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
0424 if (rtp->percpu_enqueue_lim > 1) {
0425 WRITE_ONCE(rtp->percpu_enqueue_shift, order_base_2(nr_cpu_ids));
0426 smp_store_release(&rtp->percpu_enqueue_lim, 1);
0427 rtp->percpu_dequeue_gpseq = get_state_synchronize_rcu();
0428 pr_info("Starting switch %s to CPU-0 callback queuing.\n", rtp->name);
0429 }
0430 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
0431 }
0432 if (rcu_task_cb_adjust && !ncbsnz &&
0433 poll_state_synchronize_rcu(rtp->percpu_dequeue_gpseq)) {
0434 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
0435 if (rtp->percpu_enqueue_lim < rtp->percpu_dequeue_lim) {
0436 WRITE_ONCE(rtp->percpu_dequeue_lim, 1);
0437 pr_info("Completing switch %s to CPU-0 callback queuing.\n", rtp->name);
0438 }
0439 for (cpu = rtp->percpu_dequeue_lim; cpu < nr_cpu_ids; cpu++) {
0440 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
0441
0442 WARN_ON_ONCE(rcu_segcblist_n_cbs(&rtpcp->cblist));
0443 }
0444 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
0445 }
0446
0447 return needgpcb;
0448 }
0449
0450
0451 static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu *rtpcp)
0452 {
0453 int cpu;
0454 int cpunext;
0455 unsigned long flags;
0456 int len;
0457 struct rcu_head *rhp;
0458 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
0459 struct rcu_tasks_percpu *rtpcp_next;
0460
0461 cpu = rtpcp->cpu;
0462 cpunext = cpu * 2 + 1;
0463 if (cpunext < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
0464 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
0465 queue_work_on(cpunext, system_wq, &rtpcp_next->rtp_work);
0466 cpunext++;
0467 if (cpunext < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
0468 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
0469 queue_work_on(cpunext, system_wq, &rtpcp_next->rtp_work);
0470 }
0471 }
0472
0473 if (rcu_segcblist_empty(&rtpcp->cblist) || !cpu_possible(cpu))
0474 return;
0475 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
0476 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
0477 rcu_segcblist_extract_done_cbs(&rtpcp->cblist, &rcl);
0478 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
0479 len = rcl.len;
0480 for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
0481 local_bh_disable();
0482 rhp->func(rhp);
0483 local_bh_enable();
0484 cond_resched();
0485 }
0486 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
0487 rcu_segcblist_add_len(&rtpcp->cblist, -len);
0488 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
0489 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
0490 }
0491
0492
0493 static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp)
0494 {
0495 struct rcu_tasks *rtp;
0496 struct rcu_tasks_percpu *rtpcp = container_of(wp, struct rcu_tasks_percpu, rtp_work);
0497
0498 rtp = rtpcp->rtpp;
0499 rcu_tasks_invoke_cbs(rtp, rtpcp);
0500 }
0501
0502
0503 static void rcu_tasks_one_gp(struct rcu_tasks *rtp, bool midboot)
0504 {
0505 int needgpcb;
0506
0507 mutex_lock(&rtp->tasks_gp_mutex);
0508
0509
0510 if (unlikely(midboot)) {
0511 needgpcb = 0x2;
0512 } else {
0513 set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
0514 rcuwait_wait_event(&rtp->cbs_wait,
0515 (needgpcb = rcu_tasks_need_gpcb(rtp)),
0516 TASK_IDLE);
0517 }
0518
0519 if (needgpcb & 0x2) {
0520
0521 set_tasks_gp_state(rtp, RTGS_WAIT_GP);
0522 rtp->gp_start = jiffies;
0523 rcu_seq_start(&rtp->tasks_gp_seq);
0524 rtp->gp_func(rtp);
0525 rcu_seq_end(&rtp->tasks_gp_seq);
0526 }
0527
0528
0529 set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
0530 rcu_tasks_invoke_cbs(rtp, per_cpu_ptr(rtp->rtpcpu, 0));
0531 mutex_unlock(&rtp->tasks_gp_mutex);
0532 }
0533
0534
0535 static int __noreturn rcu_tasks_kthread(void *arg)
0536 {
0537 struct rcu_tasks *rtp = arg;
0538
0539
0540 housekeeping_affine(current, HK_TYPE_RCU);
0541 WRITE_ONCE(rtp->kthread_ptr, current);
0542
0543
0544
0545
0546
0547
0548
0549 for (;;) {
0550
0551
0552 rcu_tasks_one_gp(rtp, false);
0553
0554
0555 schedule_timeout_idle(rtp->gp_sleep);
0556 }
0557 }
0558
0559
0560 static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
0561 {
0562
0563 RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
0564 "synchronize_rcu_tasks called too soon");
0565
0566
0567 if (READ_ONCE(rtp->kthread_ptr)) {
0568 wait_rcu_gp(rtp->call_func);
0569 return;
0570 }
0571 rcu_tasks_one_gp(rtp, true);
0572 }
0573
0574
0575 static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
0576 {
0577 struct task_struct *t;
0578
0579 t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
0580 if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
0581 return;
0582 smp_mb();
0583 }
0584
0585 #ifndef CONFIG_TINY_RCU
0586
0587
0588
0589
0590 static void __init rcu_tasks_bootup_oddness(void)
0591 {
0592 #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
0593 int rtsimc;
0594
0595 if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
0596 pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
0597 rtsimc = clamp(rcu_task_stall_info_mult, 1, 10);
0598 if (rtsimc != rcu_task_stall_info_mult) {
0599 pr_info("\tTasks-RCU CPU stall info multiplier clamped to %d (rcu_task_stall_info_mult).\n", rtsimc);
0600 rcu_task_stall_info_mult = rtsimc;
0601 }
0602 #endif
0603 #ifdef CONFIG_TASKS_RCU
0604 pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
0605 #endif
0606 #ifdef CONFIG_TASKS_RUDE_RCU
0607 pr_info("\tRude variant of Tasks RCU enabled.\n");
0608 #endif
0609 #ifdef CONFIG_TASKS_TRACE_RCU
0610 pr_info("\tTracing variant of Tasks RCU enabled.\n");
0611 #endif
0612 }
0613
0614 #endif
0615
0616 #ifndef CONFIG_TINY_RCU
0617
0618 static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
0619 {
0620 int cpu;
0621 bool havecbs = false;
0622
0623 for_each_possible_cpu(cpu) {
0624 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
0625
0626 if (!data_race(rcu_segcblist_empty(&rtpcp->cblist))) {
0627 havecbs = true;
0628 break;
0629 }
0630 }
0631 pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c %s\n",
0632 rtp->kname,
0633 tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
0634 jiffies - data_race(rtp->gp_jiffies),
0635 data_race(rcu_seq_current(&rtp->tasks_gp_seq)),
0636 data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
0637 ".k"[!!data_race(rtp->kthread_ptr)],
0638 ".C"[havecbs],
0639 s);
0640 }
0641 #endif
0642
0643 static void exit_tasks_rcu_finish_trace(struct task_struct *t);
0644
0645 #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
0646
0647
0648
0649
0650
0651
0652 static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
0653 {
0654 struct task_struct *g;
0655 int fract;
0656 LIST_HEAD(holdouts);
0657 unsigned long j;
0658 unsigned long lastinfo;
0659 unsigned long lastreport;
0660 bool reported = false;
0661 int rtsi;
0662 struct task_struct *t;
0663
0664 set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
0665 rtp->pregp_func(&holdouts);
0666
0667
0668
0669
0670
0671
0672
0673 set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
0674 if (rtp->pertask_func) {
0675 rcu_read_lock();
0676 for_each_process_thread(g, t)
0677 rtp->pertask_func(t, &holdouts);
0678 rcu_read_unlock();
0679 }
0680
0681 set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
0682 rtp->postscan_func(&holdouts);
0683
0684
0685
0686
0687
0688
0689 lastreport = jiffies;
0690 lastinfo = lastreport;
0691 rtsi = READ_ONCE(rcu_task_stall_info);
0692
0693
0694 fract = rtp->init_fract;
0695
0696 while (!list_empty(&holdouts)) {
0697 ktime_t exp;
0698 bool firstreport;
0699 bool needreport;
0700 int rtst;
0701
0702
0703 set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
0704 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
0705 schedule_timeout_idle(fract);
0706 } else {
0707 exp = jiffies_to_nsecs(fract);
0708 __set_current_state(TASK_IDLE);
0709 schedule_hrtimeout_range(&exp, jiffies_to_nsecs(HZ / 2), HRTIMER_MODE_REL_HARD);
0710 }
0711
0712 if (fract < HZ)
0713 fract++;
0714
0715 rtst = READ_ONCE(rcu_task_stall_timeout);
0716 needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
0717 if (needreport) {
0718 lastreport = jiffies;
0719 reported = true;
0720 }
0721 firstreport = true;
0722 WARN_ON(signal_pending(current));
0723 set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
0724 rtp->holdouts_func(&holdouts, needreport, &firstreport);
0725
0726
0727 j = jiffies;
0728 if (rtsi > 0 && !reported && time_after(j, lastinfo + rtsi)) {
0729 lastinfo = j;
0730 rtsi = rtsi * rcu_task_stall_info_mult;
0731 pr_info("%s: %s grace period %lu is %lu jiffies old.\n",
0732 __func__, rtp->kname, rtp->tasks_gp_seq, j - rtp->gp_start);
0733 }
0734 }
0735
0736 set_tasks_gp_state(rtp, RTGS_POST_GP);
0737 rtp->postgp_func(rtp);
0738 }
0739
0740 #endif
0741
0742 #ifdef CONFIG_TASKS_RCU
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797 static void rcu_tasks_pregp_step(struct list_head *hop)
0798 {
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812 synchronize_rcu();
0813 }
0814
0815
0816 static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
0817 {
0818 if (t != current && READ_ONCE(t->on_rq) && !is_idle_task(t)) {
0819 get_task_struct(t);
0820 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
0821 WRITE_ONCE(t->rcu_tasks_holdout, true);
0822 list_add(&t->rcu_tasks_holdout_list, hop);
0823 }
0824 }
0825
0826
0827 static void rcu_tasks_postscan(struct list_head *hop)
0828 {
0829
0830
0831
0832
0833
0834
0835
0836 synchronize_srcu(&tasks_rcu_exit_srcu);
0837 }
0838
0839
0840 static void check_holdout_task(struct task_struct *t,
0841 bool needreport, bool *firstreport)
0842 {
0843 int cpu;
0844
0845 if (!READ_ONCE(t->rcu_tasks_holdout) ||
0846 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
0847 !READ_ONCE(t->on_rq) ||
0848 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
0849 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
0850 WRITE_ONCE(t->rcu_tasks_holdout, false);
0851 list_del_init(&t->rcu_tasks_holdout_list);
0852 put_task_struct(t);
0853 return;
0854 }
0855 rcu_request_urgent_qs_task(t);
0856 if (!needreport)
0857 return;
0858 if (*firstreport) {
0859 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
0860 *firstreport = false;
0861 }
0862 cpu = task_cpu(t);
0863 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
0864 t, ".I"[is_idle_task(t)],
0865 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
0866 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
0867 t->rcu_tasks_idle_cpu, cpu);
0868 sched_show_task(t);
0869 }
0870
0871
0872 static void check_all_holdout_tasks(struct list_head *hop,
0873 bool needreport, bool *firstreport)
0874 {
0875 struct task_struct *t, *t1;
0876
0877 list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
0878 check_holdout_task(t, needreport, firstreport);
0879 cond_resched();
0880 }
0881 }
0882
0883
0884 static void rcu_tasks_postgp(struct rcu_tasks *rtp)
0885 {
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903 synchronize_rcu();
0904 }
0905
0906 void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
0907 DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927 void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
0928 {
0929 call_rcu_tasks_generic(rhp, func, &rcu_tasks);
0930 }
0931 EXPORT_SYMBOL_GPL(call_rcu_tasks);
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951 void synchronize_rcu_tasks(void)
0952 {
0953 synchronize_rcu_tasks_generic(&rcu_tasks);
0954 }
0955 EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
0956
0957
0958
0959
0960
0961
0962
0963 void rcu_barrier_tasks(void)
0964 {
0965 rcu_barrier_tasks_generic(&rcu_tasks);
0966 }
0967 EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
0968
0969 static int __init rcu_spawn_tasks_kthread(void)
0970 {
0971 cblist_init_generic(&rcu_tasks);
0972 rcu_tasks.gp_sleep = HZ / 10;
0973 rcu_tasks.init_fract = HZ / 10;
0974 rcu_tasks.pregp_func = rcu_tasks_pregp_step;
0975 rcu_tasks.pertask_func = rcu_tasks_pertask;
0976 rcu_tasks.postscan_func = rcu_tasks_postscan;
0977 rcu_tasks.holdouts_func = check_all_holdout_tasks;
0978 rcu_tasks.postgp_func = rcu_tasks_postgp;
0979 rcu_spawn_tasks_kthread_generic(&rcu_tasks);
0980 return 0;
0981 }
0982
0983 #if !defined(CONFIG_TINY_RCU)
0984 void show_rcu_tasks_classic_gp_kthread(void)
0985 {
0986 show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
0987 }
0988 EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
0989 #endif
0990
0991
0992 void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
0993 {
0994 preempt_disable();
0995 current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
0996 preempt_enable();
0997 }
0998
0999
1000 void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
1001 {
1002 struct task_struct *t = current;
1003
1004 preempt_disable();
1005 __srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
1006 preempt_enable();
1007 exit_tasks_rcu_finish_trace(t);
1008 }
1009
1010 #else
1011 void exit_tasks_rcu_start(void) { }
1012 void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
1013 #endif
1014
1015 #ifdef CONFIG_TASKS_RUDE_RCU
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032 static void rcu_tasks_be_rude(struct work_struct *work)
1033 {
1034 }
1035
1036
1037 static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
1038 {
1039 if (num_online_cpus() <= 1)
1040 return;
1041
1042 rtp->n_ipis += cpumask_weight(cpu_online_mask);
1043 schedule_on_each_cpu(rcu_tasks_be_rude);
1044 }
1045
1046 void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
1047 DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
1048 "RCU Tasks Rude");
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
1069 {
1070 call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
1071 }
1072 EXPORT_SYMBOL_GPL(call_rcu_tasks_rude);
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 void synchronize_rcu_tasks_rude(void)
1093 {
1094 synchronize_rcu_tasks_generic(&rcu_tasks_rude);
1095 }
1096 EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
1097
1098
1099
1100
1101
1102
1103
1104 void rcu_barrier_tasks_rude(void)
1105 {
1106 rcu_barrier_tasks_generic(&rcu_tasks_rude);
1107 }
1108 EXPORT_SYMBOL_GPL(rcu_barrier_tasks_rude);
1109
1110 static int __init rcu_spawn_tasks_rude_kthread(void)
1111 {
1112 cblist_init_generic(&rcu_tasks_rude);
1113 rcu_tasks_rude.gp_sleep = HZ / 10;
1114 rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
1115 return 0;
1116 }
1117
1118 #if !defined(CONFIG_TINY_RCU)
1119 void show_rcu_tasks_rude_gp_kthread(void)
1120 {
1121 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
1122 }
1123 EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
1124 #endif
1125 #endif
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1185 static struct lock_class_key rcu_lock_trace_key;
1186 struct lockdep_map rcu_trace_lock_map =
1187 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
1188 EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
1189 #endif
1190
1191 #ifdef CONFIG_TASKS_TRACE_RCU
1192
1193
1194 static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
1195
1196
1197
1198 static unsigned long n_heavy_reader_attempts;
1199 static unsigned long n_heavy_reader_updates;
1200 static unsigned long n_heavy_reader_ofl_updates;
1201 static unsigned long n_trc_holdouts;
1202
1203 void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
1204 DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
1205 "RCU Tasks Trace");
1206
1207
1208 static u8 rcu_ld_need_qs(struct task_struct *t)
1209 {
1210 smp_mb();
1211 return smp_load_acquire(&t->trc_reader_special.b.need_qs);
1212 }
1213
1214
1215 static void rcu_st_need_qs(struct task_struct *t, u8 v)
1216 {
1217 smp_store_release(&t->trc_reader_special.b.need_qs, v);
1218 smp_mb();
1219 }
1220
1221
1222
1223
1224
1225
1226 u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new)
1227 {
1228 union rcu_special ret;
1229 union rcu_special trs_old = READ_ONCE(t->trc_reader_special);
1230 union rcu_special trs_new = trs_old;
1231
1232 if (trs_old.b.need_qs != old)
1233 return trs_old.b.need_qs;
1234 trs_new.b.need_qs = new;
1235 ret.s = cmpxchg(&t->trc_reader_special.s, trs_old.s, trs_new.s);
1236 return ret.b.need_qs;
1237 }
1238 EXPORT_SYMBOL_GPL(rcu_trc_cmpxchg_need_qs);
1239
1240
1241
1242
1243
1244 void rcu_read_unlock_trace_special(struct task_struct *t)
1245 {
1246 unsigned long flags;
1247 struct rcu_tasks_percpu *rtpcp;
1248 union rcu_special trs;
1249
1250
1251 smp_mb();
1252 trs = smp_load_acquire(&t->trc_reader_special);
1253
1254 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && t->trc_reader_special.b.need_mb)
1255 smp_mb();
1256
1257 if (trs.b.need_qs == (TRC_NEED_QS_CHECKED | TRC_NEED_QS)) {
1258 u8 result = rcu_trc_cmpxchg_need_qs(t, TRC_NEED_QS_CHECKED | TRC_NEED_QS,
1259 TRC_NEED_QS_CHECKED);
1260
1261 WARN_ONCE(result != trs.b.need_qs, "%s: result = %d", __func__, result);
1262 }
1263 if (trs.b.blocked) {
1264 rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, t->trc_blkd_cpu);
1265 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1266 list_del_init(&t->trc_blkd_node);
1267 WRITE_ONCE(t->trc_reader_special.b.blocked, false);
1268 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1269 }
1270 WRITE_ONCE(t->trc_reader_nesting, 0);
1271 }
1272 EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
1273
1274
1275 void rcu_tasks_trace_qs_blkd(struct task_struct *t)
1276 {
1277 unsigned long flags;
1278 struct rcu_tasks_percpu *rtpcp;
1279
1280 local_irq_save(flags);
1281 rtpcp = this_cpu_ptr(rcu_tasks_trace.rtpcpu);
1282 raw_spin_lock_rcu_node(rtpcp);
1283 t->trc_blkd_cpu = smp_processor_id();
1284 if (!rtpcp->rtp_blkd_tasks.next)
1285 INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
1286 list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
1287 WRITE_ONCE(t->trc_reader_special.b.blocked, true);
1288 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1289 }
1290 EXPORT_SYMBOL_GPL(rcu_tasks_trace_qs_blkd);
1291
1292
1293 static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
1294 {
1295 if (list_empty(&t->trc_holdout_list)) {
1296 get_task_struct(t);
1297 list_add(&t->trc_holdout_list, bhp);
1298 n_trc_holdouts++;
1299 }
1300 }
1301
1302
1303 static void trc_del_holdout(struct task_struct *t)
1304 {
1305 if (!list_empty(&t->trc_holdout_list)) {
1306 list_del_init(&t->trc_holdout_list);
1307 put_task_struct(t);
1308 n_trc_holdouts--;
1309 }
1310 }
1311
1312
1313 static void trc_read_check_handler(void *t_in)
1314 {
1315 int nesting;
1316 struct task_struct *t = current;
1317 struct task_struct *texp = t_in;
1318
1319
1320 if (unlikely(texp != t))
1321 goto reset_ipi;
1322
1323
1324
1325 nesting = READ_ONCE(t->trc_reader_nesting);
1326 if (likely(!nesting)) {
1327 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
1328 goto reset_ipi;
1329 }
1330
1331 if (unlikely(nesting < 0))
1332 goto reset_ipi;
1333
1334
1335
1336
1337 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED);
1338
1339 reset_ipi:
1340
1341
1342
1343 smp_store_release(per_cpu_ptr(&trc_ipi_to_cpu, smp_processor_id()), false);
1344 smp_store_release(&texp->trc_ipi_to_cpu, -1);
1345 }
1346
1347
1348 static int trc_inspect_reader(struct task_struct *t, void *bhp_in)
1349 {
1350 struct list_head *bhp = bhp_in;
1351 int cpu = task_cpu(t);
1352 int nesting;
1353 bool ofl = cpu_is_offline(cpu);
1354
1355 if (task_curr(t) && !ofl) {
1356
1357 if (!IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB))
1358 return -EINVAL;
1359
1360
1361
1362
1363 n_heavy_reader_attempts++;
1364
1365 if (!rcu_dynticks_zero_in_eqs(cpu, &t->trc_reader_nesting))
1366 return -EINVAL;
1367 n_heavy_reader_updates++;
1368 nesting = 0;
1369 } else {
1370
1371 nesting = t->trc_reader_nesting;
1372 WARN_ON_ONCE(ofl && task_curr(t) && !is_idle_task(t));
1373 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && ofl)
1374 n_heavy_reader_ofl_updates++;
1375 }
1376
1377
1378
1379
1380 if (!nesting) {
1381 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
1382 return 0;
1383 }
1384 if (nesting < 0)
1385 return -EINVAL;
1386
1387
1388
1389
1390 if (!rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED))
1391 trc_add_holdout(t, bhp);
1392 return 0;
1393 }
1394
1395
1396 static void trc_wait_for_one_reader(struct task_struct *t,
1397 struct list_head *bhp)
1398 {
1399 int cpu;
1400
1401
1402 if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1)
1403 return;
1404
1405
1406 if (t == current) {
1407 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
1408 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
1409 return;
1410 }
1411
1412
1413 get_task_struct(t);
1414 if (!task_call_func(t, trc_inspect_reader, bhp)) {
1415 put_task_struct(t);
1416 return;
1417 }
1418 put_task_struct(t);
1419
1420
1421
1422
1423
1424
1425
1426
1427 trc_add_holdout(t, bhp);
1428 if (task_curr(t) &&
1429 time_after(jiffies + 1, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
1430
1431 cpu = task_cpu(t);
1432
1433
1434 if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
1435 return;
1436
1437 per_cpu(trc_ipi_to_cpu, cpu) = true;
1438 t->trc_ipi_to_cpu = cpu;
1439 rcu_tasks_trace.n_ipis++;
1440 if (smp_call_function_single(cpu, trc_read_check_handler, t, 0)) {
1441
1442
1443 WARN_ONCE(1, "%s(): smp_call_function_single() failed for CPU: %d\n",
1444 __func__, cpu);
1445 rcu_tasks_trace.n_ipis_fails++;
1446 per_cpu(trc_ipi_to_cpu, cpu) = false;
1447 t->trc_ipi_to_cpu = -1;
1448 }
1449 }
1450 }
1451
1452
1453
1454
1455
1456 static bool rcu_tasks_trace_pertask_prep(struct task_struct *t, bool notself)
1457 {
1458
1459
1460
1461
1462 if (unlikely(t == NULL) || (t == current && notself) || !list_empty(&t->trc_holdout_list))
1463 return false;
1464
1465 rcu_st_need_qs(t, 0);
1466 t->trc_ipi_to_cpu = -1;
1467 return true;
1468 }
1469
1470
1471 static void rcu_tasks_trace_pertask(struct task_struct *t, struct list_head *hop)
1472 {
1473 if (rcu_tasks_trace_pertask_prep(t, true))
1474 trc_wait_for_one_reader(t, hop);
1475 }
1476
1477
1478 static void rcu_tasks_trace_pregp_step(struct list_head *hop)
1479 {
1480 LIST_HEAD(blkd_tasks);
1481 int cpu;
1482 unsigned long flags;
1483 struct rcu_tasks_percpu *rtpcp;
1484 struct task_struct *t;
1485
1486
1487 for_each_possible_cpu(cpu)
1488 WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
1489
1490
1491
1492
1493 cpus_read_lock();
1494
1495
1496
1497 for_each_online_cpu(cpu) {
1498 rcu_read_lock();
1499 t = cpu_curr_snapshot(cpu);
1500 if (rcu_tasks_trace_pertask_prep(t, true))
1501 trc_add_holdout(t, hop);
1502 rcu_read_unlock();
1503 }
1504
1505
1506
1507
1508 for_each_possible_cpu(cpu) {
1509 rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, cpu);
1510 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1511 list_splice_init(&rtpcp->rtp_blkd_tasks, &blkd_tasks);
1512 while (!list_empty(&blkd_tasks)) {
1513 rcu_read_lock();
1514 t = list_first_entry(&blkd_tasks, struct task_struct, trc_blkd_node);
1515 list_del_init(&t->trc_blkd_node);
1516 list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
1517 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1518 rcu_tasks_trace_pertask(t, hop);
1519 rcu_read_unlock();
1520 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1521 }
1522 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1523 }
1524
1525
1526 cpus_read_unlock();
1527 }
1528
1529
1530
1531
1532 static void rcu_tasks_trace_postscan(struct list_head *hop)
1533 {
1534
1535
1536 synchronize_rcu();
1537
1538
1539 }
1540
1541
1542 struct trc_stall_chk_rdr {
1543 int nesting;
1544 int ipi_to_cpu;
1545 u8 needqs;
1546 };
1547
1548 static int trc_check_slow_task(struct task_struct *t, void *arg)
1549 {
1550 struct trc_stall_chk_rdr *trc_rdrp = arg;
1551
1552 if (task_curr(t) && cpu_online(task_cpu(t)))
1553 return false;
1554 trc_rdrp->nesting = READ_ONCE(t->trc_reader_nesting);
1555 trc_rdrp->ipi_to_cpu = READ_ONCE(t->trc_ipi_to_cpu);
1556 trc_rdrp->needqs = rcu_ld_need_qs(t);
1557 return true;
1558 }
1559
1560
1561 static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
1562 {
1563 int cpu;
1564 struct trc_stall_chk_rdr trc_rdr;
1565 bool is_idle_tsk = is_idle_task(t);
1566
1567 if (*firstreport) {
1568 pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
1569 *firstreport = false;
1570 }
1571 cpu = task_cpu(t);
1572 if (!task_call_func(t, trc_check_slow_task, &trc_rdr))
1573 pr_alert("P%d: %c%c\n",
1574 t->pid,
1575 ".I"[t->trc_ipi_to_cpu >= 0],
1576 ".i"[is_idle_tsk]);
1577 else
1578 pr_alert("P%d: %c%c%c%c nesting: %d%c%c cpu: %d%s\n",
1579 t->pid,
1580 ".I"[trc_rdr.ipi_to_cpu >= 0],
1581 ".i"[is_idle_tsk],
1582 ".N"[cpu >= 0 && tick_nohz_full_cpu(cpu)],
1583 ".B"[!!data_race(t->trc_reader_special.b.blocked)],
1584 trc_rdr.nesting,
1585 " !CN"[trc_rdr.needqs & 0x3],
1586 " ?"[trc_rdr.needqs > 0x3],
1587 cpu, cpu_online(cpu) ? "" : "(offline)");
1588 sched_show_task(t);
1589 }
1590
1591
1592 static void show_stalled_ipi_trace(void)
1593 {
1594 int cpu;
1595
1596 for_each_possible_cpu(cpu)
1597 if (per_cpu(trc_ipi_to_cpu, cpu))
1598 pr_alert("\tIPI outstanding to CPU %d\n", cpu);
1599 }
1600
1601
1602 static void check_all_holdout_tasks_trace(struct list_head *hop,
1603 bool needreport, bool *firstreport)
1604 {
1605 struct task_struct *g, *t;
1606
1607
1608 cpus_read_lock();
1609
1610 list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
1611
1612 if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
1613 !(rcu_ld_need_qs(t) & TRC_NEED_QS_CHECKED))
1614 trc_wait_for_one_reader(t, hop);
1615
1616
1617 if (smp_load_acquire(&t->trc_ipi_to_cpu) == -1 &&
1618 rcu_ld_need_qs(t) == TRC_NEED_QS_CHECKED)
1619 trc_del_holdout(t);
1620 else if (needreport)
1621 show_stalled_task_trace(t, firstreport);
1622 }
1623
1624
1625 cpus_read_unlock();
1626
1627 if (needreport) {
1628 if (*firstreport)
1629 pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
1630 show_stalled_ipi_trace();
1631 }
1632 }
1633
1634 static void rcu_tasks_trace_empty_fn(void *unused)
1635 {
1636 }
1637
1638
1639 static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
1640 {
1641 int cpu;
1642
1643
1644
1645
1646
1647
1648 for_each_online_cpu(cpu)
1649 if (WARN_ON_ONCE(smp_load_acquire(per_cpu_ptr(&trc_ipi_to_cpu, cpu))))
1650 smp_call_function_single(cpu, rcu_tasks_trace_empty_fn, NULL, 1);
1651
1652 smp_mb();
1653
1654 }
1655
1656
1657 static void exit_tasks_rcu_finish_trace(struct task_struct *t)
1658 {
1659 union rcu_special trs = READ_ONCE(t->trc_reader_special);
1660
1661 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
1662 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
1663 if (WARN_ON_ONCE(rcu_ld_need_qs(t) & TRC_NEED_QS || trs.b.blocked))
1664 rcu_read_unlock_trace_special(t);
1665 else
1666 WRITE_ONCE(t->trc_reader_nesting, 0);
1667 }
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683 void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
1684 {
1685 call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
1686 }
1687 EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706 void synchronize_rcu_tasks_trace(void)
1707 {
1708 RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
1709 synchronize_rcu_tasks_generic(&rcu_tasks_trace);
1710 }
1711 EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
1712
1713
1714
1715
1716
1717
1718
1719 void rcu_barrier_tasks_trace(void)
1720 {
1721 rcu_barrier_tasks_generic(&rcu_tasks_trace);
1722 }
1723 EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
1724
1725 static int __init rcu_spawn_tasks_trace_kthread(void)
1726 {
1727 cblist_init_generic(&rcu_tasks_trace);
1728 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB)) {
1729 rcu_tasks_trace.gp_sleep = HZ / 10;
1730 rcu_tasks_trace.init_fract = HZ / 10;
1731 } else {
1732 rcu_tasks_trace.gp_sleep = HZ / 200;
1733 if (rcu_tasks_trace.gp_sleep <= 0)
1734 rcu_tasks_trace.gp_sleep = 1;
1735 rcu_tasks_trace.init_fract = HZ / 200;
1736 if (rcu_tasks_trace.init_fract <= 0)
1737 rcu_tasks_trace.init_fract = 1;
1738 }
1739 rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
1740 rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
1741 rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
1742 rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
1743 rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
1744 return 0;
1745 }
1746
1747 #if !defined(CONFIG_TINY_RCU)
1748 void show_rcu_tasks_trace_gp_kthread(void)
1749 {
1750 char buf[64];
1751
1752 sprintf(buf, "N%lu h:%lu/%lu/%lu",
1753 data_race(n_trc_holdouts),
1754 data_race(n_heavy_reader_ofl_updates),
1755 data_race(n_heavy_reader_updates),
1756 data_race(n_heavy_reader_attempts));
1757 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
1758 }
1759 EXPORT_SYMBOL_GPL(show_rcu_tasks_trace_gp_kthread);
1760 #endif
1761
1762 #else
1763 static void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
1764 #endif
1765
1766 #ifndef CONFIG_TINY_RCU
1767 void show_rcu_tasks_gp_kthreads(void)
1768 {
1769 show_rcu_tasks_classic_gp_kthread();
1770 show_rcu_tasks_rude_gp_kthread();
1771 show_rcu_tasks_trace_gp_kthread();
1772 }
1773 #endif
1774
1775 #ifdef CONFIG_PROVE_RCU
1776 struct rcu_tasks_test_desc {
1777 struct rcu_head rh;
1778 const char *name;
1779 bool notrun;
1780 unsigned long runstart;
1781 };
1782
1783 static struct rcu_tasks_test_desc tests[] = {
1784 {
1785 .name = "call_rcu_tasks()",
1786
1787 .notrun = IS_ENABLED(CONFIG_TASKS_RCU),
1788 },
1789 {
1790 .name = "call_rcu_tasks_rude()",
1791
1792 .notrun = IS_ENABLED(CONFIG_TASKS_RUDE_RCU),
1793 },
1794 {
1795 .name = "call_rcu_tasks_trace()",
1796
1797 .notrun = IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
1798 }
1799 };
1800
1801 static void test_rcu_tasks_callback(struct rcu_head *rhp)
1802 {
1803 struct rcu_tasks_test_desc *rttd =
1804 container_of(rhp, struct rcu_tasks_test_desc, rh);
1805
1806 pr_info("Callback from %s invoked.\n", rttd->name);
1807
1808 rttd->notrun = false;
1809 }
1810
1811 static void rcu_tasks_initiate_self_tests(void)
1812 {
1813 unsigned long j = jiffies;
1814
1815 pr_info("Running RCU-tasks wait API self tests\n");
1816 #ifdef CONFIG_TASKS_RCU
1817 tests[0].runstart = j;
1818 synchronize_rcu_tasks();
1819 call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
1820 #endif
1821
1822 #ifdef CONFIG_TASKS_RUDE_RCU
1823 tests[1].runstart = j;
1824 synchronize_rcu_tasks_rude();
1825 call_rcu_tasks_rude(&tests[1].rh, test_rcu_tasks_callback);
1826 #endif
1827
1828 #ifdef CONFIG_TASKS_TRACE_RCU
1829 tests[2].runstart = j;
1830 synchronize_rcu_tasks_trace();
1831 call_rcu_tasks_trace(&tests[2].rh, test_rcu_tasks_callback);
1832 #endif
1833 }
1834
1835
1836
1837
1838
1839
1840 static int rcu_tasks_verify_self_tests(void)
1841 {
1842 int ret = 0;
1843 int i;
1844 unsigned long bst = rcu_task_stall_timeout;
1845
1846 if (bst <= 0 || bst > RCU_TASK_BOOT_STALL_TIMEOUT)
1847 bst = RCU_TASK_BOOT_STALL_TIMEOUT;
1848 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1849 while (tests[i].notrun) {
1850 if (time_after(jiffies, tests[i].runstart + bst)) {
1851 pr_err("%s has failed boot-time tests.\n", tests[i].name);
1852 ret = -1;
1853 break;
1854 }
1855 ret = 1;
1856 break;
1857 }
1858 }
1859 WARN_ON(ret < 0);
1860
1861 return ret;
1862 }
1863
1864
1865
1866
1867
1868 static struct delayed_work rcu_tasks_verify_work;
1869 static void rcu_tasks_verify_work_fn(struct work_struct *work __maybe_unused)
1870 {
1871 int ret = rcu_tasks_verify_self_tests();
1872
1873 if (ret <= 0)
1874 return;
1875
1876
1877 schedule_delayed_work(&rcu_tasks_verify_work, HZ);
1878 }
1879
1880 static int rcu_tasks_verify_schedule_work(void)
1881 {
1882 INIT_DELAYED_WORK(&rcu_tasks_verify_work, rcu_tasks_verify_work_fn);
1883 rcu_tasks_verify_work_fn(NULL);
1884 return 0;
1885 }
1886 late_initcall(rcu_tasks_verify_schedule_work);
1887 #else
1888 static void rcu_tasks_initiate_self_tests(void) { }
1889 #endif
1890
1891 void __init rcu_init_tasks_generic(void)
1892 {
1893 #ifdef CONFIG_TASKS_RCU
1894 rcu_spawn_tasks_kthread();
1895 #endif
1896
1897 #ifdef CONFIG_TASKS_RUDE_RCU
1898 rcu_spawn_tasks_rude_kthread();
1899 #endif
1900
1901 #ifdef CONFIG_TASKS_TRACE_RCU
1902 rcu_spawn_tasks_trace_kthread();
1903 #endif
1904
1905
1906 rcu_tasks_initiate_self_tests();
1907 }
1908
1909 #else
1910 static inline void rcu_tasks_bootup_oddness(void) {}
1911 #endif