From nobody Wed Feb 11 06:30:50 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 02FA52E5B09 for ; Sat, 7 Feb 2026 20:35:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770496537; cv=none; b=N6lgqCwCb9F3+0cJm/WIpPVsY9ip7Y3RzPd4hPWYdxBLqIdUVb+z2TigQqUsWoQcFO/81plpShNuStiXz72SnyAonSRISuo8C3TTYnXd4Di35ddtntqq9w30lcv4hcXSPizA4PtwTYBPAVoHEWkDIVQQGEwE9eLvsQW5U49MAC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770496537; c=relaxed/simple; bh=fD05X49EXwfqvhTysZERSCDlRRwzwdEFryaQIKxVghg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z3s/YLaEAFYH6p2wbFZZp5E1/+c+RSzSFTEemofmwavLNDyKLdS4SS0OPVuasyZfnDwU1UzRz7BdmroCSih91nvzq4FbXob3PwYlIlS4cIdNX4nhppUk1uGwjxc2oDR90vo1p23bOOHEIv/YpVhqaFYuPxoVfn5ZG02yI4ZIAks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=u+M//bQ5; arc=none smtp.client-ip=91.218.175.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="u+M//bQ5" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1770496535; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=oyAt5kypXwICaUE7SjWvJMusm3wunnSu/xrn7MPeD2k=; b=u+M//bQ55RwmVCpYCfragInf3Adn1Xnuba5iQVZ/E7+9+5z9NUccf/aC+sAZlS8azhz7+h G35q0Z3Xte+TSUn7v2kEva2okC3OsvyZ9b+QprCVweRC7NSqXmsPyQHc3UDF5DwUMkXqew zdSJEjxxR/C6bBDcVXfWBX899/tKSag= From: wen.yang@linux.dev To: Joel Granados Cc: linux-kernel@vger.kernel.org, Wen Yang Subject: [RFC PATCH v2 2/2] sysctl: convert kernel/sysctl-test.c to use SYSCTL_TBL_ENTRY() Date: Sun, 8 Feb 2026 04:35:17 +0800 Message-Id: <8996bbfd86079cff39e1bbf7f645e4c099c5081e.1770496163.git.wen.yang@linux.dev> In-Reply-To: References: 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Wen Yang Convert sysctl test cases to use the new SYSCTL_TBL_ENTRY() macro, demonstrating the macro's benefits in reducing boilerplate and improving code readability. This conversion shows typical usage patterns: - Standard entries with range checking: Before (11 lines): struct ctl_table table =3D { .procname =3D "foo", .data =3D &data, .maxlen =3D sizeof(int), .mode =3D 0644, .proc_handler =3D proc_dointvec, .extra1 =3D SYSCTL_ZERO, .extra2 =3D SYSCTL_ONE_HUNDRED, }; After (2 lines): struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, SYSCTL_ZERO, SYSCTL_ONE_HUNDRE= D); - NULL data entries: Before (11 lines with explicit .data =3D NULL): struct ctl_table null_data_table =3D { .procname =3D "foo", .data =3D NULL, .maxlen =3D sizeof(int), ... }; After (3 lines using SYSCTL_NULL marker): struct ctl_table null_data_table =3D SYSCTL_TBL_ENTRY("foo", SYSCTL_NULL= , 0644, proc_dointvec, SYSCTL_ZERO, SYSCTL= _ONE_HUNDRED); - Explicit maxlen override: SYSCTL_TBL_ENTRY("foo", SYSCTL_NULL, 0644, proc_dointvec, SYSCTL_ZERO, SYSCTL_ONE_HUNDRED, 0); Benefits demonstrated: - 60% reduction in lines of code (from ~110 to ~44 lines for table definiti= ons) - Automatic address-of operator (&data) - macro handles it - Automatic maxlen calculation via sizeof() when appropriate - Improved readability - focus on important parameters - Consistent formatting across all test cases The macro automatically: - Takes the address of 'data' variable - Computes maxlen as sizeof(int) - Selects proc_dointvec based on type (though explicitly provided here) - Validates all parameters at compile time No functional change - all tests continue to pass. [17:55:08] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D sysctl_test (10= subtests) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [17:55:08] [PASSED] sysctl_test_api_dointvec_null_tbl_data [17:55:08] [PASSED] sysctl_test_api_dointvec_table_maxlen_unset [17:55:08] [PASSED] sysctl_test_api_dointvec_table_len_is_zero [17:55:08] [PASSED] sysctl_test_api_dointvec_table_read_but_position_set [17:55:08] [PASSED] sysctl_test_dointvec_read_happy_single_positive [17:55:08] [PASSED] sysctl_test_dointvec_read_happy_single_negative [17:55:08] [PASSED] sysctl_test_dointvec_write_happy_single_positive [17:55:08] [PASSED] sysctl_test_dointvec_write_happy_single_negative [17:55:08] [PASSED] sysctl_test_api_dointvec_write_single_less_int_min [17:55:08] [PASSED] sysctl_test_api_dointvec_write_single_greater_int_max [17:55:08] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [PASSE= D] sysctl_test =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [17:55:08] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [17:55:08] Testing complete. Ran 10 tests: passed: 10 Suggested-by: Joel Granados Signed-off-by: Wen Yang --- kernel/sysctl-test.c | 139 +++++++++++++------------------------------ 1 file changed, 40 insertions(+), 99 deletions(-) diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c index 92f94ea28957..bacd782d8cee 100644 --- a/kernel/sysctl-test.c +++ b/kernel/sysctl-test.c @@ -15,20 +15,15 @@ */ static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test) { - struct ctl_table null_data_table =3D { - .procname =3D "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 =3D NULL, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D 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 =3D SYSCTL_TBL_ENTRY("foo", SYSCTL_NULL,= 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 +61,14 @@ static void sysctl_test_api_dointvec_null_tbl_data(stru= ct kunit *test) static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit *test) { int data =3D 0; - struct ctl_table data_maxlen_unset_table =3D { - .procname =3D "foo", - .data =3D &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 =3D 0, - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D 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 =3D SYSCTL_TBL_ENTRY("foo", \ + data, 0644, proc_dointvec, SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED, 0); + void __user *buffer =3D (void __user *)kunit_kzalloc(test, sizeof(int), GFP_USER); size_t len; @@ -113,15 +103,8 @@ static void sysctl_test_api_dointvec_table_len_is_zero= (struct kunit *test) { int data =3D 0; /* Good table. */ - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", \ + data, 0644, SYSCTL_ZERO, SYSCTL_ONE_HUNDRED); void __user *buffer =3D (void __user *)kunit_kzalloc(test, sizeof(int), GFP_USER); /* @@ -147,15 +130,9 @@ static void sysctl_test_api_dointvec_table_read_but_po= sition_set( { int data =3D 0; /* Good table. */ - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); void __user *buffer =3D (void __user *)kunit_kzalloc(test, sizeof(int), GFP_USER); /* @@ -182,15 +159,9 @@ static void sysctl_test_dointvec_read_happy_single_pos= itive(struct kunit *test) { int data =3D 0; /* Good table. */ - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); size_t len =3D 4; loff_t pos =3D 0; char *buffer =3D kunit_kzalloc(test, len, GFP_USER); @@ -213,15 +184,9 @@ static void sysctl_test_dointvec_read_happy_single_neg= ative(struct kunit *test) { int data =3D 0; /* Good table. */ - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); size_t len =3D 5; loff_t pos =3D 0; char *buffer =3D kunit_kzalloc(test, len, GFP_USER); @@ -242,15 +207,9 @@ static void sysctl_test_dointvec_write_happy_single_po= sitive(struct kunit *test) { int data =3D 0; /* Good table. */ - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); char input[] =3D "9"; size_t len =3D sizeof(input) - 1; loff_t pos =3D 0; @@ -272,15 +231,9 @@ static void sysctl_test_dointvec_write_happy_single_po= sitive(struct kunit *test) static void sysctl_test_dointvec_write_happy_single_negative(struct kunit = *test) { int data =3D 0; - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); char input[] =3D "-9"; size_t len =3D sizeof(input) - 1; loff_t pos =3D 0; @@ -304,15 +257,9 @@ static void sysctl_test_api_dointvec_write_single_less= _int_min( struct kunit *test) { int data =3D 0; - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); size_t max_len =3D 32, len =3D max_len; loff_t pos =3D 0; char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); @@ -342,15 +289,9 @@ static void sysctl_test_api_dointvec_write_single_grea= ter_int_max( struct kunit *test) { int data =3D 0; - struct ctl_table table =3D { - .procname =3D "foo", - .data =3D &data, - .maxlen =3D sizeof(int), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - .extra1 =3D SYSCTL_ZERO, - .extra2 =3D SYSCTL_ONE_HUNDRED, - }; + struct ctl_table table =3D SYSCTL_TBL_ENTRY("foo", data, 0644, \ + SYSCTL_ZERO, \ + SYSCTL_ONE_HUNDRED); size_t max_len =3D 32, len =3D max_len; loff_t pos =3D 0; char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); --=20 2.25.1