Since 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. Fixes: a8a79eba22dc ("ublk: lift checks out of ublk_{,un}map_io()") Fixes: 4d4a512a1f87 ("ublk: add PFN-based buffer matching in I/O path") Cc: stable@vger.kernel.org Signed-off-by: Ming Lei Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 55 +++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 10dd20e7a3de..e38293b23b0f 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -1586,11 +1586,9 @@ 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) { - unsigned int unmapped_bytes; blk_status_t res = BLK_STS_OK; bool requeue; @@ -1599,36 +1597,53 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, goto exit; } + /* + * Only READ and WRITE are completed by the byte count in io->res. End + * any other request directly: + * + * - FLUSH, DISCARD, WRITE_ZEROES and zone management usually don't + * return a byte count + * - the reply of report zones (DRV_IN) can be shorter than the buffer, + * and a requeued rest would rerun the report + * - a requeued rest of zone append would be appended again at a moved + * write pointer + */ + if (!ublk_req_is_rw(req)) + goto exit; + /* * The result of READ and WRITE is the number of bytes done. Fail an * invalid one: more than the request, not aligned, or nothing done. */ - if (unlikely(io->res != blk_rq_bytes(req)) && ublk_req_is_rw(req) && + if (unlikely(io->res != blk_rq_bytes(req)) && !ublk_short_rw_valid(req, io->res)) { res = BLK_STS_IOERR; goto exit; } - /* shmem zero copy: no data to unmap, pages already shared */ - if (!need_map || !ublk_need_unmap_req(req) || - ublk_iod_is_shmem_zc(req->mq_hctx->driver_data, req->tag)) - goto exit; - - /* for READ request, writing data in iod->addr to rq buffers */ - unmapped_bytes = ublk_unmap_io(req, io); - /* - * Extremely impossible since we got data filled in just before - * - * Re-read simply for this unlikely case. + * Data is copied back only for a READ, and not on a user copy, zero + * copy or auto buffer registration device, nor for a shmem zero copy + * request. Either way io->res tells how many bytes are done. */ - if (unlikely(unmapped_bytes < io->res)) { - if (!ublk_short_rw_valid(req, unmapped_bytes)) { - res = BLK_STS_IOERR; - goto exit; - } + if (need_map && ublk_need_unmap_req(req) && + !ublk_iod_is_shmem_zc(req->mq_hctx->driver_data, req->tag)) { + /* for READ request, writing data in iod->addr to rq buffers */ + unsigned int unmapped_bytes = ublk_unmap_io(req, io); - io->res = unmapped_bytes; + /* + * Extremely impossible since we got data filled in just before + * + * Re-read simply for this unlikely case. + */ + if (unlikely(unmapped_bytes < io->res)) { + if (!ublk_short_rw_valid(req, unmapped_bytes)) { + res = BLK_STS_IOERR; + goto exit; + } + + io->res = unmapped_bytes; + } } /* -- 2.56.1