Blame view

include/linux/rculist.h 15.9 KB
82524746c   Franck Bui-Huu   rcu: split list.h...
1
2
3
4
5
6
7
8
9
  #ifndef _LINUX_RCULIST_H
  #define _LINUX_RCULIST_H
  
  #ifdef __KERNEL__
  
  /*
   * RCU-protected list version
   */
  #include <linux/list.h>
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
10
  #include <linux/rcupdate.h>
82524746c   Franck Bui-Huu   rcu: split list.h...
11
12
13
14
15
16
17
18
19
20
21
22
  
  /*
   * Insert a new entry between two known consecutive entries.
   *
   * This is only for internal list manipulation where we know
   * the prev/next entries already!
   */
  static inline void __list_add_rcu(struct list_head *new,
  		struct list_head *prev, struct list_head *next)
  {
  	new->next = next;
  	new->prev = prev;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
23
  	rcu_assign_pointer(prev->next, new);
82524746c   Franck Bui-Huu   rcu: split list.h...
24
  	next->prev = new;
82524746c   Franck Bui-Huu   rcu: split list.h...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  }
  
  /**
   * list_add_rcu - add a new entry to rcu-protected list
   * @new: new entry to be added
   * @head: list head to add it after
   *
   * Insert a new entry after the specified head.
   * This is good for implementing stacks.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as list_add_rcu()
   * or list_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * list_for_each_entry_rcu().
   */
  static inline void list_add_rcu(struct list_head *new, struct list_head *head)
  {
  	__list_add_rcu(new, head, head->next);
  }
  
  /**
   * list_add_tail_rcu - add a new entry to rcu-protected list
   * @new: new entry to be added
   * @head: list head to add it before
   *
   * Insert a new entry before the specified head.
   * This is useful for implementing queues.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as list_add_tail_rcu()
   * or list_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * list_for_each_entry_rcu().
   */
  static inline void list_add_tail_rcu(struct list_head *new,
  					struct list_head *head)
  {
  	__list_add_rcu(new, head->prev, head);
  }
  
  /**
   * list_del_rcu - deletes entry from list without re-initialization
   * @entry: the element to delete from the list.
   *
   * Note: list_empty() on entry does not return true after this,
   * the entry is in an undefined state. It is useful for RCU based
   * lockfree traversal.
   *
   * In particular, it means that we can not poison the forward
   * pointers that may still be used for walking the list.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as list_del_rcu()
   * or list_add_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * list_for_each_entry_rcu().
   *
   * Note that the caller is not permitted to immediately free
   * the newly deleted entry.  Instead, either synchronize_rcu()
   * or call_rcu() must be used to defer freeing until an RCU
   * grace period has elapsed.
   */
  static inline void list_del_rcu(struct list_head *entry)
  {
  	__list_del(entry->prev, entry->next);
  	entry->prev = LIST_POISON2;
  }
  
  /**
6beeac76f   Andrea Arcangeli   mmu-notifiers: ad...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
   * hlist_del_init_rcu - deletes entry from hash list with re-initialization
   * @n: the element to delete from the hash list.
   *
   * Note: list_unhashed() on the node return true after this. It is
   * useful for RCU based read lockfree traversal if the writer side
   * must know if the list entry is still hashed or already unhashed.
   *
   * In particular, it means that we can not poison the forward pointers
   * that may still be used for walking the hash list and we can only
   * zero the pprev pointer so list_unhashed() will return true after
   * this.
   *
   * The caller must take whatever precautions are necessary (such as
   * holding appropriate locks) to avoid racing with another
   * list-mutation primitive, such as hlist_add_head_rcu() or
   * hlist_del_rcu(), running on this same list.  However, it is
   * perfectly legal to run concurrently with the _rcu list-traversal
   * primitives, such as hlist_for_each_entry_rcu().
   */
  static inline void hlist_del_init_rcu(struct hlist_node *n)
  {
  	if (!hlist_unhashed(n)) {
  		__hlist_del(n);
  		n->pprev = NULL;
  	}
  }
  
  /**
82524746c   Franck Bui-Huu   rcu: split list.h...
129
130
131
132
133
134
135
136
137
138
139
140
   * list_replace_rcu - replace old entry by new one
   * @old : the element to be replaced
   * @new : the new element to insert
   *
   * The @old entry will be replaced with the @new entry atomically.
   * Note: @old should not be empty.
   */
  static inline void list_replace_rcu(struct list_head *old,
  				struct list_head *new)
  {
  	new->next = old->next;
  	new->prev = old->prev;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
141
  	rcu_assign_pointer(new->prev->next, new);
82524746c   Franck Bui-Huu   rcu: split list.h...
142
  	new->next->prev = new;
82524746c   Franck Bui-Huu   rcu: split list.h...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  	old->prev = LIST_POISON2;
  }
  
  /**
   * list_splice_init_rcu - splice an RCU-protected list into an existing list.
   * @list:	the RCU-protected list to splice
   * @head:	the place in the list to splice the first list into
   * @sync:	function to sync: synchronize_rcu(), synchronize_sched(), ...
   *
   * @head can be RCU-read traversed concurrently with this function.
   *
   * Note that this function blocks.
   *
   * Important note: the caller must take whatever action is necessary to
   *	prevent any other updates to @head.  In principle, it is possible
   *	to modify the list as soon as sync() begins execution.
   *	If this sort of thing becomes necessary, an alternative version
   *	based on call_rcu() could be created.  But only if -really-
   *	needed -- there is no shortage of RCU API members.
   */
  static inline void list_splice_init_rcu(struct list_head *list,
  					struct list_head *head,
  					void (*sync)(void))
  {
  	struct list_head *first = list->next;
  	struct list_head *last = list->prev;
  	struct list_head *at = head->next;
  
  	if (list_empty(head))
  		return;
  
  	/* "first" and "last" tracking list, so initialize it. */
  
  	INIT_LIST_HEAD(list);
  
  	/*
  	 * At this point, the list body still points to the source list.
  	 * Wait for any readers to finish using the list before splicing
  	 * the list body into the new list.  Any new readers will see
  	 * an empty list.
  	 */
  
  	sync();
  
  	/*
  	 * Readers are finished with the source list, so perform splice.
  	 * The order is important if the new list is global and accessible
  	 * to concurrent RCU readers.  Note that RCU readers are not
  	 * permitted to traverse the prev pointers without excluding
  	 * this function.
  	 */
  
  	last->next = at;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
196
  	rcu_assign_pointer(head->next, first);
82524746c   Franck Bui-Huu   rcu: split list.h...
197
198
199
  	first->prev = head;
  	at->prev = last;
  }
72c6a9870   Jiri Pirko   rculist.h: introd...
200
201
202
203
204
205
206
207
208
209
  /**
   * list_entry_rcu - get the struct for this entry
   * @ptr:        the &struct list_head pointer.
   * @type:       the type of the struct this is embedded in.
   * @member:     the name of the list_struct within the struct.
   *
   * This primitive may safely run concurrently with the _rcu list-mutation
   * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
   */
  #define list_entry_rcu(ptr, type, member) \
3120438ad   Paul E. McKenney   rcu: Disable lock...
210
  	container_of(rcu_dereference_raw(ptr), type, member)
72c6a9870   Jiri Pirko   rculist.h: introd...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  
  /**
   * list_first_entry_rcu - get the first element from a list
   * @ptr:        the list head to take the element from.
   * @type:       the type of the struct this is embedded in.
   * @member:     the name of the list_struct within the struct.
   *
   * Note, that list is expected to be not empty.
   *
   * This primitive may safely run concurrently with the _rcu list-mutation
   * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
   */
  #define list_first_entry_rcu(ptr, type, member) \
  	list_entry_rcu((ptr)->next, type, member)
82524746c   Franck Bui-Huu   rcu: split list.h...
225
  #define __list_for_each_rcu(pos, head) \
3120438ad   Paul E. McKenney   rcu: Disable lock...
226
  	for (pos = rcu_dereference_raw((head)->next); \
78b0e0e9b   Paul E. McKenney   RCU, rculist.h: f...
227
  		pos != (head); \
3120438ad   Paul E. McKenney   rcu: Disable lock...
228
  		pos = rcu_dereference_raw(pos->next))
82524746c   Franck Bui-Huu   rcu: split list.h...
229
230
231
232
233
234
235
236
237
238
239
240
  
  /**
   * list_for_each_entry_rcu	-	iterate over rcu list of given type
   * @pos:	the type * to use as a loop cursor.
   * @head:	the head for your list.
   * @member:	the name of the list_struct within the struct.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */
  #define list_for_each_entry_rcu(pos, head, member) \
72c6a9870   Jiri Pirko   rculist.h: introd...
241
  	for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
78b0e0e9b   Paul E. McKenney   RCU, rculist.h: f...
242
  		prefetch(pos->member.next), &pos->member != (head); \
72c6a9870   Jiri Pirko   rculist.h: introd...
243
  		pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
82524746c   Franck Bui-Huu   rcu: split list.h...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  
  
  /**
   * list_for_each_continue_rcu
   * @pos:	the &struct list_head to use as a loop cursor.
   * @head:	the head for your list.
   *
   * Iterate over an rcu-protected list, continuing after current point.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as list_add_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */
  #define list_for_each_continue_rcu(pos, head) \
3120438ad   Paul E. McKenney   rcu: Disable lock...
258
  	for ((pos) = rcu_dereference_raw((pos)->next); \
78b0e0e9b   Paul E. McKenney   RCU, rculist.h: f...
259
  		prefetch((pos)->next), (pos) != (head); \
3120438ad   Paul E. McKenney   rcu: Disable lock...
260
  		(pos) = rcu_dereference_raw((pos)->next))
82524746c   Franck Bui-Huu   rcu: split list.h...
261
262
  
  /**
254245d23   stephen hemminger   netdev: add netde...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
   * list_for_each_entry_continue_rcu - continue iteration over list of given type
   * @pos:	the type * to use as a loop cursor.
   * @head:	the head for your list.
   * @member:	the name of the list_struct within the struct.
   *
   * Continue to iterate over list of given type, continuing after
   * the current position.
   */
  #define list_for_each_entry_continue_rcu(pos, head, member) 		\
  	for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \
  	     prefetch(pos->member.next), &pos->member != (head);	\
  	     pos = list_entry_rcu(pos->member.next, typeof(*pos), member))
  
  /**
82524746c   Franck Bui-Huu   rcu: split list.h...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
   * hlist_del_rcu - deletes entry from hash list without re-initialization
   * @n: the element to delete from the hash list.
   *
   * Note: list_unhashed() on entry does not return true after this,
   * the entry is in an undefined state. It is useful for RCU based
   * lockfree traversal.
   *
   * In particular, it means that we can not poison the forward
   * pointers that may still be used for walking the hash list.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as hlist_add_head_rcu()
   * or hlist_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * hlist_for_each_entry().
   */
  static inline void hlist_del_rcu(struct hlist_node *n)
  {
  	__hlist_del(n);
  	n->pprev = LIST_POISON2;
  }
  
  /**
   * hlist_replace_rcu - replace old entry by new one
   * @old : the element to be replaced
   * @new : the new element to insert
   *
   * The @old entry will be replaced with the @new entry atomically.
   */
  static inline void hlist_replace_rcu(struct hlist_node *old,
  					struct hlist_node *new)
  {
  	struct hlist_node *next = old->next;
  
  	new->next = next;
  	new->pprev = old->pprev;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
315
  	rcu_assign_pointer(*new->pprev, new);
82524746c   Franck Bui-Huu   rcu: split list.h...
316
317
  	if (next)
  		new->next->pprev = &new->next;
82524746c   Franck Bui-Huu   rcu: split list.h...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  	old->pprev = LIST_POISON2;
  }
  
  /**
   * hlist_add_head_rcu
   * @n: the element to add to the hash list.
   * @h: the list to add to.
   *
   * Description:
   * Adds the specified element to the specified hlist,
   * while permitting racing traversals.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as hlist_add_head_rcu()
   * or hlist_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * hlist_for_each_entry_rcu(), used to prevent memory-consistency
   * problems on Alpha CPUs.  Regardless of the type of CPU, the
   * list-traversal primitive must be guarded by rcu_read_lock().
   */
  static inline void hlist_add_head_rcu(struct hlist_node *n,
  					struct hlist_head *h)
  {
  	struct hlist_node *first = h->first;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
344

82524746c   Franck Bui-Huu   rcu: split list.h...
345
346
  	n->next = first;
  	n->pprev = &h->first;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
347
  	rcu_assign_pointer(h->first, n);
82524746c   Franck Bui-Huu   rcu: split list.h...
348
349
  	if (first)
  		first->pprev = &n->next;
82524746c   Franck Bui-Huu   rcu: split list.h...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  }
  
  /**
   * hlist_add_before_rcu
   * @n: the new element to add to the hash list.
   * @next: the existing element to add the new element before.
   *
   * Description:
   * Adds the specified element to the specified hlist
   * before the specified node while permitting racing traversals.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as hlist_add_head_rcu()
   * or hlist_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * hlist_for_each_entry_rcu(), used to prevent memory-consistency
   * problems on Alpha CPUs.
   */
  static inline void hlist_add_before_rcu(struct hlist_node *n,
  					struct hlist_node *next)
  {
  	n->pprev = next->pprev;
  	n->next = next;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
375
  	rcu_assign_pointer(*(n->pprev), n);
82524746c   Franck Bui-Huu   rcu: split list.h...
376
  	next->pprev = &n->next;
82524746c   Franck Bui-Huu   rcu: split list.h...
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
  }
  
  /**
   * hlist_add_after_rcu
   * @prev: the existing element to add the new element after.
   * @n: the new element to add to the hash list.
   *
   * Description:
   * Adds the specified element to the specified hlist
   * after the specified node while permitting racing traversals.
   *
   * The caller must take whatever precautions are necessary
   * (such as holding appropriate locks) to avoid racing
   * with another list-mutation primitive, such as hlist_add_head_rcu()
   * or hlist_del_rcu(), running on this same list.
   * However, it is perfectly legal to run concurrently with
   * the _rcu list-traversal primitives, such as
   * hlist_for_each_entry_rcu(), used to prevent memory-consistency
   * problems on Alpha CPUs.
   */
  static inline void hlist_add_after_rcu(struct hlist_node *prev,
  				       struct hlist_node *n)
  {
  	n->next = prev->next;
  	n->pprev = &prev->next;
10aa9d2cf   Franck Bui-Huu   rculist.h: use th...
402
  	rcu_assign_pointer(prev->next, n);
82524746c   Franck Bui-Huu   rcu: split list.h...
403
404
405
  	if (n->next)
  		n->next->pprev = &n->next;
  }
1cc523271   stephen hemminger   seq_file: add RCU...
406
407
408
409
  #define __hlist_for_each_rcu(pos, head)			\
  	for (pos = rcu_dereference((head)->first);	\
  	     pos && ({ prefetch(pos->next); 1; });	\
  	     pos = rcu_dereference(pos->next))
82524746c   Franck Bui-Huu   rcu: split list.h...
410
411
412
413
414
415
416
417
418
419
420
421
  /**
   * hlist_for_each_entry_rcu - iterate over rcu list of given type
   * @tpos:	the type * to use as a loop cursor.
   * @pos:	the &struct hlist_node to use as a loop cursor.
   * @head:	the head for your list.
   * @member:	the name of the hlist_node within the struct.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as hlist_add_head_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */
  #define hlist_for_each_entry_rcu(tpos, pos, head, member)		 \
3120438ad   Paul E. McKenney   rcu: Disable lock...
422
  	for (pos = rcu_dereference_raw((head)->first);			 \
78b0e0e9b   Paul E. McKenney   RCU, rculist.h: f...
423
  		pos && ({ prefetch(pos->next); 1; }) &&			 \
82524746c   Franck Bui-Huu   rcu: split list.h...
424
  		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
3120438ad   Paul E. McKenney   rcu: Disable lock...
425
  		pos = rcu_dereference_raw(pos->next))
82524746c   Franck Bui-Huu   rcu: split list.h...
426

5c578aedc   stephen hemminger   IPv6: convert add...
427
  /**
4f70ecca9   Eric Dumazet   net: rcu fixes
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
   * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
   * @tpos:	the type * to use as a loop cursor.
   * @pos:	the &struct hlist_node to use as a loop cursor.
   * @head:	the head for your list.
   * @member:	the name of the hlist_node within the struct.
   *
   * This list-traversal primitive may safely run concurrently with
   * the _rcu list-mutation primitives such as hlist_add_head_rcu()
   * as long as the traversal is guarded by rcu_read_lock().
   */
  #define hlist_for_each_entry_rcu_bh(tpos, pos, head, member)		 \
  	for (pos = rcu_dereference_bh((head)->first);			 \
  		pos && ({ prefetch(pos->next); 1; }) &&			 \
  		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
  		pos = rcu_dereference_bh(pos->next))
  
  /**
5c578aedc   stephen hemminger   IPv6: convert add...
445
446
447
448
449
450
451
452
453
454
   * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point
   * @tpos:	the type * to use as a loop cursor.
   * @pos:	the &struct hlist_node to use as a loop cursor.
   * @member:	the name of the hlist_node within the struct.
   */
  #define hlist_for_each_entry_continue_rcu(tpos, pos, member)		\
  	for (pos = rcu_dereference((pos)->next);			\
  	     pos && ({ prefetch(pos->next); 1; }) &&			\
  	     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });  \
  	     pos = rcu_dereference(pos->next))
4f70ecca9   Eric Dumazet   net: rcu fixes
455
456
457
458
459
460
461
462
463
464
465
  /**
   * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point
   * @tpos:	the type * to use as a loop cursor.
   * @pos:	the &struct hlist_node to use as a loop cursor.
   * @member:	the name of the hlist_node within the struct.
   */
  #define hlist_for_each_entry_continue_rcu_bh(tpos, pos, member)		\
  	for (pos = rcu_dereference_bh((pos)->next);			\
  	     pos && ({ prefetch(pos->next); 1; }) &&			\
  	     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; });  \
  	     pos = rcu_dereference_bh(pos->next))
5c578aedc   stephen hemminger   IPv6: convert add...
466

82524746c   Franck Bui-Huu   rcu: split list.h...
467
468
  #endif	/* __KERNEL__ */
  #endif