@samitouri / QOSamiQemu / commits / 1b4573f538

i386/sev: add notifiers only once

The various notifiers that are used needs to be installed only once not on every initialization. This includes the vm state change notifier and others. This change uses 'cgs->ready' flag to install the notifiers only one time, the first time. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-21-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC 1b4573f538837aca7638c9a57f5808f935bd1c65
1 file changed +19 -17
target/i386/sev.c
+19 -17
@@ -1920,8 +1920,9 @@ static int sev_common_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1920 return -1;
1921 }
1922
1923 - qemu_add_vm_change_state_handler(sev_vm_state_change, sev_common);
1924 -
1923 + if (!cgs->ready) {
1924 + qemu_add_vm_change_state_handler(sev_vm_state_change, sev_common);
1925 + }
1926 cgs->ready = true;
1927
1928 return 0;
@@ -1943,22 +1944,23 @@ static int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
1944 return -1;
1945 }
1946
1946 - /*
1947 - * SEV uses these notifiers to register/pin pages prior to guest use,
1948 - * but SNP relies on guest_memfd for private pages, which has its
1949 - * own internal mechanisms for registering/pinning private memory.
1950 - */
1951 - ram_block_notifier_add(&sev_ram_notifier);
1952 -
1953 - /*
1954 - * The machine done notify event is used for SEV guests to get the
1955 - * measurement of the encrypted images. When SEV-SNP is enabled, the
1956 - * measurement is part of the guest attestation process where it can
1957 - * be collected without any reliance on the VMM. So skip registering
1958 - * the notifier for SNP in favor of using guest attestation instead.
1959 - */
1960 - qemu_add_machine_init_done_notifier(&sev_machine_done_notify);
1947 + if (!cgs->ready) {
1948 + /*
1949 + * SEV uses these notifiers to register/pin pages prior to guest use,
1950 + * but SNP relies on guest_memfd for private pages, which has its
1951 + * own internal mechanisms for registering/pinning private memory.
1952 + */
1953 + ram_block_notifier_add(&sev_ram_notifier);
1954
1955 + /*
1956 + * The machine done notify event is used for SEV guests to get the
1957 + * measurement of the encrypted images. When SEV-SNP is enabled, the
1958 + * measurement is part of the guest attestation process where it can
1959 + * be collected without any reliance on the VMM. So skip registering
1960 + * the notifier for SNP in favor of using guest attestation instead.
1961 + */
1962 + qemu_add_machine_init_done_notifier(&sev_machine_done_notify);
1963 + }
1964 return 0;
1965 }
1966