From patchwork Fri May 8 20:36:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 11537547 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC54781 for ; Fri, 8 May 2020 20:37:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B391B2184D for ; Fri, 8 May 2020 20:37:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Qk7wdSMs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727100AbgEHUhA (ORCPT ); Fri, 8 May 2020 16:37:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726811AbgEHUhA (ORCPT ); Fri, 8 May 2020 16:37:00 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F335C061A0C for ; Fri, 8 May 2020 13:37:00 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id j4so3535353ybj.20 for ; Fri, 08 May 2020 13:37:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Qcb4vvcQqRXsDBmKyBIQkPZTiuE/XsF+tWw9+Q+7+I0=; b=Qk7wdSMsN5vkbs1NfrrX4UKqwjbWzalid1t69mr1A/Q/Q6B8d/wND5S285btB7y1MK PWoULzyfLqq7yUCfYmmBV4GitfC8aY5/hDsix5zpZghRJSb6jzDyuNKwt8vtd78o4NVM e9eJLqqFSUIUKs1OZXu0s66Isaeo/1+hp5wMH+1DGjLHnMWDL6+GpJbwji7Gcx2pB7bl TKV6NeplKgjKeDkEQ4jB5ULWeMboLQODRfJFl958H+lt+kFY6TL+pEMVjmfdPznnUPmf G8djp0QvjD2+Rzv2GoEN48gIqkekQUWm+Wsq/kKQMd02zIALC8MEABPnEEYiSJZL30q+ IgWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Qcb4vvcQqRXsDBmKyBIQkPZTiuE/XsF+tWw9+Q+7+I0=; b=I8norwl+JvuiQtew2Flz7DpNf167iE7A5cIQFizaoQkafTY//O9YP0OdXJjt0JN1Gg zc5k4c78egzKJ9ay+8kJoCfXMzNDdUb9DQEq1MuNPWrwwin3GygCUXWLIZDINKYE5Zva LbO3uDGPnGUyqe7X62mWHFOjABE/3FJZ/bkzSANOpSaqTu2W92z4yzVxRrZSJaLIhfif vnDhyl52AIAeKvUvr3FJWtZwiPFZRNCLlgHvlKVPsassVMPXOZyPVrlv0MhgKsA+Bs4b ERVBuYBKD/D8bxAdT6eBr2PxF/NoVrYxGuYZes/yi0nPNUphy8WCi8FrwYt+0SSMSOrJ 8sIQ== X-Gm-Message-State: AGi0PuYbdtbqQzin2C+bqjVerYpkGzw1tRV4QST6M46A5pncNBcOwH6G SQXiDT/LGnKECUU5rk4lfZuQDxujg+4s5OyiPumdKupC4IkRiv8dEeQwWqXSZbZgyVB88EkRfNr Z3YTAYhiHFoMXKN/3UsuVK6paTMCpAgkBYR5wvSzTs/25/RBuladwxWzCBiviHZw= X-Google-Smtp-Source: APiQypLLg2dj+v1i92RV4I4X9Okba9Ag3baU2Adrb5e5XceLCXCuLRYuq1IX6g5Y7lVid8C2citxOzJjh/nxKQ== X-Received: by 2002:a25:9a42:: with SMTP id r2mr7472703ybo.281.1588970219625; Fri, 08 May 2020 13:36:59 -0700 (PDT) Date: Fri, 8 May 2020 13:36:41 -0700 In-Reply-To: <20200508203643.85477-1-jmattson@google.com> Message-Id: <20200508203643.85477-2-jmattson@google.com> Mime-Version: 1.0 References: <20200508203643.85477-1-jmattson@google.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH 1/3] KVM: nVMX: Really make emulated nested preemption timer pinned From: Jim Mattson To: kvm@vger.kernel.org, Paolo Bonzini Cc: Jim Mattson , Peter Shier , Oliver Upton Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The PINNED bit is ignored by hrtimer_init. It is only considered when starting the timer. When the hrtimer isn't pinned to the same logical processor as the vCPU thread to be interrupted, the emulated VMX-preemption timer often fails to adhere to the architectural specification. Fixes: f15a75eedc18e ("KVM: nVMX: make emulated nested preemption timer pinned") Signed-off-by: Jim Mattson Reviewed-by: Peter Shier Reviewed-by: Oliver Upton --- arch/x86/kvm/vmx/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index fd78ffbde644..1f7fe6f47cbc 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2041,7 +2041,7 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) preemption_timeout *= 1000000; do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); hrtimer_start(&vmx->nested.preemption_timer, - ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); + ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL_PINNED); } static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) From patchwork Fri May 8 20:36:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 11537549 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E703481 for ; Fri, 8 May 2020 20:37:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFA232184D for ; Fri, 8 May 2020 20:37:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KmJ03/aH" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727798AbgEHUhE (ORCPT ); Fri, 8 May 2020 16:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727119AbgEHUhD (ORCPT ); Fri, 8 May 2020 16:37:03 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AF5FC05BD09 for ; Fri, 8 May 2020 13:37:03 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e2so3483431ybm.19 for ; Fri, 08 May 2020 13:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=bDA1/4UWq3adzuN/eFYQ6blf4dCGtForBLd4rlJVQeQ=; b=KmJ03/aH3GNYNIjMppA4tkWIBFQOpt/I9LWla5xHPJOe2xUqR1mXebaYnz+u43orHd ksABti4aur9lE2CaeeXV8L5OZg+zbw4+Pr3N3D9U0nds/H5NByo/VAojx/Ht9BWz3txy FJ64rA0hKG1FZrjNap/oZHV1PBe9HaXotsbRb9JGfsqaXlgAt18YLnelLnPb58XhLc56 Ic8JR1pR97RwGgh1D8lC5FIyZBLjNQbDmB+bP0cfBMIGepQUhzhGPBScP84AR+X4+l/w FpLceRZiFmsM8+5RchzuneWNzLFCigbYk3ssVkK94VMdHel4f2aWS9/p0X2qZCgd4S+t o4uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=bDA1/4UWq3adzuN/eFYQ6blf4dCGtForBLd4rlJVQeQ=; b=qEXKjZc/OO+BygQJkuaA4yMEwVPFXU4TeHVwgCVS2Lk/PlPQX7Cb8lAZUfhAI0s3Yw kVpQqw6as/A4D4ARfEEVCkNFZm4vegHJi2ekLEpwh2CwF+mpcS7AofACBt9Mx43yIT8f BX2smQigcarY3iDW2dUxB9WMlDhLP1gQo6tB+d/ypYx73KnzK9rae6MgPf4LJ1z5b5j2 sPAaC1rxBkmf8MsqUuStLHbc860y7P4UGK9tSMUmKB+S3kR058w8eMIb0XeaCE4Et3eQ mJubDCzN17jSaL3MQ5N/Jk7U8ZymyjE1MDcayp6qveoZz8c0yX6SZgvjsDwM7nQYK3Wv cF4w== X-Gm-Message-State: AGi0PuaHeIhpkyUEVibtX6JPeZrKh0Is+1tt2JK8JX9+OUzbzFsZgNgX 1SBiqxa+/URr80LUlzaw/6OzCFI0ZW09/WYEw8X+oP7btZgsxlF+69IVAFqwzOLY4FviL87222w iv98q2UZj47Pn6hCH/1MD+LOfjfPFgGhgkdAkVwFnC5K/4kVhHVaQLPddgms13cI= X-Google-Smtp-Source: APiQypLLKa9+gLSOiHY7sDhxzhx+tvo03jD6CV3zZ4lMk9RLpxYBNHJvvfIFNrQjcKigi5IfEsnBg1FBmzhNwg== X-Received: by 2002:a25:ce88:: with SMTP id x130mr1178224ybe.523.1588970222696; Fri, 08 May 2020 13:37:02 -0700 (PDT) Date: Fri, 8 May 2020 13:36:42 -0700 In-Reply-To: <20200508203643.85477-1-jmattson@google.com> Message-Id: <20200508203643.85477-3-jmattson@google.com> Mime-Version: 1.0 References: <20200508203643.85477-1-jmattson@google.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH 2/3] KVM: nVMX: Change emulated VMX-preemption timer hrtimer to absolute From: Jim Mattson To: kvm@vger.kernel.org, Paolo Bonzini Cc: Jim Mattson , Peter Shier , Oliver Upton Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Prepare for migration of this hrtimer, by changing it from relative to absolute. (I couldn't get migration to work with a relative timer.) Signed-off-by: Jim Mattson Reviewed-by: Peter Shier Reviewed-by: Oliver Upton --- arch/x86/kvm/vmx/nested.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 1f7fe6f47cbc..3ca792e3cd66 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -2041,7 +2041,8 @@ static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) preemption_timeout *= 1000000; do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); hrtimer_start(&vmx->nested.preemption_timer, - ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL_PINNED); + ktime_add_ns(ktime_get(), preemption_timeout), + HRTIMER_MODE_ABS_PINNED); } static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) @@ -4614,7 +4615,7 @@ static int enter_vmx_operation(struct kvm_vcpu *vcpu) goto out_shadow_vmcs; hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, - HRTIMER_MODE_REL_PINNED); + HRTIMER_MODE_ABS_PINNED); vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; vmx->nested.vpid02 = allocate_vpid(); From patchwork Fri May 8 20:36:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Mattson X-Patchwork-Id: 11537551 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D018992A for ; Fri, 8 May 2020 20:37:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B7DAA218AC for ; Fri, 8 May 2020 20:37:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c5Ie1kbX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727119AbgEHUhG (ORCPT ); Fri, 8 May 2020 16:37:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727108AbgEHUhG (ORCPT ); Fri, 8 May 2020 16:37:06 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F1FFC05BD43 for ; Fri, 8 May 2020 13:37:06 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id h185so3542081ybg.6 for ; Fri, 08 May 2020 13:37:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=w4qW/VNK1GgqP6kbHy5LSEPtJWF2jV3O0V1qObPzmFA=; b=c5Ie1kbX0guPcgMQEvwztbLcikBY8qYpIPyKl3/uQuStvCvAktbbhjFssCEcZTaxd+ 0+ITTk+kGrgK5h/OGT1lXOzuQ4BOAuVlfqIajuk3C1aTO5OkBsUIYhkZIQEjMsQnANo8 XPVOGgz7GEMttLQPYVw9fwsiJyhSlrPp1WWuhMGBMm6KBycDfdSFf4z+zJtwtl25kklW 27QXlBaTIIRUfcnhlDaJgHBOkuY6tutdbG/RRLYlyyx8TkqnR4VCtVL7TjIJELkj3kax qTz+rHmp/mo/O98POKM0h94siSAVBIdTh52loifb0qvF07qsEfRN7qUnX6NsYOLNx2fJ 2e5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=w4qW/VNK1GgqP6kbHy5LSEPtJWF2jV3O0V1qObPzmFA=; b=QIgK/mitHMWhJWnbt9UOZRqosthYwEMM44sDrz+JhPuDr0K+pKe/G++gX4uA92l5gT 822SVxTGmjsu/qkzZjCnEg7zBF5z+xm7GxVZwSxSKnDpm7Bu48IApmkcqwOfmCs0YEXh i5bZp7nnlTEp84kLd/8b3qRJ77SEb2qWbd0ZX/7N5rT5Xfy3wq+qfAq+l8covDRpf+SS +m56EZCgDIW2KIwo93jKv56nsEWTBIBviBUwoqBzqZgtoQEeVV2RL2myOqYO8nPJvU5j i+IQORcBKVJU/gY9WzM0keJqMj2r3WeQvFjpDAo2C4pBtUX8Ut7pl3g2h6075liwX5wj 3wMA== X-Gm-Message-State: AGi0PuaRxuqTj1WZsjAJV25DLPpsyxJzjbYGY2Ko9zThz+DqlYfRJYT8 9/wXqWopoS1dXzaiL7TgJ15MaTVZPQ8oVjUXS44U5IKeZq51Dxp/SBx2TvxXKsMo6lswIu6oo8G S3bWgDQ1nG73sKtC4zG5ky4RF94fxj9gcB0Y2tQtR/utUCRAtHh6TvrxHU0DlmQg= X-Google-Smtp-Source: APiQypKH0sBHGa5DkYxxf1H3iw4LhgfmRCRIOY2M6wDnkw/EWf4VgkJ3aYEXF2GDVwk8/enrmY14dXMwL7qwKg== X-Received: by 2002:a25:ac13:: with SMTP id w19mr8083110ybi.467.1588970225447; Fri, 08 May 2020 13:37:05 -0700 (PDT) Date: Fri, 8 May 2020 13:36:43 -0700 In-Reply-To: <20200508203643.85477-1-jmattson@google.com> Message-Id: <20200508203643.85477-4-jmattson@google.com> Mime-Version: 1.0 References: <20200508203643.85477-1-jmattson@google.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH 3/3] KVM: nVMX: Migrate the VMX-preemption timer From: Jim Mattson To: kvm@vger.kernel.org, Paolo Bonzini Cc: Jim Mattson , Peter Shier , Oliver Upton Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The hrtimer used to emulate the VMX-preemption timer must be pinned to the same logical processor as the vCPU thread to be interrupted if we want to have any hope of adhering to the architectural specification of the VMX-preemption timer. Even with this change, the emulated VMX-preemption timer VM-exit occasionally arrives too late. Signed-off-by: Jim Mattson Reviewed-by: Peter Shier Reviewed-by: Oliver Upton --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/irq.c | 2 ++ arch/x86/kvm/vmx/vmx.c | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 42a2d0d3984a..a47c71d13039 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1254,6 +1254,8 @@ struct kvm_x86_ops { bool (*apic_init_signal_blocked)(struct kvm_vcpu *vcpu); int (*enable_direct_tlbflush)(struct kvm_vcpu *vcpu); + + void (*migrate_timers)(struct kvm_vcpu *vcpu); }; struct kvm_x86_init_ops { diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index e330e7d125f7..54f7ea68083b 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -159,6 +159,8 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu) { __kvm_migrate_apic_timer(vcpu); __kvm_migrate_pit_timer(vcpu); + if (kvm_x86_ops.migrate_timers) + kvm_x86_ops.migrate_timers(vcpu); } bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c2c6335a998c..3896dea72082 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7687,6 +7687,16 @@ static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu) return to_vmx(vcpu)->nested.vmxon; } +static void vmx_migrate_timers(struct kvm_vcpu *vcpu) +{ + if (is_guest_mode(vcpu)) { + struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer; + + if (hrtimer_try_to_cancel(timer) == 1) + hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); + } +} + static void hardware_unsetup(void) { if (nested) @@ -7838,6 +7848,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .nested_get_evmcs_version = NULL, .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault, .apic_init_signal_blocked = vmx_apic_init_signal_blocked, + .migrate_timers = vmx_migrate_timers, }; static __init int hardware_setup(void)