Skip to contentSkip to conversation

block: fix memory leak in early exit

PATCH In review Stefan Hajnoczi 4 participants 6 messages Oct 15, 2015 – Oct 16, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix memory leak in early exit

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

---
 block/stream.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
block/stream.c +10 -5
index ab0bd05..1986e9a 100644
@@ -120,16 +120,16 @@ static void coroutine_fn stream_run(void *opaque)
     int ret = 0;
     int n = 0;
     void *buf;
+    bool reached_end = false;
 
     if (!bs->backing_hd) {
-        block_job_completed(&s->common, 0);
-        return;
+        goto out;
     }
 
     s->common.len = bdrv_getlength(bs);
     if (s->common.len < 0) {
-        block_job_completed(&s->common, s->common.len);
-        return;
+        ret = s->common.len;
+        goto out;
     }
 
     end = s->common.len >> BDRV_SECTOR_BITS;
@@ -207,6 +207,10 @@ wait:
         s->common.offset += n * BDRV_SECTOR_SIZE;
     }
 
+    if (sector_num == end) {
+        reached_end = true;
+    }
+
     if (!base) {
         bdrv_disable_copy_on_read(bs);
     }
@@ -216,10 +220,11 @@ wait:
 
     qemu_vfree(buf);
 
+out:
     /* Modify backing chain and close BDSes in main loop */
     data = g_malloc(sizeof(*data));
     data->ret = ret;
-    data->reached_end = sector_num == end;
+    data->reached_end = reached_end;
     block_job_defer_to_main_loop(&s->common, stream_complete, data);
 }
 
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 68 lines · Stefan Hajnoczi

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

---
 block/stream.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
block/stream.c +10 -5
index ab0bd05..1986e9a 100644
@@ -120,16 +120,16 @@ static void coroutine_fn stream_run(void *opaque)
     int ret = 0;
     int n = 0;
     void *buf;
+    bool reached_end = false;
 
     if (!bs->backing_hd) {
-        block_job_completed(&s->common, 0);
-        return;
+        goto out;
     }
 
     s->common.len = bdrv_getlength(bs);
     if (s->common.len < 0) {
-        block_job_completed(&s->common, s->common.len);
-        return;
+        ret = s->common.len;
+        goto out;
     }
 
     end = s->common.len >> BDRV_SECTOR_BITS;
@@ -207,6 +207,10 @@ wait:
         s->common.offset += n * BDRV_SECTOR_SIZE;
     }
 
+    if (sector_num == end) {
+        reached_end = true;
+    }
+
     if (!base) {
         bdrv_disable_copy_on_read(bs);
     }
@@ -216,10 +220,11 @@ wait:
 
     qemu_vfree(buf);
 
+out:
     /* Modify backing chain and close BDSes in main loop */
     data = g_malloc(sizeof(*data));
     data->ret = ret;
-    data->reached_end = sector_num == end;
+    data->reached_end = reached_end;
     block_job_defer_to_main_loop(&s->common, stream_complete, data);
 }
 
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefan Hajnoczi
Quoted 68 lines · Stefan Hajnoczi

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

---
 block/stream.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
block/stream.c +10 -5
index ab0bd05..1986e9a 100644
@@ -120,16 +120,16 @@ static void coroutine_fn stream_run(void *opaque)
     int ret = 0;
     int n = 0;
     void *buf;
+    bool reached_end = false;
 
     if (!bs->backing_hd) {
-        block_job_completed(&s->common, 0);
-        return;
+        goto out;
     }
 
     s->common.len = bdrv_getlength(bs);
     if (s->common.len < 0) {
-        block_job_completed(&s->common, s->common.len);
-        return;
+        ret = s->common.len;
+        goto out;
     }
 
     end = s->common.len >> BDRV_SECTOR_BITS;
@@ -207,6 +207,10 @@ wait:
         s->common.offset += n * BDRV_SECTOR_SIZE;
     }
 
+    if (sector_num == end) {
+        reached_end = true;
+    }
+
     if (!base) {
         bdrv_disable_copy_on_read(bs);
     }
@@ -216,10 +220,11 @@ wait:
 
     qemu_vfree(buf);
 
+out:
     /* Modify backing chain and close BDSes in main loop */
     data = g_malloc(sizeof(*data));
     data->ret = ret;
-    data->reached_end = sector_num == end;
+    data->reached_end = reached_end;
     block_job_defer_to_main_loop(&s->common, stream_complete, data);
 }
 
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#3 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

Quoted 3 lines

I had a slightly simpler version of this in my intermediate block streaming series in case you're interested:

https://patchwork.ozlabs.org/patch/471881/

But this one looks good to me too, so:

Berto

#4 lore raw
Quoted 14 lines · Alberto Garcia
Quoted 9 lines · Stefan Hajnoczi

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

Quoted 3 lines
Quoted 8 lines

I had a slightly simpler version of this in my intermediate block streaming series in case you're interested:

   https://patchwork.ozlabs.org/patch/471881/

But this one looks good to me too, so:

Kevin, please take Alberto's patch instead of mine. The Message-ID is:

d575a576c18d8972ac1a200c4022b39cbbce2507.1435008395.git.berto@igalia.com

BTW, I notice that Jeff isn't listed as maintainer for block/stream.c. So according to MAINTAINERS this patch goes through you.

Stefan

#5 lore raw
Quoted 24 lines · Stefan Hajnoczi
Quoted 14 lines · Alberto Garcia
Quoted 9 lines · Stefan Hajnoczi

The stream block job has two early exit code paths. They do not free s->backing_file_str.

Also, the early exits rely on the fact that the coroutine hasn't yielded yet and was launched from the main thread. Therefore the coroutine is guaranteed to be running in the main thread where block_job_completed() may be called safely. This is very subtle so it's nice to eliminate the assumption by unifying the early exit with the normal exit code path.

Quoted 3 lines
Quoted 8 lines

I had a slightly simpler version of this in my intermediate block streaming series in case you're interested:

   https://patchwork.ozlabs.org/patch/471881/

But this one looks good to me too, so:

Quoted 8 lines

Kevin, please take Alberto's patch instead of mine. The Message-ID is:

d575a576c18d8972ac1a200c4022b39cbbce2507.1435008395.git.berto@igalia.com

BTW, I notice that Jeff isn't listed as maintainer for block/stream.c. So according to MAINTAINERS this patch goes through you.

Stefan

I think that is probably just a typo - it lists block/stream.h in MAINTAINERS, not block/stream.c.