blob: 3bac1db85a85b1018d0081721479b89ad648c670 [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;
37 INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
38 INIT_LIST_HEAD(&ssp->srcu_work.entry);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070039 return 0;
40}
41
42#ifdef CONFIG_DEBUG_LOCK_ALLOC
43
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070044int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070045 struct lock_class_key *key)
46{
47 /* Don't re-initialize a lock while it is held. */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070048 debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
49 lockdep_init_map(&ssp->dep_map, name, key, 0);
50 return init_srcu_struct_fields(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070051}
52EXPORT_SYMBOL_GPL(__init_srcu_struct);
53
54#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
55
56/*
57 * init_srcu_struct - initialize a sleep-RCU structure
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070058 * @ssp: structure to initialize.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070059 *
60 * Must invoke this on a given srcu_struct before passing that srcu_struct
61 * to any other function. Each srcu_struct represents a separate domain
62 * of SRCU protection.
63 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070064int init_srcu_struct(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070065{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070066 return init_srcu_struct_fields(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070067}
68EXPORT_SYMBOL_GPL(init_srcu_struct);
69
70#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
71
72/*
73 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070074 * @ssp: structure to clean up.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070075 *
76 * Must invoke this after you are finished using a given srcu_struct that
77 * was initialized via init_srcu_struct(), else you leak memory.
78 */
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080079void cleanup_srcu_struct(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070080{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070081 WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080082 flush_work(&ssp->srcu_work);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070083 WARN_ON(ssp->srcu_gp_running);
84 WARN_ON(ssp->srcu_gp_waiting);
85 WARN_ON(ssp->srcu_cb_head);
86 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070087}
Paul E. McKenneyf5ad3992019-02-13 13:54:37 -080088EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070089
90/*
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070091 * Removes the count for the old reader from the appropriate element of
Paolo Bonzinicdf7abc2017-05-31 14:03:10 +020092 * the srcu_struct.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070093 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070094void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070095{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070096 int newval = ssp->srcu_lock_nesting[idx] - 1;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070097
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070098 WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
99 if (!newval && READ_ONCE(ssp->srcu_gp_waiting))
100 swake_up_one(&ssp->srcu_wq);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700101}
102EXPORT_SYMBOL_GPL(__srcu_read_unlock);
103
104/*
105 * Workqueue handler to drive one grace period and invoke any callbacks
Sebastian Andrzej Siewior90326f02019-10-15 21:18:14 +0200106 * that become ready as a result. Single-CPU and !PREEMPTION operation
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700107 * means that we get away with murder on synchronization. ;-)
108 */
109void srcu_drive_gp(struct work_struct *wp)
110{
111 int idx;
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700112 struct rcu_head *lh;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700113 struct rcu_head *rhp;
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700114 struct srcu_struct *ssp;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700115
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700116 ssp = container_of(wp, struct srcu_struct, srcu_work);
117 if (ssp->srcu_gp_running || !READ_ONCE(ssp->srcu_cb_head))
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700118 return; /* Already running or nothing to do. */
119
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700120 /* Remove recently arrived callbacks and wait for readers. */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700121 WRITE_ONCE(ssp->srcu_gp_running, true);
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700122 local_irq_disable();
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700123 lh = ssp->srcu_cb_head;
124 ssp->srcu_cb_head = NULL;
125 ssp->srcu_cb_tail = &ssp->srcu_cb_head;
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700126 local_irq_enable();
Paul E. McKenney74612a02020-11-12 16:34:09 -0800127 idx = (ssp->srcu_idx & 0x2) / 2;
128 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700129 WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
130 swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
131 WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
Paul E. McKenney74612a02020-11-12 16:34:09 -0800132 WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700133
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700134 /* Invoke the callbacks we removed above. */
135 while (lh) {
136 rhp = lh;
137 lh = lh->next;
138 local_bh_disable();
139 rhp->func(rhp);
140 local_bh_enable();
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700141 }
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700142
143 /*
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700144 * Enable rescheduling, and if there are more callbacks,
145 * reschedule ourselves. This can race with a call_srcu()
146 * at interrupt level, but the ->srcu_gp_running checks will
147 * straighten that out.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700148 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700149 WRITE_ONCE(ssp->srcu_gp_running, false);
150 if (READ_ONCE(ssp->srcu_cb_head))
151 schedule_work(&ssp->srcu_work);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700152}
153EXPORT_SYMBOL_GPL(srcu_drive_gp);
154
Paul E. McKenney1a893c72020-11-13 09:37:39 -0800155static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
156{
157 if (!READ_ONCE(ssp->srcu_gp_running)) {
158 if (likely(srcu_init_done))
159 schedule_work(&ssp->srcu_work);
160 else if (list_empty(&ssp->srcu_work.entry))
161 list_add(&ssp->srcu_work.entry, &srcu_boot_list);
162 }
163}
164
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700165/*
166 * Enqueue an SRCU callback on the specified srcu_struct structure,
167 * initiating grace-period processing if it is not already running.
168 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700169void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700170 rcu_callback_t func)
171{
172 unsigned long flags;
173
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700174 rhp->func = func;
175 rhp->next = NULL;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700176 local_irq_save(flags);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700177 *ssp->srcu_cb_tail = rhp;
178 ssp->srcu_cb_tail = &rhp->next;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700179 local_irq_restore(flags);
Paul E. McKenney1a893c72020-11-13 09:37:39 -0800180 srcu_gp_start_if_needed(ssp);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700181}
182EXPORT_SYMBOL_GPL(call_srcu);
183
184/*
185 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
186 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700187void synchronize_srcu(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700188{
189 struct rcu_synchronize rs;
190
191 init_rcu_head_on_stack(&rs.head);
192 init_completion(&rs.completion);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700193 call_srcu(ssp, &rs.head, wakeme_after_rcu);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700194 wait_for_completion(&rs.completion);
195 destroy_rcu_head_on_stack(&rs.head);
196}
197EXPORT_SYMBOL_GPL(synchronize_srcu);
Paul E. McKenney825c5bd2017-05-26 16:16:40 -0700198
199/* Lockdep diagnostics. */
200void __init rcu_scheduler_starting(void)
201{
202 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
203}
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700204
205/*
206 * Queue work for srcu_struct structures with early boot callbacks.
207 * The work won't actually execute until the workqueue initialization
208 * phase that takes place after the scheduler starts.
209 */
210void __init srcu_init(void)
211{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700212 struct srcu_struct *ssp;
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700213
214 srcu_init_done = true;
215 while (!list_empty(&srcu_boot_list)) {
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700216 ssp = list_first_entry(&srcu_boot_list,
Paul E. McKenney4e6ea4e2018-08-14 14:41:49 -0700217 struct srcu_struct, srcu_work.entry);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700218 list_del_init(&ssp->srcu_work.entry);
219 schedule_work(&ssp->srcu_work);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700220 }
221}