| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ | 
|---|
| 2 | #ifndef LLIST_H | 
|---|
| 3 | #define LLIST_H | 
|---|
| 4 | /* | 
|---|
| 5 | * Lock-less NULL terminated single linked list | 
|---|
| 6 | * | 
|---|
| 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. | 
|---|
| 12 | * | 
|---|
| 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. | 
|---|
| 24 | * | 
|---|
| 25 | * This can be summarized as follows: | 
|---|
| 26 | * | 
|---|
| 27 | *           |   add    | del_first |  del_all | 
|---|
| 28 | * add       |    -     |     -     |     - | 
|---|
| 29 | * del_first |          |     L     |     L | 
|---|
| 30 | * del_all   |          |           |     - | 
|---|
| 31 | * | 
|---|
| 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. | 
|---|
| 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 | 
|---|
| 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. | 
|---|
| 46 | * | 
|---|
| 47 | * Copyright 2010,2011 Intel Corp. | 
|---|
| 48 | *   Author: Huang Ying <ying.huang@intel.com> | 
|---|
| 49 | */ | 
|---|
| 50 |  | 
|---|
| 51 | #include <linux/atomic.h> | 
|---|
| 52 | #include <linux/container_of.h> | 
|---|
| 53 | #include <linux/stddef.h> | 
|---|
| 54 | #include <linux/types.h> | 
|---|
| 55 |  | 
|---|
| 56 | struct llist_head { | 
|---|
| 57 | struct llist_node *first; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | struct llist_node { | 
|---|
| 61 | struct llist_node *next; | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | #define LLIST_HEAD_INIT(name)	{ NULL } | 
|---|
| 65 | #define LLIST_HEAD(name)	struct llist_head name = LLIST_HEAD_INIT(name) | 
|---|
| 66 |  | 
|---|
| 67 | /** | 
|---|
| 68 | * init_llist_head - initialize lock-less list head | 
|---|
| 69 | * @head:	the head for your lock-less list | 
|---|
| 70 | */ | 
|---|
| 71 | static inline void init_llist_head(struct llist_head *list) | 
|---|
| 72 | { | 
|---|
| 73 | list->first = NULL; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /** | 
|---|
| 77 | * init_llist_node - initialize lock-less list node | 
|---|
| 78 | * @node:	the node to be initialised | 
|---|
| 79 | * | 
|---|
| 80 | * In cases where there is a need to test if a node is on | 
|---|
| 81 | * a list or not, this initialises the node to clearly | 
|---|
| 82 | * not be on any list. | 
|---|
| 83 | */ | 
|---|
| 84 | static inline void init_llist_node(struct llist_node *node) | 
|---|
| 85 | { | 
|---|
| 86 | WRITE_ONCE(node->next, node); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** | 
|---|
| 90 | * llist_on_list - test if a lock-list list node is on a list | 
|---|
| 91 | * @node:	the node to test | 
|---|
| 92 | * | 
|---|
| 93 | * When a node is on a list the ->next pointer will be NULL or | 
|---|
| 94 | * some other node.  It can never point to itself.  We use that | 
|---|
| 95 | * in init_llist_node() to record that a node is not on any list, | 
|---|
| 96 | * and here to test whether it is on any list. | 
|---|
| 97 | */ | 
|---|
| 98 | static inline bool llist_on_list(const struct llist_node *node) | 
|---|
| 99 | { | 
|---|
| 100 | return READ_ONCE(node->next) != node; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | /** | 
|---|
| 104 | * llist_entry - get the struct of this entry | 
|---|
| 105 | * @ptr:	the &struct llist_node pointer. | 
|---|
| 106 | * @type:	the type of the struct this is embedded in. | 
|---|
| 107 | * @member:	the name of the llist_node within the struct. | 
|---|
| 108 | */ | 
|---|
| 109 | #define llist_entry(ptr, type, member)		\ | 
|---|
| 110 | container_of(ptr, type, member) | 
|---|
| 111 |  | 
|---|
| 112 | /** | 
|---|
| 113 | * member_address_is_nonnull - check whether the member address is not NULL | 
|---|
| 114 | * @ptr:	the object pointer (struct type * that contains the llist_node) | 
|---|
| 115 | * @member:	the name of the llist_node within the struct. | 
|---|
| 116 | * | 
|---|
| 117 | * This macro is conceptually the same as | 
|---|
| 118 | *	&ptr->member != NULL | 
|---|
| 119 | * but it works around the fact that compilers can decide that taking a member | 
|---|
| 120 | * address is never a NULL pointer. | 
|---|
| 121 | * | 
|---|
| 122 | * Real objects that start at a high address and have a member at NULL are | 
|---|
| 123 | * unlikely to exist, but such pointers may be returned e.g. by the | 
|---|
| 124 | * container_of() macro. | 
|---|
| 125 | */ | 
|---|
| 126 | #define member_address_is_nonnull(ptr, member)	\ | 
|---|
| 127 | ((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0) | 
|---|
| 128 |  | 
|---|
| 129 | /** | 
|---|
| 130 | * llist_for_each - iterate over some deleted entries of a lock-less list | 
|---|
| 131 | * @pos:	the &struct llist_node to use as a loop cursor | 
|---|
| 132 | * @node:	the first entry of deleted list entries | 
|---|
| 133 | * | 
|---|
| 134 | * In general, some entries of the lock-less list can be traversed | 
|---|
| 135 | * safely only after being deleted from list, so start with an entry | 
|---|
| 136 | * instead of list head. | 
|---|
| 137 | * | 
|---|
| 138 | * If being used on entries deleted from lock-less list directly, the | 
|---|
| 139 | * traverse order is from the newest to the oldest added entry.  If | 
|---|
| 140 | * you want to traverse from the oldest to the newest, you must | 
|---|
| 141 | * reverse the order by yourself before traversing. | 
|---|
| 142 | */ | 
|---|
| 143 | #define llist_for_each(pos, node)			\ | 
|---|
| 144 | for ((pos) = (node); pos; (pos) = (pos)->next) | 
|---|
| 145 |  | 
|---|
| 146 | /** | 
|---|
| 147 | * llist_for_each_safe - iterate over some deleted entries of a lock-less list | 
|---|
| 148 | *			 safe against removal of list entry | 
|---|
| 149 | * @pos:	the &struct llist_node to use as a loop cursor | 
|---|
| 150 | * @n:		another &struct llist_node to use as temporary storage | 
|---|
| 151 | * @node:	the first entry of deleted list entries | 
|---|
| 152 | * | 
|---|
| 153 | * In general, some entries of the lock-less list can be traversed | 
|---|
| 154 | * safely only after being deleted from list, so start with an entry | 
|---|
| 155 | * instead of list head. | 
|---|
| 156 | * | 
|---|
| 157 | * If being used on entries deleted from lock-less list directly, the | 
|---|
| 158 | * traverse order is from the newest to the oldest added entry.  If | 
|---|
| 159 | * you want to traverse from the oldest to the newest, you must | 
|---|
| 160 | * reverse the order by yourself before traversing. | 
|---|
| 161 | */ | 
|---|
| 162 | #define llist_for_each_safe(pos, n, node)			\ | 
|---|
| 163 | for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n)) | 
|---|
| 164 |  | 
|---|
| 165 | /** | 
|---|
| 166 | * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type | 
|---|
| 167 | * @pos:	the type * to use as a loop cursor. | 
|---|
| 168 | * @node:	the fist entry of deleted list entries. | 
|---|
| 169 | * @member:	the name of the llist_node with the struct. | 
|---|
| 170 | * | 
|---|
| 171 | * In general, some entries of the lock-less list can be traversed | 
|---|
| 172 | * safely only after being removed from list, so start with an entry | 
|---|
| 173 | * instead of list head. | 
|---|
| 174 | * | 
|---|
| 175 | * If being used on entries deleted from lock-less list directly, the | 
|---|
| 176 | * traverse order is from the newest to the oldest added entry.  If | 
|---|
| 177 | * you want to traverse from the oldest to the newest, you must | 
|---|
| 178 | * reverse the order by yourself before traversing. | 
|---|
| 179 | */ | 
|---|
| 180 | #define llist_for_each_entry(pos, node, member)				\ | 
|---|
| 181 | for ((pos) = llist_entry((node), typeof(*(pos)), member);	\ | 
|---|
| 182 | member_address_is_nonnull(pos, member);			\ | 
|---|
| 183 | (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member)) | 
|---|
| 184 |  | 
|---|
| 185 | /** | 
|---|
| 186 | * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type | 
|---|
| 187 | *			       safe against removal of list entry | 
|---|
| 188 | * @pos:	the type * to use as a loop cursor. | 
|---|
| 189 | * @n:		another type * to use as temporary storage | 
|---|
| 190 | * @node:	the first entry of deleted list entries. | 
|---|
| 191 | * @member:	the name of the llist_node with the struct. | 
|---|
| 192 | * | 
|---|
| 193 | * In general, some entries of the lock-less list can be traversed | 
|---|
| 194 | * safely only after being removed from list, so start with an entry | 
|---|
| 195 | * instead of list head. | 
|---|
| 196 | * | 
|---|
| 197 | * If being used on entries deleted from lock-less list directly, the | 
|---|
| 198 | * traverse order is from the newest to the oldest added entry.  If | 
|---|
| 199 | * you want to traverse from the oldest to the newest, you must | 
|---|
| 200 | * reverse the order by yourself before traversing. | 
|---|
| 201 | */ | 
|---|
| 202 | #define llist_for_each_entry_safe(pos, n, node, member)			       \ | 
|---|
| 203 | for (pos = llist_entry((node), typeof(*pos), member);		       \ | 
|---|
| 204 | member_address_is_nonnull(pos, member) &&			       \ | 
|---|
| 205 | (n = llist_entry(pos->member.next, typeof(*n), member), true); \ | 
|---|
| 206 | pos = n) | 
|---|
| 207 |  | 
|---|
| 208 | /** | 
|---|
| 209 | * llist_empty - tests whether a lock-less list is empty | 
|---|
| 210 | * @head:	the list to test | 
|---|
| 211 | * | 
|---|
| 212 | * Not guaranteed to be accurate or up to date.  Just a quick way to | 
|---|
| 213 | * test whether the list is empty without deleting something from the | 
|---|
| 214 | * list. | 
|---|
| 215 | */ | 
|---|
| 216 | static inline bool llist_empty(const struct llist_head *head) | 
|---|
| 217 | { | 
|---|
| 218 | return READ_ONCE(head->first) == NULL; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | static inline struct llist_node *llist_next(struct llist_node *node) | 
|---|
| 222 | { | 
|---|
| 223 | return READ_ONCE(node->next); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /** | 
|---|
| 227 | * llist_add_batch - add several linked entries in batch | 
|---|
| 228 | * @new_first:	first entry in batch to be added | 
|---|
| 229 | * @new_last:	last entry in batch to be added | 
|---|
| 230 | * @head:	the head for your lock-less list | 
|---|
| 231 | * | 
|---|
| 232 | * Return whether list is empty before adding. | 
|---|
| 233 | */ | 
|---|
| 234 | static inline bool llist_add_batch(struct llist_node *new_first, | 
|---|
| 235 | struct llist_node *new_last, | 
|---|
| 236 | struct llist_head *head) | 
|---|
| 237 | { | 
|---|
| 238 | struct llist_node *first = READ_ONCE(head->first); | 
|---|
| 239 |  | 
|---|
| 240 | do { | 
|---|
| 241 | new_last->next = first; | 
|---|
| 242 | } while (!try_cmpxchg(&head->first, &first, new_first)); | 
|---|
| 243 |  | 
|---|
| 244 | return !first; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | static inline bool __llist_add_batch(struct llist_node *new_first, | 
|---|
| 248 | struct llist_node *new_last, | 
|---|
| 249 | struct llist_head *head) | 
|---|
| 250 | { | 
|---|
| 251 | new_last->next = head->first; | 
|---|
| 252 | head->first = new_first; | 
|---|
| 253 | return new_last->next == NULL; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | /** | 
|---|
| 257 | * llist_add - add a new entry | 
|---|
| 258 | * @new:	new entry to be added | 
|---|
| 259 | * @head:	the head for your lock-less list | 
|---|
| 260 | * | 
|---|
| 261 | * Returns true if the list was empty prior to adding this entry. | 
|---|
| 262 | */ | 
|---|
| 263 | static inline bool llist_add(struct llist_node *new, struct llist_head *head) | 
|---|
| 264 | { | 
|---|
| 265 | return llist_add_batch(new_first: new, new_last: new, head); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | static inline bool __llist_add(struct llist_node *new, struct llist_head *head) | 
|---|
| 269 | { | 
|---|
| 270 | return __llist_add_batch(new_first: new, new_last: new, head); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | /** | 
|---|
| 274 | * llist_del_all - delete all entries from lock-less list | 
|---|
| 275 | * @head:	the head of lock-less list to delete all entries | 
|---|
| 276 | * | 
|---|
| 277 | * If list is empty, return NULL, otherwise, delete all entries and | 
|---|
| 278 | * return the pointer to the first entry.  The order of entries | 
|---|
| 279 | * deleted is from the newest to the oldest added one. | 
|---|
| 280 | */ | 
|---|
| 281 | static inline struct llist_node *llist_del_all(struct llist_head *head) | 
|---|
| 282 | { | 
|---|
| 283 | return xchg(&head->first, NULL); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | static inline struct llist_node *__llist_del_all(struct llist_head *head) | 
|---|
| 287 | { | 
|---|
| 288 | struct llist_node *first = head->first; | 
|---|
| 289 |  | 
|---|
| 290 | head->first = NULL; | 
|---|
| 291 | return first; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | extern struct llist_node *llist_del_first(struct llist_head *head); | 
|---|
| 295 |  | 
|---|
| 296 | /** | 
|---|
| 297 | * llist_del_first_init - delete first entry from lock-list and mark is as being off-list | 
|---|
| 298 | * @head:	the head of lock-less list to delete from. | 
|---|
| 299 | * | 
|---|
| 300 | * This behave the same as llist_del_first() except that llist_init_node() is called | 
|---|
| 301 | * on the returned node so that llist_on_list() will report false for the node. | 
|---|
| 302 | */ | 
|---|
| 303 | static inline struct llist_node *llist_del_first_init(struct llist_head *head) | 
|---|
| 304 | { | 
|---|
| 305 | struct llist_node *n = llist_del_first(head); | 
|---|
| 306 |  | 
|---|
| 307 | if (n) | 
|---|
| 308 | init_llist_node(node: n); | 
|---|
| 309 | return n; | 
|---|
| 310 | } | 
|---|
| 311 |  | 
|---|
| 312 | extern bool llist_del_first_this(struct llist_head *head, | 
|---|
| 313 | struct llist_node *this); | 
|---|
| 314 |  | 
|---|
| 315 | struct llist_node *llist_reverse_order(struct llist_node *head); | 
|---|
| 316 |  | 
|---|
| 317 | #endif /* LLIST_H */ | 
|---|
| 318 |  | 
|---|