Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v3 2/5] ublk: fail READ/WRITE if the result from the server is invalid
       [not found] <20261202072705.1288588-1-tom.leiming@gmail.com>
@ 2026-12-02  7:27 ` Ming Lei
  2026-12-02  7:27 ` [PATCH v3 3/5] ublk: don't complete a short request in full Ming Lei
  1 sibling, 0 replies; 2+ messages in thread
From: Ming Lei @ 2026-12-02  7:27 UTC (permalink / raw)
  To: Jens Axboe, linux-block
  Cc: Ming Lei, Caleb Sander Mateos, Shuah Khan, linux-kselftest,
	stable, Utkarsh Singh

__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.

Fixes: 71f28f3136af ("ublk_drv: add io_uring based userspace block driver")
Cc: stable@vger.kernel.org
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>
Tested-by: Utkarsh Singh <utsingh@redhat.com>
---
 drivers/block/ublk_drv.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 0699f22714fd..1a0cbaf5690a 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -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 (unlikely(io->res != blk_rq_bytes(req)) && io->res >= 0 &&
+	    (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) &&
+	    (!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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH v3 3/5] ublk: don't complete a short request in full
       [not found] <20261202072705.1288588-1-tom.leiming@gmail.com>
  2026-12-02  7:27 ` [PATCH v3 2/5] ublk: fail READ/WRITE if the result from the server is invalid Ming Lei
@ 2026-12-02  7:27 ` Ming Lei
  1 sibling, 0 replies; 2+ messages in thread
From: Ming Lei @ 2026-12-02  7:27 UTC (permalink / raw)
  To: Jens Axboe, linux-block
  Cc: Ming Lei, Caleb Sander Mateos, Shuah Khan, linux-kselftest,
	stable, Utkarsh Singh

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 <tom.leiming@gmail.com>
Reviewed-by: Caleb Sander Mateos <csander@purestorage.com>
Tested-by: Utkarsh Singh <utsingh@redhat.com>
---
 drivers/block/ublk_drv.c | 52 ++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 1a0cbaf5690a..61da50dcc298 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -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,28 +1593,45 @@ 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 */
-	if (!need_map || !ublk_need_unmap_req(req) ||
-	    ublk_iod_is_shmem_zc(req->mq_hctx->driver_data, req->tag))
+	/*
+	 * 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 (req_op(req) != REQ_OP_READ && req_op(req) != REQ_OP_WRITE)
 		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.
+	 * Only a READ on a device without user copy, zero copy, auto buffer
+	 * registration or shmem zero copy has data to copy back. Either way
+	 * io->res tells how many bytes are done.
 	 */
-	if (unlikely(unmapped_bytes < io->res)) {
-		if (unlikely(!unmapped_bytes ||
-			     !IS_ALIGNED(unmapped_bytes,
-					 queue_logical_block_size(req->q)))) {
-			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 */
+		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 (unlikely(!unmapped_bytes ||
+				     !IS_ALIGNED(unmapped_bytes,
+						 queue_logical_block_size(req->q)))) {
+				res = BLK_STS_IOERR;
+				goto exit;
+			}
+
+			io->res = unmapped_bytes;
+		}
 	}
 
 	/*
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  7:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261202072705.1288588-1-tom.leiming@gmail.com>
2026-12-02  7:27 ` [PATCH v3 2/5] ublk: fail READ/WRITE if the result from the server is invalid Ming Lei
2026-12-02  7:27 ` [PATCH v3 3/5] ublk: don't complete a short request in full Ming Lei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox