xref: /openbmc/linux/lib/test_ref_tracker.c (revision b6d7c0eb)
1914a7b50SEric Dumazet // SPDX-License-Identifier: GPL-2.0-only
2914a7b50SEric Dumazet /*
3914a7b50SEric Dumazet  * Referrence tracker self test.
4914a7b50SEric Dumazet  *
5914a7b50SEric Dumazet  * Copyright (c) 2021 Eric Dumazet <edumazet@google.com>
6914a7b50SEric Dumazet  */
7914a7b50SEric Dumazet #include <linux/init.h>
8914a7b50SEric Dumazet #include <linux/module.h>
9914a7b50SEric Dumazet #include <linux/delay.h>
10914a7b50SEric Dumazet #include <linux/ref_tracker.h>
11914a7b50SEric Dumazet #include <linux/slab.h>
12914a7b50SEric Dumazet #include <linux/timer.h>
13914a7b50SEric Dumazet 
14914a7b50SEric Dumazet static struct ref_tracker_dir ref_dir;
15914a7b50SEric Dumazet static struct ref_tracker *tracker[20];
16914a7b50SEric Dumazet 
17914a7b50SEric Dumazet #define TRT_ALLOC(X) static noinline void 				\
18914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc##X(struct ref_tracker_dir *dir, 	\
19914a7b50SEric Dumazet 				    struct ref_tracker **trackerp)	\
20914a7b50SEric Dumazet 	{								\
21914a7b50SEric Dumazet 		ref_tracker_alloc(dir, trackerp, GFP_KERNEL);		\
22914a7b50SEric Dumazet 	}
23914a7b50SEric Dumazet 
24914a7b50SEric Dumazet TRT_ALLOC(1)
25914a7b50SEric Dumazet TRT_ALLOC(2)
26914a7b50SEric Dumazet TRT_ALLOC(3)
27914a7b50SEric Dumazet TRT_ALLOC(4)
28914a7b50SEric Dumazet TRT_ALLOC(5)
29914a7b50SEric Dumazet TRT_ALLOC(6)
30914a7b50SEric Dumazet TRT_ALLOC(7)
31914a7b50SEric Dumazet TRT_ALLOC(8)
32914a7b50SEric Dumazet TRT_ALLOC(9)
33914a7b50SEric Dumazet TRT_ALLOC(10)
34914a7b50SEric Dumazet TRT_ALLOC(11)
35914a7b50SEric Dumazet TRT_ALLOC(12)
36914a7b50SEric Dumazet TRT_ALLOC(13)
37914a7b50SEric Dumazet TRT_ALLOC(14)
38914a7b50SEric Dumazet TRT_ALLOC(15)
39914a7b50SEric Dumazet TRT_ALLOC(16)
40914a7b50SEric Dumazet TRT_ALLOC(17)
41914a7b50SEric Dumazet TRT_ALLOC(18)
42914a7b50SEric Dumazet TRT_ALLOC(19)
43914a7b50SEric Dumazet 
44914a7b50SEric Dumazet #undef TRT_ALLOC
45914a7b50SEric Dumazet 
46914a7b50SEric Dumazet static noinline void
alloctest_ref_tracker_free(struct ref_tracker_dir * dir,struct ref_tracker ** trackerp)47914a7b50SEric Dumazet alloctest_ref_tracker_free(struct ref_tracker_dir *dir,
48914a7b50SEric Dumazet 			   struct ref_tracker **trackerp)
49914a7b50SEric Dumazet {
50914a7b50SEric Dumazet 	ref_tracker_free(dir, trackerp);
51914a7b50SEric Dumazet }
52914a7b50SEric Dumazet 
53914a7b50SEric Dumazet 
54914a7b50SEric Dumazet static struct timer_list test_ref_tracker_timer;
55914a7b50SEric Dumazet static atomic_t test_ref_timer_done = ATOMIC_INIT(0);
56914a7b50SEric Dumazet 
test_ref_tracker_timer_func(struct timer_list * t)57914a7b50SEric Dumazet static void test_ref_tracker_timer_func(struct timer_list *t)
58914a7b50SEric Dumazet {
59914a7b50SEric Dumazet 	ref_tracker_alloc(&ref_dir, &tracker[0], GFP_ATOMIC);
60914a7b50SEric Dumazet 	atomic_set(&test_ref_timer_done, 1);
61914a7b50SEric Dumazet }
62914a7b50SEric Dumazet 
test_ref_tracker_init(void)63914a7b50SEric Dumazet static int __init test_ref_tracker_init(void)
64914a7b50SEric Dumazet {
65914a7b50SEric Dumazet 	int i;
66914a7b50SEric Dumazet 
67*b6d7c0ebSAndrzej Hajda 	ref_tracker_dir_init(&ref_dir, 100, "selftest");
68914a7b50SEric Dumazet 
69914a7b50SEric Dumazet 	timer_setup(&test_ref_tracker_timer, test_ref_tracker_timer_func, 0);
70914a7b50SEric Dumazet 	mod_timer(&test_ref_tracker_timer, jiffies + 1);
71914a7b50SEric Dumazet 
72914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc1(&ref_dir, &tracker[1]);
73914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc2(&ref_dir, &tracker[2]);
74914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc3(&ref_dir, &tracker[3]);
75914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc4(&ref_dir, &tracker[4]);
76914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc5(&ref_dir, &tracker[5]);
77914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc6(&ref_dir, &tracker[6]);
78914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc7(&ref_dir, &tracker[7]);
79914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc8(&ref_dir, &tracker[8]);
80914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc9(&ref_dir, &tracker[9]);
81914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc10(&ref_dir, &tracker[10]);
82914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc11(&ref_dir, &tracker[11]);
83914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc12(&ref_dir, &tracker[12]);
84914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc13(&ref_dir, &tracker[13]);
85914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc14(&ref_dir, &tracker[14]);
86914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc15(&ref_dir, &tracker[15]);
87914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc16(&ref_dir, &tracker[16]);
88914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc17(&ref_dir, &tracker[17]);
89914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc18(&ref_dir, &tracker[18]);
90914a7b50SEric Dumazet 	alloctest_ref_tracker_alloc19(&ref_dir, &tracker[19]);
91914a7b50SEric Dumazet 
92914a7b50SEric Dumazet 	/* free all trackers but first 0 and 1. */
93914a7b50SEric Dumazet 	for (i = 2; i < ARRAY_SIZE(tracker); i++)
94914a7b50SEric Dumazet 		alloctest_ref_tracker_free(&ref_dir, &tracker[i]);
95914a7b50SEric Dumazet 
96914a7b50SEric Dumazet 	/* Attempt to free an already freed tracker. */
97914a7b50SEric Dumazet 	alloctest_ref_tracker_free(&ref_dir, &tracker[2]);
98914a7b50SEric Dumazet 
99914a7b50SEric Dumazet 	while (!atomic_read(&test_ref_timer_done))
100914a7b50SEric Dumazet 		msleep(1);
101914a7b50SEric Dumazet 
102914a7b50SEric Dumazet 	/* This should warn about tracker[0] & tracker[1] being not freed. */
103914a7b50SEric Dumazet 	ref_tracker_dir_exit(&ref_dir);
104914a7b50SEric Dumazet 
105914a7b50SEric Dumazet 	return 0;
106914a7b50SEric Dumazet }
107914a7b50SEric Dumazet 
test_ref_tracker_exit(void)108914a7b50SEric Dumazet static void __exit test_ref_tracker_exit(void)
109914a7b50SEric Dumazet {
110914a7b50SEric Dumazet }
111914a7b50SEric Dumazet 
112914a7b50SEric Dumazet module_init(test_ref_tracker_init);
113914a7b50SEric Dumazet module_exit(test_ref_tracker_exit);
114914a7b50SEric Dumazet 
115914a7b50SEric Dumazet MODULE_LICENSE("GPL v2");
116