list.h 27 KB

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