xref: /openbmc/linux/lib/list_debug.c (revision cc8bbe1a)
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 
9 #include <linux/export.h>
10 #include <linux/list.h>
11 #include <linux/bug.h>
12 #include <linux/kernel.h>
13 #include <linux/rculist.h>
14 
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 
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 {
33 	WARN(new->next == &force_poison || new->prev == &force_poison,
34 		"list_add attempted on force-poisoned entry\n");
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);
43 	WARN(new == prev || new == next,
44 	     "list_add double add: new=%p, prev=%p, next=%p.\n",
45 	     new, prev, next);
46 	next->prev = new;
47 	new->next = next;
48 	new->prev = prev;
49 	WRITE_ONCE(prev->next, new);
50 }
51 EXPORT_SYMBOL(__list_add);
52 
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 
78 /**
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 {
86 	__list_del_entry(entry);
87 	entry->next = LIST_POISON1;
88 	entry->prev = LIST_POISON2;
89 }
90 EXPORT_SYMBOL(list_del);
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,
99 		"list_add_rcu corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
100 		prev, next->prev, next);
101 	WARN(prev->next != next,
102 		"list_add_rcu corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
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);
110