list.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #ifndef _LINUX_LIST_H
  2. #define _LINUX_LIST_H
  3. #ifdef __KERNEL__
  4. #include <linux/stddef.h>
  5. #include <linux/prefetch.h>
  6. #include <asm/system.h>
  7. /*
  8. * These are non-NULL pointers that will result in page faults
  9. * under normal circumstances, used to verify that nobody uses
  10. * non-initialized list entries.
  11. */
  12. #define LIST_POISON1 ((void *) 0x00100100)
  13. #define LIST_POISON2 ((void *) 0x00200200)
  14. /*
  15. * Simple doubly linked list implementation.
  16. *
  17. * Some of the internal functions ("__xxx") are useful when
  18. * manipulating whole lists rather than single entries, as
  19. * sometimes we already know the next/prev entries and we can
  20. * generate better code by using them directly rather than
  21. * using the generic single-entry routines.
  22. */
  23. struct list_head {
  24. struct list_head *next, *prev;
  25. };
  26. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  27. #define LIST_HEAD(name) \
  28. struct list_head name = LIST_HEAD_INIT(name)
  29. #define INIT_LIST_HEAD(ptr) do { \
  30. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  31. } while (0)
  32. /*
  33. * Insert a new entry between two known consecutive entries.
  34. *
  35. * This is only for internal list manipulation where we know
  36. * the prev/next entries already!
  37. */
  38. static inline void __list_add(struct list_head *new,
  39. struct list_head *prev,
  40. struct list_head *next)
  41. {
  42. next->prev = new;
  43. new->next = next;
  44. new->prev = prev;
  45. prev->next = new;
  46. }
  47. /**
  48. * list_add - add a new entry
  49. * @new: new entry to be added
  50. * @head: list head to add it after
  51. *
  52. * Insert a new entry after the specified head.
  53. * This is good for implementing stacks.
  54. */
  55. static inline void list_add(struct list_head *new, struct list_head *head)
  56. {
  57. __list_add(new, head, head->next);
  58. }
  59. /**
  60. * list_add_tail - add a new entry
  61. * @new: new entry to be added
  62. * @head: list head to add it before
  63. *
  64. * Insert a new entry before the specified head.
  65. * This is useful for implementing queues.
  66. */
  67. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  68. {
  69. __list_add(new, head->prev, head);
  70. }
  71. /*
  72. * Insert a new entry between two known consecutive entries.
  73. *
  74. * This is only for internal list manipulation where we know
  75. * the prev/next entries already!
  76. */
  77. static inline void __list_add_rcu(struct list_head * new,
  78. struct list_head * prev, struct list_head * next)
  79. {
  80. new->next = next;
  81. new->prev = prev;
  82. smp_wmb();
  83. next->prev = new;
  84. prev->next = new;
  85. }
  86. /**
  87. * list_add_rcu - add a new entry to rcu-protected list
  88. * @new: new entry to be added
  89. * @head: list head to add it after
  90. *
  91. * Insert a new entry after the specified head.
  92. * This is good for implementing stacks.
  93. *
  94. * The caller must take whatever precautions are necessary
  95. * (such as holding appropriate locks) to avoid racing
  96. * with another list-mutation primitive, such as list_add_rcu()
  97. * or list_del_rcu(), running on this same list.
  98. * However, it is perfectly legal to run concurrently with
  99. * the _rcu list-traversal primitives, such as
  100. * list_for_each_entry_rcu().
  101. */
  102. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  103. {
  104. __list_add_rcu(new, head, head->next);
  105. }
  106. /**
  107. * list_add_tail_rcu - add a new entry to rcu-protected list
  108. * @new: new entry to be added
  109. * @head: list head to add it before
  110. *
  111. * Insert a new entry before the specified head.
  112. * This is useful for implementing queues.
  113. *
  114. * The caller must take whatever precautions are necessary
  115. * (such as holding appropriate locks) to avoid racing
  116. * with another list-mutation primitive, such as list_add_tail_rcu()
  117. * or list_del_rcu(), running on this same list.
  118. * However, it is perfectly legal to run concurrently with
  119. * the _rcu list-traversal primitives, such as
  120. * list_for_each_entry_rcu().
  121. */
  122. static inline void list_add_tail_rcu(struct list_head *new,
  123. struct list_head *head)
  124. {
  125. __list_add_rcu(new, head->prev, head);
  126. }
  127. /*
  128. * Delete a list entry by making the prev/next entries
  129. * point to each other.
  130. *
  131. * This is only for internal list manipulation where we know
  132. * the prev/next entries already!
  133. */
  134. static inline void __list_del(struct list_head * prev, struct list_head * next)
  135. {
  136. next->prev = prev;
  137. prev->next = next;
  138. }
  139. /**
  140. * list_del - deletes entry from list.
  141. * @entry: the element to delete from the list.
  142. * Note: list_empty on entry does not return true after this, the entry is
  143. * in an undefined state.
  144. */
  145. static inline void list_del(struct list_head *entry)
  146. {
  147. __list_del(entry->prev, entry->next);
  148. entry->next = LIST_POISON1;
  149. entry->prev = LIST_POISON2;
  150. }
  151. /**
  152. * list_del_rcu - deletes entry from list without re-initialization
  153. * @entry: the element to delete from the list.
  154. *
  155. * Note: list_empty on entry does not return true after this,
  156. * the entry is in an undefined state. It is useful for RCU based
  157. * lockfree traversal.
  158. *
  159. * In particular, it means that we can not poison the forward
  160. * pointers that may still be used for walking the list.
  161. *
  162. * The caller must take whatever precautions are necessary
  163. * (such as holding appropriate locks) to avoid racing
  164. * with another list-mutation primitive, such as list_del_rcu()
  165. * or list_add_rcu(), running on this same list.
  166. * However, it is perfectly legal to run concurrently with
  167. * the _rcu list-traversal primitives, such as
  168. * list_for_each_entry_rcu().
  169. *
  170. * Note that the caller is not permitted to immediately free
  171. * the newly deleted entry. Instead, either synchronize_rcu()
  172. * or call_rcu() must be used to defer freeing until an RCU
  173. * grace period has elapsed.
  174. */
  175. static inline void list_del_rcu(struct list_head *entry)
  176. {
  177. __list_del(entry->prev, entry->next);
  178. entry->prev = LIST_POISON2;
  179. }
  180. /*
  181. * list_replace_rcu - replace old entry by new one
  182. * @old : the element to be replaced
  183. * @new : the new element to insert
  184. *
  185. * The old entry will be replaced with the new entry atomically.
  186. */
  187. static inline void list_replace_rcu(struct list_head *old, struct list_head *new){
  188. new->next = old->next;
  189. new->prev = old->prev;
  190. smp_wmb();
  191. new->next->prev = new;
  192. new->prev->next = new;
  193. }
  194. /**
  195. * list_del_init - deletes entry from list and reinitialize it.
  196. * @entry: the element to delete from the list.
  197. */
  198. static inline void list_del_init(struct list_head *entry)
  199. {
  200. __list_del(entry->prev, entry->next);
  201. INIT_LIST_HEAD(entry);
  202. }
  203. /**
  204. * list_move - delete from one list and add as another's head
  205. * @list: the entry to move
  206. * @head: the head that will precede our entry
  207. */
  208. static inline void list_move(struct list_head *list, struct list_head *head)
  209. {
  210. __list_del(list->prev, list->next);
  211. list_add(list, head);
  212. }
  213. /**
  214. * list_move_tail - delete from one list and add as another's tail
  215. * @list: the entry to move
  216. * @head: the head that will follow our entry
  217. */
  218. static inline void list_move_tail(struct list_head *list,
  219. struct list_head *head)
  220. {
  221. __list_del(list->prev, list->next);
  222. list_add_tail(list, head);
  223. }
  224. /**
  225. * list_empty - tests whether a list is empty
  226. * @head: the list to test.
  227. */
  228. static inline int list_empty(const struct list_head *head)
  229. {
  230. return head->next == head;
  231. }
  232. /**
  233. * list_empty_careful - tests whether a list is
  234. * empty _and_ checks that no other CPU might be
  235. * in the process of still modifying either member
  236. *
  237. * NOTE: using list_empty_careful() without synchronization
  238. * can only be safe if the only activity that can happen
  239. * to the list entry is list_del_init(). Eg. it cannot be used
  240. * if another CPU could re-list_add() it.
  241. *
  242. * @head: the list to test.
  243. */
  244. static inline int list_empty_careful(const struct list_head *head)
  245. {
  246. struct list_head *next = head->next;
  247. return (next == head) && (next == head->prev);
  248. }
  249. static inline void __list_splice(struct list_head *list,
  250. struct list_head *head)
  251. {
  252. struct list_head *first = list->next;
  253. struct list_head *last = list->prev;
  254. struct list_head *at = head->next;
  255. first->prev = head;
  256. head->next = first;
  257. last->next = at;
  258. at->prev = last;
  259. }
  260. /**
  261. * list_splice - join two lists
  262. * @list: the new list to add.
  263. * @head: the place to add it in the first list.
  264. */
  265. static inline void list_splice(struct list_head *list, struct list_head *head)
  266. {
  267. if (!list_empty(list))
  268. __list_splice(list, head);
  269. }
  270. /**
  271. * list_splice_init - join two lists and reinitialise the emptied list.
  272. * @list: the new list to add.
  273. * @head: the place to add it in the first list.
  274. *
  275. * The list at @list is reinitialised
  276. */
  277. static inline void list_splice_init(struct list_head *list,
  278. struct list_head *head)
  279. {
  280. if (!list_empty(list)) {
  281. __list_splice(list, head);
  282. INIT_LIST_HEAD(list);
  283. }
  284. }
  285. /**
  286. * list_entry - get the struct for this entry
  287. * @ptr: the &struct list_head pointer.
  288. * @type: the type of the struct this is embedded in.
  289. * @member: the name of the list_struct within the struct.
  290. */
  291. #define list_entry(ptr, type, member) \
  292. container_of(ptr, type, member)
  293. /**
  294. * list_for_each - iterate over a list
  295. * @pos: the &struct list_head to use as a loop counter.
  296. * @head: the head for your list.
  297. */
  298. #define list_for_each(pos, head) \
  299. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  300. pos = pos->next)
  301. /**
  302. * __list_for_each - iterate over a list
  303. * @pos: the &struct list_head to use as a loop counter.
  304. * @head: the head for your list.
  305. *
  306. * This variant differs from list_for_each() in that it's the
  307. * simplest possible list iteration code, no prefetching is done.
  308. * Use this for code that knows the list to be very short (empty
  309. * or 1 entry) most of the time.
  310. */
  311. #define __list_for_each(pos, head) \
  312. for (pos = (head)->next; pos != (head); pos = pos->next)
  313. /**
  314. * list_for_each_prev - iterate over a list backwards
  315. * @pos: the &struct list_head to use as a loop counter.
  316. * @head: the head for your list.
  317. */
  318. #define list_for_each_prev(pos, head) \
  319. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  320. pos = pos->prev)
  321. /**
  322. * list_for_each_safe - iterate over a list safe against removal of list entry
  323. * @pos: the &struct list_head to use as a loop counter.
  324. * @n: another &struct list_head to use as temporary storage
  325. * @head: the head for your list.
  326. */
  327. #define list_for_each_safe(pos, n, head) \
  328. for (pos = (head)->next, n = pos->next; pos != (head); \
  329. pos = n, n = pos->next)
  330. /**
  331. * list_for_each_entry - iterate over list of given type
  332. * @pos: the type * to use as a loop counter.
  333. * @head: the head for your list.
  334. * @member: the name of the list_struct within the struct.
  335. */
  336. #define list_for_each_entry(pos, head, member) \
  337. for (pos = list_entry((head)->next, typeof(*pos), member); \
  338. prefetch(pos->member.next), &pos->member != (head); \
  339. pos = list_entry(pos->member.next, typeof(*pos), member))
  340. /**
  341. * list_for_each_entry_reverse - iterate backwards over list of given type.
  342. * @pos: the type * to use as a loop counter.
  343. * @head: the head for your list.
  344. * @member: the name of the list_struct within the struct.
  345. */
  346. #define list_for_each_entry_reverse(pos, head, member) \
  347. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  348. prefetch(pos->member.prev), &pos->member != (head); \
  349. pos = list_entry(pos->member.prev, typeof(*pos), member))
  350. /**
  351. * list_prepare_entry - prepare a pos entry for use as a start point in
  352. * list_for_each_entry_continue
  353. * @pos: the type * to use as a start point
  354. * @head: the head of the list
  355. * @member: the name of the list_struct within the struct.
  356. */
  357. #define list_prepare_entry(pos, head, member) \
  358. ((pos) ? : list_entry(head, typeof(*pos), member))
  359. /**
  360. * list_for_each_entry_continue - iterate over list of given type
  361. * continuing after existing point
  362. * @pos: the type * to use as a loop counter.
  363. * @head: the head for your list.
  364. * @member: the name of the list_struct within the struct.
  365. */
  366. #define list_for_each_entry_continue(pos, head, member) \
  367. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  368. prefetch(pos->member.next), &pos->member != (head); \
  369. pos = list_entry(pos->member.next, typeof(*pos), member))
  370. /**
  371. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  372. * @pos: the type * to use as a loop counter.
  373. * @n: another type * to use as temporary storage
  374. * @head: the head for your list.
  375. * @member: the name of the list_struct within the struct.
  376. */
  377. #define list_for_each_entry_safe(pos, n, head, member) \
  378. for (pos = list_entry((head)->next, typeof(*pos), member), \
  379. n = list_entry(pos->member.next, typeof(*pos), member); \
  380. &pos->member != (head); \
  381. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  382. /**
  383. * list_for_each_entry_safe_continue - iterate over list of given type
  384. * continuing after existing point safe against removal of list entry
  385. * @pos: the type * to use as a loop counter.
  386. * @n: another type * to use as temporary storage
  387. * @head: the head for your list.
  388. * @member: the name of the list_struct within the struct.
  389. */
  390. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  391. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  392. n = list_entry(pos->member.next, typeof(*pos), member); \
  393. &pos->member != (head); \
  394. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  395. /**
  396. * list_for_each_rcu - iterate over an rcu-protected list
  397. * @pos: the &struct list_head to use as a loop counter.
  398. * @head: the head for your list.
  399. *
  400. * This list-traversal primitive may safely run concurrently with
  401. * the _rcu list-mutation primitives such as list_add_rcu()
  402. * as long as the traversal is guarded by rcu_read_lock().
  403. */
  404. #define list_for_each_rcu(pos, head) \
  405. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  406. pos = rcu_dereference(pos->next))
  407. #define __list_for_each_rcu(pos, head) \
  408. for (pos = (head)->next; pos != (head); \
  409. pos = rcu_dereference(pos->next))
  410. /**
  411. * list_for_each_safe_rcu - iterate over an rcu-protected list safe
  412. * against removal of list entry
  413. * @pos: the &struct list_head to use as a loop counter.
  414. * @n: another &struct list_head to use as temporary storage
  415. * @head: the head for your list.
  416. *
  417. * This list-traversal primitive may safely run concurrently with
  418. * the _rcu list-mutation primitives such as list_add_rcu()
  419. * as long as the traversal is guarded by rcu_read_lock().
  420. */
  421. #define list_for_each_safe_rcu(pos, n, head) \
  422. for (pos = (head)->next, n = pos->next; pos != (head); \
  423. pos = rcu_dereference(n), n = pos->next)
  424. /**
  425. * list_for_each_entry_rcu - iterate over rcu list of given type
  426. * @pos: the type * to use as a loop counter.
  427. * @head: the head for your list.
  428. * @member: the name of the list_struct within the struct.
  429. *
  430. * This list-traversal primitive may safely run concurrently with
  431. * the _rcu list-mutation primitives such as list_add_rcu()
  432. * as long as the traversal is guarded by rcu_read_lock().
  433. */
  434. #define list_for_each_entry_rcu(pos, head, member) \
  435. for (pos = list_entry((head)->next, typeof(*pos), member); \
  436. prefetch(pos->member.next), &pos->member != (head); \
  437. pos = rcu_dereference(list_entry(pos->member.next, \
  438. typeof(*pos), member)))
  439. /**
  440. * list_for_each_continue_rcu - iterate over an rcu-protected list
  441. * continuing after existing point.
  442. * @pos: the &struct list_head to use as a loop counter.
  443. * @head: the head for your list.
  444. *
  445. * This list-traversal primitive may safely run concurrently with
  446. * the _rcu list-mutation primitives such as list_add_rcu()
  447. * as long as the traversal is guarded by rcu_read_lock().
  448. */
  449. #define list_for_each_continue_rcu(pos, head) \
  450. for ((pos) = (pos)->next; prefetch((pos)->next), (pos) != (head); \
  451. (pos) = rcu_dereference((pos)->next))
  452. /*
  453. * Double linked lists with a single pointer list head.
  454. * Mostly useful for hash tables where the two pointer list head is
  455. * too wasteful.
  456. * You lose the ability to access the tail in O(1).
  457. */
  458. struct hlist_head {
  459. struct hlist_node *first;
  460. };
  461. struct hlist_node {
  462. struct hlist_node *next, **pprev;
  463. };
  464. #define HLIST_HEAD_INIT { .first = NULL }
  465. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  466. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  467. #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
  468. static inline int hlist_unhashed(const struct hlist_node *h)
  469. {
  470. return !h->pprev;
  471. }
  472. static inline int hlist_empty(const struct hlist_head *h)
  473. {
  474. return !h->first;
  475. }
  476. static inline void __hlist_del(struct hlist_node *n)
  477. {
  478. struct hlist_node *next = n->next;
  479. struct hlist_node **pprev = n->pprev;
  480. *pprev = next;
  481. if (next)
  482. next->pprev = pprev;
  483. }
  484. static inline void hlist_del(struct hlist_node *n)
  485. {
  486. __hlist_del(n);
  487. n->next = LIST_POISON1;
  488. n->pprev = LIST_POISON2;
  489. }
  490. /**
  491. * hlist_del_rcu - deletes entry from hash list without re-initialization
  492. * @n: the element to delete from the hash list.
  493. *
  494. * Note: list_unhashed() on entry does not return true after this,
  495. * the entry is in an undefined state. It is useful for RCU based
  496. * lockfree traversal.
  497. *
  498. * In particular, it means that we can not poison the forward
  499. * pointers that may still be used for walking the hash list.
  500. *
  501. * The caller must take whatever precautions are necessary
  502. * (such as holding appropriate locks) to avoid racing
  503. * with another list-mutation primitive, such as hlist_add_head_rcu()
  504. * or hlist_del_rcu(), running on this same list.
  505. * However, it is perfectly legal to run concurrently with
  506. * the _rcu list-traversal primitives, such as
  507. * hlist_for_each_entry().
  508. */
  509. static inline void hlist_del_rcu(struct hlist_node *n)
  510. {
  511. __hlist_del(n);
  512. n->pprev = LIST_POISON2;
  513. }
  514. static inline void hlist_del_init(struct hlist_node *n)
  515. {
  516. if (n->pprev) {
  517. __hlist_del(n);
  518. INIT_HLIST_NODE(n);
  519. }
  520. }
  521. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  522. {
  523. struct hlist_node *first = h->first;
  524. n->next = first;
  525. if (first)
  526. first->pprev = &n->next;
  527. h->first = n;
  528. n->pprev = &h->first;
  529. }
  530. /**
  531. * hlist_add_head_rcu - adds the specified element to the specified hlist,
  532. * while permitting racing traversals.
  533. * @n: the element to add to the hash list.
  534. * @h: the list to add to.
  535. *
  536. * The caller must take whatever precautions are necessary
  537. * (such as holding appropriate locks) to avoid racing
  538. * with another list-mutation primitive, such as hlist_add_head_rcu()
  539. * or hlist_del_rcu(), running on this same list.
  540. * However, it is perfectly legal to run concurrently with
  541. * the _rcu list-traversal primitives, such as
  542. * hlist_for_each_rcu(), used to prevent memory-consistency
  543. * problems on Alpha CPUs. Regardless of the type of CPU, the
  544. * list-traversal primitive must be guarded by rcu_read_lock().
  545. */
  546. static inline void hlist_add_head_rcu(struct hlist_node *n,
  547. struct hlist_head *h)
  548. {
  549. struct hlist_node *first = h->first;
  550. n->next = first;
  551. n->pprev = &h->first;
  552. smp_wmb();
  553. if (first)
  554. first->pprev = &n->next;
  555. h->first = n;
  556. }
  557. /* next must be != NULL */
  558. static inline void hlist_add_before(struct hlist_node *n,
  559. struct hlist_node *next)
  560. {
  561. n->pprev = next->pprev;
  562. n->next = next;
  563. next->pprev = &n->next;
  564. *(n->pprev) = n;
  565. }
  566. static inline void hlist_add_after(struct hlist_node *n,
  567. struct hlist_node *next)
  568. {
  569. next->next = n->next;
  570. n->next = next;
  571. next->pprev = &n->next;
  572. if(next->next)
  573. next->next->pprev = &next->next;
  574. }
  575. /**
  576. * hlist_add_before_rcu - adds the specified element to the specified hlist
  577. * before the specified node while permitting racing traversals.
  578. * @n: the new element to add to the hash list.
  579. * @next: the existing element to add the new element before.
  580. *
  581. * The caller must take whatever precautions are necessary
  582. * (such as holding appropriate locks) to avoid racing
  583. * with another list-mutation primitive, such as hlist_add_head_rcu()
  584. * or hlist_del_rcu(), running on this same list.
  585. * However, it is perfectly legal to run concurrently with
  586. * the _rcu list-traversal primitives, such as
  587. * hlist_for_each_rcu(), used to prevent memory-consistency
  588. * problems on Alpha CPUs.
  589. */
  590. static inline void hlist_add_before_rcu(struct hlist_node *n,
  591. struct hlist_node *next)
  592. {
  593. n->pprev = next->pprev;
  594. n->next = next;
  595. smp_wmb();
  596. next->pprev = &n->next;
  597. *(n->pprev) = n;
  598. }
  599. /**
  600. * hlist_add_after_rcu - adds the specified element to the specified hlist
  601. * after the specified node while permitting racing traversals.
  602. * @prev: the existing element to add the new element after.
  603. * @n: the new element to add to the hash list.
  604. *
  605. * The caller must take whatever precautions are necessary
  606. * (such as holding appropriate locks) to avoid racing
  607. * with another list-mutation primitive, such as hlist_add_head_rcu()
  608. * or hlist_del_rcu(), running on this same list.
  609. * However, it is perfectly legal to run concurrently with
  610. * the _rcu list-traversal primitives, such as
  611. * hlist_for_each_rcu(), used to prevent memory-consistency
  612. * problems on Alpha CPUs.
  613. */
  614. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  615. struct hlist_node *n)
  616. {
  617. n->next = prev->next;
  618. n->pprev = &prev->next;
  619. smp_wmb();
  620. prev->next = n;
  621. if (n->next)
  622. n->next->pprev = &n->next;
  623. }
  624. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  625. #define hlist_for_each(pos, head) \
  626. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  627. pos = pos->next)
  628. #define hlist_for_each_safe(pos, n, head) \
  629. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  630. pos = n)
  631. #define hlist_for_each_rcu(pos, head) \
  632. for ((pos) = (head)->first; pos && ({ prefetch((pos)->next); 1; }); \
  633. (pos) = rcu_dereference((pos)->next))
  634. /**
  635. * hlist_for_each_entry - iterate over list of given type
  636. * @tpos: the type * to use as a loop counter.
  637. * @pos: the &struct hlist_node to use as a loop counter.
  638. * @head: the head for your list.
  639. * @member: the name of the hlist_node within the struct.
  640. */
  641. #define hlist_for_each_entry(tpos, pos, head, member) \
  642. for (pos = (head)->first; \
  643. pos && ({ prefetch(pos->next); 1;}) && \
  644. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  645. pos = pos->next)
  646. /**
  647. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  648. * @tpos: the type * to use as a loop counter.
  649. * @pos: the &struct hlist_node to use as a loop counter.
  650. * @member: the name of the hlist_node within the struct.
  651. */
  652. #define hlist_for_each_entry_continue(tpos, pos, member) \
  653. for (pos = (pos)->next; \
  654. pos && ({ prefetch(pos->next); 1;}) && \
  655. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  656. pos = pos->next)
  657. /**
  658. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  659. * @tpos: the type * to use as a loop counter.
  660. * @pos: the &struct hlist_node to use as a loop counter.
  661. * @member: the name of the hlist_node within the struct.
  662. */
  663. #define hlist_for_each_entry_from(tpos, pos, member) \
  664. for (; pos && ({ prefetch(pos->next); 1;}) && \
  665. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  666. pos = pos->next)
  667. /**
  668. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  669. * @tpos: the type * to use as a loop counter.
  670. * @pos: the &struct hlist_node to use as a loop counter.
  671. * @n: another &struct hlist_node to use as temporary storage
  672. * @head: the head for your list.
  673. * @member: the name of the hlist_node within the struct.
  674. */
  675. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  676. for (pos = (head)->first; \
  677. pos && ({ n = pos->next; 1; }) && \
  678. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  679. pos = n)
  680. /**
  681. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  682. * @pos: the type * to use as a loop counter.
  683. * @pos: the &struct hlist_node to use as a loop counter.
  684. * @head: the head for your list.
  685. * @member: the name of the hlist_node within the struct.
  686. *
  687. * This list-traversal primitive may safely run concurrently with
  688. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  689. * as long as the traversal is guarded by rcu_read_lock().
  690. */
  691. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  692. for (pos = (head)->first; \
  693. pos && ({ prefetch(pos->next); 1;}) && \
  694. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  695. pos = rcu_dereference(pos->next))
  696. #else
  697. #warning "don't include kernel headers in userspace"
  698. #endif /* __KERNEL__ */
  699. #endif