Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp1189602imj; Thu, 14 Feb 2019 02:41:26 -0800 (PST) X-Google-Smtp-Source: AHgI3IZD+/ZjMcVbB5Rw7HZ68FE2fVHnIYx62vaTcw+EEAtGCYsyVLau/gTnrTi6V5cMBgGStQsM X-Received: by 2002:a63:1cd:: with SMTP id 196mr3164463pgb.58.1550140885998; Thu, 14 Feb 2019 02:41:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550140885; cv=none; d=google.com; s=arc-20160816; b=YIywApIPLQSWmrQa1BSHQ1/5fyWQobRbXfmLMQFEvl2oAL+taIV0rhuKVtJS9e/Eme cSBYBkikoId49SoG9mIGTGmau7juVJtW9T6/jP4vD1gSktMojsfVVM8Gbt3+A4WKvZsT KpGEp1TTv7VmjUmj/mgofa6i4rpyHAUO27/wgpy96Gyn1C6sUhwBjEQmrV7/LuT6qZcH ko5m5l2LlNxp66L47n1UD9CuPedh53UE7flYT7sbXneGO+jQAVHgDW1PfnqxGT+Vvx5l Fh4dV/H16SprCZiqKkHryZsmvFZkxhk6U9gZ5TXzZosJgSFF56RIZwSai+oYX9lciIuE fTAg== 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; bh=kiL7uflAto/0xsdwzwzQnDn+foglC4+cLDn4Pp4jL98=; b=VOguBUNCYh/HAstAJhVQBeGlcICyih2xm88AGbTscy0RMzvzzpc26/dUn64d9FTQYl g7d0WSyMyX4fUcvk27N+3OR/PLt6X1mOK0lxoJzX7Ej6cCWVSzqF4C2sNNTCaTztWSx0 Z9mAULqwnRf9GPURFHAXpxefLnS2b6U0Fk2we6wxmjcR7HIwy8QEsfwQIC/p4JhxgCK/ 5n2uy3U32a2v7h4BPN57dF2Lu/MZNEHupU5jqKMpAhry0F7OAXxCcuNmzwrBJt1/VnX8 WbxQdUA4R5vXUu2EE8/BeeAYutNspApOwKFK/9LpSjhatS0DKkZrUehCYLKKm7f87421 3eRA== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11si1949546pgv.101.2019.02.14.02.41.09; Thu, 14 Feb 2019 02:41:25 -0800 (PST) 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405161AbfBNCsy (ORCPT + 99 others); Wed, 13 Feb 2019 21:48:54 -0500 Received: from mga01.intel.com ([192.55.52.88]:39458 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732564AbfBNCsx (ORCPT ); Wed, 13 Feb 2019 21:48:53 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 13 Feb 2019 18:48:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,367,1544515200"; d="scan'208";a="299599924" Received: from skl-s2.bj.intel.com ([10.240.192.103]) by orsmga005.jf.intel.com with ESMTP; 13 Feb 2019 18:48:50 -0800 From: Luwei Kang To: pbonzini@redhat.com, rkrcmar@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, x86@kernel.org Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Luwei Kang Subject: [PATCH V4] KVM: x86: Sync the pending Posted-Interrupts Date: Thu, 14 Feb 2019 10:48:07 +0800 Message-Id: <1550112487-4482-1-git-send-email-luwei.kang@intel.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some Posted-Interrupts from passthrough devices may be lost or overwritten when the vCPU is in runnable state. The SN (Suppress Notification) of PID (Posted Interrupt Descriptor) will be set when the vCPU is preempted (vCPU in KVM_MP_STATE_RUNNABLE state but not running on physical CPU). If a posted interrupt coming at this time, the irq remmaping facility will set the bit of PIR (Posted Interrupt Requests) without ON (Outstanding Notification). So this interrupt can't be sync to APIC virtualization register and will not be handled by Guest because ON is zero. Signed-off-by: Luwei Kang --- arch/x86/kvm/vmx/vmx.c | 26 +++++++++++--------------- arch/x86/kvm/vmx/vmx.h | 6 ++++++ arch/x86/kvm/x86.c | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f6915f1..fe59199 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1192,21 +1192,6 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) return; - /* - * First handle the simple case where no cmpxchg is necessary; just - * allow posting non-urgent interrupts. - * - * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change - * PI.NDST: pi_post_block will do it for us and the wakeup_handler - * expects the VCPU to be on the blocked_vcpu_list that matches - * PI.NDST. - */ - if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || - vcpu->cpu == cpu) { - pi_clear_sn(pi_desc); - return; - } - /* The full case. */ do { old.control = new.control = pi_desc->control; @@ -1221,6 +1206,17 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) new.sn = 0; } while (cmpxchg64(&pi_desc->control, old.control, new.control) != old.control); + + /* + * Clear SN before reading the bitmap. The VT-d firmware + * writes the bitmap and reads SN atomically (5.2.3 in the + * spec), so it doesn't really have a memory barrier that + * pairs with this, but we cannot do that and we need one. + */ + smp_mb__after_atomic(); + + if (!bitmap_empty((unsigned long *)pi_desc->pir, NR_VECTORS)) + pi_set_on(pi_desc); } /* diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 9932895..a4527e1 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -349,6 +349,12 @@ static inline void pi_set_sn(struct pi_desc *pi_desc) (unsigned long *)&pi_desc->control); } +static inline void pi_set_on(struct pi_desc *pi_desc) +{ + set_bit(POSTED_INTR_ON, + (unsigned long *)&pi_desc->control); +} + static inline void pi_clear_on(struct pi_desc *pi_desc) { clear_bit(POSTED_INTR_ON, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3d32b8f..ebd6737 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7795,7 +7795,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) * 1) We should set ->mode before checking ->requests. Please see * the comment in kvm_vcpu_exiting_guest_mode(). * - * 2) For APICv, we should set ->mode before checking PIR.ON. This + * 2) For APICv, we should set ->mode before checking PID.ON. This * pairs with the memory barrier implicit in pi_test_and_set_on * (see vmx_deliver_posted_interrupt). * -- 1.8.3.1