rcutiny_plugin.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. #include <linux/kthread.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/seq_file.h>
  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. RCU_TRACE(long qlen); /* Number of pending CBs. */
  33. RCU_TRACE(char *name); /* Name of RCU type. */
  34. };
  35. /* Definition for rcupdate control block. */
  36. static struct rcu_ctrlblk rcu_sched_ctrlblk = {
  37. .donetail = &rcu_sched_ctrlblk.rcucblist,
  38. .curtail = &rcu_sched_ctrlblk.rcucblist,
  39. RCU_TRACE(.name = "rcu_sched")
  40. };
  41. static struct rcu_ctrlblk rcu_bh_ctrlblk = {
  42. .donetail = &rcu_bh_ctrlblk.rcucblist,
  43. .curtail = &rcu_bh_ctrlblk.rcucblist,
  44. RCU_TRACE(.name = "rcu_bh")
  45. };
  46. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  47. int rcu_scheduler_active __read_mostly;
  48. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  49. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  50. #ifdef CONFIG_TINY_PREEMPT_RCU
  51. #include <linux/delay.h>
  52. /* Global control variables for preemptible RCU. */
  53. struct rcu_preempt_ctrlblk {
  54. struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */
  55. struct rcu_head **nexttail;
  56. /* Tasks blocked in a preemptible RCU */
  57. /* read-side critical section while an */
  58. /* preemptible-RCU grace period is in */
  59. /* progress must wait for a later grace */
  60. /* period. This pointer points to the */
  61. /* ->next pointer of the last task that */
  62. /* must wait for a later grace period, or */
  63. /* to &->rcb.rcucblist if there is no */
  64. /* such task. */
  65. struct list_head blkd_tasks;
  66. /* Tasks blocked in RCU read-side critical */
  67. /* section. Tasks are placed at the head */
  68. /* of this list and age towards the tail. */
  69. struct list_head *gp_tasks;
  70. /* Pointer to the first task blocking the */
  71. /* current grace period, or NULL if there */
  72. /* is no such task. */
  73. struct list_head *exp_tasks;
  74. /* Pointer to first task blocking the */
  75. /* current expedited grace period, or NULL */
  76. /* if there is no such task. If there */
  77. /* is no current expedited grace period, */
  78. /* then there cannot be any such task. */
  79. #ifdef CONFIG_RCU_BOOST
  80. struct list_head *boost_tasks;
  81. /* Pointer to first task that needs to be */
  82. /* priority-boosted, or NULL if no priority */
  83. /* boosting is needed. If there is no */
  84. /* current or expedited grace period, there */
  85. /* can be no such task. */
  86. #endif /* #ifdef CONFIG_RCU_BOOST */
  87. u8 gpnum; /* Current grace period. */
  88. u8 gpcpu; /* Last grace period blocked by the CPU. */
  89. u8 completed; /* Last grace period completed. */
  90. /* If all three are equal, RCU is idle. */
  91. #ifdef CONFIG_RCU_BOOST
  92. unsigned long boost_time; /* When to start boosting (jiffies) */
  93. #endif /* #ifdef CONFIG_RCU_BOOST */
  94. #ifdef CONFIG_RCU_TRACE
  95. unsigned long n_grace_periods;
  96. #ifdef CONFIG_RCU_BOOST
  97. unsigned long n_tasks_boosted;
  98. /* Total number of tasks boosted. */
  99. unsigned long n_exp_boosts;
  100. /* Number of tasks boosted for expedited GP. */
  101. unsigned long n_normal_boosts;
  102. /* Number of tasks boosted for normal GP. */
  103. unsigned long n_balk_blkd_tasks;
  104. /* Refused to boost: no blocked tasks. */
  105. unsigned long n_balk_exp_gp_tasks;
  106. /* Refused to boost: nothing blocking GP. */
  107. unsigned long n_balk_boost_tasks;
  108. /* Refused to boost: already boosting. */
  109. unsigned long n_balk_notyet;
  110. /* Refused to boost: not yet time. */
  111. unsigned long n_balk_nos;
  112. /* Refused to boost: not sure why, though. */
  113. /* This can happen due to race conditions. */
  114. #endif /* #ifdef CONFIG_RCU_BOOST */
  115. #endif /* #ifdef CONFIG_RCU_TRACE */
  116. };
  117. static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = {
  118. .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  119. .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  120. .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  121. .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks),
  122. RCU_TRACE(.rcb.name = "rcu_preempt")
  123. };
  124. static int rcu_preempted_readers_exp(void);
  125. static void rcu_report_exp_done(void);
  126. /*
  127. * Return true if the CPU has not yet responded to the current grace period.
  128. */
  129. static int rcu_cpu_blocking_cur_gp(void)
  130. {
  131. return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum;
  132. }
  133. /*
  134. * Check for a running RCU reader. Because there is only one CPU,
  135. * there can be but one running RCU reader at a time. ;-)
  136. */
  137. static int rcu_preempt_running_reader(void)
  138. {
  139. return current->rcu_read_lock_nesting;
  140. }
  141. /*
  142. * Check for preempted RCU readers blocking any grace period.
  143. * If the caller needs a reliable answer, it must disable hard irqs.
  144. */
  145. static int rcu_preempt_blocked_readers_any(void)
  146. {
  147. return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks);
  148. }
  149. /*
  150. * Check for preempted RCU readers blocking the current grace period.
  151. * If the caller needs a reliable answer, it must disable hard irqs.
  152. */
  153. static int rcu_preempt_blocked_readers_cgp(void)
  154. {
  155. return rcu_preempt_ctrlblk.gp_tasks != NULL;
  156. }
  157. /*
  158. * Return true if another preemptible-RCU grace period is needed.
  159. */
  160. static int rcu_preempt_needs_another_gp(void)
  161. {
  162. return *rcu_preempt_ctrlblk.rcb.curtail != NULL;
  163. }
  164. /*
  165. * Return true if a preemptible-RCU grace period is in progress.
  166. * The caller must disable hardirqs.
  167. */
  168. static int rcu_preempt_gp_in_progress(void)
  169. {
  170. return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum;
  171. }
  172. /*
  173. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  174. * returning NULL if at the end of the list.
  175. */
  176. static struct list_head *rcu_next_node_entry(struct task_struct *t)
  177. {
  178. struct list_head *np;
  179. np = t->rcu_node_entry.next;
  180. if (np == &rcu_preempt_ctrlblk.blkd_tasks)
  181. np = NULL;
  182. return np;
  183. }
  184. #ifdef CONFIG_RCU_TRACE
  185. #ifdef CONFIG_RCU_BOOST
  186. static void rcu_initiate_boost_trace(void);
  187. #endif /* #ifdef CONFIG_RCU_BOOST */
  188. /*
  189. * Dump additional statistice for TINY_PREEMPT_RCU.
  190. */
  191. static void show_tiny_preempt_stats(struct seq_file *m)
  192. {
  193. seq_printf(m, "rcu_preempt: qlen=%ld gp=%lu g%u/p%u/c%u tasks=%c%c%c\n",
  194. rcu_preempt_ctrlblk.rcb.qlen,
  195. rcu_preempt_ctrlblk.n_grace_periods,
  196. rcu_preempt_ctrlblk.gpnum,
  197. rcu_preempt_ctrlblk.gpcpu,
  198. rcu_preempt_ctrlblk.completed,
  199. "T."[list_empty(&rcu_preempt_ctrlblk.blkd_tasks)],
  200. "N."[!rcu_preempt_ctrlblk.gp_tasks],
  201. "E."[!rcu_preempt_ctrlblk.exp_tasks]);
  202. #ifdef CONFIG_RCU_BOOST
  203. seq_printf(m, "%sttb=%c ntb=%lu neb=%lu nnb=%lu j=%04x bt=%04x\n",
  204. " ",
  205. "B."[!rcu_preempt_ctrlblk.boost_tasks],
  206. rcu_preempt_ctrlblk.n_tasks_boosted,
  207. rcu_preempt_ctrlblk.n_exp_boosts,
  208. rcu_preempt_ctrlblk.n_normal_boosts,
  209. (int)(jiffies & 0xffff),
  210. (int)(rcu_preempt_ctrlblk.boost_time & 0xffff));
  211. seq_printf(m, "%s: nt=%lu egt=%lu bt=%lu ny=%lu nos=%lu\n",
  212. " balk",
  213. rcu_preempt_ctrlblk.n_balk_blkd_tasks,
  214. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks,
  215. rcu_preempt_ctrlblk.n_balk_boost_tasks,
  216. rcu_preempt_ctrlblk.n_balk_notyet,
  217. rcu_preempt_ctrlblk.n_balk_nos);
  218. #endif /* #ifdef CONFIG_RCU_BOOST */
  219. }
  220. #endif /* #ifdef CONFIG_RCU_TRACE */
  221. #ifdef CONFIG_RCU_BOOST
  222. #include "rtmutex_common.h"
  223. #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
  224. /* Controls for rcu_kthread() kthread. */
  225. static struct task_struct *rcu_kthread_task;
  226. static DECLARE_WAIT_QUEUE_HEAD(rcu_kthread_wq);
  227. static unsigned long have_rcu_kthread_work;
  228. /*
  229. * Carry out RCU priority boosting on the task indicated by ->boost_tasks,
  230. * and advance ->boost_tasks to the next task in the ->blkd_tasks list.
  231. */
  232. static int rcu_boost(void)
  233. {
  234. unsigned long flags;
  235. struct rt_mutex mtx;
  236. struct task_struct *t;
  237. struct list_head *tb;
  238. if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
  239. rcu_preempt_ctrlblk.exp_tasks == NULL)
  240. return 0; /* Nothing to boost. */
  241. raw_local_irq_save(flags);
  242. /*
  243. * Recheck with irqs disabled: all tasks in need of boosting
  244. * might exit their RCU read-side critical sections on their own
  245. * if we are preempted just before disabling irqs.
  246. */
  247. if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
  248. rcu_preempt_ctrlblk.exp_tasks == NULL) {
  249. raw_local_irq_restore(flags);
  250. return 0;
  251. }
  252. /*
  253. * Preferentially boost tasks blocking expedited grace periods.
  254. * This cannot starve the normal grace periods because a second
  255. * expedited grace period must boost all blocked tasks, including
  256. * those blocking the pre-existing normal grace period.
  257. */
  258. if (rcu_preempt_ctrlblk.exp_tasks != NULL) {
  259. tb = rcu_preempt_ctrlblk.exp_tasks;
  260. RCU_TRACE(rcu_preempt_ctrlblk.n_exp_boosts++);
  261. } else {
  262. tb = rcu_preempt_ctrlblk.boost_tasks;
  263. RCU_TRACE(rcu_preempt_ctrlblk.n_normal_boosts++);
  264. }
  265. RCU_TRACE(rcu_preempt_ctrlblk.n_tasks_boosted++);
  266. /*
  267. * We boost task t by manufacturing an rt_mutex that appears to
  268. * be held by task t. We leave a pointer to that rt_mutex where
  269. * task t can find it, and task t will release the mutex when it
  270. * exits its outermost RCU read-side critical section. Then
  271. * simply acquiring this artificial rt_mutex will boost task
  272. * t's priority. (Thanks to tglx for suggesting this approach!)
  273. */
  274. t = container_of(tb, struct task_struct, rcu_node_entry);
  275. rt_mutex_init_proxy_locked(&mtx, t);
  276. t->rcu_boost_mutex = &mtx;
  277. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED;
  278. raw_local_irq_restore(flags);
  279. rt_mutex_lock(&mtx);
  280. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  281. return rcu_preempt_ctrlblk.boost_tasks != NULL ||
  282. rcu_preempt_ctrlblk.exp_tasks != NULL;
  283. }
  284. /*
  285. * Check to see if it is now time to start boosting RCU readers blocking
  286. * the current grace period, and, if so, tell the rcu_kthread_task to
  287. * start boosting them. If there is an expedited boost in progress,
  288. * we wait for it to complete.
  289. *
  290. * If there are no blocked readers blocking the current grace period,
  291. * return 0 to let the caller know, otherwise return 1. Note that this
  292. * return value is independent of whether or not boosting was done.
  293. */
  294. static int rcu_initiate_boost(void)
  295. {
  296. if (!rcu_preempt_blocked_readers_cgp() &&
  297. rcu_preempt_ctrlblk.exp_tasks == NULL) {
  298. RCU_TRACE(rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++);
  299. return 0;
  300. }
  301. if (rcu_preempt_ctrlblk.exp_tasks != NULL ||
  302. (rcu_preempt_ctrlblk.gp_tasks != NULL &&
  303. rcu_preempt_ctrlblk.boost_tasks == NULL &&
  304. ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))) {
  305. if (rcu_preempt_ctrlblk.exp_tasks == NULL)
  306. rcu_preempt_ctrlblk.boost_tasks =
  307. rcu_preempt_ctrlblk.gp_tasks;
  308. invoke_rcu_callbacks();
  309. } else
  310. RCU_TRACE(rcu_initiate_boost_trace());
  311. return 1;
  312. }
  313. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  314. /*
  315. * Do priority-boost accounting for the start of a new grace period.
  316. */
  317. static void rcu_preempt_boost_start_gp(void)
  318. {
  319. rcu_preempt_ctrlblk.boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  320. }
  321. #else /* #ifdef CONFIG_RCU_BOOST */
  322. /*
  323. * If there is no RCU priority boosting, we don't initiate boosting,
  324. * but we do indicate whether there are blocked readers blocking the
  325. * current grace period.
  326. */
  327. static int rcu_initiate_boost(void)
  328. {
  329. return rcu_preempt_blocked_readers_cgp();
  330. }
  331. /*
  332. * If there is no RCU priority boosting, nothing to do at grace-period start.
  333. */
  334. static void rcu_preempt_boost_start_gp(void)
  335. {
  336. }
  337. #endif /* else #ifdef CONFIG_RCU_BOOST */
  338. /*
  339. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  340. * that this just means that the task currently running on the CPU is
  341. * in a quiescent state. There might be any number of tasks blocked
  342. * while in an RCU read-side critical section.
  343. *
  344. * Unlike the other rcu_*_qs() functions, callers to this function
  345. * must disable irqs in order to protect the assignment to
  346. * ->rcu_read_unlock_special.
  347. *
  348. * Because this is a single-CPU implementation, the only way a grace
  349. * period can end is if the CPU is in a quiescent state. The reason is
  350. * that a blocked preemptible-RCU reader can exit its critical section
  351. * only if the CPU is running it at the time. Therefore, when the
  352. * last task blocking the current grace period exits its RCU read-side
  353. * critical section, neither the CPU nor blocked tasks will be stopping
  354. * the current grace period. (In contrast, SMP implementations
  355. * might have CPUs running in RCU read-side critical sections that
  356. * block later grace periods -- but this is not possible given only
  357. * one CPU.)
  358. */
  359. static void rcu_preempt_cpu_qs(void)
  360. {
  361. /* Record both CPU and task as having responded to current GP. */
  362. rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum;
  363. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  364. /* If there is no GP then there is nothing more to do. */
  365. if (!rcu_preempt_gp_in_progress())
  366. return;
  367. /*
  368. * Check up on boosting. If there are readers blocking the
  369. * current grace period, leave.
  370. */
  371. if (rcu_initiate_boost())
  372. return;
  373. /* Advance callbacks. */
  374. rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum;
  375. rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail;
  376. rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail;
  377. /* If there are no blocked readers, next GP is done instantly. */
  378. if (!rcu_preempt_blocked_readers_any())
  379. rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail;
  380. /* If there are done callbacks, cause them to be invoked. */
  381. if (*rcu_preempt_ctrlblk.rcb.donetail != NULL)
  382. invoke_rcu_callbacks();
  383. }
  384. /*
  385. * Start a new RCU grace period if warranted. Hard irqs must be disabled.
  386. */
  387. static void rcu_preempt_start_gp(void)
  388. {
  389. if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) {
  390. /* Official start of GP. */
  391. rcu_preempt_ctrlblk.gpnum++;
  392. RCU_TRACE(rcu_preempt_ctrlblk.n_grace_periods++);
  393. /* Any blocked RCU readers block new GP. */
  394. if (rcu_preempt_blocked_readers_any())
  395. rcu_preempt_ctrlblk.gp_tasks =
  396. rcu_preempt_ctrlblk.blkd_tasks.next;
  397. /* Set up for RCU priority boosting. */
  398. rcu_preempt_boost_start_gp();
  399. /* If there is no running reader, CPU is done with GP. */
  400. if (!rcu_preempt_running_reader())
  401. rcu_preempt_cpu_qs();
  402. }
  403. }
  404. /*
  405. * We have entered the scheduler, and the current task might soon be
  406. * context-switched away from. If this task is in an RCU read-side
  407. * critical section, we will no longer be able to rely on the CPU to
  408. * record that fact, so we enqueue the task on the blkd_tasks list.
  409. * If the task started after the current grace period began, as recorded
  410. * by ->gpcpu, we enqueue at the beginning of the list. Otherwise
  411. * before the element referenced by ->gp_tasks (or at the tail if
  412. * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element.
  413. * The task will dequeue itself when it exits the outermost enclosing
  414. * RCU read-side critical section. Therefore, the current grace period
  415. * cannot be permitted to complete until the ->gp_tasks pointer becomes
  416. * NULL.
  417. *
  418. * Caller must disable preemption.
  419. */
  420. void rcu_preempt_note_context_switch(void)
  421. {
  422. struct task_struct *t = current;
  423. unsigned long flags;
  424. local_irq_save(flags); /* must exclude scheduler_tick(). */
  425. if (rcu_preempt_running_reader() &&
  426. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  427. /* Possibly blocking in an RCU read-side critical section. */
  428. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  429. /*
  430. * If this CPU has already checked in, then this task
  431. * will hold up the next grace period rather than the
  432. * current grace period. Queue the task accordingly.
  433. * If the task is queued for the current grace period
  434. * (i.e., this CPU has not yet passed through a quiescent
  435. * state for the current grace period), then as long
  436. * as that task remains queued, the current grace period
  437. * cannot end.
  438. */
  439. list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks);
  440. if (rcu_cpu_blocking_cur_gp())
  441. rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry;
  442. }
  443. /*
  444. * Either we were not in an RCU read-side critical section to
  445. * begin with, or we have now recorded that critical section
  446. * globally. Either way, we can now note a quiescent state
  447. * for this CPU. Again, if we were in an RCU read-side critical
  448. * section, and if that critical section was blocking the current
  449. * grace period, then the fact that the task has been enqueued
  450. * means that current grace period continues to be blocked.
  451. */
  452. rcu_preempt_cpu_qs();
  453. local_irq_restore(flags);
  454. }
  455. /*
  456. * Tiny-preemptible RCU implementation for rcu_read_lock().
  457. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  458. * if we block.
  459. */
  460. void __rcu_read_lock(void)
  461. {
  462. current->rcu_read_lock_nesting++;
  463. barrier(); /* needed if we ever invoke rcu_read_lock in rcutiny.c */
  464. }
  465. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  466. /*
  467. * Handle special cases during rcu_read_unlock(), such as needing to
  468. * notify RCU core processing or task having blocked during the RCU
  469. * read-side critical section.
  470. */
  471. static void rcu_read_unlock_special(struct task_struct *t)
  472. {
  473. int empty;
  474. int empty_exp;
  475. unsigned long flags;
  476. struct list_head *np;
  477. int special;
  478. /*
  479. * NMI handlers cannot block and cannot safely manipulate state.
  480. * They therefore cannot possibly be special, so just leave.
  481. */
  482. if (in_nmi())
  483. return;
  484. local_irq_save(flags);
  485. /*
  486. * If RCU core is waiting for this CPU to exit critical section,
  487. * let it know that we have done so.
  488. */
  489. special = t->rcu_read_unlock_special;
  490. if (special & RCU_READ_UNLOCK_NEED_QS)
  491. rcu_preempt_cpu_qs();
  492. /* Hardware IRQ handlers cannot block. */
  493. if (in_irq()) {
  494. local_irq_restore(flags);
  495. return;
  496. }
  497. /* Clean up if blocked during RCU read-side critical section. */
  498. if (special & RCU_READ_UNLOCK_BLOCKED) {
  499. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  500. /*
  501. * Remove this task from the ->blkd_tasks list and adjust
  502. * any pointers that might have been referencing it.
  503. */
  504. empty = !rcu_preempt_blocked_readers_cgp();
  505. empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
  506. np = rcu_next_node_entry(t);
  507. list_del_init(&t->rcu_node_entry);
  508. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
  509. rcu_preempt_ctrlblk.gp_tasks = np;
  510. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
  511. rcu_preempt_ctrlblk.exp_tasks = np;
  512. #ifdef CONFIG_RCU_BOOST
  513. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks)
  514. rcu_preempt_ctrlblk.boost_tasks = np;
  515. #endif /* #ifdef CONFIG_RCU_BOOST */
  516. /*
  517. * If this was the last task on the current list, and if
  518. * we aren't waiting on the CPU, report the quiescent state
  519. * and start a new grace period if needed.
  520. */
  521. if (!empty && !rcu_preempt_blocked_readers_cgp()) {
  522. rcu_preempt_cpu_qs();
  523. rcu_preempt_start_gp();
  524. }
  525. /*
  526. * If this was the last task on the expedited lists,
  527. * then we need wake up the waiting task.
  528. */
  529. if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
  530. rcu_report_exp_done();
  531. }
  532. #ifdef CONFIG_RCU_BOOST
  533. /* Unboost self if was boosted. */
  534. if (special & RCU_READ_UNLOCK_BOOSTED) {
  535. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
  536. rt_mutex_unlock(t->rcu_boost_mutex);
  537. t->rcu_boost_mutex = NULL;
  538. }
  539. #endif /* #ifdef CONFIG_RCU_BOOST */
  540. local_irq_restore(flags);
  541. }
  542. /*
  543. * Tiny-preemptible RCU implementation for rcu_read_unlock().
  544. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  545. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  546. * invoke rcu_read_unlock_special() to clean up after a context switch
  547. * in an RCU read-side critical section and other special cases.
  548. */
  549. void __rcu_read_unlock(void)
  550. {
  551. struct task_struct *t = current;
  552. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
  553. --t->rcu_read_lock_nesting;
  554. barrier(); /* decrement before load of ->rcu_read_unlock_special */
  555. if (t->rcu_read_lock_nesting == 0 &&
  556. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  557. rcu_read_unlock_special(t);
  558. #ifdef CONFIG_PROVE_LOCKING
  559. WARN_ON_ONCE(t->rcu_read_lock_nesting < 0);
  560. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  561. }
  562. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  563. /*
  564. * Check for a quiescent state from the current CPU. When a task blocks,
  565. * the task is recorded in the rcu_preempt_ctrlblk structure, which is
  566. * checked elsewhere. This is called from the scheduling-clock interrupt.
  567. *
  568. * Caller must disable hard irqs.
  569. */
  570. static void rcu_preempt_check_callbacks(void)
  571. {
  572. struct task_struct *t = current;
  573. if (rcu_preempt_gp_in_progress() &&
  574. (!rcu_preempt_running_reader() ||
  575. !rcu_cpu_blocking_cur_gp()))
  576. rcu_preempt_cpu_qs();
  577. if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
  578. rcu_preempt_ctrlblk.rcb.donetail)
  579. invoke_rcu_callbacks();
  580. if (rcu_preempt_gp_in_progress() &&
  581. rcu_cpu_blocking_cur_gp() &&
  582. rcu_preempt_running_reader())
  583. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  584. }
  585. /*
  586. * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
  587. * update, so this is invoked from rcu_process_callbacks() to
  588. * handle that case. Of course, it is invoked for all flavors of
  589. * RCU, but RCU callbacks can appear only on one of the lists, and
  590. * neither ->nexttail nor ->donetail can possibly be NULL, so there
  591. * is no need for an explicit check.
  592. */
  593. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  594. {
  595. if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
  596. rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
  597. }
  598. /*
  599. * Process callbacks for preemptible RCU.
  600. */
  601. static void rcu_preempt_process_callbacks(void)
  602. {
  603. __rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
  604. }
  605. /*
  606. * Queue a preemptible -RCU callback for invocation after a grace period.
  607. */
  608. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  609. {
  610. unsigned long flags;
  611. debug_rcu_head_queue(head);
  612. head->func = func;
  613. head->next = NULL;
  614. local_irq_save(flags);
  615. *rcu_preempt_ctrlblk.nexttail = head;
  616. rcu_preempt_ctrlblk.nexttail = &head->next;
  617. RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
  618. rcu_preempt_start_gp(); /* checks to see if GP needed. */
  619. local_irq_restore(flags);
  620. }
  621. EXPORT_SYMBOL_GPL(call_rcu);
  622. /*
  623. * synchronize_rcu - wait until a grace period has elapsed.
  624. *
  625. * Control will return to the caller some time after a full grace
  626. * period has elapsed, in other words after all currently executing RCU
  627. * read-side critical sections have completed. RCU read-side critical
  628. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  629. * and may be nested.
  630. */
  631. void synchronize_rcu(void)
  632. {
  633. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  634. if (!rcu_scheduler_active)
  635. return;
  636. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  637. WARN_ON_ONCE(rcu_preempt_running_reader());
  638. if (!rcu_preempt_blocked_readers_any())
  639. return;
  640. /* Once we get past the fastpath checks, same code as rcu_barrier(). */
  641. rcu_barrier();
  642. }
  643. EXPORT_SYMBOL_GPL(synchronize_rcu);
  644. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  645. static unsigned long sync_rcu_preempt_exp_count;
  646. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  647. /*
  648. * Return non-zero if there are any tasks in RCU read-side critical
  649. * sections blocking the current preemptible-RCU expedited grace period.
  650. * If there is no preemptible-RCU expedited grace period currently in
  651. * progress, returns zero unconditionally.
  652. */
  653. static int rcu_preempted_readers_exp(void)
  654. {
  655. return rcu_preempt_ctrlblk.exp_tasks != NULL;
  656. }
  657. /*
  658. * Report the exit from RCU read-side critical section for the last task
  659. * that queued itself during or before the current expedited preemptible-RCU
  660. * grace period.
  661. */
  662. static void rcu_report_exp_done(void)
  663. {
  664. wake_up(&sync_rcu_preempt_exp_wq);
  665. }
  666. /*
  667. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  668. * is to rely in the fact that there is but one CPU, and that it is
  669. * illegal for a task to invoke synchronize_rcu_expedited() while in a
  670. * preemptible-RCU read-side critical section. Therefore, any such
  671. * critical sections must correspond to blocked tasks, which must therefore
  672. * be on the ->blkd_tasks list. So just record the current head of the
  673. * list in the ->exp_tasks pointer, and wait for all tasks including and
  674. * after the task pointed to by ->exp_tasks to drain.
  675. */
  676. void synchronize_rcu_expedited(void)
  677. {
  678. unsigned long flags;
  679. struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
  680. unsigned long snap;
  681. barrier(); /* ensure prior action seen before grace period. */
  682. WARN_ON_ONCE(rcu_preempt_running_reader());
  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. local_irq_save(flags);
  693. /*
  694. * All RCU readers have to already be on blkd_tasks because
  695. * we cannot legally be executing in an RCU read-side critical
  696. * section.
  697. */
  698. /* Snapshot current head of ->blkd_tasks list. */
  699. rpcp->exp_tasks = rpcp->blkd_tasks.next;
  700. if (rpcp->exp_tasks == &rpcp->blkd_tasks)
  701. rpcp->exp_tasks = NULL;
  702. /* Wait for tail of ->blkd_tasks list to drain. */
  703. if (!rcu_preempted_readers_exp())
  704. local_irq_restore(flags);
  705. else {
  706. rcu_initiate_boost();
  707. local_irq_restore(flags);
  708. wait_event(sync_rcu_preempt_exp_wq,
  709. !rcu_preempted_readers_exp());
  710. }
  711. /* Clean up and exit. */
  712. barrier(); /* ensure expedited GP seen before counter increment. */
  713. sync_rcu_preempt_exp_count++;
  714. unlock_mb_ret:
  715. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  716. barrier(); /* ensure subsequent action seen after grace period. */
  717. }
  718. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  719. /*
  720. * Does preemptible RCU need the CPU to stay out of dynticks mode?
  721. */
  722. int rcu_preempt_needs_cpu(void)
  723. {
  724. if (!rcu_preempt_running_reader())
  725. rcu_preempt_cpu_qs();
  726. return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
  727. }
  728. /*
  729. * Check for a task exiting while in a preemptible -RCU read-side
  730. * critical section, clean up if so. No need to issue warnings,
  731. * as debug_check_no_locks_held() already does this if lockdep
  732. * is enabled.
  733. */
  734. void exit_rcu(void)
  735. {
  736. struct task_struct *t = current;
  737. if (t->rcu_read_lock_nesting == 0)
  738. return;
  739. t->rcu_read_lock_nesting = 1;
  740. __rcu_read_unlock();
  741. }
  742. #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
  743. #ifdef CONFIG_RCU_TRACE
  744. /*
  745. * Because preemptible RCU does not exist, it is not necessary to
  746. * dump out its statistics.
  747. */
  748. static void show_tiny_preempt_stats(struct seq_file *m)
  749. {
  750. }
  751. #endif /* #ifdef CONFIG_RCU_TRACE */
  752. /*
  753. * Because preemptible RCU does not exist, it never has any callbacks
  754. * to check.
  755. */
  756. static void rcu_preempt_check_callbacks(void)
  757. {
  758. }
  759. /*
  760. * Because preemptible RCU does not exist, it never has any callbacks
  761. * to remove.
  762. */
  763. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  764. {
  765. }
  766. /*
  767. * Because preemptible RCU does not exist, it never has any callbacks
  768. * to process.
  769. */
  770. static void rcu_preempt_process_callbacks(void)
  771. {
  772. }
  773. #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
  774. #ifdef CONFIG_RCU_BOOST
  775. /*
  776. * Wake up rcu_kthread() to process callbacks now eligible for invocation
  777. * or to boost readers.
  778. */
  779. static void invoke_rcu_callbacks(void)
  780. {
  781. have_rcu_kthread_work = 1;
  782. wake_up(&rcu_kthread_wq);
  783. }
  784. /*
  785. * This kthread invokes RCU callbacks whose grace periods have
  786. * elapsed. It is awakened as needed, and takes the place of the
  787. * RCU_SOFTIRQ that is used for this purpose when boosting is disabled.
  788. * This is a kthread, but it is never stopped, at least not until
  789. * the system goes down.
  790. */
  791. static int rcu_kthread(void *arg)
  792. {
  793. unsigned long work;
  794. unsigned long morework;
  795. unsigned long flags;
  796. for (;;) {
  797. wait_event_interruptible(rcu_kthread_wq,
  798. have_rcu_kthread_work != 0);
  799. morework = rcu_boost();
  800. local_irq_save(flags);
  801. work = have_rcu_kthread_work;
  802. have_rcu_kthread_work = morework;
  803. local_irq_restore(flags);
  804. if (work)
  805. rcu_process_callbacks(NULL);
  806. schedule_timeout_interruptible(1); /* Leave CPU for others. */
  807. }
  808. return 0; /* Not reached, but needed to shut gcc up. */
  809. }
  810. /*
  811. * Spawn the kthread that invokes RCU callbacks.
  812. */
  813. static int __init rcu_spawn_kthreads(void)
  814. {
  815. struct sched_param sp;
  816. rcu_kthread_task = kthread_run(rcu_kthread, NULL, "rcu_kthread");
  817. sp.sched_priority = RCU_BOOST_PRIO;
  818. sched_setscheduler_nocheck(rcu_kthread_task, SCHED_FIFO, &sp);
  819. return 0;
  820. }
  821. early_initcall(rcu_spawn_kthreads);
  822. #else /* #ifdef CONFIG_RCU_BOOST */
  823. /*
  824. * Start up softirq processing of callbacks.
  825. */
  826. void invoke_rcu_callbacks(void)
  827. {
  828. raise_softirq(RCU_SOFTIRQ);
  829. }
  830. void rcu_init(void)
  831. {
  832. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  833. }
  834. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  835. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  836. #include <linux/kernel_stat.h>
  837. /*
  838. * During boot, we forgive RCU lockdep issues. After this function is
  839. * invoked, we start taking RCU lockdep issues seriously.
  840. */
  841. void __init rcu_scheduler_starting(void)
  842. {
  843. WARN_ON(nr_context_switches() > 0);
  844. rcu_scheduler_active = 1;
  845. }
  846. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  847. #ifdef CONFIG_RCU_TRACE
  848. #ifdef CONFIG_RCU_BOOST
  849. static void rcu_initiate_boost_trace(void)
  850. {
  851. if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
  852. rcu_preempt_ctrlblk.n_balk_blkd_tasks++;
  853. else if (rcu_preempt_ctrlblk.gp_tasks == NULL &&
  854. rcu_preempt_ctrlblk.exp_tasks == NULL)
  855. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++;
  856. else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
  857. rcu_preempt_ctrlblk.n_balk_boost_tasks++;
  858. else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
  859. rcu_preempt_ctrlblk.n_balk_notyet++;
  860. else
  861. rcu_preempt_ctrlblk.n_balk_nos++;
  862. }
  863. #endif /* #ifdef CONFIG_RCU_BOOST */
  864. static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
  865. {
  866. unsigned long flags;
  867. raw_local_irq_save(flags);
  868. rcp->qlen -= n;
  869. raw_local_irq_restore(flags);
  870. }
  871. /*
  872. * Dump statistics for TINY_RCU, such as they are.
  873. */
  874. static int show_tiny_stats(struct seq_file *m, void *unused)
  875. {
  876. show_tiny_preempt_stats(m);
  877. seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
  878. seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
  879. return 0;
  880. }
  881. static int show_tiny_stats_open(struct inode *inode, struct file *file)
  882. {
  883. return single_open(file, show_tiny_stats, NULL);
  884. }
  885. static const struct file_operations show_tiny_stats_fops = {
  886. .owner = THIS_MODULE,
  887. .open = show_tiny_stats_open,
  888. .read = seq_read,
  889. .llseek = seq_lseek,
  890. .release = single_release,
  891. };
  892. static struct dentry *rcudir;
  893. static int __init rcutiny_trace_init(void)
  894. {
  895. struct dentry *retval;
  896. rcudir = debugfs_create_dir("rcu", NULL);
  897. if (!rcudir)
  898. goto free_out;
  899. retval = debugfs_create_file("rcudata", 0444, rcudir,
  900. NULL, &show_tiny_stats_fops);
  901. if (!retval)
  902. goto free_out;
  903. return 0;
  904. free_out:
  905. debugfs_remove_recursive(rcudir);
  906. return 1;
  907. }
  908. static void __exit rcutiny_trace_cleanup(void)
  909. {
  910. debugfs_remove_recursive(rcudir);
  911. }
  912. module_init(rcutiny_trace_init);
  913. module_exit(rcutiny_trace_cleanup);
  914. MODULE_AUTHOR("Paul E. McKenney");
  915. MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
  916. MODULE_LICENSE("GPL");
  917. #endif /* #ifdef CONFIG_RCU_TRACE */