@samitouri / QOSamiQemu / commits / 0f53f021ad

accel/kvm: notify when KVM VM file fd is about to be changed

Various subsystems might need to take some steps before the KVM file descriptor for a virtual machine is changed. So a new boolean attribute is added to the vmfd_notifier structure which is passed to the notifier callbacks. vmfd_notifer.pre is true for pre-notification of vmfd change and false for post notification. Notifier callback implementations can simply check the boolean value for (vmfd_notifer*)->pre and can take actions for pre or post vmfd change based on the value. Subsequent patches will add callback implementations for specific components that need this pre-notification. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-9-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC 0f53f021ad1ede28dc8944686544e496cab02e5e
2 files changed +13 -2
accel/kvm/kvm-all.c
+9
@@ -2654,6 +2654,13 @@ static int kvm_reset_vmfd(MachineState *ms)
2654 memory_listener_unregister(&kml->listener);
2655 memory_listener_unregister(&kvm_io_listener);
2656
2657 + vmfd_notifier.pre = true;
2658 + ret = kvm_vmfd_change_notify(&err);
2659 + if (ret < 0) {
2660 + return ret;
2661 + }
2662 + assert(!err);
2663 +
2664 if (s->vmfd >= 0) {
2665 close(s->vmfd);
2666 }
@@ -2695,6 +2702,8 @@ static int kvm_reset_vmfd(MachineState *ms)
2702 * notify everyone that vmfd has changed.
2703 */
2704 vmfd_notifier.vmfd = s->vmfd;
2705 + vmfd_notifier.pre = false;
2706 +
2707 ret = kvm_vmfd_change_notify(&err);
2708 if (ret < 0) {
2709 return ret;
include/system/kvm.h
+4 -2
@@ -571,12 +571,14 @@ int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private);
571 /* argument to vmfd change notifier */
572 typedef struct VmfdChangeNotifier {
573 int vmfd;
574 + bool pre;
575 } VmfdChangeNotifier;
576
577 /**
578 * kvm_vmfd_add_change_notifier - register a notifier to get notified when
578 - * a KVM vm file descriptor changes as a part of the confidential guest "reset"
579 - * process. Various subsystems should use this mechanism to take actions such
579 + * a KVM vm file descriptor changes or about to be changed as a part of the
580 + * confidential guest "reset" process.
581 + * Various subsystems should use this mechanism to take actions such
582 * as creating new fds against this new vm file descriptor.
583 * @n: notifier with return value.
584 */