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.

3497 lines
110KB

  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
  795. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  796. /*****************************************************************************
  797. *
  798. * Linux kernel header adapted for user-mode
  799. * The 2.6.17-rt1 version was used.
  800. *
  801. * Original copyright holders of this code are unknown, they were not
  802. * mentioned in the original file.
  803. *
  804. * This program is free software; you can redistribute it and/or modify
  805. * it under the terms of the GNU General Public License as published by
  806. * the Free Software Foundation; version 2 of the License
  807. *
  808. * This program is distributed in the hope that it will be useful,
  809. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  810. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  811. * GNU General Public License for more details.
  812. *
  813. * You should have received a copy of the GNU General Public License
  814. * along with this program; if not, write to the Free Software
  815. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  816. *
  817. *****************************************************************************/
  818. #ifndef _LINUX_LIST_H
  819. #define _LINUX_LIST_H
  820. #include <stddef.h>
  821. #if !defined(offsetof)
  822. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  823. #endif
  824. /**
  825. * container_of - cast a member of a structure out to the containing structure
  826. * @ptr: the pointer to the member.
  827. * @type: the type of the container struct this is embedded in.
  828. * @member: the name of the member within the struct.
  829. *
  830. */
  831. #define container_of(ptr, type, member) ({ \
  832. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  833. (type *)( (char *)__mptr - offsetof(type,member) );})
  834. #define prefetch(x) (x = x)
  835. /*
  836. * These are non-NULL pointers that will result in page faults
  837. * under normal circumstances, used to verify that nobody uses
  838. * non-initialized list entries.
  839. */
  840. #define LIST_POISON1 ((void *) 0x00100100)
  841. #define LIST_POISON2 ((void *) 0x00200200)
  842. /*
  843. * Simple doubly linked list implementation.
  844. *
  845. * Some of the internal functions ("__xxx") are useful when
  846. * manipulating whole lists rather than single entries, as
  847. * sometimes we already know the next/prev entries and we can
  848. * generate better code by using them directly rather than
  849. * using the generic single-entry routines.
  850. */
  851. struct list_head {
  852. struct list_head *next, *prev;
  853. };
  854. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  855. #define LIST_HEAD(name) \
  856. struct list_head name = LIST_HEAD_INIT(name)
  857. static inline void INIT_LIST_HEAD(struct list_head *list)
  858. {
  859. list->next = list;
  860. list->prev = list;
  861. }
  862. /*
  863. * Insert a new entry between two known consecutive entries.
  864. *
  865. * This is only for internal list manipulation where we know
  866. * the prev/next entries already!
  867. */
  868. static inline void __list_add(struct list_head *new,
  869. struct list_head *prev,
  870. struct list_head *next)
  871. {
  872. next->prev = new;
  873. new->next = next;
  874. new->prev = prev;
  875. prev->next = new;
  876. }
  877. /**
  878. * list_add - add a new entry
  879. * @new: new entry to be added
  880. * @head: list head to add it after
  881. *
  882. * Insert a new entry after the specified head.
  883. * This is good for implementing stacks.
  884. */
  885. static inline void list_add(struct list_head *new, struct list_head *head)
  886. {
  887. __list_add(new, head, head->next);
  888. }
  889. /**
  890. * list_add_tail - add a new entry
  891. * @new: new entry to be added
  892. * @head: list head to add it before
  893. *
  894. * Insert a new entry before the specified head.
  895. * This is useful for implementing queues.
  896. */
  897. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  898. {
  899. __list_add(new, head->prev, head);
  900. }
  901. /*
  902. * Insert a new entry between two known consecutive entries.
  903. *
  904. * This is only for internal list manipulation where we know
  905. * the prev/next entries already!
  906. */
  907. static inline void __list_add_rcu(struct list_head * new,
  908. struct list_head * prev, struct list_head * next)
  909. {
  910. new->next = next;
  911. new->prev = prev;
  912. // smp_wmb();
  913. next->prev = new;
  914. prev->next = new;
  915. }
  916. /**
  917. * list_add_rcu - add a new entry to rcu-protected list
  918. * @new: new entry to be added
  919. * @head: list head to add it after
  920. *
  921. * Insert a new entry after the specified head.
  922. * This is good for implementing stacks.
  923. *
  924. * The caller must take whatever precautions are necessary
  925. * (such as holding appropriate locks) to avoid racing
  926. * with another list-mutation primitive, such as list_add_rcu()
  927. * or list_del_rcu(), running on this same list.
  928. * However, it is perfectly legal to run concurrently with
  929. * the _rcu list-traversal primitives, such as
  930. * list_for_each_entry_rcu().
  931. */
  932. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  933. {
  934. __list_add_rcu(new, head, head->next);
  935. }
  936. /**
  937. * list_add_tail_rcu - add a new entry to rcu-protected list
  938. * @new: new entry to be added
  939. * @head: list head to add it before
  940. *
  941. * Insert a new entry before the specified head.
  942. * This is useful for implementing queues.
  943. *
  944. * The caller must take whatever precautions are necessary
  945. * (such as holding appropriate locks) to avoid racing
  946. * with another list-mutation primitive, such as list_add_tail_rcu()
  947. * or list_del_rcu(), running on this same list.
  948. * However, it is perfectly legal to run concurrently with
  949. * the _rcu list-traversal primitives, such as
  950. * list_for_each_entry_rcu().
  951. */
  952. static inline void list_add_tail_rcu(struct list_head *new,
  953. struct list_head *head)
  954. {
  955. __list_add_rcu(new, head->prev, head);
  956. }
  957. /*
  958. * Delete a list entry by making the prev/next entries
  959. * point to each other.
  960. *
  961. * This is only for internal list manipulation where we know
  962. * the prev/next entries already!
  963. */
  964. static inline void __list_del(struct list_head * prev, struct list_head * next)
  965. {
  966. next->prev = prev;
  967. prev->next = next;
  968. }
  969. /**
  970. * list_del - deletes entry from list.
  971. * @entry: the element to delete from the list.
  972. * Note: list_empty on entry does not return true after this, the entry is
  973. * in an undefined state.
  974. */
  975. static inline void list_del(struct list_head *entry)
  976. {
  977. __list_del(entry->prev, entry->next);
  978. entry->next = LIST_POISON1;
  979. entry->prev = LIST_POISON2;
  980. }
  981. /**
  982. * list_del_rcu - deletes entry from list without re-initialization
  983. * @entry: the element to delete from the list.
  984. *
  985. * Note: list_empty on entry does not return true after this,
  986. * the entry is in an undefined state. It is useful for RCU based
  987. * lockfree traversal.
  988. *
  989. * In particular, it means that we can not poison the forward
  990. * pointers that may still be used for walking the list.
  991. *
  992. * The caller must take whatever precautions are necessary
  993. * (such as holding appropriate locks) to avoid racing
  994. * with another list-mutation primitive, such as list_del_rcu()
  995. * or list_add_rcu(), running on this same list.
  996. * However, it is perfectly legal to run concurrently with
  997. * the _rcu list-traversal primitives, such as
  998. * list_for_each_entry_rcu().
  999. *
  1000. * Note that the caller is not permitted to immediately free
  1001. * the newly deleted entry. Instead, either synchronize_rcu()
  1002. * or call_rcu() must be used to defer freeing until an RCU
  1003. * grace period has elapsed.
  1004. */
  1005. static inline void list_del_rcu(struct list_head *entry)
  1006. {
  1007. __list_del(entry->prev, entry->next);
  1008. entry->prev = LIST_POISON2;
  1009. }
  1010. /*
  1011. * list_replace_rcu - replace old entry by new one
  1012. * @old : the element to be replaced
  1013. * @new : the new element to insert
  1014. *
  1015. * The old entry will be replaced with the new entry atomically.
  1016. */
  1017. static inline void list_replace_rcu(struct list_head *old,
  1018. struct list_head *new)
  1019. {
  1020. new->next = old->next;
  1021. new->prev = old->prev;
  1022. // smp_wmb();
  1023. new->next->prev = new;
  1024. new->prev->next = new;
  1025. old->prev = LIST_POISON2;
  1026. }
  1027. /**
  1028. * list_del_init - deletes entry from list and reinitialize it.
  1029. * @entry: the element to delete from the list.
  1030. */
  1031. static inline void list_del_init(struct list_head *entry)
  1032. {
  1033. __list_del(entry->prev, entry->next);
  1034. INIT_LIST_HEAD(entry);
  1035. }
  1036. /**
  1037. * list_move - delete from one list and add as another's head
  1038. * @list: the entry to move
  1039. * @head: the head that will precede our entry
  1040. */
  1041. static inline void list_move(struct list_head *list, struct list_head *head)
  1042. {
  1043. __list_del(list->prev, list->next);
  1044. list_add(list, head);
  1045. }
  1046. /**
  1047. * list_move_tail - delete from one list and add as another's tail
  1048. * @list: the entry to move
  1049. * @head: the head that will follow our entry
  1050. */
  1051. static inline void list_move_tail(struct list_head *list,
  1052. struct list_head *head)
  1053. {
  1054. __list_del(list->prev, list->next);
  1055. list_add_tail(list, head);
  1056. }
  1057. /**
  1058. * list_empty - tests whether a list is empty
  1059. * @head: the list to test.
  1060. */
  1061. static inline int list_empty(const struct list_head *head)
  1062. {
  1063. return head->next == head;
  1064. }
  1065. /**
  1066. * list_empty_careful - tests whether a list is
  1067. * empty _and_ checks that no other CPU might be
  1068. * in the process of still modifying either member
  1069. *
  1070. * NOTE: using list_empty_careful() without synchronization
  1071. * can only be safe if the only activity that can happen
  1072. * to the list entry is list_del_init(). Eg. it cannot be used
  1073. * if another CPU could re-list_add() it.
  1074. *
  1075. * @head: the list to test.
  1076. */
  1077. static inline int list_empty_careful(const struct list_head *head)
  1078. {
  1079. struct list_head *next = head->next;
  1080. return (next == head) && (next == head->prev);
  1081. }
  1082. static inline void __list_splice(struct list_head *list,
  1083. struct list_head *head)
  1084. {
  1085. struct list_head *first = list->next;
  1086. struct list_head *last = list->prev;
  1087. struct list_head *at = head->next;
  1088. first->prev = head;
  1089. head->next = first;
  1090. last->next = at;
  1091. at->prev = last;
  1092. }
  1093. /**
  1094. * list_splice - join two lists
  1095. * @list: the new list to add.
  1096. * @head: the place to add it in the first list.
  1097. */
  1098. static inline void list_splice(struct list_head *list, struct list_head *head)
  1099. {
  1100. if (!list_empty(list))
  1101. __list_splice(list, head);
  1102. }
  1103. /**
  1104. * list_splice_init - join two lists and reinitialise the emptied list.
  1105. * @list: the new list to add.
  1106. * @head: the place to add it in the first list.
  1107. *
  1108. * The list at @list is reinitialised
  1109. */
  1110. static inline void list_splice_init(struct list_head *list,
  1111. struct list_head *head)
  1112. {
  1113. if (!list_empty(list)) {
  1114. __list_splice(list, head);
  1115. INIT_LIST_HEAD(list);
  1116. }
  1117. }
  1118. /**
  1119. * list_entry - get the struct for this entry
  1120. * @ptr: the &struct list_head pointer.
  1121. * @type: the type of the struct this is embedded in.
  1122. * @member: the name of the list_struct within the struct.
  1123. */
  1124. #define list_entry(ptr, type, member) \
  1125. container_of(ptr, type, member)
  1126. /**
  1127. * list_for_each - iterate over a list
  1128. * @pos: the &struct list_head to use as a loop counter.
  1129. * @head: the head for your list.
  1130. */
  1131. #define list_for_each(pos, head) \
  1132. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  1133. pos = pos->next)
  1134. /**
  1135. * __list_for_each - iterate over a list
  1136. * @pos: the &struct list_head to use as a loop counter.
  1137. * @head: the head for your list.
  1138. *
  1139. * This variant differs from list_for_each() in that it's the
  1140. * simplest possible list iteration code, no prefetching is done.
  1141. * Use this for code that knows the list to be very short (empty
  1142. * or 1 entry) most of the time.
  1143. */
  1144. #define __list_for_each(pos, head) \
  1145. for (pos = (head)->next; pos != (head); pos = pos->next)
  1146. /**
  1147. * list_for_each_prev - iterate over a list backwards
  1148. * @pos: the &struct list_head to use as a loop counter.
  1149. * @head: the head for your list.
  1150. */
  1151. #define list_for_each_prev(pos, head) \
  1152. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  1153. pos = pos->prev)
  1154. /**
  1155. * list_for_each_safe - iterate over a list safe against removal of list entry
  1156. * @pos: the &struct list_head to use as a loop counter.
  1157. * @n: another &struct list_head to use as temporary storage
  1158. * @head: the head for your list.
  1159. */
  1160. #define list_for_each_safe(pos, n, head) \
  1161. for (pos = (head)->next, n = pos->next; pos != (head); \
  1162. pos = n, n = pos->next)
  1163. /**
  1164. * list_for_each_entry - iterate over list of given type
  1165. * @pos: the type * to use as a loop counter.
  1166. * @head: the head for your list.
  1167. * @member: the name of the list_struct within the struct.
  1168. */
  1169. #define list_for_each_entry(pos, head, member) \
  1170. for (pos = list_entry((head)->next, typeof(*pos), member); \
  1171. prefetch(pos->member.next), &pos->member != (head); \
  1172. pos = list_entry(pos->member.next, typeof(*pos), member))
  1173. /**
  1174. * list_for_each_entry_reverse - iterate backwards over list of given type.
  1175. * @pos: the type * to use as a loop counter.
  1176. * @head: the head for your list.
  1177. * @member: the name of the list_struct within the struct.
  1178. */
  1179. #define list_for_each_entry_reverse(pos, head, member) \
  1180. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  1181. prefetch(pos->member.prev), &pos->member != (head); \
  1182. pos = list_entry(pos->member.prev, typeof(*pos), member))
  1183. /**
  1184. * list_prepare_entry - prepare a pos entry for use as a start point in
  1185. * list_for_each_entry_continue
  1186. * @pos: the type * to use as a start point
  1187. * @head: the head of the list
  1188. * @member: the name of the list_struct within the struct.
  1189. */
  1190. #define list_prepare_entry(pos, head, member) \
  1191. ((pos) ? : list_entry(head, typeof(*pos), member))
  1192. /**
  1193. * list_for_each_entry_continue - iterate over list of given type
  1194. * continuing after existing point
  1195. * @pos: the type * to use as a loop counter.
  1196. * @head: the head for your list.
  1197. * @member: the name of the list_struct within the struct.
  1198. */
  1199. #define list_for_each_entry_continue(pos, head, member) \
  1200. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  1201. prefetch(pos->member.next), &pos->member != (head); \
  1202. pos = list_entry(pos->member.next, typeof(*pos), member))
  1203. /**
  1204. * list_for_each_entry_from - iterate over list of given type
  1205. * continuing from existing point
  1206. * @pos: the type * to use as a loop counter.
  1207. * @head: the head for your list.
  1208. * @member: the name of the list_struct within the struct.
  1209. */
  1210. #define list_for_each_entry_from(pos, head, member) \
  1211. for (; prefetch(pos->member.next), &pos->member != (head); \
  1212. pos = list_entry(pos->member.next, typeof(*pos), member))
  1213. /**
  1214. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  1215. * @pos: the type * to use as a loop counter.
  1216. * @n: another type * to use as temporary storage
  1217. * @head: the head for your list.
  1218. * @member: the name of the list_struct within the struct.
  1219. */
  1220. #define list_for_each_entry_safe(pos, n, head, member) \
  1221. for (pos = list_entry((head)->next, typeof(*pos), member), \
  1222. n = list_entry(pos->member.next, typeof(*pos), member); \
  1223. &pos->member != (head); \
  1224. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  1225. /**
  1226. * list_for_each_entry_safe_continue - iterate over list of given type
  1227. * continuing after existing point safe against removal of list entry
  1228. * @pos: the type * to use as a loop counter.
  1229. * @n: another type * to use as temporary storage
  1230. * @head: the head for your list.
  1231. * @member: the name of the list_struct within the struct.
  1232. */
  1233. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  1234. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  1235. n = list_entry(pos->member.next, typeof(*pos), member); \
  1236. &pos->member != (head); \
  1237. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  1238. /**
  1239. * list_for_each_entry_safe_from - iterate over list of given type
  1240. * from existing point safe against removal of list entry
  1241. * @pos: the type * to use as a loop counter.
  1242. * @n: another type * to use as temporary storage
  1243. * @head: the head for your list.
  1244. * @member: the name of the list_struct within the struct.
  1245. */
  1246. #define list_for_each_entry_safe_from(pos, n, head, member) \
  1247. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  1248. &pos->member != (head); \
  1249. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  1250. /**
  1251. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
  1252. * removal of list entry
  1253. * @pos: the type * to use as a loop counter.
  1254. * @n: another type * to use as temporary storage
  1255. * @head: the head for your list.
  1256. * @member: the name of the list_struct within the struct.
  1257. */
  1258. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  1259. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  1260. n = list_entry(pos->member.prev, typeof(*pos), member); \
  1261. &pos->member != (head); \
  1262. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  1263. /**
  1264. * list_for_each_rcu - iterate over an rcu-protected list
  1265. * @pos: the &struct list_head to use as a loop counter.
  1266. * @head: the head for your list.
  1267. *
  1268. * This list-traversal primitive may safely run concurrently with
  1269. * the _rcu list-mutation primitives such as list_add_rcu()
  1270. * as long as the traversal is guarded by rcu_read_lock().
  1271. */
  1272. #define list_for_each_rcu(pos, head) \
  1273. for (pos = (head)->next; \
  1274. prefetch(rcu_dereference(pos)->next), pos != (head); \
  1275. pos = pos->next)
  1276. #define __list_for_each_rcu(pos, head) \
  1277. for (pos = (head)->next; \
  1278. rcu_dereference(pos) != (head); \
  1279. pos = pos->next)
  1280. /**
  1281. * list_for_each_safe_rcu - iterate over an rcu-protected list safe
  1282. * against removal of list entry
  1283. * @pos: the &struct list_head to use as a loop counter.
  1284. * @n: another &struct list_head to use as temporary storage
  1285. * @head: the head for your list.
  1286. *
  1287. * This list-traversal primitive may safely run concurrently with
  1288. * the _rcu list-mutation primitives such as list_add_rcu()
  1289. * as long as the traversal is guarded by rcu_read_lock().
  1290. */
  1291. #define list_for_each_safe_rcu(pos, n, head) \
  1292. for (pos = (head)->next; \
  1293. n = rcu_dereference(pos)->next, pos != (head); \
  1294. pos = n)
  1295. /**
  1296. * list_for_each_entry_rcu - iterate over rcu list of given type
  1297. * @pos: the type * to use as a loop counter.
  1298. * @head: the head for your list.
  1299. * @member: the name of the list_struct within the struct.
  1300. *
  1301. * This list-traversal primitive may safely run concurrently with
  1302. * the _rcu list-mutation primitives such as list_add_rcu()
  1303. * as long as the traversal is guarded by rcu_read_lock().
  1304. */
  1305. #define list_for_each_entry_rcu(pos, head, member) \
  1306. for (pos = list_entry((head)->next, typeof(*pos), member); \
  1307. prefetch(rcu_dereference(pos)->member.next), \
  1308. &pos->member != (head); \
  1309. pos = list_entry(pos->member.next, typeof(*pos), member))
  1310. /**
  1311. * list_for_each_continue_rcu - iterate over an rcu-protected list
  1312. * continuing after existing point.
  1313. * @pos: the &struct list_head to use as a loop counter.
  1314. * @head: the head for your list.
  1315. *
  1316. * This list-traversal primitive may safely run concurrently with
  1317. * the _rcu list-mutation primitives such as list_add_rcu()
  1318. * as long as the traversal is guarded by rcu_read_lock().
  1319. */
  1320. #define list_for_each_continue_rcu(pos, head) \
  1321. for ((pos) = (pos)->next; \
  1322. prefetch(rcu_dereference((pos))->next), (pos) != (head); \
  1323. (pos) = (pos)->next)
  1324. /*
  1325. * Double linked lists with a single pointer list head.
  1326. * Mostly useful for hash tables where the two pointer list head is
  1327. * too wasteful.
  1328. * You lose the ability to access the tail in O(1).
  1329. */
  1330. struct hlist_head {
  1331. struct hlist_node *first;
  1332. };
  1333. struct hlist_node {
  1334. struct hlist_node *next, **pprev;
  1335. };
  1336. #define HLIST_HEAD_INIT { .first = NULL }
  1337. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  1338. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  1339. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  1340. {
  1341. h->next = NULL;
  1342. h->pprev = NULL;
  1343. }
  1344. static inline int hlist_unhashed(const struct hlist_node *h)
  1345. {
  1346. return !h->pprev;
  1347. }
  1348. static inline int hlist_empty(const struct hlist_head *h)
  1349. {
  1350. return !h->first;
  1351. }
  1352. static inline void __hlist_del(struct hlist_node *n)
  1353. {
  1354. struct hlist_node *next = n->next;
  1355. struct hlist_node **pprev = n->pprev;
  1356. *pprev = next;
  1357. if (next)
  1358. next->pprev = pprev;
  1359. }
  1360. static inline void hlist_del(struct hlist_node *n)
  1361. {
  1362. __hlist_del(n);
  1363. n->next = LIST_POISON1;
  1364. n->pprev = LIST_POISON2;
  1365. }
  1366. /**
  1367. * hlist_del_rcu - deletes entry from hash list without re-initialization
  1368. * @n: the element to delete from the hash list.
  1369. *
  1370. * Note: list_unhashed() on entry does not return true after this,
  1371. * the entry is in an undefined state. It is useful for RCU based
  1372. * lockfree traversal.
  1373. *
  1374. * In particular, it means that we can not poison the forward
  1375. * pointers that may still be used for walking the hash list.
  1376. *
  1377. * The caller must take whatever precautions are necessary
  1378. * (such as holding appropriate locks) to avoid racing
  1379. * with another list-mutation primitive, such as hlist_add_head_rcu()
  1380. * or hlist_del_rcu(), running on this same list.
  1381. * However, it is perfectly legal to run concurrently with
  1382. * the _rcu list-traversal primitives, such as
  1383. * hlist_for_each_entry().
  1384. */
  1385. static inline void hlist_del_rcu(struct hlist_node *n)
  1386. {
  1387. __hlist_del(n);
  1388. n->pprev = LIST_POISON2;
  1389. }
  1390. static inline void hlist_del_init(struct hlist_node *n)
  1391. {
  1392. if (!hlist_unhashed(n)) {
  1393. __hlist_del(n);
  1394. INIT_HLIST_NODE(n);
  1395. }
  1396. }
  1397. /*
  1398. * hlist_replace_rcu - replace old entry by new one
  1399. * @old : the element to be replaced
  1400. * @new : the new element to insert
  1401. *
  1402. * The old entry will be replaced with the new entry atomically.
  1403. */
  1404. static inline void hlist_replace_rcu(struct hlist_node *old,
  1405. struct hlist_node *new)
  1406. {
  1407. struct hlist_node *next = old->next;
  1408. new->next = next;
  1409. new->pprev = old->pprev;
  1410. // smp_wmb();
  1411. if (next)
  1412. new->next->pprev = &new->next;
  1413. *new->pprev = new;
  1414. old->pprev = LIST_POISON2;
  1415. }
  1416. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  1417. {
  1418. struct hlist_node *first = h->first;
  1419. n->next = first;
  1420. if (first)
  1421. first->pprev = &n->next;
  1422. h->first = n;
  1423. n->pprev = &h->first;
  1424. }
  1425. /**
  1426. * hlist_add_head_rcu - adds the specified element to the specified hlist,
  1427. * while permitting racing traversals.
  1428. * @n: the element to add to the hash list.
  1429. * @h: the list to add to.
  1430. *
  1431. * The caller must take whatever precautions are necessary
  1432. * (such as holding appropriate locks) to avoid racing
  1433. * with another list-mutation primitive, such as hlist_add_head_rcu()
  1434. * or hlist_del_rcu(), running on this same list.
  1435. * However, it is perfectly legal to run concurrently with
  1436. * the _rcu list-traversal primitives, such as
  1437. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  1438. * problems on Alpha CPUs. Regardless of the type of CPU, the
  1439. * list-traversal primitive must be guarded by rcu_read_lock().
  1440. */
  1441. static inline void hlist_add_head_rcu(struct hlist_node *n,
  1442. struct hlist_head *h)
  1443. {
  1444. struct hlist_node *first = h->first;
  1445. n->next = first;
  1446. n->pprev = &h->first;
  1447. // smp_wmb();
  1448. if (first)
  1449. first->pprev = &n->next;
  1450. h->first = n;
  1451. }
  1452. /* next must be != NULL */
  1453. static inline void hlist_add_before(struct hlist_node *n,
  1454. struct hlist_node *next)
  1455. {
  1456. n->pprev = next->pprev;
  1457. n->next = next;
  1458. next->pprev = &n->next;
  1459. *(n->pprev) = n;
  1460. }
  1461. static inline void hlist_add_after(struct hlist_node *n,
  1462. struct hlist_node *next)
  1463. {
  1464. next->next = n->next;
  1465. n->next = next;
  1466. next->pprev = &n->next;
  1467. if(next->next)
  1468. next->next->pprev = &next->next;
  1469. }
  1470. /**
  1471. * hlist_add_before_rcu - adds the specified element to the specified hlist
  1472. * before the specified node while permitting racing traversals.
  1473. * @n: the new element to add to the hash list.
  1474. * @next: the existing element to add the new element before.
  1475. *
  1476. * The caller must take whatever precautions are necessary
  1477. * (such as holding appropriate locks) to avoid racing
  1478. * with another list-mutation primitive, such as hlist_add_head_rcu()
  1479. * or hlist_del_rcu(), running on this same list.
  1480. * However, it is perfectly legal to run concurrently with
  1481. * the _rcu list-traversal primitives, such as
  1482. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  1483. * problems on Alpha CPUs.
  1484. */
  1485. static inline void hlist_add_before_rcu(struct hlist_node *n,
  1486. struct hlist_node *next)
  1487. {
  1488. n->pprev = next->pprev;
  1489. n->next = next;
  1490. // smp_wmb();
  1491. next->pprev = &n->next;
  1492. *(n->pprev) = n;
  1493. }
  1494. /**
  1495. * hlist_add_after_rcu - adds the specified element to the specified hlist
  1496. * after the specified node while permitting racing traversals.
  1497. * @prev: the existing element to add the new element after.
  1498. * @n: the new element to add to the hash list.
  1499. *
  1500. * The caller must take whatever precautions are necessary
  1501. * (such as holding appropriate locks) to avoid racing
  1502. * with another list-mutation primitive, such as hlist_add_head_rcu()
  1503. * or hlist_del_rcu(), running on this same list.
  1504. * However, it is perfectly legal to run concurrently with
  1505. * the _rcu list-traversal primitives, such as
  1506. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  1507. * problems on Alpha CPUs.
  1508. */
  1509. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  1510. struct hlist_node *n)
  1511. {
  1512. n->next = prev->next;
  1513. n->pprev = &prev->next;
  1514. // smp_wmb();
  1515. prev->next = n;
  1516. if (n->next)
  1517. n->next->pprev = &n->next;
  1518. }
  1519. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  1520. #define hlist_for_each(pos, head) \
  1521. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  1522. pos = pos->next)
  1523. #define hlist_for_each_safe(pos, n, head) \
  1524. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  1525. pos = n)
  1526. /**
  1527. * hlist_for_each_entry - iterate over list of given type
  1528. * @tpos: the type * to use as a loop counter.
  1529. * @pos: the &struct hlist_node to use as a loop counter.
  1530. * @head: the head for your list.
  1531. * @member: the name of the hlist_node within the struct.
  1532. */
  1533. #define hlist_for_each_entry(tpos, pos, head, member) \
  1534. for (pos = (head)->first; \
  1535. pos && ({ prefetch(pos->next); 1;}) && \
  1536. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  1537. pos = pos->next)
  1538. /**
  1539. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  1540. * @tpos: the type * to use as a loop counter.
  1541. * @pos: the &struct hlist_node to use as a loop counter.
  1542. * @member: the name of the hlist_node within the struct.
  1543. */
  1544. #define hlist_for_each_entry_continue(tpos, pos, member) \
  1545. for (pos = (pos)->next; \
  1546. pos && ({ prefetch(pos->next); 1;}) && \
  1547. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  1548. pos = pos->next)
  1549. /**
  1550. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  1551. * @tpos: the type * to use as a loop counter.
  1552. * @pos: the &struct hlist_node to use as a loop counter.
  1553. * @member: the name of the hlist_node within the struct.
  1554. */
  1555. #define hlist_for_each_entry_from(tpos, pos, member) \
  1556. for (; pos && ({ prefetch(pos->next); 1;}) && \
  1557. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  1558. pos = pos->next)
  1559. /**
  1560. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  1561. * @tpos: the type * to use as a loop counter.
  1562. * @pos: the &struct hlist_node to use as a loop counter.
  1563. * @n: another &struct hlist_node to use as temporary storage
  1564. * @head: the head for your list.
  1565. * @member: the name of the hlist_node within the struct.
  1566. */
  1567. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  1568. for (pos = (head)->first; \
  1569. pos && ({ n = pos->next; 1; }) && \
  1570. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  1571. pos = n)
  1572. /**
  1573. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  1574. * @tpos: the type * to use as a loop counter.
  1575. * @pos: the &struct hlist_node to use as a loop counter.
  1576. * @head: the head for your list.
  1577. * @member: the name of the hlist_node within the struct.
  1578. *
  1579. * This list-traversal primitive may safely run concurrently with
  1580. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  1581. * as long as the traversal is guarded by rcu_read_lock().
  1582. */
  1583. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  1584. for (pos = (head)->first; \
  1585. rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
  1586. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  1587. pos = pos->next)
  1588. #endif
  1589. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  1590. /*****************************************************************************
  1591. *
  1592. * Linux kernel header adapted for user-mode
  1593. * The 2.6.17-rt1 version was used.
  1594. *
  1595. * Original copyright holders of this code are unknown, they were not
  1596. * mentioned in the original file.
  1597. *
  1598. * This program is free software; you can redistribute it and/or modify
  1599. * it under the terms of the GNU General Public License as published by
  1600. * the Free Software Foundation; version 2 of the License
  1601. *
  1602. * This program is distributed in the hope that it will be useful,
  1603. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1604. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1605. * GNU General Public License for more details.
  1606. *
  1607. * You should have received a copy of the GNU General Public License
  1608. * along with this program; if not, write to the Free Software
  1609. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  1610. *
  1611. *****************************************************************************/
  1612. #ifndef _LINUX_LIST_H
  1613. #define _LINUX_LIST_H
  1614. #include <stddef.h>
  1615. #if !defined(offsetof)
  1616. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  1617. #endif
  1618. /**
  1619. * container_of - cast a member of a structure out to the containing structure
  1620. * @ptr: the pointer to the member.
  1621. * @type: the type of the container struct this is embedded in.
  1622. * @member: the name of the member within the struct.
  1623. *
  1624. */
  1625. #define container_of(ptr, type, member) ({ \
  1626. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  1627. (type *)( (char *)__mptr - offsetof(type,member) );})
  1628. #define prefetch(x) (x = x)
  1629. /*
  1630. * These are non-NULL pointers that will result in page faults
  1631. * under normal circumstances, used to verify that nobody uses
  1632. * non-initialized list entries.
  1633. */
  1634. #define LIST_POISON1 ((void *) 0x00100100)
  1635. #define LIST_POISON2 ((void *) 0x00200200)
  1636. /*
  1637. * Simple doubly linked list implementation.
  1638. *
  1639. * Some of the internal functions ("__xxx") are useful when
  1640. * manipulating whole lists rather than single entries, as
  1641. * sometimes we already know the next/prev entries and we can
  1642. * generate better code by using them directly rather than
  1643. * using the generic single-entry routines.
  1644. */
  1645. struct list_head {
  1646. struct list_head *next, *prev;
  1647. };
  1648. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  1649. #define LIST_HEAD(name) \
  1650. struct list_head name = LIST_HEAD_INIT(name)
  1651. static inline void INIT_LIST_HEAD(struct list_head *list)
  1652. {
  1653. list->next = list;
  1654. list->prev = list;
  1655. }
  1656. /*
  1657. * Insert a new entry between two known consecutive entries.
  1658. *
  1659. * This is only for internal list manipulation where we know
  1660. * the prev/next entries already!
  1661. */
  1662. static inline void __list_add(struct list_head *new,
  1663. struct list_head *prev,
  1664. struct list_head *next)
  1665. {
  1666. next->prev = new;
  1667. new->next = next;
  1668. new->prev = prev;
  1669. prev->next = new;
  1670. }
  1671. /**
  1672. * list_add - add a new entry
  1673. * @new: new entry to be added
  1674. * @head: list head to add it after
  1675. *
  1676. * Insert a new entry after the specified head.
  1677. * This is good for implementing stacks.
  1678. */
  1679. static inline void list_add(struct list_head *new, struct list_head *head)
  1680. {
  1681. __list_add(new, head, head->next);
  1682. }
  1683. /**
  1684. * list_add_tail - add a new entry
  1685. * @new: new entry to be added
  1686. * @head: list head to add it before
  1687. *
  1688. * Insert a new entry before the specified head.
  1689. * This is useful for implementing queues.
  1690. */
  1691. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  1692. {
  1693. __list_add(new, head->prev, head);
  1694. }
  1695. /*
  1696. * Insert a new entry between two known consecutive entries.
  1697. *
  1698. * This is only for internal list manipulation where we know
  1699. * the prev/next entries already!
  1700. */
  1701. static inline void __list_add_rcu(struct list_head * new,
  1702. struct list_head * prev, struct list_head * next)
  1703. {
  1704. new->next = next;
  1705. new->prev = prev;
  1706. // smp_wmb();
  1707. next->prev = new;
  1708. prev->next = new;
  1709. }
  1710. /**
  1711. * list_add_rcu - add a new entry to rcu-protected list
  1712. * @new: new entry to be added
  1713. * @head: list head to add it after
  1714. *
  1715. * Insert a new entry after the specified head.
  1716. * This is good for implementing stacks.
  1717. *
  1718. * The caller must take whatever precautions are necessary
  1719. * (such as holding appropriate locks) to avoid racing
  1720. * with another list-mutation primitive, such as list_add_rcu()
  1721. * or list_del_rcu(), running on this same list.
  1722. * However, it is perfectly legal to run concurrently with
  1723. * the _rcu list-traversal primitives, such as
  1724. * list_for_each_entry_rcu().
  1725. */
  1726. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  1727. {
  1728. __list_add_rcu(new, head, head->next);
  1729. }
  1730. /**
  1731. * list_add_tail_rcu - add a new entry to rcu-protected list
  1732. * @new: new entry to be added
  1733. * @head: list head to add it before
  1734. *
  1735. * Insert a new entry before the specified head.
  1736. * This is useful for implementing queues.
  1737. *
  1738. * The caller must take whatever precautions are necessary
  1739. * (such as holding appropriate locks) to avoid racing
  1740. * with another list-mutation primitive, such as list_add_tail_rcu()
  1741. * or list_del_rcu(), running on this same list.
  1742. * However, it is perfectly legal to run concurrently with
  1743. * the _rcu list-traversal primitives, such as
  1744. * list_for_each_entry_rcu().
  1745. */
  1746. static inline void list_add_tail_rcu(struct list_head *new,
  1747. struct list_head *head)
  1748. {
  1749. __list_add_rcu(new, head->prev, head);
  1750. }
  1751. /*
  1752. * Delete a list entry by making the prev/next entries
  1753. * point to each other.
  1754. *
  1755. * This is only for internal list manipulation where we know
  1756. * the prev/next entries already!
  1757. */
  1758. static inline void __list_del(struct list_head * prev, struct list_head * next)
  1759. {
  1760. next->prev = prev;
  1761. prev->next = next;
  1762. }
  1763. /**
  1764. * list_del - deletes entry from list.
  1765. * @entry: the element to delete from the list.
  1766. * Note: list_empty on entry does not return true after this, the entry is
  1767. * in an undefined state.
  1768. */
  1769. static inline void list_del(struct list_head *entry)
  1770. {
  1771. __list_del(entry->prev, entry->next);
  1772. entry->next = LIST_POISON1;
  1773. entry->prev = LIST_POISON2;
  1774. }
  1775. /**
  1776. * list_del_rcu - deletes entry from list without re-initialization
  1777. * @entry: the element to delete from the list.
  1778. *
  1779. * Note: list_empty on entry does not return true after this,
  1780. * the entry is in an undefined state. It is useful for RCU based
  1781. * lockfree traversal.
  1782. *
  1783. * In particular, it means that we can not poison the forward
  1784. * pointers that may still be used for walking the list.
  1785. *
  1786. * The caller must take whatever precautions are necessary
  1787. * (such as holding appropriate locks) to avoid racing
  1788. * with another list-mutation primitive, such as list_del_rcu()
  1789. * or list_add_rcu(), running on this same list.
  1790. * However, it is perfectly legal to run concurrently with
  1791. * the _rcu list-traversal primitives, such as
  1792. * list_for_each_entry_rcu().
  1793. *
  1794. * Note that the caller is not permitted to immediately free
  1795. * the newly deleted entry. Instead, either synchronize_rcu()
  1796. * or call_rcu() must be used to defer freeing until an RCU
  1797. * grace period has elapsed.
  1798. */
  1799. static inline void list_del_rcu(struct list_head *entry)
  1800. {
  1801. __list_del(entry->prev, entry->next);
  1802. entry->prev = LIST_POISON2;
  1803. }
  1804. /*
  1805. * list_replace_rcu - replace old entry by new one
  1806. * @old : the element to be replaced
  1807. * @new : the new element to insert
  1808. *
  1809. * The old entry will be replaced with the new entry atomically.
  1810. */
  1811. static inline void list_replace_rcu(struct list_head *old,
  1812. struct list_head *new)
  1813. {
  1814. new->next = old->next;
  1815. new->prev = old->prev;
  1816. // smp_wmb();
  1817. new->next->prev = new;
  1818. new->prev->next = new;
  1819. old->prev = LIST_POISON2;
  1820. }
  1821. /**
  1822. * list_del_init - deletes entry from list and reinitialize it.
  1823. * @entry: the element to delete from the list.
  1824. */
  1825. static inline void list_del_init(struct list_head *entry)
  1826. {
  1827. __list_del(entry->prev, entry->next);
  1828. INIT_LIST_HEAD(entry);
  1829. }
  1830. /**
  1831. * list_move - delete from one list and add as another's head
  1832. * @list: the entry to move
  1833. * @head: the head that will precede our entry
  1834. */
  1835. static inline void list_move(struct list_head *list, struct list_head *head)
  1836. {
  1837. __list_del(list->prev, list->next);
  1838. list_add(list, head);
  1839. }
  1840. /**
  1841. * list_move_tail - delete from one list and add as another's tail
  1842. * @list: the entry to move
  1843. * @head: the head that will follow our entry
  1844. */
  1845. static inline void list_move_tail(struct list_head *list,
  1846. struct list_head *head)
  1847. {
  1848. __list_del(list->prev, list->next);
  1849. list_add_tail(list, head);
  1850. }
  1851. /**
  1852. * list_empty - tests whether a list is empty
  1853. * @head: the list to test.
  1854. */
  1855. static inline int list_empty(const struct list_head *head)
  1856. {
  1857. return head->next == head;
  1858. }
  1859. /**
  1860. * list_empty_careful - tests whether a list is
  1861. * empty _and_ checks that no other CPU might be
  1862. * in the process of still modifying either member
  1863. *
  1864. * NOTE: using list_empty_careful() without synchronization
  1865. * can only be safe if the only activity that can happen
  1866. * to the list entry is list_del_init(). Eg. it cannot be used
  1867. * if another CPU could re-list_add() it.
  1868. *
  1869. * @head: the list to test.
  1870. */
  1871. static inline int list_empty_careful(const struct list_head *head)
  1872. {
  1873. struct list_head *next = head->next;
  1874. return (next == head) && (next == head->prev);
  1875. }
  1876. static inline void __list_splice(struct list_head *list,
  1877. struct list_head *head)
  1878. {
  1879. struct list_head *first = list->next;
  1880. struct list_head *last = list->prev;
  1881. struct list_head *at = head->next;
  1882. first->prev = head;
  1883. head->next = first;
  1884. last->next = at;
  1885. at->prev = last;
  1886. }
  1887. /**
  1888. * list_splice - join two lists
  1889. * @list: the new list to add.
  1890. * @head: the place to add it in the first list.
  1891. */
  1892. static inline void list_splice(struct list_head *list, struct list_head *head)
  1893. {
  1894. if (!list_empty(list))
  1895. __list_splice(list, head);
  1896. }
  1897. /**
  1898. * list_splice_init - join two lists and reinitialise the emptied list.
  1899. * @list: the new list to add.
  1900. * @head: the place to add it in the first list.
  1901. *
  1902. * The list at @list is reinitialised
  1903. */
  1904. static inline void list_splice_init(struct list_head *list,
  1905. struct list_head *head)
  1906. {
  1907. if (!list_empty(list)) {
  1908. __list_splice(list, head);
  1909. INIT_LIST_HEAD(list);
  1910. }
  1911. }
  1912. /**
  1913. * list_entry - get the struct for this entry
  1914. * @ptr: the &struct list_head pointer.
  1915. * @type: the type of the struct this is embedded in.
  1916. * @member: the name of the list_struct within the struct.
  1917. */
  1918. #define list_entry(ptr, type, member) \
  1919. container_of(ptr, type, member)
  1920. /**
  1921. * list_for_each - iterate over a list
  1922. * @pos: the &struct list_head to use as a loop counter.
  1923. * @head: the head for your list.
  1924. */
  1925. #define list_for_each(pos, head) \
  1926. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  1927. pos = pos->next)
  1928. /**
  1929. * __list_for_each - iterate over a list
  1930. * @pos: the &struct list_head to use as a loop counter.
  1931. * @head: the head for your list.
  1932. *
  1933. * This variant differs from list_for_each() in that it's the
  1934. * simplest possible list iteration code, no prefetching is done.
  1935. * Use this for code that knows the list to be very short (empty
  1936. * or 1 entry) most of the time.
  1937. */
  1938. #define __list_for_each(pos, head) \
  1939. for (pos = (head)->next; pos != (head); pos = pos->next)
  1940. /**
  1941. * list_for_each_prev - iterate over a list backwards
  1942. * @pos: the &struct list_head to use as a loop counter.
  1943. * @head: the head for your list.
  1944. */
  1945. #define list_for_each_prev(pos, head) \
  1946. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  1947. pos = pos->prev)
  1948. /**
  1949. * list_for_each_safe - iterate over a list safe against removal of list entry
  1950. * @pos: the &struct list_head to use as a loop counter.
  1951. * @n: another &struct list_head to use as temporary storage
  1952. * @head: the head for your list.
  1953. */
  1954. #define list_for_each_safe(pos, n, head) \
  1955. for (pos = (head)->next, n = pos->next; pos != (head); \
  1956. pos = n, n = pos->next)
  1957. /**
  1958. * list_for_each_entry - iterate over list of given type
  1959. * @pos: the type * to use as a loop counter.
  1960. * @head: the head for your list.
  1961. * @member: the name of the list_struct within the struct.
  1962. */
  1963. #define list_for_each_entry(pos, head, member) \
  1964. for (pos = list_entry((head)->next, typeof(*pos), member); \
  1965. prefetch(pos->member.next), &pos->member != (head); \
  1966. pos = list_entry(pos->member.next, typeof(*pos), member))
  1967. /**
  1968. * list_for_each_entry_reverse - iterate backwards over list of given type.
  1969. * @pos: the type * to use as a loop counter.
  1970. * @head: the head for your list.
  1971. * @member: the name of the list_struct within the struct.
  1972. */
  1973. #define list_for_each_entry_reverse(pos, head, member) \
  1974. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  1975. prefetch(pos->member.prev), &pos->member != (head); \
  1976. pos = list_entry(pos->member.prev, typeof(*pos), member))
  1977. /**
  1978. * list_prepare_entry - prepare a pos entry for use as a start point in
  1979. * list_for_each_entry_continue
  1980. * @pos: the type * to use as a start point
  1981. * @head: the head of the list
  1982. * @member: the name of the list_struct within the struct.
  1983. */
  1984. #define list_prepare_entry(pos, head, member) \
  1985. ((pos) ? : list_entry(head, typeof(*pos), member))
  1986. /**
  1987. * list_for_each_entry_continue - iterate over list of given type
  1988. * continuing after existing point
  1989. * @pos: the type * to use as a loop counter.
  1990. * @head: the head for your list.
  1991. * @member: the name of the list_struct within the struct.
  1992. */
  1993. #define list_for_each_entry_continue(pos, head, member) \
  1994. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  1995. prefetch(pos->member.next), &pos->member != (head); \
  1996. pos = list_entry(pos->member.next, typeof(*pos), member))
  1997. /**
  1998. * list_for_each_entry_from - iterate over list of given type
  1999. * continuing from existing point
  2000. * @pos: the type * to use as a loop counter.
  2001. * @head: the head for your list.
  2002. * @member: the name of the list_struct within the struct.
  2003. */
  2004. #define list_for_each_entry_from(pos, head, member) \
  2005. for (; prefetch(pos->member.next), &pos->member != (head); \
  2006. pos = list_entry(pos->member.next, typeof(*pos), member))
  2007. /**
  2008. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  2009. * @pos: the type * to use as a loop counter.
  2010. * @n: another type * to use as temporary storage
  2011. * @head: the head for your list.
  2012. * @member: the name of the list_struct within the struct.
  2013. */
  2014. #define list_for_each_entry_safe(pos, n, head, member) \
  2015. for (pos = list_entry((head)->next, typeof(*pos), member), \
  2016. n = list_entry(pos->member.next, typeof(*pos), member); \
  2017. &pos->member != (head); \
  2018. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2019. /**
  2020. * list_for_each_entry_safe_continue - iterate over list of given type
  2021. * continuing after existing point safe against removal of list entry
  2022. * @pos: the type * to use as a loop counter.
  2023. * @n: another type * to use as temporary storage
  2024. * @head: the head for your list.
  2025. * @member: the name of the list_struct within the struct.
  2026. */
  2027. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  2028. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  2029. n = list_entry(pos->member.next, typeof(*pos), member); \
  2030. &pos->member != (head); \
  2031. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2032. /**
  2033. * list_for_each_entry_safe_from - iterate over list of given type
  2034. * from existing point safe against removal of list entry
  2035. * @pos: the type * to use as a loop counter.
  2036. * @n: another type * to use as temporary storage
  2037. * @head: the head for your list.
  2038. * @member: the name of the list_struct within the struct.
  2039. */
  2040. #define list_for_each_entry_safe_from(pos, n, head, member) \
  2041. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  2042. &pos->member != (head); \
  2043. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2044. /**
  2045. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
  2046. * removal of list entry
  2047. * @pos: the type * to use as a loop counter.
  2048. * @n: another type * to use as temporary storage
  2049. * @head: the head for your list.
  2050. * @member: the name of the list_struct within the struct.
  2051. */
  2052. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  2053. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  2054. n = list_entry(pos->member.prev, typeof(*pos), member); \
  2055. &pos->member != (head); \
  2056. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  2057. /**
  2058. * list_for_each_rcu - iterate over an rcu-protected list
  2059. * @pos: the &struct list_head to use as a loop counter.
  2060. * @head: the head for your list.
  2061. *
  2062. * This list-traversal primitive may safely run concurrently with
  2063. * the _rcu list-mutation primitives such as list_add_rcu()
  2064. * as long as the traversal is guarded by rcu_read_lock().
  2065. */
  2066. #define list_for_each_rcu(pos, head) \
  2067. for (pos = (head)->next; \
  2068. prefetch(rcu_dereference(pos)->next), pos != (head); \
  2069. pos = pos->next)
  2070. #define __list_for_each_rcu(pos, head) \
  2071. for (pos = (head)->next; \
  2072. rcu_dereference(pos) != (head); \
  2073. pos = pos->next)
  2074. /**
  2075. * list_for_each_safe_rcu - iterate over an rcu-protected list safe
  2076. * against removal of list entry
  2077. * @pos: the &struct list_head to use as a loop counter.
  2078. * @n: another &struct list_head to use as temporary storage
  2079. * @head: the head for your list.
  2080. *
  2081. * This list-traversal primitive may safely run concurrently with
  2082. * the _rcu list-mutation primitives such as list_add_rcu()
  2083. * as long as the traversal is guarded by rcu_read_lock().
  2084. */
  2085. #define list_for_each_safe_rcu(pos, n, head) \
  2086. for (pos = (head)->next; \
  2087. n = rcu_dereference(pos)->next, pos != (head); \
  2088. pos = n)
  2089. /**
  2090. * list_for_each_entry_rcu - iterate over rcu list of given type
  2091. * @pos: the type * to use as a loop counter.
  2092. * @head: the head for your list.
  2093. * @member: the name of the list_struct within the struct.
  2094. *
  2095. * This list-traversal primitive may safely run concurrently with
  2096. * the _rcu list-mutation primitives such as list_add_rcu()
  2097. * as long as the traversal is guarded by rcu_read_lock().
  2098. */
  2099. #define list_for_each_entry_rcu(pos, head, member) \
  2100. for (pos = list_entry((head)->next, typeof(*pos), member); \
  2101. prefetch(rcu_dereference(pos)->member.next), \
  2102. &pos->member != (head); \
  2103. pos = list_entry(pos->member.next, typeof(*pos), member))
  2104. /**
  2105. * list_for_each_continue_rcu - iterate over an rcu-protected list
  2106. * continuing after existing point.
  2107. * @pos: the &struct list_head to use as a loop counter.
  2108. * @head: the head for your list.
  2109. *
  2110. * This list-traversal primitive may safely run concurrently with
  2111. * the _rcu list-mutation primitives such as list_add_rcu()
  2112. * as long as the traversal is guarded by rcu_read_lock().
  2113. */
  2114. #define list_for_each_continue_rcu(pos, head) \
  2115. for ((pos) = (pos)->next; \
  2116. prefetch(rcu_dereference((pos))->next), (pos) != (head); \
  2117. (pos) = (pos)->next)
  2118. /*
  2119. * Double linked lists with a single pointer list head.
  2120. * Mostly useful for hash tables where the two pointer list head is
  2121. * too wasteful.
  2122. * You lose the ability to access the tail in O(1).
  2123. */
  2124. struct hlist_head {
  2125. struct hlist_node *first;
  2126. };
  2127. struct hlist_node {
  2128. struct hlist_node *next, **pprev;
  2129. };
  2130. #define HLIST_HEAD_INIT { .first = NULL }
  2131. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  2132. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  2133. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  2134. {
  2135. h->next = NULL;
  2136. h->pprev = NULL;
  2137. }
  2138. static inline int hlist_unhashed(const struct hlist_node *h)
  2139. {
  2140. return !h->pprev;
  2141. }
  2142. static inline int hlist_empty(const struct hlist_head *h)
  2143. {
  2144. return !h->first;
  2145. }
  2146. static inline void __hlist_del(struct hlist_node *n)
  2147. {
  2148. struct hlist_node *next = n->next;
  2149. struct hlist_node **pprev = n->pprev;
  2150. *pprev = next;
  2151. if (next)
  2152. next->pprev = pprev;
  2153. }
  2154. static inline void hlist_del(struct hlist_node *n)
  2155. {
  2156. __hlist_del(n);
  2157. n->next = LIST_POISON1;
  2158. n->pprev = LIST_POISON2;
  2159. }
  2160. /**
  2161. * hlist_del_rcu - deletes entry from hash list without re-initialization
  2162. * @n: the element to delete from the hash list.
  2163. *
  2164. * Note: list_unhashed() on entry does not return true after this,
  2165. * the entry is in an undefined state. It is useful for RCU based
  2166. * lockfree traversal.
  2167. *
  2168. * In particular, it means that we can not poison the forward
  2169. * pointers that may still be used for walking the hash list.
  2170. *
  2171. * The caller must take whatever precautions are necessary
  2172. * (such as holding appropriate locks) to avoid racing
  2173. * with another list-mutation primitive, such as hlist_add_head_rcu()
  2174. * or hlist_del_rcu(), running on this same list.
  2175. * However, it is perfectly legal to run concurrently with
  2176. * the _rcu list-traversal primitives, such as
  2177. * hlist_for_each_entry().
  2178. */
  2179. static inline void hlist_del_rcu(struct hlist_node *n)
  2180. {
  2181. __hlist_del(n);
  2182. n->pprev = LIST_POISON2;
  2183. }
  2184. static inline void hlist_del_init(struct hlist_node *n)
  2185. {
  2186. if (!hlist_unhashed(n)) {
  2187. __hlist_del(n);
  2188. INIT_HLIST_NODE(n);
  2189. }
  2190. }
  2191. /*
  2192. * hlist_replace_rcu - replace old entry by new one
  2193. * @old : the element to be replaced
  2194. * @new : the new element to insert
  2195. *
  2196. * The old entry will be replaced with the new entry atomically.
  2197. */
  2198. static inline void hlist_replace_rcu(struct hlist_node *old,
  2199. struct hlist_node *new)
  2200. {
  2201. struct hlist_node *next = old->next;
  2202. new->next = next;
  2203. new->pprev = old->pprev;
  2204. // smp_wmb();
  2205. if (next)
  2206. new->next->pprev = &new->next;
  2207. *new->pprev = new;
  2208. old->pprev = LIST_POISON2;
  2209. }
  2210. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  2211. {
  2212. struct hlist_node *first = h->first;
  2213. n->next = first;
  2214. if (first)
  2215. first->pprev = &n->next;
  2216. h->first = n;
  2217. n->pprev = &h->first;
  2218. }
  2219. /**
  2220. * hlist_add_head_rcu - adds the specified element to the specified hlist,
  2221. * while permitting racing traversals.
  2222. * @n: the element to add to the hash list.
  2223. * @h: the list to add to.
  2224. *
  2225. * The caller must take whatever precautions are necessary
  2226. * (such as holding appropriate locks) to avoid racing
  2227. * with another list-mutation primitive, such as hlist_add_head_rcu()
  2228. * or hlist_del_rcu(), running on this same list.
  2229. * However, it is perfectly legal to run concurrently with
  2230. * the _rcu list-traversal primitives, such as
  2231. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  2232. * problems on Alpha CPUs. Regardless of the type of CPU, the
  2233. * list-traversal primitive must be guarded by rcu_read_lock().
  2234. */
  2235. static inline void hlist_add_head_rcu(struct hlist_node *n,
  2236. struct hlist_head *h)
  2237. {
  2238. struct hlist_node *first = h->first;
  2239. n->next = first;
  2240. n->pprev = &h->first;
  2241. // smp_wmb();
  2242. if (first)
  2243. first->pprev = &n->next;
  2244. h->first = n;
  2245. }
  2246. /* next must be != NULL */
  2247. static inline void hlist_add_before(struct hlist_node *n,
  2248. struct hlist_node *next)
  2249. {
  2250. n->pprev = next->pprev;
  2251. n->next = next;
  2252. next->pprev = &n->next;
  2253. *(n->pprev) = n;
  2254. }
  2255. static inline void hlist_add_after(struct hlist_node *n,
  2256. struct hlist_node *next)
  2257. {
  2258. next->next = n->next;
  2259. n->next = next;
  2260. next->pprev = &n->next;
  2261. if(next->next)
  2262. next->next->pprev = &next->next;
  2263. }
  2264. /**
  2265. * hlist_add_before_rcu - adds the specified element to the specified hlist
  2266. * before the specified node while permitting racing traversals.
  2267. * @n: the new element to add to the hash list.
  2268. * @next: the existing element to add the new element before.
  2269. *
  2270. * The caller must take whatever precautions are necessary
  2271. * (such as holding appropriate locks) to avoid racing
  2272. * with another list-mutation primitive, such as hlist_add_head_rcu()
  2273. * or hlist_del_rcu(), running on this same list.
  2274. * However, it is perfectly legal to run concurrently with
  2275. * the _rcu list-traversal primitives, such as
  2276. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  2277. * problems on Alpha CPUs.
  2278. */
  2279. static inline void hlist_add_before_rcu(struct hlist_node *n,
  2280. struct hlist_node *next)
  2281. {
  2282. n->pprev = next->pprev;
  2283. n->next = next;
  2284. // smp_wmb();
  2285. next->pprev = &n->next;
  2286. *(n->pprev) = n;
  2287. }
  2288. /**
  2289. * hlist_add_after_rcu - adds the specified element to the specified hlist
  2290. * after the specified node while permitting racing traversals.
  2291. * @prev: the existing element to add the new element after.
  2292. * @n: the new element to add to the hash list.
  2293. *
  2294. * The caller must take whatever precautions are necessary
  2295. * (such as holding appropriate locks) to avoid racing
  2296. * with another list-mutation primitive, such as hlist_add_head_rcu()
  2297. * or hlist_del_rcu(), running on this same list.
  2298. * However, it is perfectly legal to run concurrently with
  2299. * the _rcu list-traversal primitives, such as
  2300. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  2301. * problems on Alpha CPUs.
  2302. */
  2303. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  2304. struct hlist_node *n)
  2305. {
  2306. n->next = prev->next;
  2307. n->pprev = &prev->next;
  2308. // smp_wmb();
  2309. prev->next = n;
  2310. if (n->next)
  2311. n->next->pprev = &n->next;
  2312. }
  2313. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  2314. #define hlist_for_each(pos, head) \
  2315. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  2316. pos = pos->next)
  2317. #define hlist_for_each_safe(pos, n, head) \
  2318. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  2319. pos = n)
  2320. /**
  2321. * hlist_for_each_entry - iterate over list of given type
  2322. * @tpos: the type * to use as a loop counter.
  2323. * @pos: the &struct hlist_node to use as a loop counter.
  2324. * @head: the head for your list.
  2325. * @member: the name of the hlist_node within the struct.
  2326. */
  2327. #define hlist_for_each_entry(tpos, pos, head, member) \
  2328. for (pos = (head)->first; \
  2329. pos && ({ prefetch(pos->next); 1;}) && \
  2330. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  2331. pos = pos->next)
  2332. /**
  2333. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  2334. * @tpos: the type * to use as a loop counter.
  2335. * @pos: the &struct hlist_node to use as a loop counter.
  2336. * @member: the name of the hlist_node within the struct.
  2337. */
  2338. #define hlist_for_each_entry_continue(tpos, pos, member) \
  2339. for (pos = (pos)->next; \
  2340. pos && ({ prefetch(pos->next); 1;}) && \
  2341. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  2342. pos = pos->next)
  2343. /**
  2344. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  2345. * @tpos: the type * to use as a loop counter.
  2346. * @pos: the &struct hlist_node to use as a loop counter.
  2347. * @member: the name of the hlist_node within the struct.
  2348. */
  2349. #define hlist_for_each_entry_from(tpos, pos, member) \
  2350. for (; pos && ({ prefetch(pos->next); 1;}) && \
  2351. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  2352. pos = pos->next)
  2353. /**
  2354. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  2355. * @tpos: the type * to use as a loop counter.
  2356. * @pos: the &struct hlist_node to use as a loop counter.
  2357. * @n: another &struct hlist_node to use as temporary storage
  2358. * @head: the head for your list.
  2359. * @member: the name of the hlist_node within the struct.
  2360. */
  2361. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  2362. for (pos = (head)->first; \
  2363. pos && ({ n = pos->next; 1; }) && \
  2364. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  2365. pos = n)
  2366. /**
  2367. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  2368. * @tpos: the type * to use as a loop counter.
  2369. * @pos: the &struct hlist_node to use as a loop counter.
  2370. * @head: the head for your list.
  2371. * @member: the name of the hlist_node within the struct.
  2372. *
  2373. * This list-traversal primitive may safely run concurrently with
  2374. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  2375. * as long as the traversal is guarded by rcu_read_lock().
  2376. */
  2377. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  2378. for (pos = (head)->first; \
  2379. rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
  2380. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  2381. pos = pos->next)
  2382. #endif
  2383. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  2384. /*****************************************************************************
  2385. *
  2386. * Linux kernel header adapted for user-mode
  2387. * The 2.6.17-rt1 version was used.
  2388. *
  2389. * Original copyright holders of this code are unknown, they were not
  2390. * mentioned in the original file.
  2391. *
  2392. * This program is free software; you can redistribute it and/or modify
  2393. * it under the terms of the GNU General Public License as published by
  2394. * the Free Software Foundation; version 2 of the License
  2395. *
  2396. * This program is distributed in the hope that it will be useful,
  2397. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2398. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2399. * GNU General Public License for more details.
  2400. *
  2401. * You should have received a copy of the GNU General Public License
  2402. * along with this program; if not, write to the Free Software
  2403. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  2404. *
  2405. *****************************************************************************/
  2406. #ifndef _LINUX_LIST_H
  2407. #define _LINUX_LIST_H
  2408. #include <stddef.h>
  2409. #if !defined(offsetof)
  2410. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  2411. #endif
  2412. /**
  2413. * container_of - cast a member of a structure out to the containing structure
  2414. * @ptr: the pointer to the member.
  2415. * @type: the type of the container struct this is embedded in.
  2416. * @member: the name of the member within the struct.
  2417. *
  2418. */
  2419. #define container_of(ptr, type, member) ({ \
  2420. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  2421. (type *)( (char *)__mptr - offsetof(type,member) );})
  2422. #define prefetch(x) (x = x)
  2423. /*
  2424. * These are non-NULL pointers that will result in page faults
  2425. * under normal circumstances, used to verify that nobody uses
  2426. * non-initialized list entries.
  2427. */
  2428. #define LIST_POISON1 ((void *) 0x00100100)
  2429. #define LIST_POISON2 ((void *) 0x00200200)
  2430. /*
  2431. * Simple doubly linked list implementation.
  2432. *
  2433. * Some of the internal functions ("__xxx") are useful when
  2434. * manipulating whole lists rather than single entries, as
  2435. * sometimes we already know the next/prev entries and we can
  2436. * generate better code by using them directly rather than
  2437. * using the generic single-entry routines.
  2438. */
  2439. struct list_head {
  2440. struct list_head *next, *prev;
  2441. };
  2442. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  2443. #define LIST_HEAD(name) \
  2444. struct list_head name = LIST_HEAD_INIT(name)
  2445. static inline void INIT_LIST_HEAD(struct list_head *list)
  2446. {
  2447. list->next = list;
  2448. list->prev = list;
  2449. }
  2450. /*
  2451. * Insert a new entry between two known consecutive entries.
  2452. *
  2453. * This is only for internal list manipulation where we know
  2454. * the prev/next entries already!
  2455. */
  2456. static inline void __list_add(struct list_head *new,
  2457. struct list_head *prev,
  2458. struct list_head *next)
  2459. {
  2460. next->prev = new;
  2461. new->next = next;
  2462. new->prev = prev;
  2463. prev->next = new;
  2464. }
  2465. /**
  2466. * list_add - add a new entry
  2467. * @new: new entry to be added
  2468. * @head: list head to add it after
  2469. *
  2470. * Insert a new entry after the specified head.
  2471. * This is good for implementing stacks.
  2472. */
  2473. static inline void list_add(struct list_head *new, struct list_head *head)
  2474. {
  2475. __list_add(new, head, head->next);
  2476. }
  2477. /**
  2478. * list_add_tail - add a new entry
  2479. * @new: new entry to be added
  2480. * @head: list head to add it before
  2481. *
  2482. * Insert a new entry before the specified head.
  2483. * This is useful for implementing queues.
  2484. */
  2485. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  2486. {
  2487. __list_add(new, head->prev, head);
  2488. }
  2489. /*
  2490. * Insert a new entry between two known consecutive entries.
  2491. *
  2492. * This is only for internal list manipulation where we know
  2493. * the prev/next entries already!
  2494. */
  2495. static inline void __list_add_rcu(struct list_head * new,
  2496. struct list_head * prev, struct list_head * next)
  2497. {
  2498. new->next = next;
  2499. new->prev = prev;
  2500. // smp_wmb();
  2501. next->prev = new;
  2502. prev->next = new;
  2503. }
  2504. /**
  2505. * list_add_rcu - add a new entry to rcu-protected list
  2506. * @new: new entry to be added
  2507. * @head: list head to add it after
  2508. *
  2509. * Insert a new entry after the specified head.
  2510. * This is good for implementing stacks.
  2511. *
  2512. * The caller must take whatever precautions are necessary
  2513. * (such as holding appropriate locks) to avoid racing
  2514. * with another list-mutation primitive, such as list_add_rcu()
  2515. * or list_del_rcu(), running on this same list.
  2516. * However, it is perfectly legal to run concurrently with
  2517. * the _rcu list-traversal primitives, such as
  2518. * list_for_each_entry_rcu().
  2519. */
  2520. static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  2521. {
  2522. __list_add_rcu(new, head, head->next);
  2523. }
  2524. /**
  2525. * list_add_tail_rcu - add a new entry to rcu-protected list
  2526. * @new: new entry to be added
  2527. * @head: list head to add it before
  2528. *
  2529. * Insert a new entry before the specified head.
  2530. * This is useful for implementing queues.
  2531. *
  2532. * The caller must take whatever precautions are necessary
  2533. * (such as holding appropriate locks) to avoid racing
  2534. * with another list-mutation primitive, such as list_add_tail_rcu()
  2535. * or list_del_rcu(), running on this same list.
  2536. * However, it is perfectly legal to run concurrently with
  2537. * the _rcu list-traversal primitives, such as
  2538. * list_for_each_entry_rcu().
  2539. */
  2540. static inline void list_add_tail_rcu(struct list_head *new,
  2541. struct list_head *head)
  2542. {
  2543. __list_add_rcu(new, head->prev, head);
  2544. }
  2545. /*
  2546. * Delete a list entry by making the prev/next entries
  2547. * point to each other.
  2548. *
  2549. * This is only for internal list manipulation where we know
  2550. * the prev/next entries already!
  2551. */
  2552. static inline void __list_del(struct list_head * prev, struct list_head * next)
  2553. {
  2554. next->prev = prev;
  2555. prev->next = next;
  2556. }
  2557. /**
  2558. * list_del - deletes entry from list.
  2559. * @entry: the element to delete from the list.
  2560. * Note: list_empty on entry does not return true after this, the entry is
  2561. * in an undefined state.
  2562. */
  2563. static inline void list_del(struct list_head *entry)
  2564. {
  2565. __list_del(entry->prev, entry->next);
  2566. entry->next = LIST_POISON1;
  2567. entry->prev = LIST_POISON2;
  2568. }
  2569. /**
  2570. * list_del_rcu - deletes entry from list without re-initialization
  2571. * @entry: the element to delete from the list.
  2572. *
  2573. * Note: list_empty on entry does not return true after this,
  2574. * the entry is in an undefined state. It is useful for RCU based
  2575. * lockfree traversal.
  2576. *
  2577. * In particular, it means that we can not poison the forward
  2578. * pointers that may still be used for walking the list.
  2579. *
  2580. * The caller must take whatever precautions are necessary
  2581. * (such as holding appropriate locks) to avoid racing
  2582. * with another list-mutation primitive, such as list_del_rcu()
  2583. * or list_add_rcu(), running on this same list.
  2584. * However, it is perfectly legal to run concurrently with
  2585. * the _rcu list-traversal primitives, such as
  2586. * list_for_each_entry_rcu().
  2587. *
  2588. * Note that the caller is not permitted to immediately free
  2589. * the newly deleted entry. Instead, either synchronize_rcu()
  2590. * or call_rcu() must be used to defer freeing until an RCU
  2591. * grace period has elapsed.
  2592. */
  2593. static inline void list_del_rcu(struct list_head *entry)
  2594. {
  2595. __list_del(entry->prev, entry->next);
  2596. entry->prev = LIST_POISON2;
  2597. }
  2598. /*
  2599. * list_replace_rcu - replace old entry by new one
  2600. * @old : the element to be replaced
  2601. * @new : the new element to insert
  2602. *
  2603. * The old entry will be replaced with the new entry atomically.
  2604. */
  2605. static inline void list_replace_rcu(struct list_head *old,
  2606. struct list_head *new)
  2607. {
  2608. new->next = old->next;
  2609. new->prev = old->prev;
  2610. // smp_wmb();
  2611. new->next->prev = new;
  2612. new->prev->next = new;
  2613. old->prev = LIST_POISON2;
  2614. }
  2615. /**
  2616. * list_del_init - deletes entry from list and reinitialize it.
  2617. * @entry: the element to delete from the list.
  2618. */
  2619. static inline void list_del_init(struct list_head *entry)
  2620. {
  2621. __list_del(entry->prev, entry->next);
  2622. INIT_LIST_HEAD(entry);
  2623. }
  2624. /**
  2625. * list_move - delete from one list and add as another's head
  2626. * @list: the entry to move
  2627. * @head: the head that will precede our entry
  2628. */
  2629. static inline void list_move(struct list_head *list, struct list_head *head)
  2630. {
  2631. __list_del(list->prev, list->next);
  2632. list_add(list, head);
  2633. }
  2634. /**
  2635. * list_move_tail - delete from one list and add as another's tail
  2636. * @list: the entry to move
  2637. * @head: the head that will follow our entry
  2638. */
  2639. static inline void list_move_tail(struct list_head *list,
  2640. struct list_head *head)
  2641. {
  2642. __list_del(list->prev, list->next);
  2643. list_add_tail(list, head);
  2644. }
  2645. /**
  2646. * list_empty - tests whether a list is empty
  2647. * @head: the list to test.
  2648. */
  2649. static inline int list_empty(const struct list_head *head)
  2650. {
  2651. return head->next == head;
  2652. }
  2653. /**
  2654. * list_empty_careful - tests whether a list is
  2655. * empty _and_ checks that no other CPU might be
  2656. * in the process of still modifying either member
  2657. *
  2658. * NOTE: using list_empty_careful() without synchronization
  2659. * can only be safe if the only activity that can happen
  2660. * to the list entry is list_del_init(). Eg. it cannot be used
  2661. * if another CPU could re-list_add() it.
  2662. *
  2663. * @head: the list to test.
  2664. */
  2665. static inline int list_empty_careful(const struct list_head *head)
  2666. {
  2667. struct list_head *next = head->next;
  2668. return (next == head) && (next == head->prev);
  2669. }
  2670. static inline void __list_splice(struct list_head *list,
  2671. struct list_head *head)
  2672. {
  2673. struct list_head *first = list->next;
  2674. struct list_head *last = list->prev;
  2675. struct list_head *at = head->next;
  2676. first->prev = head;
  2677. head->next = first;
  2678. last->next = at;
  2679. at->prev = last;
  2680. }
  2681. /**
  2682. * list_splice - join two lists
  2683. * @list: the new list to add.
  2684. * @head: the place to add it in the first list.
  2685. */
  2686. static inline void list_splice(struct list_head *list, struct list_head *head)
  2687. {
  2688. if (!list_empty(list))
  2689. __list_splice(list, head);
  2690. }
  2691. /**
  2692. * list_splice_init - join two lists and reinitialise the emptied list.
  2693. * @list: the new list to add.
  2694. * @head: the place to add it in the first list.
  2695. *
  2696. * The list at @list is reinitialised
  2697. */
  2698. static inline void list_splice_init(struct list_head *list,
  2699. struct list_head *head)
  2700. {
  2701. if (!list_empty(list)) {
  2702. __list_splice(list, head);
  2703. INIT_LIST_HEAD(list);
  2704. }
  2705. }
  2706. /**
  2707. * list_entry - get the struct for this entry
  2708. * @ptr: the &struct list_head pointer.
  2709. * @type: the type of the struct this is embedded in.
  2710. * @member: the name of the list_struct within the struct.
  2711. */
  2712. #define list_entry(ptr, type, member) \
  2713. container_of(ptr, type, member)
  2714. /**
  2715. * list_for_each - iterate over a list
  2716. * @pos: the &struct list_head to use as a loop counter.
  2717. * @head: the head for your list.
  2718. */
  2719. #define list_for_each(pos, head) \
  2720. for (pos = (head)->next; prefetch(pos->next), pos != (head); \
  2721. pos = pos->next)
  2722. /**
  2723. * __list_for_each - iterate over a list
  2724. * @pos: the &struct list_head to use as a loop counter.
  2725. * @head: the head for your list.
  2726. *
  2727. * This variant differs from list_for_each() in that it's the
  2728. * simplest possible list iteration code, no prefetching is done.
  2729. * Use this for code that knows the list to be very short (empty
  2730. * or 1 entry) most of the time.
  2731. */
  2732. #define __list_for_each(pos, head) \
  2733. for (pos = (head)->next; pos != (head); pos = pos->next)
  2734. /**
  2735. * list_for_each_prev - iterate over a list backwards
  2736. * @pos: the &struct list_head to use as a loop counter.
  2737. * @head: the head for your list.
  2738. */
  2739. #define list_for_each_prev(pos, head) \
  2740. for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
  2741. pos = pos->prev)
  2742. /**
  2743. * list_for_each_safe - iterate over a list safe against removal of list entry
  2744. * @pos: the &struct list_head to use as a loop counter.
  2745. * @n: another &struct list_head to use as temporary storage
  2746. * @head: the head for your list.
  2747. */
  2748. #define list_for_each_safe(pos, n, head) \
  2749. for (pos = (head)->next, n = pos->next; pos != (head); \
  2750. pos = n, n = pos->next)
  2751. /**
  2752. * list_for_each_entry - iterate over list of given type
  2753. * @pos: the type * to use as a loop counter.
  2754. * @head: the head for your list.
  2755. * @member: the name of the list_struct within the struct.
  2756. */
  2757. #define list_for_each_entry(pos, head, member) \
  2758. for (pos = list_entry((head)->next, typeof(*pos), member); \
  2759. prefetch(pos->member.next), &pos->member != (head); \
  2760. pos = list_entry(pos->member.next, typeof(*pos), member))
  2761. /**
  2762. * list_for_each_entry_reverse - iterate backwards over list of given type.
  2763. * @pos: the type * to use as a loop counter.
  2764. * @head: the head for your list.
  2765. * @member: the name of the list_struct within the struct.
  2766. */
  2767. #define list_for_each_entry_reverse(pos, head, member) \
  2768. for (pos = list_entry((head)->prev, typeof(*pos), member); \
  2769. prefetch(pos->member.prev), &pos->member != (head); \
  2770. pos = list_entry(pos->member.prev, typeof(*pos), member))
  2771. /**
  2772. * list_prepare_entry - prepare a pos entry for use as a start point in
  2773. * list_for_each_entry_continue
  2774. * @pos: the type * to use as a start point
  2775. * @head: the head of the list
  2776. * @member: the name of the list_struct within the struct.
  2777. */
  2778. #define list_prepare_entry(pos, head, member) \
  2779. ((pos) ? : list_entry(head, typeof(*pos), member))
  2780. /**
  2781. * list_for_each_entry_continue - iterate over list of given type
  2782. * continuing after existing point
  2783. * @pos: the type * to use as a loop counter.
  2784. * @head: the head for your list.
  2785. * @member: the name of the list_struct within the struct.
  2786. */
  2787. #define list_for_each_entry_continue(pos, head, member) \
  2788. for (pos = list_entry(pos->member.next, typeof(*pos), member); \
  2789. prefetch(pos->member.next), &pos->member != (head); \
  2790. pos = list_entry(pos->member.next, typeof(*pos), member))
  2791. /**
  2792. * list_for_each_entry_from - iterate over list of given type
  2793. * continuing from existing point
  2794. * @pos: the type * to use as a loop counter.
  2795. * @head: the head for your list.
  2796. * @member: the name of the list_struct within the struct.
  2797. */
  2798. #define list_for_each_entry_from(pos, head, member) \
  2799. for (; prefetch(pos->member.next), &pos->member != (head); \
  2800. pos = list_entry(pos->member.next, typeof(*pos), member))
  2801. /**
  2802. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  2803. * @pos: the type * to use as a loop counter.
  2804. * @n: another type * to use as temporary storage
  2805. * @head: the head for your list.
  2806. * @member: the name of the list_struct within the struct.
  2807. */
  2808. #define list_for_each_entry_safe(pos, n, head, member) \
  2809. for (pos = list_entry((head)->next, typeof(*pos), member), \
  2810. n = list_entry(pos->member.next, typeof(*pos), member); \
  2811. &pos->member != (head); \
  2812. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2813. /**
  2814. * list_for_each_entry_safe_continue - iterate over list of given type
  2815. * continuing after existing point safe against removal of list entry
  2816. * @pos: the type * to use as a loop counter.
  2817. * @n: another type * to use as temporary storage
  2818. * @head: the head for your list.
  2819. * @member: the name of the list_struct within the struct.
  2820. */
  2821. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  2822. for (pos = list_entry(pos->member.next, typeof(*pos), member), \
  2823. n = list_entry(pos->member.next, typeof(*pos), member); \
  2824. &pos->member != (head); \
  2825. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2826. /**
  2827. * list_for_each_entry_safe_from - iterate over list of given type
  2828. * from existing point safe against removal of list entry
  2829. * @pos: the type * to use as a loop counter.
  2830. * @n: another type * to use as temporary storage
  2831. * @head: the head for your list.
  2832. * @member: the name of the list_struct within the struct.
  2833. */
  2834. #define list_for_each_entry_safe_from(pos, n, head, member) \
  2835. for (n = list_entry(pos->member.next, typeof(*pos), member); \
  2836. &pos->member != (head); \
  2837. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  2838. /**
  2839. * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
  2840. * removal of list entry
  2841. * @pos: the type * to use as a loop counter.
  2842. * @n: another type * to use as temporary storage
  2843. * @head: the head for your list.
  2844. * @member: the name of the list_struct within the struct.
  2845. */
  2846. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  2847. for (pos = list_entry((head)->prev, typeof(*pos), member), \
  2848. n = list_entry(pos->member.prev, typeof(*pos), member); \
  2849. &pos->member != (head); \
  2850. pos = n, n = list_entry(n->member.prev, typeof(*n), member))
  2851. /**
  2852. * list_for_each_rcu - iterate over an rcu-protected list
  2853. * @pos: the &struct list_head to use as a loop counter.
  2854. * @head: the head for your list.
  2855. *
  2856. * This list-traversal primitive may safely run concurrently with
  2857. * the _rcu list-mutation primitives such as list_add_rcu()
  2858. * as long as the traversal is guarded by rcu_read_lock().
  2859. */
  2860. #define list_for_each_rcu(pos, head) \
  2861. for (pos = (head)->next; \
  2862. prefetch(rcu_dereference(pos)->next), pos != (head); \
  2863. pos = pos->next)
  2864. #define __list_for_each_rcu(pos, head) \
  2865. for (pos = (head)->next; \
  2866. rcu_dereference(pos) != (head); \
  2867. pos = pos->next)
  2868. /**
  2869. * list_for_each_safe_rcu - iterate over an rcu-protected list safe
  2870. * against removal of list entry
  2871. * @pos: the &struct list_head to use as a loop counter.
  2872. * @n: another &struct list_head to use as temporary storage
  2873. * @head: the head for your list.
  2874. *
  2875. * This list-traversal primitive may safely run concurrently with
  2876. * the _rcu list-mutation primitives such as list_add_rcu()
  2877. * as long as the traversal is guarded by rcu_read_lock().
  2878. */
  2879. #define list_for_each_safe_rcu(pos, n, head) \
  2880. for (pos = (head)->next; \
  2881. n = rcu_dereference(pos)->next, pos != (head); \
  2882. pos = n)
  2883. /**
  2884. * list_for_each_entry_rcu - iterate over rcu list of given type
  2885. * @pos: the type * to use as a loop counter.
  2886. * @head: the head for your list.
  2887. * @member: the name of the list_struct within the struct.
  2888. *
  2889. * This list-traversal primitive may safely run concurrently with
  2890. * the _rcu list-mutation primitives such as list_add_rcu()
  2891. * as long as the traversal is guarded by rcu_read_lock().
  2892. */
  2893. #define list_for_each_entry_rcu(pos, head, member) \
  2894. for (pos = list_entry((head)->next, typeof(*pos), member); \
  2895. prefetch(rcu_dereference(pos)->member.next), \
  2896. &pos->member != (head); \
  2897. pos = list_entry(pos->member.next, typeof(*pos), member))
  2898. /**
  2899. * list_for_each_continue_rcu - iterate over an rcu-protected list
  2900. * continuing after existing point.
  2901. * @pos: the &struct list_head to use as a loop counter.
  2902. * @head: the head for your list.
  2903. *
  2904. * This list-traversal primitive may safely run concurrently with
  2905. * the _rcu list-mutation primitives such as list_add_rcu()
  2906. * as long as the traversal is guarded by rcu_read_lock().
  2907. */
  2908. #define list_for_each_continue_rcu(pos, head) \
  2909. for ((pos) = (pos)->next; \
  2910. prefetch(rcu_dereference((pos))->next), (pos) != (head); \
  2911. (pos) = (pos)->next)
  2912. /*
  2913. * Double linked lists with a single pointer list head.
  2914. * Mostly useful for hash tables where the two pointer list head is
  2915. * too wasteful.
  2916. * You lose the ability to access the tail in O(1).
  2917. */
  2918. struct hlist_head {
  2919. struct hlist_node *first;
  2920. };
  2921. struct hlist_node {
  2922. struct hlist_node *next, **pprev;
  2923. };
  2924. #define HLIST_HEAD_INIT { .first = NULL }
  2925. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  2926. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  2927. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  2928. {
  2929. h->next = NULL;
  2930. h->pprev = NULL;
  2931. }
  2932. static inline int hlist_unhashed(const struct hlist_node *h)
  2933. {
  2934. return !h->pprev;
  2935. }
  2936. static inline int hlist_empty(const struct hlist_head *h)
  2937. {
  2938. return !h->first;
  2939. }
  2940. static inline void __hlist_del(struct hlist_node *n)
  2941. {
  2942. struct hlist_node *next = n->next;
  2943. struct hlist_node **pprev = n->pprev;
  2944. *pprev = next;
  2945. if (next)
  2946. next->pprev = pprev;
  2947. }
  2948. static inline void hlist_del(struct hlist_node *n)
  2949. {
  2950. __hlist_del(n);
  2951. n->next = LIST_POISON1;
  2952. n->pprev = LIST_POISON2;
  2953. }
  2954. /**
  2955. * hlist_del_rcu - deletes entry from hash list without re-initialization
  2956. * @n: the element to delete from the hash list.
  2957. *
  2958. * Note: list_unhashed() on entry does not return true after this,
  2959. * the entry is in an undefined state. It is useful for RCU based
  2960. * lockfree traversal.
  2961. *
  2962. * In particular, it means that we can not poison the forward
  2963. * pointers that may still be used for walking the hash list.
  2964. *
  2965. * The caller must take whatever precautions are necessary
  2966. * (such as holding appropriate locks) to avoid racing
  2967. * with another list-mutation primitive, such as hlist_add_head_rcu()
  2968. * or hlist_del_rcu(), running on this same list.
  2969. * However, it is perfectly legal to run concurrently with
  2970. * the _rcu list-traversal primitives, such as
  2971. * hlist_for_each_entry().
  2972. */
  2973. static inline void hlist_del_rcu(struct hlist_node *n)
  2974. {
  2975. __hlist_del(n);
  2976. n->pprev = LIST_POISON2;
  2977. }
  2978. static inline void hlist_del_init(struct hlist_node *n)
  2979. {
  2980. if (!hlist_unhashed(n)) {
  2981. __hlist_del(n);
  2982. INIT_HLIST_NODE(n);
  2983. }
  2984. }
  2985. /*
  2986. * hlist_replace_rcu - replace old entry by new one
  2987. * @old : the element to be replaced
  2988. * @new : the new element to insert
  2989. *
  2990. * The old entry will be replaced with the new entry atomically.
  2991. */
  2992. static inline void hlist_replace_rcu(struct hlist_node *old,
  2993. struct hlist_node *new)
  2994. {
  2995. struct hlist_node *next = old->next;
  2996. new->next = next;
  2997. new->pprev = old->pprev;
  2998. // smp_wmb();
  2999. if (next)
  3000. new->next->pprev = &new->next;
  3001. *new->pprev = new;
  3002. old->pprev = LIST_POISON2;
  3003. }
  3004. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  3005. {
  3006. struct hlist_node *first = h->first;
  3007. n->next = first;
  3008. if (first)
  3009. first->pprev = &n->next;
  3010. h->first = n;
  3011. n->pprev = &h->first;
  3012. }
  3013. /**
  3014. * hlist_add_head_rcu - adds the specified element to the specified hlist,
  3015. * while permitting racing traversals.
  3016. * @n: the element to add to the hash list.
  3017. * @h: the list to add to.
  3018. *
  3019. * The caller must take whatever precautions are necessary
  3020. * (such as holding appropriate locks) to avoid racing
  3021. * with another list-mutation primitive, such as hlist_add_head_rcu()
  3022. * or hlist_del_rcu(), running on this same list.
  3023. * However, it is perfectly legal to run concurrently with
  3024. * the _rcu list-traversal primitives, such as
  3025. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  3026. * problems on Alpha CPUs. Regardless of the type of CPU, the
  3027. * list-traversal primitive must be guarded by rcu_read_lock().
  3028. */
  3029. static inline void hlist_add_head_rcu(struct hlist_node *n,
  3030. struct hlist_head *h)
  3031. {
  3032. struct hlist_node *first = h->first;
  3033. n->next = first;
  3034. n->pprev = &h->first;
  3035. // smp_wmb();
  3036. if (first)
  3037. first->pprev = &n->next;
  3038. h->first = n;
  3039. }
  3040. /* next must be != NULL */
  3041. static inline void hlist_add_before(struct hlist_node *n,
  3042. struct hlist_node *next)
  3043. {
  3044. n->pprev = next->pprev;
  3045. n->next = next;
  3046. next->pprev = &n->next;
  3047. *(n->pprev) = n;
  3048. }
  3049. static inline void hlist_add_after(struct hlist_node *n,
  3050. struct hlist_node *next)
  3051. {
  3052. next->next = n->next;
  3053. n->next = next;
  3054. next->pprev = &n->next;
  3055. if(next->next)
  3056. next->next->pprev = &next->next;
  3057. }
  3058. /**
  3059. * hlist_add_before_rcu - adds the specified element to the specified hlist
  3060. * before the specified node while permitting racing traversals.
  3061. * @n: the new element to add to the hash list.
  3062. * @next: the existing element to add the new element before.
  3063. *
  3064. * The caller must take whatever precautions are necessary
  3065. * (such as holding appropriate locks) to avoid racing
  3066. * with another list-mutation primitive, such as hlist_add_head_rcu()
  3067. * or hlist_del_rcu(), running on this same list.
  3068. * However, it is perfectly legal to run concurrently with
  3069. * the _rcu list-traversal primitives, such as
  3070. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  3071. * problems on Alpha CPUs.
  3072. */
  3073. static inline void hlist_add_before_rcu(struct hlist_node *n,
  3074. struct hlist_node *next)
  3075. {
  3076. n->pprev = next->pprev;
  3077. n->next = next;
  3078. // smp_wmb();
  3079. next->pprev = &n->next;
  3080. *(n->pprev) = n;
  3081. }
  3082. /**
  3083. * hlist_add_after_rcu - adds the specified element to the specified hlist
  3084. * after the specified node while permitting racing traversals.
  3085. * @prev: the existing element to add the new element after.
  3086. * @n: the new element to add to the hash list.
  3087. *
  3088. * The caller must take whatever precautions are necessary
  3089. * (such as holding appropriate locks) to avoid racing
  3090. * with another list-mutation primitive, such as hlist_add_head_rcu()
  3091. * or hlist_del_rcu(), running on this same list.
  3092. * However, it is perfectly legal to run concurrently with
  3093. * the _rcu list-traversal primitives, such as
  3094. * hlist_for_each_entry_rcu(), used to prevent memory-consistency
  3095. * problems on Alpha CPUs.
  3096. */
  3097. static inline void hlist_add_after_rcu(struct hlist_node *prev,
  3098. struct hlist_node *n)
  3099. {
  3100. n->next = prev->next;
  3101. n->pprev = &prev->next;
  3102. // smp_wmb();
  3103. prev->next = n;
  3104. if (n->next)
  3105. n->next->pprev = &n->next;
  3106. }
  3107. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  3108. #define hlist_for_each(pos, head) \
  3109. for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  3110. pos = pos->next)
  3111. #define hlist_for_each_safe(pos, n, head) \
  3112. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  3113. pos = n)
  3114. /**
  3115. * hlist_for_each_entry - iterate over list of given type
  3116. * @tpos: the type * to use as a loop counter.
  3117. * @pos: the &struct hlist_node to use as a loop counter.
  3118. * @head: the head for your list.
  3119. * @member: the name of the hlist_node within the struct.
  3120. */
  3121. #define hlist_for_each_entry(tpos, pos, head, member) \
  3122. for (pos = (head)->first; \
  3123. pos && ({ prefetch(pos->next); 1;}) && \
  3124. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  3125. pos = pos->next)
  3126. /**
  3127. * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
  3128. * @tpos: the type * to use as a loop counter.
  3129. * @pos: the &struct hlist_node to use as a loop counter.
  3130. * @member: the name of the hlist_node within the struct.
  3131. */
  3132. #define hlist_for_each_entry_continue(tpos, pos, member) \
  3133. for (pos = (pos)->next; \
  3134. pos && ({ prefetch(pos->next); 1;}) && \
  3135. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  3136. pos = pos->next)
  3137. /**
  3138. * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
  3139. * @tpos: the type * to use as a loop counter.
  3140. * @pos: the &struct hlist_node to use as a loop counter.
  3141. * @member: the name of the hlist_node within the struct.
  3142. */
  3143. #define hlist_for_each_entry_from(tpos, pos, member) \
  3144. for (; pos && ({ prefetch(pos->next); 1;}) && \
  3145. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  3146. pos = pos->next)
  3147. /**
  3148. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  3149. * @tpos: the type * to use as a loop counter.
  3150. * @pos: the &struct hlist_node to use as a loop counter.
  3151. * @n: another &struct hlist_node to use as temporary storage
  3152. * @head: the head for your list.
  3153. * @member: the name of the hlist_node within the struct.
  3154. */
  3155. #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
  3156. for (pos = (head)->first; \
  3157. pos && ({ n = pos->next; 1; }) && \
  3158. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  3159. pos = n)
  3160. /**
  3161. * hlist_for_each_entry_rcu - iterate over rcu list of given type
  3162. * @tpos: the type * to use as a loop counter.
  3163. * @pos: the &struct hlist_node to use as a loop counter.
  3164. * @head: the head for your list.
  3165. * @member: the name of the hlist_node within the struct.
  3166. *
  3167. * This list-traversal primitive may safely run concurrently with
  3168. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  3169. * as long as the traversal is guarded by rcu_read_lock().
  3170. */
  3171. #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
  3172. for (pos = (head)->first; \
  3173. rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
  3174. ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  3175. pos = pos->next)
  3176. #endif