1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for the Kernel Linked-list structures.
5 * Copyright (C) 2019, Google LLC.
6 * Author: David Gow <davidgow@google.com>
8 #include <kunit/test.h>
10 #include <linux/list.h>
11 #include <linux/klist.h>
13 struct list_test_struct {
15 struct list_head list;
18 static void list_test_list_init(struct kunit *test)
20 /* Test the different ways of initialising a list. */
21 struct list_head list1 = LIST_HEAD_INIT(list1);
22 struct list_head list2;
24 struct list_head *list4;
25 struct list_head *list5;
27 INIT_LIST_HEAD(&list2);
29 list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
30 INIT_LIST_HEAD(list4);
32 list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
33 memset(list5, 0xFF, sizeof(*list5));
34 INIT_LIST_HEAD(list5);
36 /* list_empty_careful() checks both next and prev. */
37 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list1));
38 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
39 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list3));
40 KUNIT_EXPECT_TRUE(test, list_empty_careful(list4));
41 KUNIT_EXPECT_TRUE(test, list_empty_careful(list5));
47 static void list_test_list_add(struct kunit *test)
49 struct list_head a, b;
55 /* should be [list] -> b -> a */
56 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
57 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
58 KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
61 static void list_test_list_add_tail(struct kunit *test)
63 struct list_head a, b;
66 list_add_tail(&a, &list);
67 list_add_tail(&b, &list);
69 /* should be [list] -> a -> b */
70 KUNIT_EXPECT_PTR_EQ(test, list.next, &a);
71 KUNIT_EXPECT_PTR_EQ(test, a.prev, &list);
72 KUNIT_EXPECT_PTR_EQ(test, a.next, &b);
75 static void list_test_list_del(struct kunit *test)
77 struct list_head a, b;
80 list_add_tail(&a, &list);
81 list_add_tail(&b, &list);
83 /* before: [list] -> a -> b */
86 /* now: [list] -> b */
87 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
88 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
91 static void list_test_list_replace(struct kunit *test)
93 struct list_head a_old, a_new, b;
96 list_add_tail(&a_old, &list);
97 list_add_tail(&b, &list);
99 /* before: [list] -> a_old -> b */
100 list_replace(&a_old, &a_new);
102 /* now: [list] -> a_new -> b */
103 KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
104 KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
107 static void list_test_list_replace_init(struct kunit *test)
109 struct list_head a_old, a_new, b;
112 list_add_tail(&a_old, &list);
113 list_add_tail(&b, &list);
115 /* before: [list] -> a_old -> b */
116 list_replace_init(&a_old, &a_new);
118 /* now: [list] -> a_new -> b */
119 KUNIT_EXPECT_PTR_EQ(test, list.next, &a_new);
120 KUNIT_EXPECT_PTR_EQ(test, b.prev, &a_new);
122 /* check a_old is empty (initialized) */
123 KUNIT_EXPECT_TRUE(test, list_empty_careful(&a_old));
126 static void list_test_list_swap(struct kunit *test)
128 struct list_head a, b;
131 list_add_tail(&a, &list);
132 list_add_tail(&b, &list);
134 /* before: [list] -> a -> b */
137 /* after: [list] -> b -> a */
138 KUNIT_EXPECT_PTR_EQ(test, &b, list.next);
139 KUNIT_EXPECT_PTR_EQ(test, &a, list.prev);
141 KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
142 KUNIT_EXPECT_PTR_EQ(test, &list, b.prev);
144 KUNIT_EXPECT_PTR_EQ(test, &list, a.next);
145 KUNIT_EXPECT_PTR_EQ(test, &b, a.prev);
148 static void list_test_list_del_init(struct kunit *test)
150 struct list_head a, b;
153 list_add_tail(&a, &list);
154 list_add_tail(&b, &list);
156 /* before: [list] -> a -> b */
158 /* after: [list] -> b, a initialised */
160 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
161 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
162 KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
165 static void list_test_list_del_init_careful(struct kunit *test)
167 /* NOTE: This test only checks the behaviour of this function in
168 * isolation. It does not verify memory model guarantees.
170 struct list_head a, b;
173 list_add_tail(&a, &list);
174 list_add_tail(&b, &list);
176 /* before: [list] -> a -> b */
177 list_del_init_careful(&a);
178 /* after: [list] -> b, a initialised */
180 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
181 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
182 KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
185 static void list_test_list_move(struct kunit *test)
187 struct list_head a, b;
191 list_add_tail(&a, &list1);
192 list_add_tail(&b, &list2);
194 /* before: [list1] -> a, [list2] -> b */
195 list_move(&a, &list2);
196 /* after: [list1] empty, [list2] -> a -> b */
198 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
200 KUNIT_EXPECT_PTR_EQ(test, &a, list2.next);
201 KUNIT_EXPECT_PTR_EQ(test, &b, a.next);
204 static void list_test_list_move_tail(struct kunit *test)
206 struct list_head a, b;
210 list_add_tail(&a, &list1);
211 list_add_tail(&b, &list2);
213 /* before: [list1] -> a, [list2] -> b */
214 list_move_tail(&a, &list2);
215 /* after: [list1] empty, [list2] -> b -> a */
217 KUNIT_EXPECT_TRUE(test, list_empty(&list1));
219 KUNIT_EXPECT_PTR_EQ(test, &b, list2.next);
220 KUNIT_EXPECT_PTR_EQ(test, &a, b.next);
223 static void list_test_list_bulk_move_tail(struct kunit *test)
225 struct list_head a, b, c, d, x, y;
226 struct list_head *list1_values[] = { &x, &b, &c, &y };
227 struct list_head *list2_values[] = { &a, &d };
228 struct list_head *ptr;
233 list_add_tail(&x, &list1);
234 list_add_tail(&y, &list1);
236 list_add_tail(&a, &list2);
237 list_add_tail(&b, &list2);
238 list_add_tail(&c, &list2);
239 list_add_tail(&d, &list2);
241 /* before: [list1] -> x -> y, [list2] -> a -> b -> c -> d */
242 list_bulk_move_tail(&y, &b, &c);
243 /* after: [list1] -> x -> b -> c -> y, [list2] -> a -> d */
245 list_for_each(ptr, &list1) {
246 KUNIT_EXPECT_PTR_EQ(test, ptr, list1_values[i]);
249 KUNIT_EXPECT_EQ(test, i, 4);
251 list_for_each(ptr, &list2) {
252 KUNIT_EXPECT_PTR_EQ(test, ptr, list2_values[i]);
255 KUNIT_EXPECT_EQ(test, i, 2);
258 static void list_test_list_is_head(struct kunit *test)
260 struct list_head a, b, c;
262 /* Two lists: [a] -> b, [c] */
265 list_add_tail(&b, &a);
267 KUNIT_EXPECT_TRUE_MSG(test, list_is_head(&a, &a),
268 "Head element of same list");
269 KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &b),
270 "Non-head element of same list");
271 KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c),
272 "Head element of different list");
276 static void list_test_list_is_first(struct kunit *test)
278 struct list_head a, b;
281 list_add_tail(&a, &list);
282 list_add_tail(&b, &list);
284 KUNIT_EXPECT_TRUE(test, list_is_first(&a, &list));
285 KUNIT_EXPECT_FALSE(test, list_is_first(&b, &list));
288 static void list_test_list_is_last(struct kunit *test)
290 struct list_head a, b;
293 list_add_tail(&a, &list);
294 list_add_tail(&b, &list);
296 KUNIT_EXPECT_FALSE(test, list_is_last(&a, &list));
297 KUNIT_EXPECT_TRUE(test, list_is_last(&b, &list));
300 static void list_test_list_empty(struct kunit *test)
306 list_add_tail(&a, &list1);
308 KUNIT_EXPECT_FALSE(test, list_empty(&list1));
309 KUNIT_EXPECT_TRUE(test, list_empty(&list2));
312 static void list_test_list_empty_careful(struct kunit *test)
314 /* This test doesn't check correctness under concurrent access */
319 list_add_tail(&a, &list1);
321 KUNIT_EXPECT_FALSE(test, list_empty_careful(&list1));
322 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
325 static void list_test_list_rotate_left(struct kunit *test)
327 struct list_head a, b;
330 list_add_tail(&a, &list);
331 list_add_tail(&b, &list);
333 /* before: [list] -> a -> b */
334 list_rotate_left(&list);
335 /* after: [list] -> b -> a */
337 KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
338 KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
339 KUNIT_EXPECT_PTR_EQ(test, b.next, &a);
342 static void list_test_list_rotate_to_front(struct kunit *test)
344 struct list_head a, b, c, d;
345 struct list_head *list_values[] = { &c, &d, &a, &b };
346 struct list_head *ptr;
350 list_add_tail(&a, &list);
351 list_add_tail(&b, &list);
352 list_add_tail(&c, &list);
353 list_add_tail(&d, &list);
355 /* before: [list] -> a -> b -> c -> d */
356 list_rotate_to_front(&c, &list);
357 /* after: [list] -> c -> d -> a -> b */
359 list_for_each(ptr, &list) {
360 KUNIT_EXPECT_PTR_EQ(test, ptr, list_values[i]);
363 KUNIT_EXPECT_EQ(test, i, 4);
366 static void list_test_list_is_singular(struct kunit *test)
368 struct list_head a, b;
372 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
374 list_add_tail(&a, &list);
377 KUNIT_EXPECT_TRUE(test, list_is_singular(&list));
379 list_add_tail(&b, &list);
381 /* [list] -> a -> b */
382 KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
385 static void list_test_list_cut_position(struct kunit *test)
387 struct list_head entries[3], *cur;
392 list_add_tail(&entries[0], &list1);
393 list_add_tail(&entries[1], &list1);
394 list_add_tail(&entries[2], &list1);
396 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
397 list_cut_position(&list2, &list1, &entries[1]);
398 /* after: [list2] -> entries[0] -> entries[1], [list1] -> entries[2] */
400 list_for_each(cur, &list2) {
401 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
405 KUNIT_EXPECT_EQ(test, i, 2);
407 list_for_each(cur, &list1) {
408 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
413 static void list_test_list_cut_before(struct kunit *test)
415 struct list_head entries[3], *cur;
420 list_add_tail(&entries[0], &list1);
421 list_add_tail(&entries[1], &list1);
422 list_add_tail(&entries[2], &list1);
424 /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
425 list_cut_before(&list2, &list1, &entries[1]);
426 /* after: [list2] -> entries[0], [list1] -> entries[1] -> entries[2] */
428 list_for_each(cur, &list2) {
429 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
433 KUNIT_EXPECT_EQ(test, i, 1);
435 list_for_each(cur, &list1) {
436 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
441 static void list_test_list_splice(struct kunit *test)
443 struct list_head entries[5], *cur;
448 list_add_tail(&entries[0], &list1);
449 list_add_tail(&entries[1], &list1);
450 list_add_tail(&entries[2], &list2);
451 list_add_tail(&entries[3], &list2);
452 list_add_tail(&entries[4], &list1);
454 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
455 list_splice(&list2, &entries[1]);
456 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
458 list_for_each(cur, &list1) {
459 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
463 KUNIT_EXPECT_EQ(test, i, 5);
466 static void list_test_list_splice_tail(struct kunit *test)
468 struct list_head entries[5], *cur;
473 list_add_tail(&entries[0], &list1);
474 list_add_tail(&entries[1], &list1);
475 list_add_tail(&entries[2], &list2);
476 list_add_tail(&entries[3], &list2);
477 list_add_tail(&entries[4], &list1);
479 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
480 list_splice_tail(&list2, &entries[4]);
481 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] uninit */
483 list_for_each(cur, &list1) {
484 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
488 KUNIT_EXPECT_EQ(test, i, 5);
491 static void list_test_list_splice_init(struct kunit *test)
493 struct list_head entries[5], *cur;
498 list_add_tail(&entries[0], &list1);
499 list_add_tail(&entries[1], &list1);
500 list_add_tail(&entries[2], &list2);
501 list_add_tail(&entries[3], &list2);
502 list_add_tail(&entries[4], &list1);
504 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
505 list_splice_init(&list2, &entries[1]);
506 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
508 list_for_each(cur, &list1) {
509 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
513 KUNIT_EXPECT_EQ(test, i, 5);
515 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
518 static void list_test_list_splice_tail_init(struct kunit *test)
520 struct list_head entries[5], *cur;
525 list_add_tail(&entries[0], &list1);
526 list_add_tail(&entries[1], &list1);
527 list_add_tail(&entries[2], &list2);
528 list_add_tail(&entries[3], &list2);
529 list_add_tail(&entries[4], &list1);
531 /* before: [list1]->e[0]->e[1]->e[4], [list2]->e[2]->e[3] */
532 list_splice_tail_init(&list2, &entries[4]);
533 /* after: [list1]->e[0]->e[1]->e[2]->e[3]->e[4], [list2] empty */
535 list_for_each(cur, &list1) {
536 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
540 KUNIT_EXPECT_EQ(test, i, 5);
542 KUNIT_EXPECT_TRUE(test, list_empty_careful(&list2));
545 static void list_test_list_entry(struct kunit *test)
547 struct list_test_struct test_struct;
549 KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list),
550 struct list_test_struct, list));
553 static void list_test_list_entry_is_head(struct kunit *test)
555 struct list_test_struct test_struct1, test_struct2, test_struct3;
557 INIT_LIST_HEAD(&test_struct1.list);
558 INIT_LIST_HEAD(&test_struct3.list);
560 list_add_tail(&test_struct2.list, &test_struct1.list);
562 KUNIT_EXPECT_TRUE_MSG(test,
563 list_entry_is_head((&test_struct1), &test_struct1.list, list),
564 "Head element of same list");
565 KUNIT_EXPECT_FALSE_MSG(test,
566 list_entry_is_head((&test_struct2), &test_struct1.list, list),
567 "Non-head element of same list");
568 KUNIT_EXPECT_FALSE_MSG(test,
569 list_entry_is_head((&test_struct3), &test_struct1.list, list),
570 "Head element of different list");
573 static void list_test_list_first_entry(struct kunit *test)
575 struct list_test_struct test_struct1, test_struct2;
578 list_add_tail(&test_struct1.list, &list);
579 list_add_tail(&test_struct2.list, &list);
582 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list,
583 struct list_test_struct, list));
586 static void list_test_list_last_entry(struct kunit *test)
588 struct list_test_struct test_struct1, test_struct2;
591 list_add_tail(&test_struct1.list, &list);
592 list_add_tail(&test_struct2.list, &list);
595 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_last_entry(&list,
596 struct list_test_struct, list));
599 static void list_test_list_first_entry_or_null(struct kunit *test)
601 struct list_test_struct test_struct1, test_struct2;
604 KUNIT_EXPECT_FALSE(test, list_first_entry_or_null(&list,
605 struct list_test_struct, list));
607 list_add_tail(&test_struct1.list, &list);
608 list_add_tail(&test_struct2.list, &list);
610 KUNIT_EXPECT_PTR_EQ(test, &test_struct1,
611 list_first_entry_or_null(&list,
612 struct list_test_struct, list));
615 static void list_test_list_next_entry(struct kunit *test)
617 struct list_test_struct test_struct1, test_struct2;
620 list_add_tail(&test_struct1.list, &list);
621 list_add_tail(&test_struct2.list, &list);
624 KUNIT_EXPECT_PTR_EQ(test, &test_struct2, list_next_entry(&test_struct1,
628 static void list_test_list_prev_entry(struct kunit *test)
630 struct list_test_struct test_struct1, test_struct2;
633 list_add_tail(&test_struct1.list, &list);
634 list_add_tail(&test_struct2.list, &list);
637 KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_prev_entry(&test_struct2,
641 static void list_test_list_for_each(struct kunit *test)
643 struct list_head entries[3], *cur;
647 list_add_tail(&entries[0], &list);
648 list_add_tail(&entries[1], &list);
649 list_add_tail(&entries[2], &list);
651 list_for_each(cur, &list) {
652 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
656 KUNIT_EXPECT_EQ(test, i, 3);
659 static void list_test_list_for_each_prev(struct kunit *test)
661 struct list_head entries[3], *cur;
665 list_add_tail(&entries[0], &list);
666 list_add_tail(&entries[1], &list);
667 list_add_tail(&entries[2], &list);
669 list_for_each_prev(cur, &list) {
670 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
674 KUNIT_EXPECT_EQ(test, i, -1);
677 static void list_test_list_for_each_safe(struct kunit *test)
679 struct list_head entries[3], *cur, *n;
684 list_add_tail(&entries[0], &list);
685 list_add_tail(&entries[1], &list);
686 list_add_tail(&entries[2], &list);
688 list_for_each_safe(cur, n, &list) {
689 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
690 list_del(&entries[i]);
694 KUNIT_EXPECT_EQ(test, i, 3);
695 KUNIT_EXPECT_TRUE(test, list_empty(&list));
698 static void list_test_list_for_each_prev_safe(struct kunit *test)
700 struct list_head entries[3], *cur, *n;
704 list_add_tail(&entries[0], &list);
705 list_add_tail(&entries[1], &list);
706 list_add_tail(&entries[2], &list);
708 list_for_each_prev_safe(cur, n, &list) {
709 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
710 list_del(&entries[i]);
714 KUNIT_EXPECT_EQ(test, i, -1);
715 KUNIT_EXPECT_TRUE(test, list_empty(&list));
718 static void list_test_list_for_each_entry(struct kunit *test)
720 struct list_test_struct entries[5], *cur;
724 for (i = 0; i < 5; ++i) {
726 list_add_tail(&entries[i].list, &list);
731 list_for_each_entry(cur, &list, list) {
732 KUNIT_EXPECT_EQ(test, cur->data, i);
736 KUNIT_EXPECT_EQ(test, i, 5);
739 static void list_test_list_for_each_entry_reverse(struct kunit *test)
741 struct list_test_struct entries[5], *cur;
745 for (i = 0; i < 5; ++i) {
747 list_add_tail(&entries[i].list, &list);
752 list_for_each_entry_reverse(cur, &list, list) {
753 KUNIT_EXPECT_EQ(test, cur->data, i);
757 KUNIT_EXPECT_EQ(test, i, -1);
760 static struct kunit_case list_test_cases[] = {
761 KUNIT_CASE(list_test_list_init),
762 KUNIT_CASE(list_test_list_add),
763 KUNIT_CASE(list_test_list_add_tail),
764 KUNIT_CASE(list_test_list_del),
765 KUNIT_CASE(list_test_list_replace),
766 KUNIT_CASE(list_test_list_replace_init),
767 KUNIT_CASE(list_test_list_swap),
768 KUNIT_CASE(list_test_list_del_init),
769 KUNIT_CASE(list_test_list_del_init_careful),
770 KUNIT_CASE(list_test_list_move),
771 KUNIT_CASE(list_test_list_move_tail),
772 KUNIT_CASE(list_test_list_bulk_move_tail),
773 KUNIT_CASE(list_test_list_is_head),
774 KUNIT_CASE(list_test_list_is_first),
775 KUNIT_CASE(list_test_list_is_last),
776 KUNIT_CASE(list_test_list_empty),
777 KUNIT_CASE(list_test_list_empty_careful),
778 KUNIT_CASE(list_test_list_rotate_left),
779 KUNIT_CASE(list_test_list_rotate_to_front),
780 KUNIT_CASE(list_test_list_is_singular),
781 KUNIT_CASE(list_test_list_cut_position),
782 KUNIT_CASE(list_test_list_cut_before),
783 KUNIT_CASE(list_test_list_splice),
784 KUNIT_CASE(list_test_list_splice_tail),
785 KUNIT_CASE(list_test_list_splice_init),
786 KUNIT_CASE(list_test_list_splice_tail_init),
787 KUNIT_CASE(list_test_list_entry),
788 KUNIT_CASE(list_test_list_entry_is_head),
789 KUNIT_CASE(list_test_list_first_entry),
790 KUNIT_CASE(list_test_list_last_entry),
791 KUNIT_CASE(list_test_list_first_entry_or_null),
792 KUNIT_CASE(list_test_list_next_entry),
793 KUNIT_CASE(list_test_list_prev_entry),
794 KUNIT_CASE(list_test_list_for_each),
795 KUNIT_CASE(list_test_list_for_each_prev),
796 KUNIT_CASE(list_test_list_for_each_safe),
797 KUNIT_CASE(list_test_list_for_each_prev_safe),
798 KUNIT_CASE(list_test_list_for_each_entry),
799 KUNIT_CASE(list_test_list_for_each_entry_reverse),
803 static struct kunit_suite list_test_module = {
804 .name = "list-kunit-test",
805 .test_cases = list_test_cases,
808 struct hlist_test_struct {
810 struct hlist_node list;
813 static void hlist_test_init(struct kunit *test)
815 /* Test the different ways of initialising a list. */
816 struct hlist_head list1 = HLIST_HEAD_INIT;
817 struct hlist_head list2;
819 struct hlist_head *list4;
820 struct hlist_head *list5;
822 INIT_HLIST_HEAD(&list2);
824 list4 = kzalloc(sizeof(*list4), GFP_KERNEL | __GFP_NOFAIL);
825 INIT_HLIST_HEAD(list4);
827 list5 = kmalloc(sizeof(*list5), GFP_KERNEL | __GFP_NOFAIL);
828 memset(list5, 0xFF, sizeof(*list5));
829 INIT_HLIST_HEAD(list5);
831 KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
832 KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
833 KUNIT_EXPECT_TRUE(test, hlist_empty(&list3));
834 KUNIT_EXPECT_TRUE(test, hlist_empty(list4));
835 KUNIT_EXPECT_TRUE(test, hlist_empty(list5));
841 static void hlist_test_unhashed(struct kunit *test)
848 /* is unhashed by default */
849 KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));
851 hlist_add_head(&a, &list);
853 /* is hashed once added to list */
854 KUNIT_EXPECT_FALSE(test, hlist_unhashed(&a));
858 /* is again unhashed after del_init */
859 KUNIT_EXPECT_TRUE(test, hlist_unhashed(&a));
862 /* Doesn't test concurrency guarantees */
863 static void hlist_test_unhashed_lockless(struct kunit *test)
870 /* is unhashed by default */
871 KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));
873 hlist_add_head(&a, &list);
875 /* is hashed once added to list */
876 KUNIT_EXPECT_FALSE(test, hlist_unhashed_lockless(&a));
880 /* is again unhashed after del_init */
881 KUNIT_EXPECT_TRUE(test, hlist_unhashed_lockless(&a));
884 static void hlist_test_del(struct kunit *test)
886 struct hlist_node a, b;
889 hlist_add_head(&a, &list);
890 hlist_add_behind(&b, &a);
892 /* before: [list] -> a -> b */
895 /* now: [list] -> b */
896 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
897 KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);
900 static void hlist_test_del_init(struct kunit *test)
902 struct hlist_node a, b;
905 hlist_add_head(&a, &list);
906 hlist_add_behind(&b, &a);
908 /* before: [list] -> a -> b */
911 /* now: [list] -> b */
912 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
913 KUNIT_EXPECT_PTR_EQ(test, b.pprev, &list.first);
915 /* a is now initialised */
916 KUNIT_EXPECT_PTR_EQ(test, a.next, NULL);
917 KUNIT_EXPECT_PTR_EQ(test, a.pprev, NULL);
920 /* Tests all three hlist_add_* functions */
921 static void hlist_test_add(struct kunit *test)
923 struct hlist_node a, b, c, d;
926 hlist_add_head(&a, &list);
927 hlist_add_head(&b, &list);
928 hlist_add_before(&c, &a);
929 hlist_add_behind(&d, &a);
931 /* should be [list] -> b -> c -> a -> d */
932 KUNIT_EXPECT_PTR_EQ(test, list.first, &b);
934 KUNIT_EXPECT_PTR_EQ(test, c.pprev, &(b.next));
935 KUNIT_EXPECT_PTR_EQ(test, b.next, &c);
937 KUNIT_EXPECT_PTR_EQ(test, a.pprev, &(c.next));
938 KUNIT_EXPECT_PTR_EQ(test, c.next, &a);
940 KUNIT_EXPECT_PTR_EQ(test, d.pprev, &(a.next));
941 KUNIT_EXPECT_PTR_EQ(test, a.next, &d);
944 /* Tests both hlist_fake() and hlist_add_fake() */
945 static void hlist_test_fake(struct kunit *test)
951 /* not fake after init */
952 KUNIT_EXPECT_FALSE(test, hlist_fake(&a));
957 KUNIT_EXPECT_TRUE(test, hlist_fake(&a));
960 static void hlist_test_is_singular_node(struct kunit *test)
962 struct hlist_node a, b;
966 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));
968 hlist_add_head(&a, &list);
969 KUNIT_EXPECT_TRUE(test, hlist_is_singular_node(&a, &list));
971 hlist_add_head(&b, &list);
972 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&a, &list));
973 KUNIT_EXPECT_FALSE(test, hlist_is_singular_node(&b, &list));
976 static void hlist_test_empty(struct kunit *test)
981 /* list starts off empty */
982 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
984 hlist_add_head(&a, &list);
986 /* list is no longer empty */
987 KUNIT_EXPECT_FALSE(test, hlist_empty(&list));
990 static void hlist_test_move_list(struct kunit *test)
996 hlist_add_head(&a, &list1);
998 KUNIT_EXPECT_FALSE(test, hlist_empty(&list1));
999 KUNIT_EXPECT_TRUE(test, hlist_empty(&list2));
1000 hlist_move_list(&list1, &list2);
1001 KUNIT_EXPECT_TRUE(test, hlist_empty(&list1));
1002 KUNIT_EXPECT_FALSE(test, hlist_empty(&list2));
1006 static void hlist_test_entry(struct kunit *test)
1008 struct hlist_test_struct test_struct;
1010 KUNIT_EXPECT_PTR_EQ(test, &test_struct,
1011 hlist_entry(&(test_struct.list),
1012 struct hlist_test_struct, list));
1015 static void hlist_test_entry_safe(struct kunit *test)
1017 struct hlist_test_struct test_struct;
1019 KUNIT_EXPECT_PTR_EQ(test, &test_struct,
1020 hlist_entry_safe(&(test_struct.list),
1021 struct hlist_test_struct, list));
1023 KUNIT_EXPECT_PTR_EQ(test, NULL,
1024 hlist_entry_safe((struct hlist_node *)NULL,
1025 struct hlist_test_struct, list));
1028 static void hlist_test_for_each(struct kunit *test)
1030 struct hlist_node entries[3], *cur;
1034 hlist_add_head(&entries[0], &list);
1035 hlist_add_behind(&entries[1], &entries[0]);
1036 hlist_add_behind(&entries[2], &entries[1]);
1038 hlist_for_each(cur, &list) {
1039 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
1043 KUNIT_EXPECT_EQ(test, i, 3);
1047 static void hlist_test_for_each_safe(struct kunit *test)
1049 struct hlist_node entries[3], *cur, *n;
1053 hlist_add_head(&entries[0], &list);
1054 hlist_add_behind(&entries[1], &entries[0]);
1055 hlist_add_behind(&entries[2], &entries[1]);
1057 hlist_for_each_safe(cur, n, &list) {
1058 KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
1059 hlist_del(&entries[i]);
1063 KUNIT_EXPECT_EQ(test, i, 3);
1064 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
1067 static void hlist_test_for_each_entry(struct kunit *test)
1069 struct hlist_test_struct entries[5], *cur;
1073 entries[0].data = 0;
1074 hlist_add_head(&entries[0].list, &list);
1075 for (i = 1; i < 5; ++i) {
1076 entries[i].data = i;
1077 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1082 hlist_for_each_entry(cur, &list, list) {
1083 KUNIT_EXPECT_EQ(test, cur->data, i);
1087 KUNIT_EXPECT_EQ(test, i, 5);
1090 static void hlist_test_for_each_entry_continue(struct kunit *test)
1092 struct hlist_test_struct entries[5], *cur;
1096 entries[0].data = 0;
1097 hlist_add_head(&entries[0].list, &list);
1098 for (i = 1; i < 5; ++i) {
1099 entries[i].data = i;
1100 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1103 /* We skip the first (zero-th) entry. */
1107 hlist_for_each_entry_continue(cur, list) {
1108 KUNIT_EXPECT_EQ(test, cur->data, i);
1109 /* Stamp over the entry. */
1114 KUNIT_EXPECT_EQ(test, i, 5);
1115 /* The first entry was not visited. */
1116 KUNIT_EXPECT_EQ(test, entries[0].data, 0);
1117 /* The second (and presumably others), were. */
1118 KUNIT_EXPECT_EQ(test, entries[1].data, 42);
1121 static void hlist_test_for_each_entry_from(struct kunit *test)
1123 struct hlist_test_struct entries[5], *cur;
1127 entries[0].data = 0;
1128 hlist_add_head(&entries[0].list, &list);
1129 for (i = 1; i < 5; ++i) {
1130 entries[i].data = i;
1131 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1137 hlist_for_each_entry_from(cur, list) {
1138 KUNIT_EXPECT_EQ(test, cur->data, i);
1139 /* Stamp over the entry. */
1144 KUNIT_EXPECT_EQ(test, i, 5);
1145 /* The first entry was visited. */
1146 KUNIT_EXPECT_EQ(test, entries[0].data, 42);
1149 static void hlist_test_for_each_entry_safe(struct kunit *test)
1151 struct hlist_test_struct entries[5], *cur;
1152 struct hlist_node *tmp_node;
1156 entries[0].data = 0;
1157 hlist_add_head(&entries[0].list, &list);
1158 for (i = 1; i < 5; ++i) {
1159 entries[i].data = i;
1160 hlist_add_behind(&entries[i].list, &entries[i-1].list);
1165 hlist_for_each_entry_safe(cur, tmp_node, &list, list) {
1166 KUNIT_EXPECT_EQ(test, cur->data, i);
1167 hlist_del(&cur->list);
1171 KUNIT_EXPECT_EQ(test, i, 5);
1172 KUNIT_EXPECT_TRUE(test, hlist_empty(&list));
1176 static struct kunit_case hlist_test_cases[] = {
1177 KUNIT_CASE(hlist_test_init),
1178 KUNIT_CASE(hlist_test_unhashed),
1179 KUNIT_CASE(hlist_test_unhashed_lockless),
1180 KUNIT_CASE(hlist_test_del),
1181 KUNIT_CASE(hlist_test_del_init),
1182 KUNIT_CASE(hlist_test_add),
1183 KUNIT_CASE(hlist_test_fake),
1184 KUNIT_CASE(hlist_test_is_singular_node),
1185 KUNIT_CASE(hlist_test_empty),
1186 KUNIT_CASE(hlist_test_move_list),
1187 KUNIT_CASE(hlist_test_entry),
1188 KUNIT_CASE(hlist_test_entry_safe),
1189 KUNIT_CASE(hlist_test_for_each),
1190 KUNIT_CASE(hlist_test_for_each_safe),
1191 KUNIT_CASE(hlist_test_for_each_entry),
1192 KUNIT_CASE(hlist_test_for_each_entry_continue),
1193 KUNIT_CASE(hlist_test_for_each_entry_from),
1194 KUNIT_CASE(hlist_test_for_each_entry_safe),
1198 static struct kunit_suite hlist_test_module = {
1200 .test_cases = hlist_test_cases,
1204 struct klist_test_struct {
1207 struct klist_node klist_node;
1210 static int node_count;
1211 static struct klist_node *last_node;
1213 static void check_node(struct klist_node *node_ptr)
1216 last_node = node_ptr;
1219 static void check_delete_node(struct klist_node *node_ptr)
1222 last_node = node_ptr;
1225 static void klist_test_add_tail(struct kunit *test)
1227 struct klist_node a, b;
1228 struct klist mylist;
1229 struct klist_iter i;
1232 klist_init(&mylist, &check_node, NULL);
1234 klist_add_tail(&a, &mylist);
1235 KUNIT_EXPECT_EQ(test, node_count, 1);
1236 KUNIT_EXPECT_PTR_EQ(test, last_node, &a);
1238 klist_add_tail(&b, &mylist);
1239 KUNIT_EXPECT_EQ(test, node_count, 2);
1240 KUNIT_EXPECT_PTR_EQ(test, last_node, &b);
1242 /* should be [list] -> a -> b */
1243 klist_iter_init(&mylist, &i);
1245 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1246 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1247 KUNIT_EXPECT_NULL(test, klist_next(&i));
1249 klist_iter_exit(&i);
1253 static void klist_test_add_head(struct kunit *test)
1255 struct klist_node a, b;
1256 struct klist mylist;
1257 struct klist_iter i;
1260 klist_init(&mylist, &check_node, NULL);
1262 klist_add_head(&a, &mylist);
1263 KUNIT_EXPECT_EQ(test, node_count, 1);
1264 KUNIT_EXPECT_PTR_EQ(test, last_node, &a);
1266 klist_add_head(&b, &mylist);
1267 KUNIT_EXPECT_EQ(test, node_count, 2);
1268 KUNIT_EXPECT_PTR_EQ(test, last_node, &b);
1270 /* should be [list] -> b -> a */
1271 klist_iter_init(&mylist, &i);
1273 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1274 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1275 KUNIT_EXPECT_NULL(test, klist_next(&i));
1277 klist_iter_exit(&i);
1281 static void klist_test_add_behind(struct kunit *test)
1283 struct klist_node a, b, c, d;
1284 struct klist mylist;
1285 struct klist_iter i;
1288 klist_init(&mylist, &check_node, NULL);
1290 klist_add_head(&a, &mylist);
1291 klist_add_head(&b, &mylist);
1293 klist_add_behind(&c, &a);
1294 KUNIT_EXPECT_EQ(test, node_count, 3);
1295 KUNIT_EXPECT_PTR_EQ(test, last_node, &c);
1297 klist_add_behind(&d, &b);
1298 KUNIT_EXPECT_EQ(test, node_count, 4);
1299 KUNIT_EXPECT_PTR_EQ(test, last_node, &d);
1301 klist_iter_init(&mylist, &i);
1303 /* should be [list] -> b -> d -> a -> c*/
1304 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1305 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &d);
1306 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1307 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &c);
1308 KUNIT_EXPECT_NULL(test, klist_next(&i));
1310 klist_iter_exit(&i);
1314 static void klist_test_add_before(struct kunit *test)
1316 struct klist_node a, b, c, d;
1317 struct klist mylist;
1318 struct klist_iter i;
1321 klist_init(&mylist, &check_node, NULL);
1323 klist_add_head(&a, &mylist);
1324 klist_add_head(&b, &mylist);
1325 klist_add_before(&c, &a);
1326 KUNIT_EXPECT_EQ(test, node_count, 3);
1327 KUNIT_EXPECT_PTR_EQ(test, last_node, &c);
1329 klist_add_before(&d, &b);
1330 KUNIT_EXPECT_EQ(test, node_count, 4);
1331 KUNIT_EXPECT_PTR_EQ(test, last_node, &d);
1333 klist_iter_init(&mylist, &i);
1335 /* should be [list] -> b -> d -> a -> c*/
1336 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &d);
1337 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1338 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &c);
1339 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1340 KUNIT_EXPECT_NULL(test, klist_next(&i));
1342 klist_iter_exit(&i);
1347 * Verify that klist_del() delays the deletion of a node until there
1348 * are no other references to it
1350 static void klist_test_del_refcount_greater_than_zero(struct kunit *test)
1352 struct klist_node a, b, c, d;
1353 struct klist mylist;
1354 struct klist_iter i;
1357 klist_init(&mylist, &check_node, &check_delete_node);
1359 /* Add nodes a,b,c,d to the list*/
1360 klist_add_tail(&a, &mylist);
1361 klist_add_tail(&b, &mylist);
1362 klist_add_tail(&c, &mylist);
1363 klist_add_tail(&d, &mylist);
1365 klist_iter_init(&mylist, &i);
1367 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1368 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1369 /* Advance the iterator to point to node c*/
1370 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &c);
1372 /* Try to delete node c while there is a reference to it*/
1376 * Verify that node c is still attached to the list even after being
1377 * deleted. Since the iterator still points to c, the reference count is not
1380 KUNIT_EXPECT_TRUE(test, klist_node_attached(&c));
1382 /* Check that node c has not been removed yet*/
1383 KUNIT_EXPECT_EQ(test, node_count, 4);
1384 KUNIT_EXPECT_PTR_EQ(test, last_node, &d);
1386 klist_iter_exit(&i);
1389 * Since the iterator is no longer pointing to node c, node c is removed
1392 KUNIT_EXPECT_EQ(test, node_count, 3);
1393 KUNIT_EXPECT_PTR_EQ(test, last_node, &c);
1398 * Verify that klist_del() deletes a node immediately when there are no
1399 * other references to it.
1401 static void klist_test_del_refcount_zero(struct kunit *test)
1403 struct klist_node a, b, c, d;
1404 struct klist mylist;
1405 struct klist_iter i;
1408 klist_init(&mylist, &check_node, &check_delete_node);
1410 /* Add nodes a,b,c,d to the list*/
1411 klist_add_tail(&a, &mylist);
1412 klist_add_tail(&b, &mylist);
1413 klist_add_tail(&c, &mylist);
1414 klist_add_tail(&d, &mylist);
1418 /* Check that node c is deleted from the list*/
1419 KUNIT_EXPECT_EQ(test, node_count, 3);
1420 KUNIT_EXPECT_PTR_EQ(test, last_node, &c);
1422 /* Should be [list] -> a -> b -> d*/
1423 klist_iter_init(&mylist, &i);
1425 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1426 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1427 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &d);
1428 KUNIT_EXPECT_NULL(test, klist_next(&i));
1430 klist_iter_exit(&i);
1434 static void klist_test_remove(struct kunit *test)
1436 /* This test doesn't check correctness under concurrent access */
1437 struct klist_node a, b, c, d;
1438 struct klist mylist;
1439 struct klist_iter i;
1442 klist_init(&mylist, &check_node, &check_delete_node);
1444 /* Add nodes a,b,c,d to the list*/
1445 klist_add_tail(&a, &mylist);
1446 klist_add_tail(&b, &mylist);
1447 klist_add_tail(&c, &mylist);
1448 klist_add_tail(&d, &mylist);
1452 /* Check the nodes in the list*/
1453 KUNIT_EXPECT_EQ(test, node_count, 3);
1454 KUNIT_EXPECT_PTR_EQ(test, last_node, &c);
1456 /* should be [list] -> a -> b -> d*/
1457 klist_iter_init(&mylist, &i);
1459 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &a);
1460 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &b);
1461 KUNIT_EXPECT_PTR_EQ(test, klist_next(&i), &d);
1462 KUNIT_EXPECT_NULL(test, klist_next(&i));
1464 klist_iter_exit(&i);
1468 static void klist_test_node_attached(struct kunit *test)
1470 struct klist_node a = {};
1471 struct klist mylist;
1473 klist_init(&mylist, NULL, NULL);
1475 KUNIT_EXPECT_FALSE(test, klist_node_attached(&a));
1476 klist_add_head(&a, &mylist);
1477 KUNIT_EXPECT_TRUE(test, klist_node_attached(&a));
1479 KUNIT_EXPECT_FALSE(test, klist_node_attached(&a));
1483 static struct kunit_case klist_test_cases[] = {
1484 KUNIT_CASE(klist_test_add_tail),
1485 KUNIT_CASE(klist_test_add_head),
1486 KUNIT_CASE(klist_test_add_behind),
1487 KUNIT_CASE(klist_test_add_before),
1488 KUNIT_CASE(klist_test_del_refcount_greater_than_zero),
1489 KUNIT_CASE(klist_test_del_refcount_zero),
1490 KUNIT_CASE(klist_test_remove),
1491 KUNIT_CASE(klist_test_node_attached),
1495 static struct kunit_suite klist_test_module = {
1497 .test_cases = klist_test_cases,
1500 kunit_test_suites(&list_test_module, &hlist_test_module, &klist_test_module);
1502 MODULE_LICENSE("GPL v2");