[PATCH] sysctl: Do jiffy / ms conversion in ulong space
From: Joel Granados
Date: Wed Nov 18 2026 - 08:45:10 EST
Replace jiffies_to_msecs & msecs_to_jiffies calls with sysctl specific
converters. Removes the need to handle overflow corner cases when moving
to and from the unsigned int and the unsigned long spaces.
Handle the unsigned long overflow in the sysctl specific converters
(sysctl_jiffies_to_msecs & sysctl_msecs_to_jiffies). The unsigned int
overflow is handled in the generic sysctl int converters (e.g.
proc_int_u2k_conv_uop).
Round up on writes and down on reads for both unsigned int and unsigned
long types.
This is the behavior being fixed:
SUL="/proc/sys/net/rxrpc/idle_conn_fast_expiry"
SSI="/proc/sys/debug/test_sysctl/int_ms_jiffies"
1. Before
# HZ = 300, CONFIG_TEST_SYSCTL=y & CONFIG_AF_RXRPC=y
echo 1 > $SSI; cat $SSI # -> 4
echo 4 > $SSI; cat $SSI # -> 7
echo 7 > $SSI; cat $SSI # -> 10
echo 10 > $SSI; cat $SSI # -> 10
echo 4 > $SUL; cat $SUL # -> 3
echo 3 > $SUL; cat $SUL # echo: write error: Invalid argument
# HZ = 100, CONFIG_TEST_SYSCTL=y & CONFIG_AF_RXRPC=y
echo 2147483648 > $SUL; cat $SUL # -> 4294967276
echo 5000000000 > $SUL; cat $SUL # -> 4294967276
2. Now
# HZ = 300, CONFIG_TEST_SYSCTL=y & CONFIG_AF_RXRPC=y
echo 1 > $SSI; cat $SSI # -> 3
echo 3 > $SSI; cat $SSI # -> 3
echo 4 > $SSI; cat $SSI # -> 6
echo 6 > $SSI; cat $SSI # -> 6
echo 7 > $SSI; cat $SSI # -> 10
echo 10 > $SSI; cat $SSI # -> 10
echo 4 > $SUL; cat $SUL # -> 6
echo 6 > $SUL; cat $SUL # -> 6
# HZ = 100, CONFIG_TEST_SYSCTL=y & CONFIG_AF_RXRPC=y
echo 2147483648 > $SUL; cat $SUL # -> 2147483650
echo 5000000000 > $SUL; cat $SUL # -> 5000000000
Fixes: afdf35cfae0d ("sysctl: Fix type truncation in sysctl_msec_to_jiffies")
Fixes: b96b5c6708ea ("sysctl: Replace do_proc_do{int,ulong,uint}vec with do_proc_vec")
Reported-by: Avi Weiss <thnkslprpt@xxxxxxxxx>
Link: https://lore.kernel.org/all/20261102111516.1398262-2-thnkslprpt@xxxxxxxxx
Signed-off-by: Joel Granados <joel.granados@xxxxxxxxxx>
---
Avi:
This is how it will be upstreamed. In case you have any opinions.
Best
---
kernel/time/jiffies.c | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
index af1c69bf0c23dbcf1788e951dd9872167388bd1c..b861dc860d3452b59868128ec83340eab9e18a39 100644
--- a/kernel/time/jiffies.c
+++ b/kernel/time/jiffies.c
@@ -137,11 +137,19 @@ static int sysctl_k2u_int_conv_userhz(bool *negp, ulong *u_ptr, const int *k_ptr
return proc_int_k2u_conv_kop(u_ptr, k_ptr, negp, sysctl_jiffies_to_clock_t);
}
+/*
+ * Calc ms <-> jiffies conversions for sysctls in unsigned long arithmetic.
+ * Think hard about using the non-sysctl converters (msecs_to_jiffies &
+ * jiffies_to_msecs) as the interaction between ulong and uint is not trivial.
+ */
static ulong sysctl_msecs_to_jiffies(const ulong val)
{
- if (val > jiffies_to_msecs(MAX_JIFFY_OFFSET))
- return MAX_JIFFY_OFFSET;
- return msecs_to_jiffies(val);
+ ulong secs = val / MSEC_PER_SEC;
+
+ if (secs >= ULONG_MAX / HZ)
+ return ULONG_MAX;
+
+ return secs * HZ + DIV_ROUND_UP((val % MSEC_PER_SEC) * HZ, MSEC_PER_SEC);
}
static int sysctl_u2k_int_conv_ms(const bool *negp, const ulong *u_ptr, int *k_ptr)
@@ -151,7 +159,12 @@ static int sysctl_u2k_int_conv_ms(const bool *negp, const ulong *u_ptr, int *k_p
static ulong sysctl_jiffies_to_msecs(const ulong val)
{
- return jiffies_to_msecs(val);
+ ulong secs = val / HZ;
+
+ if (secs >= ULONG_MAX / MSEC_PER_SEC)
+ return ULONG_MAX;
+
+ return secs * MSEC_PER_SEC + (val % HZ) * MSEC_PER_SEC / HZ;
}
static int sysctl_k2u_int_conv_ms(bool *negp, ulong *u_ptr, const int *k_ptr)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261118-lklm-sysctl-jiffie-ms-conv-708b96e4590e
Best regards,
--
Joel Granados <joel.granados@xxxxxxxxxx>