ublk: fix completion of short READ/WRITE
Subsystems (4)
Ming Lei
maintainercover ublk: fix completion of short READ/WRITE
Hi Jens,
Since commit a8a79eba22dc ("ublk: lift checks out of ublk_{,un}map_io()"), a short result from the ublk server is ignored for WRITE, and for READ on user copy, zero copy, auto buffer registration and shmem zero copy devices: the request is completed in full.
Patch 1 fails READ/WRITE with an invalid result, so that partial completion is safe.
Patch 2 fixes the kublk stripe target to commit the full length.
Patch 3 completes a short READ/WRITE by io->res again and requeues the rest.
Patch 4 fails a short zone append.
Patch 5 adds a selftest covering short write and short read.
Thanks,
Ming
Ming Lei (5):
ublk: fail READ/WRITE if the result from the server is invalid
selftests: ublk: complete a striped request with its full length
ublk: don't complete a short request in full
ublk: fail a short zone append
selftests: ublk: test that a short request isn't completed in full
drivers/block/ublk_drv.c | 43 ++- tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/stripe.c | 5 +- tools/testing/selftests/ublk/test_loop_09.sh | 294 +++++++++++++++++++ 4 files changed, 334 insertions(+), 9 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_loop_09.sh
base-commit: 5f22fb20497f4983094131f00a734362300d22d2
Signature · 1 line
2.56.1
Ming Lei
maintainerPATCH 1/5 ublk: fail READ/WRITE if the result from the server is invalid
__ublk_complete_rq() passes io->res from the server straight to blk_update_request(). If it isn't a multiple of the logical block size, the data moves by io->res bytes but the start sector by io->res >> 9, so the requeued rest is read or written at the wrong place.
Fail a READ or WRITE with -EIO if io->res isn't a multiple of the logical block size or is larger than the request, as is already done for a READ that returns 0. Fail a WRITE that returns 0 too, so that it can't be requeued forever once a short WRITE is completed by io->res again.
The byte count of a READ's data that is copied only in part from the server's buffer has the same problem. Fail the READ if that count isn't a multiple of the logical block size.
--- drivers/block/ublk_drv.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +13 -3
@@ -1577,8 +1577,16 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,blk_status_t res =BLK_STS_OK ;bool requeue; -/* failed read IO if nothing is read */ -if (!io->res &&req_op (req) ==REQ_OP_READ ) +/* +* The result of READ and WRITE is the number of bytes done. A short +* one has to be whole logical blocks, so that a requeued rest starts +* at the right sector. Fail anything else: more than the request, not +* aligned, or nothing done. +*/ +if ((req_op (req) ==REQ_OP_READ ||req_op (req) ==REQ_OP_WRITE ) && + io->res >=0 &&unlikely (io->res !=blk_rq_bytes (req)) && + (!io->res || (unsigned int )io->res >blk_rq_bytes (req) || + !IS_ALIGNED (io->res,queue_logical_block_size (req->q)))) io->res = -EIO ;if (io->res <0 ) {
@@ -1600,7 +1608,9 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,* Re-read simply for this unlikely case. */ if (unlikely (unmapped_bytes < io->res)) { -if (unlikely (!unmapped_bytes)) { +if (unlikely (!unmapped_bytes || + !IS_ALIGNED (unmapped_bytes, +queue_logical_block_size (req->q)))) { res =BLK_STS_IOERR ; goto exit; }
Signature · 1 line
2.56.1
Re: [PATCH 1/5] ublk: fail READ/WRITE if the result from the server is invalid
Quoted 30 lines · Ming Lei
__ublk_complete_rq()passesio->resfrom the server straight toblk_update_request(). If it isn't a multiple of the logical block size, the data moves byio->resbytes but the start sector byio->res>> 9, so the requeued rest is read or written at the wrong place.Fail a READ or WRITE with
-EIOifio->resisn't a multiple of the logical block size or is larger than the request, as is already done for a READ that returns 0. Fail a WRITE that returns 0 too, so that it can't be requeued forever once a short WRITE is completed byio->resagain.The byte count of a READ's data that is copied only in part from the server's buffer has the same problem. Fail the READ if that count isn't a multiple of the logical block size.
Fixes 71f28f3136af ("ublk_drv: add io_uring based userspace block driver")Signed-off-by Ming Lei <[email protected]>--- drivers/block/ublk_drv.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)drivers/block/ublk_drv.c +0
-0@@ -1577,8 +1577,16 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,blk_status_t res =BLK_STS_OK ;bool requeue;
drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, · line 1586
Quoted 10 lines
drivers/block/ublk_drv.c +7
-2-/* failed read IO if nothing is read */ -if (!io->res &&req_op (req) ==REQ_OP_READ ) +/* +* The result of READ and WRITE is the number of bytes done. A short +* one has to be whole logical blocks, so that a requeued rest starts +* at the right sector. Fail anything else: more than the request, not +* aligned, or nothing done. +*/ + if ((req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) &&
Are these the only I/O types that need to be checked? Looks like partial ZONE_APPEND operations are failed in blk_update_request(), but do we still need to validate that io->res <= blk_rq_bytes(req)? How about for DISCARD, WRITE_ZEROES, and DRV_IN (for the zone report)? And what about requests without data (FLUSH, zone management, etc.)?
drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, · line 1587
Quoted 1 line
drivers/block/ublk_drv.c +1
-0+ io->res >= 0 && unlikely(io->res != blk_rq_bytes(req)) &&
Move the unlikely() branch first?
Best,
Caleb
Quoted 19 lines
drivers/block/ublk_drv.c +5
-2+ (!io->res || (unsigned int )io->res >blk_rq_bytes (req) || + !IS_ALIGNED(io->res, queue_logical_block_size(req->q)))) io->res = -EIO;if (io->res <0 ) {@@ -1600,7 +1608,9 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,* Re-read simply for this unlikely case. */ if (unlikely (unmapped_bytes < io->res)) { -if (unlikely (!unmapped_bytes)) { +if (unlikely (!unmapped_bytes || + !IS_ALIGNED (unmapped_bytes, +queue_logical_block_size (req->q)))) { res =BLK_STS_IOERR ; goto exit; } --2.56.1
Ming Lei
maintainerRe: [PATCH 1/5] ublk: fail READ/WRITE if the result from the server is invalid
Quoted 32 lines · Caleb Sander Mateos
Quoted 31 lines · Ming Lei
__ublk_complete_rq()passesio->resfrom the server straight toblk_update_request(). If it isn't a multiple of the logical block size, the data moves byio->resbytes but the start sector byio->res>> 9, so the requeued rest is read or written at the wrong place.Fail a READ or WRITE with
-EIOifio->resisn't a multiple of the logical block size or is larger than the request, as is already done for a READ that returns 0. Fail a WRITE that returns 0 too, so that it can't be requeued forever once a short WRITE is completed byio->resagain.The byte count of a READ's data that is copied only in part from the server's buffer has the same problem. Fail the READ if that count isn't a multiple of the logical block size.
Fixes 71f28f3136af ("ublk_drv: add io_uring based userspace block driver")Signed-off-by Ming Lei <[email protected]>--- drivers/block/ublk_drv.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)drivers/block/ublk_drv.c +0
-0@@ -1577,8 +1577,16 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,blk_status_t res =BLK_STS_OK ;bool requeue;
Quoted 12 lines
drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, · line 1586
Quoted 9 lines
drivers/block/ublk_drv.c +7
-2-/* failed read IO if nothing is read */ -if (!io->res &&req_op (req) ==REQ_OP_READ ) +/* +* The result of READ and WRITE is the number of bytes done. A short +* one has to be whole logical blocks, so that a requeued rest starts +* at the right sector. Fail anything else: more than the request, not +* aligned, or nothing done. +*/ + if ((req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) &&Are these the only I/O types that need to be checked? Looks like partial
ZONE_APPENDoperations are failed inblk_update_request(), but
Patch 4 fails res < blk_rq_bytes(req) for ZONE_APPEND.
Quoted 3 lines
do we still need to validate that
io->res<=blk_rq_bytes(req)? How about for DISCARD,WRITE_ZEROES, andDRV_IN(for the zone report)? And what about requests without data (FLUSH, zone management, etc.)?
Yes, for correctness. Only READ and WRITE pass io->res to blk_update_request(). Every other op goes to exit: and ublk_end_request(req, res), where only the sign of io->res matters, so an oversized or unaligned value can't misplace anything.
Quoted 4 lines
drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, · line 1587
Quoted 2 lines
drivers/block/ublk_drv.c +1
-0+ io->res >= 0 && unlikely(io->res != blk_rq_bytes(req)) &&Move the
unlikely()branch first?
Agree, will do.
Thanks,
Ming
Ming Lei
maintainerPATCH 2/5 selftests: ublk: complete a striped request with its full length
In reply to Ming Leiublk_stripe_io_done() commits the result of the first CQE for the whole request, though the request may span several stripes. The driver is about to act on a short result for WRITE and zero copy again, so each such request would be requeued and its rest redone.
Keep only errors in io->result, and commit the request length if all stripes succeed.
The removed check of UBLK_U_IO_UNREGISTER_IO_BUF only let the byte count of a READ/WRITE CQE into io->result. It did nothing for the unregister
itself: its success is zero, which never changes io->result, and its failure is still recorded.
--- tools/testing/selftests/ublk/stripe.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/ublk/stripe.c +2 -3
@@ -240,11 +240,10 @@ static void ublk_stripe_io_done(struct ublk_thread *t, struct ublk_queue *q,struct ublk_io *io =ublk_get_io (q, tag);int res = cqe->res; -if (res <0 || op !=ublk_cmd_op_nr (UBLK_U_IO_UNREGISTER_IO_BUF )) { +if (res <0 ) {if (!io->result) io->result = res; -if (res <0 ) -ublk_err ("%s: io failure %d tag %u\n" , __func__, res, tag); +ublk_err ("%s: io failure %d tag %u\n" , __func__, res, tag); }/* buffer register op is IOSQE_CQE_SKIP_SUCCESS */
Signature · 1 line
2.56.1
Re: [PATCH 2/5] selftests: ublk: complete a striped request with its full length
Quoted 9 lines · Ming Lei
ublk_stripe_io_done()commits the result of the first CQE for the whole request, though the request may span several stripes. The driver is about to act on a short result for WRITE and zero copy again, so each such request would be requeued and its rest redone.Keep only errors in
io->result, and commit the request length if all stripes succeed.
Quoted 6 lines
The removed check of
UBLK_U_IO_UNREGISTER_IO_BUFonly let the byte count of a READ/WRITE CQE intoio->result. It did nothing for the unregister
itself: its success is zero, which never changesio->result, and its failure is still recorded.Signed-off-by Ming Lei <[email protected]>
Should this be reordered before patch 1/5 ("ublk: fail READ/WRITE if the result from the server is invalid")?
Otherwise,
Quoted 25 lines
--- tools/testing/selftests/ublk/stripe.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)tools/testing/selftests/ublk/stripe.c +2
-4@@ -240,11 +240,10 @@ static void ublk_stripe_io_done(struct ublk_thread *t, struct ublk_queue *q,struct ublk_io *io =ublk_get_io (q, tag);int res = cqe->res; -if (res <0 || op !=ublk_cmd_op_nr (UBLK_U_IO_UNREGISTER_IO_BUF )) { +if (res <0 ) {if (!io->result) io->result = res; -if (res <0 ) -ublk_err ("%s: io failure %d tag %u\n" , __func__, res, tag); +ublk_err ("%s: io failure %d tag %u\n" , __func__, res, tag); }/* buffer register op is IOSQE_CQE_SKIP_SUCCESS */ --2.56.1
Ming Lei
maintainerPATCH 3/5 ublk: don't complete a short request in full
In reply to Ming LeiSince commit a8a79eba22dc ("ublk: lift checks out of ublk_{,un}map_io()"), __ublk_complete_rq() ends the request in full and ignores io->res when there is nothing to copy back: every WRITE, and every request on a user copy, zero copy or auto buffer registration device. A short write or read from the server is reported as complete. Shmem zero copy requests have had the same problem since commit 4d4a512a1f87 ("ublk: add PFN-based buffer matching in I/O path").
Skip only the copy step, and complete READ and WRITE by io->res through blk_update_request() again, requeueing the rest. Other requests are still ended directly, including report zones, whose reply can be short.
--- drivers/block/ublk_drv.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +17 -3
@@ -1569,7 +1569,6 @@ static void ublk_end_request(struct request *req, blk_status_t error)local_bh_enable (); } -/* todo: handle partial completion */ static inline void __ublk_complete_rq (struct request *req,struct ublk_io *io,bool need_map,struct io_comp_batch *iob) {
@@ -1594,10 +1593,24 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, goto exit; } -/* shmem zero copy: no data to unmap, pages already shared */ +/* +* FLUSH, DISCARD, WRITE_ZEROES and zone management requests usually +* don't return a byte count, so end them directly. So is report zones +* (DRV_IN): its reply can be shorter than the buffer, and a requeued +* rest would rerun the report. So is zone append: a requeued rest would +* be appended again at a moved write pointer. +*/ +if (req_op (req) !=REQ_OP_READ &&req_op (req) !=REQ_OP_WRITE ) + goto exit; + +/* +* Nothing to copy for WRITE, user copy, zero copy, auto buffer +* registration or shmem zero copy, but io->res still tells how many +* bytes are done. +*/ if (!need_map || !ublk_need_unmap_req (req) ||ublk_iod_is_shmem_zc (req->mq_hctx->driver_data, req->tag)) - goto exit; + goto update;/* for READ request, writing data in iod->addr to rq buffers */ unmapped_bytes =ublk_unmap_io (req, io);
@@ -1618,6 +1631,7 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, io->res = unmapped_bytes; } +update:/* * Run bio->bi_end_io() with softirqs disabled. If the final fput * happens off this path, then that will prevent ublk's blkdev_release()
Signature · 1 line
2.56.1
Ming Lei
maintainerPATCH 4/5 ublk: fail a short zone append
In reply to Ming LeiA zone append that the server completes short is ended in full, so the caller is told that the whole bio was written while its tail never was. Requeueing the rest, as is done for a short write, doesn't work either:
the rest is sent as a new zone append and written again at the moved write pointer, and blk_update_request() then fails the bio anyway.
Fail a short zone append with BLK_STS_IOERR.
--- drivers/block/ublk_drv.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +10 -3
@@ -1597,11 +1597,18 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,* FLUSH, DISCARD, WRITE_ZEROES and zone management requests usually * don't return a byte count, so end them directly. So is report zones * (DRV_IN): its reply can be shorter than the buffer, and a requeued -* rest would rerun the report. So is zone append: a requeued rest would -* be appended again at a moved write pointer. +* rest would rerun the report. +* +* A zone append can't be completed in part: a requeued rest would be +* appended again at a moved write pointer. Fail a short one, as +* blk_update_request() does for a partial zone append. */ -if (req_op (req) !=REQ_OP_READ &&req_op (req) !=REQ_OP_WRITE ) +if (req_op (req) !=REQ_OP_READ &&req_op (req) !=REQ_OP_WRITE ) { +if (req_op (req) ==REQ_OP_ZONE_APPEND && + (unsigned int )io->res <blk_rq_bytes (req)) + res =BLK_STS_IOERR ; goto exit; + }/* * Nothing to copy for WRITE, user copy, zero copy, auto buffer
Signature · 1 line
2.56.1
Ming Lei
maintainerPATCH 5/5 selftests: ublk: test that a short request isn't completed in full
In reply to Ming LeiPut the loop target's backing file on a tmpfs with room for 16 blocks and one page, and write 17 blocks with O_DIRECT. The server completes the last write short, with one page. Check that the write stops with ENOSPC at block 16, which means the kernel requeued the rest and the server failed it, and that every block reported as written reads back.
Then cut the backing file to one page and read the first block. The server completes that read short and the rest reads nothing, so the read must fail.
Run it with dd in the copy, user copy, zero copy, auto buffer registration and batch modes, and with fio in shmem zero copy mode, using a hugetlbfs buffer that kublk has registered.
--- tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/test_loop_09.sh | 294 +++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100755 tools/testing/selftests/ublk/test_loop_09.sh
tools/testing/selftests/ublk/Makefile +1 -0
@@ -38,6 +38,7 @@ TEST_PROGS += test_loop_05.sh TEST_PROGS += test_loop_06.sh TEST_PROGS += test_loop_07.sh TEST_PROGS += test_loop_08.sh +TEST_PROGS += test_loop_09.sh TEST_PROGS += test_integrity_01.sh TEST_PROGS += test_integrity_02.sh
tools/testing/selftests/ublk/test_loop_09.sh new file +294 -0
294 lines not on this page · raw message
Signature · 1 line
2.56.1