From nobody Mon Nov 23 04:34:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1793976001; cv=none; d=zohomail.com; s=zohoarc; b=kFeetju2oU1wsiN/cymKm0N68G6454I46qRgIYos0k8zh+FEfNGmYjGGs/TW3Iht+QYPLStFO2l/KYfaqAJii6uUHjmbMRm8fVSChiO6Lbp1GBPNVZLVpOlYronnDgJfcxVFqKoTRp9mrwHEcDuRYMWd5ETsCiKLMx3z1IppSPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793976001; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=oeQeQeXUQsNpZytmLKma9+O/3mVMklEdjRC9NGx0OYI=; b=nAA4o0pTkKgwlgbYGsql1hNFqUt48u06CnRRrwMf1iFnvfRQ1IvnCh+QrFpwuzWRqnUkLmD2Dj5c9Irs9MMGyBLx5xdroCIW1P3/pwO9N1NDxE0hG31ch2YQAKUeuiRLSGdrN8nqrWwwe5wqGkb0wziQAsLX60VH1x9QHMae6RQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793976001080819.430657034778; Fri, 6 Nov 2026 06:40:01 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPL6c-0000HD-Co; Fri, 06 Nov 2026 09:39:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xPL6T-0000DN-Ui for qemu-devel@nongnu.org; Fri, 06 Nov 2026 09:39:43 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xPL6R-0000hD-Tp for qemu-devel@nongnu.org; Fri, 06 Nov 2026 09:39:41 -0500 Received: from DESKTOP-TUU1E5L.fritz.box (p50892c72.dip0.t-ipconnect.de [80.137.44.114]) by linux.microsoft.com (Postfix) with ESMTPSA id A3E9D20B7167; Fri, 6 Nov 2026 06:39:32 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A3E9D20B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1793975974; bh=oeQeQeXUQsNpZytmLKma9+O/3mVMklEdjRC9NGx0OYI=; h=From:To:Cc:Subject:Date:From; b=ST6vaeuVzxLJOGgpZcsuUTaNIB4PB2v4oB34cl9DgbBF27AfAnr4aXPCzBBWoM1Do 5STi4bd+thCbrV/gG6X/wFNIC1Hn/NxyIKBs+l66NW2fezvFwMjWb6O6krD5bwi+Xd pxPKSHBtaZWdiXl4zTSMn2zcmHh5JDS6iPBYzBwQ= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Paolo Bonzini , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= , Magnus Kulke , Wei Liu , Wei Liu , Feng Liang , Richard Henderson , =?UTF-8?q?Doru=20Bl=C3=A2nzeanu?= Subject: [PATCH] accel/mshv: freeze time during TSC MSR write Date: Fri, 6 Nov 2026 15:39:28 +0100 Message-Id: <20261106143928.443144-1-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=magnuskulke@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1793976002860158500 Content-Type: text/plain; charset="utf-8" A regression was introduced when the HV_X64_REGISTER_TSC MSR state is being written to the hypervisor as part of the migration framework. If the MSR is written without the time being frozen, the tsc_page (a hyper-v enlightenment) is zeroed out and cannot be used by a guest, which results in a performance regression, because the guest falls back to costly reads of HV_X64_MSR_TIME_REF_COUNT. An implementation of pre_resume_vm hook was added to handle the clock lifecycle, relying on the dirty_vcpu state to avoid redundant state writes. Fixes: d540dcc5e28c (target/i386/mshv: migrate MSRs) Signed-off-by: Magnus Kulke Reviewed-by: Doru Bl=C3=A2nzeanu --- accel/mshv/mshv-all.c | 33 ++++++++++++- hw/i386/mshv/clock.c | 111 +++++++++++++++++++++++++++++------------- include/system/mshv.h | 4 +- 3 files changed, 113 insertions(+), 35 deletions(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index d820a67b95..ae7577d407 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -34,6 +34,7 @@ #include "accel/accel-cpu-ops.h" #include "exec/cpu-common.h" #include "system/cpus.h" +#include "system/hw_accel.h" #include "system/runstate.h" #include "system/accel-blocker.h" #include "system/address-spaces.h" @@ -578,7 +579,10 @@ static int mshv_init(AccelState *as, MachineState *ms) =20 register_savevm_live("mshv", 0, 1, &savevm_mshv, s); =20 - mshv_clock_init(); + ret =3D mshv_clock_init(vm_fd); + if (ret < 0) { + return -1; + } =20 return 0; } @@ -927,6 +931,32 @@ static void mshv_cpu_synchronize_pre_loadvm(CPUState *= cpu) run_on_cpu(cpu, do_mshv_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL); } =20 +static void mshv_pre_resume_vm(AccelState *as, bool step_pending) +{ + MshvState *s =3D MSHV_STATE(as); + CPUState *cpu; + int ret, vm_fd =3D s->vm; + + ret =3D mshv_clock_prepare_resume(vm_fd); + if (ret < 0) { + error_report("Failed to prepare clock for resume"); + abort(); + } + + CPU_FOREACH(cpu) { + if (!cpu->vcpu_dirty) { + continue; + } + cpu_synchronize_post_init(cpu); + } + + ret =3D mshv_clock_finish_resume(vm_fd); + if (ret < 0) { + error_report("Failed to finish clock for resume"); + abort(); + } +} + static void do_mshv_cpu_synchronize(CPUState *cpu, run_on_cpu_data arg) { if (!cpu->vcpu_dirty) { @@ -962,6 +992,7 @@ static void mshv_accel_class_init(ObjectClass *oc, cons= t void *data) ac->name =3D "MSHV"; ac->init_machine =3D mshv_init; ac->allowed =3D &mshv_allowed; + ac->pre_resume_vm =3D mshv_pre_resume_vm; } =20 static void mshv_accel_instance_init(Object *obj) diff --git a/hw/i386/mshv/clock.c b/hw/i386/mshv/clock.c index 02c586503a..2a03d3cef9 100644 --- a/hw/i386/mshv/clock.c +++ b/hw/i386/mshv/clock.c @@ -29,6 +29,7 @@ typedef struct MshvClockState { uint64_t ref_time; bool ref_time_pending; + bool time_frozen; } MshvClockState; =20 static MshvClockState mshv_clock; @@ -110,52 +111,85 @@ static int mshv_set_time_freeze(int vm_fd, int freeze) return 0; } =20 -static void mshv_clock_vm_state_change(void *opaque, bool running, - RunState state) -{ - MshvClockState *s =3D opaque; - int vm_fd =3D mshv_state->vm; +static int ensure_time_frozen(int vm_fd) { int ret; =20 - if (running) { - /* Skip if we have nothing to restore, e.g. on initial boot. */ - if (!s->ref_time_pending) { - return; - } + /* Ensure time is frozen */ + if (mshv_clock.time_frozen) { + return 0; + } =20 - ret =3D mshv_set_time_freeze(vm_fd, 1); - if (ret < 0) { - error_report("Failed to freeze partition time on resume"); - abort(); - } + ret =3D mshv_set_time_freeze(vm_fd, 1); + if (ret < 0) { + error_report("Failed to freeze partition"); + return -1; + } + mshv_clock.time_frozen =3D true; =20 + return 0; +} + +int mshv_clock_prepare_resume(int vm_fd) { + return ensure_time_frozen(vm_fd); +} + +int mshv_clock_finish_resume(int vm_fd) { + int ret; + MshvClockState *s =3D &mshv_clock; + + assert(mshv_clock.time_frozen); + + /* Skip if we have nothing to restore, e.g. on initial boot. */ + if (s->ref_time_pending) { /* INVARIANT: reference time can only be written if time is frozen= . */ ret =3D mshv_set_reference_time(vm_fd, s->ref_time); if (ret < 0) { error_report("Failed to restore reference time on resume"); - abort(); + return -1; } + s->ref_time_pending =3D false; + } =20 - if (mshv_set_time_freeze(vm_fd, 0) < 0) { - error_report("Failed to unfreeze partition time on resume"); - abort(); - } + ret =3D mshv_set_time_freeze(vm_fd, 0); + if (ret < 0) { + error_report("Failed to thaw partition"); + return -1; + } + mshv_clock.time_frozen =3D false; =20 - s->ref_time_pending =3D false; - } else { - /* Skip if we already have a to-be-set ref time */ - if (s->ref_time_pending) { - return; - } + return 0; +} =20 - ret =3D mshv_get_reference_time(vm_fd, &s->ref_time); - if (ret < 0) { - error_report("Failed to capture reference time on stop"); - abort(); - } +static void mshv_clock_vm_state_change(void *opaque, bool running, + RunState state) +{ + MshvClockState *s =3D opaque; + int vm_fd =3D mshv_state->vm; + int ret; =20 - s->ref_time_pending =3D true; + if (running) { + return; } + + /* Ensure the time if frozen before we capture the reference time. */ + ret =3D ensure_time_frozen(vm_fd); + if (ret < 0) { + error_report("Failed to freeze partition time on stop"); + abort(); + } + + /* Skip if we already have a to-be-set ref time */ + if (s->ref_time_pending) { + return; + } + + ret =3D mshv_get_reference_time(vm_fd, &s->ref_time); + if (ret < 0) { + error_report("Failed to capture reference time on stop"); + abort(); + } + + s->ref_time_pending =3D true; } =20 /* @@ -182,8 +216,19 @@ static const VMStateDescription vmstate_mshv_clock =3D= { }, }; =20 -void mshv_clock_init(void) +int mshv_clock_init(int vm_fd) { + int ret; + + ret =3D mshv_set_time_freeze(vm_fd, 1); + if (ret < 0) { + error_report("Failed to freeze partition time on init"); + return -1; + } + mshv_clock.time_frozen =3D true; + vmstate_register(NULL, 0, &vmstate_mshv_clock, &mshv_clock); qemu_add_vm_change_state_handler(mshv_clock_vm_state_change, &mshv_clo= ck); + + return 0; } diff --git a/include/system/mshv.h b/include/system/mshv.h index f98ddff9b5..864f2aa34d 100644 --- a/include/system/mshv.h +++ b/include/system/mshv.h @@ -58,7 +58,9 @@ DECLARE_INSTANCE_CHECKER(MshvState, MSHV_STATE, extern MshvState *mshv_state; =20 /* clock (partition reference time) */ -void mshv_clock_init(void); +int mshv_clock_init(int vm_fd); +int mshv_clock_prepare_resume(int vm_fd); +int mshv_clock_finish_resume(int vm_fd); =20 /* interrupt */ int mshv_request_interrupt(MshvState *mshv_state, uint32_t interrupt_type,= uint32_t vector, --=20 2.34.1