Skip to content

Commit

Permalink
Fix shift out of bound exception
Browse files Browse the repository at this point in the history
avoiding unnecessary calculating of 1<<-1 in case of reset_idx == -1
the value -1, is a valid and used value for reset_idx


```
1.006965] UBSAN: shift-out-of-bounds in drivers/clk/clk-aspeed.c:214:12
[    1.007271] shift exponent -1 is negative
[    1.007412] CPU: 0 PID: 1 Comm: swapper Not tainted 6.6.58-345bcb4-19272-g345bcb402a84 openbmc#1
[    1.007563] Hardware name: Generic DT based system
[    1.007749]  unwind_backtrace from show_stack+0x10/0x14
[    1.008434]  show_stack from dump_stack_lvl+0x24/0x2c
[    1.008479]  dump_stack_lvl from ubsan_epilogue+0x8/0x34
[    1.008518]  ubsan_epilogue from __ubsan_handle_shift_out_of_bounds+0xe0/0x110
[    1.008559]  __ubsan_handle_shift_out_of_bounds from aspeed_clk_enable+0x44/0x124
[    1.008600]  aspeed_clk_enable from clk_core_enable+0x80/0xec
[    1.008634]  clk_core_enable from clk_core_enable_lock+0x18/0x2c
[    1.008668]  clk_core_enable_lock from __clk_register+0x594/0x6ac
[    1.008705]  __clk_register from clk_hw_register+0x30/0x3c
[    1.008741]  clk_hw_register from aspeed_clk_probe+0x5fc/0x6b4
[    1.008776]  aspeed_clk_probe from platform_probe+0x58/0xc0
[    1.008813]  platform_probe from really_probe+0x1b0/0x3d0
[    1.008848]  really_probe from __driver_probe_device+0x1c0/0x1f8
[    1.008882]  __driver_probe_device from driver_probe_device+0x2c/0xc4
[    1.008916]  driver_probe_device from __driver_attach+0x148/0x16c
[    1.008950]  __driver_attach from bus_for_each_dev+0x78/0xb8
[    1.008989]  bus_for_each_dev from bus_add_driver+0xf8/0x220
[    1.009027]  bus_add_driver from driver_register+0xb8/0x100
[    1.009060]  driver_register from do_one_initcall+0x70/0x224
[    1.009095]  do_one_initcall from kernel_init_freeable+0x1e8/0x238
[    1.009136]  kernel_init_freeable from kernel_init+0x14/0x108
[    1.009171]  kernel_init from ret_from_fork+0x14/0x28
[    1.009256] Exception stack(0xbc815fb0 to 0xbc815ff8)
```

Signed-off-by: Ben Peled <bpeled@nvidia.com>
  • Loading branch information
bpeled committed Jan 17, 2025
1 parent 345bcb4 commit 7607185
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions drivers/clk/clk-aspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ static int aspeed_clk_is_enabled(struct clk_hw *hw)
{
struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
u32 clk = BIT(gate->clock_idx);
u32 rst = BIT(gate->reset_idx);
u32 enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
u32 reg;

Expand All @@ -196,6 +195,8 @@ static int aspeed_clk_is_enabled(struct clk_hw *hw)
* will fail to lift the reset.
*/
if (gate->reset_idx >= 0) {
u32 rst = BIT(gate->reset_idx);

regmap_read(gate->map, ASPEED_RESET_CTRL, &reg);
if (reg & rst)
return 0;
Expand All @@ -211,8 +212,8 @@ static int aspeed_clk_enable(struct clk_hw *hw)
struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
unsigned long flags;
u32 clk = BIT(gate->clock_idx);
u32 rst = BIT(gate->reset_idx);
u32 enval;
u32 rst;

spin_lock_irqsave(gate->lock, flags);

Expand All @@ -222,6 +223,7 @@ static int aspeed_clk_enable(struct clk_hw *hw)
}

if (gate->reset_idx >= 0) {
rst = BIT(gate->reset_idx);
/* Put IP in reset */
regmap_update_bits(gate->map, ASPEED_RESET_CTRL, rst, rst);

Expand Down

0 comments on commit 7607185

Please sign in to comment.