mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: wen.yang@linux.dev
To: Joel Granados <joel.granados@kernel.org>
Cc: Wen Yang <wen.yang@linux.dev>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH 05/39] sysctl: cover priv in KUnit and the debug test module
Date: Thu, 29 Oct 2026 00:34:44 +0800	[thread overview]
Message-ID: <0384433c6fd37d307100b5c8eaf33c6ec4b442ba.1793204511.git.wen.yang@linux.dev> (raw)
In-Reply-To: <cover.1793204511.git.wen.yang@linux.dev>

From: Wen Yang <wen.yang@linux.dev>

Add KUnit cases for priv: a standard handler must ignore it, registration
must reject priv with a standard handler, and a re-registered table must
keep both the value pointer and the context.

Move the debug module's dynamic-value entry to the same shape so it
exercises data-as-value and priv-as-context.

Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
 kernel/sysctl-test.c | 72 ++++++++++++++++++++++++++++++++++++++++++++
 lib/test_sysctl.c    |  9 +++---
 2 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c
index dd658ca18793..f9efc146f219 100644
--- a/kernel/sysctl-test.c
+++ b/kernel/sysctl-test.c
@@ -699,10 +699,56 @@ static int sysctl_test_entry_write(struct kunit *test, const struct ctl_table *t
  * entry must not change what is stored, so a stray context pointer cannot
  * redirect a write to the wrong address.
  */
+static void sysctl_test_priv_standard_handler(struct kunit *test)
+{
+	int value = 0;
+	struct sysctl_test_demo_ctx {
+		int marker;
+	} ctx = { .marker = -1 };
+	const struct ctl_table table = {
+		.procname = "standard",
+		.data = &value,
+		.maxlen = sizeof(value),
+		.mode = 0644,
+		.proc_handler = proc_dointvec_minmax,
+		.min = 0,
+		.max = 100,
+		.priv = &ctx,
+	};
+
+	sysctl_test_entry_roundtrip(test, &table, "42");
+	KUNIT_EXPECT_EQ(test, value, 42);
+	KUNIT_EXPECT_EQ(test, ctx.marker, -1);
+	KUNIT_EXPECT_PTR_EQ(test, table.data, &value);
+	KUNIT_EXPECT_PTR_EQ(test, table.priv, &ctx);
+}
+
 /*
  * Registration refuses priv on a standard handler: the context would be
  * silently ignored, which is always a configuration mistake.
  */
+static void sysctl_test_priv_standard_handler_reject(struct kunit *test)
+{
+	int value = 0;
+	struct sysctl_test_demo_ctx {
+		int marker;
+	} ctx = { .marker = -1 };
+	const struct ctl_table table = {
+		.procname = "invalid_priv",
+		.data = &value,
+		.maxlen = sizeof(value),
+		.mode = 0644,
+		.proc_handler = proc_dointvec_minmax,
+		.min = 0,
+		.max = 100,
+		.priv = &ctx,
+	};
+
+	KUNIT_EXPECT_PTR_EQ(test, NULL,
+			    register_sysctl_sz("sysctl_kunit_priv_invalid",
+					       &table, 1));
+}
+
 
 /*
  * A dynamic range wrapper reads its related limit through priv on each call
@@ -1159,6 +1205,29 @@ static void sysctl_test_context_registration_lifecycle(struct kunit *test)
  * at the caller-owned objects; the handler resolves storage through data and
  * context through priv on every call.
  */
+static void sysctl_test_priv_registration_lifecycle(struct kunit *test)
+{
+	struct sysctl_test_lifecycle ctx = {};
+	struct ctl_table table = SYSCTL_TEST_CTX(ctx.value, "priv_lifecycle",
+						 0644,
+						 sysctl_test_lifecycle_handler, ctx);
+	struct ctl_table_header *header;
+
+	header = register_sysctl_sz("sysctl_kunit_priv", &table, 1);
+	KUNIT_ASSERT_NOT_NULL(test, header);
+	kunit_add_action(test, sysctl_test_unregister_table, header);
+
+	sysctl_test_entry_roundtrip(test, &table, "23");
+	KUNIT_EXPECT_EQ(test, ctx.value, 23);
+	KUNIT_EXPECT_PTR_EQ(test, table.priv, &ctx);
+	KUNIT_EXPECT_PTR_EQ(test, table.data, &ctx.value);
+
+	kunit_release_action(test, sysctl_test_unregister_table, header);
+	sysctl_test_entry_roundtrip(test, &table, "17");
+	KUNIT_EXPECT_EQ(test, ctx.value, 17);
+	KUNIT_EXPECT_PTR_EQ(test, table.priv, &ctx);
+}
+
 /* Registration rejects numeric bounds that cannot represent the element type. */
 static void sysctl_test_context_registration_reject(struct kunit *test)
 {
@@ -1381,6 +1450,9 @@ static struct kunit_case sysctl_test_cases[] = {
 	KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative),
 	KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
 	KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
+	KUNIT_CASE(sysctl_test_priv_standard_handler),
+	KUNIT_CASE(sysctl_test_priv_standard_handler_reject),
+	KUNIT_CASE(sysctl_test_priv_registration_lifecycle),
 	KUNIT_CASE(sysctl_test_entry_null_handler),
 	KUNIT_CASE(sysctl_test_entry_explicit_handler),
 	KUNIT_CASE(sysctl_test_entry_int_limits),
diff --git a/lib/test_sysctl.c b/lib/test_sysctl.c
index 1411896fdbd0..64c82fd53975 100644
--- a/lib/test_sysctl.c
+++ b/lib/test_sysctl.c
@@ -97,7 +97,7 @@ static struct test_sysctl_data test_data = {
 static int test_sysctl_dynamic_value(const struct ctl_table *table, int dir,
 				     void *buffer, size_t *lenp, loff_t *ppos)
 {
-	struct test_sysctl_range_context *context = table->data;
+	struct test_sysctl_range_context *context = table->priv;
 	struct ctl_table resolved = *table;
 
 	resolved.data = &context->value;
@@ -215,12 +215,11 @@ static const struct ctl_table test_table[] = {
 	},
 	{
 		.procname	= "dynamic_value",
-		.data		= &test_data.dynamic,
-		.maxlen		= sizeof(test_data.dynamic),
+		.data		= &test_data.dynamic.value,
+		.maxlen		= sizeof(test_data.dynamic.value),
 		.mode		= 0644,
 		.proc_handler	= test_sysctl_dynamic_value,
-		.min		= 0,
-		.max		= ULONG_MAX,
+		.priv		= &test_data.dynamic,
 	},
 	{
 		.procname	= "int_zero",
-- 
2.25.1


  parent reply	other threads:[~2026-10-28 16:36 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 16:34 [RFC PATCH 00/39] sysctl: replace overloaded extra pointers with typed bounds and context wen.yang
2026-10-28 16:34 ` [RFC PATCH 01/39] " wen.yang
2026-10-28 21:09   ` David Laight
2026-10-28 16:34 ` [RFC PATCH 02/39] test_sysctl: convert bounds to numeric values wen.yang
2026-10-28 16:34 ` [RFC PATCH 03/39] sysctl: add KUnit coverage for numeric bounds and handlers wen.yang
2026-10-28 16:34 ` [RFC PATCH 04/39] sysctl: cover runtime ranges and context lifecycles in KUnit wen.yang
2026-10-28 16:34 ` wen.yang [this message]
2026-10-28 16:34 ` [RFC PATCH 06/39] sysctl: convert core table bounds to numeric values wen.yang
2026-10-28 16:34 ` [RFC PATCH 07/39] sysctl: store architecture sysctl bounds by value wen.yang
2026-10-28 16:34 ` [RFC PATCH 08/39] sysctl: store crypto " wen.yang
2026-10-28 16:34 ` [RFC PATCH 09/39] sysctl: store device " wen.yang
2026-10-28 16:34 ` [RFC PATCH 10/39] sysctl: store filesystem " wen.yang
2026-10-28 16:34 ` [RFC PATCH 11/39] sysctl: store message-queue " wen.yang
2026-10-28 16:34 ` [RFC PATCH 12/39] sysctl: store kernel " wen.yang
2026-10-28 16:34 ` [RFC PATCH 13/39] sysctl: store memory-management " wen.yang
2026-10-28 16:34 ` [RFC PATCH 14/39] sysctl: store IPv6 scalar " wen.yang
2026-10-28 16:34 ` [RFC PATCH 15/39] sysctl: store netfilter " wen.yang
2026-10-28 16:34 ` [RFC PATCH 16/39] sysctl: store RPC " wen.yang
2026-10-28 16:34 ` [RFC PATCH 17/39] sysctl: store remaining network " wen.yang
2026-10-28 16:34 ` [RFC PATCH 18/39] sysctl: store security " wen.yang
2026-10-28 16:34 ` [RFC PATCH 19/39] io_uring: store its " wen.yang
2026-10-28 16:34 ` [RFC PATCH 20/39] sysctl: refresh dynamic scheduler and PID bounds per call wen.yang
2026-10-28 16:35 ` [RFC PATCH 21/39] sysctl: refresh device-dependent " wen.yang
2026-10-28 16:35 ` [RFC PATCH 22/39] sysctl: refresh namespace IPC " wen.yang
2026-10-28 16:35 ` [RFC PATCH 23/39] yama: preserve scope locking with numeric sysctl bounds wen.yang
2026-10-28 16:35 ` [RFC PATCH 24/39] net: refresh core sysctl limits without bound pointers wen.yang
2026-10-28 16:35 ` [RFC PATCH 25/39] sctp: resolve numeric ranges from the current namespace wen.yang
2026-10-28 16:35 ` [RFC PATCH 26/39] rds: preserve live reconnect limits with numeric bounds wen.yang
2026-10-28 16:35 ` [RFC PATCH 27/39] ipv4: preserve live and vector bounds with numeric sysctls wen.yang
2026-10-28 16:35 ` [RFC PATCH 28/39] net: refresh fragment and remaining dynamic sysctl bounds wen.yang
2026-10-28 16:35 ` [RFC PATCH 29/39] neighbour: derive sysctl ownership independently of bounds wen.yang
2026-10-28 16:35 ` [RFC PATCH 30/39] net: resolve sysctl owners from their backing data wen.yang
2026-10-28 16:35 ` [RFC PATCH 31/39] ipvs: derive sysctl ownership from numeric storage wen.yang
2026-10-28 16:35 ` [RFC PATCH 32/39] sysctl: remove obsolete nr_open bound variables wen.yang
2026-10-28 16:35 ` [RFC PATCH 33/39] ipv6: move address sysctl context to priv wen.yang
2026-10-28 16:35 ` [RFC PATCH 34/39] ipv4: move device " wen.yang
2026-10-28 16:35 ` [RFC PATCH 35/39] netfilter: move logger " wen.yang
2026-10-28 16:35 ` [RFC PATCH 36/39] parport: move device " wen.yang
2026-10-28 16:35 ` [RFC PATCH 37/39] net: move namespace " wen.yang
2026-10-28 16:35 ` [RFC PATCH 38/39] arm64: move vector-length " wen.yang
2026-10-28 16:35 ` [RFC PATCH 39/39] sysctl: remove the extra pointers and the compatibility bounds read wen.yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0384433c6fd37d307100b5c8eaf33c6ec4b442ba.1793204511.git.wen.yang@linux.dev \
    --to=wen.yang@linux.dev \
    --cc=joel.granados@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®