Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp355111lqa; Sat, 27 Apr 2024 05:23:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWB+Bh6O41Y+eVBWQg31UAavjx8CAacoe6T5AfUgAQx51iwdqIMGiFbTDyYMf0Efi3u/66n987bFnXZof0cR6RIMRuAml8xzlOOrebbMQ== X-Google-Smtp-Source: AGHT+IEM5QmQpj+nw+8DwTNlC2XjTyM5cCNe+O/piBR/W1LEG5Na5YCr+F1tkVYblQdvpg4pgUyt X-Received: by 2002:a17:902:f551:b0:1e6:11ae:dcde with SMTP id h17-20020a170902f55100b001e611aedcdemr6082855plf.58.1714220600787; Sat, 27 Apr 2024 05:23:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714220600; cv=pass; d=google.com; s=arc-20160816; b=jJk5H3F7j/W3XGeGWU2Dk/xxL7vvrtEJ9ndkvye7YdSnvtVgSajJQXl2H9KO7M40bJ FUx0/tgjX4ZzbNA4ew2ZdEY0XzPAPrx4bFpxxFL8A5iS7xhycpBLTw96CnxGlCUOiFUA tLDb6p2mAUrV0DCwDRsp88WEL3i+p6xve82z6uJRclJ//LVN//Jg79gguShqksAPGuMk 5bwI11a/M9AK5pSf6jiT4ZKtjR2sjCohGRuwxV+vc3jPyov2u4NRkl4syBm247+BEple qE1a+bRptyO2Ibj4CAMpMsIfGWkY2f3qi56JJXD3OQ9xKB1T+c7SEaIFcqZEa5e4cOIr rBDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=LhUA0U4IOeUA/e+o4NGCvx07Pxtovz/JSXQ/6S+Lttw=; fh=GxGcIBasQq2Gc/176mTrpEex7eS1coPy2FlYCD7Qc50=; b=NrBrtTDREjwpbnfLJ0XadlQnLkII6ZbUkktm5WKHCaBfOF1u0TcD7I3JvOlNW71ISS pNWqVhzZPGKp9e6C1xVOrSFv4vl6fMFn6xLWe9wZbKoXMXuUXakZO8sIFuaBYHhtk3a+ MOTwtvniBC3ts8ml0MDa/aVMkDMBfXj9OpoZRxbqYJNQ2fViDJlKQtbKaGkuFBMt7NCy vn439coYnZiUOprZbrMqfq7YSb8s2baEIhVjzCXCr9LPzjtXsKnNtyHvDjUzL4/qBsAC XxSlYfrB91tQlhnWfT1mnvDWUFggNZFC/Mg7Ov8STji+uhDCNxsAuRtxKrdDHLGVBpd4 Uy/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jkwhuFHy; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161018-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161018-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j16-20020a170903029000b001e0f961759bsi16989648plr.80.2024.04.27.05.23.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Apr 2024 05:23:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161018-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=jkwhuFHy; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-161018-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161018-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id F111A283214 for ; Sat, 27 Apr 2024 11:20:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D2D34F616; Sat, 27 Apr 2024 11:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="jkwhuFHy" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2CEB4AEC7; Sat, 27 Apr 2024 11:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216786; cv=none; b=a4tpuIxzALZaVb9T545/Njs5BNyN0zp3k80WOUg/R9xjBWBekkSUps/61fgm+UJW/x4CwHqFKS2TBzeZ5/N27ARCHeGkSySR3q6Tw8SaZ0xcGeUuEzfhOYMrafV5HGWdobUM7UUl3Fkg0yll/Ciy5XrTdknhwm1Dyz7K/HIgKTo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714216786; c=relaxed/simple; bh=MfsZSEbRwBJ9w5jCG1r9UNHwXDhRvX+YH1b8L7lq6jc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WvsxMcOhGwVyYv9Jr2ifIvwEclTL9IQB8DYS+zoZTndWHwaY+9D9ffXc8bmgIV7mgK9GyvwqJmoV9iu3THDt+It6tVbj5Mcp6Y9LWnRRMzcum5LgV3iYCw5APBEJ3EVCeCeWHj7B6xbKgvy/OvY7syFkMcuvxgw6JazM5l9d/Ek= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=jkwhuFHy; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=LhUA0U4IOeUA/e+o4NGCvx07Pxtovz/JSXQ/6S+Lttw=; b=jkwhuFHyGP0W6eB0FCnQXRFH1i k4V8nJu/xHElplfQ/c7+njx0qvE8deBVDcTyE9rgOoghUUN5igilN5V+rqmTDMsJdhi77EBjPax08 lA8P+A6XkAY08ZqqKSYqa0ObaIFU/zIhg1qD8D+i6rdt/dusU1Gb8Kp+2Zr3Z0Db+IG0+gwih/M8j rz68Ifm9ozpxAEai5f4RwNjVPSIy0hdirWJRWl8LRdwQuonmQ0XMaLj6mpsdCgF+19itDnfWgpwvq E8mZg+A1HzWTtwr9kEqbWA1PL8cyvMk6XTgd8953Bb8gr1Wj+cnR/ghH+HjPdxiNrpFw0z1wxLzV9 y/8okUJw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5h-00000007Jwn-00cY; Sat, 27 Apr 2024 11:19:37 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s0g5g-000000002bh-20OW; Sat, 27 Apr 2024 12:19:36 +0100 From: David Woodhouse To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Durrant , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Oliver Upton , Marcelo Tosatti , jalliste@amazon.co.uk, sveith@amazon.de, zide.chen@intel.com, Dongli Zhang Subject: [PATCH v2 11/15] KVM: x86: Remove implicit rdtsc() from kvm_compute_l1_tsc_offset() Date: Sat, 27 Apr 2024 12:05:08 +0100 Message-ID: <20240427111929.9600-12-dwmw2@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240427111929.9600-1-dwmw2@infradead.org> References: <20240427111929.9600-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse Let the callers pass the host TSC value in as an explicit parameter. This leaves some fairly obviously stupid code, which using this function to compare the guest TSC at some *other* time, with the newly-minted TSC value from rdtsc(). Unless it's being used to measure *elapsed* time, that isn't very sensible. In this case, "obviously stupid" is an improvement over being non-obviously so. No functional change intended. Signed-off-by: David Woodhouse --- arch/x86/kvm/x86.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ef3cd6113037..ea59694d712a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2601,11 +2601,12 @@ u64 kvm_scale_tsc(u64 tsc, u64 ratio) return _tsc; } -static u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) +static u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 host_tsc, + u64 target_tsc) { u64 tsc; - tsc = kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); + tsc = kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); return target_tsc - tsc; } @@ -2758,7 +2759,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value) bool synchronizing = false; raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); - offset = kvm_compute_l1_tsc_offset(vcpu, data); + offset = kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data); ns = get_kvmclock_base_ns(); elapsed = ns - kvm->arch.last_tsc_nsec; @@ -2809,7 +2810,7 @@ static void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *user_value) } else { u64 delta = nsec_to_cycles(vcpu, elapsed); data += delta; - offset = kvm_compute_l1_tsc_offset(vcpu, data); + offset = kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data); } matched = true; } @@ -4024,7 +4025,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) if (msr_info->host_initiated) { kvm_synchronize_tsc(vcpu, &data); } else { - u64 adj = kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_offset; + u64 adj = kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data) - + vcpu->arch.l1_tsc_offset; adjust_tsc_offset_guest(vcpu, adj); vcpu->arch.ia32_tsc_adjust_msr += adj; } @@ -5098,7 +5100,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) mark_tsc_unstable("KVM discovered backwards TSC"); if (kvm_check_tsc_unstable()) { - u64 offset = kvm_compute_l1_tsc_offset(vcpu, + u64 offset = kvm_compute_l1_tsc_offset(vcpu, rdtsc(), vcpu->arch.last_guest_tsc); kvm_vcpu_write_tsc_offset(vcpu, offset); vcpu->arch.tsc_catchup = 1; -- 2.44.0