rcu_sync: Simplify rcu_sync using new rcu_sync_ops structure
[linux-block.git] / kernel / rcu / sync.c
CommitLineData
cc44ca84
ON
1/*
2 * RCU-based infrastructure for lightweight reader-writer locking
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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright (c) 2015, Red Hat, Inc.
19 *
20 * Author: Oleg Nesterov <oleg@redhat.com>
21 */
22
23#include <linux/rcu_sync.h>
24#include <linux/sched.h>
25
82e8c565
ON
26static const struct {
27 void (*sync)(void);
28 void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
29} gp_ops[] = {
30 [RCU_SYNC] = {
31 .sync = synchronize_rcu,
32 .call = call_rcu,
33 },
34 [RCU_SCHED_SYNC] = {
35 .sync = synchronize_sched,
36 .call = call_rcu_sched,
37 },
38 [RCU_BH_SYNC] = {
39 .sync = synchronize_rcu_bh,
40 .call = call_rcu_bh,
41 },
42};
43
cc44ca84
ON
44enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
45enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
46
47#define rss_lock gp_wait.lock
48
49/**
50 * rcu_sync_init() - Initialize an rcu_sync structure
51 * @rsp: Pointer to rcu_sync structure to be initialized
52 * @type: Flavor of RCU with which to synchronize rcu_sync structure
53 */
54void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
55{
56 memset(rsp, 0, sizeof(*rsp));
57 init_waitqueue_head(&rsp->gp_wait);
82e8c565 58 rsp->gp_type = type;
cc44ca84
ON
59}
60
61/**
62 * rcu_sync_enter() - Force readers onto slowpath
63 * @rsp: Pointer to rcu_sync structure to use for synchronization
64 *
65 * This function is used by updaters who need readers to make use of
66 * a slowpath during the update. After this function returns, all
67 * subsequent calls to rcu_sync_is_idle() will return false, which
68 * tells readers to stay off their fastpaths. A later call to
69 * rcu_sync_exit() re-enables reader slowpaths.
70 *
71 * When called in isolation, rcu_sync_enter() must wait for a grace
72 * period, however, closely spaced calls to rcu_sync_enter() can
73 * optimize away the grace-period wait via a state machine implemented
74 * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
75 */
76void rcu_sync_enter(struct rcu_sync *rsp)
77{
78 bool need_wait, need_sync;
79
80 spin_lock_irq(&rsp->rss_lock);
81 need_wait = rsp->gp_count++;
82 need_sync = rsp->gp_state == GP_IDLE;
83 if (need_sync)
84 rsp->gp_state = GP_PENDING;
85 spin_unlock_irq(&rsp->rss_lock);
86
87 BUG_ON(need_wait && need_sync);
88
89 if (need_sync) {
82e8c565 90 gp_ops[rsp->gp_type].sync();
cc44ca84
ON
91 rsp->gp_state = GP_PASSED;
92 wake_up_all(&rsp->gp_wait);
93 } else if (need_wait) {
94 wait_event(rsp->gp_wait, rsp->gp_state == GP_PASSED);
95 } else {
96 /*
97 * Possible when there's a pending CB from a rcu_sync_exit().
98 * Nobody has yet been allowed the 'fast' path and thus we can
99 * avoid doing any sync(). The callback will get 'dropped'.
100 */
101 BUG_ON(rsp->gp_state != GP_PASSED);
102 }
103}
104
105/**
106 * rcu_sync_func() - Callback function managing reader access to fastpath
107 * @rsp: Pointer to rcu_sync structure to use for synchronization
108 *
109 * This function is passed to one of the call_rcu() functions by
110 * rcu_sync_exit(), so that it is invoked after a grace period following the
111 * that invocation of rcu_sync_exit(). It takes action based on events that
112 * have taken place in the meantime, so that closely spaced rcu_sync_enter()
113 * and rcu_sync_exit() pairs need not wait for a grace period.
114 *
115 * If another rcu_sync_enter() is invoked before the grace period
116 * ended, reset state to allow the next rcu_sync_exit() to let the
117 * readers back onto their fastpaths (after a grace period). If both
118 * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
119 * before the grace period ended, re-invoke call_rcu() on behalf of that
120 * rcu_sync_exit(). Otherwise, set all state back to idle so that readers
121 * can again use their fastpaths.
122 */
123static void rcu_sync_func(struct rcu_head *rcu)
124{
125 struct rcu_sync *rsp = container_of(rcu, struct rcu_sync, cb_head);
126 unsigned long flags;
127
128 BUG_ON(rsp->gp_state != GP_PASSED);
129 BUG_ON(rsp->cb_state == CB_IDLE);
130
131 spin_lock_irqsave(&rsp->rss_lock, flags);
132 if (rsp->gp_count) {
133 /*
134 * A new rcu_sync_begin() has happened; drop the callback.
135 */
136 rsp->cb_state = CB_IDLE;
137 } else if (rsp->cb_state == CB_REPLAY) {
138 /*
139 * A new rcu_sync_exit() has happened; requeue the callback
140 * to catch a later GP.
141 */
142 rsp->cb_state = CB_PENDING;
82e8c565 143 gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
cc44ca84
ON
144 } else {
145 /*
146 * We're at least a GP after rcu_sync_exit(); eveybody will now
147 * have observed the write side critical section. Let 'em rip!.
148 */
149 rsp->cb_state = CB_IDLE;
150 rsp->gp_state = GP_IDLE;
151 }
152 spin_unlock_irqrestore(&rsp->rss_lock, flags);
153}
154
155/**
156 * rcu_sync_exit() - Allow readers back onto fast patch after grace period
157 * @rsp: Pointer to rcu_sync structure to use for synchronization
158 *
159 * This function is used by updaters who have completed, and can therefore
160 * now allow readers to make use of their fastpaths after a grace period
161 * has elapsed. After this grace period has completed, all subsequent
162 * calls to rcu_sync_is_idle() will return true, which tells readers that
163 * they can once again use their fastpaths.
164 */
165void rcu_sync_exit(struct rcu_sync *rsp)
166{
167 spin_lock_irq(&rsp->rss_lock);
168 if (!--rsp->gp_count) {
169 if (rsp->cb_state == CB_IDLE) {
170 rsp->cb_state = CB_PENDING;
82e8c565 171 gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
cc44ca84
ON
172 } else if (rsp->cb_state == CB_PENDING) {
173 rsp->cb_state = CB_REPLAY;
174 }
175 }
176 spin_unlock_irq(&rsp->rss_lock);
177}