HomeFreeBSD

eventtimer: Fix several races in the timer reload code

Description

eventtimer: Fix several races in the timer reload code

In handleevents(), lock the timer state before fetching the time for the
next event. A concurrent callout_cc_add() call might be changing the
next event time, and the race can cause handleevents() to program an
out-of-date time, causing the callout to run later (by an unbounded
period, up to the idle hardclock period of 1s) than requested.

In cpu_idleclock(), call getnextcpuevent() with the timer state mutex
held, for similar reasons. In particular, cpu_idleclock() runs with
interrupts enabled, so an untimely timer interrupt can result in a stale
next event time being programmed. Further, an interrupt can cause
cpu_idleclock() to use a stale value for "now".

In cpu_activeclock(), disable interrupts before loading "now", so as to
avoid going backwards in time when calling handleevents(). It's ok to
leave interrupts enabled when checking "state->idle", since the race at
worst will cause handleevents() to be called unnecessarily. But use an
atomic load to indicate that the test is racy.

PR: 264867
Reviewed by: mav, jhb, kib
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D35735

Details

Provenance
markjAuthored on Jun 30 2022, 6:27 PM
Reviewer
mav
Differential Revision
D35735: eventtimer: Fix several races in the timer reload code
Parents
rGebb3cb61958d: eventtimer: Pass a pcpu state pointer to getnext(cpu)event()
Branches
Unknown
Tags
Unknown