list.h 30 KB

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