Skip to content

Commit d19e9fa

Browse files
committed
lib: Add trivial kunit test for ratelimit
Add a simple single-threaded smoke test for lib/ratelimit.c To run on x86: make ARCH=x86_64 mrproper ./tools/testing/kunit/kunit.py run --arch x86_64 --kconfig_add CONFIG_RATELIMIT_KUNIT_TEST=y --kconfig_add CONFIG_SMP=y lib_ratelimit This will fail on old ___ratelimit(), and subsequent patches provide the fixes that are required. [ paulmck: Apply timeout and kunit feedback from Petr Mladek. ] Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paulmck-laptop/ Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Paul E. McKenney <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Kuniyuki Iwashima <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: John Ogness <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Jon Pan-Doh <[email protected]> Cc: Bjorn Helgaas <[email protected]> Cc: Karolina Stolarek <[email protected]>
1 parent 19272b3 commit d19e9fa

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

lib/Kconfig.debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,6 +3225,17 @@ config TEST_OBJPOOL
32253225

32263226
If unsure, say N.
32273227

3228+
config RATELIMIT_KUNIT_TEST
3229+
tristate "KUnit Test for correctness and stress of ratelimit" if !KUNIT_ALL_TESTS
3230+
depends on KUNIT
3231+
default KUNIT_ALL_TESTS
3232+
help
3233+
This builds the "test_ratelimit" module that should be used
3234+
for correctness verification and concurrent testings of rate
3235+
limiting.
3236+
3237+
If unsure, say N.
3238+
32283239
config INT_POW_KUNIT_TEST
32293240
tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS
32303241
depends on KUNIT

lib/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o
4646
obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
4747
obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
4848
obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o
49+
obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o
4950

5051
obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/

lib/tests/test_ratelimit.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include <kunit/test.h>
4+
5+
#include <linux/ratelimit.h>
6+
#include <linux/module.h>
7+
8+
/* a simple boot-time regression test */
9+
10+
#define TESTRL_INTERVAL (5 * HZ)
11+
static DEFINE_RATELIMIT_STATE(testrl, TESTRL_INTERVAL, 3);
12+
13+
#define test_ratelimited(test, expected) \
14+
KUNIT_ASSERT_EQ(test, ___ratelimit(&testrl, "test_ratelimit_smoke"), (expected))
15+
16+
static void test_ratelimit_smoke(struct kunit *test)
17+
{
18+
// Check settings.
19+
KUNIT_ASSERT_GE(test, TESTRL_INTERVAL, 100);
20+
21+
// Test normal operation.
22+
test_ratelimited(test, true);
23+
test_ratelimited(test, true);
24+
test_ratelimited(test, true);
25+
test_ratelimited(test, false);
26+
27+
schedule_timeout_idle(TESTRL_INTERVAL - 40);
28+
test_ratelimited(test, false);
29+
30+
schedule_timeout_idle(50);
31+
test_ratelimited(test, true);
32+
33+
schedule_timeout_idle(2 * TESTRL_INTERVAL);
34+
test_ratelimited(test, true);
35+
test_ratelimited(test, true);
36+
37+
schedule_timeout_idle(TESTRL_INTERVAL - 40);
38+
test_ratelimited(test, true);
39+
schedule_timeout_idle(50);
40+
test_ratelimited(test, true);
41+
test_ratelimited(test, true);
42+
test_ratelimited(test, true);
43+
test_ratelimited(test, false);
44+
45+
// Test disabling.
46+
testrl.burst = 0;
47+
test_ratelimited(test, false);
48+
testrl.burst = 2;
49+
testrl.interval = 0;
50+
test_ratelimited(test, true);
51+
test_ratelimited(test, true);
52+
test_ratelimited(test, true);
53+
test_ratelimited(test, true);
54+
test_ratelimited(test, true);
55+
test_ratelimited(test, true);
56+
test_ratelimited(test, true);
57+
58+
// Testing re-enabling.
59+
testrl.interval = TESTRL_INTERVAL;
60+
test_ratelimited(test, true);
61+
test_ratelimited(test, true);
62+
test_ratelimited(test, false);
63+
test_ratelimited(test, false);
64+
}
65+
66+
static struct kunit_case sort_test_cases[] = {
67+
KUNIT_CASE_SLOW(test_ratelimit_smoke),
68+
{}
69+
};
70+
71+
static struct kunit_suite ratelimit_test_suite = {
72+
.name = "lib_ratelimit",
73+
.test_cases = sort_test_cases,
74+
};
75+
76+
kunit_test_suites(&ratelimit_test_suite);
77+
78+
MODULE_DESCRIPTION("___ratelimit() KUnit test suite");
79+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)