Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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/20250423115409.3425-1-spasswolf@web.de/
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>
Cc: Mateusz Guzik <mjguzik@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Jon Pan-Doh <pandoh@google.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Karolina Stolarek <karolina.stolarek@oracle.com>

+91
+11
lib/Kconfig.debug
··· 3225 3225 3226 3226 If unsure, say N. 3227 3227 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 + 3228 3239 config INT_POW_KUNIT_TEST 3229 3240 tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS 3230 3241 depends on KUNIT
+1
lib/tests/Makefile
··· 46 46 obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o 47 47 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o 48 48 obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o 49 + obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o 49 50 50 51 obj-$(CONFIG_TEST_RUNTIME_MODULE) += module/
+79
lib/tests/test_ratelimit.c
··· 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");