blob: eb4443c7e05be213f49596b81b50f209923ab4da [file] [log] [blame]
Franck Bui-Huu82524742008-05-12 21:21:05 +02001#ifndef _LINUX_RCULIST_H
2#define _LINUX_RCULIST_H
3
4#ifdef __KERNEL__
5
6/*
7 * RCU-protected list version
8 */
9#include <linux/list.h>
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +020010#include <linux/rcupdate.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020011
12/*
13 * Insert a new entry between two known consecutive entries.
14 *
15 * This is only for internal list manipulation where we know
16 * the prev/next entries already!
17 */
18static inline void __list_add_rcu(struct list_head *new,
19 struct list_head *prev, struct list_head *next)
20{
21 new->next = next;
22 new->prev = prev;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +020023 rcu_assign_pointer(prev->next, new);
Franck Bui-Huu82524742008-05-12 21:21:05 +020024 next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +020025}
26
27/**
28 * list_add_rcu - add a new entry to rcu-protected list
29 * @new: new entry to be added
30 * @head: list head to add it after
31 *
32 * Insert a new entry after the specified head.
33 * This is good for implementing stacks.
34 *
35 * The caller must take whatever precautions are necessary
36 * (such as holding appropriate locks) to avoid racing
37 * with another list-mutation primitive, such as list_add_rcu()
38 * or list_del_rcu(), running on this same list.
39 * However, it is perfectly legal to run concurrently with
40 * the _rcu list-traversal primitives, such as
41 * list_for_each_entry_rcu().
42 */
43static inline void list_add_rcu(struct list_head *new, struct list_head *head)
44{
45 __list_add_rcu(new, head, head->next);
46}
47
48/**
49 * list_add_tail_rcu - add a new entry to rcu-protected list
50 * @new: new entry to be added
51 * @head: list head to add it before
52 *
53 * Insert a new entry before the specified head.
54 * This is useful for implementing queues.
55 *
56 * The caller must take whatever precautions are necessary
57 * (such as holding appropriate locks) to avoid racing
58 * with another list-mutation primitive, such as list_add_tail_rcu()
59 * or list_del_rcu(), running on this same list.
60 * However, it is perfectly legal to run concurrently with
61 * the _rcu list-traversal primitives, such as
62 * list_for_each_entry_rcu().
63 */
64static inline void list_add_tail_rcu(struct list_head *new,
65 struct list_head *head)
66{
67 __list_add_rcu(new, head->prev, head);
68}
69
70/**
71 * list_del_rcu - deletes entry from list without re-initialization
72 * @entry: the element to delete from the list.
73 *
74 * Note: list_empty() on entry does not return true after this,
75 * the entry is in an undefined state. It is useful for RCU based
76 * lockfree traversal.
77 *
78 * In particular, it means that we can not poison the forward
79 * pointers that may still be used for walking the list.
80 *
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 list_del_rcu()
84 * or list_add_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 * list_for_each_entry_rcu().
88 *
89 * Note that the caller is not permitted to immediately free
90 * the newly deleted entry. Instead, either synchronize_rcu()
91 * or call_rcu() must be used to defer freeing until an RCU
92 * grace period has elapsed.
93 */
94static inline void list_del_rcu(struct list_head *entry)
95{
96 __list_del(entry->prev, entry->next);
97 entry->prev = LIST_POISON2;
98}
99
100/**
Andrea Arcangeli6beeac72008-07-28 15:46:22 -0700101 * hlist_del_init_rcu - deletes entry from hash list with re-initialization
102 * @n: the element to delete from the hash list.
103 *
104 * Note: list_unhashed() on the node return true after this. It is
105 * useful for RCU based read lockfree traversal if the writer side
106 * must know if the list entry is still hashed or already unhashed.
107 *
108 * In particular, it means that we can not poison the forward pointers
109 * that may still be used for walking the hash list and we can only
110 * zero the pprev pointer so list_unhashed() will return true after
111 * this.
112 *
113 * The caller must take whatever precautions are necessary (such as
114 * holding appropriate locks) to avoid racing with another
115 * list-mutation primitive, such as hlist_add_head_rcu() or
116 * hlist_del_rcu(), running on this same list. However, it is
117 * perfectly legal to run concurrently with the _rcu list-traversal
118 * primitives, such as hlist_for_each_entry_rcu().
119 */
120static inline void hlist_del_init_rcu(struct hlist_node *n)
121{
122 if (!hlist_unhashed(n)) {
123 __hlist_del(n);
124 n->pprev = NULL;
125 }
126}
127
128/**
Franck Bui-Huu82524742008-05-12 21:21:05 +0200129 * list_replace_rcu - replace old entry by new one
130 * @old : the element to be replaced
131 * @new : the new element to insert
132 *
133 * The @old entry will be replaced with the @new entry atomically.
134 * Note: @old should not be empty.
135 */
136static inline void list_replace_rcu(struct list_head *old,
137 struct list_head *new)
138{
139 new->next = old->next;
140 new->prev = old->prev;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200141 rcu_assign_pointer(new->prev->next, new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200142 new->next->prev = new;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200143 old->prev = LIST_POISON2;
144}
145
146/**
147 * list_splice_init_rcu - splice an RCU-protected list into an existing list.
148 * @list: the RCU-protected list to splice
149 * @head: the place in the list to splice the first list into
150 * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ...
151 *
152 * @head can be RCU-read traversed concurrently with this function.
153 *
154 * Note that this function blocks.
155 *
156 * Important note: the caller must take whatever action is necessary to
157 * prevent any other updates to @head. In principle, it is possible
158 * to modify the list as soon as sync() begins execution.
159 * If this sort of thing becomes necessary, an alternative version
160 * based on call_rcu() could be created. But only if -really-
161 * needed -- there is no shortage of RCU API members.
162 */
163static inline void list_splice_init_rcu(struct list_head *list,
164 struct list_head *head,
165 void (*sync)(void))
166{
167 struct list_head *first = list->next;
168 struct list_head *last = list->prev;
169 struct list_head *at = head->next;
170
171 if (list_empty(head))
172 return;
173
174 /* "first" and "last" tracking list, so initialize it. */
175
176 INIT_LIST_HEAD(list);
177
178 /*
179 * At this point, the list body still points to the source list.
180 * Wait for any readers to finish using the list before splicing
181 * the list body into the new list. Any new readers will see
182 * an empty list.
183 */
184
185 sync();
186
187 /*
188 * Readers are finished with the source list, so perform splice.
189 * The order is important if the new list is global and accessible
190 * to concurrent RCU readers. Note that RCU readers are not
191 * permitted to traverse the prev pointers without excluding
192 * this function.
193 */
194
195 last->next = at;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200196 rcu_assign_pointer(head->next, first);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200197 first->prev = head;
198 at->prev = last;
199}
200
201/**
202 * list_for_each_rcu - iterate over an rcu-protected list
203 * @pos: the &struct list_head to use as a loop cursor.
204 * @head: the head for your list.
205 *
206 * This list-traversal primitive may safely run concurrently with
207 * the _rcu list-mutation primitives such as list_add_rcu()
208 * as long as the traversal is guarded by rcu_read_lock().
209 */
210#define list_for_each_rcu(pos, head) \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200211 for (pos = rcu_dereference((head)->next); \
212 prefetch(pos->next), pos != (head); \
213 pos = rcu_dereference(pos->next))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200214
215#define __list_for_each_rcu(pos, head) \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200216 for (pos = rcu_dereference((head)->next); \
217 pos != (head); \
218 pos = rcu_dereference(pos->next))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200219
220/**
221 * list_for_each_entry_rcu - iterate over rcu list of given type
222 * @pos: the type * to use as a loop cursor.
223 * @head: the head for your list.
224 * @member: the name of the list_struct within the struct.
225 *
226 * This list-traversal primitive may safely run concurrently with
227 * the _rcu list-mutation primitives such as list_add_rcu()
228 * as long as the traversal is guarded by rcu_read_lock().
229 */
230#define list_for_each_entry_rcu(pos, head, member) \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200231 for (pos = list_entry(rcu_dereference((head)->next), typeof(*pos), member); \
232 prefetch(pos->member.next), &pos->member != (head); \
233 pos = list_entry(rcu_dereference(pos->member.next), typeof(*pos), member))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200234
235
236/**
237 * list_for_each_continue_rcu
238 * @pos: the &struct list_head to use as a loop cursor.
239 * @head: the head for your list.
240 *
241 * Iterate over an rcu-protected list, continuing after current point.
242 *
243 * This list-traversal primitive may safely run concurrently with
244 * the _rcu list-mutation primitives such as list_add_rcu()
245 * as long as the traversal is guarded by rcu_read_lock().
246 */
247#define list_for_each_continue_rcu(pos, head) \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200248 for ((pos) = rcu_dereference((pos)->next); \
249 prefetch((pos)->next), (pos) != (head); \
250 (pos) = rcu_dereference((pos)->next))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200251
252/**
253 * hlist_del_rcu - deletes entry from hash list without re-initialization
254 * @n: the element to delete from the hash list.
255 *
256 * Note: list_unhashed() on entry does not return true after this,
257 * the entry is in an undefined state. It is useful for RCU based
258 * lockfree traversal.
259 *
260 * In particular, it means that we can not poison the forward
261 * pointers that may still be used for walking the hash list.
262 *
263 * The caller must take whatever precautions are necessary
264 * (such as holding appropriate locks) to avoid racing
265 * with another list-mutation primitive, such as hlist_add_head_rcu()
266 * or hlist_del_rcu(), running on this same list.
267 * However, it is perfectly legal to run concurrently with
268 * the _rcu list-traversal primitives, such as
269 * hlist_for_each_entry().
270 */
271static inline void hlist_del_rcu(struct hlist_node *n)
272{
273 __hlist_del(n);
274 n->pprev = LIST_POISON2;
275}
276
277/**
278 * hlist_replace_rcu - replace old entry by new one
279 * @old : the element to be replaced
280 * @new : the new element to insert
281 *
282 * The @old entry will be replaced with the @new entry atomically.
283 */
284static inline void hlist_replace_rcu(struct hlist_node *old,
285 struct hlist_node *new)
286{
287 struct hlist_node *next = old->next;
288
289 new->next = next;
290 new->pprev = old->pprev;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200291 rcu_assign_pointer(*new->pprev, new);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200292 if (next)
293 new->next->pprev = &new->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200294 old->pprev = LIST_POISON2;
295}
296
297/**
298 * hlist_add_head_rcu
299 * @n: the element to add to the hash list.
300 * @h: the list to add to.
301 *
302 * Description:
303 * Adds the specified element to the specified hlist,
304 * while permitting racing traversals.
305 *
306 * The caller must take whatever precautions are necessary
307 * (such as holding appropriate locks) to avoid racing
308 * with another list-mutation primitive, such as hlist_add_head_rcu()
309 * or hlist_del_rcu(), running on this same list.
310 * However, it is perfectly legal to run concurrently with
311 * the _rcu list-traversal primitives, such as
312 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
313 * problems on Alpha CPUs. Regardless of the type of CPU, the
314 * list-traversal primitive must be guarded by rcu_read_lock().
315 */
316static inline void hlist_add_head_rcu(struct hlist_node *n,
317 struct hlist_head *h)
318{
319 struct hlist_node *first = h->first;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200320
Franck Bui-Huu82524742008-05-12 21:21:05 +0200321 n->next = first;
322 n->pprev = &h->first;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200323 rcu_assign_pointer(h->first, n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200324 if (first)
325 first->pprev = &n->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200326}
327
328/**
329 * hlist_add_before_rcu
330 * @n: the new element to add to the hash list.
331 * @next: the existing element to add the new element before.
332 *
333 * Description:
334 * Adds the specified element to the specified hlist
335 * before the specified node while permitting racing traversals.
336 *
337 * The caller must take whatever precautions are necessary
338 * (such as holding appropriate locks) to avoid racing
339 * with another list-mutation primitive, such as hlist_add_head_rcu()
340 * or hlist_del_rcu(), running on this same list.
341 * However, it is perfectly legal to run concurrently with
342 * the _rcu list-traversal primitives, such as
343 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
344 * problems on Alpha CPUs.
345 */
346static inline void hlist_add_before_rcu(struct hlist_node *n,
347 struct hlist_node *next)
348{
349 n->pprev = next->pprev;
350 n->next = next;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200351 rcu_assign_pointer(*(n->pprev), n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200352 next->pprev = &n->next;
Franck Bui-Huu82524742008-05-12 21:21:05 +0200353}
354
355/**
356 * hlist_add_after_rcu
357 * @prev: the existing element to add the new element after.
358 * @n: the new element to add to the hash list.
359 *
360 * Description:
361 * Adds the specified element to the specified hlist
362 * after the specified node while permitting racing traversals.
363 *
364 * The caller must take whatever precautions are necessary
365 * (such as holding appropriate locks) to avoid racing
366 * with another list-mutation primitive, such as hlist_add_head_rcu()
367 * or hlist_del_rcu(), running on this same list.
368 * However, it is perfectly legal to run concurrently with
369 * the _rcu list-traversal primitives, such as
370 * hlist_for_each_entry_rcu(), used to prevent memory-consistency
371 * problems on Alpha CPUs.
372 */
373static inline void hlist_add_after_rcu(struct hlist_node *prev,
374 struct hlist_node *n)
375{
376 n->next = prev->next;
377 n->pprev = &prev->next;
Franck Bui-Huu10aa9d22008-05-12 21:21:06 +0200378 rcu_assign_pointer(prev->next, n);
Franck Bui-Huu82524742008-05-12 21:21:05 +0200379 if (n->next)
380 n->next->pprev = &n->next;
381}
382
383/**
384 * hlist_for_each_entry_rcu - iterate over rcu list of given type
385 * @tpos: the type * to use as a loop cursor.
386 * @pos: the &struct hlist_node to use as a loop cursor.
387 * @head: the head for your list.
388 * @member: the name of the hlist_node within the struct.
389 *
390 * This list-traversal primitive may safely run concurrently with
391 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
392 * as long as the traversal is guarded by rcu_read_lock().
393 */
394#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200395 for (pos = rcu_dereference((head)->first); \
396 pos && ({ prefetch(pos->next); 1; }) && \
Franck Bui-Huu82524742008-05-12 21:21:05 +0200397 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
Paul E. McKenney78b0e0e2008-05-12 21:21:06 +0200398 pos = rcu_dereference(pos->next))
Franck Bui-Huu82524742008-05-12 21:21:05 +0200399
400#endif /* __KERNEL__ */
401#endif