Skip to content

Commit 0bee3b6

Browse files
Frederic WeisbeckerKAGA-KOKO
authored andcommitted
hrtimer: Improve comments on handling priority inversion against softirq kthread
The handling of a priority inversion between timer cancelling and a a not well defined possible preemption of softirq kthread is not very clear. Especially in the posix timers side it's unclear why there is a specific RT wait callback. All the nice explanations can be found in the initial changelog of f61eff8 (hrtimer: Prepare support for PREEMPT_RT"). Extract the detailed informations from there and put it into comments. Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lkml.kernel.org/r/20190820132656.GC2093@lenoir
1 parent ec8f954 commit 0bee3b6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

kernel/time/hrtimer.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,10 +1201,16 @@ static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base,
12011201
* deletion of a timer failed because the timer callback function was
12021202
* running.
12031203
*
1204-
* This prevents priority inversion, if the softirq thread on a remote CPU
1205-
* got preempted, and it prevents a life lock when the task which tries to
1206-
* delete a timer preempted the softirq thread running the timer callback
1207-
* function.
1204+
* This prevents priority inversion: if the soft irq thread is preempted
1205+
* in the middle of a timer callback, then calling del_timer_sync() can
1206+
* lead to two issues:
1207+
*
1208+
* - If the caller is on a remote CPU then it has to spin wait for the timer
1209+
* handler to complete. This can result in unbound priority inversion.
1210+
*
1211+
* - If the caller originates from the task which preempted the timer
1212+
* handler on the same CPU, then spin waiting for the timer handler to
1213+
* complete is never going to end.
12081214
*/
12091215
void hrtimer_cancel_wait_running(const struct hrtimer *timer)
12101216
{

kernel/time/posix-timers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,12 @@ static void common_timer_wait_running(struct k_itimer *timer)
810810
hrtimer_cancel_wait_running(&timer->it.real.timer);
811811
}
812812

813+
/*
814+
* On PREEMPT_RT this prevent priority inversion against softirq kthread in
815+
* case it gets preempted while executing a timer callback. See comments in
816+
* hrtimer_cancel_wait_running. For PREEMPT_RT=n this just results in a
817+
* cpu_relax().
818+
*/
813819
static struct k_itimer *timer_wait_running(struct k_itimer *timer,
814820
unsigned long *flags)
815821
{

0 commit comments

Comments
 (0)