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_reader_flavor; /* Reader flavor for 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 /* Values for ->srcu_reader_flavor. */ 47 #define SRCU_READ_FLAVOR_NORMAL 0x1 // srcu_read_lock(). 48 #define SRCU_READ_FLAVOR_NMI 0x2 // srcu_read_lock_nmisafe(). 49 50 /* 51 * Node in SRCU combining tree, similar in function to rcu_data. 52 */ 53 struct srcu_node { 54 spinlock_t __private lock; 55 unsigned long srcu_have_cbs[4]; /* GP seq for children having CBs, but only */ 56 /* if greater than ->srcu_gp_seq. */ 57 unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs have CBs for given GP? */ 58 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */ 59 struct srcu_node *srcu_parent; /* Next up in tree. */ 60 int grplo; /* Least CPU for node. */ 61 int grphi; /* Biggest CPU for node. */ 62 }; 63 64 /* 65 * Per-SRCU-domain structure, update-side data linked from srcu_struct. 66 */ 67 struct srcu_usage { 68 struct srcu_node *node; /* Combining tree. */ 69 struct srcu_node *level[RCU_NUM_LVLS + 1]; 70 /* First node at each level. */ 71 int srcu_size_state; /* Small-to-big transition state. */ 72 struct mutex srcu_cb_mutex; /* Serialize CB preparation. */ 73 spinlock_t __private lock; /* Protect counters and size state. */ 74 struct mutex srcu_gp_mutex; /* Serialize GP work. */ 75 unsigned long srcu_gp_seq; /* Grace-period seq #. */ 76 unsigned long srcu_gp_seq_needed; /* Latest gp_seq needed. */ 77 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */ 78 unsigned long srcu_gp_start; /* Last GP start timestamp (jiffies) */ 79 unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */ 80 unsigned long srcu_size_jiffies; /* Current contention-measurement interval. */ 81 unsigned long srcu_n_lock_retries; /* Contention events in current interval. */ 82 unsigned long srcu_n_exp_nodelay; /* # expedited no-delays in current GP phase. */ 83 bool sda_is_static; /* May ->sda be passed to free_percpu()? */ 84 unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */ 85 struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */ 86 struct completion srcu_barrier_completion; 87 /* Awaken barrier rq at end. */ 88 atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */ 89 /* callback for the barrier */ 90 /* operation. */ 91 unsigned long reschedule_jiffies; 92 unsigned long reschedule_count; 93 struct delayed_work work; 94 struct srcu_struct *srcu_ssp; 95 }; 96 97 /* 98 * Per-SRCU-domain structure, similar in function to rcu_state. 99 */ 100 struct srcu_struct { 101 unsigned int srcu_idx; /* Current rdr array element. */ 102 struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */ 103 struct lockdep_map dep_map; 104 struct srcu_usage *srcu_sup; /* Update-side data. */ 105 }; 106 107 // Values for size state variable (->srcu_size_state). Once the state 108 // has been set to SRCU_SIZE_ALLOC, the grace-period code advances through 109 // this state machine one step per grace period until the SRCU_SIZE_BIG state 110 // is reached. Otherwise, the state machine remains in the SRCU_SIZE_SMALL 111 // state indefinitely. 112 #define SRCU_SIZE_SMALL 0 // No srcu_node combining tree, ->node == NULL 113 #define SRCU_SIZE_ALLOC 1 // An srcu_node tree is being allocated, initialized, 114 // and then referenced by ->node. It will not be used. 115 #define SRCU_SIZE_WAIT_BARRIER 2 // The srcu_node tree starts being used by everything 116 // except call_srcu(), especially by srcu_barrier(). 117 // By the end of this state, all CPUs and threads 118 // are aware of this tree's existence. 119 #define SRCU_SIZE_WAIT_CALL 3 // The srcu_node tree starts being used by call_srcu(). 120 // By the end of this state, all of the call_srcu() 121 // invocations that were running on a non-boot CPU 122 // and using the boot CPU's callback queue will have 123 // completed. 124 #define SRCU_SIZE_WAIT_CBS1 4 // Don't trust the ->srcu_have_cbs[] grace-period 125 #define SRCU_SIZE_WAIT_CBS2 5 // sequence elements or the ->srcu_data_have_cbs[] 126 #define SRCU_SIZE_WAIT_CBS3 6 // CPU-bitmask elements until all four elements of 127 #define SRCU_SIZE_WAIT_CBS4 7 // each array have been initialized. 128 #define SRCU_SIZE_BIG 8 // The srcu_node combining tree is fully initialized 129 // and all aspects of it are being put to use. 130 131 /* Values for state variable (bottom bits of ->srcu_gp_seq). */ 132 #define SRCU_STATE_IDLE 0 133 #define SRCU_STATE_SCAN1 1 134 #define SRCU_STATE_SCAN2 2 135 136 /* 137 * Values for initializing gp sequence fields. Higher values allow wrap arounds to 138 * occur earlier. 139 * The second value with state is useful in the case of static initialization of 140 * srcu_usage where srcu_gp_seq_needed is expected to have some state value in its 141 * lower bits (or else it will appear to be already initialized within 142 * the call check_init_srcu_struct()). 143 */ 144 #define SRCU_GP_SEQ_INITIAL_VAL ((0UL - 100UL) << RCU_SEQ_CTR_SHIFT) 145 #define SRCU_GP_SEQ_INITIAL_VAL_WITH_STATE (SRCU_GP_SEQ_INITIAL_VAL - 1) 146 147 #define __SRCU_USAGE_INIT(name) \ 148 { \ 149 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ 150 .srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL, \ 151 .srcu_gp_seq_needed = SRCU_GP_SEQ_INITIAL_VAL_WITH_STATE, \ 152 .srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL, \ 153 .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \ 154 } 155 156 #define __SRCU_STRUCT_INIT_COMMON(name, usage_name) \ 157 .srcu_sup = &usage_name, \ 158 __SRCU_DEP_MAP_INIT(name) 159 160 #define __SRCU_STRUCT_INIT_MODULE(name, usage_name) \ 161 { \ 162 __SRCU_STRUCT_INIT_COMMON(name, usage_name) \ 163 } 164 165 #define __SRCU_STRUCT_INIT(name, usage_name, pcpu_name) \ 166 { \ 167 .sda = &pcpu_name, \ 168 __SRCU_STRUCT_INIT_COMMON(name, usage_name) \ 169 } 170 171 /* 172 * Define and initialize a srcu struct at build time. 173 * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it. 174 * 175 * Note that although DEFINE_STATIC_SRCU() hides the name from other 176 * files, the per-CPU variable rules nevertheless require that the 177 * chosen name be globally unique. These rules also prohibit use of 178 * DEFINE_STATIC_SRCU() within a function. If these rules are too 179 * restrictive, declare the srcu_struct manually. For example, in 180 * each file: 181 * 182 * static struct srcu_struct my_srcu; 183 * 184 * Then, before the first use of each my_srcu, manually initialize it: 185 * 186 * init_srcu_struct(&my_srcu); 187 * 188 * See include/linux/percpu-defs.h for the rules on per-CPU variables. 189 */ 190 #ifdef MODULE 191 # define __DEFINE_SRCU(name, is_static) \ 192 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \ 193 is_static struct srcu_struct name = __SRCU_STRUCT_INIT_MODULE(name, name##_srcu_usage); \ 194 extern struct srcu_struct * const __srcu_struct_##name; \ 195 struct srcu_struct * const __srcu_struct_##name \ 196 __section("___srcu_struct_ptrs") = &name 197 #else 198 # define __DEFINE_SRCU(name, is_static) \ 199 static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \ 200 static struct srcu_usage name##_srcu_usage = __SRCU_USAGE_INIT(name##_srcu_usage); \ 201 is_static struct srcu_struct name = \ 202 __SRCU_STRUCT_INIT(name, name##_srcu_usage, name##_srcu_data) 203 #endif 204 #define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */) 205 #define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static) 206 207 void synchronize_srcu_expedited(struct srcu_struct *ssp); 208 void srcu_barrier(struct srcu_struct *ssp); 209 void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf); 210 211 #endif 212