]> git.ipfire.org Git - thirdparty/qemu.git/commitdiff
system/physmem: add helper to reattach existing memory after KVM VM fd change
authorAni Sinha <anisinha@redhat.com>
Wed, 25 Feb 2026 03:49:09 +0000 (09:19 +0530)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 27 Feb 2026 13:22:07 +0000 (14:22 +0100)
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>
include/system/physmem.h
system/physmem.c

index 7bb7d3e154530414d33ec8b3a30363bf1e18ec98..da91b77bd9b518ff562e3cd80936da5fa38aa41b 100644 (file)
@@ -51,5 +51,6 @@ physical_memory_snapshot_and_clear_dirty(MemoryRegion *mr, hwaddr offset,
 bool physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
                                         ram_addr_t start,
                                         ram_addr_t length);
+int ram_block_rebind(Error **errp);
 
 #endif
index 2fb0c25c93b5c59dc84130428977cf7b7aed4c9a..e5ff26acecd26cc50b42a9af3a7da62852411716 100644 (file)
@@ -2826,6 +2826,34 @@ found:
     return block;
 }
 
+/*
+ * Creates new guest memfd for the ramblocks and closes the
+ * existing memfd.
+ */
+int ram_block_rebind(Error **errp)
+{
+    RAMBlock *block;
+
+    qemu_mutex_lock_ramlist();
+
+    RAMBLOCK_FOREACH(block) {
+        if (block->flags & RAM_GUEST_MEMFD) {
+            if (block->guest_memfd >= 0) {
+                close(block->guest_memfd);
+            }
+            block->guest_memfd = kvm_create_guest_memfd(block->max_length,
+                                                        0, errp);
+            if (block->guest_memfd < 0) {
+                qemu_mutex_unlock_ramlist();
+                return -1;
+            }
+
+        }
+    }
+    qemu_mutex_unlock_ramlist();
+    return 0;
+}
+
 /*
  * Finds the named RAMBlock
  *