blob: de7a42116b2eb0510cf95baf057188b4b7a28fdc [file] [log] [blame]
Paul E. McKenney8c366db2019-01-17 10:39:22 -08001/* SPDX-License-Identifier: GPL-2.0+ */
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07002/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion,
4 * tree variant.
5 *
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07006 * Copyright (C) IBM Corporation, 2017
7 *
Paul E. McKenney8c366db2019-01-17 10:39:22 -08008 * Author: Paul McKenney <paulmck@linux.ibm.com>
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07009 */
10
11#ifndef _LINUX_SRCU_TREE_H
12#define _LINUX_SRCU_TREE_H
13
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070014#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. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -070030 spinlock_t __private lock ____cacheline_internodealigned_in_smp;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070031 struct rcu_segcblist srcu_cblist; /* List of callbacks.*/
32 unsigned long srcu_gp_seq_needed; /* Furthest future GP needed. */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -070033 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070034 bool srcu_cblist_invoking; /* Invoking these CBs? */
35 struct delayed_work work; /* Context for CB invoking. */
36 struct rcu_head srcu_barrier_head; /* For srcu_barrier() use. */
37 struct srcu_node *mynode; /* Leaf srcu_node. */
Paul E. McKenneyc7e88062017-04-18 16:01:46 -070038 unsigned long grpmask; /* Mask for leaf srcu_node */
39 /* ->srcu_data_have_cbs[]. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070040 int cpu;
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070041 struct srcu_struct *ssp;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070042};
43
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070044/*
45 * Node in SRCU combining tree, similar in function to rcu_data.
46 */
47struct srcu_node {
Paul E. McKenneyd6331982017-10-10 13:52:30 -070048 spinlock_t __private lock;
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070049 unsigned long srcu_have_cbs[4]; /* GP seq for children */
50 /* having CBs, but only */
51 /* is > ->srcu_gq_seq. */
Paul E. McKenneyc7e88062017-04-18 16:01:46 -070052 unsigned long srcu_data_have_cbs[4]; /* Which srcu_data structs */
53 /* have CBs for given GP? */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -070054 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070055 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, similar in function to rcu_state.
62 */
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070063struct srcu_struct {
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070064 struct srcu_node node[NUM_RCU_NODES]; /* Combining tree. */
65 struct srcu_node *level[RCU_NUM_LVLS + 1];
66 /* First node at each level. */
67 struct mutex srcu_cb_mutex; /* Serialize CB preparation. */
Paul E. McKenneyd6331982017-10-10 13:52:30 -070068 spinlock_t __private lock; /* Protect counters */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070069 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. */
Paul E. McKenney1e9a0382017-04-24 16:02:09 -070073 unsigned long srcu_gp_seq_needed_exp; /* Furthest future exp GP. */
Paul E. McKenney22607d62017-04-25 14:03:11 -070074 unsigned long srcu_last_gp_end; /* Last GP end timestamp (ns) */
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070075 struct srcu_data __percpu *sda; /* Per-CPU srcu_data array. */
76 unsigned long srcu_barrier_seq; /* srcu_barrier seq #. */
77 struct mutex srcu_barrier_mutex; /* Serialize barrier ops. */
78 struct completion srcu_barrier_completion;
79 /* Awaken barrier rq at end. */
80 atomic_t srcu_barrier_cpu_cnt; /* # CPUs not yet posting a */
81 /* callback for the barrier */
82 /* operation. */
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070083 struct delayed_work work;
84#ifdef CONFIG_DEBUG_LOCK_ALLOC
85 struct lockdep_map dep_map;
86#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
87};
88
Paul E. McKenneyda915ad2017-04-05 09:01:53 -070089/* Values for state variable (bottom bits of ->srcu_gp_seq). */
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070090#define SRCU_STATE_IDLE 0
91#define SRCU_STATE_SCAN1 1
92#define SRCU_STATE_SCAN2 2
93
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +020094#define __SRCU_STRUCT_INIT(name, pcpu_name) \
Paul E. McKenneye0fcba92018-08-14 08:45:54 -070095{ \
96 .sda = &pcpu_name, \
97 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
98 .srcu_gp_seq_needed = -1UL, \
Paul E. McKenney4e6ea4e2018-08-14 14:41:49 -070099 .work = __DELAYED_WORK_INITIALIZER(name.work, NULL, 0), \
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700100 __SRCU_DEP_MAP_INIT(name) \
101}
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700102
103/*
104 * Define and initialize a srcu struct at build time.
105 * Do -not- call init_srcu_struct() nor cleanup_srcu_struct() on it.
106 *
107 * Note that although DEFINE_STATIC_SRCU() hides the name from other
108 * files, the per-CPU variable rules nevertheless require that the
109 * chosen name be globally unique. These rules also prohibit use of
110 * DEFINE_STATIC_SRCU() within a function. If these rules are too
111 * restrictive, declare the srcu_struct manually. For example, in
112 * each file:
113 *
114 * static struct srcu_struct my_srcu;
115 *
116 * Then, before the first use of each my_srcu, manually initialize it:
117 *
118 * init_srcu_struct(&my_srcu);
119 *
120 * See include/linux/percpu-defs.h for the rules on per-CPU variables.
121 */
122#define __DEFINE_SRCU(name, is_static) \
Paul E. McKenneyda915ad2017-04-05 09:01:53 -0700123 static DEFINE_PER_CPU(struct srcu_data, name##_srcu_data);\
Sebastian Andrzej Siewior9c801722018-05-25 12:19:57 +0200124 is_static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name##_srcu_data)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700125#define DEFINE_SRCU(name) __DEFINE_SRCU(name, /* not static */)
126#define DEFINE_STATIC_SRCU(name) __DEFINE_SRCU(name, static)
127
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700128void synchronize_srcu_expedited(struct srcu_struct *ssp);
129void srcu_barrier(struct srcu_struct *ssp);
130void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700131
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700132#endif