list.h 25 KB

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