blob: d11738110a7aeff53b1349f994e3e435d2f0e76b [file] [log] [blame]
Huang Yingf49f23a2011-07-13 13:14:23 +08001#ifndef LLIST_H
2#define LLIST_H
3/*
4 * Lock-less NULL terminated single linked list
5 *
Joel Fernandesd78973c2016-12-12 18:01:45 -08006 * Cases where locking is not needed:
7 * If there are multiple producers and multiple consumers, llist_add can be
8 * used in producers and llist_del_all can be used in consumers simultaneously
9 * without locking. Also a single consumer can use llist_del_first while
10 * multiple producers simultaneously use llist_add, without any locking.
Huang Yingf49f23a2011-07-13 13:14:23 +080011 *
Joel Fernandesd78973c2016-12-12 18:01:45 -080012 * Cases where locking is needed:
13 * If we have multiple consumers with llist_del_first used in one consumer, and
14 * llist_del_first or llist_del_all used in other consumers, then a lock is
15 * needed. This is because llist_del_first depends on list->first->next not
16 * changing, but without lock protection, there's no way to be sure about that
17 * if a preemption happens in the middle of the delete operation and on being
18 * preempted back, the list->first is the same as before causing the cmpxchg in
19 * llist_del_first to succeed. For example, while a llist_del_first operation
20 * is in progress in one consumer, then a llist_del_first, llist_add,
21 * llist_add (or llist_del_all, llist_add, llist_add) sequence in another
22 * consumer may cause violations.
Huang Yingf49f23a2011-07-13 13:14:23 +080023 *
Joel Fernandesd78973c2016-12-12 18:01:45 -080024 * This can be summarized as follows:
Huang Yingf49f23a2011-07-13 13:14:23 +080025 *
26 * | add | del_first | del_all
27 * add | - | - | -
28 * del_first | | L | L
29 * del_all | | | -
30 *
Joel Fernandesd78973c2016-12-12 18:01:45 -080031 * Where, a particular row's operation can happen concurrently with a column's
32 * operation, with "-" being no lock needed, while "L" being lock is needed.
Huang Yingf49f23a2011-07-13 13:14:23 +080033 *
34 * The list entries deleted via llist_del_all can be traversed with
35 * traversing function such as llist_for_each etc. But the list
36 * entries can not be traversed safely before deleted from the list.
37 * The order of deleted entries is from the newest to the oldest added
38 * one. If you want to traverse from the oldest to the newest, you
39 * must reverse the order by yourself before traversing.
40 *
41 * The basic atomic operation of this list is cmpxchg on long. On
42 * architectures that don't have NMI-safe cmpxchg implementation, the
Ingo Molnar2c302452011-10-04 12:43:11 +020043 * list can NOT be used in NMI handlers. So code that uses the list in
44 * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
Huang Ying1230db8e2011-09-08 14:00:42 +080045 *
46 * Copyright 2010,2011 Intel Corp.
47 * Author: Huang Ying <ying.huang@intel.com>
48 *
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License version
51 * 2 as published by the Free Software Foundation;
52 *
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
56 * GNU General Public License for more details.
57 *
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Huang Yingf49f23a2011-07-13 13:14:23 +080061 */
62
Will Deaconcd074ae2015-08-06 17:54:43 +010063#include <linux/atomic.h>
Huang Ying1230db8e2011-09-08 14:00:42 +080064#include <linux/kernel.h>
Huang Ying1230db8e2011-09-08 14:00:42 +080065
Huang Yingf49f23a2011-07-13 13:14:23 +080066struct llist_head {
67 struct llist_node *first;
68};
69
70struct llist_node {
71 struct llist_node *next;
72};
73
74#define LLIST_HEAD_INIT(name) { NULL }
75#define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name)
76
77/**
78 * init_llist_head - initialize lock-less list head
79 * @head: the head for your lock-less list
80 */
81static inline void init_llist_head(struct llist_head *list)
82{
83 list->first = NULL;
84}
85
86/**
87 * llist_entry - get the struct of this entry
88 * @ptr: the &struct llist_node pointer.
89 * @type: the type of the struct this is embedded in.
90 * @member: the name of the llist_node within the struct.
91 */
92#define llist_entry(ptr, type, member) \
93 container_of(ptr, type, member)
94
95/**
96 * llist_for_each - iterate over some deleted entries of a lock-less list
97 * @pos: the &struct llist_node to use as a loop cursor
98 * @node: the first entry of deleted list entries
99 *
100 * In general, some entries of the lock-less list can be traversed
101 * safely only after being deleted from list, so start with an entry
102 * instead of list head.
103 *
104 * If being used on entries deleted from lock-less list directly, the
105 * traverse order is from the newest to the oldest added entry. If
106 * you want to traverse from the oldest to the newest, you must
107 * reverse the order by yourself before traversing.
108 */
109#define llist_for_each(pos, node) \
110 for ((pos) = (node); pos; (pos) = (pos)->next)
111
112/**
Byungchul Parkd7148932017-05-12 09:36:56 +0900113 * llist_for_each_safe - iterate over some deleted entries of a lock-less list
114 * safe against removal of list entry
115 * @pos: the &struct llist_node to use as a loop cursor
116 * @n: another &struct llist_node to use as temporary storage
117 * @node: the first entry of deleted list entries
118 *
119 * In general, some entries of the lock-less list can be traversed
120 * safely only after being deleted from list, so start with an entry
121 * instead of list head.
122 *
123 * If being used on entries deleted from lock-less list directly, the
124 * traverse order is from the newest to the oldest added entry. If
125 * you want to traverse from the oldest to the newest, you must
126 * reverse the order by yourself before traversing.
127 */
128#define llist_for_each_safe(pos, n, node) \
129 for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
130
131/**
Huang Yingf49f23a2011-07-13 13:14:23 +0800132 * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
133 * @pos: the type * to use as a loop cursor.
134 * @node: the fist entry of deleted list entries.
135 * @member: the name of the llist_node with the struct.
136 *
137 * In general, some entries of the lock-less list can be traversed
138 * safely only after being removed from list, so start with an entry
139 * instead of list head.
140 *
141 * If being used on entries deleted from lock-less list directly, the
142 * traverse order is from the newest to the oldest added entry. If
143 * you want to traverse from the oldest to the newest, you must
144 * reverse the order by yourself before traversing.
145 */
146#define llist_for_each_entry(pos, node, member) \
147 for ((pos) = llist_entry((node), typeof(*(pos)), member); \
148 &(pos)->member != NULL; \
149 (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
150
151/**
Peter Hurley809850b2013-06-15 09:36:06 -0400152 * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
153 * safe against removal of list entry
154 * @pos: the type * to use as a loop cursor.
155 * @n: another type * to use as temporary storage
156 * @node: the first entry of deleted list entries.
157 * @member: the name of the llist_node with the struct.
158 *
159 * In general, some entries of the lock-less list can be traversed
160 * safely only after being removed from list, so start with an entry
161 * instead of list head.
162 *
163 * If being used on entries deleted from lock-less list directly, the
164 * traverse order is from the newest to the oldest added entry. If
165 * you want to traverse from the oldest to the newest, you must
166 * reverse the order by yourself before traversing.
167 */
168#define llist_for_each_entry_safe(pos, n, node, member) \
169 for (pos = llist_entry((node), typeof(*pos), member); \
170 &pos->member != NULL && \
171 (n = llist_entry(pos->member.next, typeof(*n), member), true); \
172 pos = n)
173
174/**
Huang Yingf49f23a2011-07-13 13:14:23 +0800175 * llist_empty - tests whether a lock-less list is empty
176 * @head: the list to test
177 *
178 * Not guaranteed to be accurate or up to date. Just a quick way to
179 * test whether the list is empty without deleting something from the
180 * list.
181 */
Huang Ying1230db8e2011-09-08 14:00:42 +0800182static inline bool llist_empty(const struct llist_head *head)
Huang Yingf49f23a2011-07-13 13:14:23 +0800183{
184 return ACCESS_ONCE(head->first) == NULL;
185}
186
Peter Zijlstra924f8f52011-09-12 13:12:28 +0200187static inline struct llist_node *llist_next(struct llist_node *node)
188{
189 return node->next;
190}
191
Oleg Nesterove9a17bd2013-07-08 14:24:19 -0700192extern bool llist_add_batch(struct llist_node *new_first,
193 struct llist_node *new_last,
194 struct llist_head *head);
Huang Ying1230db8e2011-09-08 14:00:42 +0800195/**
196 * llist_add - add a new entry
197 * @new: new entry to be added
198 * @head: the head for your lock-less list
Huang Ying781f7fd2011-09-08 14:00:45 +0800199 *
Andrew Mortonfc23af32011-10-31 17:13:08 -0700200 * Returns true if the list was empty prior to adding this entry.
Huang Ying1230db8e2011-09-08 14:00:42 +0800201 */
Huang Ying781f7fd2011-09-08 14:00:45 +0800202static inline bool llist_add(struct llist_node *new, struct llist_head *head)
Huang Ying1230db8e2011-09-08 14:00:42 +0800203{
Oleg Nesterove9a17bd2013-07-08 14:24:19 -0700204 return llist_add_batch(new, new, head);
Huang Ying1230db8e2011-09-08 14:00:42 +0800205}
206
207/**
208 * llist_del_all - delete all entries from lock-less list
209 * @head: the head of lock-less list to delete all entries
210 *
211 * If list is empty, return NULL, otherwise, delete all entries and
212 * return the pointer to the first entry. The order of entries
213 * deleted is from the newest to the oldest added one.
214 */
215static inline struct llist_node *llist_del_all(struct llist_head *head)
216{
Huang Ying1230db8e2011-09-08 14:00:42 +0800217 return xchg(&head->first, NULL);
218}
Stephen Rothwell540f41e2011-10-05 17:25:28 +1100219
Stephen Rothwell540f41e2011-10-05 17:25:28 +1100220extern struct llist_node *llist_del_first(struct llist_head *head);
221
Christoph Hellwigb89241e2013-11-14 14:32:11 -0800222struct llist_node *llist_reverse_order(struct llist_node *head);
223
Huang Yingf49f23a2011-07-13 13:14:23 +0800224#endif /* LLIST_H */