From patchwork Mon Apr 11 14:01:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Po-Hsu Lin X-Patchwork-Id: 1615750 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=aUEP+WVK; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KcVtF0fLlz9sDX for ; Tue, 12 Apr 2022 00:02:44 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1nduco-00027V-DU; Mon, 11 Apr 2022 14:02:38 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1nducn-00027C-4d for kernel-team@lists.ubuntu.com; Mon, 11 Apr 2022 14:02:37 +0000 Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 1EAC23F80E for ; Mon, 11 Apr 2022 14:02:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1649685756; bh=24hmt0CqQl4aHNvTNpqE67TT6SwINWvLj2VCToyAtBM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aUEP+WVKfKcT4O9NpvpfhVvr+KH52qOq92iRO6PSCgIJgOsVHU8tdUxrC2lgwUrRI XwS51prlNuiy+iRdHa7EYh+imjbW3jcgWgXPeNYirJ6oXM6oaPZsWBtpslzTH+ksom RJpVQI4wqPOFqLN4F0X8kHjTKmma6jWTHi6J6nhRsKi/l8okr+10ms7GUDS5BkfDgH eRgkBSD4kXpiVLcBL2VuGRJiwrfXK/MPKsSSz8UB6eZL3jECfki02UDkaxyt34lxU+ vgGy3dSnB9O+sGO17QGFlXGG8FudcZQ4bn2TqXN7P7zc7qq2ui1tHhkPsgYUSJ8p/0 zVZut9OKq9ZrQ== Received: by mail-pj1-f69.google.com with SMTP id q1-20020a17090a2dc100b001cba43e127dso1140923pjm.9 for ; Mon, 11 Apr 2022 07:02:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=24hmt0CqQl4aHNvTNpqE67TT6SwINWvLj2VCToyAtBM=; b=4s88EX7KEJdMa8nyTg/3Fj5SbT99OSYEVB3C+sbkkt38xVypxfZwOMYJuuR02KJcYr oTsDQdYlpwv/Dr92wPzQbvdVbWi+IE79wvMnHFd88GMmqNygZ6CvJ8axf3iynQAhY7w5 lIn28jTqtzZs6cDT4b29FhhO3g1I+bLix+QiqkkZrIO9mnNM9m0l2paRUuXj41uwdBDc 3Wji14sYPoDeRbFM0JjN+gSTGoS5gJxvrqWiebpQQah0ag7icQZLhpN/LWG8Sd+dCKxK HdLy80QjPDJVGSmgugWnS8ZERLqFzVqz1KlyXc4EjrloGA08R03YAB10KhXydYlNg6gv hT2A== X-Gm-Message-State: AOAM531oesEurPuBAvyu4IvkXdfugrUk2pauydyEdzcdi+bJLsoNq91T /KsqfHxOu/ChflESXMa0jzgfHcQ/9Wow6ggV/5BxgtCLXIwar7pg0ciFrhXl1FYwnPKxifgot8A Maa6ElvagYWlNXHdMOynqbpNF6gFIT4N0Za2h8Y8g X-Received: by 2002:a63:4b5b:0:b0:39d:3808:c226 with SMTP id k27-20020a634b5b000000b0039d3808c226mr7094752pgl.603.1649685754511; Mon, 11 Apr 2022 07:02:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxhXpBzHRPYk6y7mm2vDydUW8/JQtw3Web2mUHoeWbSDfBLCkRGw6w/8uF+adTsw1A78gjT1A== X-Received: by 2002:a63:4b5b:0:b0:39d:3808:c226 with SMTP id k27-20020a634b5b000000b0039d3808c226mr7094696pgl.603.1649685753682; Mon, 11 Apr 2022 07:02:33 -0700 (PDT) Received: from localhost.localdomain (223-136-39-236.emome-ip.hinet.net. [223.136.39.236]) by smtp.gmail.com with ESMTPSA id x6-20020a17090a294600b001cba3274bd0sm2742157pjf.28.2022.04.11.07.02.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Apr 2022 07:02:32 -0700 (PDT) From: Po-Hsu Lin To: kernel-team@lists.ubuntu.com Subject: [Impish][SRU][PATCH 1/3] KVM: Add infrastructure and macro to mark VM as bugged Date: Mon, 11 Apr 2022 22:01:55 +0800 Message-Id: <20220411140157.97354-2-po-hsu.lin@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220411140157.97354-1-po-hsu.lin@canonical.com> References: <20220411140157.97354-1-po-hsu.lin@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Sean Christopherson BugLink: https://bugs.launchpad.net/bugs/1966499 Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini Message-Id: <3a0998645c328bf0895f1290e61821b70f048549.1625186503.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini (backported from commit 0b8f11737cffc1a406d1134b58687abc29d76b52) [PHLin: missing 2fdef3a2ae CONFIG_HAVE_KVM_PM_NOTIFIER check] [PHLin: missing fcfe1baedd char stats_id] Signed-off-by: Po-Hsu Lin --- include/linux/kvm_host.h | 28 +++++++++++++++++++++++++++- virt/kvm/kvm_main.c | 10 +++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8583ed3f..41b652f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -149,6 +149,7 @@ static inline bool is_error_page(struct page *page) #define KVM_REQ_MMU_RELOAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_UNBLOCK 2 #define KVM_REQ_UNHALT 3 +#define KVM_REQ_VM_BUGGED (4 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQUEST_ARCH_BASE 8 #define KVM_ARCH_REQ_FLAGS(nr, flags) ({ \ @@ -585,6 +586,7 @@ struct kvm { pid_t userspace_pid; unsigned int max_halt_poll_ns; u32 dirty_ring_size; + bool vm_bugged; }; #define kvm_err(fmt, ...) \ @@ -613,6 +615,31 @@ struct kvm { #define vcpu_err(vcpu, fmt, ...) \ kvm_err("vcpu%i " fmt, (vcpu)->vcpu_id, ## __VA_ARGS__) +bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req); +static inline void kvm_vm_bugged(struct kvm *kvm) +{ + kvm->vm_bugged = true; + kvm_make_all_cpus_request(kvm, KVM_REQ_VM_BUGGED); +} + +#define KVM_BUG(cond, kvm, fmt...) \ +({ \ + int __ret = (cond); \ + \ + if (WARN_ONCE(__ret && !(kvm)->vm_bugged, fmt)) \ + kvm_vm_bugged(kvm); \ + unlikely(__ret); \ +}) + +#define KVM_BUG_ON(cond, kvm) \ +({ \ + int __ret = (cond); \ + \ + if (WARN_ON_ONCE(__ret && !(kvm)->vm_bugged)) \ + kvm_vm_bugged(kvm); \ + unlikely(__ret); \ +}) + static inline bool kvm_dirty_log_manual_protect_and_init_set(struct kvm *kvm) { return !!(kvm->manual_dirty_log_protect & KVM_DIRTY_LOG_INITIALLY_SET); @@ -930,7 +957,6 @@ void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc); bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req, struct kvm_vcpu *except, unsigned long *vcpu_bitmap, cpumask_var_t tmp); -bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req); bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req, struct kvm_vcpu *except); bool kvm_make_cpus_request_mask(struct kvm *kvm, unsigned int req, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 93b5f76..309db8d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3455,7 +3455,7 @@ static long kvm_vcpu_ioctl(struct file *filp, struct kvm_fpu *fpu = NULL; struct kvm_sregs *kvm_sregs = NULL; - if (vcpu->kvm->mm != current->mm) + if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_bugged) return -EIO; if (unlikely(_IOC_TYPE(ioctl) != KVMIO)) @@ -3661,7 +3661,7 @@ static long kvm_vcpu_compat_ioctl(struct file *filp, void __user *argp = compat_ptr(arg); int r; - if (vcpu->kvm->mm != current->mm) + if (vcpu->kvm->mm != current->mm || vcpu->kvm->vm_bugged) return -EIO; switch (ioctl) { @@ -3727,7 +3727,7 @@ static long kvm_device_ioctl(struct file *filp, unsigned int ioctl, { struct kvm_device *dev = filp->private_data; - if (dev->kvm->mm != current->mm) + if (dev->kvm->mm != current->mm || dev->kvm->vm_bugged) return -EIO; switch (ioctl) { @@ -4011,7 +4011,7 @@ static long kvm_vm_ioctl(struct file *filp, void __user *argp = (void __user *)arg; int r; - if (kvm->mm != current->mm) + if (kvm->mm != current->mm || kvm->vm_bugged) return -EIO; switch (ioctl) { case KVM_CREATE_VCPU: @@ -4219,7 +4219,7 @@ static long kvm_vm_compat_ioctl(struct file *filp, struct kvm *kvm = filp->private_data; int r; - if (kvm->mm != current->mm) + if (kvm->mm != current->mm || kvm->vm_bugged) return -EIO; switch (ioctl) { #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT From patchwork Mon Apr 11 14:01:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Po-Hsu Lin X-Patchwork-Id: 1615753 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=TIBfL7YX; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KcVtJ11myz9sDX for ; Tue, 12 Apr 2022 00:02:48 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1nducs-0002Al-N1; Mon, 11 Apr 2022 14:02:42 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1nducq-000294-F3 for kernel-team@lists.ubuntu.com; Mon, 11 Apr 2022 14:02:40 +0000 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 3DD863F80E for ; Mon, 11 Apr 2022 14:02:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1649685759; bh=r0AYQr1Npm+ueymv9SSRShilbw1NWYH6+NrNzasCerw=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TIBfL7YXadA3XuefiEXOjp7Pq3gNyH3EJ6+9+w7fgjVUdPKJRAvXb7fsl8oSz1NTv RQZVBwZdHQKrlKTw6uyep2vBilcO0NS6ae53YKxYIyVuSB3UDBHbM5tOQDkS7MZKId CDe2gUm0v5ZHxjviX6j7pPmLPXy3pK9EGooGxZbLYgbhFuU5xU5d4Hsml8d1MOEP2K rXg1Lgoma5huOLMnmPuSEw5iafpeo/oJM3zmgJXXOV8C/3ZK53rJnNfb6lKaPP0TZa RLu+A8sPFBn0MlVkvVhEGVS/lOJn/7i3o65u9/HHqquARjC1cmjBl27+vBrMAv0EIq GdVjiXtXIH4RQ== Received: by mail-pj1-f70.google.com with SMTP id i8-20020a17090a2a0800b001cb5c76ea21so3506793pjd.2 for ; Mon, 11 Apr 2022 07:02:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r0AYQr1Npm+ueymv9SSRShilbw1NWYH6+NrNzasCerw=; b=7DTGbch6AEphYYwa/7R+j43kJIP+ZQqxKlDYoZgbcW+FsYS2Rv7rrY26coTQocg18x BOHK+FOn6rl8aDxcnsiugEx0QXATeUwGtnRpvAUskLUceaY78Un/h6wXFnqpKBFNs7JP wqRWIhKYnp+UGhqrUie6e6tSgCWZBbK+NJX2PkoSLKofQwpDLGAUXOCBLJeSrB1o2jCi 9MgGieRLTWy3yeL5lq/HacxXOTfRHVi4PakHJfcYD0OwWVj4UmSk552AL9P3ZJDLqJkk D9z8D/vlpwvpun9NpyociqwCcPR/UMqDOP5It4GPWv1rEHy1UXlq3k66BzWgGq0OuxfC o/bg== X-Gm-Message-State: AOAM530vjhg2IU23AYanry/fBpP90s2EizzQJPm+GmqvRkEPuDMiymnr b6ZFUvRvGL1PMy4NW2AbDxNGewOdupT9VOT2U2/h2+gzmTuDf5L00GwABG1hI0KEI7LHxthfLkV jToZ7+p8MHSwvGXqwMSh7mvnVGEeATrtEUTEM9UhT X-Received: by 2002:a17:902:d482:b0:154:6f46:a602 with SMTP id c2-20020a170902d48200b001546f46a602mr32466731plg.155.1649685756334; Mon, 11 Apr 2022 07:02:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOwI3jcFYNzJDVJkPIIn1mlyYk3tkgQR5ShiDFKkIJarnQdFTaspCau4S45KbIbUlajERgcQ== X-Received: by 2002:a17:902:d482:b0:154:6f46:a602 with SMTP id c2-20020a170902d48200b001546f46a602mr32466689plg.155.1649685755854; Mon, 11 Apr 2022 07:02:35 -0700 (PDT) Received: from localhost.localdomain (223-136-39-236.emome-ip.hinet.net. [223.136.39.236]) by smtp.gmail.com with ESMTPSA id x6-20020a17090a294600b001cba3274bd0sm2742157pjf.28.2022.04.11.07.02.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Apr 2022 07:02:34 -0700 (PDT) From: Po-Hsu Lin To: kernel-team@lists.ubuntu.com Subject: [Impish][SRU][PATCH 2/3] KVM: x86: Use KVM_BUG/KVM_BUG_ON to handle bugs that are fatal to the VM Date: Mon, 11 Apr 2022 22:01:56 +0800 Message-Id: <20220411140157.97354-3-po-hsu.lin@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220411140157.97354-1-po-hsu.lin@canonical.com> References: <20220411140157.97354-1-po-hsu.lin@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Sean Christopherson BugLink: https://bugs.launchpad.net/bugs/1966499 Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini Message-Id: <0e8760a26151f47dc47052b25ca8b84fffe0641e.1625186503.git.isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini (backported from commit 673692735fdc40ed7da32c0cb3517adaf4227b2b) [PHLin: context adjustment] Signed-off-by: Po-Hsu Lin --- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 23 ++++++++++++++--------- arch/x86/kvm/x86.c | 4 ++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e4bbca0..b61493a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1538,7 +1538,7 @@ static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); break; default: - WARN_ON_ONCE(1); + KVM_BUG_ON(1, vcpu->kvm); } } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index fc6fbab..e51008e 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2355,7 +2355,7 @@ static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits; break; default: - WARN_ON_ONCE(1); + KVM_BUG_ON(1, vcpu->kvm); break; } } @@ -5041,6 +5041,7 @@ static int handle_cr(struct kvm_vcpu *vcpu) return kvm_complete_insn_gp(vcpu, err); case 3: WARN_ON_ONCE(enable_unrestricted_guest); + err = kvm_set_cr3(vcpu, val); return kvm_complete_insn_gp(vcpu, err); case 4: @@ -5066,14 +5067,13 @@ static int handle_cr(struct kvm_vcpu *vcpu) } break; case 2: /* clts */ - WARN_ONCE(1, "Guest should always own CR0.TS"); - vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); - trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); - return kvm_skip_emulated_instruction(vcpu); + KVM_BUG(1, vcpu->kvm, "Guest always owns CR0.TS"); + return -EIO; case 1: /*mov from cr*/ switch (cr) { case 3: WARN_ON_ONCE(enable_unrestricted_guest); + val = kvm_read_cr3(vcpu); kvm_register_write(vcpu, reg, val); trace_kvm_cr_read(cr, val); @@ -5383,7 +5383,9 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu) static int handle_nmi_window(struct kvm_vcpu *vcpu) { - WARN_ON_ONCE(!enable_vnmi); + if (KVM_BUG_ON(!enable_vnmi, vcpu->kvm)) + return -EIO; + exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING); ++vcpu->stat.nmi_window_exits; kvm_make_request(KVM_REQ_EVENT, vcpu); @@ -5939,7 +5941,8 @@ static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) * invalid guest state should never happen as that means KVM knowingly * allowed a nested VM-Enter with an invalid vmcs12. More below. */ - WARN_ON_ONCE(vmx->nested.nested_run_pending); + if (KVM_BUG_ON(vmx->nested.nested_run_pending, vcpu->kvm)) + return -EIO; if (is_guest_mode(vcpu)) { /* @@ -6333,7 +6336,9 @@ static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) int max_irr; bool max_irr_updated; - WARN_ON(!vcpu->arch.apicv_active); + if (KVM_BUG_ON(!vcpu->arch.apicv_active, vcpu->kvm)) + return -EIO; + if (pi_test_on(&vmx->pi_desc)) { pi_clear_on(&vmx->pi_desc); /* @@ -6411,7 +6416,7 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) unsigned int vector = intr_info & INTR_INFO_VECTOR_MASK; gate_desc *desc = (gate_desc *)host_idt_base + vector; - if (WARN_ONCE(!is_external_intr(intr_info), + if (KVM_BUG(!is_external_intr(intr_info), vcpu->kvm, "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info)) return; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c010c51..451e23b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9263,6 +9263,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) } if (kvm_request_pending(vcpu)) { + if (kvm_check_request(KVM_REQ_VM_BUGGED, vcpu)) { + r = -EIO; + goto out; + } if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { if (unlikely(!kvm_x86_ops.nested_ops->get_nested_state_pages(vcpu))) { r = 0; From patchwork Mon Apr 11 14:01:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Po-Hsu Lin X-Patchwork-Id: 1615752 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=canonical.com header.i=@canonical.com header.a=rsa-sha256 header.s=20210705 header.b=vEYrbp5C; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KcVtJ0nh5z9sBJ for ; Tue, 12 Apr 2022 00:02:48 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1nducs-0002BE-S6; Mon, 11 Apr 2022 14:02:42 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1nducq-00029M-Sq for kernel-team@lists.ubuntu.com; Mon, 11 Apr 2022 14:02:40 +0000 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id A21273F80E for ; Mon, 11 Apr 2022 14:02:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1649685760; bh=wCN7f2p51NcJposg/6tiOjuimCmj1A6R3doOPrpKlxg=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vEYrbp5C3JWFdYj++6pojTY68WV8AYjaa/FWdVm41Oo1QarlzfqF6gGN/Xe6DPvPO S9NVUXmX9NSCL8xGrFVwPBQbxahALyMrSO+OkFO/zLwTR32Y4CbsFLdCS7BCHIgNZa tQz0Nq1O20jUsv9bRBQt9eqbhQkMG7jcXu7UM2foi+VPNTjbF3nzRrs//rxhHoKepq OAIIfkDGaQn2250DmO4AJIQHxhtdDPuERcn6yCh4TOvlhPudPu1xcxY2RPp8pDHHmK LBgaZEJwAIubyQ5jMQR60nCFuqPBlHh3RtaAWmf4F3g42BZejkpi07Krpy9uHXNzEe cKKpKi8v8aVEA== Received: by mail-pf1-f199.google.com with SMTP id t4-20020a628104000000b005056f132662so6748350pfd.21 for ; Mon, 11 Apr 2022 07:02:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wCN7f2p51NcJposg/6tiOjuimCmj1A6R3doOPrpKlxg=; b=MzdiWRC1zL4FsJxZXOL5oiyq2lvtf5leo9mkMBqeRhByOUb3ITHEkJSdrF/wn/UA1S JwtffUAseFOQScie0ZFQMbJC5q7lLFoH0UXxg2Oo8PZroRFE0wn/KHvZepTf1M+KivK1 fQfK5yVpDas/sDrDcyPhUcq1DNFXy45m46NsfTP8sSXyVL+NGXKfKdDfjmK+BxHSJwEA gDRGP/sSo4jUaip7qfzjuaU4QkTklmjVA94j126/6CUeHdamHU/sMSk6QKS0Eb/c4zju eBz2P+5tvP54a1eYctJkh+hayDOuCR6W37NcmCxSEM7wjOkMbf5/Dr6IGqXkkILhzwzP 1fBw== X-Gm-Message-State: AOAM530KZs76TW70tlzH6N4Akpkd0SnsmspcXFAWYE6LsHcRIAjJdYcv XR/emotDSrxqF9Ilh7B1SEqMmcp9X4wR1KukqygtevQaXbRYilP/2aggl2R9iJLowhtzBh4VHxi 9qacJM9LYxfJFJqcDMGa9OcJANNcspPSj4SeFMSHL X-Received: by 2002:a63:4147:0:b0:382:9ac9:b12b with SMTP id o68-20020a634147000000b003829ac9b12bmr26683188pga.277.1649685757993; Mon, 11 Apr 2022 07:02:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/beumXzVApCkUAejtcJl04NTwV8CDo39MDzoDDc1ehE6t9bvHjZ/C4Py/wxDETvkrydXOZg== X-Received: by 2002:a63:4147:0:b0:382:9ac9:b12b with SMTP id o68-20020a634147000000b003829ac9b12bmr26683164pga.277.1649685757649; Mon, 11 Apr 2022 07:02:37 -0700 (PDT) Received: from localhost.localdomain (223-136-39-236.emome-ip.hinet.net. [223.136.39.236]) by smtp.gmail.com with ESMTPSA id x6-20020a17090a294600b001cba3274bd0sm2742157pjf.28.2022.04.11.07.02.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Apr 2022 07:02:36 -0700 (PDT) From: Po-Hsu Lin To: kernel-team@lists.ubuntu.com Subject: [Impish][SRU][PATCH 3/3] KVM: VMX: prepare sync_pir_to_irr for running with APICv disabled Date: Mon, 11 Apr 2022 22:01:57 +0800 Message-Id: <20220411140157.97354-4-po-hsu.lin@canonical.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220411140157.97354-1-po-hsu.lin@canonical.com> References: <20220411140157.97354-1-po-hsu.lin@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Paolo Bonzini BugLink: https://bugs.launchpad.net/bugs/1966499 If APICv is disabled for this vCPU, assigned devices may still attempt to post interrupts. In that case, we need to cancel the vmentry and deliver the interrupt with KVM_REQ_EVENT. Extend the existing code that handles injection of L1 interrupts into L2 to cover this case as well. vmx_hwapic_irr_update is only called when APICv is active so it would be confusing to add a check for vcpu->arch.apicv_active in there. Instead, just use vmx_set_rvi directly in vmx_sync_pir_to_irr. Cc: stable@vger.kernel.org Reviewed-by: Maxim Levitsky Reviewed-by: David Matlack Reviewed-by: Sean Christopherson Message-Id: <20211123004311.2954158-3-pbonzini@redhat.com> Signed-off-by: Paolo Bonzini (cherry picked from commit 7e1901f6c86c896acff6609e0176f93f756d8b2a) Signed-off-by: Po-Hsu Lin --- arch/x86/kvm/vmx/vmx.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e51008e..50d9bf6 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6334,9 +6334,9 @@ static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); int max_irr; - bool max_irr_updated; + bool got_posted_interrupt; - if (KVM_BUG_ON(!vcpu->arch.apicv_active, vcpu->kvm)) + if (KVM_BUG_ON(!enable_apicv, vcpu->kvm)) return -EIO; if (pi_test_on(&vmx->pi_desc)) { @@ -6346,22 +6346,33 @@ static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) * But on x86 this is just a compiler barrier anyway. */ smp_mb__after_atomic(); - max_irr_updated = + got_posted_interrupt = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); - - /* - * If we are running L2 and L1 has a new pending interrupt - * which can be injected, this may cause a vmexit or it may - * be injected into L2. Either way, this interrupt will be - * processed via KVM_REQ_EVENT, not RVI, because we do not use - * virtual interrupt delivery to inject L1 interrupts into L2. - */ - if (is_guest_mode(vcpu) && max_irr_updated) - kvm_make_request(KVM_REQ_EVENT, vcpu); } else { max_irr = kvm_lapic_find_highest_irr(vcpu); + got_posted_interrupt = false; } - vmx_hwapic_irr_update(vcpu, max_irr); + + /* + * Newly recognized interrupts are injected via either virtual interrupt + * delivery (RVI) or KVM_REQ_EVENT. Virtual interrupt delivery is + * disabled in two cases: + * + * 1) If L2 is running and the vCPU has a new pending interrupt. If L1 + * wants to exit on interrupts, KVM_REQ_EVENT is needed to synthesize a + * VM-Exit to L1. If L1 doesn't want to exit, the interrupt is injected + * into L2, but KVM doesn't use virtual interrupt delivery to inject + * interrupts into L2, and so KVM_REQ_EVENT is again needed. + * + * 2) If APICv is disabled for this vCPU, assigned devices may still + * attempt to post interrupts. The posted interrupt vector will cause + * a VM-Exit and the subsequent entry will call sync_pir_to_irr. + */ + if (!is_guest_mode(vcpu) && kvm_vcpu_apicv_active(vcpu)) + vmx_set_rvi(max_irr); + else if (got_posted_interrupt) + kvm_make_request(KVM_REQ_EVENT, vcpu); + return max_irr; }