list.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. #ifdef __KERNEL__
  4. #include <linux/stddef.h>
  5. #include <linux/poison.h>
  6. #include <linux/prefetch.h>
  7. #include <asm/system.h>
  8. /*
  9. * Simple doubly linked list implementation.
  10. *
  11. * Some of the internal functions ("__xxx") are useful when
  12. * manipulating whole lists rather than single entries, as
  13. * sometimes we already know the next/prev entries and we can
  14. * generate better code by using them directly rather than
  15. * using the generic single-entry routines.
  16. */
  17. struct list_head {
  18. struct list_head *next, *prev;
  19. };
  20. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  21. #define LIST_HEAD(name) \
  22. struct list_head name = LIST_HEAD_INIT(name)
  23. static inline void INIT_LIST_HEAD(struct list_head *list)
  24. {
  25. list->next = list;
  26. list->prev = list;
  27. }
  28. /*
  29. * Insert a new entry between two known consecutive entries.
  30. *
  31. * This is only for internal list manipulation where we know
  32. * the prev/next entries already!
  33. */
  34. #ifndef CONFIG_DEBUG_LIST
  35. static inline void __list_add(struct list_head *new,
  36. struct list_head *prev,
  37. struct list_head *next)
  38. {
  39. next->prev = new;
  40. new->next = next;
  41. new->prev = prev;
  42. prev->next = new;
  43. }
  44. #else
  45. extern void __list_add(struct list_head *new,
  46. struct list_head *prev,
  47. struct list_head *next);
  48. #endif
  49. /**
  50. * list_add - add a new entry
  51. * @new: new entry to be added
  52. * @head: list head to add it after
  53. *
  54. * Insert a new entry after the specified head.
  55. * This is good for implementing stacks.
  56. */
  57. #ifndef CONFIG_DEBUG_LIST
  58. static inline void list_add(struct list_head *new, struct list_head *head)
  59. {
  60. __list_add(new, head, head->next);
  61. }
  62. #else
  63. extern void list_add(struct list_head *new, struct list_head *head);
  64. #endif
  65. /**
  66. * list_add_tail - add a new entry
  67. * @new: new entry to be added
  68. * @head: list head to add it before
  69. *
  70. * Insert a new entry before the specified head.
  71. * This is useful for implementing queues.
  72. */
  73. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  74. {
  75. __list_add(new, head->prev, head);
  76. }
  77. /*
  78. * Insert a new entry between two known consecutive entries.
  79. *
  80. * This is only for internal list manipulation where we know
  81. * the prev/next entries already!
  82. */
  83. static inline void __list_add_rcu(struct list_head * new,
  84. struct list_head * prev, struct list_head * next)
  85. {
  86. new->next = next;
  87. new->prev = prev;
  88. smp_wmb();
  89. next->prev = new;
  90. prev->next = new;
  91. }
  92. /**
  93. * list_add_rcu - add a new entry to rcu-protected list
  94. * @new: new entry to be added
  95. * @head: list head to add it after
  96. *
  97. * Insert a new entry after the specified head.
  98. * This is good for implementing stacks.
  99. *
  100. * The caller must take whatever precautions are necessary
  101. * (such as holding appropriate locks) to avoid racing
  102. * with another list-mutation primitive, such as list_add_rcu()
  103. * or list_del_rcu(), running on this same list.
  104. * However, it is perfectly legal to run concurrently with
  105. * the _rcu list-traversal primitives, such as
  106. * list_for_each_entry_rcu().
  107. */
  108. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  109. {
  110. __list_add_rcu(new, head, head->next);
  111. }
  112. /**
  113. * list_add_tail_rcu - add a new entry to rcu-protected list
  114. * @new: new entry to be added
  115. * @head: list head to add it before
  116. *
  117. * Insert a new entry before the specified head.
  118. * This is useful for implementing queues.
  119. *
  120. * The caller must take whatever precautions are necessary
  121. * (such as holding appropriate locks) to avoid racing
  122. * with another list-mutation primitive, such as list_add_tail_rcu()
  123. * or list_del_rcu(), running on this same list.
  124. * However, it is perfectly legal to run concurrently with
  125. * the _rcu list-traversal primitives, such as
  126. * list_for_each_entry_rcu().
  127. */
  128. static inline void list_add_tail_rcu(struct list_head *new,
  129. struct list_head *head)
  130. {
  131. __list_add_rcu(new, head->prev, head);
  132. }
  133. /*
  134. * Delete a list entry by making the prev/next entries
  135. * point to each other.
  136. *
  137. * This is only for internal list manipulation where we know
  138. * the prev/next entries already!
  139. */
  140. static inline void __list_del(struct list_head * prev, struct list_head * next)
  141. {
  142. next->prev = prev;
  143. prev->next = next;
  144. }
  145. /**
  146. * list_del - deletes entry from list.
  147. * @entry: the element to delete from the list.
  148. * Note: list_empty() on entry does not return true after this, the entry is
  149. * in an undefined state.
  150. */
  151. #ifndef CONFIG_DEBUG_LIST
  152. static inline void list_del(struct list_head *entry)
  153. {
  154. __list_del(entry->prev, entry->next);
  155. entry->next = LIST_POISON1;
  156. entry->prev = LIST_POISON2;
  157. }
  158. #else
  159. extern void list_del(struct list_head *entry);
  160. #endif
  161. /**
  162. * list_del_rcu - deletes entry from list without re-initialization
  163. * @entry: the element to delete from the list.
  164. *
  165. * Note: list_empty() on entry does not return true after this,
  166. * the entry is in an undefined state. It is useful for RCU based
  167. * lockfree traversal.
  168. *
  169. * In particular, it means that we can not poison the forward
  170. * pointers that may still be used for walking the list.
  171. *
  172. * The caller must take whatever precautions are necessary
  173. * (such as holding appropriate locks) to avoid racing
  174. * with another list-mutation primitive, such as list_del_rcu()
  175. * or list_add_rcu(), running on this same list.
  176. * However, it is perfectly legal to run concurrently with
  177. * the _rcu list-traversal primitives, such as
  178. * list_for_each_entry_rcu().
  179. *
  180. * Note that the caller is not permitted to immediately free
  181. * the newly deleted entry. Instead, either synchronize_rcu()
  182. * or call_rcu() must be used to defer freeing until an RCU
  183. * grace period has elapsed.
  184. */
  185. static inline void list_del_rcu(struct list_head *entry)
  186. {
  187. __list_del(entry->prev, entry->next);
  188. entry->prev = LIST_POISON2;
  189. }
  190. /**
  191. * list_replace - replace old entry by new one
  192. * @old : the element to be replaced
  193. * @new : the new element to insert
  194. *
  195. * If @old was empty, it will be overwritten.
  196. */
  197. static inline void list_replace(struct list_head *old,
  198. struct list_head *new)
  199. {
  200. new->next = old->next;
  201. new->next->prev = new;
  202. new->prev = old->prev;
  203. new->prev->next = new;
  204. }
  205. static inline void list_replace_init(struct list_head *old,
  206. struct list_head *new)
  207. {
  208. list_replace(old, new);
  209. INIT_LIST_HEAD(old);
  210. }
  211. /**
  212. * list_replace_rcu - replace old entry by new one
  213. * @old : the element to be replaced
  214. * @new : the new element to insert
  215. *
  216. * The @old entry will be replaced with the @new entry atomically.
  217. * Note: @old should not be empty.
  218. */
  219. static inline void list_replace_rcu(struct list_head *old,
  220. struct list_head *new)
  221. {
  222. new->next = old->next;
  223. new->prev = old->prev;
  224. smp_wmb();
  225. new->next->prev = new;
  226. new->prev->next = new;
  227. old->prev = LIST_POISON2;
  228. }
  229. /**
  230. * list_del_init - deletes entry from list and reinitialize it.
  231. * @entry: the element to delete from the list.
  232. */
  233. static inline void list_del_init(struct list_head *entry)
  234. {
  235. __list_del(entry->prev, entry->next);
  236. INIT_LIST_HEAD(entry);
  237. }
  238. /**
  239. * list_move - delete from one list and add as another's head
  240. * @list: the entry to move
  241. * @head: the head that will precede our entry
  242. */
  243. static inline void list_move(struct list_head *list, struct list_head *head)
  244. {
  245. __list_del(list->prev, list->next);
  246. list_add(list, head);
  247. }
  248. /**
  249. * list_move_tail - delete from one list and add as another's tail
  250. * @list: the entry to move
  251. * @head: the head that will follow our entry
  252. */
  253. static inline void list_move_tail(struct list_head *list,
  254. struct list_head *head)
  255. {
  256. __list_del(list->prev, list->next);
  257. list_add_tail(list, head);
  258. }
  259. /**
  260. * list_is_last - tests whether @list is the last entry in list @head
  261. * @list: the entry to test
  262. * @head: the head of the list
  263. */
  264. static inline int list_is_last(const struct list_head *list,
  265. const struct list_head *head)
  266. {
  267. return list->next == head;
  268. }
  269. /**
  270. * list_empty - tests whether a list is empty
  271. * @head: the list to test.
  272. */
  273. static inline int list_empty(const struct list_head *head)
  274. {
  275. return head->next == head;
  276. }
  277. /**
  278. * list_empty_careful - tests whether a list is empty and not being modified
  279. * @head: the list to test
  280. *
  281. * Description:
  282. * tests whether a list is empty _and_ checks that no other CPU might be
  283. * in the process of modifying either member (next or prev)
  284. *
  285. * NOTE: using list_empty_careful() without synchronization
  286. * can only be safe if the only activity that can happen
  287. * to the list entry is list_del_init(). Eg. it cannot be used
  288. * if another CPU could re-list_add() it.
  289. */
  290. static inline int list_empty_careful(const struct list_head *head)
  291. {
  292. struct list_head *next = head->next;
  293. return (next == head) && (next == head->prev);
  294. }
  295. static inline void __list_splice(struct list_head *list,
  296. struct list_head *head)
  297. {
  298. struct list_head *first = list->next;
  299. struct list_head *last = list->prev;
  300. struct list_head *at = head->next;
  301. first->prev = head;
  302. head->next = first;
  303. last->next = at;
  304. at->prev = last;
  305. }
  306. /**
  307. * list_splice - join two lists
  308. * @list: the new list to add.
  309. * @head: the place to add it in the first list.
  310. */
  311. static inline void list_splice(struct list_head *list, struct list_head *head)
  312. {
  313. if (!list_empty(list))
  314. __list_splice(list, head);
  315. }
  316. /**
  317. * list_splice_init - join two lists and reinitialise the emptied list.
  318. * @list: the new list to add.
  319. * @head: the place to add it in the first list.
  320. *
  321. * The list at @list is reinitialised
  322. */
  323. static inline void list_splice_init(struct list_head *list,
  324. struct list_head *head)
  325. {
  326. if (!list_empty(list)) {
  327. __list_splice(list, head);
  328. INIT_LIST_HEAD(list);
  329. }
  330. }
  331. /**
  332. * list_splice_init_rcu - splice an RCU-protected list into an existing list.
  333. * @list: the RCU-protected list to splice
  334. * @head: the place in the list to splice the first list into
  335. * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
  336. *
  337. * @head can be RCU-read traversed concurrently with this function.
  338. *
  339. * Note that this function blocks.
  340. *
  341. * Important note: the caller must take whatever action is necessary to
  342. * prevent any other updates to @head. In principle, it is possible
  343. * to modify the list as soon as sync() begins execution.
  344. * If this sort of thing becomes necessary, an alternative version
  345. * based on call_rcu() could be created. But only if -really-
  346. * needed -- there is no shortage of RCU API members.
  347. */
  348. static inline void list_splice_init_rcu(struct list_head *list,
  349. struct list_head *head,
  350. void (*sync)(void))
  351. {
  352. struct list_head *first = list->next;
  353. struct list_head *last = list->prev;
  354. struct list_head *at = head->next;
  355. if (list_empty(head))
  356. return;
  357. /* "first" and "last" tracking list, so initialize it. */
  358. INIT_LIST_HEAD(list);
  359. /*
  360. * At this point, the list body still points to the source list.
  361. * Wait for any readers to finish using the list before splicing
  362. * the list body into the new list. Any new readers will see
  363. * an empty list.
  364. */
  365. sync();
  366. /*
  367. * Readers are finished with the source list, so perform splice.
  368. * The order is important if the new list is global and accessible
  369. * to concurrent RCU readers. Note that RCU readers are not
  370. * permitted to traverse the prev pointers without excluding
  371. * this function.
  372. */
  373. last->next = at;
  374. smp_wmb();
  375. head->next = first;
  376. first->prev = head;
  377. at->prev = last;
  378. }
  379. /**
  380. * list_entry - get the struct for this entry
  381. * @ptr: the &struct list_head pointer.
  382. * @type: the type of the struct this is embedded in.
  383. * @member: the name of the list_struct within the struct.
  384. */
  385. #define list_entry(ptr, type, member) \
  386. container_of(ptr, type, member)
  387. /**
  388. * list_first_entry - get the first element from a list
  389. * @ptr: the list head to take the element from.
  390. * @type: the type of the struct this is embedded in.
  391. * @member: the name of the list_struct within the struct.
  392. *
  393. * Note, that list is expected to be not empty.
  394. */
  395. #define list_first_entry(ptr, type, member) \
  396. list_entry((ptr)->next, type, member)
  397. /**
  398. * list_for_each - iterate over a list
  399. * @pos: the &struct list_head to use as a loop cursor.
  400. * @head: the head for your list.
  401. */
  402. #define list_for_each(pos, head) \
  403. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  404. pos = pos->next)
  405. /**
  406. * __list_for_each - iterate over a list
  407. * @pos: the &struct list_head to use as a loop cursor.
  408. * @head: the head for your list.
  409. *
  410. * This variant differs from list_for_each() in that it's the
  411. * simplest possible list iteration code, no prefetching is done.
  412. * Use this for code that knows the list to be very short (empty
  413. * or 1 entry) most of the time.
  414. */
  415. #define __list_for_each(pos, head) \
  416. for (pos = (head)->next; pos != (head); pos = pos->next)
  417. /**
  418. * list_for_each_prev - iterate over a list backwards
  419. * @pos: the &struct list_head to use as a loop cursor.
  420. * @head: the head for your list.
  421. */
  422. #define list_for_each_prev(pos, head) \
  423. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  424. pos = pos->prev)
  425. /**
  426. * list_for_each_safe - iterate over a list safe against removal of list entry
  427. * @pos: the &struct list_head to use as a loop cursor.
  428. * @n: another &struct list_head to use as temporary storage
  429. * @head: the head for your list.
  430. */
  431. #define list_for_each_safe(pos, n, head) \
  432. for (pos = (head)->next, n = pos->next; pos != (head); \
  433. pos = n, n = pos->next)
  434. /**
  435. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  436. * @pos: the &struct list_head to use as a loop cursor.
  437. * @n: another &struct list_head to use as temporary storage
  438. * @head: the head for your list.
  439. */
  440. #define list_for_each_prev_safe(pos, n, head) \
  441. for (pos = (head)->prev, n = pos->prev; \
  442. prefetch(pos->prev), pos != (head); \
  443. pos = n, n = pos->prev)
  444. /**
  445. * list_for_each_entry - iterate over list of given type
  446. * @pos: the type * to use as a loop cursor.
  447. * @head: the head for your list.
  448. * @member: the name of the list_struct within the struct.
  449. */
  450. #define list_for_each_entry(pos, head, member) \
  451. for (pos = list_entry((head)->next, typeof(*pos), member); \
  452. prefetch(pos->member.next), &pos->member != (head); \
  453. pos = list_entry(pos->member.next, typeof(*pos), member))
  454. /**
  455. * list_for_each_entry_reverse - iterate backwards over list of given type.
  456. * @pos: the type * to use as a loop cursor.
  457. * @head: the head for your list.
  458. * @member: the name of the list_struct within the struct.
  459. */
  460. #define list_for_each_entry_reverse(pos, head, member) \
  461. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  462. prefetch(pos->member.prev), &pos->member != (head); \
  463. pos = list_entry(pos->member.prev, typeof(*pos), member))
  464. /**
  465. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  466. * @pos: the type * to use as a start point
  467. * @head: the head of the list
  468. * @member: the name of the list_struct within the struct.
  469. *
  470. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  471. */
  472. #define list_prepare_entry(pos, head, member) \
  473. ((pos) ? : list_entry(head, typeof(*pos), member))
  474. /**
  475. * list_for_each_entry_continue - continue iteration over list of given type
  476. * @pos: the type * to use as a loop cursor.
  477. * @head: the head for your list.
  478. * @member: the name of the list_struct within the struct.
  479. *
  480. * Continue to iterate over list of given type, continuing after
  481. * the current position.
  482. */
  483. #define list_for_each_entry_continue(pos, head, member) \
  484. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  485. prefetch(pos->member.next), &pos->member != (head); \
  486. pos = list_entry(pos->member.next, typeof(*pos), member))
  487. /**
  488. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  489. * @pos: the type * to use as a loop cursor.
  490. * @head: the head for your list.
  491. * @member: the name of the list_struct within the struct.
  492. *
  493. * Start to iterate over list of given type backwards, continuing after
  494. * the current position.
  495. */
  496. #define list_for_each_entry_continue_reverse(pos, head, member) \
  497. for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
  498. prefetch(pos->member.prev), &pos->member != (head); \
  499. pos = list_entry(pos->member.prev, typeof(*pos), member))
  500. /**
  501. * list_for_each_entry_from - iterate over list of given type from the current point
  502. * @pos: the type * to use as a loop cursor.
  503. * @head: the head for your list.
  504. * @member: the name of the list_struct within the struct.
  505. *
  506. * Iterate over list of given type, continuing from current position.
  507. */
  508. #define list_for_each_entry_from(pos, head, member) \
  509. for (; prefetch(pos->member.next), &pos->member != (head); \
  510. pos = list_entry(pos->member.next, typeof(*pos), member))
  511. /**
  512. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  513. * @pos: the type * to use as a loop cursor.
  514. * @n: another type * to use as temporary storage
  515. * @head: the head for your list.
  516. * @member: the name of the list_struct within the struct.
  517. */
  518. #define list_for_each_entry_safe(pos, n, head, member) \
  519. for (pos = list_entry((head)->next, typeof(*pos), member), \
  520. n = list_entry(pos->member.next, typeof(*pos), member); \
  521. &pos->member != (head); \
  522. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  523. /**
  524. * list_for_each_entry_safe_continue
  525. * @pos: the type * to use as a loop cursor.
  526. * @n: another type * to use as temporary storage
  527. * @head: the head for your list.
  528. * @member: the name of the list_struct within the struct.
  529. *
  530. * Iterate over list of given type, continuing after current point,
  531. * safe against removal of list entry.
  532. */
  533. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  534. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  535. n = list_entry(pos->member.next, typeof(*pos), member); \
  536. &pos->member != (head); \
  537. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  538. /**
  539. * list_for_each_entry_safe_from
  540. * @pos: the type * to use as a loop cursor.
  541. * @n: another type * to use as temporary storage
  542. * @head: the head for your list.
  543. * @member: the name of the list_struct within the struct.
  544. *
  545. * Iterate over list of given type from current point, safe against
  546. * removal of list entry.
  547. */
  548. #define list_for_each_entry_safe_from(pos, n, head, member) \
  549. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  550. &pos->member != (head); \
  551. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  552. /**
  553. * list_for_each_entry_safe_reverse
  554. * @pos: the type * to use as a loop cursor.
  555. * @n: another type * to use as temporary storage
  556. * @head: the head for your list.
  557. * @member: the name of the list_struct within the struct.
  558. *
  559. * Iterate backwards over list of given type, safe against removal
  560. * of list entry.
  561. */
  562. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  563. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  564. n = list_entry(pos->member.prev, typeof(*pos), member); \
  565. &pos->member != (head); \
  566. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  567. /**
  568. * list_for_each_rcu - iterate over an rcu-protected list
  569. * @pos: the &struct list_head to use as a loop cursor.
  570. * @head: the head for your list.
  571. *
  572. * This list-traversal primitive may safely run concurrently with
  573. * the _rcu list-mutation primitives such as list_add_rcu()
  574. * as long as the traversal is guarded by rcu_read_lock().
  575. */
  576. #define list_for_each_rcu(pos, head) \
  577. for (pos = rcu_dereference((head)->next); \
  578. prefetch(pos->next), pos != (head); \
  579. pos = rcu_dereference(pos->next))
  580. #define __list_for_each_rcu(pos, head) \
  581. for (pos = rcu_dereference((head)->next); \
  582. pos != (head); \
  583. pos = rcu_dereference(pos->next))
  584. /**
  585. * list_for_each_entry_rcu - iterate over rcu list of given type
  586. * @pos: the type * to use as a loop cursor.
  587. * @head: the head for your list.
  588. * @member: the name of the list_struct within the struct.
  589. *
  590. * This list-traversal primitive may safely run concurrently with
  591. * the _rcu list-mutation primitives such as list_add_rcu()
  592. * as long as the traversal is guarded by rcu_read_lock().
  593. */
  594. #define list_for_each_entry_rcu(pos, head, member) \
  595. for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
  596. prefetch(pos->member.next), &pos->member != (head); \
  597. pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
  598. /**
  599. * list_for_each_continue_rcu
  600. * @pos: the &struct list_head to use as a loop cursor.
  601. * @head: the head for your list.
  602. *
  603. * Iterate over an rcu-protected list, continuing after current point.
  604. *
  605. * This list-traversal primitive may safely run concurrently with
  606. * the _rcu list-mutation primitives such as list_add_rcu()
  607. * as long as the traversal is guarded by rcu_read_lock().
  608. */
  609. #define list_for_each_continue_rcu(pos, head) \
  610. for ((pos) = rcu_dereference((pos)->next); \
  611. prefetch((pos)->next), (pos) != (head); \
  612. (pos) = rcu_dereference((pos)->next))
  613. /*
  614. * Double linked lists with a single pointer list head.
  615. * Mostly useful for hash tables where the two pointer list head is
  616. * too wasteful.
  617. * You lose the ability to access the tail in O(1).
  618. */
  619. struct hlist_head {
  620. struct hlist_node *first;
  621. };
  622. struct hlist_node {
  623. struct hlist_node *next, **pprev;
  624. };
  625. #define HLIST_HEAD_INIT { .first = NULL }
  626. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  627. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  628. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  629. {
  630. h->next = NULL;
  631. h->pprev = NULL;
  632. }
  633. static inline int hlist_unhashed(const struct hlist_node *h)
  634. {
  635. return !h->pprev;
  636. }
  637. static inline int hlist_empty(const struct hlist_head *h)
  638. {
  639. return !h->first;
  640. }
  641. static inline void __hlist_del(struct hlist_node *n)
  642. {
  643. struct hlist_node *next = n->next;
  644. struct hlist_node **pprev = n->pprev;
  645. *pprev = next;
  646. if (next)
  647. next->pprev = pprev;
  648. }
  649. static inline void hlist_del(struct hlist_node *n)
  650. {
  651. __hlist_del(n);
  652. n->next = LIST_POISON1;
  653. n->pprev = LIST_POISON2;
  654. }
  655. /**
  656. * hlist_del_rcu - deletes entry from hash list without re-initialization
  657. * @n: the element to delete from the hash list.
  658. *
  659. * Note: list_unhashed() on entry does not return true after this,
  660. * the entry is in an undefined state. It is useful for RCU based
  661. * lockfree traversal.
  662. *
  663. * In particular, it means that we can not poison the forward
  664. * pointers that may still be used for walking the hash list.
  665. *
  666. * The caller must take whatever precautions are necessary
  667. * (such as holding appropriate locks) to avoid racing
  668. * with another list-mutation primitive, such as hlist_add_head_rcu()
  669. * or hlist_del_rcu(), running on this same list.
  670. * However, it is perfectly legal to run concurrently with
  671. * the _rcu list-traversal primitives, such as
  672. * hlist_for_each_entry().
  673. */
  674. static inline void hlist_del_rcu(struct hlist_node *n)
  675. {
  676. __hlist_del(n);
  677. n->pprev = LIST_POISON2;
  678. }
  679. static inline void hlist_del_init(struct hlist_node *n)
  680. {
  681. if (!hlist_unhashed(n)) {
  682. __hlist_del(n);
  683. INIT_HLIST_NODE(n);
  684. }
  685. }
  686. /**
  687. * hlist_replace_rcu - replace old entry by new one
  688. * @old : the element to be replaced
  689. * @new : the new element to insert
  690. *
  691. * The @old entry will be replaced with the @new entry atomically.
  692. */
  693. static inline void hlist_replace_rcu(struct hlist_node *old,
  694. struct hlist_node *new)
  695. {
  696. struct hlist_node *next = old->next;
  697. new->next = next;
  698. new->pprev = old->pprev;
  699. smp_wmb();
  700. if (next)
  701. new->next->pprev = &new->next;
  702. *new->pprev = new;
  703. old->pprev = LIST_POISON2;
  704. }
  705. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  706. {
  707. struct hlist_node *first = h->first;
  708. n->next = first;
  709. if (first)
  710. first->pprev = &n->next;
  711. h->first = n;
  712. n->pprev = &h->first;
  713. }
  714. /**
  715. * hlist_add_head_rcu
  716. * @n: the element to add to the hash list.
  717. * @h: the list to add to.
  718. *
  719. * Description:
  720. * Adds the specified element to the specified hlist,
  721. * while permitting racing traversals.
  722. *
  723. * The caller must take whatever precautions are necessary
  724. * (such as holding appropriate locks) to avoid racing
  725. * with another list-mutation primitive, such as hlist_add_head_rcu()
  726. * or hlist_del_rcu(), running on this same list.
  727. * However, it is perfectly legal to run concurrently with
  728. * the _rcu list-traversal primitives, such as
  729. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  730. * problems on Alpha CPUs. Regardless of the type of CPU, the
  731. * list-traversal primitive must be guarded by rcu_read_lock().
  732. */
  733. static inline void hlist_add_head_rcu(struct hlist_node *n,
  734. struct hlist_head *h)
  735. {
  736. struct hlist_node *first = h->first;
  737. n->next = first;
  738. n->pprev = &h->first;
  739. smp_wmb();
  740. if (first)
  741. first->pprev = &n->next;
  742. h->first = n;
  743. }
  744. /* next must be != NULL */
  745. static inline void hlist_add_before(struct hlist_node *n,
  746. struct hlist_node *next)
  747. {
  748. n->pprev = next->pprev;
  749. n->next = next;
  750. next->pprev = &n->next;
  751. *(n->pprev) = n;
  752. }
  753. static inline void hlist_add_after(struct hlist_node *n,
  754. struct hlist_node *next)
  755. {
  756. next->next = n->next;
  757. n->next = next;
  758. next->pprev = &n->next;
  759. if(next->next)
  760. next->next->pprev = &next->next;
  761. }
  762. /**
  763. * hlist_add_before_rcu
  764. * @n: the new element to add to the hash list.
  765. * @next: the existing element to add the new element before.
  766. *
  767. * Description:
  768. * Adds the specified element to the specified hlist
  769. * before the specified node while permitting racing traversals.
  770. *
  771. * The caller must take whatever precautions are necessary
  772. * (such as holding appropriate locks) to avoid racing
  773. * with another list-mutation primitive, such as hlist_add_head_rcu()
  774. * or hlist_del_rcu(), running on this same list.
  775. * However, it is perfectly legal to run concurrently with
  776. * the _rcu list-traversal primitives, such as
  777. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  778. * problems on Alpha CPUs.
  779. */
  780. static inline void hlist_add_before_rcu(struct hlist_node *n,
  781. struct hlist_node *next)
  782. {
  783. n->pprev = next->pprev;
  784. n->next = next;
  785. smp_wmb();
  786. next->pprev = &n->next;
  787. *(n->pprev) = n;
  788. }
  789. /**
  790. * hlist_add_after_rcu
  791. * @prev: the existing element to add the new element after.
  792. * @n: the new element to add to the hash list.
  793. *
  794. * Description:
  795. * Adds the specified element to the specified hlist
  796. * after the specified node while permitting racing traversals.
  797. *
  798. * The caller must take whatever precautions are necessary
  799. * (such as holding appropriate locks) to avoid racing
  800. * with another list-mutation primitive, such as hlist_add_head_rcu()
  801. * or hlist_del_rcu(), running on this same list.
  802. * However, it is perfectly legal to run concurrently with
  803. * the _rcu list-traversal primitives, such as
  804. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  805. * problems on Alpha CPUs.
  806. */
  807. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  808. struct hlist_node *n)
  809. {
  810. n->next = prev->next;
  811. n->pprev = &prev->next;
  812. smp_wmb();
  813. prev->next = n;
  814. if (n->next)
  815. n->next->pprev = &n->next;
  816. }
  817. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  818. #define hlist_for_each(pos, head) \
  819. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  820. pos = pos->next)
  821. #define hlist_for_each_safe(pos, n, head) \
  822. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  823. pos = n)
  824. /**
  825. * hlist_for_each_entry - iterate over list of given type
  826. * @tpos: the type * to use as a loop cursor.
  827. * @pos: the &struct hlist_node to use as a loop cursor.
  828. * @head: the head for your list.
  829. * @member: the name of the hlist_node within the struct.
  830. */
  831. #define hlist_for_each_entry(tpos, pos, head, member) \
  832. for (pos = (head)->first; \
  833. pos && ({ prefetch(pos->next); 1;}) && \
  834. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  835. pos = pos->next)
  836. /**
  837. * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  838. * @tpos: the type * to use as a loop cursor.
  839. * @pos: the &struct hlist_node to use as a loop cursor.
  840. * @member: the name of the hlist_node within the struct.
  841. */
  842. #define hlist_for_each_entry_continue(tpos, pos, member) \
  843. for (pos = (pos)->next; \
  844. pos && ({ prefetch(pos->next); 1;}) && \
  845. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  846. pos = pos->next)
  847. /**
  848. * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  849. * @tpos: the type * to use as a loop cursor.
  850. * @pos: the &struct hlist_node to use as a loop cursor.
  851. * @member: the name of the hlist_node within the struct.
  852. */
  853. #define hlist_for_each_entry_from(tpos, pos, member) \
  854. for (; pos && ({ prefetch(pos->next); 1;}) && \
  855. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  856. pos = pos->next)
  857. /**
  858. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  859. * @tpos: the type * to use as a loop cursor.
  860. * @pos: the &struct hlist_node to use as a loop cursor.
  861. * @n: another &struct hlist_node to use as temporary storage
  862. * @head: the head for your list.
  863. * @member: the name of the hlist_node within the struct.
  864. */
  865. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  866. for (pos = (head)->first; \
  867. pos && ({ n = pos->next; 1; }) && \
  868. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  869. pos = n)
  870. /**
  871. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  872. * @tpos: the type * to use as a loop cursor.
  873. * @pos: the &struct hlist_node to use as a loop cursor.
  874. * @head: the head for your list.
  875. * @member: the name of the hlist_node within the struct.
  876. *
  877. * This list-traversal primitive may safely run concurrently with
  878. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  879. * as long as the traversal is guarded by rcu_read_lock().
  880. */
  881. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  882. for (pos = rcu_dereference((head)->first); \
  883. pos && ({ prefetch(pos->next); 1;}) && \
  884. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  885. pos = rcu_dereference(pos->next))
  886. #else
  887. #warning "don't include kernel headers in userspace"
  888. #endif /* __KERNEL__ */
  889. #endif