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