jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

875 lines
28KB

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