hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/include/linux/list.h
....@@ -23,6 +23,13 @@
2323 #define LIST_HEAD(name) \
2424 struct list_head name = LIST_HEAD_INIT(name)
2525
26
+/**
27
+ * INIT_LIST_HEAD - Initialize a list_head structure
28
+ * @list: list_head structure to be initialized.
29
+ *
30
+ * Initializes the list_head to point to itself. If it is a list header,
31
+ * the result is an empty list.
32
+ */
2633 static inline void INIT_LIST_HEAD(struct list_head *list)
2734 {
2835 WRITE_ONCE(list->next, list);
....@@ -106,12 +113,20 @@
106113 WRITE_ONCE(prev->next, next);
107114 }
108115
109
-/**
110
- * list_del - deletes entry from list.
111
- * @entry: the element to delete from the list.
112
- * Note: list_empty() on entry does not return true after this, the entry is
113
- * in an undefined state.
116
+/*
117
+ * Delete a list entry and clear the 'prev' pointer.
118
+ *
119
+ * This is a special-purpose list clearing method used in the networking code
120
+ * for lists allocated as per-cpu, where we don't want to incur the extra
121
+ * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
122
+ * needs to check the node 'prev' pointer instead of calling list_empty().
114123 */
124
+static inline void __list_del_clearprev(struct list_head *entry)
125
+{
126
+ __list_del(entry->prev, entry->next);
127
+ entry->prev = NULL;
128
+}
129
+
115130 static inline void __list_del_entry(struct list_head *entry)
116131 {
117132 if (!__list_del_entry_valid(entry))
....@@ -120,6 +135,12 @@
120135 __list_del(entry->prev, entry->next);
121136 }
122137
138
+/**
139
+ * list_del - deletes entry from list.
140
+ * @entry: the element to delete from the list.
141
+ * Note: list_empty() on entry does not return true after this, the entry is
142
+ * in an undefined state.
143
+ */
123144 static inline void list_del(struct list_head *entry)
124145 {
125146 __list_del_entry(entry);
....@@ -143,11 +164,35 @@
143164 new->prev->next = new;
144165 }
145166
167
+/**
168
+ * list_replace_init - replace old entry by new one and initialize the old one
169
+ * @old : the element to be replaced
170
+ * @new : the new element to insert
171
+ *
172
+ * If @old was empty, it will be overwritten.
173
+ */
146174 static inline void list_replace_init(struct list_head *old,
147
- struct list_head *new)
175
+ struct list_head *new)
148176 {
149177 list_replace(old, new);
150178 INIT_LIST_HEAD(old);
179
+}
180
+
181
+/**
182
+ * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
183
+ * @entry1: the location to place entry2
184
+ * @entry2: the location to place entry1
185
+ */
186
+static inline void list_swap(struct list_head *entry1,
187
+ struct list_head *entry2)
188
+{
189
+ struct list_head *pos = entry2->prev;
190
+
191
+ list_del(entry2);
192
+ list_replace(entry1, entry2);
193
+ if (pos == entry1)
194
+ pos = entry2;
195
+ list_add(entry1, pos);
151196 }
152197
153198 /**
....@@ -184,6 +229,40 @@
184229 }
185230
186231 /**
232
+ * list_bulk_move_tail - move a subsection of a list to its tail
233
+ * @head: the head that will follow our entry
234
+ * @first: first entry to move
235
+ * @last: last entry to move, can be the same as first
236
+ *
237
+ * Move all entries between @first and including @last before @head.
238
+ * All three entries must belong to the same linked list.
239
+ */
240
+static inline void list_bulk_move_tail(struct list_head *head,
241
+ struct list_head *first,
242
+ struct list_head *last)
243
+{
244
+ first->prev->next = last->next;
245
+ last->next->prev = first->prev;
246
+
247
+ head->prev->next = first;
248
+ first->prev = head->prev;
249
+
250
+ last->next = head;
251
+ head->prev = last;
252
+}
253
+
254
+/**
255
+ * list_is_first -- tests whether @list is the first entry in list @head
256
+ * @list: the entry to test
257
+ * @head: the head of the list
258
+ */
259
+static inline int list_is_first(const struct list_head *list,
260
+ const struct list_head *head)
261
+{
262
+ return list->prev == head;
263
+}
264
+
265
+/**
187266 * list_is_last - tests whether @list is the last entry in list @head
188267 * @list: the entry to test
189268 * @head: the head of the list
....@@ -204,6 +283,24 @@
204283 }
205284
206285 /**
286
+ * list_del_init_careful - deletes entry from list and reinitialize it.
287
+ * @entry: the element to delete from the list.
288
+ *
289
+ * This is the same as list_del_init(), except designed to be used
290
+ * together with list_empty_careful() in a way to guarantee ordering
291
+ * of other memory operations.
292
+ *
293
+ * Any memory operations done before a list_del_init_careful() are
294
+ * guaranteed to be visible after a list_empty_careful() test.
295
+ */
296
+static inline void list_del_init_careful(struct list_head *entry)
297
+{
298
+ __list_del_entry(entry);
299
+ entry->prev = entry;
300
+ smp_store_release(&entry->next, entry);
301
+}
302
+
303
+/**
207304 * list_empty_careful - tests whether a list is empty and not being modified
208305 * @head: the list to test
209306 *
....@@ -218,7 +315,7 @@
218315 */
219316 static inline int list_empty_careful(const struct list_head *head)
220317 {
221
- struct list_head *next = head->next;
318
+ struct list_head *next = smp_load_acquire(&head->next);
222319 return (next == head) && (next == head->prev);
223320 }
224321
....@@ -234,6 +331,24 @@
234331 first = head->next;
235332 list_move_tail(first, head);
236333 }
334
+}
335
+
336
+/**
337
+ * list_rotate_to_front() - Rotate list to specific item.
338
+ * @list: The desired new front of the list.
339
+ * @head: The head of the list.
340
+ *
341
+ * Rotates list so that @list becomes the new front of the list.
342
+ */
343
+static inline void list_rotate_to_front(struct list_head *list,
344
+ struct list_head *head)
345
+{
346
+ /*
347
+ * Deletes the list head from the list denoted by @head and
348
+ * places it as the tail of @list, this effectively rotates the
349
+ * list so that @list is at the front.
350
+ */
351
+ list_move_tail(head, list);
237352 }
238353
239354 /**
....@@ -456,6 +571,16 @@
456571 for (pos = (head)->next; pos != (head); pos = pos->next)
457572
458573 /**
574
+ * list_for_each_continue - continue iteration over a list
575
+ * @pos: the &struct list_head to use as a loop cursor.
576
+ * @head: the head for your list.
577
+ *
578
+ * Continue to iterate over a list, continuing after the current position.
579
+ */
580
+#define list_for_each_continue(pos, head) \
581
+ for (pos = pos->next; pos != (head); pos = pos->next)
582
+
583
+/**
459584 * list_for_each_prev - iterate over a list backwards
460585 * @pos: the &struct list_head to use as a loop cursor.
461586 * @head: the head for your list.
....@@ -670,11 +795,36 @@
670795 h->pprev = NULL;
671796 }
672797
798
+/**
799
+ * hlist_unhashed - Has node been removed from list and reinitialized?
800
+ * @h: Node to be checked
801
+ *
802
+ * Not that not all removal functions will leave a node in unhashed
803
+ * state. For example, hlist_nulls_del_init_rcu() does leave the
804
+ * node in unhashed state, but hlist_nulls_del() does not.
805
+ */
673806 static inline int hlist_unhashed(const struct hlist_node *h)
674807 {
675808 return !h->pprev;
676809 }
677810
811
+/**
812
+ * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
813
+ * @h: Node to be checked
814
+ *
815
+ * This variant of hlist_unhashed() must be used in lockless contexts
816
+ * to avoid potential load-tearing. The READ_ONCE() is paired with the
817
+ * various WRITE_ONCE() in hlist helpers that are defined below.
818
+ */
819
+static inline int hlist_unhashed_lockless(const struct hlist_node *h)
820
+{
821
+ return !READ_ONCE(h->pprev);
822
+}
823
+
824
+/**
825
+ * hlist_empty - Is the specified hlist_head structure an empty hlist?
826
+ * @h: Structure to check.
827
+ */
678828 static inline int hlist_empty(const struct hlist_head *h)
679829 {
680830 return !READ_ONCE(h->first);
....@@ -687,9 +837,16 @@
687837
688838 WRITE_ONCE(*pprev, next);
689839 if (next)
690
- next->pprev = pprev;
840
+ WRITE_ONCE(next->pprev, pprev);
691841 }
692842
843
+/**
844
+ * hlist_del - Delete the specified hlist_node from its list
845
+ * @n: Node to delete.
846
+ *
847
+ * Note that this function leaves the node in hashed state. Use
848
+ * hlist_del_init() or similar instead to unhash @n.
849
+ */
693850 static inline void hlist_del(struct hlist_node *n)
694851 {
695852 __hlist_del(n);
....@@ -697,6 +854,12 @@
697854 n->pprev = LIST_POISON2;
698855 }
699856
857
+/**
858
+ * hlist_del_init - Delete the specified hlist_node from its list and initialize
859
+ * @n: Node to delete.
860
+ *
861
+ * Note that this function leaves the node in unhashed state.
862
+ */
700863 static inline void hlist_del_init(struct hlist_node *n)
701864 {
702865 if (!hlist_unhashed(n)) {
....@@ -705,51 +868,83 @@
705868 }
706869 }
707870
871
+/**
872
+ * hlist_add_head - add a new entry at the beginning of the hlist
873
+ * @n: new entry to be added
874
+ * @h: hlist head to add it after
875
+ *
876
+ * Insert a new entry after the specified head.
877
+ * This is good for implementing stacks.
878
+ */
708879 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
709880 {
710881 struct hlist_node *first = h->first;
711
- n->next = first;
882
+ WRITE_ONCE(n->next, first);
712883 if (first)
713
- first->pprev = &n->next;
884
+ WRITE_ONCE(first->pprev, &n->next);
714885 WRITE_ONCE(h->first, n);
715
- n->pprev = &h->first;
886
+ WRITE_ONCE(n->pprev, &h->first);
716887 }
717888
718
-/* next must be != NULL */
889
+/**
890
+ * hlist_add_before - add a new entry before the one specified
891
+ * @n: new entry to be added
892
+ * @next: hlist node to add it before, which must be non-NULL
893
+ */
719894 static inline void hlist_add_before(struct hlist_node *n,
720
- struct hlist_node *next)
895
+ struct hlist_node *next)
721896 {
722
- n->pprev = next->pprev;
723
- n->next = next;
724
- next->pprev = &n->next;
897
+ WRITE_ONCE(n->pprev, next->pprev);
898
+ WRITE_ONCE(n->next, next);
899
+ WRITE_ONCE(next->pprev, &n->next);
725900 WRITE_ONCE(*(n->pprev), n);
726901 }
727902
903
+/**
904
+ * hlist_add_behing - add a new entry after the one specified
905
+ * @n: new entry to be added
906
+ * @prev: hlist node to add it after, which must be non-NULL
907
+ */
728908 static inline void hlist_add_behind(struct hlist_node *n,
729909 struct hlist_node *prev)
730910 {
731
- n->next = prev->next;
911
+ WRITE_ONCE(n->next, prev->next);
732912 WRITE_ONCE(prev->next, n);
733
- n->pprev = &prev->next;
913
+ WRITE_ONCE(n->pprev, &prev->next);
734914
735915 if (n->next)
736
- n->next->pprev = &n->next;
916
+ WRITE_ONCE(n->next->pprev, &n->next);
737917 }
738918
739
-/* after that we'll appear to be on some hlist and hlist_del will work */
919
+/**
920
+ * hlist_add_fake - create a fake hlist consisting of a single headless node
921
+ * @n: Node to make a fake list out of
922
+ *
923
+ * This makes @n appear to be its own predecessor on a headless hlist.
924
+ * The point of this is to allow things like hlist_del() to work correctly
925
+ * in cases where there is no list.
926
+ */
740927 static inline void hlist_add_fake(struct hlist_node *n)
741928 {
742929 n->pprev = &n->next;
743930 }
744931
932
+/**
933
+ * hlist_fake: Is this node a fake hlist?
934
+ * @h: Node to check for being a self-referential fake hlist.
935
+ */
745936 static inline bool hlist_fake(struct hlist_node *h)
746937 {
747938 return h->pprev == &h->next;
748939 }
749940
750
-/*
941
+/**
942
+ * hlist_is_singular_node - is node the only element of the specified hlist?
943
+ * @n: Node to check for singularity.
944
+ * @h: Header for potentially singular list.
945
+ *
751946 * Check whether the node is the only node of the head without
752
- * accessing head:
947
+ * accessing head, thus avoiding unnecessary cache misses.
753948 */
754949 static inline bool
755950 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
....@@ -757,7 +952,11 @@
757952 return !n->next && n->pprev == &h->first;
758953 }
759954
760
-/*
955
+/**
956
+ * hlist_move_list - Move an hlist
957
+ * @old: hlist_head for old list.
958
+ * @new: hlist_head for new list.
959
+ *
761960 * Move a list from one list head to another. Fixup the pprev
762961 * reference of the first entry if it exists.
763962 */
....@@ -817,7 +1016,7 @@
8171016 /**
8181017 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
8191018 * @pos: the type * to use as a loop cursor.
820
- * @n: another &struct hlist_node to use as temporary storage
1019
+ * @n: a &struct hlist_node to use as temporary storage
8211020 * @head: the head for your list.
8221021 * @member: the name of the hlist_node within the struct.
8231022 */