list_nulls.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_LIST_NULLS_H
  3. #define _LINUX_LIST_NULLS_H
  4. #include <linux/poison.h>
  5. #include <linux/const.h>
  6. /*
  7. * Special version of lists, where end of list is not a NULL pointer,
  8. * but a 'nulls' marker, which can have many different values.
  9. * (up to 2^31 different values guaranteed on all platforms)
  10. *
  11. * In the standard hlist, termination of a list is the NULL pointer.
  12. * In this special 'nulls' variant, we use the fact that objects stored in
  13. * a list are aligned on a word (4 or 8 bytes alignment).
  14. * We therefore use the last significant bit of 'ptr' :
  15. * Set to 1 : This is a 'nulls' end-of-list marker (ptr >> 1)
  16. * Set to 0 : This is a pointer to some object (ptr)
  17. */
  18. struct hlist_nulls_head {
  19. struct hlist_nulls_node *first;
  20. };
  21. struct hlist_nulls_node {
  22. struct hlist_nulls_node *next, **pprev;
  23. };
  24. #define NULLS_MARKER(value) (1UL | (((long)value) << 1))
  25. #define INIT_HLIST_NULLS_HEAD(ptr, nulls) \
  26. ((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))
  27. #define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
  28. #define hlist_nulls_entry_safe(ptr, type, member) \
  29. ({ typeof(ptr) ____ptr = (ptr); \
  30. !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \
  31. })
  32. /**
  33. * ptr_is_a_nulls - Test if a ptr is a nulls
  34. * @ptr: ptr to be tested
  35. *
  36. */
  37. static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
  38. {
  39. return ((unsigned long)ptr & 1);
  40. }
  41. /**
  42. * get_nulls_value - Get the 'nulls' value of the end of chain
  43. * @ptr: end of chain
  44. *
  45. * Should be called only if is_a_nulls(ptr);
  46. */
  47. static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
  48. {
  49. return ((unsigned long)ptr) >> 1;
  50. }
  51. /**
  52. * hlist_nulls_unhashed - Has node been removed and reinitialized?
  53. * @h: Node to be checked
  54. *
  55. * Not that not all removal functions will leave a node in unhashed state.
  56. * For example, hlist_del_init_rcu() leaves the node in unhashed state,
  57. * but hlist_nulls_del() does not.
  58. */
  59. static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
  60. {
  61. return !h->pprev;
  62. }
  63. /**
  64. * hlist_nulls_unhashed_lockless - Has node been removed and reinitialized?
  65. * @h: Node to be checked
  66. *
  67. * Not that not all removal functions will leave a node in unhashed state.
  68. * For example, hlist_del_init_rcu() leaves the node in unhashed state,
  69. * but hlist_nulls_del() does not. Unlike hlist_nulls_unhashed(), this
  70. * function may be used locklessly.
  71. */
  72. static inline int hlist_nulls_unhashed_lockless(const struct hlist_nulls_node *h)
  73. {
  74. return !READ_ONCE(h->pprev);
  75. }
  76. static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
  77. {
  78. return is_a_nulls(READ_ONCE(h->first));
  79. }
  80. static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
  81. struct hlist_nulls_head *h)
  82. {
  83. struct hlist_nulls_node *first = h->first;
  84. n->next = first;
  85. WRITE_ONCE(n->pprev, &h->first);
  86. h->first = n;
  87. if (!is_a_nulls(first))
  88. WRITE_ONCE(first->pprev, &n->next);
  89. }
  90. static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
  91. {
  92. struct hlist_nulls_node *next = n->next;
  93. struct hlist_nulls_node **pprev = n->pprev;
  94. WRITE_ONCE(*pprev, next);
  95. if (!is_a_nulls(next))
  96. WRITE_ONCE(next->pprev, pprev);
  97. }
  98. static inline void hlist_nulls_del(struct hlist_nulls_node *n)
  99. {
  100. __hlist_nulls_del(n);
  101. WRITE_ONCE(n->pprev, LIST_POISON2);
  102. }
  103. /**
  104. * hlist_nulls_for_each_entry - iterate over list of given type
  105. * @tpos: the type * to use as a loop cursor.
  106. * @pos: the &struct hlist_node to use as a loop cursor.
  107. * @head: the head for your list.
  108. * @member: the name of the hlist_node within the struct.
  109. *
  110. */
  111. #define hlist_nulls_for_each_entry(tpos, pos, head, member) \
  112. for (pos = (head)->first; \
  113. (!is_a_nulls(pos)) && \
  114. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  115. pos = pos->next)
  116. /**
  117. * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
  118. * @tpos: the type * to use as a loop cursor.
  119. * @pos: the &struct hlist_node to use as a loop cursor.
  120. * @member: the name of the hlist_node within the struct.
  121. *
  122. */
  123. #define hlist_nulls_for_each_entry_from(tpos, pos, member) \
  124. for (; (!is_a_nulls(pos)) && \
  125. ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
  126. pos = pos->next)
  127. #endif