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 6D549C433EF for ; Tue, 11 Jan 2022 17:37:55 +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:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HoJgGs6MNIRsKy6ETiJvKfL/yv/tvDZQNBfMOZMSlZ4=; b=e82vNUPVmX4bAg RWMHQz21iO0qa37C/T1tUjyNUbCdBJAWXQ0ViuqyC/IFoIzamcefu71LfrFzHhcQ+PhNiaPAyNJo7 dDv9YdmiFGvrU1fHJbFILNayvwWKsHX6E8fFqz5M+vN/XzaFOv1q9ZUfvLtubraoQMu3XnoCKKwQQ a2HnvLNQGXtpx92pSMG7gBkF4O9ymKjXPlZsHjLWcE4goLz0REBcAT/TaOY8NFt2nbjhcYJV2nVQ0 kq19dCBw9AHdMOZkNPBg4i9PzFA7VUYRRZkFg2gDEaC0xbFBtQIbFCMd4ofMJ6u+QWHdV61oEXOgN 0uwiSmhQ/30kzxzmiqLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7L4V-00H4pk-Hw; Tue, 11 Jan 2022 17:36:35 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7L4R-00H4or-IG for linux-arm-kernel@lists.infradead.org; Tue, 11 Jan 2022 17:36:33 +0000 Received: by mail-pl1-x631.google.com with SMTP id t18so8793475plg.9 for ; Tue, 11 Jan 2022 09:36:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=kmqD567CtlmXllpc1rQayweyZNMIGqk+RZtPJBuYT90=; b=sivzBEkWVy1S0pf5Z71owP5T4t13Zyykw7vqCCakVV0L1ZomD5pSrWWdeOgJuBAzWL zFUHh31Ym7mqCNUa7K4JxPVQhFbPy4N3qrzqt5roRk7BcHeqGnIc2hOgv6DFtWK5tTy2 WdWZCAOQdakcQ38IpcuCVuoEK9rwgLOE50yRhvkZiFuadJGbVSlIPWXMc7yIFrHCMtIG bTt98MWR4oDq6HIq7Y/sVn7MmFX9CS1xFwzTO6bvb1DR9JWIcoX/Hrrl/rCckOdh5LoP mdC2nr6t5awVklH9vDGx4GBF7LvyiGNU3Chu6orbgSoWmiYl0jqTamZ/7yzMNKGRgmwF 24Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=kmqD567CtlmXllpc1rQayweyZNMIGqk+RZtPJBuYT90=; b=HZTfgzx72V0Wea9ZwK08NNrNNpntgZQBqxmNCBSKUMfGtOohsavroAobJ7bBHeDq6l n54cyg2FqDIJW+Hezj+7/F3VPqb8K9jTrEJ1h0Mrnnvkck+ng8D17TGsFW8FUGgKZRXd P5skcKosb36FeKXXHSX0H+vSAPZfvcXGKtfN4gJsVpp62XoN18gzCkN3hCYjvrcoQhuZ g3E05s+3K2Z3Znjk30q8yYrNSi3gjjf5a2KjZeFTKMG7wGipHVUUQLbzRZ903yZpLWYY Xgu0exT1HaV40TJPlLXtWU2YjbKHxuNzcXKwds/SRB2toHX1QLTD90cYc6VEVmrUyVX+ cUnQ== X-Gm-Message-State: AOAM531Ryj4oBY/yyyfjZRzAfoH/Ruz8PNnr6i94shNmvaGC2x74kidl uekGiqI/iyqKXBhRD7zK4H2FbQ== X-Google-Smtp-Source: ABdhPJyi/ql3rEX/0G3ckRnHJKB6twfx7NB9W6on7gUjJywSRrz2rhGBuYgadAJVOgr9Cf/e9+m1Mg== X-Received: by 2002:a62:79c2:0:b0:4bd:e9da:c173 with SMTP id u185-20020a6279c2000000b004bde9dac173mr5462897pfc.65.1641922590326; Tue, 11 Jan 2022 09:36:30 -0800 (PST) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id qe10sm3488021pjb.5.2022.01.11.09.36.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jan 2022 09:36:29 -0800 (PST) Date: Tue, 11 Jan 2022 17:36:25 +0000 From: Sean Christopherson To: Raghavendra Rao Ananta 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 Subject: Re: [RFC PATCH v3 01/11] KVM: Capture VM start Message-ID: References: <20220104194918.373612-1-rananta@google.com> <20220104194918.373612-2-rananta@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220111_093631_636856_A2A7E5C8 X-CRM114-Status: GOOD ( 26.05 ) 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 Mon, Jan 10, 2022, Raghavendra Rao Ananta wrote: > On Fri, Jan 7, 2022 at 5:06 PM Sean Christopherson wrote: > > > > On Tue, Jan 04, 2022, Raghavendra Rao Ananta wrote: > > > +#define kvm_vm_has_started(kvm) (kvm->vm_started) > > > > Needs parantheses around (kvm), but why bother with a macro? This is the same > > header that defines struct kvm. > > > No specific reason for creating a macro as such. I can remove it if it > feels noisy. Please do. In the future, don't use a macro unless there's a good reason to do so. Don't get me wrong, I love abusing macros, but for things like this they are completely inferior to static inline bool kvm_vm_has_started(struct kvm *kvm) { return kvm->vm_started; } because a helper function gives us type safety, doesn't suffer from concatenation of tokens potentially doing weird things, is easier to extend to a multi-line implementation, etc... An example of when it's ok to use a macro is x86's #define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) which uses a macro instead of a proper function to avoid a circular dependency due to arch/x86/include/asm/kvm_host.h being included by include/linux/kvm_host.h and thus x86's implementation of kvm_arch_vcpu_memslots_id() coming before the definition of struct kvm_vcpu. But that's very much an exception and done only because the alternatives suck more. > > > + */ > > > + mutex_lock(&kvm->lock); > > > > This adds unnecessary lock contention when running vCPUs. The naive solution > > would be: > > if (!kvm->vm_started) { > > ... > > } > > > Not sure if I understood the solution.. 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). > > > + 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. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel