]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - kernel/rcu/srcutiny.c
srcu: Create a tiny SRCU
[mirror_ubuntu-focal-kernel.git] / kernel / rcu / srcutiny.c
1 /*
2 * Sleepable Read-Copy Update mechanism for mutual exclusion,
3 * tiny version for non-preemptible single-CPU use.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, you can access it online at
17 * http://www.gnu.org/licenses/gpl-2.0.html.
18 *
19 * Copyright (C) IBM Corporation, 2017
20 *
21 * Author: Paul McKenney <paulmck@us.ibm.com>
22 */
23
24 #include <linux/export.h>
25 #include <linux/mutex.h>
26 #include <linux/preempt.h>
27 #include <linux/rcupdate_wait.h>
28 #include <linux/sched.h>
29 #include <linux/delay.h>
30 #include <linux/srcu.h>
31
32 #include <linux/rcu_node_tree.h>
33 #include "rcu.h"
34
35 static int init_srcu_struct_fields(struct srcu_struct *sp)
36 {
37 sp->srcu_lock_nesting[0] = 0;
38 sp->srcu_lock_nesting[1] = 0;
39 init_swait_queue_head(&sp->srcu_wq);
40 sp->srcu_gp_seq = 0;
41 rcu_segcblist_init(&sp->srcu_cblist);
42 sp->srcu_gp_running = false;
43 sp->srcu_gp_waiting = false;
44 sp->srcu_idx = 0;
45 INIT_WORK(&sp->srcu_work, srcu_drive_gp);
46 return 0;
47 }
48
49 #ifdef CONFIG_DEBUG_LOCK_ALLOC
50
51 int __init_srcu_struct(struct srcu_struct *sp, const char *name,
52 struct lock_class_key *key)
53 {
54 /* Don't re-initialize a lock while it is held. */
55 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
56 lockdep_init_map(&sp->dep_map, name, key, 0);
57 return init_srcu_struct_fields(sp);
58 }
59 EXPORT_SYMBOL_GPL(__init_srcu_struct);
60
61 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
62
63 /*
64 * init_srcu_struct - initialize a sleep-RCU structure
65 * @sp: structure to initialize.
66 *
67 * Must invoke this on a given srcu_struct before passing that srcu_struct
68 * to any other function. Each srcu_struct represents a separate domain
69 * of SRCU protection.
70 */
71 int init_srcu_struct(struct srcu_struct *sp)
72 {
73 return init_srcu_struct_fields(sp);
74 }
75 EXPORT_SYMBOL_GPL(init_srcu_struct);
76
77 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
78
79 /*
80 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
81 * @sp: structure to clean up.
82 *
83 * Must invoke this after you are finished using a given srcu_struct that
84 * was initialized via init_srcu_struct(), else you leak memory.
85 */
86 void cleanup_srcu_struct(struct srcu_struct *sp)
87 {
88 WARN_ON(sp->srcu_lock_nesting[0] || sp->srcu_lock_nesting[1]);
89 flush_work(&sp->srcu_work);
90 WARN_ON(rcu_seq_state(sp->srcu_gp_seq));
91 WARN_ON(sp->srcu_gp_running);
92 WARN_ON(sp->srcu_gp_waiting);
93 WARN_ON(!rcu_segcblist_empty(&sp->srcu_cblist));
94 }
95 EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
96
97 /*
98 * Counts the new reader in the appropriate per-CPU element of the
99 * srcu_struct. Must be called from process context.
100 * Returns an index that must be passed to the matching srcu_read_unlock().
101 */
102 int __srcu_read_lock(struct srcu_struct *sp)
103 {
104 int idx;
105
106 idx = READ_ONCE(sp->srcu_idx);
107 WRITE_ONCE(sp->srcu_lock_nesting[idx], sp->srcu_lock_nesting[idx] + 1);
108 return idx;
109 }
110 EXPORT_SYMBOL_GPL(__srcu_read_lock);
111
112 /*
113 * Removes the count for the old reader from the appropriate element of
114 * the srcu_struct. Must be called from process context.
115 */
116 void __srcu_read_unlock(struct srcu_struct *sp, int idx)
117 {
118 int newval = sp->srcu_lock_nesting[idx] - 1;
119
120 WRITE_ONCE(sp->srcu_lock_nesting[idx], newval);
121 if (!newval && READ_ONCE(sp->srcu_gp_waiting))
122 swake_up(&sp->srcu_wq);
123 }
124 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
125
126 /*
127 * Workqueue handler to drive one grace period and invoke any callbacks
128 * that become ready as a result. Single-CPU and !PREEMPT operation
129 * means that we get away with murder on synchronization. ;-)
130 */
131 void srcu_drive_gp(struct work_struct *wp)
132 {
133 int idx;
134 struct rcu_cblist ready_cbs;
135 struct srcu_struct *sp;
136 struct rcu_head *rhp;
137
138 sp = container_of(wp, struct srcu_struct, srcu_work);
139 if (sp->srcu_gp_running || rcu_segcblist_empty(&sp->srcu_cblist))
140 return; /* Already running or nothing to do. */
141
142 /* Tag recently arrived callbacks and wait for readers. */
143 WRITE_ONCE(sp->srcu_gp_running, true);
144 rcu_segcblist_accelerate(&sp->srcu_cblist,
145 rcu_seq_snap(&sp->srcu_gp_seq));
146 rcu_seq_start(&sp->srcu_gp_seq);
147 idx = sp->srcu_idx;
148 WRITE_ONCE(sp->srcu_idx, !sp->srcu_idx);
149 WRITE_ONCE(sp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
150 swait_event(sp->srcu_wq, !READ_ONCE(sp->srcu_lock_nesting[idx]));
151 WRITE_ONCE(sp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
152 rcu_seq_end(&sp->srcu_gp_seq);
153
154 /* Update callback list based on GP, and invoke ready callbacks. */
155 rcu_segcblist_advance(&sp->srcu_cblist,
156 rcu_seq_current(&sp->srcu_gp_seq));
157 if (rcu_segcblist_ready_cbs(&sp->srcu_cblist)) {
158 rcu_cblist_init(&ready_cbs);
159 local_irq_disable();
160 rcu_segcblist_extract_done_cbs(&sp->srcu_cblist, &ready_cbs);
161 local_irq_enable();
162 rhp = rcu_cblist_dequeue(&ready_cbs);
163 for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
164 local_bh_disable();
165 rhp->func(rhp);
166 local_bh_enable();
167 }
168 local_irq_disable();
169 rcu_segcblist_insert_count(&sp->srcu_cblist, &ready_cbs);
170 local_irq_enable();
171 }
172 WRITE_ONCE(sp->srcu_gp_running, false);
173
174 /*
175 * If more callbacks, reschedule ourselves. This can race with
176 * a call_srcu() at interrupt level, but the ->srcu_gp_running
177 * checks will straighten that out.
178 */
179 if (!rcu_segcblist_empty(&sp->srcu_cblist))
180 schedule_work(&sp->srcu_work);
181 }
182 EXPORT_SYMBOL_GPL(srcu_drive_gp);
183
184 /*
185 * Enqueue an SRCU callback on the specified srcu_struct structure,
186 * initiating grace-period processing if it is not already running.
187 */
188 void call_srcu(struct srcu_struct *sp, struct rcu_head *head,
189 rcu_callback_t func)
190 {
191 unsigned long flags;
192
193 head->func = func;
194 local_irq_save(flags);
195 rcu_segcblist_enqueue(&sp->srcu_cblist, head, false);
196 local_irq_restore(flags);
197 if (!READ_ONCE(sp->srcu_gp_running))
198 schedule_work(&sp->srcu_work);
199 }
200 EXPORT_SYMBOL_GPL(call_srcu);
201
202 /*
203 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
204 */
205 void synchronize_srcu(struct srcu_struct *sp)
206 {
207 struct rcu_synchronize rs;
208
209 init_rcu_head_on_stack(&rs.head);
210 init_completion(&rs.completion);
211 call_srcu(sp, &rs.head, wakeme_after_rcu);
212 wait_for_completion(&rs.completion);
213 destroy_rcu_head_on_stack(&rs.head);
214 }
215 EXPORT_SYMBOL_GPL(synchronize_srcu);