rcutiny_plugin.h 30 KB

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