Skip to content

Commit ec8f954

Browse files
committed
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: Anna-Maria Gleixenr <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Frederic Weisbecker <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 5d99b32 commit ec8f954

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

kernel/time/alarmtimer.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ static int alarm_timer_try_to_cancel(struct k_itimer *timr)
605605
return alarm_try_to_cancel(&timr->it.alarm.alarmtimer);
606606
}
607607

608+
/**
609+
* alarm_timer_wait_running - Posix timer callback to wait for a timer
610+
* @timr: Pointer to the posixtimer data struct
611+
*
612+
* Called from the core code when timer cancel detected that the callback
613+
* is running. @timr is unlocked and rcu read lock is held to prevent it
614+
* from being freed.
615+
*/
616+
static void alarm_timer_wait_running(struct k_itimer *timr)
617+
{
618+
hrtimer_cancel_wait_running(&timr->it.alarm.alarmtimer.timer);
619+
}
620+
608621
/**
609622
* alarm_timer_arm - Posix timer callback to arm a timer
610623
* @timr: Pointer to the posixtimer data struct
@@ -834,6 +847,7 @@ const struct k_clock alarm_clock = {
834847
.timer_forward = alarm_timer_forward,
835848
.timer_remaining = alarm_timer_remaining,
836849
.timer_try_to_cancel = alarm_timer_try_to_cancel,
850+
.timer_wait_running = alarm_timer_wait_running,
837851
.nsleep = alarm_timer_nsleep,
838852
};
839853
#endif /* CONFIG_POSIX_TIMERS */

kernel/time/posix-timers.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,13 +805,25 @@ static int common_hrtimer_try_to_cancel(struct k_itimer *timr)
805805
return hrtimer_try_to_cancel(&timr->it.real.timer);
806806
}
807807

808+
static void common_timer_wait_running(struct k_itimer *timer)
809+
{
810+
hrtimer_cancel_wait_running(&timer->it.real.timer);
811+
}
812+
808813
static struct k_itimer *timer_wait_running(struct k_itimer *timer,
809814
unsigned long *flags)
810815
{
816+
const struct k_clock *kc = READ_ONCE(timer->kclock);
811817
timer_t timer_id = READ_ONCE(timer->it_id);
812818

819+
/* Prevent kfree(timer) after dropping the lock */
820+
rcu_read_lock();
813821
unlock_timer(timer, *flags);
814-
cpu_relax();
822+
823+
if (!WARN_ON_ONCE(!kc->timer_wait_running))
824+
kc->timer_wait_running(timer);
825+
826+
rcu_read_unlock();
815827
/* Relock the timer. It might be not longer hashed. */
816828
return lock_timer(timer_id, flags);
817829
}
@@ -1255,6 +1267,7 @@ static const struct k_clock clock_realtime = {
12551267
.timer_forward = common_hrtimer_forward,
12561268
.timer_remaining = common_hrtimer_remaining,
12571269
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
1270+
.timer_wait_running = common_timer_wait_running,
12581271
.timer_arm = common_hrtimer_arm,
12591272
};
12601273

@@ -1270,6 +1283,7 @@ static const struct k_clock clock_monotonic = {
12701283
.timer_forward = common_hrtimer_forward,
12711284
.timer_remaining = common_hrtimer_remaining,
12721285
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
1286+
.timer_wait_running = common_timer_wait_running,
12731287
.timer_arm = common_hrtimer_arm,
12741288
};
12751289

@@ -1300,6 +1314,7 @@ static const struct k_clock clock_tai = {
13001314
.timer_forward = common_hrtimer_forward,
13011315
.timer_remaining = common_hrtimer_remaining,
13021316
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
1317+
.timer_wait_running = common_timer_wait_running,
13031318
.timer_arm = common_hrtimer_arm,
13041319
};
13051320

@@ -1315,6 +1330,7 @@ static const struct k_clock clock_boottime = {
13151330
.timer_forward = common_hrtimer_forward,
13161331
.timer_remaining = common_hrtimer_remaining,
13171332
.timer_try_to_cancel = common_hrtimer_try_to_cancel,
1333+
.timer_wait_running = common_timer_wait_running,
13181334
.timer_arm = common_hrtimer_arm,
13191335
};
13201336

kernel/time/posix-timers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct k_clock {
2424
int (*timer_try_to_cancel)(struct k_itimer *timr);
2525
void (*timer_arm)(struct k_itimer *timr, ktime_t expires,
2626
bool absolute, bool sigev_none);
27+
void (*timer_wait_running)(struct k_itimer *timr);
2728
};
2829

2930
extern const struct k_clock clock_posix_cpu;

0 commit comments

Comments
 (0)