Skip to contentSkip to conversation

ublk: setup ublk_io correctly in case of ublk_get_data() failure

PATCH Applied Ming Lei 4 participants 5 messages Jun 24, 2025

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 ublk: setup ublk_io correctly in case of ublk_get_data() failure

If ublk_get_data() fails, -EIOCBQUEUED is returned and the current command becomes ASYNC. And the only reason is that mapping data can't move on, because of no enough pages or pending signal, then the current ublk request has to be requeued.

Once the request need to be requeued, we have to setup ublk_io correctly, including io->cmd and flags, otherwise the request may not be forwarded to ublk server successfully.

---
V2:
- move io->addr assignment into ublk_fill_io_cmd()

 drivers/block/ublk_drv.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
drivers/block/ublk_drv.c +25 -10
index d36f44f5ee80..3566d7c36b8d 100644
@@ -1148,8 +1148,8 @@ static inline void __ublk_complete_rq(struct request *req)
 	blk_mq_end_request(req, res);
 }
 
-static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
-				 int res, unsigned issue_flags)
+static struct io_uring_cmd *__ublk_prep_compl_io_cmd(struct ublk_io *io,
+						     struct request *req)
 {
 	/* read cmd first because req will overwrite it */
 	struct io_uring_cmd *cmd = io->cmd;
@@ -1164,6 +1164,13 @@ static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
 	io->flags &= ~UBLK_IO_FLAG_ACTIVE;
 
 	io->req = req;
+	return cmd;
+}
+
+static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
+				 int res, unsigned issue_flags)
+{
+	struct io_uring_cmd *cmd = __ublk_prep_compl_io_cmd(io, req);
 
 	/* tell ublksrv one io request is coming */
 	io_uring_cmd_done(cmd, res, 0, issue_flags);
@@ -2148,10 +2155,9 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
 	return 0;
 }
 
-static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io)
+static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io,
+			  struct request *req)
 {
-	struct request *req = io->req;
-
 	/*
 	 * We have handled UBLK_IO_NEED_GET_DATA command,
 	 * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
@@ -2178,6 +2184,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
 	u32 cmd_op = cmd->cmd_op;
 	unsigned tag = ub_cmd->tag;
 	int ret = -EINVAL;
+	struct request *req;
 
 	pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
 			__func__, cmd->cmd_op, ub_cmd->q_id, tag,
@@ -2236,11 +2243,19 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
 			goto out;
 		break;
 	case UBLK_IO_NEED_GET_DATA:
-		io->addr = ub_cmd->addr;
-		if (!ublk_get_data(ubq, io))
-			return -EIOCBQUEUED;
-
-		return UBLK_IO_RES_OK;
+		/*
+		 * ublk_get_data() may fail and fallback to requeue, so keep
+		 * uring_cmd active first and prepare for handling new requeued
+		 * request
+		 */
+		req = io->req;
+		ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
+		io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
+		if (likely(ublk_get_data(ubq, io, req))) {
+			__ublk_prep_compl_io_cmd(io, req);
+			return UBLK_IO_RES_OK;
+		}
+		break;
 	default:
 		goto out;
 	}
Signature · 1 line
2.49.0
Tested-by on v2 1/1
#1 lore raw
Quoted 97 lines · Ming Lei

If ublk_get_data() fails, -EIOCBQUEUED is returned and the current command becomes ASYNC. And the only reason is that mapping data can't move on, because of no enough pages or pending signal, then the current ublk request has to be requeued.

Once the request need to be requeued, we have to setup ublk_io correctly, including io->cmd and flags, otherwise the request may not be forwarded to ublk server successfully.

---
V2:
- move io->addr assignment into ublk_fill_io_cmd()

 drivers/block/ublk_drv.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
drivers/block/ublk_drv.c +25 -11
index d36f44f5ee80..3566d7c36b8d 100644
@@ -1148,8 +1148,8 @@ static inline void __ublk_complete_rq(struct request *req)
        blk_mq_end_request(req, res);
 }
 
-static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
-                                int res, unsigned issue_flags)
+static struct io_uring_cmd *__ublk_prep_compl_io_cmd(struct ublk_io *io,
+                                                    struct request *req)
 {
        /* read cmd first because req will overwrite it */
        struct io_uring_cmd *cmd = io->cmd;
@@ -1164,6 +1164,13 @@ static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
        io->flags &= ~UBLK_IO_FLAG_ACTIVE;
 
        io->req = req;
+       return cmd;
+}
+
+static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
+                                int res, unsigned issue_flags)
+{
+       struct io_uring_cmd *cmd = __ublk_prep_compl_io_cmd(io, req);
 
        /* tell ublksrv one io request is coming */
        io_uring_cmd_done(cmd, res, 0, issue_flags);
@@ -2148,10 +2155,9 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
        return 0;
 }
 
-static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io)
+static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io,
+                         struct request *req)
 {
-       struct request *req = io->req;
-
        /*
         * We have handled UBLK_IO_NEED_GET_DATA command,
         * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
@@ -2178,6 +2184,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
        u32 cmd_op = cmd->cmd_op;
        unsigned tag = ub_cmd->tag;
        int ret = -EINVAL;
+       struct request *req;
 
        pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
                        __func__, cmd->cmd_op, ub_cmd->q_id, tag,
@@ -2236,11 +2243,19 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                        goto out;
                break;
        case UBLK_IO_NEED_GET_DATA:
-               io->addr = ub_cmd->addr;
-               if (!ublk_get_data(ubq, io))
-                       return -EIOCBQUEUED;
-
-               return UBLK_IO_RES_OK;
+               /*
+                * ublk_get_data() may fail and fallback to requeue, so keep
+                * uring_cmd active first and prepare for handling new requeued
+                * request
+                */
+               req = io->req;
+               ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
+               io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
+               if (likely(ublk_get_data(ubq, io, req))) {
+                       __ublk_prep_compl_io_cmd(io, req);
+                       return UBLK_IO_RES_OK;
+               }
+               break;
        default:
                goto out;
        }
--
Quoted 2 lines

2.49.0

Hello,Ming

I ran the test 'make test T=generic' 50 times and it didn't hit this issue any more.
your patch V2 looks good.

#2 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

If ublk_get_data() fails, -EIOCBQUEUED is returned and the current command becomes ASYNC. And the only reason is that mapping data can't move on, because of no enough pages or pending signal, then the current ublk request has to be requeued.

Once the request need to be requeued, we have to setup ublk_io correctly, including io->cmd and flags, otherwise the request may not be forwarded to ublk server successfully.

Fixes 9810362a57cb ("ublk: don't call ublk_dispatch_req() for NEED_GET_DATA")

Don't think this is the right Fixes tag; the issue predates this
refactoring. I pointed out this existing issue when we discussed that
patch: https://lore.kernel.org/linux-block/CADUfDZroQ4zHanPjytcEUhn4tQc3BYMPZD2uLOik7jAXvOCjGg@mail.gmail.com/

Looks like the correct commit would be c86019ff75c1 ("ublk_drv: add support for UBLK_IO_NEED_GET_DATA").

Quoted 15 lines

---
V2:
- move io->addr assignment into ublk_fill_io_cmd()

 drivers/block/ublk_drv.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
drivers/block/ublk_drv.c +0 -0
index d36f44f5ee80..3566d7c36b8d 100644

drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req) · line 1152

Quoted 7 lines
drivers/block/ublk_drv.c +2 -2
@@ -1148,8 +1148,8 @@ static inline void __ublk_complete_rq(struct request *req)
        blk_mq_end_request(req, res);
 }
 
-static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
-                                int res, unsigned issue_flags)
+static struct io_uring_cmd *__ublk_prep_compl_io_cmd(struct ublk_io *io,
+                                                    struct request *req)

nit: don't think the underscores are necessary

Quoted 47 lines
drivers/block/ublk_drv.c +10 -8
 {
        /* read cmd first because req will overwrite it */
        struct io_uring_cmd *cmd = io->cmd;
@@ -1164,6 +1164,13 @@ static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
        io->flags &= ~UBLK_IO_FLAG_ACTIVE;
 
        io->req = req;
+       return cmd;
+}
+
+static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
+                                int res, unsigned issue_flags)
+{
+       struct io_uring_cmd *cmd = __ublk_prep_compl_io_cmd(io, req);
 
        /* tell ublksrv one io request is coming */
        io_uring_cmd_done(cmd, res, 0, issue_flags);
@@ -2148,10 +2155,9 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
        return 0;
 }
 
-static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io)
+static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io,
+                         struct request *req)
 {
-       struct request *req = io->req;
-
        /*
         * We have handled UBLK_IO_NEED_GET_DATA command,
         * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
@@ -2178,6 +2184,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
        u32 cmd_op = cmd->cmd_op;
        unsigned tag = ub_cmd->tag;
        int ret = -EINVAL;
+       struct request *req;
 
        pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
                        __func__, cmd->cmd_op, ub_cmd->q_id, tag,
@@ -2236,11 +2243,19 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                        goto out;
                break;
        case UBLK_IO_NEED_GET_DATA:
-               io->addr = ub_cmd->addr;
-               if (!ublk_get_data(ubq, io))
-                       return -EIOCBQUEUED;
-
-               return UBLK_IO_RES_OK;

drivers/block/ublk_drv.c static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, · line 2255

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+               /*
+                * ublk_get_data() may fail and fallback to requeue, so keep
+                * uring_cmd active first and prepare for handling new requeued
+                * request
+                */
+               req = io->req;
+               ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
+               io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
+               if (likely(ublk_get_data(ubq, io, req))) {
+                       __ublk_prep_compl_io_cmd(io, req);

Isn't __ublk_prep_compl_io_cmd() basically undoing the ublk_fill_io_cmd() and io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV? How about something like this instead?

io->addr = ub_cmd->addr;
if (likely(ublk_get_data(ubq, io)))

        return UBLK_IO_RES_OK;

ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
break;

Best,
Caleb

Quoted 9 lines
drivers/block/ublk_drv.c +3 -1
+                       return UBLK_IO_RES_OK;
+               }
+               break;
        default:
                goto out;
        }
--

2.49.0

#3 lore raw
Quoted 12 lines · Caleb Sander Mateos
Quoted 10 lines · Ming Lei

If ublk_get_data() fails, -EIOCBQUEUED is returned and the current command becomes ASYNC. And the only reason is that mapping data can't move on, because of no enough pages or pending signal, then the current ublk request has to be requeued.

Once the request need to be requeued, we have to setup ublk_io correctly, including io->cmd and flags, otherwise the request may not be forwarded to ublk server successfully.

Quoted 8 lines
Quoted 1 line
Fixes 9810362a57cb ("ublk: don't call ublk_dispatch_req() for NEED_GET_DATA")

Don't think this is the right Fixes tag; the issue predates this
refactoring. I pointed out this existing issue when we discussed that
patch: https://lore.kernel.org/linux-block/CADUfDZroQ4zHanPjytcEUhn4tQc3BYMPZD2uLOik7jAXvOCjGg@mail.gmail.com/

Looks like the correct commit would be c86019ff75c1 ("ublk_drv: add support for UBLK_IO_NEED_GET_DATA").

Ah never mind, I see my commit removed the call to ublk_fill_io_cmd(). I see that's necessary for the -EIOCBQUEUED return to work correctly.

Quoted 108 lines
Quoted 16 lines

---
V2:
- move io->addr assignment into ublk_fill_io_cmd()

 drivers/block/ublk_drv.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
drivers/block/ublk_drv.c +0 -0
index d36f44f5ee80..3566d7c36b8d 100644

drivers/block/ublk_drv.c static inline void __ublk_complete_rq(struct request *req) · line 1152

Quoted 7 lines
drivers/block/ublk_drv.c +2 -2
@@ -1148,8 +1148,8 @@ static inline void __ublk_complete_rq(struct request *req)
        blk_mq_end_request(req, res);
 }
 
-static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
-                                int res, unsigned issue_flags)
+static struct io_uring_cmd *__ublk_prep_compl_io_cmd(struct ublk_io *io,
+                                                    struct request *req)

nit: don't think the underscores are necessary

Quoted 47 lines
drivers/block/ublk_drv.c +10 -8
 {
        /* read cmd first because req will overwrite it */
        struct io_uring_cmd *cmd = io->cmd;
@@ -1164,6 +1164,13 @@ static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
        io->flags &= ~UBLK_IO_FLAG_ACTIVE;
 
        io->req = req;
+       return cmd;
+}
+
+static void ublk_complete_io_cmd(struct ublk_io *io, struct request *req,
+                                int res, unsigned issue_flags)
+{
+       struct io_uring_cmd *cmd = __ublk_prep_compl_io_cmd(io, req);
 
        /* tell ublksrv one io request is coming */
        io_uring_cmd_done(cmd, res, 0, issue_flags);
@@ -2148,10 +2155,9 @@ static int ublk_commit_and_fetch(const struct ublk_queue *ubq,
        return 0;
 }
 
-static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io)
+static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io,
+                         struct request *req)
 {
-       struct request *req = io->req;
-
        /*
         * We have handled UBLK_IO_NEED_GET_DATA command,
         * so clear UBLK_IO_FLAG_NEED_GET_DATA now and just
@@ -2178,6 +2184,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
        u32 cmd_op = cmd->cmd_op;
        unsigned tag = ub_cmd->tag;
        int ret = -EINVAL;
+       struct request *req;
 
        pr_devel("%s: received: cmd op %d queue %d tag %d result %d\n",
                        __func__, cmd->cmd_op, ub_cmd->q_id, tag,
@@ -2236,11 +2243,19 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                        goto out;
                break;
        case UBLK_IO_NEED_GET_DATA:
-               io->addr = ub_cmd->addr;
-               if (!ublk_get_data(ubq, io))
-                       return -EIOCBQUEUED;
-
-               return UBLK_IO_RES_OK;

drivers/block/ublk_drv.c static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, · line 2255

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+               /*
+                * ublk_get_data() may fail and fallback to requeue, so keep
+                * uring_cmd active first and prepare for handling new requeued
+                * request
+                */
+               req = io->req;
+               ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
+               io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
+               if (likely(ublk_get_data(ubq, io, req))) {
+                       __ublk_prep_compl_io_cmd(io, req);

Isn't __ublk_prep_compl_io_cmd() basically undoing the ublk_fill_io_cmd() and io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV? How about something like this instead?

io->addr = ub_cmd->addr;
if (likely(ublk_get_data(ubq, io)))

        return UBLK_IO_RES_OK;

ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
break;

Best,
Caleb

Quoted 9 lines
drivers/block/ublk_drv.c +3 -1
+                       return UBLK_IO_RES_OK;
+               }
+               break;
        default:
                goto out;
        }
--

2.49.0

maintainer Applied
#4 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei

If ublk_get_data() fails, -EIOCBQUEUED is returned and the current command becomes ASYNC. And the only reason is that mapping data can't move on, because of no enough pages or pending signal, then the current ublk request has to be requeued.

Once the request need to be requeued, we have to setup ublk_io correctly, including io->cmd and flags, otherwise the request may not be forwarded to ublk server successfully.

[...]

Applied, thanks!

[1/1] ublk: setup ublk_io correctly in case of ublk_get_data() failure
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe