From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12D92C433F5 for ; Wed, 27 Oct 2021 09:51:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E3B216103C for ; Wed, 27 Oct 2021 09:51:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241285AbhJ0Jxx (ORCPT ); Wed, 27 Oct 2021 05:53:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43540 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238828AbhJ0Jxs (ORCPT ); Wed, 27 Oct 2021 05:53:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635328282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OLKZI82hmPisWACp/QMd/03BQs+RHKrDBliqptWWYzE=; b=JlGCLXib6mP67nqrMVKEdro08E7es/nLdFS9D05/GNFiNHY9ph7aXU44AK/fU+Dc/IGRMP s1/6NQ0khHHN6yWVIGN7Mar4ykVEAYFpyoZICsyvfOTvkwAwHCz3AOpA2RuUuWUi00+Ck7 WdpGR1IytQ8EAYqoQ3leHhSI7Y9SORs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-4gOjCQ60OwesHHSXcF-XSQ-1; Wed, 27 Oct 2021 05:51:19 -0400 X-MC-Unique: 4gOjCQ60OwesHHSXcF-XSQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD15980A5C0; Wed, 27 Oct 2021 09:51:15 +0000 (UTC) Received: from starship (unknown [10.40.194.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 401B019D9F; Wed, 27 Oct 2021 09:50:31 +0000 (UTC) Message-ID: Subject: Re: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell From: Maxim Levitsky To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Date: Wed, 27 Oct 2021 12:50:30 +0300 In-Reply-To: <20211009021236.4122790-3-seanjc@google.com> References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-3-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2021-10-08 at 19:11 -0700, Sean Christopherson wrote: > Ensure vcpu->cpu is read once when signalling the AVIC doorbell. If the > compiler rereads the field and the vCPU is migrated between the check and > writing the doorbell, KVM would signal the wrong physical CPU. Since vcpu->cpu can change any moment anyway, adding READ_ONCE I think can't really fix anything but I do agree that it makes this more readable. Reviewed-by: Maxim Levitsky > > Functionally, signalling the wrong CPU in this case is not an issue as > task migration means the vCPU has exited and will pick up any pending > interrupts on the next VMRUN. Add the READ_ONCE() purely to clean up the > code. > > Opportunistically add a comment explaining the task migration behavior, > and rename cpuid=>cpu to avoid conflating the CPU number with KVM's more > common usage of CPUID. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 8052d92069e0..208c5c71e827 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -675,10 +675,17 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > smp_mb__after_atomic(); > > if (avic_vcpu_is_running(vcpu)) { > - int cpuid = vcpu->cpu; > + int cpu = READ_ONCE(vcpu->cpu); > > - if (cpuid != get_cpu()) > - wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); > + /* > + * Note, the vCPU could get migrated to a different pCPU at any > + * point, which could result in signalling the wrong/previous > + * pCPU. But if that happens the vCPU is guaranteed to do a > + * VMRUN (after being migrated) and thus will process pending > + * interrupts, i.e. a doorbell is not needed (and the spurious) > + */ > + if (cpu != get_cpu()) > + wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > put_cpu(); > } else > kvm_vcpu_wake_up(vcpu); From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9007DC433EF for ; Wed, 27 Oct 2021 09:54:12 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5247760F21 for ; Wed, 27 Oct 2021 09:54:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5247760F21 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Date:Cc:To:From:Subject:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=p06viguoOcwgLru5o3LUDQGhVzkb6DB0Um6lnpGtk4Y=; b=yVxDI0UBzua4ZQ IVhtrsOtb16shVe0vTIcGJ4IMwooCr/+EsH4GpBgnIx1eWe5IX5B0QyJep2fLuoM8E0iTKPhKN7hG 0/yzbqOkcHbFLDMmRzk5NPzjkSSWqFmCjGpy3XE0COsL1Afyj6HajUD7BUdmVg31L7sIy/CqPI0JI +XYxKFwZN7KFuOYR2t6nbwlH/MQThzNV4ls4L5Tl+5T+/0uO6uRRjscale0KsBOTFS12u+57gn0Bw b9gWEOhTRj/JlCpgLSPyrkHhMcqCBlPXyBs9ku/vKLpomMJHbdk+jBTa/zfmmBTiuU6rw6404pnUj azyJGNzuNiVD6TqewyTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mffdE-004V0k-2C; Wed, 27 Oct 2021 09:54:04 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mffdB-004UyK-ET for linux-riscv@lists.infradead.org; Wed, 27 Oct 2021 09:54:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635328440; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OLKZI82hmPisWACp/QMd/03BQs+RHKrDBliqptWWYzE=; b=QdaTAUfDJ9+mnQF8+RnK5RQMyKSX+xPPdiFHFLeKeNnMhyaOyg8DnNmqaH644PWXaIlBKq 30WBuSOl/+kpAGfjUJci96z0pgGj5vmH1kHgeR0509WA/uhDoy8IOYiDuiVaRqd3qOV0Gy YRzQ8mH7WsB5Iy+EAaIGqXVcumvwB6E= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-4gOjCQ60OwesHHSXcF-XSQ-1; Wed, 27 Oct 2021 05:51:19 -0400 X-MC-Unique: 4gOjCQ60OwesHHSXcF-XSQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD15980A5C0; Wed, 27 Oct 2021 09:51:15 +0000 (UTC) Received: from starship (unknown [10.40.194.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 401B019D9F; Wed, 27 Oct 2021 09:50:31 +0000 (UTC) Message-ID: Subject: Re: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell From: Maxim Levitsky To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Date: Wed, 27 Oct 2021 12:50:30 +0300 In-Reply-To: <20211009021236.4122790-3-seanjc@google.com> References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-3-seanjc@google.com> User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211027_025401_635938_EC3A3952 X-CRM114-Status: GOOD ( 25.67 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On Fri, 2021-10-08 at 19:11 -0700, Sean Christopherson wrote: > Ensure vcpu->cpu is read once when signalling the AVIC doorbell. If the > compiler rereads the field and the vCPU is migrated between the check and > writing the doorbell, KVM would signal the wrong physical CPU. Since vcpu->cpu can change any moment anyway, adding READ_ONCE I think can't really fix anything but I do agree that it makes this more readable. Reviewed-by: Maxim Levitsky > > Functionally, signalling the wrong CPU in this case is not an issue as > task migration means the vCPU has exited and will pick up any pending > interrupts on the next VMRUN. Add the READ_ONCE() purely to clean up the > code. > > Opportunistically add a comment explaining the task migration behavior, > and rename cpuid=>cpu to avoid conflating the CPU number with KVM's more > common usage of CPUID. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 8052d92069e0..208c5c71e827 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -675,10 +675,17 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > smp_mb__after_atomic(); > > if (avic_vcpu_is_running(vcpu)) { > - int cpuid = vcpu->cpu; > + int cpu = READ_ONCE(vcpu->cpu); > > - if (cpuid != get_cpu()) > - wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); > + /* > + * Note, the vCPU could get migrated to a different pCPU at any > + * point, which could result in signalling the wrong/previous > + * pCPU. But if that happens the vCPU is guaranteed to do a > + * VMRUN (after being migrated) and thus will process pending > + * interrupts, i.e. a doorbell is not needed (and the spurious) > + */ > + if (cpu != get_cpu()) > + wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > put_cpu(); > } else > kvm_vcpu_wake_up(vcpu); _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80C02C433FE for ; Wed, 27 Oct 2021 09:54:28 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 472F161052 for ; Wed, 27 Oct 2021 09:54:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 472F161052 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Date:Cc:To:From:Subject:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QGle6RkOLePuAaXd/2NqHpAxoZ5fMkNnQYchFX58Hyw=; b=JykhSIIbnRARkP lT9gn20f8vDujBXttKTNOfSTs6NTcXSNXiMAgx30sz2VIxrj2kQ+6OaIApmO1+3o0juYQVkUTxkT0 /3+86hey43b993PPAjiyfAMvv/OkrsOOvXaR9a9yBaKBvrHnXSUbUVZYkIOok8czaI9kbzcnSGFKU BxANNF7aTqHDsGcTQ2YDj/mz352iJjARSMbcyzFuClYXTd3CH56MjAHj5JF1iT4vBdAHGVmijW5EY NBZokjpm6BI7TgJmIaoALcqUEt5L7kKhYXt+neawfc8LAQ7VYpKISk3UOV7FmItHEKqHidb05ejwo MNaZNIiiYovBFCvP/QGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mffcO-004UXJ-Gm; Wed, 27 Oct 2021 09:53:12 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mffcK-004UWQ-Mx for linux-arm-kernel@lists.infradead.org; Wed, 27 Oct 2021 09:53:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635328386; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OLKZI82hmPisWACp/QMd/03BQs+RHKrDBliqptWWYzE=; b=Z7tEqKlC94ELam/5K3DwX93JiwpgZqZ4xAmCSq355Ckanha8p35EerQZKBDyAwyf9BkCzg w8Xrnb+tS8VvKZKCjt3fXhC77Zyu4tLCvZmUDXHfqnneIdAkAmrOkWT4M53Syr6TD9Ra/9 chjgCNQCLK7sV/si0VCgGxZd8adSbXU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-4gOjCQ60OwesHHSXcF-XSQ-1; Wed, 27 Oct 2021 05:51:19 -0400 X-MC-Unique: 4gOjCQ60OwesHHSXcF-XSQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD15980A5C0; Wed, 27 Oct 2021 09:51:15 +0000 (UTC) Received: from starship (unknown [10.40.194.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 401B019D9F; Wed, 27 Oct 2021 09:50:31 +0000 (UTC) Message-ID: Subject: Re: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell From: Maxim Levitsky To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Date: Wed, 27 Oct 2021 12:50:30 +0300 In-Reply-To: <20211009021236.4122790-3-seanjc@google.com> References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-3-seanjc@google.com> User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211027_025308_936792_7F804B41 X-CRM114-Status: GOOD ( 26.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 2021-10-08 at 19:11 -0700, Sean Christopherson wrote: > Ensure vcpu->cpu is read once when signalling the AVIC doorbell. If the > compiler rereads the field and the vCPU is migrated between the check and > writing the doorbell, KVM would signal the wrong physical CPU. Since vcpu->cpu can change any moment anyway, adding READ_ONCE I think can't really fix anything but I do agree that it makes this more readable. Reviewed-by: Maxim Levitsky > > Functionally, signalling the wrong CPU in this case is not an issue as > task migration means the vCPU has exited and will pick up any pending > interrupts on the next VMRUN. Add the READ_ONCE() purely to clean up the > code. > > Opportunistically add a comment explaining the task migration behavior, > and rename cpuid=>cpu to avoid conflating the CPU number with KVM's more > common usage of CPUID. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 8052d92069e0..208c5c71e827 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -675,10 +675,17 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > smp_mb__after_atomic(); > > if (avic_vcpu_is_running(vcpu)) { > - int cpuid = vcpu->cpu; > + int cpu = READ_ONCE(vcpu->cpu); > > - if (cpuid != get_cpu()) > - wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); > + /* > + * Note, the vCPU could get migrated to a different pCPU at any > + * point, which could result in signalling the wrong/previous > + * pCPU. But if that happens the vCPU is guaranteed to do a > + * VMRUN (after being migrated) and thus will process pending > + * interrupts, i.e. a doorbell is not needed (and the spurious) > + */ > + if (cpu != get_cpu()) > + wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > put_cpu(); > } else > kvm_vcpu_wake_up(vcpu); _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F620C433F5 for ; Wed, 27 Oct 2021 10:49:45 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 891C260F6F for ; Wed, 27 Oct 2021 10:49:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 891C260F6F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id EFCEA4B176; Wed, 27 Oct 2021 06:49:43 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@redhat.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 14JzWRcVwux5; Wed, 27 Oct 2021 06:49:42 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 525834B09C; Wed, 27 Oct 2021 06:49:42 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 157BB4A534 for ; Wed, 27 Oct 2021 05:51:24 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id o67L-Bx9wsAT for ; Wed, 27 Oct 2021 05:51:23 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 11281406E7 for ; Wed, 27 Oct 2021 05:51:23 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635328282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OLKZI82hmPisWACp/QMd/03BQs+RHKrDBliqptWWYzE=; b=JlGCLXib6mP67nqrMVKEdro08E7es/nLdFS9D05/GNFiNHY9ph7aXU44AK/fU+Dc/IGRMP s1/6NQ0khHHN6yWVIGN7Mar4ykVEAYFpyoZICsyvfOTvkwAwHCz3AOpA2RuUuWUi00+Ck7 WdpGR1IytQ8EAYqoQ3leHhSI7Y9SORs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-4gOjCQ60OwesHHSXcF-XSQ-1; Wed, 27 Oct 2021 05:51:19 -0400 X-MC-Unique: 4gOjCQ60OwesHHSXcF-XSQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AD15980A5C0; Wed, 27 Oct 2021 09:51:15 +0000 (UTC) Received: from starship (unknown [10.40.194.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id 401B019D9F; Wed, 27 Oct 2021 09:50:31 +0000 (UTC) Message-ID: Subject: Re: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell From: Maxim Levitsky To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Date: Wed, 27 Oct 2021 12:50:30 +0300 In-Reply-To: <20211009021236.4122790-3-seanjc@google.com> References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-3-seanjc@google.com> User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mailman-Approved-At: Wed, 27 Oct 2021 06:49:40 -0400 Cc: Wanpeng Li , kvm@vger.kernel.org, David Hildenbrand , linux-kernel@vger.kernel.org, Atish Patra , linux-riscv@lists.infradead.org, Claudio Imbrenda , kvmarm@lists.cs.columbia.edu, Joerg Roedel , kvm-ppc@vger.kernel.org, David Matlack , linux-arm-kernel@lists.infradead.org, Jim Mattson , Cornelia Huck , linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, Vitaly Kuznetsov X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Fri, 2021-10-08 at 19:11 -0700, Sean Christopherson wrote: > Ensure vcpu->cpu is read once when signalling the AVIC doorbell. If the > compiler rereads the field and the vCPU is migrated between the check and > writing the doorbell, KVM would signal the wrong physical CPU. Since vcpu->cpu can change any moment anyway, adding READ_ONCE I think can't really fix anything but I do agree that it makes this more readable. Reviewed-by: Maxim Levitsky > > Functionally, signalling the wrong CPU in this case is not an issue as > task migration means the vCPU has exited and will pick up any pending > interrupts on the next VMRUN. Add the READ_ONCE() purely to clean up the > code. > > Opportunistically add a comment explaining the task migration behavior, > and rename cpuid=>cpu to avoid conflating the CPU number with KVM's more > common usage of CPUID. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 8052d92069e0..208c5c71e827 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -675,10 +675,17 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > smp_mb__after_atomic(); > > if (avic_vcpu_is_running(vcpu)) { > - int cpuid = vcpu->cpu; > + int cpu = READ_ONCE(vcpu->cpu); > > - if (cpuid != get_cpu()) > - wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); > + /* > + * Note, the vCPU could get migrated to a different pCPU at any > + * point, which could result in signalling the wrong/previous > + * pCPU. But if that happens the vCPU is guaranteed to do a > + * VMRUN (after being migrated) and thus will process pending > + * interrupts, i.e. a doorbell is not needed (and the spurious) > + */ > + if (cpu != get_cpu()) > + wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > put_cpu(); > } else > kvm_vcpu_wake_up(vcpu); _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 From: Maxim Levitsky Date: Wed, 27 Oct 2021 09:50:30 +0000 Subject: Re: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell Message-Id: List-Id: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-3-seanjc@google.com> In-Reply-To: <20211009021236.4122790-3-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang On Fri, 2021-10-08 at 19:11 -0700, Sean Christopherson wrote: > Ensure vcpu->cpu is read once when signalling the AVIC doorbell. If the > compiler rereads the field and the vCPU is migrated between the check and > writing the doorbell, KVM would signal the wrong physical CPU. Since vcpu->cpu can change any moment anyway, adding READ_ONCE I think can't really fix anything but I do agree that it makes this more readable. Reviewed-by: Maxim Levitsky > > Functionally, signalling the wrong CPU in this case is not an issue as > task migration means the vCPU has exited and will pick up any pending > interrupts on the next VMRUN. Add the READ_ONCE() purely to clean up the > code. > > Opportunistically add a comment explaining the task migration behavior, > and rename cpuid=>cpu to avoid conflating the CPU number with KVM's more > common usage of CPUID. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/svm/avic.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c > index 8052d92069e0..208c5c71e827 100644 > --- a/arch/x86/kvm/svm/avic.c > +++ b/arch/x86/kvm/svm/avic.c > @@ -675,10 +675,17 @@ int svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec) > smp_mb__after_atomic(); > > if (avic_vcpu_is_running(vcpu)) { > - int cpuid = vcpu->cpu; > + int cpu = READ_ONCE(vcpu->cpu); > > - if (cpuid != get_cpu()) > - wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpuid)); > + /* > + * Note, the vCPU could get migrated to a different pCPU at any > + * point, which could result in signalling the wrong/previous > + * pCPU. But if that happens the vCPU is guaranteed to do a > + * VMRUN (after being migrated) and thus will process pending > + * interrupts, i.e. a doorbell is not needed (and the spurious) > + */ > + if (cpu != get_cpu()) > + wrmsrl(SVM_AVIC_DOORBELL, kvm_cpu_get_apicid(cpu)); > put_cpu(); > } else > kvm_vcpu_wake_up(vcpu);