accel/kvm: add a notifier to indicate KVM VM file descriptor has changed
A notifier callback can be used by various subsystems to perform actions when KVM file descriptor for a virtual machine changes as a part of confidential guest reset process. This change adds this notifier mechanism. Subsequent patches will add specific implementations for various notifier callbacks corresponding to various subsystems that need to take action when KVM VM file descriptor changed. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-8-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Ani Sinha committed
Feb 25, 2026 at 09:19 UTC
4dd184cb4134e2bc439d6e03360230bef266b5cc
3 files changed
+59
accel/kvm/kvm-all.c
+30
@@ -90,6 +90,7 @@ struct KVMParkedVcpu {
90
};
91
92
KVMState *kvm_state;
93
+VmfdChangeNotifier vmfd_notifier;
94
bool kvm_kernel_irqchip;
95
bool kvm_split_irqchip;
96
bool kvm_async_interrupts_allowed;
@@ -123,6 +124,9 @@ static const KVMCapabilityInfo kvm_required_capabilites[] = {
124
static NotifierList kvm_irqchip_change_notifiers =
125
NOTIFIER_LIST_INITIALIZER(kvm_irqchip_change_notifiers);
126
127
+static NotifierWithReturnList register_vmfd_changed_notifiers =
128
+ NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vmfd_changed_notifiers);
129
+
130
struct KVMResampleFd {
131
int gsi;
132
EventNotifier *resample_event;
@@ -2173,6 +2177,22 @@ void kvm_irqchip_change_notify(void)
2177
notifier_list_notify(&kvm_irqchip_change_notifiers, NULL);
2178
}
2179
2180
+void kvm_vmfd_add_change_notifier(NotifierWithReturn *n)
2181
+{
2182
+ notifier_with_return_list_add(®ister_vmfd_changed_notifiers, n);
2183
+}
2184
+
2185
+void kvm_vmfd_remove_change_notifier(NotifierWithReturn *n)
2186
+{
2187
+ notifier_with_return_remove(n);
2188
+}
2189
+
2190
+static int kvm_vmfd_change_notify(Error **errp)
2191
+{
2192
+ return notifier_with_return_list_notify(®ister_vmfd_changed_notifiers,
2193
+ &vmfd_notifier, errp);
2194
+}
2195
+
2196
int kvm_irqchip_get_virq(KVMState *s)
2197
{
2198
int next_virq;
@@ -2671,6 +2691,16 @@ static int kvm_reset_vmfd(MachineState *ms)
2691
do_kvm_irqchip_create(s);
2692
}
2693
2694
+ /*
2695
+ * notify everyone that vmfd has changed.
2696
+ */
2697
+ vmfd_notifier.vmfd = s->vmfd;
2698
+ ret = kvm_vmfd_change_notify(&err);
2699
+ if (ret < 0) {
2700
+ return ret;
2701
+ }
2702
+ assert(!err);
2703
+
2704
/* these can be only called after ram_block_rebind() */
2705
memory_listener_register(&kml->listener, &address_space_memory);
2706
memory_listener_register(&kvm_io_listener, &address_space_io);
accel/stubs/kvm-stub.c
+8
@@ -79,6 +79,14 @@ void kvm_irqchip_change_notify(void)
79
{
80
}
81
82
+void kvm_vmfd_add_change_notifier(NotifierWithReturn *n)
83
+{
84
+}
85
+
86
+void kvm_vmfd_remove_change_notifier(NotifierWithReturn *n)
87
+{
88
+}
89
+
90
int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
91
EventNotifier *rn, int virq)
92
{
include/system/kvm.h
+21
@@ -181,6 +181,7 @@ DECLARE_INSTANCE_CHECKER(KVMState, KVM_STATE,
181
182
extern KVMState *kvm_state;
183
typedef struct Notifier Notifier;
184
+typedef struct NotifierWithReturn NotifierWithReturn;
185
186
typedef struct KVMRouteChange {
187
KVMState *s;
@@ -567,4 +568,24 @@ int kvm_set_memory_attributes_shared(hwaddr start, uint64_t size);
568
569
int kvm_convert_memory(hwaddr start, hwaddr size, bool to_private);
570
571
+/* argument to vmfd change notifier */
572
+typedef struct VmfdChangeNotifier {
573
+ int vmfd;
574
+} VmfdChangeNotifier;
575
+
576
+/**
577
+ * 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
580
+ * as creating new fds against this new vm file descriptor.
581
+ * @n: notifier with return value.
582
+ */
583
+void kvm_vmfd_add_change_notifier(NotifierWithReturn *n);
584
+/**
585
+ * kvm_vmfd_remove_change_notifier - de-register a notifer previously
586
+ * registered with kvm_vmfd_add_change_notifier call.
587
+ * @n: notifier that was previously registered.
588
+ */
589
+void kvm_vmfd_remove_change_notifier(NotifierWithReturn *n);
590
+
591
#endif