* [PATCH] Use qemu_memalign instead of qemu_malloc
@ 2008-06-24 18:49 Anthony Liguori
2008-06-25 9:19 ` Kevin Wolf
0 siblings, 1 reply; 11+ messages in thread
From: Anthony Liguori @ 2008-06-24 18:49 UTC (permalink / raw)
To: kvm; +Cc: Avi Kivity, Anthony Liguori
I guess the main block code is not as defensive as I thought it was. This patch
uses qemu_memalign to allocate the buffers for IO so that you don't get errors
when using O_DIRECT.
It applies on top of my previous patch to introduce copies in virtio-blk.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
diff --git a/qemu/hw/virtio-blk.c b/qemu/hw/virtio-blk.c
index 2ea5669..669e55f 100644
--- a/qemu/hw/virtio-blk.c
+++ b/qemu/hw/virtio-blk.c
@@ -174,7 +174,7 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
for (i = 1; i < req->elem.out_num; i++)
req->size += req->elem.out_sg[i].iov_len;
- req->buffer = qemu_malloc(req->size);
+ req->buffer = qemu_memalign(512, req->size);
if (req->buffer == NULL) {
qemu_free(req);
break;
@@ -203,7 +203,7 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
for (i = 0; i < req->elem.in_num - 1; i++)
req->size += req->elem.in_sg[i].iov_len;
- req->buffer = qemu_malloc(req->size);
+ req->buffer = qemu_memalign(512, req->size);
if (req->buffer == NULL) {
qemu_free(req);
break;
^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-24 18:49 [PATCH] Use qemu_memalign instead of qemu_malloc Anthony Liguori
@ 2008-06-25 9:19 ` Kevin Wolf
2008-06-25 13:48 ` Anthony Liguori
0 siblings, 1 reply; 11+ messages in thread
From: Kevin Wolf @ 2008-06-25 9:19 UTC (permalink / raw)
To: Anthony Liguori; +Cc: kvm
Anthony Liguori schrieb:
> I guess the main block code is not as defensive as I thought it was. This patch
> uses qemu_memalign to allocate the buffers for IO so that you don't get errors
> when using O_DIRECT.
Actually, the block code should be able to deal with unaligned buffers
since qemu rev. 4599. This change seems to be present in current KVM.
Can you tell exactly which operation failed?
But apart from that, qemu_memalign is the right thing to do, because
copying from/into an aligned buffer in the block code costs performance
(don't know how much, though).
Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 9:19 ` Kevin Wolf
@ 2008-06-25 13:48 ` Anthony Liguori
2008-06-25 14:15 ` Kevin Wolf
0 siblings, 1 reply; 11+ messages in thread
From: Anthony Liguori @ 2008-06-25 13:48 UTC (permalink / raw)
To: Kevin Wolf; +Cc: kvm
Kevin Wolf wrote:
> Anthony Liguori schrieb:
>
>> I guess the main block code is not as defensive as I thought it was. This patch
>> uses qemu_memalign to allocate the buffers for IO so that you don't get errors
>> when using O_DIRECT.
>>
>
> Actually, the block code should be able to deal with unaligned buffers
> since qemu rev. 4599. This change seems to be present in current KVM.
>
That was what I thought at first too.
> Can you tell exactly which operation failed?
>
The aio requests fail with -22 (EINVAL).
> But apart from that, qemu_memalign is the right thing to do, because
> copying from/into an aligned buffer in the block code costs performance
> (don't know how much, though).
>
Regards,
Anthony Liguori
> Kevin
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 13:48 ` Anthony Liguori
@ 2008-06-25 14:15 ` Kevin Wolf
2008-06-25 14:19 ` Anthony Liguori
2008-06-25 14:55 ` Laurent Vivier
0 siblings, 2 replies; 11+ messages in thread
From: Kevin Wolf @ 2008-06-25 14:15 UTC (permalink / raw)
To: Anthony Liguori; +Cc: kvm
Anthony Liguori schrieb:
> Kevin Wolf wrote:
>> Anthony Liguori schrieb:
>>
>>> I guess the main block code is not as defensive as I thought it was.
>>> This patch
>>> uses qemu_memalign to allocate the buffers for IO so that you don't
>>> get errors
>>> when using O_DIRECT.
>>>
>>
>> Actually, the block code should be able to deal with unaligned buffers
>> since qemu rev. 4599. This change seems to be present in current KVM.
>>
>
> That was what I thought at first too.
>
>> Can you tell exactly which operation failed?
>
> The aio requests fail with -22 (EINVAL).
Yes, if it fails, the EINVAL is no surprise. I meant what code path it
was using. Obviously we missed something in our patch and I'd like to
fix that. Did the error occur on raw images or something like qcow2?
Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 14:15 ` Kevin Wolf
@ 2008-06-25 14:19 ` Anthony Liguori
2008-06-25 14:29 ` Kevin Wolf
2008-06-25 14:55 ` Laurent Vivier
1 sibling, 1 reply; 11+ messages in thread
From: Anthony Liguori @ 2008-06-25 14:19 UTC (permalink / raw)
To: Kevin Wolf; +Cc: kvm
Kevin Wolf wrote:
> Anthony Liguori schrieb:
>
>> Kevin Wolf wrote:
>>
>>> Anthony Liguori schrieb:
>>>
>>>
>>>> I guess the main block code is not as defensive as I thought it was.
>>>> This patch
>>>> uses qemu_memalign to allocate the buffers for IO so that you don't
>>>> get errors
>>>> when using O_DIRECT.
>>>>
>>>>
>>> Actually, the block code should be able to deal with unaligned buffers
>>> since qemu rev. 4599. This change seems to be present in current KVM.
>>>
>>>
>> That was what I thought at first too.
>>
>>
>>> Can you tell exactly which operation failed?
>>>
>> The aio requests fail with -22 (EINVAL).
>>
>
> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
> was using. Obviously we missed something in our patch and I'd like to
> fix that. Did the error occur on raw images or something like qcow2?
>
It's a raw image and the calls are being made via
bdrv_aio_read/bdrv_aio_write. It doesn't occur with a qcow2 but then
cache=off doesn't seem to do what it's supposed to with cache=off (I
believe the underlying backing file is not opened O_DIRECT?).
Regards,
Anthony Liguori
> Kevin
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 14:19 ` Anthony Liguori
@ 2008-06-25 14:29 ` Kevin Wolf
2008-06-25 15:22 ` Anthony Liguori
0 siblings, 1 reply; 11+ messages in thread
From: Kevin Wolf @ 2008-06-25 14:29 UTC (permalink / raw)
To: Anthony Liguori; +Cc: kvm
Anthony Liguori schrieb:
> Kevin Wolf wrote:
>> Anthony Liguori schrieb:
>>
>> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
>> was using. Obviously we missed something in our patch and I'd like to
>> fix that. Did the error occur on raw images or something like qcow2?
>>
>
> It's a raw image and the calls are being made via
> bdrv_aio_read/bdrv_aio_write. It doesn't occur with a qcow2 but then
> cache=off doesn't seem to do what it's supposed to with cache=off (I
> believe the underlying backing file is not opened O_DIRECT?).
This is really strange. In raw_aio_read/write there is a check like this:
if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
// emulate it using raw_pread/write which uses
// s->aligned_buf for the request then
}
For qcow2 I think O_DIRECT actually is in effect. Otherwise it would
have worked even without our patch, and it didn't. And indeed, looking
at the code, it passes flags to bdrv_file_open when it opens the image file.
Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 14:15 ` Kevin Wolf
2008-06-25 14:19 ` Anthony Liguori
@ 2008-06-25 14:55 ` Laurent Vivier
2008-06-25 15:14 ` Kevin Wolf
1 sibling, 1 reply; 11+ messages in thread
From: Laurent Vivier @ 2008-06-25 14:55 UTC (permalink / raw)
To: Kevin Wolf; +Cc: Anthony Liguori, kvm
Le mercredi 25 juin 2008 à 16:15 +0200, Kevin Wolf a écrit :
> Anthony Liguori schrieb:
> > Kevin Wolf wrote:
> >> Anthony Liguori schrieb:
> >>
> >>> I guess the main block code is not as defensive as I thought it was.
> >>> This patch
> >>> uses qemu_memalign to allocate the buffers for IO so that you don't
> >>> get errors
> >>> when using O_DIRECT.
> >>>
> >>
> >> Actually, the block code should be able to deal with unaligned buffers
> >> since qemu rev. 4599. This change seems to be present in current KVM.
> >>
> >
> > That was what I thought at first too.
> >
> >> Can you tell exactly which operation failed?
> >
> > The aio requests fail with -22 (EINVAL).
>
> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
> was using. Obviously we missed something in our patch and I'd like to
> fix that. Did the error occur on raw images or something like qcow2?
Generally EINVAL with O_DIRECT opened files means there is an alignment
problem with offset, buffer address or size to read (must be multiple of
512).
Regards,
Laurent
--
------------- Laurent.Vivier@bull.net ---------------
"The best way to predict the future is to invent it."
- Alan Kay
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 14:55 ` Laurent Vivier
@ 2008-06-25 15:14 ` Kevin Wolf
0 siblings, 0 replies; 11+ messages in thread
From: Kevin Wolf @ 2008-06-25 15:14 UTC (permalink / raw)
To: Laurent Vivier; +Cc: Anthony Liguori, kvm
Laurent Vivier schrieb:
> Generally EINVAL with O_DIRECT opened files means there is an alignment
> problem with offset, buffer address or size to read (must be multiple of
> 512).
Apparently the qemu_memalign for the buffer helps, so it's the buffer
address in this case.
Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 14:29 ` Kevin Wolf
@ 2008-06-25 15:22 ` Anthony Liguori
2008-06-25 15:29 ` Kevin Wolf
0 siblings, 1 reply; 11+ messages in thread
From: Anthony Liguori @ 2008-06-25 15:22 UTC (permalink / raw)
To: Kevin Wolf; +Cc: kvm
Kevin Wolf wrote:
> Anthony Liguori schrieb:
>
>> Kevin Wolf wrote:
>>
>>> Anthony Liguori schrieb:
>>>
>>> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
>>> was using. Obviously we missed something in our patch and I'd like to
>>> fix that. Did the error occur on raw images or something like qcow2?
>>>
>>>
>> It's a raw image and the calls are being made via
>> bdrv_aio_read/bdrv_aio_write. It doesn't occur with a qcow2 but then
>> cache=off doesn't seem to do what it's supposed to with cache=off (I
>> believe the underlying backing file is not opened O_DIRECT?).
>>
>
> This is really strange. In raw_aio_read/write there is a check like this:
>
> if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
> // emulate it using raw_pread/write which uses
> // s->aligned_buf for the request then
> }
>
Something is goofy then.
> For qcow2 I think O_DIRECT actually is in effect. Otherwise it would
> have worked even without our patch, and it didn't. And indeed, looking
> at the code, it passes flags to bdrv_file_open when it opens the image file.
>
Something's broken then. Maybe -snapshot doesn't pick up the
O_DIRECT'ness? I'll have to check again. I was definitely seeing page
cache behavior with cache=off.
> Kevin
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 15:22 ` Anthony Liguori
@ 2008-06-25 15:29 ` Kevin Wolf
2008-06-25 15:44 ` Anthony Liguori
0 siblings, 1 reply; 11+ messages in thread
From: Kevin Wolf @ 2008-06-25 15:29 UTC (permalink / raw)
To: Anthony Liguori; +Cc: kvm
Anthony Liguori schrieb:
> Kevin Wolf wrote:
>> Anthony Liguori schrieb:
>>
>>> Kevin Wolf wrote:
>>>
>>>> Anthony Liguori schrieb:
>>>>
>>>> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
>>>> was using. Obviously we missed something in our patch and I'd like to
>>>> fix that. Did the error occur on raw images or something like qcow2?
>>>>
>>> It's a raw image and the calls are being made via
>>> bdrv_aio_read/bdrv_aio_write. It doesn't occur with a qcow2 but then
>>> cache=off doesn't seem to do what it's supposed to with cache=off (I
>>> believe the underlying backing file is not opened O_DIRECT?).
>>>
>>
>> This is really strange. In raw_aio_read/write there is a check like this:
>>
>> if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
>> // emulate it using raw_pread/write which uses
>> // s->aligned_buf for the request then
>> }
>>
>
> Something is goofy then.
>
>> For qcow2 I think O_DIRECT actually is in effect. Otherwise it would
>> have worked even without our patch, and it didn't. And indeed, looking
>> at the code, it passes flags to bdrv_file_open when it opens the image
>> file.
>>
>
> Something's broken then. Maybe -snapshot doesn't pick up the
> O_DIRECT'ness? I'll have to check again. I was definitely seeing page
> cache behavior with cache=off.
Right, qemu seems to drop the flags for the backing file when using
BDRV_O_SNAPSHOT (bdrv2_open in block.c opens the file). So O_DIRECT
applies only to new data.
Have you been using -snapshot when you had trouble with the unaligned
buffer, too? I don't think I have tested this one when I made the patch...
Kevin
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH] Use qemu_memalign instead of qemu_malloc
2008-06-25 15:29 ` Kevin Wolf
@ 2008-06-25 15:44 ` Anthony Liguori
0 siblings, 0 replies; 11+ messages in thread
From: Anthony Liguori @ 2008-06-25 15:44 UTC (permalink / raw)
To: Kevin Wolf; +Cc: kvm
Kevin Wolf wrote:
> Anthony Liguori schrieb:
>
>> Kevin Wolf wrote:
>>
>>> Anthony Liguori schrieb:
>>>
>>>
>>>> Kevin Wolf wrote:
>>>>
>>>>
>>>>> Anthony Liguori schrieb:
>>>>>
>>>>> Yes, if it fails, the EINVAL is no surprise. I meant what code path it
>>>>> was using. Obviously we missed something in our patch and I'd like to
>>>>> fix that. Did the error occur on raw images or something like qcow2?
>>>>>
>>>>>
>>>> It's a raw image and the calls are being made via
>>>> bdrv_aio_read/bdrv_aio_write. It doesn't occur with a qcow2 but then
>>>> cache=off doesn't seem to do what it's supposed to with cache=off (I
>>>> believe the underlying backing file is not opened O_DIRECT?).
>>>>
>>>>
>>> This is really strange. In raw_aio_read/write there is a check like this:
>>>
>>> if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
>>> // emulate it using raw_pread/write which uses
>>> // s->aligned_buf for the request then
>>> }
>>>
>>>
>> Something is goofy then.
>>
>>
>>> For qcow2 I think O_DIRECT actually is in effect. Otherwise it would
>>> have worked even without our patch, and it didn't. And indeed, looking
>>> at the code, it passes flags to bdrv_file_open when it opens the image
>>> file.
>>>
>>>
>> Something's broken then. Maybe -snapshot doesn't pick up the
>> O_DIRECT'ness? I'll have to check again. I was definitely seeing page
>> cache behavior with cache=off.
>>
>
> Right, qemu seems to drop the flags for the backing file when using
> BDRV_O_SNAPSHOT (bdrv2_open in block.c opens the file). So O_DIRECT
> applies only to new data.
>
> Have you been using -snapshot when you had trouble with the unaligned
> buffer, too? I don't think I have tested this one when I made the patch...
>
Nope. I was using a raw image. Actually, an LVM partition.
Regards,
Anthony Liguori
> Kevin
>
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2008-06-25 15:44 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-24 18:49 [PATCH] Use qemu_memalign instead of qemu_malloc Anthony Liguori
2008-06-25 9:19 ` Kevin Wolf
2008-06-25 13:48 ` Anthony Liguori
2008-06-25 14:15 ` Kevin Wolf
2008-06-25 14:19 ` Anthony Liguori
2008-06-25 14:29 ` Kevin Wolf
2008-06-25 15:22 ` Anthony Liguori
2008-06-25 15:29 ` Kevin Wolf
2008-06-25 15:44 ` Anthony Liguori
2008-06-25 14:55 ` Laurent Vivier
2008-06-25 15:14 ` Kevin Wolf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox