Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp569324lqb; Fri, 24 May 2024 07:04:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXN1kmhWav46GC+Y6rQqq2w0l0esyzs3Lxy2CSZsyGn2omIvMcGKNOroOTUkxFtHtQ7vTt29JHzodC7CzH7USZZKTGEGk55ia3m7BTNbw== X-Google-Smtp-Source: AGHT+IFpbTPOowV5+7PCS51qoaUJAwbAkdZtYbo4RGQ8wqd9i/NtSmVjXmj9oF6xSJqoDSQa2LcA X-Received: by 2002:a17:906:3c05:b0:a59:cf0a:4e4d with SMTP id a640c23a62f3a-a626407d4f5mr183166166b.12.1716559452806; Fri, 24 May 2024 07:04:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716559452; cv=pass; d=google.com; s=arc-20160816; b=DtWRMVdGP5KKpYNPpUcsySSTQkUhqBrel0MvDENJxV5D5OJIQfc4TqxdSbyxRKaubO JtpxLd6cMd6BfeeHU4UeNkiYn/1SYY9Dya+nA32LqMuMUHbS6MOVmdzzQ1zKQon6hFz0 Dt11mABo3sKa7CxvEz4w/p0wLPa56VA34+9GtowE6nMlo/NDH/cMhLBYmWXghSgdt26F ggJn4T7oZi3kbaOpk+xApv7p9Hr112JCWQCikhVyUcH+JjLCSS2gQQZBc+gIq5Khcx1j +VvHiKODHrpQwXr7HSsY8lT/tEPzZD+s8bs641npyqxrlHsgWmPNcgRQAkHbsYR0OOAT Ruow== 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=gK/kt0+G1Gu5Dj3cp1xqTKPcVZ4x2Lbeq9ePii2LIz4=; fh=vAZ2Yl/a8AQMm065tHvZx5xx4yAkuAt1cfwPq0CpTu4=; b=bj0u0sMCQ4edJ9jHzXdTcLT1NJc5cyoden+m2S0qaZ2jP3HUe6ITtp3D7gTxPo2+Ks hYpvOm3+LgVPijIxBE1/9cif86M3EsP/obN24A+tvDQzzkXXnGHI1pfwe3+e4At3W7MT JfWv8JOgZE5YvBOWUNR/xyGbwSGnZjuLwz0NDln1CwIfiHzAIvVchMbXOqkvXkuWAK5k vYGHqU6V9JB9HlAzX8dzHMajPvfRYIhsVe23XHRwdKKIWPvMXBHzuCHDL/UhPyKcJSAG vZzDKjHZkxF1WFImJ0RUySGzNyQsrKfYH9I0NEG/3bYk0Qr7h3LAxBXUqgUbWU1IFna6 ZJlg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QRLeeLgx; 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-188764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188764-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a626cd9018bsi85995466b.701.2024.05.24.07.04.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 07:04:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QRLeeLgx; 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-188764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188764-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 222EF1F2290B for ; Fri, 24 May 2024 14:03:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6212E12C483; Fri, 24 May 2024 14:03:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QRLeeLgx" Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 E8BF339ACC; Fri, 24 May 2024 14:03:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716559422; cv=none; b=rn0eCUWGJYqVeptutU9HPPrpwo58AXqUQ1HCadeLRsPmTpXrmW1GS6IgnH+0K2Vgb1P17E1jbBJeXWcNADziXLZCEabz1x6/KK2OE+V6VbR4+G4cTX5EPkUWiKwM2SKcNw+T5NpOqzNm8q+0JKSxG28xBNEaiSwFcrXm9wqXVuI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716559422; c=relaxed/simple; bh=r24Qga0ed7HOag24vEjlFAJ6R/BjnaRDrz7HKRoHTFs=; h=From:Message-ID:Date:MIME-Version:Subject:To:Cc:References: In-Reply-To:Content-Type; b=sBsgUIIiKEk+3DCJiN1Bnlf03QmIrdlj7lkD6u+Jhwdmmep3HLd5mNuroW29USrcAQBAD12XHevo1IziA3AgqoVbns9wVp/1XHoA+4Y5E+c6aEuBdDTJtjp+75lKnbF2Ey7zRuwTCPabBfVqDsPutSZomZyWToMwfwnde2VA4lA= 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=QRLeeLgx; arc=none smtp.client-ip=209.85.208.173 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-lj1-f173.google.com with SMTP id 38308e7fff4ca-2e95a883101so11073951fa.3; Fri, 24 May 2024 07:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716559419; x=1717164219; 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=gK/kt0+G1Gu5Dj3cp1xqTKPcVZ4x2Lbeq9ePii2LIz4=; b=QRLeeLgxVCQMe62VhQiwOnXgyn38Is5I7KDRY/fxdhs8vgXwhGpGAQMUrBSy0Jee+R nQV7+Shnx4Pl8Vn0NRS/b8rKTaSV1bOEehuEnbOI60j1iYDgBqqEHjcl5TRgLr/9VjLO OKl9MvxWz/pLX5AgkSEPV1cM8ci9t9HniWFOpKEVcp7IHWXSSD0InhIUyBOfY0L6BdQK E5O5Tuiju6g6jAP8dbNP+OGFgskNUsTSKPgQKuBE5nFJO5axDgy/zIAqlAvPsnycx89L b2cA6fDtxdt3Sjt8Efw0sxn4CbxayLdLd+1opgtgTThieltouR37Dno0rq52xUhVe9wi YdyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716559419; x=1717164219; 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=gK/kt0+G1Gu5Dj3cp1xqTKPcVZ4x2Lbeq9ePii2LIz4=; b=l0i94828RU2pv043XJECrkVlfy2wm2+V77cl7gw+2haLqxv6CuYDVpF48juKeRtLIu mNhDZgIQ+xE6kVXKey/IPECAOGY42oeCxUxGybeVuH9J1Ydu+8tej4VZqy3iyk4GgzQF OErU/krDpDBB/QnayoNhZee05Je5QQB2QLi3vHX8Ped/MS8UMla2eJ1W92qw/EPQ3zJA dpgaeGuNwIb8s609EUR2AwCUmqzsVCOU8xj1+XnZ8K7bXkynvkEEcYVchaPAjfNQ/NSU FRqvZf3w5dGjV3QDaaiboixQZH+UWC2PkzsxRGdGd/17mJf8IrjVl3nh4oTK26PWgi7r fedg== X-Forwarded-Encrypted: i=1; AJvYcCWlg1pEnVOdVkC4HD21jBndJJh8xRpdSpminYQLZX6+Iu/KQB4/PG5Z6mwUe7gstFwxl+DGcJexsduMs1k8o/PdQshV4Q0lxqWcBZIPzNBLJpJJQzYv3J/gjULXnmhRPh35bc7jtn9FbmdM5PzKDuspLpD07E2BjOOizLYk X-Gm-Message-State: AOJu0YwoVfu4gTbXOtDm76Wch6H726m3sDOqPU3bmfP2UX/3sqOSk62q zWqXsues7rCZjeqSzjLznk6Zix03oP6LLD1aiuNuQzyVt2NU/QDn X-Received: by 2002:a2e:a315:0:b0:2e0:a39b:2b25 with SMTP id 38308e7fff4ca-2e95b27f5cbmr16925081fa.48.1716559418969; Fri, 24 May 2024 07:03:38 -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 5b1f17b1804b1-42100ee806esm54025705e9.3.2024.05.24.07.03.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 May 2024 07:03:37 -0700 (PDT) From: Paul Durrant X-Google-Original-From: Paul Durrant Message-ID: <04ec9d6c-e761-4cfc-a2fe-a2d7d398c334@xen.org> Date: Fri, 24 May 2024 15:03:35 +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 13/21] KVM: x86: Improve synchronization in kvm_synchronize_tsc() 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-14-dwmw2@infradead.org> Content-Language: en-US Organization: Xen Project In-Reply-To: <20240522001817.619072-14-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 > > When synchronizing to an existing TSC (either by explicitly writing zero, > or the legacy hack where the TSC is written within one second's worth of > the previously written TSC), the last_tsc_write and last_tsc_nsec values > were being misrecorded by __kvm_synchronize_tsc(). The *unsynchronized* > value of the TSC (perhaps even zero) was bring recorded, along with the > current time at which kvm_synchronize_tsc() was called. This could cause > *subsequent* writes to fail to synchronize correctly. > > Fix that by resetting {data, ns} to the previous values before passing > them to __kvm_synchronize_tsc() when synchronization is detected. Except > in the case where the TSC is unstable and *has* to be synthesised from > the host clock, in which case attempt to create a nsec/tsc pair which is > on the correct line. > > Furthermore, there were *three* different TSC reads used for calculating > the "current" time, all slightly different from each other. Fix that by > using kvm_get_time_and_clockread() where possible and using the same > host_tsc value in all cases. > > Signed-off-by: David Woodhouse > --- > arch/x86/kvm/x86.c | 32 ++++++++++++++++++++++++++++---- > 1 file changed, 28 insertions(+), 4 deletions(-) > Reviewed-by: Paul Durrant