blob: 97797df2e031d4fa9fa512afc93fa0fe3f8dbfab [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition
3 * Internal non-public definitions that provide either classic
4 * or preemptible semantics.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 * Copyright (c) 2010 Linaro
21 *
22 * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
23 */
24
25#include <linux/kthread.h>
26#include <linux/module.h>
27#include <linux/debugfs.h>
28#include <linux/seq_file.h>
29#include <linux/wait-simple.h>
30
31/* Global control variables for rcupdate callback mechanism. */
32struct rcu_ctrlblk {
33 struct rcu_head *rcucblist; /* List of pending callbacks (CBs). */
34 struct rcu_head **donetail; /* ->next pointer of last "done" CB. */
35 struct rcu_head **curtail; /* ->next pointer of last CB. */
36 RCU_TRACE(long qlen); /* Number of pending CBs. */
37 RCU_TRACE(char *name); /* Name of RCU type. */
38};
39
40/* Definition for rcupdate control block. */
41static struct rcu_ctrlblk rcu_sched_ctrlblk = {
42 .donetail = &rcu_sched_ctrlblk.rcucblist,
43 .curtail = &rcu_sched_ctrlblk.rcucblist,
44 RCU_TRACE(.name = "rcu_sched")
45};
46
47static struct rcu_ctrlblk rcu_bh_ctrlblk = {
48 .donetail = &rcu_bh_ctrlblk.rcucblist,
49 .curtail = &rcu_bh_ctrlblk.rcucblist,
50 RCU_TRACE(.name = "rcu_bh")
51};
52
53#ifdef CONFIG_DEBUG_LOCK_ALLOC
54int rcu_scheduler_active __read_mostly;
55EXPORT_SYMBOL_GPL(rcu_scheduler_active);
56#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
57
58#ifdef CONFIG_TINY_PREEMPT_RCU
59
60#include <linux/delay.h>
61
62/* Global control variables for preemptible RCU. */
63struct rcu_preempt_ctrlblk {
64 struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */
65 struct rcu_head **nexttail;
66 /* Tasks blocked in a preemptible RCU */
67 /* read-side critical section while an */
68 /* preemptible-RCU grace period is in */
69 /* progress must wait for a later grace */
70 /* period. This pointer points to the */
71 /* ->next pointer of the last task that */
72 /* must wait for a later grace period, or */
73 /* to &->rcb.rcucblist if there is no */
74 /* such task. */
75 struct list_head blkd_tasks;
76 /* Tasks blocked in RCU read-side critical */
77 /* section. Tasks are placed at the head */
78 /* of this list and age towards the tail. */
79 struct list_head *gp_tasks;
80 /* Pointer to the first task blocking the */
81 /* current grace period, or NULL if there */
82 /* is no such task. */
83 struct list_head *exp_tasks;
84 /* Pointer to first task blocking the */
85 /* current expedited grace period, or NULL */
86 /* if there is no such task. If there */
87 /* is no current expedited grace period, */
88 /* then there cannot be any such task. */
89#ifdef CONFIG_RCU_BOOST
90 struct list_head *boost_tasks;
91 /* Pointer to first task that needs to be */
92 /* priority-boosted, or NULL if no priority */
93 /* boosting is needed. If there is no */
94 /* current or expedited grace period, there */
95 /* can be no such task. */
96#endif /* #ifdef CONFIG_RCU_BOOST */
97 u8 gpnum; /* Current grace period. */
98 u8 gpcpu; /* Last grace period blocked by the CPU. */
99 u8 completed; /* Last grace period completed. */
100 /* If all three are equal, RCU is idle. */
101#ifdef CONFIG_RCU_BOOST
102 unsigned long boost_time; /* When to start boosting (jiffies) */
103#endif /* #ifdef CONFIG_RCU_BOOST */
104#ifdef CONFIG_RCU_TRACE
105 unsigned long n_grace_periods;
106#ifdef CONFIG_RCU_BOOST
107 unsigned long n_tasks_boosted;
108 /* Total number of tasks boosted. */
109 unsigned long n_exp_boosts;
110 /* Number of tasks boosted for expedited GP. */
111 unsigned long n_normal_boosts;
112 /* Number of tasks boosted for normal GP. */
113 unsigned long n_balk_blkd_tasks;
114 /* Refused to boost: no blocked tasks. */
115 unsigned long n_balk_exp_gp_tasks;
116 /* Refused to boost: nothing blocking GP. */
117 unsigned long n_balk_boost_tasks;
118 /* Refused to boost: already boosting. */
119 unsigned long n_balk_notyet;
120 /* Refused to boost: not yet time. */
121 unsigned long n_balk_nos;
122 /* Refused to boost: not sure why, though. */
123 /* This can happen due to race conditions. */
124#endif /* #ifdef CONFIG_RCU_BOOST */
125#endif /* #ifdef CONFIG_RCU_TRACE */
126};
127
128static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = {
129 .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist,
130 .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist,
131 .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist,
132 .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks),
133 RCU_TRACE(.rcb.name = "rcu_preempt")
134};
135
136static void rcu_read_unlock_special(struct task_struct *t);
137static int rcu_preempted_readers_exp(void);
138static void rcu_report_exp_done(void);
139
140/*
141 * Return true if the CPU has not yet responded to the current grace period.
142 */
143static int rcu_cpu_blocking_cur_gp(void)
144{
145 return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum;
146}
147
148/*
149 * Check for a running RCU reader. Because there is only one CPU,
150 * there can be but one running RCU reader at a time. ;-)
151 *
152 * Returns zero if there are no running readers. Returns a positive
153 * number if there is at least one reader within its RCU read-side
154 * critical section. Returns a negative number if an outermost reader
155 * is in the midst of exiting from its RCU read-side critical section
156 *
157 * Returns zero if there are no running readers. Returns a positive
158 * number if there is at least one reader within its RCU read-side
159 * critical section. Returns a negative number if an outermost reader
160 * is in the midst of exiting from its RCU read-side critical section.
161 */
162static int rcu_preempt_running_reader(void)
163{
164 return current->rcu_read_lock_nesting;
165}
166
167/*
168 * Check for preempted RCU readers blocking any grace period.
169 * If the caller needs a reliable answer, it must disable hard irqs.
170 */
171static int rcu_preempt_blocked_readers_any(void)
172{
173 return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks);
174}
175
176/*
177 * Check for preempted RCU readers blocking the current grace period.
178 * If the caller needs a reliable answer, it must disable hard irqs.
179 */
180static int rcu_preempt_blocked_readers_cgp(void)
181{
182 return rcu_preempt_ctrlblk.gp_tasks != NULL;
183}
184
185/*
186 * Return true if another preemptible-RCU grace period is needed.
187 */
188static int rcu_preempt_needs_another_gp(void)
189{
190 return *rcu_preempt_ctrlblk.rcb.curtail != NULL;
191}
192
193/*
194 * Return true if a preemptible-RCU grace period is in progress.
195 * The caller must disable hardirqs.
196 */
197static int rcu_preempt_gp_in_progress(void)
198{
199 return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum;
200}
201
202/*
203 * Advance a ->blkd_tasks-list pointer to the next entry, instead
204 * returning NULL if at the end of the list.
205 */
206static struct list_head *rcu_next_node_entry(struct task_struct *t)
207{
208 struct list_head *np;
209
210 np = t->rcu_node_entry.next;
211 if (np == &rcu_preempt_ctrlblk.blkd_tasks)
212 np = NULL;
213 return np;
214}
215
216#ifdef CONFIG_RCU_TRACE
217
218#ifdef CONFIG_RCU_BOOST
219static void rcu_initiate_boost_trace(void);
220#endif /* #ifdef CONFIG_RCU_BOOST */
221
222/*
223 * Dump additional statistice for TINY_PREEMPT_RCU.
224 */
225static void show_tiny_preempt_stats(struct seq_file *m)
226{
227 seq_printf(m, "rcu_preempt: qlen=%ld gp=%lu g%u/p%u/c%u tasks=%c%c%c\n",
228 rcu_preempt_ctrlblk.rcb.qlen,
229 rcu_preempt_ctrlblk.n_grace_periods,
230 rcu_preempt_ctrlblk.gpnum,
231 rcu_preempt_ctrlblk.gpcpu,
232 rcu_preempt_ctrlblk.completed,
233 "T."[list_empty(&rcu_preempt_ctrlblk.blkd_tasks)],
234 "N."[!rcu_preempt_ctrlblk.gp_tasks],
235 "E."[!rcu_preempt_ctrlblk.exp_tasks]);
236#ifdef CONFIG_RCU_BOOST
237 seq_printf(m, "%sttb=%c ntb=%lu neb=%lu nnb=%lu j=%04x bt=%04x\n",
238 " ",
239 "B."[!rcu_preempt_ctrlblk.boost_tasks],
240 rcu_preempt_ctrlblk.n_tasks_boosted,
241 rcu_preempt_ctrlblk.n_exp_boosts,
242 rcu_preempt_ctrlblk.n_normal_boosts,
243 (int)(jiffies & 0xffff),
244 (int)(rcu_preempt_ctrlblk.boost_time & 0xffff));
245 seq_printf(m, "%s: nt=%lu egt=%lu bt=%lu ny=%lu nos=%lu\n",
246 " balk",
247 rcu_preempt_ctrlblk.n_balk_blkd_tasks,
248 rcu_preempt_ctrlblk.n_balk_exp_gp_tasks,
249 rcu_preempt_ctrlblk.n_balk_boost_tasks,
250 rcu_preempt_ctrlblk.n_balk_notyet,
251 rcu_preempt_ctrlblk.n_balk_nos);
252#endif /* #ifdef CONFIG_RCU_BOOST */
253}
254
255#endif /* #ifdef CONFIG_RCU_TRACE */
256
257#ifdef CONFIG_RCU_BOOST
258
259#include "rtmutex_common.h"
260
261#define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
262
263/* Controls for rcu_kthread() kthread. */
264static struct task_struct *rcu_kthread_task;
265static DEFINE_SWAIT_HEAD(rcu_kthread_wq);
266static unsigned long have_rcu_kthread_work;
267
268/*
269 * Carry out RCU priority boosting on the task indicated by ->boost_tasks,
270 * and advance ->boost_tasks to the next task in the ->blkd_tasks list.
271 */
272static int rcu_boost(void)
273{
274 unsigned long flags;
275 struct rt_mutex mtx;
276 struct task_struct *t;
277 struct list_head *tb;
278
279 if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
280 rcu_preempt_ctrlblk.exp_tasks == NULL)
281 return 0; /* Nothing to boost. */
282
283 raw_local_irq_save(flags);
284
285 /*
286 * Recheck with irqs disabled: all tasks in need of boosting
287 * might exit their RCU read-side critical sections on their own
288 * if we are preempted just before disabling irqs.
289 */
290 if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
291 rcu_preempt_ctrlblk.exp_tasks == NULL) {
292 raw_local_irq_restore(flags);
293 return 0;
294 }
295
296 /*
297 * Preferentially boost tasks blocking expedited grace periods.
298 * This cannot starve the normal grace periods because a second
299 * expedited grace period must boost all blocked tasks, including
300 * those blocking the pre-existing normal grace period.
301 */
302 if (rcu_preempt_ctrlblk.exp_tasks != NULL) {
303 tb = rcu_preempt_ctrlblk.exp_tasks;
304 RCU_TRACE(rcu_preempt_ctrlblk.n_exp_boosts++);
305 } else {
306 tb = rcu_preempt_ctrlblk.boost_tasks;
307 RCU_TRACE(rcu_preempt_ctrlblk.n_normal_boosts++);
308 }
309 RCU_TRACE(rcu_preempt_ctrlblk.n_tasks_boosted++);
310
311 /*
312 * We boost task t by manufacturing an rt_mutex that appears to
313 * be held by task t. We leave a pointer to that rt_mutex where
314 * task t can find it, and task t will release the mutex when it
315 * exits its outermost RCU read-side critical section. Then
316 * simply acquiring this artificial rt_mutex will boost task
317 * t's priority. (Thanks to tglx for suggesting this approach!)
318 */
319 t = container_of(tb, struct task_struct, rcu_node_entry);
320 rt_mutex_init_proxy_locked(&mtx, t);
321 t->rcu_boost_mutex = &mtx;
322 raw_local_irq_restore(flags);
323 rt_mutex_lock(&mtx);
324 rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
325
326 return ACCESS_ONCE(rcu_preempt_ctrlblk.boost_tasks) != NULL ||
327 ACCESS_ONCE(rcu_preempt_ctrlblk.exp_tasks) != NULL;
328}
329
330/*
331 * Check to see if it is now time to start boosting RCU readers blocking
332 * the current grace period, and, if so, tell the rcu_kthread_task to
333 * start boosting them. If there is an expedited boost in progress,
334 * we wait for it to complete.
335 *
336 * If there are no blocked readers blocking the current grace period,
337 * return 0 to let the caller know, otherwise return 1. Note that this
338 * return value is independent of whether or not boosting was done.
339 */
340static int rcu_initiate_boost(void)
341{
342 if (!rcu_preempt_blocked_readers_cgp() &&
343 rcu_preempt_ctrlblk.exp_tasks == NULL) {
344 RCU_TRACE(rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++);
345 return 0;
346 }
347 if (rcu_preempt_ctrlblk.exp_tasks != NULL ||
348 (rcu_preempt_ctrlblk.gp_tasks != NULL &&
349 rcu_preempt_ctrlblk.boost_tasks == NULL &&
350 ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))) {
351 if (rcu_preempt_ctrlblk.exp_tasks == NULL)
352 rcu_preempt_ctrlblk.boost_tasks =
353 rcu_preempt_ctrlblk.gp_tasks;
354 invoke_rcu_callbacks();
355 } else
356 RCU_TRACE(rcu_initiate_boost_trace());
357 return 1;
358}
359
360#define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
361
362/*
363 * Do priority-boost accounting for the start of a new grace period.
364 */
365static void rcu_preempt_boost_start_gp(void)
366{
367 rcu_preempt_ctrlblk.boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
368}
369
370#else /* #ifdef CONFIG_RCU_BOOST */
371
372/*
373 * If there is no RCU priority boosting, we don't initiate boosting,
374 * but we do indicate whether there are blocked readers blocking the
375 * current grace period.
376 */
377static int rcu_initiate_boost(void)
378{
379 return rcu_preempt_blocked_readers_cgp();
380}
381
382/*
383 * If there is no RCU priority boosting, nothing to do at grace-period start.
384 */
385static void rcu_preempt_boost_start_gp(void)
386{
387}
388
389#endif /* else #ifdef CONFIG_RCU_BOOST */
390
391/*
392 * Record a preemptible-RCU quiescent state for the specified CPU. Note
393 * that this just means that the task currently running on the CPU is
394 * in a quiescent state. There might be any number of tasks blocked
395 * while in an RCU read-side critical section.
396 *
397 * Unlike the other rcu_*_qs() functions, callers to this function
398 * must disable irqs in order to protect the assignment to
399 * ->rcu_read_unlock_special.
400 *
401 * Because this is a single-CPU implementation, the only way a grace
402 * period can end is if the CPU is in a quiescent state. The reason is
403 * that a blocked preemptible-RCU reader can exit its critical section
404 * only if the CPU is running it at the time. Therefore, when the
405 * last task blocking the current grace period exits its RCU read-side
406 * critical section, neither the CPU nor blocked tasks will be stopping
407 * the current grace period. (In contrast, SMP implementations
408 * might have CPUs running in RCU read-side critical sections that
409 * block later grace periods -- but this is not possible given only
410 * one CPU.)
411 */
412static void rcu_preempt_cpu_qs(void)
413{
414 /* Record both CPU and task as having responded to current GP. */
415 rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum;
416 current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
417
418 /* If there is no GP then there is nothing more to do. */
419 if (!rcu_preempt_gp_in_progress())
420 return;
421 /*
422 * Check up on boosting. If there are readers blocking the
423 * current grace period, leave.
424 */
425 if (rcu_initiate_boost())
426 return;
427
428 /* Advance callbacks. */
429 rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum;
430 rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail;
431 rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail;
432
433 /* If there are no blocked readers, next GP is done instantly. */
434 if (!rcu_preempt_blocked_readers_any())
435 rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail;
436
437 /* If there are done callbacks, cause them to be invoked. */
438 if (*rcu_preempt_ctrlblk.rcb.donetail != NULL)
439 invoke_rcu_callbacks();
440}
441
442/*
443 * Start a new RCU grace period if warranted. Hard irqs must be disabled.
444 */
445static void rcu_preempt_start_gp(void)
446{
447 if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) {
448
449 /* Official start of GP. */
450 rcu_preempt_ctrlblk.gpnum++;
451 RCU_TRACE(rcu_preempt_ctrlblk.n_grace_periods++);
452
453 /* Any blocked RCU readers block new GP. */
454 if (rcu_preempt_blocked_readers_any())
455 rcu_preempt_ctrlblk.gp_tasks =
456 rcu_preempt_ctrlblk.blkd_tasks.next;
457
458 /* Set up for RCU priority boosting. */
459 rcu_preempt_boost_start_gp();
460
461 /* If there is no running reader, CPU is done with GP. */
462 if (!rcu_preempt_running_reader())
463 rcu_preempt_cpu_qs();
464 }
465}
466
467/*
468 * We have entered the scheduler, and the current task might soon be
469 * context-switched away from. If this task is in an RCU read-side
470 * critical section, we will no longer be able to rely on the CPU to
471 * record that fact, so we enqueue the task on the blkd_tasks list.
472 * If the task started after the current grace period began, as recorded
473 * by ->gpcpu, we enqueue at the beginning of the list. Otherwise
474 * before the element referenced by ->gp_tasks (or at the tail if
475 * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element.
476 * The task will dequeue itself when it exits the outermost enclosing
477 * RCU read-side critical section. Therefore, the current grace period
478 * cannot be permitted to complete until the ->gp_tasks pointer becomes
479 * NULL.
480 *
481 * Caller must disable preemption.
482 */
483void rcu_preempt_note_context_switch(void)
484{
485 struct task_struct *t = current;
486 unsigned long flags;
487
488 local_irq_save(flags); /* must exclude scheduler_tick(). */
489 if (rcu_preempt_running_reader() > 0 &&
490 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
491
492 /* Possibly blocking in an RCU read-side critical section. */
493 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
494
495 /*
496 * If this CPU has already checked in, then this task
497 * will hold up the next grace period rather than the
498 * current grace period. Queue the task accordingly.
499 * If the task is queued for the current grace period
500 * (i.e., this CPU has not yet passed through a quiescent
501 * state for the current grace period), then as long
502 * as that task remains queued, the current grace period
503 * cannot end.
504 */
505 list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks);
506 if (rcu_cpu_blocking_cur_gp())
507 rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry;
508 } else if (rcu_preempt_running_reader() < 0 &&
509 t->rcu_read_unlock_special) {
510 /*
511 * Complete exit from RCU read-side critical section on
512 * behalf of preempted instance of __rcu_read_unlock().
513 */
514 rcu_read_unlock_special(t);
515 }
516
517 /*
518 * Either we were not in an RCU read-side critical section to
519 * begin with, or we have now recorded that critical section
520 * globally. Either way, we can now note a quiescent state
521 * for this CPU. Again, if we were in an RCU read-side critical
522 * section, and if that critical section was blocking the current
523 * grace period, then the fact that the task has been enqueued
524 * means that current grace period continues to be blocked.
525 */
526 rcu_preempt_cpu_qs();
527 local_irq_restore(flags);
528}
529
530/*
531 * Tiny-preemptible RCU implementation for rcu_read_lock().
532 * Just increment ->rcu_read_lock_nesting, shared state will be updated
533 * if we block.
534 */
535void __rcu_read_lock(void)
536{
537 current->rcu_read_lock_nesting++;
538 barrier(); /* needed if we ever invoke rcu_read_lock in rcutiny.c */
539}
540EXPORT_SYMBOL_GPL(__rcu_read_lock);
541
542/*
543 * Handle special cases during rcu_read_unlock(), such as needing to
544 * notify RCU core processing or task having blocked during the RCU
545 * read-side critical section.
546 */
547static noinline void rcu_read_unlock_special(struct task_struct *t)
548{
549 int empty;
550 int empty_exp;
551 unsigned long flags;
552 struct list_head *np;
553#ifdef CONFIG_RCU_BOOST
554 struct rt_mutex *rbmp = NULL;
555#endif /* #ifdef CONFIG_RCU_BOOST */
556 int special;
557
558 /*
559 * NMI handlers cannot block and cannot safely manipulate state.
560 * They therefore cannot possibly be special, so just leave.
561 */
562 if (in_nmi())
563 return;
564
565 local_irq_save(flags);
566
567 /*
568 * If RCU core is waiting for this CPU to exit critical section,
569 * let it know that we have done so.
570 */
571 special = t->rcu_read_unlock_special;
572 if (special & RCU_READ_UNLOCK_NEED_QS)
573 rcu_preempt_cpu_qs();
574
575 /* Hardware IRQ handlers cannot block. */
576 if (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_OFFSET)) {
577 local_irq_restore(flags);
578 return;
579 }
580
581 /* Clean up if blocked during RCU read-side critical section. */
582 if (special & RCU_READ_UNLOCK_BLOCKED) {
583 t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
584
585 /*
586 * Remove this task from the ->blkd_tasks list and adjust
587 * any pointers that might have been referencing it.
588 */
589 empty = !rcu_preempt_blocked_readers_cgp();
590 empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
591 np = rcu_next_node_entry(t);
592 list_del_init(&t->rcu_node_entry);
593 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
594 rcu_preempt_ctrlblk.gp_tasks = np;
595 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
596 rcu_preempt_ctrlblk.exp_tasks = np;
597#ifdef CONFIG_RCU_BOOST
598 if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks)
599 rcu_preempt_ctrlblk.boost_tasks = np;
600#endif /* #ifdef CONFIG_RCU_BOOST */
601
602 /*
603 * If this was the last task on the current list, and if
604 * we aren't waiting on the CPU, report the quiescent state
605 * and start a new grace period if needed.
606 */
607 if (!empty && !rcu_preempt_blocked_readers_cgp()) {
608 rcu_preempt_cpu_qs();
609 rcu_preempt_start_gp();
610 }
611
612 /*
613 * If this was the last task on the expedited lists,
614 * then we need wake up the waiting task.
615 */
616 if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
617 rcu_report_exp_done();
618 }
619#ifdef CONFIG_RCU_BOOST
620 /* Unboost self if was boosted. */
621 if (t->rcu_boost_mutex != NULL) {
622 rbmp = t->rcu_boost_mutex;
623 t->rcu_boost_mutex = NULL;
624 rt_mutex_unlock(rbmp);
625 }
626#endif /* #ifdef CONFIG_RCU_BOOST */
627 local_irq_restore(flags);
628}
629
630/*
631 * Tiny-preemptible RCU implementation for rcu_read_unlock().
632 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
633 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
634 * invoke rcu_read_unlock_special() to clean up after a context switch
635 * in an RCU read-side critical section and other special cases.
636 */
637void __rcu_read_unlock(void)
638{
639 struct task_struct *t = current;
640
641 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
642 if (t->rcu_read_lock_nesting != 1)
643 --t->rcu_read_lock_nesting;
644 else {
645 t->rcu_read_lock_nesting = INT_MIN;
646 barrier(); /* assign before ->rcu_read_unlock_special load */
647 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
648 rcu_read_unlock_special(t);
649 barrier(); /* ->rcu_read_unlock_special load before assign */
650 t->rcu_read_lock_nesting = 0;
651 }
652#ifdef CONFIG_PROVE_LOCKING
653 {
654 int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
655
656 WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
657 }
658#endif /* #ifdef CONFIG_PROVE_LOCKING */
659}
660EXPORT_SYMBOL_GPL(__rcu_read_unlock);
661
662/*
663 * Check for a quiescent state from the current CPU. When a task blocks,
664 * the task is recorded in the rcu_preempt_ctrlblk structure, which is
665 * checked elsewhere. This is called from the scheduling-clock interrupt.
666 *
667 * Caller must disable hard irqs.
668 */
669static void rcu_preempt_check_callbacks(void)
670{
671 struct task_struct *t = current;
672
673 if (rcu_preempt_gp_in_progress() &&
674 (!rcu_preempt_running_reader() ||
675 !rcu_cpu_blocking_cur_gp()))
676 rcu_preempt_cpu_qs();
677 if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
678 rcu_preempt_ctrlblk.rcb.donetail)
679 invoke_rcu_callbacks();
680 if (rcu_preempt_gp_in_progress() &&
681 rcu_cpu_blocking_cur_gp() &&
682 rcu_preempt_running_reader() > 0)
683 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
684}
685
686/*
687 * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
688 * update, so this is invoked from rcu_process_callbacks() to
689 * handle that case. Of course, it is invoked for all flavors of
690 * RCU, but RCU callbacks can appear only on one of the lists, and
691 * neither ->nexttail nor ->donetail can possibly be NULL, so there
692 * is no need for an explicit check.
693 */
694static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
695{
696 if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
697 rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
698}
699
700/*
701 * Process callbacks for preemptible RCU.
702 */
703static void rcu_preempt_process_callbacks(void)
704{
705 __rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
706}
707
708/*
709 * Queue a preemptible -RCU callback for invocation after a grace period.
710 */
711void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
712{
713 unsigned long flags;
714
715 debug_rcu_head_queue(head);
716 head->func = func;
717 head->next = NULL;
718
719 local_irq_save(flags);
720 *rcu_preempt_ctrlblk.nexttail = head;
721 rcu_preempt_ctrlblk.nexttail = &head->next;
722 RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
723 rcu_preempt_start_gp(); /* checks to see if GP needed. */
724 local_irq_restore(flags);
725}
726EXPORT_SYMBOL(call_rcu);
727
728/*
729 * synchronize_rcu - wait until a grace period has elapsed.
730 *
731 * Control will return to the caller some time after a full grace
732 * period has elapsed, in other words after all currently executing RCU
733 * read-side critical sections have completed. RCU read-side critical
734 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
735 * and may be nested.
736 */
737void synchronize_rcu(void)
738{
739 rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
740 !lock_is_held(&rcu_lock_map) &&
741 !lock_is_held(&rcu_sched_lock_map),
742 "Illegal synchronize_rcu() in RCU read-side critical section");
743
744#ifdef CONFIG_DEBUG_LOCK_ALLOC
745 if (!rcu_scheduler_active)
746 return;
747#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
748
749 WARN_ON_ONCE(rcu_preempt_running_reader());
750 if (!rcu_preempt_blocked_readers_any())
751 return;
752
753 /* Once we get past the fastpath checks, same code as rcu_barrier(). */
754 rcu_barrier();
755}
756EXPORT_SYMBOL_GPL(synchronize_rcu);
757
758static DEFINE_SWAIT_HEAD(sync_rcu_preempt_exp_wq);
759static unsigned long sync_rcu_preempt_exp_count;
760static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
761
762/*
763 * Return non-zero if there are any tasks in RCU read-side critical
764 * sections blocking the current preemptible-RCU expedited grace period.
765 * If there is no preemptible-RCU expedited grace period currently in
766 * progress, returns zero unconditionally.
767 */
768static int rcu_preempted_readers_exp(void)
769{
770 return rcu_preempt_ctrlblk.exp_tasks != NULL;
771}
772
773/*
774 * Report the exit from RCU read-side critical section for the last task
775 * that queued itself during or before the current expedited preemptible-RCU
776 * grace period.
777 */
778static void rcu_report_exp_done(void)
779{
780 swait_wake(&sync_rcu_preempt_exp_wq);
781}
782
783/*
784 * Wait for an rcu-preempt grace period, but expedite it. The basic idea
785 * is to rely in the fact that there is but one CPU, and that it is
786 * illegal for a task to invoke synchronize_rcu_expedited() while in a
787 * preemptible-RCU read-side critical section. Therefore, any such
788 * critical sections must correspond to blocked tasks, which must therefore
789 * be on the ->blkd_tasks list. So just record the current head of the
790 * list in the ->exp_tasks pointer, and wait for all tasks including and
791 * after the task pointed to by ->exp_tasks to drain.
792 */
793void synchronize_rcu_expedited(void)
794{
795 unsigned long flags;
796 struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
797 unsigned long snap;
798
799 barrier(); /* ensure prior action seen before grace period. */
800
801 WARN_ON_ONCE(rcu_preempt_running_reader());
802
803 /*
804 * Acquire lock so that there is only one preemptible RCU grace
805 * period in flight. Of course, if someone does the expedited
806 * grace period for us while we are acquiring the lock, just leave.
807 */
808 snap = sync_rcu_preempt_exp_count + 1;
809 mutex_lock(&sync_rcu_preempt_exp_mutex);
810 if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
811 goto unlock_mb_ret; /* Others did our work for us. */
812
813 local_irq_save(flags);
814
815 /*
816 * All RCU readers have to already be on blkd_tasks because
817 * we cannot legally be executing in an RCU read-side critical
818 * section.
819 */
820
821 /* Snapshot current head of ->blkd_tasks list. */
822 rpcp->exp_tasks = rpcp->blkd_tasks.next;
823 if (rpcp->exp_tasks == &rpcp->blkd_tasks)
824 rpcp->exp_tasks = NULL;
825
826 /* Wait for tail of ->blkd_tasks list to drain. */
827 if (!rcu_preempted_readers_exp())
828 local_irq_restore(flags);
829 else {
830 rcu_initiate_boost();
831 local_irq_restore(flags);
832 swait_event(sync_rcu_preempt_exp_wq,
833 !rcu_preempted_readers_exp());
834 }
835
836 /* Clean up and exit. */
837 barrier(); /* ensure expedited GP seen before counter increment. */
838 sync_rcu_preempt_exp_count++;
839unlock_mb_ret:
840 mutex_unlock(&sync_rcu_preempt_exp_mutex);
841 barrier(); /* ensure subsequent action seen after grace period. */
842}
843EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
844
845/*
846 * Does preemptible RCU need the CPU to stay out of dynticks mode?
847 */
848int rcu_preempt_needs_cpu(void)
849{
850 if (!rcu_preempt_running_reader())
851 rcu_preempt_cpu_qs();
852 return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
853}
854
855/*
856 * Check for a task exiting while in a preemptible -RCU read-side
857 * critical section, clean up if so. No need to issue warnings,
858 * as debug_check_no_locks_held() already does this if lockdep
859 * is enabled.
860 */
861void exit_rcu(void)
862{
863 struct task_struct *t = current;
864
865 if (t->rcu_read_lock_nesting == 0)
866 return;
867 t->rcu_read_lock_nesting = 1;
868 __rcu_read_unlock();
869}
870
871#else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
872
873#ifdef CONFIG_RCU_TRACE
874
875/*
876 * Because preemptible RCU does not exist, it is not necessary to
877 * dump out its statistics.
878 */
879static void show_tiny_preempt_stats(struct seq_file *m)
880{
881}
882
883#endif /* #ifdef CONFIG_RCU_TRACE */
884
885/*
886 * Because preemptible RCU does not exist, it never has any callbacks
887 * to check.
888 */
889static void rcu_preempt_check_callbacks(void)
890{
891}
892
893/*
894 * Because preemptible RCU does not exist, it never has any callbacks
895 * to remove.
896 */
897static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
898{
899}
900
901/*
902 * Because preemptible RCU does not exist, it never has any callbacks
903 * to process.
904 */
905static void rcu_preempt_process_callbacks(void)
906{
907}
908
909#endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
910
911#ifdef CONFIG_RCU_BOOST
912
913/*
914 * Wake up rcu_kthread() to process callbacks now eligible for invocation
915 * or to boost readers.
916 */
917static void invoke_rcu_callbacks(void)
918{
919 have_rcu_kthread_work = 1;
920 if (rcu_kthread_task != NULL)
921 swait_wake(&rcu_kthread_wq);
922}
923
924#ifdef CONFIG_RCU_TRACE
925
926/*
927 * Is the current CPU running the RCU-callbacks kthread?
928 * Caller must have preemption disabled.
929 */
930static bool rcu_is_callbacks_kthread(void)
931{
932 return rcu_kthread_task == current;
933}
934
935#endif /* #ifdef CONFIG_RCU_TRACE */
936
937/*
938 * This kthread invokes RCU callbacks whose grace periods have
939 * elapsed. It is awakened as needed, and takes the place of the
940 * RCU_SOFTIRQ that is used for this purpose when boosting is disabled.
941 * This is a kthread, but it is never stopped, at least not until
942 * the system goes down.
943 */
944static int rcu_kthread(void *arg)
945{
946 unsigned long work;
947 unsigned long morework;
948 unsigned long flags;
949
950 for (;;) {
951 swait_event_interruptible(rcu_kthread_wq,
952 have_rcu_kthread_work != 0);
953 morework = rcu_boost();
954 local_irq_save(flags);
955 work = have_rcu_kthread_work;
956 have_rcu_kthread_work = morework;
957 local_irq_restore(flags);
958 if (work)
959 rcu_process_callbacks(NULL);
960 schedule_timeout_interruptible(1); /* Leave CPU for others. */
961 }
962
963 return 0; /* Not reached, but needed to shut gcc up. */
964}
965
966/*
967 * Spawn the kthread that invokes RCU callbacks.
968 */
969static int __init rcu_spawn_kthreads(void)
970{
971 struct sched_param sp;
972
973 rcu_kthread_task = kthread_run(rcu_kthread, NULL, "rcu_kthread");
974 sp.sched_priority = RCU_BOOST_PRIO;
975 sched_setscheduler_nocheck(rcu_kthread_task, SCHED_FIFO, &sp);
976 return 0;
977}
978early_initcall(rcu_spawn_kthreads);
979
980#else /* #ifdef CONFIG_RCU_BOOST */
981
982/* Hold off callback invocation until early_initcall() time. */
983static int rcu_scheduler_fully_active __read_mostly;
984
985/*
986 * Start up softirq processing of callbacks.
987 */
988void invoke_rcu_callbacks(void)
989{
990 if (rcu_scheduler_fully_active)
991 raise_softirq(RCU_SOFTIRQ);
992}
993
994#ifdef CONFIG_RCU_TRACE
995
996/*
997 * There is no callback kthread, so this thread is never it.
998 */
999static bool rcu_is_callbacks_kthread(void)
1000{
1001 return false;
1002}
1003
1004#endif /* #ifdef CONFIG_RCU_TRACE */
1005
1006static int __init rcu_scheduler_really_started(void)
1007{
1008 rcu_scheduler_fully_active = 1;
1009 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
1010 raise_softirq(RCU_SOFTIRQ); /* Invoke any callbacks from early boot. */
1011 return 0;
1012}
1013early_initcall(rcu_scheduler_really_started);
1014
1015#endif /* #else #ifdef CONFIG_RCU_BOOST */
1016
1017#ifdef CONFIG_DEBUG_LOCK_ALLOC
1018#include <linux/kernel_stat.h>
1019
1020/*
1021 * During boot, we forgive RCU lockdep issues. After this function is
1022 * invoked, we start taking RCU lockdep issues seriously.
1023 */
1024void __init rcu_scheduler_starting(void)
1025{
1026 WARN_ON(nr_context_switches() > 0);
1027 rcu_scheduler_active = 1;
1028}
1029
1030#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
1031
1032#ifdef CONFIG_RCU_TRACE
1033
1034#ifdef CONFIG_RCU_BOOST
1035
1036static void rcu_initiate_boost_trace(void)
1037{
1038 if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
1039 rcu_preempt_ctrlblk.n_balk_blkd_tasks++;
1040 else if (rcu_preempt_ctrlblk.gp_tasks == NULL &&
1041 rcu_preempt_ctrlblk.exp_tasks == NULL)
1042 rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++;
1043 else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
1044 rcu_preempt_ctrlblk.n_balk_boost_tasks++;
1045 else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
1046 rcu_preempt_ctrlblk.n_balk_notyet++;
1047 else
1048 rcu_preempt_ctrlblk.n_balk_nos++;
1049}
1050
1051#endif /* #ifdef CONFIG_RCU_BOOST */
1052
1053static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
1054{
1055 unsigned long flags;
1056
1057 raw_local_irq_save(flags);
1058 rcp->qlen -= n;
1059 raw_local_irq_restore(flags);
1060}
1061
1062/*
1063 * Dump statistics for TINY_RCU, such as they are.
1064 */
1065static int show_tiny_stats(struct seq_file *m, void *unused)
1066{
1067 show_tiny_preempt_stats(m);
1068 seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
1069 seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
1070 return 0;
1071}
1072
1073static int show_tiny_stats_open(struct inode *inode, struct file *file)
1074{
1075 return single_open(file, show_tiny_stats, NULL);
1076}
1077
1078static const struct file_operations show_tiny_stats_fops = {
1079 .owner = THIS_MODULE,
1080 .open = show_tiny_stats_open,
1081 .read = seq_read,
1082 .llseek = seq_lseek,
1083 .release = single_release,
1084};
1085
1086static struct dentry *rcudir;
1087
1088static int __init rcutiny_trace_init(void)
1089{
1090 struct dentry *retval;
1091
1092 rcudir = debugfs_create_dir("rcu", NULL);
1093 if (!rcudir)
1094 goto free_out;
1095 retval = debugfs_create_file("rcudata", 0444, rcudir,
1096 NULL, &show_tiny_stats_fops);
1097 if (!retval)
1098 goto free_out;
1099 return 0;
1100free_out:
1101 debugfs_remove_recursive(rcudir);
1102 return 1;
1103}
1104
1105static void __exit rcutiny_trace_cleanup(void)
1106{
1107 debugfs_remove_recursive(rcudir);
1108}
1109
1110module_init(rcutiny_trace_init);
1111module_exit(rcutiny_trace_cleanup);
1112
1113MODULE_AUTHOR("Paul E. McKenney");
1114MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
1115MODULE_LICENSE("GPL");
1116
1117#endif /* #ifdef CONFIG_RCU_TRACE */