Skip to content

Commit 19d9f42

Browse files
committed
hrtimer: Avoid locking in hrtimer_cancel() if timer not active
We can do a lockless check for hrtimer_active before actually taking the lock in hrtimer[_try_to]_cancel. This is useful for hotpath users like nanosleep as they avoid the lock dance when the timer has expired. This is safe because active is true when the timer is enqueued or the callback is running. Taking the hrtimer base lock does not protect against concurrent hrtimer_start calls, the callsite has to do the proper serialization itself. Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Cc: Preeti U Murthy <[email protected]> Cc: Viresh Kumar <[email protected]> Cc: Marcelo Tosatti <[email protected]> Cc: Frederic Weisbecker <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 61699e1 commit 19d9f42

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

kernel/time/hrtimer.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,15 @@ int hrtimer_try_to_cancel(struct hrtimer *timer)
991991
unsigned long flags;
992992
int ret = -1;
993993

994+
/*
995+
* Check lockless first. If the timer is not active (neither
996+
* enqueued nor running the callback, nothing to do here. The
997+
* base lock does not serialize against a concurrent enqueue,
998+
* so we can avoid taking it.
999+
*/
1000+
if (!hrtimer_active(timer))
1001+
return 0;
1002+
9941003
base = lock_hrtimer_base(timer, &flags);
9951004

9961005
if (!hrtimer_callback_running(timer))

0 commit comments

Comments
 (0)