@samitouri / QOSamiQemu / commits / df8df3cb6b

system/physmem: add helper to reattach existing memory after KVM VM fd change

After the guest KVM file descriptor has changed as a part of the process of confidential guest reset mechanism, existing memory needs to be reattached to the new file descriptor. This change adds a helper function ram_block_rebind() for this purpose. The next patch will make use of this function. Signed-off-by: Ani Sinha <anisinha@redhat.com> Link: https://lore.kernel.org/r/20260225035000.385950-5-anisinha@redhat.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Ani Sinha committed Feb 25, 2026 at 09:19 UTC df8df3cb6b743372ebb335bd8404bc3d748da350
2 files changed +29
include/system/physmem.h
+1
@@ -51,5 +51,6 @@ physical_memory_snapshot_and_clear_dirty(MemoryRegion *mr, hwaddr offset,
51 bool physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
52 ram_addr_t start,
53 ram_addr_t length);
54 +int ram_block_rebind(Error **errp);
55
56 #endif
system/physmem.c
+28
@@ -2826,6 +2826,34 @@ found:
2826 return block;
2827 }
2828
2829 +/*
2830 + * Creates new guest memfd for the ramblocks and closes the
2831 + * existing memfd.
2832 + */
2833 +int ram_block_rebind(Error **errp)
2834 +{
2835 + RAMBlock *block;
2836 +
2837 + qemu_mutex_lock_ramlist();
2838 +
2839 + RAMBLOCK_FOREACH(block) {
2840 + if (block->flags & RAM_GUEST_MEMFD) {
2841 + if (block->guest_memfd >= 0) {
2842 + close(block->guest_memfd);
2843 + }
2844 + block->guest_memfd = kvm_create_guest_memfd(block->max_length,
2845 + 0, errp);
2846 + if (block->guest_memfd < 0) {
2847 + qemu_mutex_unlock_ramlist();
2848 + return -1;
2849 + }
2850 +
2851 + }
2852 + }
2853 + qemu_mutex_unlock_ramlist();
2854 + return 0;
2855 +}
2856 +
2857 /*
2858 * Finds the named RAMBlock
2859 *