xref: /linux-6.15/lib/list_debug.c (revision b116ee4d)
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/module.h>
10 #include <linux/list.h>
11 #include <linux/kernel.h>
12 
13 /*
14  * Insert a new entry between two known consecutive entries.
15  *
16  * This is only for internal list manipulation where we know
17  * the prev/next entries already!
18  */
19 
20 void __list_add(struct list_head *new,
21 			      struct list_head *prev,
22 			      struct list_head *next)
23 {
24 	WARN(next->prev != prev,
25 		"list_add corruption. next->prev should be "
26 		"prev (%p), but was %p. (next=%p).\n",
27 		prev, next->prev, next);
28 	WARN(prev->next != next,
29 		"list_add corruption. prev->next should be "
30 		"next (%p), but was %p. (prev=%p).\n",
31 		next, prev->next, prev);
32 	next->prev = new;
33 	new->next = next;
34 	new->prev = prev;
35 	prev->next = new;
36 }
37 EXPORT_SYMBOL(__list_add);
38 
39 void __list_del_entry(struct list_head *entry)
40 {
41 	struct list_head *prev, *next;
42 
43 	prev = entry->prev;
44 	next = entry->next;
45 
46 	if (WARN(next == LIST_POISON1,
47 		"list_del corruption, %p->next is LIST_POISON1 (%p)\n",
48 		entry, LIST_POISON1) ||
49 	    WARN(prev == LIST_POISON2,
50 		"list_del corruption, %p->prev is LIST_POISON2 (%p)\n",
51 		entry, LIST_POISON2) ||
52 	    WARN(prev->next != entry,
53 		"list_del corruption. prev->next should be %p, "
54 		"but was %p\n", entry, prev->next) ||
55 	    WARN(next->prev != entry,
56 		"list_del corruption. next->prev should be %p, "
57 		"but was %p\n", entry, next->prev))
58 		return;
59 
60 	__list_del(prev, next);
61 }
62 EXPORT_SYMBOL(__list_del_entry);
63 
64 /**
65  * list_del - deletes entry from list.
66  * @entry: the element to delete from the list.
67  * Note: list_empty on entry does not return true after this, the entry is
68  * in an undefined state.
69  */
70 void list_del(struct list_head *entry)
71 {
72 	__list_del_entry(entry);
73 	entry->next = LIST_POISON1;
74 	entry->prev = LIST_POISON2;
75 }
76 EXPORT_SYMBOL(list_del);
77