Skip to content

Commit b9023b9

Browse files
bvivekanKAGA-KOKO
authored andcommitted
tick: broadcast-hrtimer: Fix a race in bc_set_next
When a cpu requests broadcasting, before starting the tick broadcast hrtimer, bc_set_next() checks if the timer callback (bc_handler) is active using hrtimer_try_to_cancel(). But hrtimer_try_to_cancel() does not provide the required synchronization when the callback is active on other core. The callback could have already executed tick_handle_oneshot_broadcast() and could have also returned. But still there is a small time window where the hrtimer_try_to_cancel() returns -1. In that case bc_set_next() returns without doing anything, but the next_event of the tick broadcast clock device is already set to a timeout value. In the race condition diagram below, CPU #1 is running the timer callback and CPU #2 is entering idle state and so calls bc_set_next(). In the worst case, the next_event will contain an expiry time, but the hrtimer will not be started which happens when the racing callback returns HRTIMER_NORESTART. The hrtimer might never recover if all further requests from the CPUs to subscribe to tick broadcast have timeout greater than the next_event of tick broadcast clock device. This leads to cascading of failures and finally noticed as rcu stall warnings Here is a depiction of the race condition CPU #1 (Running timer callback) CPU #2 (Enter idle and subscribe to tick broadcast) --------------------- --------------------- __run_hrtimer() tick_broadcast_enter() bc_handler() __tick_broadcast_oneshot_control() tick_handle_oneshot_broadcast() raw_spin_lock(&tick_broadcast_lock); dev->next_event = KTIME_MAX; //wait for tick_broadcast_lock //next_event for tick broadcast clock set to KTIME_MAX since no other cores subscribed to tick broadcasting raw_spin_unlock(&tick_broadcast_lock); if (dev->next_event == KTIME_MAX) return HRTIMER_NORESTART // callback function exits without restarting the hrtimer //tick_broadcast_lock acquired raw_spin_lock(&tick_broadcast_lock); tick_broadcast_set_event() clockevents_program_event() dev->next_event = expires; bc_set_next() hrtimer_try_to_cancel() //returns -1 since the timer callback is active. Exits without restarting the timer cpu_base->running = NULL; The comment that hrtimer cannot be armed from within the callback is wrong. It is fine to start the hrtimer from within the callback. Also it is safe to start the hrtimer from the enter/exit idle code while the broadcast handler is active. The enter/exit idle code and the broadcast handler are synchronized using tick_broadcast_lock. So there is no need for the existing try to cancel logic. All this can be removed which will eliminate the race condition as well. Fixes: 5d1638a ("tick: Introduce hrtimer based broadcast") Originally-by: Thomas Gleixner <[email protected]> Signed-off-by: Balasubramani Vivekanandan <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent da05b5e commit b9023b9

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

kernel/time/tick-broadcast-hrtimer.c

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,39 @@ static int bc_shutdown(struct clock_event_device *evt)
4242
*/
4343
static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
4444
{
45-
int bc_moved;
4645
/*
47-
* We try to cancel the timer first. If the callback is on
48-
* flight on some other cpu then we let it handle it. If we
49-
* were able to cancel the timer nothing can rearm it as we
50-
* own broadcast_lock.
46+
* This is called either from enter/exit idle code or from the
47+
* broadcast handler. In all cases tick_broadcast_lock is held.
5148
*
52-
* However we can also be called from the event handler of
53-
* ce_broadcast_hrtimer itself when it expires. We cannot
54-
* restart the timer because we are in the callback, but we
55-
* can set the expiry time and let the callback return
56-
* HRTIMER_RESTART.
49+
* hrtimer_cancel() cannot be called here neither from the
50+
* broadcast handler nor from the enter/exit idle code. The idle
51+
* code can run into the problem described in bc_shutdown() and the
52+
* broadcast handler cannot wait for itself to complete for obvious
53+
* reasons.
5754
*
58-
* Since we are in the idle loop at this point and because
59-
* hrtimer_{start/cancel} functions call into tracing,
60-
* calls to these functions must be bound within RCU_NONIDLE.
55+
* Each caller tries to arm the hrtimer on its own CPU, but if the
56+
* hrtimer callbback function is currently running, then
57+
* hrtimer_start() cannot move it and the timer stays on the CPU on
58+
* which it is assigned at the moment.
59+
*
60+
* As this can be called from idle code, the hrtimer_start()
61+
* invocation has to be wrapped with RCU_NONIDLE() as
62+
* hrtimer_start() can call into tracing.
6163
*/
62-
RCU_NONIDLE(
63-
{
64-
bc_moved = hrtimer_try_to_cancel(&bctimer) >= 0;
65-
if (bc_moved) {
66-
hrtimer_start(&bctimer, expires,
67-
HRTIMER_MODE_ABS_PINNED_HARD);
68-
}
69-
}
70-
);
71-
72-
if (bc_moved) {
73-
/* Bind the "device" to the cpu */
74-
bc->bound_on = smp_processor_id();
75-
} else if (bc->bound_on == smp_processor_id()) {
76-
hrtimer_set_expires(&bctimer, expires);
77-
}
64+
RCU_NONIDLE( {
65+
hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED_HARD);
66+
/*
67+
* The core tick broadcast mode expects bc->bound_on to be set
68+
* correctly to prevent a CPU which has the broadcast hrtimer
69+
* armed from going deep idle.
70+
*
71+
* As tick_broadcast_lock is held, nothing can change the cpu
72+
* base which was just established in hrtimer_start() above. So
73+
* the below access is safe even without holding the hrtimer
74+
* base lock.
75+
*/
76+
bc->bound_on = bctimer.base->cpu_base->cpu;
77+
} );
7878
return 0;
7979
}
8080

@@ -100,10 +100,6 @@ static enum hrtimer_restart bc_handler(struct hrtimer *t)
100100
{
101101
ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
102102

103-
if (clockevent_state_oneshot(&ce_broadcast_hrtimer))
104-
if (ce_broadcast_hrtimer.next_event != KTIME_MAX)
105-
return HRTIMER_RESTART;
106-
107103
return HRTIMER_NORESTART;
108104
}
109105

0 commit comments

Comments
 (0)