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