From nobody Mon Feb 9 23:44:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5A1C233701 for ; Thu, 8 May 2025 23:33:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; cv=none; b=KL8elFHo/GuP8Ni4zynRjC98gvjGHOlmwpIFMKtAm91/C4Mf/QA5kRvygQCsaVMcI4EjHd5u3svL1KcEg2Y96AqOGg7yvXgGtkWCyTvxV90gGzApzVfK7pgCWMOZdVTs54vEaDZLLVYud//8kbsqZscToyIweSp4qWir47qSVVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746747217; c=relaxed/simple; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=loahuvOk+7E0O6tHKdUzNju9K7vHt2/fi95vcDDRpWU2wF8pjAr8RoXQv7EFQP8tACDciTBiB5vEhXzBvMRzOzWsg7z1K809BpO5RkRNepgTgUOIpX6XnTOkhwjuwhTfKCLIUP3a7xV5MPTjqTpLTYj2FJkpeG4q62dKXAtTh5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MGSIrDM8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MGSIrDM8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86F1BC4CEFB; Thu, 8 May 2025 23:33:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746747217; bh=U7zgE6IzCLrPpMWVpq2+GUdcrSUMCh/2FsvZ9huMwiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MGSIrDM8OOsNfk8o7cQbfOhtCHTXe3lVBuo2ciQbEBz0UAol+TDpwUY1OgVvza6T/ 1xRRWr5r1P4zieSS47IzoNQa79a4c9o1TCCe380WYa6YK/427ib/EVju+PavWlGILE rpTdTLKRyKwx7QYfO/c+SjBMziIDQukHp3YVjNbJT4JFC+SZaRw13O8JdX/WAwYoY3 lVBA0AVySc59GwMLyHytX3qo8H76b5P0gyoChsP7TdEaQrhV+KrN+PTVooe/3xhiwN bgQsHFEjYW1etL9nM1w9X+gJAJ/FVqq/rQyjjkipa67Bg5IAohjpqG+NE78vtzLaTX 3HCURgF425Reg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 96C17CE1432; Thu, 8 May 2025 16:33:36 -0700 (PDT) From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Andrew Morton , Kuniyuki Iwashima , Mateusz Guzik , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jon Pan-Doh , Bjorn Helgaas , Karolina Stolarek , "Paul E. McKenney" Subject: [PATCH v5 20/21] lib: Add trivial kunit test for ratelimit Date: Thu, 8 May 2025 16:33:34 -0700 Message-Id: <20250508233335.1996059-20-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> References: <1bcf7d5e-b89c-4118-b872-c8896bdbdc19@paulmck-laptop> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a simple single-threaded smoke test for lib/ratelimit.c To run on x86: make ARCH=3Dx86_64 mrproper ./tools/testing/kunit/kunit.py run --arch x86_64 --kconfig_add CONFIG_RATE= LIMIT_KUNIT_TEST=3Dy --kconfig_add CONFIG_SMP=3Dy 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@paul= mck-laptop/ Link: https://lore.kernel.org/all/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney Reviewed-by: Petr Mladek Cc: Andrew Morton Cc: Kuniyuki Iwashima Cc: Mateusz Guzik Cc: Steven Rostedt Cc: John Ogness Cc: Sergey Senozhatsky Cc: Jon Pan-Doh Cc: Bjorn Helgaas Cc: Karolina Stolarek --- lib/Kconfig.debug | 11 ++++++ lib/tests/Makefile | 1 + lib/tests/test_ratelimit.c | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lib/tests/test_ratelimit.c diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 9fe4d8dfe5782..c239099218c2b 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -3232,6 +3232,17 @@ config TEST_OBJPOOL =20 If unsure, say N. =20 +config RATELIMIT_KUNIT_TEST + tristate "KUnit Test for correctness and stress of ratelimit" if !KUNIT_A= LL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the "test_ratelimit" module that should be used + for correctness verification and concurrent testings of rate + limiting. + + If unsure, say N. + config INT_POW_KUNIT_TEST tristate "Integer exponentiation (int_pow) test" if !KUNIT_ALL_TESTS depends on KUNIT diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 5a4794c1826e7..1c7c2d20fe501 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -45,5 +45,6 @@ obj-$(CONFIG_STRING_KUNIT_TEST) +=3D string_kunit.o obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) +=3D string_helpers_kunit.o obj-$(CONFIG_USERCOPY_KUNIT_TEST) +=3D usercopy_kunit.o obj-$(CONFIG_UTIL_MACROS_KUNIT) +=3D util_macros_kunit.o +obj-$(CONFIG_RATELIMIT_KUNIT_TEST) +=3D test_ratelimit.o =20 obj-$(CONFIG_TEST_RUNTIME_MODULE) +=3D module/ diff --git a/lib/tests/test_ratelimit.c b/lib/tests/test_ratelimit.c new file mode 100644 index 0000000000000..0374107f5ea89 --- /dev/null +++ b/lib/tests/test_ratelimit.c @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include + +#include +#include + +/* a simple boot-time regression test */ + +#define TESTRL_INTERVAL (5 * HZ) +static DEFINE_RATELIMIT_STATE(testrl, TESTRL_INTERVAL, 3); + +#define test_ratelimited(test, expected) \ + KUNIT_ASSERT_EQ(test, ___ratelimit(&testrl, "test_ratelimit_smoke"), (exp= ected)) + +static void test_ratelimit_smoke(struct kunit *test) +{ + // Check settings. + KUNIT_ASSERT_GE(test, TESTRL_INTERVAL, 100); + + // Test normal operation. + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + + schedule_timeout_idle(TESTRL_INTERVAL - 40); + test_ratelimited(test, false); + + schedule_timeout_idle(50); + test_ratelimited(test, true); + + schedule_timeout_idle(2 * TESTRL_INTERVAL); + test_ratelimited(test, true); + test_ratelimited(test, true); + + schedule_timeout_idle(TESTRL_INTERVAL - 40); + test_ratelimited(test, true); + schedule_timeout_idle(50); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + + // Test disabling. + testrl.burst =3D 0; + test_ratelimited(test, false); + testrl.burst =3D 2; + testrl.interval =3D 0; + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, true); + + // Testing re-enabling. + testrl.interval =3D TESTRL_INTERVAL; + test_ratelimited(test, true); + test_ratelimited(test, true); + test_ratelimited(test, false); + test_ratelimited(test, false); +} + +static struct kunit_case sort_test_cases[] =3D { + KUNIT_CASE_SLOW(test_ratelimit_smoke), + {} +}; + +static struct kunit_suite ratelimit_test_suite =3D { + .name =3D "lib_ratelimit", + .test_cases =3D sort_test_cases, +}; + +kunit_test_suites(&ratelimit_test_suite); + +MODULE_DESCRIPTION("___ratelimit() KUnit test suite"); +MODULE_LICENSE("GPL"); --=20 2.40.1