5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cunhao Lu <1579567540@qq.com> commit 13467fd6998f5da4f5b4f172f1af3d9c48d5de8f upstream. The debounce interval calculations use unsigned long operands. On 32-bit systems, the numerator used to calculate the maximum interval overflows before division, while multiplying the requested interval by the clock rate overflows before it is assigned to a u64. A zero debounce clock rate also causes division by zero. Use 64-bit arithmetic for both calculations and reject a zero clock rate before division. At 32768 Hz, a 1 us request rounds to zero divider periods. Subtracting one then wraps the divider value to ULONG_MAX. Decrement only a nonzero rounded period count so that such requests use the smallest divider value. Fixes: 3bcbd1a85b68 ("gpio/rockchip: support next version gpio controller") Cc: stable@vger.kernel.org Signed-off-by: Cunhao Lu <1579567540@qq.com> Link: https://patch.msgid.link/20261023-fix-rockchip-gpio-v2-debounce-v1-1-f1141bd5b58f@qq.com Signed-off-by: Bartosz Golaszewski Signed-off-by: Greg Kroah-Hartman --- drivers/gpio/gpio-rockchip.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) --- a/drivers/gpio/gpio-rockchip.c +++ b/drivers/gpio/gpio-rockchip.c @@ -198,20 +198,25 @@ static int rockchip_gpio_set_debounce(st { struct rockchip_pin_bank *bank = gpiochip_get_data(gc); const struct rockchip_gpio_regs *reg = bank->gpio_regs; - unsigned long flags, div_reg, freq, max_debounce; + unsigned long flags, div_reg, freq; bool div_debounce_support; unsigned int cur_div_reg; - u64 div; + u64 div, max_debounce; if (bank->gpio_type == GPIO_TYPE_V2 && !IS_ERR(bank->db_clk)) { div_debounce_support = true; freq = clk_get_rate(bank->db_clk); - max_debounce = (GENMASK(23, 0) + 1) * 2 * 1000000 / freq; + if (!freq) + return -EINVAL; + + div = (u64)(GENMASK(23, 0) + 1) * 2 * USEC_PER_SEC; + max_debounce = div64_u64(div, freq); if (debounce > max_debounce) return -EINVAL; - div = debounce * freq; - div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC) - 1; + div = (u64)debounce * freq; + div_reg = DIV_ROUND_CLOSEST_ULL(div, 2 * USEC_PER_SEC); + div_reg = div_reg ? div_reg - 1 : 0; } else { div_debounce_support = false; }