Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp543431lqb; Fri, 24 May 2024 06:26:28 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqiK6YW+eQ2wWFSyMZwVpBCoh1Sd2J10st+17G2TehqA19ET8szlct4ynikBlKEzZD3kt6WfKHYOPzgoTJrge8FTotJg3y9TCLLcpzDA== X-Google-Smtp-Source: AGHT+IFKj0+m+DDLtoyxbx7niN6W4BpXf7+9m4Tj/ZoW/Qi4t0EXqHLRWCERitjeq6IX0R5eTuGv X-Received: by 2002:a17:90b:188f:b0:2bd:e0dd:f232 with SMTP id 98e67ed59e1d1-2bf5e138e72mr2025159a91.8.1716557188262; Fri, 24 May 2024 06:26:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716557188; cv=pass; d=google.com; s=arc-20160816; b=lFauwR0VOak8fG+8CXWuwsWRibNFSqv7obIBkU2UdFcZbBoBGhejzI9D4rAWtSqvyr QSG9kJT+F/gnLAtRL7TEOGuGKcVlXt/ghkZKBgzOAsbr2vVv/2eV2p7bjJygf0zYI9kr grCuLKvDGT9Y/reknckWSOsFpTu/bTAO0pRwu96zqm1hWQYx7BAzCx338BLq8GrPAKSp 8RPxD5TZnpnNFjyBXbqVT5BaZct0UTp4XQctQsfzB9CSOi11sydGYAYyW6Wlt4Ma01az +CEQo6AGkxssW3YGz1LndxkM+PBcSZpYoweJGcHn858+AZilObcMrwWYi50hGLBAoi5d Kjbg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:content-language :references:cc:to:subject:reply-to:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :from:dkim-signature; bh=SolOo+u07aKBHiQy8wdX4zOZwYE5CwLfUJ/MrBrsu9A=; fh=ZbmdK+3B9/IfAJt17NTF4iHhInDDm/dOjhHHvBG5Xdc=; b=cTSlvwfZ4JgvEYnPFcP6MAFDs2C+Ic0DeaPtoPvYVJrnnqzxA4NpOARfforn01ts3h qm3jYdWDRu4MW3BYck9+uqUb1+OTP0X4sPjg+phBMeNLb/e0hKLlG/YB1JrwfJo0ntmJ Azk+RYovAzcQcL7YNWp9mGhCIMvEpIMPrSqw7gQTzL7c/JzYRHX5vSXmaHjXHjsNvAik h8mLWQ0VBWXThjtmlAWqdcpR6cVRvLA5b0kp2Ca43FvnCqTgI4ccAOneh1WXCJsD5ZBK EbvJtilYeTNGRD+4bkq42mHHh/dfMmPXlu4DLFNeHhts46YhideZV0PfGpqQ4GGxXS8v eX4g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=C6ciXNQ+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-188738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188738-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bf5fe605cesi1454633a91.136.2024.05.24.06.26.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 06:26:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=C6ciXNQ+; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-188738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188738-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 67429B20404 for ; Fri, 24 May 2024 13:26:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B9FD012BF3A; Fri, 24 May 2024 13:26:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="C6ciXNQ+" Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 427883FBB7; Fri, 24 May 2024 13:26:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716557172; cv=none; b=YOL3903mIk1AMdJxbspSlnLl9BUe6+4rQUtc1XGK8yeEIJBEPGQdMMkaCYwi9sSdSrxjBoBV4hjS0M/ynejp3r1xpX9delT/PT4caQVxzJpZoBT5i4PBpZNsy86zI7BICOnxo6D/ClWdM7pEV7osdEUoB4fy/biuoPHAUxLs1fI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716557172; c=relaxed/simple; bh=j5xm6XpzuOK09zeyH58U/GJ0F/Rrq7mRKYGfu5Vr8sU=; h=From:Message-ID:Date:MIME-Version:Subject:To:Cc:References: In-Reply-To:Content-Type; b=QfGXFWYd7voIZBBeYpUcBI5iN0QoVmznmR7GSsPhvR8aftfXAHxI6gHCAeNYKZtdiLRfEGvZ5I+zpO1zWsWy0CQdu/7AknIa9QdO0XuI3tDqSjMt5+ZcNvHOEkUdccepKsB2Dn8YLX9KLMJnHVlDT2xpNMfSl1rlX93R9pZaSWk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=C6ciXNQ+; arc=none smtp.client-ip=209.85.167.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-523b20e2615so10190541e87.1; Fri, 24 May 2024 06:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716557169; x=1717161969; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:organization:content-language :references:cc:to:subject:reply-to:user-agent:mime-version:date :message-id:from:from:to:cc:subject:date:message-id:reply-to; bh=SolOo+u07aKBHiQy8wdX4zOZwYE5CwLfUJ/MrBrsu9A=; b=C6ciXNQ+ozV+RbxvAm+P3kraqt9qGdz5v5oCaahZCLtnda1KTk69ea08wyHLziCJIO FBgQUuvKgwHvUvKV3dwm0VfwIAba2EZuLYRFZYHds0FbZAN86LIlbAc77QEw5+Q4wO4i uuK35b1pu3Ws0N2AdTCj9MeFEHs1xw9InU8eoe0ztsDwbmQB2krJk5LJ2aXhSYtNnkzK qp7KKlxXEY0kEYpKNNGkldolz+1KFTH/aPmnJ+7GLI6MG2WHuXhUN0TVtnWcKozcMtcN HFNMVax+ZqIw3U+VaE/R9e5KLf88n+umttjKEQvDS7v7lr9R5syWmhEUUEPu46hr8ErK 7AeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716557169; x=1717161969; h=content-transfer-encoding:in-reply-to:organization:content-language :references:cc:to:subject:reply-to:user-agent:mime-version:date :message-id:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=SolOo+u07aKBHiQy8wdX4zOZwYE5CwLfUJ/MrBrsu9A=; b=i5uV6AdgUO+48yhFs6S+XvjAvRADICy4++CZycD0rKZQ96XiyjOf4mLDXFyvaygB9d KjVo8gN2pq1KsFxS1Gir/dr//zAqHhkJAi+hW+u151nTa9uDMS46O1Q7et81CyCAHYbn zldhG5jSpT833ymno7pxh63gnss638i+wMwEg6YUin3ILq8KsSCymskh2CkCRUsxLVPK HsjnAVNv0ut5/oka2+dtLFQnOw1M2GugoaEGIaGlRB2J6PBw3JqZ4FIs1XBEzUJeiyll ROXB+ou5/eCg7AJ4nnLD3jiy8Ei+8sbvZDyTriopLyUnR/5Zb7uvNN6SDijNtURdyK4c B+mg== X-Forwarded-Encrypted: i=1; AJvYcCUOkQiD4htE5L5+0TSmtpZ+PRkWwHs8e7IzWxuWHlsxqaGBphGk4R+Y0zTFhEwD+VUtCxbsumFRa6Z1ck/5sJrL/exMVB95OnTbuGRRochF+sdw0DQL+I0sEyi865ZFY8FEKslp/WC7Y5dDHEIdgSUpsIu92mw4uA91HkO3 X-Gm-Message-State: AOJu0Yzx2YZzkGAaq1xVy78Pnc8oVRDaQCH2wPr4ftMLHa/fnT2v6ZAK iQUl1qZnh8ibZ9kv7FlSGWNOTQ/TVy6hsxdhuSFD1pW+0KjYBZsx X-Received: by 2002:a19:f506:0:b0:524:34ad:ba7c with SMTP id 2adb3069b0e04-52966d9d996mr1260613e87.66.1716557168974; Fri, 24 May 2024 06:26:08 -0700 (PDT) Received: from [192.168.0.200] (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5296e887a33sm181182e87.45.2024.05.24.06.26.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 May 2024 06:26:08 -0700 (PDT) From: Paul Durrant X-Google-Original-From: Paul Durrant Message-ID: <04a1543d-4156-4048-b4ec-99240a43d4c4@xen.org> Date: Fri, 24 May 2024 14:26:05 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Reply-To: paul@xen.org Subject: Re: [RFC PATCH v3 10/21] KVM: x86: Fix software TSC upscaling in kvm_update_guest_time() To: David Woodhouse , 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" , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, jalliste@amazon.co.uk, sveith@amazon.de, zide.chen@intel.com, Dongli Zhang , Chenyi Qiang References: <20240522001817.619072-1-dwmw2@infradead.org> <20240522001817.619072-11-dwmw2@infradead.org> Content-Language: en-US Organization: Xen Project In-Reply-To: <20240522001817.619072-11-dwmw2@infradead.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 22/05/2024 01:17, David Woodhouse wrote: > From: David Woodhouse > > There was some confusion in kvm_update_guest_time() when software needs > to advance the guest TSC. > > In master clock mode, there are two points of time which need to be taken > into account. First there is the master clock reference point, stored in > kvm->arch.master_kernel_ns (and associated host TSC ->master_cycle_now). > Secondly, there is the time *now*, at the point kvm_update_guest_time() > is being called. > > With software TSC upscaling, the guest TSC is getting further and further > ahead of the host TSC as time elapses. So at time "now", the guest TSC > should be further ahead of the host, than it was at master_kernel_ns. > > The adjustment in kvm_update_guest_time() was not taking that into > account, and was only advancing the guest TSC by the appropriate amount > for master_kernel_ns, *not* the current time. > > Fix it to calculate them both correctly. > > Since the KVM clock reference point in master_kernel_ns might actually > be *earlier* than the reference point used for the guest TSC > (vcpu->last_tsc_nsec), this might lead to a negative delta. Fix the > compute_guest_tsc() function to cope with negative numbers, which > then means there is no need to force a master clock update when the > guest TSC is written. > > Signed-off-by: David Woodhouse --- > arch/x86/kvm/x86.c | 73 +++++++++++++++++++++++++++++++++++----------- > 1 file changed, 56 insertions(+), 17 deletions(-) > Reviewed-by: Paul Durrant