003-v6.3-clk-ralink-fix-mt7621_gate_is_enabled-function.patch 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. From 35dcae535afc153fa83f2fe51c0812536c192c58 Mon Sep 17 00:00:00 2001
  2. From: Sergio Paracuellos <[email protected]>
  3. Date: Mon, 6 Feb 2023 09:33:05 +0100
  4. Subject: [PATCH] clk: ralink: fix 'mt7621_gate_is_enabled()' function
  5. Compiling clock driver with CONFIG_UBSAN enabled shows the following trace:
  6. UBSAN: shift-out-of-bounds in drivers/clk/ralink/clk-mt7621.c:121:15
  7. shift exponent 131072 is too large for 32-bit type 'long unsigned int'
  8. CPU: 1 PID: 1 Comm: swapper/0 Not tainted 5.15.86 #0
  9. Stack : ...
  10. Call Trace:
  11. [<80009a58>] show_stack+0x38/0x118
  12. [<8045ce04>] dump_stack_lvl+0x60/0x80
  13. [<80458868>] ubsan_epilogue+0x10/0x54
  14. [<804590e0>] __ubsan_handle_shift_out_of_bounds+0x118/0x190
  15. [<804c9a10>] mt7621_gate_is_enabled+0x98/0xa0
  16. [<804bb774>] clk_core_is_enabled+0x34/0x90
  17. [<80aad73c>] clk_disable_unused_subtree+0x98/0x1e4
  18. [<80aad6d4>] clk_disable_unused_subtree+0x30/0x1e4
  19. [<80aad6d4>] clk_disable_unused_subtree+0x30/0x1e4
  20. [<80aad900>] clk_disable_unused+0x78/0x120
  21. [<80002030>] do_one_initcall+0x54/0x1f0
  22. [<80a922a4>] kernel_init_freeable+0x280/0x31c
  23. [<808047c4>] kernel_init+0x20/0x118
  24. [<80003e58>] ret_from_kernel_thread+0x14/0x1c
  25. Shifting a value (131032) larger than the type (32 bit unsigned integer)
  26. is undefined behaviour in C.
  27. The problem is in 'mt7621_gate_is_enabled()' function which is using the
  28. 'BIT()' kernel macro with the bit index for the clock gate to check if the
  29. bit is set. When the clock gates structure is created driver is already
  30. setting 'bit_idx' using 'BIT()' macro, so we are wrongly applying an extra
  31. 'BIT()' mask here. Removing it solve the problem and makes this function
  32. correct. However when clock gating is correctly working, the kernel starts
  33. disabling those clocks that are not requested. Some drivers for this SoC
  34. are older than this clock driver itself. So to avoid the kernel to disable
  35. clocks that have been enabled until now, we must apply 'CLK_IS_CRITICAL'
  36. flag on gates initialization code.
  37. Fixes: 48df7a26f470 ("clk: ralink: add clock driver for mt7621 SoC")
  38. Signed-off-by: Sergio Paracuellos <[email protected]>
  39. Link: https://lore.kernel.org/r/[email protected]
  40. Signed-off-by: Stephen Boyd <[email protected]>
  41. ---
  42. drivers/clk/ralink/clk-mt7621.c | 10 ++++++++--
  43. 1 file changed, 8 insertions(+), 2 deletions(-)
  44. --- a/drivers/clk/ralink/clk-mt7621.c
  45. +++ b/drivers/clk/ralink/clk-mt7621.c
  46. @@ -121,7 +121,7 @@ static int mt7621_gate_is_enabled(struct
  47. if (regmap_read(sysc, SYSC_REG_CLKCFG1, &val))
  48. return 0;
  49. - return val & BIT(clk_gate->bit_idx);
  50. + return val & clk_gate->bit_idx;
  51. }
  52. static const struct clk_ops mt7621_gate_ops = {
  53. @@ -133,8 +133,14 @@ static const struct clk_ops mt7621_gate_
  54. static int mt7621_gate_ops_init(struct device *dev,
  55. struct mt7621_gate *sclk)
  56. {
  57. + /*
  58. + * There are drivers for this SoC that are older
  59. + * than clock driver and are not prepared for the clock.
  60. + * We don't want the kernel to disable anything so we
  61. + * add CLK_IS_CRITICAL flag here.
  62. + */
  63. struct clk_init_data init = {
  64. - .flags = CLK_SET_RATE_PARENT,
  65. + .flags = CLK_SET_RATE_PARENT | CLK_IS_CRITICAL,
  66. .num_parents = 1,
  67. .parent_names = &sclk->parent_name,
  68. .ops = &mt7621_gate_ops,