srcu.h 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Sleepable Read-Copy Update mechanism for mutual exclusion
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15 16
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
17 18
 *
 * Copyright (C) IBM Corporation, 2006
19
 * Copyright (C) Fujitsu, 2012
20 21
 *
 * Author: Paul McKenney <paulmck@us.ibm.com>
22
 *	   Lai Jiangshan <laijs@cn.fujitsu.com>
23 24
 *
 * For detailed explanation of Read-Copy Update mechanism see -
25
 *		Documentation/RCU/ *.txt
26 27 28
 *
 */

29 30 31
#ifndef _LINUX_SRCU_H
#define _LINUX_SRCU_H

32
#include <linux/mutex.h>
33
#include <linux/rcupdate.h>
34
#include <linux/workqueue.h>
35
#include <linux/rcu_segcblist.h>
36

P
Paul E. McKenney 已提交
37
struct srcu_struct;
38

39 40 41 42 43 44 45 46 47 48 49 50
#ifdef CONFIG_DEBUG_LOCK_ALLOC

int __init_srcu_struct(struct srcu_struct *sp, const char *name,
		       struct lock_class_key *key);

#define init_srcu_struct(sp) \
({ \
	static struct lock_class_key __srcu_key; \
	\
	__init_srcu_struct((sp), #sp, &__srcu_key); \
})

L
Lai Jiangshan 已提交
51
#define __SRCU_DEP_MAP_INIT(srcu_name)	.dep_map = { .name = #srcu_name },
52 53
#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */

54
int init_srcu_struct(struct srcu_struct *sp);
55

L
Lai Jiangshan 已提交
56
#define __SRCU_DEP_MAP_INIT(srcu_name)
57 58
#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */

P
Paul E. McKenney 已提交
59 60 61 62
#ifdef CONFIG_TINY_SRCU
#include <linux/srcutiny.h>
#elif defined(CONFIG_TREE_SRCU)
#include <linux/srcutree.h>
63 64
#elif defined(CONFIG_CLASSIC_SRCU)
#include <linux/srcuclassic.h>
P
Paul E. McKenney 已提交
65 66 67
#else
#error "Unknown SRCU implementation specified to kernel configuration"
#endif
L
Lai Jiangshan 已提交
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * call_srcu() - Queue a callback for invocation after an SRCU grace period
 * @sp: srcu_struct in queue the callback
 * @head: structure to be used for queueing the SRCU callback.
 * @func: function to be invoked after the SRCU grace period
 *
 * The callback function will be invoked some time after a full SRCU
 * grace period elapses, in other words after all pre-existing SRCU
 * read-side critical sections have completed.  However, the callback
 * function might well execute concurrently with other SRCU read-side
 * critical sections that started after call_srcu() was invoked.  SRCU
 * read-side critical sections are delimited by srcu_read_lock() and
 * srcu_read_unlock(), and may be nested.
 *
 * The callback will be invoked from process context, but must nevertheless
 * be fast and must not block.
 */
void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
		void (*func)(struct rcu_head *head));

89
void cleanup_srcu_struct(struct srcu_struct *sp);
90 91
int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp);
void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp);
92
void synchronize_srcu(struct srcu_struct *sp);
93

94 95 96 97 98
#ifdef CONFIG_DEBUG_LOCK_ALLOC

/**
 * srcu_read_lock_held - might we be in SRCU read-side critical section?
 *
99 100
 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU
 * read-side critical section.  In absence of CONFIG_DEBUG_LOCK_ALLOC,
101 102
 * this assumes we are in an SRCU read-side critical section unless it can
 * prove otherwise.
103
 *
104 105 106
 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
 * and while lockdep is disabled.
 *
107 108 109
 * Note that SRCU is based on its own statemachine and it doesn't
 * relies on normal RCU, it can be called from the CPU which
 * is in the idle loop from an RCU point of view or offline.
110 111 112
 */
static inline int srcu_read_lock_held(struct srcu_struct *sp)
{
113
	if (!debug_lockdep_rcu_enabled())
114 115
		return 1;
	return lock_is_held(&sp->dep_map);
116 117 118 119 120 121 122 123 124 125 126
}

#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */

static inline int srcu_read_lock_held(struct srcu_struct *sp)
{
	return 1;
}

#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */

127
/**
128 129 130 131 132
 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing
 * @p: the pointer to fetch and protect for later dereferencing
 * @sp: pointer to the srcu_struct, which is used to check that we
 *	really are in an SRCU read-side critical section.
 * @c: condition to check for update-side use
133
 *
134 135 136 137
 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side
 * critical section will result in an RCU-lockdep splat, unless @c evaluates
 * to 1.  The @c argument will normally be a logical expression containing
 * lockdep_is_held() calls.
138
 */
139
#define srcu_dereference_check(p, sp, c) \
140
	__rcu_dereference_check((p), (c) || srcu_read_lock_held(sp), __rcu)
141 142 143 144 145 146 147 148 149 150 151 152

/**
 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing
 * @p: the pointer to fetch and protect for later dereferencing
 * @sp: pointer to the srcu_struct, which is used to check that we
 *	really are in an SRCU read-side critical section.
 *
 * Makes rcu_dereference_check() do the dirty work.  If PROVE_RCU
 * is enabled, invoking this outside of an RCU read-side critical
 * section will result in an RCU-lockdep splat.
 */
#define srcu_dereference(p, sp) srcu_dereference_check((p), (sp), 0)
153

154 155 156 157 158
/**
 * srcu_read_lock - register a new reader for an SRCU-protected structure.
 * @sp: srcu_struct in which to register the new reader.
 *
 * Enter an SRCU read-side critical section.  Note that SRCU read-side
159 160 161 162 163 164
 * critical sections may be nested.  However, it is illegal to
 * call anything that waits on an SRCU grace period for the same
 * srcu_struct, whether directly or indirectly.  Please note that
 * one way to indirectly wait on an SRCU grace period is to acquire
 * a mutex that is held elsewhere while calling synchronize_srcu() or
 * synchronize_srcu_expedited().
165 166 167 168 169
 *
 * Note that srcu_read_lock() and the matching srcu_read_unlock() must
 * occur in the same context, for example, it is illegal to invoke
 * srcu_read_unlock() in an irq handler if the matching srcu_read_lock()
 * was invoked in process context.
170 171 172
 */
static inline int srcu_read_lock(struct srcu_struct *sp) __acquires(sp)
{
173
	int retval;
174

175
	retval = __srcu_read_lock(sp);
176
	rcu_lock_acquire(&(sp)->dep_map);
177 178 179 180 181 182 183 184 185 186 187 188 189
	return retval;
}

/**
 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
 * @sp: srcu_struct in which to unregister the old reader.
 * @idx: return value from corresponding srcu_read_lock().
 *
 * Exit an SRCU read-side critical section.
 */
static inline void srcu_read_unlock(struct srcu_struct *sp, int idx)
	__releases(sp)
{
190
	rcu_lock_release(&(sp)->dep_map);
191 192 193
	__srcu_read_unlock(sp, idx);
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207
/**
 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock
 *
 * Converts the preceding srcu_read_unlock into a two-way memory barrier.
 *
 * Call this after srcu_read_unlock, to guarantee that all memory operations
 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after
 * the preceding srcu_read_unlock.
 */
static inline void smp_mb__after_srcu_read_unlock(void)
{
	/* __srcu_read_unlock has smp_mb() internally so nothing to do here. */
}

208
#endif