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: linux-kernel@vger.kernel.org, Wen Yang <wen.yang@linux.dev>
Subject: [RFC PATCH v3 2/2] sysctl: convert kernel/sysctl-test.c to use CTLTBL_ENTRY_XXX()
Date: Mon,  2 Mar 2026 14:32:21 +0800	[thread overview]
Message-ID: <ae37f783d33aeab5769551df4d500467eb0676e5.1772432502.git.wen.yang@linux.dev> (raw)
In-Reply-To: <cover.1772432501.git.wen.yang@linux.dev>

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

Convert all struct ctl_table initializations in kernel/sysctl-test.c
from direct field assignment to indirect initialization via the
CTLTBL_ENTRY_XXX() macros introduced in the previous patch. This
demonstrates the macros in real usage and validates their correctness
against the existing test suite.

The following macro variants are exercised:

- CTLTBL_ENTRY_VNMR(var, name, mode, min, max)
  Used for the majority of test cases that initialize a standard int
  entry with range checking. The macro automatically selects the
  range-checking handler (proc_dointvec_minmax for int) based on the
  variable type and computes maxlen via sizeof():

    Before:
      struct ctl_table table = {
          .procname     = "foo",
          .data         = &data,
          .maxlen       = sizeof(int),
          .mode         = 0644,
          .proc_handler = proc_dointvec,
          .extra1       = SYSCTL_ZERO,
          .extra2       = SYSCTL_ONE_HUNDRED,
      };

    After:
      struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
              SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);

- CTLTBL_ENTRY_VNMHR(var, name, mode, handler, min, max)
  Used for the NULL data test case, passing SYSCTL_NULL as the data
  argument and proc_dointvec as the explicit handler:

    Before:
      struct ctl_table null_data_table = {
          .procname     = "foo",
          .data         = NULL,
          .maxlen       = sizeof(int),
          .mode         = 0644,
          .proc_handler = proc_dointvec,
          .extra1       = SYSCTL_ZERO,
          .extra2       = SYSCTL_ONE_HUNDRED,
      };

    After:
      struct ctl_table null_data_table = CTLTBL_ENTRY_VNMHR(
              SYSCTL_NULL, "foo", 0644, proc_dointvec,
              SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);

- CTLTBL_ENTRY_VNMHRL(var, name, mode, handler, min, max, maxlen)
  Used for the maxlen=0 test case, passing an explicit maxlen of 0 to
  override the default sizeof()-based value:

    Before:
      struct ctl_table data_maxlen_unset_table = {
          .procname     = "foo",
          .data         = &data,
          .maxlen       = 0,
          .mode         = 0644,
          .proc_handler = proc_dointvec,
          .extra1       = SYSCTL_ZERO,
          .extra2       = SYSCTL_ONE_HUNDRED,
      };

    After:
      struct ctl_table data_maxlen_unset_table = CTLTBL_ENTRY_VNMHRL(
              data, "foo", 0644, proc_dointvec,
              SYSCTL_ZERO, SYSCTL_ONE_HUNDRED, 0);

New tests are added to cover the remaining macro variants:

- CTLTBL_ENTRY_V(var)
  Verifies that the macro initializes .procname to the variable name,
  .mode to 0444, .data to &var, .maxlen to sizeof(var), and selects
  proc_dointvec as the handler for an int variable.

- CTLTBL_ENTRY_VM(var, mode)
  Verifies that the macro initializes .procname to the variable name,
  .mode to 0644, .data to &var, .maxlen to sizeof(var), and selects
  proc_dointvec as the handler for an int variable.

- CTLTBL_ENTRY_VMR(var, mode, min, max)
  Verifies that the macro initializes .procname to the variable name,
  .mode to 0644, .data to &var, .maxlen to sizeof(var),
  and auto-selecting proc_dointvec_minmax as the handler.

- CTLTBL_ENTRY_VN(var, name)
  Verifies that the macro initializes .procname to the custom name while
  keeping .mode at 0444 and auto-selecting the handler.

- CTLTBL_ENTRY_VNM(name, var, mode)
  Verifies that the macro initializes .procname and .mode as specified
  while auto-selecting the handler.

- CTLTBL_ENTRY_VNMH(var, name, mode, handler)
  Verifies that the macro initializes all fields including an explicitly
  provided proc_handler.

All tests pass:

[13:53:49] ================ sysctl_test (16 subtests) =================
[13:53:49] [PASSED] sysctl_test_api_dointvec_null_tbl_data
[13:53:49] [PASSED] sysctl_test_api_dointvec_table_maxlen_unset
[13:53:49] [PASSED] sysctl_test_api_dointvec_table_len_is_zero
[13:53:49] [PASSED] sysctl_test_api_dointvec_table_read_but_position_set
[13:53:49] [PASSED] sysctl_test_dointvec_read_happy_single_positive
[13:53:49] [PASSED] sysctl_test_dointvec_read_happy_single_negative
[13:53:49] [PASSED] sysctl_test_dointvec_write_happy_single_positive
[13:53:49] [PASSED] sysctl_test_dointvec_write_happy_single_negative
[13:53:49] [PASSED] sysctl_test_api_dointvec_write_single_less_int_min
[13:53:49] [PASSED] sysctl_test_api_dointvec_write_single_greater_int_max
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_v
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_vm
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_vmr
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_vn
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_vnm
[13:53:49] [PASSED] sysctl_test_api_ctltbl_entry_vnmh
[13:53:49] =================== [PASSED] sysctl_test ===================
[13:53:49] ============================================================
[13:53:49] Testing complete. Ran 16 tests: passed: 16

Suggested-by: Joel Granados <joel.granados@kernel.org>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
 kernel/sysctl-test.c | 240 +++++++++++++++++++++++++------------------
 1 file changed, 141 insertions(+), 99 deletions(-)

diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c
index 92f94ea28957..410e6ce32ee1 100644
--- a/kernel/sysctl-test.c
+++ b/kernel/sysctl-test.c
@@ -15,20 +15,14 @@
  */
 static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test)
 {
-	struct ctl_table null_data_table = {
-		.procname = "foo",
-		/*
-		 * Here we are testing that proc_dointvec behaves correctly when
-		 * we give it a NULL .data field. Normally this would point to a
-		 * piece of memory where the value would be stored.
-		 */
-		.data		= NULL,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	/*
+	 * Here we are testing that proc_dointvec behaves correctly when
+	 * we give it a NULL .data field. Normally this would point to a
+	 * piece of memory where the value would be stored.
+	 */
+	struct ctl_table null_data_table = CTLTBL_ENTRY_VNMHR(
+			SYSCTL_NULL, "foo", 0644, proc_dointvec,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	/*
 	 * proc_dointvec expects a buffer in user space, so we allocate one. We
 	 * also need to cast it to __user so sparse doesn't get mad.
@@ -66,19 +60,14 @@ static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test)
 static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit *test)
 {
 	int data = 0;
-	struct ctl_table data_maxlen_unset_table = {
-		.procname = "foo",
-		.data		= &data,
-		/*
-		 * So .data is no longer NULL, but we tell proc_dointvec its
-		 * length is 0, so it still shouldn't try to use it.
-		 */
-		.maxlen		= 0,
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	/*
+	 * So .data is no longer NULL, but we tell proc_dointvec its
+	 * length is 0, so it still shouldn't try to use it.
+	 */
+	struct ctl_table data_maxlen_unset_table = CTLTBL_ENTRY_VNMHRL(
+			data, "foo", 0644, proc_dointvec,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED, 0);
+
 	void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
 							   GFP_USER);
 	size_t len;
@@ -113,15 +102,8 @@ static void sysctl_test_api_dointvec_table_len_is_zero(struct kunit *test)
 {
 	int data = 0;
 	/* Good table. */
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo",
+			0644, SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
 							   GFP_USER);
 	/*
@@ -147,15 +129,8 @@ static void sysctl_test_api_dointvec_table_read_but_position_set(
 {
 	int data = 0;
 	/* Good table. */
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
 							   GFP_USER);
 	/*
@@ -182,15 +157,8 @@ static void sysctl_test_dointvec_read_happy_single_positive(struct kunit *test)
 {
 	int data = 0;
 	/* Good table. */
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	size_t len = 4;
 	loff_t pos = 0;
 	char *buffer = kunit_kzalloc(test, len, GFP_USER);
@@ -213,15 +181,8 @@ static void sysctl_test_dointvec_read_happy_single_negative(struct kunit *test)
 {
 	int data = 0;
 	/* Good table. */
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	size_t len = 5;
 	loff_t pos = 0;
 	char *buffer = kunit_kzalloc(test, len, GFP_USER);
@@ -242,15 +203,8 @@ static void sysctl_test_dointvec_write_happy_single_positive(struct kunit *test)
 {
 	int data = 0;
 	/* Good table. */
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	char input[] = "9";
 	size_t len = sizeof(input) - 1;
 	loff_t pos = 0;
@@ -272,15 +226,8 @@ static void sysctl_test_dointvec_write_happy_single_positive(struct kunit *test)
 static void sysctl_test_dointvec_write_happy_single_negative(struct kunit *test)
 {
 	int data = 0;
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	char input[] = "-9";
 	size_t len = sizeof(input) - 1;
 	loff_t pos = 0;
@@ -304,15 +251,8 @@ static void sysctl_test_api_dointvec_write_single_less_int_min(
 		struct kunit *test)
 {
 	int data = 0;
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	size_t max_len = 32, len = max_len;
 	loff_t pos = 0;
 	char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
@@ -342,15 +282,8 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
 		struct kunit *test)
 {
 	int data = 0;
-	struct ctl_table table = {
-		.procname = "foo",
-		.data		= &data,
-		.maxlen		= sizeof(int),
-		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
-		.extra1		= SYSCTL_ZERO,
-		.extra2         = SYSCTL_ONE_HUNDRED,
-	};
+	struct ctl_table table = CTLTBL_ENTRY_VNMR(data, "foo", 0644,
+			SYSCTL_ZERO, SYSCTL_ONE_HUNDRED);
 	size_t max_len = 32, len = max_len;
 	loff_t pos = 0;
 	char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
@@ -367,6 +300,109 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
 	KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
 }
 
+/*
+ * Test CTLTBL_ENTRY_V: variable name used as procname, mode 0444,
+ * auto-selected handler (proc_dointvec for int).
+ */
+static void sysctl_test_api_ctltbl_entry_v(struct kunit *test)
+{
+	int foo = 123;
+	struct ctl_table table = CTLTBL_ENTRY_V(foo);
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0444, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &foo, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec,
+			table.proc_handler);
+}
+
+/*
+ * Test CTLTBL_ENTRY_VM: variable name used as procname, custom mode,
+ * auto-selected handler (proc_dointvec for int).
+ */
+static void sysctl_test_api_ctltbl_entry_vm(struct kunit *test)
+{
+	int foo = 123;
+	struct ctl_table table = CTLTBL_ENTRY_VM(foo, 0644);
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0644, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &foo, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec,
+			table.proc_handler);
+}
+
+/*
+ * Test CTLTBL_ENTRY_VMR: variable name used as procname, custom mode,
+ * auto-selected range checkint handler (proc_dointvec_minmax for int).
+ */
+static void sysctl_test_api_ctltbl_entry_vmr(struct kunit *test)
+{
+	int foo = 123;
+	struct ctl_table table = CTLTBL_ENTRY_VMR(foo, 0644, SYSCTL_ZERO,
+						  SYSCTL_ONE_HUNDRED);
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0644, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &foo, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec_minmax,
+			table.proc_handler);
+}
+
+/*
+ * Test CTLTBL_ENTRY_VN: custom procname, mode 0444,
+ * auto-selected handler (proc_dointvec for int).
+ */
+static void sysctl_test_api_ctltbl_entry_vn(struct kunit *test)
+{
+	int data = 0;
+	struct ctl_table table = CTLTBL_ENTRY_VN(data, "foo");
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0444, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &data, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec,
+			table.proc_handler);
+}
+
+/*
+ * Test CTLTBL_ENTRY_VNM: custom procname and mode,
+ * auto-selected handler (proc_dointvec for int).
+ */
+static void sysctl_test_api_ctltbl_entry_vnm(struct kunit *test)
+{
+	int data = 0;
+	struct ctl_table table = CTLTBL_ENTRY_VNM(data, "foo", 0644);
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0644, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &data, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec,
+			table.proc_handler);
+}
+
+/*
+ * Test CTLTBL_ENTRY_VNMH: custom procname, mode and explicit handler.
+ */
+static void sysctl_test_api_ctltbl_entry_vnmh(struct kunit *test)
+{
+	int data = 0;
+	struct ctl_table table = CTLTBL_ENTRY_VNMH(data, "foo", 0644,
+			proc_dointvec);
+
+	KUNIT_EXPECT_STREQ(test, "foo", table.procname);
+	KUNIT_EXPECT_EQ(test, (umode_t)0644, table.mode);
+	KUNIT_EXPECT_PTR_EQ(test, &data, table.data);
+	KUNIT_EXPECT_EQ(test, sizeof(int), table.maxlen);
+	KUNIT_EXPECT_PTR_EQ(test, (proc_handler *)proc_dointvec,
+			table.proc_handler);
+}
+
 static struct kunit_case sysctl_test_cases[] = {
 	KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
 	KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
@@ -378,6 +414,12 @@ 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_api_ctltbl_entry_v),
+	KUNIT_CASE(sysctl_test_api_ctltbl_entry_vm),
+	KUNIT_CASE(sysctl_test_api_ctltbl_entry_vmr),
+	KUNIT_CASE(sysctl_test_api_ctltbl_entry_vn),
+	KUNIT_CASE(sysctl_test_api_ctltbl_entry_vnm),
+	KUNIT_CASE(sysctl_test_api_ctltbl_entry_vnmh),
 	{}
 };
 
-- 
2.25.1


      parent reply	other threads:[~2026-03-02  6:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-02  6:32 [RFC PATCH v3 0/2] sysctl: refactor ctl_table initialization wen.yang
2026-03-02  6:32 ` [RFC PATCH v3 1/2] sysctl: introduce CTLTBL_ENTRY_XXX() helper macros wen.yang
2026-03-02  6:32 ` wen.yang [this message]

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=ae37f783d33aeab5769551df4d500467eb0676e5.1772432502.git.wen.yang@linux.dev \
    --to=wen.yang@linux.dev \
    --cc=joel.granados@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®