Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Sleepable Read-Copy Update mechanism for mutual exclusion,
  4 *	tiny version for non-preemptible single-CPU use.
  5 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 * Copyright (C) IBM Corporation, 2017
  7 *
  8 * Author: Paul McKenney <paulmck@linux.ibm.com>
  9 */
 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>
 20#include "rcu_segcblist.h"
 21#include "rcu.h"
 22
 23int rcu_scheduler_active __read_mostly;
 24static LIST_HEAD(srcu_boot_list);
 25static bool srcu_init_done;
 26
 27static int init_srcu_struct_fields(struct srcu_struct *ssp)
 28{
 29	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	ssp->srcu_idx_max = 0;
 38	INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
 39	INIT_LIST_HEAD(&ssp->srcu_work.entry);
 40	return 0;
 41}
 42
 43#ifdef CONFIG_DEBUG_LOCK_ALLOC
 44
 45int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
 46		       struct lock_class_key *key)
 47{
 48	/* Don't re-initialize a lock while it is held. */
 49	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);
 52}
 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
 59 * @ssp: structure to initialize.
 60 *
 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 */
 65int init_srcu_struct(struct srcu_struct *ssp)
 66{
 67	return init_srcu_struct_fields(ssp);
 68}
 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
 75 * @ssp: structure to clean up.
 76 *
 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 */
 80void cleanup_srcu_struct(struct srcu_struct *ssp)
 81{
 82	WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
 83	flush_work(&ssp->srcu_work);
 84	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);
 88	WARN_ON(ssp->srcu_idx != ssp->srcu_idx_max);
 89	WARN_ON(ssp->srcu_idx & 0x1);
 90}
 91EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
 92
 93/*
 94 * Removes the count for the old reader from the appropriate element of
 95 * the srcu_struct.
 96 */
 97void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
 98{
 99	int newval;
100
101	preempt_disable();  // Needed for PREEMPT_AUTO
102	newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) - 1;
103	WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
104	preempt_enable();
105	if (!newval && READ_ONCE(ssp->srcu_gp_waiting) && in_task())
106		swake_up_one(&ssp->srcu_wq);
107}
108EXPORT_SYMBOL_GPL(__srcu_read_unlock);
109
110/*
111 * Workqueue handler to drive one grace period and invoke any callbacks
112 * that become ready as a result.  Single-CPU and !PREEMPTION operation
113 * means that we get away with murder on synchronization.  ;-)
114 */
115void srcu_drive_gp(struct work_struct *wp)
116{
117	int idx;
118	struct rcu_head *lh;
119	struct rcu_head *rhp;
120	struct srcu_struct *ssp;
121
122	ssp = container_of(wp, struct srcu_struct, srcu_work);
123	preempt_disable();  // Needed for PREEMPT_AUTO
124	if (ssp->srcu_gp_running || ULONG_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) {
125		preempt_enable();
126		return; /* Already running or nothing to do. */
127	}
128
129	/* Remove recently arrived callbacks and wait for readers. */
130	WRITE_ONCE(ssp->srcu_gp_running, true);
131	local_irq_disable();
132	lh = ssp->srcu_cb_head;
133	ssp->srcu_cb_head = NULL;
134	ssp->srcu_cb_tail = &ssp->srcu_cb_head;
135	local_irq_enable();
136	idx = (ssp->srcu_idx & 0x2) / 2;
137	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
138	WRITE_ONCE(ssp->srcu_gp_waiting, true);  /* srcu_read_unlock() wakes! */
139	preempt_enable();
140	swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
141	preempt_disable();  // Needed for PREEMPT_AUTO
142	WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
143	WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1);
144	preempt_enable();
145
146	/* Invoke the callbacks we removed above. */
147	while (lh) {
148		rhp = lh;
149		lh = lh->next;
150		debug_rcu_head_callback(rhp);
151		local_bh_disable();
152		rhp->func(rhp);
153		local_bh_enable();
154	}
155
156	/*
157	 * Enable rescheduling, and if there are more callbacks,
158	 * reschedule ourselves.  This can race with a call_srcu()
159	 * at interrupt level, but the ->srcu_gp_running checks will
160	 * straighten that out.
161	 */
162	preempt_disable();  // Needed for PREEMPT_AUTO
163	WRITE_ONCE(ssp->srcu_gp_running, false);
164	idx = ULONG_CMP_LT(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max));
165	preempt_enable();
166	if (idx)
167		schedule_work(&ssp->srcu_work);
168}
169EXPORT_SYMBOL_GPL(srcu_drive_gp);
170
171static void srcu_gp_start_if_needed(struct srcu_struct *ssp)
172{
173	unsigned long cookie;
174
175	preempt_disable();  // Needed for PREEMPT_AUTO
176	cookie = get_state_synchronize_srcu(ssp);
177	if (ULONG_CMP_GE(READ_ONCE(ssp->srcu_idx_max), cookie)) {
178		preempt_enable();
179		return;
180	}
181	WRITE_ONCE(ssp->srcu_idx_max, cookie);
182	if (!READ_ONCE(ssp->srcu_gp_running)) {
183		if (likely(srcu_init_done))
184			schedule_work(&ssp->srcu_work);
185		else if (list_empty(&ssp->srcu_work.entry))
186			list_add(&ssp->srcu_work.entry, &srcu_boot_list);
187	}
188	preempt_enable();
189}
190
191/*
192 * Enqueue an SRCU callback on the specified srcu_struct structure,
193 * initiating grace-period processing if it is not already running.
194 */
195void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
196	       rcu_callback_t func)
197{
198	unsigned long flags;
199
200	rhp->func = func;
201	rhp->next = NULL;
202	preempt_disable();  // Needed for PREEMPT_AUTO
203	local_irq_save(flags);
204	*ssp->srcu_cb_tail = rhp;
205	ssp->srcu_cb_tail = &rhp->next;
206	local_irq_restore(flags);
207	srcu_gp_start_if_needed(ssp);
208	preempt_enable();
209}
210EXPORT_SYMBOL_GPL(call_srcu);
211
212/*
213 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
214 */
215void synchronize_srcu(struct srcu_struct *ssp)
216{
217	struct rcu_synchronize rs;
218
219	srcu_lock_sync(&ssp->dep_map);
220
221	RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
222			lock_is_held(&rcu_bh_lock_map) ||
223			lock_is_held(&rcu_lock_map) ||
224			lock_is_held(&rcu_sched_lock_map),
225			"Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
226
227	if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
228		return;
229
230	might_sleep();
231	init_rcu_head_on_stack(&rs.head);
232	init_completion(&rs.completion);
233	call_srcu(ssp, &rs.head, wakeme_after_rcu);
234	wait_for_completion(&rs.completion);
235	destroy_rcu_head_on_stack(&rs.head);
236}
237EXPORT_SYMBOL_GPL(synchronize_srcu);
238
239/*
240 * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
241 */
242unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
243{
244	unsigned long ret;
245
246	barrier();
247	ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1;
248	barrier();
249	return ret;
250}
251EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
252
253/*
254 * start_poll_synchronize_srcu - Provide cookie and start grace period
255 *
256 * The difference between this and get_state_synchronize_srcu() is that
257 * this function ensures that the poll_state_synchronize_srcu() will
258 * eventually return the value true.
259 */
260unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
261{
262	unsigned long ret;
263
264	preempt_disable();  // Needed for PREEMPT_AUTO
265	ret = get_state_synchronize_srcu(ssp);
266	srcu_gp_start_if_needed(ssp);
267	preempt_enable();
268	return ret;
269}
270EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
271
272/*
273 * poll_state_synchronize_srcu - Has cookie's grace period ended?
274 */
275bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
276{
277	unsigned long cur_s = READ_ONCE(ssp->srcu_idx);
278
279	barrier();
280	return cookie == SRCU_GET_STATE_COMPLETED ||
281	       ULONG_CMP_GE(cur_s, cookie) || ULONG_CMP_LT(cur_s, cookie - 3);
282}
283EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
284
285/* Lockdep diagnostics.  */
286void __init rcu_scheduler_starting(void)
287{
288	rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
289}
290
291/*
292 * Queue work for srcu_struct structures with early boot callbacks.
293 * The work won't actually execute until the workqueue initialization
294 * phase that takes place after the scheduler starts.
295 */
296void __init srcu_init(void)
297{
298	struct srcu_struct *ssp;
299
300	srcu_init_done = true;
301	while (!list_empty(&srcu_boot_list)) {
302		ssp = list_first_entry(&srcu_boot_list,
303				      struct srcu_struct, srcu_work.entry);
304		list_del_init(&ssp->srcu_work.entry);
305		schedule_work(&ssp->srcu_work);
306	}
307}
v4.17
 
  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_segcblist.h"
 34#include "rcu.h"
 35
 36int rcu_scheduler_active __read_mostly;
 
 
 37
 38static int init_srcu_struct_fields(struct srcu_struct *sp)
 39{
 40	sp->srcu_lock_nesting[0] = 0;
 41	sp->srcu_lock_nesting[1] = 0;
 42	init_swait_queue_head(&sp->srcu_wq);
 43	sp->srcu_cb_head = NULL;
 44	sp->srcu_cb_tail = &sp->srcu_cb_head;
 45	sp->srcu_gp_running = false;
 46	sp->srcu_gp_waiting = false;
 47	sp->srcu_idx = 0;
 48	INIT_WORK(&sp->srcu_work, srcu_drive_gp);
 
 
 49	return 0;
 50}
 51
 52#ifdef CONFIG_DEBUG_LOCK_ALLOC
 53
 54int __init_srcu_struct(struct srcu_struct *sp, const char *name,
 55		       struct lock_class_key *key)
 56{
 57	/* Don't re-initialize a lock while it is held. */
 58	debug_check_no_locks_freed((void *)sp, sizeof(*sp));
 59	lockdep_init_map(&sp->dep_map, name, key, 0);
 60	return init_srcu_struct_fields(sp);
 61}
 62EXPORT_SYMBOL_GPL(__init_srcu_struct);
 63
 64#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 65
 66/*
 67 * init_srcu_struct - initialize a sleep-RCU structure
 68 * @sp: structure to initialize.
 69 *
 70 * Must invoke this on a given srcu_struct before passing that srcu_struct
 71 * to any other function.  Each srcu_struct represents a separate domain
 72 * of SRCU protection.
 73 */
 74int init_srcu_struct(struct srcu_struct *sp)
 75{
 76	return init_srcu_struct_fields(sp);
 77}
 78EXPORT_SYMBOL_GPL(init_srcu_struct);
 79
 80#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 81
 82/*
 83 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
 84 * @sp: structure to clean up.
 85 *
 86 * Must invoke this after you are finished using a given srcu_struct that
 87 * was initialized via init_srcu_struct(), else you leak memory.
 88 */
 89void cleanup_srcu_struct(struct srcu_struct *sp)
 90{
 91	WARN_ON(sp->srcu_lock_nesting[0] || sp->srcu_lock_nesting[1]);
 92	flush_work(&sp->srcu_work);
 93	WARN_ON(sp->srcu_gp_running);
 94	WARN_ON(sp->srcu_gp_waiting);
 95	WARN_ON(sp->srcu_cb_head);
 96	WARN_ON(&sp->srcu_cb_head != sp->srcu_cb_tail);
 
 
 97}
 98EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
 99
100/*
101 * Removes the count for the old reader from the appropriate element of
102 * the srcu_struct.
103 */
104void __srcu_read_unlock(struct srcu_struct *sp, int idx)
105{
106	int newval = sp->srcu_lock_nesting[idx] - 1;
107
108	WRITE_ONCE(sp->srcu_lock_nesting[idx], newval);
109	if (!newval && READ_ONCE(sp->srcu_gp_waiting))
110		swake_up(&sp->srcu_wq);
 
 
 
111}
112EXPORT_SYMBOL_GPL(__srcu_read_unlock);
113
114/*
115 * Workqueue handler to drive one grace period and invoke any callbacks
116 * that become ready as a result.  Single-CPU and !PREEMPT operation
117 * means that we get away with murder on synchronization.  ;-)
118 */
119void srcu_drive_gp(struct work_struct *wp)
120{
121	int idx;
122	struct rcu_head *lh;
123	struct rcu_head *rhp;
124	struct srcu_struct *sp;
125
126	sp = container_of(wp, struct srcu_struct, srcu_work);
127	if (sp->srcu_gp_running || !READ_ONCE(sp->srcu_cb_head))
 
 
128		return; /* Already running or nothing to do. */
 
129
130	/* Remove recently arrived callbacks and wait for readers. */
131	WRITE_ONCE(sp->srcu_gp_running, true);
132	local_irq_disable();
133	lh = sp->srcu_cb_head;
134	sp->srcu_cb_head = NULL;
135	sp->srcu_cb_tail = &sp->srcu_cb_head;
136	local_irq_enable();
137	idx = sp->srcu_idx;
138	WRITE_ONCE(sp->srcu_idx, !sp->srcu_idx);
139	WRITE_ONCE(sp->srcu_gp_waiting, true);  /* srcu_read_unlock() wakes! */
140	swait_event(sp->srcu_wq, !READ_ONCE(sp->srcu_lock_nesting[idx]));
141	WRITE_ONCE(sp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
 
 
 
 
142
143	/* Invoke the callbacks we removed above. */
144	while (lh) {
145		rhp = lh;
146		lh = lh->next;
 
147		local_bh_disable();
148		rhp->func(rhp);
149		local_bh_enable();
150	}
151
152	/*
153	 * Enable rescheduling, and if there are more callbacks,
154	 * reschedule ourselves.  This can race with a call_srcu()
155	 * at interrupt level, but the ->srcu_gp_running checks will
156	 * straighten that out.
157	 */
158	WRITE_ONCE(sp->srcu_gp_running, false);
159	if (READ_ONCE(sp->srcu_cb_head))
160		schedule_work(&sp->srcu_work);
 
 
 
161}
162EXPORT_SYMBOL_GPL(srcu_drive_gp);
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164/*
165 * Enqueue an SRCU callback on the specified srcu_struct structure,
166 * initiating grace-period processing if it is not already running.
167 */
168void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
169	       rcu_callback_t func)
170{
171	unsigned long flags;
172
173	rhp->func = func;
174	rhp->next = NULL;
 
175	local_irq_save(flags);
176	*sp->srcu_cb_tail = rhp;
177	sp->srcu_cb_tail = &rhp->next;
178	local_irq_restore(flags);
179	if (!READ_ONCE(sp->srcu_gp_running))
180		schedule_work(&sp->srcu_work);
181}
182EXPORT_SYMBOL_GPL(call_srcu);
183
184/*
185 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
186 */
187void synchronize_srcu(struct srcu_struct *sp)
188{
189	struct rcu_synchronize rs;
190
 
 
 
 
 
 
 
 
 
 
 
 
191	init_rcu_head_on_stack(&rs.head);
192	init_completion(&rs.completion);
193	call_srcu(sp, &rs.head, wakeme_after_rcu);
194	wait_for_completion(&rs.completion);
195	destroy_rcu_head_on_stack(&rs.head);
196}
197EXPORT_SYMBOL_GPL(synchronize_srcu);
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199/* Lockdep diagnostics.  */
200void __init rcu_scheduler_starting(void)
201{
202	rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203}