@samitouri / QOSamiQemu / commits / 956200b180

kvm/vcpu: add notifiers to inform vcpu file descriptor change

When new vcpu file descriptors are created and bound to the new kvm file descriptor as a part of the confidential guest reset mechanism, various subsystems needs to know about it. This change adds notifiers so that various subsystems can take appropriate actions when vcpu fds change by registering their handlers to this notifier. Subsequent changes will register specific handlers to this notifier. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-31-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC 956200b180001b0ea46c9684dda1b6c36212cf87
3 files changed +53
accel/kvm/kvm-all.c
+26
@@ -127,6 +127,9 @@ static NotifierList kvm_irqchip_change_notifiers =
127 static NotifierWithReturnList register_vmfd_changed_notifiers =
128 NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vmfd_changed_notifiers);
129
130 +static NotifierWithReturnList register_vcpufd_changed_notifiers =
131 + NOTIFIER_WITH_RETURN_LIST_INITIALIZER(register_vcpufd_changed_notifiers);
132 +
133 static int map_kvm_run(KVMState *s, CPUState *cpu, Error **errp);
134 static int map_kvm_dirty_gfns(KVMState *s, CPUState *cpu, Error **errp);
135 static int vcpu_unmap_regions(KVMState *s, CPUState *cpu);
@@ -2314,6 +2317,22 @@ static int kvm_vmfd_change_notify(Error **errp)
2317 &vmfd_notifier, errp);
2318 }
2319
2320 +void kvm_vcpufd_add_change_notifier(NotifierWithReturn *n)
2321 +{
2322 + notifier_with_return_list_add(&register_vcpufd_changed_notifiers, n);
2323 +}
2324 +
2325 +void kvm_vcpufd_remove_change_notifier(NotifierWithReturn *n)
2326 +{
2327 + notifier_with_return_remove(n);
2328 +}
2329 +
2330 +static int kvm_vcpufd_change_notify(Error **errp)
2331 +{
2332 + return notifier_with_return_list_notify(&register_vcpufd_changed_notifiers,
2333 + &vmfd_notifier, errp);
2334 +}
2335 +
2336 int kvm_irqchip_get_virq(KVMState *s)
2337 {
2338 int next_virq;
@@ -2841,6 +2860,13 @@ static int kvm_reset_vmfd(MachineState *ms)
2860 }
2861 assert(!err);
2862
2863 + /* notify everyone that vcpu fd has changed. */
2864 + ret = kvm_vcpufd_change_notify(&err);
2865 + if (ret < 0) {
2866 + return ret;
2867 + }
2868 + assert(!err);
2869 +
2870 /* these can be only called after ram_block_rebind() */
2871 memory_listener_register(&kml->listener, &address_space_memory);
2872 memory_listener_register(&kvm_io_listener, &address_space_io);
accel/stubs/kvm-stub.c
+10
@@ -87,6 +87,16 @@ void kvm_vmfd_remove_change_notifier(NotifierWithReturn *n)
87 {
88 }
89
90 +void kvm_vcpufd_add_change_notifier(NotifierWithReturn *n)
91 +{
92 + return;
93 +}
94 +
95 +void kvm_vcpufd_remove_change_notifier(NotifierWithReturn *n)
96 +{
97 + return;
98 +}
99 +
100 int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
101 EventNotifier *rn, int virq)
102 {
include/system/kvm.h
+17
@@ -590,4 +590,21 @@ void kvm_vmfd_add_change_notifier(NotifierWithReturn *n);
590 */
591 void kvm_vmfd_remove_change_notifier(NotifierWithReturn *n);
592
593 +/**
594 + * kvm_vcpufd_add_change_notifier - register a notifier to get notified when
595 + * a KVM vcpu file descriptors changes as a part of the confidential guest
596 + * "reset" process. Various subsystems should use this mechanism to take
597 + * actions such as re-issuing vcpu ioctls as a part of setting up vcpu
598 + * features.
599 + * @n: notifier with return value.
600 + */
601 +void kvm_vcpufd_add_change_notifier(NotifierWithReturn *n);
602 +
603 +/**
604 + * kvm_vcpufd_remove_change_notifier - de-register a notifer previously
605 + * registered with kvm_vcpufd_add_change_notifier call.
606 + * @n: notifier that was previously registered.
607 + */
608 +void kvm_vcpufd_remove_change_notifier(NotifierWithReturn *n);
609 +
610 #endif