rcutiny_plugin.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 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_init(&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. /*
  530. * If this was the last task on the current list, and if
  531. * we aren't waiting on the CPU, report the quiescent state
  532. * and start a new grace period if needed.
  533. */
  534. if (!empty && !rcu_preempt_blocked_readers_cgp()) {
  535. rcu_preempt_cpu_qs();
  536. rcu_preempt_start_gp();
  537. }
  538. /*
  539. * If this was the last task on the expedited lists,
  540. * then we need wake up the waiting task.
  541. */
  542. if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
  543. rcu_report_exp_done();
  544. }
  545. #ifdef CONFIG_RCU_BOOST
  546. /* Unboost self if was boosted. */
  547. if (special & RCU_READ_UNLOCK_BOOSTED) {
  548. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
  549. rt_mutex_unlock(t->rcu_boost_mutex);
  550. t->rcu_boost_mutex = NULL;
  551. }
  552. #endif /* #ifdef CONFIG_RCU_BOOST */
  553. local_irq_restore(flags);
  554. }
  555. /*
  556. * Tiny-preemptible RCU implementation for rcu_read_unlock().
  557. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  558. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  559. * invoke rcu_read_unlock_special() to clean up after a context switch
  560. * in an RCU read-side critical section and other special cases.
  561. */
  562. void __rcu_read_unlock(void)
  563. {
  564. struct task_struct *t = current;
  565. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
  566. --t->rcu_read_lock_nesting;
  567. barrier(); /* decrement before load of ->rcu_read_unlock_special */
  568. if (t->rcu_read_lock_nesting == 0 &&
  569. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  570. rcu_read_unlock_special(t);
  571. #ifdef CONFIG_PROVE_LOCKING
  572. WARN_ON_ONCE(t->rcu_read_lock_nesting < 0);
  573. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  574. }
  575. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  576. /*
  577. * Check for a quiescent state from the current CPU. When a task blocks,
  578. * the task is recorded in the rcu_preempt_ctrlblk structure, which is
  579. * checked elsewhere. This is called from the scheduling-clock interrupt.
  580. *
  581. * Caller must disable hard irqs.
  582. */
  583. static void rcu_preempt_check_callbacks(void)
  584. {
  585. struct task_struct *t = current;
  586. if (rcu_preempt_gp_in_progress() &&
  587. (!rcu_preempt_running_reader() ||
  588. !rcu_cpu_blocking_cur_gp()))
  589. rcu_preempt_cpu_qs();
  590. if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
  591. rcu_preempt_ctrlblk.rcb.donetail)
  592. invoke_rcu_kthread();
  593. if (rcu_preempt_gp_in_progress() &&
  594. rcu_cpu_blocking_cur_gp() &&
  595. rcu_preempt_running_reader())
  596. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  597. }
  598. /*
  599. * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
  600. * update, so this is invoked from rcu_process_callbacks() to
  601. * handle that case. Of course, it is invoked for all flavors of
  602. * RCU, but RCU callbacks can appear only on one of the lists, and
  603. * neither ->nexttail nor ->donetail can possibly be NULL, so there
  604. * is no need for an explicit check.
  605. */
  606. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  607. {
  608. if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
  609. rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
  610. }
  611. /*
  612. * Process callbacks for preemptible RCU.
  613. */
  614. static void rcu_preempt_process_callbacks(void)
  615. {
  616. rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
  617. }
  618. /*
  619. * Queue a preemptible -RCU callback for invocation after a grace period.
  620. */
  621. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  622. {
  623. unsigned long flags;
  624. debug_rcu_head_queue(head);
  625. head->func = func;
  626. head->next = NULL;
  627. local_irq_save(flags);
  628. *rcu_preempt_ctrlblk.nexttail = head;
  629. rcu_preempt_ctrlblk.nexttail = &head->next;
  630. RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
  631. rcu_preempt_start_gp(); /* checks to see if GP needed. */
  632. local_irq_restore(flags);
  633. }
  634. EXPORT_SYMBOL_GPL(call_rcu);
  635. void rcu_barrier(void)
  636. {
  637. struct rcu_synchronize rcu;
  638. init_rcu_head_on_stack(&rcu.head);
  639. init_completion(&rcu.completion);
  640. /* Will wake me after RCU finished. */
  641. call_rcu(&rcu.head, wakeme_after_rcu);
  642. /* Wait for it. */
  643. wait_for_completion(&rcu.completion);
  644. destroy_rcu_head_on_stack(&rcu.head);
  645. }
  646. EXPORT_SYMBOL_GPL(rcu_barrier);
  647. /*
  648. * synchronize_rcu - wait until a grace period has elapsed.
  649. *
  650. * Control will return to the caller some time after a full grace
  651. * period has elapsed, in other words after all currently executing RCU
  652. * read-side critical sections have completed. RCU read-side critical
  653. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  654. * and may be nested.
  655. */
  656. void synchronize_rcu(void)
  657. {
  658. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  659. if (!rcu_scheduler_active)
  660. return;
  661. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  662. WARN_ON_ONCE(rcu_preempt_running_reader());
  663. if (!rcu_preempt_blocked_readers_any())
  664. return;
  665. /* Once we get past the fastpath checks, same code as rcu_barrier(). */
  666. rcu_barrier();
  667. }
  668. EXPORT_SYMBOL_GPL(synchronize_rcu);
  669. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  670. static unsigned long sync_rcu_preempt_exp_count;
  671. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  672. /*
  673. * Return non-zero if there are any tasks in RCU read-side critical
  674. * sections blocking the current preemptible-RCU expedited grace period.
  675. * If there is no preemptible-RCU expedited grace period currently in
  676. * progress, returns zero unconditionally.
  677. */
  678. static int rcu_preempted_readers_exp(void)
  679. {
  680. return rcu_preempt_ctrlblk.exp_tasks != NULL;
  681. }
  682. /*
  683. * Report the exit from RCU read-side critical section for the last task
  684. * that queued itself during or before the current expedited preemptible-RCU
  685. * grace period.
  686. */
  687. static void rcu_report_exp_done(void)
  688. {
  689. wake_up(&sync_rcu_preempt_exp_wq);
  690. }
  691. /*
  692. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  693. * is to rely in the fact that there is but one CPU, and that it is
  694. * illegal for a task to invoke synchronize_rcu_expedited() while in a
  695. * preemptible-RCU read-side critical section. Therefore, any such
  696. * critical sections must correspond to blocked tasks, which must therefore
  697. * be on the ->blkd_tasks list. So just record the current head of the
  698. * list in the ->exp_tasks pointer, and wait for all tasks including and
  699. * after the task pointed to by ->exp_tasks to drain.
  700. */
  701. void synchronize_rcu_expedited(void)
  702. {
  703. unsigned long flags;
  704. struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
  705. unsigned long snap;
  706. barrier(); /* ensure prior action seen before grace period. */
  707. WARN_ON_ONCE(rcu_preempt_running_reader());
  708. /*
  709. * Acquire lock so that there is only one preemptible RCU grace
  710. * period in flight. Of course, if someone does the expedited
  711. * grace period for us while we are acquiring the lock, just leave.
  712. */
  713. snap = sync_rcu_preempt_exp_count + 1;
  714. mutex_lock(&sync_rcu_preempt_exp_mutex);
  715. if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
  716. goto unlock_mb_ret; /* Others did our work for us. */
  717. local_irq_save(flags);
  718. /*
  719. * All RCU readers have to already be on blkd_tasks because
  720. * we cannot legally be executing in an RCU read-side critical
  721. * section.
  722. */
  723. /* Snapshot current head of ->blkd_tasks list. */
  724. rpcp->exp_tasks = rpcp->blkd_tasks.next;
  725. if (rpcp->exp_tasks == &rpcp->blkd_tasks)
  726. rpcp->exp_tasks = NULL;
  727. local_irq_restore(flags);
  728. /* Wait for tail of ->blkd_tasks list to drain. */
  729. if (rcu_preempted_readers_exp())
  730. rcu_initiate_expedited_boost();
  731. wait_event(sync_rcu_preempt_exp_wq,
  732. !rcu_preempted_readers_exp());
  733. /* Clean up and exit. */
  734. barrier(); /* ensure expedited GP seen before counter increment. */
  735. sync_rcu_preempt_exp_count++;
  736. unlock_mb_ret:
  737. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  738. barrier(); /* ensure subsequent action seen after grace period. */
  739. }
  740. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  741. /*
  742. * Does preemptible RCU need the CPU to stay out of dynticks mode?
  743. */
  744. int rcu_preempt_needs_cpu(void)
  745. {
  746. if (!rcu_preempt_running_reader())
  747. rcu_preempt_cpu_qs();
  748. return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
  749. }
  750. /*
  751. * Check for a task exiting while in a preemptible -RCU read-side
  752. * critical section, clean up if so. No need to issue warnings,
  753. * as debug_check_no_locks_held() already does this if lockdep
  754. * is enabled.
  755. */
  756. void exit_rcu(void)
  757. {
  758. struct task_struct *t = current;
  759. if (t->rcu_read_lock_nesting == 0)
  760. return;
  761. t->rcu_read_lock_nesting = 1;
  762. __rcu_read_unlock();
  763. }
  764. #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
  765. #ifdef CONFIG_RCU_TRACE
  766. /*
  767. * Because preemptible RCU does not exist, it is not necessary to
  768. * dump out its statistics.
  769. */
  770. static void show_tiny_preempt_stats(struct seq_file *m)
  771. {
  772. }
  773. #endif /* #ifdef CONFIG_RCU_TRACE */
  774. /*
  775. * Because preemptible RCU does not exist, it is never necessary to
  776. * boost preempted RCU readers.
  777. */
  778. static int rcu_boost(void)
  779. {
  780. return 0;
  781. }
  782. /*
  783. * Because preemptible RCU does not exist, it never has any callbacks
  784. * to check.
  785. */
  786. static void rcu_preempt_check_callbacks(void)
  787. {
  788. }
  789. /*
  790. * Because preemptible RCU does not exist, it never has any callbacks
  791. * to remove.
  792. */
  793. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  794. {
  795. }
  796. /*
  797. * Because preemptible RCU does not exist, it never has any callbacks
  798. * to process.
  799. */
  800. static void rcu_preempt_process_callbacks(void)
  801. {
  802. }
  803. #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
  804. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  805. #include <linux/kernel_stat.h>
  806. /*
  807. * During boot, we forgive RCU lockdep issues. After this function is
  808. * invoked, we start taking RCU lockdep issues seriously.
  809. */
  810. void __init rcu_scheduler_starting(void)
  811. {
  812. WARN_ON(nr_context_switches() > 0);
  813. rcu_scheduler_active = 1;
  814. }
  815. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  816. #ifdef CONFIG_RCU_BOOST
  817. #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
  818. #else /* #ifdef CONFIG_RCU_BOOST */
  819. #define RCU_BOOST_PRIO 1
  820. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  821. #ifdef CONFIG_RCU_TRACE
  822. #ifdef CONFIG_RCU_BOOST
  823. static void rcu_initiate_boost_trace(void)
  824. {
  825. if (rcu_preempt_ctrlblk.gp_tasks == NULL)
  826. rcu_preempt_ctrlblk.n_normal_balk_gp_tasks++;
  827. else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
  828. rcu_preempt_ctrlblk.n_normal_balk_boost_tasks++;
  829. else if (rcu_preempt_ctrlblk.boosted_this_gp != 0)
  830. rcu_preempt_ctrlblk.n_normal_balk_boosted++;
  831. else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
  832. rcu_preempt_ctrlblk.n_normal_balk_notyet++;
  833. else
  834. rcu_preempt_ctrlblk.n_normal_balk_nos++;
  835. }
  836. static void rcu_initiate_exp_boost_trace(void)
  837. {
  838. if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
  839. rcu_preempt_ctrlblk.n_exp_balk_blkd_tasks++;
  840. else
  841. rcu_preempt_ctrlblk.n_exp_balk_nos++;
  842. }
  843. #endif /* #ifdef CONFIG_RCU_BOOST */
  844. static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
  845. {
  846. unsigned long flags;
  847. raw_local_irq_save(flags);
  848. rcp->qlen -= n;
  849. raw_local_irq_restore(flags);
  850. }
  851. /*
  852. * Dump statistics for TINY_RCU, such as they are.
  853. */
  854. static int show_tiny_stats(struct seq_file *m, void *unused)
  855. {
  856. show_tiny_preempt_stats(m);
  857. seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
  858. seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
  859. return 0;
  860. }
  861. static int show_tiny_stats_open(struct inode *inode, struct file *file)
  862. {
  863. return single_open(file, show_tiny_stats, NULL);
  864. }
  865. static const struct file_operations show_tiny_stats_fops = {
  866. .owner = THIS_MODULE,
  867. .open = show_tiny_stats_open,
  868. .read = seq_read,
  869. .llseek = seq_lseek,
  870. .release = single_release,
  871. };
  872. static struct dentry *rcudir;
  873. static int __init rcutiny_trace_init(void)
  874. {
  875. struct dentry *retval;
  876. rcudir = debugfs_create_dir("rcu", NULL);
  877. if (!rcudir)
  878. goto free_out;
  879. retval = debugfs_create_file("rcudata", 0444, rcudir,
  880. NULL, &show_tiny_stats_fops);
  881. if (!retval)
  882. goto free_out;
  883. return 0;
  884. free_out:
  885. debugfs_remove_recursive(rcudir);
  886. return 1;
  887. }
  888. static void __exit rcutiny_trace_cleanup(void)
  889. {
  890. debugfs_remove_recursive(rcudir);
  891. }
  892. module_init(rcutiny_trace_init);
  893. module_exit(rcutiny_trace_cleanup);
  894. MODULE_AUTHOR("Paul E. McKenney");
  895. MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
  896. MODULE_LICENSE("GPL");
  897. #endif /* #ifdef CONFIG_RCU_TRACE */