blob: 5d4a39a6505a43aa9c1c4b11f629be7922113535 [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. McKenneyaacb5d92018-10-28 10:32:51 -070079void _cleanup_srcu_struct(struct srcu_struct *ssp, bool quiesced)
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. McKenneyf7194ac2018-04-05 17:19:17 -070082 if (quiesced)
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070083 WARN_ON(work_pending(&ssp->srcu_work));
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -070084 else
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -070085 flush_work(&ssp->srcu_work);
86 WARN_ON(ssp->srcu_gp_running);
87 WARN_ON(ssp->srcu_gp_waiting);
88 WARN_ON(ssp->srcu_cb_head);
89 WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -070090}
Paul E. McKenneyf7194ac2018-04-05 17:19:17 -070091EXPORT_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. McKenneyaacb5d92018-10-28 10:32:51 -070099 int newval = 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);
102 if (!newval && READ_ONCE(ssp->srcu_gp_waiting))
103 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
109 * that become ready as a result. Single-CPU and !PREEMPT operation
110 * 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);
120 if (ssp->srcu_gp_running || !READ_ONCE(ssp->srcu_cb_head))
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. McKenneyaacb5d92018-10-28 10:32:51 -0700130 idx = ssp->srcu_idx;
131 WRITE_ONCE(ssp->srcu_idx, !ssp->srcu_idx);
132 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. McKenneyd8be8172017-03-25 09:59:38 -0700135
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700136 /* Invoke the callbacks we removed above. */
137 while (lh) {
138 rhp = lh;
139 lh = lh->next;
140 local_bh_disable();
141 rhp->func(rhp);
142 local_bh_enable();
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700143 }
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700144
145 /*
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700146 * Enable rescheduling, and if there are more callbacks,
147 * reschedule ourselves. This can race with a call_srcu()
148 * at interrupt level, but the ->srcu_gp_running checks will
149 * straighten that out.
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700150 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700151 WRITE_ONCE(ssp->srcu_gp_running, false);
152 if (READ_ONCE(ssp->srcu_cb_head))
153 schedule_work(&ssp->srcu_work);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700154}
155EXPORT_SYMBOL_GPL(srcu_drive_gp);
156
157/*
158 * Enqueue an SRCU callback on the specified srcu_struct structure,
159 * initiating grace-period processing if it is not already running.
160 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700161void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700162 rcu_callback_t func)
163{
164 unsigned long flags;
165
Paul E. McKenney2464dd92017-05-04 14:29:16 -0700166 rhp->func = func;
167 rhp->next = NULL;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700168 local_irq_save(flags);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700169 *ssp->srcu_cb_tail = rhp;
170 ssp->srcu_cb_tail = &rhp->next;
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700171 local_irq_restore(flags);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700172 if (!READ_ONCE(ssp->srcu_gp_running)) {
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700173 if (likely(srcu_init_done))
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700174 schedule_work(&ssp->srcu_work);
175 else if (list_empty(&ssp->srcu_work.entry))
176 list_add(&ssp->srcu_work.entry, &srcu_boot_list);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700177 }
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700178}
179EXPORT_SYMBOL_GPL(call_srcu);
180
181/*
182 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
183 */
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700184void synchronize_srcu(struct srcu_struct *ssp)
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700185{
186 struct rcu_synchronize rs;
187
188 init_rcu_head_on_stack(&rs.head);
189 init_completion(&rs.completion);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700190 call_srcu(ssp, &rs.head, wakeme_after_rcu);
Paul E. McKenneyd8be8172017-03-25 09:59:38 -0700191 wait_for_completion(&rs.completion);
192 destroy_rcu_head_on_stack(&rs.head);
193}
194EXPORT_SYMBOL_GPL(synchronize_srcu);
Paul E. McKenney825c5bd2017-05-26 16:16:40 -0700195
196/* Lockdep diagnostics. */
197void __init rcu_scheduler_starting(void)
198{
199 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
200}
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700201
202/*
203 * Queue work for srcu_struct structures with early boot callbacks.
204 * The work won't actually execute until the workqueue initialization
205 * phase that takes place after the scheduler starts.
206 */
207void __init srcu_init(void)
208{
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700209 struct srcu_struct *ssp;
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700210
211 srcu_init_done = true;
212 while (!list_empty(&srcu_boot_list)) {
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700213 ssp = list_first_entry(&srcu_boot_list,
Paul E. McKenney4e6ea4e2018-08-14 14:41:49 -0700214 struct srcu_struct, srcu_work.entry);
Paul E. McKenneyaacb5d92018-10-28 10:32:51 -0700215 list_del_init(&ssp->srcu_work.entry);
216 schedule_work(&ssp->srcu_work);
Paul E. McKenneye0fcba92018-08-14 08:45:54 -0700217 }
218}