]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - lib/test_list_sort.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "list_sort_test: " fmt
4 #include <linux/kernel.h>
5 #include <linux/list_sort.h>
6 #include <linux/list.h>
7 #include <linux/module.h>
8 #include <linux/printk.h>
9 #include <linux/slab.h>
10 #include <linux/random.h>
13 * The pattern of set bits in the list length determines which cases
14 * are hit in list_sort().
16 #define TEST_LIST_LEN (512+128+2) /* not including head */
18 #define TEST_POISON1 0xDEADBEEF
19 #define TEST_POISON2 0xA324354C
23 struct list_head list
;
29 /* Array, containing pointers to all elements in the test list */
30 static struct debug_el
**elts __initdata
;
32 static int __init
check(struct debug_el
*ela
, struct debug_el
*elb
)
34 if (ela
->serial
>= TEST_LIST_LEN
) {
35 pr_err("error: incorrect serial %d\n", ela
->serial
);
38 if (elb
->serial
>= TEST_LIST_LEN
) {
39 pr_err("error: incorrect serial %d\n", elb
->serial
);
42 if (elts
[ela
->serial
] != ela
|| elts
[elb
->serial
] != elb
) {
43 pr_err("error: phantom element\n");
46 if (ela
->poison1
!= TEST_POISON1
|| ela
->poison2
!= TEST_POISON2
) {
47 pr_err("error: bad poison: %#x/%#x\n",
48 ela
->poison1
, ela
->poison2
);
51 if (elb
->poison1
!= TEST_POISON1
|| elb
->poison2
!= TEST_POISON2
) {
52 pr_err("error: bad poison: %#x/%#x\n",
53 elb
->poison1
, elb
->poison2
);
59 static int __init
cmp(void *priv
, const struct list_head
*a
,
60 const struct list_head
*b
)
62 struct debug_el
*ela
, *elb
;
64 ela
= container_of(a
, struct debug_el
, list
);
65 elb
= container_of(b
, struct debug_el
, list
);
68 return ela
->value
- elb
->value
;
71 static int __init
list_sort_test(void)
73 int i
, count
= 1, err
= -ENOMEM
;
75 struct list_head
*cur
;
78 pr_debug("start testing list_sort()\n");
80 elts
= kcalloc(TEST_LIST_LEN
, sizeof(*elts
), GFP_KERNEL
);
84 for (i
= 0; i
< TEST_LIST_LEN
; i
++) {
85 el
= kmalloc(sizeof(*el
), GFP_KERNEL
);
89 /* force some equivalencies */
90 el
->value
= prandom_u32() % (TEST_LIST_LEN
/ 3);
92 el
->poison1
= TEST_POISON1
;
93 el
->poison2
= TEST_POISON2
;
95 list_add_tail(&el
->list
, &head
);
98 list_sort(NULL
, &head
, cmp
);
101 for (cur
= head
.next
; cur
->next
!= &head
; cur
= cur
->next
) {
102 struct debug_el
*el1
;
105 if (cur
->next
->prev
!= cur
) {
106 pr_err("error: list is corrupted\n");
110 cmp_result
= cmp(NULL
, cur
, cur
->next
);
111 if (cmp_result
> 0) {
112 pr_err("error: list is not sorted\n");
116 el
= container_of(cur
, struct debug_el
, list
);
117 el1
= container_of(cur
->next
, struct debug_el
, list
);
118 if (cmp_result
== 0 && el
->serial
>= el1
->serial
) {
119 pr_err("error: order of equivalent elements not "
124 if (check(el
, el1
)) {
125 pr_err("error: element check failed\n");
130 if (head
.prev
!= cur
) {
131 pr_err("error: list is corrupted\n");
136 if (count
!= TEST_LIST_LEN
) {
137 pr_err("error: bad list length %d", count
);
143 for (i
= 0; i
< TEST_LIST_LEN
; i
++)
148 module_init(list_sort_test
);
149 MODULE_LICENSE("GPL");