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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D580C43219 for ; Wed, 12 Jan 2022 18:10:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356014AbiALSK1 (ORCPT ); Wed, 12 Jan 2022 13:10:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356070AbiALSJ1 (ORCPT ); Wed, 12 Jan 2022 13:09:27 -0500 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C96BC061748 for ; Wed, 12 Jan 2022 10:08:44 -0800 (PST) Received: by mail-yb1-xb33.google.com with SMTP id g14so7897843ybs.8 for ; Wed, 12 Jan 2022 10:08:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=eLoe+yCOLX8twUoVnJSmKEThBpM9HQQFjJ/YCsOmEqo=; b=sM2pOGw+VpV9fEIP1NjSY4EwV7AcQhlq5kQpevs1vpJUY1/wqvZGO14UsjVSIGi4KO 0xFyyWnKF8RnRiQ9QTjCBqspBtCwUaJMbb1JnyobjHfOw7HAOB45fyPrbx6ULMES3QfA 1K+5NVv6/9/HPmtYoPxk7tPP+UAka3Gns3nyHc7K3xrpJHSngSgARkxqZMQ47npXn644 DE9+7odqYt8BstnTgUifJbQ7/R9av7njwqtwSCbDdmjN753efTawRlUW5KocyfCemjeW pPHXtnlznKcgGaSD30YnQph+Ju3ocJHis1S1NlIQFcHPcVRynTbi1J386LRNrmp+S+zE ukWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=eLoe+yCOLX8twUoVnJSmKEThBpM9HQQFjJ/YCsOmEqo=; b=nu6PV8dM3Ds7DEqXzj/Y0v71+wgMtoozifLU8wsUlFFqypwgDJ/5x7xFkvI9jwBXdn aupYLY2V2TncJYMSCjkJzmufJvAu22F/0TUkQoII8QfKjXwUXvsrPy6b/7J68aw4Rv3n QJUnxNBM2LnqsFQVioCrWMTO8bj4wA80EQ1QErBzQYqd8guj6vzMaQ8O/Z0JFOH/z1Mh UVw/8CF3UnPrJpDvgLINirX/BQA369gPIowe5xguVk8d1Ubi4bUvmBvbvyuzxJk8YjNv Wsl8zP2jkgzfNyKi64UIamhvEPjsv/gwheRWA5wfxRp3kVWZVWjzuXPpmp3m6Q1x0f7b RkWQ== X-Gm-Message-State: AOAM531bDenzH/WLa4mpuB68vgWu8W5DDo3ZNoBhBxLWBH1ZIIWJFd7D KVwNZQMtMil4m2bOqy64K4remoi31odpXn5OGOgQ1A== X-Google-Smtp-Source: ABdhPJyOENQ36F72MCUSVYL6GkdmncC92a9wV1s6msf5ZpTvvHnphmMhSYKEkA/XtQw4bc2lP+lyOA6otSG7YAZ1iDM= X-Received: by 2002:a25:d750:: with SMTP id o77mr1052699ybg.543.1642010923523; Wed, 12 Jan 2022 10:08:43 -0800 (PST) MIME-Version: 1.0 References: <20220104194918.373612-1-rananta@google.com> <20220104194918.373612-2-rananta@google.com> In-Reply-To: From: Raghavendra Rao Ananta Date: Wed, 12 Jan 2022 10:08:32 -0800 Message-ID: Subject: Re: [RFC PATCH v3 01/11] KVM: Capture VM start To: Sean Christopherson Cc: Marc Zyngier , Andrew Jones , James Morse , Alexandru Elisei , Suzuki K Poulose , kvm@vger.kernel.org, Catalin Marinas , Peter Shier , linux-kernel@vger.kernel.org, Paolo Bonzini , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 11, 2022 at 11:04 AM Sean Christopherson wrote: > > On Tue, Jan 11, 2022, Raghavendra Rao Ananta wrote: > > On Tue, Jan 11, 2022 at 9:36 AM Sean Christopherson wrote: > > > In your proposed patch, KVM_RUN will take kvm->lock _every_ time. That introduces > > > unnecessary contention as it will serialize this bit of code if multiple vCPUs > > > are attempting KVM_RUN. By checking !vm_started, only the "first" KVM_RUN for a > > > VM will acquire kvm->lock and thus avoid contention once the VM is up and running. > > > There's still a possibility that multiple vCPUs will contend for kvm->lock on their > > > first KVM_RUN, hence the quotes. I called it "naive" because it's possible there's > > > a more elegant solution depending on the use case, e.g. a lockless approach might > > > work (or it might not). > > > > > But is it safe to read kvm->vm_started without grabbing the lock in > > the first place? > > Don't know, but that's my point. Without a consumer in generic KVM and due to > my lack of arm64 knowledge, without a high-level description of how the flag will > be used by arm64, it's really difficult to determine what's safe and what's not. > For other architectures, it's an impossible question to answer because we don't > know how the flag might be used. > > > use atomic_t maybe for this? > > No. An atomic_t is generally useful only if there are multiple writers that can > possibly write different values. It's highly unlikely that simply switching to an > atomic address the needs of arm64. > > > > > > > + kvm->vm_started = true; > > > > > > + mutex_unlock(&kvm->lock); > > > > > > > > > > Lastly, why is this in generic KVM? > > > > > > > > > The v1 of the series originally had it in the arm specific code. > > > > However, I was suggested to move it to the generic code since the book > > > > keeping is not arch specific and could be helpful to others too [1]. > > > > > > I'm definitely in favor of moving/adding thing to generic KVM when it makes sense, > > > but I'm skeptical in this particular case. The code _is_ arch specific in that > > > arm64 apparently needs to acquire kvm->lock when checking if a vCPU has run, e.g. > > > versus a hypothetical x86 use case that might be completely ok with a lockless > > > implementation. And it's not obvious that there's a plausible, safe use case > > > outside of arm64, e.g. on x86, there is very, very little that is truly shared > > > across the entire VM/system, most things are per-thread/core/package in some way, > > > shape, or form. In other words, I'm a wary of providing something like this for > > > x86 because odds are good that any use will be functionally incorrect. > > I've been going back and forth on this. I've seen a couple of > > variables declared in the generic struct and used only in the arch > > code. vcpu->valid_wakeup for instance, which is used only by s390 > > arch. Maybe I'm looking at it the wrong way as to what can and can't > > go in the generic kvm code. > > Ya, valid_wakeup is an oddball, I don't know why it's in kvm_vcpu instead of > arch code that's wrapped with e.g. kvm_arch_vcpu_valid_wakeup(). > > That said, valid_wakeup is consumed by generic KVM, i.e. has well defined semantics > for how it is used, so it's purely a "this code is rather odd" issue. vm_started > on the other hand is only produced by generic KVM, and so its required semantics are > unclear. Understood. I'll move it to arm64 and we can refactor it if there's a need for any other arch(s). Thanks, Raghavendra