117 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Reference tracker self test.4 *5 * Copyright (c) 2021 Eric Dumazet <edumazet@google.com>6 */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>13 14static struct ref_tracker_dir ref_dir;15static struct ref_tracker *tracker[20];16 17#define TRT_ALLOC(X) static noinline void \18 alloctest_ref_tracker_alloc##X(struct ref_tracker_dir *dir, \19 struct ref_tracker **trackerp) \20 { \21 ref_tracker_alloc(dir, trackerp, GFP_KERNEL); \22 }23 24TRT_ALLOC(1)25TRT_ALLOC(2)26TRT_ALLOC(3)27TRT_ALLOC(4)28TRT_ALLOC(5)29TRT_ALLOC(6)30TRT_ALLOC(7)31TRT_ALLOC(8)32TRT_ALLOC(9)33TRT_ALLOC(10)34TRT_ALLOC(11)35TRT_ALLOC(12)36TRT_ALLOC(13)37TRT_ALLOC(14)38TRT_ALLOC(15)39TRT_ALLOC(16)40TRT_ALLOC(17)41TRT_ALLOC(18)42TRT_ALLOC(19)43 44#undef TRT_ALLOC45 46static noinline void47alloctest_ref_tracker_free(struct ref_tracker_dir *dir,48 struct ref_tracker **trackerp)49{50 ref_tracker_free(dir, trackerp);51}52 53 54static struct timer_list test_ref_tracker_timer;55static atomic_t test_ref_timer_done = ATOMIC_INIT(0);56 57static void test_ref_tracker_timer_func(struct timer_list *t)58{59 ref_tracker_alloc(&ref_dir, &tracker[0], GFP_ATOMIC);60 atomic_set(&test_ref_timer_done, 1);61}62 63static int __init test_ref_tracker_init(void)64{65 int i;66 67 ref_tracker_dir_init(&ref_dir, 100, "selftest");68 69 timer_setup(&test_ref_tracker_timer, test_ref_tracker_timer_func, 0);70 mod_timer(&test_ref_tracker_timer, jiffies + 1);71 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]);91 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]);95 96 /* Attempt to free an already freed tracker. */97 alloctest_ref_tracker_free(&ref_dir, &tracker[2]);98 99 while (!atomic_read(&test_ref_timer_done))100 msleep(1);101 102 /* This should warn about tracker[0] & tracker[1] being not freed. */103 ref_tracker_dir_exit(&ref_dir);104 105 return 0;106}107 108static void __exit test_ref_tracker_exit(void)109{110}111 112module_init(test_ref_tracker_init);113module_exit(test_ref_tracker_exit);114 115MODULE_DESCRIPTION("Reference tracker self test");116MODULE_LICENSE("GPL v2");117