Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6807041rdb; Tue, 2 Jan 2024 14:22:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGQmD0/Dzjg80wS9ztzetXqpX8ZrL6+z8t9zSjZynVsB5QBPhfjJD6k9geNezSWcvgeJDYu X-Received: by 2002:adf:ea10:0:b0:333:2fd2:5d5d with SMTP id q16-20020adfea10000000b003332fd25d5dmr11938865wrm.143.1704234135149; Tue, 02 Jan 2024 14:22:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704234135; cv=none; d=google.com; s=arc-20160816; b=xPdfdcsiHq12J3XZtEtcxOPDJ5USiOlBC/uQj1YABmXmklRcmSPlk8Z1DbZWwGe4kp KvQw9O5MI10IBy274m+DpIV//q7RL+97j6znS2n34nVFMIj3YQVcEeopx0G5FCPyJpY/ WgKlu3aQQvmWO6z24Q6Op1E5iQ8PuiQTr+zCcd8VP649Eym3mT6M9FE7i7Vg9Zkg2oJw ZjupIi0/ChsftXUU+WNtiSwNTYfhk/hppokeMpoUio7UW89N0ve89CDHHyh8t9mW1E8J RQ3HphzSCx6OlhPT2D5qCI7fPQmVsfoF2E00cO0zjsqPGN4A346zMVBF8fc+OA2eoyr7 eS5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :date:cc:to:from:subject:message-id:dkim-signature; bh=RshiW/YYDsnsJFyUGAY3lZJunQEflUHgqX3fmfeIPig=; fh=hyaP96c/uSsoY+1aE41h9w9FlhSu6L9WKOnutZ5DoCs=; b=o8HGLyPgXoP9cRSF+MVrjsbdBpq+OSTnq7R7yHuUCDAfOryvoMMW6qgNbq/HVzu28U LMNMh4aKLcsl8uyK9Em5xVmC1ZOlMH5LU6YXv+DjS3OT/ykhxJVdmgKWGhfATpVZzHrx jtkoajBYJXBrV6vRh4vlfQhSIkVx1IEItj6NzFK2y1kF7SSvtNuYyDCV7GnZcEOMpJDK qsRNhQT6xZRYlauVogPf5Rn3+U6ihfO1lkPF/LtNPBBrp2JhX2YjWqtyFsdJCWH1VZyD 8SMy4QNWrSbrvKPLk08HEOGwQi0wq65nEZRUzalg/dW98JKk3YPpxrfwvE1dHL5bncGE jJqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OFw+aqMg; spf=pass (google.com: domain of linux-kernel+bounces-14933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id s6-20020a508dc6000000b0055539b908a6si6341600edh.95.2024.01.02.14.22.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 14:22:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-14933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OFw+aqMg; spf=pass (google.com: domain of linux-kernel+bounces-14933-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-14933-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 ADC661F2346A for ; Tue, 2 Jan 2024 22:22:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 743F4171D7; Tue, 2 Jan 2024 22:21:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OFw+aqMg" X-Original-To: linux-kernel@vger.kernel.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 90469171BB for ; Tue, 2 Jan 2024 22:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704234114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RshiW/YYDsnsJFyUGAY3lZJunQEflUHgqX3fmfeIPig=; b=OFw+aqMgPiJ+PdzsXosLmlauazCgwsu1urt2PJd9oS9vc9BknKT01zn6N7L8DGIa8awogi HhDeYaqbvYhx5TyYHSCMqgW/zCZGlpA61gaNck/Mjybap844Wk53Zmlb648E5kohScy1/z UWf2oR5J9yN1c5q3fGpS4qFNyxh/O+o= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-240-HwQH1ykqPnmhPBBZ9fdsgA-1; Tue, 02 Jan 2024 17:21:53 -0500 X-MC-Unique: HwQH1ykqPnmhPBBZ9fdsgA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-336a41f0cc3so5942382f8f.2 for ; Tue, 02 Jan 2024 14:21:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704234112; x=1704838912; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RshiW/YYDsnsJFyUGAY3lZJunQEflUHgqX3fmfeIPig=; b=Y8O7LUQMTS94GXThmyjQLwaCthT5fWcAhuTHaTPEhtHctDMpy10PwykgmAetKc2kMm V8GytQE9hqox+8SDaUUcGdRsbggbz0Etek9Wy1uoiOnJwfz8WnTS81tiq2MUdsXdeqMJ duLzALf1h3/1WSFD1VZkPusXt0uQZueTG6w+SjDq3QmxBDfsZs8sqj1jT++WmmOZhwkZ 75Cr2PgMsnW3YS6pEn4hsZCX+VXotwP/8x69KUiBVqAsyFEiSB7dz2XEZPSbKSdJ5GCq NHq9ZUwoSjg8l4JbXJ7wYAQZcCsSdbuGBlC7HaW2DnBzoZQNuVs6YgirU4Onq572ATwL uEpQ== X-Gm-Message-State: AOJu0YyUj2c7gE6DzzBV9yL3+FcwIVzifBBJlz0Y8mt6KpBDIinDfkRg Ly77dIvn6DD/k6uiKvqMR9J3S3f2erMSDDHXfYRooOUAHVjb6g7PZQXdyGRTqsNPJAlhXMTYPt1 SRuqOLvgmFsZjrjboitGrlp0T2Tp9SnFF X-Received: by 2002:a05:600c:348b:b0:40d:900a:73fb with SMTP id a11-20020a05600c348b00b0040d900a73fbmr46500wmq.65.1704234112070; Tue, 02 Jan 2024 14:21:52 -0800 (PST) X-Received: by 2002:a05:600c:348b:b0:40d:900a:73fb with SMTP id a11-20020a05600c348b00b0040d900a73fbmr46498wmq.65.1704234111753; Tue, 02 Jan 2024 14:21:51 -0800 (PST) Received: from starship ([147.235.223.38]) by smtp.gmail.com with ESMTPSA id l26-20020a1c791a000000b0040d839de5c2sm303457wme.33.2024.01.02.14.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 14:21:51 -0800 (PST) Message-ID: <481be19e33915804c855a55181c310dd8071b546.camel@redhat.com> Subject: Re: RFC: NTP adjustments interfere with KVM emulation of TSC deadline timers From: Maxim Levitsky To: Jim Mattson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Marc Zyngier , Thomas Gleixner , Vitaly Kuznetsov Date: Wed, 03 Jan 2024 00:21:49 +0200 In-Reply-To: References: <20c9c21619aa44363c2c7503db1581cb816a1c0f.camel@redhat.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Thu, 2023-12-21 at 11:09 -0800, Jim Mattson wrote: > On Thu, Dec 21, 2023 at 8:52 AM Maxim Levitsky wrote: > > > > Hi! > > > > Recently I was tasked with triage of the failures of 'vmx_preemption_timer' > > that happen in our kernel CI pipeline. > > > > > > The test usually fails because L2 observes TSC after the > > preemption timer deadline, before the VM exit happens. > > > > This happens because KVM emulates nested preemption timer with HR timers, > > so it converts the preemption timer value to nanoseconds, taking in account > > tsc scaling and host tsc frequency, and sets HR timer. > > > > HR timer however as I found out the hard way is bound to CLOCK_MONOTONIC, > > and thus its rate can be adjusted by NTP, which means that it can run slower or > > faster than KVM expects, which can result in the interrupt arriving earlier, > > or late, which is what is happening. > > > > This is how you can reproduce it on an Intel machine: > > > > > > 1. stop the NTP daemon: > > sudo systemctl stop chronyd.service > > 2. introduce a small error in the system time: > > sudo date -s "$(date)" > > > > 3. start NTP daemon: > > sudo chronyd -d -n (for debug) or start the systemd service again > > > > 4. run the vmx_preemption_timer test a few times until it fails: > > > > > > I did some research and it looks like I am not the first to encounter this: > > > > From the ARM side there was an attempt to support CLOCK_MONOTONIC_RAW with > > timer subsystem which was even merged but then reverted due to issues: > > > > https://lore.kernel.org/all/1452879670-16133-3-git-send-email-marc.zyngier@arm.com/T/#u > > > > It looks like this issue was later worked around in the ARM code: > > > > > > commit 1c5631c73fc2261a5df64a72c155cb53dcdc0c45 > > Author: Marc Zyngier > > Date: Wed Apr 6 09:37:22 2016 +0100 > > > > KVM: arm/arm64: Handle forward time correction gracefully > > > > On a host that runs NTP, corrections can have a direct impact on > > the background timer that we program on the behalf of a vcpu. > > > > In particular, NTP performing a forward correction will result in > > a timer expiring sooner than expected from a guest point of view. > > Not a big deal, we kick the vcpu anyway. > > > > But on wake-up, the vcpu thread is going to perform a check to > > find out whether or not it should block. And at that point, the > > timer check is going to say "timer has not expired yet, go back > > to sleep". This results in the timer event being lost forever. > > > > There are multiple ways to handle this. One would be record that > > the timer has expired and let kvm_cpu_has_pending_timer return > > true in that case, but that would be fairly invasive. Another is > > to check for the "short sleep" condition in the hrtimer callback, > > and restart the timer for the remaining time when the condition > > is detected. > > > > This patch implements the latter, with a bit of refactoring in > > order to avoid too much code duplication. > > > > Cc: > > Reported-by: Alexander Graf > > Reviewed-by: Alexander Graf > > Signed-off-by: Marc Zyngier > > Signed-off-by: Christoffer Dall > > > > > > So to solve this issue there are two options: > > > > > > 1. Have another go at implementing support for CLOCK_MONOTONIC_RAW timers. > > I don't know if that is feasible and I would be very happy to hear a feedback from you. > > > > 2. Also work this around in KVM. KVM does listen to changes in the timekeeping system > > (kernel calls its update_pvclock_gtod), and it even notes rates of both regular and raw clocks. > > > > When starting a HR timer I can adjust its period for the difference in rates, which will in most > > cases produce more correct result that what we have now, but will still fail if the rate > > is changed at the same time the timer is started or before it expires. > > > > Or I can also restart the timer, although that might cause more harm than > > good to the accuracy. > > > > > > What do you think? > > Is this what the "adaptive tuning" in the local APIC TSC_DEADLINE > timer is all about (lapic_timer_advance_ns = -1)? Hi, I don't think that 'lapic_timer_advance' is designed for that but it does mask this problem somewhat. The goal of 'lapic_timer_advance' is to decrease time between deadline passing and start of guest timer irq routine by making the deadline happen a bit earlier (by timer_advance_ns), and then busy-waiting (hopefully only a bit) until the deadline passes, and then immediately do the VM entry. This way instead of overhead of VM exit and VM entry that both happen after the deadline, only the VM entry happens after the deadline. In relation to NTP interference: If the deadline happens earlier than expected, then KVM will busy wait and decrease the 'timer_advance_ns', and next time the deadline will happen a bit later thus adopting for the NTP adjustment somewhat. Note though that 'timer_advance_ns' variable is unsigned and adjust_lapic_timer_advance can underflow it, which can be fixed. Now if the deadline happens later than expected, then the guest will see this happen, but at least adjust_lapic_timer_advance should increase the 'timer_advance_ns' so next time the deadline will happen earlier which will also eventually hide the problem. So overall I do think that implementing the 'lapic_timer_advance' for nested VMX preemption timer is a good idea, especially since this feature is not really nested in some sense - the timer is just delivered as a VM exit but it is always delivered to L1, so VMX preemption timer can be seen as just an extra L1's deadline timer. I do think that nested VMX preemption timer should use its own value of timer_advance_ns, thus we need to extract the common code and make both timers use it. Does this make sense? Best regards, Maxim Levitsky > If so, can we > leverage that for the VMX-preemption timer as well? > > Best regards, > > Maxim Levitsky > > > > > >