Skip to contentSkip to conversation

qcow2: Small error path fixes for l2_allocate

PATCH Applied Max Reitz 3 participants 11 messages Sep 25, 2013 – Sep 30, 2013 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover qcow2: Small error path fixes for l2_allocate

Errors in l2_allocate should always go down the error path. If this path is taken, the newly allocated L2 cluster is abandoned and should thus be freed. The L2 table on the other hand should only be put back into the cache if it was taken from it before.

Max Reitz (3):
qcow2: Don't put invalid L2 table into cache
qcow2: Free allocated L2 cluster on error
qcow2: Always use error path in l2_allocate

 block/qcow2-cluster.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/3 qcow2: Don't put invalid L2 table into cache

In l2_allocate, the fail path is executed if qcow2_cache_flush fails. However, the L2 table has not yet been fetched from the L2 table cache. The qcow2_cache_put in the fail path therefore basically gives an undefined argument as the L2 table address (in this case).

---
 block/qcow2-cluster.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +4 -2
index 738ff73..f6d47c9 100644
@@ -188,7 +188,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t old_l2_offset;
-    uint64_t *l2_table;
+    uint64_t *l2_table = NULL;
     int64_t l2_offset;
     int ret;
 
@@ -265,7 +265,9 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 
 fail:
     trace_qcow2_l2_allocate_done(bs, l1_index, ret);
-    qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
+    if (l2_table != NULL) {
+        qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
+    }
     s->l1_table[l1_index] = old_l2_offset;
     return ret;
 }
Signature · 1 line
1.8.3.1
Reviewed-by Benoit Canet on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qcow2: Don't put invalid L2 table into cache

Quoted 38 lines · Max Reitz

In l2_allocate, the fail path is executed if qcow2_cache_flush fails. However, the L2 table has not yet been fetched from the L2 table cache. The qcow2_cache_put in the fail path therefore basically gives an undefined argument as the L2 table address (in this case).

---
 block/qcow2-cluster.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +4 -2
index 738ff73..f6d47c9 100644
@@ -188,7 +188,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t old_l2_offset;
-    uint64_t *l2_table;
+    uint64_t *l2_table = NULL;
     int64_t l2_offset;
     int ret;
 
@@ -265,7 +265,9 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 
 fail:
     trace_qcow2_l2_allocate_done(bs, l1_index, ret);
-    qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
+    if (l2_table != NULL) {
+        qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
+    }
     s->l1_table[l1_index] = old_l2_offset;
     return ret;
 }
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 2/3 qcow2: Free allocated L2 cluster on error

In reply to Max Reitz

If an error occurs in l2_allocate, the allocated (but unused) L2 cluster should be freed.

---
 block/qcow2-cluster.c | 4 ++++
 1 file changed, 4 insertions(+)
block/qcow2-cluster.c +4 -0
index f6d47c9..1c3d3fc 100644
@@ -269,6 +269,10 @@ fail:
         qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
     }
     s->l1_table[l1_index] = old_l2_offset;
+    if (l2_offset > 0) {
+        qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
+                            QCOW2_DISCARD_ALWAYS);
+    }
     return ret;
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by Benoit Canet on v1 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] qcow2: Free allocated L2 cluster on error

Quoted 27 lines · Max Reitz

If an error occurs in l2_allocate, the allocated (but unused) L2 cluster should be freed.

---
 block/qcow2-cluster.c | 4 ++++
 1 file changed, 4 insertions(+)
block/qcow2-cluster.c +4 -0
index f6d47c9..1c3d3fc 100644
@@ -269,6 +269,10 @@ fail:
         qcow2_cache_put(bs, s->l2_table_cache, (void**) table);
     }
     s->l1_table[l1_index] = old_l2_offset;
+    if (l2_offset > 0) {
+        qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t),
+                            QCOW2_DISCARD_ALWAYS);
+    }
     return ret;
 }
 
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] qcow2: Free allocated L2 cluster on error

In reply to Max Reitz

Am 25.09.2013 um 16:37 hat Max Reitz geschrieben:

Quoted 7 lines

If an error occurs in l2_allocate, the allocated (but unused) L2 cluster should be freed.

---
 block/qcow2-cluster.c | 4 ++++
 1 file changed, 4 insertions(+)

This needs an update of the reference output for test case 026 (both for -nocache and writethrough).

Most of the changes look expected and good, like cluster leaks disappearing. With -nocache, however, there are a few cases that failed previously and result in successful writes now. It would be interesting to see the explanation for these before we merge the patch.

Kevin

author
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] qcow2: Free allocated L2 cluster on error

Quoted 10 lines · Kevin Wolf

Am 25.09.2013 um 16:37 hat Max Reitz geschrieben:

Quoted 7 lines

If an error occurs in l2_allocate, the allocated (but unused) L2 cluster should be freed.

---
  block/qcow2-cluster.c | 4 ++++
  1 file changed, 4 insertions(+)

This needs an update of the reference output for test case 026 (both for -nocache and writethrough).

Yes, right.

Quoted 4 lines

Most of the changes look expected and good, like cluster leaks disappearing. With -nocache, however, there are a few cases that failed previously and result in successful writes now. It would be interesting to see the explanation for these before we merge the patch.

I personally don't see this cases. Could you give an example?

Max

#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] qcow2: Free allocated L2 cluster on error

Am 30.09.2013 um 11:48 hat Max Reitz geschrieben:

Quoted 11 lines · Kevin Wolf
Quoted 10 lines

Am 25.09.2013 um 16:37 hat Max Reitz geschrieben:

Quoted 7 lines

If an error occurs in l2_allocate, the allocated (but unused) L2 cluster should be freed.

---
 block/qcow2-cluster.c | 4 ++++
 1 file changed, 4 insertions(+)

This needs an update of the reference output for test case 026 (both for -nocache and writethrough).

Quoted 7 lines

Yes, right.

Quoted 4 lines

Most of the changes look expected and good, like cluster leaks disappearing. With -nocache, however, there are a few cases that failed previously and result in successful writes now. It would be interesting to see the explanation for these before we merge the patch.

I personally don't see this cases. Could you give an example?

Weird, I can't reproduce it any more. I guess it was a problem on my side then.

Kevin

author
#8 lore raw

PATCH 3/3 qcow2: Always use error path in l2_allocate

In reply to Max Reitz

Just returning -errno in some cases prevents trace_qcow2_l2_allocate_done from being executed (and, in one case, also the unused allocated L2 table from being freed). Always going down the error path fixes this.

---
 block/qcow2-cluster.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +3 -2
index 1c3d3fc..c442f6c 100644
@@ -200,7 +200,8 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 
     l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
     if (l2_offset < 0) {
-        return l2_offset;
+        ret = l2_offset;
+        goto fail;
     }
 
     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
@@ -213,7 +214,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
     trace_qcow2_l2_allocate_get_empty(bs, l1_index);
     ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table);
     if (ret < 0) {
-        return ret;
+        goto fail;
     }
 
     l2_table = *table;
Signature · 1 line
1.8.3.1
Reviewed-by Benoit Canet on v1 3/3
#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] qcow2: Always use error path in l2_allocate

Quoted 37 lines · Max Reitz

Just returning -errno in some cases prevents trace_qcow2_l2_allocate_done from being executed (and, in one case, also the unused allocated L2 table from being freed). Always going down the error path fixes this.

---
 block/qcow2-cluster.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block/qcow2-cluster.c +3 -2
index 1c3d3fc..c442f6c 100644
@@ -200,7 +200,8 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
 
     l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
     if (l2_offset < 0) {
-        return l2_offset;
+        ret = l2_offset;
+        goto fail;
     }
 
     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
@@ -213,7 +214,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
     trace_qcow2_l2_allocate_get_empty(bs, l1_index);
     ret = qcow2_cache_get_empty(bs, s->l2_table_cache, l2_offset, (void**) table);
     if (ret < 0) {
-        return ret;
+        goto fail;
     }
 
     l2_table = *table;
Signature · 1 line
1.8.3.1
Applied
#10 lore raw
In reply to Max Reitz

Am 25.09.2013 um 16:37 hat Max Reitz geschrieben:

Quoted 5 lines

Errors in l2_allocate should always go down the error path. If this path is taken, the newly allocated L2 cluster is abandoned and should thus be freed. The L2 table on the other hand should only be put back into the cache if it was taken from it before.

Quoted 7 lines

Max Reitz (3):
qcow2: Don't put invalid L2 table into cache
qcow2: Free allocated L2 cluster on error
qcow2: Always use error path in l2_allocate

 block/qcow2-cluster.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

Thanks, applied all to the block branch.

Kevin