Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2917745pxb; Fri, 8 Oct 2021 19:14:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxC+K5N3zNc7mP3J8l+YqP4UAye1V5xGIz5ceDs2ETZzoTOilDJpn+Ftsj+ZF/H4l7/bkdd X-Received: by 2002:a05:6402:1908:: with SMTP id e8mr7493476edz.252.1633745674646; Fri, 08 Oct 2021 19:14:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633745674; cv=none; d=google.com; s=arc-20160816; b=CWaPamT6whro787/HJnFuB+5TTekA81Xgftlp1PRo3+/KnpdJYkk212sl87XMkX4O/ M4zVwj9uhUFU9h7IPhw/t436Y8TUIIYzO3D0YAJ9p6FXrSfLtEReKKgemV3c7ub+FjN2 mYFwFSsZqLLjM3FliTPjxgCTU+TwYxBjoKv68sOAstMkoCgQSXj79YwzfF821lQECXnc QW5Pa1FlViqR/LKQ8narODdBgxXLMr3BrSRah7EUx09xl3a//BmfH944xbX0EJ2R9k3f iUOHjJHAjnHucQYnoQ96yIjkv7Z+58ZuEKGCT/nEilX1w9DRTOWkJQakO54aQ2xFvM78 qs7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=79LIBqpnbdhRmfT7kSoV1WyIctghtEtxlcdZEfLixOI=; b=tGDZvoS9QY6l4S/shbz1KBq4vPMjqk3ql4LsvzE92rKJ0YCkKHLRVKyPJvRhzlvRaH vHC63TcEAs4eQkFpKgcEcCSiHfLLelczGHnqVwnsbvowyJSeYbF4Z4/HdYzZvgUOGPxP 0ss1LiIgdPj8fJD8GzLGaJL+CQVAZVOuA5D30h9B7NjkiV3+QT6+LepK8fsoMeeoLrYp 4yGIt1wSEHesG6MFpaA9EQShSGgjbYDNyZWs4Ldwo9mhP9kcydO7ssAQjUxOxX94okZN yjdltGd7/f1S/ngMz3FPmoF0b+d4IahZOtRbfPxTm+slaZI2Z9+cqexg+AHBLo76dIk3 9TFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="td6Jp/Qo"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kl24si1911696ejc.190.2021.10.08.19.14.11; Fri, 08 Oct 2021 19:14:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="td6Jp/Qo"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244164AbhJICOp (ORCPT + 99 others); Fri, 8 Oct 2021 22:14:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244161AbhJICOn (ORCPT ); Fri, 8 Oct 2021 22:14:43 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64FDEC061570 for ; Fri, 8 Oct 2021 19:12:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id f8-20020a2585480000b02905937897e3daso15197235ybn.2 for ; Fri, 08 Oct 2021 19:12:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=79LIBqpnbdhRmfT7kSoV1WyIctghtEtxlcdZEfLixOI=; b=td6Jp/Qoz+WOt+cvb/jSNSqRd38PBqxXjUOlkCeRRmF+Xq32DT9663UFIERzbBP+7k sIaPHTm4Vgj+KHKuAZRxJtlQiFNwnW7Gr6Np/g8/gHiZEP/vq1b8eXJehgPH0k67g74g Eejc31bexmjXOP0iVOhiJxMHqh3sykybQ5VDUN0mBT1X+xmhe4U8/hRuzrlGW28cUSb9 g22fsln1P8HNH27yLN8eZT01CH6+UAgE5LZNQQheC1aFigNp8cVDpq7HbECp3WAvt9tG GqRirzTAEB5G45ET1seL/TOuwe+ZOUYFxzi+NEQTSlGInnVjBGl4BhooEPA1Uoist32O 48qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=79LIBqpnbdhRmfT7kSoV1WyIctghtEtxlcdZEfLixOI=; b=XNXULV+6P+w3Sf66CFByGx/RPeT1uxdHSBZ7g068F+11VP+tUokqilN2OwIdJDNH3u mFWiwKlyjyeQWIv3X5lf5sJNaTp5rXKMvwP97PSh/uHAp1jxu9d/XgVwzu4P19EbedFJ kfOtiARHhS2Z1HxWNrLX7nGp8fIlCpe19zIMuy3kEXr9lNBXh/0lJmyI1nzkjH0BoMUH whZfUVt/bcT8Yl/a2EvCZjpxEFcXzyWgGZIJ5Yve6efVBDVOFm82dYhtEK9WURZz1pDY 0orqZMETjyzQEvtpRSFeDcI56AcHyNUODFyXnZ7NDdJtg16nsVKgqQ60EOdlwoM8sGtE Efjw== X-Gm-Message-State: AOAM532gk18V2Ghvw6e8E8ICkmNL4tmyEz/2ytkKYe1/2dgYBK7/Qdi/ 1t+UQgnxh4+a5yQrhqOvFpzVRzmvB6k= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:e39b:6333:b001:cb]) (user=seanjc job=sendgmr) by 2002:a25:bd03:: with SMTP id f3mr6935305ybk.412.1633745566669; Fri, 08 Oct 2021 19:12:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 Oct 2021 19:11:55 -0700 In-Reply-To: <20211009021236.4122790-1-seanjc@google.com> Message-Id: <20211009021236.4122790-3-seanjc@google.com> Mime-Version: 1.0 References: <20211009021236.4122790-1-seanjc@google.com> X-Mailer: git-send-email 2.33.0.882.g93a45727a2-goog Subject: [PATCH v2 02/43] KVM: SVM: Ensure target pCPU is read once when signalling AVIC doorbell From: Sean Christopherson To: 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 , Sean Christopherson , 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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); -- 2.33.0.882.g93a45727a2-goog