public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>, linux-block@vger.kernel.org
Cc: Caleb Sander Mateos <csander@purestorage.com>,
	Uday Shankar <ushankar@purestorage.com>,
	Ming Lei <ming.lei@redhat.com>, Keith Busch <kbusch@kernel.org>
Subject: [PATCH 01/13] ublk: delay aborting zc request until io_uring returns the buffer
Date: Mon,  7 Apr 2025 21:15:12 +0800	[thread overview]
Message-ID: <20250407131526.1927073-2-ming.lei@redhat.com> (raw)
In-Reply-To: <20250407131526.1927073-1-ming.lei@redhat.com>

When one request buffer is leased to io_uring via
io_buffer_register_bvec(), io_uring guarantees that the buffer will
be returned. However ublk aborts request in case that io_uring context
is exiting, then ublk_io_release() may observe freed request, and
kernel panic is triggered.

Fix the issue by delaying to abort zc request until io_uring returns
the buffer back.

Cc: Keith Busch <kbusch@kernel.org>
Fixes: 1f6540e2aabb ("ublk: zc register/unregister bvec")
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/ublk_drv.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 2fd05c1bd30b..76caec28e5ac 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -140,6 +140,17 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+
+/*
+ * Set when this request buffer is leased to ublk server, and cleared when
+ * the buffer is returned back.
+ *
+ * If this flag is set, this request can't be aborted until buffer is
+ * returned back from io_uring since io_uring is guaranteed to release the
+ * buffer.
+ */
+#define UBLK_IO_FLAG_BUF_LEASED   0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED	0x80000000
 
@@ -1550,7 +1561,8 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
 			rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
 			if (rq && blk_mq_request_started(rq)) {
 				io->flags |= UBLK_IO_FLAG_ABORTED;
-				__ublk_fail_req(ubq, io, rq);
+				if (!(io->flags & UBLK_IO_FLAG_BUF_LEASED))
+					__ublk_fail_req(ubq, io, rq);
 			}
 		}
 	}
@@ -1874,8 +1886,18 @@ static void ublk_io_release(void *priv)
 {
 	struct request *rq = priv;
 	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+	struct ublk_io *io = &ubq->ios[rq->tag];
 
-	ublk_put_req_ref(ubq, rq);
+	io->flags &= ~UBLK_IO_FLAG_BUF_LEASED;
+	/*
+	 * request has been aborted, and the queue context is exiting,
+	 * and ublk server can't be relied for completing this IO cmd,
+	 * so force to complete it
+	 */
+	if (unlikely(io->flags & UBLK_IO_FLAG_ABORTED))
+		__ublk_complete_rq(rq);
+	else
+		ublk_put_req_ref(ubq, rq);
 }
 
 static int ublk_register_io_buf(struct io_uring_cmd *cmd,
@@ -1958,7 +1980,10 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
 	ret = -EINVAL;
 	switch (_IOC_NR(cmd_op)) {
 	case UBLK_IO_REGISTER_IO_BUF:
-		return ublk_register_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
+		ret = ublk_register_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
+		if (!ret)
+			io->flags |= UBLK_IO_FLAG_BUF_LEASED;
+		return ret;
 	case UBLK_IO_UNREGISTER_IO_BUF:
 		return ublk_unregister_io_buf(cmd, ub_cmd->addr, issue_flags);
 	case UBLK_IO_FETCH_REQ:
-- 
2.47.0


  reply	other threads:[~2025-04-07 13:15 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-07 13:15 [PATCH 00/13] ublk: one driver bug fix and selftest change Ming Lei
2025-04-07 13:15 ` Ming Lei [this message]
2025-04-07 15:02   ` [PATCH 01/13] ublk: delay aborting zc request until io_uring returns the buffer Caleb Sander Mateos
2025-04-08  2:18     ` Ming Lei
2025-04-07 13:15 ` [PATCH 02/13] selftests: ublk: fix ublk_find_tgt() Ming Lei
2025-04-08  6:05   ` Johannes Thumshirn
2025-04-12  1:25     ` Ming Lei
2025-04-07 13:15 ` [PATCH 03/13] selftests: ublk: add io_uring uapi header Ming Lei
2025-04-08  6:08   ` Johannes Thumshirn
2025-04-07 13:15 ` [PATCH 04/13] selftests: ublk: cleanup backfile automatically Ming Lei
2025-04-07 13:15 ` [PATCH 05/13] selftests: ublk: make sure _add_ublk_dev can return in sub-shell Ming Lei
2025-04-07 13:15 ` [PATCH 06/13] selftests: ublk: run stress tests in parallel Ming Lei
2025-04-07 13:15 ` [PATCH 07/13] selftests: ublk: add two stress tests for zero copy feature Ming Lei
2025-04-07 13:15 ` [PATCH 08/13] selftests: ublk: setup ring with IORING_SETUP_SINGLE_ISSUER/IORING_SETUP_DEFER_TASKRUN Ming Lei
2025-04-07 13:15 ` [PATCH 09/13] selftests: ublk: set queue pthread's cpu affinity Ming Lei
2025-04-07 13:15 ` [PATCH 10/13] selftests: ublk: increase max nr_queues and queue depth Ming Lei
2025-04-07 13:15 ` [PATCH 11/13] selftests: ublk: support target specific command line Ming Lei
2025-04-07 13:15 ` [PATCH 12/13] selftests: ublk: support user recovery Ming Lei
2025-04-07 13:15 ` [PATCH 13/13] selftests: ublk: add test_stress_05.sh Ming Lei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250407131526.1927073-2-ming.lei@redhat.com \
    --to=ming.lei@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=csander@purestorage.com \
    --cc=kbusch@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=ushankar@purestorage.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox