rcupdate.h 40 KB

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