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 X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF9E5C433F5 for ; Thu, 2 Sep 2021 19:25:14 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 74BB860F12 for ; Thu, 2 Sep 2021 19:25:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 74BB860F12 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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=NgqKIVrWxsKN3zEts6M83B8rbFdeXDqvUeaIuhFNXbM=; b=QoXwzTcgTN0fKF 8HjNiUR1KE1SSFBojcSEoPVSU0NHMrk2GlbXh/WmUmM3AVFdInikGjitZl3M7CXgQH1lE4MlXt2ep +6R+u9G3w7DqszsGlmVpc0/aBy1bG2yJ7rXrqGQdF0C5LrDfUJ7kAyL3cCEh7EloMU2DAKLogZljp D/wwgpLR7zOOfkYBe6FLKLp9Bx28SD2iI5DsM38Uv2iAFrIAxPrOBkXLp8BYv1tYd2lEg5FlYexg7 2G4MhbnLMdEtpIB2rScEmEmmf0nm0i5QOxKUGJDlaGuaG6smH+jeXMGgEMlV9s5noRsFLEOKVJrDH 8SfWfn612S68csA98hbw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mLsHW-00AM5g-U3; Thu, 02 Sep 2021 19:21:51 +0000 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mLsHS-00AM5A-Sf for linux-arm-kernel@lists.infradead.org; Thu, 02 Sep 2021 19:21:48 +0000 Received: by mail-pf1-x431.google.com with SMTP id v123so2430030pfb.11 for ; Thu, 02 Sep 2021 12:21:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=bTPb2LtwY0BuCTlFiP191M1j/fLfSg4VVRY2NYVbQM8=; b=S/7EM5Sy//hkeACCvKyqm5sVVqBrQaqsdvxj8V+pqH+VLsMswwElPvPODqMd7fVjBQ 5i230e6v/OtvAgnS/CLoAsXnvILeltliV0s92h56yoUA0fUf11fzBxIPynn5EMbuXXRb gl5wjP5M2Lau95yABeHZjPjOapP29zLMK0IS+/+ygST5fVXgaJPDUEEdyNeVz93uhZ/d 85dVXSxIWc3ixj4OrK1qCnh5UqmWozTZAXAk8AOj9Av8RLaDEsT6WgMig+7/yc9yTAd0 QUPr4LbBjx9ibxnCt7BKdmJvrZfo0aFFeURS5oDPX3mGs2EAYO6KWacDYmpbuNM4tQl0 b25A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=bTPb2LtwY0BuCTlFiP191M1j/fLfSg4VVRY2NYVbQM8=; b=UDQodmYcR8fZsCuh0K8lionRCIYOZGAf0YxCkDBNcoTAL12H0/Opl5SUnQZU5h9NZr rd1oZii6wytob9DfZqH++HN5tgqLp9eXgWJngAAnD3fd2/sD0J8vHNoLb85OEjP+hlwR gZFr8gbCaji/ovShvAU8Ii01f8XcKCNGQnhgNdOjsH6HYlaUP68HwkQ88X5Hgm+Q5rxY HkFvqFDNEVvsjAC83mmmqUfF5DZMQJFnrZUoXaQvJwCv/RwZk3NHZkm1rH+/F09YdaiO s5Nozb8+hzZe5XqpDdl9GQ09eXY6CWQzyTKbep9rm7Ty5py88seW5XPzYUyVAkyr6tKP khbA== X-Gm-Message-State: AOAM530uMHZgj0lKIyVraIbBGY9LpriyJaJtzYcPAPIxl4PxqoKcE7DL z6KgeiJ2ugtxswtFgDg1GWNyHIHZQ0i7Aw== X-Google-Smtp-Source: ABdhPJyHmQYfdDfH5nHCWTHDLHiMkKrNuJmjtl34uxM72CrPEH11VQku3MHYEgMhSvkmPIwPWS9zAg== X-Received: by 2002:aa7:85d8:0:b0:408:78f4:a5fe with SMTP id z24-20020aa785d8000000b0040878f4a5femr4725944pfn.2.1630610505325; Thu, 02 Sep 2021 12:21:45 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id 6sm3086990pjz.8.2021.09.02.12.21.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Sep 2021 12:21:44 -0700 (PDT) Date: Thu, 2 Sep 2021 19:21:41 +0000 From: Sean Christopherson To: Oliver Upton Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Paolo Bonzini , Marc Zyngier , Peter Shier , Jim Mattson , David Matlack , Ricardo Koller , Jing Zhang , Raghavendra Rao Anata , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones , Will Deacon , Catalin Marinas Subject: Re: [PATCH v7 5/6] KVM: x86: Refactor tsc synchronization code Message-ID: References: <20210816001130.3059564-1-oupton@google.com> <20210816001130.3059564-6-oupton@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210816001130.3059564-6-oupton@google.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210902_122147_004303_928D4153 X-CRM114-Status: GOOD ( 18.74 ) 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, Aug 16, 2021, Oliver Upton wrote: > Refactor kvm_synchronize_tsc to make a new function that allows callers > to specify TSC parameters (offset, value, nanoseconds, etc.) explicitly > for the sake of participating in TSC synchronization. > > Signed-off-by: Oliver Upton > --- > + struct kvm *kvm = vcpu->kvm; > + bool already_matched; > + > + lockdep_assert_held(&kvm->arch.tsc_write_lock); > + > + already_matched = > + (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); > + ... > + if (!matched) { > + /* > + * We split periods of matched TSC writes into generations. > + * For each generation, we track the original measured > + * nanosecond time, offset, and write, so if TSCs are in > + * sync, we can match exact offset, and if not, we can match > + * exact software computation in compute_guest_tsc() > + * > + * These values are tracked in kvm->arch.cur_xxx variables. > + */ > + kvm->arch.cur_tsc_generation++; > + kvm->arch.cur_tsc_nsec = ns; > + kvm->arch.cur_tsc_write = tsc; > + kvm->arch.cur_tsc_offset = offset; > + > + spin_lock(&kvm->arch.pvclock_gtod_sync_lock); > + kvm->arch.nr_vcpus_matched_tsc = 0; > + } else if (!already_matched) { > + spin_lock(&kvm->arch.pvclock_gtod_sync_lock); > + kvm->arch.nr_vcpus_matched_tsc++; > + } > + > + kvm_track_tsc_matching(vcpu); > + spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); This unlock is imbalanced if matched and already_matched are both true. It's not immediately obvious that that _can't_ happen, and if it truly can't happen then conditionally locking is pointless (because it's not actually conditional). The previous code took the lock unconditionally, I don't see a strong argument to change that, e.g. holding it for a few extra cycles while kvm->arch.cur_tsc_* are updated is unlikely to be noticable. If you really want to delay taking the locking, you could do if (!matched) { kvm->arch.cur_tsc_generation++; kvm->arch.cur_tsc_nsec = ns; kvm->arch.cur_tsc_write = data; kvm->arch.cur_tsc_offset = offset; } spin_lock(&kvm->arch.pvclock_gtod_sync_lock); if (!matched) kvm->arch.nr_vcpus_matched_tsc = 0; else if (!already_matched) kvm->arch.nr_vcpus_matched_tsc++; spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); or if you want to get greedy if (!matched || !already_matched) { spin_lock(&kvm->arch.pvclock_gtod_sync_lock); if (!matched) kvm->arch.nr_vcpus_matched_tsc = 0; else kvm->arch.nr_vcpus_matched_tsc++; spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); } Though I'm not sure the minor complexity is worth avoiding spinlock contention. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel