1
2#ifndef _LINUX_LIST_H
3#define _LINUX_LIST_H
4
5#include <linux/container_of.h>
6#include <linux/types.h>
7#include <linux/stddef.h>
8#include <linux/poison.h>
9#include <linux/const.h>
10
11#include <asm/barrier.h>
12
13
14
15
16
17
18
19
20
21
22
23#define LIST_HEAD_INIT(name) { &(name), &(name) }
24
25#define LIST_HEAD(name) \
26 struct list_head name = LIST_HEAD_INIT(name)
27
28
29
30
31
32
33
34
35static inline void INIT_LIST_HEAD(struct list_head *list)
36{
37 WRITE_ONCE(list->next, list);
38 list->prev = list;
39}
40
41#ifdef CONFIG_DEBUG_LIST
42extern bool __list_add_valid(struct list_head *new,
43 struct list_head *prev,
44 struct list_head *next);
45extern bool __list_del_entry_valid(struct list_head *entry);
46#else
47static inline bool __list_add_valid(struct list_head *new,
48 struct list_head *prev,
49 struct list_head *next)
50{
51 return true;
52}
53static inline bool __list_del_entry_valid(struct list_head *entry)
54{
55 return true;
56}
57#endif
58
59
60
61
62
63
64
65static inline void __list_add(struct list_head *new,
66 struct list_head *prev,
67 struct list_head *next)
68{
69 if (!__list_add_valid(new, prev, next))
70 return;
71
72 next->prev = new;
73 new->next = next;
74 new->prev = prev;
75 WRITE_ONCE(prev->next, new);
76}
77
78
79
80
81
82
83
84
85
86static inline void list_add(struct list_head *new, struct list_head *head)
87{
88 __list_add(new, head, head->next);
89}
90
91
92
93
94
95
96
97
98
99
100static inline void list_add_tail(struct list_head *new, struct list_head *head)
101{
102 __list_add(new, head->prev, head);
103}
104
105
106
107
108
109
110
111
112static inline void __list_del(struct list_head * prev, struct list_head * next)
113{
114 next->prev = prev;
115 WRITE_ONCE(prev->next, next);
116}
117
118
119
120
121
122
123
124
125
126static inline void __list_del_clearprev(struct list_head *entry)
127{
128 __list_del(entry->prev, entry->next);
129 entry->prev = NULL;
130}
131
132static inline void __list_del_entry(struct list_head *entry)
133{
134 if (!__list_del_entry_valid(entry))
135 return;
136
137 __list_del(entry->prev, entry->next);
138}
139
140
141
142
143
144
145
146static inline void list_del(struct list_head *entry)
147{
148 __list_del_entry(entry);
149 entry->next = LIST_POISON1;
150 entry->prev = LIST_POISON2;
151}
152
153
154
155
156
157
158
159
160static inline void list_replace(struct list_head *old,
161 struct list_head *new)
162{
163 new->next = old->next;
164 new->next->prev = new;
165 new->prev = old->prev;
166 new->prev->next = new;
167}
168
169
170
171
172
173
174
175
176static inline void list_replace_init(struct list_head *old,
177 struct list_head *new)
178{
179 list_replace(old, new);
180 INIT_LIST_HEAD(old);
181}
182
183
184
185
186
187
188static inline void list_swap(struct list_head *entry1,
189 struct list_head *entry2)
190{
191 struct list_head *pos = entry2->prev;
192
193 list_del(entry2);
194 list_replace(entry1, entry2);
195 if (pos == entry1)
196 pos = entry2;
197 list_add(entry1, pos);
198}
199
200
201
202
203
204static inline void list_del_init(struct list_head *entry)
205{
206 __list_del_entry(entry);
207 INIT_LIST_HEAD(entry);
208}
209
210
211
212
213
214
215static inline void list_move(struct list_head *list, struct list_head *head)
216{
217 __list_del_entry(list);
218 list_add(list, head);
219}
220
221
222
223
224
225
226static inline void list_move_tail(struct list_head *list,
227 struct list_head *head)
228{
229 __list_del_entry(list);
230 list_add_tail(list, head);
231}
232
233
234
235
236
237
238
239
240
241
242static inline void list_bulk_move_tail(struct list_head *head,
243 struct list_head *first,
244 struct list_head *last)
245{
246 first->prev->next = last->next;
247 last->next->prev = first->prev;
248
249 head->prev->next = first;
250 first->prev = head->prev;
251
252 last->next = head;
253 head->prev = last;
254}
255
256
257
258
259
260
261static inline int list_is_first(const struct list_head *list, const struct list_head *head)
262{
263 return list->prev == head;
264}
265
266
267
268
269
270
271static inline int list_is_last(const struct list_head *list, const struct list_head *head)
272{
273 return list->next == head;
274}
275
276
277
278
279
280
281static inline int list_is_head(const struct list_head *list, const struct list_head *head)
282{
283 return list == head;
284}
285
286
287
288
289
290static inline int list_empty(const struct list_head *head)
291{
292 return READ_ONCE(head->next) == head;
293}
294
295
296
297
298
299
300
301
302
303
304
305
306static inline void list_del_init_careful(struct list_head *entry)
307{
308 __list_del_entry(entry);
309 entry->prev = entry;
310 smp_store_release(&entry->next, entry);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326static inline int list_empty_careful(const struct list_head *head)
327{
328 struct list_head *next = smp_load_acquire(&head->next);
329 return list_is_head(next, head) && (next == head->prev);
330}
331
332
333
334
335
336static inline void list_rotate_left(struct list_head *head)
337{
338 struct list_head *first;
339
340 if (!list_empty(head)) {
341 first = head->next;
342 list_move_tail(first, head);
343 }
344}
345
346
347
348
349
350
351
352
353static inline void list_rotate_to_front(struct list_head *list,
354 struct list_head *head)
355{
356
357
358
359
360
361 list_move_tail(head, list);
362}
363
364
365
366
367
368static inline int list_is_singular(const struct list_head *head)
369{
370 return !list_empty(head) && (head->next == head->prev);
371}
372
373static inline void __list_cut_position(struct list_head *list,
374 struct list_head *head, struct list_head *entry)
375{
376 struct list_head *new_first = entry->next;
377 list->next = head->next;
378 list->next->prev = list;
379 list->prev = entry;
380 entry->next = list;
381 head->next = new_first;
382 new_first->prev = head;
383}
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399static inline void list_cut_position(struct list_head *list,
400 struct list_head *head, struct list_head *entry)
401{
402 if (list_empty(head))
403 return;
404 if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
405 return;
406 if (list_is_head(entry, head))
407 INIT_LIST_HEAD(list);
408 else
409 __list_cut_position(list, head, entry);
410}
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426static inline void list_cut_before(struct list_head *list,
427 struct list_head *head,
428 struct list_head *entry)
429{
430 if (head->next == entry) {
431 INIT_LIST_HEAD(list);
432 return;
433 }
434 list->next = head->next;
435 list->next->prev = list;
436 list->prev = entry->prev;
437 list->prev->next = list;
438 head->next = entry;
439 entry->prev = head;
440}
441
442static inline void __list_splice(const struct list_head *list,
443 struct list_head *prev,
444 struct list_head *next)
445{
446 struct list_head *first = list->next;
447 struct list_head *last = list->prev;
448
449 first->prev = prev;
450 prev->next = first;
451
452 last->next = next;
453 next->prev = last;
454}
455
456
457
458
459
460
461static inline void list_splice(const struct list_head *list,
462 struct list_head *head)
463{
464 if (!list_empty(list))
465 __list_splice(list, head, head->next);
466}
467
468
469
470
471
472
473static inline void list_splice_tail(struct list_head *list,
474 struct list_head *head)
475{
476 if (!list_empty(list))
477 __list_splice(list, head->prev, head);
478}
479
480
481
482
483
484
485
486
487static inline void list_splice_init(struct list_head *list,
488 struct list_head *head)
489{
490 if (!list_empty(list)) {
491 __list_splice(list, head, head->next);
492 INIT_LIST_HEAD(list);
493 }
494}
495
496
497
498
499
500
501
502
503
504static inline void list_splice_tail_init(struct list_head *list,
505 struct list_head *head)
506{
507 if (!list_empty(list)) {
508 __list_splice(list, head->prev, head);
509 INIT_LIST_HEAD(list);
510 }
511}
512
513
514
515
516
517
518
519#define list_entry(ptr, type, member) \
520 container_of(ptr, type, member)
521
522
523
524
525
526
527
528
529
530#define list_first_entry(ptr, type, member) \
531 list_entry((ptr)->next, type, member)
532
533
534
535
536
537
538
539
540
541#define list_last_entry(ptr, type, member) \
542 list_entry((ptr)->prev, type, member)
543
544
545
546
547
548
549
550
551
552#define list_first_entry_or_null(ptr, type, member) ({ \
553 struct list_head *head__ = (ptr); \
554 struct list_head *pos__ = READ_ONCE(head__->next); \
555 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
556})
557
558
559
560
561
562
563#define list_next_entry(pos, member) \
564 list_entry((pos)->member.next, typeof(*(pos)), member)
565
566
567
568
569
570
571#define list_prev_entry(pos, member) \
572 list_entry((pos)->member.prev, typeof(*(pos)), member)
573
574
575
576
577
578
579#define list_for_each(pos, head) \
580 for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
581
582
583
584
585
586
587
588
589#define list_for_each_continue(pos, head) \
590 for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
591
592
593
594
595
596
597#define list_for_each_prev(pos, head) \
598 for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
599
600
601
602
603
604
605
606#define list_for_each_safe(pos, n, head) \
607 for (pos = (head)->next, n = pos->next; \
608 !list_is_head(pos, (head)); \
609 pos = n, n = pos->next)
610
611
612
613
614
615
616
617#define list_for_each_prev_safe(pos, n, head) \
618 for (pos = (head)->prev, n = pos->prev; \
619 !list_is_head(pos, (head)); \
620 pos = n, n = pos->prev)
621
622
623
624
625
626
627
628#define list_entry_is_head(pos, head, member) \
629 (&pos->member == (head))
630
631
632
633
634
635
636
637#define list_for_each_entry(pos, head, member) \
638 for (pos = list_first_entry(head, typeof(*pos), member); \
639 !list_entry_is_head(pos, head, member); \
640 pos = list_next_entry(pos, member))
641
642
643
644
645
646
647
648#define list_for_each_entry_reverse(pos, head, member) \
649 for (pos = list_last_entry(head, typeof(*pos), member); \
650 !list_entry_is_head(pos, head, member); \
651 pos = list_prev_entry(pos, member))
652
653
654
655
656
657
658
659
660
661#define list_prepare_entry(pos, head, member) \
662 ((pos) ? : list_entry(head, typeof(*pos), member))
663
664
665
666
667
668
669
670
671
672
673#define list_for_each_entry_continue(pos, head, member) \
674 for (pos = list_next_entry(pos, member); \
675 !list_entry_is_head(pos, head, member); \
676 pos = list_next_entry(pos, member))
677
678
679
680
681
682
683
684
685
686
687#define list_for_each_entry_continue_reverse(pos, head, member) \
688 for (pos = list_prev_entry(pos, member); \
689 !list_entry_is_head(pos, head, member); \
690 pos = list_prev_entry(pos, member))
691
692
693
694
695
696
697
698
699
700#define list_for_each_entry_from(pos, head, member) \
701 for (; !list_entry_is_head(pos, head, member); \
702 pos = list_next_entry(pos, member))
703
704
705
706
707
708
709
710
711
712
713#define list_for_each_entry_from_reverse(pos, head, member) \
714 for (; !list_entry_is_head(pos, head, member); \
715 pos = list_prev_entry(pos, member))
716
717
718
719
720
721
722
723
724#define list_for_each_entry_safe(pos, n, head, member) \
725 for (pos = list_first_entry(head, typeof(*pos), member), \
726 n = list_next_entry(pos, member); \
727 !list_entry_is_head(pos, head, member); \
728 pos = n, n = list_next_entry(n, member))
729
730
731
732
733
734
735
736
737
738
739
740#define list_for_each_entry_safe_continue(pos, n, head, member) \
741 for (pos = list_next_entry(pos, member), \
742 n = list_next_entry(pos, member); \
743 !list_entry_is_head(pos, head, member); \
744 pos = n, n = list_next_entry(n, member))
745
746
747
748
749
750
751
752
753
754
755
756#define list_for_each_entry_safe_from(pos, n, head, member) \
757 for (n = list_next_entry(pos, member); \
758 !list_entry_is_head(pos, head, member); \
759 pos = n, n = list_next_entry(n, member))
760
761
762
763
764
765
766
767
768
769
770
771#define list_for_each_entry_safe_reverse(pos, n, head, member) \
772 for (pos = list_last_entry(head, typeof(*pos), member), \
773 n = list_prev_entry(pos, member); \
774 !list_entry_is_head(pos, head, member); \
775 pos = n, n = list_prev_entry(n, member))
776
777
778
779
780
781
782
783
784
785
786
787
788
789#define list_safe_reset_next(pos, n, member) \
790 n = list_next_entry(pos, member)
791
792
793
794
795
796
797
798
799#define HLIST_HEAD_INIT { .first = NULL }
800#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
801#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
802static inline void INIT_HLIST_NODE(struct hlist_node *h)
803{
804 h->next = NULL;
805 h->pprev = NULL;
806}
807
808
809
810
811
812
813
814
815
816static inline int hlist_unhashed(const struct hlist_node *h)
817{
818 return !h->pprev;
819}
820
821
822
823
824
825
826
827
828
829static inline int hlist_unhashed_lockless(const struct hlist_node *h)
830{
831 return !READ_ONCE(h->pprev);
832}
833
834
835
836
837
838static inline int hlist_empty(const struct hlist_head *h)
839{
840 return !READ_ONCE(h->first);
841}
842
843static inline void __hlist_del(struct hlist_node *n)
844{
845 struct hlist_node *next = n->next;
846 struct hlist_node **pprev = n->pprev;
847
848 WRITE_ONCE(*pprev, next);
849 if (next)
850 WRITE_ONCE(next->pprev, pprev);
851}
852
853
854
855
856
857
858
859
860static inline void hlist_del(struct hlist_node *n)
861{
862 __hlist_del(n);
863 n->next = LIST_POISON1;
864 n->pprev = LIST_POISON2;
865}
866
867
868
869
870
871
872
873static inline void hlist_del_init(struct hlist_node *n)
874{
875 if (!hlist_unhashed(n)) {
876 __hlist_del(n);
877 INIT_HLIST_NODE(n);
878 }
879}
880
881
882
883
884
885
886
887
888
889static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
890{
891 struct hlist_node *first = h->first;
892 WRITE_ONCE(n->next, first);
893 if (first)
894 WRITE_ONCE(first->pprev, &n->next);
895 WRITE_ONCE(h->first, n);
896 WRITE_ONCE(n->pprev, &h->first);
897}
898
899
900
901
902
903
904static inline void hlist_add_before(struct hlist_node *n,
905 struct hlist_node *next)
906{
907 WRITE_ONCE(n->pprev, next->pprev);
908 WRITE_ONCE(n->next, next);
909 WRITE_ONCE(next->pprev, &n->next);
910 WRITE_ONCE(*(n->pprev), n);
911}
912
913
914
915
916
917
918static inline void hlist_add_behind(struct hlist_node *n,
919 struct hlist_node *prev)
920{
921 WRITE_ONCE(n->next, prev->next);
922 WRITE_ONCE(prev->next, n);
923 WRITE_ONCE(n->pprev, &prev->next);
924
925 if (n->next)
926 WRITE_ONCE(n->next->pprev, &n->next);
927}
928
929
930
931
932
933
934
935
936
937static inline void hlist_add_fake(struct hlist_node *n)
938{
939 n->pprev = &n->next;
940}
941
942
943
944
945
946static inline bool hlist_fake(struct hlist_node *h)
947{
948 return h->pprev == &h->next;
949}
950
951
952
953
954
955
956
957
958
959static inline bool
960hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
961{
962 return !n->next && n->pprev == &h->first;
963}
964
965
966
967
968
969
970
971
972
973static inline void hlist_move_list(struct hlist_head *old,
974 struct hlist_head *new)
975{
976 new->first = old->first;
977 if (new->first)
978 new->first->pprev = &new->first;
979 old->first = NULL;
980}
981
982#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
983
984#define hlist_for_each(pos, head) \
985 for (pos = (head)->first; pos ; pos = pos->next)
986
987#define hlist_for_each_safe(pos, n, head) \
988 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
989 pos = n)
990
991#define hlist_entry_safe(ptr, type, member) \
992 ({ typeof(ptr) ____ptr = (ptr); \
993 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
994 })
995
996
997
998
999
1000
1001
1002#define hlist_for_each_entry(pos, head, member) \
1003 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1004 pos; \
1005 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1006
1007
1008
1009
1010
1011
1012#define hlist_for_each_entry_continue(pos, member) \
1013 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1014 pos; \
1015 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1016
1017
1018
1019
1020
1021
1022#define hlist_for_each_entry_from(pos, member) \
1023 for (; pos; \
1024 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1025
1026
1027
1028
1029
1030
1031
1032
1033#define hlist_for_each_entry_safe(pos, n, head, member) \
1034 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1035 pos && ({ n = pos->member.next; 1; }); \
1036 pos = hlist_entry_safe(n, typeof(*pos), member))
1037
1038#endif
1039