Skip to contentSkip to conversation

blkio: Respect memory-alignment for bounce buffer allocations

PATCH In review Kevin Wolf 3 participants 3 messages Jan 31, 2024 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 blkio: Respect memory-alignment for bounce buffer allocations

blkio_alloc_mem_region() requires that the requested buffer size is a multiple of the memory-alignment property. If it isn't, the allocation fails with a return value of -EINVAL.

Fix the call in blkio_resize_bounce_pool() to make sure the requested size is properly aligned.

I observed this problem with vhost-vdpa, which requires page aligned memory. As the virtio-blk device behind it still had 512 byte blocks, we got bs->bl.request_alignment = 512, but actually any request that needed a bounce buffer and was not aligned to 4k would fail without this fix.

---
 block/blkio.c | 3 +++
 1 file changed, 3 insertions(+)
block/blkio.c +3 -0
index 0a0a6c0f5f..b989617608 100644
@@ -89,6 +89,9 @@ static int blkio_resize_bounce_pool(BDRVBlkioState *s, int64_t bytes)
     /* Pad size to reduce frequency of resize calls */
     bytes += 128 * 1024;
 
+    /* Align the pool size to avoid blkio_alloc_mem_region() failure */
+    bytes = QEMU_ALIGN_UP(bytes, s->mem_region_alignment);
+
     WITH_QEMU_LOCK_GUARD(&s->blkio_lock) {
         int ret;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Kevin Wolf

blkio_alloc_mem_region() requires that the requested buffer size is a multiple of the memory-alignment property. If it isn't, the allocation fails with a return value of -EINVAL.

Fix the call in blkio_resize_bounce_pool() to make sure the requested size is properly aligned.

I observed this problem with vhost-vdpa, which requires page aligned memory. As the virtio-blk device behind it still had 512 byte blocks, we got bs->bl.request_alignment = 512, but actually any request that needed a bounce buffer and was not aligned to 4k would fail without this fix.

Quoted 5 lines

---
block/blkio.c | 3 +++

1 file changed, 3 insertions(+)

Thanks for fixinig this!

Quoted 18 lines
block/blkio.c +3 -0
index 0a0a6c0f5f..b989617608 100644
@@ -89,6 +89,9 @@ static int blkio_resize_bounce_pool(BDRVBlkioState *s, int64_t bytes)
    /* Pad size to reduce frequency of resize calls */
    bytes += 128 * 1024;
 
+    /* Align the pool size to avoid blkio_alloc_mem_region() failure */
+    bytes = QEMU_ALIGN_UP(bytes, s->mem_region_alignment);
+
    WITH_QEMU_LOCK_GUARD(&s->blkio_lock) {
        int ret;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Kevin Wolf
Quoted 12 lines · Kevin Wolf

blkio_alloc_mem_region() requires that the requested buffer size is a multiple of the memory-alignment property. If it isn't, the allocation fails with a return value of -EINVAL.

Fix the call in blkio_resize_bounce_pool() to make sure the requested size is properly aligned.

I observed this problem with vhost-vdpa, which requires page aligned memory. As the virtio-blk device behind it still had 512 byte blocks, we got bs->bl.request_alignment = 512, but actually any request that needed a bounce buffer and was not aligned to 4k would fail without this fix.

Quoted 5 lines
---
 block/blkio.c | 3 +++
 1 file changed, 3 insertions(+)