Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp884471imm; Fri, 27 Jul 2018 07:46:51 -0700 (PDT) X-Google-Smtp-Source: AAOMgpc6QI7jnfQZR3v8F/egj5JfPR8ff4Vba1gpq7ncq98o4xdWf6ytcYKC0zNr66MBlXicnpnJ X-Received: by 2002:a62:129a:: with SMTP id 26-v6mr7024681pfs.102.1532702811709; Fri, 27 Jul 2018 07:46:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532702811; cv=none; d=google.com; s=arc-20160816; b=MoFzw9OxA+zSyOD4/hbq/QBHACjccDrh9+u4Y7fvbSqfT5Cbk+Zjs6gSh87GxaEuQ7 OpSzGM6MIiutzNZ8D2AtsouJX15iRbZOohM8XoxQAISDUooLJ2LS8FpcSadbLKJQ1rER 9HnfimlIM1lTFf03gpY6BwqViR/cF+2E23MHUe9mnqW2Mnii1/5jAoz6/xnOiFgPAKbC t0T/KMTzphSq0CmY3P32KkOSF1NxGGrKw5ptmKBpa8/KgkLkACQ8TSDJxuwakA8L6dFD mYHAB9dXYpLmJumdc8KrZMUDq4OUvY7OS+3gsbypxRdeBXZQE108FAKbneT5XtbJbRYi E4bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=RBVJjvH0HkkIpY9YdD1uHNKfdflP9VJEGy4ufhRIYPQ=; b=TyZJU6Or4KR0r0xPrBpp6KjeiLJmqS8TiCtr8BScMpWJVF9yFZEhKSB/2G8PTSz9XA Cw8jboUlE69HvHUv7kUH/QF78Bfsh+UioA101dd5yTL+MGQLN4PKPwjUx8GqtCUrqwD+ EuyV59ylWura7fWemef7Tg1YyLSjqkrf6wiUIXPJFzUij5BRqS7dpiDA5Q5DoVYAhgoi hyqAn3R3MpiTjyyLxAoGQpjK0YAN6rt+1WjIlhYUq8ERKUtuasdIlOyey5qZDgPEOzN8 tWTHufMCGqUTAtZ0Dr5fSDp9UzahrECIq+wHuJgOCakIJ+WNpM/rw/3QWHQ6GRvesthJ CUWA== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v14-v6si3766905pga.270.2018.07.27.07.46.34; Fri, 27 Jul 2018 07:46:51 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388619AbeG0QHG (ORCPT + 99 others); Fri, 27 Jul 2018 12:07:06 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:57820 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730714AbeG0QHF (ORCPT ); Fri, 27 Jul 2018 12:07:05 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A9E928197008; Fri, 27 Jul 2018 14:44:50 +0000 (UTC) Received: from vitty.brq.redhat.com (unknown [10.43.2.155]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B34F2026D6B; Fri, 27 Jul 2018 14:44:49 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH RFC] x86/kvm/lapic: always disable MMIO interface in x2APIC mode Date: Fri, 27 Jul 2018 16:44:48 +0200 Message-Id: <20180727144448.9606-1-vkuznets@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 27 Jul 2018 14:44:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Fri, 27 Jul 2018 14:44:50 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'vkuznets@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. The other solution would be to pre-allocate a 'shadow' page for lAPICs in disabled/x2APIC mode and re-direct all reads and writes there. I'm, however, not convinced this is a good thing to do. Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/lapic.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index b5cd8465d44f..89b59d6410ae 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1291,9 +1291,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, @@ -1305,6 +1304,11 @@ static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this, if (!apic_mmio_in_range(apic, address)) return -EOPNOTSUPP; + if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) { + memset(data, 0xff, len); + return 0; + } + kvm_lapic_reg_read(apic, offset, len, data); return 0; @@ -1864,6 +1868,9 @@ static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, if (!apic_mmio_in_range(apic, address)) return -EOPNOTSUPP; + if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) + return 0; + /* * APIC register must be aligned on 128-bits boundary. * 32/64/128 bits registers must be accessed thru 32 bits. -- 2.14.4