rcupreempt.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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/completion.h>
  50. #include <linux/moduleparam.h>
  51. #include <linux/percpu.h>
  52. #include <linux/notifier.h>
  53. #include <linux/rcupdate.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 tasklet_struct rcu_tasklet;
  83. struct rcu_head *nextlist;
  84. struct rcu_head **nexttail;
  85. struct rcu_head *waitlist[GP_STAGES];
  86. struct rcu_head **waittail[GP_STAGES];
  87. struct rcu_head *donelist;
  88. struct rcu_head **donetail;
  89. long rcu_flipctr[2];
  90. #ifdef CONFIG_RCU_TRACE
  91. struct rcupreempt_trace trace;
  92. #endif /* #ifdef CONFIG_RCU_TRACE */
  93. };
  94. /*
  95. * States for rcu_try_flip() and friends.
  96. */
  97. enum rcu_try_flip_states {
  98. /*
  99. * Stay here if nothing is happening. Flip the counter if somthing
  100. * starts happening. Denoted by "I"
  101. */
  102. rcu_try_flip_idle_state,
  103. /*
  104. * Wait here for all CPUs to notice that the counter has flipped. This
  105. * prevents the old set of counters from ever being incremented once
  106. * we leave this state, which in turn is necessary because we cannot
  107. * test any individual counter for zero -- we can only check the sum.
  108. * Denoted by "A".
  109. */
  110. rcu_try_flip_waitack_state,
  111. /*
  112. * Wait here for the sum of the old per-CPU counters to reach zero.
  113. * Denoted by "Z".
  114. */
  115. rcu_try_flip_waitzero_state,
  116. /*
  117. * Wait here for each of the other CPUs to execute a memory barrier.
  118. * This is necessary to ensure that these other CPUs really have
  119. * completed executing their RCU read-side critical sections, despite
  120. * their CPUs wildly reordering memory. Denoted by "M".
  121. */
  122. rcu_try_flip_waitmb_state,
  123. };
  124. struct rcu_ctrlblk {
  125. spinlock_t fliplock; /* Protect state-machine transitions. */
  126. long completed; /* Number of last completed batch. */
  127. enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
  128. the rcu state machine */
  129. };
  130. static DEFINE_PER_CPU(struct rcu_data, rcu_data);
  131. static struct rcu_ctrlblk rcu_ctrlblk = {
  132. .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
  133. .completed = 0,
  134. .rcu_try_flip_state = rcu_try_flip_idle_state,
  135. };
  136. #ifdef CONFIG_RCU_TRACE
  137. static char *rcu_try_flip_state_names[] =
  138. { "idle", "waitack", "waitzero", "waitmb" };
  139. #endif /* #ifdef CONFIG_RCU_TRACE */
  140. static cpumask_t rcu_cpu_online_map __read_mostly = CPU_MASK_NONE;
  141. /*
  142. * Enum and per-CPU flag to determine when each CPU has seen
  143. * the most recent counter flip.
  144. */
  145. enum rcu_flip_flag_values {
  146. rcu_flip_seen, /* Steady/initial state, last flip seen. */
  147. /* Only GP detector can update. */
  148. rcu_flipped /* Flip just completed, need confirmation. */
  149. /* Only corresponding CPU can update. */
  150. };
  151. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
  152. = rcu_flip_seen;
  153. /*
  154. * Enum and per-CPU flag to determine when each CPU has executed the
  155. * needed memory barrier to fence in memory references from its last RCU
  156. * read-side critical section in the just-completed grace period.
  157. */
  158. enum rcu_mb_flag_values {
  159. rcu_mb_done, /* Steady/initial state, no mb()s required. */
  160. /* Only GP detector can update. */
  161. rcu_mb_needed /* Flip just completed, need an mb(). */
  162. /* Only corresponding CPU can update. */
  163. };
  164. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
  165. = rcu_mb_done;
  166. /*
  167. * RCU_DATA_ME: find the current CPU's rcu_data structure.
  168. * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
  169. */
  170. #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
  171. #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
  172. /*
  173. * Helper macro for tracing when the appropriate rcu_data is not
  174. * cached in a local variable, but where the CPU number is so cached.
  175. */
  176. #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
  177. /*
  178. * Helper macro for tracing when the appropriate rcu_data is not
  179. * cached in a local variable.
  180. */
  181. #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
  182. /*
  183. * Helper macro for tracing when the appropriate rcu_data is pointed
  184. * to by a local variable.
  185. */
  186. #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
  187. /*
  188. * Return the number of RCU batches processed thus far. Useful
  189. * for debug and statistics.
  190. */
  191. long rcu_batches_completed(void)
  192. {
  193. return rcu_ctrlblk.completed;
  194. }
  195. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  196. EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
  197. void __rcu_read_lock(void)
  198. {
  199. int idx;
  200. struct task_struct *t = current;
  201. int nesting;
  202. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  203. if (nesting != 0) {
  204. /* An earlier rcu_read_lock() covers us, just count it. */
  205. t->rcu_read_lock_nesting = nesting + 1;
  206. } else {
  207. unsigned long flags;
  208. /*
  209. * We disable interrupts for the following reasons:
  210. * - If we get scheduling clock interrupt here, and we
  211. * end up acking the counter flip, it's like a promise
  212. * that we will never increment the old counter again.
  213. * Thus we will break that promise if that
  214. * scheduling clock interrupt happens between the time
  215. * we pick the .completed field and the time that we
  216. * increment our counter.
  217. *
  218. * - We don't want to be preempted out here.
  219. *
  220. * NMIs can still occur, of course, and might themselves
  221. * contain rcu_read_lock().
  222. */
  223. local_irq_save(flags);
  224. /*
  225. * Outermost nesting of rcu_read_lock(), so increment
  226. * the current counter for the current CPU. Use volatile
  227. * casts to prevent the compiler from reordering.
  228. */
  229. idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
  230. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
  231. /*
  232. * Now that the per-CPU counter has been incremented, we
  233. * are protected from races with rcu_read_lock() invoked
  234. * from NMI handlers on this CPU. We can therefore safely
  235. * increment the nesting counter, relieving further NMIs
  236. * of the need to increment the per-CPU counter.
  237. */
  238. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
  239. /*
  240. * Now that we have preventing any NMIs from storing
  241. * to the ->rcu_flipctr_idx, we can safely use it to
  242. * remember which counter to decrement in the matching
  243. * rcu_read_unlock().
  244. */
  245. ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
  246. local_irq_restore(flags);
  247. }
  248. }
  249. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  250. void __rcu_read_unlock(void)
  251. {
  252. int idx;
  253. struct task_struct *t = current;
  254. int nesting;
  255. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  256. if (nesting > 1) {
  257. /*
  258. * We are still protected by the enclosing rcu_read_lock(),
  259. * so simply decrement the counter.
  260. */
  261. t->rcu_read_lock_nesting = nesting - 1;
  262. } else {
  263. unsigned long flags;
  264. /*
  265. * Disable local interrupts to prevent the grace-period
  266. * detection state machine from seeing us half-done.
  267. * NMIs can still occur, of course, and might themselves
  268. * contain rcu_read_lock() and rcu_read_unlock().
  269. */
  270. local_irq_save(flags);
  271. /*
  272. * Outermost nesting of rcu_read_unlock(), so we must
  273. * decrement the current counter for the current CPU.
  274. * This must be done carefully, because NMIs can
  275. * occur at any point in this code, and any rcu_read_lock()
  276. * and rcu_read_unlock() pairs in the NMI handlers
  277. * must interact non-destructively with this code.
  278. * Lots of volatile casts, and -very- careful ordering.
  279. *
  280. * Changes to this code, including this one, must be
  281. * inspected, validated, and tested extremely carefully!!!
  282. */
  283. /*
  284. * First, pick up the index.
  285. */
  286. idx = ACCESS_ONCE(t->rcu_flipctr_idx);
  287. /*
  288. * Now that we have fetched the counter index, it is
  289. * safe to decrement the per-task RCU nesting counter.
  290. * After this, any interrupts or NMIs will increment and
  291. * decrement the per-CPU counters.
  292. */
  293. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
  294. /*
  295. * It is now safe to decrement this task's nesting count.
  296. * NMIs that occur after this statement will route their
  297. * rcu_read_lock() calls through this "else" clause, and
  298. * will thus start incrementing the per-CPU counter on
  299. * their own. They will also clobber ->rcu_flipctr_idx,
  300. * but that is OK, since we have already fetched it.
  301. */
  302. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
  303. local_irq_restore(flags);
  304. }
  305. }
  306. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  307. /*
  308. * If a global counter flip has occurred since the last time that we
  309. * advanced callbacks, advance them. Hardware interrupts must be
  310. * disabled when calling this function.
  311. */
  312. static void __rcu_advance_callbacks(struct rcu_data *rdp)
  313. {
  314. int cpu;
  315. int i;
  316. int wlc = 0;
  317. if (rdp->completed != rcu_ctrlblk.completed) {
  318. if (rdp->waitlist[GP_STAGES - 1] != NULL) {
  319. *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
  320. rdp->donetail = rdp->waittail[GP_STAGES - 1];
  321. RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
  322. }
  323. for (i = GP_STAGES - 2; i >= 0; i--) {
  324. if (rdp->waitlist[i] != NULL) {
  325. rdp->waitlist[i + 1] = rdp->waitlist[i];
  326. rdp->waittail[i + 1] = rdp->waittail[i];
  327. wlc++;
  328. } else {
  329. rdp->waitlist[i + 1] = NULL;
  330. rdp->waittail[i + 1] =
  331. &rdp->waitlist[i + 1];
  332. }
  333. }
  334. if (rdp->nextlist != NULL) {
  335. rdp->waitlist[0] = rdp->nextlist;
  336. rdp->waittail[0] = rdp->nexttail;
  337. wlc++;
  338. rdp->nextlist = NULL;
  339. rdp->nexttail = &rdp->nextlist;
  340. RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
  341. } else {
  342. rdp->waitlist[0] = NULL;
  343. rdp->waittail[0] = &rdp->waitlist[0];
  344. }
  345. rdp->waitlistcount = wlc;
  346. rdp->completed = rcu_ctrlblk.completed;
  347. }
  348. /*
  349. * Check to see if this CPU needs to report that it has seen
  350. * the most recent counter flip, thereby declaring that all
  351. * subsequent rcu_read_lock() invocations will respect this flip.
  352. */
  353. cpu = raw_smp_processor_id();
  354. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  355. smp_mb(); /* Subsequent counter accesses must see new value */
  356. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  357. smp_mb(); /* Subsequent RCU read-side critical sections */
  358. /* seen -after- acknowledgement. */
  359. }
  360. }
  361. #ifdef CONFIG_NO_HZ
  362. DEFINE_PER_CPU(long, dynticks_progress_counter) = 1;
  363. static DEFINE_PER_CPU(long, rcu_dyntick_snapshot);
  364. static DEFINE_PER_CPU(int, rcu_update_flag);
  365. /**
  366. * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
  367. *
  368. * If the CPU was idle with dynamic ticks active, this updates the
  369. * dynticks_progress_counter to let the RCU handling know that the
  370. * CPU is active.
  371. */
  372. void rcu_irq_enter(void)
  373. {
  374. int cpu = smp_processor_id();
  375. if (per_cpu(rcu_update_flag, cpu))
  376. per_cpu(rcu_update_flag, cpu)++;
  377. /*
  378. * Only update if we are coming from a stopped ticks mode
  379. * (dynticks_progress_counter is even).
  380. */
  381. if (!in_interrupt() &&
  382. (per_cpu(dynticks_progress_counter, cpu) & 0x1) == 0) {
  383. /*
  384. * The following might seem like we could have a race
  385. * with NMI/SMIs. But this really isn't a problem.
  386. * Here we do a read/modify/write, and the race happens
  387. * when an NMI/SMI comes in after the read and before
  388. * the write. But NMI/SMIs will increment this counter
  389. * twice before returning, so the zero bit will not
  390. * be corrupted by the NMI/SMI which is the most important
  391. * part.
  392. *
  393. * The only thing is that we would bring back the counter
  394. * to a postion that it was in during the NMI/SMI.
  395. * But the zero bit would be set, so the rest of the
  396. * counter would again be ignored.
  397. *
  398. * On return from the IRQ, the counter may have the zero
  399. * bit be 0 and the counter the same as the return from
  400. * the NMI/SMI. If the state machine was so unlucky to
  401. * see that, it still doesn't matter, since all
  402. * RCU read-side critical sections on this CPU would
  403. * have already completed.
  404. */
  405. per_cpu(dynticks_progress_counter, cpu)++;
  406. /*
  407. * The following memory barrier ensures that any
  408. * rcu_read_lock() primitives in the irq handler
  409. * are seen by other CPUs to follow the above
  410. * increment to dynticks_progress_counter. This is
  411. * required in order for other CPUs to correctly
  412. * determine when it is safe to advance the RCU
  413. * grace-period state machine.
  414. */
  415. smp_mb(); /* see above block comment. */
  416. /*
  417. * Since we can't determine the dynamic tick mode from
  418. * the dynticks_progress_counter after this routine,
  419. * we use a second flag to acknowledge that we came
  420. * from an idle state with ticks stopped.
  421. */
  422. per_cpu(rcu_update_flag, cpu)++;
  423. /*
  424. * If we take an NMI/SMI now, they will also increment
  425. * the rcu_update_flag, and will not update the
  426. * dynticks_progress_counter on exit. That is for
  427. * this IRQ to do.
  428. */
  429. }
  430. }
  431. /**
  432. * rcu_irq_exit - Called from exiting Hard irq context.
  433. *
  434. * If the CPU was idle with dynamic ticks active, update the
  435. * dynticks_progress_counter to put let the RCU handling be
  436. * aware that the CPU is going back to idle with no ticks.
  437. */
  438. void rcu_irq_exit(void)
  439. {
  440. int cpu = smp_processor_id();
  441. /*
  442. * rcu_update_flag is set if we interrupted the CPU
  443. * when it was idle with ticks stopped.
  444. * Once this occurs, we keep track of interrupt nesting
  445. * because a NMI/SMI could also come in, and we still
  446. * only want the IRQ that started the increment of the
  447. * dynticks_progress_counter to be the one that modifies
  448. * it on exit.
  449. */
  450. if (per_cpu(rcu_update_flag, cpu)) {
  451. if (--per_cpu(rcu_update_flag, cpu))
  452. return;
  453. /* This must match the interrupt nesting */
  454. WARN_ON(in_interrupt());
  455. /*
  456. * If an NMI/SMI happens now we are still
  457. * protected by the dynticks_progress_counter being odd.
  458. */
  459. /*
  460. * The following memory barrier ensures that any
  461. * rcu_read_unlock() primitives in the irq handler
  462. * are seen by other CPUs to preceed the following
  463. * increment to dynticks_progress_counter. This
  464. * is required in order for other CPUs to determine
  465. * when it is safe to advance the RCU grace-period
  466. * state machine.
  467. */
  468. smp_mb(); /* see above block comment. */
  469. per_cpu(dynticks_progress_counter, cpu)++;
  470. WARN_ON(per_cpu(dynticks_progress_counter, cpu) & 0x1);
  471. }
  472. }
  473. static void dyntick_save_progress_counter(int cpu)
  474. {
  475. per_cpu(rcu_dyntick_snapshot, cpu) =
  476. per_cpu(dynticks_progress_counter, cpu);
  477. }
  478. static inline int
  479. rcu_try_flip_waitack_needed(int cpu)
  480. {
  481. long curr;
  482. long snap;
  483. curr = per_cpu(dynticks_progress_counter, cpu);
  484. snap = per_cpu(rcu_dyntick_snapshot, cpu);
  485. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  486. /*
  487. * If the CPU remained in dynticks mode for the entire time
  488. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  489. * then it cannot be in the middle of an rcu_read_lock(), so
  490. * the next rcu_read_lock() it executes must use the new value
  491. * of the counter. So we can safely pretend that this CPU
  492. * already acknowledged the counter.
  493. */
  494. if ((curr == snap) && ((curr & 0x1) == 0))
  495. return 0;
  496. /*
  497. * If the CPU passed through or entered a dynticks idle phase with
  498. * no active irq handlers, then, as above, we can safely pretend
  499. * that this CPU already acknowledged the counter.
  500. */
  501. if ((curr - snap) > 2 || (snap & 0x1) == 0)
  502. return 0;
  503. /* We need this CPU to explicitly acknowledge the counter flip. */
  504. return 1;
  505. }
  506. static inline int
  507. rcu_try_flip_waitmb_needed(int cpu)
  508. {
  509. long curr;
  510. long snap;
  511. curr = per_cpu(dynticks_progress_counter, cpu);
  512. snap = per_cpu(rcu_dyntick_snapshot, cpu);
  513. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  514. /*
  515. * If the CPU remained in dynticks mode for the entire time
  516. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  517. * then it cannot have executed an RCU read-side critical section
  518. * during that time, so there is no need for it to execute a
  519. * memory barrier.
  520. */
  521. if ((curr == snap) && ((curr & 0x1) == 0))
  522. return 0;
  523. /*
  524. * If the CPU either entered or exited an outermost interrupt,
  525. * SMI, NMI, or whatever handler, then we know that it executed
  526. * a memory barrier when doing so. So we don't need another one.
  527. */
  528. if (curr != snap)
  529. return 0;
  530. /* We need the CPU to execute a memory barrier. */
  531. return 1;
  532. }
  533. #else /* !CONFIG_NO_HZ */
  534. # define dyntick_save_progress_counter(cpu) do { } while (0)
  535. # define rcu_try_flip_waitack_needed(cpu) (1)
  536. # define rcu_try_flip_waitmb_needed(cpu) (1)
  537. #endif /* CONFIG_NO_HZ */
  538. /*
  539. * Get here when RCU is idle. Decide whether we need to
  540. * move out of idle state, and return non-zero if so.
  541. * "Straightforward" approach for the moment, might later
  542. * use callback-list lengths, grace-period duration, or
  543. * some such to determine when to exit idle state.
  544. * Might also need a pre-idle test that does not acquire
  545. * the lock, but let's get the simple case working first...
  546. */
  547. static int
  548. rcu_try_flip_idle(void)
  549. {
  550. int cpu;
  551. RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
  552. if (!rcu_pending(smp_processor_id())) {
  553. RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
  554. return 0;
  555. }
  556. /*
  557. * Do the flip.
  558. */
  559. RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
  560. rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
  561. /*
  562. * Need a memory barrier so that other CPUs see the new
  563. * counter value before they see the subsequent change of all
  564. * the rcu_flip_flag instances to rcu_flipped.
  565. */
  566. smp_mb(); /* see above block comment. */
  567. /* Now ask each CPU for acknowledgement of the flip. */
  568. for_each_cpu_mask(cpu, rcu_cpu_online_map) {
  569. per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
  570. dyntick_save_progress_counter(cpu);
  571. }
  572. return 1;
  573. }
  574. /*
  575. * Wait for CPUs to acknowledge the flip.
  576. */
  577. static int
  578. rcu_try_flip_waitack(void)
  579. {
  580. int cpu;
  581. RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
  582. for_each_cpu_mask(cpu, rcu_cpu_online_map)
  583. if (rcu_try_flip_waitack_needed(cpu) &&
  584. per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
  585. RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
  586. return 0;
  587. }
  588. /*
  589. * Make sure our checks above don't bleed into subsequent
  590. * waiting for the sum of the counters to reach zero.
  591. */
  592. smp_mb(); /* see above block comment. */
  593. RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
  594. return 1;
  595. }
  596. /*
  597. * Wait for collective ``last'' counter to reach zero,
  598. * then tell all CPUs to do an end-of-grace-period memory barrier.
  599. */
  600. static int
  601. rcu_try_flip_waitzero(void)
  602. {
  603. int cpu;
  604. int lastidx = !(rcu_ctrlblk.completed & 0x1);
  605. int sum = 0;
  606. /* Check to see if the sum of the "last" counters is zero. */
  607. RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
  608. for_each_cpu_mask(cpu, rcu_cpu_online_map)
  609. sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
  610. if (sum != 0) {
  611. RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
  612. return 0;
  613. }
  614. /*
  615. * This ensures that the other CPUs see the call for
  616. * memory barriers -after- the sum to zero has been
  617. * detected here
  618. */
  619. smp_mb(); /* ^^^^^^^^^^^^ */
  620. /* Call for a memory barrier from each CPU. */
  621. for_each_cpu_mask(cpu, rcu_cpu_online_map) {
  622. per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
  623. dyntick_save_progress_counter(cpu);
  624. }
  625. RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
  626. return 1;
  627. }
  628. /*
  629. * Wait for all CPUs to do their end-of-grace-period memory barrier.
  630. * Return 0 once all CPUs have done so.
  631. */
  632. static int
  633. rcu_try_flip_waitmb(void)
  634. {
  635. int cpu;
  636. RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
  637. for_each_cpu_mask(cpu, rcu_cpu_online_map)
  638. if (rcu_try_flip_waitmb_needed(cpu) &&
  639. per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
  640. RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
  641. return 0;
  642. }
  643. smp_mb(); /* Ensure that the above checks precede any following flip. */
  644. RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
  645. return 1;
  646. }
  647. /*
  648. * Attempt a single flip of the counters. Remember, a single flip does
  649. * -not- constitute a grace period. Instead, the interval between
  650. * at least GP_STAGES consecutive flips is a grace period.
  651. *
  652. * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
  653. * on a large SMP, they might want to use a hierarchical organization of
  654. * the per-CPU-counter pairs.
  655. */
  656. static void rcu_try_flip(void)
  657. {
  658. unsigned long flags;
  659. RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
  660. if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
  661. RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
  662. return;
  663. }
  664. /*
  665. * Take the next transition(s) through the RCU grace-period
  666. * flip-counter state machine.
  667. */
  668. switch (rcu_ctrlblk.rcu_try_flip_state) {
  669. case rcu_try_flip_idle_state:
  670. if (rcu_try_flip_idle())
  671. rcu_ctrlblk.rcu_try_flip_state =
  672. rcu_try_flip_waitack_state;
  673. break;
  674. case rcu_try_flip_waitack_state:
  675. if (rcu_try_flip_waitack())
  676. rcu_ctrlblk.rcu_try_flip_state =
  677. rcu_try_flip_waitzero_state;
  678. break;
  679. case rcu_try_flip_waitzero_state:
  680. if (rcu_try_flip_waitzero())
  681. rcu_ctrlblk.rcu_try_flip_state =
  682. rcu_try_flip_waitmb_state;
  683. break;
  684. case rcu_try_flip_waitmb_state:
  685. if (rcu_try_flip_waitmb())
  686. rcu_ctrlblk.rcu_try_flip_state =
  687. rcu_try_flip_idle_state;
  688. }
  689. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  690. }
  691. /*
  692. * Check to see if this CPU needs to do a memory barrier in order to
  693. * ensure that any prior RCU read-side critical sections have committed
  694. * their counter manipulations and critical-section memory references
  695. * before declaring the grace period to be completed.
  696. */
  697. static void rcu_check_mb(int cpu)
  698. {
  699. if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
  700. smp_mb(); /* Ensure RCU read-side accesses are visible. */
  701. per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
  702. }
  703. }
  704. void rcu_check_callbacks(int cpu, int user)
  705. {
  706. unsigned long flags;
  707. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  708. rcu_check_mb(cpu);
  709. if (rcu_ctrlblk.completed == rdp->completed)
  710. rcu_try_flip();
  711. spin_lock_irqsave(&rdp->lock, flags);
  712. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  713. __rcu_advance_callbacks(rdp);
  714. if (rdp->donelist == NULL) {
  715. spin_unlock_irqrestore(&rdp->lock, flags);
  716. } else {
  717. spin_unlock_irqrestore(&rdp->lock, flags);
  718. raise_softirq(RCU_SOFTIRQ);
  719. }
  720. }
  721. /*
  722. * Needed by dynticks, to make sure all RCU processing has finished
  723. * when we go idle:
  724. */
  725. void rcu_advance_callbacks(int cpu, int user)
  726. {
  727. unsigned long flags;
  728. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  729. if (rcu_ctrlblk.completed == rdp->completed) {
  730. rcu_try_flip();
  731. if (rcu_ctrlblk.completed == rdp->completed)
  732. return;
  733. }
  734. spin_lock_irqsave(&rdp->lock, flags);
  735. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  736. __rcu_advance_callbacks(rdp);
  737. spin_unlock_irqrestore(&rdp->lock, flags);
  738. }
  739. #ifdef CONFIG_HOTPLUG_CPU
  740. #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
  741. *dsttail = srclist; \
  742. if (srclist != NULL) { \
  743. dsttail = srctail; \
  744. srclist = NULL; \
  745. srctail = &srclist;\
  746. } \
  747. } while (0)
  748. void rcu_offline_cpu(int cpu)
  749. {
  750. int i;
  751. struct rcu_head *list = NULL;
  752. unsigned long flags;
  753. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  754. struct rcu_head **tail = &list;
  755. /*
  756. * Remove all callbacks from the newly dead CPU, retaining order.
  757. * Otherwise rcu_barrier() will fail
  758. */
  759. spin_lock_irqsave(&rdp->lock, flags);
  760. rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
  761. for (i = GP_STAGES - 1; i >= 0; i--)
  762. rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
  763. list, tail);
  764. rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
  765. spin_unlock_irqrestore(&rdp->lock, flags);
  766. rdp->waitlistcount = 0;
  767. /* Disengage the newly dead CPU from the grace-period computation. */
  768. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  769. rcu_check_mb(cpu);
  770. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  771. smp_mb(); /* Subsequent counter accesses must see new value */
  772. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  773. smp_mb(); /* Subsequent RCU read-side critical sections */
  774. /* seen -after- acknowledgement. */
  775. }
  776. RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  777. RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
  778. RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
  779. RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
  780. cpu_clear(cpu, rcu_cpu_online_map);
  781. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  782. /*
  783. * Place the removed callbacks on the current CPU's queue.
  784. * Make them all start a new grace period: simple approach,
  785. * in theory could starve a given set of callbacks, but
  786. * you would need to be doing some serious CPU hotplugging
  787. * to make this happen. If this becomes a problem, adding
  788. * a synchronize_rcu() to the hotplug path would be a simple
  789. * fix.
  790. */
  791. local_irq_save(flags);
  792. rdp = RCU_DATA_ME();
  793. spin_lock(&rdp->lock);
  794. *rdp->nexttail = list;
  795. if (list)
  796. rdp->nexttail = tail;
  797. spin_unlock_irqrestore(&rdp->lock, flags);
  798. }
  799. void __devinit rcu_online_cpu(int cpu)
  800. {
  801. unsigned long flags;
  802. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  803. cpu_set(cpu, rcu_cpu_online_map);
  804. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  805. }
  806. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  807. void rcu_offline_cpu(int cpu)
  808. {
  809. }
  810. void __devinit rcu_online_cpu(int cpu)
  811. {
  812. }
  813. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  814. static void rcu_process_callbacks(struct softirq_action *unused)
  815. {
  816. unsigned long flags;
  817. struct rcu_head *next, *list;
  818. struct rcu_data *rdp;
  819. local_irq_save(flags);
  820. rdp = RCU_DATA_ME();
  821. spin_lock(&rdp->lock);
  822. list = rdp->donelist;
  823. if (list == NULL) {
  824. spin_unlock_irqrestore(&rdp->lock, flags);
  825. return;
  826. }
  827. rdp->donelist = NULL;
  828. rdp->donetail = &rdp->donelist;
  829. RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
  830. spin_unlock_irqrestore(&rdp->lock, flags);
  831. while (list) {
  832. next = list->next;
  833. list->func(list);
  834. list = next;
  835. RCU_TRACE_ME(rcupreempt_trace_invoke);
  836. }
  837. }
  838. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  839. {
  840. unsigned long flags;
  841. struct rcu_data *rdp;
  842. head->func = func;
  843. head->next = NULL;
  844. local_irq_save(flags);
  845. rdp = RCU_DATA_ME();
  846. spin_lock(&rdp->lock);
  847. __rcu_advance_callbacks(rdp);
  848. *rdp->nexttail = head;
  849. rdp->nexttail = &head->next;
  850. RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
  851. spin_unlock(&rdp->lock);
  852. local_irq_restore(flags);
  853. }
  854. EXPORT_SYMBOL_GPL(call_rcu);
  855. /*
  856. * Wait until all currently running preempt_disable() code segments
  857. * (including hardware-irq-disable segments) complete. Note that
  858. * in -rt this does -not- necessarily result in all currently executing
  859. * interrupt -handlers- having completed.
  860. */
  861. void __synchronize_sched(void)
  862. {
  863. cpumask_t oldmask;
  864. int cpu;
  865. if (sched_getaffinity(0, &oldmask) < 0)
  866. oldmask = cpu_possible_map;
  867. for_each_online_cpu(cpu) {
  868. sched_setaffinity(0, &cpumask_of_cpu(cpu));
  869. schedule();
  870. }
  871. sched_setaffinity(0, &oldmask);
  872. }
  873. EXPORT_SYMBOL_GPL(__synchronize_sched);
  874. /*
  875. * Check to see if any future RCU-related work will need to be done
  876. * by the current CPU, even if none need be done immediately, returning
  877. * 1 if so. Assumes that notifiers would take care of handling any
  878. * outstanding requests from the RCU core.
  879. *
  880. * This function is part of the RCU implementation; it is -not-
  881. * an exported member of the RCU API.
  882. */
  883. int rcu_needs_cpu(int cpu)
  884. {
  885. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  886. return (rdp->donelist != NULL ||
  887. !!rdp->waitlistcount ||
  888. rdp->nextlist != NULL);
  889. }
  890. int rcu_pending(int cpu)
  891. {
  892. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  893. /* The CPU has at least one callback queued somewhere. */
  894. if (rdp->donelist != NULL ||
  895. !!rdp->waitlistcount ||
  896. rdp->nextlist != NULL)
  897. return 1;
  898. /* The RCU core needs an acknowledgement from this CPU. */
  899. if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
  900. (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
  901. return 1;
  902. /* This CPU has fallen behind the global grace-period number. */
  903. if (rdp->completed != rcu_ctrlblk.completed)
  904. return 1;
  905. /* Nothing needed from this CPU. */
  906. return 0;
  907. }
  908. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  909. unsigned long action, void *hcpu)
  910. {
  911. long cpu = (long)hcpu;
  912. switch (action) {
  913. case CPU_UP_PREPARE:
  914. case CPU_UP_PREPARE_FROZEN:
  915. rcu_online_cpu(cpu);
  916. break;
  917. case CPU_UP_CANCELED:
  918. case CPU_UP_CANCELED_FROZEN:
  919. case CPU_DEAD:
  920. case CPU_DEAD_FROZEN:
  921. rcu_offline_cpu(cpu);
  922. break;
  923. default:
  924. break;
  925. }
  926. return NOTIFY_OK;
  927. }
  928. static struct notifier_block __cpuinitdata rcu_nb = {
  929. .notifier_call = rcu_cpu_notify,
  930. };
  931. void __init __rcu_init(void)
  932. {
  933. int cpu;
  934. int i;
  935. struct rcu_data *rdp;
  936. printk(KERN_NOTICE "Preemptible RCU implementation.\n");
  937. for_each_possible_cpu(cpu) {
  938. rdp = RCU_DATA_CPU(cpu);
  939. spin_lock_init(&rdp->lock);
  940. rdp->completed = 0;
  941. rdp->waitlistcount = 0;
  942. rdp->nextlist = NULL;
  943. rdp->nexttail = &rdp->nextlist;
  944. for (i = 0; i < GP_STAGES; i++) {
  945. rdp->waitlist[i] = NULL;
  946. rdp->waittail[i] = &rdp->waitlist[i];
  947. }
  948. rdp->donelist = NULL;
  949. rdp->donetail = &rdp->donelist;
  950. rdp->rcu_flipctr[0] = 0;
  951. rdp->rcu_flipctr[1] = 0;
  952. }
  953. register_cpu_notifier(&rcu_nb);
  954. /*
  955. * We don't need protection against CPU-Hotplug here
  956. * since
  957. * a) If a CPU comes online while we are iterating over the
  958. * cpu_online_map below, we would only end up making a
  959. * duplicate call to rcu_online_cpu() which sets the corresponding
  960. * CPU's mask in the rcu_cpu_online_map.
  961. *
  962. * b) A CPU cannot go offline at this point in time since the user
  963. * does not have access to the sysfs interface, nor do we
  964. * suspend the system.
  965. */
  966. for_each_online_cpu(cpu)
  967. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
  968. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks, NULL);
  969. }
  970. /*
  971. * Deprecated, use synchronize_rcu() or synchronize_sched() instead.
  972. */
  973. void synchronize_kernel(void)
  974. {
  975. synchronize_rcu();
  976. }
  977. #ifdef CONFIG_RCU_TRACE
  978. long *rcupreempt_flipctr(int cpu)
  979. {
  980. return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  981. }
  982. EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
  983. int rcupreempt_flip_flag(int cpu)
  984. {
  985. return per_cpu(rcu_flip_flag, cpu);
  986. }
  987. EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
  988. int rcupreempt_mb_flag(int cpu)
  989. {
  990. return per_cpu(rcu_mb_flag, cpu);
  991. }
  992. EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
  993. char *rcupreempt_try_flip_state_name(void)
  994. {
  995. return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
  996. }
  997. EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
  998. struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
  999. {
  1000. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1001. return &rdp->trace;
  1002. }
  1003. EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
  1004. #endif /* #ifdef RCU_TRACE */