rcupdate.h 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2001
  19. *
  20. * Author: Dipankar Sarma <dipankar@in.ibm.com>
  21. *
  22. * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  23. * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  24. * Papers:
  25. * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  26. * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  27. *
  28. * For detailed explanation of Read-Copy Update mechanism see -
  29. * http://lse.sourceforge.net/locking/rcupdate.html
  30. *
  31. */
  32. #ifndef __LINUX_RCUPDATE_H
  33. #define __LINUX_RCUPDATE_H
  34. #include <linux/types.h>
  35. #include <linux/cache.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/threads.h>
  38. #include <linux/cpumask.h>
  39. #include <linux/seqlock.h>
  40. #include <linux/lockdep.h>
  41. #include <linux/debugobjects.h>
  42. #include <linux/bug.h>
  43. #include <linux/compiler.h>
  44. #include <linux/ktime.h>
  45. #include <linux/irqflags.h>
  46. #include <asm/barrier.h>
  47. #ifndef CONFIG_TINY_RCU
  48. extern int rcu_expedited; /* for sysctl */
  49. extern int rcu_normal; /* also for sysctl */
  50. #endif /* #ifndef CONFIG_TINY_RCU */
  51. #ifdef CONFIG_TINY_RCU
  52. /* Tiny RCU doesn't expedite, as its purpose in life is instead to be tiny. */
  53. static inline bool rcu_gp_is_normal(void) /* Internal RCU use. */
  54. {
  55. return true;
  56. }
  57. static inline bool rcu_gp_is_expedited(void) /* Internal RCU use. */
  58. {
  59. return false;
  60. }
  61. static inline void rcu_expedite_gp(void)
  62. {
  63. }
  64. static inline void rcu_unexpedite_gp(void)
  65. {
  66. }
  67. #else /* #ifdef CONFIG_TINY_RCU */
  68. bool rcu_gp_is_normal(void); /* Internal RCU use. */
  69. bool rcu_gp_is_expedited(void); /* Internal RCU use. */
  70. void rcu_expedite_gp(void);
  71. void rcu_unexpedite_gp(void);
  72. void rcupdate_announce_bootup_oddness(void);
  73. #endif /* #else #ifdef CONFIG_TINY_RCU */
  74. enum rcutorture_type {
  75. RCU_FLAVOR,
  76. RCU_BH_FLAVOR,
  77. RCU_SCHED_FLAVOR,
  78. RCU_TASKS_FLAVOR,
  79. SRCU_FLAVOR,
  80. INVALID_RCU_FLAVOR
  81. };
  82. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
  83. void rcutorture_get_gp_data(enum rcutorture_type test_type, int *flags,
  84. unsigned long *gpnum, unsigned long *completed);
  85. void rcutorture_record_test_transition(void);
  86. void rcutorture_record_progress(unsigned long vernum);
  87. void do_trace_rcu_torture_read(const char *rcutorturename,
  88. struct rcu_head *rhp,
  89. unsigned long secs,
  90. unsigned long c_old,
  91. unsigned long c);
  92. bool rcu_irq_enter_disabled(void);
  93. #else
  94. static inline void rcutorture_get_gp_data(enum rcutorture_type test_type,
  95. int *flags,
  96. unsigned long *gpnum,
  97. unsigned long *completed)
  98. {
  99. *flags = 0;
  100. *gpnum = 0;
  101. *completed = 0;
  102. }
  103. static inline void rcutorture_record_test_transition(void)
  104. {
  105. }
  106. static inline void rcutorture_record_progress(unsigned long vernum)
  107. {
  108. }
  109. static inline bool rcu_irq_enter_disabled(void)
  110. {
  111. return false;
  112. }
  113. #ifdef CONFIG_RCU_TRACE
  114. void do_trace_rcu_torture_read(const char *rcutorturename,
  115. struct rcu_head *rhp,
  116. unsigned long secs,
  117. unsigned long c_old,
  118. unsigned long c);
  119. #else
  120. #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
  121. do { } while (0)
  122. #endif
  123. #endif
  124. #define UINT_CMP_GE(a, b) (UINT_MAX / 2 >= (a) - (b))
  125. #define UINT_CMP_LT(a, b) (UINT_MAX / 2 < (a) - (b))
  126. #define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b))
  127. #define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b))
  128. #define ulong2long(a) (*(long *)(&(a)))
  129. /* Exported common interfaces */
  130. #ifdef CONFIG_PREEMPT_RCU
  131. /**
  132. * call_rcu() - Queue an RCU callback for invocation after a grace period.
  133. * @head: structure to be used for queueing the RCU updates.
  134. * @func: actual callback function to be invoked after the grace period
  135. *
  136. * The callback function will be invoked some time after a full grace
  137. * period elapses, in other words after all pre-existing RCU read-side
  138. * critical sections have completed. However, the callback function
  139. * might well execute concurrently with RCU read-side critical sections
  140. * that started after call_rcu() was invoked. RCU read-side critical
  141. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  142. * and may be nested.
  143. *
  144. * Note that all CPUs must agree that the grace period extended beyond
  145. * all pre-existing RCU read-side critical section. On systems with more
  146. * than one CPU, this means that when "func()" is invoked, each CPU is
  147. * guaranteed to have executed a full memory barrier since the end of its
  148. * last RCU read-side critical section whose beginning preceded the call
  149. * to call_rcu(). It also means that each CPU executing an RCU read-side
  150. * critical section that continues beyond the start of "func()" must have
  151. * executed a memory barrier after the call_rcu() but before the beginning
  152. * of that RCU read-side critical section. Note that these guarantees
  153. * include CPUs that are offline, idle, or executing in user mode, as
  154. * well as CPUs that are executing in the kernel.
  155. *
  156. * Furthermore, if CPU A invoked call_rcu() and CPU B invoked the
  157. * resulting RCU callback function "func()", then both CPU A and CPU B are
  158. * guaranteed to execute a full memory barrier during the time interval
  159. * between the call to call_rcu() and the invocation of "func()" -- even
  160. * if CPU A and CPU B are the same CPU (but again only if the system has
  161. * more than one CPU).
  162. */
  163. void call_rcu(struct rcu_head *head,
  164. rcu_callback_t func);
  165. #else /* #ifdef CONFIG_PREEMPT_RCU */
  166. /* In classic RCU, call_rcu() is just call_rcu_sched(). */
  167. #define call_rcu call_rcu_sched
  168. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  169. /**
  170. * call_rcu_bh() - Queue an RCU for invocation after a quicker grace period.
  171. * @head: structure to be used for queueing the RCU updates.
  172. * @func: actual callback function to be invoked after the grace period
  173. *
  174. * The callback function will be invoked some time after a full grace
  175. * period elapses, in other words after all currently executing RCU
  176. * read-side critical sections have completed. call_rcu_bh() assumes
  177. * that the read-side critical sections end on completion of a softirq
  178. * handler. This means that read-side critical sections in process
  179. * context must not be interrupted by softirqs. This interface is to be
  180. * used when most of the read-side critical sections are in softirq context.
  181. * RCU read-side critical sections are delimited by :
  182. * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context.
  183. * OR
  184. * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context.
  185. * These may be nested.
  186. *
  187. * See the description of call_rcu() for more detailed information on
  188. * memory ordering guarantees.
  189. */
  190. void call_rcu_bh(struct rcu_head *head,
  191. rcu_callback_t func);
  192. /**
  193. * call_rcu_sched() - Queue an RCU for invocation after sched grace period.
  194. * @head: structure to be used for queueing the RCU updates.
  195. * @func: actual callback function to be invoked after the grace period
  196. *
  197. * The callback function will be invoked some time after a full grace
  198. * period elapses, in other words after all currently executing RCU
  199. * read-side critical sections have completed. call_rcu_sched() assumes
  200. * that the read-side critical sections end on enabling of preemption
  201. * or on voluntary preemption.
  202. * RCU read-side critical sections are delimited by :
  203. * - rcu_read_lock_sched() and rcu_read_unlock_sched(),
  204. * OR
  205. * anything that disables preemption.
  206. * These may be nested.
  207. *
  208. * See the description of call_rcu() for more detailed information on
  209. * memory ordering guarantees.
  210. */
  211. void call_rcu_sched(struct rcu_head *head,
  212. rcu_callback_t func);
  213. void synchronize_sched(void);
  214. /**
  215. * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
  216. * @head: structure to be used for queueing the RCU updates.
  217. * @func: actual callback function to be invoked after the grace period
  218. *
  219. * The callback function will be invoked some time after a full grace
  220. * period elapses, in other words after all currently executing RCU
  221. * read-side critical sections have completed. call_rcu_tasks() assumes
  222. * that the read-side critical sections end at a voluntary context
  223. * switch (not a preemption!), entry into idle, or transition to usermode
  224. * execution. As such, there are no read-side primitives analogous to
  225. * rcu_read_lock() and rcu_read_unlock() because this primitive is intended
  226. * to determine that all tasks have passed through a safe state, not so
  227. * much for data-strcuture synchronization.
  228. *
  229. * See the description of call_rcu() for more detailed information on
  230. * memory ordering guarantees.
  231. */
  232. void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func);
  233. void synchronize_rcu_tasks(void);
  234. void rcu_barrier_tasks(void);
  235. #ifdef CONFIG_PREEMPT_RCU
  236. void __rcu_read_lock(void);
  237. void __rcu_read_unlock(void);
  238. void rcu_read_unlock_special(struct task_struct *t);
  239. void synchronize_rcu(void);
  240. /*
  241. * Defined as a macro as it is a very low level header included from
  242. * areas that don't even know about current. This gives the rcu_read_lock()
  243. * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other
  244. * types of kernel builds, the rcu_read_lock() nesting depth is unknowable.
  245. */
  246. #define rcu_preempt_depth() (current->rcu_read_lock_nesting)
  247. #else /* #ifdef CONFIG_PREEMPT_RCU */
  248. static inline void __rcu_read_lock(void)
  249. {
  250. if (IS_ENABLED(CONFIG_PREEMPT_COUNT))
  251. preempt_disable();
  252. }
  253. static inline void __rcu_read_unlock(void)
  254. {
  255. if (IS_ENABLED(CONFIG_PREEMPT_COUNT))
  256. preempt_enable();
  257. }
  258. static inline void synchronize_rcu(void)
  259. {
  260. synchronize_sched();
  261. }
  262. static inline int rcu_preempt_depth(void)
  263. {
  264. return 0;
  265. }
  266. #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
  267. /* Internal to kernel */
  268. void rcu_init(void);
  269. void rcu_sched_qs(void);
  270. void rcu_bh_qs(void);
  271. void rcu_check_callbacks(int user);
  272. void rcu_report_dead(unsigned int cpu);
  273. void rcu_cpu_starting(unsigned int cpu);
  274. #ifndef CONFIG_TINY_RCU
  275. void rcu_end_inkernel_boot(void);
  276. #else /* #ifndef CONFIG_TINY_RCU */
  277. static inline void rcu_end_inkernel_boot(void) { }
  278. #endif /* #ifndef CONFIG_TINY_RCU */
  279. #ifdef CONFIG_RCU_STALL_COMMON
  280. void rcu_sysrq_start(void);
  281. void rcu_sysrq_end(void);
  282. #else /* #ifdef CONFIG_RCU_STALL_COMMON */
  283. static inline void rcu_sysrq_start(void)
  284. {
  285. }
  286. static inline void rcu_sysrq_end(void)
  287. {
  288. }
  289. #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */
  290. #ifdef CONFIG_NO_HZ_FULL
  291. void rcu_user_enter(void);
  292. void rcu_user_exit(void);
  293. #else
  294. static inline void rcu_user_enter(void) { }
  295. static inline void rcu_user_exit(void) { }
  296. #endif /* CONFIG_NO_HZ_FULL */
  297. #ifdef CONFIG_RCU_NOCB_CPU
  298. void rcu_init_nohz(void);
  299. #else /* #ifdef CONFIG_RCU_NOCB_CPU */
  300. static inline void rcu_init_nohz(void)
  301. {
  302. }
  303. #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */
  304. /**
  305. * RCU_NONIDLE - Indicate idle-loop code that needs RCU readers
  306. * @a: Code that RCU needs to pay attention to.
  307. *
  308. * RCU, RCU-bh, and RCU-sched read-side critical sections are forbidden
  309. * in the inner idle loop, that is, between the rcu_idle_enter() and
  310. * the rcu_idle_exit() -- RCU will happily ignore any such read-side
  311. * critical sections. However, things like powertop need tracepoints
  312. * in the inner idle loop.
  313. *
  314. * This macro provides the way out: RCU_NONIDLE(do_something_with_RCU())
  315. * will tell RCU that it needs to pay attention, invoke its argument
  316. * (in this example, calling the do_something_with_RCU() function),
  317. * and then tell RCU to go back to ignoring this CPU. It is permissible
  318. * to nest RCU_NONIDLE() wrappers, but not indefinitely (but the limit is
  319. * on the order of a million or so, even on 32-bit systems). It is
  320. * not legal to block within RCU_NONIDLE(), nor is it permissible to
  321. * transfer control either into or out of RCU_NONIDLE()'s statement.
  322. */
  323. #define RCU_NONIDLE(a) \
  324. do { \
  325. rcu_irq_enter_irqson(); \
  326. do { a; } while (0); \
  327. rcu_irq_exit_irqson(); \
  328. } while (0)
  329. /*
  330. * Note a voluntary context switch for RCU-tasks benefit. This is a
  331. * macro rather than an inline function to avoid #include hell.
  332. */
  333. #ifdef CONFIG_TASKS_RCU
  334. #define TASKS_RCU(x) x
  335. extern struct srcu_struct tasks_rcu_exit_srcu;
  336. #define rcu_note_voluntary_context_switch_lite(t) \
  337. do { \
  338. if (READ_ONCE((t)->rcu_tasks_holdout)) \
  339. WRITE_ONCE((t)->rcu_tasks_holdout, false); \
  340. } while (0)
  341. #define rcu_note_voluntary_context_switch(t) \
  342. do { \
  343. rcu_all_qs(); \
  344. rcu_note_voluntary_context_switch_lite(t); \
  345. } while (0)
  346. #else /* #ifdef CONFIG_TASKS_RCU */
  347. #define TASKS_RCU(x) do { } while (0)
  348. #define rcu_note_voluntary_context_switch_lite(t) do { } while (0)
  349. #define rcu_note_voluntary_context_switch(t) rcu_all_qs()
  350. #endif /* #else #ifdef CONFIG_TASKS_RCU */
  351. /**
  352. * cond_resched_rcu_qs - Report potential quiescent states to RCU
  353. *
  354. * This macro resembles cond_resched(), except that it is defined to
  355. * report potential quiescent states to RCU-tasks even if the cond_resched()
  356. * machinery were to be shut off, as some advocate for PREEMPT kernels.
  357. */
  358. #define cond_resched_rcu_qs() \
  359. do { \
  360. if (!cond_resched()) \
  361. rcu_note_voluntary_context_switch(current); \
  362. } while (0)
  363. #if defined(CONFIG_DEBUG_LOCK_ALLOC) || defined(CONFIG_RCU_TRACE) || defined(CONFIG_SMP)
  364. bool __rcu_is_watching(void);
  365. #endif /* #if defined(CONFIG_DEBUG_LOCK_ALLOC) || defined(CONFIG_RCU_TRACE) || defined(CONFIG_SMP) */
  366. /*
  367. * Infrastructure to implement the synchronize_() primitives in
  368. * TREE_RCU and rcu_barrier_() primitives in TINY_RCU.
  369. */
  370. #if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU)
  371. #include <linux/rcutree.h>
  372. #elif defined(CONFIG_TINY_RCU)
  373. #include <linux/rcutiny.h>
  374. #else
  375. #error "Unknown RCU implementation specified to kernel configuration"
  376. #endif
  377. #define RCU_SCHEDULER_INACTIVE 0
  378. #define RCU_SCHEDULER_INIT 1
  379. #define RCU_SCHEDULER_RUNNING 2
  380. /*
  381. * init_rcu_head_on_stack()/destroy_rcu_head_on_stack() are needed for dynamic
  382. * initialization and destruction of rcu_head on the stack. rcu_head structures
  383. * allocated dynamically in the heap or defined statically don't need any
  384. * initialization.
  385. */
  386. #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
  387. void init_rcu_head(struct rcu_head *head);
  388. void destroy_rcu_head(struct rcu_head *head);
  389. void init_rcu_head_on_stack(struct rcu_head *head);
  390. void destroy_rcu_head_on_stack(struct rcu_head *head);
  391. #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  392. static inline void init_rcu_head(struct rcu_head *head)
  393. {
  394. }
  395. static inline void destroy_rcu_head(struct rcu_head *head)
  396. {
  397. }
  398. static inline void init_rcu_head_on_stack(struct rcu_head *head)
  399. {
  400. }
  401. static inline void destroy_rcu_head_on_stack(struct rcu_head *head)
  402. {
  403. }
  404. #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */
  405. #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU)
  406. bool rcu_lockdep_current_cpu_online(void);
  407. #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
  408. static inline bool rcu_lockdep_current_cpu_online(void)
  409. {
  410. return true;
  411. }
  412. #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */
  413. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  414. static inline void rcu_lock_acquire(struct lockdep_map *map)
  415. {
  416. lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_);
  417. }
  418. static inline void rcu_lock_release(struct lockdep_map *map)
  419. {
  420. lock_release(map, 1, _THIS_IP_);
  421. }
  422. extern struct lockdep_map rcu_lock_map;
  423. extern struct lockdep_map rcu_bh_lock_map;
  424. extern struct lockdep_map rcu_sched_lock_map;
  425. extern struct lockdep_map rcu_callback_map;
  426. int debug_lockdep_rcu_enabled(void);
  427. int rcu_read_lock_held(void);
  428. int rcu_read_lock_bh_held(void);
  429. /**
  430. * rcu_read_lock_sched_held() - might we be in RCU-sched read-side critical section?
  431. *
  432. * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an
  433. * RCU-sched read-side critical section. In absence of
  434. * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
  435. * critical section unless it can prove otherwise.
  436. */
  437. int rcu_read_lock_sched_held(void);
  438. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  439. # define rcu_lock_acquire(a) do { } while (0)
  440. # define rcu_lock_release(a) do { } while (0)
  441. static inline int rcu_read_lock_held(void)
  442. {
  443. return 1;
  444. }
  445. static inline int rcu_read_lock_bh_held(void)
  446. {
  447. return 1;
  448. }
  449. static inline int rcu_read_lock_sched_held(void)
  450. {
  451. return !preemptible();
  452. }
  453. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  454. #ifdef CONFIG_PROVE_RCU
  455. /**
  456. * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met
  457. * @c: condition to check
  458. * @s: informative message
  459. */
  460. #define RCU_LOCKDEP_WARN(c, s) \
  461. do { \
  462. static bool __section(.data.unlikely) __warned; \
  463. if (debug_lockdep_rcu_enabled() && !__warned && (c)) { \
  464. __warned = true; \
  465. lockdep_rcu_suspicious(__FILE__, __LINE__, s); \
  466. } \
  467. } while (0)
  468. #if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU)
  469. static inline void rcu_preempt_sleep_check(void)
  470. {
  471. RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map),
  472. "Illegal context switch in RCU read-side critical section");
  473. }
  474. #else /* #ifdef CONFIG_PROVE_RCU */
  475. static inline void rcu_preempt_sleep_check(void)
  476. {
  477. }
  478. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  479. #define rcu_sleep_check() \
  480. do { \
  481. rcu_preempt_sleep_check(); \
  482. RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \
  483. "Illegal context switch in RCU-bh read-side critical section"); \
  484. RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \
  485. "Illegal context switch in RCU-sched read-side critical section"); \
  486. } while (0)
  487. #else /* #ifdef CONFIG_PROVE_RCU */
  488. #define RCU_LOCKDEP_WARN(c, s) do { } while (0)
  489. #define rcu_sleep_check() do { } while (0)
  490. #endif /* #else #ifdef CONFIG_PROVE_RCU */
  491. /*
  492. * Helper functions for rcu_dereference_check(), rcu_dereference_protected()
  493. * and rcu_assign_pointer(). Some of these could be folded into their
  494. * callers, but they are left separate in order to ease introduction of
  495. * multiple flavors of pointers to match the multiple flavors of RCU
  496. * (e.g., __rcu_bh, * __rcu_sched, and __srcu), should this make sense in
  497. * the future.
  498. */
  499. #ifdef __CHECKER__
  500. #define rcu_dereference_sparse(p, space) \
  501. ((void)(((typeof(*p) space *)p) == p))
  502. #else /* #ifdef __CHECKER__ */
  503. #define rcu_dereference_sparse(p, space)
  504. #endif /* #else #ifdef __CHECKER__ */
  505. #define __rcu_access_pointer(p, space) \
  506. ({ \
  507. typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \
  508. rcu_dereference_sparse(p, space); \
  509. ((typeof(*p) __force __kernel *)(_________p1)); \
  510. })
  511. #define __rcu_dereference_check(p, c, space) \
  512. ({ \
  513. /* Dependency order vs. p above. */ \
  514. typeof(*p) *________p1 = (typeof(*p) *__force)lockless_dereference(p); \
  515. RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \
  516. rcu_dereference_sparse(p, space); \
  517. ((typeof(*p) __force __kernel *)(________p1)); \
  518. })
  519. #define __rcu_dereference_protected(p, c, space) \
  520. ({ \
  521. RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \
  522. rcu_dereference_sparse(p, space); \
  523. ((typeof(*p) __force __kernel *)(p)); \
  524. })
  525. #define rcu_dereference_raw(p) \
  526. ({ \
  527. /* Dependency order vs. p above. */ \
  528. typeof(p) ________p1 = lockless_dereference(p); \
  529. ((typeof(*p) __force __kernel *)(________p1)); \
  530. })
  531. /**
  532. * RCU_INITIALIZER() - statically initialize an RCU-protected global variable
  533. * @v: The value to statically initialize with.
  534. */
  535. #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v)
  536. /**
  537. * rcu_assign_pointer() - assign to RCU-protected pointer
  538. * @p: pointer to assign to
  539. * @v: value to assign (publish)
  540. *
  541. * Assigns the specified value to the specified RCU-protected
  542. * pointer, ensuring that any concurrent RCU readers will see
  543. * any prior initialization.
  544. *
  545. * Inserts memory barriers on architectures that require them
  546. * (which is most of them), and also prevents the compiler from
  547. * reordering the code that initializes the structure after the pointer
  548. * assignment. More importantly, this call documents which pointers
  549. * will be dereferenced by RCU read-side code.
  550. *
  551. * In some special cases, you may use RCU_INIT_POINTER() instead
  552. * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due
  553. * to the fact that it does not constrain either the CPU or the compiler.
  554. * That said, using RCU_INIT_POINTER() when you should have used
  555. * rcu_assign_pointer() is a very bad thing that results in
  556. * impossible-to-diagnose memory corruption. So please be careful.
  557. * See the RCU_INIT_POINTER() comment header for details.
  558. *
  559. * Note that rcu_assign_pointer() evaluates each of its arguments only
  560. * once, appearances notwithstanding. One of the "extra" evaluations
  561. * is in typeof() and the other visible only to sparse (__CHECKER__),
  562. * neither of which actually execute the argument. As with most cpp
  563. * macros, this execute-arguments-only-once property is important, so
  564. * please be careful when making changes to rcu_assign_pointer() and the
  565. * other macros that it invokes.
  566. */
  567. #define rcu_assign_pointer(p, v) \
  568. ({ \
  569. uintptr_t _r_a_p__v = (uintptr_t)(v); \
  570. \
  571. if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \
  572. WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \
  573. else \
  574. smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \
  575. _r_a_p__v; \
  576. })
  577. /**
  578. * rcu_access_pointer() - fetch RCU pointer with no dereferencing
  579. * @p: The pointer to read
  580. *
  581. * Return the value of the specified RCU-protected pointer, but omit the
  582. * smp_read_barrier_depends() and keep the READ_ONCE(). This is useful
  583. * when the value of this pointer is accessed, but the pointer is not
  584. * dereferenced, for example, when testing an RCU-protected pointer against
  585. * NULL. Although rcu_access_pointer() may also be used in cases where
  586. * update-side locks prevent the value of the pointer from changing, you
  587. * should instead use rcu_dereference_protected() for this use case.
  588. *
  589. * It is also permissible to use rcu_access_pointer() when read-side
  590. * access to the pointer was removed at least one grace period ago, as
  591. * is the case in the context of the RCU callback that is freeing up
  592. * the data, or after a synchronize_rcu() returns. This can be useful
  593. * when tearing down multi-linked structures after a grace period
  594. * has elapsed.
  595. */
  596. #define rcu_access_pointer(p) __rcu_access_pointer((p), __rcu)
  597. /**
  598. * rcu_dereference_check() - rcu_dereference with debug checking
  599. * @p: The pointer to read, prior to dereferencing
  600. * @c: The conditions under which the dereference will take place
  601. *
  602. * Do an rcu_dereference(), but check that the conditions under which the
  603. * dereference will take place are correct. Typically the conditions
  604. * indicate the various locking conditions that should be held at that
  605. * point. The check should return true if the conditions are satisfied.
  606. * An implicit check for being in an RCU read-side critical section
  607. * (rcu_read_lock()) is included.
  608. *
  609. * For example:
  610. *
  611. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock));
  612. *
  613. * could be used to indicate to lockdep that foo->bar may only be dereferenced
  614. * if either rcu_read_lock() is held, or that the lock required to replace
  615. * the bar struct at foo->bar is held.
  616. *
  617. * Note that the list of conditions may also include indications of when a lock
  618. * need not be held, for example during initialisation or destruction of the
  619. * target struct:
  620. *
  621. * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) ||
  622. * atomic_read(&foo->usage) == 0);
  623. *
  624. * Inserts memory barriers on architectures that require them
  625. * (currently only the Alpha), prevents the compiler from refetching
  626. * (and from merging fetches), and, more importantly, documents exactly
  627. * which pointers are protected by RCU and checks that the pointer is
  628. * annotated as __rcu.
  629. */
  630. #define rcu_dereference_check(p, c) \
  631. __rcu_dereference_check((p), (c) || rcu_read_lock_held(), __rcu)
  632. /**
  633. * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking
  634. * @p: The pointer to read, prior to dereferencing
  635. * @c: The conditions under which the dereference will take place
  636. *
  637. * This is the RCU-bh counterpart to rcu_dereference_check().
  638. */
  639. #define rcu_dereference_bh_check(p, c) \
  640. __rcu_dereference_check((p), (c) || rcu_read_lock_bh_held(), __rcu)
  641. /**
  642. * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking
  643. * @p: The pointer to read, prior to dereferencing
  644. * @c: The conditions under which the dereference will take place
  645. *
  646. * This is the RCU-sched counterpart to rcu_dereference_check().
  647. */
  648. #define rcu_dereference_sched_check(p, c) \
  649. __rcu_dereference_check((p), (c) || rcu_read_lock_sched_held(), \
  650. __rcu)
  651. /*
  652. * The tracing infrastructure traces RCU (we want that), but unfortunately
  653. * some of the RCU checks causes tracing to lock up the system.
  654. *
  655. * The no-tracing version of rcu_dereference_raw() must not call
  656. * rcu_read_lock_held().
  657. */
  658. #define rcu_dereference_raw_notrace(p) __rcu_dereference_check((p), 1, __rcu)
  659. /**
  660. * rcu_dereference_protected() - fetch RCU pointer when updates prevented
  661. * @p: The pointer to read, prior to dereferencing
  662. * @c: The conditions under which the dereference will take place
  663. *
  664. * Return the value of the specified RCU-protected pointer, but omit
  665. * both the smp_read_barrier_depends() and the READ_ONCE(). This
  666. * is useful in cases where update-side locks prevent the value of the
  667. * pointer from changing. Please note that this primitive does -not-
  668. * prevent the compiler from repeating this reference or combining it
  669. * with other references, so it should not be used without protection
  670. * of appropriate locks.
  671. *
  672. * This function is only for update-side use. Using this function
  673. * when protected only by rcu_read_lock() will result in infrequent
  674. * but very ugly failures.
  675. */
  676. #define rcu_dereference_protected(p, c) \
  677. __rcu_dereference_protected((p), (c), __rcu)
  678. /**
  679. * rcu_dereference() - fetch RCU-protected pointer for dereferencing
  680. * @p: The pointer to read, prior to dereferencing
  681. *
  682. * This is a simple wrapper around rcu_dereference_check().
  683. */
  684. #define rcu_dereference(p) rcu_dereference_check(p, 0)
  685. /**
  686. * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing
  687. * @p: The pointer to read, prior to dereferencing
  688. *
  689. * Makes rcu_dereference_check() do the dirty work.
  690. */
  691. #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0)
  692. /**
  693. * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing
  694. * @p: The pointer to read, prior to dereferencing
  695. *
  696. * Makes rcu_dereference_check() do the dirty work.
  697. */
  698. #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0)
  699. /**
  700. * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism
  701. * @p: The pointer to hand off
  702. *
  703. * This is simply an identity function, but it documents where a pointer
  704. * is handed off from RCU to some other synchronization mechanism, for
  705. * example, reference counting or locking. In C11, it would map to
  706. * kill_dependency(). It could be used as follows:
  707. *
  708. * rcu_read_lock();
  709. * p = rcu_dereference(gp);
  710. * long_lived = is_long_lived(p);
  711. * if (long_lived) {
  712. * if (!atomic_inc_not_zero(p->refcnt))
  713. * long_lived = false;
  714. * else
  715. * p = rcu_pointer_handoff(p);
  716. * }
  717. * rcu_read_unlock();
  718. */
  719. #define rcu_pointer_handoff(p) (p)
  720. /**
  721. * rcu_read_lock() - mark the beginning of an RCU read-side critical section
  722. *
  723. * When synchronize_rcu() is invoked on one CPU while other CPUs
  724. * are within RCU read-side critical sections, then the
  725. * synchronize_rcu() is guaranteed to block until after all the other
  726. * CPUs exit their critical sections. Similarly, if call_rcu() is invoked
  727. * on one CPU while other CPUs are within RCU read-side critical
  728. * sections, invocation of the corresponding RCU callback is deferred
  729. * until after the all the other CPUs exit their critical sections.
  730. *
  731. * Note, however, that RCU callbacks are permitted to run concurrently
  732. * with new RCU read-side critical sections. One way that this can happen
  733. * is via the following sequence of events: (1) CPU 0 enters an RCU
  734. * read-side critical section, (2) CPU 1 invokes call_rcu() to register
  735. * an RCU callback, (3) CPU 0 exits the RCU read-side critical section,
  736. * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU
  737. * callback is invoked. This is legal, because the RCU read-side critical
  738. * section that was running concurrently with the call_rcu() (and which
  739. * therefore might be referencing something that the corresponding RCU
  740. * callback would free up) has completed before the corresponding
  741. * RCU callback is invoked.
  742. *
  743. * RCU read-side critical sections may be nested. Any deferred actions
  744. * will be deferred until the outermost RCU read-side critical section
  745. * completes.
  746. *
  747. * You can avoid reading and understanding the next paragraph by
  748. * following this rule: don't put anything in an rcu_read_lock() RCU
  749. * read-side critical section that would block in a !PREEMPT kernel.
  750. * But if you want the full story, read on!
  751. *
  752. * In non-preemptible RCU implementations (TREE_RCU and TINY_RCU),
  753. * it is illegal to block while in an RCU read-side critical section.
  754. * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPT
  755. * kernel builds, RCU read-side critical sections may be preempted,
  756. * but explicit blocking is illegal. Finally, in preemptible RCU
  757. * implementations in real-time (with -rt patchset) kernel builds, RCU
  758. * read-side critical sections may be preempted and they may also block, but
  759. * only when acquiring spinlocks that are subject to priority inheritance.
  760. */
  761. static inline void rcu_read_lock(void)
  762. {
  763. __rcu_read_lock();
  764. __acquire(RCU);
  765. rcu_lock_acquire(&rcu_lock_map);
  766. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  767. "rcu_read_lock() used illegally while idle");
  768. }
  769. /*
  770. * So where is rcu_write_lock()? It does not exist, as there is no
  771. * way for writers to lock out RCU readers. This is a feature, not
  772. * a bug -- this property is what provides RCU's performance benefits.
  773. * Of course, writers must coordinate with each other. The normal
  774. * spinlock primitives work well for this, but any other technique may be
  775. * used as well. RCU does not care how the writers keep out of each
  776. * others' way, as long as they do so.
  777. */
  778. /**
  779. * rcu_read_unlock() - marks the end of an RCU read-side critical section.
  780. *
  781. * In most situations, rcu_read_unlock() is immune from deadlock.
  782. * However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
  783. * is responsible for deboosting, which it does via rt_mutex_unlock().
  784. * Unfortunately, this function acquires the scheduler's runqueue and
  785. * priority-inheritance spinlocks. This means that deadlock could result
  786. * if the caller of rcu_read_unlock() already holds one of these locks or
  787. * any lock that is ever acquired while holding them; or any lock which
  788. * can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
  789. * does not disable irqs while taking ->wait_lock.
  790. *
  791. * That said, RCU readers are never priority boosted unless they were
  792. * preempted. Therefore, one way to avoid deadlock is to make sure
  793. * that preemption never happens within any RCU read-side critical
  794. * section whose outermost rcu_read_unlock() is called with one of
  795. * rt_mutex_unlock()'s locks held. Such preemption can be avoided in
  796. * a number of ways, for example, by invoking preempt_disable() before
  797. * critical section's outermost rcu_read_lock().
  798. *
  799. * Given that the set of locks acquired by rt_mutex_unlock() might change
  800. * at any time, a somewhat more future-proofed approach is to make sure
  801. * that that preemption never happens within any RCU read-side critical
  802. * section whose outermost rcu_read_unlock() is called with irqs disabled.
  803. * This approach relies on the fact that rt_mutex_unlock() currently only
  804. * acquires irq-disabled locks.
  805. *
  806. * The second of these two approaches is best in most situations,
  807. * however, the first approach can also be useful, at least to those
  808. * developers willing to keep abreast of the set of locks acquired by
  809. * rt_mutex_unlock().
  810. *
  811. * See rcu_read_lock() for more information.
  812. */
  813. static inline void rcu_read_unlock(void)
  814. {
  815. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  816. "rcu_read_unlock() used illegally while idle");
  817. __release(RCU);
  818. __rcu_read_unlock();
  819. rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */
  820. }
  821. /**
  822. * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section
  823. *
  824. * This is equivalent of rcu_read_lock(), but to be used when updates
  825. * are being done using call_rcu_bh() or synchronize_rcu_bh(). Since
  826. * both call_rcu_bh() and synchronize_rcu_bh() consider completion of a
  827. * softirq handler to be a quiescent state, a process in RCU read-side
  828. * critical section must be protected by disabling softirqs. Read-side
  829. * critical sections in interrupt context can use just rcu_read_lock(),
  830. * though this should at least be commented to avoid confusing people
  831. * reading the code.
  832. *
  833. * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh()
  834. * must occur in the same context, for example, it is illegal to invoke
  835. * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh()
  836. * was invoked from some other task.
  837. */
  838. static inline void rcu_read_lock_bh(void)
  839. {
  840. local_bh_disable();
  841. __acquire(RCU_BH);
  842. rcu_lock_acquire(&rcu_bh_lock_map);
  843. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  844. "rcu_read_lock_bh() used illegally while idle");
  845. }
  846. /*
  847. * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section
  848. *
  849. * See rcu_read_lock_bh() for more information.
  850. */
  851. static inline void rcu_read_unlock_bh(void)
  852. {
  853. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  854. "rcu_read_unlock_bh() used illegally while idle");
  855. rcu_lock_release(&rcu_bh_lock_map);
  856. __release(RCU_BH);
  857. local_bh_enable();
  858. }
  859. /**
  860. * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section
  861. *
  862. * This is equivalent of rcu_read_lock(), but to be used when updates
  863. * are being done using call_rcu_sched() or synchronize_rcu_sched().
  864. * Read-side critical sections can also be introduced by anything that
  865. * disables preemption, including local_irq_disable() and friends.
  866. *
  867. * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched()
  868. * must occur in the same context, for example, it is illegal to invoke
  869. * rcu_read_unlock_sched() from process context if the matching
  870. * rcu_read_lock_sched() was invoked from an NMI handler.
  871. */
  872. static inline void rcu_read_lock_sched(void)
  873. {
  874. preempt_disable();
  875. __acquire(RCU_SCHED);
  876. rcu_lock_acquire(&rcu_sched_lock_map);
  877. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  878. "rcu_read_lock_sched() used illegally while idle");
  879. }
  880. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  881. static inline notrace void rcu_read_lock_sched_notrace(void)
  882. {
  883. preempt_disable_notrace();
  884. __acquire(RCU_SCHED);
  885. }
  886. /*
  887. * rcu_read_unlock_sched - marks the end of a RCU-classic critical section
  888. *
  889. * See rcu_read_lock_sched for more information.
  890. */
  891. static inline void rcu_read_unlock_sched(void)
  892. {
  893. RCU_LOCKDEP_WARN(!rcu_is_watching(),
  894. "rcu_read_unlock_sched() used illegally while idle");
  895. rcu_lock_release(&rcu_sched_lock_map);
  896. __release(RCU_SCHED);
  897. preempt_enable();
  898. }
  899. /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */
  900. static inline notrace void rcu_read_unlock_sched_notrace(void)
  901. {
  902. __release(RCU_SCHED);
  903. preempt_enable_notrace();
  904. }
  905. /**
  906. * RCU_INIT_POINTER() - initialize an RCU protected pointer
  907. *
  908. * Initialize an RCU-protected pointer in special cases where readers
  909. * do not need ordering constraints on the CPU or the compiler. These
  910. * special cases are:
  911. *
  912. * 1. This use of RCU_INIT_POINTER() is NULLing out the pointer -or-
  913. * 2. The caller has taken whatever steps are required to prevent
  914. * RCU readers from concurrently accessing this pointer -or-
  915. * 3. The referenced data structure has already been exposed to
  916. * readers either at compile time or via rcu_assign_pointer() -and-
  917. * a. You have not made -any- reader-visible changes to
  918. * this structure since then -or-
  919. * b. It is OK for readers accessing this structure from its
  920. * new location to see the old state of the structure. (For
  921. * example, the changes were to statistical counters or to
  922. * other state where exact synchronization is not required.)
  923. *
  924. * Failure to follow these rules governing use of RCU_INIT_POINTER() will
  925. * result in impossible-to-diagnose memory corruption. As in the structures
  926. * will look OK in crash dumps, but any concurrent RCU readers might
  927. * see pre-initialized values of the referenced data structure. So
  928. * please be very careful how you use RCU_INIT_POINTER()!!!
  929. *
  930. * If you are creating an RCU-protected linked structure that is accessed
  931. * by a single external-to-structure RCU-protected pointer, then you may
  932. * use RCU_INIT_POINTER() to initialize the internal RCU-protected
  933. * pointers, but you must use rcu_assign_pointer() to initialize the
  934. * external-to-structure pointer -after- you have completely initialized
  935. * the reader-accessible portions of the linked structure.
  936. *
  937. * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no
  938. * ordering guarantees for either the CPU or the compiler.
  939. */
  940. #define RCU_INIT_POINTER(p, v) \
  941. do { \
  942. rcu_dereference_sparse(p, __rcu); \
  943. WRITE_ONCE(p, RCU_INITIALIZER(v)); \
  944. } while (0)
  945. /**
  946. * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer
  947. *
  948. * GCC-style initialization for an RCU-protected pointer in a structure field.
  949. */
  950. #define RCU_POINTER_INITIALIZER(p, v) \
  951. .p = RCU_INITIALIZER(v)
  952. /*
  953. * Does the specified offset indicate that the corresponding rcu_head
  954. * structure can be handled by kfree_rcu()?
  955. */
  956. #define __is_kfree_rcu_offset(offset) ((offset) < 4096)
  957. /*
  958. * Helper macro for kfree_rcu() to prevent argument-expansion eyestrain.
  959. */
  960. #define __kfree_rcu(head, offset) \
  961. do { \
  962. BUILD_BUG_ON(!__is_kfree_rcu_offset(offset)); \
  963. kfree_call_rcu(head, (rcu_callback_t)(unsigned long)(offset)); \
  964. } while (0)
  965. /**
  966. * kfree_rcu() - kfree an object after a grace period.
  967. * @ptr: pointer to kfree
  968. * @rcu_head: the name of the struct rcu_head within the type of @ptr.
  969. *
  970. * Many rcu callbacks functions just call kfree() on the base structure.
  971. * These functions are trivial, but their size adds up, and furthermore
  972. * when they are used in a kernel module, that module must invoke the
  973. * high-latency rcu_barrier() function at module-unload time.
  974. *
  975. * The kfree_rcu() function handles this issue. Rather than encoding a
  976. * function address in the embedded rcu_head structure, kfree_rcu() instead
  977. * encodes the offset of the rcu_head structure within the base structure.
  978. * Because the functions are not allowed in the low-order 4096 bytes of
  979. * kernel virtual memory, offsets up to 4095 bytes can be accommodated.
  980. * If the offset is larger than 4095 bytes, a compile-time error will
  981. * be generated in __kfree_rcu(). If this error is triggered, you can
  982. * either fall back to use of call_rcu() or rearrange the structure to
  983. * position the rcu_head structure into the first 4096 bytes.
  984. *
  985. * Note that the allowable offset might decrease in the future, for example,
  986. * to allow something like kmem_cache_free_rcu().
  987. *
  988. * The BUILD_BUG_ON check must not involve any function calls, hence the
  989. * checks are done in macros here.
  990. */
  991. #define kfree_rcu(ptr, rcu_head) \
  992. __kfree_rcu(&((ptr)->rcu_head), offsetof(typeof(*(ptr)), rcu_head))
  993. #ifdef CONFIG_TINY_RCU
  994. static inline int rcu_needs_cpu(u64 basemono, u64 *nextevt)
  995. {
  996. *nextevt = KTIME_MAX;
  997. return 0;
  998. }
  999. #endif /* #ifdef CONFIG_TINY_RCU */
  1000. #if defined(CONFIG_RCU_NOCB_CPU_ALL)
  1001. static inline bool rcu_is_nocb_cpu(int cpu) { return true; }
  1002. #elif defined(CONFIG_RCU_NOCB_CPU)
  1003. bool rcu_is_nocb_cpu(int cpu);
  1004. #else
  1005. static inline bool rcu_is_nocb_cpu(int cpu) { return false; }
  1006. #endif
  1007. /* Only for use by adaptive-ticks code. */
  1008. #ifdef CONFIG_NO_HZ_FULL_SYSIDLE
  1009. bool rcu_sys_is_idle(void);
  1010. void rcu_sysidle_force_exit(void);
  1011. #else /* #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  1012. static inline bool rcu_sys_is_idle(void)
  1013. {
  1014. return false;
  1015. }
  1016. static inline void rcu_sysidle_force_exit(void)
  1017. {
  1018. }
  1019. #endif /* #else #ifdef CONFIG_NO_HZ_FULL_SYSIDLE */
  1020. /*
  1021. * Dump the ftrace buffer, but only one time per callsite per boot.
  1022. */
  1023. #define rcu_ftrace_dump(oops_dump_mode) \
  1024. do { \
  1025. static atomic_t ___rfd_beenhere = ATOMIC_INIT(0); \
  1026. \
  1027. if (!atomic_read(&___rfd_beenhere) && \
  1028. !atomic_xchg(&___rfd_beenhere, 1)) \
  1029. ftrace_dump(oops_dump_mode); \
  1030. } while (0)
  1031. /*
  1032. * Place this after a lock-acquisition primitive to guarantee that
  1033. * an UNLOCK+LOCK pair acts as a full barrier. This guarantee applies
  1034. * if the UNLOCK and LOCK are executed by the same CPU or if the
  1035. * UNLOCK and LOCK operate on the same lock variable.
  1036. */
  1037. #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE
  1038. #define smp_mb__after_unlock_lock() smp_mb() /* Full ordering for lock. */
  1039. #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
  1040. #define smp_mb__after_unlock_lock() do { } while (0)
  1041. #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */
  1042. #endif /* __LINUX_RCUPDATE_H */