| 1 | /* |
| 2 | * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition |
| 3 | * Internal non-public definitions that provide either classic |
| 4 | * or preemptible semantics. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | * |
| 20 | * Copyright (c) 2010 Linaro |
| 21 | * |
| 22 | * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com> |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kthread.h> |
| 26 | |
| 27 | /* Global control variables for rcupdate callback mechanism. */ |
| 28 | struct rcu_ctrlblk { |
| 29 | struct rcu_head *rcucblist; /* List of pending callbacks (CBs). */ |
| 30 | struct rcu_head **donetail; /* ->next pointer of last "done" CB. */ |
| 31 | struct rcu_head **curtail; /* ->next pointer of last CB. */ |
| 32 | }; |
| 33 | |
| 34 | /* Definition for rcupdate control block. */ |
| 35 | static struct rcu_ctrlblk rcu_sched_ctrlblk = { |
| 36 | .donetail = &rcu_sched_ctrlblk.rcucblist, |
| 37 | .curtail = &rcu_sched_ctrlblk.rcucblist, |
| 38 | }; |
| 39 | |
| 40 | static struct rcu_ctrlblk rcu_bh_ctrlblk = { |
| 41 | .donetail = &rcu_bh_ctrlblk.rcucblist, |
| 42 | .curtail = &rcu_bh_ctrlblk.rcucblist, |
| 43 | }; |
| 44 | |
| 45 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 46 | int rcu_scheduler_active __read_mostly; |
| 47 | EXPORT_SYMBOL_GPL(rcu_scheduler_active); |
| 48 | #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 49 | |
| 50 | #ifdef CONFIG_TINY_PREEMPT_RCU |
| 51 | |
| 52 | #include <linux/delay.h> |
| 53 | |
| 54 | /* Global control variables for preemptible RCU. */ |
| 55 | struct rcu_preempt_ctrlblk { |
| 56 | struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */ |
| 57 | struct rcu_head **nexttail; |
| 58 | /* Tasks blocked in a preemptible RCU */ |
| 59 | /* read-side critical section while an */ |
| 60 | /* preemptible-RCU grace period is in */ |
| 61 | /* progress must wait for a later grace */ |
| 62 | /* period. This pointer points to the */ |
| 63 | /* ->next pointer of the last task that */ |
| 64 | /* must wait for a later grace period, or */ |
| 65 | /* to &->rcb.rcucblist if there is no */ |
| 66 | /* such task. */ |
| 67 | struct list_head blkd_tasks; |
| 68 | /* Tasks blocked in RCU read-side critical */ |
| 69 | /* section. Tasks are placed at the head */ |
| 70 | /* of this list and age towards the tail. */ |
| 71 | struct list_head *gp_tasks; |
| 72 | /* Pointer to the first task blocking the */ |
| 73 | /* current grace period, or NULL if there */ |
| 74 | /* is no such task. */ |
| 75 | struct list_head *exp_tasks; |
| 76 | /* Pointer to first task blocking the */ |
| 77 | /* current expedited grace period, or NULL */ |
| 78 | /* if there is no such task. If there */ |
| 79 | /* is no current expedited grace period, */ |
| 80 | /* then there cannot be any such task. */ |
| 81 | #ifdef CONFIG_RCU_BOOST |
| 82 | struct list_head *boost_tasks; |
| 83 | /* Pointer to first task that needs to be */ |
| 84 | /* priority-boosted, or NULL if no priority */ |
| 85 | /* boosting is needed. If there is no */ |
| 86 | /* current or expedited grace period, there */ |
| 87 | /* can be no such task. */ |
| 88 | #endif /* #ifdef CONFIG_RCU_BOOST */ |
| 89 | u8 gpnum; /* Current grace period. */ |
| 90 | u8 gpcpu; /* Last grace period blocked by the CPU. */ |
| 91 | u8 completed; /* Last grace period completed. */ |
| 92 | /* If all three are equal, RCU is idle. */ |
| 93 | s8 boosted_this_gp; /* Has boosting already happened? */ |
| 94 | unsigned long boost_time; /* When to start boosting (jiffies) */ |
| 95 | }; |
| 96 | |
| 97 | static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = { |
| 98 | .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist, |
| 99 | .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist, |
| 100 | .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist, |
| 101 | .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks), |
| 102 | }; |
| 103 | |
| 104 | static int rcu_preempted_readers_exp(void); |
| 105 | static void rcu_report_exp_done(void); |
| 106 | |
| 107 | /* |
| 108 | * Return true if the CPU has not yet responded to the current grace period. |
| 109 | */ |
| 110 | static int rcu_cpu_blocking_cur_gp(void) |
| 111 | { |
| 112 | return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Check for a running RCU reader. Because there is only one CPU, |
| 117 | * there can be but one running RCU reader at a time. ;-) |
| 118 | */ |
| 119 | static int rcu_preempt_running_reader(void) |
| 120 | { |
| 121 | return current->rcu_read_lock_nesting; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Check for preempted RCU readers blocking any grace period. |
| 126 | * If the caller needs a reliable answer, it must disable hard irqs. |
| 127 | */ |
| 128 | static int rcu_preempt_blocked_readers_any(void) |
| 129 | { |
| 130 | return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Check for preempted RCU readers blocking the current grace period. |
| 135 | * If the caller needs a reliable answer, it must disable hard irqs. |
| 136 | */ |
| 137 | static int rcu_preempt_blocked_readers_cgp(void) |
| 138 | { |
| 139 | return rcu_preempt_ctrlblk.gp_tasks != NULL; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Return true if another preemptible-RCU grace period is needed. |
| 144 | */ |
| 145 | static int rcu_preempt_needs_another_gp(void) |
| 146 | { |
| 147 | return *rcu_preempt_ctrlblk.rcb.curtail != NULL; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Return true if a preemptible-RCU grace period is in progress. |
| 152 | * The caller must disable hardirqs. |
| 153 | */ |
| 154 | static int rcu_preempt_gp_in_progress(void) |
| 155 | { |
| 156 | return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Advance a ->blkd_tasks-list pointer to the next entry, instead |
| 161 | * returning NULL if at the end of the list. |
| 162 | */ |
| 163 | static struct list_head *rcu_next_node_entry(struct task_struct *t) |
| 164 | { |
| 165 | struct list_head *np; |
| 166 | |
| 167 | np = t->rcu_node_entry.next; |
| 168 | if (np == &rcu_preempt_ctrlblk.blkd_tasks) |
| 169 | np = NULL; |
| 170 | return np; |
| 171 | } |
| 172 | |
| 173 | #ifdef CONFIG_RCU_BOOST |
| 174 | |
| 175 | #include "rtmutex_common.h" |
| 176 | |
| 177 | /* |
| 178 | * Carry out RCU priority boosting on the task indicated by ->boost_tasks, |
| 179 | * and advance ->boost_tasks to the next task in the ->blkd_tasks list. |
| 180 | */ |
| 181 | static int rcu_boost(void) |
| 182 | { |
| 183 | unsigned long flags; |
| 184 | struct rt_mutex mtx; |
| 185 | struct list_head *np; |
| 186 | struct task_struct *t; |
| 187 | |
| 188 | if (rcu_preempt_ctrlblk.boost_tasks == NULL) |
| 189 | return 0; /* Nothing to boost. */ |
| 190 | raw_local_irq_save(flags); |
| 191 | rcu_preempt_ctrlblk.boosted_this_gp++; |
| 192 | t = container_of(rcu_preempt_ctrlblk.boost_tasks, struct task_struct, |
| 193 | rcu_node_entry); |
| 194 | np = rcu_next_node_entry(t); |
| 195 | rt_mutex_init_proxy_locked(&mtx, t); |
| 196 | t->rcu_boost_mutex = &mtx; |
| 197 | t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED; |
| 198 | raw_local_irq_restore(flags); |
| 199 | rt_mutex_lock(&mtx); |
| 200 | rt_mutex_unlock(&mtx); |
| 201 | return rcu_preempt_ctrlblk.boost_tasks != NULL; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Check to see if it is now time to start boosting RCU readers blocking |
| 206 | * the current grace period, and, if so, tell the rcu_kthread_task to |
| 207 | * start boosting them. If there is an expedited boost in progress, |
| 208 | * we wait for it to complete. |
| 209 | */ |
| 210 | static void rcu_initiate_boost(void) |
| 211 | { |
| 212 | if (rcu_preempt_ctrlblk.gp_tasks != NULL && |
| 213 | rcu_preempt_ctrlblk.boost_tasks == NULL && |
| 214 | rcu_preempt_ctrlblk.boosted_this_gp == 0 && |
| 215 | ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time)) { |
| 216 | rcu_preempt_ctrlblk.boost_tasks = rcu_preempt_ctrlblk.gp_tasks; |
| 217 | invoke_rcu_kthread(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Initiate boosting for an expedited grace period. |
| 223 | */ |
| 224 | static void rcu_initiate_expedited_boost(void) |
| 225 | { |
| 226 | unsigned long flags; |
| 227 | |
| 228 | raw_local_irq_save(flags); |
| 229 | if (!list_empty(&rcu_preempt_ctrlblk.blkd_tasks)) { |
| 230 | rcu_preempt_ctrlblk.boost_tasks = |
| 231 | rcu_preempt_ctrlblk.blkd_tasks.next; |
| 232 | rcu_preempt_ctrlblk.boosted_this_gp = -1; |
| 233 | invoke_rcu_kthread(); |
| 234 | } |
| 235 | raw_local_irq_restore(flags); |
| 236 | } |
| 237 | |
| 238 | #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000); |
| 239 | |
| 240 | /* |
| 241 | * Do priority-boost accounting for the start of a new grace period. |
| 242 | */ |
| 243 | static void rcu_preempt_boost_start_gp(void) |
| 244 | { |
| 245 | rcu_preempt_ctrlblk.boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES; |
| 246 | if (rcu_preempt_ctrlblk.boosted_this_gp > 0) |
| 247 | rcu_preempt_ctrlblk.boosted_this_gp = 0; |
| 248 | } |
| 249 | |
| 250 | #else /* #ifdef CONFIG_RCU_BOOST */ |
| 251 | |
| 252 | /* |
| 253 | * If there is no RCU priority boosting, we don't boost. |
| 254 | */ |
| 255 | static int rcu_boost(void) |
| 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * If there is no RCU priority boosting, we don't initiate boosting. |
| 262 | */ |
| 263 | static void rcu_initiate_boost(void) |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * If there is no RCU priority boosting, we don't initiate expedited boosting. |
| 269 | */ |
| 270 | static void rcu_initiate_expedited_boost(void) |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * If there is no RCU priority boosting, nothing to do at grace-period start. |
| 276 | */ |
| 277 | static void rcu_preempt_boost_start_gp(void) |
| 278 | { |
| 279 | } |
| 280 | |
| 281 | #endif /* else #ifdef CONFIG_RCU_BOOST */ |
| 282 | |
| 283 | /* |
| 284 | * Record a preemptible-RCU quiescent state for the specified CPU. Note |
| 285 | * that this just means that the task currently running on the CPU is |
| 286 | * in a quiescent state. There might be any number of tasks blocked |
| 287 | * while in an RCU read-side critical section. |
| 288 | * |
| 289 | * Unlike the other rcu_*_qs() functions, callers to this function |
| 290 | * must disable irqs in order to protect the assignment to |
| 291 | * ->rcu_read_unlock_special. |
| 292 | * |
| 293 | * Because this is a single-CPU implementation, the only way a grace |
| 294 | * period can end is if the CPU is in a quiescent state. The reason is |
| 295 | * that a blocked preemptible-RCU reader can exit its critical section |
| 296 | * only if the CPU is running it at the time. Therefore, when the |
| 297 | * last task blocking the current grace period exits its RCU read-side |
| 298 | * critical section, neither the CPU nor blocked tasks will be stopping |
| 299 | * the current grace period. (In contrast, SMP implementations |
| 300 | * might have CPUs running in RCU read-side critical sections that |
| 301 | * block later grace periods -- but this is not possible given only |
| 302 | * one CPU.) |
| 303 | */ |
| 304 | static void rcu_preempt_cpu_qs(void) |
| 305 | { |
| 306 | /* Record both CPU and task as having responded to current GP. */ |
| 307 | rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum; |
| 308 | current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS; |
| 309 | |
| 310 | /* If there is no GP then there is nothing more to do. */ |
| 311 | if (!rcu_preempt_gp_in_progress() || rcu_preempt_blocked_readers_cgp()) |
| 312 | return; |
| 313 | /* If there are blocked readers, go check up on boosting. */ |
| 314 | if (rcu_preempt_blocked_readers_cgp()) { |
| 315 | rcu_initiate_boost(); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | /* Advance callbacks. */ |
| 320 | rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum; |
| 321 | rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail; |
| 322 | rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail; |
| 323 | |
| 324 | /* If there are no blocked readers, next GP is done instantly. */ |
| 325 | if (!rcu_preempt_blocked_readers_any()) |
| 326 | rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail; |
| 327 | |
| 328 | /* If there are done callbacks, cause them to be invoked. */ |
| 329 | if (*rcu_preempt_ctrlblk.rcb.donetail != NULL) |
| 330 | invoke_rcu_kthread(); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Start a new RCU grace period if warranted. Hard irqs must be disabled. |
| 335 | */ |
| 336 | static void rcu_preempt_start_gp(void) |
| 337 | { |
| 338 | if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) { |
| 339 | |
| 340 | /* Official start of GP. */ |
| 341 | rcu_preempt_ctrlblk.gpnum++; |
| 342 | |
| 343 | /* Any blocked RCU readers block new GP. */ |
| 344 | if (rcu_preempt_blocked_readers_any()) |
| 345 | rcu_preempt_ctrlblk.gp_tasks = |
| 346 | rcu_preempt_ctrlblk.blkd_tasks.next; |
| 347 | |
| 348 | /* Set up for RCU priority boosting. */ |
| 349 | rcu_preempt_boost_start_gp(); |
| 350 | |
| 351 | /* If there is no running reader, CPU is done with GP. */ |
| 352 | if (!rcu_preempt_running_reader()) |
| 353 | rcu_preempt_cpu_qs(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * We have entered the scheduler, and the current task might soon be |
| 359 | * context-switched away from. If this task is in an RCU read-side |
| 360 | * critical section, we will no longer be able to rely on the CPU to |
| 361 | * record that fact, so we enqueue the task on the blkd_tasks list. |
| 362 | * If the task started after the current grace period began, as recorded |
| 363 | * by ->gpcpu, we enqueue at the beginning of the list. Otherwise |
| 364 | * before the element referenced by ->gp_tasks (or at the tail if |
| 365 | * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element. |
| 366 | * The task will dequeue itself when it exits the outermost enclosing |
| 367 | * RCU read-side critical section. Therefore, the current grace period |
| 368 | * cannot be permitted to complete until the ->gp_tasks pointer becomes |
| 369 | * NULL. |
| 370 | * |
| 371 | * Caller must disable preemption. |
| 372 | */ |
| 373 | void rcu_preempt_note_context_switch(void) |
| 374 | { |
| 375 | struct task_struct *t = current; |
| 376 | unsigned long flags; |
| 377 | |
| 378 | local_irq_save(flags); /* must exclude scheduler_tick(). */ |
| 379 | if (rcu_preempt_running_reader() && |
| 380 | (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) { |
| 381 | |
| 382 | /* Possibly blocking in an RCU read-side critical section. */ |
| 383 | t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED; |
| 384 | |
| 385 | /* |
| 386 | * If this CPU has already checked in, then this task |
| 387 | * will hold up the next grace period rather than the |
| 388 | * current grace period. Queue the task accordingly. |
| 389 | * If the task is queued for the current grace period |
| 390 | * (i.e., this CPU has not yet passed through a quiescent |
| 391 | * state for the current grace period), then as long |
| 392 | * as that task remains queued, the current grace period |
| 393 | * cannot end. |
| 394 | */ |
| 395 | list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks); |
| 396 | if (rcu_cpu_blocking_cur_gp()) |
| 397 | rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Either we were not in an RCU read-side critical section to |
| 402 | * begin with, or we have now recorded that critical section |
| 403 | * globally. Either way, we can now note a quiescent state |
| 404 | * for this CPU. Again, if we were in an RCU read-side critical |
| 405 | * section, and if that critical section was blocking the current |
| 406 | * grace period, then the fact that the task has been enqueued |
| 407 | * means that current grace period continues to be blocked. |
| 408 | */ |
| 409 | rcu_preempt_cpu_qs(); |
| 410 | local_irq_restore(flags); |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Tiny-preemptible RCU implementation for rcu_read_lock(). |
| 415 | * Just increment ->rcu_read_lock_nesting, shared state will be updated |
| 416 | * if we block. |
| 417 | */ |
| 418 | void __rcu_read_lock(void) |
| 419 | { |
| 420 | current->rcu_read_lock_nesting++; |
| 421 | barrier(); /* needed if we ever invoke rcu_read_lock in rcutiny.c */ |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(__rcu_read_lock); |
| 424 | |
| 425 | /* |
| 426 | * Handle special cases during rcu_read_unlock(), such as needing to |
| 427 | * notify RCU core processing or task having blocked during the RCU |
| 428 | * read-side critical section. |
| 429 | */ |
| 430 | static void rcu_read_unlock_special(struct task_struct *t) |
| 431 | { |
| 432 | int empty; |
| 433 | int empty_exp; |
| 434 | unsigned long flags; |
| 435 | struct list_head *np; |
| 436 | int special; |
| 437 | |
| 438 | /* |
| 439 | * NMI handlers cannot block and cannot safely manipulate state. |
| 440 | * They therefore cannot possibly be special, so just leave. |
| 441 | */ |
| 442 | if (in_nmi()) |
| 443 | return; |
| 444 | |
| 445 | local_irq_save(flags); |
| 446 | |
| 447 | /* |
| 448 | * If RCU core is waiting for this CPU to exit critical section, |
| 449 | * let it know that we have done so. |
| 450 | */ |
| 451 | special = t->rcu_read_unlock_special; |
| 452 | if (special & RCU_READ_UNLOCK_NEED_QS) |
| 453 | rcu_preempt_cpu_qs(); |
| 454 | |
| 455 | /* Hardware IRQ handlers cannot block. */ |
| 456 | if (in_irq()) { |
| 457 | local_irq_restore(flags); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | /* Clean up if blocked during RCU read-side critical section. */ |
| 462 | if (special & RCU_READ_UNLOCK_BLOCKED) { |
| 463 | t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED; |
| 464 | |
| 465 | /* |
| 466 | * Remove this task from the ->blkd_tasks list and adjust |
| 467 | * any pointers that might have been referencing it. |
| 468 | */ |
| 469 | empty = !rcu_preempt_blocked_readers_cgp(); |
| 470 | empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL; |
| 471 | np = rcu_next_node_entry(t); |
| 472 | list_del(&t->rcu_node_entry); |
| 473 | if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks) |
| 474 | rcu_preempt_ctrlblk.gp_tasks = np; |
| 475 | if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks) |
| 476 | rcu_preempt_ctrlblk.exp_tasks = np; |
| 477 | #ifdef CONFIG_RCU_BOOST |
| 478 | if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks) |
| 479 | rcu_preempt_ctrlblk.boost_tasks = np; |
| 480 | #endif /* #ifdef CONFIG_RCU_BOOST */ |
| 481 | INIT_LIST_HEAD(&t->rcu_node_entry); |
| 482 | |
| 483 | /* |
| 484 | * If this was the last task on the current list, and if |
| 485 | * we aren't waiting on the CPU, report the quiescent state |
| 486 | * and start a new grace period if needed. |
| 487 | */ |
| 488 | if (!empty && !rcu_preempt_blocked_readers_cgp()) { |
| 489 | rcu_preempt_cpu_qs(); |
| 490 | rcu_preempt_start_gp(); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * If this was the last task on the expedited lists, |
| 495 | * then we need wake up the waiting task. |
| 496 | */ |
| 497 | if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL) |
| 498 | rcu_report_exp_done(); |
| 499 | } |
| 500 | #ifdef CONFIG_RCU_BOOST |
| 501 | /* Unboost self if was boosted. */ |
| 502 | if (special & RCU_READ_UNLOCK_BOOSTED) { |
| 503 | t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED; |
| 504 | rt_mutex_unlock(t->rcu_boost_mutex); |
| 505 | t->rcu_boost_mutex = NULL; |
| 506 | } |
| 507 | #endif /* #ifdef CONFIG_RCU_BOOST */ |
| 508 | local_irq_restore(flags); |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Tiny-preemptible RCU implementation for rcu_read_unlock(). |
| 513 | * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost |
| 514 | * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then |
| 515 | * invoke rcu_read_unlock_special() to clean up after a context switch |
| 516 | * in an RCU read-side critical section and other special cases. |
| 517 | */ |
| 518 | void __rcu_read_unlock(void) |
| 519 | { |
| 520 | struct task_struct *t = current; |
| 521 | |
| 522 | barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */ |
| 523 | --t->rcu_read_lock_nesting; |
| 524 | barrier(); /* decrement before load of ->rcu_read_unlock_special */ |
| 525 | if (t->rcu_read_lock_nesting == 0 && |
| 526 | unlikely(ACCESS_ONCE(t->rcu_read_unlock_special))) |
| 527 | rcu_read_unlock_special(t); |
| 528 | #ifdef CONFIG_PROVE_LOCKING |
| 529 | WARN_ON_ONCE(t->rcu_read_lock_nesting < 0); |
| 530 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(__rcu_read_unlock); |
| 533 | |
| 534 | /* |
| 535 | * Check for a quiescent state from the current CPU. When a task blocks, |
| 536 | * the task is recorded in the rcu_preempt_ctrlblk structure, which is |
| 537 | * checked elsewhere. This is called from the scheduling-clock interrupt. |
| 538 | * |
| 539 | * Caller must disable hard irqs. |
| 540 | */ |
| 541 | static void rcu_preempt_check_callbacks(void) |
| 542 | { |
| 543 | struct task_struct *t = current; |
| 544 | |
| 545 | if (rcu_preempt_gp_in_progress() && |
| 546 | (!rcu_preempt_running_reader() || |
| 547 | !rcu_cpu_blocking_cur_gp())) |
| 548 | rcu_preempt_cpu_qs(); |
| 549 | if (&rcu_preempt_ctrlblk.rcb.rcucblist != |
| 550 | rcu_preempt_ctrlblk.rcb.donetail) |
| 551 | invoke_rcu_kthread(); |
| 552 | if (rcu_preempt_gp_in_progress() && |
| 553 | rcu_cpu_blocking_cur_gp() && |
| 554 | rcu_preempt_running_reader()) |
| 555 | t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * TINY_PREEMPT_RCU has an extra callback-list tail pointer to |
| 560 | * update, so this is invoked from rcu_process_callbacks() to |
| 561 | * handle that case. Of course, it is invoked for all flavors of |
| 562 | * RCU, but RCU callbacks can appear only on one of the lists, and |
| 563 | * neither ->nexttail nor ->donetail can possibly be NULL, so there |
| 564 | * is no need for an explicit check. |
| 565 | */ |
| 566 | static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp) |
| 567 | { |
| 568 | if (rcu_preempt_ctrlblk.nexttail == rcp->donetail) |
| 569 | rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Process callbacks for preemptible RCU. |
| 574 | */ |
| 575 | static void rcu_preempt_process_callbacks(void) |
| 576 | { |
| 577 | rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Queue a preemptible -RCU callback for invocation after a grace period. |
| 582 | */ |
| 583 | void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu)) |
| 584 | { |
| 585 | unsigned long flags; |
| 586 | |
| 587 | debug_rcu_head_queue(head); |
| 588 | head->func = func; |
| 589 | head->next = NULL; |
| 590 | |
| 591 | local_irq_save(flags); |
| 592 | *rcu_preempt_ctrlblk.nexttail = head; |
| 593 | rcu_preempt_ctrlblk.nexttail = &head->next; |
| 594 | rcu_preempt_start_gp(); /* checks to see if GP needed. */ |
| 595 | local_irq_restore(flags); |
| 596 | } |
| 597 | EXPORT_SYMBOL_GPL(call_rcu); |
| 598 | |
| 599 | void rcu_barrier(void) |
| 600 | { |
| 601 | struct rcu_synchronize rcu; |
| 602 | |
| 603 | init_rcu_head_on_stack(&rcu.head); |
| 604 | init_completion(&rcu.completion); |
| 605 | /* Will wake me after RCU finished. */ |
| 606 | call_rcu(&rcu.head, wakeme_after_rcu); |
| 607 | /* Wait for it. */ |
| 608 | wait_for_completion(&rcu.completion); |
| 609 | destroy_rcu_head_on_stack(&rcu.head); |
| 610 | } |
| 611 | EXPORT_SYMBOL_GPL(rcu_barrier); |
| 612 | |
| 613 | /* |
| 614 | * synchronize_rcu - wait until a grace period has elapsed. |
| 615 | * |
| 616 | * Control will return to the caller some time after a full grace |
| 617 | * period has elapsed, in other words after all currently executing RCU |
| 618 | * read-side critical sections have completed. RCU read-side critical |
| 619 | * sections are delimited by rcu_read_lock() and rcu_read_unlock(), |
| 620 | * and may be nested. |
| 621 | */ |
| 622 | void synchronize_rcu(void) |
| 623 | { |
| 624 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 625 | if (!rcu_scheduler_active) |
| 626 | return; |
| 627 | #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 628 | |
| 629 | WARN_ON_ONCE(rcu_preempt_running_reader()); |
| 630 | if (!rcu_preempt_blocked_readers_any()) |
| 631 | return; |
| 632 | |
| 633 | /* Once we get past the fastpath checks, same code as rcu_barrier(). */ |
| 634 | rcu_barrier(); |
| 635 | } |
| 636 | EXPORT_SYMBOL_GPL(synchronize_rcu); |
| 637 | |
| 638 | static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq); |
| 639 | static unsigned long sync_rcu_preempt_exp_count; |
| 640 | static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex); |
| 641 | |
| 642 | /* |
| 643 | * Return non-zero if there are any tasks in RCU read-side critical |
| 644 | * sections blocking the current preemptible-RCU expedited grace period. |
| 645 | * If there is no preemptible-RCU expedited grace period currently in |
| 646 | * progress, returns zero unconditionally. |
| 647 | */ |
| 648 | static int rcu_preempted_readers_exp(void) |
| 649 | { |
| 650 | return rcu_preempt_ctrlblk.exp_tasks != NULL; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Report the exit from RCU read-side critical section for the last task |
| 655 | * that queued itself during or before the current expedited preemptible-RCU |
| 656 | * grace period. |
| 657 | */ |
| 658 | static void rcu_report_exp_done(void) |
| 659 | { |
| 660 | wake_up(&sync_rcu_preempt_exp_wq); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Wait for an rcu-preempt grace period, but expedite it. The basic idea |
| 665 | * is to rely in the fact that there is but one CPU, and that it is |
| 666 | * illegal for a task to invoke synchronize_rcu_expedited() while in a |
| 667 | * preemptible-RCU read-side critical section. Therefore, any such |
| 668 | * critical sections must correspond to blocked tasks, which must therefore |
| 669 | * be on the ->blkd_tasks list. So just record the current head of the |
| 670 | * list in the ->exp_tasks pointer, and wait for all tasks including and |
| 671 | * after the task pointed to by ->exp_tasks to drain. |
| 672 | */ |
| 673 | void synchronize_rcu_expedited(void) |
| 674 | { |
| 675 | unsigned long flags; |
| 676 | struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk; |
| 677 | unsigned long snap; |
| 678 | |
| 679 | barrier(); /* ensure prior action seen before grace period. */ |
| 680 | |
| 681 | WARN_ON_ONCE(rcu_preempt_running_reader()); |
| 682 | |
| 683 | /* |
| 684 | * Acquire lock so that there is only one preemptible RCU grace |
| 685 | * period in flight. Of course, if someone does the expedited |
| 686 | * grace period for us while we are acquiring the lock, just leave. |
| 687 | */ |
| 688 | snap = sync_rcu_preempt_exp_count + 1; |
| 689 | mutex_lock(&sync_rcu_preempt_exp_mutex); |
| 690 | if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count)) |
| 691 | goto unlock_mb_ret; /* Others did our work for us. */ |
| 692 | |
| 693 | local_irq_save(flags); |
| 694 | |
| 695 | /* |
| 696 | * All RCU readers have to already be on blkd_tasks because |
| 697 | * we cannot legally be executing in an RCU read-side critical |
| 698 | * section. |
| 699 | */ |
| 700 | |
| 701 | /* Snapshot current head of ->blkd_tasks list. */ |
| 702 | rpcp->exp_tasks = rpcp->blkd_tasks.next; |
| 703 | if (rpcp->exp_tasks == &rpcp->blkd_tasks) |
| 704 | rpcp->exp_tasks = NULL; |
| 705 | local_irq_restore(flags); |
| 706 | |
| 707 | /* Wait for tail of ->blkd_tasks list to drain. */ |
| 708 | if (rcu_preempted_readers_exp()) |
| 709 | rcu_initiate_expedited_boost(); |
| 710 | wait_event(sync_rcu_preempt_exp_wq, |
| 711 | !rcu_preempted_readers_exp()); |
| 712 | |
| 713 | /* Clean up and exit. */ |
| 714 | barrier(); /* ensure expedited GP seen before counter increment. */ |
| 715 | sync_rcu_preempt_exp_count++; |
| 716 | unlock_mb_ret: |
| 717 | mutex_unlock(&sync_rcu_preempt_exp_mutex); |
| 718 | barrier(); /* ensure subsequent action seen after grace period. */ |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(synchronize_rcu_expedited); |
| 721 | |
| 722 | /* |
| 723 | * Does preemptible RCU need the CPU to stay out of dynticks mode? |
| 724 | */ |
| 725 | int rcu_preempt_needs_cpu(void) |
| 726 | { |
| 727 | if (!rcu_preempt_running_reader()) |
| 728 | rcu_preempt_cpu_qs(); |
| 729 | return rcu_preempt_ctrlblk.rcb.rcucblist != NULL; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Check for a task exiting while in a preemptible -RCU read-side |
| 734 | * critical section, clean up if so. No need to issue warnings, |
| 735 | * as debug_check_no_locks_held() already does this if lockdep |
| 736 | * is enabled. |
| 737 | */ |
| 738 | void exit_rcu(void) |
| 739 | { |
| 740 | struct task_struct *t = current; |
| 741 | |
| 742 | if (t->rcu_read_lock_nesting == 0) |
| 743 | return; |
| 744 | t->rcu_read_lock_nesting = 1; |
| 745 | rcu_read_unlock(); |
| 746 | } |
| 747 | |
| 748 | #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */ |
| 749 | |
| 750 | /* |
| 751 | * Because preemptible RCU does not exist, it is never necessary to |
| 752 | * boost preempted RCU readers. |
| 753 | */ |
| 754 | static int rcu_boost(void) |
| 755 | { |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * Because preemptible RCU does not exist, it never has any callbacks |
| 761 | * to check. |
| 762 | */ |
| 763 | static void rcu_preempt_check_callbacks(void) |
| 764 | { |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * Because preemptible RCU does not exist, it never has any callbacks |
| 769 | * to remove. |
| 770 | */ |
| 771 | static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp) |
| 772 | { |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Because preemptible RCU does not exist, it never has any callbacks |
| 777 | * to process. |
| 778 | */ |
| 779 | static void rcu_preempt_process_callbacks(void) |
| 780 | { |
| 781 | } |
| 782 | |
| 783 | #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */ |
| 784 | |
| 785 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 786 | #include <linux/kernel_stat.h> |
| 787 | |
| 788 | /* |
| 789 | * During boot, we forgive RCU lockdep issues. After this function is |
| 790 | * invoked, we start taking RCU lockdep issues seriously. |
| 791 | */ |
| 792 | void __init rcu_scheduler_starting(void) |
| 793 | { |
| 794 | WARN_ON(nr_context_switches() > 0); |
| 795 | rcu_scheduler_active = 1; |
| 796 | } |
| 797 | |
| 798 | #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ |
| 799 | |
| 800 | #ifdef CONFIG_RCU_BOOST |
| 801 | #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO |
| 802 | #else /* #ifdef CONFIG_RCU_BOOST */ |
| 803 | #define RCU_BOOST_PRIO 1 |
| 804 | #endif /* #else #ifdef CONFIG_RCU_BOOST */ |