Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp5451530imm; Tue, 16 Oct 2018 10:25:10 -0700 (PDT) X-Google-Smtp-Source: ACcGV61bdN3Gd+FCjvT5yY6Pstm1Y0wHRYsjp4CE5RdGMssJkZbElpKhGmizW+Fn+kLITB+gLjjN X-Received: by 2002:a63:450b:: with SMTP id s11-v6mr21463693pga.301.1539710710398; Tue, 16 Oct 2018 10:25:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539710710; cv=none; d=google.com; s=arc-20160816; b=eizyrEIb//8jcNztmLkXpU6am1ReFC0R4YUwifvDU4NULk98IarYsO3GhufVmcLROb 0K96Hj8Ijh8vLYWfWh5esBdZPHOMOYkrGv84A5zKkxU5fXvdyDoHBT/+vS7dfZcwh6vg MFyrTOPsiMpHwAHI0ST21637nchv3r/1bzWApngXRES9NzY5kO+f1EASG4teTZKf1xcs wa3oxfKVICQod/YvIDdUnnQlQrkaeJ0tqBVEv+1NcYsICrfJRESmAabBN6x/bh03zpVL xHWSNfgMKaiy1CgRVBGJTKGvfVcCy6D9T68zjMKLt9Ezs6O2wcw8NC6eNYuvsj5xBRjJ Y3iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4K4uhudBnvpSXdrIsTXY51RsJSLhRYxUTuN4dkH7Oz0=; b=PC2cuEbhDd8eowsp6kWxTlravG7tcNQ6ues/2I+5ZAWNQGX6HMm/SV+Qmn+f4TvuY5 2PJjIz7WOkAIuKzdvFZPTvSmO3s9EJzCBLk+zyzQvtWV4zsWuwH3dy5z/cVjNg10f+19 V8rRiFkb+CxU978f/H98bZZvSyHzRZCAJKWxd7h5TW+uollQ7XPjungEhduheD4uzxyc PnjSQnaFsueEqxIm6UXjrxR9SINSA2GcL2fCiovL0od2wQdyyxJ0YLhTwUrP91vk9+2a B2vO0unnP4zM9Nxqt0mUC7JNiXh3OfY0zBMkrRJlMsl5CMOb6Pv/YtvdXIag/jMjpURz sQrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Zmy32Zaz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u18-v6si14552266pgl.59.2018.10.16.10.24.54; Tue, 16 Oct 2018 10:25:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Zmy32Zaz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731727AbeJQBQC (ORCPT + 99 others); Tue, 16 Oct 2018 21:16:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:34710 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730396AbeJQBQB (ORCPT ); Tue, 16 Oct 2018 21:16:01 -0400 Received: from localhost (ip-213-127-77-176.ip.prioritytelecom.net [213.127.77.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3049D20658; Tue, 16 Oct 2018 17:24:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1539710675; bh=zhhOgwSQp0O+pX7OKUfhZ2oyu2DmZ2pwdWT2eySAeEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zmy32ZazKaPRf0Sws5rMEP9Bl1cwWQLq4gju3ZxL5k5CPyFhdm5SxO8jsQC9n+hSk YUhHgXQkz7tyv/EGcmj6CqcsnZx2PzYIsrmU5fOOqgtws2OXnCkW5EyhEFeLOnPLhE n4L+ig7ztblYz52T/2bbFZR6MiabeqcU11JR69Bk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vitaly Kuznetsov , Paolo Bonzini , Sasha Levin Subject: [PATCH 4.9 12/71] x86/kvm/lapic: always disable MMIO interface in x2APIC mode Date: Tue, 16 Oct 2018 19:09:09 +0200 Message-Id: <20181016170540.022539856@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181016170539.315587743@linuxfoundation.org> References: <20181016170539.315587743@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Vitaly Kuznetsov [ Upstream commit d1766202779e81d0f2a94c4650a6ba31497d369d ] When VMX is used with flexpriority disabled (because of no support or if disabled with module parameter) MMIO interface to lAPIC is still available in x2APIC mode while it shouldn't be (kvm-unit-tests): PASS: apic_disable: Local apic enabled in x2APIC mode PASS: apic_disable: CPUID.1H:EDX.APIC[bit 9] is set FAIL: apic_disable: *0xfee00030: 50014 The issue appears because we basically do nothing while switching to x2APIC mode when APIC access page is not used. apic_mmio_{read,write} only check if lAPIC is disabled before proceeding to actual write. When APIC access is virtualized we correctly manipulate with VMX controls in vmx_set_virtual_apic_mode() and we don't get vmexits from memory writes in x2APIC mode so there's no issue. Disabling MMIO interface seems to be easy. The question is: what do we do with these reads and writes? If we add apic_x2apic_mode() check to apic_mmio_in_range() and return -EOPNOTSUPP these reads and writes will go to userspace. When lAPIC is in kernel, Qemu uses this interface to inject MSIs only (see kvm_apic_mem_write() in hw/i386/kvm/apic.c). This somehow works with disabled lAPIC but when we're in xAPIC mode we will get a real injected MSI from every write to lAPIC. Not good. The simplest solution seems to be to just ignore writes to the region and return ~0 for all reads when we're in x2APIC mode. This is what this patch does. However, this approach is inconsistent with what currently happens when flexpriority is enabled: we allocate APIC access page and create KVM memory region so in x2APIC modes all reads and writes go to this pre-allocated page which is, btw, the same for all vCPUs. Signed-off-by: Vitaly Kuznetsov Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/lapic.c | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -356,5 +356,6 @@ struct kvm_sync_regs { #define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0) #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1) +#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2) #endif /* _ASM_X86_KVM_H */ --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1220,9 +1220,8 @@ EXPORT_SYMBOL_GPL(kvm_lapic_reg_read); static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr) { - return kvm_apic_hw_enabled(apic) && - addr >= apic->base_address && - addr < apic->base_address + LAPIC_MMIO_LENGTH; + return addr >= apic->base_address && + addr < apic->base_address + LAPIC_MMIO_LENGTH; } static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this, @@ -1234,6 +1233,15 @@ static int apic_mmio_read(struct kvm_vcp if (!apic_mmio_in_range(apic, address)) return -EOPNOTSUPP; + if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) { + if (!kvm_check_has_quirk(vcpu->kvm, + KVM_X86_QUIRK_LAPIC_MMIO_HOLE)) + return -EOPNOTSUPP; + + memset(data, 0xff, len); + return 0; + } + kvm_lapic_reg_read(apic, offset, len, data); return 0; @@ -1646,6 +1654,14 @@ static int apic_mmio_write(struct kvm_vc if (!apic_mmio_in_range(apic, address)) return -EOPNOTSUPP; + if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) { + if (!kvm_check_has_quirk(vcpu->kvm, + KVM_X86_QUIRK_LAPIC_MMIO_HOLE)) + return -EOPNOTSUPP; + + return 0; + } + /* * APIC register must be aligned on 128-bits boundary. * 32/64/128 bits registers must be accessed thru 32 bits.