Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / kernel / rcu / sync.c
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
26 #ifdef CONFIG_PROVE_RCU
27 #define __INIT_HELD(func)       .held = func,
28 #else
29 #define __INIT_HELD(func)
30 #endif
31
32 static const struct {
33         void (*sync)(void);
34         void (*call)(struct rcu_head *, void (*)(struct rcu_head *));
35         void (*wait)(void);
36 #ifdef CONFIG_PROVE_RCU
37         int  (*held)(void);
38 #endif
39 } gp_ops[] = {
40         [RCU_SYNC] = {
41                 .sync = synchronize_rcu,
42                 .call = call_rcu,
43                 .wait = rcu_barrier,
44                 __INIT_HELD(rcu_read_lock_held)
45         },
46         [RCU_SCHED_SYNC] = {
47                 .sync = synchronize_sched,
48                 .call = call_rcu_sched,
49                 .wait = rcu_barrier_sched,
50                 __INIT_HELD(rcu_read_lock_sched_held)
51         },
52         [RCU_BH_SYNC] = {
53                 .sync = synchronize_rcu_bh,
54                 .call = call_rcu_bh,
55                 .wait = rcu_barrier_bh,
56                 __INIT_HELD(rcu_read_lock_bh_held)
57         },
58 };
59
60 enum { GP_IDLE = 0, GP_PENDING, GP_PASSED };
61 enum { CB_IDLE = 0, CB_PENDING, CB_REPLAY };
62
63 #define rss_lock        gp_wait.lock
64
65 #ifdef CONFIG_PROVE_RCU
66 void rcu_sync_lockdep_assert(struct rcu_sync *rsp)
67 {
68         RCU_LOCKDEP_WARN(!gp_ops[rsp->gp_type].held(),
69                          "suspicious rcu_sync_is_idle() usage");
70 }
71 EXPORT_SYMBOL_GPL(rcu_sync_lockdep_assert);
72 #endif
73
74 /**
75  * rcu_sync_init() - Initialize an rcu_sync structure
76  * @rsp: Pointer to rcu_sync structure to be initialized
77  * @type: Flavor of RCU with which to synchronize rcu_sync structure
78  */
79 void rcu_sync_init(struct rcu_sync *rsp, enum rcu_sync_type type)
80 {
81         memset(rsp, 0, sizeof(*rsp));
82         init_waitqueue_head(&rsp->gp_wait);
83         rsp->gp_type = type;
84 }
85
86 /**
87  * Must be called after rcu_sync_init() and before first use.
88  *
89  * Ensures rcu_sync_is_idle() returns false and rcu_sync_{enter,exit}()
90  * pairs turn into NO-OPs.
91  */
92 void rcu_sync_enter_start(struct rcu_sync *rsp)
93 {
94         rsp->gp_count++;
95         rsp->gp_state = GP_PASSED;
96 }
97
98 /**
99  * rcu_sync_enter() - Force readers onto slowpath
100  * @rsp: Pointer to rcu_sync structure to use for synchronization
101  *
102  * This function is used by updaters who need readers to make use of
103  * a slowpath during the update.  After this function returns, all
104  * subsequent calls to rcu_sync_is_idle() will return false, which
105  * tells readers to stay off their fastpaths.  A later call to
106  * rcu_sync_exit() re-enables reader slowpaths.
107  *
108  * When called in isolation, rcu_sync_enter() must wait for a grace
109  * period, however, closely spaced calls to rcu_sync_enter() can
110  * optimize away the grace-period wait via a state machine implemented
111  * by rcu_sync_enter(), rcu_sync_exit(), and rcu_sync_func().
112  */
113 void rcu_sync_enter(struct rcu_sync *rsp)
114 {
115         bool need_wait, need_sync;
116
117         spin_lock_irq(&rsp->rss_lock);
118         need_wait = rsp->gp_count++;
119         need_sync = rsp->gp_state == GP_IDLE;
120         if (need_sync)
121                 rsp->gp_state = GP_PENDING;
122         spin_unlock_irq(&rsp->rss_lock);
123
124         BUG_ON(need_wait && need_sync);
125
126         if (need_sync) {
127                 gp_ops[rsp->gp_type].sync();
128                 rsp->gp_state = GP_PASSED;
129                 wake_up_all(&rsp->gp_wait);
130         } else if (need_wait) {
131                 wait_event(rsp->gp_wait, rsp->gp_state == GP_PASSED);
132         } else {
133                 /*
134                  * Possible when there's a pending CB from a rcu_sync_exit().
135                  * Nobody has yet been allowed the 'fast' path and thus we can
136                  * avoid doing any sync(). The callback will get 'dropped'.
137                  */
138                 BUG_ON(rsp->gp_state != GP_PASSED);
139         }
140 }
141
142 /**
143  * rcu_sync_func() - Callback function managing reader access to fastpath
144  * @rsp: Pointer to rcu_sync structure to use for synchronization
145  *
146  * This function is passed to one of the call_rcu() functions by
147  * rcu_sync_exit(), so that it is invoked after a grace period following the
148  * that invocation of rcu_sync_exit().  It takes action based on events that
149  * have taken place in the meantime, so that closely spaced rcu_sync_enter()
150  * and rcu_sync_exit() pairs need not wait for a grace period.
151  *
152  * If another rcu_sync_enter() is invoked before the grace period
153  * ended, reset state to allow the next rcu_sync_exit() to let the
154  * readers back onto their fastpaths (after a grace period).  If both
155  * another rcu_sync_enter() and its matching rcu_sync_exit() are invoked
156  * before the grace period ended, re-invoke call_rcu() on behalf of that
157  * rcu_sync_exit().  Otherwise, set all state back to idle so that readers
158  * can again use their fastpaths.
159  */
160 static void rcu_sync_func(struct rcu_head *rcu)
161 {
162         struct rcu_sync *rsp = container_of(rcu, struct rcu_sync, cb_head);
163         unsigned long flags;
164
165         BUG_ON(rsp->gp_state != GP_PASSED);
166         BUG_ON(rsp->cb_state == CB_IDLE);
167
168         spin_lock_irqsave(&rsp->rss_lock, flags);
169         if (rsp->gp_count) {
170                 /*
171                  * A new rcu_sync_begin() has happened; drop the callback.
172                  */
173                 rsp->cb_state = CB_IDLE;
174         } else if (rsp->cb_state == CB_REPLAY) {
175                 /*
176                  * A new rcu_sync_exit() has happened; requeue the callback
177                  * to catch a later GP.
178                  */
179                 rsp->cb_state = CB_PENDING;
180                 gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
181         } else {
182                 /*
183                  * We're at least a GP after rcu_sync_exit(); eveybody will now
184                  * have observed the write side critical section. Let 'em rip!.
185                  */
186                 rsp->cb_state = CB_IDLE;
187                 rsp->gp_state = GP_IDLE;
188         }
189         spin_unlock_irqrestore(&rsp->rss_lock, flags);
190 }
191
192 /**
193  * rcu_sync_exit() - Allow readers back onto fast patch after grace period
194  * @rsp: Pointer to rcu_sync structure to use for synchronization
195  *
196  * This function is used by updaters who have completed, and can therefore
197  * now allow readers to make use of their fastpaths after a grace period
198  * has elapsed.  After this grace period has completed, all subsequent
199  * calls to rcu_sync_is_idle() will return true, which tells readers that
200  * they can once again use their fastpaths.
201  */
202 void rcu_sync_exit(struct rcu_sync *rsp)
203 {
204         spin_lock_irq(&rsp->rss_lock);
205         if (!--rsp->gp_count) {
206                 if (rsp->cb_state == CB_IDLE) {
207                         rsp->cb_state = CB_PENDING;
208                         gp_ops[rsp->gp_type].call(&rsp->cb_head, rcu_sync_func);
209                 } else if (rsp->cb_state == CB_PENDING) {
210                         rsp->cb_state = CB_REPLAY;
211                 }
212         }
213         spin_unlock_irq(&rsp->rss_lock);
214 }
215
216 /**
217  * rcu_sync_dtor() - Clean up an rcu_sync structure
218  * @rsp: Pointer to rcu_sync structure to be cleaned up
219  */
220 void rcu_sync_dtor(struct rcu_sync *rsp)
221 {
222         int cb_state;
223
224         BUG_ON(rsp->gp_count);
225
226         spin_lock_irq(&rsp->rss_lock);
227         if (rsp->cb_state == CB_REPLAY)
228                 rsp->cb_state = CB_PENDING;
229         cb_state = rsp->cb_state;
230         spin_unlock_irq(&rsp->rss_lock);
231
232         if (cb_state != CB_IDLE) {
233                 gp_ops[rsp->gp_type].wait();
234                 BUG_ON(rsp->cb_state != CB_IDLE);
235         }
236 }