Commit ec8f954a authored by Thomas Gleixner's avatar Thomas Gleixner

posix-timers: Use a callback for cancel synchronization on PREEMPT_RT

Posix timer delete retry loops are affected by the same priority inversion
and live lock issues as the other timers.
    
Provide a RT specific synchronization function which keeps a reference to
the timer by holding rcu read lock to prevent the timer from being freed,
dropping the timer lock and invoking the timer specific wait function via a
new callback.
    
This does not yet cover posix CPU timers because they need more special
treatment on PREEMPT_RT.

[ This is folded into the original attempt which did not use a callback. ]
Originally-by: default avatarAnna-Maria Gleixenr <anna-maria@linutronix.de>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Reviewed-by: default avatarFrederic Weisbecker <frederic@kernel.org>
Link: https://lkml.kernel.org/r/20190819143801.656864506@linutronix.de
parent 5d99b32a
...@@ -605,6 +605,19 @@ static int alarm_timer_try_to_cancel(struct k_itimer *timr) ...@@ -605,6 +605,19 @@ static int alarm_timer_try_to_cancel(struct k_itimer *timr)
return alarm_try_to_cancel(&timr->it.alarm.alarmtimer); return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
} }
/**
* alarm_timer_wait_running - Posix timer callback to wait for a timer
* @timr: Pointer to the posixtimer data struct
*
* Called from the core code when timer cancel detected that the callback
* is running. @timr is unlocked and rcu read lock is held to prevent it
* from being freed.
*/
static void alarm_timer_wait_running(struct k_itimer *timr)
{
hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
}
/** /**
* alarm_timer_arm - Posix timer callback to arm a timer * alarm_timer_arm - Posix timer callback to arm a timer
* @timr: Pointer to the posixtimer data struct * @timr: Pointer to the posixtimer data struct
...@@ -834,6 +847,7 @@ const struct k_clock alarm_clock = { ...@@ -834,6 +847,7 @@ const struct k_clock alarm_clock = {
.timer_forward = alarm_timer_forward, .timer_forward = alarm_timer_forward,
.timer_remaining = alarm_timer_remaining, .timer_remaining = alarm_timer_remaining,
.timer_try_to_cancel = alarm_timer_try_to_cancel, .timer_try_to_cancel = alarm_timer_try_to_cancel,
.timer_wait_running = alarm_timer_wait_running,
.nsleep = alarm_timer_nsleep, .nsleep = alarm_timer_nsleep,
}; };
#endif /* CONFIG_POSIX_TIMERS */ #endif /* CONFIG_POSIX_TIMERS */
......
...@@ -805,13 +805,25 @@ static int common_hrtimer_try_to_cancel(struct k_itimer *timr) ...@@ -805,13 +805,25 @@ static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
return hrtimer_try_to_cancel(&timr->it.real.timer); return hrtimer_try_to_cancel(&timr->it.real.timer);
} }
static void common_timer_wait_running(struct k_itimer *timer)
{
hrtimer_cancel_wait_running(&timer->it.real.timer);
}
static struct k_itimer *timer_wait_running(struct k_itimer *timer, static struct k_itimer *timer_wait_running(struct k_itimer *timer,
unsigned long *flags) unsigned long *flags)
{ {
const struct k_clock *kc = READ_ONCE(timer->kclock);
timer_t timer_id = READ_ONCE(timer->it_id); timer_t timer_id = READ_ONCE(timer->it_id);
/* Prevent kfree(timer) after dropping the lock */
rcu_read_lock();
unlock_timer(timer, *flags); unlock_timer(timer, *flags);
cpu_relax();
if (!WARN_ON_ONCE(!kc->timer_wait_running))
kc->timer_wait_running(timer);
rcu_read_unlock();
/* Relock the timer. It might be not longer hashed. */ /* Relock the timer. It might be not longer hashed. */
return lock_timer(timer_id, flags); return lock_timer(timer_id, flags);
} }
...@@ -1255,6 +1267,7 @@ static const struct k_clock clock_realtime = { ...@@ -1255,6 +1267,7 @@ static const struct k_clock clock_realtime = {
.timer_forward = common_hrtimer_forward, .timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining, .timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel, .timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm, .timer_arm = common_hrtimer_arm,
}; };
...@@ -1270,6 +1283,7 @@ static const struct k_clock clock_monotonic = { ...@@ -1270,6 +1283,7 @@ static const struct k_clock clock_monotonic = {
.timer_forward = common_hrtimer_forward, .timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining, .timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel, .timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm, .timer_arm = common_hrtimer_arm,
}; };
...@@ -1300,6 +1314,7 @@ static const struct k_clock clock_tai = { ...@@ -1300,6 +1314,7 @@ static const struct k_clock clock_tai = {
.timer_forward = common_hrtimer_forward, .timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining, .timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel, .timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm, .timer_arm = common_hrtimer_arm,
}; };
...@@ -1315,6 +1330,7 @@ static const struct k_clock clock_boottime = { ...@@ -1315,6 +1330,7 @@ static const struct k_clock clock_boottime = {
.timer_forward = common_hrtimer_forward, .timer_forward = common_hrtimer_forward,
.timer_remaining = common_hrtimer_remaining, .timer_remaining = common_hrtimer_remaining,
.timer_try_to_cancel = common_hrtimer_try_to_cancel, .timer_try_to_cancel = common_hrtimer_try_to_cancel,
.timer_wait_running = common_timer_wait_running,
.timer_arm = common_hrtimer_arm, .timer_arm = common_hrtimer_arm,
}; };
......
...@@ -24,6 +24,7 @@ struct k_clock { ...@@ -24,6 +24,7 @@ struct k_clock {
int (*timer_try_to_cancel)(struct k_itimer *timr); int (*timer_try_to_cancel)(struct k_itimer *timr);
void (*timer_arm)(struct k_itimer *timr, ktime_t expires, void (*timer_arm)(struct k_itimer *timr, ktime_t expires,
bool absolute, bool sigev_none); bool absolute, bool sigev_none);
void (*timer_wait_running)(struct k_itimer *timr);
}; };
extern const struct k_clock clock_posix_cpu; extern const struct k_clock clock_posix_cpu;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment