From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kvm@vger.kernel.org
Subject: [Qemu-devel] [PATCH 7/7] KVM: use KVM_CAP_IMMEDIATE_EXIT
Date: Fri, 10 Feb 2017 10:50:12 +0100 [thread overview]
Message-ID: <20170210095012.16039-8-pbonzini@redhat.com> (raw)
In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com>
The purpose of the KVM_SET_SIGNAL_MASK API is to let userspace "kick"
a VCPU out of KVM_RUN through a POSIX signal. A signal is attached
to a dummy signal handler; by blocking the signal outside KVM_RUN and
unblocking it inside, this possible race is closed:
VCPU thread service thread
--------------------------------------------------------------
check flag
set flag
raise signal
(signal handler does nothing)
KVM_RUN
However, one issue with KVM_SET_SIGNAL_MASK is that it has to take
tsk->sighand->siglock on every KVM_RUN. This lock is often on a
remote NUMA node, because it is on the node of a thread's creator.
Taking this lock can be very expensive if there are many userspace
exits (as is the case for SMP Windows VMs without Hyper-V reference
time counter).
KVM_CAP_IMMEDIATE_EXIT provides an alternative, where the flag is
placed directly in kvm_run so that KVM can see it:
VCPU thread service thread
--------------------------------------------------------------
raise signal
signal handler
set run->immediate_exit
KVM_RUN
check run->immediate_exit
The previous patches changed QEMU so that the only blocked signal is
SIG_IPI, so we can now stop using KVM_SET_SIGNAL_MASK and sigtimedwait
if KVM_CAP_IMMEDIATE_EXIT is available.
On a 14-VCPU guest, an "inl" operation goes down from 30k to 6k on
an unlocked (no BQL) MemoryRegion, or from 30k to 15k if the BQL
is involved.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
kvm-all.c | 36 ++++++++++++++++++++++++++++++++----
| 4 +++-
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/kvm-all.c b/kvm-all.c
index eaff0dc..083143f 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -120,6 +120,7 @@ bool kvm_vm_attributes_allowed;
bool kvm_direct_msi_allowed;
bool kvm_ioeventfd_any_length_allowed;
bool kvm_msi_use_devid;
+static bool kvm_immediate_exit;
static const KVMCapabilityInfo kvm_required_capabilites[] = {
KVM_CAP_INFO(USER_MEMORY),
@@ -1619,6 +1620,7 @@ static int kvm_init(MachineState *ms)
goto err;
}
+ kvm_immediate_exit = kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT);
s->nr_slots = kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS);
/* If unspecified, use the default value */
@@ -1897,6 +1899,20 @@ static __thread void *pending_sigbus_addr;
static __thread int pending_sigbus_code;
static __thread bool have_sigbus_pending;
+static void kvm_cpu_kick(CPUState *cpu)
+{
+ atomic_set(&cpu->kvm_run->immediate_exit, 1);
+}
+
+static void kvm_cpu_kick_self(void)
+{
+ if (kvm_immediate_exit) {
+ kvm_cpu_kick(current_cpu);
+ } else {
+ qemu_cpu_kick_self();
+ }
+}
+
static void kvm_eat_signals(CPUState *cpu)
{
struct timespec ts = { 0, 0 };
@@ -1905,6 +1921,10 @@ static void kvm_eat_signals(CPUState *cpu)
sigset_t chkset;
int r;
+ if (kvm_immediate_exit) {
+ return;
+ }
+
sigemptyset(&waitset);
sigaddset(&waitset, SIG_IPI);
@@ -1953,7 +1973,7 @@ int kvm_cpu_exec(CPUState *cpu)
* instruction emulation. This self-signal will ensure that we
* leave ASAP again.
*/
- qemu_cpu_kick_self();
+ kvm_cpu_kick_self();
}
run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
@@ -2426,8 +2446,12 @@ static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset)
return r;
}
-static void dummy_signal(int sig)
+static void kvm_ipi_signal(int sig)
{
+ if (current_cpu) {
+ assert(kvm_immediate_exit);
+ kvm_cpu_kick(current_cpu);
+ }
}
void kvm_init_cpu_signals(CPUState *cpu)
@@ -2437,7 +2461,7 @@ void kvm_init_cpu_signals(CPUState *cpu)
struct sigaction sigact;
memset(&sigact, 0, sizeof(sigact));
- sigact.sa_handler = dummy_signal;
+ sigact.sa_handler = kvm_ipi_signal;
sigaction(SIG_IPI, &sigact, NULL);
pthread_sigmask(SIG_BLOCK, NULL, &set);
@@ -2446,7 +2470,11 @@ void kvm_init_cpu_signals(CPUState *cpu)
pthread_sigmask(SIG_SETMASK, &set, NULL);
#endif
sigdelset(&set, SIG_IPI);
- r = kvm_set_signal_mask(cpu, &set);
+ if (kvm_immediate_exit) {
+ r = pthread_sigmask(SIG_SETMASK, &set, NULL);
+ } else {
+ r = kvm_set_signal_mask(cpu, &set);
+ }
if (r) {
fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
exit(1);
--git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h
index bb0ed71..b7d53ee 100644
--- a/linux-headers/linux/kvm.h
+++ b/linux-headers/linux/kvm.h
@@ -218,7 +218,8 @@ struct kvm_hyperv_exit {
struct kvm_run {
/* in */
__u8 request_interrupt_window;
- __u8 padding1[7];
+ __u8 immediate_exit;
+ __u8 padding1[6];
/* out */
__u32 exit_reason;
@@ -870,6 +871,7 @@ struct kvm_ppc_smmu_info {
#define KVM_CAP_S390_USER_INSTR0 130
#define KVM_CAP_MSI_DEVID 131
#define KVM_CAP_PPC_HTM 132
+#define KVM_CAP_IMMEDIATE_EXIT 136
#ifdef KVM_CAP_IRQ_ROUTING
--
1.8.3.1
next prev parent reply other threads:[~2017-02-10 9:50 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-10 9:50 [Qemu-devel] [PATCH qemu 0/7] KVM: race-free exit from KVM_RUN without POSIX signals Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 1/7] cpus: remove ugly cast on sigbus_handler Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 2/7] KVM: x86: cleanup SIGBUS handlers Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 3/7] cpus: reorganize signal handling code Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 4/7] KVM: remove kvm_arch_on_sigbus Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 5/7] KVM: do not use sigtimedwait to catch SIGBUS Paolo Bonzini
2017-02-10 9:50 ` [Qemu-devel] [PATCH 6/7] KVM: move SIG_IPI handling to kvm-all.c Paolo Bonzini
2017-02-10 9:50 ` Paolo Bonzini [this message]
2017-02-10 10:11 ` [Qemu-devel] [PATCH qemu 0/7] KVM: race-free exit from KVM_RUN without POSIX signals no-reply
2017-02-15 15:57 ` 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=20170210095012.16039-8-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).