Patch Detail
get:
Show a patch.
patch:
Update a patch.
put:
Update a patch.
GET /api/1.1/patches/2229148/?format=api
{ "id": 2229148, "url": "http://patchwork.ozlabs.org/api/1.1/patches/2229148/?format=api", "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20260427195516.46256-1-scottjgo@gmail.com/", "project": { "id": 14, "url": "http://patchwork.ozlabs.org/api/1.1/projects/14/?format=api", "name": "QEMU Development", "link_name": "qemu-devel", "list_id": "qemu-devel.nongnu.org", "list_email": "qemu-devel@nongnu.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20260427195516.46256-1-scottjgo@gmail.com>", "date": "2026-04-27T19:55:16", "name": "[v3] target/arm/hvf: Fix WFI halting to stop idle vCPU spinning", "commit_ref": null, "pull_url": null, "state": "new", "archived": false, "hash": "0d12a9de4968897d8010c40c4d1fd2db147feed2", "submitter": { "id": 93060, "url": "http://patchwork.ozlabs.org/api/1.1/people/93060/?format=api", "name": "Scott J. Goldman", "email": "scottjgo@gmail.com" }, "delegate": null, "mbox": "http://patchwork.ozlabs.org/project/qemu-devel/patch/20260427195516.46256-1-scottjgo@gmail.com/mbox/", "series": [ { "id": 501718, "url": "http://patchwork.ozlabs.org/api/1.1/series/501718/?format=api", "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/list/?series=501718", "date": "2026-04-27T19:55:16", "name": "[v3] target/arm/hvf: Fix WFI halting to stop idle vCPU spinning", "version": 3, "mbox": "http://patchwork.ozlabs.org/series/501718/mbox/" } ], "comments": "http://patchwork.ozlabs.org/api/patches/2229148/comments/", "check": "pending", "checks": "http://patchwork.ozlabs.org/api/patches/2229148/checks/", "tags": {}, "headers": { "Return-Path": "<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>", "X-Original-To": "incoming@patchwork.ozlabs.org", "Delivered-To": "patchwork-incoming@legolas.ozlabs.org", "Authentication-Results": [ "legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256\n header.s=20251104 header.b=UJSKXaOk;\n\tdkim-atps=neutral", "legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists1p.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=patchwork.ozlabs.org)" ], "Received": [ "from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g4Dsh1WBTz1yHv\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 05:59:04 +1000 (AEST)", "from localhost ([::1] helo=lists1p.gnu.org)\n\tby lists1p.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces@nongnu.org>)\n\tid 1wHS6U-000411-NU; Mon, 27 Apr 2026 15:58:50 -0400", "from eggs.gnu.org ([2001:470:142:3::10])\n by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <scottjgo@gmail.com>)\n id 1wHS6J-0003nB-QE\n for qemu-devel@nongnu.org; Mon, 27 Apr 2026 15:58:43 -0400", "from mail-dl1-x122d.google.com ([2607:f8b0:4864:20::122d])\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <scottjgo@gmail.com>)\n id 1wHS6E-00032G-Kq\n for qemu-devel@nongnu.org; Mon, 27 Apr 2026 15:58:39 -0400", "by mail-dl1-x122d.google.com with SMTP id\n a92af1059eb24-12c6df0b9bbso1221750c88.1\n for <qemu-devel@nongnu.org>; Mon, 27 Apr 2026 12:58:34 -0700 (PDT)", "from localhost.localdomain ([2601:645:8200:47:f4a5:bd04:3ca7:5727])\n by smtp.gmail.com with ESMTPSA id\n a92af1059eb24-12ddd927c5fsm347084c88.3.2026.04.27.12.58.31\n (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256);\n Mon, 27 Apr 2026 12:58:31 -0700 (PDT)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=gmail.com; s=20251104; t=1777319913; x=1777924713; darn=nongnu.org;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n :message-id:reply-to;\n bh=8jgHfKteQ6Y95ijog+Upvmsgb+UL3IMJGtNmXlOgJv4=;\n b=UJSKXaOkadLBX5nopnjuG8Gk44GN/CB4dEi9vtYHAD/E3QXWH9vIfE9PDp2EdI1+VT\n 57L4Frg3vy5p8765treFPDU55a4zrXedFpHYDgAzWD6M7r1lvT1uTJxM6e840nflhtI+\n bWGjvpbmNu7vjzb4kkWt/IMvzrZH3rZiJUauL8DzZ5yUsxkIe3mHRyMTf3covzanYkQh\n 22w2E8lsvvGS4i7bHMmZ7iAtbinX7S9fsHIHxMoVueev7asY/lsr0HlTkjUkLPcRpfnN\n irIBxcZCl6xKhZ4pP7plYukqRmP81os8dWt1AG0NR/jjT7c4zjll7uEwmMX2HL37F6rZ\n Q9Vw==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=1e100.net; s=20251104; t=1777319913; x=1777924713;\n h=content-transfer-encoding:mime-version:references:in-reply-to\n :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n :to:cc:subject:date:message-id:reply-to;\n bh=8jgHfKteQ6Y95ijog+Upvmsgb+UL3IMJGtNmXlOgJv4=;\n b=npIlub27Laj8RY5d0C6QEYlzzUnYi1J+Ygxeht040A+3l5YkgLaqHYP+jzsqYnjNJ0\n +Di6p4zczpnGvuP5dnhKEhBe8wQTH3MzZIRaJppNz/GUtiY/GQedIVW4ftexxUMg2pDP\n 4Ecyn5VnxL3KyAnbdh4bT7KpaRANuh8VpMgPPoRgs0wGgN/KuVZDcjVXeRzcANBS5dZ5\n SFeTT92Zzun8fW1u5SOBoSXzGxVCoVd8IQoQUrNQfE3c8Zd0/MIDcSXrh/rvpgCUBy5q\n npc8kib/tvIRyQ2zMd8ryidS/cwNro5j6FZ+Kh2ij071mp6PADJdzOYWFCPJ1YX8HHez\n wgUg==", "X-Gm-Message-State": "AOJu0YwjMhYRk+VQx4sX6ZhxF5aB32NNFHOWZiXzzy1HS436e/kuLT2v\n lADWyfJ8sKQJNY391ODQQJ9Ha9sHK4WbgK4posg0EQ1USMay/jm0QK80/0jhzXopeOU=", "X-Gm-Gg": "AeBDiesiipp3PVp4lx1YW5aXCYDr3DEPJunoDMO9JRL7Q7RVisfQin2tPqsHUaUQonp\n sbHBLd5cr9Sd5FakJ8ZjTMfDsK9ytYeo8ImveqlqYKAihDEBHA3tVCC3LnG4eNsNpV95L+u9V/V\n DpEH85xuUBzvUwat5soZ+sutQw3uUU59mrrSwEo63GyKg97DzfUzPAH/g8zauYjRxa4sFyoWXD7\n vY8SWjjiir+jBLFumQRgFqB8+NKdaJKp+eSeG/zUfDrKo5IXCHgIZUejlI02huSQ5e+VBEp9dBv\n UKsOqrLkukwmqsD3XXCRIJV4Rt6TKgWzxZ8Nyl8fpy5LeTIgQJ/SsRDEPOfZMZxPnMVwlcSjICL\n oe6AHdZWFwmFtX1RqanpRisodigPZ8f3xiwoimR1RLXGOT/IWoLsEXDiRInwhMI44uJRJKTYOam\n ygRW0BSCrNO+sDvZsfPgZh1mUmk1D3QY0k4VqNFTIZZZw/hSDRBNk7cBFkMDkTYCUKtijWl64OV\n r2c9gY34OE1h1MVaiCoCLUhXiAIYL7p6xWP7w==", "X-Received": "by 2002:a05:7022:e28:b0:128:d51a:5161 with SMTP id\n a92af1059eb24-12ddd9dceb7mr164546c88.27.1777319912386;\n Mon, 27 Apr 2026 12:58:32 -0700 (PDT)", "From": "\"Scott J. Goldman\" <scottjgo@gmail.com>", "To": "qemu-devel@nongnu.org", "Cc": "qemu-arm@nongnu.org, Peter Maydell <peter.maydell@linaro.org>,\n Alexander Graf <agraf@csgraf.de>, Phil Dennis-Jordan <phil@philjordan.eu>,\n Roman Bolshakov <rbolshakov@ddn.com>,\n =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= <philmd@linaro.org>,\n \"Scott J. Goldman\" <scottjgo@gmail.com>", "Subject": "[PATCH v3] target/arm/hvf: Fix WFI halting to stop idle vCPU spinning", "Date": "Mon, 27 Apr 2026 12:55:16 -0700", "Message-ID": "<20260427195516.46256-1-scottjgo@gmail.com>", "X-Mailer": "git-send-email 2.50.1", "In-Reply-To": "<20260410055045.63001-1-scottjgo@gmail.com>", "References": "<20260410055045.63001-1-scottjgo@gmail.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Received-SPF": "pass client-ip=2607:f8b0:4864:20::122d;\n envelope-from=scottjgo@gmail.com; helo=mail-dl1-x122d.google.com", "X-Spam_score_int": "-20", "X-Spam_score": "-2.1", "X-Spam_bar": "--", "X-Spam_report": "(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,\n RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,\n 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 <qemu-devel.nongnu.org>", "List-Unsubscribe": "<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>", "List-Archive": "<https://lists.nongnu.org/archive/html/qemu-devel>", "List-Post": "<mailto:qemu-devel@nongnu.org>", "List-Help": "<mailto:qemu-devel-request@nongnu.org?subject=help>", "List-Subscribe": "<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>", "Errors-To": "qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org", "Sender": "qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org" }, "content": "Commit b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\nchanged hvf_wfi() from blocking the vCPU thread with pselect() to\nreturning EXCP_HLT, intending QEMU's main event loop to handle the\nidle wait. However, cpu->halted was never set, so cpu_thread_is_idle()\nalways returns false and the vCPU thread spins at 100% CPU per core\nwhile the guest is idle.\n\nFix this by:\n\n1. Setting cpu->halted = 1 in hvf_wfi() so the vCPU thread sleeps on\n halt_cond in qemu_process_cpu_events().\n\n2. Arming a per-vCPU QEMU_CLOCK_VIRTUAL timer to fire when the guest's\n virtual timer (CNTV_CVAL_EL0) would expire. This is necessary\n because HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during\n hv_vcpu_run(), which is not called while the CPU is halted. The\n timer callback mirrors the VTIMER_ACTIVATED handler: it raises the\n vtimer IRQ through the GIC and marks vtimer_masked, causing the\n interrupt delivery chain to wake the vCPU via qemu_cpu_kick().\n\n3. Clearing cpu->halted in hvf_arch_vcpu_exec() when cpu_has_work()\n indicates a pending interrupt, and cancelling the WFI timer.\n\n4. Re-arming the WFI timer from hvf_vm_state_change() on the resume\n transition for any halted vCPU, since the QEMUTimer is per-instance\n state and is not migrated. After cpu_synchronize_all_states() the\n migrated vtimer state is mirrored in env, so we can read CNTV_CTL\n and CNTV_CVAL from there. If the vtimer has already expired by the\n time the destination resumes, hvf_wfi_timer_cb() is invoked\n directly so the halted vCPU is woken up.\n\nFixes: b5f8f77271 (\"accel/hvf: Implement WFI without using pselect()\")\nSigned-off-by: Scott J. Goldman <scottjgo@gmail.com>\n---\nChanges since v2:\n - Use QEMU_CLOCK_VIRTUAL instead of QEMU_CLOCK_HOST so the timer\n pauses with the VM and a halted vCPU isn't woken (or its IRQ\n raised) while the user has stopped the guest. (Peter)\n - Convert vtimer ticks to nanoseconds with muldiv64() to avoid\n intermediate overflow. (Peter)\n - Re-arm the WFI timer from hvf_vm_state_change() on the resume\n transition so a halted vCPU on the migration destination is\n woken when its vtimer expires (the QEMUTimer is per-instance\n state and isn't migrated). (Peter)\nv2: https://lore.kernel.org/qemu-devel/20260410055045.63001-1-scottjgo@gmail.com/\nv1: https://lore.kernel.org/qemu-devel/20260410044726.61853-1-scottjgo@gmail.com/\n\n include/system/hvf_int.h | 1 +\n target/arm/hvf/hvf.c | 124 ++++++++++++++++++++++++++++++++++++++-\n 2 files changed, 124 insertions(+), 1 deletion(-)", "diff": "diff --git a/include/system/hvf_int.h b/include/system/hvf_int.h\nindex 2621164cb2..58fb865eba 100644\n--- a/include/system/hvf_int.h\n+++ b/include/system/hvf_int.h\n@@ -48,6 +48,7 @@ struct AccelCPUState {\n hv_vcpu_exit_t *exit;\n bool vtimer_masked;\n bool guest_debug_enabled;\n+ struct QEMUTimer *wfi_timer;\n #endif\n };\n \ndiff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c\nindex 678afe5c8e..a19d7a5e1f 100644\n--- a/target/arm/hvf/hvf.c\n+++ b/target/arm/hvf/hvf.c\n@@ -28,6 +28,7 @@\n #include \"hw/core/boards.h\"\n #include \"hw/core/irq.h\"\n #include \"qemu/main-loop.h\"\n+#include \"qemu/timer.h\"\n #include \"system/cpus.h\"\n #include \"arm-powerctl.h\"\n #include \"target/arm/cpu.h\"\n@@ -301,6 +302,8 @@ void hvf_arm_init_debug(void)\n #define TMR_CTL_IMASK (1 << 1)\n #define TMR_CTL_ISTATUS (1 << 2)\n \n+static void hvf_wfi_timer_cb(void *opaque);\n+\n static uint32_t chosen_ipa_bit_size;\n \n typedef struct HVFVTimer {\n@@ -1214,6 +1217,9 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)\n {\n hv_return_t ret;\n \n+ timer_free(cpu->accel->wfi_timer);\n+ cpu->accel->wfi_timer = NULL;\n+\n ret = hv_vcpu_destroy(cpu->accel->fd);\n assert_hvf_ok(ret);\n }\n@@ -1352,6 +1358,9 @@ int hvf_arch_init_vcpu(CPUState *cpu)\n arm_cpu->isar.idregs[ID_AA64MMFR0_EL1_IDX]);\n assert_hvf_ok(ret);\n \n+ cpu->accel->wfi_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,\n+ hvf_wfi_timer_cb, cpu);\n+\n aarch64_add_sme_properties(OBJECT(cpu));\n return 0;\n }\n@@ -2027,8 +2036,67 @@ static uint64_t hvf_vtimer_val_raw(void)\n return mach_absolute_time() - hvf_state->vtimer_offset;\n }\n \n+static void hvf_wfi_timer_cb(void *opaque)\n+{\n+ CPUState *cpu = opaque;\n+ ARMCPU *arm_cpu = ARM_CPU(cpu);\n+\n+ /*\n+ * vtimer expired while the CPU was halted for WFI.\n+ * Mirror HV_EXIT_REASON_VTIMER_ACTIVATED: raise the vtimer\n+ * interrupt and mark as masked so hvf_sync_vtimer() will\n+ * check and unmask when the guest handles it.\n+ *\n+ * The interrupt delivery chain (GIC -> cpu_interrupt ->\n+ * qemu_cpu_kick) wakes the vCPU thread from halt_cond.\n+ */\n+ qemu_set_irq(arm_cpu->gt_timer_outputs[GTIMER_VIRT], 1);\n+ cpu->accel->vtimer_masked = true;\n+}\n+\n+/*\n+ * Arm a host-side QEMU_CLOCK_VIRTUAL timer to fire when the guest's\n+ * vtimer (CNTV_CVAL_EL0) is scheduled to expire. HVF only delivers\n+ * HV_EXIT_REASON_VTIMER_ACTIVATED during hv_vcpu_run(), which we won't\n+ * call while the vCPU is halted, so we need this to wake the vCPU.\n+ *\n+ * QEMU_CLOCK_VIRTUAL pauses while the VM is stopped, which keeps the\n+ * timer in lockstep with the guest's view of vtime across pause/resume.\n+ *\n+ * Caller must supply the current CNTV_CTL_EL0 and CNTV_CVAL_EL0 values,\n+ * since the appropriate source (HVF vs. env) depends on context.\n+ *\n+ * Returns 0 if the timer was armed (or if the vtimer is disabled/masked\n+ * and the vCPU should still halt waiting on another event), or -1 if\n+ * the vtimer has already expired.\n+ */\n+static int hvf_arm_wfi_timer(CPUState *cpu, uint64_t ctl, uint64_t cval)\n+{\n+ ARMCPU *arm_cpu = ARM_CPU(cpu);\n+ uint64_t now;\n+ int64_t delta_ns;\n+\n+ if (!(ctl & TMR_CTL_ENABLE) || (ctl & TMR_CTL_IMASK)) {\n+ return 0;\n+ }\n+\n+ now = hvf_vtimer_val_raw();\n+ if (cval <= now) {\n+ return -1;\n+ }\n+\n+ delta_ns = muldiv64(cval - now, NANOSECONDS_PER_SECOND,\n+ arm_cpu->gt_cntfrq_hz);\n+ timer_mod(cpu->accel->wfi_timer,\n+ qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delta_ns);\n+ return 0;\n+}\n+\n static int hvf_wfi(CPUState *cpu)\n {\n+ uint64_t ctl, cval;\n+ hv_return_t r;\n+\n if (cpu_has_work(cpu)) {\n /*\n * Don't bother to go into our \"low power state\" if\n@@ -2037,6 +2105,22 @@ static int hvf_wfi(CPUState *cpu)\n return 0;\n }\n \n+ /*\n+ * Read the vtimer state directly from HVF. We're on the vCPU thread,\n+ * just exited from hv_vcpu_run(), so HVF holds the authoritative\n+ * values and env may be stale.\n+ */\n+ r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, &ctl);\n+ assert_hvf_ok(r);\n+ r = hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CVAL_EL0, &cval);\n+ assert_hvf_ok(r);\n+\n+ if (hvf_arm_wfi_timer(cpu, ctl, cval) < 0) {\n+ /* vtimer already expired, don't halt */\n+ return 0;\n+ }\n+\n+ cpu->halted = 1;\n return EXCP_HLT;\n }\n \n@@ -2332,7 +2416,11 @@ int hvf_arch_vcpu_exec(CPUState *cpu)\n hv_return_t r;\n \n if (cpu->halted) {\n- return EXCP_HLT;\n+ if (!cpu_has_work(cpu)) {\n+ return EXCP_HLT;\n+ }\n+ cpu->halted = 0;\n+ timer_del(cpu->accel->wfi_timer);\n }\n \n flush_cpu_state(cpu);\n@@ -2376,11 +2464,45 @@ static const VMStateDescription vmstate_hvf_vtimer = {\n static void hvf_vm_state_change(void *opaque, bool running, RunState state)\n {\n HVFVTimer *s = opaque;\n+ CPUState *cpu;\n \n if (running) {\n /* Update vtimer offset on all CPUs */\n hvf_state->vtimer_offset = mach_absolute_time() - s->vtimer_val;\n cpu_synchronize_all_states();\n+\n+ /*\n+ * After migration restore (or any resume), the wfi_timer is not\n+ * scheduled on this QEMU instance, so re-arm it for any halted\n+ * vCPU with a pending vtimer. For a non-migration resume the\n+ * QEMU_CLOCK_VIRTUAL timer was already scheduled; recomputing the\n+ * deadline produces the same value and is a harmless no-op.\n+ *\n+ * cpu_synchronize_all_states() above ensures env mirrors the\n+ * authoritative vtimer state (whether that came from HVF or from\n+ * the migration stream), so we can safely read it here from the\n+ * iothread.\n+ */\n+ CPU_FOREACH(cpu) {\n+ ARMCPU *arm_cpu;\n+ uint64_t ctl, cval;\n+\n+ if (!cpu->accel || !cpu->halted) {\n+ continue;\n+ }\n+\n+ arm_cpu = ARM_CPU(cpu);\n+ ctl = arm_cpu->env.cp15.c14_timer[GTIMER_VIRT].ctl;\n+ cval = arm_cpu->env.cp15.c14_timer[GTIMER_VIRT].cval;\n+\n+ if (hvf_arm_wfi_timer(cpu, ctl, cval) < 0) {\n+ /*\n+ * vtimer already expired while we were paused; raise the\n+ * IRQ now so the halted vCPU wakes up.\n+ */\n+ hvf_wfi_timer_cb(cpu);\n+ }\n+ }\n } else {\n /* Remember vtimer value on every pause */\n s->vtimer_val = hvf_vtimer_val_raw();\n", "prefixes": [ "v3" ] }