From nobody Sat Apr 11 17:07:51 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1775800308; cv=none; d=zohomail.com; s=zohoarc; b=WHVeO9blZiWcC0iEaW/WwMklg0dLIezmxl8lmtSPx4ftJHfB6nNk1/JQG7V6OqKGNnXdNkDgdUozxR2m8wGiVJZDV8j9T8efrGUlmvMa8Y0GJ6GM1JKvQANo39MJAJUVHF8U6rfxxpCeokTm23aSjAqBWvxJl8cLCXGYkP8RbRk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1775800308; 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:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TKYjpnBzm9b6R7ZTmGA7hRa20prfMvO0wbO3fSp1aeo=; b=P6iBw+xO0DK4MPW3m0iCfEcTvlmki0uKJr8SANsLU5tCCYspwnixlm273lZpePHUOdGZmLQRwREYpORLfgwps9uxGPW+lNiwNAGgMWvxCcbF0/6CRaPeh9ccCi3910K1UfFwq/HQTdzWj+rFY11W5VonhQzhyS3ad2wBLnuDSw4= 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 lists.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1775800308425538.8692560953486; Thu, 9 Apr 2026 22:51:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wB4lh-0003qb-5b; Fri, 10 Apr 2026 01:51:01 -0400 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 1wB4lf-0003q6-C8 for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:50:59 -0400 Received: from mail-dl1-x1231.google.com ([2607:f8b0:4864:20::1231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wB4lc-0006Kv-8g for qemu-devel@nongnu.org; Fri, 10 Apr 2026 01:50:59 -0400 Received: by mail-dl1-x1231.google.com with SMTP id a92af1059eb24-1271195d2a7so1414263c88.0 for ; Thu, 09 Apr 2026 22:50:55 -0700 (PDT) Received: from localhost.localdomain ([2601:645:8200:47:ade2:73a1:54c5:e0fe]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12c346fb031sm2132117c88.13.2026.04.09.22.50.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 09 Apr 2026 22:50:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775800254; x=1776405054; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TKYjpnBzm9b6R7ZTmGA7hRa20prfMvO0wbO3fSp1aeo=; b=P4gojteKrCybMWzpD5NdYyfEeIsEpZd3GPY4hctXQOt1HnLp50EQbp69TQfhei4vLf wwU+eGJb+B8zup9KsK5Hvw0h/Hq4kV9+d62fCNx5hbhoaaMmXVQVTPzL06PtAhF3HZjR yzmACTnwW0AtA7LtfF1vRTGQPHHSZIbDNkVZRovW79uAK/KrHEgFC6HueaVzg1nKbQuj JRBel5E3Ud8O3liUPuh5ZlK4zlgZPxtAHGLmCb/Kh0D45s8j77NOaJ8hypoGR9+6pBqf wbPCSgUD65X+gpzQ6HGstQBWx+Fl70/Co8Re+oT37tJQB/u0TnC04LX3AYhpUU483Elq 9VaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775800254; x=1776405054; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TKYjpnBzm9b6R7ZTmGA7hRa20prfMvO0wbO3fSp1aeo=; b=NRhPHDfnaMpiJWG6+c/cnsgIyoPMjOojF2o55QMvqj3ks66ZPdOn4lEPh5MzltJK2w ZXZrF8o4xJ/4NsmzxkNXk2XOv3JEZhQCRKhImvqGw0dOntZmb4gvDfxhJrmn/10Wd9nH uKswYZNhqmqmVVjj8zC2wMyqce4sLgHilQzqPrLxkGmhtxwBDYvUgKBa4hxcHEVqxs8Z f9+sueCxtis7Xa2CpoX5gE1qsbRtvXpIf+hpHhCIJBbC/FHMEzXcMfpzpJfyaWHm5u/N tHE9EHTCKYmJSY2PKNEVY1hy9yCSNeORZ0kXRfpSyT7LGP7jbJBEfcfBw6twJrcTIvYW R4ag== X-Gm-Message-State: AOJu0Yyrdo60o5whihnBATKRoJNkdMUAj11lUh7hxxr8M3HXFeBh2Zo3 GaOG7SSif0YjTYr1sT/j3KSJ3UXD/Au2A/uRBh15ZC4pxs5JTYssIjxxJIhA1XAQYwk= X-Gm-Gg: AeBDieuy9wX/qs6cJ1D28fBDD3goxtH2kyerASeNwF6wO7TEZdXguIgATn6WWDokbxv OSSOQqZmDH6vVnyD/qLdKr2nBCcUMHf5+X1EqU3mgKYYmQmcFrZNymS1oXQahilZSRAtj0XenJ8 U3Hr58aFPxH/Fk4RybdwwiK3WIJRPpW3UwqgzrWhlCbqeal3YyA4op/TEyIUhF8X6SKgz0ia2Qu SBNh/Ux6LLeHNJbjFY40yq63nKJ+p9GTw8DFSePd+lfLGO1rbQNAnJzN6plhQaYRnu7sVfd1gv7 YVKtpswfIpYdGVFyGIMmE0GakJ5Gm7JZcU8rNwakv4n0+Nv41mRA6aTLwpmeY0DeUA2YiL4DeqA hzcVffEnqi+M3Qm6ObTQS5n/safTjcnApExRIUsuvn2DkR96FoTFD+m3WQ6+U91X1yiwt+Lej6V k23qw42Yh8me3KkouEuH8SVuR97WkXg7eKADG6LoT9p6ZRZ/A+F7MBT6UdFY+td5hRQoDu+pUNJ NIPevK8sFzWKPC10CB5JjUsE7I= X-Received: by 2002:a05:7022:6a9:b0:128:cf86:d1cb with SMTP id a92af1059eb24-12c34f16faamr1215214c88.31.1775800254186; Thu, 09 Apr 2026 22:50:54 -0700 (PDT) From: "Scott J. Goldman" To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, rbolshakov@ddn.com, phil@philjordan.eu, agraf@csgraf.de, peter.maydell@linaro.org, "Scott J. Goldman" Subject: [PATCH v2] target/arm/hvf: Fix WFI halting to stop idle vCPU spinning Date: Thu, 9 Apr 2026 22:50:26 -0700 Message-ID: <20260410055045.63001-1-scottjgo@gmail.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260410044726.61853-1-scottjgo@gmail.com> References: <20260410044726.61853-1-scottjgo@gmail.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=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1231; envelope-from=scottjgo@gmail.com; helo=mail-dl1-x1231.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, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @gmail.com) X-ZM-MESSAGEID: 1775800311384158500 Content-Type: text/plain; charset="utf-8" Commit b5f8f77271 ("accel/hvf: Implement WFI without using pselect()") changed hvf_wfi() from blocking the vCPU thread with pselect() to returning EXCP_HLT, intending QEMU's main event loop to handle the idle wait. However, cpu->halted was never set, so cpu_thread_is_idle() always returns false and the vCPU thread spins at 100% CPU per core while the guest is idle. Fix this by: 1. Setting cpu->halted =3D 1 in hvf_wfi() so the vCPU thread sleeps on halt_cond in qemu_process_cpu_events(). 2. Arming a host-side QEMU_CLOCK_HOST timer to fire when the guest's virtual timer (CNTV_CVAL_EL0) would expire. This is necessary because HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during hv_vcpu_run(), which is not called while the CPU is halted. The timer callback mirrors the VTIMER_ACTIVATED handler: it raises the vtimer IRQ through the GIC and marks vtimer_masked, causing the interrupt delivery chain to wake the vCPU via qemu_cpu_kick(). 3. Clearing cpu->halted in hvf_arch_vcpu_exec() when cpu_has_work() indicates a pending interrupt, and cancelling the WFI timer. Fixes: b5f8f77271 ("accel/hvf: Implement WFI without using pselect()") Signed-off-by: Scott J. Goldman Reviewed-by: Mohamed Mediouni --- include/system/hvf_int.h | 1 + target/arm/hvf/hvf.c | 65 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/include/system/hvf_int.h b/include/system/hvf_int.h index 2621164cb2..58fb865eba 100644 --- a/include/system/hvf_int.h +++ b/include/system/hvf_int.h @@ -48,6 +48,7 @@ struct AccelCPUState { hv_vcpu_exit_t *exit; bool vtimer_masked; bool guest_debug_enabled; + struct QEMUTimer *wfi_timer; #endif }; =20 diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c index 5fc8f6bbbd..f5d7221845 100644 --- a/target/arm/hvf/hvf.c +++ b/target/arm/hvf/hvf.c @@ -28,6 +28,7 @@ #include "hw/core/boards.h" #include "hw/core/irq.h" #include "qemu/main-loop.h" +#include "qemu/timer.h" #include "system/cpus.h" #include "arm-powerctl.h" #include "target/arm/cpu.h" @@ -301,6 +302,8 @@ void hvf_arm_init_debug(void) #define TMR_CTL_IMASK (1 << 1) #define TMR_CTL_ISTATUS (1 << 2) =20 +static void hvf_wfi_timer_cb(void *opaque); + static uint32_t chosen_ipa_bit_size; =20 typedef struct HVFVTimer { @@ -1214,6 +1217,9 @@ void hvf_arch_vcpu_destroy(CPUState *cpu) { hv_return_t ret; =20 + timer_free(cpu->accel->wfi_timer); + cpu->accel->wfi_timer =3D NULL; + ret =3D hv_vcpu_destroy(cpu->accel->fd); assert_hvf_ok(ret); } @@ -1352,6 +1358,9 @@ int hvf_arch_init_vcpu(CPUState *cpu) arm_cpu->isar.idregs[ID_AA64MMFR0_EL1_IDX]); assert_hvf_ok(ret); =20 + cpu->accel->wfi_timer =3D timer_new_ns(QEMU_CLOCK_HOST, + hvf_wfi_timer_cb, cpu); + aarch64_add_sme_properties(OBJECT(cpu)); return 0; } @@ -2027,8 +2036,30 @@ static uint64_t hvf_vtimer_val_raw(void) return mach_absolute_time() - hvf_state->vtimer_offset; } =20 +static void hvf_wfi_timer_cb(void *opaque) +{ + CPUState *cpu =3D opaque; + ARMCPU *arm_cpu =3D ARM_CPU(cpu); + + /* + * vtimer expired while the CPU was halted for WFI. + * Mirror HV_EXIT_REASON_VTIMER_ACTIVATED: raise the vtimer + * interrupt and mark as masked so hvf_sync_vtimer() will + * check and unmask when the guest handles it. + * + * The interrupt delivery chain (GIC -> cpu_interrupt -> + * qemu_cpu_kick) wakes the vCPU thread from halt_cond. + */ + qemu_set_irq(arm_cpu->gt_timer_outputs[GTIMER_VIRT], 1); + cpu->accel->vtimer_masked =3D true; +} + static int hvf_wfi(CPUState *cpu) { + ARMCPU *arm_cpu =3D ARM_CPU(cpu); + uint64_t ctl, cval; + hv_return_t r; + if (cpu_has_work(cpu)) { /* * Don't bother to go into our "low power state" if @@ -2037,6 +2068,34 @@ static int hvf_wfi(CPUState *cpu) return 0; } =20 + /* + * Set up a host-side timer to wake us when the vtimer expires. + * HVF only delivers HV_EXIT_REASON_VTIMER_ACTIVATED during + * hv_vcpu_run(), which we won't call while halted. + */ + r =3D hv_vcpu_get_sys_reg(cpu->accel->fd, HV_SYS_REG_CNTV_CTL_EL0, &ct= l); + assert_hvf_ok(r); + + if ((ctl & TMR_CTL_ENABLE) && !(ctl & TMR_CTL_IMASK)) { + r =3D hv_vcpu_get_sys_reg(cpu->accel->fd, + HV_SYS_REG_CNTV_CVAL_EL0, &cval); + assert_hvf_ok(r); + + uint64_t now =3D hvf_vtimer_val_raw(); + if (cval <=3D now) { + /* Timer already expired, don't halt */ + return 0; + } + + uint64_t delta_ticks =3D cval - now; + int64_t delta_ns =3D delta_ticks * NANOSECONDS_PER_SECOND + / arm_cpu->gt_cntfrq_hz; + int64_t deadline =3D qemu_clock_get_ns(QEMU_CLOCK_HOST) + delta_ns; + + timer_mod(cpu->accel->wfi_timer, deadline); + } + + cpu->halted =3D 1; return EXCP_HLT; } =20 @@ -2332,7 +2391,11 @@ int hvf_arch_vcpu_exec(CPUState *cpu) hv_return_t r; =20 if (cpu->halted) { - return EXCP_HLT; + if (!cpu_has_work(cpu)) { + return EXCP_HLT; + } + cpu->halted =3D 0; + timer_del(cpu->accel->wfi_timer); } =20 flush_cpu_state(cpu); --=20 2.50.1 (Apple Git-155)