hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / kernel / rcu / srcutiny.c
blobb46e6683f8c9ec871b26f716d7dd6a3d59c61566
1 /*
2 * Sleepable Read-Copy Update mechanism for mutual exclusion,
3 * tiny version for non-preemptible single-CPU use.
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2017
21 * Author: Paul McKenney <paulmck@us.ibm.com>
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>
32 #include <linux/rcu_node_tree.h>
33 #include "rcu_segcblist.h"
34 #include "rcu.h"
36 int rcu_scheduler_active __read_mostly;
37 static LIST_HEAD(srcu_boot_list);
38 static bool srcu_init_done;
40 static int init_srcu_struct_fields(struct srcu_struct *sp)
42 sp->srcu_lock_nesting[0] = 0;
43 sp->srcu_lock_nesting[1] = 0;
44 init_swait_queue_head(&sp->srcu_wq);
45 sp->srcu_cb_head = NULL;
46 sp->srcu_cb_tail = &sp->srcu_cb_head;
47 sp->srcu_gp_running = false;
48 sp->srcu_gp_waiting = false;
49 sp->srcu_idx = 0;
50 INIT_WORK(&sp->srcu_work, srcu_drive_gp);
51 INIT_LIST_HEAD(&sp->srcu_work.entry);
52 return 0;
55 #ifdef CONFIG_DEBUG_LOCK_ALLOC
57 int __init_srcu_struct(struct srcu_struct *sp, const char *name,
58 struct lock_class_key *key)
60 /* Don't re-initialize a lock while it is held. */
61 debug_check_no_locks_freed((void *)sp, sizeof(*sp));
62 lockdep_init_map(&sp->dep_map, name, key, 0);
63 return init_srcu_struct_fields(sp);
65 EXPORT_SYMBOL_GPL(__init_srcu_struct);
67 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
70 * init_srcu_struct - initialize a sleep-RCU structure
71 * @sp: structure to initialize.
73 * Must invoke this on a given srcu_struct before passing that srcu_struct
74 * to any other function. Each srcu_struct represents a separate domain
75 * of SRCU protection.
77 int init_srcu_struct(struct srcu_struct *sp)
79 return init_srcu_struct_fields(sp);
81 EXPORT_SYMBOL_GPL(init_srcu_struct);
83 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
86 * cleanup_srcu_struct - deconstruct a sleep-RCU structure
87 * @sp: structure to clean up.
89 * Must invoke this after you are finished using a given srcu_struct that
90 * was initialized via init_srcu_struct(), else you leak memory.
92 void _cleanup_srcu_struct(struct srcu_struct *sp, bool quiesced)
94 WARN_ON(sp->srcu_lock_nesting[0] || sp->srcu_lock_nesting[1]);
95 if (quiesced)
96 WARN_ON(work_pending(&sp->srcu_work));
97 else
98 flush_work(&sp->srcu_work);
99 WARN_ON(sp->srcu_gp_running);
100 WARN_ON(sp->srcu_gp_waiting);
101 WARN_ON(sp->srcu_cb_head);
102 WARN_ON(&sp->srcu_cb_head != sp->srcu_cb_tail);
104 EXPORT_SYMBOL_GPL(_cleanup_srcu_struct);
107 * Removes the count for the old reader from the appropriate element of
108 * the srcu_struct.
110 void __srcu_read_unlock(struct srcu_struct *sp, int idx)
112 int newval = sp->srcu_lock_nesting[idx] - 1;
114 WRITE_ONCE(sp->srcu_lock_nesting[idx], newval);
115 if (!newval && READ_ONCE(sp->srcu_gp_waiting))
116 swake_up_one(&sp->srcu_wq);
118 EXPORT_SYMBOL_GPL(__srcu_read_unlock);
121 * Workqueue handler to drive one grace period and invoke any callbacks
122 * that become ready as a result. Single-CPU and !PREEMPT operation
123 * means that we get away with murder on synchronization. ;-)
125 void srcu_drive_gp(struct work_struct *wp)
127 int idx;
128 struct rcu_head *lh;
129 struct rcu_head *rhp;
130 struct srcu_struct *sp;
132 sp = container_of(wp, struct srcu_struct, srcu_work);
133 if (sp->srcu_gp_running || !READ_ONCE(sp->srcu_cb_head))
134 return; /* Already running or nothing to do. */
136 /* Remove recently arrived callbacks and wait for readers. */
137 WRITE_ONCE(sp->srcu_gp_running, true);
138 local_irq_disable();
139 lh = sp->srcu_cb_head;
140 sp->srcu_cb_head = NULL;
141 sp->srcu_cb_tail = &sp->srcu_cb_head;
142 local_irq_enable();
143 idx = sp->srcu_idx;
144 WRITE_ONCE(sp->srcu_idx, !sp->srcu_idx);
145 WRITE_ONCE(sp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
146 swait_event_exclusive(sp->srcu_wq, !READ_ONCE(sp->srcu_lock_nesting[idx]));
147 WRITE_ONCE(sp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
149 /* Invoke the callbacks we removed above. */
150 while (lh) {
151 rhp = lh;
152 lh = lh->next;
153 local_bh_disable();
154 rhp->func(rhp);
155 local_bh_enable();
159 * Enable rescheduling, and if there are more callbacks,
160 * reschedule ourselves. This can race with a call_srcu()
161 * at interrupt level, but the ->srcu_gp_running checks will
162 * straighten that out.
164 WRITE_ONCE(sp->srcu_gp_running, false);
165 if (READ_ONCE(sp->srcu_cb_head))
166 schedule_work(&sp->srcu_work);
168 EXPORT_SYMBOL_GPL(srcu_drive_gp);
171 * Enqueue an SRCU callback on the specified srcu_struct structure,
172 * initiating grace-period processing if it is not already running.
174 void call_srcu(struct srcu_struct *sp, struct rcu_head *rhp,
175 rcu_callback_t func)
177 unsigned long flags;
179 rhp->func = func;
180 rhp->next = NULL;
181 local_irq_save(flags);
182 *sp->srcu_cb_tail = rhp;
183 sp->srcu_cb_tail = &rhp->next;
184 local_irq_restore(flags);
185 if (!READ_ONCE(sp->srcu_gp_running)) {
186 if (likely(srcu_init_done))
187 schedule_work(&sp->srcu_work);
188 else if (list_empty(&sp->srcu_work.entry))
189 list_add(&sp->srcu_work.entry, &srcu_boot_list);
192 EXPORT_SYMBOL_GPL(call_srcu);
195 * synchronize_srcu - wait for prior SRCU read-side critical-section completion
197 void synchronize_srcu(struct srcu_struct *sp)
199 struct rcu_synchronize rs;
201 init_rcu_head_on_stack(&rs.head);
202 init_completion(&rs.completion);
203 call_srcu(sp, &rs.head, wakeme_after_rcu);
204 wait_for_completion(&rs.completion);
205 destroy_rcu_head_on_stack(&rs.head);
207 EXPORT_SYMBOL_GPL(synchronize_srcu);
209 /* Lockdep diagnostics. */
210 void __init rcu_scheduler_starting(void)
212 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
216 * Queue work for srcu_struct structures with early boot callbacks.
217 * The work won't actually execute until the workqueue initialization
218 * phase that takes place after the scheduler starts.
220 void __init srcu_init(void)
222 struct srcu_struct *sp;
224 srcu_init_done = true;
225 while (!list_empty(&srcu_boot_list)) {
226 sp = list_first_entry(&srcu_boot_list,
227 struct srcu_struct, srcu_work.entry);
228 list_del_init(&sp->srcu_work.entry);
229 schedule_work(&sp->srcu_work);