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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham 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 D54BDC433EF for ; Thu, 2 Sep 2021 19:21:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AFF5660F42 for ; Thu, 2 Sep 2021 19:21:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347257AbhIBTWq (ORCPT ); Thu, 2 Sep 2021 15:22:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243832AbhIBTWo (ORCPT ); Thu, 2 Sep 2021 15:22:44 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EBD4C061575 for ; Thu, 2 Sep 2021 12:21:46 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id c17so3084099pgc.0 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=MlfeSw2rKDF8XDvcAeFUk6pHHOXZjByXGCS56c57N83ob/f/Xwj0xt1r4hMNpreNF7 P5R8oCZSPq6q6J3IcuQbwaSoZIGEjrVZ/AQ/fMdbsllJiU4NriKTXDhq7oToGAdqHl24 01DcY7F0ekuEMPRsvv+hbbMnrRuhPvMZQhj+Y4ttwS3j7qUKmL7RRdcq4k2zRFppejqN oKNVLaKT8YE5b55ilbDB8I3dyrXZgOwU8l7883dZPGjAwlWoIuf1r3CRNQ/8eBLfFdYv Z9njqBAuhPeOmXJfqX57v4jwzb6t2vEmfGSKwCyQSy+g6cT+u1wbiESkd/nTD2mjLbT5 D2dQ== X-Gm-Message-State: AOAM531AtXk74z2dyPtmQaJ+WOas4SCn1nynTg9L+eWPbPfhuuV7S/YP p0hgWW/10qahei0UX/vRwdcDAg== 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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210816001130.3059564-6-oupton@google.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.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. 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 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=-8.5 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,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 97DDEC433F5 for ; Thu, 2 Sep 2021 21:01:22 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 10A6460FD7 for ; Thu, 2 Sep 2021 21:01:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 10A6460FD7 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 679A94B128; Thu, 2 Sep 2021 17:01:21 -0400 (EDT) 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 zjmy95kea4K4; Thu, 2 Sep 2021 17:01:19 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id E7A3F4B11A; Thu, 2 Sep 2021 17:01:19 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id D71934B10A for ; Thu, 2 Sep 2021 15:21:47 -0400 (EDT) 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 RhUVyMCfe1Km for ; Thu, 2 Sep 2021 15:21:46 -0400 (EDT) Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id C45D54A524 for ; Thu, 2 Sep 2021 15:21:46 -0400 (EDT) Received: by mail-pg1-f178.google.com with SMTP id e7so3059963pgk.2 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=VBM2eeSA0Ixx1B7csZ+clTRHooOnCh8Zp9AvV1Fi+iK0zaP8Jksy7M+mZSN4V4sA9E 50gIcXeyVqT+Sb9M5tdOUohl9mNm1tzzDrRhRwDhXxGCenWszkQZutHNHTAxcOnYq10S JmXllvzYiEwXwtlRQY4Vz3PlJggJf4y6QxTrUMvzQ8I/g0SEF2pUYiHhhevCw/F6p6yK ZQqVqG4MifC0pEr0awOPi0FggWWbmJ7Lj4k8mvMUiS3Y5X6RUIoi7m5MbMCQ4MoFHFCL tVc4k+jApKMold5aMTs3fAC+NkQQudr5rxXbWxrvzIVeV/h0fVG1SOtIM47Hbqgsk0Np w6Rg== X-Gm-Message-State: AOAM532kB0NjM+0atllSkRy6qJAHT306ZXzGWfSWs0l0t9t3YaafLMxC I/VIJNKlrSFINaI/kmuOAvYFEg== 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 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-Mailman-Approved-At: Thu, 02 Sep 2021 17:01:18 -0400 Cc: Catalin Marinas , kvm@vger.kernel.org, Will Deacon , Marc Zyngier , Peter Shier , David Matlack , Paolo Bonzini , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Jim Mattson 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 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. _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm