Skip to content

fix(hvisor-tool): release virtio and SCMI resources on zone shutdown - #451

Open
agicy wants to merge 5 commits into
devfrom
luanlai/virtio-resource-shutdown-release
Open

agicy wants to merge 5 commits into
devfrom
luanlai/virtio-resource-shutdown-release

Conversation

@agicy

@agicy agicy commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix per-zone virtio and SCMI resource cleanup on administrator-initiated zone shutdown in hvisor-tool:

  • Synchronize epoll event removal (abd79ec4): look up events by fd under event_lock, track active handlers so remove_event() waits before device teardown frees memory, reuse freed event slots, and unregister console/net events before closing their file descriptors.
  • Flush virtio-blk image on close (c4517e10): call fdatasync via blk_do_flush() after joining the block worker thread and before closing img_fd.
  • Route admin zone shutdown through the virtio daemon (8591ada4): add a shutdown-zone control-socket operation (preserving the existing request/response wire layout and restricting /run/hvisor-virtio.sock to 0600), detach the target zone's VirtIODevices, wait for in-flight MMIO references to drain (5 s deadline with rollback on -EBUSY), close per-zone devices, and unmap the zone's guest memory after HVISOR_ZONE_SHUTDOWN succeeds. Fall back to the direct HVISOR_ZONE_SHUTDOWN ioctl when no daemon is running or when talking to an older daemon.
  • Make SCMI resource lifecycle kernel-owned (54f9714d): align with syswonder/hvisor-tool#122 by stamping zone_id on SCMI clock/power ioctls and tracking per-zone positive enable/power-on counts in hvisor.ko. Release a zone's clocks first and power domains second upon HVISOR_ZONE_SHUTDOWN, before HVISOR_ZONE_START when no live instance of that zone_id exists, and on module unload, serialized under scmi_lifecycle_lock.

Motivation

Previously, hvisor zone shutdown -id <N> invoked HVISOR_ZONE_SHUTDOWN directly without notifying the long-lived virtio daemon (hvisor virtio start), and hvisor.ko never undid SCMI clock or power-domain operations performed on behalf of a zone:

  1. Stale daemon devices, fds, and mappings: a shut-down zone's VirtIODevice entries, block image fds, PTY/TAP fds, worker threads, epoll registrations, and zone_mem mmaps remained open in the daemon. Re-provisioning the zone via hvisor virtio add failed MMIO overlap validation, and closing virtio-blk without fdatasync risked losing buffered writes.
  2. Runtime epoll teardown race: removing console/net devices while the event_monitor thread was running could dereference a freed epoll_event.data.ptr or EPOLL_CTL_DEL a recycled fd, and event slots were never reclaimed.
  3. Leaked host SCMI state: clocks enabled (clk_prepare_enable) and power domains resumed (pm_runtime_resume_and_get) by a guest stayed active in the host kernel across zone shutdowns and restarts.

Verification

  • Userspace & hypervisor builds (passed):
    • make -C hvisor-tool/tools -B ARCH=arm64 VIRTIO_SCMI=n CROSS_COMPILE=aarch64-linux-gnu- LOG=LOG_INFO (and VIRTIO_SCMI=y, verified across each individual commit and HEAD)
    • make elf BID=aarch64/qemu-gicv3 MODE=release
  • Kernel module build (passed):
    • make -C hvisor-tool/driver ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- KDIR=/lib/modules/6.8.0-117-generic/build VIRTIO_SCMI=y
  • QEMU aarch64/qemu-gicv3 end-to-end admin shutdown test (passed):
    • Booted zone0 Linux + zone1 Linux with virtio-blk and virtio-console served by the virtio daemon.
    • Verified /proc/<daemon_pid>/fd held zone1's disk image fd and PTY master/slave fds before hvisor zone shutdown -id 1.
    • Executed hvisor zone shutdown -id 1: confirmed hvisor logged zone 1 has been shutdown, zone1's disk image and PTY fds were closed, the control socket and daemon PID remained alive, and zone0 continued running normally.
  • Formatting & static checks (passed):
    • git ls-files -z -- '*.c' '*.h' | xargs -0 clang-format --dry-run --Werror (clang-format 23.1.5)
    • cargo fmt --all -- --check & non-src rustfmt --check --edition 2021
    • ruff format --check . (ruff 0.17.6)
    • typos --config _typos.toml & ./tools/license_checker.sh
  • Not run:
    • Hardware validation of SCMI clock/power teardown on RK3588 (QEMU qemu-gicv3 has no SCMI DT clocks/power-domains, so the SCMI accounting path in this branch is compile-verified only; companion design verified on RK3588 in syswonder/hvisor-tool#122).

Risks and Limitations

  • Matched deployment required (hvisor + hvisor.ko): struct hvisor_scmi_clock_args and struct hvisor_scmi_power_args add zone_id, changing _IOWR-encoded ioctl command numbers. hvisor userspace and hvisor.ko must be built and deployed from the same revision (mismatched binaries fail SCMI ioctls cleanly with -EINVAL).
  • Admin-shutdown scope for daemon devices: guest-initiated shutdown (PSCI_SYSTEM_OFF) still lacks a hypervisor-to-userspace zone-death notification, so daemon-side VirtIODevice cleanup only runs on explicit hvisor zone shutdown (whereas kernel-owned SCMI state is also cleaned up before the next HVISOR_ZONE_START of that zone_id).
  • Zone restart workflow: because hvisor zone shutdown now removes the zone's virtio devices from the daemon, restarting that zone under a persistent daemon requires re-adding its devices via hvisor virtio add before hvisor zone start.
  • SCMI scope: per-zone SCMI accounting covers clock enable/disable and power-domain on/off counts (up to SCMI_MAX_ZONES = 16, matching hvisor's MAX_ZONE_NUM); clock rates and reset domains are not restored.

@agicy
agicy requested a review from li041 November 26, 2026 04:53
@github-actions github-actions Bot added the bug Something isn't working label Nov 26, 2026
Comment thread hvisor-tool/tools/virtio/virtio.c Resolved
Comment thread hvisor-tool/driver/virtio/scmi/clock.c Resolved
Comment thread hvisor-tool/driver/virtio/scmi/server.c Resolved
Comment thread hvisor-tool/driver/virtio/scmi/server.h Resolved
@agicy
agicy force-pushed the luanlai/virtio-resource-shutdown-release branch from 54f9714 to 3eaea9e Compare November 26, 2026 13:38
@agicy
agicy requested a review from li041 November 26, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants