Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp5950805ybi; Wed, 31 Jul 2019 06:01:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqxklIxPCqUocDobYBl/H971GeW6K4+oW86UE66MsiOWZc1U0tArCwLwWSt/tbQGGIogQIVy X-Received: by 2002:a17:902:aa03:: with SMTP id be3mr120077890plb.240.1564578069638; Wed, 31 Jul 2019 06:01:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564578069; cv=none; d=google.com; s=arc-20160816; b=NcJFatidy8I+bAkj/UZHVxYxfqwSf9jnl9ZiJTSC2/NFTorlGSMoYL75V0750IGZml WwvS/zs+b+22CjCBT5bUZQsiBiyzOVMngaA1+bbc0rJ3Ev/91fBbWTJUbUUGjjvBx19U vxy8Xt0z7i71LdCp+zh4TZeGTNVctsJftMzwm8YI2l2Omqg4KL+WMeudc+/KrGFW8EAe fTMv/P+M0GceSUVPU+aq8rWltqbD7uZlvbjzLePRsCthsL/sFK2R5tpv5rb08ZIFrrwY KU+m9uNKICsIANemlqt1SMvkPDaXTh8cU3UtltBb0WabqZ2x4ZCzJy113sodfVsFHUAk tkmg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:openpgp:from:references:cc:to:subject; bh=inYUgJMClSBxeCZ5ua9oZG9WtRjGPUuf4CzpS2AwxKs=; b=097ckBBJds4TW2jyPw40yMqLJa0bdjr68EeSaog20sSr/RxnlMUZptNrlM0QDi/x+P Ha3P/SiZZvmXHenckU5fa8ch7sY5Bl9mPbGPBg8Oe/1o/xr+FhkjKl44beX1M9xmF4xX 9HhZzyzT1SmSyVM7k3KeXIPNCUKtUcAGpMlFwMwBtfC/lHfCKrsPzJ5+CAVPjhJiimE6 l9GlnmvkfhBRa/DFeNdFkaIe4a5LyE6zsvDGs7S7HYZV4w6Q8sCYJeCN6mtKGS59Irbb a7cx+KbmtrNrpOsbMRRa1xC6G24r0252QeR/7dS3z7nkQFD2f8w6rEY+AAHCKW77gR6x IjyA== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t31si1563787pjb.25.2019.07.31.06.00.54; Wed, 31 Jul 2019 06:01:09 -0700 (PDT) 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387830AbfGaMzg (ORCPT + 99 others); Wed, 31 Jul 2019 08:55:36 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:46239 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728653AbfGaMzg (ORCPT ); Wed, 31 Jul 2019 08:55:36 -0400 Received: by mail-wr1-f68.google.com with SMTP id z1so69538654wru.13 for ; Wed, 31 Jul 2019 05:55:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=inYUgJMClSBxeCZ5ua9oZG9WtRjGPUuf4CzpS2AwxKs=; b=Cp+E9D90boyAPv9wPriJLEqubSgiOPSL8giyKXc0jn39u1w/P9BrBQTnjAVDZAXZ0W mzyN7AFR7sTDFnIjDkKA8D2fbbvqAjBKvb9/5X+lL+IgiCIGDfsaQMlefDX57B48QVt0 YuUUDtLC9uHgFytxZX2jvj34qrceTW/3YaATISpjjq5PaHn3l73fEG986qfux37GW5KH rrlL04ckQ66x2t2yUbFt6jGzK0aoyL9SJA9YwiUHHLLsB3tPDijy61Q3+zYLOvcxijKa sLG3ZERrtwvomnzvIcayC/Qsbr6p8Pn96PvG4FbTW9GUH2xYQ2DVl9qAWi9ptVMqVqPJ x5Dw== X-Gm-Message-State: APjAAAVipU71jyUmvgcKwdvD2ept1CLHolVr/XSOqZEoTnvipv2S9JBN N2WlmHCqsBzmdc9oxoBexyq++w== X-Received: by 2002:a05:6000:118a:: with SMTP id g10mr18463602wrx.175.1564577734089; Wed, 31 Jul 2019 05:55:34 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:91e7:65e:d8cd:fdb3? ([2001:b07:6468:f312:91e7:65e:d8cd:fdb3]) by smtp.gmail.com with ESMTPSA id g19sm128484284wrb.52.2019.07.31.05.55.33 (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 05:55:33 -0700 (PDT) Subject: Re: [PATCH v2 3/3] KVM: Fix leak vCPU's VMCS value into other pCPU To: Wanpeng Li , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , stable@vger.kernel.org, Marc Zyngier , Christian Borntraeger References: <1564572438-15518-3-git-send-email-wanpengli@tencent.com> <1564573198-16219-1-git-send-email-wanpengli@tencent.com> From: Paolo Bonzini Openpgp: preference=signencrypt Message-ID: <9240ada8-8e18-d2b2-006e-41ededb89efb@redhat.com> Date: Wed, 31 Jul 2019 14:55:33 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <1564573198-16219-1-git-send-email-wanpengli@tencent.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 31/07/19 13:39, Wanpeng Li wrote: > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index ed061d8..12f2c91 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -2506,7 +2506,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) > continue; > if (vcpu == me) > continue; > - if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu)) > + if (READ_ONCE(vcpu->preempted) && swait_active(&vcpu->wq)) > continue; > if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && > !kvm_arch_vcpu_in_kernel(vcpu)) > This cannot work. swait_active means you are waiting, so you cannot be involuntarily preempted. The problem here is simply that kvm_vcpu_has_events is being called without holding the lock. So kvm_arch_vcpu_runnable is okay, it's the implementation that's wrong. Just rename the existing function to just vcpu_runnable and make a new arch callback kvm_arch_dy_runnable. kvm_arch_dy_runnable can be conservative and only returns true for a subset of events, in particular for x86 it can check: - vcpu->arch.pv.pv_unhalted - KVM_REQ_NMI or KVM_REQ_SMI or KVM_REQ_EVENT - PIR.ON if APICv is set Ultimately, all variables accessed in kvm_arch_dy_runnable should be accessed with READ_ONCE or atomic_read. And for all architectures, kvm_vcpu_on_spin should check list_empty_careful(&vcpu->async_pf.done) It's okay if your patch renames the function in non-x86 architectures, leaving the fix to maintainers. So, let's CC Marc and Christian since ARM and s390 have pretty complex kvm_arch_vcpu_runnable as well. Paolo