Merge tag 'kvm-s390-next-5.19-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-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. */
26 unsigned long srcu_lock_count[2]; /* Locks per CPU. */
27 unsigned long srcu_unlock_count[2]; /* Unlocks per CPU. */
28
29 /* Update-side state. */
d6331980 30 spinlock_t __private lock ____cacheline_internodealigned_in_smp;
da915ad5
PM
31 struct rcu_segcblist srcu_cblist; /* List of callbacks.*/
32 unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */
1e9a038b 33 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
da915ad5 34 bool srcu_cblist_invoking; /* Invoking these CBs? */
e81baf4c
SAS
35 struct timer_list delay_work; /* Delay for CB invoking */
36 struct work_struct work; /* Context for CB invoking. */
da915ad5
PM
37 struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */
38 struct srcu_node *mynode; /* Leaf srcu_node. */
c7e88067
PM
39 unsigned long grpmask; /* Mask for leaf srcu_node */
40 /* ->srcu_data_have_cbs[]. */
da915ad5 41 int cpu;
aacb5d91 42 struct srcu_struct *ssp;
d8be8173
PM
43};
44
da915ad5
PM
45/*
46 * Node in SRCU combining tree, similar in function to rcu_data.
47 */
48struct srcu_node {
d6331980 49 spinlock_t __private lock;
95ebe80d
PM
50 unsigned long srcu_have_cbs[4]; /* GP seq for children having CBs, but only */
51 /* if greater than ->srcu_gq_seq. */
52 unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs have CBs for given GP? */
1e9a038b 53 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
da915ad5
PM
54 struct srcu_node *srcu_parent; /* Next up in tree. */
55 int grplo; /* Least CPU for node. */
56 int grphi; /* Biggest CPU for node. */
57};
58
59/*
60 * Per-SRCU-domain structure, similar in function to rcu_state.
61 */
d8be8173 62struct srcu_struct {
2ec30311 63 struct srcu_node *node; /* Combining tree. */
da915ad5
PM
64 struct srcu_node *level[RCU_NUM_LVLS + 1];
65 /* First node at each level. */
994f7068 66 int srcu_size_state; /* Small-to-big transition state. */
da915ad5 67 struct mutex srcu_cb_mutex; /* Serialize CB preparation. */
994f7068 68 spinlock_t __private lock; /* Protect counters and size state. */
da915ad5
PM
69 struct mutex srcu_gp_mutex; /* Serialize GP work. */
70 unsigned int srcu_idx; /* Current rdr array element. */
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. */
da915ad5 79 struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
46470cf8 80 bool sda_is_static; /* May ->sda be passed to free_percpu()? */
da915ad5
PM
81 unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */
82 struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */
83 struct completion srcu_barrier_completion;
84 /* Awaken barrier rq at end. */
85 atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */
86 /* callback for the barrier */
87 /* operation. */
282d8998
PM
88 unsigned long reschedule_jiffies;
89 unsigned long reschedule_count;
d8be8173 90 struct delayed_work work;
d8be8173 91 struct lockdep_map dep_map;
d8be8173
PM
92};
93
994f7068
PM
94/* Values for size state variable (->srcu_size_state). */
95#define SRCU_SIZE_SMALL 0
96#define SRCU_SIZE_ALLOC 1
97#define SRCU_SIZE_WAIT_BARRIER 2
98#define SRCU_SIZE_WAIT_CALL 3
99#define SRCU_SIZE_WAIT_CBS1 4
100#define SRCU_SIZE_WAIT_CBS2 5
101#define SRCU_SIZE_WAIT_CBS3 6
102#define SRCU_SIZE_WAIT_CBS4 7
103#define SRCU_SIZE_BIG 8
104
da915ad5 105/* Values for state variable (bottom bits of ->srcu_gp_seq). */
d8be8173
PM
106#define SRCU_STATE_IDLE 0
107#define SRCU_STATE_SCAN1 1
108#define SRCU_STATE_SCAN2 2
109
9c80172b 110#define __SRCU_STRUCT_INIT(name, pcpu_name) \
e0fcba9a
PM
111{ \
112 .sda = &pcpu_name, \
113 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
114 .srcu_gp_seq_needed = -1UL, \
4e6ea4ef 115 .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \
e0fcba9a
PM
116 __SRCU_DEP_MAP_INIT(name) \
117}
d8be8173
PM
118
119/*
120 * Define and initialize a srcu struct at build time.
121 * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
122 *
123 * Note that although DEFINE_STATIC_SRCU() hides the name from other
124 * files, the per-CPU variable rules nevertheless require that the
125 * chosen name be globally unique. These rules also prohibit use of
126 * DEFINE_STATIC_SRCU() within a function. If these rules are too
127 * restrictive, declare the srcu_struct manually. For example, in
128 * each file:
129 *
130 * static struct srcu_struct my_srcu;
131 *
132 * Then, before the first use of each my_srcu, manually initialize it:
133 *
134 * init_srcu_struct(&my_srcu);
135 *
136 * See include/linux/percpu-defs.h for the rules on per-CPU variables.
137 */
fe15b50c
PM
138#ifdef MODULE
139# define __DEFINE_SRCU(name, is_static) \
140 is_static struct srcu_struct name; \
db8f1471 141 extern struct srcu_struct * const __srcu_struct_##name; \
056b89e7 142 struct srcu_struct * const __srcu_struct_##name \
fe15b50c
PM
143 __section("___srcu_struct_ptrs") = &name
144#else
145# define __DEFINE_SRCU(name, is_static) \
146 static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data); \
147 is_static struct srcu_struct name = \
148 __SRCU_STRUCT_INIT(name, name##_srcu_data)
149#endif
d8be8173
PM
150#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
151#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
152
aacb5d91
PM
153void synchronize_srcu_expedited(struct srcu_struct *ssp);
154void srcu_barrier(struct srcu_struct *ssp);
155void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
d8be8173
PM
156
157#endif