Blame SOURCES/kvm-memory-cleanup-side-effects-of-memory_region_init_fo.patch

7711c0
From 2d02fc0e933a7d4a30e8bb7c665d898d00ac5e00 Mon Sep 17 00:00:00 2001
7711c0
From: Igor Mammedov <imammedo@redhat.com>
7711c0
Date: Fri, 23 Nov 2018 11:58:47 +0100
7711c0
Subject: [PATCH 14/34] memory: cleanup side effects of
7711c0
 memory_region_init_foo() on failure
7711c0
MIME-Version: 1.0
7711c0
Content-Type: text/plain; charset=UTF-8
7711c0
Content-Transfer-Encoding: 8bit
7711c0
7711c0
RH-Author: Igor Mammedov <imammedo@redhat.com>
7711c0
Message-id: <1542974327-165907-1-git-send-email-imammedo@redhat.com>
7711c0
Patchwork-id: 83127
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH v2] memory: cleanup side effects of memory_region_init_foo() on failure
7711c0
Bugzilla: 1585155
7711c0
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
7711c0
7711c0
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1585155
7711c0
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=19261375
7711c0
7711c0
if MemoryRegion intialization fails it's left in semi-initialized state,
7711c0
where it's size is not 0 and attached as child to owner object.
7711c0
And this leds to crash in following use-case:
7711c0
    (monitor) object_add memory-backend-file,id=mem1,size=99999G,mem-path=/tmp/foo,discard-data=yes
7711c0
    memory.c:2083: memory_region_get_ram_ptr: Assertion `mr->ram_block' failed
7711c0
    Aborted (core dumped)
7711c0
it happens due to assumption that memory region is intialized when
7711c0
   memory_region_size() != 0
7711c0
and therefore it's ok to access it in
7711c0
   file_backend_unparent()
7711c0
      if (memory_region_size() != 0)
7711c0
          memory_region_get_ram_ptr()
7711c0
7711c0
which happens when object_add fails and unparents failed backend making
7711c0
file_backend_unparent() access invalid memory region.
7711c0
7711c0
Fix it by making sure that memory_region_init_foo() APIs cleanup externally
7711c0
visible side effects on failure (like set size to 0 and unparenting object)
7711c0
7711c0
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
7711c0
Message-Id: <1536064777-42312-1-git-send-email-imammedo@redhat.com>
7711c0
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
7711c0
(cherry picked from commit 1cd3d492624da399d66c4c3e6a5eabb8f96bb0a2)
7711c0
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
7711c0
---
7711c0
v2:
7711c0
  - clean backport since dependency has been backported and merged as
7711c0
      (commit 754dd8f2 memory, exec: switch file ram allocation functions to 'flags' parameters)
7711c0
7711c0
 enter the commit message for your changes. Lines starting
7711c0
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 memory.c | 48 ++++++++++++++++++++++++++++++++++++++++++------
7711c0
 1 file changed, 42 insertions(+), 6 deletions(-)
7711c0
7711c0
diff --git a/memory.c b/memory.c
7711c0
index 7b47f53..4974f97 100644
7711c0
--- a/memory.c
7711c0
+++ b/memory.c
7711c0
@@ -1519,12 +1519,18 @@ void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
7711c0
                                              bool share,
7711c0
                                              Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ram = true;
7711c0
     mr->terminates = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
-    mr->ram_block = qemu_ram_alloc(size, share, mr, errp);
7711c0
+    mr->ram_block = qemu_ram_alloc(size, share, mr, &err;;
7711c0
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 
7711c0
 void memory_region_init_resizeable_ram(MemoryRegion *mr,
7711c0
@@ -1537,13 +1543,19 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr,
7711c0
                                                        void *host),
7711c0
                                        Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ram = true;
7711c0
     mr->terminates = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
     mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
7711c0
-                                              mr, errp);
7711c0
+                                              mr, &err;;
7711c0
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 
7711c0
 #ifdef __linux__
7711c0
@@ -1556,13 +1568,19 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
7711c0
                                       const char *path,
7711c0
                                       Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ram = true;
7711c0
     mr->terminates = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
     mr->align = align;
7711c0
-    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, errp);
7711c0
+    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err;;
7711c0
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 
7711c0
 void memory_region_init_ram_from_fd(MemoryRegion *mr,
7711c0
@@ -1573,14 +1591,20 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
7711c0
                                     int fd,
7711c0
                                     Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ram = true;
7711c0
     mr->terminates = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
     mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
7711c0
                                            share ? RAM_SHARED : 0,
7711c0
-                                           fd, errp);
7711c0
+                                           fd, &err;;
7711c0
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 #endif
7711c0
 
7711c0
@@ -1631,13 +1655,19 @@ void memory_region_init_rom_nomigrate(MemoryRegion *mr,
7711c0
                                       uint64_t size,
7711c0
                                       Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ram = true;
7711c0
     mr->readonly = true;
7711c0
     mr->terminates = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
-    mr->ram_block = qemu_ram_alloc(size, false, mr, errp);
7711c0
+    mr->ram_block = qemu_ram_alloc(size, false, mr, &err;;
7711c0
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 
7711c0
 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
7711c0
@@ -1648,6 +1678,7 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
7711c0
                                              uint64_t size,
7711c0
                                              Error **errp)
7711c0
 {
7711c0
+    Error *err = NULL;
7711c0
     assert(ops);
7711c0
     memory_region_init(mr, owner, name, size);
7711c0
     mr->ops = ops;
7711c0
@@ -1655,7 +1686,12 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
7711c0
     mr->terminates = true;
7711c0
     mr->rom_device = true;
7711c0
     mr->destructor = memory_region_destructor_ram;
7711c0
-    mr->ram_block = qemu_ram_alloc(size, false,  mr, errp);
7711c0
+    mr->ram_block = qemu_ram_alloc(size, false,  mr, &err;;
7711c0
+    if (err) {
7711c0
+        mr->size = int128_zero();
7711c0
+        object_unparent(OBJECT(mr));
7711c0
+        error_propagate(errp, err);
7711c0
+    }
7711c0
 }
7711c0
 
7711c0
 void memory_region_init_iommu(void *_iommu_mr,
7711c0
-- 
7711c0
1.8.3.1
7711c0