Re: [PATCH] clockevents: Prevent timer interrupt starvation

Peter Zijlstra
2026-04-03T12:17:27Z

On Thu, Apr 02, 2026 at 07:07:49PM +0200, Thomas Gleixner wrote:

> --- a/kernel/time/clockevents.c
> +++ b/kernel/time/clockevents.c
> @@ -172,6 +172,7 @@ void clockevents_shutdown(struct clock_e
> {
> clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
> dev->next_event = KTIME_MAX;
> + dev->next_event_forced = 0;
> }
>
> /**
> @@ -224,13 +225,7 @@ static int clockevents_increase_min_delt
> return 0;
> }
>
> -/**
> - * clockevents_program_min_delta - Set clock event device to the minimum delay.
> - * @dev: device to program
> - *
> - * Returns 0 on success, -ETIME when the retry loop failed.
> - */
> -static int clockevents_program_min_delta(struct clock_event_device *dev)
> +static int __clockevents_program_min_delta(struct clock_event_device *dev)
> {
> unsigned long long clc;
> int64_t delta;
> @@ -263,13 +258,7 @@ static int clockevents_program_min_delta
>
> #else /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
>
> -/**
> - * clockevents_program_min_delta - Set clock event device to the minimum delay.
> - * @dev: device to program
> - *
> - * Returns 0 on success, -ETIME when the retry loop failed.
> - */
> -static int clockevents_program_min_delta(struct clock_event_device *dev)
> +static int __clockevents_program_min_delta(struct clock_event_device *dev)
> {
> unsigned long long clc;
> int64_t delta = 0;
> @@ -293,6 +282,21 @@ static int clockevents_program_min_delta
> #endif /* CONFIG_GENERIC_CLOCKEVENTS_MIN_ADJUST */
>
> /**
> + * clockevents_program_min_delta - Set clock event device to the minimum delay.
> + * @dev: device to program
> + *
> + * Returns 0 on success, -ETIME when the retry loop failed.
> + */
> +static int clockevents_program_min_delta(struct clock_event_device *dev)
> +{
> + if (dev->next_event_forced)
> + return 0;
> +
> + dev->next_event_forced = 1;
> + return __clockevents_program_min_delta(dev);
> +}
> +
> +/**
> * clockevents_program_event - Reprogram the clock event device.
> * @dev: device to program
> * @expires: absolute expiry time (monotonic clock)
> @@ -324,6 +328,11 @@ int clockevents_program_event(struct clo
> return dev->set_next_ktime(expires, dev);
>
> delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
> +
> + /* Don't reprogram when a forced event is pending */
> + if (dev->next_event_forced && delta <= (int64_t)dev->min_delta_ns)
> + return 0;
> +
> if (delta <= 0)
> return force ? clockevents_program_min_delta(dev) : -ETIME;
>

This last hunk seems duplicate of the clockevents_program_min_delta()
change, that also will bail when next_event_forced is set.

Also, I note, thing (except shutdown) will ever clear next_event_forced;
and I'm thinking that any interrupt or normal reprogram should be doing
that.

Now, there doesn't seem to be a generic intercept for
dev->event_handler, so clearing on interrupt is hard, but would
something like the completely untested below work?

---
diff --git a/include/linux/clockchips.h b/include/linux/clockchips.h
index b0df28ddd394..a79f8fa10104 100644
--- a/include/linux/clockchips.h
+++ b/include/linux/clockchips.h
@@ -108,6 +108,7 @@ struct clock_event_device {
u32 shift;
enum clock_event_state state_use_accessors;
unsigned int features;
+ unsigned int next_event_forced;
unsigned long retries;

int (*set_state_periodic)(struct clock_event_device *);
diff --git a/kernel/time/clockevents.c b/kernel/time/clockevents.c
index eaae1ce9f060..8f6621361e46 100644
--- a/kernel/time/clockevents.c
+++ b/kernel/time/clockevents.c
@@ -172,6 +172,7 @@ void clockevents_shutdown(struct clock_event_device *dev)
{
clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
dev->next_event = KTIME_MAX;
+ dev->next_event_forced = 0;
}

/**
@@ -324,16 +325,31 @@ int clockevents_program_event(struct clock_event_device *dev, ktime_t expires,
return dev->set_next_ktime(expires, dev);

delta = ktime_to_ns(ktime_sub(expires, ktime_get()));
- if (delta <= 0)
- return force ? clockevents_program_min_delta(dev) : -ETIME;
+ if (delta <= 0) {
+ rc = -ETIME;
+ goto error;
+ }

delta = min(delta, (int64_t) dev->max_delta_ns);
delta = max(delta, (int64_t) dev->min_delta_ns);

clc = ((unsigned long long) delta * dev->mult) >> dev->shift;
rc = dev->set_next_event((unsigned long) clc, dev);
+ if (rc)
+ goto error;

- return (rc && force) ? clockevents_program_min_delta(dev) : rc;
+ dev->next_event_forced = 0;
+ return 0;
+
+error:
+ if (force) {
+ if (dev->next_event_forced)
+ return 0;
+
+ dev->next_event_forced = 1;
+ return clockevents_program_min_delta(dev);
+ }
+ return rc;
}

/*