Merge tag 'docs-4.12-2' of git://git.lwn.net/linux
[sfrench/cifs-2.6.git] / kernel / rcu / rcu.h
1 /*
2  * Read-Copy Update definitions shared among RCU implementations.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, you can access it online at
16  * http://www.gnu.org/licenses/gpl-2.0.html.
17  *
18  * Copyright IBM Corporation, 2011
19  *
20  * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
21  */
22
23 #ifndef __LINUX_RCU_H
24 #define __LINUX_RCU_H
25
26 #include <trace/events/rcu.h>
27 #ifdef CONFIG_RCU_TRACE
28 #define RCU_TRACE(stmt) stmt
29 #else /* #ifdef CONFIG_RCU_TRACE */
30 #define RCU_TRACE(stmt)
31 #endif /* #else #ifdef CONFIG_RCU_TRACE */
32
33 /*
34  * Process-level increment to ->dynticks_nesting field.  This allows for
35  * architectures that use half-interrupts and half-exceptions from
36  * process context.
37  *
38  * DYNTICK_TASK_NEST_MASK defines a field of width DYNTICK_TASK_NEST_WIDTH
39  * that counts the number of process-based reasons why RCU cannot
40  * consider the corresponding CPU to be idle, and DYNTICK_TASK_NEST_VALUE
41  * is the value used to increment or decrement this field.
42  *
43  * The rest of the bits could in principle be used to count interrupts,
44  * but this would mean that a negative-one value in the interrupt
45  * field could incorrectly zero out the DYNTICK_TASK_NEST_MASK field.
46  * We therefore provide a two-bit guard field defined by DYNTICK_TASK_MASK
47  * that is set to DYNTICK_TASK_FLAG upon initial exit from idle.
48  * The DYNTICK_TASK_EXIT_IDLE value is thus the combined value used upon
49  * initial exit from idle.
50  */
51 #define DYNTICK_TASK_NEST_WIDTH 7
52 #define DYNTICK_TASK_NEST_VALUE ((LLONG_MAX >> DYNTICK_TASK_NEST_WIDTH) + 1)
53 #define DYNTICK_TASK_NEST_MASK  (LLONG_MAX - DYNTICK_TASK_NEST_VALUE + 1)
54 #define DYNTICK_TASK_FLAG          ((DYNTICK_TASK_NEST_VALUE / 8) * 2)
55 #define DYNTICK_TASK_MASK          ((DYNTICK_TASK_NEST_VALUE / 8) * 3)
56 #define DYNTICK_TASK_EXIT_IDLE     (DYNTICK_TASK_NEST_VALUE + \
57                                     DYNTICK_TASK_FLAG)
58
59
60 /*
61  * Grace-period counter management.
62  */
63
64 #define RCU_SEQ_CTR_SHIFT       2
65 #define RCU_SEQ_STATE_MASK      ((1 << RCU_SEQ_CTR_SHIFT) - 1)
66
67 /*
68  * Return the counter portion of a sequence number previously returned
69  * by rcu_seq_snap() or rcu_seq_current().
70  */
71 static inline unsigned long rcu_seq_ctr(unsigned long s)
72 {
73         return s >> RCU_SEQ_CTR_SHIFT;
74 }
75
76 /*
77  * Return the state portion of a sequence number previously returned
78  * by rcu_seq_snap() or rcu_seq_current().
79  */
80 static inline int rcu_seq_state(unsigned long s)
81 {
82         return s & RCU_SEQ_STATE_MASK;
83 }
84
85 /*
86  * Set the state portion of the pointed-to sequence number.
87  * The caller is responsible for preventing conflicting updates.
88  */
89 static inline void rcu_seq_set_state(unsigned long *sp, int newstate)
90 {
91         WARN_ON_ONCE(newstate & ~RCU_SEQ_STATE_MASK);
92         WRITE_ONCE(*sp, (*sp & ~RCU_SEQ_STATE_MASK) + newstate);
93 }
94
95 /* Adjust sequence number for start of update-side operation. */
96 static inline void rcu_seq_start(unsigned long *sp)
97 {
98         WRITE_ONCE(*sp, *sp + 1);
99         smp_mb(); /* Ensure update-side operation after counter increment. */
100         WARN_ON_ONCE(rcu_seq_state(*sp) != 1);
101 }
102
103 /* Adjust sequence number for end of update-side operation. */
104 static inline void rcu_seq_end(unsigned long *sp)
105 {
106         smp_mb(); /* Ensure update-side operation before counter increment. */
107         WARN_ON_ONCE(!rcu_seq_state(*sp));
108         WRITE_ONCE(*sp, (*sp | RCU_SEQ_STATE_MASK) + 1);
109 }
110
111 /* Take a snapshot of the update side's sequence number. */
112 static inline unsigned long rcu_seq_snap(unsigned long *sp)
113 {
114         unsigned long s;
115
116         s = (READ_ONCE(*sp) + 2 * RCU_SEQ_STATE_MASK + 1) & ~RCU_SEQ_STATE_MASK;
117         smp_mb(); /* Above access must not bleed into critical section. */
118         return s;
119 }
120
121 /* Return the current value the update side's sequence number, no ordering. */
122 static inline unsigned long rcu_seq_current(unsigned long *sp)
123 {
124         return READ_ONCE(*sp);
125 }
126
127 /*
128  * Given a snapshot from rcu_seq_snap(), determine whether or not a
129  * full update-side operation has occurred.
130  */
131 static inline bool rcu_seq_done(unsigned long *sp, unsigned long s)
132 {
133         return ULONG_CMP_GE(READ_ONCE(*sp), s);
134 }
135
136 /*
137  * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally
138  * by call_rcu() and rcu callback execution, and are therefore not part of the
139  * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors.
140  */
141
142 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
143 # define STATE_RCU_HEAD_READY   0
144 # define STATE_RCU_HEAD_QUEUED  1
145
146 extern struct debug_obj_descr rcuhead_debug_descr;
147
148 static inline int debug_rcu_head_queue(struct rcu_head *head)
149 {
150         int r1;
151
152         r1 = debug_object_activate(head, &rcuhead_debug_descr);
153         debug_object_active_state(head, &rcuhead_debug_descr,
154                                   STATE_RCU_HEAD_READY,
155                                   STATE_RCU_HEAD_QUEUED);
156         return r1;
157 }
158
159 static inline void debug_rcu_head_unqueue(struct rcu_head *head)
160 {
161         debug_object_active_state(head, &rcuhead_debug_descr,
162                                   STATE_RCU_HEAD_QUEUED,
163                                   STATE_RCU_HEAD_READY);
164         debug_object_deactivate(head, &rcuhead_debug_descr);
165 }
166 #else   /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
167 static inline int debug_rcu_head_queue(struct rcu_head *head)
168 {
169         return 0;
170 }
171
172 static inline void debug_rcu_head_unqueue(struct rcu_head *head)
173 {
174 }
175 #endif  /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
176
177 void kfree(const void *);
178
179 /*
180  * Reclaim the specified callback, either by invoking it (non-lazy case)
181  * or freeing it directly (lazy case).  Return true if lazy, false otherwise.
182  */
183 static inline bool __rcu_reclaim(const char *rn, struct rcu_head *head)
184 {
185         unsigned long offset = (unsigned long)head->func;
186
187         rcu_lock_acquire(&rcu_callback_map);
188         if (__is_kfree_rcu_offset(offset)) {
189                 RCU_TRACE(trace_rcu_invoke_kfree_callback(rn, head, offset);)
190                 kfree((void *)head - offset);
191                 rcu_lock_release(&rcu_callback_map);
192                 return true;
193         } else {
194                 RCU_TRACE(trace_rcu_invoke_callback(rn, head);)
195                 head->func(head);
196                 rcu_lock_release(&rcu_callback_map);
197                 return false;
198         }
199 }
200
201 #ifdef CONFIG_RCU_STALL_COMMON
202
203 extern int rcu_cpu_stall_suppress;
204 int rcu_jiffies_till_stall_check(void);
205
206 #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
207
208 /*
209  * Strings used in tracepoints need to be exported via the
210  * tracing system such that tools like perf and trace-cmd can
211  * translate the string address pointers to actual text.
212  */
213 #define TPS(x)  tracepoint_string(x)
214
215 void rcu_early_boot_tests(void);
216 void rcu_test_sync_prims(void);
217
218 /*
219  * This function really isn't for public consumption, but RCU is special in
220  * that context switches can allow the state machine to make progress.
221  */
222 extern void resched_cpu(int cpu);
223
224 #if defined(SRCU) || !defined(TINY_RCU)
225
226 #include <linux/rcu_node_tree.h>
227
228 extern int rcu_num_lvls;
229 extern int num_rcu_lvl[];
230 extern int rcu_num_nodes;
231 static bool rcu_fanout_exact;
232 static int rcu_fanout_leaf;
233
234 /*
235  * Compute the per-level fanout, either using the exact fanout specified
236  * or balancing the tree, depending on the rcu_fanout_exact boot parameter.
237  */
238 static inline void rcu_init_levelspread(int *levelspread, const int *levelcnt)
239 {
240         int i;
241
242         if (rcu_fanout_exact) {
243                 levelspread[rcu_num_lvls - 1] = rcu_fanout_leaf;
244                 for (i = rcu_num_lvls - 2; i >= 0; i--)
245                         levelspread[i] = RCU_FANOUT;
246         } else {
247                 int ccur;
248                 int cprv;
249
250                 cprv = nr_cpu_ids;
251                 for (i = rcu_num_lvls - 1; i >= 0; i--) {
252                         ccur = levelcnt[i];
253                         levelspread[i] = (cprv + ccur - 1) / ccur;
254                         cprv = ccur;
255                 }
256         }
257 }
258
259 /*
260  * Do a full breadth-first scan of the rcu_node structures for the
261  * specified rcu_state structure.
262  */
263 #define rcu_for_each_node_breadth_first(rsp, rnp) \
264         for ((rnp) = &(rsp)->node[0]; \
265              (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
266
267 /*
268  * Do a breadth-first scan of the non-leaf rcu_node structures for the
269  * specified rcu_state structure.  Note that if there is a singleton
270  * rcu_node tree with but one rcu_node structure, this loop is a no-op.
271  */
272 #define rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) \
273         for ((rnp) = &(rsp)->node[0]; \
274              (rnp) < (rsp)->level[rcu_num_lvls - 1]; (rnp)++)
275
276 /*
277  * Scan the leaves of the rcu_node hierarchy for the specified rcu_state
278  * structure.  Note that if there is a singleton rcu_node tree with but
279  * one rcu_node structure, this loop -will- visit the rcu_node structure.
280  * It is still a leaf node, even if it is also the root node.
281  */
282 #define rcu_for_each_leaf_node(rsp, rnp) \
283         for ((rnp) = (rsp)->level[rcu_num_lvls - 1]; \
284              (rnp) < &(rsp)->node[rcu_num_nodes]; (rnp)++)
285
286 /*
287  * Iterate over all possible CPUs in a leaf RCU node.
288  */
289 #define for_each_leaf_node_possible_cpu(rnp, cpu) \
290         for ((cpu) = cpumask_next(rnp->grplo - 1, cpu_possible_mask); \
291              cpu <= rnp->grphi; \
292              cpu = cpumask_next((cpu), cpu_possible_mask))
293
294 #endif /* #if defined(SRCU) || !defined(TINY_RCU) */
295
296 #endif /* __LINUX_RCU_H */