1 // SPDX-License-Identifier: GPL-2.0-only
3 * Reference tracker self test.
5 * Copyright (c) 2021 Eric Dumazet <edumazet@google.com>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/delay.h>
10 #include <linux/ref_tracker.h>
11 #include <linux/slab.h>
12 #include <linux/timer.h>
14 static struct ref_tracker_dir ref_dir
;
15 static struct ref_tracker
*tracker
[20];
17 #define TRT_ALLOC(X) static noinline void \
18 alloctest_ref_tracker_alloc##X(struct ref_tracker_dir *dir, \
19 struct ref_tracker **trackerp) \
21 ref_tracker_alloc(dir, trackerp, GFP_KERNEL); \
47 alloctest_ref_tracker_free(struct ref_tracker_dir
*dir
,
48 struct ref_tracker
**trackerp
)
50 ref_tracker_free(dir
, trackerp
);
54 static struct timer_list test_ref_tracker_timer
;
55 static atomic_t test_ref_timer_done
= ATOMIC_INIT(0);
57 static void test_ref_tracker_timer_func(struct timer_list
*t
)
59 ref_tracker_alloc(&ref_dir
, &tracker
[0], GFP_ATOMIC
);
60 atomic_set(&test_ref_timer_done
, 1);
63 static int __init
test_ref_tracker_init(void)
67 ref_tracker_dir_init(&ref_dir
, 100, "selftest");
69 timer_setup(&test_ref_tracker_timer
, test_ref_tracker_timer_func
, 0);
70 mod_timer(&test_ref_tracker_timer
, jiffies
+ 1);
72 alloctest_ref_tracker_alloc1(&ref_dir
, &tracker
[1]);
73 alloctest_ref_tracker_alloc2(&ref_dir
, &tracker
[2]);
74 alloctest_ref_tracker_alloc3(&ref_dir
, &tracker
[3]);
75 alloctest_ref_tracker_alloc4(&ref_dir
, &tracker
[4]);
76 alloctest_ref_tracker_alloc5(&ref_dir
, &tracker
[5]);
77 alloctest_ref_tracker_alloc6(&ref_dir
, &tracker
[6]);
78 alloctest_ref_tracker_alloc7(&ref_dir
, &tracker
[7]);
79 alloctest_ref_tracker_alloc8(&ref_dir
, &tracker
[8]);
80 alloctest_ref_tracker_alloc9(&ref_dir
, &tracker
[9]);
81 alloctest_ref_tracker_alloc10(&ref_dir
, &tracker
[10]);
82 alloctest_ref_tracker_alloc11(&ref_dir
, &tracker
[11]);
83 alloctest_ref_tracker_alloc12(&ref_dir
, &tracker
[12]);
84 alloctest_ref_tracker_alloc13(&ref_dir
, &tracker
[13]);
85 alloctest_ref_tracker_alloc14(&ref_dir
, &tracker
[14]);
86 alloctest_ref_tracker_alloc15(&ref_dir
, &tracker
[15]);
87 alloctest_ref_tracker_alloc16(&ref_dir
, &tracker
[16]);
88 alloctest_ref_tracker_alloc17(&ref_dir
, &tracker
[17]);
89 alloctest_ref_tracker_alloc18(&ref_dir
, &tracker
[18]);
90 alloctest_ref_tracker_alloc19(&ref_dir
, &tracker
[19]);
92 /* free all trackers but first 0 and 1. */
93 for (i
= 2; i
< ARRAY_SIZE(tracker
); i
++)
94 alloctest_ref_tracker_free(&ref_dir
, &tracker
[i
]);
96 /* Attempt to free an already freed tracker. */
97 alloctest_ref_tracker_free(&ref_dir
, &tracker
[2]);
99 while (!atomic_read(&test_ref_timer_done
))
102 /* This should warn about tracker[0] & tracker[1] being not freed. */
103 ref_tracker_dir_exit(&ref_dir
);
108 static void __exit
test_ref_tracker_exit(void)
112 module_init(test_ref_tracker_init
);
113 module_exit(test_ref_tracker_exit
);
115 MODULE_DESCRIPTION("Reference tracker self test");
116 MODULE_LICENSE("GPL v2");