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 2/4] sysctl: add helper functions to extract table->extra1/extra2
Date: Wed, 14 Jan 2026 01:40:31 +0800 [thread overview]
Message-ID: <9ebbce66874c884f7112476bfd4132510d06854c.1768324215.git.wen.yang@linux.dev> (raw)
In-Reply-To: <cover.1768324215.git.wen.yang@linux.dev>
From: Wen Yang <wen.yang@linux.dev>
Add some sysctl helper functions to avoid direct access to
table->extra1/extra2.
No functional change intended.
Selftest were also made:
[14:52:13] ================ sysctl_test (10 subtests) =================
[14:52:13] [PASSED] sysctl_test_api_dointvec_null_tbl_data
[14:52:13] [PASSED] sysctl_test_api_dointvec_table_maxlen_unset
[14:52:13] [PASSED] sysctl_test_api_dointvec_table_len_is_zero
[14:52:13] [PASSED] sysctl_test_api_dointvec_table_read_but_position_set
[14:52:13] [PASSED] sysctl_test_dointvec_read_happy_single_positive
[14:52:13] [PASSED] sysctl_test_dointvec_read_happy_single_negative
[14:52:13] [PASSED] sysctl_test_dointvec_write_happy_single_positive
[14:52:13] [PASSED] sysctl_test_dointvec_write_happy_single_negative
[14:52:13] [PASSED] sysctl_test_api_dointvec_write_single_less_int_min
[14:52:13] [PASSED] sysctl_test_api_dointvec_write_single_greater_int_max
[14:52:13] =================== [PASSED] sysctl_test ===================
[14:52:13] ============================================================
[14:52:13] Testing complete. Ran 10 tests: passed: 10
Suggested-by: Joel Granados <joel.granados@kernel.org>
Signed-off-by: Wen Yang <wen.yang@linux.dev>
---
include/linux/sysctl.h | 10 ++++++++++
kernel/sysctl.c | 14 ++++----------
2 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 683422bc3e7f..9690740885ab 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -164,6 +164,16 @@ static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
#define DEFINE_CTL_TABLE_POLL(name) \
struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
+#define SYSCTL_IN_RANGE(tbl, val, type) \
+ ((tbl) && \
+ (!(tbl)->extra1 || (*(type *)(tbl)->extra1 <= (type)(val))) && \
+ (!(tbl)->extra2 || (*(type *)(tbl)->extra2 >= (type)(val))))
+
+#define SYSCTL_IN_RANGE_INT(tbl, val) SYSCTL_IN_RANGE(tbl, val, int)
+#define SYSCTL_IN_RANGE_LONG(tbl, val) SYSCTL_IN_RANGE(tbl, val, long)
+#define SYSCTL_IN_RANGE_UINT(tbl, val) SYSCTL_IN_RANGE(tbl, val, unsigned int)
+#define SYSCTL_IN_RANGE_ULONG(tbl, val) SYSCTL_IN_RANGE(tbl, val, unsigned long)
+
/* A sysctl table is an array of struct ctl_table: */
struct ctl_table {
const char *procname; /* Text ID for /proc/sys */
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 2e769550b268..990e692f626b 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -428,10 +428,7 @@ int proc_uint_conv(ulong *u_ptr, uint *k_ptr, int dir,
ret = user_to_kern(u_ptr, &tmp_k);
if (ret)
return ret;
- if ((tbl->extra1 &&
- *(uint *)tbl->extra1 > tmp_k) ||
- (tbl->extra2 &&
- *(uint *)tbl->extra2 < tmp_k))
+ if (!SYSCTL_IN_RANGE_UINT(tbl, tmp_k))
return -ERANGE;
WRITE_ONCE(*k_ptr, tmp_k);
} else
@@ -531,8 +528,7 @@ int proc_int_conv(bool *negp, ulong *u_ptr, int *k_ptr, int dir,
ret = user_to_kern(negp, u_ptr, &tmp_k);
if (ret)
return ret;
- if ((tbl->extra1 && *(int *)tbl->extra1 > tmp_k) ||
- (tbl->extra2 && *(int *)tbl->extra2 < tmp_k))
+ if (!SYSCTL_IN_RANGE_INT(tbl, tmp_k))
return -EINVAL;
WRITE_ONCE(*k_ptr, tmp_k);
} else
@@ -969,7 +965,7 @@ static int do_proc_doulongvec_minmax(const struct ctl_table *table, int dir,
unsigned long convmul,
unsigned long convdiv)
{
- unsigned long *i, *min, *max;
+ unsigned long *i ;
int vleft, first = 1, err = 0;
size_t left;
char *p;
@@ -981,8 +977,6 @@ static int do_proc_doulongvec_minmax(const struct ctl_table *table, int dir,
}
i = table->data;
- min = table->extra1;
- max = table->extra2;
vleft = table->maxlen / sizeof(unsigned long);
left = *lenp;
@@ -1014,7 +1008,7 @@ static int do_proc_doulongvec_minmax(const struct ctl_table *table, int dir,
}
val = convmul * val / convdiv;
- if ((min && val < *min) || (max && val > *max)) {
+ if (!SYSCTL_IN_RANGE_ULONG(table, val)) {
err = -EINVAL;
break;
}
--
2.25.1
next prev parent reply other threads:[~2026-01-13 17:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-13 17:40 [RFC PATCH 0/4] sysctl: refactor ctl_table creation and change extra{1,2} type wen.yang
2026-01-13 17:40 ` [RFC PATCH 1/4] sysctl: add SYSCTL_ENTRY/SYSCTL_RANGE_ENTRY helpers for ctl_table init wen.yang
2026-01-21 14:30 ` Joel Granados
2026-01-22 9:56 ` Joel Granados
2026-01-25 20:08 ` Wen Yang
2026-01-13 17:40 ` wen.yang [this message]
2026-01-13 17:40 ` [RFC PATCH 3/4] sysctl: support encoding values directly in the table entry wen.yang
2026-01-13 17:40 ` [RFC PATCH 4/4] scripts/coccinelle: add sysctl_table_init.cocci 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=9ebbce66874c884f7112476bfd4132510d06854c.1768324215.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®