mshv: Add an ioctl to scrub partition state
Subsystems (2)
[email protected]
maintainerPATCH 1/1 mshv: Add an ioctl to scrub partition state
From: John Starks <[email protected]>
Userspace VMMs need to reset a child partition's hypervisor-managed state without destroying its VPs or rebuilding its memory and eventfd registrations. Exposing HvCallScrubPartition through generic hypercall passthrough would bypass the synchronization and cached-state updates needed by the driver.
Add MSHV_SCRUB_PARTITION as a no-argument partition-fd ioctl. Hold the partition mutex and try-lock each populated VP mutex in index order, using the partition mutex as the lockdep nesting lock. Return -EBUSY without issuing the hypercall if a VP is busy. This excludes VP ioctls without waiting indefinitely for a running VP; userspace must stop its run threads before requesting scrub.
After a successful scrub, clear cached intercept suspend so the next run clears explicit suspend rather than trying to consume a stale intercept. Preserve deferred blocked waits and notification accounting: a VP becoming dispatchable does not imply its host notification has already arrived. Keep the locks through this update and unwind them on every exit.
Scrub is nonallocating, so issue one fast hypercall, trace its status and return the existing errno translation. Let the hypervisor determine which partition configurations support scrub. Device quiescing and restoring guest execution state remain userspace responsibilities.
---
Tested repeated scrub/run, sparse and 64-VP locking, busy unwinding, interrupted blocked runs, concurrent memory invalidation and irqfd activity on x86-64, and delayed notifications with separate diagnostic instrumentation. The affected ARM64 objects compile. ARM64 runtime and hypervisor scheduling remain untested. Doorbell delivery across scrub works, but exposes an existing eventfd hardirq wait-context warning under lockdep, separate from this change.
GitHub Copilot generated the implementation and test probes, investigated locking and notification handling, ran builds and Incubator tests, and drafted this description. Human review selected the dedicated ioctl and try-lock policy and corrected the nonallocating hypercall contract.
--- drivers/hv/mshv_root.h | 1 + drivers/hv/mshv_root_hv_call.c | 11 ++++++++ drivers/hv/mshv_root_main.c | 50 ++++++++++++++++++++++++++++++++++ drivers/hv/mshv_trace.h | 17 ++++++++++++ include/hyperv/hvgdk_mini.h | 1 + include/hyperv/hvhdk.h | 4 +++ include/uapi/linux/mshv.h | 11 ++++++++ 7 files changed, 95 insertions(+)
drivers/hv/mshv_root.h +1 -0
@@ -279,6 +279,7 @@ int hv_call_create_partition(u64 flags,union hv_partition_isolation_properties isolation_properties, u64 *partition_id);int hv_call_initialize_partition (u64 partition_id); +int hv_call_scrub_partition (u64 partition_id);int hv_call_finalize_partition (u64 partition_id);int hv_call_delete_partition (u64 partition_id);int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs);
drivers/hv/mshv_root_hv_call.c +11 -0
@@ -161,6 +161,17 @@ int hv_call_initialize_partition(u64 partition_id)return ret; } +int hv_call_scrub_partition (u64 partition_id) +{ +u64 status; + + status =hv_do_fast_hypercall8 (HVCALL_SCRUB_PARTITION , partition_id); + +trace_mshv_hvcall_scrub_partition (partition_id, status); + +return hv_result_to_errno (status); +} +int hv_call_finalize_partition (u64 partition_id) {struct hv_input_finalize_partition input;
drivers/hv/mshv_root_main.c +50 -0
62 lines not on this page · raw message
drivers/hv/mshv_trace.h +17 -0
@@ -71,6 +71,23 @@ TRACE_EVENT(mshv_hvcall_initialize_partition, ) ); +TRACE_EVENT (mshv_hvcall_scrub_partition, + TP_PROTO(u64 partition_id, u64 status), +TP_ARGS (partition_id, status), +TP_STRUCT__entry ( + __field(u64, partition_id) +__field (u64, status) + ), +TP_fast_assign ( + __entry->partition_id = partition_id; + __entry->status = status; + ), +TP_printk ("partition_id=%llu status=%#llx" , + __entry->partition_id, + __entry->status + ) +); + TRACE_EVENT(mshv_partition_release, TP_PROTO(u64 partition_id), TP_ARGS(partition_id),
include/hyperv/hvgdk_mini.h +1 -0
@@ -478,6 +478,7 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HVCALL_NOTIFY_PARTITION_EVENT 0x0087#define HVCALL_ENTER_SLEEP_STATE 0x0084#define HVCALL_NOTIFY_PORT_RING_EMPTY 0x008b +#define HVCALL_SCRUB_PARTITION 0x008d#define HVCALL_REGISTER_INTERCEPT_RESULT 0x0091#define HVCALL_ASSERT_VIRTUAL_INTERRUPT 0x0094#define HVCALL_CREATE_PORT 0x0095
include/hyperv/hvhdk.h +4 -0
@@ -369,6 +369,10 @@ struct hv_input_delete_partition {u64 partition_id; } __packed; +struct hv_input_scrub_partition { +u64 partition_id; +} __packed; +struct hv_input_get_partition_property {u64 partition_id;u32 property_code;/* enum hv_partition_property_code */
include/uapi/linux/mshv.h +11 -0
@@ -255,6 +255,17 @@ struct mshv_root_hvcall {/* Generic hypercall */ #define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) +/* +* Scrub a partition's guest state; takes no arguments. +* Stop all VP run threads first: a busy VP causes -EBUSY without issuing +* the hypercall. On success, the VPs are explicitly suspended. +* +* Kernel resource registrations and VP fd/mmap lifetimes are preserved. +* Userspace must quiesce its devices and restore guest execution state. +* Failure does not imply successful reset or guaranteed rollback. +*/ +#define MSHV_SCRUB_PARTITION _IO(MSHV_IOCTL, 0x0B) +/* ******************************** * VP APIs for child partitions *
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mshv: false WARN_ONCE() in mshv_run_vp_with_root_scheduler() after scrub --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mshv: false WARN_ONCE() in mshv_run_vp_with_root_scheduler() after scrub --
commit 1f05bee845772dade9b43bed66085850115eb2ca
Author: John Starks <[email protected]>
mshv: Add an ioctl to scrub partition state
This commit adds the MSHV_SCRUB_PARTITION partition-fd ioctl to invoke HvCallScrubPartition while holding the partition and VP mutexes, and clears cached intercept_suspend state on all VPs so the next run clears explicit suspend.
Quoted 34 lines
drivers/hv/mshv_root_main.c +26
-0@@ -1648,6 +1648,53 @@ mshv_partition_ioctl_initialize(struct mshv_partition *partition)return ret; } +static int mshv_partition_ioctl_scrub (struct mshv_partition *partition) +{ +unsigned int i, vps_locked =0 ; +int ret; + +lockdep_assert_held (&partition->pt_mutex); + +/* +* Run ioctls may wait indefinitely. Userspace must stop its run +* threads; never wait for a VP while holding the others. The +* partition mutex stabilizes even a sparse VP array. +*/ +for (; vps_locked <MSHV_MAX_VPS ; vps_locked++) { +struct mshv_vp *vp = partition->pt_vp_array[vps_locked]; + +if (vp && !mutex_trylock_nest_lock (&vp->vp_mutex, + &partition->pt_mutex)) { + ret = -EBUSY ; + goto unlock; + } + } + + ret =hv_call_scrub_partition (partition->pt_id); +if (ret) + goto unlock; +
drivers/hv/mshv_root_main.c mshv_partition_ioctl_initialize(struct mshv_partition *partition) · line 1686
Quoted 10 lines
drivers/hv/mshv_root_main.c +10
-0+/* +* Scrub clears intercept suspend and sets explicit suspend. +* Retain deferred waits and lifetime notification accounting. +*/ +for (i =0 ; i <MSHV_MAX_VPS ; i++) { +struct mshv_vp *vp = partition->pt_vp_array[i]; + +if (vp) + vp->run.flags.intercept_suspend =0 ; + }
[Severity: Medium]
When a previously dispatched VP is run again under HV_SCHEDULER_TYPE_ROOT after MSHV_SCRUB_PARTITION, will mshv_run_vp_with_root_scheduler() trigger its WARN_ONCE()?
During the first MSHV_RUN_VP before scrub, clearing the initial explicit suspend causes the hypervisor to signal the VP via kick_vp() in drivers/hv/mshv_synic.c:
static void kick_vp(struct mshv_vp *vp)
{
atomic64_inc(&vp->run.vp_signaled_count);
vp->run.kicked_by_hv = 1;
wake_up(&vp->run.vp_suspend_queue);
}This increments vp->run.vp_signaled_count to a non-zero value, which mshv_partition_ioctl_scrub() retains across the scrub while resetting vp->run.flags.intercept_suspend = 0 and leaving the VP in explicit suspend.
On the next MSHV_RUN_VP call after scrub, mshv_run_vp_with_root_scheduler() in drivers/hv/mshv_root_main.c dispatches the VP and enters the HV_VP_DISPATCH_EVENT_SUSPEND branch to clear explicit suspend:
if (output.dispatch_state == HV_VP_DISPATCH_STATE_BLOCKED) {
if (output.dispatch_event ==
HV_VP_DISPATCH_EVENT_SUSPEND) {
/*
* TODO: remove the warning once VP canceling
* is supported
*/
WARN_ONCE(atomic64_read(&vp->run.vp_signaled_count),
"%s: vp#%d: unexpected explicit suspend\n",
__func__, vp->vp_index);
...
ret = mshv_vp_clear_explicit_suspend(vp);Because vp->run.vp_signaled_count is already non-zero from the pre-scrub run, doesn't resuming a VP after MSHV_SCRUB_PARTITION unconditionally hit this WARN_ONCE()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 233 lines · On Wed, Oct 28, 2026 at 11:27:54PM -0700, [email protected] wrote:
From: John Starks <[email protected]>
Userspace VMMs need to reset a child partition's hypervisor-managed state without destroying its VPs or rebuilding its memory and eventfd registrations. Exposing HvCallScrubPartition through generic hypercall passthrough would bypass the synchronization and cached-state updates needed by the driver.
Add
MSHV_SCRUB_PARTITIONas a no-argument partition-fd ioctl. Hold the partition mutex and try-lock each populated VP mutex in index order, using the partition mutex as the lockdep nesting lock. Return-EBUSYwithout issuing the hypercall if a VP is busy. This excludes VP ioctls without waiting indefinitely for a running VP; userspace must stop its run threads before requesting scrub.After a successful scrub, clear cached intercept suspend so the next run clears explicit suspend rather than trying to consume a stale intercept. Preserve deferred blocked waits and notification accounting: a VP becoming dispatchable does not imply its host notification has already arrived. Keep the locks through this update and unwind them on every exit.
Scrub is nonallocating, so issue one fast hypercall, trace its status and return the existing errno translation. Let the hypervisor determine which partition configurations support scrub. Device quiescing and restoring guest execution state remain userspace responsibilities.
Signed-off-by John Starks <[email protected]>Signed-off-by Wei Liu <[email protected]>---
Tested repeated scrub/run, sparse and 64-VP locking, busy unwinding, interrupted blocked runs, concurrent memory invalidation and irqfd activity on x86-64, and delayed notifications with separate diagnostic instrumentation. The affected ARM64 objects compile. ARM64 runtime and hypervisor scheduling remain untested. Doorbell delivery across scrub works, but exposes an existing eventfd hardirq wait-context warning under lockdep, separate from this change.GitHub Copilot generated the implementation and test probes, investigated locking and notification handling, ran builds and Incubator tests, and drafted this description. Human review selected the dedicated ioctl and try-lock policy and corrected the nonallocating hypercall contract.
--- drivers/hv/mshv_root.h | 1 + drivers/hv/mshv_root_hv_call.c | 11 ++++++++ drivers/hv/mshv_root_main.c | 50 ++++++++++++++++++++++++++++++++++ drivers/hv/mshv_trace.h | 17 ++++++++++++ include/hyperv/hvgdk_mini.h | 1 + include/hyperv/hvhdk.h | 4 +++ include/uapi/linux/mshv.h | 11 ++++++++ 7 files changed, 95 insertions(+)drivers/hv/mshv_root.h +1
-0@@ -279,6 +279,7 @@ int hv_call_create_partition(u64 flags,union hv_partition_isolation_properties isolation_properties, u64 *partition_id);int hv_call_initialize_partition (u64 partition_id); +int hv_call_scrub_partition (u64 partition_id);int hv_call_finalize_partition (u64 partition_id);int hv_call_delete_partition (u64 partition_id);int hv_call_map_mmio_pages (u64 partition_id,u64 gfn,u64 mmio_spa,u64 numpgs);drivers/hv/mshv_root_hv_call.c +11
-0@@ -161,6 +161,17 @@ int hv_call_initialize_partition(u64 partition_id)return ret; } +int hv_call_scrub_partition (u64 partition_id) +{ +u64 status; + + status =hv_do_fast_hypercall8 (HVCALL_SCRUB_PARTITION , partition_id); + +trace_mshv_hvcall_scrub_partition (partition_id, status); + +return hv_result_to_errno (status); +} +int hv_call_finalize_partition (u64 partition_id) {struct hv_input_finalize_partition input;drivers/hv/mshv_root_main.c +50
-0@@ -1648,6 +1648,53 @@ mshv_partition_ioctl_initialize(struct mshv_partition *partition)return ret; } +static int mshv_partition_ioctl_scrub (struct mshv_partition *partition) +{ +unsigned int i, vps_locked =0 ; +int ret; + +lockdep_assert_held (&partition->pt_mutex); + +/* +* Run ioctls may wait indefinitely. Userspace must stop its run +* threads; never wait for a VP while holding the others. The +* partition mutex stabilizes even a sparse VP array. +*/ +for (; vps_locked <MSHV_MAX_VPS ; vps_locked++) { +struct mshv_vp *vp = partition->pt_vp_array[vps_locked]; + +if (vp && !mutex_trylock_nest_lock (&vp->vp_mutex, + &partition->pt_mutex)) { + ret = -EBUSY ; + goto unlock; + } + } + + ret =hv_call_scrub_partition (partition->pt_id); +if (ret) + goto unlock; + +/* +* Scrub clears intercept suspend and sets explicit suspend. +* Retain deferred waits and lifetime notification accounting. +*/ +for (i =0 ; i <MSHV_MAX_VPS ; i++) { +struct mshv_vp *vp = partition->pt_vp_array[i]; + +if (vp) + vp->run.flags.intercept_suspend =0 ; + } + +unlock: +while (vps_locked) { +struct mshv_vp *vp = partition->pt_vp_array[--vps_locked]; + +if (vp) +mutex_unlock (&vp->vp_mutex); + } +return ret; +} +static long mshv_partition_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {@@ -1662,6 +1709,9 @@ mshv_partition_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)case MSHV_INITIALIZE_PARTITION : ret =mshv_partition_ioctl_initialize (partition);break ; +case MSHV_SCRUB_PARTITION : + ret =mshv_partition_ioctl_scrub (partition); +break ;case MSHV_SET_GUEST_MEMORY : ret =mshv_partition_ioctl_set_memory (partition, uarg);break ;drivers/hv/mshv_trace.h +17
-0@@ -71,6 +71,23 @@ TRACE_EVENT(mshv_hvcall_initialize_partition, ) ); +TRACE_EVENT (mshv_hvcall_scrub_partition, + TP_PROTO(u64 partition_id, u64 status), +TP_ARGS (partition_id, status), +TP_STRUCT__entry ( + __field(u64, partition_id) +__field (u64, status) + ), +TP_fast_assign ( + __entry->partition_id = partition_id; + __entry->status = status; + ), +TP_printk ("partition_id=%llu status=%#llx" , + __entry->partition_id, + __entry->status + ) +); + TRACE_EVENT(mshv_partition_release, TP_PROTO(u64 partition_id), TP_ARGS(partition_id),include/hyperv/hvgdk_mini.h +1
-0@@ -478,6 +478,7 @@ union hv_vp_assist_msr_contents { /* HV_REGISTER_VP_ASSIST_PAGE */#define HVCALL_NOTIFY_PARTITION_EVENT 0x0087#define HVCALL_ENTER_SLEEP_STATE 0x0084#define HVCALL_NOTIFY_PORT_RING_EMPTY 0x008b +#define HVCALL_SCRUB_PARTITION 0x008d#define HVCALL_REGISTER_INTERCEPT_RESULT 0x0091#define HVCALL_ASSERT_VIRTUAL_INTERRUPT 0x0094#define HVCALL_CREATE_PORT 0x0095include/hyperv/hvhdk.h +4
-0@@ -369,6 +369,10 @@ struct hv_input_delete_partition {u64 partition_id; } __packed; +struct hv_input_scrub_partition { +u64 partition_id; +} __packed; +struct hv_input_get_partition_property {u64 partition_id;u32 property_code;/* enum hv_partition_property_code */ include/uapi/linux/mshv.h +11
-0@@ -255,6 +255,17 @@ struct mshv_root_hvcall {/* Generic hypercall */ #define MSHV_ROOT_HVCALL _IOWR(MSHV_IOCTL, 0x07, struct mshv_root_hvcall) +/* +* Scrub a partition's guest state; takes no arguments. +* Stop all VP run threads first: a busy VP causes -EBUSY without issuing +* the hypercall. On success, the VPs are explicitly suspended. +* +* Kernel resource registrations and VP fd/mmap lifetimes are preserved. +* Userspace must quiesce its devices and restore guest execution state. +* Failure does not imply successful reset or guaranteed rollback. +*/ +#define MSHV_SCRUB_PARTITION _IO(MSHV_IOCTL, 0x0B) +/* ******************************** * VP APIs for child partitions * Signature · 1 line
2.53.0
Quoted 51 lines · On 10/28/26 23:27, [email protected] wrote:
From: John Starks <[email protected]>
Userspace VMMs need to reset a child partition's hypervisor-managed state without destroying its VPs or rebuilding its memory and eventfd registrations. Exposing HvCallScrubPartition through generic hypercall passthrough would bypass the synchronization and cached-state updates needed by the driver.
Add
MSHV_SCRUB_PARTITIONas a no-argument partition-fd ioctl. Hold the partition mutex and try-lock each populated VP mutex in index order, using the partition mutex as the lockdep nesting lock. Return-EBUSYwithout issuing the hypercall if a VP is busy. This excludes VP ioctls without waiting indefinitely for a running VP; userspace must stop its run threads before requesting scrub.After a successful scrub, clear cached intercept suspend so the next run clears explicit suspend rather than trying to consume a stale intercept. Preserve deferred blocked waits and notification accounting: a VP becoming dispatchable does not imply its host notification has already arrived. Keep the locks through this update and unwind them on every exit.
Scrub is nonallocating, so issue one fast hypercall, trace its status and return the existing errno translation. Let the hypervisor determine which partition configurations support scrub. Device quiescing and restoring guest execution state remain userspace responsibilities.
Signed-off-by John Starks <[email protected]>Signed-off-by Wei Liu <[email protected]>---
Tested repeated scrub/run, sparse and 64-VP locking, busy unwinding, interrupted blocked runs, concurrent memory invalidation and irqfd activity on x86-64, and delayed notifications with separate diagnostic instrumentation. The affected ARM64 objects compile. ARM64 runtime and hypervisor scheduling remain untested. Doorbell delivery across scrub works, but exposes an existing eventfd hardirq wait-context warning under lockdep, separate from this change.GitHub Copilot generated the implementation and test probes, investigated locking and notification handling, ran builds and Incubator tests, and drafted this description. Human review selected the dedicated ioctl and try-lock policy and corrected the nonallocating hypercall contract.
--- drivers/hv/mshv_root.h | 1 + drivers/hv/mshv_root_hv_call.c | 11 ++++++++ drivers/hv/mshv_root_main.c | 50 ++++++++++++++++++++++++++++++++++ drivers/hv/mshv_trace.h | 17 ++++++++++++ include/hyperv/hvgdk_mini.h | 1 + include/hyperv/hvhdk.h | 4 +++ include/uapi/linux/mshv.h | 11 ++++++++ 7 files changed, 95 insertions(+)
Quoted 1 line
<snip>