|
4 | 4 |
|
5 | 5 | #include <linux/ratelimit.h>
|
6 | 6 | #include <linux/module.h>
|
| 7 | +#include <linux/kthread.h> |
| 8 | +#include <linux/cpumask.h> |
7 | 9 |
|
8 | 10 | /* a simple boot-time regression test */
|
9 | 11 |
|
@@ -63,14 +65,77 @@ static void test_ratelimit_smoke(struct kunit *test)
|
63 | 65 | test_ratelimited(test, false);
|
64 | 66 | }
|
65 | 67 |
|
66 |
| -static struct kunit_case sort_test_cases[] = { |
| 68 | +static struct ratelimit_state stressrl = RATELIMIT_STATE_INIT_FLAGS("stressrl", HZ / 10, 3, |
| 69 | + RATELIMIT_MSG_ON_RELEASE); |
| 70 | + |
| 71 | +static int doneflag; |
| 72 | +static const int stress_duration = 2 * HZ; |
| 73 | + |
| 74 | +struct stress_kthread { |
| 75 | + unsigned long nattempts; |
| 76 | + unsigned long nunlimited; |
| 77 | + unsigned long nlimited; |
| 78 | + unsigned long nmissed; |
| 79 | + struct task_struct *tp; |
| 80 | +}; |
| 81 | + |
| 82 | +static int test_ratelimit_stress_child(void *arg) |
| 83 | +{ |
| 84 | + struct stress_kthread *sktp = arg; |
| 85 | + |
| 86 | + set_user_nice(current, MAX_NICE); |
| 87 | + WARN_ON_ONCE(!sktp->tp); |
| 88 | + |
| 89 | + while (!READ_ONCE(doneflag)) { |
| 90 | + sktp->nattempts++; |
| 91 | + if (___ratelimit(&stressrl, __func__)) |
| 92 | + sktp->nunlimited++; |
| 93 | + else |
| 94 | + sktp->nlimited++; |
| 95 | + cond_resched(); |
| 96 | + } |
| 97 | + |
| 98 | + sktp->nmissed = ratelimit_state_reset_miss(&stressrl); |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +static void test_ratelimit_stress(struct kunit *test) |
| 103 | +{ |
| 104 | + int i; |
| 105 | + const int n_stress_kthread = cpumask_weight(cpu_online_mask); |
| 106 | + struct stress_kthread skt = { 0 }; |
| 107 | + struct stress_kthread *sktp = kcalloc(n_stress_kthread, sizeof(*sktp), GFP_KERNEL); |
| 108 | + |
| 109 | + KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "Memory allocation failure"); |
| 110 | + for (i = 0; i < n_stress_kthread; i++) { |
| 111 | + sktp[i].tp = kthread_run(test_ratelimit_stress_child, &sktp[i], "%s/%i", |
| 112 | + "test_ratelimit_stress_child", i); |
| 113 | + KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "kthread creation failure"); |
| 114 | + pr_alert("Spawned test_ratelimit_stress_child %d\n", i); |
| 115 | + } |
| 116 | + schedule_timeout_idle(stress_duration); |
| 117 | + WRITE_ONCE(doneflag, 1); |
| 118 | + for (i = 0; i < n_stress_kthread; i++) { |
| 119 | + kthread_stop(sktp[i].tp); |
| 120 | + skt.nattempts += sktp[i].nattempts; |
| 121 | + skt.nunlimited += sktp[i].nunlimited; |
| 122 | + skt.nlimited += sktp[i].nlimited; |
| 123 | + skt.nmissed += sktp[i].nmissed; |
| 124 | + } |
| 125 | + KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts, |
| 126 | + "Outcomes not equal to attempts"); |
| 127 | + KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, "Misses not equal to limits"); |
| 128 | +} |
| 129 | + |
| 130 | +static struct kunit_case ratelimit_test_cases[] = { |
67 | 131 | KUNIT_CASE_SLOW(test_ratelimit_smoke),
|
| 132 | + KUNIT_CASE_SLOW(test_ratelimit_stress), |
68 | 133 | {}
|
69 | 134 | };
|
70 | 135 |
|
71 | 136 | static struct kunit_suite ratelimit_test_suite = {
|
72 | 137 | .name = "lib_ratelimit",
|
73 |
| - .test_cases = sort_test_cases, |
| 138 | + .test_cases = ratelimit_test_cases, |
74 | 139 | };
|
75 | 140 |
|
76 | 141 | kunit_test_suites(&ratelimit_test_suite);
|
|
0 commit comments