debuggers.hg

view xen/common/rcupdate.c @ 22802:bb0d0141ebf7

rcupdate: Implement rcu_barrier() to synchronously flush RCU work.

Signed-off-by: Keir Fraser <keir@xen.org>
author Keir Fraser <keir@xen.org>
date Fri Jan 14 14:16:42 2011 +0000 (2011-01-14)
parents bc2364ad816d
children 75b6287626ee
line source
1 /*
2 * Read-Copy Update mechanism for mutual exclusion
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright (C) IBM Corporation, 2001
19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 *
23 * Modifications for Xen: Jose Renato Santos
24 * Copyright (C) Hewlett-Packard, 2006
25 *
26 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
27 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
28 * Papers:
29 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
30 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
31 *
32 * For detailed explanation of Read-Copy Update mechanism see -
33 * http://lse.sourceforge.net/locking/rcupdate.html
34 */
35 #include <xen/types.h>
36 #include <xen/kernel.h>
37 #include <xen/init.h>
38 #include <xen/spinlock.h>
39 #include <xen/smp.h>
40 #include <xen/rcupdate.h>
41 #include <xen/sched.h>
42 #include <asm/atomic.h>
43 #include <xen/bitops.h>
44 #include <xen/percpu.h>
45 #include <xen/softirq.h>
46 #include <xen/cpu.h>
47 #include <xen/stop_machine.h>
49 /* Definition for rcupdate control block. */
50 struct rcu_ctrlblk rcu_ctrlblk = {
51 .cur = -300,
52 .completed = -300,
53 .lock = SPIN_LOCK_UNLOCKED,
54 .cpumask = CPU_MASK_NONE,
55 };
57 DEFINE_PER_CPU(struct rcu_data, rcu_data);
59 static int blimit = 10;
60 static int qhimark = 10000;
61 static int qlowmark = 100;
62 static int rsinterval = 1000;
64 static int rcu_barrier_action(void *unused)
65 {
66 unsigned int cpu = smp_processor_id();
68 ASSERT(!local_irq_is_enabled());
69 local_irq_enable();
71 while ( rcu_needs_cpu(cpu) )
72 {
73 rcu_check_callbacks(cpu);
74 process_pending_softirqs();
75 cpu_relax();
76 }
78 local_irq_disable();
80 return 0;
81 }
83 int rcu_barrier(void)
84 {
85 return stop_machine_run(rcu_barrier_action, NULL, NR_CPUS);
86 }
88 static void force_quiescent_state(struct rcu_data *rdp,
89 struct rcu_ctrlblk *rcp)
90 {
91 cpumask_t cpumask;
92 raise_softirq(SCHEDULE_SOFTIRQ);
93 if (unlikely(rdp->qlen - rdp->last_rs_qlen > rsinterval)) {
94 rdp->last_rs_qlen = rdp->qlen;
95 /*
96 * Don't send IPI to itself. With irqs disabled,
97 * rdp->cpu is the current cpu.
98 */
99 cpumask = rcp->cpumask;
100 cpu_clear(rdp->cpu, cpumask);
101 cpumask_raise_softirq(cpumask, SCHEDULE_SOFTIRQ);
102 }
103 }
105 /**
106 * call_rcu - Queue an RCU callback for invocation after a grace period.
107 * @head: structure to be used for queueing the RCU updates.
108 * @func: actual update function to be invoked after the grace period
109 *
110 * The update function will be invoked some time after a full grace
111 * period elapses, in other words after all currently executing RCU
112 * read-side critical sections have completed. RCU read-side critical
113 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
114 * and may be nested.
115 */
116 void fastcall call_rcu(struct rcu_head *head,
117 void (*func)(struct rcu_head *rcu))
118 {
119 unsigned long flags;
120 struct rcu_data *rdp;
122 head->func = func;
123 head->next = NULL;
124 local_irq_save(flags);
125 rdp = &__get_cpu_var(rcu_data);
126 *rdp->nxttail = head;
127 rdp->nxttail = &head->next;
128 if (unlikely(++rdp->qlen > qhimark)) {
129 rdp->blimit = INT_MAX;
130 force_quiescent_state(rdp, &rcu_ctrlblk);
131 }
132 local_irq_restore(flags);
133 }
135 /*
136 * Invoke the completed RCU callbacks. They are expected to be in
137 * a per-cpu list.
138 */
139 static void rcu_do_batch(struct rcu_data *rdp)
140 {
141 struct rcu_head *next, *list;
142 int count = 0;
144 list = rdp->donelist;
145 while (list) {
146 next = rdp->donelist = list->next;
147 list->func(list);
148 list = next;
149 rdp->qlen--;
150 if (++count >= rdp->blimit)
151 break;
152 }
153 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
154 rdp->blimit = blimit;
155 if (!rdp->donelist)
156 rdp->donetail = &rdp->donelist;
157 else
158 raise_softirq(RCU_SOFTIRQ);
159 }
161 /*
162 * Grace period handling:
163 * The grace period handling consists out of two steps:
164 * - A new grace period is started.
165 * This is done by rcu_start_batch. The start is not broadcasted to
166 * all cpus, they must pick this up by comparing rcp->cur with
167 * rdp->quiescbatch. All cpus are recorded in the
168 * rcu_ctrlblk.cpumask bitmap.
169 * - All cpus must go through a quiescent state.
170 * Since the start of the grace period is not broadcasted, at least two
171 * calls to rcu_check_quiescent_state are required:
172 * The first call just notices that a new grace period is running. The
173 * following calls check if there was a quiescent state since the beginning
174 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
175 * the bitmap is empty, then the grace period is completed.
176 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
177 * period (if necessary).
178 */
179 /*
180 * Register a new batch of callbacks, and start it up if there is currently no
181 * active batch and the batch to be registered has not already occurred.
182 * Caller must hold rcu_ctrlblk.lock.
183 */
184 static void rcu_start_batch(struct rcu_ctrlblk *rcp)
185 {
186 if (rcp->next_pending &&
187 rcp->completed == rcp->cur) {
188 rcp->next_pending = 0;
189 /*
190 * next_pending == 0 must be visible in
191 * __rcu_process_callbacks() before it can see new value of cur.
192 */
193 smp_wmb();
194 rcp->cur++;
196 rcp->cpumask = cpu_online_map;
197 }
198 }
200 /*
201 * cpu went through a quiescent state since the beginning of the grace period.
202 * Clear it from the cpu mask and complete the grace period if it was the last
203 * cpu. Start another grace period if someone has further entries pending
204 */
205 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
206 {
207 cpu_clear(cpu, rcp->cpumask);
208 if (cpus_empty(rcp->cpumask)) {
209 /* batch completed ! */
210 rcp->completed = rcp->cur;
211 rcu_start_batch(rcp);
212 }
213 }
215 /*
216 * Check if the cpu has gone through a quiescent state (say context
217 * switch). If so and if it already hasn't done so in this RCU
218 * quiescent cycle, then indicate that it has done so.
219 */
220 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
221 struct rcu_data *rdp)
222 {
223 if (rdp->quiescbatch != rcp->cur) {
224 /* start new grace period: */
225 rdp->qs_pending = 1;
226 rdp->quiescbatch = rcp->cur;
227 return;
228 }
230 /* Grace period already completed for this cpu?
231 * qs_pending is checked instead of the actual bitmap to avoid
232 * cacheline trashing.
233 */
234 if (!rdp->qs_pending)
235 return;
237 rdp->qs_pending = 0;
239 spin_lock(&rcp->lock);
240 /*
241 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
242 * during cpu startup. Ignore the quiescent state.
243 */
244 if (likely(rdp->quiescbatch == rcp->cur))
245 cpu_quiet(rdp->cpu, rcp);
247 spin_unlock(&rcp->lock);
248 }
251 /*
252 * This does the RCU processing work from softirq context.
253 */
254 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
255 struct rcu_data *rdp)
256 {
257 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
258 *rdp->donetail = rdp->curlist;
259 rdp->donetail = rdp->curtail;
260 rdp->curlist = NULL;
261 rdp->curtail = &rdp->curlist;
262 }
264 local_irq_disable();
265 if (rdp->nxtlist && !rdp->curlist) {
266 rdp->curlist = rdp->nxtlist;
267 rdp->curtail = rdp->nxttail;
268 rdp->nxtlist = NULL;
269 rdp->nxttail = &rdp->nxtlist;
270 local_irq_enable();
272 /*
273 * start the next batch of callbacks
274 */
276 /* determine batch number */
277 rdp->batch = rcp->cur + 1;
278 /* see the comment and corresponding wmb() in
279 * the rcu_start_batch()
280 */
281 smp_rmb();
283 if (!rcp->next_pending) {
284 /* and start it/schedule start if it's a new batch */
285 spin_lock(&rcp->lock);
286 rcp->next_pending = 1;
287 rcu_start_batch(rcp);
288 spin_unlock(&rcp->lock);
289 }
290 } else {
291 local_irq_enable();
292 }
293 rcu_check_quiescent_state(rcp, rdp);
294 if (rdp->donelist)
295 rcu_do_batch(rdp);
296 }
298 static void rcu_process_callbacks(void)
299 {
300 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
301 }
303 static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
304 {
305 /* This cpu has pending rcu entries and the grace period
306 * for them has completed.
307 */
308 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
309 return 1;
311 /* This cpu has no pending entries, but there are new entries */
312 if (!rdp->curlist && rdp->nxtlist)
313 return 1;
315 /* This cpu has finished callbacks to invoke */
316 if (rdp->donelist)
317 return 1;
319 /* The rcu core waits for a quiescent state from the cpu */
320 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
321 return 1;
323 /* nothing to do */
324 return 0;
325 }
327 int rcu_pending(int cpu)
328 {
329 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu));
330 }
332 /*
333 * Check to see if any future RCU-related work will need to be done
334 * by the current CPU, even if none need be done immediately, returning
335 * 1 if so. This function is part of the RCU implementation; it is -not-
336 * an exported member of the RCU API.
337 */
338 int rcu_needs_cpu(int cpu)
339 {
340 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
342 return (!!rdp->curlist || rcu_pending(cpu));
343 }
345 void rcu_check_callbacks(int cpu)
346 {
347 raise_softirq(RCU_SOFTIRQ);
348 }
350 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
351 struct rcu_head **tail)
352 {
353 local_irq_disable();
354 *this_rdp->nxttail = list;
355 if (list)
356 this_rdp->nxttail = tail;
357 local_irq_enable();
358 }
360 static void rcu_offline_cpu(struct rcu_data *this_rdp,
361 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
362 {
363 /* If the cpu going offline owns the grace period we can block
364 * indefinitely waiting for it, so flush it here.
365 */
366 spin_lock(&rcp->lock);
367 if (rcp->cur != rcp->completed)
368 cpu_quiet(rdp->cpu, rcp);
369 spin_unlock(&rcp->lock);
371 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
372 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
373 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
375 local_irq_disable();
376 this_rdp->qlen += rdp->qlen;
377 local_irq_enable();
378 }
380 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
381 struct rcu_data *rdp)
382 {
383 memset(rdp, 0, sizeof(*rdp));
384 rdp->curtail = &rdp->curlist;
385 rdp->nxttail = &rdp->nxtlist;
386 rdp->donetail = &rdp->donelist;
387 rdp->quiescbatch = rcp->completed;
388 rdp->qs_pending = 0;
389 rdp->cpu = cpu;
390 rdp->blimit = blimit;
391 }
393 static int cpu_callback(
394 struct notifier_block *nfb, unsigned long action, void *hcpu)
395 {
396 unsigned int cpu = (unsigned long)hcpu;
397 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
399 switch ( action )
400 {
401 case CPU_UP_PREPARE:
402 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
403 break;
404 case CPU_UP_CANCELED:
405 case CPU_DEAD:
406 rcu_offline_cpu(&this_cpu(rcu_data), &rcu_ctrlblk, rdp);
407 break;
408 default:
409 break;
410 }
412 return NOTIFY_DONE;
413 }
415 static struct notifier_block cpu_nfb = {
416 .notifier_call = cpu_callback
417 };
419 void __init rcu_init(void)
420 {
421 void *cpu = (void *)(long)smp_processor_id();
422 cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
423 register_cpu_notifier(&cpu_nfb);
424 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
425 }