xref: /linux-6.15/include/linux/srcutree.h (revision b3fb11f7)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Sleepable Read-Copy Update mechanism for mutual exclusion,
4  *	tree variant.
5  *
6  * Copyright (C) IBM Corporation, 2017
7  *
8  * Author: Paul McKenney <[email protected]>
9  */
10 
11 #ifndef _LINUX_SRCU_TREE_H
12 #define _LINUX_SRCU_TREE_H
13 
14 #include <linux/rcu_node_tree.h>
15 #include <linux/completion.h>
16 
17 struct srcu_node;
18 struct srcu_struct;
19 
20 /*
21  * Per-CPU structure feeding into leaf srcu_node, similar in function
22  * to rcu_node.
23  */
24 struct srcu_data {
25 	/* Read-side state. */
26 	atomic_long_t srcu_lock_count[2];	/* Locks per CPU. */
27 	atomic_long_t srcu_unlock_count[2];	/* Unlocks per CPU. */
28 	int srcu_nmi_safety;			/* NMI-safe srcu_struct structure? */
29 
30 	/* Update-side state. */
31 	spinlock_t __private lock ____cacheline_internodealigned_in_smp;
32 	struct rcu_segcblist srcu_cblist;	/* List of callbacks.*/
33 	unsigned long srcu_gp_seq_needed;	/* Furthest future GP needed. */
34 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
35 	bool srcu_cblist_invoking;		/* Invoking these CBs? */
36 	struct timer_list delay_work;		/* Delay for CB invoking */
37 	struct work_struct work;		/* Context for CB invoking. */
38 	struct rcu_head srcu_barrier_head;	/* For srcu_barrier() use. */
39 	struct srcu_node *mynode;		/* Leaf srcu_node. */
40 	unsigned long grpmask;			/* Mask for leaf srcu_node */
41 						/*  ->srcu_data_have_cbs[]. */
42 	int cpu;
43 	struct srcu_struct *ssp;
44 };
45 
46 /*
47  * Node in SRCU combining tree, similar in function to rcu_data.
48  */
49 struct srcu_node {
50 	spinlock_t __private lock;
51 	unsigned long srcu_have_cbs[4];		/* GP seq for children having CBs, but only */
52 						/*  if greater than ->srcu_gp_seq. */
53 	unsigned long srcu_data_have_cbs[4];	/* Which srcu_data structs have CBs for given GP? */
54 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
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 /*
61  * Per-SRCU-domain structure, update-side data linked from srcu_struct.
62  */
63 struct srcu_usage {
64 	struct srcu_node *node;			/* Combining tree. */
65 	struct srcu_node *level[RCU_NUM_LVLS + 1];
66 						/* First node at each level. */
67 	int srcu_size_state;			/* Small-to-big transition state. */
68 	struct mutex srcu_cb_mutex;		/* Serialize CB preparation. */
69 	spinlock_t __private lock;		/* Protect counters and size state. */
70 };
71 
72 /*
73  * Per-SRCU-domain structure, similar in function to rcu_state.
74  */
75 struct srcu_struct {
76 	struct mutex srcu_gp_mutex;		/* Serialize GP work. */
77 	unsigned int srcu_idx;			/* Current rdr array element. */
78 	unsigned long srcu_gp_seq;		/* Grace-period seq #. */
79 	unsigned long srcu_gp_seq_needed;	/* Latest gp_seq needed. */
80 	unsigned long srcu_gp_seq_needed_exp;	/* Furthest future exp GP. */
81 	unsigned long srcu_gp_start;		/* Last GP start timestamp (jiffies) */
82 	unsigned long srcu_last_gp_end;		/* Last GP end timestamp (ns) */
83 	unsigned long srcu_size_jiffies;	/* Current contention-measurement interval. */
84 	unsigned long srcu_n_lock_retries;	/* Contention events in current interval. */
85 	unsigned long srcu_n_exp_nodelay;	/* # expedited no-delays in current GP phase. */
86 	struct srcu_data __percpu *sda;		/* Per-CPU srcu_data array. */
87 	bool sda_is_static;			/* May ->sda be passed to free_percpu()? */
88 	unsigned long srcu_barrier_seq;		/* srcu_barrier seq #. */
89 	struct mutex srcu_barrier_mutex;	/* Serialize barrier ops. */
90 	struct completion srcu_barrier_completion;
91 						/* Awaken barrier rq at end. */
92 	atomic_t srcu_barrier_cpu_cnt;		/* # CPUs not yet posting a */
93 						/*  callback for the barrier */
94 						/*  operation. */
95 	unsigned long reschedule_jiffies;
96 	unsigned long reschedule_count;
97 	struct delayed_work work;
98 	struct lockdep_map dep_map;
99 	struct srcu_usage *srcu_sup;		/* Update-side data. */
100 };
101 
102 /* Values for size state variable (->srcu_size_state). */
103 #define SRCU_SIZE_SMALL		0
104 #define SRCU_SIZE_ALLOC		1
105 #define SRCU_SIZE_WAIT_BARRIER	2
106 #define SRCU_SIZE_WAIT_CALL	3
107 #define SRCU_SIZE_WAIT_CBS1	4
108 #define SRCU_SIZE_WAIT_CBS2	5
109 #define SRCU_SIZE_WAIT_CBS3	6
110 #define SRCU_SIZE_WAIT_CBS4	7
111 #define SRCU_SIZE_BIG		8
112 
113 /* Values for state variable (bottom bits of ->srcu_gp_seq). */
114 #define SRCU_STATE_IDLE		0
115 #define SRCU_STATE_SCAN1	1
116 #define SRCU_STATE_SCAN2	2
117 
118 #define __SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
119 	.srcu_gp_seq_needed = -1UL,								\
120 	.work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0),					\
121 	.srcu_sup = &usage_name,								\
122 	__SRCU_DEP_MAP_INIT(name)
123 
124 #define __SRCU_STRUCT_INIT_MODULE(name, usage_name)						\
125 {												\
126 	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
127 }
128 
129 #define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name)						\
130 {												\
131 	.sda = &pcpu_name,									\
132 	__SRCU_STRUCT_INIT_COMMON(name, usage_name)						\
133 }
134 
135 /*
136  * Define and initialize a srcu struct at build time.
137  * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
138  *
139  * Note that although DEFINE_STATIC_SRCU() hides the name from other
140  * files, the per-CPU variable rules nevertheless require that the
141  * chosen name be globally unique.  These rules also prohibit use of
142  * DEFINE_STATIC_SRCU() within a function.  If these rules are too
143  * restrictive, declare the srcu_struct manually.  For example, in
144  * each file:
145  *
146  *	static struct srcu_struct my_srcu;
147  *
148  * Then, before the first use of each my_srcu, manually initialize it:
149  *
150  *	init_srcu_struct(&my_srcu);
151  *
152  * See include/linux/percpu-defs.h for the rules on per-CPU variables.
153  */
154 #ifdef MODULE
155 # define __DEFINE_SRCU(name, is_static)								\
156 	static struct srcu_usage name##_srcu_usage = {						\
157 		.lock = __SPIN_LOCK_UNLOCKED(name.lock),					\
158 	};											\
159 	is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage);	\
160 	extern struct srcu_struct * const __srcu_struct_##name;					\
161 	struct srcu_struct * const __srcu_struct_##name						\
162 		__section("___srcu_struct_ptrs") = &name
163 #else
164 # define __DEFINE_SRCU(name, is_static)								\
165 	static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);				\
166 	static struct srcu_usage name##_srcu_usage = {						\
167 		.lock = __SPIN_LOCK_UNLOCKED(name.lock),					\
168 	};											\
169 	is_static struct srcu_struct name =							\
170 		__SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data)
171 #endif
172 #define DEFINE_SRCU(name)		__DEFINE_SRCU(name, /* not static */)
173 #define DEFINE_STATIC_SRCU(name)	__DEFINE_SRCU(name, static)
174 
175 void synchronize_srcu_expedited(struct srcu_struct *ssp);
176 void srcu_barrier(struct srcu_struct *ssp);
177 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
178 
179 #endif
180