From nobody Mon Nov 23 04:44:06 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=1794429778; cv=none; d=zohomail.com; s=zohoarc; b=TSE/ZrIIdMvfrmjOD77hbUP6hTl1tbiG6oCFPPppcgotl4fTrzSULv6zgzv9dNUDVh4yZMwJ0e1qkE61qVAZ3mbpKaJts/czVXuFxwVHEJ7HV3/FX9u4nYKatBx2xMdNU8r4/OwrQ1J8B+hsT7efzeeSQZg4fC+WybtMmcOJPNA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794429778; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=oN0BFQE0r9iYoJZoaoOolLeu+FIg48QtzzEE8ItBUdI=; b=i6gSXP3oWG1GjZ3+xd+2zlsJ+k6V4sShqE0Q4q3b/9crBppb31o+RbpS2Yw3mMp99fs4+/04KWxkI3wqeBYK2VesEza9SrNHIHvOPgCl7WixQCbv+JsoNrYCKfeBqbbkx/S86cMNFFzrJUDo2Bx61FoBDFYZDCzNWKqIHoLwe5o= 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 1794429778423600.7361117876627; Wed, 11 Nov 2026 12:42:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9G-0001PL-LX; Wed, 11 Nov 2026 15:42:26 -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 1xRF9E-0001Nv-HO for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:24 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9C-0005w6-QF for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:24 -0500 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.43]) by linux.microsoft.com (Postfix) with ESMTPSA id 009C120B7168; Wed, 11 Nov 2026 12:42:16 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 009C120B7168 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1794429738; bh=oN0BFQE0r9iYoJZoaoOolLeu+FIg48QtzzEE8ItBUdI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nA8A6r7/Mp3NcmhTLYHnEDpFrVQAg9TrTRWXnAcaeXv9XKnyTWMM3BYCeTUMKA2vq mwZuMqikCy2cRkyGkp8rhcFHEEZK9saIW5lZa0KwrlxcNu85iabKvd93i1RFc5jxM5 TN3Xg0ZHuDprZ1mJnD2Y5wDzHclZvYTlZW+cHn6Q= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , Doru Blanzeanu , Magnus Kulke , Paolo Bonzini , Richard Henderson Subject: [PULL 1/4] accel/mshv: skip double unmap of mem regions Date: Wed, 11 Nov 2026 21:41:52 +0100 Message-Id: <20261111204155.36683-2-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> References: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: 1794429779108158500 This change addresses a regression that was introduced when the dynamic mapping handler was removed, from the MSHV controller, breaking OVMF support. We introduce a small uaddr-indexed table to track mapped regions. The current logic turns an add into a remove op if a rom-device region is currently not in romd_mode. We could ignore both map + unmap for this condition, but this misses a the case of a region transitioning from romd_mode=3Dtrue =3D> false. In this case we still do want to unmap a region. Fixes: 626e5dc99935 ("accel/mshv: Remove remap overlapping mappings code") Signed-off-by: Magnus Kulke Reviewed-by: Doru Bl=C3=A2nzeanu --- accel/mshv/mem.c | 41 ++++++++++++++++++++++++++++++++++++--- accel/mshv/mshv-all.c | 1 + include/system/mshv_int.h | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/accel/mshv/mem.c b/accel/mshv/mem.c index 820f87ef0c..00d69fca8d 100644 --- a/accel/mshv/mem.c +++ b/accel/mshv/mem.c @@ -13,6 +13,7 @@ #include "qemu/osdep.h" #include "qemu/error-report.h" #include "qapi/error.h" +#include "qemu/main-loop.h" #include "linux/mshv.h" #include "system/address-spaces.h" #include "system/mshv.h" @@ -168,6 +169,25 @@ static hwaddr align_section(MemoryRegionSection *secti= on, hwaddr *start) return (size - delta) & qemu_real_host_page_mask(); } =20 +static bool uaddr_is_mapped(MshvMemoryListener *mml, hwaddr start_addr) +{ + assert(bql_locked()); + + return g_hash_table_contains(mml->mapped_regions, + GINT_TO_POINTER(start_addr)); +} + +static void track_region(MshvMemoryListener *mml, hwaddr start_addr, bool = add) +{ + assert(bql_locked()); + + if (add) { + g_hash_table_add(mml->mapped_regions, GINT_TO_POINTER(start_addr)); + } else { + g_hash_table_remove(mml->mapped_regions, GINT_TO_POINTER(start_add= r)); + } +} + void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *secti= on, bool add) { @@ -176,15 +196,19 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, Memor= yRegionSection *section, bool writable =3D !area->readonly && !area->rom_device; hwaddr start_addr, mr_offset, size; void *ram; + bool is_mapped; MshvMemoryRegion mshv_mr =3D {0}; =20 size =3D align_section(section, &start_addr); trace_mshv_set_phys_mem(add, section->mr->name, start_addr); =20 /* - * If the memory device is a writable non-ram area, we do not - * want to map it into the guest memory. If it is not a ROM device, - * we want to remove mshv memory mapping, so accesses will trap. + * ROM devices (e.g. pflash) have ram=3Dfalse, rom_device=3Dtrue. + * In romd_mode, they behave like RAM and should be mapped. + * Outside romd_mode, accesses should trap to QEMU for emulation. + * + * For non-RAM, non-rom_device regions (writable MMIO), we never map. + * For rom_device regions not in romd_mode, we want them unmapped. */ if (!memory_region_is_ram(area)) { if (writable) { @@ -198,6 +222,15 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, Memory= RegionSection *section, return; } =20 + is_mapped =3D uaddr_is_mapped(mml, start_addr); + + if (add && is_mapped) { + return; + } + if (!add && !is_mapped) { + return; + } + mr_offset =3D section->offset_within_region + start_addr - section->offset_within_address_space; =20 @@ -213,6 +246,8 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryR= egionSection *section, error_report("Failed to set memory region"); abort(); } + + track_region(mml, start_addr, add); } =20 static int enable_dirty_page_tracking(int vm_fd) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index d820a67b95..be3abb0824 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -479,6 +479,7 @@ static void register_mshv_memory_listener(MshvState *s,= MshvMemoryListener *mml, =20 mml->listener =3D mshv_memory_listener; mml->listener.name =3D name; + mml->mapped_regions =3D g_hash_table_new(g_direct_hash, g_direct_equal= ); memory_listener_register(&mml->listener, as); for (i =3D 0; i < s->nr_as; ++i) { if (!s->as[i].as) { diff --git a/include/system/mshv_int.h b/include/system/mshv_int.h index 1f77ffd152..89b44f387a 100644 --- a/include/system/mshv_int.h +++ b/include/system/mshv_int.h @@ -49,6 +49,7 @@ struct AccelCPUState { =20 typedef struct MshvMemoryListener { MemoryListener listener; + GHashTable *mapped_regions; int as_id; } MshvMemoryListener; =20 --=20 2.34.1 From nobody Mon Nov 23 04:44:06 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=1794429774; cv=none; d=zohomail.com; s=zohoarc; b=nwuiQ8aLgGG8voqvL+ZBhhjA4fh596ZiO7JWIDx7GERp2y4PMGQTxF9eFN9ZoK0kpA2Cw47fMCeMkJLPp8SUsRL8i9Ix5x0LKAv8Qf7iXDad0RM2AU9h8rQeEeckO4tTzR5LA+BBcImWlEn4rKTbb2bHwPEclrJfDCk+/hTLckY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794429774; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=4wI5YIcOwzY6HBpD8sMmTioh0AxAuhQ6aYkQ1hEXT5o=; b=gSogJ5xo60u2Jh24CrdIla4LNh7tcgM/JdejLAfCGCKRd5uub+mJ+hZOFP2VhH2E9WB5w2xoZoncSjWDmz9vT+KE+LK2cvxqNhBEaE9Blx4WizK49MB3lY1Sa67rP8jG8L722DW09pO5RSLdtIj4/lNRxsfSc/W3X+uTei9veCM= 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 1794429774034400.9957891432573; Wed, 11 Nov 2026 12:42:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9K-0001aX-80; Wed, 11 Nov 2026 15:42:30 -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 1xRF9H-0001Pu-Sk for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:28 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9G-0005wJ-DX for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:27 -0500 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.43]) by linux.microsoft.com (Postfix) with ESMTPSA id 2486B20B7166; Wed, 11 Nov 2026 12:42:19 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2486B20B7166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1794429741; bh=4wI5YIcOwzY6HBpD8sMmTioh0AxAuhQ6aYkQ1hEXT5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MCYHCntcMC364/6r5Lak0qpDhjYS3mrCuVn8lmQlxPgUJBlswT0Dr0hHI0hlvCX1w aVwW23o/F3+qH4wy2sKroEPQJIxbqMqzXL7lmfxeTYem9kKkKbz0Sa5te+0rMNzqD4 G9uOxxZ766b3IvE6rptqHFC5K/vqJ3Qhf33eoLYM= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , Doru Blanzeanu , Magnus Kulke , Paolo Bonzini , Richard Henderson Subject: [PULL 2/4] target/i386/mshv: fix vCPU run result contract Date: Wed, 11 Nov 2026 21:41:53 +0100 Message-Id: <20261111204155.36683-3-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> References: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: 1794429775225158500 Currently the return semantics of mshv_run_vcpu() are broken. They return a mix of int and exit_reason, ignoring the out param *exit. This change propagates errno's to the call site and sets the *exit value accordingly. We also consider the case when an ioctl has return due to a qemu-requested interruption of the call, this is a legitimate condition that should not produce an error. Fixes: 6dec60528c419 ("target/i386/mshv: Implement mshv_vcpu_run()") Signed-off-by: Magnus Kulke Reviewed-by: Doru Bl=C3=A2nzeanu --- target/i386/mshv/mshv-cpu.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index 4df73bb497..e3c155da2e 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -2124,7 +2124,6 @@ static int arch_set_single_step(CPUState *cpu, bool e= nable) int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_message *msg, MshvVmExit *e= xit) { int ret; - enum MshvVmExit exit_reason; int cpu_fd =3D mshv_vcpufd(cpu); bool single_step; =20 @@ -2142,7 +2141,12 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_messa= ge *msg, MshvVmExit *exit) =20 ret =3D ioctl(cpu_fd, MSHV_RUN_VP, msg); if (ret < 0) { - return MshvVmExitShutdown; + if (errno =3D=3D EINTR) { + *exit =3D MshvVmExitSpecial; + return 0; + } + error_report("failed to run vCPU: %s", strerror(errno)); + return -errno; } =20 /* disable single stepping by flipping RFLAGS.TF */ @@ -2154,31 +2158,33 @@ int mshv_run_vcpu(int vm_fd, CPUState *cpu, hv_mess= age *msg, MshvVmExit *exit) =20 switch (msg->header.message_type) { case HVMSG_UNRECOVERABLE_EXCEPTION: - return MshvVmExitShutdown; + *exit =3D MshvVmExitShutdown; + return 0; case HVMSG_UNMAPPED_GPA: case HVMSG_GPA_INTERCEPT: - ret =3D handle_mmio(cpu, msg, &exit_reason); + ret =3D handle_mmio(cpu, msg, exit); if (ret < 0) { error_report("failed to handle mmio"); - return -1; + return ret; } - return exit_reason; + return 0; case HVMSG_X64_IO_PORT_INTERCEPT: ret =3D handle_pio(cpu, msg); if (ret < 0) { - return MshvVmExitSpecial; + error_report("failed to handle PIO"); + return ret; } - return MshvVmExitIgnore; + *exit =3D MshvVmExitIgnore; + return 0; case HVMSG_X64_EXCEPTION_INTERCEPT: bql_lock(); - ret =3D handle_exception_interrupt(cpu, msg, &exit_reason); + ret =3D handle_exception_interrupt(cpu, msg, exit); bql_unlock(); if (ret < 0) { error_report("failed to handle exception intercept"); return -1; } - *exit =3D exit_reason; - return exit_reason; + return 0; default: break; } --=20 2.34.1 From nobody Mon Nov 23 04:44:06 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=1794429786; cv=none; d=zohomail.com; s=zohoarc; b=KJHE99vHhWOwXZRdV19Kl7y1w+a2TL/tRCLyH/paEN3/fF/eMVwdwl80OE4Ea+znBOSFtc7B58lbWRt9GRH47Y27Xl0CU4vazWhslzWuWRksa5l+Vfac6+ShSvDvaMF2m437+erxcYziJjjB9l9h27DJf4yOnC5M5CrHtVI4zgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794429786; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=wrea9xdjCfl0pQyOzEjpZJyw1biXxN2kELhw93eXq40=; b=ntA7f2BxPGq8S7WZdVraAGTtS6h/LSDFS9AuP9Lw/bXDI9F/oqkVzBuXOKDucfK9eQBrkUvoMMB0CwhmQWAG560VLf496+mIHVQZeNlRLVtj3dwcbHheqwgkcZ3Dll/YWdIwnmjC0CMr+JxvsZbbvY+i4HZQb1Qm3Yjn8txugxQ= 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 1794429786783698.8424180610117; Wed, 11 Nov 2026 12:43:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9M-0001b2-Dr; Wed, 11 Nov 2026 15:42:32 -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 1xRF9K-0001aa-TF for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:30 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9J-0005wY-1K for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:30 -0500 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.43]) by linux.microsoft.com (Postfix) with ESMTPSA id 1784520B7168; Wed, 11 Nov 2026 12:42:22 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1784520B7168 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1794429744; bh=wrea9xdjCfl0pQyOzEjpZJyw1biXxN2kELhw93eXq40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cYp/BoLhwn1rHSDWzse7FpZSKvGRZqeKX1mi0p4FVRqbaOjkewoJyDjqmKTjaBHWP IaTOG9mVCWS1TqL8ftObTXPvXWQcSZIFnAY0T5bVQ7xr9x/x+/HgtkMovkmmzyAdQI 1uF89i3XlGB4OL2CsSwclyoFLKAKkySGZM1D2Y6k= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , Doru Blanzeanu , Magnus Kulke , Paolo Bonzini , Richard Henderson Subject: [PULL 3/4] accel/mshv: freeze time during TSC MSR write Date: Wed, 11 Nov 2026 21:41:54 +0100 Message-Id: <20261111204155.36683-4-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> References: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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: 1794429787277158500 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 | 112 ++++++++++++++++++++++++++++++------------ include/system/mshv.h | 4 +- 3 files changed, 115 insertions(+), 34 deletions(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index be3abb0824..5519768cc2 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" @@ -579,7 +580,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; } @@ -928,6 +932,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) { @@ -963,6 +993,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..50c9aa6417 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,88 @@ 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) +static int ensure_time_frozen(int vm_fd) { - MshvClockState *s =3D opaque; - int vm_fd =3D mshv_state->vm; 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; + + return 0; +} =20 +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 +219,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 From nobody Mon Nov 23 04:44:06 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=1794429797; cv=none; d=zohomail.com; s=zohoarc; b=kThH4HGHwQW5UcEmt/QQ/wccRu+1TrPcDyEtPgkhiOGFIhrerYCwUbE6w0/yrBx6uqWKoL70pK6s/onehHlsZbpzGEiQE4L99mJGbH1OTnGUNgYe/354Cgj8fw9D3QYqZBJN3Ua5+5FKFQvLX7ZWBxyP30ATIeszdw2KzbGXp44= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794429797; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To: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=b30rocUZA9hdcsXaaoAC/EZ/DXZST4Kxh/zPSdwuBjQ=; b=mwEPaS4UR8DqBKjSewppFt5hpByPyjeJqiqVVIjix0MSTr6H5NoAvMKrUasTsDZOyyVVFtJFaCOQxbK/U+V8meDS33inLhnTTcksAgs9wA8IS8wmewawSaydgUI+720F2EoT+boQgK2oS23BnBoSj8swOIMeAXkHOp+vj73yLeo= 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 1794429797790371.1414337143136; Wed, 11 Nov 2026 12:43:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9Q-0001bg-S8; Wed, 11 Nov 2026 15:42:36 -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 1xRF9O-0001bJ-SG for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:35 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRF9M-0005ww-VY for qemu-devel@nongnu.org; Wed, 11 Nov 2026 15:42:34 -0500 Received: from DESKTOP-TUU1E5L.localdomain (unknown [167.220.208.43]) by linux.microsoft.com (Postfix) with ESMTPSA id A350720B7169; Wed, 11 Nov 2026 12:42:26 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A350720B7169 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1794429748; bh=b30rocUZA9hdcsXaaoAC/EZ/DXZST4Kxh/zPSdwuBjQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PXRl/O9nQQuYd79x/bbsjgb/eE5Yeb3Nyhv+LWDGo0kcuMpYwX7qSnb7IPRMoirfs yYgSWDCKX69Q30ZwPMzHlGmhg44ui4ne3yVsuGmYqMvVvDLdpXGaEWKHOHgjkHmWLk UgOyDxdTvADcxbZG3+8B0yvWIN6/t+/wN7/bG+dI= From: Magnus Kulke To: qemu-devel@nongnu.org Cc: Wei Liu , Doru Blanzeanu , Magnus Kulke , Paolo Bonzini , Richard Henderson Subject: [PULL 4/4] target/i386/mshv: add MshvPutState for vcpu writes Date: Wed, 11 Nov 2026 21:41:55 +0100 Message-Id: <20261111204155.36683-5-magnuskulke@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> References: <20261111204155.36683-1-magnuskulke@linux.microsoft.com> 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: 1794429799238158501 Content-Type: text/plain; charset="utf-8" Currently QEMU marks a vcpu state as dirty also on merely inspectional reads, e.g. via "info registers", which triggers a writeback to the hypervisor. For MSRs that cause a side-effect when written to this is undesired. We introduce an MshvPutState flag into the writeback logic to differentiate "runtime" from "full" and "reset" writebacks that should occur e.g. after a migration or startup. Discrete side-effectful MSRs are excluded in runtime writebacks. So far we only mark one MSR as not runtime-safe, but probably more will be added eventually. Signed-off-by: Magnus Kulke Reviewed-by: Wei Liu Reviewed-by: Mohamed Mediouni --- accel/mshv/mshv-all.c | 10 +++++----- include/system/mshv_int.h | 13 +++++++++++-- target/i386/mshv/mshv-cpu.c | 10 ++++++++-- target/i386/mshv/msr.c | 21 ++++++++++++++++++++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/accel/mshv/mshv-all.c b/accel/mshv/mshv-all.c index 5519768cc2..cd5b5d1863 100644 --- a/accel/mshv/mshv-all.c +++ b/accel/mshv/mshv-all.c @@ -764,10 +764,10 @@ static int mshv_cpu_exec(CPUState *cpu) =20 do { if (cpu->vcpu_dirty) { - ret =3D mshv_arch_store_vcpu_state(cpu); + ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_RUNTIME_STATE= ); if (ret) { - error_report("Failed to put registers after init: %s", - strerror(-ret)); + error_report("Failed to put registers at runtime: %s", + strerror(-ret)); ret =3D -1; break; } @@ -895,7 +895,7 @@ static void mshv_start_vcpu_thread(CPUState *cpu) static void do_mshv_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg) { - int ret =3D mshv_arch_store_vcpu_state(cpu); + int ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_FULL_STATE); if (ret < 0) { error_report("Failed to put registers after init: %s", strerror(-r= et)); abort(); @@ -911,7 +911,7 @@ static void mshv_cpu_synchronize_post_init(CPUState *cp= u) =20 static void mshv_cpu_synchronize_post_reset(CPUState *cpu) { - int ret =3D mshv_arch_store_vcpu_state(cpu); + int ret =3D mshv_arch_store_vcpu_state(cpu, MSHV_PUT_RESET_STATE); if (ret) { error_report("Failed to put registers after reset: %s", strerror(-ret)); diff --git a/include/system/mshv_int.h b/include/system/mshv_int.h index 89b44f387a..01da21affe 100644 --- a/include/system/mshv_int.h +++ b/include/system/mshv_int.h @@ -99,6 +99,15 @@ typedef enum MshvVmExit { MshvVmExitDebug =3D 3, } MshvVmExit; =20 +typedef enum MshvPutState { + /* state subset only touched by the VCPU itself during runtime */ + MSHV_PUT_RUNTIME_STATE =3D 1, + /* state subset modified during VCPU reset */ + MSHV_PUT_RESET_STATE =3D 2, + /* full state set, modified during initialization or on vmload */ + MSHV_PUT_FULL_STATE =3D 3, +} MshvPutState; + void mshv_init_mmio_emu(void); int mshv_create_vcpu(int vm_fd, uint8_t vp_index, int *cpu_fd); void mshv_remove_vcpu(int vm_fd, int cpu_fd); @@ -108,7 +117,7 @@ int mshv_set_generic_regs(const CPUState *cpu, const hv= _register_assoc *assocs, size_t n_regs); int mshv_get_generic_regs(CPUState *cpu, hv_register_assoc *assocs, size_t n_regs); -int mshv_arch_store_vcpu_state(const CPUState *cpu); +int mshv_arch_store_vcpu_state(const CPUState *cpu, MshvPutState state); int mshv_arch_load_vcpu_state(CPUState *cpu); int mshv_arch_set_partition_msrs(const CPUState *cpu); int mshv_arch_set_mp_state(const CPUState *cpu); @@ -153,7 +162,7 @@ void mshv_log_global_stop(MemoryListener *listener); /* msr */ int mshv_init_msrs(const CPUState *cpu); int mshv_get_msrs(CPUState *cpu); -int mshv_set_msrs(const CPUState *cpu); +int mshv_set_msrs(const CPUState *cpu, MshvPutState put_state); =20 /* synic */ int mshv_get_simp(int cpu_fd, uint8_t *page); diff --git a/target/i386/mshv/mshv-cpu.c b/target/i386/mshv/mshv-cpu.c index e3c155da2e..c145bbc447 100644 --- a/target/i386/mshv/mshv-cpu.c +++ b/target/i386/mshv/mshv-cpu.c @@ -1511,7 +1511,13 @@ static int set_synic_state(const CPUState *cpu) return 0; } =20 -int mshv_arch_store_vcpu_state(const CPUState *cpu) +/* + * The put_state param is passed to decide whether we need a full restorat= ion + * of all registers (e.g. after a migration ) or just a subset during runt= ime + * (e.g after "info registers"). The latter should avoid writing registers= that + * cause a side-effect. + */ +int mshv_arch_store_vcpu_state(const CPUState *cpu, MshvPutState put_state) { int ret; =20 @@ -1541,7 +1547,7 @@ int mshv_arch_store_vcpu_state(const CPUState *cpu) return ret; } =20 - ret =3D mshv_set_msrs(cpu); + ret =3D mshv_set_msrs(cpu, put_state); if (ret < 0) { return ret; } diff --git a/target/i386/mshv/msr.c b/target/i386/mshv/msr.c index 96bd552916..a06844cc99 100644 --- a/target/i386/mshv/msr.c +++ b/target/i386/mshv/msr.c @@ -324,6 +324,20 @@ static bool msr_supported(uint32_t name) return true; } =20 +static bool msr_is_runtime_safe(uint32_t name) +{ + switch (name) { + /* + * HV_X64_REGISTER_TSC will invalidate the guest's tsc_page if written= to + * while the guest's timer is not frozen. + */ + case HV_X64_REGISTER_TSC: + return false; + default: + return true; + } +} + int mshv_get_msrs(CPUState *cpu) { int ret =3D 0; @@ -406,13 +420,14 @@ static void load_from_env(const CPUState *cpu, struct= hv_register_assoc *assocs, load_from_env_mtrr_phys(cpu, &assocs[mtrr_offset], MSHV_MTRR_MSR_COUNT= ); } =20 -int mshv_set_msrs(const CPUState *cpu) +int mshv_set_msrs(const CPUState *cpu, MshvPutState put_state) { size_t n_assocs =3D MSHV_MSR_TOTAL_COUNT; struct hv_register_assoc assocs[MSHV_MSR_TOTAL_COUNT] =3D { 0 }; int ret; size_t i, j; X86CPU *x86cpu =3D X86_CPU(cpu); + bool runtime =3D (put_state <=3D MSHV_PUT_RUNTIME_STATE); =20 load_from_env(cpu, assocs, n_assocs); =20 @@ -432,6 +447,10 @@ int mshv_set_msrs(const CPUState *cpu) continue; } =20 + if (runtime && !msr_is_runtime_safe(name)) { + continue; + } + if (j !=3D i) { assocs[j] =3D assocs[i]; } --=20 2.34.1