Merge patch series "Use block pr_ops in LIO"
[linux-2.6-block.git] / include / linux / srcutree.h
CommitLineData
8c366db0 1/* SPDX-License-Identifier: GPL-2.0+ */
d8be8173
PM
2/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion,
4 * tree variant.
5 *
d8be8173
PM
6 * Copyright (C) IBM Corporation, 2017
7 *
8c366db0 8 * Author: Paul McKenney <paulmck@linux.ibm.com>
d8be8173
PM
9 */
10
11#ifndef _LINUX_SRCU_TREE_H
12#define _LINUX_SRCU_TREE_H
13
da915ad5
PM
14#include <linux/rcu_node_tree.h>
15#include <linux/completion.h>
16
17struct srcu_node;
18struct srcu_struct;
19
20/*
21 * Per-CPU structure feeding into leaf srcu_node, similar in function
22 * to rcu_node.
23 */
24struct srcu_data {
25 /* Read-side state. */
5d0f5953
PM
26 atomic_long_t srcu_lock_count[2]; /* Locks per CPU. */
27 atomic_long_t srcu_unlock_count[2]; /* Unlocks per CPU. */
27120e7d 28 int srcu_nmi_safety; /* NMI-safe srcu_struct structure? */
da915ad5
PM
29
30 /* Update-side state. */
d6331980 31 spinlock_t __private lock ____cacheline_internodealigned_in_smp;
da915ad5
PM
32 struct rcu_segcblist srcu_cblist; /* List of callbacks.*/
33 unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */
1e9a038b 34 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
da915ad5 35 bool srcu_cblist_invoking; /* Invoking these CBs? */
e81baf4c
SAS
36 struct timer_list delay_work; /* Delay for CB invoking */
37 struct work_struct work; /* Context for CB invoking. */
da915ad5
PM
38 struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */
39 struct srcu_node *mynode; /* Leaf srcu_node. */
c7e88067
PM
40 unsigned long grpmask; /* Mask for leaf srcu_node */
41 /* ->srcu_data_have_cbs[]. */
da915ad5 42 int cpu;
aacb5d91 43 struct srcu_struct *ssp;
d8be8173
PM
44};
45
da915ad5
PM
46/*
47 * Node in SRCU combining tree, similar in function to rcu_data.
48 */
49struct srcu_node {
d6331980 50 spinlock_t __private lock;
95ebe80d 51 unsigned long srcu_have_cbs[4]; /* GP seq for children having CBs, but only */
aa5210f5 52 /* if greater than ->srcu_gp_seq. */
95ebe80d 53 unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs have CBs for given GP? */
1e9a038b 54 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
da915ad5
PM
55 struct srcu_node *srcu_parent; /* Next up in tree. */
56 int grplo; /* Least CPU for node. */
57 int grphi; /* Biggest CPU for node. */
58};
59
60/*
95433f72 61 * Per-SRCU-domain structure, update-side data linked from srcu_struct.
da915ad5 62 */
95433f72 63struct srcu_usage {
2ec30311 64 struct srcu_node *node; /* Combining tree. */
da915ad5
PM
65 struct srcu_node *level[RCU_NUM_LVLS + 1];
66 /* First node at each level. */
994f7068 67 int srcu_size_state; /* Small-to-big transition state. */
da915ad5 68 struct mutex srcu_cb_mutex; /* Serialize CB preparation. */
994f7068 69 spinlock_t __private lock; /* Protect counters and size state. */
da915ad5 70 struct mutex srcu_gp_mutex; /* Serialize GP work. */
da915ad5
PM
71 unsigned long srcu_gp_seq; /* Grace-period seq #. */
72 unsigned long srcu_gp_seq_needed; /* Latest gp_seq needed. */
1e9a038b 73 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
282d8998 74 unsigned long srcu_gp_start; /* Last GP start timestamp (jiffies) */
22607d66 75 unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */
9f2e91d9
PM
76 unsigned long srcu_size_jiffies; /* Current contention-measurement interval. */
77 unsigned long srcu_n_lock_retries; /* Contention events in current interval. */
282d8998 78 unsigned long srcu_n_exp_nodelay; /* # expedited no-delays in current GP phase. */
46470cf8 79 bool sda_is_static; /* May ->sda be passed to free_percpu()? */
da915ad5
PM
80 unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */
81 struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */
82 struct completion srcu_barrier_completion;
83 /* Awaken barrier rq at end. */
84 atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */
85 /* callback for the barrier */
86 /* operation. */
282d8998
PM
87 unsigned long reschedule_jiffies;
88 unsigned long reschedule_count;
d8be8173 89 struct delayed_work work;
fd1b3f8e 90 struct srcu_struct *srcu_ssp;
95433f72
PM
91};
92
da915ad5
PM
93/*
94 * Per-SRCU-domain structure, similar in function to rcu_state.
95 */
d8be8173 96struct srcu_struct {
da915ad5 97 unsigned int srcu_idx; /* Current rdr array element. */
da915ad5 98 struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
d8be8173 99 struct lockdep_map dep_map;
95433f72 100 struct srcu_usage *srcu_sup; /* Update-side data. */
d8be8173
PM
101};
102
e15a1930
PL
103// Values for size state variable (->srcu_size_state). Once the state
104// has been set to SRCU_SIZE_ALLOC, the grace-period code advances through
105// this state machine one step per grace period until the SRCU_SIZE_BIG state
106// is reached. Otherwise, the state machine remains in the SRCU_SIZE_SMALL
107// state indefinitely.
108#define SRCU_SIZE_SMALL 0 // No srcu_node combining tree, ->node == NULL
109#define SRCU_SIZE_ALLOC 1 // An srcu_node tree is being allocated, initialized,
110 // and then referenced by ->node. It will not be used.
111#define SRCU_SIZE_WAIT_BARRIER 2 // The srcu_node tree starts being used by everything
112 // except call_srcu(), especially by srcu_barrier().
113 // By the end of this state, all CPUs and threads
114 // are aware of this tree's existence.
115#define SRCU_SIZE_WAIT_CALL 3 // The srcu_node tree starts being used by call_srcu().
116 // By the end of this state, all of the call_srcu()
117 // invocations that were running on a non-boot CPU
118 // and using the boot CPU's callback queue will have
119 // completed.
120#define SRCU_SIZE_WAIT_CBS1 4 // Don't trust the ->srcu_have_cbs[] grace-period
121#define SRCU_SIZE_WAIT_CBS2 5 // sequence elements or the ->srcu_data_have_cbs[]
122#define SRCU_SIZE_WAIT_CBS3 6 // CPU-bitmask elements until all four elements of
123#define SRCU_SIZE_WAIT_CBS4 7 // each array have been initialized.
124#define SRCU_SIZE_BIG 8 // The srcu_node combining tree is fully initialized
125 // and all aspects of it are being put to use.
994f7068 126
da915ad5 127/* Values for state variable (bottom bits of ->srcu_gp_seq). */
d8be8173
PM
128#define SRCU_STATE_IDLE 0
129#define SRCU_STATE_SCAN1 1
130#define SRCU_STATE_SCAN2 2
131
03200b5c
PM
132#define __SRCU_USAGE_INIT(name) \
133{ \
134 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
3e67cb8a 135 .srcu_gp_seq_needed = -1UL, \
fd1b3f8e 136 .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \
03200b5c
PM
137}
138
139#define __SRCU_STRUCT_INIT_COMMON(name, usage_name) \
95433f72 140 .srcu_sup = &usage_name, \
f4d01a25
PM
141 __SRCU_DEP_MAP_INIT(name)
142
95433f72 143#define __SRCU_STRUCT_INIT_MODULE(name, usage_name) \
f4d01a25 144{ \
95433f72 145 __SRCU_STRUCT_INIT_COMMON(name, usage_name) \
e0fcba9a 146}
d8be8173 147
95433f72 148#define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name) \
f4d01a25
PM
149{ \
150 .sda = &pcpu_name, \
95433f72 151 __SRCU_STRUCT_INIT_COMMON(name, usage_name) \
e0fcba9a 152}
d8be8173
PM
153
154/*
155 * Define and initialize a srcu struct at build time.
156 * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
157 *
158 * Note that although DEFINE_STATIC_SRCU() hides the name from other
159 * files, the per-CPU variable rules nevertheless require that the
160 * chosen name be globally unique. These rules also prohibit use of
161 * DEFINE_STATIC_SRCU() within a function. If these rules are too
162 * restrictive, declare the srcu_struct manually. For example, in
163 * each file:
164 *
165 * static struct srcu_struct my_srcu;
166 *
167 * Then, before the first use of each my_srcu, manually initialize it:
168 *
169 * init_srcu_struct(&my_srcu);
170 *
171 * See include/linux/percpu-defs.h for the rules on per-CPU variables.
172 */
fe15b50c 173#ifdef MODULE
3e67cb8a 174# define __DEFINE_SRCU(name, is_static) \
03200b5c 175 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \
95433f72 176 is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage); \
3e67cb8a
PM
177 extern struct srcu_struct * const __srcu_struct_##name; \
178 struct srcu_struct * const __srcu_struct_##name \
fe15b50c
PM
179 __section("___srcu_struct_ptrs") = &name
180#else
3e67cb8a
PM
181# define __DEFINE_SRCU(name, is_static) \
182 static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \
03200b5c 183 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \
3e67cb8a 184 is_static struct srcu_struct name = \
95433f72 185 __SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data)
fe15b50c 186#endif
d8be8173
PM
187#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
188#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
189
aacb5d91
PM
190void synchronize_srcu_expedited(struct srcu_struct *ssp);
191void srcu_barrier(struct srcu_struct *ssp);
192void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
d8be8173
PM
193
194#endif