From: Sean Christopherson <seanjc@google.com>
To: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
x86@kernel.org, Paolo Bonzini <pbonzini@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>,
Sean Christopherson <seanjc@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Subject: [PATCH 3/4] x86/irq: KVM: Harden posted interrupt (un)registration paths
Date: Fri, 8 Oct 2021 17:11:06 -0700 [thread overview]
Message-ID: <20211009001107.3936588-4-seanjc@google.com> (raw)
In-Reply-To: <20211009001107.3936588-1-seanjc@google.com>
Split the register and unregister paths for the posted interrupt wakeup
handler, and WARN on conditions that are blatant bugs, e.g. attempting to
overwrite an existing handler, unregistering the wrong handler, etc...
This is very much a "low hanging fruit" hardening, e.g. a broken module
could foul things up by doing concurrent registration from multiple CPUs.
Drop the use of a dummy handler so that the rejection logic can use a
simple NULL check. There is zero benefit to blindly calling into a dummy
handler.
Note, the registration path doesn't require synchronization, as it's the
caller's responsibility to not generate interrupts it cares about until
after its handler is registered, i.e. there can't be a relevant in-flight
interrupt.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/include/asm/irq.h | 3 ++-
arch/x86/kernel/irq.c | 29 ++++++++++++++++++++---------
arch/x86/kvm/vmx/vmx.c | 4 ++--
3 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/arch/x86/include/asm/irq.h b/arch/x86/include/asm/irq.h
index 768aa234cbb4..c79014c2443d 100644
--- a/arch/x86/include/asm/irq.h
+++ b/arch/x86/include/asm/irq.h
@@ -30,7 +30,8 @@ struct irq_desc;
extern void fixup_irqs(void);
#ifdef CONFIG_HAVE_KVM
-extern void kvm_set_posted_intr_wakeup_handler(void (*handler)(void));
+extern void kvm_register_posted_intr_wakeup_handler(void (*handler)(void));
+extern void kvm_unregister_posted_intr_wakeup_handler(void (*handler)(void));
#endif
extern void (*x86_platform_ipi_callback)(void);
diff --git a/arch/x86/kernel/irq.c b/arch/x86/kernel/irq.c
index 20773d315308..97f452cc84be 100644
--- a/arch/x86/kernel/irq.c
+++ b/arch/x86/kernel/irq.c
@@ -284,18 +284,26 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_x86_platform_ipi)
#endif
#ifdef CONFIG_HAVE_KVM
-static void dummy_handler(void) {}
-static void (*kvm_posted_intr_wakeup_handler)(void) = dummy_handler;
+static void (*kvm_posted_intr_wakeup_handler)(void);
-void kvm_set_posted_intr_wakeup_handler(void (*handler)(void))
+void kvm_register_posted_intr_wakeup_handler(void (*handler)(void))
{
- if (handler)
- kvm_posted_intr_wakeup_handler = handler;
- else
- kvm_posted_intr_wakeup_handler = dummy_handler;
+ if (WARN_ON_ONCE(!handler || kvm_posted_intr_wakeup_handler))
+ return;
+
+ WRITE_ONCE(kvm_posted_intr_wakeup_handler, handler);
+}
+EXPORT_SYMBOL_GPL(kvm_register_posted_intr_wakeup_handler);
+
+void kvm_unregister_posted_intr_wakeup_handler(void (*handler)(void))
+{
+ if (WARN_ON_ONCE(!handler || handler != kvm_posted_intr_wakeup_handler))
+ return;
+
+ WRITE_ONCE(kvm_posted_intr_wakeup_handler, NULL);
synchronize_rcu();
}
-EXPORT_SYMBOL_GPL(kvm_set_posted_intr_wakeup_handler);
+EXPORT_SYMBOL_GPL(kvm_unregister_posted_intr_wakeup_handler);
/*
* Handler for POSTED_INTERRUPT_VECTOR.
@@ -311,9 +319,12 @@ DEFINE_IDTENTRY_SYSVEC_SIMPLE(sysvec_kvm_posted_intr_ipi)
*/
DEFINE_IDTENTRY_SYSVEC(sysvec_kvm_posted_intr_wakeup_ipi)
{
+ void (*handler)(void) = READ_ONCE(kvm_posted_intr_wakeup_handler);
+
ack_APIC_irq();
inc_irq_stat(kvm_posted_intr_wakeup_ipis);
- kvm_posted_intr_wakeup_handler();
+ if (handler)
+ handler();
}
/*
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index bfdcdb399212..9164f1870d49 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7553,7 +7553,7 @@ static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
static void hardware_unsetup(void)
{
- kvm_set_posted_intr_wakeup_handler(NULL);
+ kvm_unregister_posted_intr_wakeup_handler(pi_wakeup_handler);
if (nested)
nested_vmx_hardware_unsetup();
@@ -7907,7 +7907,7 @@ static __init int hardware_setup(void)
if (r)
nested_vmx_hardware_unsetup();
- kvm_set_posted_intr_wakeup_handler(pi_wakeup_handler);
+ kvm_register_posted_intr_wakeup_handler(pi_wakeup_handler);
return r;
}
--
2.33.0.882.g93a45727a2-goog
next prev parent reply other threads:[~2021-10-09 0:11 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-09 0:11 [PATCH 0/4] x86/x86: KVM: Fixes for KVM's PI wakeup handler Sean Christopherson
2021-10-09 0:11 ` [PATCH 1/4] x86/irq: Ensure PI wakeup handler is unregistered before module unload Sean Christopherson
2021-10-09 0:11 ` [PATCH 2/4] KVM: VMX: Unregister posted interrupt wakeup handler on hardware unsetup Sean Christopherson
2021-10-09 0:11 ` Sean Christopherson [this message]
2021-10-21 19:42 ` [PATCH 3/4] x86/irq: KVM: Harden posted interrupt (un)registration paths Paolo Bonzini
2021-10-09 0:11 ` [PATCH 4/4] KVM: VMX: Register posted interrupt wakeup handler iff APICv is enabled Sean Christopherson
2021-10-21 19:43 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211009001107.3936588-4-seanjc@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®