From patchwork Mon Jan 18 23:22:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ioanna Alifieraki X-Patchwork-Id: 1428335 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) 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 ozlabs.org (Postfix) with ESMTPS id 4DKSVg3b8tz9sWL; Tue, 19 Jan 2021 10:22:19 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l1dqi-0006mr-6A; Mon, 18 Jan 2021 23:22:16 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqg-0006mJ-7N for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:14 +0000 Received: from mail-wr1-f69.google.com ([209.85.221.69]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqf-0005Ol-VW for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:14 +0000 Received: by mail-wr1-f69.google.com with SMTP id r8so8983013wro.22 for ; Mon, 18 Jan 2021 15:22:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=3JpBwK2Gm172Gy4PIiCtSfqV4Fsn5Yd0LTKIflL7TXs=; b=HX6pjpy6F+mquRA4ZSAdj0iHHWdN6AUfWziPCaLkqOBR7NeB9DUqtkAknHlskdprXe zDMlnaG2UYuWs6xr/3owBAtDkPzZD8RBMufvmTM/HRycoJxfFuDb+VAGI31mlW+DfiUV xo4aVZLV3aodeK5zmYd+Ggqp03iNy2BRPayBclTkluV7U3qDXA8d7gWzJaSqo/21XPe3 dSTugK2H4v+A/Hfjj4vCDIW0WCSVd6MDXydvANZKoxteDejekrhgg4n80LZ/KQ6VTYKz zBrfPLBhRvUQDbpQTZNPIt8GnywCg1g/6OzYTzuvFDGQ9WfjmZWi5teChM1WSmqTSPhn fRug== X-Gm-Message-State: AOAM530J8gS5yad6dWPgf8i3fZGIVnPeSdz5Yxl03POygNIPPd6QMAre DQy3kX1f+YF0EG5REf4kQxzgKV7/coI9kR7s+SP+Mv+2pIctkgMXVQ/fGPFkCTeUZBHZGw4JCgZ xomFbslDVuqMmftFC3lpjtcbc+/NrJXolVhwUV3vn4A== X-Received: by 2002:adf:ef12:: with SMTP id e18mr1567787wro.192.1611012133459; Mon, 18 Jan 2021 15:22:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJxBBCif57O67l0D3q/C33nxMLNNY45p+wz6tPE08MCw27PMQ7xoF/iCYO4WcQaJNvjyR3m//w== X-Received: by 2002:adf:ef12:: with SMTP id e18mr1567774wro.192.1611012133220; Mon, 18 Jan 2021 15:22:13 -0800 (PST) Received: from localhost ([2001:67c:1560:8007::aac:c2e0]) by smtp.gmail.com with ESMTPSA id u16sm21018497wrn.68.2021.01.18.15.22.12 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jan 2021 15:22:12 -0800 (PST) From: Ioanna Alifieraki To: kernel-team@lists.ubuntu.com, ioanna-maria.alifieraki@canonical.com Subject: [SRU][Xenial][PATCH v3 1/4] kvm: vmx: rename vmx_pre/post_block to pi_pre/post_block Date: Mon, 18 Jan 2021 23:22:05 +0000 Message-Id: <20210118232208.11178-2-ioanna-maria.alifieraki@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210118232208.11178-1-ioanna-maria.alifieraki@canonical.com> References: <20210118232208.11178-1-ioanna-maria.alifieraki@canonical.com> 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: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Yunhong Jiang BugLink: https://bugs.launchpad.net/bugs/1908428 Prepare to switch from preemption timer to hrtimer in the vmx_pre/post_block. Current functions are only for posted interrupt, rename them accordingly. Signed-off-by: Yunhong Jiang Signed-off-by: Paolo Bonzini (cherry picked from commit bc22512bb24c480fae8ae96b233378ef96007590) Signed-off-by: Ioanna Alifieraki --- arch/x86/kvm/vmx.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 46d1293..d73ad4b 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11158,7 +11158,7 @@ static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, * this case, return 1, otherwise, return 0. * */ -static int vmx_pre_block(struct kvm_vcpu *vcpu) +static int pi_pre_block(struct kvm_vcpu *vcpu) { unsigned long flags; unsigned int dest; @@ -11224,7 +11224,15 @@ static int vmx_pre_block(struct kvm_vcpu *vcpu) return 0; } -static void vmx_post_block(struct kvm_vcpu *vcpu) +static int vmx_pre_block(struct kvm_vcpu *vcpu) +{ + if (pi_pre_block(vcpu)) + return 1; + + return 0; +} + +static void pi_post_block(struct kvm_vcpu *vcpu) { struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); struct pi_desc old, new; @@ -11265,6 +11273,11 @@ static void vmx_post_block(struct kvm_vcpu *vcpu) } } +static void vmx_post_block(struct kvm_vcpu *vcpu) +{ + pi_post_block(vcpu); +} + /* * vmx_update_pi_irte - set IRTE for Posted-Interrupts * From patchwork Mon Jan 18 23:22:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ioanna Alifieraki X-Patchwork-Id: 1428336 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) 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 ozlabs.org (Postfix) with ESMTPS id 4DKSVn2D0Rz9sVR; Tue, 19 Jan 2021 10:22:25 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l1dqn-0006pX-BN; Mon, 18 Jan 2021 23:22:21 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqi-0006mq-8Z for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:16 +0000 Received: from mail-wr1-f70.google.com ([209.85.221.70]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqi-0005Ou-0v for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:16 +0000 Received: by mail-wr1-f70.google.com with SMTP id u3so8927355wri.19 for ; Mon, 18 Jan 2021 15:22:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xFbn3l7HRnT2Rwt2/XWrB2WcAm3h4yKuwJzED5Y7Bh0=; b=K5ddLpMniHWWJxFRU1O7nIbqq12lViZaimiMD6t/ITjRIHIFNSfrwZuwPwt4L3MaT4 wA0bKNoRNYPusXiODnGAYPVhvRG3/KeA7Y7q86JIm67qyafgdbi5c10+fPKmwSIlZAXB kSX/bkBl9SU1qUXdeRSF9urcwuAFRn0gAn8YmxF074HA5l935JYqOFBdnTkmNAySorhS 6PvY7RnJuNhwrHOK9ZFv/xyfQjTD0VKFrir8VRXqFUGdpeKi40QVp6KROR+wrz4arK4e wguTZEv1KyYp+4F9Ykk6oEqNgqvrMj5oQxhcS5UZhB3c8IkRRYVCmGfFr49pxWs+Es74 kgQQ== X-Gm-Message-State: AOAM530XWP/8H0w1zLC1sSkfvTDwWSIsEqOdxGydLH80IH5dd2+cnmqm X88OoYdPQvPBkjC8U4RaO6lqrPGQLdqUAz7PrtAp/0dJ13o2FGzluV+D8/7moF/rVqWzCb87JhJ 0ZAEaEDBN/WAmTaBcDtqWSfJa6nkPtODBFP9uPiF0tw== X-Received: by 2002:a1c:ba04:: with SMTP id k4mr1376715wmf.16.1611012135472; Mon, 18 Jan 2021 15:22:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdfIJVOVnfee/u25BpHXqz1IqElnp+l2MEMTPFYUPwVtzGZSGx6ntopxbQYeS3mSFDbk7vIg== X-Received: by 2002:a1c:ba04:: with SMTP id k4mr1376709wmf.16.1611012135237; Mon, 18 Jan 2021 15:22:15 -0800 (PST) Received: from localhost ([2001:67c:1560:8007::aac:c2e0]) by smtp.gmail.com with ESMTPSA id h125sm1637754wmh.16.2021.01.18.15.22.14 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jan 2021 15:22:14 -0800 (PST) From: Ioanna Alifieraki To: kernel-team@lists.ubuntu.com, ioanna-maria.alifieraki@canonical.com Subject: [SRU][Xenial][PATCH v3 2/4] KVM: VMX: extract __pi_post_block Date: Mon, 18 Jan 2021 23:22:06 +0000 Message-Id: <20210118232208.11178-3-ioanna-maria.alifieraki@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210118232208.11178-1-ioanna-maria.alifieraki@canonical.com> References: <20210118232208.11178-1-ioanna-maria.alifieraki@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/1908428 Simple code movement patch, preparing for the next one. Cc: Huangweidong Cc: Gonglei Cc: wangxin Cc: Radim Krčmář Tested-by: Longpeng (Mike) Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini (backported from commit cd39e1176d320157831ce030b4c869bd2d5eb142) [hunk 1 : replace cmpxchg with cmpxchg64 for i386 build. hunk 2 : resolve if-statement conflict.] Signed-off-by: Ioanna Alifieraki --- arch/x86/kvm/vmx.c | 71 +++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index d73ad4b..93d922a 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11145,6 +11145,43 @@ static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); } +static void __pi_post_block(struct kvm_vcpu *vcpu) +{ + struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); + struct pi_desc old, new; + unsigned int dest; + unsigned long flags; + + do { + old.control = new.control = pi_desc->control; + + dest = cpu_physical_id(vcpu->cpu); + + if (x2apic_enabled()) + new.ndst = dest; + else + new.ndst = (dest << 8) & 0xFF00; + + /* Allow posting non-urgent interrupts */ + new.sn = 0; + + /* set 'NV' to 'notification vector' */ + new.nv = POSTED_INTR_VECTOR; + } while (cmpxchg64(&pi_desc->control, old.control, + new.control) != old.control); + + if(vcpu->pre_pcpu != -1) { + spin_lock_irqsave( + &per_cpu(blocked_vcpu_on_cpu_lock, + vcpu->pre_pcpu), flags); + list_del(&vcpu->blocked_vcpu_list); + spin_unlock_irqrestore( + &per_cpu(blocked_vcpu_on_cpu_lock, + vcpu->pre_pcpu), flags); + vcpu->pre_pcpu = -1; + } +} + /* * This routine does the following things for vCPU which is going * to be blocked if VT-d PI is enabled. @@ -11234,43 +11271,11 @@ static int vmx_pre_block(struct kvm_vcpu *vcpu) static void pi_post_block(struct kvm_vcpu *vcpu) { - struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); - struct pi_desc old, new; - unsigned int dest; - unsigned long flags; - if (!kvm_arch_has_assigned_device(vcpu->kvm) || !irq_remapping_cap(IRQ_POSTING_CAP)) return; - do { - old.control = new.control = pi_desc->control; - - dest = cpu_physical_id(vcpu->cpu); - - if (x2apic_enabled()) - new.ndst = dest; - else - new.ndst = (dest << 8) & 0xFF00; - - /* Allow posting non-urgent interrupts */ - new.sn = 0; - - /* set 'NV' to 'notification vector' */ - new.nv = POSTED_INTR_VECTOR; - } while (cmpxchg64(&pi_desc->control, old.control, - new.control) != old.control); - - if(vcpu->pre_pcpu != -1) { - spin_lock_irqsave( - &per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); - list_del(&vcpu->blocked_vcpu_list); - spin_unlock_irqrestore( - &per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); - vcpu->pre_pcpu = -1; - } + __pi_post_block(vcpu); } static void vmx_post_block(struct kvm_vcpu *vcpu) From patchwork Mon Jan 18 23:22:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ioanna Alifieraki X-Patchwork-Id: 1428337 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) 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 ozlabs.org (Postfix) with ESMTPS id 4DKSVn6xJYz9sWD; Tue, 19 Jan 2021 10:22:25 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l1dqo-0006qK-60; Mon, 18 Jan 2021 23:22:22 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqk-0006oL-Ey for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:18 +0000 Received: from mail-wr1-f72.google.com ([209.85.221.72]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqk-0005PV-6I for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:18 +0000 Received: by mail-wr1-f72.google.com with SMTP id u3so8927373wri.19 for ; Mon, 18 Jan 2021 15:22:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=leflLJ9iWn63OIWg8KBV449QXBXxtRxTB/ajDrvuA7g=; b=fdq19XFysX9/GagjmJyfUSBNPt0fEbNnIba1+RBvmtkizjqqgo6aZV7OnZSC43t70V 1KwTXPOxTY0Jq84ia1CfStOwcfud3MYl3sXkxrQqx1lMFJITCjJs2ummSaHdR781jALu 8RLeNHBzIyVroeXH85tavulfJwBNgy9vxG3QdZqGqh7kM7CGBrX4Kro1aLzcAZqv5sIr /GUr4mcfZkptjPxGWfOzpVmhnF0p2BE4auJdJl/V0mQc36deG4tuuK752PzwvGXoqLCc nk9yFW48mItIdAGgX/DZcFyrIT2LoaU5y8OCyLa5soUGYS7kDW0JiK6YEEzKhFaMIK0X +2qQ== X-Gm-Message-State: AOAM533sVcw2uhJvh354/imi/wQr6DDgA7tA758/ei9K+z+JCuDnGjoZ 8YUPjHULIAk4b4fjCVF755AKjIXCUNFY0/M3yZ+GXbh0wEmIXqflrmCQWtZU+x4HYoB/p7sis4Z Eq5sD+5KFZS9wjqqo8G5JIjeNsLXFUdwqcgIX7MD93A== X-Received: by 2002:a1c:740b:: with SMTP id p11mr1432043wmc.34.1611012137618; Mon, 18 Jan 2021 15:22:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJw0RAkNNTTY5d8f+lMsKUTGLUf8KTvAfS4VEijyzo0PG/f5M9931hQyQl5dPuCi6ldVGKOOjA== X-Received: by 2002:a1c:740b:: with SMTP id p11mr1432025wmc.34.1611012137359; Mon, 18 Jan 2021 15:22:17 -0800 (PST) Received: from localhost ([2001:67c:1560:8007::aac:c2e0]) by smtp.gmail.com with ESMTPSA id w18sm32401539wrn.2.2021.01.18.15.22.16 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jan 2021 15:22:16 -0800 (PST) From: Ioanna Alifieraki To: kernel-team@lists.ubuntu.com, ioanna-maria.alifieraki@canonical.com Subject: [SRU][Xenial][PATCH v3 3/4] KVM: VMX: avoid double list add with VT-d posted interrupts Date: Mon, 18 Jan 2021 23:22:07 +0000 Message-Id: <20210118232208.11178-4-ioanna-maria.alifieraki@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210118232208.11178-1-ioanna-maria.alifieraki@canonical.com> References: <20210118232208.11178-1-ioanna-maria.alifieraki@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/1908428 In some cases, for example involving hot-unplug of assigned devices, pi_post_block can forget to remove the vCPU from the blocked_vcpu_list. When this happens, the next call to pi_pre_block corrupts the list. Fix this in two ways. First, check vcpu->pre_pcpu in pi_pre_block and WARN instead of adding the element twice in the list. Second, always do the list removal in pi_post_block if vcpu->pre_pcpu is set (not -1). The new code keeps interrupts disabled for the whole duration of pi_pre_block/pi_post_block. This is not strictly necessary, but easier to follow. For the same reason, PI.ON is checked only after the cmpxchg, and to handle it we just call the post-block code. This removes duplication of the list removal code. Cc: Huangweidong Cc: Gonglei Cc: wangxin Cc: Radim Krčmář Tested-by: Longpeng (Mike) Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini (backported from commit 8b306e2f3c41939ea528e6174c88cfbfff893ce1) [hunk 5 : resolve if-statement conflict.] Signed-off-by: Ioanna Alifieraki --- arch/x86/kvm/vmx.c | 61 ++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 93d922a..a9d677c 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -11150,10 +11150,11 @@ static void __pi_post_block(struct kvm_vcpu *vcpu) struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); struct pi_desc old, new; unsigned int dest; - unsigned long flags; do { old.control = new.control = pi_desc->control; + WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, + "Wakeup handler not enabled while the VCPU is blocked\n"); dest = cpu_physical_id(vcpu->cpu); @@ -11170,14 +11171,10 @@ static void __pi_post_block(struct kvm_vcpu *vcpu) } while (cmpxchg64(&pi_desc->control, old.control, new.control) != old.control); - if(vcpu->pre_pcpu != -1) { - spin_lock_irqsave( - &per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); + if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { + spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); list_del(&vcpu->blocked_vcpu_list); - spin_unlock_irqrestore( - &per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); + spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); vcpu->pre_pcpu = -1; } } @@ -11197,7 +11194,6 @@ static void __pi_post_block(struct kvm_vcpu *vcpu) */ static int pi_pre_block(struct kvm_vcpu *vcpu) { - unsigned long flags; unsigned int dest; struct pi_desc old, new; struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); @@ -11206,34 +11202,20 @@ static int pi_pre_block(struct kvm_vcpu *vcpu) !irq_remapping_cap(IRQ_POSTING_CAP)) return 0; - vcpu->pre_pcpu = vcpu->cpu; - spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); - list_add_tail(&vcpu->blocked_vcpu_list, - &per_cpu(blocked_vcpu_on_cpu, - vcpu->pre_pcpu)); - spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); + WARN_ON(irqs_disabled()); + local_irq_disable(); + if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { + vcpu->pre_pcpu = vcpu->cpu; + spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); + list_add_tail(&vcpu->blocked_vcpu_list, + &per_cpu(blocked_vcpu_on_cpu, + vcpu->pre_pcpu)); + spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); + } do { old.control = new.control = pi_desc->control; - /* - * We should not block the vCPU if - * an interrupt is posted for it. - */ - if (pi_test_on(pi_desc) == 1) { - spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); - list_del(&vcpu->blocked_vcpu_list); - spin_unlock_irqrestore( - &per_cpu(blocked_vcpu_on_cpu_lock, - vcpu->pre_pcpu), flags); - vcpu->pre_pcpu = -1; - - return 1; - } - WARN((pi_desc->sn == 1), "Warning: SN field of posted-interrupts " "is set before blocking\n"); @@ -11258,7 +11240,12 @@ static int pi_pre_block(struct kvm_vcpu *vcpu) } while (cmpxchg64(&pi_desc->control, old.control, new.control) != old.control); - return 0; + /* We should not block the vCPU if an interrupt is posted for it. */ + if (pi_test_on(pi_desc) == 1) + __pi_post_block(vcpu); + + local_irq_enable(); + return (vcpu->pre_pcpu == -1); } static int vmx_pre_block(struct kvm_vcpu *vcpu) @@ -11271,11 +11258,13 @@ static int vmx_pre_block(struct kvm_vcpu *vcpu) static void pi_post_block(struct kvm_vcpu *vcpu) { - if (!kvm_arch_has_assigned_device(vcpu->kvm) || - !irq_remapping_cap(IRQ_POSTING_CAP)) + if (vcpu->pre_pcpu == -1) return; + WARN_ON(irqs_disabled()); + local_irq_disable(); __pi_post_block(vcpu); + local_irq_enable(); } static void vmx_post_block(struct kvm_vcpu *vcpu) From patchwork Mon Jan 18 23:22:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ioanna Alifieraki X-Patchwork-Id: 1428338 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) 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 ozlabs.org (Postfix) with ESMTPS id 4DKSVp4Xlbz9sWX; Tue, 19 Jan 2021 10:22:26 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l1dqp-0006r7-1f; Mon, 18 Jan 2021 23:22:23 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqm-0006p5-DT for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:20 +0000 Received: from mail-wm1-f69.google.com ([209.85.128.69]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l1dqm-0005Pm-5z for kernel-team@lists.ubuntu.com; Mon, 18 Jan 2021 23:22:20 +0000 Received: by mail-wm1-f69.google.com with SMTP id x20so5121781wmc.0 for ; Mon, 18 Jan 2021 15:22:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OrRrpGrsNvUrZ08fe0RgVzKzZGC8SbbvnactNJ1ItNI=; b=Hpi8DMAvT9HQmX1VDf3/bb0SGTgNfprtygTyKQHAx1dw5dGBERW/PZXXlrRVGScBiA otjPyn+cWGa0/sYyXOcURG4Ae5QVE1GjL88UxwDzgkBub8fDUkusqWVEtwmbre0pfk6B k647kapbAagXzTEmRG5pxbcyevlED4B9YXsyNtqmO0llmSQ3VOxnUkDjYPW1g/h1Rw+D 1Q65NDCXIEp2o7bhe6lv85qSXD0q3IBZhzSXS40qrA8ARsUyjsrW/tem/RqsBY9muEQo 8oCNtvPYCkXgbZnqDhjDjZVcjYi0lZHZSxD3r3teRx9gxqxqbHBDGtiaCovJ++6CpXwg 6WMQ== X-Gm-Message-State: AOAM533BZt1sGVt+82WL8uk0s3ERXwNjW38S1uBZVBMIfPTFhVINfzdK 6zVG8RmnZd4UBokT4owXUyI8EnxRMNOs7zn6vhUy7HE1U5Od9qxx95BfonTPAzdJqnFG3Xn7IC8 ycFu+ywOz2L0KPKvkfRhZOxubMa8/uIXm+A9fcyFnsg== X-Received: by 2002:adf:fb85:: with SMTP id a5mr1521643wrr.331.1611012139649; Mon, 18 Jan 2021 15:22:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJyBdJAlBhHcWPwl4eBVTIqknHkhD3G8OKXgSF0+mH7fIGPtAMGbFWdrWS0uS/XOgCNnNvz9zw== X-Received: by 2002:adf:fb85:: with SMTP id a5mr1521637wrr.331.1611012139429; Mon, 18 Jan 2021 15:22:19 -0800 (PST) Received: from localhost ([2001:67c:1560:8007::aac:c2e0]) by smtp.gmail.com with ESMTPSA id n6sm1476388wmi.23.2021.01.18.15.22.18 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 Jan 2021 15:22:18 -0800 (PST) From: Ioanna Alifieraki To: kernel-team@lists.ubuntu.com, ioanna-maria.alifieraki@canonical.com Subject: [SRU][Xenial][PATCH v3 4/4] KVM: VMX: simplify and fix vmx_vcpu_pi_load Date: Mon, 18 Jan 2021 23:22:08 +0000 Message-Id: <20210118232208.11178-5-ioanna-maria.alifieraki@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210118232208.11178-1-ioanna-maria.alifieraki@canonical.com> References: <20210118232208.11178-1-ioanna-maria.alifieraki@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/1908428 The simplify part: do not touch pi_desc.nv, we can set it when the VCPU is first created. Likewise, pi_desc.sn is only handled by vmx_vcpu_pi_load, do not touch it in __pi_post_block. The fix part: do not check kvm_arch_has_assigned_device, instead check the SN bit to figure out whether vmx_vcpu_pi_put ran before. This matches what the previous patch did in pi_post_block. Cc: Huangweidong Cc: Gonglei Cc: wangxin Cc: Radim Krčmář Tested-by: Longpeng (Mike) Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini (backported from commit 31afb2ea2b10a7d17ce3db4cdb0a12b63b2fe08a) [hunk 1 : resolve if-statement conflict.] Signed-off-by: Ioanna Alifieraki --- arch/x86/kvm/vmx.c | 67 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index a9d677c..4a56bf3 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2235,42 +2235,41 @@ static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) struct pi_desc old, new; unsigned int dest; - if (!kvm_arch_has_assigned_device(vcpu->kvm) || - !irq_remapping_cap(IRQ_POSTING_CAP)) + /* + * In case of hot-plug or hot-unplug, we may have to undo + * vmx_vcpu_pi_put even if there is no assigned device. And we + * always keep PI.NDST up to date for simplicity: it makes the + * code easier, and CPU migration is not a fast path. + */ + 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; - /* - * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there - * are two possible cases: - * 1. After running 'pre_block', context switch - * happened. For this case, 'sn' was set in - * vmx_vcpu_put(), so we need to clear it here. - * 2. After running 'pre_block', we were blocked, - * and woken up by some other guy. For this case, - * we don't need to do anything, 'pi_post_block' - * will do everything for us. However, we cannot - * check whether it is case #1 or case #2 here - * (maybe, not needed), so we also clear sn here, - * I think it is not a big deal. - */ - if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) { - if (vcpu->cpu != cpu) { - dest = cpu_physical_id(cpu); - - if (x2apic_enabled()) - new.ndst = dest; - else - new.ndst = (dest << 8) & 0xFF00; - } + dest = cpu_physical_id(cpu); - /* set 'NV' to 'notification vector' */ - new.nv = POSTED_INTR_VECTOR; - } + if (x2apic_enabled()) + new.ndst = dest; + else + new.ndst = (dest << 8) & 0xFF00; - /* Allow posting non-urgent interrupts */ new.sn = 0; } while (cmpxchg64(&pi_desc->control, old.control, new.control) != old.control); @@ -9283,6 +9282,13 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) vmx->nested.current_vmptr = -1ull; vmx->nested.current_vmcs12 = NULL; + /* + * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR + * or POSTED_INTR_WAKEUP_VECTOR. + */ + vmx->pi_desc.nv = POSTED_INTR_VECTOR; + vmx->pi_desc.sn = 1; + return &vmx->vcpu; free_vmcs: @@ -11163,9 +11169,6 @@ static void __pi_post_block(struct kvm_vcpu *vcpu) else new.ndst = (dest << 8) & 0xFF00; - /* Allow posting non-urgent interrupts */ - new.sn = 0; - /* set 'NV' to 'notification vector' */ new.nv = POSTED_INTR_VECTOR; } while (cmpxchg64(&pi_desc->control, old.control,