Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp899235ybx; Wed, 6 Nov 2019 10:03:07 -0800 (PST) X-Google-Smtp-Source: APXvYqx9WFuPhhcjS610xexGE/jgSrGMyPi4o+0Zk/nk0Aqv5FKuQOCa71xX302loL9DFsoWAldt X-Received: by 2002:a1c:4c10:: with SMTP id z16mr3437695wmf.24.1573063387443; Wed, 06 Nov 2019 10:03:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573063387; cv=none; d=google.com; s=arc-20160816; b=W8bezgiMM+gM51SEtgplZlOGgn4kAdVdUKp6fVtCBru5k1KXm+zMjMWmDBXTR0XXBd Z7bXYy9K6GQ9YqTi5aGb6bsOY6Llth7wI4v/4vi0GoZr7zMj5f1RzHydzS6FfyERNY2u q0gB9mQZEMZjyQpr4Sztq9JOqDJfAALeXCBQkdAyz0HJlFLydj6fPKKf6zuKxQzy2gbh A0cPHzEZQcDWEicO42DyBknubSxX9jM0SxT9v2OaxNVvJ7wak0SbCL8h0iOhTlodn3LM g19Upd/hNJAygeg6Z6/zH2MSjz1vWy4s9CU7IcCEawp3Lv27Ad409K8Z5kfj3XpLCh00 aOdg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oZuMeHaMaOYuKrxpCHjtxszEeyvg+X97FPZxAHAp6C4=; b=ALyGP4OZBSYNeZFJ6XDPwkDHfaLw5rDXrXqfuzmnYs25a6RslJi+GWR3oDCaIA+NDa zX9hNzOX8KSbKyVL5T4ZoLiVcmMJcCPLHb/ew0aiOnxXrjVT0LIdztBkEInbjO/wQTL7 /Pg3ZPZ1GlSVXvu8dHN4aGPKNvFEe+0FawLlT3oF6nhp3Cupsw80Wxzx+TJ5r11TdVxz KctODfcQ969YZxDZd1a28DQt7p4sMKmPUJTMRyE2wt1g6Y1Pi2laGMvMa9N+XEOMaZrI nvHHx2P/1skN3rjqktYBVyYG3coRFKGenbjR1oAthpiBj9ccHe3Ro7neKhgQwZK4aUxe mRBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2019-08-05 header.b=EY6rnttZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q40si13521532eda.218.2019.11.06.10.02.43; Wed, 06 Nov 2019 10:03:07 -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; dkim=pass header.i=@oracle.com header.s=corp-2019-08-05 header.b=EY6rnttZ; 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=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732451AbfKFR7y (ORCPT + 99 others); Wed, 6 Nov 2019 12:59:54 -0500 Received: from userp2130.oracle.com ([156.151.31.86]:33612 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727286AbfKFR7y (ORCPT ); Wed, 6 Nov 2019 12:59:54 -0500 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xA6HsRgI015900; Wed, 6 Nov 2019 17:58:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=oZuMeHaMaOYuKrxpCHjtxszEeyvg+X97FPZxAHAp6C4=; b=EY6rnttZWedzTFX/wcpCd2oM0+W/5yaQ04qdDT0/rDiJCiDVbD8NU2VdpNAKuWFXoMYm L7AFUVK33ETtp5+NcI6b6RANVQB9dbpJaWrGMPnvbFLgtj3w+4fjUOU2L6MhtOI9ZSBL ZxIJ/a//DW57FsrKErDiVhwr50GwEF7QeWDPPC0saHT58ROQMAWEXd/A/v3Sp5ELXRaP u4jatYyb2c75iQXrTVSqjqoRu5mUqDY5tC7pgFofd3qNszhdmgN+mdmxLuGrhqz6e32L w8ysmasiXoAJraOuCKZmfN5hYMQ+GNeDKGGVht7H1wHQs60ko6+8e6fG/6rLBkE1r9A1 3g== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 2w41w10mp7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Nov 2019 17:58:32 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id xA6HrjYl142999; Wed, 6 Nov 2019 17:56:31 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 2w41wds1jp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 06 Nov 2019 17:56:31 +0000 Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id xA6HuUOR015793; Wed, 6 Nov 2019 17:56:30 GMT Received: from paddy.uk.oracle.com (/10.175.178.239) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 06 Nov 2019 09:56:29 -0800 From: Joao Martins To: kvm@vger.kernel.org Cc: Joao Martins , linux-kernel@vger.kernel.org, Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Liran Alon , Jag Raman Subject: [PATCH v1 2/3] KVM: VMX: Do not change PID.NDST when loading a blocked vCPU Date: Wed, 6 Nov 2019 17:56:01 +0000 Message-Id: <20191106175602.4515-3-joao.m.martins@oracle.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20191106175602.4515-1-joao.m.martins@oracle.com> References: <20191106175602.4515-1-joao.m.martins@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9433 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1910280000 definitions=main-1911060173 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9433 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1910280000 definitions=main-1911060174 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When vCPU enters block phase, pi_pre_block() inserts vCPU to a per pCPU linked list of all vCPUs that are blocked on this pCPU. Afterwards, it changes PID.NV to POSTED_INTR_WAKEUP_VECTOR which its handler (wakeup_handler()) is responsible to kick (unblock) any vCPU on that linked list that now has pending posted interrupts. While vCPU is blocked (in kvm_vcpu_block()), it may be preempted which will cause vmx_vcpu_pi_put() to set PID.SN. If later the vCPU will be scheduled to run on a different pCPU, vmx_vcpu_pi_load() will clear PID.SN but will also *overwrite PID.NDST to this different pCPU*. Instead of keeping it with original pCPU which vCPU had entered block phase on. This results in an issue because when a posted interrupt is delivered, the wakeup_handler() will be executed and fail to find blocked vCPU on its per pCPU linked list of all vCPUs that are blocked on this pCPU. Which is due to the vCPU being placed on a *different* per pCPU linked list than the original pCPU that it had entered block phase. The regression is introduced by commit c112b5f50232 ("KVM: x86: Recompute PID.ON when clearing PID.SN"). Therefore, partially revert it and reintroduce the condition in vmx_vcpu_pi_load() responsible for avoiding changing PID.NDST when loading a blocked vCPU. Fixes: c112b5f50232 ("KVM: x86: Recompute PID.ON when clearing PID.SN") Signed-off-by: Joao Martins Signed-off-by: Liran Alon --- arch/x86/kvm/vmx/vmx.c | 14 ++++++++++++++ arch/x86/kvm/vmx/vmx.h | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 18b0bee662a5..75d903455e1c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1274,6 +1274,18 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) return; + /* + * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change + * PI.NDST: pi_post_block is the one expected to change PID.NDST and the + * wakeup handler expects the vCPU to be on the blocked_vcpu_list that + * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up + * correctly. + */ + if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) { + pi_clear_sn(pi_desc); + goto after_clear_sn; + } + /* The full case. */ do { old.control = new.control = pi_desc->control; @@ -1289,6 +1301,8 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) } while (cmpxchg64(&pi_desc->control, old.control, new.control) != old.control); +after_clear_sn: + /* * Clear SN before reading the bitmap. The VT-d firmware * writes the bitmap and reads SN atomically (5.2.3 in the diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index bee16687dc0b..1e32ab54fc2d 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -373,6 +373,12 @@ static inline void pi_clear_on(struct pi_desc *pi_desc) (unsigned long *)&pi_desc->control); } +static inline void pi_clear_sn(struct pi_desc *pi_desc) +{ + clear_bit(POSTED_INTR_SN, + (unsigned long *)&pi_desc->control); +} + static inline int pi_test_on(struct pi_desc *pi_desc) { return test_bit(POSTED_INTR_ON, -- 2.11.0