Skip to contentSkip to conversation

hrtimer,sched: General optimizations and hrtick enablement

PATCH Applied Thomas Gleixner 12 participants 130 messages Feb 24 – Jul 10 Page 2 of 2

Subsystems (12)
x86 Library code Tracing Scheduler +8 more subsystems
lore
Share & tools
lore
Tested-by on v1 20/48
#63 lore raw

Re: [patch 20/48] clocksource: Update clocksource::freq_khz on registration

In reply to Thomas Gleixner
Quoted 14 lines · Thomas Gleixner

Borislav reported a division by zero in the timekeeping code and random hangs with the new coupled clocksource/clockevent functionality.

It turned out that the TSC clocksource is not always updating the freq_khz field of the clocksource on registration. The coupled mode conversion calculation requires the frequency and as it's not initialized the resulting factor is zero or a random value. As a consequence this causes a division by zero or random boot hangs.

Instead of chasing down all clocksources which fail to update that member, fill it in at registration time where the caller has to supply the frequency anyway. Except for special clocksources like jiffies which never can have coupled mode.

Quoted 8 lines

To make this more robust put a check into the registration function to validate that the caller supplied a frequency if the coupled mode feature bit is set. If not, emit a warning and clear the feature bit.

Fixes cd38bdb8e696 ("timekeeping: Provide infrastructure for coupled clockevents")
Quoted 27 lines
---
 kernel/time/clocksource.c |    7 +++++++
 1 file changed, 7 insertions(+)
kernel/time/clocksource.c +7 -0
@@ -1169,6 +1169,9 @@ void __clocksource_update_freq_scale(str
 
 		clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
 				       NSEC_PER_SEC / scale, sec * scale);
+
+		/* Update cs::freq_khz */
+		cs->freq_khz = div_u64((u64)freq * scale, 1000);
 	}
 
 	/*
@@ -1241,6 +1244,10 @@ int __clocksource_register_scale(struct
 
 	if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX))
 		cs->id = CSID_GENERIC;
+
+	if (WARN_ON_ONCE(!freq && cs->flags & CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT))
+		cs->flags &= ~CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT;
+
 	if (cs->vdso_clock_mode < 0 ||
 	    cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) {
 		pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n",
#64 lore raw

[tip: sched/hrtick] clocksource: Update clocksource::freq_khz on registration

In reply to Thomas Gleixner
The following commit has been merged into the sched/hrtick branch of tip:

The following commit has been merged into the sched/hrtick branch of tip:

Commit-ID:     53007d526e17d29f0e5b81c07eb594a93bc4d29c
Gitweb:        https://git.kernel.org/tip/53007d526e17d29f0e5b81c07eb594a93bc4d29c
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Wed, 04 Mar 2026 19:49:29 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 05 Mar 2026 17:41:06 +01:00

clocksource: Update clocksource::freq_khz on registration

Borislav reported a division by zero in the timekeeping code and random hangs with the new coupled clocksource/clockevent functionality.

It turned out that the TSC clocksource is not always updating the freq_khz field of the clocksource on registration. The coupled mode conversion calculation requires the frequency and as it's not initialized the resulting factor is zero or a random value. As a consequence this causes a division by zero or random boot hangs.

Instead of chasing down all clocksources which fail to update that member, fill it in at registration time where the caller has to supply the frequency anyway. Except for special clocksources like jiffies which never can have coupled mode.

To make this more robust put a check into the registration function to validate that the caller supplied a frequency if the coupled mode feature bit is set. If not, emit a warning and clear the feature bit.

---
 kernel/time/clocksource.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/time/clocksource.c +7 -0
index df71949..3c20544 100644
@@ -1169,6 +1169,9 @@ void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq
 
 		clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
 				       NSEC_PER_SEC / scale, sec * scale);
+
+		/* Update cs::freq_khz */
+		cs->freq_khz = div_u64((u64)freq * scale, 1000);
 	}
 
 	/*
@@ -1241,6 +1244,10 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
 
 	if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX))
 		cs->id = CSID_GENERIC;
+
+	if (WARN_ON_ONCE(!freq && cs->flags & CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT))
+		cs->flags &= ~CLOCK_SOURCE_HAS_COUPLED_CLOCK_EVENT;
+
 	if (cs->vdso_clock_mode < 0 ||
 	    cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) {
 		pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n",
#67 lore raw

[tip: sched/hrtick] timekeeping: Initialize the coupled clocksource conversion completely

In reply to Thomas Gleixner
The following commit has been merged into the sched/hrtick branch of tip:

The following commit has been merged into the sched/hrtick branch of tip:

Commit-ID:     9d5e25b361b7228b422fd32bd1c327fd7fb919b4
Gitweb:        https://git.kernel.org/tip/9d5e25b361b7228b422fd32bd1c327fd7fb919b4
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 03 Mar 2026 22:56:27 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 05 Mar 2026 17:40:46 +01:00

timekeeping: Initialize the coupled clocksource conversion completely

Nathan reported a boot failure after the coupled clocksource/event support was enabled for the TSC deadline timer. It turns out that on the affected test systems the TSC frequency is not refined against HPET, so it is registered with the same frequency as the TSC-early clocksource.

As a consequence the update function which checks for a change of the shift/mult pair of the clocksource fails to compute the conversion limit, which is zero initialized. This check is there to avoid pointless computations on every timekeeping update cycle (tick).

So the actual clockevent conversion function limits the delta expiry to zero, which means the timer is always programmed to expire in the past. This obviously results in a spectacular timer interrupt storm, which goes unnoticed because the per CPU interrupts on x86 are not exposed to the runaway detection mechanism and the NMI watchdog is not yet functional. So the machine simply stops booting.

That did not show up in testing. All test machines refine the TSC frequency so TSC has a differrent shift/mult pair than TSC-early and the conversion limit is properly initialized.

Cure that by setting the conversion limit right at the point where the new clocksource is installed.

---
 kernel/time/timekeeping.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/time/timekeeping.c +7 -0
index b7a0f93..5153218 100644
@@ -404,6 +404,13 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
 		 */
 		clocks_calc_mult_shift(&tk->cs_ns_to_cyc_mult, &tk->cs_ns_to_cyc_shift,
 				       NSEC_PER_MSEC, clock->freq_khz, 3600 * 1000);
+		/*
+		 * Initialize the conversion limit as the previous clocksource
+		 * might have the same shift/mult pair so the quick check in
+		 * tk_update_ns_to_cyc() fails to update it after a clocksource
+		 * change leaving it effectivly zero.
+		 */
+		tk->cs_ns_to_cyc_maxns = div_u64(clock->mask, tk->cs_ns_to_cyc_mult);
 	}
 }
 
#3 lore raw

Re: [tip: sched/hrtick] sched/eevdf: Fix HRTICK duration

In reply to tip-bot2 for Peter Zijlstra

Sorry for very very late reply. I was trying to go through this.

Quoted 64 lines · On 2/28/26 9:07 PM, tip-bot2 for Peter Zijlstra wrote:

The following commit has been merged into the sched/hrtick branch of tip:

Commit-ID:     558c18d3fbb6c5b9c0b42629d7fe34476363ac00
Gitweb:        https://git.kernel.org/tip/558c18d3fbb6c5b9c0b42629d7fe34476363ac00
Author:        Peter Zijlstra <[email protected]>
AuthorDate:    Tue, 24 Feb 2026 17:35:17 +01:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Fri, 27 Feb 2026 16:40:03 +01:00

sched/eevdf: Fix HRTICK duration

The nominal duration for an EEVDF task to run is until its deadline. At which point the deadline is moved ahead and a new task selection is done.

Try and predict the time 'lost' to higher scheduling classes. Since this is an estimate, the timer can be both early or late. In case it is early task_tick_fair() will take the !need_resched() path and restarts the timer.

---
  kernel/sched/fair.c | 41 +++++++++++++++++++++++++++--------------
  1 file changed, 27 insertions(+), 14 deletions(-)
kernel/sched/fair.c +14 -10
index eea99ec..247fecd 100644
@@ -6735,21 +6735,37 @@ static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct
  static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  {
  	struct sched_entity *se = &p->se;
+	unsigned long scale = 1024;
+	unsigned long util = 0;
+	u64 vdelta;
+	u64 delta;
 
  	WARN_ON_ONCE(task_rq(p) != rq);
 
-	if (rq->cfs.h_nr_queued > 1) {
-		u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
-		u64 slice = se->slice;
-		s64 delta = slice - ran;
+	if (rq->cfs.h_nr_queued <= 1)
+		return;
 
-		if (delta < 0) {
-			if (task_current_donor(rq, p))
-				resched_curr(rq);
-			return;
-		}
-		hrtick_start(rq, delta);
+	/*
+	 * Compute time until virtual deadline
+	 */
+	vdelta = se->deadline - se->vruntime;
+	if ((s64)vdelta < 0) {
+		if (task_current_donor(rq, p))
+			resched_curr(rq);
+		return;
  	}

kernel/sched/fair.c static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct · line 6766

Quoted 10 lines
kernel/sched/fair.c +10 -0
+	delta = (se->load.weight * vdelta) / NICE_0_LOAD;
+
+	/*
+	 * Correct for instantaneous load of other classes.
+	 */
+	util += cpu_util_irq(rq);
+	if (util && util < 1024) {
+		scale *= 1024;
+		scale /= (1024 - util);
+	}

Comments/Changelog says other classes.

Then why not consider cpu_util_dl, cpu_util_rq too? Is there a reason why these are not taken into calculations?

Quoted 18 lines
kernel/sched/fair.c +3 -4
+
+	hrtick_start(rq, (scale * delta) / 1024);
  }
 
  /*
@@ -13365,11 +13381,8 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  		entity_tick(cfs_rq, se, queued);
  	}
 
-	if (queued) {
-		if (!need_resched())
-			hrtick_start_fair(rq, curr);
+	if (queued)
  		return;
-	}
 
  	if (static_branch_unlikely(&sched_numa_balancing))
  		task_tick_numa(rq, curr);
maintainer
#4 lore raw

Re: [tip: sched/hrtick] sched/eevdf: Fix HRTICK duration

Quoted 11 lines · Shrikanth Hegde

kernel/sched/fair.c static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct · line 6766

Quoted 8 lines
kernel/sched/fair.c +8 -0
+	/*
+	 * Correct for instantaneous load of other classes.
+	 */
+	util += cpu_util_irq(rq);
+	if (util && util < 1024) {
+		scale *= 1024;
+		scale /= (1024 - util);
+	}

Comments/Changelog says other classes.

Quoted 2 lines

Then why not consider cpu_util_dl, cpu_util_rq too? Is there a reason why these are not taken into calculations?

Damn, forgot to fix that comment.

So yes, it used to correct for those, but then I realized that the hrtick is strictly for current. So running RT/DL tasks means current is different.

The only thing that can actually interrupt current and soak time are interrupts.

Does that make sense?

#5 lore raw

Re: [tip: sched/hrtick] sched/eevdf: Fix HRTICK duration

Quoted 18 lines · Peter Zijlstra
Quoted 11 lines · Shrikanth Hegde

kernel/sched/fair.c static inline void sched_fair_update_stop_tick(struct rq *rq, struct task_struct · line 6766

Quoted 8 lines
kernel/sched/fair.c +8 -0
+	/*
+	 * Correct for instantaneous load of other classes.
+	 */
+	util += cpu_util_irq(rq);
+	if (util && util < 1024) {
+		scale *= 1024;
+		scale /= (1024 - util);
+	}

Comments/Changelog says other classes.

Quoted 2 lines

Then why not consider cpu_util_dl, cpu_util_rq too? Is there a reason why these are not taken into calculations?

Damn, forgot to fix that comment.

Quoted 8 lines

So yes, it used to correct for those, but then I realized that the hrtick is strictly for current. So running RT/DL tasks means current is different.

The only thing that can actually interrupt current and soak time are interrupts.

Does that make sense?

Yes. That helps.

#46 lore raw

RE: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

In reply to Michael Kelley

From: Michael Kelley Sent: Tuesday, March 3, 2026 10:45 AM

Quoted 15 lines

From: Thomas Gleixner <[email protected]> Sent: Tuesday, February 24, 2026 8:37 AM

Quoted 4 lines

Some clockevent devices are coupled to the system clocksource by implementing a less than or equal comparator which compares the programmed absolute expiry time against the underlying time counter.

I've been playing with this in linux-next, and particularly to set up the Hyper-V TSC page clocksource and Hyper-V timer as coupled. Most Hyper-V guests these days are running on hardware that allows using the TSC directly as the clocksource. But even if the Hyper-V TSC page clocksource isn't used, the timer is still the Hyper-V timer, so the coupling isn't active. However, SEV-SNP and TDX CoCo VMs on Hyper-V must use both the Hyper-V TSC page clocksource and the Hyper-V timer, so they would benefit from coupling. It's a nice idea!

[snip]

Quoted 18 lines

kernel/time/clockevents.c static int clockevents_program_min_delta · line 313

Quoted 12 lines
kernel/time/clockevents.c +12 -0
+
+static inline bool clockevent_set_next_coupled(struct clock_event_device *dev, ktime_t expires)
+{
+	u64 cycles;
+
+	if (unlikely(!(dev->features & CLOCK_EVT_FEAT_CLOCKSOURCE_COUPLED)))
+		return false;
+
+	if (unlikely(!ktime_expiry_to_cycles(dev->cs_id, expires, &cycles)))
+		return false;
+
+	if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_COUPLED_INLINE))

Since COUPLED_INLINE is always selected for x64, there's no way to add the Hyper-V clockevent that is coupled but not inline. Adding the machinery to allow a second inline clockevent type may not be worth it, but adding a second coupled but not inline clockevent type on x64 should be supported. Thoughts?

Quoted 5 lines

After fixing the u64 typo, and temporarily not always selecting COUPLED_INLINE in arch/x86/Kconfig, the coupled Hyper-V TSC page clocksource and timer seem to work correctly, though I'm still doing some testing. I'm also working on counting the number of time reads to confirm the expected benefit.

Thomas --

Gentle ping. Any thoughts on this? (And on Peter Zijlstra's "deliciously insane" follow-up?)

Michael

author maintainer
#47 lore raw

RE: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

Quoted 14 lines · Michael Kelley

From: Michael Kelley Sent: Tuesday, March 3, 2026 10:45 AM

Quoted 13 lines

From: Thomas Gleixner <[email protected]> Sent: Tuesday, February 24, 2026 8:37 AM

Quoted 4 lines

Some clockevent devices are coupled to the system clocksource by implementing a less than or equal comparator which compares the programmed absolute expiry time against the underlying time counter.

I've been playing with this in linux-next, and particularly to set up the Hyper-V TSC page clocksource and Hyper-V timer as coupled. Most Hyper-V guests these days are running on hardware that allows using the TSC directly as the clocksource. But even if the Hyper-V TSC page clocksource isn't used, the timer is still the Hyper-V timer, so the coupling isn't active. However, SEV-SNP and TDX CoCo VMs on Hyper-V must use both the Hyper-V TSC page clocksource and the Hyper-V timer, so they would benefit from coupling. It's a nice idea!

Did not think about that. I try to avoid the virt dungeon as much as it goes :)

Quoted 17 lines
Quoted 17 lines

kernel/time/clockevents.c static int clockevents_program_min_delta · line 313

Quoted 11 lines
kernel/time/clockevents.c +11 -0
+static inline bool clockevent_set_next_coupled(struct clock_event_device *dev, ktime_t expires)
+{
+	u64 cycles;
+
+	if (unlikely(!(dev->features & CLOCK_EVT_FEAT_CLOCKSOURCE_COUPLED)))
+		return false;
+
+	if (unlikely(!ktime_expiry_to_cycles(dev->cs_id, expires, &cycles)))
+		return false;
+
+	if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_COUPLED_INLINE))

Since COUPLED_INLINE is always selected for x64, there's no way to add the Hyper-V clockevent that is coupled but not inline. Adding the machinery to allow a second inline clockevent type may not be worth it, but adding a second coupled but not inline clockevent type on x64 should be supported. Thoughts?

Quoted 7 lines
Quoted 4 lines

After fixing the u64 typo, and temporarily not always selecting COUPLED_INLINE in arch/x86/Kconfig, the coupled Hyper-V TSC page clocksource and timer seem to work correctly, though I'm still doing some testing. I'm also working on counting the number of time reads to confirm the expected benefit.

Gentle ping. Any thoughts on this? (And on Peter Zijlstra's "deliciously insane" follow-up?)

Sure, we should be able to support that and I think Peter's suggestion is pretty clever. Did you get it working?

Thanks,

tglx

#48 lore raw

RE: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

From: Thomas Gleixner <[email protected]> Sent: Monday, March 23, 2026 2:37 PM

Quoted 3 lines · Michael Kelley
Quoted 1 line
Quoted 1 line
Quoted 1 line

+static inline bool clockevent_set_next_coupled(struct clock_event_device

*dev, ktime_t expires)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/time/clockevents.c +4 -0
+{
+	u64 cycles;
+
+	if (unlikely(!(dev->features & CLOCK_EVT_FEAT_CLOCKSOURCE_COUPLED)))
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

kernel/time/clockevents.c static int clockevents_program_min_delta · line 313

Quoted 6 lines
kernel/time/clockevents.c +6 -0
+		return false;
+
+	if (unlikely(!ktime_expiry_to_cycles(dev->cs_id, expires, &cycles)))
+		return false;
+
+	if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_COUPLED_INLINE))

Since COUPLED_INLINE is always selected for x64, there's no way to add the

Hyper-V

Quoted 1 line
Quoted 1 line
Quoted 1 line

clockevent that is coupled but not inline. Adding the machinery to allow a

second

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

inline clockevent type may not be worth it, but adding a second coupled but not inline clockevent type on x64 should be supported. Thoughts?

After fixing the u64 typo, and temporarily not always selecting COUPLED_INLINE

in

Quoted 1 line
Quoted 1 line
Quoted 1 line

arch/x86/Kconfig, the coupled Hyper-V TSC page clocksource and timer seem to

work

Quoted 1 line
Quoted 1 line
Quoted 1 line

correctly, though I'm still doing some testing. I'm also working on counting the

number

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

of time reads to confirm the expected benefit.

Gentle ping. Any thoughts on this? (And on Peter Zijlstra's "deliciously

insane"

Quoted 5 lines
Quoted 1 line

follow-up?)

Sure, we should be able to support that and I think Peter's suggestion is pretty clever. Did you get it working?

I got the coupling working with the Hyper-V clocksource and timer in non-inlined form, and observed the expected reduction in time reads. That was straightforward.

But I did not try Peter's suggestion, just to keep things simple. The Hyper-V timer can be loaded with a single wrmsrq just like the TSC deadline timer, but it's a different MSR that's synthetic and always traps to the hypervisor. Given the unavoidable overhead of trapping to the hypervisor, the relative gain of inlining would be small.

Michael

#49 lore raw

RE: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

From: [email protected] <[email protected]> Sent: Monday, March 23, 2026 5:22 PM

Quoted 37 lines

From: Thomas Gleixner <[email protected]> Sent: Monday, March 23, 2026 2:37 PM

Quoted 24 lines · Michael Kelley
Quoted 22 lines
Quoted 21 lines

Review

Quoted 11 lines
(quoted diff) +11 -0
+static inline bool clockevent_set_next_coupled(struct clock_event_device *dev, ktime_t expires)
+{
+	u64 cycles;
+
+	if (unlikely(!(dev->features & CLOCK_EVT_FEAT_CLOCKSOURCE_COUPLED)))
+		return false;
+
+	if (unlikely(!ktime_expiry_to_cycles(dev->cs_id, expires, &cycles)))
+		return false;
+
+	if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_COUPLED_INLINE))

Since COUPLED_INLINE is always selected for x64, there's no way to add the Hyper-V clockevent that is coupled but not inline. Adding the machinery to allow a second inline clockevent type may not be worth it, but adding a second coupled but not inline clockevent type on x64 should be supported. Thoughts?

After fixing the u64 typo, and temporarily not always selecting COUPLED_INLINE in arch/x86/Kconfig, the coupled Hyper-V TSC page clocksource and timer seem to work correctly, though I'm still doing some testing. I'm also working on counting the number of time reads to confirm the expected benefit.

Quoted 6 lines
Quoted 2 lines

Gentle ping. Any thoughts on this? (And on Peter Zijlstra's "deliciously insane" follow-up?)

Sure, we should be able to support that and I think Peter's suggestion is pretty clever. Did you get it working?

I got the coupling working with the Hyper-V clocksource and timer in non-inlined form, and observed the expected reduction in time reads. That was straightforward.

Quoted 8 lines

But I did not try Peter's suggestion, just to keep things simple. The Hyper-V timer can be loaded with a single wrmsrq just like the TSC deadline timer, but it's a different MSR that's synthetic and always traps to the hypervisor. Given the unavoidable overhead of trapping to the hypervisor, the relative gain of inlining would be small.

Michael

Another thought occurred to me. Since the Hyper-V timer "set next event" does wrmsrq just like the TSC deadline timer, add an "msr" field to struct clock_event_device, and require clock events that specify COUPLED to also specify the MSR. Here's a diff of the core changes:

arch/x86/include/asm/clock_inlined.h +1 -1
index b2dee8db2fb9..a3a3c3670ad4 100644
@@ -16,7 +16,7 @@ struct clock_event_device;
 static __always_inline void
 arch_inlined_clockevent_set_next_coupled(u64 cycles, struct clock_event_device *evt)
 {
-	native_wrmsrq(MSR_IA32_TSC_DEADLINE, cycles);
+	native_wrmsrq(evt->msr, cycles);
 }
 
 #endif
arch/x86/kernel/apic/apic.c +1 -0
index 60cab20b7901..42326c7d3f41 100644
@@ -593,6 +593,7 @@ static void setup_APIC_timer(void)
 		levt->name = "lapic-deadline";
 		levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_DUMMY);
 		levt->features |= CLOCK_EVT_FEAT_CLOCKSOURCE_COUPLED;
+		levt->msr = MSR_IA32_TSC_DEADLINE;
 		levt->cs_id = CSID_X86_TSC;
 		levt->set_next_event = lapic_next_deadline;
 		clockevents_config_and_register(levt, tsc_khz * (1000 / TSC_DIVISOR), 0xF, ~0UL);
include/linux/clockchips.h +1 -0
index 92d90220c0d4..deea69580db0 100644
@@ -110,6 +110,7 @@ struct clock_event_device {
 	enum clock_event_state	state_use_accessors;
 	unsigned int		features;
 	enum clocksource_ids	cs_id;
+	u32			msr;
 	unsigned long		retries;
 
 	int			(*set_state_periodic)(struct clock_event_device *);

This approach is not as general as Peter's, but it covers the Hyper-V timer case, and is simpler. The cost is an extra memory reference in arch_inlined_clockevent_set_next_coupled(). arch/x86/Kconfig can continue to select GENERIC_CLOCKEVENTS_COUPLED_INLINE without preventing coupling of the Hyper-V clocksource and timer. I've built and run this with a coupled Hyper-V clocksource and timer, and a basic smoke test works. So something to consider ...

If you like this approach, I'm happy to submit this as a patch. It would be a prerequisite to my patch for the Hyper-V clocksource/timer changes to enable coupling.

Michael

author maintainer
#50 lore raw

RE: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

Quoted 3 lines · Michael Kelley

This approach is not as general as Peter's, but it covers the Hyper-V timer case, and is simpler. The cost is an extra memory reference in arch_inlined_clockevent_set_next_coupled(). arch/x86/Kconfig can continue

Which can be avoided with a runtime_const if the decision between hyperv timer and tscdeadline timer happens before either of them registered the clockevent and does not change later on.

Thanks,

tglx

maintainer
#51 lore raw

Re: [patch 19/48] clockevents: Provide support for clocksource coupled comparators

Quoted 8 lines · Thomas Gleixner
Quoted 3 lines · Michael Kelley

This approach is not as general as Peter's, but it covers the Hyper-V timer case, and is simpler. The cost is an extra memory reference in arch_inlined_clockevent_set_next_coupled(). arch/x86/Kconfig can continue

Which can be avoided with a runtime_const if the decision between hyperv timer and tscdeadline timer happens before either of them registered the clockevent and does not change later on.

If the wrmsr immediate form is faster (that was their purpose) than the current form this will no longer work and we'll have to get more creative, but yes, until that time this should work.

#38 lore raw

Re: [patch 17/48] x86/apic: Avoid the PVOPS indirection for the TSC deadline timer

In reply to Thomas Gleixner
Quoted 22 lines · Thomas Gleixner

XEN PV does not emulate the TSC deadline timer, so the PVOPS indirection for writing the deadline MSR can be avoided completely.

Use native_wrmsrq() instead.

---
 arch/x86/kernel/apic/apic.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kernel/apic/apic.c +1 -1
@@ -426,7 +426,7 @@ static int lapic_next_deadline(unsigned
 	 */
 	u64 tsc = rdtsc();
 
-	wrmsrq(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
+	native_wrmsrq(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
 	return 0;
 }
 

arch/x86/kernel/apic/apic.c static int lapic_timer_shutdown(struct c · line 453

Quoted 5 lines
arch/x86/kernel/apic/apic.c +1 -1
@@ -450,7 +450,7 @@ static int lapic_timer_shutdown(struct c
 	 * the timer _and_ zero the counter registers:
 	 */
 	if (v & APIC_LVT_TIMER_TSCDEADLINE)
-		wrmsrq(MSR_IA32_TSC_DEADLINE, 0);
+		native_wrmsrq(MSR_IA32_TSC_DEADLINE, 0);

Hi Thomas,

unfortunately for me and anyone who wants to trace MSR_IA32_TSC_DEADLINE:

- wrmsrq() did trigger the 'write_msr' trace event.
- native_wrmsrq() does not trigger it.

I am not sure what is the best way to add the tracepoint back, but before I even try, I wanted to ask if you would be OK with that.

Thank you!

#39 lore raw

Re: [patch 17/48] x86/apic: Avoid the PVOPS indirection for the TSC deadline timer

Quoted 31 lines · Artem Bityutskiy
Quoted 22 lines · Thomas Gleixner

XEN PV does not emulate the TSC deadline timer, so the PVOPS indirection for writing the deadline MSR can be avoided completely.

Use native_wrmsrq() instead.

---
  arch/x86/kernel/apic/apic.c |   13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
arch/x86/kernel/apic/apic.c +1 -1
@@ -426,7 +426,7 @@ static int lapic_next_deadline(unsigned
  	 */
  	u64 tsc = rdtsc();
 
-	wrmsrq(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
+	native_wrmsrq(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
  	return 0;
  }
 

arch/x86/kernel/apic/apic.c static int lapic_timer_shutdown(struct c · line 453

Quoted 5 lines
arch/x86/kernel/apic/apic.c +1 -1
@@ -450,7 +450,7 @@ static int lapic_timer_shutdown(struct c
  	 * the timer _and_ zero the counter registers:
  	 */
  	if (v & APIC_LVT_TIMER_TSCDEADLINE)
-		wrmsrq(MSR_IA32_TSC_DEADLINE, 0);
+		native_wrmsrq(MSR_IA32_TSC_DEADLINE, 0);

Hi Thomas,

Quoted 8 lines

unfortunately for me and anyone who wants to trace MSR_IA32_TSC_DEADLINE:

- wrmsrq() did trigger the 'write_msr' trace event.
- native_wrmsrq() does not trigger it.

I am not sure what is the best way to add the tracepoint back, but before I even try, I wanted to ask if you would be OK with that.

Please have a look at my MSR series [1], [2], [3]. With those everything should be fine (trace entries being written, no pvops indirection due to direct wrmsr instruction patched into the call site).

Juergen

[1]: https://lore.kernel.org/lkml/[email protected]/T
[2]: https://lore.kernel.org/lkml/[email protected]/T
[3]: https://lore.kernel.org/lkml/[email protected]/T