blob: 4d71e3687d1ed7068e3b77617af610e0932bbd34 [file] [log] [blame]
Eric Dumazetbbaffac2008-11-16 19:37:55 -08001#ifndef _LINUX_RCULIST_NULLS_H
2#define _LINUX_RCULIST_NULLS_H
3
4#ifdef __KERNEL__
5
6/*
7 * RCU-protected list version
8 */
9#include <linux/list_nulls.h>
10#include <linux/rcupdate.h>
11
12/**
13 * hlist_nulls_del_init_rcu - deletes entry from hash list with re-initialization
14 * @n: the element to delete from the hash list.
15 *
16 * Note: hlist_nulls_unhashed() on the node return true after this. It is
17 * useful for RCU based read lockfree traversal if the writer side
18 * must know if the list entry is still hashed or already unhashed.
19 *
20 * In particular, it means that we can not poison the forward pointers
21 * that may still be used for walking the hash list and we can only
22 * zero the pprev pointer so list_unhashed() will return true after
23 * this.
24 *
25 * The caller must take whatever precautions are necessary (such as
26 * holding appropriate locks) to avoid racing with another
27 * list-mutation primitive, such as hlist_nulls_add_head_rcu() or
28 * hlist_nulls_del_rcu(), running on this same list. However, it is
29 * perfectly legal to run concurrently with the _rcu list-traversal
30 * primitives, such as hlist_nulls_for_each_entry_rcu().
31 */
32static inline void hlist_nulls_del_init_rcu(struct hlist_nulls_node *n)
33{
34 if (!hlist_nulls_unhashed(n)) {
35 __hlist_nulls_del(n);
Paul E. McKenneycd9ea1f2019-11-09 09:42:13 -080036 WRITE_ONCE(n->pprev, NULL);
Eric Dumazetbbaffac2008-11-16 19:37:55 -080037 }
38}
39
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010040#define hlist_nulls_first_rcu(head) \
41 (*((struct hlist_nulls_node __rcu __force **)&(head)->first))
42
43#define hlist_nulls_next_rcu(node) \
44 (*((struct hlist_nulls_node __rcu __force **)&(node)->next))
45
Eric Dumazetbbaffac2008-11-16 19:37:55 -080046/**
47 * hlist_nulls_del_rcu - deletes entry from hash list without re-initialization
48 * @n: the element to delete from the hash list.
49 *
50 * Note: hlist_nulls_unhashed() on entry does not return true after this,
51 * the entry is in an undefined state. It is useful for RCU based
52 * lockfree traversal.
53 *
54 * In particular, it means that we can not poison the forward
55 * pointers that may still be used for walking the hash list.
56 *
57 * The caller must take whatever precautions are necessary
58 * (such as holding appropriate locks) to avoid racing
59 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
60 * or hlist_nulls_del_rcu(), running on this same list.
61 * However, it is perfectly legal to run concurrently with
62 * the _rcu list-traversal primitives, such as
63 * hlist_nulls_for_each_entry().
64 */
65static inline void hlist_nulls_del_rcu(struct hlist_nulls_node *n)
66{
67 __hlist_nulls_del(n);
Paul E. McKenneycd9ea1f2019-11-09 09:42:13 -080068 WRITE_ONCE(n->pprev, LIST_POISON2);
Eric Dumazetbbaffac2008-11-16 19:37:55 -080069}
70
71/**
72 * hlist_nulls_add_head_rcu
73 * @n: the element to add to the hash list.
74 * @h: the list to add to.
75 *
76 * Description:
77 * Adds the specified element to the specified hlist_nulls,
78 * while permitting racing traversals.
79 *
80 * The caller must take whatever precautions are necessary
81 * (such as holding appropriate locks) to avoid racing
82 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
83 * or hlist_nulls_del_rcu(), running on this same list.
84 * However, it is perfectly legal to run concurrently with
85 * the _rcu list-traversal primitives, such as
86 * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
87 * problems on Alpha CPUs. Regardless of the type of CPU, the
88 * list-traversal primitive must be guarded by rcu_read_lock().
89 */
90static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
91 struct hlist_nulls_head *h)
92{
93 struct hlist_nulls_node *first = h->first;
94
95 n->next = first;
Paul E. McKenneycd9ea1f2019-11-09 09:42:13 -080096 WRITE_ONCE(n->pprev, &h->first);
Arnd Bergmann67bdbff2010-02-25 16:55:13 +010097 rcu_assign_pointer(hlist_nulls_first_rcu(h), n);
Eric Dumazetbbaffac2008-11-16 19:37:55 -080098 if (!is_a_nulls(first))
Paul E. McKenneycd9ea1f2019-11-09 09:42:13 -080099 WRITE_ONCE(first->pprev, &n->next);
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800100}
Craig Gallekd894ba12016-04-12 13:11:25 -0400101
102/**
Eric Dumazet792365b2019-12-13 18:20:41 -0800103 * hlist_nulls_add_tail_rcu
104 * @n: the element to add to the hash list.
105 * @h: the list to add to.
106 *
107 * Description:
108 * Adds the specified element to the specified hlist_nulls,
109 * while permitting racing traversals.
110 *
111 * The caller must take whatever precautions are necessary
112 * (such as holding appropriate locks) to avoid racing
113 * with another list-mutation primitive, such as hlist_nulls_add_head_rcu()
114 * or hlist_nulls_del_rcu(), running on this same list.
115 * However, it is perfectly legal to run concurrently with
116 * the _rcu list-traversal primitives, such as
117 * hlist_nulls_for_each_entry_rcu(), used to prevent memory-consistency
118 * problems on Alpha CPUs. Regardless of the type of CPU, the
119 * list-traversal primitive must be guarded by rcu_read_lock().
120 */
121static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n,
122 struct hlist_nulls_head *h)
123{
124 struct hlist_nulls_node *i, *last = NULL;
125
126 /* Note: write side code, so rcu accessors are not needed. */
127 for (i = h->first; !is_a_nulls(i); i = i->next)
128 last = i;
129
130 if (last) {
131 n->next = last->next;
132 n->pprev = &last->next;
133 rcu_assign_pointer(hlist_next_rcu(last), n);
134 } else {
135 hlist_nulls_add_head_rcu(n, h);
136 }
137}
138
139/**
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800140 * hlist_nulls_for_each_entry_rcu - iterate over rcu list of given type
141 * @tpos: the type * to use as a loop cursor.
142 * @pos: the &struct hlist_nulls_node to use as a loop cursor.
143 * @head: the head for your list.
144 * @member: the name of the hlist_nulls_node within the struct.
145 *
Eric Dumazetc87a1242013-05-29 09:06:27 +0000146 * The barrier() is needed to make sure compiler doesn't cache first element [1],
147 * as this loop can be restarted [2]
148 * [1] Documentation/atomic_ops.txt around line 114
149 * [2] Documentation/RCU/rculist_nulls.txt around line 146
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800150 */
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100151#define hlist_nulls_for_each_entry_rcu(tpos, pos, head, member) \
Eric Dumazetc87a1242013-05-29 09:06:27 +0000152 for (({barrier();}), \
153 pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100154 (!is_a_nulls(pos)) && \
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800155 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1; }); \
Arnd Bergmann67bdbff2010-02-25 16:55:13 +0100156 pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)))
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800157
Alexei Starovoitov82303dd2019-05-09 19:33:54 -0700158/**
159 * hlist_nulls_for_each_entry_safe -
160 * iterate over list of given type safe against removal of list entry
161 * @tpos: the type * to use as a loop cursor.
162 * @pos: the &struct hlist_nulls_node to use as a loop cursor.
163 * @head: the head for your list.
164 * @member: the name of the hlist_nulls_node within the struct.
165 */
166#define hlist_nulls_for_each_entry_safe(tpos, pos, head, member) \
167 for (({barrier();}), \
168 pos = rcu_dereference_raw(hlist_nulls_first_rcu(head)); \
169 (!is_a_nulls(pos)) && \
170 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); \
171 pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)); 1; });)
Eric Dumazetbbaffac2008-11-16 19:37:55 -0800172#endif
173#endif