2 * RCU-based infrastructure for lightweight reader-writer locking
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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright (c) 2015, Red Hat, Inc.
23 #include <linux/rcu_sync.h>
24 #include <linux/sched.h>
26 #ifdef CONFIG_PROVE_RCU
27 #define __INIT_HELD(func) .held = func,
29 #define __INIT_HELD(func)
34 void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
36 #ifdef CONFIG_PROVE_RCU
41 .sync = synchronize_rcu,
44 __INIT_HELD(rcu_read_lock_held)
47 .sync = synchronize_rcu,
50 __INIT_HELD(rcu_read_lock_sched_held)
53 .sync = synchronize_rcu,
56 __INIT_HELD(rcu_read_lock_bh_held)
60 enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
61 enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
63 #define rss_lock gp_wait.lock
65 #ifdef CONFIG_PROVE_RCU
66 void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
68 RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
69 "suspicious rcu_sync_is_idle() usage");
72 EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
76 * rcu_sync_init() - Initialize an rcu_sync structure
77 * @rsp: Pointer to rcu_sync structure to be initialized
78 * @type: Flavor of RCU with which to synchronize rcu_sync structure
80 void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
82 memset(rsp, 0, sizeof(*rsp));
83 init_waitqueue_head(&rsp->gp_wait);
88 * rcu_sync_enter_start - Force readers onto slow path for multiple updates
89 * @rsp: Pointer to rcu_sync structure to use for synchronization
91 * Must be called after rcu_sync_init() and before first use.
93 * Ensures rcu_sync_is_idle() returns false and rcu_sync_{enter,exit}()
94 * pairs turn into NO-OPs.
96 void rcu_sync_enter_start(struct rcu_sync *rsp)
99 rsp->gp_state = GP_PASSED;
103 * rcu_sync_enter() - Force readers onto slowpath
104 * @rsp: Pointer to rcu_sync structure to use for synchronization
106 * This function is used by updaters who need readers to make use of
107 * a slowpath during the update. After this function returns, all
108 * subsequent calls to rcu_sync_is_idle() will return false, which
109 * tells readers to stay off their fastpaths. A later call to
110 * rcu_sync_exit() re-enables reader slowpaths.
112 * When called in isolation, rcu_sync_enter() must wait for a grace
113 * period, however, closely spaced calls to rcu_sync_enter() can
114 * optimize away the grace-period wait via a state machine implemented
115 * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
117 void rcu_sync_enter(struct rcu_sync *rsp)
119 bool need_wait, need_sync;
121 spin_lock_irq(&rsp->rss_lock);
122 need_wait = rsp->gp_count++;
123 need_sync = rsp->gp_state == GP_IDLE;
125 rsp->gp_state = GP_PENDING;
126 spin_unlock_irq(&rsp->rss_lock);
128 WARN_ON_ONCE(need_wait && need_sync);
130 gp_ops[rsp->gp_type].sync();
131 rsp->gp_state = GP_PASSED;
132 wake_up_all(&rsp->gp_wait);
133 } else if (need_wait) {
134 wait_event(rsp->gp_wait, rsp->gp_state == GP_PASSED);
137 * Possible when there's a pending CB from a rcu_sync_exit().
138 * Nobody has yet been allowed the 'fast' path and thus we can
139 * avoid doing any sync(). The callback will get 'dropped'.
141 WARN_ON_ONCE(rsp->gp_state != GP_PASSED);
146 * rcu_sync_func() - Callback function managing reader access to fastpath
147 * @rhp: Pointer to rcu_head in rcu_sync structure to use for synchronization
149 * This function is passed to one of the call_rcu() functions by
150 * rcu_sync_exit(), so that it is invoked after a grace period following the
151 * that invocation of rcu_sync_exit(). It takes action based on events that
152 * have taken place in the meantime, so that closely spaced rcu_sync_enter()
153 * and rcu_sync_exit() pairs need not wait for a grace period.
155 * If another rcu_sync_enter() is invoked before the grace period
156 * ended, reset state to allow the next rcu_sync_exit() to let the
157 * readers back onto their fastpaths (after a grace period). If both
158 * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
159 * before the grace period ended, re-invoke call_rcu() on behalf of that
160 * rcu_sync_exit(). Otherwise, set all state back to idle so that readers
161 * can again use their fastpaths.
163 static void rcu_sync_func(struct rcu_head *rhp)
165 struct rcu_sync *rsp = container_of(rhp, struct rcu_sync, cb_head);
168 WARN_ON_ONCE(rsp->gp_state != GP_PASSED);
169 WARN_ON_ONCE(rsp->cb_state == CB_IDLE);
171 spin_lock_irqsave(&rsp->rss_lock, flags);
174 * A new rcu_sync_begin() has happened; drop the callback.
176 rsp->cb_state = CB_IDLE;
177 } else if (rsp->cb_state == CB_REPLAY) {
179 * A new rcu_sync_exit() has happened; requeue the callback
180 * to catch a later GP.
182 rsp->cb_state = CB_PENDING;
183 gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
186 * We're at least a GP after rcu_sync_exit(); eveybody will now
187 * have observed the write side critical section. Let 'em rip!.
189 rsp->cb_state = CB_IDLE;
190 rsp->gp_state = GP_IDLE;
192 spin_unlock_irqrestore(&rsp->rss_lock, flags);
196 * rcu_sync_exit() - Allow readers back onto fast patch after grace period
197 * @rsp: Pointer to rcu_sync structure to use for synchronization
199 * This function is used by updaters who have completed, and can therefore
200 * now allow readers to make use of their fastpaths after a grace period
201 * has elapsed. After this grace period has completed, all subsequent
202 * calls to rcu_sync_is_idle() will return true, which tells readers that
203 * they can once again use their fastpaths.
205 void rcu_sync_exit(struct rcu_sync *rsp)
207 spin_lock_irq(&rsp->rss_lock);
208 if (!--rsp->gp_count) {
209 if (rsp->cb_state == CB_IDLE) {
210 rsp->cb_state = CB_PENDING;
211 gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
212 } else if (rsp->cb_state == CB_PENDING) {
213 rsp->cb_state = CB_REPLAY;
216 spin_unlock_irq(&rsp->rss_lock);
220 * rcu_sync_dtor() - Clean up an rcu_sync structure
221 * @rsp: Pointer to rcu_sync structure to be cleaned up
223 void rcu_sync_dtor(struct rcu_sync *rsp)
227 WARN_ON_ONCE(rsp->gp_count);
229 spin_lock_irq(&rsp->rss_lock);
230 if (rsp->cb_state == CB_REPLAY)
231 rsp->cb_state = CB_PENDING;
232 cb_state = rsp->cb_state;
233 spin_unlock_irq(&rsp->rss_lock);
235 if (cb_state != CB_IDLE) {
236 gp_ops[rsp->gp_type].wait();
237 WARN_ON_ONCE(rsp->cb_state != CB_IDLE);