* [PATCH 1/2] i8253: Disable PIT timer 0 when not in use
@ 2024-08-02 13:55 David Woodhouse
2024-08-02 13:55 ` [PATCH 2/2] i8253: Fix stop sequence for timer 0 David Woodhouse
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: David Woodhouse @ 2024-08-02 13:55 UTC (permalink / raw)
To: tglx, x86
Cc: lirongqing, seanjc, kys, haiyangz, wei.liu, decui, mingo, bp,
dave.hansen, linux-hyperv, linux-kernel, Daniel Lezcano,
Michael Kelley
From: David Woodhouse <dwmw@amazon.co.uk>
Leaving the PIT interrupt running can cause noticeable steal time for
virtual guests. The VMM generally has a timer which toggles the IRQ input
to the PIC and I/O APIC, which takes CPU time away from the guest. Even
on real hardware, running the counter may use power needlessly (albeit
not much).
Make sure it's turned off if it isn't going to be used.
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
arch/x86/kernel/i8253.c | 11 +++++++++--
drivers/clocksource/i8253.c | 13 +++++++++----
include/linux/i8253.h | 1 +
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kernel/i8253.c b/arch/x86/kernel/i8253.c
index 2b7999a1a50a..80e262bb627f 100644
--- a/arch/x86/kernel/i8253.c
+++ b/arch/x86/kernel/i8253.c
@@ -8,6 +8,7 @@
#include <linux/timex.h>
#include <linux/i8253.h>
+#include <asm/hypervisor.h>
#include <asm/apic.h>
#include <asm/hpet.h>
#include <asm/time.h>
@@ -39,9 +40,15 @@ static bool __init use_pit(void)
bool __init pit_timer_init(void)
{
- if (!use_pit())
+ if (!use_pit()) {
+ /*
+ * Don't just ignore the PIT. Ensure it's stopped, because
+ * VMMs otherwise steal CPU time just to pointlessly waggle
+ * the (masked) IRQ.
+ */
+ clockevent_i8253_disable();
return false;
-
+ }
clockevent_i8253_init(true);
global_clock_event = &i8253_clockevent;
return true;
diff --git a/drivers/clocksource/i8253.c b/drivers/clocksource/i8253.c
index d4350bb10b83..cb215e6f2e83 100644
--- a/drivers/clocksource/i8253.c
+++ b/drivers/clocksource/i8253.c
@@ -108,11 +108,8 @@ int __init clocksource_i8253_init(void)
#endif
#ifdef CONFIG_CLKEVT_I8253
-static int pit_shutdown(struct clock_event_device *evt)
+void clockevent_i8253_disable(void)
{
- if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
- return 0;
-
raw_spin_lock(&i8253_lock);
outb_p(0x30, PIT_MODE);
@@ -123,6 +120,14 @@ static int pit_shutdown(struct clock_event_device *evt)
}
raw_spin_unlock(&i8253_lock);
+}
+
+static int pit_shutdown(struct clock_event_device *evt)
+{
+ if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
+ return 0;
+
+ clockevent_i8253_disable();
return 0;
}
diff --git a/include/linux/i8253.h b/include/linux/i8253.h
index 8336b2f6f834..bf169cfef7f1 100644
--- a/include/linux/i8253.h
+++ b/include/linux/i8253.h
@@ -24,6 +24,7 @@ extern raw_spinlock_t i8253_lock;
extern bool i8253_clear_counter_on_shutdown;
extern struct clock_event_device i8253_clockevent;
extern void clockevent_i8253_init(bool oneshot);
+extern void clockevent_i8253_disable(void);
extern void setup_pit_timer(void);
--
2.44.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2] i8253: Fix stop sequence for timer 0
2024-08-02 13:55 [PATCH 1/2] i8253: Disable PIT timer 0 when not in use David Woodhouse
@ 2024-08-02 13:55 ` David Woodhouse
2024-08-02 16:35 ` [tip: x86/timers] clockevents/drivers/i8253: " tip-bot2 for David Woodhouse
2024-08-02 15:44 ` [PATCH 1/2] i8253: Disable PIT timer 0 when not in use Michael Kelley
2024-08-02 16:35 ` [tip: x86/timers] x86/i8253: " tip-bot2 for David Woodhouse
2 siblings, 1 reply; 6+ messages in thread
From: David Woodhouse @ 2024-08-02 13:55 UTC (permalink / raw)
To: tglx, x86
Cc: lirongqing, seanjc, kys, haiyangz, wei.liu, decui, mingo, bp,
dave.hansen, linux-hyperv, linux-kernel, Daniel Lezcano,
Michael Kelley
From: David Woodhouse <dwmw@amazon.co.uk>
According to the data sheet, writing the MODE register should stop the
counter (and thus the interrupts). This appears to work on real hardware,
at least modern Intel and AMD systems. It should also work on Hyper-V.
However, on some buggy virtual machines the mode change doesn't have any
effect until the counter is subsequently loaded (or perhaps when the IRQ
next fires).
So, set MODE 0 and then load the counter, to ensure that those buggy VMs
do the right thing and the interrupts stop. And then write MODE 0 *again*
to stop the counter on compliant implementations too.
Apparently, Hyper-V keeps firing the IRQ *repeatedly* even in mode zero
when it should only happen once, but the second MODE write stops that too.
Userspace test program (mostly written by tglx):
=====
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/io.h>
static __always_inline void __out##bwl(type value, uint16_t port) \
{ \
asm volatile("out" #bwl " %" #bw "0, %w1" \
: : "a"(value), "Nd"(port)); \
} \
\
static __always_inline type __in##bwl(uint16_t port) \
{ \
type value; \
asm volatile("in" #bwl " %w1, %" #bw "0" \
: "=a"(value) : "Nd"(port)); \
return value; \
}
BUILDIO(b, b, uint8_t)
#define inb __inb
#define outb __outb
#define PIT_MODE 0x43
#define PIT_CH0 0x40
#define PIT_CH2 0x42
static int is8254;
static void dump_pit(void)
{
if (is8254) {
// Latch and output counter and status
outb(0xC2, PIT_MODE);
printf("%02x %02x %02x\n", inb(PIT_CH0), inb(PIT_CH0), inb(PIT_CH0));
} else {
// Latch and output counter
outb(0x0, PIT_MODE);
printf("%02x %02x\n", inb(PIT_CH0), inb(PIT_CH0));
}
}
int main(int argc, char* argv[])
{
int nr_counts = 2;
if (argc > 1)
nr_counts = atoi(argv[1]);
if (argc > 2)
is8254 = 1;
if (ioperm(0x40, 4, 1) != 0)
return 1;
dump_pit();
printf("Set oneshot\n");
outb(0x38, PIT_MODE);
outb(0x00, PIT_CH0);
outb(0x0F, PIT_CH0);
dump_pit();
usleep(1000);
dump_pit();
printf("Set periodic\n");
outb(0x34, PIT_MODE);
outb(0x00, PIT_CH0);
outb(0x0F, PIT_CH0);
dump_pit();
usleep(1000);
dump_pit();
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
printf("Set stop (%d counter writes)\n", nr_counts);
outb(0x30, PIT_MODE);
while (nr_counts--)
outb(0xFF, PIT_CH0);
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
printf("Set MODE 0\n");
outb(0x30, PIT_MODE);
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
return 0;
}
=====
Suggested-by: Sean Christopherson <seanjc@google.com>
Co-authored-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
arch/x86/kernel/cpu/mshyperv.c | 11 -----------
drivers/clocksource/i8253.c | 36 +++++++++++++++++++++++-----------
include/linux/i8253.h | 1 -
3 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
index e0fd57a8ba84..3d4237f27569 100644
--- a/arch/x86/kernel/cpu/mshyperv.c
+++ b/arch/x86/kernel/cpu/mshyperv.c
@@ -16,7 +16,6 @@
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kexec.h>
-#include <linux/i8253.h>
#include <linux/random.h>
#include <asm/processor.h>
#include <asm/hypervisor.h>
@@ -522,16 +521,6 @@ static void __init ms_hyperv_init_platform(void)
if (efi_enabled(EFI_BOOT))
x86_platform.get_nmi_reason = hv_get_nmi_reason;
- /*
- * Hyper-V VMs have a PIT emulation quirk such that zeroing the
- * counter register during PIT shutdown restarts the PIT. So it
- * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
- * to false tells pit_shutdown() not to zero the counter so that
- * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
- * and setting this value has no effect.
- */
- i8253_clear_counter_on_shutdown = false;
-
#if IS_ENABLED(CONFIG_HYPERV)
if ((hv_get_isolation_type() == HV_ISOLATION_TYPE_VBS) ||
ms_hyperv.paravisor_present)
diff --git a/drivers/clocksource/i8253.c b/drivers/clocksource/i8253.c
index cb215e6f2e83..39f7c2d736d1 100644
--- a/drivers/clocksource/i8253.c
+++ b/drivers/clocksource/i8253.c
@@ -20,13 +20,6 @@
DEFINE_RAW_SPINLOCK(i8253_lock);
EXPORT_SYMBOL(i8253_lock);
-/*
- * Handle PIT quirk in pit_shutdown() where zeroing the counter register
- * restarts the PIT, negating the shutdown. On platforms with the quirk,
- * platform specific code can set this to false.
- */
-bool i8253_clear_counter_on_shutdown __ro_after_init = true;
-
#ifdef CONFIG_CLKSRC_I8253
/*
* Since the PIT overflows every tick, its not very useful
@@ -112,12 +105,33 @@ void clockevent_i8253_disable(void)
{
raw_spin_lock(&i8253_lock);
+ /*
+ * Writing the MODE register should stop the counter, according to
+ * the datasheet. This appears to work on real hardware (well, on
+ * modern Intel and AMD boxes; I didn't dig the Pegasos out of the
+ * shed).
+ *
+ * However, some virtual implementations differ, and the MODE change
+ * doesn't have any effect until either the counter is written (KVM
+ * in-kernel PIT) or the next interrupt (QEMU). And in those cases,
+ * it may not stop the *count*, only the interrupts. Although in
+ * the virt case, that probably doesn't matter, as the value of the
+ * counter will only be calculated on demand if the guest reads it;
+ * it's the interrupts which cause steal time.
+ *
+ * Hyper-V apparently has a bug where even in mode 0, the IRQ keeps
+ * firing repeatedly if the counter is running. But it *does* do the
+ * right thing when the MODE register is written.
+ *
+ * So: write the MODE and then load the counter, which ensures that
+ * the IRQ is stopped on those buggy virt implementations. And then
+ * write the MODE again, which is the right way to stop it.
+ */
outb_p(0x30, PIT_MODE);
+ outb_p(0, PIT_CH0);
+ outb_p(0, PIT_CH0);
- if (i8253_clear_counter_on_shutdown) {
- outb_p(0, PIT_CH0);
- outb_p(0, PIT_CH0);
- }
+ outb_p(0x30, PIT_MODE);
raw_spin_unlock(&i8253_lock);
}
diff --git a/include/linux/i8253.h b/include/linux/i8253.h
index bf169cfef7f1..56c280eb2d4f 100644
--- a/include/linux/i8253.h
+++ b/include/linux/i8253.h
@@ -21,7 +21,6 @@
#define PIT_LATCH ((PIT_TICK_RATE + HZ/2) / HZ)
extern raw_spinlock_t i8253_lock;
-extern bool i8253_clear_counter_on_shutdown;
extern struct clock_event_device i8253_clockevent;
extern void clockevent_i8253_init(bool oneshot);
extern void clockevent_i8253_disable(void);
--
2.44.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [PATCH 1/2] i8253: Disable PIT timer 0 when not in use
2024-08-02 13:55 [PATCH 1/2] i8253: Disable PIT timer 0 when not in use David Woodhouse
2024-08-02 13:55 ` [PATCH 2/2] i8253: Fix stop sequence for timer 0 David Woodhouse
@ 2024-08-02 15:44 ` Michael Kelley
2024-08-02 16:01 ` David Woodhouse
2024-08-02 16:35 ` [tip: x86/timers] x86/i8253: " tip-bot2 for David Woodhouse
2 siblings, 1 reply; 6+ messages in thread
From: Michael Kelley @ 2024-08-02 15:44 UTC (permalink / raw)
To: David Woodhouse, tglx, x86
Cc: lirongqing, seanjc, kys, haiyangz, wei.liu, decui, mingo, bp,
dave.hansen, linux-hyperv, linux-kernel, Daniel Lezcano
From: David Woodhouse <dwmw2@infradead.org> Sent: Friday, August 2, 2024 6:56 AM
>
> Leaving the PIT interrupt running can cause noticeable steal time for
> virtual guests. The VMM generally has a timer which toggles the IRQ input
> to the PIC and I/O APIC, which takes CPU time away from the guest. Even
> on real hardware, running the counter may use power needlessly (albeit
> not much).
>
> Make sure it's turned off if it isn't going to be used.
>
> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
> ---
> arch/x86/kernel/i8253.c | 11 +++++++++--
> drivers/clocksource/i8253.c | 13 +++++++++----
> include/linux/i8253.h | 1 +
> 3 files changed, 19 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/kernel/i8253.c b/arch/x86/kernel/i8253.c
> index 2b7999a1a50a..80e262bb627f 100644
> --- a/arch/x86/kernel/i8253.c
> +++ b/arch/x86/kernel/i8253.c
> @@ -8,6 +8,7 @@
> #include <linux/timex.h>
> #include <linux/i8253.h>
>
> +#include <asm/hypervisor.h>
> #include <asm/apic.h>
> #include <asm/hpet.h>
> #include <asm/time.h>
> @@ -39,9 +40,15 @@ static bool __init use_pit(void)
>
> bool __init pit_timer_init(void)
> {
> - if (!use_pit())
> + if (!use_pit()) {
> + /*
> + * Don't just ignore the PIT. Ensure it's stopped, because
> + * VMMs otherwise steal CPU time just to pointlessly waggle
> + * the (masked) IRQ.
> + */
> + clockevent_i8253_disable();
> return false;
> -
> + }
> clockevent_i8253_init(true);
> global_clock_event = &i8253_clockevent;
> return true;
> diff --git a/drivers/clocksource/i8253.c b/drivers/clocksource/i8253.c
> index d4350bb10b83..cb215e6f2e83 100644
> --- a/drivers/clocksource/i8253.c
> +++ b/drivers/clocksource/i8253.c
> @@ -108,11 +108,8 @@ int __init clocksource_i8253_init(void)
> #endif
>
> #ifdef CONFIG_CLKEVT_I8253
> -static int pit_shutdown(struct clock_event_device *evt)
> +void clockevent_i8253_disable(void)
> {
> - if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
> - return 0;
> -
> raw_spin_lock(&i8253_lock);
>
> outb_p(0x30, PIT_MODE);
> @@ -123,6 +120,14 @@ static int pit_shutdown(struct clock_event_device *evt)
> }
>
> raw_spin_unlock(&i8253_lock);
> +}
> +
> +static int pit_shutdown(struct clock_event_device *evt)
> +{
> + if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
> + return 0;
> +
> + clockevent_i8253_disable();
> return 0;
> }
>
> diff --git a/include/linux/i8253.h b/include/linux/i8253.h
> index 8336b2f6f834..bf169cfef7f1 100644
> --- a/include/linux/i8253.h
> +++ b/include/linux/i8253.h
> @@ -24,6 +24,7 @@ extern raw_spinlock_t i8253_lock;
> extern bool i8253_clear_counter_on_shutdown;
> extern struct clock_event_device i8253_clockevent;
> extern void clockevent_i8253_init(bool oneshot);
> +extern void clockevent_i8253_disable(void);
>
> extern void setup_pit_timer(void);
>
> --
> 2.44.0
Did a basic smoke test of this two-patch series on a Hyper-V Gen 1
VM and on a Gen 2 VM. All looks good and behaves as expected.
On the Gen 1 VM, the PIT is used briefly at boot (takes ~35 interrupts)
before the Hyper-V synthetic timer takes over and the PIT is shutdown.
As expected, no further interrupts are received from the PIT.
On a Gen 2 VM, apic_needs_pit() returns true because
X86_FEATURE_ARAT isn't present. The PIT doesn't exist in a
Gen 2 VM, but the code paths handle this situation with no
problems, just as before the patch series.
For the two-patch series on Hyper-V,
Tested-by: Michael Kelley <mhkelley@outlook.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] i8253: Disable PIT timer 0 when not in use
2024-08-02 15:44 ` [PATCH 1/2] i8253: Disable PIT timer 0 when not in use Michael Kelley
@ 2024-08-02 16:01 ` David Woodhouse
0 siblings, 0 replies; 6+ messages in thread
From: David Woodhouse @ 2024-08-02 16:01 UTC (permalink / raw)
To: Michael Kelley, tglx, x86
Cc: lirongqing, seanjc, kys, haiyangz, wei.liu, decui, mingo, bp,
dave.hansen, linux-hyperv, linux-kernel, Daniel Lezcano
[-- Attachment #1: Type: text/plain, Size: 751 bytes --]
On Fri, 2024-08-02 at 15:44 +0000, Michael Kelley wrote:
> Did a basic smoke test of this two-patch series on a Hyper-V Gen 1
> VM and on a Gen 2 VM. All looks good and behaves as expected.
>
> On the Gen 1 VM, the PIT is used briefly at boot (takes ~35 interrupts)
> before the Hyper-V synthetic timer takes over and the PIT is shutdown.
> As expected, no further interrupts are received from the PIT.
>
> On a Gen 2 VM, apic_needs_pit() returns true because
> X86_FEATURE_ARAT isn't present. The PIT doesn't exist in a
> Gen 2 VM, but the code paths handle this situation with no
> problems, just as before the patch series.
>
> For the two-patch series on Hyper-V,
> Tested-by: Michael Kelley <mhkelley@outlook.com>
Thank you!
[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* [tip: x86/timers] x86/i8253: Disable PIT timer 0 when not in use
2024-08-02 13:55 [PATCH 1/2] i8253: Disable PIT timer 0 when not in use David Woodhouse
2024-08-02 13:55 ` [PATCH 2/2] i8253: Fix stop sequence for timer 0 David Woodhouse
2024-08-02 15:44 ` [PATCH 1/2] i8253: Disable PIT timer 0 when not in use Michael Kelley
@ 2024-08-02 16:35 ` tip-bot2 for David Woodhouse
2 siblings, 0 replies; 6+ messages in thread
From: tip-bot2 for David Woodhouse @ 2024-08-02 16:35 UTC (permalink / raw)
To: linux-tip-commits
Cc: David Woodhouse, Thomas Gleixner, Michael Kelley, x86, linux-kernel
The following commit has been merged into the x86/timers branch of tip:
Commit-ID: 70e6b7d9ae3c63df90a7bba7700e8d5c300c3c60
Gitweb: https://git.kernel.org/tip/70e6b7d9ae3c63df90a7bba7700e8d5c300c3c60
Author: David Woodhouse <dwmw@amazon.co.uk>
AuthorDate: Fri, 02 Aug 2024 14:55:54 +01:00
Committer: Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Fri, 02 Aug 2024 18:27:05 +02:00
x86/i8253: Disable PIT timer 0 when not in use
Leaving the PIT interrupt running can cause noticeable steal time for
virtual guests. The VMM generally has a timer which toggles the IRQ input
to the PIC and I/O APIC, which takes CPU time away from the guest. Even
on real hardware, running the counter may use power needlessly (albeit
not much).
Make sure it's turned off if it isn't going to be used.
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Michael Kelley <mhkelley@outlook.com>
Link: https://lore.kernel.org/all/20240802135555.564941-1-dwmw2@infradead.org
---
arch/x86/kernel/i8253.c | 11 +++++++++--
drivers/clocksource/i8253.c | 13 +++++++++----
include/linux/i8253.h | 1 +
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/arch/x86/kernel/i8253.c b/arch/x86/kernel/i8253.c
index 2b7999a..80e262b 100644
--- a/arch/x86/kernel/i8253.c
+++ b/arch/x86/kernel/i8253.c
@@ -8,6 +8,7 @@
#include <linux/timex.h>
#include <linux/i8253.h>
+#include <asm/hypervisor.h>
#include <asm/apic.h>
#include <asm/hpet.h>
#include <asm/time.h>
@@ -39,9 +40,15 @@ static bool __init use_pit(void)
bool __init pit_timer_init(void)
{
- if (!use_pit())
+ if (!use_pit()) {
+ /*
+ * Don't just ignore the PIT. Ensure it's stopped, because
+ * VMMs otherwise steal CPU time just to pointlessly waggle
+ * the (masked) IRQ.
+ */
+ clockevent_i8253_disable();
return false;
-
+ }
clockevent_i8253_init(true);
global_clock_event = &i8253_clockevent;
return true;
diff --git a/drivers/clocksource/i8253.c b/drivers/clocksource/i8253.c
index d4350bb..cb215e6 100644
--- a/drivers/clocksource/i8253.c
+++ b/drivers/clocksource/i8253.c
@@ -108,11 +108,8 @@ int __init clocksource_i8253_init(void)
#endif
#ifdef CONFIG_CLKEVT_I8253
-static int pit_shutdown(struct clock_event_device *evt)
+void clockevent_i8253_disable(void)
{
- if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
- return 0;
-
raw_spin_lock(&i8253_lock);
outb_p(0x30, PIT_MODE);
@@ -123,6 +120,14 @@ static int pit_shutdown(struct clock_event_device *evt)
}
raw_spin_unlock(&i8253_lock);
+}
+
+static int pit_shutdown(struct clock_event_device *evt)
+{
+ if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
+ return 0;
+
+ clockevent_i8253_disable();
return 0;
}
diff --git a/include/linux/i8253.h b/include/linux/i8253.h
index 8336b2f..bf169cf 100644
--- a/include/linux/i8253.h
+++ b/include/linux/i8253.h
@@ -24,6 +24,7 @@ extern raw_spinlock_t i8253_lock;
extern bool i8253_clear_counter_on_shutdown;
extern struct clock_event_device i8253_clockevent;
extern void clockevent_i8253_init(bool oneshot);
+extern void clockevent_i8253_disable(void);
extern void setup_pit_timer(void);
^ permalink raw reply [flat|nested] 6+ messages in thread
* [tip: x86/timers] clockevents/drivers/i8253: Fix stop sequence for timer 0
2024-08-02 13:55 ` [PATCH 2/2] i8253: Fix stop sequence for timer 0 David Woodhouse
@ 2024-08-02 16:35 ` tip-bot2 for David Woodhouse
0 siblings, 0 replies; 6+ messages in thread
From: tip-bot2 for David Woodhouse @ 2024-08-02 16:35 UTC (permalink / raw)
To: linux-tip-commits
Cc: Sean Christopherson, Li RongQing, David Woodhouse,
Thomas Gleixner, Michael Kelley, x86, linux-kernel
The following commit has been merged into the x86/timers branch of tip:
Commit-ID: 531b2ca0a940ac9db03f246c8b77c4201de72b00
Gitweb: https://git.kernel.org/tip/531b2ca0a940ac9db03f246c8b77c4201de72b00
Author: David Woodhouse <dwmw@amazon.co.uk>
AuthorDate: Fri, 02 Aug 2024 14:55:55 +01:00
Committer: Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Fri, 02 Aug 2024 18:27:05 +02:00
clockevents/drivers/i8253: Fix stop sequence for timer 0
According to the data sheet, writing the MODE register should stop the
counter (and thus the interrupts). This appears to work on real hardware,
at least modern Intel and AMD systems. It should also work on Hyper-V.
However, on some buggy virtual machines the mode change doesn't have any
effect until the counter is subsequently loaded (or perhaps when the IRQ
next fires).
So, set MODE 0 and then load the counter, to ensure that those buggy VMs
do the right thing and the interrupts stop. And then write MODE 0 *again*
to stop the counter on compliant implementations too.
Apparently, Hyper-V keeps firing the IRQ *repeatedly* even in mode zero
when it should only happen once, but the second MODE write stops that too.
Userspace test program (mostly written by tglx):
=====
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/io.h>
static __always_inline void __out##bwl(type value, uint16_t port) \
{ \
asm volatile("out" #bwl " %" #bw "0, %w1" \
: : "a"(value), "Nd"(port)); \
} \
\
static __always_inline type __in##bwl(uint16_t port) \
{ \
type value; \
asm volatile("in" #bwl " %w1, %" #bw "0" \
: "=a"(value) : "Nd"(port)); \
return value; \
}
BUILDIO(b, b, uint8_t)
#define inb __inb
#define outb __outb
#define PIT_MODE 0x43
#define PIT_CH0 0x40
#define PIT_CH2 0x42
static int is8254;
static void dump_pit(void)
{
if (is8254) {
// Latch and output counter and status
outb(0xC2, PIT_MODE);
printf("%02x %02x %02x\n", inb(PIT_CH0), inb(PIT_CH0), inb(PIT_CH0));
} else {
// Latch and output counter
outb(0x0, PIT_MODE);
printf("%02x %02x\n", inb(PIT_CH0), inb(PIT_CH0));
}
}
int main(int argc, char* argv[])
{
int nr_counts = 2;
if (argc > 1)
nr_counts = atoi(argv[1]);
if (argc > 2)
is8254 = 1;
if (ioperm(0x40, 4, 1) != 0)
return 1;
dump_pit();
printf("Set oneshot\n");
outb(0x38, PIT_MODE);
outb(0x00, PIT_CH0);
outb(0x0F, PIT_CH0);
dump_pit();
usleep(1000);
dump_pit();
printf("Set periodic\n");
outb(0x34, PIT_MODE);
outb(0x00, PIT_CH0);
outb(0x0F, PIT_CH0);
dump_pit();
usleep(1000);
dump_pit();
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
printf("Set stop (%d counter writes)\n", nr_counts);
outb(0x30, PIT_MODE);
while (nr_counts--)
outb(0xFF, PIT_CH0);
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
printf("Set MODE 0\n");
outb(0x30, PIT_MODE);
dump_pit();
usleep(100000);
dump_pit();
usleep(100000);
dump_pit();
return 0;
}
=====
Suggested-by: Sean Christopherson <seanjc@google.com>
Co-developed-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Michael Kelley <mhkelley@outlook.com>
Link: https://lore.kernel.org/all/20240802135555.564941-2-dwmw2@infradead.org
---
arch/x86/kernel/cpu/mshyperv.c | 11 +----------
drivers/clocksource/i8253.c | 36 ++++++++++++++++++++++-----------
include/linux/i8253.h | 1 +-
3 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
index e0fd57a..3d4237f 100644
--- a/arch/x86/kernel/cpu/mshyperv.c
+++ b/arch/x86/kernel/cpu/mshyperv.c
@@ -16,7 +16,6 @@
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/kexec.h>
-#include <linux/i8253.h>
#include <linux/random.h>
#include <asm/processor.h>
#include <asm/hypervisor.h>
@@ -522,16 +521,6 @@ static void __init ms_hyperv_init_platform(void)
if (efi_enabled(EFI_BOOT))
x86_platform.get_nmi_reason = hv_get_nmi_reason;
- /*
- * Hyper-V VMs have a PIT emulation quirk such that zeroing the
- * counter register during PIT shutdown restarts the PIT. So it
- * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
- * to false tells pit_shutdown() not to zero the counter so that
- * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
- * and setting this value has no effect.
- */
- i8253_clear_counter_on_shutdown = false;
-
#if IS_ENABLED(CONFIG_HYPERV)
if ((hv_get_isolation_type() == HV_ISOLATION_TYPE_VBS) ||
ms_hyperv.paravisor_present)
diff --git a/drivers/clocksource/i8253.c b/drivers/clocksource/i8253.c
index cb215e6..39f7c2d 100644
--- a/drivers/clocksource/i8253.c
+++ b/drivers/clocksource/i8253.c
@@ -20,13 +20,6 @@
DEFINE_RAW_SPINLOCK(i8253_lock);
EXPORT_SYMBOL(i8253_lock);
-/*
- * Handle PIT quirk in pit_shutdown() where zeroing the counter register
- * restarts the PIT, negating the shutdown. On platforms with the quirk,
- * platform specific code can set this to false.
- */
-bool i8253_clear_counter_on_shutdown __ro_after_init = true;
-
#ifdef CONFIG_CLKSRC_I8253
/*
* Since the PIT overflows every tick, its not very useful
@@ -112,12 +105,33 @@ void clockevent_i8253_disable(void)
{
raw_spin_lock(&i8253_lock);
+ /*
+ * Writing the MODE register should stop the counter, according to
+ * the datasheet. This appears to work on real hardware (well, on
+ * modern Intel and AMD boxes; I didn't dig the Pegasos out of the
+ * shed).
+ *
+ * However, some virtual implementations differ, and the MODE change
+ * doesn't have any effect until either the counter is written (KVM
+ * in-kernel PIT) or the next interrupt (QEMU). And in those cases,
+ * it may not stop the *count*, only the interrupts. Although in
+ * the virt case, that probably doesn't matter, as the value of the
+ * counter will only be calculated on demand if the guest reads it;
+ * it's the interrupts which cause steal time.
+ *
+ * Hyper-V apparently has a bug where even in mode 0, the IRQ keeps
+ * firing repeatedly if the counter is running. But it *does* do the
+ * right thing when the MODE register is written.
+ *
+ * So: write the MODE and then load the counter, which ensures that
+ * the IRQ is stopped on those buggy virt implementations. And then
+ * write the MODE again, which is the right way to stop it.
+ */
outb_p(0x30, PIT_MODE);
+ outb_p(0, PIT_CH0);
+ outb_p(0, PIT_CH0);
- if (i8253_clear_counter_on_shutdown) {
- outb_p(0, PIT_CH0);
- outb_p(0, PIT_CH0);
- }
+ outb_p(0x30, PIT_MODE);
raw_spin_unlock(&i8253_lock);
}
diff --git a/include/linux/i8253.h b/include/linux/i8253.h
index bf169cf..56c280e 100644
--- a/include/linux/i8253.h
+++ b/include/linux/i8253.h
@@ -21,7 +21,6 @@
#define PIT_LATCH ((PIT_TICK_RATE + HZ/2) / HZ)
extern raw_spinlock_t i8253_lock;
-extern bool i8253_clear_counter_on_shutdown;
extern struct clock_event_device i8253_clockevent;
extern void clockevent_i8253_init(bool oneshot);
extern void clockevent_i8253_disable(void);
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-08-02 16:35 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-02 13:55 [PATCH 1/2] i8253: Disable PIT timer 0 when not in use David Woodhouse
2024-08-02 13:55 ` [PATCH 2/2] i8253: Fix stop sequence for timer 0 David Woodhouse
2024-08-02 16:35 ` [tip: x86/timers] clockevents/drivers/i8253: " tip-bot2 for David Woodhouse
2024-08-02 15:44 ` [PATCH 1/2] i8253: Disable PIT timer 0 when not in use Michael Kelley
2024-08-02 16:01 ` David Woodhouse
2024-08-02 16:35 ` [tip: x86/timers] x86/i8253: " tip-bot2 for David Woodhouse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®