![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0-only */ 0002 #ifndef LLIST_H 0003 #define LLIST_H 0004 /* 0005 * Lock-less NULL terminated single linked list 0006 * 0007 * Cases where locking is not needed: 0008 * If there are multiple producers and multiple consumers, llist_add can be 0009 * used in producers and llist_del_all can be used in consumers simultaneously 0010 * without locking. Also a single consumer can use llist_del_first while 0011 * multiple producers simultaneously use llist_add, without any locking. 0012 * 0013 * Cases where locking is needed: 0014 * If we have multiple consumers with llist_del_first used in one consumer, and 0015 * llist_del_first or llist_del_all used in other consumers, then a lock is 0016 * needed. This is because llist_del_first depends on list->first->next not 0017 * changing, but without lock protection, there's no way to be sure about that 0018 * if a preemption happens in the middle of the delete operation and on being 0019 * preempted back, the list->first is the same as before causing the cmpxchg in 0020 * llist_del_first to succeed. For example, while a llist_del_first operation 0021 * is in progress in one consumer, then a llist_del_first, llist_add, 0022 * llist_add (or llist_del_all, llist_add, llist_add) sequence in another 0023 * consumer may cause violations. 0024 * 0025 * This can be summarized as follows: 0026 * 0027 * | add | del_first | del_all 0028 * add | - | - | - 0029 * del_first | | L | L 0030 * del_all | | | - 0031 * 0032 * Where, a particular row's operation can happen concurrently with a column's 0033 * operation, with "-" being no lock needed, while "L" being lock is needed. 0034 * 0035 * The list entries deleted via llist_del_all can be traversed with 0036 * traversing function such as llist_for_each etc. But the list 0037 * entries can not be traversed safely before deleted from the list. 0038 * The order of deleted entries is from the newest to the oldest added 0039 * one. If you want to traverse from the oldest to the newest, you 0040 * must reverse the order by yourself before traversing. 0041 * 0042 * The basic atomic operation of this list is cmpxchg on long. On 0043 * architectures that don't have NMI-safe cmpxchg implementation, the 0044 * list can NOT be used in NMI handlers. So code that uses the list in 0045 * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG. 0046 * 0047 * Copyright 2010,2011 Intel Corp. 0048 * Author: Huang Ying <ying.huang@intel.com> 0049 */ 0050 0051 #include <linux/atomic.h> 0052 #include <linux/container_of.h> 0053 #include <linux/stddef.h> 0054 #include <linux/types.h> 0055 0056 struct llist_head { 0057 struct llist_node *first; 0058 }; 0059 0060 struct llist_node { 0061 struct llist_node *next; 0062 }; 0063 0064 #define LLIST_HEAD_INIT(name) { NULL } 0065 #define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name) 0066 0067 /** 0068 * init_llist_head - initialize lock-less list head 0069 * @head: the head for your lock-less list 0070 */ 0071 static inline void init_llist_head(struct llist_head *list) 0072 { 0073 list->first = NULL; 0074 } 0075 0076 /** 0077 * llist_entry - get the struct of this entry 0078 * @ptr: the &struct llist_node pointer. 0079 * @type: the type of the struct this is embedded in. 0080 * @member: the name of the llist_node within the struct. 0081 */ 0082 #define llist_entry(ptr, type, member) \ 0083 container_of(ptr, type, member) 0084 0085 /** 0086 * member_address_is_nonnull - check whether the member address is not NULL 0087 * @ptr: the object pointer (struct type * that contains the llist_node) 0088 * @member: the name of the llist_node within the struct. 0089 * 0090 * This macro is conceptually the same as 0091 * &ptr->member != NULL 0092 * but it works around the fact that compilers can decide that taking a member 0093 * address is never a NULL pointer. 0094 * 0095 * Real objects that start at a high address and have a member at NULL are 0096 * unlikely to exist, but such pointers may be returned e.g. by the 0097 * container_of() macro. 0098 */ 0099 #define member_address_is_nonnull(ptr, member) \ 0100 ((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0) 0101 0102 /** 0103 * llist_for_each - iterate over some deleted entries of a lock-less list 0104 * @pos: the &struct llist_node to use as a loop cursor 0105 * @node: the first entry of deleted list entries 0106 * 0107 * In general, some entries of the lock-less list can be traversed 0108 * safely only after being deleted from list, so start with an entry 0109 * instead of list head. 0110 * 0111 * If being used on entries deleted from lock-less list directly, the 0112 * traverse order is from the newest to the oldest added entry. If 0113 * you want to traverse from the oldest to the newest, you must 0114 * reverse the order by yourself before traversing. 0115 */ 0116 #define llist_for_each(pos, node) \ 0117 for ((pos) = (node); pos; (pos) = (pos)->next) 0118 0119 /** 0120 * llist_for_each_safe - iterate over some deleted entries of a lock-less list 0121 * safe against removal of list entry 0122 * @pos: the &struct llist_node to use as a loop cursor 0123 * @n: another &struct llist_node to use as temporary storage 0124 * @node: the first entry of deleted list entries 0125 * 0126 * In general, some entries of the lock-less list can be traversed 0127 * safely only after being deleted from list, so start with an entry 0128 * instead of list head. 0129 * 0130 * If being used on entries deleted from lock-less list directly, the 0131 * traverse order is from the newest to the oldest added entry. If 0132 * you want to traverse from the oldest to the newest, you must 0133 * reverse the order by yourself before traversing. 0134 */ 0135 #define llist_for_each_safe(pos, n, node) \ 0136 for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n)) 0137 0138 /** 0139 * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type 0140 * @pos: the type * to use as a loop cursor. 0141 * @node: the fist entry of deleted list entries. 0142 * @member: the name of the llist_node with the struct. 0143 * 0144 * In general, some entries of the lock-less list can be traversed 0145 * safely only after being removed from list, so start with an entry 0146 * instead of list head. 0147 * 0148 * If being used on entries deleted from lock-less list directly, the 0149 * traverse order is from the newest to the oldest added entry. If 0150 * you want to traverse from the oldest to the newest, you must 0151 * reverse the order by yourself before traversing. 0152 */ 0153 #define llist_for_each_entry(pos, node, member) \ 0154 for ((pos) = llist_entry((node), typeof(*(pos)), member); \ 0155 member_address_is_nonnull(pos, member); \ 0156 (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member)) 0157 0158 /** 0159 * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type 0160 * safe against removal of list entry 0161 * @pos: the type * to use as a loop cursor. 0162 * @n: another type * to use as temporary storage 0163 * @node: the first entry of deleted list entries. 0164 * @member: the name of the llist_node with the struct. 0165 * 0166 * In general, some entries of the lock-less list can be traversed 0167 * safely only after being removed from list, so start with an entry 0168 * instead of list head. 0169 * 0170 * If being used on entries deleted from lock-less list directly, the 0171 * traverse order is from the newest to the oldest added entry. If 0172 * you want to traverse from the oldest to the newest, you must 0173 * reverse the order by yourself before traversing. 0174 */ 0175 #define llist_for_each_entry_safe(pos, n, node, member) \ 0176 for (pos = llist_entry((node), typeof(*pos), member); \ 0177 member_address_is_nonnull(pos, member) && \ 0178 (n = llist_entry(pos->member.next, typeof(*n), member), true); \ 0179 pos = n) 0180 0181 /** 0182 * llist_empty - tests whether a lock-less list is empty 0183 * @head: the list to test 0184 * 0185 * Not guaranteed to be accurate or up to date. Just a quick way to 0186 * test whether the list is empty without deleting something from the 0187 * list. 0188 */ 0189 static inline bool llist_empty(const struct llist_head *head) 0190 { 0191 return READ_ONCE(head->first) == NULL; 0192 } 0193 0194 static inline struct llist_node *llist_next(struct llist_node *node) 0195 { 0196 return node->next; 0197 } 0198 0199 extern bool llist_add_batch(struct llist_node *new_first, 0200 struct llist_node *new_last, 0201 struct llist_head *head); 0202 0203 static inline bool __llist_add_batch(struct llist_node *new_first, 0204 struct llist_node *new_last, 0205 struct llist_head *head) 0206 { 0207 new_last->next = head->first; 0208 head->first = new_first; 0209 return new_last->next == NULL; 0210 } 0211 0212 /** 0213 * llist_add - add a new entry 0214 * @new: new entry to be added 0215 * @head: the head for your lock-less list 0216 * 0217 * Returns true if the list was empty prior to adding this entry. 0218 */ 0219 static inline bool llist_add(struct llist_node *new, struct llist_head *head) 0220 { 0221 return llist_add_batch(new, new, head); 0222 } 0223 0224 static inline bool __llist_add(struct llist_node *new, struct llist_head *head) 0225 { 0226 return __llist_add_batch(new, new, head); 0227 } 0228 0229 /** 0230 * llist_del_all - delete all entries from lock-less list 0231 * @head: the head of lock-less list to delete all entries 0232 * 0233 * If list is empty, return NULL, otherwise, delete all entries and 0234 * return the pointer to the first entry. The order of entries 0235 * deleted is from the newest to the oldest added one. 0236 */ 0237 static inline struct llist_node *llist_del_all(struct llist_head *head) 0238 { 0239 return xchg(&head->first, NULL); 0240 } 0241 0242 static inline struct llist_node *__llist_del_all(struct llist_head *head) 0243 { 0244 struct llist_node *first = head->first; 0245 0246 head->first = NULL; 0247 return first; 0248 } 0249 0250 extern struct llist_node *llist_del_first(struct llist_head *head); 0251 0252 struct llist_node *llist_reverse_order(struct llist_node *head); 0253 0254 #endif /* LLIST_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |