Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp407992rdh; Wed, 7 Feb 2024 08:09:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGI0j+nDqqBbsbjFGXZnILW/onkmISPK5LqoGvJ+jA7n1H4flxVJzcDtfI6LsbaouWGNDDK X-Received: by 2002:a05:620a:46a2:b0:785:9549:4a6e with SMTP id bq34-20020a05620a46a200b0078595494a6emr5241916qkb.25.1707322149769; Wed, 07 Feb 2024 08:09:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707322149; cv=pass; d=google.com; s=arc-20160816; b=jVVnHS7pHKdWTSQQnHHG8coDOCLIyYWdm/OA7KCtf5p8PtOQUcxg1u+ToeLyoVu1nk aNDfUzji4DxpgRLwFg9qyoh4lXuFgs8v9Mi6psyxmz60JgPkrtnQBLbVvC5XQZM5ssTN JZCK6nHLnjwYo2w/sglwX9Ib20WsZq/axez7rBC61NJzhix+7//rM6vDYovUzaRzEwaD GT46sC02HEZhxrSbcQIAeaYS/uz/0OXezNF8OUPPwvRXYCW7YOmfUsp54U8BMwSj1jaF rvZa0BNPr88D9xByukHhFlimInIYvhXXsKLi7wqDgE1AgG481vrX0zLMhnxNTl3aREFJ A+3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=b6Pbg0aSosy9STDoBERpFBWvqP2/G4p3nZSShoKz0EA=; fh=819e6uvieSC9WDYZmS/E1v2QBVzcyPou5wlZ4FQovog=; b=OAri5IDwQPRoEVHINz4fUEsOHy5lLB/JzRvDEg+7cihy4GF60FnUSzgIE0jtLDcQAj 4vTe86WGp7zttn7+qUMo4s3MTcHkiEAopZwXECYI+2ZoB3kOxySaSB8S3CmqMqmbhkVs KfYZELGw48srdnXtpd/iox/1JGcp/i+3xorUbc5Lfpncw6Zf+IB6uleXIS2+wOg1h68/ RHSKtilUpgMf3o93VprSaxL05nCaXPHjW4vVK6l42sbFmr8i/KOxxxvF6hgIZ60icO4b 0OMOq62+wGNmdFYVKOe7sX6raw8figG/Ladd5o/MIlNF75QT1sMYjMDSYIJfk5Ac5xSU BrDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Hes8HIdE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56753-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56753-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVWdufDdzpy6h12hOdiqhMD8gyNho1mFfLN5ARrPjeMof6JxU0Qx9xmka/v8jC/Wduw0UfL1vnr9eAad7ZsFbpxJ0wo/sk7h8qThdT0DA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a15-20020a05620a16cf00b00783cc18b28bsi1330363qkn.565.2024.02.07.08.09.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 08:09:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56753-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Hes8HIdE; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-56753-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56753-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 7595C1C24218 for ; Wed, 7 Feb 2024 16:09:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1D6D7811E1; Wed, 7 Feb 2024 16:08:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Hes8HIdE" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 9ED588005F for ; Wed, 7 Feb 2024 16:08:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707322137; cv=none; b=Mof/WoOSKD/r8Ycx2WQ9XVZA0ReuqrqRT7WzKyGgi3zHd9RPy+JcBSB596CwIgZ2shDVihjk7bSXEj23uzphNpcmhUngMV9qaBOGxPJssCUJvII1tGWzT7VWUvvmx9I8QAGnjEuYydOjooXGnbkKTanhvtbxQJjJuRVEmfrZl0Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707322137; c=relaxed/simple; bh=b6Pbg0aSosy9STDoBERpFBWvqP2/G4p3nZSShoKz0EA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RPv3BvOPRNY4p9PtgOyiRRezHXIneAQf2zWf3EIbD3AmyQ0iVYmEktsxMiefHYOEVJqU2KB3nwAt+Wc2gdBS/xL839Qj8WU89+N6bby+yxyblWQxOO71weNiInNr68pq9MR+Xe1LizxytsUqCiUwSwAZDZawo4g8N8m6tBfScDU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Hes8HIdE; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26eef6cso1078586276.3 for ; Wed, 07 Feb 2024 08:08:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707322134; x=1707926934; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=b6Pbg0aSosy9STDoBERpFBWvqP2/G4p3nZSShoKz0EA=; b=Hes8HIdExo5hJfKu7G1taJvHisMdFqJ0TM3RtEQZ63NsbKXrEjkO1+YTQa1uTvhfiw jB1/5h3x4Q9uI/RoSHXQb+jRtTilb+X/8sjVD9pMJI6984Cugjg+cTvgmpL4dwwD2SkD bXAmq3JBmuwMGu8yiPPqSOzy5MFuyLYXmjAdpPmSodO8LJmpdWAtsGCrKBR0FysDb8zG ksjQcgfIVZ67tvHp72bj3m4wEV3XvdeLs/ZgkSXa1o4Jwv2D6Wcj0N3dzAc06qEvpahi AhXI06iOI+5P8p91phUyp/nX0VR2bUYjgPwvtuTX7R0HFcaYeSS5CYJL6/IIkMehZhfh ICFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707322134; x=1707926934; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=b6Pbg0aSosy9STDoBERpFBWvqP2/G4p3nZSShoKz0EA=; b=vEEkkUHYt6bPGdJf/narmO2neyUko0DqFZialmaVlAk1+OEidftFDRhYzO69ntQVdz rA8VnrBiZvfGz+11VHIzyYR1Li9AA+IancXC6Avqyvf/MQZKMxua8R669py/QaXz2Njt 3bQS50j4hxjzrCSXOR6DjAiZv5uJgGLD6qjBS9mhttY9cVsLsarAHqpjMeQmS2g+yuTJ 5o7ozFSkTIfSXzmTM1vj7524hZSjIp16aPrYGJZVhFheC5jcqpstywXdXkEdDFHcomAv tHpnkkLDD6rrp1qoJiitmKA8WUx7gZVpfEGyorZ1/3V3z+Js1iQZYAawwBMNsAxXdeAP e1Zw== X-Gm-Message-State: AOJu0YyQCANSnhVXpAg5s/M15n74+vOcn01a8meyIWaGLBzSHzXew8Lx geASldt/87MtFzLYwfoo+kNk8O8qFer1vM8nVx8D8uaDtIe9ADRfamYVjwqltSYoEUTvBgAMUwN jCA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2293:b0:dc6:207c:dc93 with SMTP id dn19-20020a056902229300b00dc6207cdc93mr194834ybb.2.1707322134581; Wed, 07 Feb 2024 08:08:54 -0800 (PST) Date: Wed, 7 Feb 2024 08:08:52 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: Message-ID: Subject: Re: [PATCH v3] KVM: x86/xen: improve accuracy of Xen timers From: Sean Christopherson To: David Woodhouse Cc: kvm@vger.kernel.org, linux-kernel , Paul Durrant , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Thu, Dec 14, 2023, David Woodhouse wrote: > From: David Woodhouse >=20 > A test program such as http://david.woodhou.se/timerlat.c=C2=A0confirms u= ser > reports that timers are increasingly inaccurate as the lifetime of a > guest increases. Reporting the actual delay observed when asking for > 100=C2=B5s of sleep, it starts off OK on a newly-launched guest but gets > worse over time, giving incorrect sleep times: >=20 > root@ip-10-0-193-21:~# ./timerlat -c -n 5 > 00000000 latency 103243/100000 (3.2430%) > 00000001 latency 103243/100000 (3.2430%) > 00000002 latency 103242/100000 (3.2420%) > 00000003 latency 103245/100000 (3.2450%) > 00000004 latency 103245/100000 (3.2450%) >=20 > The biggest problem is that get_kvmclock_ns() returns inaccurate values > when the guest TSC is scaled. The guest sees a TSC value scaled from the > host TSC by a mul/shift conversion (hopefully done in hardware). The > guest then converts that guest TSC value into nanoseconds using the > mul/shift conversion given to it by the KVM pvclock information. >=20 > But get_kvmclock_ns() performs only a single conversion directly from > host TSC to nanoseconds, giving a different result. A test program at > http://david.woodhou.se/tsdrift.c=C2=A0demonstrates the cumulative error > over a day. >=20 > It's non-trivial to fix get_kvmclock_ns(), although I'll come back to > that. The actual guest hv_clock is per-CPU, and *theoretically* each > vCPU could be running at a *different* frequency. But this patch is > needed anyway because... >=20 > The other issue with Xen timers was that the code would snapshot the > host CLOCK_MONOTONIC at some point in time, and then... after a few > interrupts may have occurred, some preemption perhaps... would also read > the guest's kvmclock. Then it would proceed under the false assumption > that those two happened at the *same* time. Any time which *actually* > elapsed between reading the two clocks was introduced as inaccuracies > in the time at which the timer fired. >=20 > Fix it to use a variant of kvm_get_time_and_clockread(), which reads the > host TSC just *once*, then use the returned TSC value to calculate the > kvmclock (making sure to do that the way the guest would instead of > making the same mistake get_kvmclock_ns() does). >=20 > Sadly, hrtimers based on CLOCK_MONOTONIC_RAW are not supported, so Xen > timers still have to use CLOCK_MONOTONIC. In practice the difference > between the two won't matter over the timescales involved, as the > *absolute* values don't matter; just the delta. >=20 > This does mean a new variant of kvm_get_time_and_clockread() is needed; > called kvm_get_monotonic_and_clockread() because that's what it does. >=20 > Fixes: 536395260582 ("KVM: x86/xen: handle PV timers oneshot mode") > Signed-off-by: David Woodhouse > --- Dagnabbit, this one is corrupt too :-/