Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 1 | #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-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 10 | #include <linux/rcupdate.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 11 | |
| 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 | */ |
| 18 | static 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-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 23 | rcu_assign_pointer(prev->next, new); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 24 | next->prev = new; |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 25 | } |
| 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 | */ |
| 43 | static 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 | */ |
| 64 | static 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 | */ |
| 94 | static 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 | /** |
| 101 | * list_replace_rcu - replace old entry by new one |
| 102 | * @old : the element to be replaced |
| 103 | * @new : the new element to insert |
| 104 | * |
| 105 | * The @old entry will be replaced with the @new entry atomically. |
| 106 | * Note: @old should not be empty. |
| 107 | */ |
| 108 | static inline void list_replace_rcu(struct list_head *old, |
| 109 | struct list_head *new) |
| 110 | { |
| 111 | new->next = old->next; |
| 112 | new->prev = old->prev; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 113 | rcu_assign_pointer(new->prev->next, new); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 114 | new->next->prev = new; |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 115 | old->prev = LIST_POISON2; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * list_splice_init_rcu - splice an RCU-protected list into an existing list. |
| 120 | * @list: the RCU-protected list to splice |
| 121 | * @head: the place in the list to splice the first list into |
| 122 | * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... |
| 123 | * |
| 124 | * @head can be RCU-read traversed concurrently with this function. |
| 125 | * |
| 126 | * Note that this function blocks. |
| 127 | * |
| 128 | * Important note: the caller must take whatever action is necessary to |
| 129 | * prevent any other updates to @head. In principle, it is possible |
| 130 | * to modify the list as soon as sync() begins execution. |
| 131 | * If this sort of thing becomes necessary, an alternative version |
| 132 | * based on call_rcu() could be created. But only if -really- |
| 133 | * needed -- there is no shortage of RCU API members. |
| 134 | */ |
| 135 | static inline void list_splice_init_rcu(struct list_head *list, |
| 136 | struct list_head *head, |
| 137 | void (*sync)(void)) |
| 138 | { |
| 139 | struct list_head *first = list->next; |
| 140 | struct list_head *last = list->prev; |
| 141 | struct list_head *at = head->next; |
| 142 | |
| 143 | if (list_empty(head)) |
| 144 | return; |
| 145 | |
| 146 | /* "first" and "last" tracking list, so initialize it. */ |
| 147 | |
| 148 | INIT_LIST_HEAD(list); |
| 149 | |
| 150 | /* |
| 151 | * At this point, the list body still points to the source list. |
| 152 | * Wait for any readers to finish using the list before splicing |
| 153 | * the list body into the new list. Any new readers will see |
| 154 | * an empty list. |
| 155 | */ |
| 156 | |
| 157 | sync(); |
| 158 | |
| 159 | /* |
| 160 | * Readers are finished with the source list, so perform splice. |
| 161 | * The order is important if the new list is global and accessible |
| 162 | * to concurrent RCU readers. Note that RCU readers are not |
| 163 | * permitted to traverse the prev pointers without excluding |
| 164 | * this function. |
| 165 | */ |
| 166 | |
| 167 | last->next = at; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 168 | rcu_assign_pointer(head->next, first); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 169 | first->prev = head; |
| 170 | at->prev = last; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * list_for_each_rcu - iterate over an rcu-protected list |
| 175 | * @pos: the &struct list_head to use as a loop cursor. |
| 176 | * @head: the head for your list. |
| 177 | * |
| 178 | * This list-traversal primitive may safely run concurrently with |
| 179 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 180 | * as long as the traversal is guarded by rcu_read_lock(). |
| 181 | */ |
| 182 | #define list_for_each_rcu(pos, head) \ |
| 183 | for (pos = (head)->next; \ |
| 184 | prefetch(rcu_dereference(pos)->next), pos != (head); \ |
| 185 | pos = pos->next) |
| 186 | |
| 187 | #define __list_for_each_rcu(pos, head) \ |
| 188 | for (pos = (head)->next; \ |
| 189 | rcu_dereference(pos) != (head); \ |
| 190 | pos = pos->next) |
| 191 | |
| 192 | /** |
| 193 | * list_for_each_safe_rcu |
| 194 | * @pos: the &struct list_head to use as a loop cursor. |
| 195 | * @n: another &struct list_head to use as temporary storage |
| 196 | * @head: the head for your list. |
| 197 | * |
| 198 | * Iterate over an rcu-protected list, safe against removal of list entry. |
| 199 | * |
| 200 | * This list-traversal primitive may safely run concurrently with |
| 201 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 202 | * as long as the traversal is guarded by rcu_read_lock(). |
| 203 | */ |
| 204 | #define list_for_each_safe_rcu(pos, n, head) \ |
| 205 | for (pos = (head)->next; \ |
| 206 | n = rcu_dereference(pos)->next, pos != (head); \ |
| 207 | pos = n) |
| 208 | |
| 209 | /** |
| 210 | * list_for_each_entry_rcu - iterate over rcu list of given type |
| 211 | * @pos: the type * to use as a loop cursor. |
| 212 | * @head: the head for your list. |
| 213 | * @member: the name of the list_struct within the struct. |
| 214 | * |
| 215 | * This list-traversal primitive may safely run concurrently with |
| 216 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 217 | * as long as the traversal is guarded by rcu_read_lock(). |
| 218 | */ |
| 219 | #define list_for_each_entry_rcu(pos, head, member) \ |
| 220 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 221 | prefetch(rcu_dereference(pos)->member.next), \ |
| 222 | &pos->member != (head); \ |
| 223 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * list_for_each_continue_rcu |
| 228 | * @pos: the &struct list_head to use as a loop cursor. |
| 229 | * @head: the head for your list. |
| 230 | * |
| 231 | * Iterate over an rcu-protected list, continuing after current point. |
| 232 | * |
| 233 | * This list-traversal primitive may safely run concurrently with |
| 234 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 235 | * as long as the traversal is guarded by rcu_read_lock(). |
| 236 | */ |
| 237 | #define list_for_each_continue_rcu(pos, head) \ |
| 238 | for ((pos) = (pos)->next; \ |
| 239 | prefetch(rcu_dereference((pos))->next), (pos) != (head); \ |
| 240 | (pos) = (pos)->next) |
| 241 | |
| 242 | /** |
| 243 | * hlist_del_rcu - deletes entry from hash list without re-initialization |
| 244 | * @n: the element to delete from the hash list. |
| 245 | * |
| 246 | * Note: list_unhashed() on entry does not return true after this, |
| 247 | * the entry is in an undefined state. It is useful for RCU based |
| 248 | * lockfree traversal. |
| 249 | * |
| 250 | * In particular, it means that we can not poison the forward |
| 251 | * pointers that may still be used for walking the hash list. |
| 252 | * |
| 253 | * The caller must take whatever precautions are necessary |
| 254 | * (such as holding appropriate locks) to avoid racing |
| 255 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 256 | * or hlist_del_rcu(), running on this same list. |
| 257 | * However, it is perfectly legal to run concurrently with |
| 258 | * the _rcu list-traversal primitives, such as |
| 259 | * hlist_for_each_entry(). |
| 260 | */ |
| 261 | static inline void hlist_del_rcu(struct hlist_node *n) |
| 262 | { |
| 263 | __hlist_del(n); |
| 264 | n->pprev = LIST_POISON2; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * hlist_replace_rcu - replace old entry by new one |
| 269 | * @old : the element to be replaced |
| 270 | * @new : the new element to insert |
| 271 | * |
| 272 | * The @old entry will be replaced with the @new entry atomically. |
| 273 | */ |
| 274 | static inline void hlist_replace_rcu(struct hlist_node *old, |
| 275 | struct hlist_node *new) |
| 276 | { |
| 277 | struct hlist_node *next = old->next; |
| 278 | |
| 279 | new->next = next; |
| 280 | new->pprev = old->pprev; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 281 | rcu_assign_pointer(*new->pprev, new); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 282 | if (next) |
| 283 | new->next->pprev = &new->next; |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 284 | old->pprev = LIST_POISON2; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * hlist_add_head_rcu |
| 289 | * @n: the element to add to the hash list. |
| 290 | * @h: the list to add to. |
| 291 | * |
| 292 | * Description: |
| 293 | * Adds the specified element to the specified hlist, |
| 294 | * while permitting racing traversals. |
| 295 | * |
| 296 | * The caller must take whatever precautions are necessary |
| 297 | * (such as holding appropriate locks) to avoid racing |
| 298 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 299 | * or hlist_del_rcu(), running on this same list. |
| 300 | * However, it is perfectly legal to run concurrently with |
| 301 | * the _rcu list-traversal primitives, such as |
| 302 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
| 303 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
| 304 | * list-traversal primitive must be guarded by rcu_read_lock(). |
| 305 | */ |
| 306 | static inline void hlist_add_head_rcu(struct hlist_node *n, |
| 307 | struct hlist_head *h) |
| 308 | { |
| 309 | struct hlist_node *first = h->first; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 310 | |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 311 | n->next = first; |
| 312 | n->pprev = &h->first; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 313 | rcu_assign_pointer(h->first, n); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 314 | if (first) |
| 315 | first->pprev = &n->next; |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * hlist_add_before_rcu |
| 320 | * @n: the new element to add to the hash list. |
| 321 | * @next: the existing element to add the new element before. |
| 322 | * |
| 323 | * Description: |
| 324 | * Adds the specified element to the specified hlist |
| 325 | * before the specified node while permitting racing traversals. |
| 326 | * |
| 327 | * The caller must take whatever precautions are necessary |
| 328 | * (such as holding appropriate locks) to avoid racing |
| 329 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 330 | * or hlist_del_rcu(), running on this same list. |
| 331 | * However, it is perfectly legal to run concurrently with |
| 332 | * the _rcu list-traversal primitives, such as |
| 333 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
| 334 | * problems on Alpha CPUs. |
| 335 | */ |
| 336 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
| 337 | struct hlist_node *next) |
| 338 | { |
| 339 | n->pprev = next->pprev; |
| 340 | n->next = next; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 341 | rcu_assign_pointer(*(n->pprev), n); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 342 | next->pprev = &n->next; |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
| 346 | * hlist_add_after_rcu |
| 347 | * @prev: the existing element to add the new element after. |
| 348 | * @n: the new element to add to the hash list. |
| 349 | * |
| 350 | * Description: |
| 351 | * Adds the specified element to the specified hlist |
| 352 | * after the specified node while permitting racing traversals. |
| 353 | * |
| 354 | * The caller must take whatever precautions are necessary |
| 355 | * (such as holding appropriate locks) to avoid racing |
| 356 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 357 | * or hlist_del_rcu(), running on this same list. |
| 358 | * However, it is perfectly legal to run concurrently with |
| 359 | * the _rcu list-traversal primitives, such as |
| 360 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
| 361 | * problems on Alpha CPUs. |
| 362 | */ |
| 363 | static inline void hlist_add_after_rcu(struct hlist_node *prev, |
| 364 | struct hlist_node *n) |
| 365 | { |
| 366 | n->next = prev->next; |
| 367 | n->pprev = &prev->next; |
Franck Bui-Huu | 10aa9d2 | 2008-05-12 21:21:06 +0200 | [diff] [blame] | 368 | rcu_assign_pointer(prev->next, n); |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 369 | if (n->next) |
| 370 | n->next->pprev = &n->next; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * hlist_for_each_entry_rcu - iterate over rcu list of given type |
| 375 | * @tpos: the type * to use as a loop cursor. |
| 376 | * @pos: the &struct hlist_node to use as a loop cursor. |
| 377 | * @head: the head for your list. |
| 378 | * @member: the name of the hlist_node within the struct. |
| 379 | * |
| 380 | * This list-traversal primitive may safely run concurrently with |
| 381 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
| 382 | * as long as the traversal is guarded by rcu_read_lock(). |
| 383 | */ |
| 384 | #define hlist_for_each_entry_rcu(tpos, pos, head, member) \ |
| 385 | for (pos = (head)->first; \ |
| 386 | rcu_dereference(pos) && ({ prefetch(pos->next); 1; }) && \ |
| 387 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1; }); \ |
| 388 | pos = pos->next) |
| 389 | |
| 390 | #endif /* __KERNEL__ */ |
| 391 | #endif |