0a122b
From b75b57e4a5b75f03c5d302ced1287f47de6c9ccc Mon Sep 17 00:00:00 2001
0a122b
From: Orit Wasserman <owasserm@redhat.com>
0a122b
Date: Tue, 11 Feb 2014 15:32:37 +0100
0a122b
Subject: [PATCH 16/28] Don't abort on memory allocation error
0a122b
0a122b
RH-Author: Orit Wasserman <owasserm@redhat.com>
0a122b
Message-id: <1392132757-18587-4-git-send-email-owasserm@redhat.com>
0a122b
Patchwork-id: 57211
0a122b
O-Subject: [RHEL7 qemu-kvm PATCH 3/3] Don't abort on memory allocation error
0a122b
Bugzilla: 1047448
0a122b
RH-Acked-by: Dr. David Alan Gilbert (git) <dgilbert@redhat.com>
0a122b
RH-Acked-by: Juan Quintela <quintela@redhat.com>
0a122b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
0a122b
It is better to fail migration in case of failure to
0a122b
allocate new cache item
0a122b
0a122b
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
0a122b
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
0a122b
Signed-off-by: Juan Quintela <quintela@redhat.com>
0a122b
(cherry picked from commit 89db9987c07977bdb78d5d4b41d65e7acb9a5a2c)
0a122b
---
0a122b
 arch_init.c                    |  4 +++-
0a122b
 include/migration/page_cache.h |  4 +++-
0a122b
 page_cache.c                   | 16 +++++++++++-----
0a122b
 3 files changed, 17 insertions(+), 7 deletions(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 arch_init.c                    |    4 +++-
0a122b
 include/migration/page_cache.h |    4 +++-
0a122b
 page_cache.c                   |   16 +++++++++++-----
0a122b
 3 files changed, 17 insertions(+), 7 deletions(-)
0a122b
0a122b
diff --git a/arch_init.c b/arch_init.c
0a122b
index fc0f569..0aacdac 100644
0a122b
--- a/arch_init.c
0a122b
+++ b/arch_init.c
0a122b
@@ -286,7 +286,9 @@ static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
0a122b
 
0a122b
     if (!cache_is_cached(XBZRLE.cache, current_addr)) {
0a122b
         if (!last_stage) {
0a122b
-            cache_insert(XBZRLE.cache, current_addr, current_data);
0a122b
+            if (cache_insert(XBZRLE.cache, current_addr, current_data) == -1) {
0a122b
+                return -1;
0a122b
+            }
0a122b
         }
0a122b
         acct_info.xbzrle_cache_miss++;
0a122b
         return -1;
0a122b
diff --git a/include/migration/page_cache.h b/include/migration/page_cache.h
0a122b
index 87894fe..d156f0d 100644
0a122b
--- a/include/migration/page_cache.h
0a122b
+++ b/include/migration/page_cache.h
0a122b
@@ -60,11 +60,13 @@ uint8_t *get_cached_data(const PageCache *cache, uint64_t addr);
0a122b
  * cache_insert: insert the page into the cache. the page cache
0a122b
  * will dup the data on insert. the previous value will be overwritten
0a122b
  *
0a122b
+ * Returns -1 on error
0a122b
+ *
0a122b
  * @cache pointer to the PageCache struct
0a122b
  * @addr: page address
0a122b
  * @pdata: pointer to the page
0a122b
  */
0a122b
-void cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata);
0a122b
+int cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata);
0a122b
 
0a122b
 /**
0a122b
  * cache_resize: resize the page cache. In case of size reduction the extra
0a122b
diff --git a/page_cache.c b/page_cache.c
0a122b
index 2920123..250772d 100644
0a122b
--- a/page_cache.c
0a122b
+++ b/page_cache.c
0a122b
@@ -151,7 +151,7 @@ uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
0a122b
     return cache_get_by_addr(cache, addr)->it_data;
0a122b
 }
0a122b
 
0a122b
-void cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
0a122b
+int cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
0a122b
 {
0a122b
 
0a122b
     CacheItem *it = NULL;
0a122b
@@ -162,16 +162,22 @@ void cache_insert(PageCache *cache, uint64_t addr, uint8_t *pdata)
0a122b
     /* actual update of entry */
0a122b
     it = cache_get_by_addr(cache, addr);
0a122b
 
0a122b
-    /* free old cached data if any */
0a122b
-    g_free(it->it_data);
0a122b
-
0a122b
+    /* allocate page */
0a122b
     if (!it->it_data) {
0a122b
+        it->it_data = g_try_malloc(cache->page_size);
0a122b
+        if (!it->it_data) {
0a122b
+            DPRINTF("Error allocating page\n");
0a122b
+            return -1;
0a122b
+        }
0a122b
         cache->num_items++;
0a122b
     }
0a122b
 
0a122b
-    it->it_data = g_memdup(pdata, cache->page_size);
0a122b
+    memcpy(it->it_data, pdata, cache->page_size);
0a122b
+
0a122b
     it->it_age = ++cache->max_item_age;
0a122b
     it->it_addr = addr;
0a122b
+
0a122b
+    return 0;
0a122b
 }
0a122b
 
0a122b
 int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
0a122b
-- 
0a122b
1.7.1
0a122b