2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
19 * Copyright IBM Corporation, 2008
21 * Author: Ingo Molnar <mingo@elte.hu>
22 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
25 #include <linux/cache.h>
26 #include <linux/spinlock.h>
27 #include <linux/rtmutex.h>
28 #include <linux/threads.h>
29 #include <linux/cpumask.h>
30 #include <linux/seqlock.h>
31 #include <linux/swait.h>
32 #include <linux/stop_machine.h>
33 #include <linux/rcu_node_tree.h>
35 #include "rcu_segcblist.h"
37 /* Communicate arguments to a workqueue handler. */
39 smp_call_func_t rew_func
;
41 struct work_struct rew_work
;
44 /* RCU's kthread states for tracing. */
45 #define RCU_KTHREAD_STOPPED 0
46 #define RCU_KTHREAD_RUNNING 1
47 #define RCU_KTHREAD_WAITING 2
48 #define RCU_KTHREAD_OFFCPU 3
49 #define RCU_KTHREAD_YIELDING 4
50 #define RCU_KTHREAD_MAX 4
53 * Definition for node within the RCU grace-period-detection hierarchy.
56 raw_spinlock_t __private lock
; /* Root rcu_node's lock protects */
57 /* some rcu_state fields as well as */
59 unsigned long gp_seq
; /* Track rsp->rcu_gp_seq. */
60 unsigned long gp_seq_needed
; /* Track rsp->rcu_gp_seq_needed. */
61 unsigned long completedqs
; /* All QSes done for this node. */
62 unsigned long qsmask
; /* CPUs or groups that need to switch in */
63 /* order for current grace period to proceed.*/
64 /* In leaf rcu_node, each bit corresponds to */
65 /* an rcu_data structure, otherwise, each */
66 /* bit corresponds to a child rcu_node */
68 unsigned long rcu_gp_init_mask
; /* Mask of offline CPUs at GP init. */
69 unsigned long qsmaskinit
;
70 /* Per-GP initial value for qsmask. */
71 /* Initialized from ->qsmaskinitnext at the */
72 /* beginning of each grace period. */
73 unsigned long qsmaskinitnext
;
74 /* Online CPUs for next grace period. */
75 unsigned long expmask
; /* CPUs or groups that need to check in */
76 /* to allow the current expedited GP */
78 unsigned long expmaskinit
;
79 /* Per-GP initial values for expmask. */
80 /* Initialized from ->expmaskinitnext at the */
81 /* beginning of each expedited GP. */
82 unsigned long expmaskinitnext
;
83 /* Online CPUs for next expedited GP. */
84 /* Any CPU that has ever been online will */
85 /* have its bit set. */
86 unsigned long ffmask
; /* Fully functional CPUs. */
87 unsigned long grpmask
; /* Mask to apply to parent qsmask. */
88 /* Only one bit will be set in this mask. */
89 int grplo
; /* lowest-numbered CPU or group here. */
90 int grphi
; /* highest-numbered CPU or group here. */
91 u8 grpnum
; /* CPU/group number for next level up. */
92 u8 level
; /* root is at level 0. */
93 bool wait_blkd_tasks
;/* Necessary to wait for blocked tasks to */
94 /* exit RCU read-side critical sections */
95 /* before propagating offline up the */
97 struct rcu_node
*parent
;
98 struct list_head blkd_tasks
;
99 /* Tasks blocked in RCU read-side critical */
100 /* section. Tasks are placed at the head */
101 /* of this list and age towards the tail. */
102 struct list_head
*gp_tasks
;
103 /* Pointer to the first task blocking the */
104 /* current grace period, or NULL if there */
105 /* is no such task. */
106 struct list_head
*exp_tasks
;
107 /* Pointer to the first task blocking the */
108 /* current expedited grace period, or NULL */
109 /* if there is no such task. If there */
110 /* is no current expedited grace period, */
111 /* then there can cannot be any such task. */
112 struct list_head
*boost_tasks
;
113 /* Pointer to first task that needs to be */
114 /* priority boosted, or NULL if no priority */
115 /* boosting is needed for this rcu_node */
116 /* structure. If there are no tasks */
117 /* queued on this rcu_node structure that */
118 /* are blocking the current grace period, */
119 /* there can be no such task. */
120 struct rt_mutex boost_mtx
;
121 /* Used only for the priority-boosting */
122 /* side effect, not as a lock. */
123 unsigned long boost_time
;
124 /* When to start boosting (jiffies). */
125 struct task_struct
*boost_kthread_task
;
126 /* kthread that takes care of priority */
127 /* boosting for this rcu_node structure. */
128 unsigned int boost_kthread_status
;
129 /* State of boost_kthread_task for tracing. */
130 #ifdef CONFIG_RCU_NOCB_CPU
131 struct swait_queue_head nocb_gp_wq
[2];
132 /* Place for rcu_nocb_kthread() to wait GP. */
133 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
134 raw_spinlock_t fqslock ____cacheline_internodealigned_in_smp
;
136 spinlock_t exp_lock ____cacheline_internodealigned_in_smp
;
137 unsigned long exp_seq_rq
;
138 wait_queue_head_t exp_wq
[4];
139 struct rcu_exp_work rew
;
140 bool exp_need_flush
; /* Need to flush workitem? */
141 } ____cacheline_internodealigned_in_smp
;
144 * Bitmasks in an rcu_node cover the interval [grplo, grphi] of CPU IDs, and
145 * are indexed relative to this interval rather than the global CPU ID space.
146 * This generates the bit for a CPU in node-local masks.
148 #define leaf_node_cpu_bit(rnp, cpu) (BIT((cpu) - (rnp)->grplo))
151 * Union to allow "aggregate OR" operation on the need for a quiescent
152 * state by the normal and expedited grace periods.
159 u16 s
; /* Set of bits, aggregate OR here. */
162 /* Per-CPU data for read-copy update. */
164 /* 1) quiescent-state and grace-period handling : */
165 unsigned long gp_seq
; /* Track rsp->rcu_gp_seq counter. */
166 unsigned long gp_seq_needed
; /* Track rsp->rcu_gp_seq_needed ctr. */
167 union rcu_noqs cpu_no_qs
; /* No QSes yet for this CPU. */
168 bool core_needs_qs
; /* Core waits for quiesc state. */
169 bool beenonline
; /* CPU online at least once. */
170 bool gpwrap
; /* Possible ->gp_seq wrap. */
171 bool deferred_qs
; /* This CPU awaiting a deferred QS? */
172 struct rcu_node
*mynode
; /* This CPU's leaf of hierarchy */
173 unsigned long grpmask
; /* Mask to apply to leaf qsmask. */
174 unsigned long ticks_this_gp
; /* The number of scheduling-clock */
175 /* ticks this CPU has handled */
176 /* during and after the last grace */
177 /* period it is aware of. */
179 /* 2) batch handling */
180 struct rcu_segcblist cblist
; /* Segmented callback list, with */
181 /* different callbacks waiting for */
182 /* different grace periods. */
183 long qlen_last_fqs_check
;
184 /* qlen at last check for QS forcing */
185 unsigned long n_force_qs_snap
;
186 /* did other CPU force QS recently? */
187 long blimit
; /* Upper limit on a processed batch */
189 /* 3) dynticks interface. */
190 int dynticks_snap
; /* Per-GP tracking for dynticks. */
191 long dynticks_nesting
; /* Track process nesting level. */
192 long dynticks_nmi_nesting
; /* Track irq/NMI nesting level. */
193 atomic_t dynticks
; /* Even value for idle, else odd. */
194 bool rcu_need_heavy_qs
; /* GP old, so heavy quiescent state! */
195 bool rcu_urgent_qs
; /* GP old need light quiescent state. */
196 #ifdef CONFIG_RCU_FAST_NO_HZ
197 bool all_lazy
; /* Are all CPU's CBs lazy? */
198 unsigned long nonlazy_posted
; /* # times non-lazy CB posted to CPU. */
199 unsigned long nonlazy_posted_snap
;
200 /* Nonlazy_posted snapshot. */
201 unsigned long last_accelerate
; /* Last jiffy CBs were accelerated. */
202 unsigned long last_advance_all
; /* Last jiffy CBs were all advanced. */
203 int tick_nohz_enabled_snap
; /* Previously seen value from sysfs. */
204 #endif /* #ifdef CONFIG_RCU_FAST_NO_HZ */
206 /* 4) rcu_barrier(), OOM callbacks, and expediting. */
207 struct rcu_head barrier_head
;
208 int exp_dynticks_snap
; /* Double-check need for IPI. */
210 /* 5) Callback offloading. */
211 #ifdef CONFIG_RCU_NOCB_CPU
212 struct rcu_head
*nocb_head
; /* CBs waiting for kthread. */
213 struct rcu_head
**nocb_tail
;
214 atomic_long_t nocb_q_count
; /* # CBs waiting for nocb */
215 atomic_long_t nocb_q_count_lazy
; /* invocation (all stages). */
216 struct rcu_head
*nocb_follower_head
; /* CBs ready to invoke. */
217 struct rcu_head
**nocb_follower_tail
;
218 struct swait_queue_head nocb_wq
; /* For nocb kthreads to sleep on. */
219 struct task_struct
*nocb_kthread
;
220 raw_spinlock_t nocb_lock
; /* Guard following pair of fields. */
221 int nocb_defer_wakeup
; /* Defer wakeup of nocb_kthread. */
222 struct timer_list nocb_timer
; /* Enforce finite deferral. */
224 /* The following fields are used by the leader, hence own cacheline. */
225 struct rcu_head
*nocb_gp_head ____cacheline_internodealigned_in_smp
;
226 /* CBs waiting for GP. */
227 struct rcu_head
**nocb_gp_tail
;
228 bool nocb_leader_sleep
; /* Is the nocb leader thread asleep? */
229 struct rcu_data
*nocb_next_follower
;
230 /* Next follower in wakeup chain. */
232 /* The following fields are used by the follower, hence new cachline. */
233 struct rcu_data
*nocb_leader ____cacheline_internodealigned_in_smp
;
234 /* Leader CPU takes GP-end wakeups. */
235 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
237 /* 6) Diagnostic data, including RCU CPU stall warnings. */
238 unsigned int softirq_snap
; /* Snapshot of softirq activity. */
239 /* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
240 struct irq_work rcu_iw
; /* Check for non-irq activity. */
241 bool rcu_iw_pending
; /* Is ->rcu_iw pending? */
242 unsigned long rcu_iw_gp_seq
; /* ->gp_seq associated with ->rcu_iw. */
243 unsigned long rcu_ofl_gp_seq
; /* ->gp_seq at last offline. */
244 short rcu_ofl_gp_flags
; /* ->gp_flags at last offline. */
245 unsigned long rcu_onl_gp_seq
; /* ->gp_seq at last online. */
246 short rcu_onl_gp_flags
; /* ->gp_flags at last online. */
247 unsigned long last_fqs_resched
; /* Time of last rcu_resched(). */
252 /* Values for nocb_defer_wakeup field in struct rcu_data. */
253 #define RCU_NOCB_WAKE_NOT 0
254 #define RCU_NOCB_WAKE 1
255 #define RCU_NOCB_WAKE_FORCE 2
257 #define RCU_JIFFIES_TILL_FORCE_QS (1 + (HZ > 250) + (HZ > 500))
258 /* For jiffies_till_first_fqs and */
259 /* and jiffies_till_next_fqs. */
261 #define RCU_JIFFIES_FQS_DIV 256 /* Very large systems need more */
262 /* delay between bouts of */
263 /* quiescent-state forcing. */
265 #define RCU_STALL_RAT_DELAY 2 /* Allow other CPUs time to take */
266 /* at least one scheduling clock */
267 /* irq before ratting on them. */
269 #define rcu_wait(cond) \
272 set_current_state(TASK_INTERRUPTIBLE); \
277 __set_current_state(TASK_RUNNING); \
281 * RCU global state, including node hierarchy. This hierarchy is
282 * represented in "heap" form in a dense array. The root (first level)
283 * of the hierarchy is in ->node[0] (referenced by ->level[0]), the second
284 * level in ->node[1] through ->node[m] (->node[1] referenced by ->level[1]),
285 * and the third level in ->node[m+1] and following (->node[m+1] referenced
286 * by ->level[2]). The number of levels is determined by the number of
287 * CPUs and by CONFIG_RCU_FANOUT. Small systems will have a "hierarchy"
288 * consisting of a single rcu_node.
291 struct rcu_node node
[NUM_RCU_NODES
]; /* Hierarchy. */
292 struct rcu_node
*level
[RCU_NUM_LVLS
+ 1];
293 /* Hierarchy levels (+1 to */
294 /* shut bogus gcc warning) */
295 int ncpus
; /* # CPUs seen so far. */
297 /* The following fields are guarded by the root rcu_node's lock. */
299 u8 boost ____cacheline_internodealigned_in_smp
;
300 /* Subject to priority boost. */
301 unsigned long gp_seq
; /* Grace-period sequence #. */
302 struct task_struct
*gp_kthread
; /* Task for grace periods. */
303 struct swait_queue_head gp_wq
; /* Where GP task waits. */
304 short gp_flags
; /* Commands for GP task. */
305 short gp_state
; /* GP kthread sleep state. */
307 /* End of fields guarded by root rcu_node's lock. */
309 struct mutex barrier_mutex
; /* Guards barrier fields. */
310 atomic_t barrier_cpu_count
; /* # CPUs waiting on. */
311 struct completion barrier_completion
; /* Wake at barrier end. */
312 unsigned long barrier_sequence
; /* ++ at start and end of */
314 /* End of fields guarded by barrier_mutex. */
316 struct mutex exp_mutex
; /* Serialize expedited GP. */
317 struct mutex exp_wake_mutex
; /* Serialize wakeup. */
318 unsigned long expedited_sequence
; /* Take a ticket. */
319 atomic_t expedited_need_qs
; /* # CPUs left to check in. */
320 struct swait_queue_head expedited_wq
; /* Wait for check-ins. */
321 int ncpus_snap
; /* # CPUs seen last time. */
323 unsigned long jiffies_force_qs
; /* Time at which to invoke */
324 /* force_quiescent_state(). */
325 unsigned long jiffies_kick_kthreads
; /* Time at which to kick */
326 /* kthreads, if configured. */
327 unsigned long n_force_qs
; /* Number of calls to */
328 /* force_quiescent_state(). */
329 unsigned long gp_start
; /* Time at which GP started, */
330 /* but in jiffies. */
331 unsigned long gp_activity
; /* Time of last GP kthread */
332 /* activity in jiffies. */
333 unsigned long gp_req_activity
; /* Time of last GP request */
335 unsigned long jiffies_stall
; /* Time at which to check */
336 /* for CPU stalls. */
337 unsigned long jiffies_resched
; /* Time at which to resched */
338 /* a reluctant CPU. */
339 unsigned long n_force_qs_gpstart
; /* Snapshot of n_force_qs at */
341 unsigned long gp_max
; /* Maximum GP duration in */
343 const char *name
; /* Name of structure. */
344 char abbr
; /* Abbreviated name. */
346 raw_spinlock_t ofl_lock ____cacheline_internodealigned_in_smp
;
347 /* Synchronize offline with */
348 /* GP pre-initialization. */
351 /* Values for rcu_state structure's gp_flags field. */
352 #define RCU_GP_FLAG_INIT 0x1 /* Need grace-period initialization. */
353 #define RCU_GP_FLAG_FQS 0x2 /* Need grace-period quiescent-state forcing. */
355 /* Values for rcu_state structure's gp_state field. */
356 #define RCU_GP_IDLE 0 /* Initial state and no GP in progress. */
357 #define RCU_GP_WAIT_GPS 1 /* Wait for grace-period start. */
358 #define RCU_GP_DONE_GPS 2 /* Wait done for grace-period start. */
359 #define RCU_GP_ONOFF 3 /* Grace-period initialization hotplug. */
360 #define RCU_GP_INIT 4 /* Grace-period initialization. */
361 #define RCU_GP_WAIT_FQS 5 /* Wait for force-quiescent-state time. */
362 #define RCU_GP_DOING_FQS 6 /* Wait done for force-quiescent-state time. */
363 #define RCU_GP_CLEANUP 7 /* Grace-period cleanup started. */
364 #define RCU_GP_CLEANED 8 /* Grace-period cleanup complete. */
366 static const char * const gp_state_names
[] = {
379 * In order to export the rcu_state name to the tracing tools, it
380 * needs to be added in the __tracepoint_string section.
381 * This requires defining a separate variable tp_<sname>_varname
382 * that points to the string being used, and this will allow
383 * the tracing userspace tools to be able to decipher the string
384 * address to the matching string.
386 #ifdef CONFIG_PREEMPT_RCU
388 #define RCU_NAME_RAW "rcu_preempt"
389 #else /* #ifdef CONFIG_PREEMPT_RCU */
391 #define RCU_NAME_RAW "rcu_sched"
392 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
393 #ifndef CONFIG_TRACING
394 #define RCU_NAME RCU_NAME_RAW
395 #else /* #ifdef CONFIG_TRACING */
396 static char rcu_name
[] = RCU_NAME_RAW
;
397 static const char *tp_rcu_varname __used __tracepoint_string
= rcu_name
;
398 #define RCU_NAME rcu_name
399 #endif /* #else #ifdef CONFIG_TRACING */
402 * RCU implementation internal declarations:
404 extern struct rcu_state rcu_sched_state
;
406 extern struct rcu_state rcu_bh_state
;
408 #ifdef CONFIG_PREEMPT_RCU
409 extern struct rcu_state rcu_preempt_state
;
410 #endif /* #ifdef CONFIG_PREEMPT_RCU */
412 int rcu_dynticks_snap(struct rcu_data
*rdp
);
414 #ifdef CONFIG_RCU_BOOST
415 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_status
);
416 DECLARE_PER_CPU(int, rcu_cpu_kthread_cpu
);
417 DECLARE_PER_CPU(unsigned int, rcu_cpu_kthread_loops
);
418 DECLARE_PER_CPU(char, rcu_cpu_has_work
);
419 #endif /* #ifdef CONFIG_RCU_BOOST */
421 /* Forward declarations for rcutree_plugin.h */
422 static void rcu_bootup_announce(void);
423 static void rcu_qs(void);
424 static int rcu_preempt_blocked_readers_cgp(struct rcu_node
*rnp
);
425 #ifdef CONFIG_HOTPLUG_CPU
426 static bool rcu_preempt_has_tasks(struct rcu_node
*rnp
);
427 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
428 static void rcu_print_detail_task_stall(void);
429 static int rcu_print_task_stall(struct rcu_node
*rnp
);
430 static int rcu_print_task_exp_stall(struct rcu_node
*rnp
);
431 static void rcu_preempt_check_blocked_tasks(struct rcu_node
*rnp
);
432 static void rcu_flavor_check_callbacks(int user
);
433 void call_rcu(struct rcu_head
*head
, rcu_callback_t func
);
434 static void dump_blkd_tasks(struct rcu_node
*rnp
, int ncheck
);
435 static void rcu_initiate_boost(struct rcu_node
*rnp
, unsigned long flags
);
436 static void rcu_preempt_boost_start_gp(struct rcu_node
*rnp
);
437 static void invoke_rcu_callbacks_kthread(void);
438 static bool rcu_is_callbacks_kthread(void);
439 static void __init
rcu_spawn_boost_kthreads(void);
440 static void rcu_prepare_kthreads(int cpu
);
441 static void rcu_cleanup_after_idle(void);
442 static void rcu_prepare_for_idle(void);
443 static void rcu_idle_count_callbacks_posted(void);
444 static bool rcu_preempt_has_tasks(struct rcu_node
*rnp
);
445 static bool rcu_preempt_need_deferred_qs(struct task_struct
*t
);
446 static void rcu_preempt_deferred_qs(struct task_struct
*t
);
447 static void print_cpu_stall_info_begin(void);
448 static void print_cpu_stall_info(int cpu
);
449 static void print_cpu_stall_info_end(void);
450 static void zero_cpu_stall_ticks(struct rcu_data
*rdp
);
451 static bool rcu_nocb_cpu_needs_barrier(int cpu
);
452 static struct swait_queue_head
*rcu_nocb_gp_get(struct rcu_node
*rnp
);
453 static void rcu_nocb_gp_cleanup(struct swait_queue_head
*sq
);
454 static void rcu_init_one_nocb(struct rcu_node
*rnp
);
455 static bool __call_rcu_nocb(struct rcu_data
*rdp
, struct rcu_head
*rhp
,
456 bool lazy
, unsigned long flags
);
457 static bool rcu_nocb_adopt_orphan_cbs(struct rcu_data
*my_rdp
,
458 struct rcu_data
*rdp
,
459 unsigned long flags
);
460 static int rcu_nocb_need_deferred_wakeup(struct rcu_data
*rdp
);
461 static void do_nocb_deferred_wakeup(struct rcu_data
*rdp
);
462 static void rcu_boot_init_nocb_percpu_data(struct rcu_data
*rdp
);
463 static void rcu_spawn_all_nocb_kthreads(int cpu
);
464 static void __init
rcu_spawn_nocb_kthreads(void);
465 #ifdef CONFIG_RCU_NOCB_CPU
466 static void __init
rcu_organize_nocb_kthreads(void);
467 #endif /* #ifdef CONFIG_RCU_NOCB_CPU */
468 static bool init_nocb_callback_list(struct rcu_data
*rdp
);
469 static void rcu_bind_gp_kthread(void);
470 static bool rcu_nohz_full_cpu(void);
471 static void rcu_dynticks_task_enter(void);
472 static void rcu_dynticks_task_exit(void);
475 void srcu_online_cpu(unsigned int cpu
);
476 void srcu_offline_cpu(unsigned int cpu
);
477 #else /* #ifdef CONFIG_SRCU */
478 void srcu_online_cpu(unsigned int cpu
) { }
479 void srcu_offline_cpu(unsigned int cpu
) { }
480 #endif /* #else #ifdef CONFIG_SRCU */