Skip to content

Commit e0aeca3

Browse files
dlezcanoIngo Molnar
authored andcommitted
clocksource/drivers/stm32: Fix kernel panic with multiple timers
The current code hides a couple of bugs: - The global variable 'clock_event_ddata' is overwritten each time the init function is invoked. This is fixed with a kmemdup() instead of assigning the global variable. That prevents a memory corruption when several timers are defined in the DT. - The clockevent's event_handler is NULL if the time framework does not select the clockevent when registering it, this is fine but the init code generates in any case an interrupt leading to dereference this NULL pointer. The stm32 timer works with shadow registers, a mechanism to cache the registers. When a change is done in one buffered register, we need to artificially generate an event to force the timer to copy the content of the register to the shadowed register. The auto-reload register (ARR) is one of the shadowed register as well as the prescaler register (PSC), so in order to force the copy, we issue an event which in turn leads to an interrupt and the NULL dereference. This is fixed by inverting two lines where we clear the status register before enabling the update event interrupt. As this kernel crash is resulting from the combination of these two bugs, the fixes are grouped into a single patch. Tested-by: Benjamin Gaignard <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Acked-by: Benjamin Gaignard <[email protected]> Cc: Alexandre Torgue <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Maxime Coquelin <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 9aea417 commit e0aeca3

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

drivers/clocksource/timer-stm32.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ static int __init stm32_clockevent_init(struct device_node *np)
106106
unsigned long rate, max_delta;
107107
int irq, ret, bits, prescaler = 1;
108108

109+
data = kmemdup(&clock_event_ddata, sizeof(*data), GFP_KERNEL);
110+
if (!data)
111+
return -ENOMEM;
112+
109113
clk = of_clk_get(np, 0);
110114
if (IS_ERR(clk)) {
111115
ret = PTR_ERR(clk);
@@ -156,8 +160,8 @@ static int __init stm32_clockevent_init(struct device_node *np)
156160

157161
writel_relaxed(prescaler - 1, data->base + TIM_PSC);
158162
writel_relaxed(TIM_EGR_UG, data->base + TIM_EGR);
159-
writel_relaxed(TIM_DIER_UIE, data->base + TIM_DIER);
160163
writel_relaxed(0, data->base + TIM_SR);
164+
writel_relaxed(TIM_DIER_UIE, data->base + TIM_DIER);
161165

162166
data->periodic_top = DIV_ROUND_CLOSEST(rate, prescaler * HZ);
163167

@@ -184,6 +188,7 @@ static int __init stm32_clockevent_init(struct device_node *np)
184188
err_clk_enable:
185189
clk_put(clk);
186190
err_clk_get:
191+
kfree(data);
187192
return ret;
188193
}
189194

0 commit comments

Comments
 (0)