Skip to contentSkip to conversation

selftests/ublk: three bug fixes

PATCH Applied Ming Lei 3 participants 8 messages Jan 13 – Jan 21

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover selftests/ublk: three bug fixes

Hi Jens,

Fix three misc issues of selftests/ublk:

- make sure all io_uring commands are completed before exiting from ublk thread

- cancel in-flight uring_cmd in case of failing to start device

- logging fix for avoiding segment in case of logging & garbage output in case of foreground

Thanks,

Ming Lei (3):
selftests/ublk: fix IO thread idle check selftests/ublk: fix error handling for starting device selftests/ublk: fix garbage output in foreground mode

 tools/testing/selftests/ublk/kublk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH 1/3 selftests/ublk: fix IO thread idle check

Include cmd_inflight in ublk_thread_is_done() check. Without this, the thread may exit before all FETCH commands are completed, which may cause device deletion to hang.

Fixes 6aecda00b7d1 ("selftests: ublk: add kernel selftests for ublk")
---
 tools/testing/selftests/ublk/kublk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/ublk/kublk.c +1 -1
index 185ba553686a..f52431fe9b6c 100644
@@ -753,7 +753,7 @@ static int ublk_thread_is_idle(struct ublk_thread *t)
 
 static int ublk_thread_is_done(struct ublk_thread *t)
 {
-	return (t->state & UBLKS_T_STOPPING) && ublk_thread_is_idle(t);
+	return (t->state & UBLKS_T_STOPPING) && ublk_thread_is_idle(t) && !t->cmd_inflight;
 }
 
 static inline void ublksrv_handle_tgt_cqe(struct ublk_thread *t,
Signature · 1 line
2.47.0
author
#2 lore raw

PATCH 2/3 selftests/ublk: fix error handling for starting device

In reply to Ming Lei

Fix error handling in ublk_start_daemon() when start_dev fails:

1. Call ublk_ctrl_stop_dev() to cancel inflight uring_cmd before cleanup. Without this, the device deletion may hang waiting for I/O completion that will never happen.

2. Add fail_start label so that pthread_join() is called on the error path. This ensures proper thread cleanup when startup fails.

Fixes 6aecda00b7d1 ("selftests: ublk: add kernel selftests for ublk")
---
 tools/testing/selftests/ublk/kublk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/ublk/kublk.c +4 -2
index f52431fe9b6c..65f59e7b6972 100644
@@ -1054,7 +1054,9 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
 	}
 	if (ret < 0) {
 		ublk_err("%s: ublk_ctrl_start_dev failed: %d\n", __func__, ret);
-		goto fail;
+		/* stop device so that inflight uring_cmd can be cancelled */
+		ublk_ctrl_stop_dev(dev);
+		goto fail_start;
 	}
 
 	ublk_ctrl_get_info(dev);
@@ -1062,7 +1064,7 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
 		ublk_ctrl_dump(dev);
 	else
 		ublk_send_dev_event(ctx, dev, dev->dev_info.dev_id);
-
+fail_start:
 	/* wait until we are terminated */
 	for (i = 0; i < dev->nthreads; i++)
 		pthread_join(tinfo[i].thread, &thread_ret);
Signature · 1 line
2.47.0
Reviewed-by on v1 2/3
#3 lore raw

Re: [PATCH 2/3] selftests/ublk: fix error handling for starting device

Quoted 7 lines · Ming Lei

Fix error handling in ublk_start_daemon() when start_dev fails:

1. Call ublk_ctrl_stop_dev() to cancel inflight uring_cmd before cleanup. Without this, the device deletion may hang waiting for I/O completion that will never happen.

Quoted 5 lines

2. Add fail_start label so that pthread_join() is called on the error path. This ensures proper thread cleanup when startup fails.

Fixes 6aecda00b7d1 ("selftests: ublk: add kernel selftests for ublk")
Quoted 31 lines
---
 tools/testing/selftests/ublk/kublk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/ublk/kublk.c +4 -3
index f52431fe9b6c..65f59e7b6972 100644
@@ -1054,7 +1054,9 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
        }
        if (ret < 0) {
                ublk_err("%s: ublk_ctrl_start_dev failed: %d\n", __func__, ret);
-               goto fail;
+               /* stop device so that inflight uring_cmd can be cancelled */
+               ublk_ctrl_stop_dev(dev);
+               goto fail_start;
        }
 
        ublk_ctrl_get_info(dev);
@@ -1062,7 +1064,7 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
                ublk_ctrl_dump(dev);
        else
                ublk_send_dev_event(ctx, dev, dev->dev_info.dev_id);
-
+fail_start:
        /* wait until we are terminated */
        for (i = 0; i < dev->nthreads; i++)
                pthread_join(tinfo[i].thread, &thread_ret);
--

2.47.0

author
#4 lore raw

PATCH 3/3 selftests/ublk: fix garbage output in foreground mode

In reply to Ming Lei

Initialize _evtfd to -1 in struct dev_ctx to prevent garbage output when running kublk in foreground mode. Without this, _evtfd is zero-initialized to 0 (stdin), and ublk_send_dev_event() writes binary data to stdin which appears as garbage on the terminal.

Also fix debug message format string.

Fixes 6aecda00b7d1 ("selftests: ublk: add kernel selftests for ublk")
---
 tools/testing/selftests/ublk/kublk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/ublk/kublk.c +2 -1
index 65f59e7b6972..f197ad9cc262 100644
@@ -1274,7 +1274,7 @@ static int __cmd_dev_add(const struct dev_ctx *ctx)
 	}
 
 	ret = ublk_start_daemon(ctx, dev);
-	ublk_dbg(UBLK_DBG_DEV, "%s: daemon exit %d\b", ret);
+	ublk_dbg(UBLK_DBG_DEV, "%s: daemon exit %d\n", __func__, ret);
 	if (ret < 0)
 		ublk_ctrl_del_dev(dev);
 
@@ -1620,6 +1620,7 @@ int main(int argc, char *argv[])
 	int option_idx, opt;
 	const char *cmd = argv[1];
 	struct dev_ctx ctx = {
+		._evtfd         =       -1,
 		.queue_depth	=	128,
 		.nr_hw_queues	=	2,
 		.dev_id		=	-1,
Signature · 1 line
2.47.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] selftests/ublk: fix garbage output in foreground mode

Quoted 19 lines · Ming Lei

Initialize _evtfd to -1 in struct dev_ctx to prevent garbage output when running kublk in foreground mode. Without this, _evtfd is zero-initialized to 0 (stdin), and ublk_send_dev_event() writes binary data to stdin which appears as garbage on the terminal.

Also fix debug message format string.

Fixes 6aecda00b7d1 ("selftests: ublk: add kernel selftests for ublk")
---
 tools/testing/selftests/ublk/kublk.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/ublk/kublk.c +0 -0
index 65f59e7b6972..f197ad9cc262 100644

tools/testing/selftests/ublk/kublk.c static int __cmd_dev_add(const struct dev_ctx *ctx) · line 1277

Quoted 5 lines
tools/testing/selftests/ublk/kublk.c +1 -1
@@ -1274,7 +1274,7 @@ static int __cmd_dev_add(const struct dev_ctx *ctx)
        }
 
        ret = ublk_start_daemon(ctx, dev);
-       ublk_dbg(UBLK_DBG_DEV, "%s: daemon exit %d\b", ret);
+       ublk_dbg(UBLK_DBG_DEV, "%s: daemon exit %d\n", __func__, ret);

Could add __attribute__((format(printf, 2, 3))) to ublk_dbg() so the compiler can warn about this in the future. Similar for ublk_err() and ublk_log().

But this fix looks good.

Quoted 14 lines
tools/testing/selftests/ublk/kublk.c +1 -1
        if (ret < 0)
                ublk_ctrl_del_dev(dev);
 
@@ -1620,6 +1620,7 @@ int main(int argc, char *argv[])
        int option_idx, opt;
        const char *cmd = argv[1];
        struct dev_ctx ctx = {
+               ._evtfd         =       -1,
                .queue_depth    =       128,
                .nr_hw_queues   =       2,
                .dev_id         =       -1,
--

2.47.0

author
#6 lore raw
In reply to Ming Lei
Quoted 13 lines · Ming Lei

Hi Jens,

Fix three misc issues of selftests/ublk:

- make sure all io_uring commands are completed before exiting from ublk thread

- cancel in-flight uring_cmd in case of failing to start device

- logging fix for avoiding segment in case of logging & garbage output in case of foreground

Quoted 7 lines

Thanks,

Ming Lei (3):
selftests/ublk: fix IO thread idle check selftests/ublk: fix error handling for starting device selftests/ublk: fix garbage output in foreground mode

Hi Jens,

Can you merge these patches if you are fine?

Thanks,
Ming

maintainer Applied
#7 lore raw
In reply to Ming Lei
Quoted 8 lines · Ming Lei

Fix three misc issues of selftests/ublk:

- make sure all io_uring commands are completed before exiting from ublk thread

- cancel in-flight uring_cmd in case of failing to start device

[...]

Applied, thanks!

[1/3] selftests/ublk: fix IO thread idle check

      commit: 75aad5ffe099a1b1a342257236dc260493917ed2
[2/3] selftests/ublk: fix error handling for starting device
      commit: 23e62cf75518825aac12e9a22bdc40f062428898
[3/3] selftests/ublk: fix garbage output in foreground mode
      commit: e7e1cc18f120a415646be12470169a978a1adcd9

Best regards,

Signature · 1 line
Jens Axboe