From nobody Fri Apr 3 01:28:27 2026 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (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 49172334C28 for ; Wed, 25 Mar 2026 18:40:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774464006; cv=none; b=D5yZL/mzS7eqRMQyy1KVSGVqIO2n8wzniVUa76LKse4iaAe7oDfxhFTou9qUizcg1TDt9wqrhAEaACCbutIZvElx74TK6oQsDUDpGbxWubSpdpWW7VU3xid1GYQzWKFx/9q1wNaX0wUGDGwm/gFN1uFC1UiBU+uMh4sc4g58EXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774464006; c=relaxed/simple; bh=fixBGJo6M7fHUae/bxhYOGyJ0GgE18cVwn2C/iuzmIE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=A3IDR6UJgobqgMJeLtXOZw17SAyOIJu40PDw04d17d1zAe9hdT0zr3zM08e+Cr5SD/8O6++B/QbdyCXEK3uY8O2oiI9LsHoWFVvce6zfbPGWm8APTOYFrPDRoKfBmoiMbl3/6JoxB6+52X2hjqmr3feAWs3zUyxnfp4k0qG+dWY= 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=WbsUkHLP; arc=none smtp.client-ip=95.215.58.171 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="WbsUkHLP" 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=1774464000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PaTNz46zEkUTNzfpveGw6ogtHadEbA20yWavHlcngfY=; b=WbsUkHLPqKIq7mNPNUCupDI5R3OERO3dOlxv2dTKYfo1X/r4bffu3YkOKELVqrGiUrWhQw hYAnsfqF+zMBpL4rURC4GysvZ+HoY9wY1T8vX10Lji4yvyZ59E4LrtUUOi6nFSStF4pW7h YehBT/NnXR/5cGEuz8cyJFyG7nYZulg= From: wen.yang@linux.dev To: Joel Granados , Kees Cook Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Wen Yang Subject: [RFC PATCH v5 2/2] sysctl: convert kernel/sysctl-test.c to use CTLTBL_ENTRY_XXX() Date: Thu, 26 Mar 2026 02:39:16 +0800 Message-Id: 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT From: Wen Yang Replace open-coded struct ctl_table initialisers with the CTLTBL_ENTRY_VNMR(), CTLTBL_ENTRY_VNMHR(), and CTLTBL_ENTRY_VNMHRL() macros introduced in the previous patch. Add a parameterized KUnit test case exercising all CTLTBL_ENTRY_XXX() variants (V, VM, VMR, VN, VNM, VNMH) for int, u8, and bool types. char[] (proc_dostring) requires CTLTBL_ENTRY_VNMH() with an explicit handler since _Generic cannot dispatch on array types. Suggested-by: Joel Granados Signed-off-by: Wen Yang --- kernel/sysctl-test.c | 237 +++++++++++++++++++++++++------------------ 1 file changed, 138 insertions(+), 99 deletions(-) diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c index 92f94ea28957..3b37bb0516e3 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 =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 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(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 CTLTBL_ENTRY_VNMHRL( + data, "foo", 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 +102,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 CTLTBL_ENTRY_VNMR(data, "foo", + 0644, SYSCTL_ZERO, SYSCTL_ONE_HUNDRED); void __user *buffer =3D (void __user *)kunit_kzalloc(test, sizeof(int), GFP_USER); /* @@ -147,15 +129,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 0644, + SYSCTL_ZERO, SYSCTL_ONE_HUNDRED); void __user *buffer =3D (void __user *)kunit_kzalloc(test, sizeof(int), GFP_USER); /* @@ -182,15 +157,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 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 +181,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 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 +203,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 0644, + SYSCTL_ZERO, SYSCTL_ONE_HUNDRED); char input[] =3D "9"; size_t len =3D sizeof(input) - 1; loff_t pos =3D 0; @@ -272,15 +226,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 0644, + SYSCTL_ZERO, SYSCTL_ONE_HUNDRED); char input[] =3D "-9"; size_t len =3D sizeof(input) - 1; loff_t pos =3D 0; @@ -304,15 +251,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 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 +282,8 @@ 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 CTLTBL_ENTRY_VNMR(data, "foo", 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); @@ -367,6 +300,111 @@ static void sysctl_test_api_dointvec_write_single_gre= ater_int_max( KUNIT_EXPECT_EQ(test, 0, *((int *)table.data)); } =20 +/* + * Test each CTLTBL_ENTRY_XXX() variant. File-scope variables are required + * so that &var is a valid constant expression in a static initializer. + */ +static int ctltbl_int; +static u8 ctltbl_u8; +static u8 ctltbl_u8_min, ctltbl_u8_max =3D 200; +static bool ctltbl_bool; +static char ctltbl_str[64]; + +struct ctltbl_param { + const char *desc; + struct ctl_table table; /* built by CTLTBL_ENTRY_XXX() */ + struct ctl_table expected; /* expected field values */ +}; + +static const struct ctltbl_param ctltbl_cases[] =3D { + /* auto handler =E2=80=94 int */ + { + .desc =3D "sysctl_test_api_ctltbl_entry_V_int", + .table =3D CTLTBL_ENTRY_V(ctltbl_int), + .expected =3D { .procname =3D "ctltbl_int", .mode =3D 0444, + .data =3D &ctltbl_int, .maxlen =3D sizeof(int), + .proc_handler =3D proc_dointvec }, + }, + { + .desc =3D "sysctl_test_api_ctltbl_entry_VM_int", + .table =3D CTLTBL_ENTRY_VM(ctltbl_int, 0644), + .expected =3D { .procname =3D "ctltbl_int", .mode =3D 0644, + .data =3D &ctltbl_int, .maxlen =3D sizeof(int), + .proc_handler =3D proc_dointvec }, + }, + { + .desc =3D "sysctl_test_api_ctltbl_entry_VMR_int", + .table =3D CTLTBL_ENTRY_VMR(ctltbl_int, 0644, + SYSCTL_ZERO, SYSCTL_ONE_HUNDRED), + .expected =3D { .procname =3D "ctltbl_int", .mode =3D 0644, + .data =3D &ctltbl_int, .maxlen =3D sizeof(int), + .proc_handler =3D proc_dointvec_minmax }, + }, + { + .desc =3D "sysctl_test_api_ctltbl_entry_VN_int", + .table =3D CTLTBL_ENTRY_VN(ctltbl_int, "my-sysctl"), + .expected =3D { .procname =3D "my-sysctl", .mode =3D 0444, + .data =3D &ctltbl_int, .maxlen =3D sizeof(int), + .proc_handler =3D proc_dointvec }, + }, + { + .desc =3D "sysctl_test_api_ctltbl_entry_VNM_int", + .table =3D CTLTBL_ENTRY_VNM(ctltbl_int, "my-sysctl", 0644), + .expected =3D { .procname =3D "my-sysctl", .mode =3D 0644, + .data =3D &ctltbl_int, .maxlen =3D sizeof(int), + .proc_handler =3D proc_dointvec }, + }, + /* auto handler =E2=80=94 u8 */ + { + .desc =3D "sysctl_test_api_ctltbl_entry_V_u8", + .table =3D CTLTBL_ENTRY_V(ctltbl_u8), + .expected =3D { .procname =3D "ctltbl_u8", .mode =3D 0444, + .data =3D &ctltbl_u8, .maxlen =3D sizeof(u8), + .proc_handler =3D proc_dou8vec_minmax }, + }, + { + .desc =3D "sysctl_test_api_ctltbl_entry_VMR_u8", + .table =3D CTLTBL_ENTRY_VMR(ctltbl_u8, 0644, + &ctltbl_u8_min, &ctltbl_u8_max), + .expected =3D { .procname =3D "ctltbl_u8", .mode =3D 0644, + .data =3D &ctltbl_u8, .maxlen =3D sizeof(u8), + .proc_handler =3D proc_dou8vec_minmax }, + }, + /* auto handler =E2=80=94 bool */ + { + .desc =3D "sysctl_test_api_ctltbl_entry_V_bool", + .table =3D CTLTBL_ENTRY_V(ctltbl_bool), + .expected =3D { .procname =3D "ctltbl_bool", .mode =3D 0444, + .data =3D &ctltbl_bool, .maxlen =3D sizeof(bool), + .proc_handler =3D proc_dobool }, + }, + /* VNMH =E2=80=94 char[] cannot be auto-dispatched by _Generic (each char= [N] + * is a distinct type); explicit handler is required for strings. + * .maxlen must be sizeof(array), not sizeof(char *). */ + { + .desc =3D "sysctl_test_api_ctltbl_entry_VNMH_string", + .table =3D CTLTBL_ENTRY_VNMH(ctltbl_str, "foo", 0644, + proc_dostring), + .expected =3D { .procname =3D "foo", .mode =3D 0644, + .data =3D ctltbl_str, .maxlen =3D sizeof(ctltbl_str), + .proc_handler =3D proc_dostring }, + }, +}; + +KUNIT_ARRAY_PARAM_DESC(ctltbl, ctltbl_cases, desc); + +static void sysctl_test_api_ctltbl_entry(struct kunit *test) +{ + const struct ctltbl_param *p =3D test->param_value; + + KUNIT_EXPECT_STREQ(test, p->expected.procname, p->table.procname); + KUNIT_EXPECT_EQ(test, p->expected.mode, p->table.mode); + KUNIT_EXPECT_PTR_EQ(test, p->expected.data, p->table.data); + KUNIT_EXPECT_EQ(test, p->expected.maxlen, p->table.maxlen); + KUNIT_EXPECT_PTR_EQ(test, p->expected.proc_handler, + p->table.proc_handler); +} + static struct kunit_case sysctl_test_cases[] =3D { KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data), KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset), @@ -378,6 +416,7 @@ static struct kunit_case sysctl_test_cases[] =3D { 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_PARAM(sysctl_test_api_ctltbl_entry, ctltbl_gen_params), {} }; =20 --=20 2.25.1