0001
0002 #ifndef _LINUX_LIST_H
0003 #define _LINUX_LIST_H
0004
0005 #include <linux/container_of.h>
0006 #include <linux/types.h>
0007 #include <linux/stddef.h>
0008 #include <linux/poison.h>
0009 #include <linux/const.h>
0010
0011 #include <asm/barrier.h>
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #define LIST_HEAD_INIT(name) { &(name), &(name) }
0024
0025 #define LIST_HEAD(name) \
0026 struct list_head name = LIST_HEAD_INIT(name)
0027
0028
0029
0030
0031
0032
0033
0034
0035 static inline void INIT_LIST_HEAD(struct list_head *list)
0036 {
0037 WRITE_ONCE(list->next, list);
0038 WRITE_ONCE(list->prev, list);
0039 }
0040
0041 #ifdef CONFIG_DEBUG_LIST
0042 extern bool __list_add_valid(struct list_head *new,
0043 struct list_head *prev,
0044 struct list_head *next);
0045 extern bool __list_del_entry_valid(struct list_head *entry);
0046 #else
0047 static inline bool __list_add_valid(struct list_head *new,
0048 struct list_head *prev,
0049 struct list_head *next)
0050 {
0051 return true;
0052 }
0053 static inline bool __list_del_entry_valid(struct list_head *entry)
0054 {
0055 return true;
0056 }
0057 #endif
0058
0059
0060
0061
0062
0063
0064
0065 static inline void __list_add(struct list_head *new,
0066 struct list_head *prev,
0067 struct list_head *next)
0068 {
0069 if (!__list_add_valid(new, prev, next))
0070 return;
0071
0072 next->prev = new;
0073 new->next = next;
0074 new->prev = prev;
0075 WRITE_ONCE(prev->next, new);
0076 }
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 static inline void list_add(struct list_head *new, struct list_head *head)
0087 {
0088 __list_add(new, head, head->next);
0089 }
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 static inline void list_add_tail(struct list_head *new, struct list_head *head)
0101 {
0102 __list_add(new, head->prev, head);
0103 }
0104
0105
0106
0107
0108
0109
0110
0111
0112 static inline void __list_del(struct list_head * prev, struct list_head * next)
0113 {
0114 next->prev = prev;
0115 WRITE_ONCE(prev->next, next);
0116 }
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 static inline void __list_del_clearprev(struct list_head *entry)
0127 {
0128 __list_del(entry->prev, entry->next);
0129 entry->prev = NULL;
0130 }
0131
0132 static inline void __list_del_entry(struct list_head *entry)
0133 {
0134 if (!__list_del_entry_valid(entry))
0135 return;
0136
0137 __list_del(entry->prev, entry->next);
0138 }
0139
0140
0141
0142
0143
0144
0145
0146 static inline void list_del(struct list_head *entry)
0147 {
0148 __list_del_entry(entry);
0149 entry->next = LIST_POISON1;
0150 entry->prev = LIST_POISON2;
0151 }
0152
0153
0154
0155
0156
0157
0158
0159
0160 static inline void list_replace(struct list_head *old,
0161 struct list_head *new)
0162 {
0163 new->next = old->next;
0164 new->next->prev = new;
0165 new->prev = old->prev;
0166 new->prev->next = new;
0167 }
0168
0169
0170
0171
0172
0173
0174
0175
0176 static inline void list_replace_init(struct list_head *old,
0177 struct list_head *new)
0178 {
0179 list_replace(old, new);
0180 INIT_LIST_HEAD(old);
0181 }
0182
0183
0184
0185
0186
0187
0188 static inline void list_swap(struct list_head *entry1,
0189 struct list_head *entry2)
0190 {
0191 struct list_head *pos = entry2->prev;
0192
0193 list_del(entry2);
0194 list_replace(entry1, entry2);
0195 if (pos == entry1)
0196 pos = entry2;
0197 list_add(entry1, pos);
0198 }
0199
0200
0201
0202
0203
0204 static inline void list_del_init(struct list_head *entry)
0205 {
0206 __list_del_entry(entry);
0207 INIT_LIST_HEAD(entry);
0208 }
0209
0210
0211
0212
0213
0214
0215 static inline void list_move(struct list_head *list, struct list_head *head)
0216 {
0217 __list_del_entry(list);
0218 list_add(list, head);
0219 }
0220
0221
0222
0223
0224
0225
0226 static inline void list_move_tail(struct list_head *list,
0227 struct list_head *head)
0228 {
0229 __list_del_entry(list);
0230 list_add_tail(list, head);
0231 }
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static inline void list_bulk_move_tail(struct list_head *head,
0243 struct list_head *first,
0244 struct list_head *last)
0245 {
0246 first->prev->next = last->next;
0247 last->next->prev = first->prev;
0248
0249 head->prev->next = first;
0250 first->prev = head->prev;
0251
0252 last->next = head;
0253 head->prev = last;
0254 }
0255
0256
0257
0258
0259
0260
0261 static inline int list_is_first(const struct list_head *list, const struct list_head *head)
0262 {
0263 return list->prev == head;
0264 }
0265
0266
0267
0268
0269
0270
0271 static inline int list_is_last(const struct list_head *list, const struct list_head *head)
0272 {
0273 return list->next == head;
0274 }
0275
0276
0277
0278
0279
0280
0281 static inline int list_is_head(const struct list_head *list, const struct list_head *head)
0282 {
0283 return list == head;
0284 }
0285
0286
0287
0288
0289
0290 static inline int list_empty(const struct list_head *head)
0291 {
0292 return READ_ONCE(head->next) == head;
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 static inline void list_del_init_careful(struct list_head *entry)
0307 {
0308 __list_del_entry(entry);
0309 WRITE_ONCE(entry->prev, entry);
0310 smp_store_release(&entry->next, entry);
0311 }
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326 static inline int list_empty_careful(const struct list_head *head)
0327 {
0328 struct list_head *next = smp_load_acquire(&head->next);
0329 return list_is_head(next, head) && (next == READ_ONCE(head->prev));
0330 }
0331
0332
0333
0334
0335
0336 static inline void list_rotate_left(struct list_head *head)
0337 {
0338 struct list_head *first;
0339
0340 if (!list_empty(head)) {
0341 first = head->next;
0342 list_move_tail(first, head);
0343 }
0344 }
0345
0346
0347
0348
0349
0350
0351
0352
0353 static inline void list_rotate_to_front(struct list_head *list,
0354 struct list_head *head)
0355 {
0356
0357
0358
0359
0360
0361 list_move_tail(head, list);
0362 }
0363
0364
0365
0366
0367
0368 static inline int list_is_singular(const struct list_head *head)
0369 {
0370 return !list_empty(head) && (head->next == head->prev);
0371 }
0372
0373 static inline void __list_cut_position(struct list_head *list,
0374 struct list_head *head, struct list_head *entry)
0375 {
0376 struct list_head *new_first = entry->next;
0377 list->next = head->next;
0378 list->next->prev = list;
0379 list->prev = entry;
0380 entry->next = list;
0381 head->next = new_first;
0382 new_first->prev = head;
0383 }
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399 static inline void list_cut_position(struct list_head *list,
0400 struct list_head *head, struct list_head *entry)
0401 {
0402 if (list_empty(head))
0403 return;
0404 if (list_is_singular(head) && !list_is_head(entry, head) && (entry != head->next))
0405 return;
0406 if (list_is_head(entry, head))
0407 INIT_LIST_HEAD(list);
0408 else
0409 __list_cut_position(list, head, entry);
0410 }
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426 static inline void list_cut_before(struct list_head *list,
0427 struct list_head *head,
0428 struct list_head *entry)
0429 {
0430 if (head->next == entry) {
0431 INIT_LIST_HEAD(list);
0432 return;
0433 }
0434 list->next = head->next;
0435 list->next->prev = list;
0436 list->prev = entry->prev;
0437 list->prev->next = list;
0438 head->next = entry;
0439 entry->prev = head;
0440 }
0441
0442 static inline void __list_splice(const struct list_head *list,
0443 struct list_head *prev,
0444 struct list_head *next)
0445 {
0446 struct list_head *first = list->next;
0447 struct list_head *last = list->prev;
0448
0449 first->prev = prev;
0450 prev->next = first;
0451
0452 last->next = next;
0453 next->prev = last;
0454 }
0455
0456
0457
0458
0459
0460
0461 static inline void list_splice(const struct list_head *list,
0462 struct list_head *head)
0463 {
0464 if (!list_empty(list))
0465 __list_splice(list, head, head->next);
0466 }
0467
0468
0469
0470
0471
0472
0473 static inline void list_splice_tail(struct list_head *list,
0474 struct list_head *head)
0475 {
0476 if (!list_empty(list))
0477 __list_splice(list, head->prev, head);
0478 }
0479
0480
0481
0482
0483
0484
0485
0486
0487 static inline void list_splice_init(struct list_head *list,
0488 struct list_head *head)
0489 {
0490 if (!list_empty(list)) {
0491 __list_splice(list, head, head->next);
0492 INIT_LIST_HEAD(list);
0493 }
0494 }
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504 static inline void list_splice_tail_init(struct list_head *list,
0505 struct list_head *head)
0506 {
0507 if (!list_empty(list)) {
0508 __list_splice(list, head->prev, head);
0509 INIT_LIST_HEAD(list);
0510 }
0511 }
0512
0513
0514
0515
0516
0517
0518
0519 #define list_entry(ptr, type, member) \
0520 container_of(ptr, type, member)
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530 #define list_first_entry(ptr, type, member) \
0531 list_entry((ptr)->next, type, member)
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541 #define list_last_entry(ptr, type, member) \
0542 list_entry((ptr)->prev, type, member)
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552 #define list_first_entry_or_null(ptr, type, member) ({ \
0553 struct list_head *head__ = (ptr); \
0554 struct list_head *pos__ = READ_ONCE(head__->next); \
0555 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
0556 })
0557
0558
0559
0560
0561
0562
0563 #define list_next_entry(pos, member) \
0564 list_entry((pos)->member.next, typeof(*(pos)), member)
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575 #define list_next_entry_circular(pos, head, member) \
0576 (list_is_last(&(pos)->member, head) ? \
0577 list_first_entry(head, typeof(*(pos)), member) : list_next_entry(pos, member))
0578
0579
0580
0581
0582
0583
0584 #define list_prev_entry(pos, member) \
0585 list_entry((pos)->member.prev, typeof(*(pos)), member)
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596 #define list_prev_entry_circular(pos, head, member) \
0597 (list_is_first(&(pos)->member, head) ? \
0598 list_last_entry(head, typeof(*(pos)), member) : list_prev_entry(pos, member))
0599
0600
0601
0602
0603
0604
0605 #define list_for_each(pos, head) \
0606 for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
0607
0608
0609
0610
0611
0612
0613 #define list_for_each_rcu(pos, head) \
0614 for (pos = rcu_dereference((head)->next); \
0615 !list_is_head(pos, (head)); \
0616 pos = rcu_dereference(pos->next))
0617
0618
0619
0620
0621
0622
0623
0624
0625 #define list_for_each_continue(pos, head) \
0626 for (pos = pos->next; !list_is_head(pos, (head)); pos = pos->next)
0627
0628
0629
0630
0631
0632
0633 #define list_for_each_prev(pos, head) \
0634 for (pos = (head)->prev; !list_is_head(pos, (head)); pos = pos->prev)
0635
0636
0637
0638
0639
0640
0641
0642 #define list_for_each_safe(pos, n, head) \
0643 for (pos = (head)->next, n = pos->next; \
0644 !list_is_head(pos, (head)); \
0645 pos = n, n = pos->next)
0646
0647
0648
0649
0650
0651
0652
0653 #define list_for_each_prev_safe(pos, n, head) \
0654 for (pos = (head)->prev, n = pos->prev; \
0655 !list_is_head(pos, (head)); \
0656 pos = n, n = pos->prev)
0657
0658
0659
0660
0661
0662
0663
0664 #define list_entry_is_head(pos, head, member) \
0665 (&pos->member == (head))
0666
0667
0668
0669
0670
0671
0672
0673 #define list_for_each_entry(pos, head, member) \
0674 for (pos = list_first_entry(head, typeof(*pos), member); \
0675 !list_entry_is_head(pos, head, member); \
0676 pos = list_next_entry(pos, member))
0677
0678
0679
0680
0681
0682
0683
0684 #define list_for_each_entry_reverse(pos, head, member) \
0685 for (pos = list_last_entry(head, typeof(*pos), member); \
0686 !list_entry_is_head(pos, head, member); \
0687 pos = list_prev_entry(pos, member))
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697 #define list_prepare_entry(pos, head, member) \
0698 ((pos) ? : list_entry(head, typeof(*pos), member))
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709 #define list_for_each_entry_continue(pos, head, member) \
0710 for (pos = list_next_entry(pos, member); \
0711 !list_entry_is_head(pos, head, member); \
0712 pos = list_next_entry(pos, member))
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723 #define list_for_each_entry_continue_reverse(pos, head, member) \
0724 for (pos = list_prev_entry(pos, member); \
0725 !list_entry_is_head(pos, head, member); \
0726 pos = list_prev_entry(pos, member))
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736 #define list_for_each_entry_from(pos, head, member) \
0737 for (; !list_entry_is_head(pos, head, member); \
0738 pos = list_next_entry(pos, member))
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749 #define list_for_each_entry_from_reverse(pos, head, member) \
0750 for (; !list_entry_is_head(pos, head, member); \
0751 pos = list_prev_entry(pos, member))
0752
0753
0754
0755
0756
0757
0758
0759
0760 #define list_for_each_entry_safe(pos, n, head, member) \
0761 for (pos = list_first_entry(head, typeof(*pos), member), \
0762 n = list_next_entry(pos, member); \
0763 !list_entry_is_head(pos, head, member); \
0764 pos = n, n = list_next_entry(n, member))
0765
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776 #define list_for_each_entry_safe_continue(pos, n, head, member) \
0777 for (pos = list_next_entry(pos, member), \
0778 n = list_next_entry(pos, member); \
0779 !list_entry_is_head(pos, head, member); \
0780 pos = n, n = list_next_entry(n, member))
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792 #define list_for_each_entry_safe_from(pos, n, head, member) \
0793 for (n = list_next_entry(pos, member); \
0794 !list_entry_is_head(pos, head, member); \
0795 pos = n, n = list_next_entry(n, member))
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
0808 for (pos = list_last_entry(head, typeof(*pos), member), \
0809 n = list_prev_entry(pos, member); \
0810 !list_entry_is_head(pos, head, member); \
0811 pos = n, n = list_prev_entry(n, member))
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825 #define list_safe_reset_next(pos, n, member) \
0826 n = list_next_entry(pos, member)
0827
0828
0829
0830
0831
0832
0833
0834
0835 #define HLIST_HEAD_INIT { .first = NULL }
0836 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
0837 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
0838 static inline void INIT_HLIST_NODE(struct hlist_node *h)
0839 {
0840 h->next = NULL;
0841 h->pprev = NULL;
0842 }
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852 static inline int hlist_unhashed(const struct hlist_node *h)
0853 {
0854 return !h->pprev;
0855 }
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
0866 {
0867 return !READ_ONCE(h->pprev);
0868 }
0869
0870
0871
0872
0873
0874 static inline int hlist_empty(const struct hlist_head *h)
0875 {
0876 return !READ_ONCE(h->first);
0877 }
0878
0879 static inline void __hlist_del(struct hlist_node *n)
0880 {
0881 struct hlist_node *next = n->next;
0882 struct hlist_node **pprev = n->pprev;
0883
0884 WRITE_ONCE(*pprev, next);
0885 if (next)
0886 WRITE_ONCE(next->pprev, pprev);
0887 }
0888
0889
0890
0891
0892
0893
0894
0895
0896 static inline void hlist_del(struct hlist_node *n)
0897 {
0898 __hlist_del(n);
0899 n->next = LIST_POISON1;
0900 n->pprev = LIST_POISON2;
0901 }
0902
0903
0904
0905
0906
0907
0908
0909 static inline void hlist_del_init(struct hlist_node *n)
0910 {
0911 if (!hlist_unhashed(n)) {
0912 __hlist_del(n);
0913 INIT_HLIST_NODE(n);
0914 }
0915 }
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
0926 {
0927 struct hlist_node *first = h->first;
0928 WRITE_ONCE(n->next, first);
0929 if (first)
0930 WRITE_ONCE(first->pprev, &n->next);
0931 WRITE_ONCE(h->first, n);
0932 WRITE_ONCE(n->pprev, &h->first);
0933 }
0934
0935
0936
0937
0938
0939
0940 static inline void hlist_add_before(struct hlist_node *n,
0941 struct hlist_node *next)
0942 {
0943 WRITE_ONCE(n->pprev, next->pprev);
0944 WRITE_ONCE(n->next, next);
0945 WRITE_ONCE(next->pprev, &n->next);
0946 WRITE_ONCE(*(n->pprev), n);
0947 }
0948
0949
0950
0951
0952
0953
0954 static inline void hlist_add_behind(struct hlist_node *n,
0955 struct hlist_node *prev)
0956 {
0957 WRITE_ONCE(n->next, prev->next);
0958 WRITE_ONCE(prev->next, n);
0959 WRITE_ONCE(n->pprev, &prev->next);
0960
0961 if (n->next)
0962 WRITE_ONCE(n->next->pprev, &n->next);
0963 }
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973 static inline void hlist_add_fake(struct hlist_node *n)
0974 {
0975 n->pprev = &n->next;
0976 }
0977
0978
0979
0980
0981
0982 static inline bool hlist_fake(struct hlist_node *h)
0983 {
0984 return h->pprev == &h->next;
0985 }
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995 static inline bool
0996 hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
0997 {
0998 return !n->next && n->pprev == &h->first;
0999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 static inline void hlist_move_list(struct hlist_head *old,
1010 struct hlist_head *new)
1011 {
1012 new->first = old->first;
1013 if (new->first)
1014 new->first->pprev = &new->first;
1015 old->first = NULL;
1016 }
1017
1018 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
1019
1020 #define hlist_for_each(pos, head) \
1021 for (pos = (head)->first; pos ; pos = pos->next)
1022
1023 #define hlist_for_each_safe(pos, n, head) \
1024 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
1025 pos = n)
1026
1027 #define hlist_entry_safe(ptr, type, member) \
1028 ({ typeof(ptr) ____ptr = (ptr); \
1029 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
1030 })
1031
1032
1033
1034
1035
1036
1037
1038 #define hlist_for_each_entry(pos, head, member) \
1039 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
1040 pos; \
1041 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1042
1043
1044
1045
1046
1047
1048 #define hlist_for_each_entry_continue(pos, member) \
1049 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
1050 pos; \
1051 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1052
1053
1054
1055
1056
1057
1058 #define hlist_for_each_entry_from(pos, member) \
1059 for (; pos; \
1060 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
1061
1062
1063
1064
1065
1066
1067
1068
1069 #define hlist_for_each_entry_safe(pos, n, head, member) \
1070 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
1071 pos && ({ n = pos->member.next; 1; }); \
1072 pos = hlist_entry_safe(n, typeof(*pos), member))
1073
1074 #endif