blob: 3345a089ef7b954d475d67965b8ce891c1b16ab0 [file] [log] [blame]
Dave Jones199a9af2006-09-29 01:59:00 -07001/*
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 Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Dave Jones199a9af2006-09-29 01:59:00 -070010#include <linux/list.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -050011#include <linux/bug.h>
Paul Gortmakerb116ee42012-01-20 18:46:49 -050012#include <linux/kernel.h>
Dave Jones559f9ba2012-03-14 22:17:39 -040013#include <linux/rculist.h>
Dave Jones199a9af2006-09-29 01:59:00 -070014
Dan Williams5c2c2582016-01-15 16:56:49 -080015static struct list_head force_poison;
16void list_force_poison(struct list_head *entry)
17{
18 entry->next = &force_poison;
19 entry->prev = &force_poison;
20}
21
Dave Jones199a9af2006-09-29 01:59:00 -070022/*
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
29void __list_add(struct list_head *new,
30 struct list_head *prev,
31 struct list_head *next)
32{
Dan Williams5c2c2582016-01-15 16:56:49 -080033 WARN(new->next == &force_poison || new->prev == &force_poison,
34 "list_add attempted on force-poisoned entry\n");
Dave Jones924d9ad2008-07-25 01:45:55 -070035 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 Metcalf17a801f2012-05-29 15:07:31 -070043 WARN(new == prev || new == next,
44 "list_add double add: new=%p, prev=%p, next=%p.\n",
45 new, prev, next);
Dave Jones199a9af2006-09-29 01:59:00 -070046 next->prev = new;
47 new->next = next;
48 new->prev = prev;
Paul E. McKenney1c97be62015-09-20 22:02:17 -070049 WRITE_ONCE(prev->next, new);
Dave Jones199a9af2006-09-29 01:59:00 -070050}
51EXPORT_SYMBOL(__list_add);
52
Linus Torvalds3c18d4d2011-02-18 11:32:28 -080053void __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}
76EXPORT_SYMBOL(__list_del_entry);
77
Dave Jones199a9af2006-09-29 01:59:00 -070078/**
Dave Jones199a9af2006-09-29 01:59:00 -070079 * 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 */
84void list_del(struct list_head *entry)
85{
Linus Torvalds3c18d4d2011-02-18 11:32:28 -080086 __list_del_entry(entry);
Dave Jones199a9af2006-09-29 01:59:00 -070087 entry->next = LIST_POISON1;
88 entry->prev = LIST_POISON2;
89}
90EXPORT_SYMBOL(list_del);
Dave Jones559f9ba2012-03-14 22:17:39 -040091
92/*
93 * RCU variants.
94 */
95void __list_add_rcu(struct list_head *new,
96 struct list_head *prev, struct list_head *next)
97{
98 WARN(next->prev != prev,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -070099 "list_add_rcu corruption. next->prev should be prev (%p), but was %p. (next=%p).\n",
Dave Jones559f9ba2012-03-14 22:17:39 -0400100 prev, next->prev, next);
101 WARN(prev->next != next,
Paul E. McKenney5cf05ad2012-05-17 15:12:45 -0700102 "list_add_rcu corruption. prev->next should be next (%p), but was %p. (prev=%p).\n",
Dave Jones559f9ba2012-03-14 22:17:39 -0400103 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}
109EXPORT_SYMBOL(__list_add_rcu);