99#include <linux/list.h>
1010#include <linux/rcupdate.h>
1111
12+ /*
13+ * return the ->next pointer of a list_head in an rcu safe
14+ * way, we must not access it directly
15+ */
16+ #define list_next_rcu (list ) (*((struct list_head __rcu **)(&(list)->next)))
17+
1218/*
1319 * Insert a new entry between two known consecutive entries.
1420 *
@@ -20,7 +26,7 @@ static inline void __list_add_rcu(struct list_head *new,
2026{
2127 new -> next = next ;
2228 new -> prev = prev ;
23- rcu_assign_pointer (prev -> next , new );
29+ rcu_assign_pointer (list_next_rcu ( prev ) , new );
2430 next -> prev = new ;
2531}
2632
@@ -138,7 +144,7 @@ static inline void list_replace_rcu(struct list_head *old,
138144{
139145 new -> next = old -> next ;
140146 new -> prev = old -> prev ;
141- rcu_assign_pointer (new -> prev -> next , new );
147+ rcu_assign_pointer (list_next_rcu ( new -> prev ) , new );
142148 new -> next -> prev = new ;
143149 old -> prev = LIST_POISON2 ;
144150}
@@ -193,7 +199,7 @@ static inline void list_splice_init_rcu(struct list_head *list,
193199 */
194200
195201 last -> next = at ;
196- rcu_assign_pointer (head -> next , first );
202+ rcu_assign_pointer (list_next_rcu ( head ) , first );
197203 first -> prev = head ;
198204 at -> prev = last ;
199205}
@@ -208,7 +214,9 @@ static inline void list_splice_init_rcu(struct list_head *list,
208214 * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
209215 */
210216#define list_entry_rcu (ptr , type , member ) \
211- container_of(rcu_dereference_raw(ptr), type, member)
217+ ({typeof (*ptr) __rcu *__ptr = (typeof (*ptr) __rcu __force *)ptr; \
218+ container_of((typeof(ptr))rcu_dereference_raw(__ptr), type, member); \
219+ })
212220
213221/**
214222 * list_first_entry_rcu - get the first element from a list
@@ -225,9 +233,9 @@ static inline void list_splice_init_rcu(struct list_head *list,
225233 list_entry_rcu((ptr)->next, type, member)
226234
227235#define __list_for_each_rcu (pos , head ) \
228- for (pos = rcu_dereference_raw((head)->next ); \
236+ for (pos = rcu_dereference_raw(list_next_rcu (head)); \
229237 pos != (head); \
230- pos = rcu_dereference_raw(pos->next ))
238+ pos = rcu_dereference_raw(list_next_rcu(( pos) ))
231239
232240/**
233241 * list_for_each_entry_rcu - iterate over rcu list of given type
@@ -257,9 +265,9 @@ static inline void list_splice_init_rcu(struct list_head *list,
257265 * as long as the traversal is guarded by rcu_read_lock().
258266 */
259267#define list_for_each_continue_rcu (pos , head ) \
260- for ((pos) = rcu_dereference_raw((pos)->next ); \
268+ for ((pos) = rcu_dereference_raw(list_next_rcu (pos)); \
261269 prefetch((pos)->next), (pos) != (head); \
262- (pos) = rcu_dereference_raw((pos)->next ))
270+ (pos) = rcu_dereference_raw(list_next_rcu (pos)))
263271
264272/**
265273 * list_for_each_entry_continue_rcu - continue iteration over list of given type
@@ -314,12 +322,19 @@ static inline void hlist_replace_rcu(struct hlist_node *old,
314322
315323 new -> next = next ;
316324 new -> pprev = old -> pprev ;
317- rcu_assign_pointer (* new -> pprev , new );
325+ rcu_assign_pointer (* ( struct hlist_node __rcu * * ) new -> pprev , new );
318326 if (next )
319327 new -> next -> pprev = & new -> next ;
320328 old -> pprev = LIST_POISON2 ;
321329}
322330
331+ /*
332+ * return the first or the next element in an RCU protected hlist
333+ */
334+ #define hlist_first_rcu (head ) (*((struct hlist_node __rcu **)(&(head)->first)))
335+ #define hlist_next_rcu (node ) (*((struct hlist_node __rcu **)(&(node)->next)))
336+ #define hlist_pprev_rcu (node ) (*((struct hlist_node __rcu **)((node)->pprev)))
337+
323338/**
324339 * hlist_add_head_rcu
325340 * @n: the element to add to the hash list.
@@ -346,7 +361,7 @@ static inline void hlist_add_head_rcu(struct hlist_node *n,
346361
347362 n -> next = first ;
348363 n -> pprev = & h -> first ;
349- rcu_assign_pointer (h -> first , n );
364+ rcu_assign_pointer (hlist_first_rcu ( h ) , n );
350365 if (first )
351366 first -> pprev = & n -> next ;
352367}
@@ -374,7 +389,7 @@ static inline void hlist_add_before_rcu(struct hlist_node *n,
374389{
375390 n -> pprev = next -> pprev ;
376391 n -> next = next ;
377- rcu_assign_pointer (* ( n -> pprev ), n );
392+ rcu_assign_pointer (hlist_pprev_rcu ( n ), n );
378393 next -> pprev = & n -> next ;
379394}
380395
@@ -401,15 +416,15 @@ static inline void hlist_add_after_rcu(struct hlist_node *prev,
401416{
402417 n -> next = prev -> next ;
403418 n -> pprev = & prev -> next ;
404- rcu_assign_pointer (prev -> next , n );
419+ rcu_assign_pointer (hlist_next_rcu ( prev ) , n );
405420 if (n -> next )
406421 n -> next -> pprev = & n -> next ;
407422}
408423
409- #define __hlist_for_each_rcu (pos , head ) \
410- for (pos = rcu_dereference((head)->first ); \
411- pos && ({ prefetch(pos->next); 1; }); \
412- pos = rcu_dereference(pos->next ))
424+ #define __hlist_for_each_rcu (pos , head ) \
425+ for (pos = rcu_dereference(hlist_first_rcu (head)); \
426+ pos && ({ prefetch(pos->next); 1; }); \
427+ pos = rcu_dereference(hlist_next_rcu( pos) ))
413428
414429/**
415430 * hlist_for_each_entry_rcu - iterate over rcu list of given type
@@ -422,11 +437,11 @@ static inline void hlist_add_after_rcu(struct hlist_node *prev,
422437 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
423438 * as long as the traversal is guarded by rcu_read_lock().
424439 */
425- #define hlist_for_each_entry_rcu (tpos , pos , head , member ) \
426- for (pos = rcu_dereference_raw((head)->first ); \
440+ #define hlist_for_each_entry_rcu (tpos , pos , head , member ) \
441+ for (pos = rcu_dereference_raw(hlist_first_rcu (head)); \
427442 pos && ({ prefetch(pos->next); 1; }) && \
428443 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \
429- pos = rcu_dereference_raw(pos->next ))
444+ pos = rcu_dereference_raw(hlist_next_rcu( pos) ))
430445
431446/**
432447 * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type
0 commit comments