Skip to content

[arch][x86][lapic] don't stop the timer on a 0 interval - #546

Merged
travisg merged 1 commit into
littlekernel:masterfrom
zhangxp1998:lapic-zero-interval
Oct 27, 2026
Merged

travisg merged 1 commit into
littlekernel:masterfrom
zhangxp1998:lapic-zero-interval

Conversation

@zhangxp1998

Copy link
Copy Markdown
Contributor

timer_cancel() passes a 0 interval to platform_set_oneshot_timer() when the timer it promotes to the head of the queue is already due. In LAPIC one-shot count-down mode (no TSC-deadline, e.g. QEMU TCG), a 0 initial count stops the timer: the new head never fires, and every later timer on that CPU stalls behind it.

  • Clamp the LAPIC count to 1 tick so a 0 interval fires as soon as possible. Only lapic_cancel_timer() writes 0.
  • Document in platform/timer.h that a 0 interval must not disarm the timer; platform_stop_timer() is the only way to do that.
  • Add test_timer_cancel_past_due_newhead. It sets two 1ms timers halfway through a tick, waits until their deadline has passed but before the hardware fires, then cancels the head. It pins itself to CPU 0, which relies on the synchronous migration from [kernel][thread] migrate running thread immediately in thread_set_pinned_cpu #544.

Tested with scripts/run-qemu-boot-tests.py: without the fix, x86-64 hangs in the new test; with it, x86-64, x86, arm64, arm and arm-m3 pass.

timer_cancel() passes a 0 interval when the timer it promotes to the head
of the queue is already due. In one-shot count-down mode a 0 initial count
stops the LAPIC timer, so that timer never fires and every later timer on
the cpu stalls behind it. Clamp the count to 1 tick so a 0 interval fires
as soon as possible; only lapic_cancel_timer() writes 0. Document that
platform_set_oneshot_timer() must not treat 0 as a stop, and add a test
that hits the race.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@travisg

travisg commented Oct 27, 2026

Copy link
Copy Markdown
Member

LGTM.

@travisg
travisg merged commit 9d94165 into littlekernel:master Oct 27, 2026
118 checks passed
@zhangxp1998
zhangxp1998 deleted the lapic-zero-interval branch October 27, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants