1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RCULIST_NULLS_H
3 #define _LINUX_RCULIST_NULLS_H
8 * RCU-protected list version
10 #include <linux/list_nulls.h>
11 #include <linux/rcupdate.h>
14 * hlist_nulls_del_init_rcu - deletes entry from hash list with re-initialization
15 * @n: the element to delete from the hash list.
17 * Note: hlist_nulls_unhashed() on the node return true after this. It is
18 * useful for RCU based read lockfree traversal if the writer side
19 * must know if the list entry is still hashed or already unhashed.
21 * In particular, it means that we can not poison the forward pointers
22 * that may still be used for walking the hash list and we can only
23 * zero the pprev pointer so list_unhashed() will return true after
26 * The caller must take whatever precautions are necessary (such as
27 * holding appropriate locks) to avoid racing with another
28 * list-mutation primitive, such as hlist_nulls_add_head_rcu() or
29 * hlist_nulls_del_rcu(), running on this same list. However, it is
30 * perfectly legal to run concurrently with the _rcu list-traversal
31 * primitives, such as hlist_nulls_for_each_entry_rcu().
33 static inline void hlist_nulls_del_init_rcu(struct hlist_nulls_node *n)
35 if (!hlist_nulls_unhashed(n)) {
37 WRITE_ONCE(n->pprev, NULL);
41 #define hlist_nulls_first_rcu(head) \
42 (*((struct hlist_nulls_node __rcu __force **)&(head)->first))
44 #define hlist_nulls_next_rcu(node) \
45 (*((struct hlist_nulls_node __rcu __force **)&(node)->next))
48 * hlist_nulls_del_rcu - deletes entry from hash list without re-initialization
49 * @n: the element to delete from the hash list.
51 * Note: hlist_nulls_unhashed() on entry does not return true after this,
52 * the entry is in an undefined state. It is useful for RCU based
55 * In particular, it means that we can not poison the forward
56 * pointers that may still be used for walking the hash list.
58 * The caller must take whatever precautions are necessary
59 * (such as holding appropriate locks) to avoid racing
60 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
61 * or hlist_nulls_del_rcu(), running on this same list.
62 * However, it is perfectly legal to run concurrently with
63 * the _rcu list-traversal primitives, such as
64 * hlist_nulls_for_each_entry().
66 static inline void hlist_nulls_del_rcu(struct hlist_nulls_node *n)
69 WRITE_ONCE(n->pprev, LIST_POISON2);
73 * hlist_nulls_add_head_rcu
74 * @n: the element to add to the hash list.
75 * @h: the list to add to.
78 * Adds the specified element to the specified hlist_nulls,
79 * while permitting racing traversals.
81 * The caller must take whatever precautions are necessary
82 * (such as holding appropriate locks) to avoid racing
83 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
84 * or hlist_nulls_del_rcu(), running on this same list.
85 * However, it is perfectly legal to run concurrently with
86 * the _rcu list-traversal primitives, such as
87 * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
88 * problems on Alpha CPUs. Regardless of the type of CPU, the
89 * list-traversal primitive must be guarded by rcu_read_lock().
91 static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
92 struct hlist_nulls_head *h)
94 struct hlist_nulls_node *first = h->first;
97 WRITE_ONCE(n->pprev, &h->first);
98 rcu_assign_pointer(hlist_nulls_first_rcu(h), n);
99 if (!is_a_nulls(first))
100 WRITE_ONCE(first->pprev, &n->next);
104 * hlist_nulls_add_tail_rcu
105 * @n: the element to add to the hash list.
106 * @h: the list to add to.
109 * Adds the specified element to the specified hlist_nulls,
110 * while permitting racing traversals.
112 * The caller must take whatever precautions are necessary
113 * (such as holding appropriate locks) to avoid racing
114 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
115 * or hlist_nulls_del_rcu(), running on this same list.
116 * However, it is perfectly legal to run concurrently with
117 * the _rcu list-traversal primitives, such as
118 * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
119 * problems on Alpha CPUs. Regardless of the type of CPU, the
120 * list-traversal primitive must be guarded by rcu_read_lock().
122 static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
123 struct hlist_nulls_head *h)
125 struct hlist_nulls_node *i, *last = NULL;
127 /* Note: write side code, so rcu accessors are not needed. */
128 for (i = h->first; !is_a_nulls(i); i = i->next)
132 n->next = last->next;
133 n->pprev = &last->next;
134 rcu_assign_pointer(hlist_next_rcu(last), n);
136 hlist_nulls_add_head_rcu(n, h);
141 * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
142 * @tpos: the type * to use as a loop cursor.
143 * @pos: the &struct hlist_nulls_node to use as a loop cursor.
144 * @head: the head for your list.
145 * @member: the name of the hlist_nulls_node within the struct.
147 * The barrier() is needed to make sure compiler doesn't cache first element [1],
148 * as this loop can be restarted [2]
149 * [1] Documentation/core-api/atomic_ops.rst around line 114
150 * [2] Documentation/RCU/rculist_nulls.txt around line 146
152 #define hlist_nulls_for_each_entry_rcu(tpos, pos, head, member) \
153 for (({barrier();}), \
154 pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
155 (!is_a_nulls(pos)) && \
156 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1; }); \
157 pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)))
160 * hlist_nulls_for_each_entry_safe -
161 * iterate over list of given type safe against removal of list entry
162 * @tpos: the type * to use as a loop cursor.
163 * @pos: the &struct hlist_nulls_node to use as a loop cursor.
164 * @head: the head for your list.
165 * @member: the name of the hlist_nulls_node within the struct.
167 #define hlist_nulls_for_each_entry_safe(tpos, pos, head, member) \
168 for (({barrier();}), \
169 pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
170 (!is_a_nulls(pos)) && \
171 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); \
172 pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)); 1; });)