rcupreempt.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion, realtime implementation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * With thanks to Esben Nielsen, Bill Huey, and Ingo Molnar
  22. * for pushing me away from locks and towards counters, and
  23. * to Suparna Bhattacharya for pushing me completely away
  24. * from atomic instructions on the read side.
  25. *
  26. * - Added handling of Dynamic Ticks
  27. * Copyright 2007 - Paul E. Mckenney <paulmck@us.ibm.com>
  28. * - Steven Rostedt <srostedt@redhat.com>
  29. *
  30. * Papers: http://www.rdrop.com/users/paulmck/RCU
  31. *
  32. * Design Document: http://lwn.net/Articles/253651/
  33. *
  34. * For detailed explanation of Read-Copy Update mechanism see -
  35. * Documentation/RCU/ *.txt
  36. *
  37. */
  38. #include <linux/types.h>
  39. #include <linux/kernel.h>
  40. #include <linux/init.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/smp.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/sched.h>
  46. #include <asm/atomic.h>
  47. #include <linux/bitops.h>
  48. #include <linux/module.h>
  49. #include <linux/kthread.h>
  50. #include <linux/completion.h>
  51. #include <linux/moduleparam.h>
  52. #include <linux/percpu.h>
  53. #include <linux/notifier.h>
  54. #include <linux/cpu.h>
  55. #include <linux/random.h>
  56. #include <linux/delay.h>
  57. #include <linux/byteorder/swabb.h>
  58. #include <linux/cpumask.h>
  59. #include <linux/rcupreempt_trace.h>
  60. /*
  61. * Macro that prevents the compiler from reordering accesses, but does
  62. * absolutely -nothing- to prevent CPUs from reordering. This is used
  63. * only to mediate communication between mainline code and hardware
  64. * interrupt and NMI handlers.
  65. */
  66. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  67. /*
  68. * PREEMPT_RCU data structures.
  69. */
  70. /*
  71. * GP_STAGES specifies the number of times the state machine has
  72. * to go through the all the rcu_try_flip_states (see below)
  73. * in a single Grace Period.
  74. *
  75. * GP in GP_STAGES stands for Grace Period ;)
  76. */
  77. #define GP_STAGES 2
  78. struct rcu_data {
  79. spinlock_t lock; /* Protect rcu_data fields. */
  80. long completed; /* Number of last completed batch. */
  81. int waitlistcount;
  82. struct rcu_head *nextlist;
  83. struct rcu_head **nexttail;
  84. struct rcu_head *waitlist[GP_STAGES];
  85. struct rcu_head **waittail[GP_STAGES];
  86. struct rcu_head *donelist; /* from waitlist & waitschedlist */
  87. struct rcu_head **donetail;
  88. long rcu_flipctr[2];
  89. struct rcu_head *nextschedlist;
  90. struct rcu_head **nextschedtail;
  91. struct rcu_head *waitschedlist;
  92. struct rcu_head **waitschedtail;
  93. int rcu_sched_sleeping;
  94. #ifdef CONFIG_RCU_TRACE
  95. struct rcupreempt_trace trace;
  96. #endif /* #ifdef CONFIG_RCU_TRACE */
  97. };
  98. /*
  99. * States for rcu_try_flip() and friends.
  100. */
  101. enum rcu_try_flip_states {
  102. /*
  103. * Stay here if nothing is happening. Flip the counter if somthing
  104. * starts happening. Denoted by "I"
  105. */
  106. rcu_try_flip_idle_state,
  107. /*
  108. * Wait here for all CPUs to notice that the counter has flipped. This
  109. * prevents the old set of counters from ever being incremented once
  110. * we leave this state, which in turn is necessary because we cannot
  111. * test any individual counter for zero -- we can only check the sum.
  112. * Denoted by "A".
  113. */
  114. rcu_try_flip_waitack_state,
  115. /*
  116. * Wait here for the sum of the old per-CPU counters to reach zero.
  117. * Denoted by "Z".
  118. */
  119. rcu_try_flip_waitzero_state,
  120. /*
  121. * Wait here for each of the other CPUs to execute a memory barrier.
  122. * This is necessary to ensure that these other CPUs really have
  123. * completed executing their RCU read-side critical sections, despite
  124. * their CPUs wildly reordering memory. Denoted by "M".
  125. */
  126. rcu_try_flip_waitmb_state,
  127. };
  128. /*
  129. * States for rcu_ctrlblk.rcu_sched_sleep.
  130. */
  131. enum rcu_sched_sleep_states {
  132. rcu_sched_not_sleeping, /* Not sleeping, callbacks need GP. */
  133. rcu_sched_sleep_prep, /* Thinking of sleeping, rechecking. */
  134. rcu_sched_sleeping, /* Sleeping, awaken if GP needed. */
  135. };
  136. struct rcu_ctrlblk {
  137. spinlock_t fliplock; /* Protect state-machine transitions. */
  138. long completed; /* Number of last completed batch. */
  139. enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
  140. the rcu state machine */
  141. spinlock_t schedlock; /* Protect rcu_sched sleep state. */
  142. enum rcu_sched_sleep_states sched_sleep; /* rcu_sched state. */
  143. wait_queue_head_t sched_wq; /* Place for rcu_sched to sleep. */
  144. };
  145. static DEFINE_PER_CPU(struct rcu_data, rcu_data);
  146. static struct rcu_ctrlblk rcu_ctrlblk = {
  147. .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
  148. .completed = 0,
  149. .rcu_try_flip_state = rcu_try_flip_idle_state,
  150. .schedlock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.schedlock),
  151. .sched_sleep = rcu_sched_not_sleeping,
  152. .sched_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rcu_ctrlblk.sched_wq),
  153. };
  154. static struct task_struct *rcu_sched_grace_period_task;
  155. #ifdef CONFIG_RCU_TRACE
  156. static char *rcu_try_flip_state_names[] =
  157. { "idle", "waitack", "waitzero", "waitmb" };
  158. #endif /* #ifdef CONFIG_RCU_TRACE */
  159. static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
  160. /*
  161. * Enum and per-CPU flag to determine when each CPU has seen
  162. * the most recent counter flip.
  163. */
  164. enum rcu_flip_flag_values {
  165. rcu_flip_seen, /* Steady/initial state, last flip seen. */
  166. /* Only GP detector can update. */
  167. rcu_flipped /* Flip just completed, need confirmation. */
  168. /* Only corresponding CPU can update. */
  169. };
  170. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
  171. = rcu_flip_seen;
  172. /*
  173. * Enum and per-CPU flag to determine when each CPU has executed the
  174. * needed memory barrier to fence in memory references from its last RCU
  175. * read-side critical section in the just-completed grace period.
  176. */
  177. enum rcu_mb_flag_values {
  178. rcu_mb_done, /* Steady/initial state, no mb()s required. */
  179. /* Only GP detector can update. */
  180. rcu_mb_needed /* Flip just completed, need an mb(). */
  181. /* Only corresponding CPU can update. */
  182. };
  183. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
  184. = rcu_mb_done;
  185. /*
  186. * RCU_DATA_ME: find the current CPU's rcu_data structure.
  187. * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
  188. */
  189. #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
  190. #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
  191. /*
  192. * Helper macro for tracing when the appropriate rcu_data is not
  193. * cached in a local variable, but where the CPU number is so cached.
  194. */
  195. #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
  196. /*
  197. * Helper macro for tracing when the appropriate rcu_data is not
  198. * cached in a local variable.
  199. */
  200. #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
  201. /*
  202. * Helper macro for tracing when the appropriate rcu_data is pointed
  203. * to by a local variable.
  204. */
  205. #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
  206. #define RCU_SCHED_BATCH_TIME (HZ / 50)
  207. /*
  208. * Return the number of RCU batches processed thus far. Useful
  209. * for debug and statistics.
  210. */
  211. long rcu_batches_completed(void)
  212. {
  213. return rcu_ctrlblk.completed;
  214. }
  215. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  216. void __rcu_read_lock(void)
  217. {
  218. int idx;
  219. struct task_struct *t = current;
  220. int nesting;
  221. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  222. if (nesting != 0) {
  223. /* An earlier rcu_read_lock() covers us, just count it. */
  224. t->rcu_read_lock_nesting = nesting + 1;
  225. } else {
  226. unsigned long flags;
  227. /*
  228. * We disable interrupts for the following reasons:
  229. * - If we get scheduling clock interrupt here, and we
  230. * end up acking the counter flip, it's like a promise
  231. * that we will never increment the old counter again.
  232. * Thus we will break that promise if that
  233. * scheduling clock interrupt happens between the time
  234. * we pick the .completed field and the time that we
  235. * increment our counter.
  236. *
  237. * - We don't want to be preempted out here.
  238. *
  239. * NMIs can still occur, of course, and might themselves
  240. * contain rcu_read_lock().
  241. */
  242. local_irq_save(flags);
  243. /*
  244. * Outermost nesting of rcu_read_lock(), so increment
  245. * the current counter for the current CPU. Use volatile
  246. * casts to prevent the compiler from reordering.
  247. */
  248. idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
  249. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
  250. /*
  251. * Now that the per-CPU counter has been incremented, we
  252. * are protected from races with rcu_read_lock() invoked
  253. * from NMI handlers on this CPU. We can therefore safely
  254. * increment the nesting counter, relieving further NMIs
  255. * of the need to increment the per-CPU counter.
  256. */
  257. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
  258. /*
  259. * Now that we have preventing any NMIs from storing
  260. * to the ->rcu_flipctr_idx, we can safely use it to
  261. * remember which counter to decrement in the matching
  262. * rcu_read_unlock().
  263. */
  264. ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
  265. local_irq_restore(flags);
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  269. void __rcu_read_unlock(void)
  270. {
  271. int idx;
  272. struct task_struct *t = current;
  273. int nesting;
  274. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  275. if (nesting > 1) {
  276. /*
  277. * We are still protected by the enclosing rcu_read_lock(),
  278. * so simply decrement the counter.
  279. */
  280. t->rcu_read_lock_nesting = nesting - 1;
  281. } else {
  282. unsigned long flags;
  283. /*
  284. * Disable local interrupts to prevent the grace-period
  285. * detection state machine from seeing us half-done.
  286. * NMIs can still occur, of course, and might themselves
  287. * contain rcu_read_lock() and rcu_read_unlock().
  288. */
  289. local_irq_save(flags);
  290. /*
  291. * Outermost nesting of rcu_read_unlock(), so we must
  292. * decrement the current counter for the current CPU.
  293. * This must be done carefully, because NMIs can
  294. * occur at any point in this code, and any rcu_read_lock()
  295. * and rcu_read_unlock() pairs in the NMI handlers
  296. * must interact non-destructively with this code.
  297. * Lots of volatile casts, and -very- careful ordering.
  298. *
  299. * Changes to this code, including this one, must be
  300. * inspected, validated, and tested extremely carefully!!!
  301. */
  302. /*
  303. * First, pick up the index.
  304. */
  305. idx = ACCESS_ONCE(t->rcu_flipctr_idx);
  306. /*
  307. * Now that we have fetched the counter index, it is
  308. * safe to decrement the per-task RCU nesting counter.
  309. * After this, any interrupts or NMIs will increment and
  310. * decrement the per-CPU counters.
  311. */
  312. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
  313. /*
  314. * It is now safe to decrement this task's nesting count.
  315. * NMIs that occur after this statement will route their
  316. * rcu_read_lock() calls through this "else" clause, and
  317. * will thus start incrementing the per-CPU counter on
  318. * their own. They will also clobber ->rcu_flipctr_idx,
  319. * but that is OK, since we have already fetched it.
  320. */
  321. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
  322. local_irq_restore(flags);
  323. }
  324. }
  325. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  326. /*
  327. * If a global counter flip has occurred since the last time that we
  328. * advanced callbacks, advance them. Hardware interrupts must be
  329. * disabled when calling this function.
  330. */
  331. static void __rcu_advance_callbacks(struct rcu_data *rdp)
  332. {
  333. int cpu;
  334. int i;
  335. int wlc = 0;
  336. if (rdp->completed != rcu_ctrlblk.completed) {
  337. if (rdp->waitlist[GP_STAGES - 1] != NULL) {
  338. *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
  339. rdp->donetail = rdp->waittail[GP_STAGES - 1];
  340. RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
  341. }
  342. for (i = GP_STAGES - 2; i >= 0; i--) {
  343. if (rdp->waitlist[i] != NULL) {
  344. rdp->waitlist[i + 1] = rdp->waitlist[i];
  345. rdp->waittail[i + 1] = rdp->waittail[i];
  346. wlc++;
  347. } else {
  348. rdp->waitlist[i + 1] = NULL;
  349. rdp->waittail[i + 1] =
  350. &rdp->waitlist[i + 1];
  351. }
  352. }
  353. if (rdp->nextlist != NULL) {
  354. rdp->waitlist[0] = rdp->nextlist;
  355. rdp->waittail[0] = rdp->nexttail;
  356. wlc++;
  357. rdp->nextlist = NULL;
  358. rdp->nexttail = &rdp->nextlist;
  359. RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
  360. } else {
  361. rdp->waitlist[0] = NULL;
  362. rdp->waittail[0] = &rdp->waitlist[0];
  363. }
  364. rdp->waitlistcount = wlc;
  365. rdp->completed = rcu_ctrlblk.completed;
  366. }
  367. /*
  368. * Check to see if this CPU needs to report that it has seen
  369. * the most recent counter flip, thereby declaring that all
  370. * subsequent rcu_read_lock() invocations will respect this flip.
  371. */
  372. cpu = raw_smp_processor_id();
  373. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  374. smp_mb(); /* Subsequent counter accesses must see new value */
  375. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  376. smp_mb(); /* Subsequent RCU read-side critical sections */
  377. /* seen -after- acknowledgement. */
  378. }
  379. }
  380. DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_dyntick_sched, rcu_dyntick_sched) = {
  381. .dynticks = 1,
  382. };
  383. #ifdef CONFIG_NO_HZ
  384. static DEFINE_PER_CPU(int, rcu_update_flag);
  385. /**
  386. * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
  387. *
  388. * If the CPU was idle with dynamic ticks active, this updates the
  389. * rcu_dyntick_sched.dynticks to let the RCU handling know that the
  390. * CPU is active.
  391. */
  392. void rcu_irq_enter(void)
  393. {
  394. int cpu = smp_processor_id();
  395. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  396. if (per_cpu(rcu_update_flag, cpu))
  397. per_cpu(rcu_update_flag, cpu)++;
  398. /*
  399. * Only update if we are coming from a stopped ticks mode
  400. * (rcu_dyntick_sched.dynticks is even).
  401. */
  402. if (!in_interrupt() &&
  403. (rdssp->dynticks & 0x1) == 0) {
  404. /*
  405. * The following might seem like we could have a race
  406. * with NMI/SMIs. But this really isn't a problem.
  407. * Here we do a read/modify/write, and the race happens
  408. * when an NMI/SMI comes in after the read and before
  409. * the write. But NMI/SMIs will increment this counter
  410. * twice before returning, so the zero bit will not
  411. * be corrupted by the NMI/SMI which is the most important
  412. * part.
  413. *
  414. * The only thing is that we would bring back the counter
  415. * to a postion that it was in during the NMI/SMI.
  416. * But the zero bit would be set, so the rest of the
  417. * counter would again be ignored.
  418. *
  419. * On return from the IRQ, the counter may have the zero
  420. * bit be 0 and the counter the same as the return from
  421. * the NMI/SMI. If the state machine was so unlucky to
  422. * see that, it still doesn't matter, since all
  423. * RCU read-side critical sections on this CPU would
  424. * have already completed.
  425. */
  426. rdssp->dynticks++;
  427. /*
  428. * The following memory barrier ensures that any
  429. * rcu_read_lock() primitives in the irq handler
  430. * are seen by other CPUs to follow the above
  431. * increment to rcu_dyntick_sched.dynticks. This is
  432. * required in order for other CPUs to correctly
  433. * determine when it is safe to advance the RCU
  434. * grace-period state machine.
  435. */
  436. smp_mb(); /* see above block comment. */
  437. /*
  438. * Since we can't determine the dynamic tick mode from
  439. * the rcu_dyntick_sched.dynticks after this routine,
  440. * we use a second flag to acknowledge that we came
  441. * from an idle state with ticks stopped.
  442. */
  443. per_cpu(rcu_update_flag, cpu)++;
  444. /*
  445. * If we take an NMI/SMI now, they will also increment
  446. * the rcu_update_flag, and will not update the
  447. * rcu_dyntick_sched.dynticks on exit. That is for
  448. * this IRQ to do.
  449. */
  450. }
  451. }
  452. /**
  453. * rcu_irq_exit - Called from exiting Hard irq context.
  454. *
  455. * If the CPU was idle with dynamic ticks active, update the
  456. * rcu_dyntick_sched.dynticks to put let the RCU handling be
  457. * aware that the CPU is going back to idle with no ticks.
  458. */
  459. void rcu_irq_exit(void)
  460. {
  461. int cpu = smp_processor_id();
  462. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  463. /*
  464. * rcu_update_flag is set if we interrupted the CPU
  465. * when it was idle with ticks stopped.
  466. * Once this occurs, we keep track of interrupt nesting
  467. * because a NMI/SMI could also come in, and we still
  468. * only want the IRQ that started the increment of the
  469. * rcu_dyntick_sched.dynticks to be the one that modifies
  470. * it on exit.
  471. */
  472. if (per_cpu(rcu_update_flag, cpu)) {
  473. if (--per_cpu(rcu_update_flag, cpu))
  474. return;
  475. /* This must match the interrupt nesting */
  476. WARN_ON(in_interrupt());
  477. /*
  478. * If an NMI/SMI happens now we are still
  479. * protected by the rcu_dyntick_sched.dynticks being odd.
  480. */
  481. /*
  482. * The following memory barrier ensures that any
  483. * rcu_read_unlock() primitives in the irq handler
  484. * are seen by other CPUs to preceed the following
  485. * increment to rcu_dyntick_sched.dynticks. This
  486. * is required in order for other CPUs to determine
  487. * when it is safe to advance the RCU grace-period
  488. * state machine.
  489. */
  490. smp_mb(); /* see above block comment. */
  491. rdssp->dynticks++;
  492. WARN_ON(rdssp->dynticks & 0x1);
  493. }
  494. }
  495. static void dyntick_save_progress_counter(int cpu)
  496. {
  497. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  498. rdssp->dynticks_snap = rdssp->dynticks;
  499. }
  500. static inline int
  501. rcu_try_flip_waitack_needed(int cpu)
  502. {
  503. long curr;
  504. long snap;
  505. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  506. curr = rdssp->dynticks;
  507. snap = rdssp->dynticks_snap;
  508. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  509. /*
  510. * If the CPU remained in dynticks mode for the entire time
  511. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  512. * then it cannot be in the middle of an rcu_read_lock(), so
  513. * the next rcu_read_lock() it executes must use the new value
  514. * of the counter. So we can safely pretend that this CPU
  515. * already acknowledged the counter.
  516. */
  517. if ((curr == snap) && ((curr & 0x1) == 0))
  518. return 0;
  519. /*
  520. * If the CPU passed through or entered a dynticks idle phase with
  521. * no active irq handlers, then, as above, we can safely pretend
  522. * that this CPU already acknowledged the counter.
  523. */
  524. if ((curr - snap) > 2 || (curr & 0x1) == 0)
  525. return 0;
  526. /* We need this CPU to explicitly acknowledge the counter flip. */
  527. return 1;
  528. }
  529. static inline int
  530. rcu_try_flip_waitmb_needed(int cpu)
  531. {
  532. long curr;
  533. long snap;
  534. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  535. curr = rdssp->dynticks;
  536. snap = rdssp->dynticks_snap;
  537. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  538. /*
  539. * If the CPU remained in dynticks mode for the entire time
  540. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  541. * then it cannot have executed an RCU read-side critical section
  542. * during that time, so there is no need for it to execute a
  543. * memory barrier.
  544. */
  545. if ((curr == snap) && ((curr & 0x1) == 0))
  546. return 0;
  547. /*
  548. * If the CPU either entered or exited an outermost interrupt,
  549. * SMI, NMI, or whatever handler, then we know that it executed
  550. * a memory barrier when doing so. So we don't need another one.
  551. */
  552. if (curr != snap)
  553. return 0;
  554. /* We need the CPU to execute a memory barrier. */
  555. return 1;
  556. }
  557. static void dyntick_save_progress_counter_sched(int cpu)
  558. {
  559. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  560. rdssp->sched_dynticks_snap = rdssp->dynticks;
  561. }
  562. static int rcu_qsctr_inc_needed_dyntick(int cpu)
  563. {
  564. long curr;
  565. long snap;
  566. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  567. curr = rdssp->dynticks;
  568. snap = rdssp->sched_dynticks_snap;
  569. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  570. /*
  571. * If the CPU remained in dynticks mode for the entire time
  572. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  573. * then it cannot be in the middle of an rcu_read_lock(), so
  574. * the next rcu_read_lock() it executes must use the new value
  575. * of the counter. Therefore, this CPU has been in a quiescent
  576. * state the entire time, and we don't need to wait for it.
  577. */
  578. if ((curr == snap) && ((curr & 0x1) == 0))
  579. return 0;
  580. /*
  581. * If the CPU passed through or entered a dynticks idle phase with
  582. * no active irq handlers, then, as above, this CPU has already
  583. * passed through a quiescent state.
  584. */
  585. if ((curr - snap) > 2 || (snap & 0x1) == 0)
  586. return 0;
  587. /* We need this CPU to go through a quiescent state. */
  588. return 1;
  589. }
  590. #else /* !CONFIG_NO_HZ */
  591. # define dyntick_save_progress_counter(cpu) do { } while (0)
  592. # define rcu_try_flip_waitack_needed(cpu) (1)
  593. # define rcu_try_flip_waitmb_needed(cpu) (1)
  594. # define dyntick_save_progress_counter_sched(cpu) do { } while (0)
  595. # define rcu_qsctr_inc_needed_dyntick(cpu) (1)
  596. #endif /* CONFIG_NO_HZ */
  597. static void save_qsctr_sched(int cpu)
  598. {
  599. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  600. rdssp->sched_qs_snap = rdssp->sched_qs;
  601. }
  602. static inline int rcu_qsctr_inc_needed(int cpu)
  603. {
  604. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  605. /*
  606. * If there has been a quiescent state, no more need to wait
  607. * on this CPU.
  608. */
  609. if (rdssp->sched_qs != rdssp->sched_qs_snap) {
  610. smp_mb(); /* force ordering with cpu entering schedule(). */
  611. return 0;
  612. }
  613. /* We need this CPU to go through a quiescent state. */
  614. return 1;
  615. }
  616. /*
  617. * Get here when RCU is idle. Decide whether we need to
  618. * move out of idle state, and return non-zero if so.
  619. * "Straightforward" approach for the moment, might later
  620. * use callback-list lengths, grace-period duration, or
  621. * some such to determine when to exit idle state.
  622. * Might also need a pre-idle test that does not acquire
  623. * the lock, but let's get the simple case working first...
  624. */
  625. static int
  626. rcu_try_flip_idle(void)
  627. {
  628. int cpu;
  629. RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
  630. if (!rcu_pending(smp_processor_id())) {
  631. RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
  632. return 0;
  633. }
  634. /*
  635. * Do the flip.
  636. */
  637. RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
  638. rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
  639. /*
  640. * Need a memory barrier so that other CPUs see the new
  641. * counter value before they see the subsequent change of all
  642. * the rcu_flip_flag instances to rcu_flipped.
  643. */
  644. smp_mb(); /* see above block comment. */
  645. /* Now ask each CPU for acknowledgement of the flip. */
  646. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  647. per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
  648. dyntick_save_progress_counter(cpu);
  649. }
  650. return 1;
  651. }
  652. /*
  653. * Wait for CPUs to acknowledge the flip.
  654. */
  655. static int
  656. rcu_try_flip_waitack(void)
  657. {
  658. int cpu;
  659. RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
  660. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  661. if (rcu_try_flip_waitack_needed(cpu) &&
  662. per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
  663. RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
  664. return 0;
  665. }
  666. /*
  667. * Make sure our checks above don't bleed into subsequent
  668. * waiting for the sum of the counters to reach zero.
  669. */
  670. smp_mb(); /* see above block comment. */
  671. RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
  672. return 1;
  673. }
  674. /*
  675. * Wait for collective ``last'' counter to reach zero,
  676. * then tell all CPUs to do an end-of-grace-period memory barrier.
  677. */
  678. static int
  679. rcu_try_flip_waitzero(void)
  680. {
  681. int cpu;
  682. int lastidx = !(rcu_ctrlblk.completed & 0x1);
  683. int sum = 0;
  684. /* Check to see if the sum of the "last" counters is zero. */
  685. RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
  686. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  687. sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
  688. if (sum != 0) {
  689. RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
  690. return 0;
  691. }
  692. /*
  693. * This ensures that the other CPUs see the call for
  694. * memory barriers -after- the sum to zero has been
  695. * detected here
  696. */
  697. smp_mb(); /* ^^^^^^^^^^^^ */
  698. /* Call for a memory barrier from each CPU. */
  699. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map) {
  700. per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
  701. dyntick_save_progress_counter(cpu);
  702. }
  703. RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
  704. return 1;
  705. }
  706. /*
  707. * Wait for all CPUs to do their end-of-grace-period memory barrier.
  708. * Return 0 once all CPUs have done so.
  709. */
  710. static int
  711. rcu_try_flip_waitmb(void)
  712. {
  713. int cpu;
  714. RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
  715. for_each_cpu_mask_nr(cpu, rcu_cpu_online_map)
  716. if (rcu_try_flip_waitmb_needed(cpu) &&
  717. per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
  718. RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
  719. return 0;
  720. }
  721. smp_mb(); /* Ensure that the above checks precede any following flip. */
  722. RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
  723. return 1;
  724. }
  725. /*
  726. * Attempt a single flip of the counters. Remember, a single flip does
  727. * -not- constitute a grace period. Instead, the interval between
  728. * at least GP_STAGES consecutive flips is a grace period.
  729. *
  730. * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
  731. * on a large SMP, they might want to use a hierarchical organization of
  732. * the per-CPU-counter pairs.
  733. */
  734. static void rcu_try_flip(void)
  735. {
  736. unsigned long flags;
  737. RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
  738. if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
  739. RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
  740. return;
  741. }
  742. /*
  743. * Take the next transition(s) through the RCU grace-period
  744. * flip-counter state machine.
  745. */
  746. switch (rcu_ctrlblk.rcu_try_flip_state) {
  747. case rcu_try_flip_idle_state:
  748. if (rcu_try_flip_idle())
  749. rcu_ctrlblk.rcu_try_flip_state =
  750. rcu_try_flip_waitack_state;
  751. break;
  752. case rcu_try_flip_waitack_state:
  753. if (rcu_try_flip_waitack())
  754. rcu_ctrlblk.rcu_try_flip_state =
  755. rcu_try_flip_waitzero_state;
  756. break;
  757. case rcu_try_flip_waitzero_state:
  758. if (rcu_try_flip_waitzero())
  759. rcu_ctrlblk.rcu_try_flip_state =
  760. rcu_try_flip_waitmb_state;
  761. break;
  762. case rcu_try_flip_waitmb_state:
  763. if (rcu_try_flip_waitmb())
  764. rcu_ctrlblk.rcu_try_flip_state =
  765. rcu_try_flip_idle_state;
  766. }
  767. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  768. }
  769. /*
  770. * Check to see if this CPU needs to do a memory barrier in order to
  771. * ensure that any prior RCU read-side critical sections have committed
  772. * their counter manipulations and critical-section memory references
  773. * before declaring the grace period to be completed.
  774. */
  775. static void rcu_check_mb(int cpu)
  776. {
  777. if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
  778. smp_mb(); /* Ensure RCU read-side accesses are visible. */
  779. per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
  780. }
  781. }
  782. void rcu_check_callbacks(int cpu, int user)
  783. {
  784. unsigned long flags;
  785. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  786. /*
  787. * If this CPU took its interrupt from user mode or from the
  788. * idle loop, and this is not a nested interrupt, then
  789. * this CPU has to have exited all prior preept-disable
  790. * sections of code. So increment the counter to note this.
  791. *
  792. * The memory barrier is needed to handle the case where
  793. * writes from a preempt-disable section of code get reordered
  794. * into schedule() by this CPU's write buffer. So the memory
  795. * barrier makes sure that the rcu_qsctr_inc() is seen by other
  796. * CPUs to happen after any such write.
  797. */
  798. if (user ||
  799. (idle_cpu(cpu) && !in_softirq() &&
  800. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  801. smp_mb(); /* Guard against aggressive schedule(). */
  802. rcu_qsctr_inc(cpu);
  803. }
  804. rcu_check_mb(cpu);
  805. if (rcu_ctrlblk.completed == rdp->completed)
  806. rcu_try_flip();
  807. spin_lock_irqsave(&rdp->lock, flags);
  808. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  809. __rcu_advance_callbacks(rdp);
  810. if (rdp->donelist == NULL) {
  811. spin_unlock_irqrestore(&rdp->lock, flags);
  812. } else {
  813. spin_unlock_irqrestore(&rdp->lock, flags);
  814. raise_softirq(RCU_SOFTIRQ);
  815. }
  816. }
  817. /*
  818. * Needed by dynticks, to make sure all RCU processing has finished
  819. * when we go idle:
  820. */
  821. void rcu_advance_callbacks(int cpu, int user)
  822. {
  823. unsigned long flags;
  824. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  825. if (rcu_ctrlblk.completed == rdp->completed) {
  826. rcu_try_flip();
  827. if (rcu_ctrlblk.completed == rdp->completed)
  828. return;
  829. }
  830. spin_lock_irqsave(&rdp->lock, flags);
  831. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  832. __rcu_advance_callbacks(rdp);
  833. spin_unlock_irqrestore(&rdp->lock, flags);
  834. }
  835. #ifdef CONFIG_HOTPLUG_CPU
  836. #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
  837. *dsttail = srclist; \
  838. if (srclist != NULL) { \
  839. dsttail = srctail; \
  840. srclist = NULL; \
  841. srctail = &srclist;\
  842. } \
  843. } while (0)
  844. void rcu_offline_cpu(int cpu)
  845. {
  846. int i;
  847. struct rcu_head *list = NULL;
  848. unsigned long flags;
  849. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  850. struct rcu_head *schedlist = NULL;
  851. struct rcu_head **schedtail = &schedlist;
  852. struct rcu_head **tail = &list;
  853. /*
  854. * Remove all callbacks from the newly dead CPU, retaining order.
  855. * Otherwise rcu_barrier() will fail
  856. */
  857. spin_lock_irqsave(&rdp->lock, flags);
  858. rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
  859. for (i = GP_STAGES - 1; i >= 0; i--)
  860. rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
  861. list, tail);
  862. rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
  863. rcu_offline_cpu_enqueue(rdp->waitschedlist, rdp->waitschedtail,
  864. schedlist, schedtail);
  865. rcu_offline_cpu_enqueue(rdp->nextschedlist, rdp->nextschedtail,
  866. schedlist, schedtail);
  867. rdp->rcu_sched_sleeping = 0;
  868. spin_unlock_irqrestore(&rdp->lock, flags);
  869. rdp->waitlistcount = 0;
  870. /* Disengage the newly dead CPU from the grace-period computation. */
  871. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  872. rcu_check_mb(cpu);
  873. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  874. smp_mb(); /* Subsequent counter accesses must see new value */
  875. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  876. smp_mb(); /* Subsequent RCU read-side critical sections */
  877. /* seen -after- acknowledgement. */
  878. }
  879. RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  880. RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
  881. RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
  882. RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
  883. cpu_clear(cpu, rcu_cpu_online_map);
  884. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  885. /*
  886. * Place the removed callbacks on the current CPU's queue.
  887. * Make them all start a new grace period: simple approach,
  888. * in theory could starve a given set of callbacks, but
  889. * you would need to be doing some serious CPU hotplugging
  890. * to make this happen. If this becomes a problem, adding
  891. * a synchronize_rcu() to the hotplug path would be a simple
  892. * fix.
  893. */
  894. local_irq_save(flags); /* disable preempt till we know what lock. */
  895. rdp = RCU_DATA_ME();
  896. spin_lock(&rdp->lock);
  897. *rdp->nexttail = list;
  898. if (list)
  899. rdp->nexttail = tail;
  900. *rdp->nextschedtail = schedlist;
  901. if (schedlist)
  902. rdp->nextschedtail = schedtail;
  903. spin_unlock_irqrestore(&rdp->lock, flags);
  904. }
  905. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  906. void rcu_offline_cpu(int cpu)
  907. {
  908. }
  909. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  910. void __cpuinit rcu_online_cpu(int cpu)
  911. {
  912. unsigned long flags;
  913. struct rcu_data *rdp;
  914. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  915. cpu_set(cpu, rcu_cpu_online_map);
  916. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  917. /*
  918. * The rcu_sched grace-period processing might have bypassed
  919. * this CPU, given that it was not in the rcu_cpu_online_map
  920. * when the grace-period scan started. This means that the
  921. * grace-period task might sleep. So make sure that if this
  922. * should happen, the first callback posted to this CPU will
  923. * wake up the grace-period task if need be.
  924. */
  925. rdp = RCU_DATA_CPU(cpu);
  926. spin_lock_irqsave(&rdp->lock, flags);
  927. rdp->rcu_sched_sleeping = 1;
  928. spin_unlock_irqrestore(&rdp->lock, flags);
  929. }
  930. static void rcu_process_callbacks(struct softirq_action *unused)
  931. {
  932. unsigned long flags;
  933. struct rcu_head *next, *list;
  934. struct rcu_data *rdp;
  935. local_irq_save(flags);
  936. rdp = RCU_DATA_ME();
  937. spin_lock(&rdp->lock);
  938. list = rdp->donelist;
  939. if (list == NULL) {
  940. spin_unlock_irqrestore(&rdp->lock, flags);
  941. return;
  942. }
  943. rdp->donelist = NULL;
  944. rdp->donetail = &rdp->donelist;
  945. RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
  946. spin_unlock_irqrestore(&rdp->lock, flags);
  947. while (list) {
  948. next = list->next;
  949. list->func(list);
  950. list = next;
  951. RCU_TRACE_ME(rcupreempt_trace_invoke);
  952. }
  953. }
  954. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  955. {
  956. unsigned long flags;
  957. struct rcu_data *rdp;
  958. head->func = func;
  959. head->next = NULL;
  960. local_irq_save(flags);
  961. rdp = RCU_DATA_ME();
  962. spin_lock(&rdp->lock);
  963. __rcu_advance_callbacks(rdp);
  964. *rdp->nexttail = head;
  965. rdp->nexttail = &head->next;
  966. RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
  967. spin_unlock_irqrestore(&rdp->lock, flags);
  968. }
  969. EXPORT_SYMBOL_GPL(call_rcu);
  970. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  971. {
  972. unsigned long flags;
  973. struct rcu_data *rdp;
  974. int wake_gp = 0;
  975. head->func = func;
  976. head->next = NULL;
  977. local_irq_save(flags);
  978. rdp = RCU_DATA_ME();
  979. spin_lock(&rdp->lock);
  980. *rdp->nextschedtail = head;
  981. rdp->nextschedtail = &head->next;
  982. if (rdp->rcu_sched_sleeping) {
  983. /* Grace-period processing might be sleeping... */
  984. rdp->rcu_sched_sleeping = 0;
  985. wake_gp = 1;
  986. }
  987. spin_unlock_irqrestore(&rdp->lock, flags);
  988. if (wake_gp) {
  989. /* Wake up grace-period processing, unless someone beat us. */
  990. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  991. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleeping)
  992. wake_gp = 0;
  993. rcu_ctrlblk.sched_sleep = rcu_sched_not_sleeping;
  994. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  995. if (wake_gp)
  996. wake_up_interruptible(&rcu_ctrlblk.sched_wq);
  997. }
  998. }
  999. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1000. /*
  1001. * Wait until all currently running preempt_disable() code segments
  1002. * (including hardware-irq-disable segments) complete. Note that
  1003. * in -rt this does -not- necessarily result in all currently executing
  1004. * interrupt -handlers- having completed.
  1005. */
  1006. synchronize_rcu_xxx(__synchronize_sched, call_rcu_sched)
  1007. EXPORT_SYMBOL_GPL(__synchronize_sched);
  1008. /*
  1009. * kthread function that manages call_rcu_sched grace periods.
  1010. */
  1011. static int rcu_sched_grace_period(void *arg)
  1012. {
  1013. int couldsleep; /* might sleep after current pass. */
  1014. int couldsleepnext = 0; /* might sleep after next pass. */
  1015. int cpu;
  1016. unsigned long flags;
  1017. struct rcu_data *rdp;
  1018. int ret;
  1019. /*
  1020. * Each pass through the following loop handles one
  1021. * rcu_sched grace period cycle.
  1022. */
  1023. do {
  1024. /* Save each CPU's current state. */
  1025. for_each_online_cpu(cpu) {
  1026. dyntick_save_progress_counter_sched(cpu);
  1027. save_qsctr_sched(cpu);
  1028. }
  1029. /*
  1030. * Sleep for about an RCU grace-period's worth to
  1031. * allow better batching and to consume less CPU.
  1032. */
  1033. schedule_timeout_interruptible(RCU_SCHED_BATCH_TIME);
  1034. /*
  1035. * If there was nothing to do last time, prepare to
  1036. * sleep at the end of the current grace period cycle.
  1037. */
  1038. couldsleep = couldsleepnext;
  1039. couldsleepnext = 1;
  1040. if (couldsleep) {
  1041. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1042. rcu_ctrlblk.sched_sleep = rcu_sched_sleep_prep;
  1043. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1044. }
  1045. /*
  1046. * Wait on each CPU in turn to have either visited
  1047. * a quiescent state or been in dynticks-idle mode.
  1048. */
  1049. for_each_online_cpu(cpu) {
  1050. while (rcu_qsctr_inc_needed(cpu) &&
  1051. rcu_qsctr_inc_needed_dyntick(cpu)) {
  1052. /* resched_cpu(cpu); @@@ */
  1053. schedule_timeout_interruptible(1);
  1054. }
  1055. }
  1056. /* Advance callbacks for each CPU. */
  1057. for_each_online_cpu(cpu) {
  1058. rdp = RCU_DATA_CPU(cpu);
  1059. spin_lock_irqsave(&rdp->lock, flags);
  1060. /*
  1061. * We are running on this CPU irq-disabled, so no
  1062. * CPU can go offline until we re-enable irqs.
  1063. * The current CPU might have already gone
  1064. * offline (between the for_each_offline_cpu and
  1065. * the spin_lock_irqsave), but in that case all its
  1066. * callback lists will be empty, so no harm done.
  1067. *
  1068. * Advance the callbacks! We share normal RCU's
  1069. * donelist, since callbacks are invoked the
  1070. * same way in either case.
  1071. */
  1072. if (rdp->waitschedlist != NULL) {
  1073. *rdp->donetail = rdp->waitschedlist;
  1074. rdp->donetail = rdp->waitschedtail;
  1075. /*
  1076. * Next rcu_check_callbacks() will
  1077. * do the required raise_softirq().
  1078. */
  1079. }
  1080. if (rdp->nextschedlist != NULL) {
  1081. rdp->waitschedlist = rdp->nextschedlist;
  1082. rdp->waitschedtail = rdp->nextschedtail;
  1083. couldsleep = 0;
  1084. couldsleepnext = 0;
  1085. } else {
  1086. rdp->waitschedlist = NULL;
  1087. rdp->waitschedtail = &rdp->waitschedlist;
  1088. }
  1089. rdp->nextschedlist = NULL;
  1090. rdp->nextschedtail = &rdp->nextschedlist;
  1091. /* Mark sleep intention. */
  1092. rdp->rcu_sched_sleeping = couldsleep;
  1093. spin_unlock_irqrestore(&rdp->lock, flags);
  1094. }
  1095. /* If we saw callbacks on the last scan, go deal with them. */
  1096. if (!couldsleep)
  1097. continue;
  1098. /* Attempt to block... */
  1099. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1100. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleep_prep) {
  1101. /*
  1102. * Someone posted a callback after we scanned.
  1103. * Go take care of it.
  1104. */
  1105. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1106. couldsleepnext = 0;
  1107. continue;
  1108. }
  1109. /* Block until the next person posts a callback. */
  1110. rcu_ctrlblk.sched_sleep = rcu_sched_sleeping;
  1111. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1112. ret = 0;
  1113. __wait_event_interruptible(rcu_ctrlblk.sched_wq,
  1114. rcu_ctrlblk.sched_sleep != rcu_sched_sleeping,
  1115. ret);
  1116. /*
  1117. * Signals would prevent us from sleeping, and we cannot
  1118. * do much with them in any case. So flush them.
  1119. */
  1120. if (ret)
  1121. flush_signals(current);
  1122. couldsleepnext = 0;
  1123. } while (!kthread_should_stop());
  1124. return (0);
  1125. }
  1126. /*
  1127. * Check to see if any future RCU-related work will need to be done
  1128. * by the current CPU, even if none need be done immediately, returning
  1129. * 1 if so. Assumes that notifiers would take care of handling any
  1130. * outstanding requests from the RCU core.
  1131. *
  1132. * This function is part of the RCU implementation; it is -not-
  1133. * an exported member of the RCU API.
  1134. */
  1135. int rcu_needs_cpu(int cpu)
  1136. {
  1137. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1138. return (rdp->donelist != NULL ||
  1139. !!rdp->waitlistcount ||
  1140. rdp->nextlist != NULL ||
  1141. rdp->nextschedlist != NULL ||
  1142. rdp->waitschedlist != NULL);
  1143. }
  1144. int rcu_pending(int cpu)
  1145. {
  1146. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1147. /* The CPU has at least one callback queued somewhere. */
  1148. if (rdp->donelist != NULL ||
  1149. !!rdp->waitlistcount ||
  1150. rdp->nextlist != NULL ||
  1151. rdp->nextschedlist != NULL ||
  1152. rdp->waitschedlist != NULL)
  1153. return 1;
  1154. /* The RCU core needs an acknowledgement from this CPU. */
  1155. if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
  1156. (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
  1157. return 1;
  1158. /* This CPU has fallen behind the global grace-period number. */
  1159. if (rdp->completed != rcu_ctrlblk.completed)
  1160. return 1;
  1161. /* Nothing needed from this CPU. */
  1162. return 0;
  1163. }
  1164. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1165. unsigned long action, void *hcpu)
  1166. {
  1167. long cpu = (long)hcpu;
  1168. switch (action) {
  1169. case CPU_UP_PREPARE:
  1170. case CPU_UP_PREPARE_FROZEN:
  1171. rcu_online_cpu(cpu);
  1172. break;
  1173. case CPU_UP_CANCELED:
  1174. case CPU_UP_CANCELED_FROZEN:
  1175. case CPU_DEAD:
  1176. case CPU_DEAD_FROZEN:
  1177. rcu_offline_cpu(cpu);
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. return NOTIFY_OK;
  1183. }
  1184. static struct notifier_block __cpuinitdata rcu_nb = {
  1185. .notifier_call = rcu_cpu_notify,
  1186. };
  1187. void __init __rcu_init(void)
  1188. {
  1189. int cpu;
  1190. int i;
  1191. struct rcu_data *rdp;
  1192. printk(KERN_NOTICE "Preemptible RCU implementation.\n");
  1193. for_each_possible_cpu(cpu) {
  1194. rdp = RCU_DATA_CPU(cpu);
  1195. spin_lock_init(&rdp->lock);
  1196. rdp->completed = 0;
  1197. rdp->waitlistcount = 0;
  1198. rdp->nextlist = NULL;
  1199. rdp->nexttail = &rdp->nextlist;
  1200. for (i = 0; i < GP_STAGES; i++) {
  1201. rdp->waitlist[i] = NULL;
  1202. rdp->waittail[i] = &rdp->waitlist[i];
  1203. }
  1204. rdp->donelist = NULL;
  1205. rdp->donetail = &rdp->donelist;
  1206. rdp->rcu_flipctr[0] = 0;
  1207. rdp->rcu_flipctr[1] = 0;
  1208. rdp->nextschedlist = NULL;
  1209. rdp->nextschedtail = &rdp->nextschedlist;
  1210. rdp->waitschedlist = NULL;
  1211. rdp->waitschedtail = &rdp->waitschedlist;
  1212. rdp->rcu_sched_sleeping = 0;
  1213. }
  1214. register_cpu_notifier(&rcu_nb);
  1215. /*
  1216. * We don't need protection against CPU-Hotplug here
  1217. * since
  1218. * a) If a CPU comes online while we are iterating over the
  1219. * cpu_online_map below, we would only end up making a
  1220. * duplicate call to rcu_online_cpu() which sets the corresponding
  1221. * CPU's mask in the rcu_cpu_online_map.
  1222. *
  1223. * b) A CPU cannot go offline at this point in time since the user
  1224. * does not have access to the sysfs interface, nor do we
  1225. * suspend the system.
  1226. */
  1227. for_each_online_cpu(cpu)
  1228. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
  1229. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1230. }
  1231. /*
  1232. * Late-boot-time RCU initialization that must wait until after scheduler
  1233. * has been initialized.
  1234. */
  1235. void __init rcu_init_sched(void)
  1236. {
  1237. rcu_sched_grace_period_task = kthread_run(rcu_sched_grace_period,
  1238. NULL,
  1239. "rcu_sched_grace_period");
  1240. WARN_ON(IS_ERR(rcu_sched_grace_period_task));
  1241. }
  1242. #ifdef CONFIG_RCU_TRACE
  1243. long *rcupreempt_flipctr(int cpu)
  1244. {
  1245. return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  1246. }
  1247. EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
  1248. int rcupreempt_flip_flag(int cpu)
  1249. {
  1250. return per_cpu(rcu_flip_flag, cpu);
  1251. }
  1252. EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
  1253. int rcupreempt_mb_flag(int cpu)
  1254. {
  1255. return per_cpu(rcu_mb_flag, cpu);
  1256. }
  1257. EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
  1258. char *rcupreempt_try_flip_state_name(void)
  1259. {
  1260. return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
  1261. }
  1262. EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
  1263. struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
  1264. {
  1265. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1266. return &rdp->trace;
  1267. }
  1268. EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
  1269. #endif /* #ifdef RCU_TRACE */