Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006, Red Hat, Inc., Dave Jones |
| 3 | * Released under the General Public License (GPL). |
| 4 | * |
| 5 | * This file contains the linked list implementations for |
| 6 | * DEBUG_LIST. |
| 7 | */ |
| 8 | |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 10 | #include <linux/list.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 11 | #include <linux/bug.h> |
Paul Gortmaker | b116ee4 | 2012-01-20 18:46:49 -0500 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Dave Jones | 559f9ba | 2012-03-14 22:17:39 -0400 | [diff] [blame] | 13 | #include <linux/rculist.h> |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 14 | |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 15 | static struct list_head force_poison; |
| 16 | void list_force_poison(struct list_head *entry) |
| 17 | { |
| 18 | entry->next = &force_poison; |
| 19 | entry->prev = &force_poison; |
| 20 | } |
| 21 | |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Insert a new entry between two known consecutive entries. |
| 24 | * |
| 25 | * This is only for internal list manipulation where we know |
| 26 | * the prev/next entries already! |
| 27 | */ |
| 28 | |
| 29 | void __list_add(struct list_head *new, |
| 30 | struct list_head *prev, |
| 31 | struct list_head *next) |
| 32 | { |
Dan Williams | 5c2c258 | 2016-01-15 16:56:49 -0800 | [diff] [blame] | 33 | WARN(new->next == &force_poison || new->prev == &force_poison, |
| 34 | "list_add attempted on force-poisoned entry\n"); |
Dave Jones | 924d9ad | 2008-07-25 01:45:55 -0700 | [diff] [blame] | 35 | WARN(next->prev != prev, |
| 36 | "list_add corruption. next->prev should be " |
| 37 | "prev (%p), but was %p. (next=%p).\n", |
| 38 | prev, next->prev, next); |
| 39 | WARN(prev->next != next, |
| 40 | "list_add corruption. prev->next should be " |
| 41 | "next (%p), but was %p. (prev=%p).\n", |
| 42 | next, prev->next, prev); |
Chris Metcalf | 17a801f | 2012-05-29 15:07:31 -0700 | [diff] [blame] | 43 | WARN(new == prev || new == next, |
| 44 | "list_add double add: new=%p, prev=%p, next=%p.\n", |
| 45 | new, prev, next); |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 46 | next->prev = new; |
| 47 | new->next = next; |
| 48 | new->prev = prev; |
Paul E. McKenney | 1c97be6 | 2015-09-20 22:02:17 -0700 | [diff] [blame] | 49 | WRITE_ONCE(prev->next, new); |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 50 | } |
| 51 | EXPORT_SYMBOL(__list_add); |
| 52 | |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 53 | void __list_del_entry(struct list_head *entry) |
| 54 | { |
| 55 | struct list_head *prev, *next; |
| 56 | |
| 57 | prev = entry->prev; |
| 58 | next = entry->next; |
| 59 | |
| 60 | if (WARN(next == LIST_POISON1, |
| 61 | "list_del corruption, %p->next is LIST_POISON1 (%p)\n", |
| 62 | entry, LIST_POISON1) || |
| 63 | WARN(prev == LIST_POISON2, |
| 64 | "list_del corruption, %p->prev is LIST_POISON2 (%p)\n", |
| 65 | entry, LIST_POISON2) || |
| 66 | WARN(prev->next != entry, |
| 67 | "list_del corruption. prev->next should be %p, " |
| 68 | "but was %p\n", entry, prev->next) || |
| 69 | WARN(next->prev != entry, |
| 70 | "list_del corruption. next->prev should be %p, " |
| 71 | "but was %p\n", entry, next->prev)) |
| 72 | return; |
| 73 | |
| 74 | __list_del(prev, next); |
| 75 | } |
| 76 | EXPORT_SYMBOL(__list_del_entry); |
| 77 | |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 78 | /** |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 79 | * list_del - deletes entry from list. |
| 80 | * @entry: the element to delete from the list. |
| 81 | * Note: list_empty on entry does not return true after this, the entry is |
| 82 | * in an undefined state. |
| 83 | */ |
| 84 | void list_del(struct list_head *entry) |
| 85 | { |
Linus Torvalds | 3c18d4d | 2011-02-18 11:32:28 -0800 | [diff] [blame] | 86 | __list_del_entry(entry); |
Dave Jones | 199a9af | 2006-09-29 01:59:00 -0700 | [diff] [blame] | 87 | entry->next = LIST_POISON1; |
| 88 | entry->prev = LIST_POISON2; |
| 89 | } |
| 90 | EXPORT_SYMBOL(list_del); |
Dave Jones | 559f9ba | 2012-03-14 22:17:39 -0400 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * RCU variants. |
| 94 | */ |
| 95 | void __list_add_rcu(struct list_head *new, |
| 96 | struct list_head *prev, struct list_head *next) |
| 97 | { |
| 98 | WARN(next->prev != prev, |
Paul E. McKenney | 5cf05ad | 2012-05-17 15:12:45 -0700 | [diff] [blame] | 99 | "list_add_rcu corruption. next->prev should be prev (%p), but was %p. (next=%p).\n", |
Dave Jones | 559f9ba | 2012-03-14 22:17:39 -0400 | [diff] [blame] | 100 | prev, next->prev, next); |
| 101 | WARN(prev->next != next, |
Paul E. McKenney | 5cf05ad | 2012-05-17 15:12:45 -0700 | [diff] [blame] | 102 | "list_add_rcu corruption. prev->next should be next (%p), but was %p. (prev=%p).\n", |
Dave Jones | 559f9ba | 2012-03-14 22:17:39 -0400 | [diff] [blame] | 103 | next, prev->next, prev); |
| 104 | new->next = next; |
| 105 | new->prev = prev; |
| 106 | rcu_assign_pointer(list_next_rcu(prev), new); |
| 107 | next->prev = new; |
| 108 | } |
| 109 | EXPORT_SYMBOL(__list_add_rcu); |