From: Joel Granados <joel.granados@kernel.org>
To: Kees Cook <kees@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
Joel Granados <joel.granados@kernel.org>
Subject: [PATCH 3/7] sysctl: Move UINT converter macros to sysctl header
Date: Fri, 17 Oct 2025 10:32:13 +0200 [thread overview]
Message-ID: <20251017-jag-sysctl_jiffies-v1-3-175d81dfdf82@kernel.org> (raw)
In-Reply-To: <20251017-jag-sysctl_jiffies-v1-0-175d81dfdf82@kernel.org>
Move SYSCTL_USER_TO_KERN_UINT_CONV and SYSCTL_UINT_CONV_CUSTOM macros to
include/linux/sysctl.h. No need to embed sysctl_kern_to_user_uint_conv
in a macro as it will not need a custom kernel pointer operation. This
is a preparation commit to enable jiffies converter creation outside
kernel/sysctl.c.
Signed-off-by: Joel Granados <joel.granados@kernel.org>
---
include/linux/sysctl.h | 40 ++++++++++++++++++++++++++++++++++++++++
kernel/sysctl.c | 42 ++----------------------------------------
2 files changed, 42 insertions(+), 40 deletions(-)
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 967a9ad6b4200266e2d9c6cfa9ee58fb8bf51090..faeeb2feefb83d5b57e67f5cbf2eadf79dd03fb2 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -134,6 +134,45 @@ int do_proc_int_conv##name(bool *negp, unsigned long *u_ptr, int *k_ptr,\
return 0; \
}
+#define SYSCTL_USER_TO_KERN_UINT_CONV(name, u_ptr_op) \
+int sysctl_user_to_kern_uint_conv##name(const unsigned long *u_ptr,\
+ unsigned int *k_ptr) \
+{ \
+ unsigned long u = u_ptr_op(*u_ptr); \
+ if (u > UINT_MAX) \
+ return -EINVAL; \
+ WRITE_ONCE(*k_ptr, u); \
+ return 0; \
+}
+
+#define SYSCTL_UINT_CONV_CUSTOM(name, user_to_kern, kern_to_user, \
+ k_ptr_range_check) \
+int do_proc_uint_conv##name(unsigned long *u_ptr, unsigned int *k_ptr, \
+ int dir, const struct ctl_table *tbl) \
+{ \
+ if (SYSCTL_KERN_TO_USER(dir)) \
+ return kern_to_user(u_ptr, k_ptr); \
+ \
+ if (k_ptr_range_check) { \
+ unsigned int tmp_k; \
+ int ret; \
+ if (!tbl) \
+ return -EINVAL; \
+ ret = user_to_kern(u_ptr, &tmp_k); \
+ if (ret) \
+ return ret; \
+ if ((tbl->extra1 && \
+ *(unsigned int *)tbl->extra1 > tmp_k) || \
+ (tbl->extra2 && \
+ *(unsigned int *)tbl->extra2 < tmp_k)) \
+ return -ERANGE; \
+ WRITE_ONCE(*k_ptr, tmp_k); \
+ } else \
+ return user_to_kern(u_ptr, k_ptr); \
+ return 0; \
+}
+
+
extern const unsigned long sysctl_long_vals[];
typedef int proc_handler(const struct ctl_table *ctl, int write, void *buffer,
@@ -166,6 +205,7 @@ int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int, void *
int proc_do_large_bitmap(const struct ctl_table *, int, void *, size_t *, loff_t *);
int proc_do_static_key(const struct ctl_table *table, int write, void *buffer,
size_t *lenp, loff_t *ppos);
+int sysctl_kern_to_user_uint_conv(unsigned long *u_ptr, const unsigned int *k_ptr);
/*
* Register a set of sysctl names by calling register_sysctl
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 70cf23d9834ef4e6b95ab80a3dc7a06237742793..f3d2385f88494da1e5ba7ee1fbb575d2545d7d84 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -387,54 +387,16 @@ static SYSCTL_INT_CONV_CUSTOM(_ms_jiffies_minmax,
sysctl_user_to_kern_int_conv_ms,
sysctl_kern_to_user_int_conv_ms, true)
-#define SYSCTL_USER_TO_KERN_UINT_CONV(name, u_ptr_op) \
-int sysctl_user_to_kern_uint_conv##name(const unsigned long *u_ptr,\
- unsigned int *k_ptr) \
-{ \
- unsigned long u = u_ptr_op(*u_ptr); \
- if (u > UINT_MAX) \
- return -EINVAL; \
- WRITE_ONCE(*k_ptr, u); \
- return 0; \
-}
-
static SYSCTL_USER_TO_KERN_UINT_CONV(,SYSCTL_CONV_IDENTITY)
-static int sysctl_kern_to_user_uint_conv(unsigned long *u_ptr,
- const unsigned int *k_ptr)
+int sysctl_kern_to_user_uint_conv(unsigned long *u_ptr,
+ const unsigned int *k_ptr)
{
unsigned int val = READ_ONCE(*k_ptr);
*u_ptr = (unsigned long)val;
return 0;
}
-#define SYSCTL_UINT_CONV_CUSTOM(name, user_to_kern, kern_to_user, \
- k_ptr_range_check) \
-int do_proc_uint_conv##name(unsigned long *u_ptr, unsigned int *k_ptr, \
- int dir, const struct ctl_table *tbl) \
-{ \
- if (SYSCTL_KERN_TO_USER(dir)) \
- return kern_to_user(u_ptr, k_ptr); \
- \
- if (k_ptr_range_check) { \
- unsigned int tmp_k; \
- int ret; \
- if (!tbl) \
- return -EINVAL; \
- ret = user_to_kern(u_ptr, &tmp_k); \
- if (ret) \
- return ret; \
- if ((tbl->extra1 && \
- *(unsigned int *)tbl->extra1 > tmp_k) || \
- (tbl->extra2 && \
- *(unsigned int *)tbl->extra2 < tmp_k)) \
- return -ERANGE; \
- WRITE_ONCE(*k_ptr, tmp_k); \
- } else \
- return user_to_kern(u_ptr, k_ptr); \
- return 0; \
-}
-
static SYSCTL_UINT_CONV_CUSTOM(, sysctl_user_to_kern_uint_conv,
sysctl_kern_to_user_uint_conv, false)
static SYSCTL_UINT_CONV_CUSTOM(_minmax, sysctl_user_to_kern_uint_conv,
--
2.50.1
next prev parent reply other threads:[~2025-10-17 8:33 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-17 8:32 [PATCH 0/7] sysctl: Move jiffies converters out of kernel/sysctl.c Joel Granados
2025-10-17 8:32 ` [PATCH 1/7] sysctl: Allow custom converters from outside sysctl Joel Granados
2025-10-17 8:32 ` [PATCH 2/7] sysctl: Move INT converter macros to sysctl header Joel Granados
2025-10-17 8:32 ` Joel Granados [this message]
2025-10-17 8:32 ` [PATCH 4/7] sysctl: Move jiffies converters to kernel/time/jiffies.c Joel Granados
2025-11-02 7:54 ` Joel Granados
2025-10-17 8:32 ` [PATCH 5/7] sysctl: Move proc_doulongvec_ms_jiffies_minmax " Joel Granados
2025-10-17 8:32 ` [PATCH 6/7] sysctl: Create pipe-max-size converter using sysctl UINT macros Joel Granados
2025-10-17 8:32 ` [PATCH 7/7] sysctl: Wrap do_proc_douintvec with the public function proc_douintvec_conv Joel Granados
2025-10-22 9:18 ` kernel test robot
2025-10-27 10:14 ` Joel Granados
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=20251017-jag-sysctl_jiffies-v1-3-175d81dfdf82@kernel.org \
--to=joel.granados@kernel.org \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=kees@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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®