* [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
@ 2009-07-07 16:09 Kevin Wolf
2009-07-07 16:26 ` Avi Kivity
0 siblings, 1 reply; 6+ messages in thread
From: Kevin Wolf @ 2009-07-07 16:09 UTC (permalink / raw)
To: qemu-devel; +Cc: 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.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/qcow2-cluster.c | 2 +-
block/qcow2-refcount.c | 2 +-
block/qcow2.c | 3 ++-
3 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index d349655..057dac5 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -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) */
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index d42c6e6..e6c857e 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -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)
diff --git a/block/qcow2.c b/block/qcow2.c
index 20c114b..607329b 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -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;
--
1.6.0.6
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
2009-07-07 16:09 [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation Kevin Wolf
@ 2009-07-07 16:26 ` Avi Kivity
2009-07-07 16:32 ` Kevin Wolf
2009-07-07 18:40 ` malc
0 siblings, 2 replies; 6+ messages in thread
From: Avi Kivity @ 2009-07-07 16:26 UTC (permalink / raw)
To: Kevin Wolf; +Cc: qemu-devel
On 07/07/2009 07:09 PM, Kevin Wolf wrote:
> 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;
> - 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.
--
error compiling committee.c: too many arguments to function
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
2009-07-07 16:26 ` Avi Kivity
@ 2009-07-07 16:32 ` Kevin Wolf
2009-07-07 16:41 ` Avi Kivity
2009-07-07 18:40 ` malc
1 sibling, 1 reply; 6+ messages in thread
From: Kevin Wolf @ 2009-07-07 16:32 UTC (permalink / raw)
To: Avi Kivity; +Cc: qemu-devel
Avi Kivity schrieb:
> On 07/07/2009 07:09 PM, Kevin Wolf wrote:
>> 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;
>> - 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
2009-07-07 16:32 ` Kevin Wolf
@ 2009-07-07 16:41 ` Avi Kivity
0 siblings, 0 replies; 6+ messages in thread
From: Avi Kivity @ 2009-07-07 16:41 UTC (permalink / raw)
To: Kevin Wolf; +Cc: qemu-devel
On 07/07/2009 07:32 PM, Kevin Wolf wrote:
>> 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.
--
error compiling committee.c: too many arguments to function
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
2009-07-07 16:26 ` Avi Kivity
2009-07-07 16:32 ` Kevin Wolf
@ 2009-07-07 18:40 ` malc
2009-07-08 7:29 ` Kevin Wolf
1 sibling, 1 reply; 6+ messages in thread
From: malc @ 2009-07-07 18:40 UTC (permalink / raw)
To: Avi Kivity; +Cc: Kevin Wolf, qemu-devel
On Tue, 7 Jul 2009, Avi Kivity wrote:
> On 07/07/2009 07:09 PM, Kevin Wolf wrote:
> > 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;
> > - 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).
--
mailto:av1474@comtv.ru
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation
2009-07-07 18:40 ` malc
@ 2009-07-08 7:29 ` Kevin Wolf
0 siblings, 0 replies; 6+ messages in thread
From: Kevin Wolf @ 2009-07-08 7:29 UTC (permalink / raw)
To: malc; +Cc: Avi Kivity, qemu-devel
malc schrieb:
> On Tue, 7 Jul 2009, Avi Kivity wrote:
>
>> On 07/07/2009 07:09 PM, Kevin Wolf wrote:
>>> 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;
>>> - 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).
Good to know that there still is an alternative that isn't broken.
Another good reason to switch to qemu_memalign.
Kevin
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-07-08 7:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-07 16:09 [Qemu-devel] [PATCH] qcow2: Fix L1 table memory allocation Kevin Wolf
2009-07-07 16:26 ` Avi Kivity
2009-07-07 16:32 ` Kevin Wolf
2009-07-07 16:41 ` Avi Kivity
2009-07-07 18:40 ` malc
2009-07-08 7:29 ` Kevin Wolf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).