Rev 5270 | Rev 6936 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5270 | Rev 6082 | ||
---|---|---|---|
1 | #ifndef _LINUX_RCULIST_H |
1 | #ifndef _LINUX_RCULIST_H |
2 | #define _LINUX_RCULIST_H |
2 | #define _LINUX_RCULIST_H |
3 | 3 | ||
4 | #ifdef __KERNEL__ |
4 | #ifdef __KERNEL__ |
5 | 5 | ||
6 | /* |
6 | /* |
7 | * RCU-protected list version |
7 | * RCU-protected list version |
8 | */ |
8 | */ |
9 | #include |
9 | #include |
10 | #include |
10 | #include |
11 | 11 | ||
12 | /* |
12 | /* |
13 | * Why is there no list_empty_rcu()? Because list_empty() serves this |
13 | * Why is there no list_empty_rcu()? Because list_empty() serves this |
14 | * purpose. The list_empty() function fetches the RCU-protected pointer |
14 | * purpose. The list_empty() function fetches the RCU-protected pointer |
15 | * and compares it to the address of the list head, but neither dereferences |
15 | * and compares it to the address of the list head, but neither dereferences |
16 | * this pointer itself nor provides this pointer to the caller. Therefore, |
16 | * this pointer itself nor provides this pointer to the caller. Therefore, |
17 | * it is not necessary to use rcu_dereference(), so that list_empty() can |
17 | * it is not necessary to use rcu_dereference(), so that list_empty() can |
18 | * be used anywhere you would want to use a list_empty_rcu(). |
18 | * be used anywhere you would want to use a list_empty_rcu(). |
19 | */ |
19 | */ |
20 | 20 | ||
21 | /* |
21 | /* |
22 | * INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers |
22 | * INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers |
23 | * @list: list to be initialized |
23 | * @list: list to be initialized |
24 | * |
24 | * |
25 | * You should instead use INIT_LIST_HEAD() for normal initialization and |
25 | * You should instead use INIT_LIST_HEAD() for normal initialization and |
26 | * cleanup tasks, when readers have no access to the list being initialized. |
26 | * cleanup tasks, when readers have no access to the list being initialized. |
27 | * However, if the list being initialized is visible to readers, you |
27 | * However, if the list being initialized is visible to readers, you |
28 | * need to keep the compiler from being too mischievous. |
28 | * need to keep the compiler from being too mischievous. |
29 | */ |
29 | */ |
30 | static inline void INIT_LIST_HEAD_RCU(struct list_head *list) |
30 | static inline void INIT_LIST_HEAD_RCU(struct list_head *list) |
31 | { |
31 | { |
32 | ACCESS_ONCE(list->next) = list; |
32 | WRITE_ONCE(list->next, list); |
33 | ACCESS_ONCE(list->prev) = list; |
33 | WRITE_ONCE(list->prev, list); |
34 | } |
34 | } |
35 | 35 | ||
36 | /* |
36 | /* |
37 | * return the ->next pointer of a list_head in an rcu safe |
37 | * return the ->next pointer of a list_head in an rcu safe |
38 | * way, we must not access it directly |
38 | * way, we must not access it directly |
39 | */ |
39 | */ |
40 | #define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next))) |
40 | #define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next))) |
41 | 41 | ||
42 | /* |
42 | /* |
43 | * Insert a new entry between two known consecutive entries. |
43 | * Insert a new entry between two known consecutive entries. |
44 | * |
44 | * |
45 | * This is only for internal list manipulation where we know |
45 | * This is only for internal list manipulation where we know |
46 | * the prev/next entries already! |
46 | * the prev/next entries already! |
47 | */ |
47 | */ |
48 | #ifndef CONFIG_DEBUG_LIST |
48 | #ifndef CONFIG_DEBUG_LIST |
49 | static inline void __list_add_rcu(struct list_head *new, |
49 | static inline void __list_add_rcu(struct list_head *new, |
50 | struct list_head *prev, struct list_head *next) |
50 | struct list_head *prev, struct list_head *next) |
51 | { |
51 | { |
52 | new->next = next; |
52 | new->next = next; |
53 | new->prev = prev; |
53 | new->prev = prev; |
54 | rcu_assign_pointer(list_next_rcu(prev), new); |
54 | rcu_assign_pointer(list_next_rcu(prev), new); |
55 | next->prev = new; |
55 | next->prev = new; |
56 | } |
56 | } |
57 | #else |
57 | #else |
58 | void __list_add_rcu(struct list_head *new, |
58 | void __list_add_rcu(struct list_head *new, |
59 | struct list_head *prev, struct list_head *next); |
59 | struct list_head *prev, struct list_head *next); |
60 | #endif |
60 | #endif |
61 | 61 | ||
62 | /** |
62 | /** |
63 | * list_add_rcu - add a new entry to rcu-protected list |
63 | * list_add_rcu - add a new entry to rcu-protected list |
64 | * @new: new entry to be added |
64 | * @new: new entry to be added |
65 | * @head: list head to add it after |
65 | * @head: list head to add it after |
66 | * |
66 | * |
67 | * Insert a new entry after the specified head. |
67 | * Insert a new entry after the specified head. |
68 | * This is good for implementing stacks. |
68 | * This is good for implementing stacks. |
69 | * |
69 | * |
70 | * The caller must take whatever precautions are necessary |
70 | * The caller must take whatever precautions are necessary |
71 | * (such as holding appropriate locks) to avoid racing |
71 | * (such as holding appropriate locks) to avoid racing |
72 | * with another list-mutation primitive, such as list_add_rcu() |
72 | * with another list-mutation primitive, such as list_add_rcu() |
73 | * or list_del_rcu(), running on this same list. |
73 | * or list_del_rcu(), running on this same list. |
74 | * However, it is perfectly legal to run concurrently with |
74 | * However, it is perfectly legal to run concurrently with |
75 | * the _rcu list-traversal primitives, such as |
75 | * the _rcu list-traversal primitives, such as |
76 | * list_for_each_entry_rcu(). |
76 | * list_for_each_entry_rcu(). |
77 | */ |
77 | */ |
78 | static inline void list_add_rcu(struct list_head *new, struct list_head *head) |
78 | static inline void list_add_rcu(struct list_head *new, struct list_head *head) |
79 | { |
79 | { |
80 | __list_add_rcu(new, head, head->next); |
80 | __list_add_rcu(new, head, head->next); |
81 | } |
81 | } |
82 | 82 | ||
83 | /** |
83 | /** |
84 | * list_add_tail_rcu - add a new entry to rcu-protected list |
84 | * list_add_tail_rcu - add a new entry to rcu-protected list |
85 | * @new: new entry to be added |
85 | * @new: new entry to be added |
86 | * @head: list head to add it before |
86 | * @head: list head to add it before |
87 | * |
87 | * |
88 | * Insert a new entry before the specified head. |
88 | * Insert a new entry before the specified head. |
89 | * This is useful for implementing queues. |
89 | * This is useful for implementing queues. |
90 | * |
90 | * |
91 | * The caller must take whatever precautions are necessary |
91 | * The caller must take whatever precautions are necessary |
92 | * (such as holding appropriate locks) to avoid racing |
92 | * (such as holding appropriate locks) to avoid racing |
93 | * with another list-mutation primitive, such as list_add_tail_rcu() |
93 | * with another list-mutation primitive, such as list_add_tail_rcu() |
94 | * or list_del_rcu(), running on this same list. |
94 | * or list_del_rcu(), running on this same list. |
95 | * However, it is perfectly legal to run concurrently with |
95 | * However, it is perfectly legal to run concurrently with |
96 | * the _rcu list-traversal primitives, such as |
96 | * the _rcu list-traversal primitives, such as |
97 | * list_for_each_entry_rcu(). |
97 | * list_for_each_entry_rcu(). |
98 | */ |
98 | */ |
99 | static inline void list_add_tail_rcu(struct list_head *new, |
99 | static inline void list_add_tail_rcu(struct list_head *new, |
100 | struct list_head *head) |
100 | struct list_head *head) |
101 | { |
101 | { |
102 | __list_add_rcu(new, head->prev, head); |
102 | __list_add_rcu(new, head->prev, head); |
103 | } |
103 | } |
104 | 104 | ||
105 | /** |
105 | /** |
106 | * list_del_rcu - deletes entry from list without re-initialization |
106 | * list_del_rcu - deletes entry from list without re-initialization |
107 | * @entry: the element to delete from the list. |
107 | * @entry: the element to delete from the list. |
108 | * |
108 | * |
109 | * Note: list_empty() on entry does not return true after this, |
109 | * Note: list_empty() on entry does not return true after this, |
110 | * the entry is in an undefined state. It is useful for RCU based |
110 | * the entry is in an undefined state. It is useful for RCU based |
111 | * lockfree traversal. |
111 | * lockfree traversal. |
112 | * |
112 | * |
113 | * In particular, it means that we can not poison the forward |
113 | * In particular, it means that we can not poison the forward |
114 | * pointers that may still be used for walking the list. |
114 | * pointers that may still be used for walking the list. |
115 | * |
115 | * |
116 | * The caller must take whatever precautions are necessary |
116 | * The caller must take whatever precautions are necessary |
117 | * (such as holding appropriate locks) to avoid racing |
117 | * (such as holding appropriate locks) to avoid racing |
118 | * with another list-mutation primitive, such as list_del_rcu() |
118 | * with another list-mutation primitive, such as list_del_rcu() |
119 | * or list_add_rcu(), running on this same list. |
119 | * or list_add_rcu(), running on this same list. |
120 | * However, it is perfectly legal to run concurrently with |
120 | * However, it is perfectly legal to run concurrently with |
121 | * the _rcu list-traversal primitives, such as |
121 | * the _rcu list-traversal primitives, such as |
122 | * list_for_each_entry_rcu(). |
122 | * list_for_each_entry_rcu(). |
123 | * |
123 | * |
124 | * Note that the caller is not permitted to immediately free |
124 | * Note that the caller is not permitted to immediately free |
125 | * the newly deleted entry. Instead, either synchronize_rcu() |
125 | * the newly deleted entry. Instead, either synchronize_rcu() |
126 | * or call_rcu() must be used to defer freeing until an RCU |
126 | * or call_rcu() must be used to defer freeing until an RCU |
127 | * grace period has elapsed. |
127 | * grace period has elapsed. |
128 | */ |
128 | */ |
129 | static inline void list_del_rcu(struct list_head *entry) |
129 | static inline void list_del_rcu(struct list_head *entry) |
130 | { |
130 | { |
131 | __list_del_entry(entry); |
131 | __list_del_entry(entry); |
132 | entry->prev = LIST_POISON2; |
132 | entry->prev = LIST_POISON2; |
133 | } |
133 | } |
134 | 134 | ||
135 | /** |
135 | /** |
136 | * hlist_del_init_rcu - deletes entry from hash list with re-initialization |
136 | * hlist_del_init_rcu - deletes entry from hash list with re-initialization |
137 | * @n: the element to delete from the hash list. |
137 | * @n: the element to delete from the hash list. |
138 | * |
138 | * |
139 | * Note: list_unhashed() on the node return true after this. It is |
139 | * Note: list_unhashed() on the node return true after this. It is |
140 | * useful for RCU based read lockfree traversal if the writer side |
140 | * useful for RCU based read lockfree traversal if the writer side |
141 | * must know if the list entry is still hashed or already unhashed. |
141 | * must know if the list entry is still hashed or already unhashed. |
142 | * |
142 | * |
143 | * In particular, it means that we can not poison the forward pointers |
143 | * In particular, it means that we can not poison the forward pointers |
144 | * that may still be used for walking the hash list and we can only |
144 | * that may still be used for walking the hash list and we can only |
145 | * zero the pprev pointer so list_unhashed() will return true after |
145 | * zero the pprev pointer so list_unhashed() will return true after |
146 | * this. |
146 | * this. |
147 | * |
147 | * |
148 | * The caller must take whatever precautions are necessary (such as |
148 | * The caller must take whatever precautions are necessary (such as |
149 | * holding appropriate locks) to avoid racing with another |
149 | * holding appropriate locks) to avoid racing with another |
150 | * list-mutation primitive, such as hlist_add_head_rcu() or |
150 | * list-mutation primitive, such as hlist_add_head_rcu() or |
151 | * hlist_del_rcu(), running on this same list. However, it is |
151 | * hlist_del_rcu(), running on this same list. However, it is |
152 | * perfectly legal to run concurrently with the _rcu list-traversal |
152 | * perfectly legal to run concurrently with the _rcu list-traversal |
153 | * primitives, such as hlist_for_each_entry_rcu(). |
153 | * primitives, such as hlist_for_each_entry_rcu(). |
154 | */ |
154 | */ |
155 | static inline void hlist_del_init_rcu(struct hlist_node *n) |
155 | static inline void hlist_del_init_rcu(struct hlist_node *n) |
156 | { |
156 | { |
157 | if (!hlist_unhashed(n)) { |
157 | if (!hlist_unhashed(n)) { |
158 | __hlist_del(n); |
158 | __hlist_del(n); |
159 | n->pprev = NULL; |
159 | n->pprev = NULL; |
160 | } |
160 | } |
161 | } |
161 | } |
162 | 162 | ||
163 | /** |
163 | /** |
164 | * list_replace_rcu - replace old entry by new one |
164 | * list_replace_rcu - replace old entry by new one |
165 | * @old : the element to be replaced |
165 | * @old : the element to be replaced |
166 | * @new : the new element to insert |
166 | * @new : the new element to insert |
167 | * |
167 | * |
168 | * The @old entry will be replaced with the @new entry atomically. |
168 | * The @old entry will be replaced with the @new entry atomically. |
169 | * Note: @old should not be empty. |
169 | * Note: @old should not be empty. |
170 | */ |
170 | */ |
171 | static inline void list_replace_rcu(struct list_head *old, |
171 | static inline void list_replace_rcu(struct list_head *old, |
172 | struct list_head *new) |
172 | struct list_head *new) |
173 | { |
173 | { |
174 | new->next = old->next; |
174 | new->next = old->next; |
175 | new->prev = old->prev; |
175 | new->prev = old->prev; |
176 | rcu_assign_pointer(list_next_rcu(new->prev), new); |
176 | rcu_assign_pointer(list_next_rcu(new->prev), new); |
177 | new->next->prev = new; |
177 | new->next->prev = new; |
178 | old->prev = LIST_POISON2; |
178 | old->prev = LIST_POISON2; |
179 | } |
179 | } |
180 | 180 | ||
181 | /** |
181 | /** |
182 | * list_splice_init_rcu - splice an RCU-protected list into an existing list. |
182 | * list_splice_init_rcu - splice an RCU-protected list into an existing list. |
183 | * @list: the RCU-protected list to splice |
183 | * @list: the RCU-protected list to splice |
184 | * @head: the place in the list to splice the first list into |
184 | * @head: the place in the list to splice the first list into |
185 | * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... |
185 | * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... |
186 | * |
186 | * |
187 | * @head can be RCU-read traversed concurrently with this function. |
187 | * @head can be RCU-read traversed concurrently with this function. |
188 | * |
188 | * |
189 | * Note that this function blocks. |
189 | * Note that this function blocks. |
190 | * |
190 | * |
191 | * Important note: the caller must take whatever action is necessary to |
191 | * Important note: the caller must take whatever action is necessary to |
192 | * prevent any other updates to @head. In principle, it is possible |
192 | * prevent any other updates to @head. In principle, it is possible |
193 | * to modify the list as soon as sync() begins execution. |
193 | * to modify the list as soon as sync() begins execution. |
194 | * If this sort of thing becomes necessary, an alternative version |
194 | * If this sort of thing becomes necessary, an alternative version |
195 | * based on call_rcu() could be created. But only if -really- |
195 | * based on call_rcu() could be created. But only if -really- |
196 | * needed -- there is no shortage of RCU API members. |
196 | * needed -- there is no shortage of RCU API members. |
197 | */ |
197 | */ |
198 | static inline void list_splice_init_rcu(struct list_head *list, |
198 | static inline void list_splice_init_rcu(struct list_head *list, |
199 | struct list_head *head, |
199 | struct list_head *head, |
200 | void (*sync)(void)) |
200 | void (*sync)(void)) |
201 | { |
201 | { |
202 | struct list_head *first = list->next; |
202 | struct list_head *first = list->next; |
203 | struct list_head *last = list->prev; |
203 | struct list_head *last = list->prev; |
204 | struct list_head *at = head->next; |
204 | struct list_head *at = head->next; |
205 | 205 | ||
206 | if (list_empty(list)) |
206 | if (list_empty(list)) |
207 | return; |
207 | return; |
208 | 208 | ||
209 | /* |
209 | /* |
210 | * "first" and "last" tracking list, so initialize it. RCU readers |
210 | * "first" and "last" tracking list, so initialize it. RCU readers |
211 | * have access to this list, so we must use INIT_LIST_HEAD_RCU() |
211 | * have access to this list, so we must use INIT_LIST_HEAD_RCU() |
212 | * instead of INIT_LIST_HEAD(). |
212 | * instead of INIT_LIST_HEAD(). |
213 | */ |
213 | */ |
214 | 214 | ||
215 | INIT_LIST_HEAD_RCU(list); |
215 | INIT_LIST_HEAD_RCU(list); |
216 | 216 | ||
217 | /* |
217 | /* |
218 | * At this point, the list body still points to the source list. |
218 | * At this point, the list body still points to the source list. |
219 | * Wait for any readers to finish using the list before splicing |
219 | * Wait for any readers to finish using the list before splicing |
220 | * the list body into the new list. Any new readers will see |
220 | * the list body into the new list. Any new readers will see |
221 | * an empty list. |
221 | * an empty list. |
222 | */ |
222 | */ |
223 | 223 | ||
224 | sync(); |
224 | sync(); |
225 | 225 | ||
226 | /* |
226 | /* |
227 | * Readers are finished with the source list, so perform splice. |
227 | * Readers are finished with the source list, so perform splice. |
228 | * The order is important if the new list is global and accessible |
228 | * The order is important if the new list is global and accessible |
229 | * to concurrent RCU readers. Note that RCU readers are not |
229 | * to concurrent RCU readers. Note that RCU readers are not |
230 | * permitted to traverse the prev pointers without excluding |
230 | * permitted to traverse the prev pointers without excluding |
231 | * this function. |
231 | * this function. |
232 | */ |
232 | */ |
233 | 233 | ||
234 | last->next = at; |
234 | last->next = at; |
235 | rcu_assign_pointer(list_next_rcu(head), first); |
235 | rcu_assign_pointer(list_next_rcu(head), first); |
236 | first->prev = head; |
236 | first->prev = head; |
237 | at->prev = last; |
237 | at->prev = last; |
238 | } |
238 | } |
239 | 239 | ||
240 | /** |
240 | /** |
241 | * list_entry_rcu - get the struct for this entry |
241 | * list_entry_rcu - get the struct for this entry |
242 | * @ptr: the &struct list_head pointer. |
242 | * @ptr: the &struct list_head pointer. |
243 | * @type: the type of the struct this is embedded in. |
243 | * @type: the type of the struct this is embedded in. |
244 | * @member: the name of the list_head within the struct. |
244 | * @member: the name of the list_head within the struct. |
245 | * |
245 | * |
246 | * This primitive may safely run concurrently with the _rcu list-mutation |
246 | * This primitive may safely run concurrently with the _rcu list-mutation |
247 | * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock(). |
247 | * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock(). |
248 | */ |
248 | */ |
249 | #define list_entry_rcu(ptr, type, member) \ |
249 | #define list_entry_rcu(ptr, type, member) \ |
250 | ({ \ |
- | |
251 | typeof(*ptr) __rcu *__ptr = (typeof(*ptr) __rcu __force *)ptr; \ |
- | |
252 | container_of((typeof(ptr))rcu_dereference_raw(__ptr), type, member); \ |
250 | container_of(lockless_dereference(ptr), type, member) |
253 | }) |
- | |
254 | 251 | ||
255 | /** |
252 | /** |
256 | * Where are list_empty_rcu() and list_first_entry_rcu()? |
253 | * Where are list_empty_rcu() and list_first_entry_rcu()? |
257 | * |
254 | * |
258 | * Implementing those functions following their counterparts list_empty() and |
255 | * Implementing those functions following their counterparts list_empty() and |
259 | * list_first_entry() is not advisable because they lead to subtle race |
256 | * list_first_entry() is not advisable because they lead to subtle race |
260 | * conditions as the following snippet shows: |
257 | * conditions as the following snippet shows: |
261 | * |
258 | * |
262 | * if (!list_empty_rcu(mylist)) { |
259 | * if (!list_empty_rcu(mylist)) { |
263 | * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member); |
260 | * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member); |
264 | * do_something(bar); |
261 | * do_something(bar); |
265 | * } |
262 | * } |
266 | * |
263 | * |
267 | * The list may not be empty when list_empty_rcu checks it, but it may be when |
264 | * The list may not be empty when list_empty_rcu checks it, but it may be when |
268 | * list_first_entry_rcu rereads the ->next pointer. |
265 | * list_first_entry_rcu rereads the ->next pointer. |
269 | * |
266 | * |
270 | * Rereading the ->next pointer is not a problem for list_empty() and |
267 | * Rereading the ->next pointer is not a problem for list_empty() and |
271 | * list_first_entry() because they would be protected by a lock that blocks |
268 | * list_first_entry() because they would be protected by a lock that blocks |
272 | * writers. |
269 | * writers. |
273 | * |
270 | * |
274 | * See list_first_or_null_rcu for an alternative. |
271 | * See list_first_or_null_rcu for an alternative. |
275 | */ |
272 | */ |
276 | 273 | ||
277 | /** |
274 | /** |
278 | * list_first_or_null_rcu - get the first element from a list |
275 | * list_first_or_null_rcu - get the first element from a list |
279 | * @ptr: the list head to take the element from. |
276 | * @ptr: the list head to take the element from. |
280 | * @type: the type of the struct this is embedded in. |
277 | * @type: the type of the struct this is embedded in. |
281 | * @member: the name of the list_head within the struct. |
278 | * @member: the name of the list_head within the struct. |
282 | * |
279 | * |
283 | * Note that if the list is empty, it returns NULL. |
280 | * Note that if the list is empty, it returns NULL. |
284 | * |
281 | * |
285 | * This primitive may safely run concurrently with the _rcu list-mutation |
282 | * This primitive may safely run concurrently with the _rcu list-mutation |
286 | * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock(). |
283 | * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock(). |
287 | */ |
284 | */ |
288 | #define list_first_or_null_rcu(ptr, type, member) \ |
285 | #define list_first_or_null_rcu(ptr, type, member) \ |
289 | ({ \ |
286 | ({ \ |
290 | struct list_head *__ptr = (ptr); \ |
287 | struct list_head *__ptr = (ptr); \ |
291 | struct list_head *__next = ACCESS_ONCE(__ptr->next); \ |
288 | struct list_head *__next = READ_ONCE(__ptr->next); \ |
292 | likely(__ptr != __next) ? list_entry_rcu(__next, type, member) : NULL; \ |
289 | likely(__ptr != __next) ? list_entry_rcu(__next, type, member) : NULL; \ |
293 | }) |
290 | }) |
294 | 291 | ||
295 | /** |
292 | /** |
296 | * list_for_each_entry_rcu - iterate over rcu list of given type |
293 | * list_for_each_entry_rcu - iterate over rcu list of given type |
297 | * @pos: the type * to use as a loop cursor. |
294 | * @pos: the type * to use as a loop cursor. |
298 | * @head: the head for your list. |
295 | * @head: the head for your list. |
299 | * @member: the name of the list_head within the struct. |
296 | * @member: the name of the list_head within the struct. |
300 | * |
297 | * |
301 | * This list-traversal primitive may safely run concurrently with |
298 | * This list-traversal primitive may safely run concurrently with |
302 | * the _rcu list-mutation primitives such as list_add_rcu() |
299 | * the _rcu list-mutation primitives such as list_add_rcu() |
303 | * as long as the traversal is guarded by rcu_read_lock(). |
300 | * as long as the traversal is guarded by rcu_read_lock(). |
304 | */ |
301 | */ |
305 | #define list_for_each_entry_rcu(pos, head, member) \ |
302 | #define list_for_each_entry_rcu(pos, head, member) \ |
306 | for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \ |
303 | for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \ |
307 | &pos->member != (head); \ |
304 | &pos->member != (head); \ |
308 | pos = list_entry_rcu(pos->member.next, typeof(*pos), member)) |
305 | pos = list_entry_rcu(pos->member.next, typeof(*pos), member)) |
309 | 306 | ||
310 | /** |
307 | /** |
311 | * list_for_each_entry_continue_rcu - continue iteration over list of given type |
308 | * list_for_each_entry_continue_rcu - continue iteration over list of given type |
312 | * @pos: the type * to use as a loop cursor. |
309 | * @pos: the type * to use as a loop cursor. |
313 | * @head: the head for your list. |
310 | * @head: the head for your list. |
314 | * @member: the name of the list_head within the struct. |
311 | * @member: the name of the list_head within the struct. |
315 | * |
312 | * |
316 | * Continue to iterate over list of given type, continuing after |
313 | * Continue to iterate over list of given type, continuing after |
317 | * the current position. |
314 | * the current position. |
318 | */ |
315 | */ |
319 | #define list_for_each_entry_continue_rcu(pos, head, member) \ |
316 | #define list_for_each_entry_continue_rcu(pos, head, member) \ |
320 | for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \ |
317 | for (pos = list_entry_rcu(pos->member.next, typeof(*pos), member); \ |
321 | &pos->member != (head); \ |
318 | &pos->member != (head); \ |
322 | pos = list_entry_rcu(pos->member.next, typeof(*pos), member)) |
319 | pos = list_entry_rcu(pos->member.next, typeof(*pos), member)) |
323 | 320 | ||
324 | /** |
321 | /** |
325 | * hlist_del_rcu - deletes entry from hash list without re-initialization |
322 | * hlist_del_rcu - deletes entry from hash list without re-initialization |
326 | * @n: the element to delete from the hash list. |
323 | * @n: the element to delete from the hash list. |
327 | * |
324 | * |
328 | * Note: list_unhashed() on entry does not return true after this, |
325 | * Note: list_unhashed() on entry does not return true after this, |
329 | * the entry is in an undefined state. It is useful for RCU based |
326 | * the entry is in an undefined state. It is useful for RCU based |
330 | * lockfree traversal. |
327 | * lockfree traversal. |
331 | * |
328 | * |
332 | * In particular, it means that we can not poison the forward |
329 | * In particular, it means that we can not poison the forward |
333 | * pointers that may still be used for walking the hash list. |
330 | * pointers that may still be used for walking the hash list. |
334 | * |
331 | * |
335 | * The caller must take whatever precautions are necessary |
332 | * The caller must take whatever precautions are necessary |
336 | * (such as holding appropriate locks) to avoid racing |
333 | * (such as holding appropriate locks) to avoid racing |
337 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
334 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
338 | * or hlist_del_rcu(), running on this same list. |
335 | * or hlist_del_rcu(), running on this same list. |
339 | * However, it is perfectly legal to run concurrently with |
336 | * However, it is perfectly legal to run concurrently with |
340 | * the _rcu list-traversal primitives, such as |
337 | * the _rcu list-traversal primitives, such as |
341 | * hlist_for_each_entry(). |
338 | * hlist_for_each_entry(). |
342 | */ |
339 | */ |
343 | static inline void hlist_del_rcu(struct hlist_node *n) |
340 | static inline void hlist_del_rcu(struct hlist_node *n) |
344 | { |
341 | { |
345 | __hlist_del(n); |
342 | __hlist_del(n); |
346 | n->pprev = LIST_POISON2; |
343 | n->pprev = LIST_POISON2; |
347 | } |
344 | } |
348 | 345 | ||
349 | /** |
346 | /** |
350 | * hlist_replace_rcu - replace old entry by new one |
347 | * hlist_replace_rcu - replace old entry by new one |
351 | * @old : the element to be replaced |
348 | * @old : the element to be replaced |
352 | * @new : the new element to insert |
349 | * @new : the new element to insert |
353 | * |
350 | * |
354 | * The @old entry will be replaced with the @new entry atomically. |
351 | * The @old entry will be replaced with the @new entry atomically. |
355 | */ |
352 | */ |
356 | static inline void hlist_replace_rcu(struct hlist_node *old, |
353 | static inline void hlist_replace_rcu(struct hlist_node *old, |
357 | struct hlist_node *new) |
354 | struct hlist_node *new) |
358 | { |
355 | { |
359 | struct hlist_node *next = old->next; |
356 | struct hlist_node *next = old->next; |
360 | 357 | ||
361 | new->next = next; |
358 | new->next = next; |
362 | new->pprev = old->pprev; |
359 | new->pprev = old->pprev; |
363 | rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new); |
360 | rcu_assign_pointer(*(struct hlist_node __rcu **)new->pprev, new); |
364 | if (next) |
361 | if (next) |
365 | new->next->pprev = &new->next; |
362 | new->next->pprev = &new->next; |
366 | old->pprev = LIST_POISON2; |
363 | old->pprev = LIST_POISON2; |
367 | } |
364 | } |
368 | 365 | ||
369 | /* |
366 | /* |
370 | * return the first or the next element in an RCU protected hlist |
367 | * return the first or the next element in an RCU protected hlist |
371 | */ |
368 | */ |
372 | #define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first))) |
369 | #define hlist_first_rcu(head) (*((struct hlist_node __rcu **)(&(head)->first))) |
373 | #define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next))) |
370 | #define hlist_next_rcu(node) (*((struct hlist_node __rcu **)(&(node)->next))) |
374 | #define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev))) |
371 | #define hlist_pprev_rcu(node) (*((struct hlist_node __rcu **)((node)->pprev))) |
375 | 372 | ||
376 | /** |
373 | /** |
377 | * hlist_add_head_rcu |
374 | * hlist_add_head_rcu |
378 | * @n: the element to add to the hash list. |
375 | * @n: the element to add to the hash list. |
379 | * @h: the list to add to. |
376 | * @h: the list to add to. |
380 | * |
377 | * |
381 | * Description: |
378 | * Description: |
382 | * Adds the specified element to the specified hlist, |
379 | * Adds the specified element to the specified hlist, |
383 | * while permitting racing traversals. |
380 | * while permitting racing traversals. |
384 | * |
381 | * |
385 | * The caller must take whatever precautions are necessary |
382 | * The caller must take whatever precautions are necessary |
386 | * (such as holding appropriate locks) to avoid racing |
383 | * (such as holding appropriate locks) to avoid racing |
387 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
384 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
388 | * or hlist_del_rcu(), running on this same list. |
385 | * or hlist_del_rcu(), running on this same list. |
389 | * However, it is perfectly legal to run concurrently with |
386 | * However, it is perfectly legal to run concurrently with |
390 | * the _rcu list-traversal primitives, such as |
387 | * the _rcu list-traversal primitives, such as |
391 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
388 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
392 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
389 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
393 | * list-traversal primitive must be guarded by rcu_read_lock(). |
390 | * list-traversal primitive must be guarded by rcu_read_lock(). |
394 | */ |
391 | */ |
395 | static inline void hlist_add_head_rcu(struct hlist_node *n, |
392 | static inline void hlist_add_head_rcu(struct hlist_node *n, |
396 | struct hlist_head *h) |
393 | struct hlist_head *h) |
397 | { |
394 | { |
398 | struct hlist_node *first = h->first; |
395 | struct hlist_node *first = h->first; |
399 | 396 | ||
400 | n->next = first; |
397 | n->next = first; |
401 | n->pprev = &h->first; |
398 | n->pprev = &h->first; |
402 | rcu_assign_pointer(hlist_first_rcu(h), n); |
399 | rcu_assign_pointer(hlist_first_rcu(h), n); |
403 | if (first) |
400 | if (first) |
404 | first->pprev = &n->next; |
401 | first->pprev = &n->next; |
405 | } |
402 | } |
406 | 403 | ||
407 | /** |
404 | /** |
408 | * hlist_add_before_rcu |
405 | * hlist_add_before_rcu |
409 | * @n: the new element to add to the hash list. |
406 | * @n: the new element to add to the hash list. |
410 | * @next: the existing element to add the new element before. |
407 | * @next: the existing element to add the new element before. |
411 | * |
408 | * |
412 | * Description: |
409 | * Description: |
413 | * Adds the specified element to the specified hlist |
410 | * Adds the specified element to the specified hlist |
414 | * before the specified node while permitting racing traversals. |
411 | * before the specified node while permitting racing traversals. |
415 | * |
412 | * |
416 | * The caller must take whatever precautions are necessary |
413 | * The caller must take whatever precautions are necessary |
417 | * (such as holding appropriate locks) to avoid racing |
414 | * (such as holding appropriate locks) to avoid racing |
418 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
415 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
419 | * or hlist_del_rcu(), running on this same list. |
416 | * or hlist_del_rcu(), running on this same list. |
420 | * However, it is perfectly legal to run concurrently with |
417 | * However, it is perfectly legal to run concurrently with |
421 | * the _rcu list-traversal primitives, such as |
418 | * the _rcu list-traversal primitives, such as |
422 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
419 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
423 | * problems on Alpha CPUs. |
420 | * problems on Alpha CPUs. |
424 | */ |
421 | */ |
425 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
422 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
426 | struct hlist_node *next) |
423 | struct hlist_node *next) |
427 | { |
424 | { |
428 | n->pprev = next->pprev; |
425 | n->pprev = next->pprev; |
429 | n->next = next; |
426 | n->next = next; |
430 | rcu_assign_pointer(hlist_pprev_rcu(n), n); |
427 | rcu_assign_pointer(hlist_pprev_rcu(n), n); |
431 | next->pprev = &n->next; |
428 | next->pprev = &n->next; |
432 | } |
429 | } |
433 | 430 | ||
434 | /** |
431 | /** |
435 | * hlist_add_behind_rcu |
432 | * hlist_add_behind_rcu |
436 | * @n: the new element to add to the hash list. |
433 | * @n: the new element to add to the hash list. |
437 | * @prev: the existing element to add the new element after. |
434 | * @prev: the existing element to add the new element after. |
438 | * |
435 | * |
439 | * Description: |
436 | * Description: |
440 | * Adds the specified element to the specified hlist |
437 | * Adds the specified element to the specified hlist |
441 | * after the specified node while permitting racing traversals. |
438 | * after the specified node while permitting racing traversals. |
442 | * |
439 | * |
443 | * The caller must take whatever precautions are necessary |
440 | * The caller must take whatever precautions are necessary |
444 | * (such as holding appropriate locks) to avoid racing |
441 | * (such as holding appropriate locks) to avoid racing |
445 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
442 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
446 | * or hlist_del_rcu(), running on this same list. |
443 | * or hlist_del_rcu(), running on this same list. |
447 | * However, it is perfectly legal to run concurrently with |
444 | * However, it is perfectly legal to run concurrently with |
448 | * the _rcu list-traversal primitives, such as |
445 | * the _rcu list-traversal primitives, such as |
449 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
446 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
450 | * problems on Alpha CPUs. |
447 | * problems on Alpha CPUs. |
451 | */ |
448 | */ |
452 | static inline void hlist_add_behind_rcu(struct hlist_node *n, |
449 | static inline void hlist_add_behind_rcu(struct hlist_node *n, |
453 | struct hlist_node *prev) |
450 | struct hlist_node *prev) |
454 | { |
451 | { |
455 | n->next = prev->next; |
452 | n->next = prev->next; |
456 | n->pprev = &prev->next; |
453 | n->pprev = &prev->next; |
457 | rcu_assign_pointer(hlist_next_rcu(prev), n); |
454 | rcu_assign_pointer(hlist_next_rcu(prev), n); |
458 | if (n->next) |
455 | if (n->next) |
459 | n->next->pprev = &n->next; |
456 | n->next->pprev = &n->next; |
460 | } |
457 | } |
461 | 458 | ||
462 | #define __hlist_for_each_rcu(pos, head) \ |
459 | #define __hlist_for_each_rcu(pos, head) \ |
463 | for (pos = rcu_dereference(hlist_first_rcu(head)); \ |
460 | for (pos = rcu_dereference(hlist_first_rcu(head)); \ |
464 | pos; \ |
461 | pos; \ |
465 | pos = rcu_dereference(hlist_next_rcu(pos))) |
462 | pos = rcu_dereference(hlist_next_rcu(pos))) |
466 | 463 | ||
467 | /** |
464 | /** |
468 | * hlist_for_each_entry_rcu - iterate over rcu list of given type |
465 | * hlist_for_each_entry_rcu - iterate over rcu list of given type |
469 | * @pos: the type * to use as a loop cursor. |
466 | * @pos: the type * to use as a loop cursor. |
470 | * @head: the head for your list. |
467 | * @head: the head for your list. |
471 | * @member: the name of the hlist_node within the struct. |
468 | * @member: the name of the hlist_node within the struct. |
472 | * |
469 | * |
473 | * This list-traversal primitive may safely run concurrently with |
470 | * This list-traversal primitive may safely run concurrently with |
474 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
471 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
475 | * as long as the traversal is guarded by rcu_read_lock(). |
472 | * as long as the traversal is guarded by rcu_read_lock(). |
476 | */ |
473 | */ |
477 | #define hlist_for_each_entry_rcu(pos, head, member) \ |
474 | #define hlist_for_each_entry_rcu(pos, head, member) \ |
478 | for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\ |
475 | for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\ |
479 | typeof(*(pos)), member); \ |
476 | typeof(*(pos)), member); \ |
480 | pos; \ |
477 | pos; \ |
481 | pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\ |
478 | pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(\ |
482 | &(pos)->member)), typeof(*(pos)), member)) |
479 | &(pos)->member)), typeof(*(pos)), member)) |
483 | 480 | ||
484 | /** |
481 | /** |
485 | * hlist_for_each_entry_rcu_notrace - iterate over rcu list of given type (for tracing) |
482 | * hlist_for_each_entry_rcu_notrace - iterate over rcu list of given type (for tracing) |
486 | * @pos: the type * to use as a loop cursor. |
483 | * @pos: the type * to use as a loop cursor. |
487 | * @head: the head for your list. |
484 | * @head: the head for your list. |
488 | * @member: the name of the hlist_node within the struct. |
485 | * @member: the name of the hlist_node within the struct. |
489 | * |
486 | * |
490 | * This list-traversal primitive may safely run concurrently with |
487 | * This list-traversal primitive may safely run concurrently with |
491 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
488 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
492 | * as long as the traversal is guarded by rcu_read_lock(). |
489 | * as long as the traversal is guarded by rcu_read_lock(). |
493 | * |
490 | * |
494 | * This is the same as hlist_for_each_entry_rcu() except that it does |
491 | * This is the same as hlist_for_each_entry_rcu() except that it does |
495 | * not do any RCU debugging or tracing. |
492 | * not do any RCU debugging or tracing. |
496 | */ |
493 | */ |
497 | #define hlist_for_each_entry_rcu_notrace(pos, head, member) \ |
494 | #define hlist_for_each_entry_rcu_notrace(pos, head, member) \ |
498 | for (pos = hlist_entry_safe (rcu_dereference_raw_notrace(hlist_first_rcu(head)),\ |
495 | for (pos = hlist_entry_safe (rcu_dereference_raw_notrace(hlist_first_rcu(head)),\ |
499 | typeof(*(pos)), member); \ |
496 | typeof(*(pos)), member); \ |
500 | pos; \ |
497 | pos; \ |
501 | pos = hlist_entry_safe(rcu_dereference_raw_notrace(hlist_next_rcu(\ |
498 | pos = hlist_entry_safe(rcu_dereference_raw_notrace(hlist_next_rcu(\ |
502 | &(pos)->member)), typeof(*(pos)), member)) |
499 | &(pos)->member)), typeof(*(pos)), member)) |
503 | 500 | ||
504 | /** |
501 | /** |
505 | * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type |
502 | * hlist_for_each_entry_rcu_bh - iterate over rcu list of given type |
506 | * @pos: the type * to use as a loop cursor. |
503 | * @pos: the type * to use as a loop cursor. |
507 | * @head: the head for your list. |
504 | * @head: the head for your list. |
508 | * @member: the name of the hlist_node within the struct. |
505 | * @member: the name of the hlist_node within the struct. |
509 | * |
506 | * |
510 | * This list-traversal primitive may safely run concurrently with |
507 | * This list-traversal primitive may safely run concurrently with |
511 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
508 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
512 | * as long as the traversal is guarded by rcu_read_lock(). |
509 | * as long as the traversal is guarded by rcu_read_lock(). |
513 | */ |
510 | */ |
514 | #define hlist_for_each_entry_rcu_bh(pos, head, member) \ |
511 | #define hlist_for_each_entry_rcu_bh(pos, head, member) \ |
515 | for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_first_rcu(head)),\ |
512 | for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_first_rcu(head)),\ |
516 | typeof(*(pos)), member); \ |
513 | typeof(*(pos)), member); \ |
517 | pos; \ |
514 | pos; \ |
518 | pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu(\ |
515 | pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu(\ |
519 | &(pos)->member)), typeof(*(pos)), member)) |
516 | &(pos)->member)), typeof(*(pos)), member)) |
520 | 517 | ||
521 | /** |
518 | /** |
522 | * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point |
519 | * hlist_for_each_entry_continue_rcu - iterate over a hlist continuing after current point |
523 | * @pos: the type * to use as a loop cursor. |
520 | * @pos: the type * to use as a loop cursor. |
524 | * @member: the name of the hlist_node within the struct. |
521 | * @member: the name of the hlist_node within the struct. |
525 | */ |
522 | */ |
526 | #define hlist_for_each_entry_continue_rcu(pos, member) \ |
523 | #define hlist_for_each_entry_continue_rcu(pos, member) \ |
527 | for (pos = hlist_entry_safe(rcu_dereference((pos)->member.next),\ |
524 | for (pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \ |
528 | typeof(*(pos)), member); \ |
525 | &(pos)->member)), typeof(*(pos)), member); \ |
529 | pos; \ |
526 | pos; \ |
530 | pos = hlist_entry_safe(rcu_dereference((pos)->member.next),\ |
527 | pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \ |
531 | typeof(*(pos)), member)) |
528 | &(pos)->member)), typeof(*(pos)), member)) |
532 | 529 | ||
533 | /** |
530 | /** |
534 | * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point |
531 | * hlist_for_each_entry_continue_rcu_bh - iterate over a hlist continuing after current point |
535 | * @pos: the type * to use as a loop cursor. |
532 | * @pos: the type * to use as a loop cursor. |
536 | * @member: the name of the hlist_node within the struct. |
533 | * @member: the name of the hlist_node within the struct. |
537 | */ |
534 | */ |
538 | #define hlist_for_each_entry_continue_rcu_bh(pos, member) \ |
535 | #define hlist_for_each_entry_continue_rcu_bh(pos, member) \ |
539 | for (pos = hlist_entry_safe(rcu_dereference_bh((pos)->member.next),\ |
536 | for (pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \ |
540 | typeof(*(pos)), member); \ |
537 | &(pos)->member)), typeof(*(pos)), member); \ |
541 | pos; \ |
538 | pos; \ |
542 | pos = hlist_entry_safe(rcu_dereference_bh((pos)->member.next),\ |
539 | pos = hlist_entry_safe(rcu_dereference_bh(hlist_next_rcu( \ |
543 | typeof(*(pos)), member)) |
540 | &(pos)->member)), typeof(*(pos)), member)) |
544 | 541 | ||
545 | /** |
542 | /** |
546 | * hlist_for_each_entry_from_rcu - iterate over a hlist continuing from current point |
543 | * hlist_for_each_entry_from_rcu - iterate over a hlist continuing from current point |
547 | * @pos: the type * to use as a loop cursor. |
544 | * @pos: the type * to use as a loop cursor. |
548 | * @member: the name of the hlist_node within the struct. |
545 | * @member: the name of the hlist_node within the struct. |
549 | */ |
546 | */ |
550 | #define hlist_for_each_entry_from_rcu(pos, member) \ |
547 | #define hlist_for_each_entry_from_rcu(pos, member) \ |
551 | for (; pos; \ |
548 | for (; pos; \ |
552 | pos = hlist_entry_safe(rcu_dereference((pos)->member.next),\ |
549 | pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu( \ |
553 | typeof(*(pos)), member)) |
550 | &(pos)->member)), typeof(*(pos)), member)) |
554 | 551 | ||
555 | #endif /* __KERNEL__ */ |
552 | #endif /* __KERNEL__ */ |
556 | #endif |
553 | #endif |