Blame view

kernel/srcu.c 10.4 KB
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*
   * 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
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   * Copyright (C) IBM Corporation, 2006
   *
   * Author: Paul McKenney <paulmck@us.ibm.com>
   *
   * For detailed explanation of Read-Copy Update mechanism see -
   * 		Documentation/RCU/ *.txt
   *
   */
9984de1a5   Paul Gortmaker   kernel: Map most ...
26
  #include <linux/export.h>
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
27
28
29
30
31
  #include <linux/mutex.h>
  #include <linux/percpu.h>
  #include <linux/preempt.h>
  #include <linux/rcupdate.h>
  #include <linux/sched.h>
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
32
  #include <linux/smp.h>
46fdb0937   Paul E. McKenney   rcu: Make synchro...
33
  #include <linux/delay.h>
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
34
  #include <linux/srcu.h>
632ee2001   Paul E. McKenney   rcu: Introduce lo...
35
36
37
38
39
40
41
42
43
44
45
46
47
  static int init_srcu_struct_fields(struct srcu_struct *sp)
  {
  	sp->completed = 0;
  	mutex_init(&sp->mutex);
  	sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
  	return sp->per_cpu_ref ? 0 : -ENOMEM;
  }
  
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
  
  int __init_srcu_struct(struct srcu_struct *sp, const char *name,
  		       struct lock_class_key *key)
  {
632ee2001   Paul E. McKenney   rcu: Introduce lo...
48
49
50
  	/* Don't re-initialize a lock while it is held. */
  	debug_check_no_locks_freed((void *)sp, sizeof(*sp));
  	lockdep_init_map(&sp->dep_map, name, key, 0);
632ee2001   Paul E. McKenney   rcu: Introduce lo...
51
52
53
54
55
  	return init_srcu_struct_fields(sp);
  }
  EXPORT_SYMBOL_GPL(__init_srcu_struct);
  
  #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
56
57
58
59
60
61
62
63
  /**
   * init_srcu_struct - initialize a sleep-RCU structure
   * @sp: structure to initialize.
   *
   * Must invoke this on a given srcu_struct before passing that srcu_struct
   * to any other function.  Each srcu_struct represents a separate domain
   * of SRCU protection.
   */
e6a92013b   Alan Stern   [PATCH] SRCU: rep...
64
  int init_srcu_struct(struct srcu_struct *sp)
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
65
  {
632ee2001   Paul E. McKenney   rcu: Introduce lo...
66
  	return init_srcu_struct_fields(sp);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
67
  }
0cd397d33   Paul E. McKenney   rcu: Add synchron...
68
  EXPORT_SYMBOL_GPL(init_srcu_struct);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
69

632ee2001   Paul E. McKenney   rcu: Introduce lo...
70
  #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  /*
   * srcu_readers_active_idx -- returns approximate number of readers
   *	active on the specified rank of per-CPU counters.
   */
  
  static int srcu_readers_active_idx(struct srcu_struct *sp, int idx)
  {
  	int cpu;
  	int sum;
  
  	sum = 0;
  	for_each_possible_cpu(cpu)
  		sum += per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx];
  	return sum;
  }
  
  /**
   * srcu_readers_active - returns approximate number of readers.
   * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
   *
   * Note that this is not an atomic primitive, and can therefore suffer
   * severe errors when invoked on an active srcu_struct.  That said, it
   * can be useful as an error check at cleanup time.
   */
bb695170d   Adrian Bunk   make srcu_readers...
95
  static int srcu_readers_active(struct srcu_struct *sp)
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  {
  	return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
  }
  
  /**
   * cleanup_srcu_struct - deconstruct a sleep-RCU structure
   * @sp: structure to clean up.
   *
   * Must invoke this after you are finished using a given srcu_struct that
   * was initialized via init_srcu_struct(), else you leak memory.
   */
  void cleanup_srcu_struct(struct srcu_struct *sp)
  {
  	int sum;
  
  	sum = srcu_readers_active(sp);
  	WARN_ON(sum);  /* Leakage unless caller handles error. */
  	if (sum != 0)
  		return;
  	free_percpu(sp->per_cpu_ref);
  	sp->per_cpu_ref = NULL;
  }
0cd397d33   Paul E. McKenney   rcu: Add synchron...
118
  EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
119

632ee2001   Paul E. McKenney   rcu: Introduce lo...
120
  /*
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
121
122
123
124
   * Counts the new reader in the appropriate per-CPU element of the
   * srcu_struct.  Must be called from process context.
   * Returns an index that must be passed to the matching srcu_read_unlock().
   */
632ee2001   Paul E. McKenney   rcu: Introduce lo...
125
  int __srcu_read_lock(struct srcu_struct *sp)
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
126
127
128
129
130
131
132
133
134
135
136
  {
  	int idx;
  
  	preempt_disable();
  	idx = sp->completed & 0x1;
  	barrier();  /* ensure compiler looks -once- at sp->completed. */
  	per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]++;
  	srcu_barrier();  /* ensure compiler won't misorder critical section. */
  	preempt_enable();
  	return idx;
  }
632ee2001   Paul E. McKenney   rcu: Introduce lo...
137
  EXPORT_SYMBOL_GPL(__srcu_read_lock);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
138

632ee2001   Paul E. McKenney   rcu: Introduce lo...
139
  /*
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
140
141
142
143
144
   * Removes the count for the old reader from the appropriate per-CPU
   * element of the srcu_struct.  Note that this may well be a different
   * CPU than that which was incremented by the corresponding srcu_read_lock().
   * Must be called from process context.
   */
632ee2001   Paul E. McKenney   rcu: Introduce lo...
145
  void __srcu_read_unlock(struct srcu_struct *sp, int idx)
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
146
147
148
149
150
151
  {
  	preempt_disable();
  	srcu_barrier();  /* ensure compiler won't misorder critical section. */
  	per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]--;
  	preempt_enable();
  }
632ee2001   Paul E. McKenney   rcu: Introduce lo...
152
  EXPORT_SYMBOL_GPL(__srcu_read_unlock);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
153

0cd397d33   Paul E. McKenney   rcu: Add synchron...
154
  /*
c072a388d   Paul E. McKenney   rcu: demote SRCU_...
155
156
157
158
159
160
161
162
163
164
   * We use an adaptive strategy for synchronize_srcu() and especially for
   * synchronize_srcu_expedited().  We spin for a fixed time period
   * (defined below) to allow SRCU readers to exit their read-side critical
   * sections.  If there are still some readers after 10 microseconds,
   * we repeatedly block for 1-millisecond time periods.  This approach
   * has done well in testing, so there is no need for a config parameter.
   */
  #define SYNCHRONIZE_SRCU_READER_DELAY 10
  
  /*
0cd397d33   Paul E. McKenney   rcu: Add synchron...
165
   * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
166
   */
017c42613   Paul E. McKenney   rcu: Fix sparse w...
167
  static void __synchronize_srcu(struct srcu_struct *sp, void (*sync_func)(void))
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  {
  	int idx;
  
  	idx = sp->completed;
  	mutex_lock(&sp->mutex);
  
  	/*
  	 * Check to see if someone else did the work for us while we were
  	 * waiting to acquire the lock.  We need -two- advances of
  	 * the counter, not just one.  If there was but one, we might have
  	 * shown up -after- our helper's first synchronize_sched(), thus
  	 * having failed to prevent CPU-reordering races with concurrent
  	 * srcu_read_unlock()s on other CPUs (see comment below).  So we
  	 * either (1) wait for two or (2) supply the second ourselves.
  	 */
  
  	if ((sp->completed - idx) >= 2) {
  		mutex_unlock(&sp->mutex);
  		return;
  	}
0cd397d33   Paul E. McKenney   rcu: Add synchron...
188
  	sync_func();  /* Force memory barrier on all CPUs. */
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  	/*
  	 * The preceding synchronize_sched() ensures that any CPU that
  	 * sees the new value of sp->completed will also see any preceding
  	 * changes to data structures made by this CPU.  This prevents
  	 * some other CPU from reordering the accesses in its SRCU
  	 * read-side critical section to precede the corresponding
  	 * srcu_read_lock() -- ensuring that such references will in
  	 * fact be protected.
  	 *
  	 * So it is now safe to do the flip.
  	 */
  
  	idx = sp->completed & 0x1;
  	sp->completed++;
0cd397d33   Paul E. McKenney   rcu: Add synchron...
204
  	sync_func();  /* Force memory barrier on all CPUs. */
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
205
206
207
208
209
210
  
  	/*
  	 * At this point, because of the preceding synchronize_sched(),
  	 * all srcu_read_lock() calls using the old counters have completed.
  	 * Their corresponding critical sections might well be still
  	 * executing, but the srcu_read_lock() primitives themselves
46fdb0937   Paul E. McKenney   rcu: Make synchro...
211
212
213
  	 * will have finished executing.  We initially give readers
  	 * an arbitrarily chosen 10 microseconds to get out of their
  	 * SRCU read-side critical sections, then loop waiting 1/HZ
c072a388d   Paul E. McKenney   rcu: demote SRCU_...
214
215
  	 * seconds per iteration.  The 10-microsecond value has done
  	 * very well in testing.
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
216
  	 */
46fdb0937   Paul E. McKenney   rcu: Make synchro...
217
  	if (srcu_readers_active_idx(sp, idx))
c072a388d   Paul E. McKenney   rcu: demote SRCU_...
218
  		udelay(SYNCHRONIZE_SRCU_READER_DELAY);
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
219
220
  	while (srcu_readers_active_idx(sp, idx))
  		schedule_timeout_interruptible(1);
0cd397d33   Paul E. McKenney   rcu: Add synchron...
221
  	sync_func();  /* Force memory barrier on all CPUs. */
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  
  	/*
  	 * The preceding synchronize_sched() forces all srcu_read_unlock()
  	 * primitives that were executing concurrently with the preceding
  	 * for_each_possible_cpu() loop to have completed by this point.
  	 * More importantly, it also forces the corresponding SRCU read-side
  	 * critical sections to have also completed, and the corresponding
  	 * references to SRCU-protected data items to be dropped.
  	 *
  	 * Note:
  	 *
  	 *	Despite what you might think at first glance, the
  	 *	preceding synchronize_sched() -must- be within the
  	 *	critical section ended by the following mutex_unlock().
  	 *	Otherwise, a task taking the early exit can race
  	 *	with a srcu_read_unlock(), which might have executed
  	 *	just before the preceding srcu_readers_active() check,
  	 *	and whose CPU might have reordered the srcu_read_unlock()
  	 *	with the preceding critical section.  In this case, there
  	 *	is nothing preventing the synchronize_sched() task that is
  	 *	taking the early exit from freeing a data structure that
  	 *	is still being referenced (out of order) by the task
  	 *	doing the srcu_read_unlock().
  	 *
  	 *	Alternatively, the comparison with "2" on the early exit
  	 *	could be changed to "3", but this increases synchronize_srcu()
  	 *	latency for bulk loads.  So the current code is preferred.
  	 */
  
  	mutex_unlock(&sp->mutex);
  }
  
  /**
0cd397d33   Paul E. McKenney   rcu: Add synchron...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
   * synchronize_srcu - wait for prior SRCU read-side critical-section completion
   * @sp: srcu_struct with which to synchronize.
   *
   * Flip the completed counter, and wait for the old count to drain to zero.
   * As with classic RCU, the updater must use some separate means of
   * synchronizing concurrent updates.  Can block; must be called from
   * process context.
   *
   * Note that it is illegal to call synchronize_srcu() from the corresponding
   * SRCU read-side critical section; doing so will result in deadlock.
   * However, it is perfectly legal to call synchronize_srcu() on one
   * srcu_struct from some other srcu_struct's read-side critical section.
   */
  void synchronize_srcu(struct srcu_struct *sp)
  {
  	__synchronize_srcu(sp, synchronize_sched);
  }
  EXPORT_SYMBOL_GPL(synchronize_srcu);
  
  /**
   * synchronize_srcu_expedited - like synchronize_srcu, but less patient
   * @sp: srcu_struct with which to synchronize.
   *
   * Flip the completed counter, and wait for the old count to drain to zero.
   * As with classic RCU, the updater must use some separate means of
   * synchronizing concurrent updates.  Can block; must be called from
   * process context.
   *
   * Note that it is illegal to call synchronize_srcu_expedited()
   * from the corresponding SRCU read-side critical section; doing so
   * will result in deadlock.  However, it is perfectly legal to call
   * synchronize_srcu_expedited() on one srcu_struct from some other
   * srcu_struct's read-side critical section.
   */
  void synchronize_srcu_expedited(struct srcu_struct *sp)
  {
  	__synchronize_srcu(sp, synchronize_sched_expedited);
  }
  EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  
  /**
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
296
297
298
299
300
301
302
303
304
305
306
   * srcu_batches_completed - return batches completed.
   * @sp: srcu_struct on which to report batch completion.
   *
   * Report the number of batches, correlated with, but not necessarily
   * precisely the same as, the number of grace periods that have elapsed.
   */
  
  long srcu_batches_completed(struct srcu_struct *sp)
  {
  	return sp->completed;
  }
621934ee7   Paul E. McKenney   [PATCH] srcu-3: R...
307
  EXPORT_SYMBOL_GPL(srcu_batches_completed);