Skip to contentSkip to conversation

qcow2: Fix L1 table memory allocation

PATCH In review Kevin Wolf 3 participants 6 messages Jul 7, 2009 – Jul 8, 2009 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 qcow2: Fix L1 table memory allocation

Contrary to what one could expect, the size of L1 tables is not cluster aligned. So as we're writing whole sectors now instead of single entries, we need to ensure that the L1 table in memory is large enough; otherwise write would access memory after the end of the L1 table.

---
 block/qcow2-cluster.c  |    2 +-
 block/qcow2-refcount.c |    2 +-
 block/qcow2.c          |    3 ++-
 3 files changed, 4 insertions(+), 3 deletions(-)
block/qcow2-cluster.c +1 -1
index d349655..057dac5 100644
@@ -47,7 +47,7 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
 #endif
 
     new_l1_size2 = sizeof(uint64_t) * new_l1_size;
-    new_l1_table = qemu_mallocz(new_l1_size2);
+    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
     memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
 
     /* write new table (align to cluster) */
block/qcow2-refcount.c +1 -1
index d42c6e6..e6c857e 100644
@@ -513,7 +513,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
     l1_size2 = l1_size * sizeof(uint64_t);
     l1_allocated = 0;
     if (l1_table_offset != s->l1_table_offset) {
-        l1_table = qemu_malloc(l1_size2);
+        l1_table = qemu_mallocz(align_offset(l1_size2, 512));
         l1_allocated = 1;
         if (bdrv_pread(s->hd, l1_table_offset,
                        l1_table, l1_size2) != l1_size2)
block/qcow2.c +2 -1
index 20c114b..607329b 100644
@@ -208,7 +208,8 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
     if (s->l1_size < s->l1_vm_state_index)
         goto fail;
     s->l1_table_offset = header.l1_table_offset;
-    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
+    s->l1_table = qemu_mallocz(
+        align_offset(s->l1_size * sizeof(uint64_t), 512));
     if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
         s->l1_size * sizeof(uint64_t))
         goto fail;
Signature · 1 line
1.6.0.6
#1 lore raw

block/qcow2-cluster.c int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) · line 51

Quoted 11 lines · Kevin Wolf

Contrary to what one could expect, the size of L1 tables is not cluster aligned. So as we're writing whole sectors now instead of single entries, we need to ensure that the L1 table in memory is large enough; otherwise write would access memory after the end of the L1 table.

      new_l1_size2 = sizeof(uint64_t) * new_l1_size;
block/qcow2-cluster.c +1 -1
-    new_l1_table = qemu_mallocz(new_l1_size2);
+    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
      memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));

Unrelated note: using qemu_memalign() here would reduce the copying for cache=none.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#2 lore raw
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Kevin Wolf

Contrary to what one could expect, the size of L1 tables is not cluster aligned. So as we're writing whole sectors now instead of single entries, we need to ensure that the L1 table in memory is large enough; otherwise write would access memory after the end of the L1 table.

Quoted 8 lines

block/qcow2-cluster.c int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) · line 51

Quoted 5 lines
      new_l1_size2 = sizeof(uint64_t) * new_l1_size;
block/qcow2-cluster.c +1 -1
-    new_l1_table = qemu_mallocz(new_l1_size2);
+    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
      memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));

Unrelated note: using qemu_memalign() here would reduce the copying for cache=none.

Good point. I guess there are more places where we could use qemu_memalign, so I would prefer to change them all in another patch.

Kevin

#3 lore raw
Quoted 8 lines · Kevin Wolf
Quoted 3 lines

Unrelated note: using qemu_memalign() here would reduce the copying for cache=none.

Good point. I guess there are more places where we could use qemu_memalign, so I would prefer to change them all in another patch.

Definitely.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
In reply to Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Kevin Wolf

Contrary to what one could expect, the size of L1 tables is not cluster aligned. So as we're writing whole sectors now instead of single entries, we need to ensure that the L1 table in memory is large enough; otherwise write would access memory after the end of the L1 table.

Quoted 9 lines

block/qcow2-cluster.c int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) · line 51

Quoted 5 lines
      new_l1_size2 = sizeof(uint64_t) * new_l1_size;
block/qcow2-cluster.c +1 -1
-    new_l1_table = qemu_mallocz(new_l1_size2);
+    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
      memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));

Unrelated note: using qemu_memalign() here would reduce the copying for cache=none.

Another unrelated note, qemu_memalign(and by extension qemu_vmalloc) is NULL happy. And FWIW 487414f1cbd638beb0227c7da71fe7b8a821e155 removed NULL checks for qemu_memalgn and after that new code was added that doesn't check for NULLs either (for instance e3f4e2a4 which has bitten me).

Signature · 1 line
mailto:[email protected]
author
#5 lore raw
Quoted 18 lines · malc
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Kevin Wolf

Contrary to what one could expect, the size of L1 tables is not cluster aligned. So as we're writing whole sectors now instead of single entries, we need to ensure that the L1 table in memory is large enough; otherwise write would access memory after the end of the L1 table.

Quoted 8 lines

block/qcow2-cluster.c int qcow2_grow_l1_table(BlockDriverState *bs, int min_size) · line 51

Quoted 5 lines
      new_l1_size2 = sizeof(uint64_t) * new_l1_size;
block/qcow2-cluster.c +1 -1
-    new_l1_table = qemu_mallocz(new_l1_size2);
+    new_l1_table = qemu_mallocz(align_offset(new_l1_size2, 512));
      memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));

Unrelated note: using qemu_memalign() here would reduce the copying for cache=none.

Quoted 5 lines

Another unrelated note, qemu_memalign(and by extension qemu_vmalloc) is NULL happy. And FWIW 487414f1cbd638beb0227c7da71fe7b8a821e155 removed NULL checks for qemu_memalgn and after that new code was added that doesn't check for NULLs either (for instance e3f4e2a4 which has bitten me).

Good to know that there still is an alternative that isn't broken. Another good reason to switch to qemu_memalign.

Kevin