Skip to contentSkip to conversation

arch_init.c: Free 'cache' in cache_fini() to avoid memory leak

Qemu-devel Applied Chen Gang 5 participants 8 messages Jun 2, 2014 – Jun 8, 2014

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

---
 arch_init.c  | 1 -
 page_cache.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
arch_init.c +0 -1
index 9f1a174..23044c1 100644
@@ -739,7 +739,6 @@ static void migration_end(void)
     XBZRLE_cache_lock();
     if (XBZRLE.cache) {
         cache_fini(XBZRLE.cache);
-        g_free(XBZRLE.cache);
         g_free(XBZRLE.encoded_buf);
         g_free(XBZRLE.current_buf);
         XBZRLE.cache = NULL;
page_cache.c +1 -0
index b033681..89bb1ec 100644
@@ -109,6 +109,7 @@ void cache_fini(PageCache *cache)
 
     g_free(cache->page_cache);
     cache->page_cache = NULL;
+    g_free(cache);
 }
 
 static size_t cache_get_cache_pos(const PageCache *cache,
Signature · 1 line
1.7.11.7
Reviewed-by
#1 lore raw
Quoted 31 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

---
 arch_init.c  | 1 -
 page_cache.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
arch_init.c +0 -1
index 9f1a174..23044c1 100644
@@ -739,7 +739,6 @@ static void migration_end(void)
     XBZRLE_cache_lock();
     if (XBZRLE.cache) {
         cache_fini(XBZRLE.cache);
-        g_free(XBZRLE.cache);
         g_free(XBZRLE.encoded_buf);
         g_free(XBZRLE.current_buf);
         XBZRLE.cache = NULL;
page_cache.c +0 -0
index b033681..89bb1ec 100644

page_cache.c void cache_fini(PageCache *cache) · line 115

Quoted 7 lines
page_cache.c +1 -0
@@ -109,6 +109,7 @@ void cache_fini(PageCache *cache)
 
     g_free(cache->page_cache);
     cache->page_cache = NULL;
+    g_free(cache);
 }
 
 static size_t cache_get_cache_pos(const PageCache *cache,
#2 lore raw
In reply to Chen Gang
Quoted 6 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

I'm not sure I get you. Is the following accurate?

migration: Plug memory leak in migrate-set-cache-size command

We call g_free() after cache_fini() in migration_end(), but we don't call it after cache_fini() in xbzrle_cache_resize(), leaking the memory.

cache_init() and cache_fini() are a pair. Since cache_init() allocates the cache, let cache_fini() free it. This plugs the leak.

author
#3 lore raw
Quoted 11 lines · Markus Armbruster
Quoted 6 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

I'm not sure I get you. Is the following accurate?

Quoted 9 lines

migration: Plug memory leak in migrate-set-cache-size command

We call g_free() after cache_fini() in migration_end(), but we don't call it after cache_fini() in xbzrle_cache_resize(), leaking the memory.

cache_init() and cache_fini() are a pair. Since cache_init() allocates the cache, let cache_fini() free it. This plugs the leak.

Yeah, and excuse me for my poor English.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
#4 lore raw
Quoted 18 lines · Chen Gang
Quoted 17 lines · Markus Armbruster
Quoted 6 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

I'm not sure I get you. Is the following accurate?

migration: Plug memory leak in migrate-set-cache-size command

We call g_free() after cache_fini() in migration_end(), but we don't call it after cache_fini() in xbzrle_cache_resize(), leaking the memory.

Quoted 5 lines
Quoted 3 lines

cache_init() and cache_fini() are a pair. Since cache_init() allocates the cache, let cache_fini() free it. This plugs the leak.

Yeah, and excuse me for my poor English.

No problem, we figured it out together :)

Would be nice to use the above text as commit message. Perhaps it can be done on commit.

author
#5 lore raw
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · Chen Gang
Quoted 20 lines · Markus Armbruster
Quoted 6 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

I'm not sure I get you. Is the following accurate?

migration: Plug memory leak in migrate-set-cache-size command

We call g_free() after cache_fini() in migration_end(), but we don't call it after cache_fini() in xbzrle_cache_resize(), leaking the memory.

cache_init() and cache_fini() are a pair. Since cache_init() allocates the cache, let cache_fini() free it. This plugs the leak.

Quoted 7 lines
Quoted 1 line

Yeah, and excuse me for my poor English.

No problem, we figured it out together :)

Would be nice to use the above text as commit message. Perhaps it can be done on commit.

That is fine to me.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
#6 lore raw
In reply to Chen Gang
Quoted 9 lines · * Chen Gang ([email protected]) wrote:

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

Yes, good catch.

Quoted 32 lines
---
 arch_init.c  | 1 -
 page_cache.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
arch_init.c +0 -1
index 9f1a174..23044c1 100644
@@ -739,7 +739,6 @@ static void migration_end(void)
     XBZRLE_cache_lock();
     if (XBZRLE.cache) {
         cache_fini(XBZRLE.cache);
-        g_free(XBZRLE.cache);
         g_free(XBZRLE.encoded_buf);
         g_free(XBZRLE.current_buf);
         XBZRLE.cache = NULL;
page_cache.c +1 -0
index b033681..89bb1ec 100644
@@ -109,6 +109,7 @@ void cache_fini(PageCache *cache)
 
     g_free(cache->page_cache);
     cache->page_cache = NULL;
+    g_free(cache);
 }
 
 static size_t cache_get_cache_pos(const PageCache *cache,
Signature · 1 line
1.7.11.7

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw
In reply to Chen Gang
Quoted 6 lines · Chen Gang

Call g_free() after cache_fini() in migration_end(), but do not call g_free() after call cache_fini() in xbzrle_cache_resize() which will cause memory leak.

cache_init() and cache_fini() are pair, so need let cache_fini() call g_free(cache) to match cache_init(), then fix current issue too.

Applied to -trivial, with the following commit message:

migration: Plug memory leak in migrate-set-cache-size command

We call g_free() after cache_fini() in migration_end(), but we don't call it after cache_fini() in xbzrle_cache_resize(), leaking the memory.

cache_init() and cache_fini() are a pair. Since cache_init() allocates the cache, let cache_fini() free it. This plugs the leak.

Thanks,

/mjt