From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.13 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796362585; b=JHjrrnm4f0AHVzvd+7e5POn3OyD867RaTmIYJzQE8FtDY7j2KcnNZXaVh/yN7hKZ5uTs U7wfYAaX25vC7/MZZBCVdJGNxdUc9eei0dHmWWy7Qj/1oxctOWlKZm6ut9/WrX3R/ueKx 24Uk1KMYmki4NHvhX/+g1B+0lIodXgpkxkmka+OOsyUXx0ZuT6aHZYu2KP1Kn8E4AWQLq 7hGhuDevucx830lctkxetnNTdSJQCZqCN1FTJMupCppGd82a90uSqBEhOkKw7aLfztmzq L6bfbx05ufZBLTC4rUv/JLZ3EFUKvoiEi9S0qTP8N07RxXbfVvBT9ADvi5amaNMffRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796362585; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:Content-Transfer-Encoding:In-Reply-To; bh=uS2whmcTq2uADUeQjOn03hWSrELfS17fQDfBjRkL/7Q=; b=JwNukCroufeS2UnkzME5rQc772AtLmlE8uYBP8KsbKFKLYM1sf4OkMsTIlE7QG1k2e1a x8eebfXg8mpxNabIesklc0Y3tJYCiaKRMXPq5chDpb59kdGk8L31mHugx0VATVk+lwOy/ VrZWaqBSTPXUAi0zSVDx48tXhLOR78mXEWVsVHWukkSek2wRkjWKOl9+CCZKo3GK/r3Zl iv+DYD+52BSNs6VK1D8oWo5domIHZrkzJf5vCR7xK5KsOBeqhW7wMWX2XZCSARGu6MJ4p NASe7svpqPNIumpnKnMOA1hLUMF1xY7pnqpOu8yXas6tRy5fRhSyD6Mr/o72AFQfgFg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Odr1b842; arc=none smtp.remote-ip=192.198.163.13 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.13]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB99F606A32D; Fri, 4 Dec 2026 05:36:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AB99F606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Odr1b842 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796362585; x=1827898585; h=date:from:to:cc:subject:message-id:references: mime-version:content-transfer-encoding:in-reply-to; bh=uS2whmcTq2uADUeQjOn03hWSrELfS17fQDfBjRkL/7Q=; b=Odr1b842zD+RD3drCG9ljTghmLhCaLIs9Fz7vBQLPYQHpvg2pQEYQeUm cozT6VWs9J3Y90mXUBGk6KIJfjpPjfLRzqAfzRDH0Kg0LQCKRozoc05iu apahaaz63E6YJ3PMju1VK88O07SrMjpi7O2ERflPF3A4SD+1/YhEl3pEM wdlpcVxG/K+3RpCwgaM5yS4zJTA2QEObs3rSGSNlBl4cWUQGxC0G1MXYw pprnQD/a1etvmtAS59s4EeQCL3KU/W5xrMYuST3CfHouq+bDQiIPaWGgp XIde1mfghkmTpdLfwu19TJhZuY1vAQGZ7iuOpTO7D2uu06giPucozEepY A==; X-CSE-ConnectionGUID: OdxKFahFSMGDHgp3A8/34A== X-CSE-MsgGUID: 9f8ithyJT4qzU1Sa07/5pg== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5310627" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5310627" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by fmvoesa107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 21:36:24 -0800 X-CSE-ConnectionGUID: WDfeV1VUQEWbBmXjdNqLEg== X-CSE-MsgGUID: SzSEYh1/RQu9ATym4w0Z1g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5483387" Received: from zhaoliu1-desk.sh.intel.com (HELO localhost) ([10.112.226.173]) by orviesa010.jf.intel.com with ESMTP; 03 Dec 2026 21:36:21 -0800 Date: Fri, 4 Dec 2026 13:36:20 +0800 From: Zhao Liu To: Jim Mattson , Chao Gao Cc: Paolo Bonzini , Sean Christopherson , kvm@vger.kernel.org, isaku.yamahata@gmail.com, linux-kernel@vger.kernel.org, Zhao Liu Subject: Re: [PATCH v2 00/36] KVM: VMX APIC timer virtualization support Message-ID: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Hi Jim, Chao, I've been evaluating these optimization ideas on CWF. Here are my thoughts and what I found. > >I'm not a fan of the hv_timer approach. One major difference between > >the VMX preemption timer and the virtual TSC deadline MSR is that the > >VMX preemption timer has to be reprogrammed every time that KVM > >switches from hrtimer to hv_timer. > > For the preemption timer value written to the VMCS, yes. But > hv_deadline_tsc (the absolute host TSC deadline, set in vmx_set_hv_timer()) > doesn't necessarily need recomputation across hrtimer/hv_timer switches. > > If this reprogram/recomputation cost matters, we can optimize the > block/unblock path for the preemption timer to avoid that, and then the > same optimization would apply naturally to APIC timer virtualization. > > The idea is: don't call cancel_hv_timer() when switching to hrtimer and > don't call set_hv_timer() when switching back. cancel_hv_timer() can > be deferred to virtual interrupt injection time (alongside setting > tscdeadline to 0, which means disarming the timer architecturally). > I think this works for TSC-deadline and oneshot modes; periodic mode > needs more thought since the timer needs to be re-armed after each expiry. For the virtual TSC deadline timer, the existing series already avoids the reprogramming. The patch 3, "KVM: x86/lapic: Start/stop sw/hv timer on vCPU un/block" leaves the guest deadline armed while the vCPU is blocked. The host hrtimer is only used to wake the vCPU, and the virtual timer is disarmed only when the interrupt is actually injected, which is essentially what Chao described above. So the guest deadline isn't reprogrammed across block/unblock during normal operation. Reusing hv_timer is mainly about sharing the existing lapic interface for switching between the hrtimer and hardware timers (preemption timer or virtual apic timer). > >The virtual TSC deadline timer > >never needs reprogramming during normal operation. > > >Another difference > >is that KVM should never inject a virtual local APIC timer interrupt > >when the virtual TSC deadline timer is in use. Even if the vCPU was > >blocked and the hrtimer fired to wake it up, KVM should just let the > >hardware deliver the virtual interrupt on the next VM-entry. I tried the "let hardware deliver it" approach as a PoC: after the wakeup, KVM keeps the expired deadline armed (of virtual apic timer) and leaves the interrupt to the next VM-entry. Functionally it works, but it needed the extra helper/interface Chao listed below. > I agree this is the ideal behavior. But I don't see what's wrong with > KVM delivering the interrupt. > > IMO, letting hardware deliver the interrupt after block would require: > > 1. We still need to mark the timer as pending to wake the vCPU from > block. But to let hardware do the injection on VM-entry, we'd > then need to consume the pending event without actually injecting > the interrupt. Right, in my PoC this is needed. The wakeup check has to treat an armed, expired virtual deadline as a pending event, otherwise the vCPU can go back to blocking. > 2. Tweaking kvm_wait_lapic_expire(). The deadline was advanced when > arming the hrtimer, so the vCPU wakes slightly before the actual > deadline. Currently kvm_wait_lapic_expire() scans vIRR to decide > whether to busy-wait until the deadline, but if we rely on > hardware to deliver the interrupt, the vector won't be in vIRR > and we'd need a different indicator. Yes, kvm_wait_lapic_expire() also needs a separate indicator, because the vector is no longer in vIRR. > Given this, I'd prefer to let KVM inject the interrupt for this specific > "hrtimer fired to wakeup" case — it works correctly as is and avoids the > additional complexity. Overall, there aren't many changes to the code. But what's important and interesting is, it actually hurts latency on this path :-(. In my measurements, timer latency on the halt-wakeup path was worse ( around ~10% timer latency regression) than when KVM injects the interrupt itself. The reason is where the work lands relative to the deadline. With lapic timer advance (timer_advance_ns), KVM wakes the vCPU slightly early and busy-waits until the deadline just before VM-entry: - When KVM injects the interrupt, the injection happens before that busy-wait, so its cost is hidden in the advance window. At the deadline, VM-entry only has to deliver an interrupt that is already pending. - When hardware delivers it (via virtual apic timer), the processor can only recognize the expired deadline and post the interrupt after VM entry, i.e. after the deadline has already passed. That work then sits directly on the latency critical path. On this path the hrtimer has already done the real work, which is waking the vCPU. Doing the injection in software as well costs nothing extra on the critical path, whereas handing it to hardware adds latency. Therefore, I think the following two cases need for different ways: 1). When the vCPU is running: this is for sure where the virtual APIC timer delivers the best performance. 2). When the vCPU is blocked and needs to be woken up: the host timer fires early (timer advance), and having KVM inject the interrupt hides that cost before the deadline, so software injection is the better choice. Thanks, Zhao