remove the build INITRAMFS
[qi-kernel.git] / kernel / srcu.c
blobb0aeeaf22ce49c8c76098aac5224b1f4c44d4c4d
1 /*
2 * Sleepable Read-Copy Update mechanism for mutual exclusion.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2006
20 * Author: Paul McKenney <paulmck@us.ibm.com>
22 * For detailed explanation of Read-Copy Update mechanism see -
23 * Documentation/RCU/ *.txt
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/percpu.h>
30 #include <linux/preempt.h>
31 #include <linux/rcupdate.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/smp.h>
35 #include <linux/srcu.h>
37 /**
38 * init_srcu_struct - initialize a sleep-RCU structure
39 * @sp: structure to initialize.
41 * Must invoke this on a given srcu_struct before passing that srcu_struct
42 * to any other function. Each srcu_struct represents a separate domain
43 * of SRCU protection.
45 int init_srcu_struct(struct srcu_struct *sp)
47 sp->completed = 0;
48 mutex_init(&sp->mutex);
49 sp->per_cpu_ref = alloc_percpu(struct srcu_struct_array);
50 return (sp->per_cpu_ref ? 0 : -ENOMEM);
54 * srcu_readers_active_idx -- returns approximate number of readers
55 * active on the specified rank of per-CPU counters.
58 static int srcu_readers_active_idx(struct srcu_struct *sp, int idx)
60 int cpu;
61 int sum;
63 sum = 0;
64 for_each_possible_cpu(cpu)
65 sum += per_cpu_ptr(sp->per_cpu_ref, cpu)->c[idx];
66 return sum;
69 /**
70 * srcu_readers_active - returns approximate number of readers.
71 * @sp: which srcu_struct to count active readers (holding srcu_read_lock).
73 * Note that this is not an atomic primitive, and can therefore suffer
74 * severe errors when invoked on an active srcu_struct. That said, it
75 * can be useful as an error check at cleanup time.
77 static int srcu_readers_active(struct srcu_struct *sp)
79 return srcu_readers_active_idx(sp, 0) + srcu_readers_active_idx(sp, 1);
82 /**
83 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
84 * @sp: structure to clean up.
86 * Must invoke this after you are finished using a given srcu_struct that
87 * was initialized via init_srcu_struct(), else you leak memory.
89 void cleanup_srcu_struct(struct srcu_struct *sp)
91 int sum;
93 sum = srcu_readers_active(sp);
94 WARN_ON(sum); /* Leakage unless caller handles error. */
95 if (sum != 0)
96 return;
97 free_percpu(sp->per_cpu_ref);
98 sp->per_cpu_ref = NULL;
102 * srcu_read_lock - register a new reader for an SRCU-protected structure.
103 * @sp: srcu_struct in which to register the new reader.
105 * Counts the new reader in the appropriate per-CPU element of the
106 * srcu_struct. Must be called from process context.
107 * Returns an index that must be passed to the matching srcu_read_unlock().
109 int srcu_read_lock(struct srcu_struct *sp)
111 int idx;
113 preempt_disable();
114 idx = sp->completed & 0x1;
115 barrier(); /* ensure compiler looks -once- at sp->completed. */
116 per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]++;
117 srcu_barrier(); /* ensure compiler won't misorder critical section. */
118 preempt_enable();
119 return idx;
123 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure.
124 * @sp: srcu_struct in which to unregister the old reader.
125 * @idx: return value from corresponding srcu_read_lock().
127 * Removes the count for the old reader from the appropriate per-CPU
128 * element of the srcu_struct. Note that this may well be a different
129 * CPU than that which was incremented by the corresponding srcu_read_lock().
130 * Must be called from process context.
132 void srcu_read_unlock(struct srcu_struct *sp, int idx)
134 preempt_disable();
135 srcu_barrier(); /* ensure compiler won't misorder critical section. */
136 per_cpu_ptr(sp->per_cpu_ref, smp_processor_id())->c[idx]--;
137 preempt_enable();
141 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
142 * @sp: srcu_struct with which to synchronize.
144 * Flip the completed counter, and wait for the old count to drain to zero.
145 * As with classic RCU, the updater must use some separate means of
146 * synchronizing concurrent updates. Can block; must be called from
147 * process context.
149 * Note that it is illegal to call synchornize_srcu() from the corresponding
150 * SRCU read-side critical section; doing so will result in deadlock.
151 * However, it is perfectly legal to call synchronize_srcu() on one
152 * srcu_struct from some other srcu_struct's read-side critical section.
154 void synchronize_srcu(struct srcu_struct *sp)
156 int idx;
158 idx = sp->completed;
159 mutex_lock(&sp->mutex);
162 * Check to see if someone else did the work for us while we were
163 * waiting to acquire the lock. We need -two- advances of
164 * the counter, not just one. If there was but one, we might have
165 * shown up -after- our helper's first synchronize_sched(), thus
166 * having failed to prevent CPU-reordering races with concurrent
167 * srcu_read_unlock()s on other CPUs (see comment below). So we
168 * either (1) wait for two or (2) supply the second ourselves.
171 if ((sp->completed - idx) >= 2) {
172 mutex_unlock(&sp->mutex);
173 return;
176 synchronize_sched(); /* Force memory barrier on all CPUs. */
179 * The preceding synchronize_sched() ensures that any CPU that
180 * sees the new value of sp->completed will also see any preceding
181 * changes to data structures made by this CPU. This prevents
182 * some other CPU from reordering the accesses in its SRCU
183 * read-side critical section to precede the corresponding
184 * srcu_read_lock() -- ensuring that such references will in
185 * fact be protected.
187 * So it is now safe to do the flip.
190 idx = sp->completed & 0x1;
191 sp->completed++;
193 synchronize_sched(); /* Force memory barrier on all CPUs. */
196 * At this point, because of the preceding synchronize_sched(),
197 * all srcu_read_lock() calls using the old counters have completed.
198 * Their corresponding critical sections might well be still
199 * executing, but the srcu_read_lock() primitives themselves
200 * will have finished executing.
203 while (srcu_readers_active_idx(sp, idx))
204 schedule_timeout_interruptible(1);
206 synchronize_sched(); /* Force memory barrier on all CPUs. */
209 * The preceding synchronize_sched() forces all srcu_read_unlock()
210 * primitives that were executing concurrently with the preceding
211 * for_each_possible_cpu() loop to have completed by this point.
212 * More importantly, it also forces the corresponding SRCU read-side
213 * critical sections to have also completed, and the corresponding
214 * references to SRCU-protected data items to be dropped.
216 * Note:
218 * Despite what you might think at first glance, the
219 * preceding synchronize_sched() -must- be within the
220 * critical section ended by the following mutex_unlock().
221 * Otherwise, a task taking the early exit can race
222 * with a srcu_read_unlock(), which might have executed
223 * just before the preceding srcu_readers_active() check,
224 * and whose CPU might have reordered the srcu_read_unlock()
225 * with the preceding critical section. In this case, there
226 * is nothing preventing the synchronize_sched() task that is
227 * taking the early exit from freeing a data structure that
228 * is still being referenced (out of order) by the task
229 * doing the srcu_read_unlock().
231 * Alternatively, the comparison with "2" on the early exit
232 * could be changed to "3", but this increases synchronize_srcu()
233 * latency for bulk loads. So the current code is preferred.
236 mutex_unlock(&sp->mutex);
240 * srcu_batches_completed - return batches completed.
241 * @sp: srcu_struct on which to report batch completion.
243 * Report the number of batches, correlated with, but not necessarily
244 * precisely the same as, the number of grace periods that have elapsed.
247 long srcu_batches_completed(struct srcu_struct *sp)
249 return sp->completed;
252 EXPORT_SYMBOL_GPL(init_srcu_struct);
253 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
254 EXPORT_SYMBOL_GPL(srcu_read_lock);
255 EXPORT_SYMBOL_GPL(srcu_read_unlock);
256 EXPORT_SYMBOL_GPL(synchronize_srcu);
257 EXPORT_SYMBOL_GPL(srcu_batches_completed);