Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_LIST_H |
| 2 | #define _LINUX_LIST_H |
| 3 | |
| 4 | #ifdef __KERNEL__ |
| 5 | |
| 6 | #include <linux/stddef.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 7 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/prefetch.h> |
| 9 | #include <asm/system.h> |
| 10 | |
| 11 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Simple doubly linked list implementation. |
| 13 | * |
| 14 | * Some of the internal functions ("__xxx") are useful when |
| 15 | * manipulating whole lists rather than single entries, as |
| 16 | * sometimes we already know the next/prev entries and we can |
| 17 | * generate better code by using them directly rather than |
| 18 | * using the generic single-entry routines. |
| 19 | */ |
| 20 | |
| 21 | struct list_head { |
| 22 | struct list_head *next, *prev; |
| 23 | }; |
| 24 | |
| 25 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 26 | |
| 27 | #define LIST_HEAD(name) \ |
| 28 | struct list_head name = LIST_HEAD_INIT(name) |
| 29 | |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 30 | static inline void INIT_LIST_HEAD(struct list_head *list) |
| 31 | { |
| 32 | list->next = list; |
| 33 | list->prev = list; |
| 34 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Insert a new entry between two known consecutive entries. |
| 38 | * |
| 39 | * This is only for internal list manipulation where we know |
| 40 | * the prev/next entries already! |
| 41 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 42 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static inline void __list_add(struct list_head *new, |
| 44 | struct list_head *prev, |
| 45 | struct list_head *next) |
| 46 | { |
| 47 | next->prev = new; |
| 48 | new->next = next; |
| 49 | new->prev = prev; |
| 50 | prev->next = new; |
| 51 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 52 | #else |
| 53 | extern void __list_add(struct list_head *new, |
| 54 | struct list_head *prev, |
| 55 | struct list_head *next); |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * list_add - add a new entry |
| 60 | * @new: new entry to be added |
| 61 | * @head: list head to add it after |
| 62 | * |
| 63 | * Insert a new entry after the specified head. |
| 64 | * This is good for implementing stacks. |
| 65 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 66 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static inline void list_add(struct list_head *new, struct list_head *head) |
| 68 | { |
| 69 | __list_add(new, head, head->next); |
| 70 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 71 | #else |
| 72 | extern void list_add(struct list_head *new, struct list_head *head); |
| 73 | #endif |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * list_add_tail - add a new entry |
| 78 | * @new: new entry to be added |
| 79 | * @head: list head to add it before |
| 80 | * |
| 81 | * Insert a new entry before the specified head. |
| 82 | * This is useful for implementing queues. |
| 83 | */ |
| 84 | static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 85 | { |
| 86 | __list_add(new, head->prev, head); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Insert a new entry between two known consecutive entries. |
| 91 | * |
| 92 | * This is only for internal list manipulation where we know |
| 93 | * the prev/next entries already! |
| 94 | */ |
| 95 | static inline void __list_add_rcu(struct list_head * new, |
| 96 | struct list_head * prev, struct list_head * next) |
| 97 | { |
| 98 | new->next = next; |
| 99 | new->prev = prev; |
| 100 | smp_wmb(); |
| 101 | next->prev = new; |
| 102 | prev->next = new; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * list_add_rcu - add a new entry to rcu-protected list |
| 107 | * @new: new entry to be added |
| 108 | * @head: list head to add it after |
| 109 | * |
| 110 | * Insert a new entry after the specified head. |
| 111 | * This is good for implementing stacks. |
| 112 | * |
| 113 | * The caller must take whatever precautions are necessary |
| 114 | * (such as holding appropriate locks) to avoid racing |
| 115 | * with another list-mutation primitive, such as list_add_rcu() |
| 116 | * or list_del_rcu(), running on this same list. |
| 117 | * However, it is perfectly legal to run concurrently with |
| 118 | * the _rcu list-traversal primitives, such as |
| 119 | * list_for_each_entry_rcu(). |
| 120 | */ |
| 121 | static inline void list_add_rcu(struct list_head *new, struct list_head *head) |
| 122 | { |
| 123 | __list_add_rcu(new, head, head->next); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * list_add_tail_rcu - add a new entry to rcu-protected list |
| 128 | * @new: new entry to be added |
| 129 | * @head: list head to add it before |
| 130 | * |
| 131 | * Insert a new entry before the specified head. |
| 132 | * This is useful for implementing queues. |
| 133 | * |
| 134 | * The caller must take whatever precautions are necessary |
| 135 | * (such as holding appropriate locks) to avoid racing |
| 136 | * with another list-mutation primitive, such as list_add_tail_rcu() |
| 137 | * or list_del_rcu(), running on this same list. |
| 138 | * However, it is perfectly legal to run concurrently with |
| 139 | * the _rcu list-traversal primitives, such as |
| 140 | * list_for_each_entry_rcu(). |
| 141 | */ |
| 142 | static inline void list_add_tail_rcu(struct list_head *new, |
| 143 | struct list_head *head) |
| 144 | { |
| 145 | __list_add_rcu(new, head->prev, head); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Delete a list entry by making the prev/next entries |
| 150 | * point to each other. |
| 151 | * |
| 152 | * This is only for internal list manipulation where we know |
| 153 | * the prev/next entries already! |
| 154 | */ |
| 155 | static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 156 | { |
| 157 | next->prev = prev; |
| 158 | prev->next = next; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * list_del - deletes entry from list. |
| 163 | * @entry: the element to delete from the list. |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 164 | * Note: list_empty() on entry does not return true after this, the entry is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * in an undefined state. |
| 166 | */ |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 167 | #ifndef CONFIG_DEBUG_LIST |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | static inline void list_del(struct list_head *entry) |
| 169 | { |
| 170 | __list_del(entry->prev, entry->next); |
| 171 | entry->next = LIST_POISON1; |
| 172 | entry->prev = LIST_POISON2; |
| 173 | } |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 174 | #else |
| 175 | extern void list_del(struct list_head *entry); |
| 176 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * list_del_rcu - deletes entry from list without re-initialization |
| 180 | * @entry: the element to delete from the list. |
| 181 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 182 | * Note: list_empty() on entry does not return true after this, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * the entry is in an undefined state. It is useful for RCU based |
| 184 | * lockfree traversal. |
| 185 | * |
| 186 | * In particular, it means that we can not poison the forward |
| 187 | * pointers that may still be used for walking the list. |
| 188 | * |
| 189 | * The caller must take whatever precautions are necessary |
| 190 | * (such as holding appropriate locks) to avoid racing |
| 191 | * with another list-mutation primitive, such as list_del_rcu() |
| 192 | * or list_add_rcu(), running on this same list. |
| 193 | * However, it is perfectly legal to run concurrently with |
| 194 | * the _rcu list-traversal primitives, such as |
| 195 | * list_for_each_entry_rcu(). |
| 196 | * |
| 197 | * Note that the caller is not permitted to immediately free |
Paul E. McKenney | b2b1866 | 2005-06-25 14:55:38 -0700 | [diff] [blame] | 198 | * the newly deleted entry. Instead, either synchronize_rcu() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | * or call_rcu() must be used to defer freeing until an RCU |
| 200 | * grace period has elapsed. |
| 201 | */ |
| 202 | static inline void list_del_rcu(struct list_head *entry) |
| 203 | { |
| 204 | __list_del(entry->prev, entry->next); |
| 205 | entry->prev = LIST_POISON2; |
| 206 | } |
| 207 | |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 208 | /** |
| 209 | * list_replace - replace old entry by new one |
| 210 | * @old : the element to be replaced |
| 211 | * @new : the new element to insert |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 212 | * |
| 213 | * If @old was empty, it will be overwritten. |
Oleg Nesterov | 54e7377 | 2006-06-23 02:05:54 -0700 | [diff] [blame] | 214 | */ |
| 215 | static inline void list_replace(struct list_head *old, |
| 216 | struct list_head *new) |
| 217 | { |
| 218 | new->next = old->next; |
| 219 | new->next->prev = new; |
| 220 | new->prev = old->prev; |
| 221 | new->prev->next = new; |
| 222 | } |
| 223 | |
| 224 | static inline void list_replace_init(struct list_head *old, |
| 225 | struct list_head *new) |
| 226 | { |
| 227 | list_replace(old, new); |
| 228 | INIT_LIST_HEAD(old); |
| 229 | } |
| 230 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 231 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | * list_replace_rcu - replace old entry by new one |
| 233 | * @old : the element to be replaced |
| 234 | * @new : the new element to insert |
| 235 | * |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 236 | * The @old entry will be replaced with the @new entry atomically. |
| 237 | * Note: @old should not be empty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | */ |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame] | 239 | static inline void list_replace_rcu(struct list_head *old, |
| 240 | struct list_head *new) |
| 241 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | new->next = old->next; |
| 243 | new->prev = old->prev; |
| 244 | smp_wmb(); |
| 245 | new->next->prev = new; |
| 246 | new->prev->next = new; |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame] | 247 | old->prev = LIST_POISON2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * list_del_init - deletes entry from list and reinitialize it. |
| 252 | * @entry: the element to delete from the list. |
| 253 | */ |
| 254 | static inline void list_del_init(struct list_head *entry) |
| 255 | { |
| 256 | __list_del(entry->prev, entry->next); |
| 257 | INIT_LIST_HEAD(entry); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * list_move - delete from one list and add as another's head |
| 262 | * @list: the entry to move |
| 263 | * @head: the head that will precede our entry |
| 264 | */ |
| 265 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 266 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 267 | __list_del(list->prev, list->next); |
| 268 | list_add(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * list_move_tail - delete from one list and add as another's tail |
| 273 | * @list: the entry to move |
| 274 | * @head: the head that will follow our entry |
| 275 | */ |
| 276 | static inline void list_move_tail(struct list_head *list, |
| 277 | struct list_head *head) |
| 278 | { |
Daniel Walker | 78db2ad | 2007-05-12 16:28:35 -0700 | [diff] [blame] | 279 | __list_del(list->prev, list->next); |
| 280 | list_add_tail(list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Shailabh Nagar | e8f4d97 | 2006-07-14 00:24:35 -0700 | [diff] [blame] | 284 | * list_is_last - tests whether @list is the last entry in list @head |
| 285 | * @list: the entry to test |
| 286 | * @head: the head of the list |
| 287 | */ |
| 288 | static inline int list_is_last(const struct list_head *list, |
| 289 | const struct list_head *head) |
| 290 | { |
| 291 | return list->next == head; |
| 292 | } |
| 293 | |
| 294 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * list_empty - tests whether a list is empty |
| 296 | * @head: the list to test. |
| 297 | */ |
| 298 | static inline int list_empty(const struct list_head *head) |
| 299 | { |
| 300 | return head->next == head; |
| 301 | } |
| 302 | |
| 303 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 304 | * list_empty_careful - tests whether a list is empty and not being modified |
| 305 | * @head: the list to test |
| 306 | * |
| 307 | * Description: |
| 308 | * tests whether a list is empty _and_ checks that no other CPU might be |
| 309 | * in the process of modifying either member (next or prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | * |
| 311 | * NOTE: using list_empty_careful() without synchronization |
| 312 | * can only be safe if the only activity that can happen |
| 313 | * to the list entry is list_del_init(). Eg. it cannot be used |
| 314 | * if another CPU could re-list_add() it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | */ |
| 316 | static inline int list_empty_careful(const struct list_head *head) |
| 317 | { |
| 318 | struct list_head *next = head->next; |
| 319 | return (next == head) && (next == head->prev); |
| 320 | } |
| 321 | |
| 322 | static inline void __list_splice(struct list_head *list, |
| 323 | struct list_head *head) |
| 324 | { |
| 325 | struct list_head *first = list->next; |
| 326 | struct list_head *last = list->prev; |
| 327 | struct list_head *at = head->next; |
| 328 | |
| 329 | first->prev = head; |
| 330 | head->next = first; |
| 331 | |
| 332 | last->next = at; |
| 333 | at->prev = last; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * list_splice - join two lists |
| 338 | * @list: the new list to add. |
| 339 | * @head: the place to add it in the first list. |
| 340 | */ |
| 341 | static inline void list_splice(struct list_head *list, struct list_head *head) |
| 342 | { |
| 343 | if (!list_empty(list)) |
| 344 | __list_splice(list, head); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * list_splice_init - join two lists and reinitialise the emptied list. |
| 349 | * @list: the new list to add. |
| 350 | * @head: the place to add it in the first list. |
| 351 | * |
| 352 | * The list at @list is reinitialised |
| 353 | */ |
| 354 | static inline void list_splice_init(struct list_head *list, |
| 355 | struct list_head *head) |
| 356 | { |
| 357 | if (!list_empty(list)) { |
| 358 | __list_splice(list, head); |
| 359 | INIT_LIST_HEAD(list); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /** |
Corey Minyard | 3678d62 | 2007-02-10 01:45:42 -0800 | [diff] [blame] | 364 | * list_splice_init_rcu - splice an RCU-protected list into an existing list. |
| 365 | * @list: the RCU-protected list to splice |
| 366 | * @head: the place in the list to splice the first list into |
| 367 | * @sync: function to sync: synchronize_rcu(), synchronize_sched(), ... |
| 368 | * |
| 369 | * @head can be RCU-read traversed concurrently with this function. |
| 370 | * |
| 371 | * Note that this function blocks. |
| 372 | * |
| 373 | * Important note: the caller must take whatever action is necessary to |
| 374 | * prevent any other updates to @head. In principle, it is possible |
| 375 | * to modify the list as soon as sync() begins execution. |
| 376 | * If this sort of thing becomes necessary, an alternative version |
| 377 | * based on call_rcu() could be created. But only if -really- |
| 378 | * needed -- there is no shortage of RCU API members. |
| 379 | */ |
| 380 | static inline void list_splice_init_rcu(struct list_head *list, |
| 381 | struct list_head *head, |
| 382 | void (*sync)(void)) |
| 383 | { |
| 384 | struct list_head *first = list->next; |
| 385 | struct list_head *last = list->prev; |
| 386 | struct list_head *at = head->next; |
| 387 | |
| 388 | if (list_empty(head)) |
| 389 | return; |
| 390 | |
| 391 | /* "first" and "last" tracking list, so initialize it. */ |
| 392 | |
| 393 | INIT_LIST_HEAD(list); |
| 394 | |
| 395 | /* |
| 396 | * At this point, the list body still points to the source list. |
| 397 | * Wait for any readers to finish using the list before splicing |
| 398 | * the list body into the new list. Any new readers will see |
| 399 | * an empty list. |
| 400 | */ |
| 401 | |
| 402 | sync(); |
| 403 | |
| 404 | /* |
| 405 | * Readers are finished with the source list, so perform splice. |
| 406 | * The order is important if the new list is global and accessible |
| 407 | * to concurrent RCU readers. Note that RCU readers are not |
| 408 | * permitted to traverse the prev pointers without excluding |
| 409 | * this function. |
| 410 | */ |
| 411 | |
| 412 | last->next = at; |
| 413 | smp_wmb(); |
| 414 | head->next = first; |
| 415 | first->prev = head; |
| 416 | at->prev = last; |
| 417 | } |
| 418 | |
| 419 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | * list_entry - get the struct for this entry |
| 421 | * @ptr: the &struct list_head pointer. |
| 422 | * @type: the type of the struct this is embedded in. |
| 423 | * @member: the name of the list_struct within the struct. |
| 424 | */ |
| 425 | #define list_entry(ptr, type, member) \ |
| 426 | container_of(ptr, type, member) |
| 427 | |
| 428 | /** |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 429 | * list_first_entry - get the first element from a list |
| 430 | * @ptr: the list head to take the element from. |
| 431 | * @type: the type of the struct this is embedded in. |
| 432 | * @member: the name of the list_struct within the struct. |
| 433 | * |
| 434 | * Note, that list is expected to be not empty. |
| 435 | */ |
| 436 | #define list_first_entry(ptr, type, member) \ |
| 437 | list_entry((ptr)->next, type, member) |
| 438 | |
| 439 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | * list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 441 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | * @head: the head for your list. |
| 443 | */ |
| 444 | #define list_for_each(pos, head) \ |
| 445 | for (pos = (head)->next; prefetch(pos->next), pos != (head); \ |
| 446 | pos = pos->next) |
| 447 | |
| 448 | /** |
| 449 | * __list_for_each - iterate over a list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 450 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * @head: the head for your list. |
| 452 | * |
| 453 | * This variant differs from list_for_each() in that it's the |
| 454 | * simplest possible list iteration code, no prefetching is done. |
| 455 | * Use this for code that knows the list to be very short (empty |
| 456 | * or 1 entry) most of the time. |
| 457 | */ |
| 458 | #define __list_for_each(pos, head) \ |
| 459 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 460 | |
| 461 | /** |
| 462 | * list_for_each_prev - iterate over a list backwards |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 463 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | * @head: the head for your list. |
| 465 | */ |
| 466 | #define list_for_each_prev(pos, head) \ |
| 467 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ |
| 468 | pos = pos->prev) |
| 469 | |
| 470 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 471 | * list_for_each_safe - iterate over a list safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 472 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * @n: another &struct list_head to use as temporary storage |
| 474 | * @head: the head for your list. |
| 475 | */ |
| 476 | #define list_for_each_safe(pos, n, head) \ |
| 477 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 478 | pos = n, n = pos->next) |
| 479 | |
| 480 | /** |
Denis V. Lunev | 37c4252 | 2007-10-16 23:29:53 -0700 | [diff] [blame] | 481 | * list_for_each_prev_safe - iterate over a list backwards safe against removal |
| 482 | of list entry |
| 483 | * @pos: the &struct list_head to use as a loop cursor. |
| 484 | * @n: another &struct list_head to use as temporary storage |
| 485 | * @head: the head for your list. |
| 486 | */ |
| 487 | #define list_for_each_prev_safe(pos, n, head) \ |
| 488 | for (pos = (head)->prev, n = pos->prev; \ |
| 489 | prefetch(pos->prev), pos != (head); \ |
| 490 | pos = n, n = pos->prev) |
| 491 | |
| 492 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | * list_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 494 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | * @head: the head for your list. |
| 496 | * @member: the name of the list_struct within the struct. |
| 497 | */ |
| 498 | #define list_for_each_entry(pos, head, member) \ |
| 499 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 500 | prefetch(pos->member.next), &pos->member != (head); \ |
| 501 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 502 | |
| 503 | /** |
| 504 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 505 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | * @head: the head for your list. |
| 507 | * @member: the name of the list_struct within the struct. |
| 508 | */ |
| 509 | #define list_for_each_entry_reverse(pos, head, member) \ |
| 510 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ |
| 511 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 512 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 513 | |
| 514 | /** |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 515 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | * @pos: the type * to use as a start point |
| 517 | * @head: the head of the list |
| 518 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 519 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 520 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | */ |
| 522 | #define list_prepare_entry(pos, head, member) \ |
| 523 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
| 524 | |
| 525 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 526 | * list_for_each_entry_continue - continue iteration over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 527 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * @head: the head for your list. |
| 529 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 530 | * |
| 531 | * Continue to iterate over list of given type, continuing after |
| 532 | * the current position. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | */ |
| 534 | #define list_for_each_entry_continue(pos, head, member) \ |
| 535 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ |
| 536 | prefetch(pos->member.next), &pos->member != (head); \ |
| 537 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 538 | |
| 539 | /** |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 540 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
| 541 | * @pos: the type * to use as a loop cursor. |
| 542 | * @head: the head for your list. |
| 543 | * @member: the name of the list_struct within the struct. |
| 544 | * |
| 545 | * Start to iterate over list of given type backwards, continuing after |
| 546 | * the current position. |
| 547 | */ |
| 548 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
| 549 | for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 550 | prefetch(pos->member.prev), &pos->member != (head); \ |
| 551 | pos = list_entry(pos->member.prev, typeof(*pos), member)) |
| 552 | |
| 553 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 554 | * list_for_each_entry_from - iterate over list of given type from the current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 555 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 556 | * @head: the head for your list. |
| 557 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 558 | * |
| 559 | * Iterate over list of given type, continuing from current position. |
Arnaldo Carvalho de Melo | e229c2f | 2006-03-20 17:19:17 -0800 | [diff] [blame] | 560 | */ |
| 561 | #define list_for_each_entry_from(pos, head, member) \ |
| 562 | for (; prefetch(pos->member.next), &pos->member != (head); \ |
| 563 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 564 | |
| 565 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 567 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * @n: another type * to use as temporary storage |
| 569 | * @head: the head for your list. |
| 570 | * @member: the name of the list_struct within the struct. |
| 571 | */ |
| 572 | #define list_for_each_entry_safe(pos, n, head, member) \ |
| 573 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 574 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 575 | &pos->member != (head); \ |
| 576 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 577 | |
| 578 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 579 | * list_for_each_entry_safe_continue |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 580 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 581 | * @n: another type * to use as temporary storage |
| 582 | * @head: the head for your list. |
| 583 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 584 | * |
| 585 | * Iterate over list of given type, continuing after current point, |
| 586 | * safe against removal of list entry. |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 587 | */ |
| 588 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 589 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ |
| 590 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
Arnaldo Carvalho de Melo | 74459dc | 2005-08-09 20:15:51 -0700 | [diff] [blame] | 591 | &pos->member != (head); \ |
| 592 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 593 | |
| 594 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 595 | * list_for_each_entry_safe_from |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 596 | * @pos: the type * to use as a loop cursor. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 597 | * @n: another type * to use as temporary storage |
| 598 | * @head: the head for your list. |
| 599 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 600 | * |
| 601 | * Iterate over list of given type from current point, safe against |
| 602 | * removal of list entry. |
Arnaldo Carvalho de Melo | d8dcffe | 2006-03-20 17:18:05 -0800 | [diff] [blame] | 603 | */ |
| 604 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
| 605 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 606 | &pos->member != (head); \ |
| 607 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 608 | |
| 609 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 610 | * list_for_each_entry_safe_reverse |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 611 | * @pos: the type * to use as a loop cursor. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 612 | * @n: another type * to use as temporary storage |
| 613 | * @head: the head for your list. |
| 614 | * @member: the name of the list_struct within the struct. |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 615 | * |
| 616 | * Iterate backwards over list of given type, safe against removal |
| 617 | * of list entry. |
David Howells | 0ad4235 | 2006-01-09 20:51:31 -0800 | [diff] [blame] | 618 | */ |
| 619 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
| 620 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ |
| 621 | n = list_entry(pos->member.prev, typeof(*pos), member); \ |
| 622 | &pos->member != (head); \ |
| 623 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) |
| 624 | |
| 625 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | * list_for_each_rcu - iterate over an rcu-protected list |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 627 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | * @head: the head for your list. |
| 629 | * |
| 630 | * This list-traversal primitive may safely run concurrently with |
| 631 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 632 | * as long as the traversal is guarded by rcu_read_lock(). |
| 633 | */ |
| 634 | #define list_for_each_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 635 | for (pos = (head)->next; \ |
| 636 | prefetch(rcu_dereference(pos)->next), pos != (head); \ |
| 637 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | #define __list_for_each_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 640 | for (pos = (head)->next; \ |
| 641 | rcu_dereference(pos) != (head); \ |
| 642 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
| 644 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 645 | * list_for_each_safe_rcu |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 646 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | * @n: another &struct list_head to use as temporary storage |
| 648 | * @head: the head for your list. |
| 649 | * |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 650 | * Iterate over an rcu-protected list, safe against removal of list entry. |
| 651 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | * This list-traversal primitive may safely run concurrently with |
| 653 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 654 | * as long as the traversal is guarded by rcu_read_lock(). |
| 655 | */ |
| 656 | #define list_for_each_safe_rcu(pos, n, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 657 | for (pos = (head)->next; \ |
| 658 | n = rcu_dereference(pos)->next, pos != (head); \ |
| 659 | pos = n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | /** |
| 662 | * list_for_each_entry_rcu - iterate over rcu list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 663 | * @pos: the type * to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | * @head: the head for your list. |
| 665 | * @member: the name of the list_struct within the struct. |
| 666 | * |
| 667 | * This list-traversal primitive may safely run concurrently with |
| 668 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 669 | * as long as the traversal is guarded by rcu_read_lock(). |
| 670 | */ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 671 | #define list_for_each_entry_rcu(pos, head, member) \ |
| 672 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
| 673 | prefetch(rcu_dereference(pos)->member.next), \ |
| 674 | &pos->member != (head); \ |
| 675 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
| 677 | |
| 678 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 679 | * list_for_each_continue_rcu |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 680 | * @pos: the &struct list_head to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | * @head: the head for your list. |
| 682 | * |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 683 | * Iterate over an rcu-protected list, continuing after current point. |
| 684 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | * This list-traversal primitive may safely run concurrently with |
| 686 | * the _rcu list-mutation primitives such as list_add_rcu() |
| 687 | * as long as the traversal is guarded by rcu_read_lock(). |
| 688 | */ |
| 689 | #define list_for_each_continue_rcu(pos, head) \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 690 | for ((pos) = (pos)->next; \ |
| 691 | prefetch(rcu_dereference((pos))->next), (pos) != (head); \ |
| 692 | (pos) = (pos)->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * Double linked lists with a single pointer list head. |
| 696 | * Mostly useful for hash tables where the two pointer list head is |
| 697 | * too wasteful. |
| 698 | * You lose the ability to access the tail in O(1). |
| 699 | */ |
| 700 | |
| 701 | struct hlist_head { |
| 702 | struct hlist_node *first; |
| 703 | }; |
| 704 | |
| 705 | struct hlist_node { |
| 706 | struct hlist_node *next, **pprev; |
| 707 | }; |
| 708 | |
| 709 | #define HLIST_HEAD_INIT { .first = NULL } |
| 710 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 711 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
Zach Brown | 490d6ab | 2006-02-03 03:03:56 -0800 | [diff] [blame] | 712 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 713 | { |
| 714 | h->next = NULL; |
| 715 | h->pprev = NULL; |
| 716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 719 | { |
| 720 | return !h->pprev; |
| 721 | } |
| 722 | |
| 723 | static inline int hlist_empty(const struct hlist_head *h) |
| 724 | { |
| 725 | return !h->first; |
| 726 | } |
| 727 | |
| 728 | static inline void __hlist_del(struct hlist_node *n) |
| 729 | { |
| 730 | struct hlist_node *next = n->next; |
| 731 | struct hlist_node **pprev = n->pprev; |
| 732 | *pprev = next; |
| 733 | if (next) |
| 734 | next->pprev = pprev; |
| 735 | } |
| 736 | |
| 737 | static inline void hlist_del(struct hlist_node *n) |
| 738 | { |
| 739 | __hlist_del(n); |
| 740 | n->next = LIST_POISON1; |
| 741 | n->pprev = LIST_POISON2; |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * hlist_del_rcu - deletes entry from hash list without re-initialization |
| 746 | * @n: the element to delete from the hash list. |
| 747 | * |
| 748 | * Note: list_unhashed() on entry does not return true after this, |
| 749 | * the entry is in an undefined state. It is useful for RCU based |
| 750 | * lockfree traversal. |
| 751 | * |
| 752 | * In particular, it means that we can not poison the forward |
| 753 | * pointers that may still be used for walking the hash list. |
| 754 | * |
| 755 | * The caller must take whatever precautions are necessary |
| 756 | * (such as holding appropriate locks) to avoid racing |
| 757 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 758 | * or hlist_del_rcu(), running on this same list. |
| 759 | * However, it is perfectly legal to run concurrently with |
| 760 | * the _rcu list-traversal primitives, such as |
| 761 | * hlist_for_each_entry(). |
| 762 | */ |
| 763 | static inline void hlist_del_rcu(struct hlist_node *n) |
| 764 | { |
| 765 | __hlist_del(n); |
| 766 | n->pprev = LIST_POISON2; |
| 767 | } |
| 768 | |
| 769 | static inline void hlist_del_init(struct hlist_node *n) |
| 770 | { |
Akinobu Mita | da753be | 2006-04-28 15:21:23 -0700 | [diff] [blame] | 771 | if (!hlist_unhashed(n)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | __hlist_del(n); |
| 773 | INIT_HLIST_NODE(n); |
| 774 | } |
| 775 | } |
| 776 | |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 777 | /** |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame] | 778 | * hlist_replace_rcu - replace old entry by new one |
| 779 | * @old : the element to be replaced |
| 780 | * @new : the new element to insert |
| 781 | * |
Robert P. J. Day | 45f8bde | 2007-01-26 00:57:09 -0800 | [diff] [blame] | 782 | * The @old entry will be replaced with the @new entry atomically. |
Ingo Molnar | b88cb42 | 2005-12-12 00:37:11 -0800 | [diff] [blame] | 783 | */ |
| 784 | static inline void hlist_replace_rcu(struct hlist_node *old, |
| 785 | struct hlist_node *new) |
| 786 | { |
| 787 | struct hlist_node *next = old->next; |
| 788 | |
| 789 | new->next = next; |
| 790 | new->pprev = old->pprev; |
| 791 | smp_wmb(); |
| 792 | if (next) |
| 793 | new->next->pprev = &new->next; |
| 794 | *new->pprev = new; |
| 795 | old->pprev = LIST_POISON2; |
| 796 | } |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 799 | { |
| 800 | struct hlist_node *first = h->first; |
| 801 | n->next = first; |
| 802 | if (first) |
| 803 | first->pprev = &n->next; |
| 804 | h->first = n; |
| 805 | n->pprev = &h->first; |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 810 | * hlist_add_head_rcu |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | * @n: the element to add to the hash list. |
| 812 | * @h: the list to add to. |
| 813 | * |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 814 | * Description: |
| 815 | * Adds the specified element to the specified hlist, |
| 816 | * while permitting racing traversals. |
| 817 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | * The caller must take whatever precautions are necessary |
| 819 | * (such as holding appropriate locks) to avoid racing |
| 820 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 821 | * or hlist_del_rcu(), running on this same list. |
| 822 | * However, it is perfectly legal to run concurrently with |
| 823 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 824 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | * problems on Alpha CPUs. Regardless of the type of CPU, the |
| 826 | * list-traversal primitive must be guarded by rcu_read_lock(). |
| 827 | */ |
| 828 | static inline void hlist_add_head_rcu(struct hlist_node *n, |
| 829 | struct hlist_head *h) |
| 830 | { |
| 831 | struct hlist_node *first = h->first; |
| 832 | n->next = first; |
| 833 | n->pprev = &h->first; |
| 834 | smp_wmb(); |
| 835 | if (first) |
| 836 | first->pprev = &n->next; |
| 837 | h->first = n; |
| 838 | } |
| 839 | |
| 840 | /* next must be != NULL */ |
| 841 | static inline void hlist_add_before(struct hlist_node *n, |
| 842 | struct hlist_node *next) |
| 843 | { |
| 844 | n->pprev = next->pprev; |
| 845 | n->next = next; |
| 846 | next->pprev = &n->next; |
| 847 | *(n->pprev) = n; |
| 848 | } |
| 849 | |
| 850 | static inline void hlist_add_after(struct hlist_node *n, |
| 851 | struct hlist_node *next) |
| 852 | { |
| 853 | next->next = n->next; |
| 854 | n->next = next; |
| 855 | next->pprev = &n->next; |
| 856 | |
| 857 | if(next->next) |
| 858 | next->next->pprev = &next->next; |
| 859 | } |
| 860 | |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 861 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 862 | * hlist_add_before_rcu |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 863 | * @n: the new element to add to the hash list. |
| 864 | * @next: the existing element to add the new element before. |
| 865 | * |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 866 | * Description: |
| 867 | * Adds the specified element to the specified hlist |
| 868 | * before the specified node while permitting racing traversals. |
| 869 | * |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 870 | * The caller must take whatever precautions are necessary |
| 871 | * (such as holding appropriate locks) to avoid racing |
| 872 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 873 | * or hlist_del_rcu(), running on this same list. |
| 874 | * However, it is perfectly legal to run concurrently with |
| 875 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 876 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 877 | * problems on Alpha CPUs. |
| 878 | */ |
Robert Olsson | e5b4376 | 2005-08-25 13:01:03 -0700 | [diff] [blame] | 879 | static inline void hlist_add_before_rcu(struct hlist_node *n, |
| 880 | struct hlist_node *next) |
| 881 | { |
| 882 | n->pprev = next->pprev; |
| 883 | n->next = next; |
| 884 | smp_wmb(); |
| 885 | next->pprev = &n->next; |
| 886 | *(n->pprev) = n; |
| 887 | } |
| 888 | |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 889 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 890 | * hlist_add_after_rcu |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 891 | * @prev: the existing element to add the new element after. |
| 892 | * @n: the new element to add to the hash list. |
| 893 | * |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 894 | * Description: |
| 895 | * Adds the specified element to the specified hlist |
| 896 | * after the specified node while permitting racing traversals. |
| 897 | * |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 898 | * The caller must take whatever precautions are necessary |
| 899 | * (such as holding appropriate locks) to avoid racing |
| 900 | * with another list-mutation primitive, such as hlist_add_head_rcu() |
| 901 | * or hlist_del_rcu(), running on this same list. |
| 902 | * However, it is perfectly legal to run concurrently with |
| 903 | * the _rcu list-traversal primitives, such as |
Paul E. McKenney | 665a758 | 2005-11-07 00:59:17 -0800 | [diff] [blame] | 904 | * hlist_for_each_entry_rcu(), used to prevent memory-consistency |
Paul E. McKenney | cf4ef01 | 2005-08-25 16:08:37 -0700 | [diff] [blame] | 905 | * problems on Alpha CPUs. |
| 906 | */ |
Robert Olsson | e5b4376 | 2005-08-25 13:01:03 -0700 | [diff] [blame] | 907 | static inline void hlist_add_after_rcu(struct hlist_node *prev, |
| 908 | struct hlist_node *n) |
| 909 | { |
| 910 | n->next = prev->next; |
| 911 | n->pprev = &prev->next; |
| 912 | smp_wmb(); |
| 913 | prev->next = n; |
| 914 | if (n->next) |
| 915 | n->next->pprev = &n->next; |
| 916 | } |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 919 | |
| 920 | #define hlist_for_each(pos, head) \ |
| 921 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ |
| 922 | pos = pos->next) |
| 923 | |
| 924 | #define hlist_for_each_safe(pos, n, head) \ |
| 925 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 926 | pos = n) |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | /** |
| 929 | * hlist_for_each_entry - iterate over list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 930 | * @tpos: the type * to use as a loop cursor. |
| 931 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | * @head: the head for your list. |
| 933 | * @member: the name of the hlist_node within the struct. |
| 934 | */ |
| 935 | #define hlist_for_each_entry(tpos, pos, head, member) \ |
| 936 | for (pos = (head)->first; \ |
| 937 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 938 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 939 | pos = pos->next) |
| 940 | |
| 941 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 942 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 943 | * @tpos: the type * to use as a loop cursor. |
| 944 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | * @member: the name of the hlist_node within the struct. |
| 946 | */ |
| 947 | #define hlist_for_each_entry_continue(tpos, pos, member) \ |
| 948 | for (pos = (pos)->next; \ |
| 949 | pos && ({ prefetch(pos->next); 1;}) && \ |
| 950 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 951 | pos = pos->next) |
| 952 | |
| 953 | /** |
Randy Dunlap | fe96e57 | 2006-06-25 05:47:42 -0700 | [diff] [blame] | 954 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 955 | * @tpos: the type * to use as a loop cursor. |
| 956 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | * @member: the name of the hlist_node within the struct. |
| 958 | */ |
| 959 | #define hlist_for_each_entry_from(tpos, pos, member) \ |
| 960 | for (; pos && ({ prefetch(pos->next); 1;}) && \ |
| 961 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 962 | pos = pos->next) |
| 963 | |
| 964 | /** |
| 965 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 966 | * @tpos: the type * to use as a loop cursor. |
| 967 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | * @n: another &struct hlist_node to use as temporary storage |
| 969 | * @head: the head for your list. |
| 970 | * @member: the name of the hlist_node within the struct. |
| 971 | */ |
| 972 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
| 973 | for (pos = (head)->first; \ |
| 974 | pos && ({ n = pos->next; 1; }) && \ |
| 975 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 976 | pos = n) |
| 977 | |
| 978 | /** |
| 979 | * hlist_for_each_entry_rcu - iterate over rcu list of given type |
Randy Dunlap | 8e3a67a | 2006-06-25 05:47:43 -0700 | [diff] [blame] | 980 | * @tpos: the type * to use as a loop cursor. |
| 981 | * @pos: the &struct hlist_node to use as a loop cursor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | * @head: the head for your list. |
| 983 | * @member: the name of the hlist_node within the struct. |
| 984 | * |
| 985 | * This list-traversal primitive may safely run concurrently with |
Paul E. McKenney | e1ba0da | 2005-04-16 15:25:51 -0700 | [diff] [blame] | 986 | * the _rcu list-mutation primitives such as hlist_add_head_rcu() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | * as long as the traversal is guarded by rcu_read_lock(). |
| 988 | */ |
| 989 | #define hlist_for_each_entry_rcu(tpos, pos, head, member) \ |
| 990 | for (pos = (head)->first; \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 991 | rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
Herbert Xu | b24d18a | 2005-10-16 20:29:20 -0700 | [diff] [blame] | 993 | pos = pos->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | #else |
| 996 | #warning "don't include kernel headers in userspace" |
| 997 | #endif /* __KERNEL__ */ |
| 998 | #endif |