Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 2 | #ifndef LLIST_H |
| 3 | #define LLIST_H |
| 4 | /* |
| 5 | * Lock-less NULL terminated single linked list |
| 6 | * |
Joel Fernandes | d78973c | 2016-12-12 18:01:45 -0800 | [diff] [blame] | 7 | * Cases where locking is not needed: |
| 8 | * If there are multiple producers and multiple consumers, llist_add can be |
| 9 | * used in producers and llist_del_all can be used in consumers simultaneously |
| 10 | * without locking. Also a single consumer can use llist_del_first while |
| 11 | * multiple producers simultaneously use llist_add, without any locking. |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 12 | * |
Joel Fernandes | d78973c | 2016-12-12 18:01:45 -0800 | [diff] [blame] | 13 | * Cases where locking is needed: |
| 14 | * If we have multiple consumers with llist_del_first used in one consumer, and |
| 15 | * llist_del_first or llist_del_all used in other consumers, then a lock is |
| 16 | * needed. This is because llist_del_first depends on list->first->next not |
| 17 | * changing, but without lock protection, there's no way to be sure about that |
| 18 | * if a preemption happens in the middle of the delete operation and on being |
| 19 | * preempted back, the list->first is the same as before causing the cmpxchg in |
| 20 | * llist_del_first to succeed. For example, while a llist_del_first operation |
| 21 | * is in progress in one consumer, then a llist_del_first, llist_add, |
| 22 | * llist_add (or llist_del_all, llist_add, llist_add) sequence in another |
| 23 | * consumer may cause violations. |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 24 | * |
Joel Fernandes | d78973c | 2016-12-12 18:01:45 -0800 | [diff] [blame] | 25 | * This can be summarized as follows: |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 26 | * |
| 27 | * | add | del_first | del_all |
| 28 | * add | - | - | - |
| 29 | * del_first | | L | L |
| 30 | * del_all | | | - |
| 31 | * |
Joel Fernandes | d78973c | 2016-12-12 18:01:45 -0800 | [diff] [blame] | 32 | * Where, a particular row's operation can happen concurrently with a column's |
| 33 | * operation, with "-" being no lock needed, while "L" being lock is needed. |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 34 | * |
| 35 | * The list entries deleted via llist_del_all can be traversed with |
| 36 | * traversing function such as llist_for_each etc. But the list |
| 37 | * entries can not be traversed safely before deleted from the list. |
| 38 | * The order of deleted entries is from the newest to the oldest added |
| 39 | * one. If you want to traverse from the oldest to the newest, you |
| 40 | * must reverse the order by yourself before traversing. |
| 41 | * |
| 42 | * The basic atomic operation of this list is cmpxchg on long. On |
| 43 | * architectures that don't have NMI-safe cmpxchg implementation, the |
Ingo Molnar | 2c30245 | 2011-10-04 12:43:11 +0200 | [diff] [blame] | 44 | * list can NOT be used in NMI handlers. So code that uses the list in |
| 45 | * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG. |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 46 | * |
| 47 | * Copyright 2010,2011 Intel Corp. |
| 48 | * Author: Huang Ying <ying.huang@intel.com> |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 49 | */ |
| 50 | |
Will Deacon | cd074ae | 2015-08-06 17:54:43 +0100 | [diff] [blame] | 51 | #include <linux/atomic.h> |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 52 | #include <linux/kernel.h> |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 53 | |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 54 | struct llist_head { |
| 55 | struct llist_node *first; |
| 56 | }; |
| 57 | |
| 58 | struct llist_node { |
| 59 | struct llist_node *next; |
| 60 | }; |
| 61 | |
| 62 | #define LLIST_HEAD_INIT(name) { NULL } |
| 63 | #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name) |
| 64 | |
| 65 | /** |
| 66 | * init_llist_head - initialize lock-less list head |
| 67 | * @head: the head for your lock-less list |
| 68 | */ |
| 69 | static inline void init_llist_head(struct llist_head *list) |
| 70 | { |
| 71 | list->first = NULL; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * llist_entry - get the struct of this entry |
| 76 | * @ptr: the &struct llist_node pointer. |
| 77 | * @type: the type of the struct this is embedded in. |
| 78 | * @member: the name of the llist_node within the struct. |
| 79 | */ |
| 80 | #define llist_entry(ptr, type, member) \ |
| 81 | container_of(ptr, type, member) |
| 82 | |
| 83 | /** |
Alexander Potapenko | beaec53 | 2017-07-19 20:27:30 +0200 | [diff] [blame] | 84 | * member_address_is_nonnull - check whether the member address is not NULL |
| 85 | * @ptr: the object pointer (struct type * that contains the llist_node) |
| 86 | * @member: the name of the llist_node within the struct. |
| 87 | * |
| 88 | * This macro is conceptually the same as |
| 89 | * &ptr->member != NULL |
| 90 | * but it works around the fact that compilers can decide that taking a member |
| 91 | * address is never a NULL pointer. |
| 92 | * |
| 93 | * Real objects that start at a high address and have a member at NULL are |
| 94 | * unlikely to exist, but such pointers may be returned e.g. by the |
| 95 | * container_of() macro. |
| 96 | */ |
| 97 | #define member_address_is_nonnull(ptr, member) \ |
| 98 | ((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0) |
| 99 | |
| 100 | /** |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 101 | * llist_for_each - iterate over some deleted entries of a lock-less list |
| 102 | * @pos: the &struct llist_node to use as a loop cursor |
| 103 | * @node: the first entry of deleted list entries |
| 104 | * |
| 105 | * In general, some entries of the lock-less list can be traversed |
| 106 | * safely only after being deleted from list, so start with an entry |
| 107 | * instead of list head. |
| 108 | * |
| 109 | * If being used on entries deleted from lock-less list directly, the |
| 110 | * traverse order is from the newest to the oldest added entry. If |
| 111 | * you want to traverse from the oldest to the newest, you must |
| 112 | * reverse the order by yourself before traversing. |
| 113 | */ |
| 114 | #define llist_for_each(pos, node) \ |
| 115 | for ((pos) = (node); pos; (pos) = (pos)->next) |
| 116 | |
| 117 | /** |
Byungchul Park | d714893 | 2017-05-12 09:36:56 +0900 | [diff] [blame] | 118 | * llist_for_each_safe - iterate over some deleted entries of a lock-less list |
| 119 | * safe against removal of list entry |
| 120 | * @pos: the &struct llist_node to use as a loop cursor |
| 121 | * @n: another &struct llist_node to use as temporary storage |
| 122 | * @node: the first entry of deleted list entries |
| 123 | * |
| 124 | * In general, some entries of the lock-less list can be traversed |
| 125 | * safely only after being deleted from list, so start with an entry |
| 126 | * instead of list head. |
| 127 | * |
| 128 | * If being used on entries deleted from lock-less list directly, the |
| 129 | * traverse order is from the newest to the oldest added entry. If |
| 130 | * you want to traverse from the oldest to the newest, you must |
| 131 | * reverse the order by yourself before traversing. |
| 132 | */ |
| 133 | #define llist_for_each_safe(pos, n, node) \ |
| 134 | for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n)) |
| 135 | |
| 136 | /** |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 137 | * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type |
| 138 | * @pos: the type * to use as a loop cursor. |
| 139 | * @node: the fist entry of deleted list entries. |
| 140 | * @member: the name of the llist_node with the struct. |
| 141 | * |
| 142 | * In general, some entries of the lock-less list can be traversed |
| 143 | * safely only after being removed from list, so start with an entry |
| 144 | * instead of list head. |
| 145 | * |
| 146 | * If being used on entries deleted from lock-less list directly, the |
| 147 | * traverse order is from the newest to the oldest added entry. If |
| 148 | * you want to traverse from the oldest to the newest, you must |
| 149 | * reverse the order by yourself before traversing. |
| 150 | */ |
| 151 | #define llist_for_each_entry(pos, node, member) \ |
| 152 | for ((pos) = llist_entry((node), typeof(*(pos)), member); \ |
Alexander Potapenko | beaec53 | 2017-07-19 20:27:30 +0200 | [diff] [blame] | 153 | member_address_is_nonnull(pos, member); \ |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 154 | (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member)) |
| 155 | |
| 156 | /** |
Peter Hurley | 809850b | 2013-06-15 09:36:06 -0400 | [diff] [blame] | 157 | * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type |
| 158 | * safe against removal of list entry |
| 159 | * @pos: the type * to use as a loop cursor. |
| 160 | * @n: another type * to use as temporary storage |
| 161 | * @node: the first entry of deleted list entries. |
| 162 | * @member: the name of the llist_node with the struct. |
| 163 | * |
| 164 | * In general, some entries of the lock-less list can be traversed |
| 165 | * safely only after being removed from list, so start with an entry |
| 166 | * instead of list head. |
| 167 | * |
| 168 | * If being used on entries deleted from lock-less list directly, the |
| 169 | * traverse order is from the newest to the oldest added entry. If |
| 170 | * you want to traverse from the oldest to the newest, you must |
| 171 | * reverse the order by yourself before traversing. |
| 172 | */ |
| 173 | #define llist_for_each_entry_safe(pos, n, node, member) \ |
| 174 | for (pos = llist_entry((node), typeof(*pos), member); \ |
Alexander Potapenko | beaec53 | 2017-07-19 20:27:30 +0200 | [diff] [blame] | 175 | member_address_is_nonnull(pos, member) && \ |
Peter Hurley | 809850b | 2013-06-15 09:36:06 -0400 | [diff] [blame] | 176 | (n = llist_entry(pos->member.next, typeof(*n), member), true); \ |
| 177 | pos = n) |
| 178 | |
| 179 | /** |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 180 | * llist_empty - tests whether a lock-less list is empty |
| 181 | * @head: the list to test |
| 182 | * |
| 183 | * Not guaranteed to be accurate or up to date. Just a quick way to |
| 184 | * test whether the list is empty without deleting something from the |
| 185 | * list. |
| 186 | */ |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 187 | static inline bool llist_empty(const struct llist_head *head) |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 188 | { |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 189 | return READ_ONCE(head->first) == NULL; |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Peter Zijlstra | 924f8f5 | 2011-09-12 13:12:28 +0200 | [diff] [blame] | 192 | static inline struct llist_node *llist_next(struct llist_node *node) |
| 193 | { |
| 194 | return node->next; |
| 195 | } |
| 196 | |
Oleg Nesterov | e9a17bd | 2013-07-08 14:24:19 -0700 | [diff] [blame] | 197 | extern bool llist_add_batch(struct llist_node *new_first, |
| 198 | struct llist_node *new_last, |
| 199 | struct llist_head *head); |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 200 | /** |
| 201 | * llist_add - add a new entry |
| 202 | * @new: new entry to be added |
| 203 | * @head: the head for your lock-less list |
Huang Ying | 781f7fd | 2011-09-08 14:00:45 +0800 | [diff] [blame] | 204 | * |
Andrew Morton | fc23af3 | 2011-10-31 17:13:08 -0700 | [diff] [blame] | 205 | * Returns true if the list was empty prior to adding this entry. |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 206 | */ |
Huang Ying | 781f7fd | 2011-09-08 14:00:45 +0800 | [diff] [blame] | 207 | static inline bool llist_add(struct llist_node *new, struct llist_head *head) |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 208 | { |
Oleg Nesterov | e9a17bd | 2013-07-08 14:24:19 -0700 | [diff] [blame] | 209 | return llist_add_batch(new, new, head); |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * llist_del_all - delete all entries from lock-less list |
| 214 | * @head: the head of lock-less list to delete all entries |
| 215 | * |
| 216 | * If list is empty, return NULL, otherwise, delete all entries and |
| 217 | * return the pointer to the first entry. The order of entries |
| 218 | * deleted is from the newest to the oldest added one. |
| 219 | */ |
| 220 | static inline struct llist_node *llist_del_all(struct llist_head *head) |
| 221 | { |
Huang Ying | 1230db8e | 2011-09-08 14:00:42 +0800 | [diff] [blame] | 222 | return xchg(&head->first, NULL); |
| 223 | } |
Stephen Rothwell | 540f41e | 2011-10-05 17:25:28 +1100 | [diff] [blame] | 224 | |
Stephen Rothwell | 540f41e | 2011-10-05 17:25:28 +1100 | [diff] [blame] | 225 | extern struct llist_node *llist_del_first(struct llist_head *head); |
| 226 | |
Christoph Hellwig | b89241e | 2013-11-14 14:32:11 -0800 | [diff] [blame] | 227 | struct llist_node *llist_reverse_order(struct llist_node *head); |
| 228 | |
Huang Ying | f49f23a | 2011-07-13 13:14:23 +0800 | [diff] [blame] | 229 | #endif /* LLIST_H */ |