2
* 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, 2001
20
* Authors: Dipankar Sarma <dipankar@in.ibm.com>
21
* Manfred Spraul <manfred@colorfullife.com>
23
* Modifications for Xen: Jose Renato Santos
24
* Copyright (C) Hewlett-Packard, 2006
26
* Based on the original work by Paul McKenney <paulmck@us.ibm.com>
27
* and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
29
* http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
30
* http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
32
* For detailed explanation of Read-Copy Update mechanism see -
33
* http://lse.sourceforge.net/locking/rcupdate.html
35
#include <xen/types.h>
36
#include <xen/kernel.h>
38
#include <xen/spinlock.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>
47
/* Definition for rcupdate control block. */
48
struct rcu_ctrlblk rcu_ctrlblk = {
51
.lock = SPIN_LOCK_UNLOCKED,
52
.cpumask = CPU_MASK_NONE,
55
DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
57
static int blimit = 10;
58
static int qhimark = 10000;
59
static int qlowmark = 100;
60
static int rsinterval = 1000;
62
static void force_quiescent_state(struct rcu_data *rdp,
63
struct rcu_ctrlblk *rcp)
66
raise_softirq(SCHEDULE_SOFTIRQ);
67
if (unlikely(rdp->qlen - rdp->last_rs_qlen > rsinterval)) {
68
rdp->last_rs_qlen = rdp->qlen;
70
* Don't send IPI to itself. With irqs disabled,
71
* rdp->cpu is the current cpu.
73
cpumask = rcp->cpumask;
74
cpu_clear(rdp->cpu, cpumask);
75
cpumask_raise_softirq(cpumask, SCHEDULE_SOFTIRQ);
80
* call_rcu - Queue an RCU callback for invocation after a grace period.
81
* @head: structure to be used for queueing the RCU updates.
82
* @func: actual update function to be invoked after the grace period
84
* The update function will be invoked some time after a full grace
85
* period elapses, in other words after all currently executing RCU
86
* read-side critical sections have completed. RCU read-side critical
87
* sections are delimited by rcu_read_lock() and rcu_read_unlock(),
90
void fastcall call_rcu(struct rcu_head *head,
91
void (*func)(struct rcu_head *rcu))
98
local_irq_save(flags);
99
rdp = &__get_cpu_var(rcu_data);
100
*rdp->nxttail = head;
101
rdp->nxttail = &head->next;
102
if (unlikely(++rdp->qlen > qhimark)) {
103
rdp->blimit = INT_MAX;
104
force_quiescent_state(rdp, &rcu_ctrlblk);
106
local_irq_restore(flags);
110
* Invoke the completed RCU callbacks. They are expected to be in
113
static void rcu_do_batch(struct rcu_data *rdp)
115
struct rcu_head *next, *list;
118
list = rdp->donelist;
120
next = rdp->donelist = list->next;
124
if (++count >= rdp->blimit)
127
if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
128
rdp->blimit = blimit;
130
rdp->donetail = &rdp->donelist;
132
raise_softirq(RCU_SOFTIRQ);
136
* Grace period handling:
137
* The grace period handling consists out of two steps:
138
* - A new grace period is started.
139
* This is done by rcu_start_batch. The start is not broadcasted to
140
* all cpus, they must pick this up by comparing rcp->cur with
141
* rdp->quiescbatch. All cpus are recorded in the
142
* rcu_ctrlblk.cpumask bitmap.
143
* - All cpus must go through a quiescent state.
144
* Since the start of the grace period is not broadcasted, at least two
145
* calls to rcu_check_quiescent_state are required:
146
* The first call just notices that a new grace period is running. The
147
* following calls check if there was a quiescent state since the beginning
148
* of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
149
* the bitmap is empty, then the grace period is completed.
150
* rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
151
* period (if necessary).
154
* Register a new batch of callbacks, and start it up if there is currently no
155
* active batch and the batch to be registered has not already occurred.
156
* Caller must hold rcu_ctrlblk.lock.
158
static void rcu_start_batch(struct rcu_ctrlblk *rcp)
160
if (rcp->next_pending &&
161
rcp->completed == rcp->cur) {
162
rcp->next_pending = 0;
164
* next_pending == 0 must be visible in
165
* __rcu_process_callbacks() before it can see new value of cur.
170
rcp->cpumask = cpu_online_map;
175
* cpu went through a quiescent state since the beginning of the grace period.
176
* Clear it from the cpu mask and complete the grace period if it was the last
177
* cpu. Start another grace period if someone has further entries pending
179
static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
181
cpu_clear(cpu, rcp->cpumask);
182
if (cpus_empty(rcp->cpumask)) {
183
/* batch completed ! */
184
rcp->completed = rcp->cur;
185
rcu_start_batch(rcp);
190
* Check if the cpu has gone through a quiescent state (say context
191
* switch). If so and if it already hasn't done so in this RCU
192
* quiescent cycle, then indicate that it has done so.
194
static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
195
struct rcu_data *rdp)
197
if (rdp->quiescbatch != rcp->cur) {
198
/* start new grace period: */
200
rdp->quiescbatch = rcp->cur;
204
/* Grace period already completed for this cpu?
205
* qs_pending is checked instead of the actual bitmap to avoid
206
* cacheline trashing.
208
if (!rdp->qs_pending)
213
spin_lock(&rcp->lock);
215
* rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
216
* during cpu startup. Ignore the quiescent state.
218
if (likely(rdp->quiescbatch == rcp->cur))
219
cpu_quiet(rdp->cpu, rcp);
221
spin_unlock(&rcp->lock);
226
* This does the RCU processing work from softirq context.
228
static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
229
struct rcu_data *rdp)
231
if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
232
*rdp->donetail = rdp->curlist;
233
rdp->donetail = rdp->curtail;
235
rdp->curtail = &rdp->curlist;
239
if (rdp->nxtlist && !rdp->curlist) {
240
rdp->curlist = rdp->nxtlist;
241
rdp->curtail = rdp->nxttail;
243
rdp->nxttail = &rdp->nxtlist;
247
* start the next batch of callbacks
250
/* determine batch number */
251
rdp->batch = rcp->cur + 1;
252
/* see the comment and corresponding wmb() in
253
* the rcu_start_batch()
257
if (!rcp->next_pending) {
258
/* and start it/schedule start if it's a new batch */
259
spin_lock(&rcp->lock);
260
rcp->next_pending = 1;
261
rcu_start_batch(rcp);
262
spin_unlock(&rcp->lock);
267
rcu_check_quiescent_state(rcp, rdp);
272
static void rcu_process_callbacks(void)
274
__rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
277
static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
279
/* This cpu has pending rcu entries and the grace period
280
* for them has completed.
282
if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
285
/* This cpu has no pending entries, but there are new entries */
286
if (!rdp->curlist && rdp->nxtlist)
289
/* This cpu has finished callbacks to invoke */
293
/* The rcu core waits for a quiescent state from the cpu */
294
if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
301
int rcu_pending(int cpu)
303
return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu));
307
* Check to see if any future RCU-related work will need to be done
308
* by the current CPU, even if none need be done immediately, returning
309
* 1 if so. This function is part of the RCU implementation; it is -not-
310
* an exported member of the RCU API.
312
int rcu_needs_cpu(int cpu)
314
struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
316
return (!!rdp->curlist || rcu_pending(cpu));
319
void rcu_check_callbacks(int cpu)
321
raise_softirq(RCU_SOFTIRQ);
324
static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
325
struct rcu_data *rdp)
327
memset(rdp, 0, sizeof(*rdp));
328
rdp->curtail = &rdp->curlist;
329
rdp->nxttail = &rdp->nxtlist;
330
rdp->donetail = &rdp->donelist;
331
rdp->quiescbatch = rcp->completed;
334
rdp->blimit = blimit;
337
void __devinit rcu_online_cpu(int cpu)
339
struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
341
rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
344
void __init rcu_init(void)
346
rcu_online_cpu(smp_processor_id());
347
open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);