Skip to contentSkip to conversation

xen_disk: split discard input to match internal representation

PATCH In review Olaf Hering 5 participants 8 messages Nov 23, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 xen_disk: split discard input to match internal representation

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Fixes f313520 ("xen_disk: add discard support")

---
v3:
turn tab into spaces to fix checkpatch warning
v2:
adjust overflow check
add Fixes revspec because the initial commit also failed to convert u64 to s32 adjust summary

 hw/block/xen_disk.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)
hw/block/xen_disk.c +36 -6
index 3a7dc19..456a2d5 100644
@@ -660,6 +660,38 @@ static void qemu_aio_complete(void *opaque, int ret)
     qemu_bh_schedule(ioreq->blkdev->bh);
 }
 
+static bool blk_split_discard(struct ioreq *ioreq, blkif_sector_t sector_number,
+                              uint64_t nr_sectors)
+{
+    struct XenBlkDev *blkdev = ioreq->blkdev;
+    int64_t byte_offset;
+    int byte_chunk;
+    uint64_t byte_remaining, limit;
+    uint64_t sec_start = sector_number;
+    uint64_t sec_count = nr_sectors;
+
+    /* Wrap around, or overflowing byte limit? */
+    if (sec_start + sec_count < sec_count ||
+        sec_start + sec_count > INT64_MAX >> BDRV_SECTOR_BITS) {
+        return false;
+    }
+
+    limit = BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS;
+    byte_offset = sec_start << BDRV_SECTOR_BITS;
+    byte_remaining = sec_count << BDRV_SECTOR_BITS;
+
+    do {
+        byte_chunk = byte_remaining > limit ? limit : byte_remaining;
+        ioreq->aio_inflight++;
+        blk_aio_pdiscard(blkdev->blk, byte_offset, byte_chunk,
+                         qemu_aio_complete, ioreq);
+        byte_remaining -= byte_chunk;
+        byte_offset += byte_chunk;
+    } while (byte_remaining > 0);
+
+    return true;
+}
+
 static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
 {
     struct XenBlkDev *blkdev = ioreq->blkdev;
@@ -708,12 +740,10 @@ static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
         break;
     case BLKIF_OP_DISCARD:
     {
-        struct blkif_request_discard *discard_req = (void *)&ioreq->req;
-        ioreq->aio_inflight++;
-        blk_aio_pdiscard(blkdev->blk,
-                         discard_req->sector_number << BDRV_SECTOR_BITS,
-                         discard_req->nr_sectors << BDRV_SECTOR_BITS,
-                         qemu_aio_complete, ioreq);
+        struct blkif_request_discard *req = (void *)&ioreq->req;
+        if (!blk_split_discard(ioreq, req->sector_number, req->nr_sectors)) {
+            goto err;
+        }
         break;
     }
     default:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Olaf Hering

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Quoted 7 lines

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Fixes f313520 ("xen_disk: add discard support")

---

Qualifies as a bug fix, so requesting 2.8 inclusion.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw

Am 23.11.2016 um 12:40 hat Eric Blake geschrieben:

Quoted 14 lines · Olaf Hering
Quoted 13 lines

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Quoted 7 lines
Quoted 4 lines

Fixes f313520 ("xen_disk: add discard support")

---

Qualifies as a bug fix, so requesting 2.8 inclusion.

Stefano, are you going to merge this or should I take a look?

Kevin

#3 lore raw
Quoted 17 lines · Kevin Wolf

Am 23.11.2016 um 12:40 hat Eric Blake geschrieben:

Quoted 16 lines · Olaf Hering
Quoted 15 lines

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Fixes f313520 ("xen_disk: add discard support")

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

---

Qualifies as a bug fix, so requesting 2.8 inclusion.

Stefano, are you going to merge this or should I take a look?

I can merge it.

Cheers,

Stefano

Acked-by on v3 1/1
#6 lore raw
In reply to Olaf Hering
Quoted 10 lines · Olaf Hering

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Quoted 6 lines

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Fixes f313520 ("xen_disk: add discard support")

Signature · 1 line
Anthony PERARD
Reviewed-by on v3 1/1
#7 lore raw
In reply to Olaf Hering
Quoted 10 lines · Olaf Hering

The guest sends discard requests as u64 sector/count pairs, but the block layer operates internally with s64/s32 pairs. The conversion leads to IO errors in the guest, the discard request is not processed.

domU.cfg:
'vdev=xvda, format=qcow2, backendtype=qdisk, target=/x.qcow2' domU:
mkfs.ext4 -F /dev/xvda
Discarding device blocks: failed - Input/output error

Quoted 6 lines

Fix this by splitting the request into chunks of BDRV_REQUEST_MAX_SECTORS. Add input range checking to avoid overflow.

Fixes f313520 ("xen_disk: add discard support")

Quoted 71 lines

v3:
turn tab into spaces to fix checkpatch warning
v2:
adjust overflow check
add Fixes revspec because the initial commit also failed to convert u64 to s32 adjust summary

 hw/block/xen_disk.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)
hw/block/xen_disk.c +36 -6
index 3a7dc19..456a2d5 100644
@@ -660,6 +660,38 @@ static void qemu_aio_complete(void *opaque, int ret)
     qemu_bh_schedule(ioreq->blkdev->bh);
 }
 
+static bool blk_split_discard(struct ioreq *ioreq, blkif_sector_t sector_number,
+                              uint64_t nr_sectors)
+{
+    struct XenBlkDev *blkdev = ioreq->blkdev;
+    int64_t byte_offset;
+    int byte_chunk;
+    uint64_t byte_remaining, limit;
+    uint64_t sec_start = sector_number;
+    uint64_t sec_count = nr_sectors;
+
+    /* Wrap around, or overflowing byte limit? */
+    if (sec_start + sec_count < sec_count ||
+        sec_start + sec_count > INT64_MAX >> BDRV_SECTOR_BITS) {
+        return false;
+    }
+
+    limit = BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS;
+    byte_offset = sec_start << BDRV_SECTOR_BITS;
+    byte_remaining = sec_count << BDRV_SECTOR_BITS;
+
+    do {
+        byte_chunk = byte_remaining > limit ? limit : byte_remaining;
+        ioreq->aio_inflight++;
+        blk_aio_pdiscard(blkdev->blk, byte_offset, byte_chunk,
+                         qemu_aio_complete, ioreq);
+        byte_remaining -= byte_chunk;
+        byte_offset += byte_chunk;
+    } while (byte_remaining > 0);
+
+    return true;
+}
+
 static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
 {
     struct XenBlkDev *blkdev = ioreq->blkdev;
@@ -708,12 +740,10 @@ static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
         break;
     case BLKIF_OP_DISCARD:
     {
-        struct blkif_request_discard *discard_req = (void *)&ioreq->req;
-        ioreq->aio_inflight++;
-        blk_aio_pdiscard(blkdev->blk,
-                         discard_req->sector_number << BDRV_SECTOR_BITS,
-                         discard_req->nr_sectors << BDRV_SECTOR_BITS,
-                         qemu_aio_complete, ioreq);
+        struct blkif_request_discard *req = (void *)&ioreq->req;
+        if (!blk_split_discard(ioreq, req->sector_number, req->nr_sectors)) {
+            goto err;
+        }
         break;
     }
     default: