blob: 92c002d65482872dc536efd16ff2317518a0a34b [file] [log] [blame]
Paul E. McKenneye7ee1502019-01-17 10:18:16 -08001// SPDX-License-Identifier: GPL-2.0+
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07002/*
3 * Sleepable Read-Copy Update mechanism for mutual exclusion,
4 * tiny version for non-preemptible single-CPU use.
5 *
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07006 * Copyright (C) IBM Corporation, 2017
7 *
Paul E. McKenneye7ee1502019-01-17 10:18:16 -08008 * Author: Paul McKenney <paulmck@linux.ibm.com>
Paul E. McKenneyd8be8172017-03-25 09:59:38 -07009 */
10
11#include <linux/export.h>
12#include <linux/mutex.h>
13#include <linux/preempt.h>
14#include <linux/rcupdate_wait.h>
15#include <linux/sched.h>
16#include <linux/delay.h>
17#include <linux/srcu.h>
18
19#include <linux/rcu_node_tree.h>
Ingo Molnar45753c52017-05-02 10:31:18 +020020#include "rcu_segcblist.h"
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070021#include "rcu.h"
22
Paul E. McKenney825c5bd2017-05-26 16:16:40 -070023int rcu_scheduler_active __read_mostly;
Paul E. McKenneye0fcba92018-08-14 08:45:54 -070024static LIST_HEAD(srcu_boot_list);
25static bool srcu_init_done;
Paul E. McKenney825c5bd2017-05-26 16:16:40 -070026
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070027static int init_srcu_struct_fields(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070028{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070029 ssp->srcu_lock_nesting[0] = 0;
30 ssp->srcu_lock_nesting[1] = 0;
31 init_swait_queue_head(&ssp->srcu_wq);
32 ssp->srcu_cb_head = NULL;
33 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
34 ssp->srcu_gp_running = false;
35 ssp->srcu_gp_waiting = false;
36 ssp->srcu_idx = 0;
Paul E. McKenney8b5bd672020-11-13 12:54:48 -080037 ssp->srcu_idx_max = 0;
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070038 INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
39 INIT_LIST_HEAD(&ssp->srcu_work.entry);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070040 return 0;
41}
42
43#ifdef CONFIG_DEBUG_LOCK_ALLOC
44
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070045int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070046 struct lock_class_key *key)
47{
48 /* Don't re-initialize a lock while it is held. */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070049 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
50 lockdep_init_map(&ssp->dep_map, name, key, 0);
51 return init_srcu_struct_fields(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070052}
53EXPORT_SYMBOL_GPL(__init_srcu_struct);
54
55#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
56
57/*
58 * init_srcu_struct - initialize a sleep-RCU structure
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070059 * @ssp: structure to initialize.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070060 *
61 * Must invoke this on a given srcu_struct before passing that srcu_struct
62 * to any other function. Each srcu_struct represents a separate domain
63 * of SRCU protection.
64 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070065int init_srcu_struct(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070066{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070067 return init_srcu_struct_fields(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070068}
69EXPORT_SYMBOL_GPL(init_srcu_struct);
70
71#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
72
73/*
74 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070075 * @ssp: structure to clean up.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070076 *
77 * Must invoke this after you are finished using a given srcu_struct that
78 * was initialized via init_srcu_struct(), else you leak memory.
79 */
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080080void cleanup_srcu_struct(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070081{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070082 WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080083 flush_work(&ssp->srcu_work);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070084 WARN_ON(ssp->srcu_gp_running);
85 WARN_ON(ssp->srcu_gp_waiting);
86 WARN_ON(ssp->srcu_cb_head);
87 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
Paul E. McKenney8b5bd672020-11-13 12:54:48 -080088 WARN_ON(ssp->srcu_idx != ssp->srcu_idx_max);
89 WARN_ON(ssp->srcu_idx & 0x1);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070090}
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080091EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070092
93/*
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070094 * Removes the count for the old reader from the appropriate element of
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +020095 * the srcu_struct.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070096 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070097void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070098{
Paul E. McKenney65bfdd32021-06-02 16:31:38 -070099 int newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700100
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700101 WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
Paul E. McKenney1f8da402021-09-23 10:07:14 -0700102 if (!newval && READ_ONCE(ssp->srcu_gp_waiting) && in_task())
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700103 swake_up_one(&ssp->srcu_wq);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700104}
105EXPORT_SYMBOL_GPL(__srcu_read_unlock);
106
107/*
108 * Workqueue handler to drive one grace period and invoke any callbacks
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +0200109 * that become ready as a result. Single-CPU and !PREEMPTION operation
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700110 * means that we get away with murder on synchronization. ;-)
111 */
112void srcu_drive_gp(struct work_struct *wp)
113{
114 int idx;
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700115 struct rcu_head *lh;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700116 struct rcu_head *rhp;
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700117 struct srcu_struct *ssp;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700118
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700119 ssp = container_of(wp, struct srcu_struct, srcu_work);
Paul E. McKenney8b5bd672020-11-13 12:54:48 -0800120 if (ssp->srcu_gp_running || USHORT_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max)))
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700121 return; /* Already running or nothing to do. */
122
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700123 /* Remove recently arrived callbacks and wait for readers. */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700124 WRITE_ONCE(ssp->srcu_gp_running, true);
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700125 local_irq_disable();
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700126 lh = ssp->srcu_cb_head;
127 ssp->srcu_cb_head = NULL;
128 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700129 local_irq_enable();
Paul E. McKenney74612a02020-11-12 16:34:09 -0800130 idx = (ssp->srcu_idx & 0x2) / 2;
131 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700132 WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
133 swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
134 WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
Paul E. McKenney74612a02020-11-12 16:34:09 -0800135 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700136
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700137 /* Invoke the callbacks we removed above. */
138 while (lh) {
139 rhp = lh;
140 lh = lh->next;
141 local_bh_disable();
142 rhp->func(rhp);
143 local_bh_enable();
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700144 }
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700145
146 /*
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700147 * Enable rescheduling, and if there are more callbacks,
148 * reschedule ourselves. This can race with a call_srcu()
149 * at interrupt level, but the ->srcu_gp_running checks will
150 * straighten that out.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700151 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700152 WRITE_ONCE(ssp->srcu_gp_running, false);
Paul E. McKenney8b5bd672020-11-13 12:54:48 -0800153 if (USHORT_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max)))
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700154 schedule_work(&ssp->srcu_work);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700155}
156EXPORT_SYMBOL_GPL(srcu_drive_gp);
157
Paul E. McKenney1a893c72020-11-13 09:37:39 -0800158static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
159{
Paul E. McKenney8b5bd672020-11-13 12:54:48 -0800160 unsigned short cookie;
161
162 cookie = get_state_synchronize_srcu(ssp);
163 if (USHORT_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie))
164 return;
165 WRITE_ONCE(ssp->srcu_idx_max, cookie);
Paul E. McKenney1a893c72020-11-13 09:37:39 -0800166 if (!READ_ONCE(ssp->srcu_gp_running)) {
167 if (likely(srcu_init_done))
168 schedule_work(&ssp->srcu_work);
169 else if (list_empty(&ssp->srcu_work.entry))
170 list_add(&ssp->srcu_work.entry, &srcu_boot_list);
171 }
172}
173
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700174/*
175 * Enqueue an SRCU callback on the specified srcu_struct structure,
176 * initiating grace-period processing if it is not already running.
177 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700178void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700179 rcu_callback_t func)
180{
181 unsigned long flags;
182
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700183 rhp->func = func;
184 rhp->next = NULL;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700185 local_irq_save(flags);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700186 *ssp->srcu_cb_tail = rhp;
187 ssp->srcu_cb_tail = &rhp->next;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700188 local_irq_restore(flags);
Paul E. McKenney1a893c72020-11-13 09:37:39 -0800189 srcu_gp_start_if_needed(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700190}
191EXPORT_SYMBOL_GPL(call_srcu);
192
193/*
194 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
195 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700196void synchronize_srcu(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700197{
198 struct rcu_synchronize rs;
199
200 init_rcu_head_on_stack(&rs.head);
201 init_completion(&rs.completion);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700202 call_srcu(ssp, &rs.head, wakeme_after_rcu);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700203 wait_for_completion(&rs.completion);
204 destroy_rcu_head_on_stack(&rs.head);
205}
206EXPORT_SYMBOL_GPL(synchronize_srcu);
Paul E. McKenney825c5bd2017-05-26 16:16:40 -0700207
Paul E. McKenney8b5bd672020-11-13 12:54:48 -0800208/*
209 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
210 */
211unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
212{
213 unsigned long ret;
214
215 barrier();
216 ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1;
217 barrier();
218 return ret & USHRT_MAX;
219}
220EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
221
222/*
223 * start_poll_synchronize_srcu - Provide cookie and start grace period
224 *
225 * The difference between this and get_state_synchronize_srcu() is that
226 * this function ensures that the poll_state_synchronize_srcu() will
227 * eventually return the value true.
228 */
229unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
230{
231 unsigned long ret = get_state_synchronize_srcu(ssp);
232
233 srcu_gp_start_if_needed(ssp);
234 return ret;
235}
236EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
237
238/*
239 * poll_state_synchronize_srcu - Has cookie's grace period ended?
240 */
241bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
242{
243 bool ret = USHORT_CMP_GE(READ_ONCE(ssp->srcu_idx), cookie);
244
245 barrier();
246 return ret;
247}
248EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
249
Paul E. McKenney825c5bd2017-05-26 16:16:40 -0700250/* Lockdep diagnostics. */
251void __init rcu_scheduler_starting(void)
252{
253 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
254}
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700255
256/*
257 * Queue work for srcu_struct structures with early boot callbacks.
258 * The work won't actually execute until the workqueue initialization
259 * phase that takes place after the scheduler starts.
260 */
261void __init srcu_init(void)
262{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700263 struct srcu_struct *ssp;
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700264
265 srcu_init_done = true;
266 while (!list_empty(&srcu_boot_list)) {
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700267 ssp = list_first_entry(&srcu_boot_list,
Paul E. McKenney4e6ea4e2018-08-14 14:41:49 -0700268 struct srcu_struct, srcu_work.entry);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700269 list_del_init(&ssp->srcu_work.entry);
270 schedule_work(&ssp->srcu_work);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700271 }
272}