Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp540623lqb; Fri, 24 May 2024 06:22:12 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV1KyCRdCQvhhy9Zm0OOtkZ7JFpQOoaMQTjcWdfsL/UhZrabdtHA+FFy3Tc1QIZv6OR2pN7/zu2qSp0qelmQ6nwMbpal92M8uOZJ+CAEQ== X-Google-Smtp-Source: AGHT+IGg60LnowLDfKegKmwazIEW3vVCEx3hIqQXdPsSjZKZXeWLZMBbo1sQcA5iR11t6xkhcJt8 X-Received: by 2002:a05:6a21:3284:b0:1af:ac31:9b8b with SMTP id adf61e73a8af0-1b212ccf7c5mr2793134637.10.1716556931736; Fri, 24 May 2024 06:22:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716556931; cv=pass; d=google.com; s=arc-20160816; b=jCpZjBw+7jaBza7Da3JDlTPTkl3aAMB8MYXxR0Ag5ughIS77UvrRKAju4VRCijWIS9 JX4Nay21FGZCC7k1b4TjRSolo0yw4Cr/m+u++qD6e94CBGpm9IooV/lpa5luQK4LNnud 5McgWwBjJsUYPy1zTfTbKEUJQA1i5Q8g1IjcdWZrKf9a6aQvmE2cxbW31V7DAeFyJAk/ TZ1b2JfqvKOqmGkKl0nkNll8uFDeoJsVbYr7vj08US1JKTwgnLQyTLqyoMq88SaEVwQq jmW4rW5GWOzknqCz7EWsUp4U/0+0GkgFMSPC9HbkzYAjG/2eHNtY7Asfje43KZUwnhsm Ektg== 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=XjUlSi6b7spXDtsoszPBRMpQom7fNDT5+VmBArxwsxM=; fh=DcWZjj2BdXmnjZTM0JXfDk01C8tKeSwgdJe5ct28IaU=; b=uu9iy0avzmJG3bjCuPxQ6XMpNNCGTsvtvWBWDfHef/QAO0VarGzJ4jB+zAYYOVvhD+ NAQZO7dIJ8eveT/zchDIOsbUvdGiPdenaxLvVRgAkLpW4KgVgP+xKAgRWFHI18tLLxCS K6JsWbTEXjSCGHyj2NvaMk5HcwxqFNnIVWka3AKgXAC0AR40PeOc5AALiXTJoSTH+qdE 5VfqBhTP+x8NdQhK/cdALtRznRx78xHxc6eagwA1PoCbzgOGObKlpKGZZr/Diigc/Jri MAJFQhw1sDV6iuxSBU5H1cbqFhaIjETl1+OmI0UYtBSOOKf+Xdcpe3O/ewvDfLCowg1+ EZYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QoI6gY+i; 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-188733-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188733-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6822bbc73fcsi1265634a12.886.2024.05.24.06.22.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 06:22:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188733-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QoI6gY+i; 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-188733-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188733-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 6AAA4281A75 for ; Fri, 24 May 2024 13:22:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0CC7612BE8D; Fri, 24 May 2024 13:20:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QoI6gY+i" Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 59D281292DE; Fri, 24 May 2024 13:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716556815; cv=none; b=YUzGUUUKX5QOsJag/UdwphEBj1C4tp19MrEVCPvDGmfteQoVCpRfjmSRUaRPu8dwdxua3zwYLYhmzGHS0AWxQ+3cDlK5Kg9vK10hUUIauk/kFtsZnqojfUv8+50Tb0C07QsJ68G/d4FWQwtKuz0bt+szH767PZVdXDsvNk7B73U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716556815; c=relaxed/simple; bh=biZ7qAWdW7zPaeONCmHV0StXEtQawhsNoESZ/hmM5VA=; h=From:Message-ID:Date:MIME-Version:Subject:To:Cc:References: In-Reply-To:Content-Type; b=KUb3rlHnMRXgmNZ/Sr0omnOsh5Wcrb1ROJbbrbX52MZadIGQPhauFuQAMiBwaGpNIHJul1pf4VfyeEw8jv6EvyXEkzs7MGYYlS9GMRNiS+0gtsEKvfgtJ/naa+QWJ7VY9daeQMdMlrR2RLJbz6IwPDpvPe78lxIOSaisZAiyJTY= 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=QoI6gY+i; arc=none smtp.client-ip=209.85.208.174 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-f174.google.com with SMTP id 38308e7fff4ca-2e6792ea67fso107285401fa.1; Fri, 24 May 2024 06:20:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716556811; x=1717161611; 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=XjUlSi6b7spXDtsoszPBRMpQom7fNDT5+VmBArxwsxM=; b=QoI6gY+iDjh8A3//EEmlMY7tCiWrqFnS6/FkKTAjeU61oMayN8Q0qIMh2F4INsDEvu yhRX5f/5hadhOsMTUuDJQPmVloQVfdnpVNrLTY8RR7xnGHHWVVUQli/C3YO7t6CUxJx1 rkvAiZ+ZVF8YKjiNchA370cLMcunCNdlKToBoJIupbXcNS+ZkK8x+Ky6tMBhhqmhvA1c 7SCW1jBP2g/CsLKKaRKEnmF8iWrPNiVJYrhoY1NGJUL+wi3LfPF+qihkAxrMOKD5kKIR O4l5N8fNcm6NiFfZFlRZNl7x4gHH2pvtkvWVRbi2e26+BH2NnUi4qnaJWT/mguH+90PK +jAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716556811; x=1717161611; 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=XjUlSi6b7spXDtsoszPBRMpQom7fNDT5+VmBArxwsxM=; b=hk5j9jIbGPG/o9CWxxFYOwZFHcUmSVjCEx+lXKSL23o3OrDXjjj7sf31qYjg7/9UOh 3pAluYVFVjIJ//I9lMioBCafwWHQOm2GLrju9U4iIN2QMnPrrcK2E7qXovahV5IFUA74 JQlRrbC665QT9TX/gh5k1QLvrzL8Zdm5cW/FstxA9QtY1y/4LsR/wQ2TcWWtm5KOG7pb 5eTiwqcBpKPSYzoccEOr0RiD/aQO/z3Fv/SdBl4ILrOZXPpMDcOjKpjd6Skj+gM0tdU7 5mM20+D4pOZlOZ3GnIKRk+sqPmoJwR7BqHIB84MrHLweWf5AESeBKQ4qMTMkepcOTeMz zhXw== X-Forwarded-Encrypted: i=1; AJvYcCUcaGKyFXwFaB5IYlLu+DQ3vS9KTF9zZM//nYTN1RMf7CDChH9zCKHYtLYUjeW7raVyQ0bCddywcf3ZNBIbWCqT0fCt43S5Uu/+APbJEPT7W8I9vYGnOBTgUAlJzAHiWqbXGBLmiLCvvlccDsHjdRhsEf37/D3NJt67koab X-Gm-Message-State: AOJu0YyQk1RdpVwlSdmvoMh4X9Y4i5XC1kfKzWMlaRSiVKn6vSEMaCDp 3LMZW9w3uHdpzS3RxrkHSQ+wVThhuLbZLPmXOo1I2dLWW/cWEYJ9 X-Received: by 2002:a2e:bc08:0:b0:2e2:72a7:843c with SMTP id 38308e7fff4ca-2e95b24dea3mr17566441fa.36.1716556811361; Fri, 24 May 2024 06:20:11 -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-42100fb8f4asm53192065e9.41.2024.05.24.06.20.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 May 2024 06:20:11 -0700 (PDT) From: Paul Durrant X-Google-Original-From: Paul Durrant Message-ID: <9255f42a-7549-4b4f-8654-e0bdc6c643b6@xen.org> Date: Fri, 24 May 2024 14:20:09 +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 09/21] KVM: x86: Fix KVM clock precision in __get_kvmclock() 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-10-dwmw2@infradead.org> Content-Language: en-US Organization: Xen Project In-Reply-To: <20240522001817.619072-10-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 in 'master clock mode' (i.e. when host and guest TSCs are behaving > sanely and in sync), the KVM clock is defined in terms of the guest TSC. > > When TSC scaling is used, calculating the KVM clock directly from *host* > TSC cycles leads to a systemic drift from the values calculated by the > guest from its TSC. > > Commit 451a707813ae ("KVM: x86/xen: improve accuracy of Xen timers") > had a simple workaround for the specific case of Xen timers, as it had an > actual vCPU to hand and could use its scaling information. That commit > noted that it was broken for the general case of get_kvmclock_ns(), and > said "I'll come back to that". > > Since __get_kvmclock() is invoked without a specific CPU, it needs to > be able to find or generate the scaling values required to perform the > correct calculation. > > Thankfully, TSC scaling can only happen with X86_FEATURE_CONSTANT_TSC, > so it isn't as complex as it might have been. > > In __kvm_synchronize_tsc(), note the current vCPU's scaling ratio in > kvm->arch.last_tsc_scaling_ratio. That is only protected by the > tsc_write_lock, so in pvclock_update_vm_gtod_copy(), copy it into a > separate kvm->arch.master_tsc_scaling_ratio so that it can be accessed > using the kvm->arch.pvclock_sc seqcount lock. Also generate the mul and > shift factors to convert to nanoseconds for the corresponding KVM clock, > just as kvm_guest_time_update() would. > > In __get_kvmclock(), which runs within a seqcount retry loop, use those > values to convert host to guest TSC and then to nanoseconds. Only fall > back to using get_kvmclock_base_ns() when not in master clock mode. > > There was previously a code path in __get_kvmclock() which looked like > it could set KVM_CLOCK_TSC_STABLE without KVM_CLOCK_REALTIME, perhaps > even on 32-bit hosts. In practice that could never happen as the > ka->use_master_clock flag couldn't be set on 32-bit, and even on 64-bit > hosts it would never be set when the system clock isn't TSC-based. So > that code path is now removed. > > The kvm_get_wall_clock_epoch() function had the same problem; make it > just call get_kvmclock() and subtract kvmclock from wallclock, with > the same fallback as before. > > Signed-off-by: David Woodhouse > --- > arch/x86/include/asm/kvm_host.h | 4 + > arch/x86/kvm/x86.c | 151 ++++++++++++++++---------------- > 2 files changed, 79 insertions(+), 76 deletions(-) > Reviewed-by: Paul Durrant