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 mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5555C433F5 for ; Wed, 12 Jan 2022 18:08:48 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 6748D4B25D; Wed, 12 Jan 2022 13:08:48 -0500 (EST) 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=@google.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 u7oUrgosYxRW; Wed, 12 Jan 2022 13:08:47 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 235414B18F; Wed, 12 Jan 2022 13:08:47 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 83EF54B18F for ; Wed, 12 Jan 2022 13:08:45 -0500 (EST) 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 tmB-BlGtrz1p for ; Wed, 12 Jan 2022 13:08:44 -0500 (EST) Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 4AF574B174 for ; Wed, 12 Jan 2022 13:08:44 -0500 (EST) Received: by mail-yb1-f178.google.com with SMTP id n68so7927556ybg.6 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=jKtxLLo+L31LeXEdQTAP5/gz6Q98RIHM8Nm8e01ukrFEHn/cssdyFdzpHrLPDvW070 udHp1Nzph60RT3G1bphUKDLQR0xeNgL7//fsHk7pqOMO0A8Gyy+us0EmNs5iAkLd06rK j+55ivgcWOe0YuN4XShXR7jxBNHibEgUvYZ7lgeXtFVPLmvcZP7ENIxvFZo5sdiLVQqw 1lLUa6fdewueyd3R0YOqYvk7cgHwFGoOjQyZQXxyGkVnwC9d1eASI5ajVHsIaDyaCFma k4rmuqYAvVxkUsRw7IV1EKdlf3ffyncYMDOfGsxMH5kDOUgsgow1Kz0DRzGj3GYPTnPU 4KKA== X-Gm-Message-State: AOAM531ITtlTphp03LRb9sLRlBiPGWFmzkX6I5KoRri7HLCyHbV0hzXX GvkJeBfHKAnGnNk87EI0KVoyWY1dJwtRbLTQ0dkfeQ== 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: kvm@vger.kernel.org, Marc Zyngier , Peter Shier , linux-kernel@vger.kernel.org, Will Deacon , Catalin Marinas , Paolo Bonzini , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org 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 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 _______________________________________________ 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 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 01D61C433F5 for ; Wed, 12 Jan 2022 18:10:00 +0000 (UTC) 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:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=z+OdzxJ29YM0bJ+iKirnSJqNcG20VqiFWTz7PIdfVfg=; b=CIKhFv2OOkCa5E 9SoC3sZC1m4jDz/LgrOEFxqClegNOr0hVABIl1UM8vPEFQrrknZJlkG1LSpoedKCV6/XmMCUh0oON QwICAIba1GF/gh8dLg78u5W86iDpFFL53i8YifUXCh07XWdiSJyxZWHOqlaNKfcCJmU3l5WKmDqmM K6n5kS76LZb9Pt9SeYLRYglbICj7zfmrLV38N75UdaQIjcTlqCic1MWYnuvfjK8PK8FuhC/pBc4sN 1tbjij/sQbj3UCMeUrU31jrSYF8C27m5kWxK/QYxD3qmrwrRXLN+MoTNvzrQ1YNEdRiYqNmoypbgT dO6cAwvp3Do8UGGGgRog==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7i3E-003QOr-10; Wed, 12 Jan 2022 18:08:48 +0000 Received: from mail-yb1-xb33.google.com ([2607:f8b0:4864:20::b33]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7i3B-003QOF-1D for linux-arm-kernel@lists.infradead.org; Wed, 12 Jan 2022 18:08:46 +0000 Received: by mail-yb1-xb33.google.com with SMTP id v186so7997270ybg.1 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=rr5Y85ogL6AdUaqfqd5gm3U98WonTUGsAMIRKbeihmRISW/U0xgc3dcmCue18ijWuY gAStAPRahWkZaDzS9jbcTj8Itp3wiRVw0n3W1X1hSnmUPapIAsfDOy40FHMnSpazH66b ulat+jXuRmGAkWaJUEbn2HzE0fumaDTzolKbRc+/26iFOvv7COVi4q12lVObeNIFqvdL 90ZlifYnwoGCDmDjKkMdzCD6lr2/EshTZ0wBS+vparEhdlQUFZpWzuaMPQ3c1sg5vbSL SaHB4M48gLqtx/7KUlI3X6AQ7audrrcd6eV5645/WGOsTyiUwEHqGkqFcQj1/RIDYL5n fchQ== X-Gm-Message-State: AOAM532dxT5m1I2C6IjwJbztHmT0LUjW/f774+loH1aq+qyxY+iZ4pd6 cMY/e4RxYwLMyjW5q7i21mRLZF4RJ4IJ3jsdRh87PQ== 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220112_100845_101510_2D25984D X-CRM114-Status: GOOD ( 37.22 ) 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 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 _______________________________________________ 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 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