public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests: ublk: fix starting ublk device
@ 2025-03-21 13:53 Ming Lei
  2025-03-21 19:47 ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: Ming Lei @ 2025-03-21 13:53 UTC (permalink / raw)
  To: Jens Axboe, linux-block; +Cc: Ming Lei

Firstly ublk char device node may not be created by udev yet, so wait
a while until it can be opened or timeout.

Secondly delete created ublk device in case of start failure, otherwise
the device becomes zombie.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 tools/testing/selftests/ublk/file_backed.c |  4 ++-
 tools/testing/selftests/ublk/kublk.c       | 30 ++++++++++++++--------
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/tools/testing/selftests/ublk/file_backed.c b/tools/testing/selftests/ublk/file_backed.c
index 8a07356eccaf..570a5158b665 100644
--- a/tools/testing/selftests/ublk/file_backed.c
+++ b/tools/testing/selftests/ublk/file_backed.c
@@ -196,11 +196,13 @@ static int ublk_loop_tgt_init(struct ublk_dev *dev)
 		},
 	};
 
-	assert(dev->tgt.nr_backing_files == 1);
 	ret = backing_file_tgt_init(dev);
 	if (ret)
 		return ret;
 
+	if (dev->tgt.nr_backing_files != 1)
+		return -EINVAL;
+
 	bytes = dev->tgt.backing_file_size[0];
 	dev->tgt.dev_size = bytes;
 	p.basic.dev_sectors = bytes >> 9;
diff --git a/tools/testing/selftests/ublk/kublk.c b/tools/testing/selftests/ublk/kublk.c
index 148355717ee7..11005a87bcfa 100644
--- a/tools/testing/selftests/ublk/kublk.c
+++ b/tools/testing/selftests/ublk/kublk.c
@@ -379,26 +379,34 @@ static int ublk_queue_init(struct ublk_queue *q)
 	return -ENOMEM;
 }
 
+#define WAIT_USEC 	100000
+#define MAX_WAIT_USEC 	(3 * 1000000)
 static int ublk_dev_prep(struct ublk_dev *dev)
 {
 	int dev_id = dev->dev_info.dev_id;
+	unsigned int wait_usec = 0;
+	int ret = 0, fd = -1;
 	char buf[64];
-	int ret = 0;
 
 	snprintf(buf, 64, "%s%d", UBLKC_DEV, dev_id);
-	dev->fds[0] = open(buf, O_RDWR);
-	if (dev->fds[0] < 0) {
-		ret = -EBADF;
-		ublk_err("can't open %s, ret %d\n", buf, dev->fds[0]);
-		goto fail;
+
+	while (wait_usec < MAX_WAIT_USEC) {
+		fd = open(buf, O_RDWR);
+		if (fd >= 0)
+			break;
+		usleep(WAIT_USEC);
+		wait_usec += WAIT_USEC;
+	}
+	if (fd < 0) {
+		ublk_err("can't open %s %s\n", buf, strerror(errno));
+		return -1;
 	}
 
+	dev->fds[0] = fd;
 	if (dev->tgt.ops->init_tgt)
 		ret = dev->tgt.ops->init_tgt(dev);
-
-	return ret;
-fail:
-	close(dev->fds[0]);
+	if (ret)
+		close(dev->fds[0]);
 	return ret;
 }
 
@@ -856,6 +864,8 @@ 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);
+	if (ret < 0)
+		ublk_ctrl_del_dev(dev);
 
 fail:
 	if (ret < 0)
-- 
2.47.1


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

* Re: [PATCH] selftests: ublk: fix starting ublk device
  2025-03-21 13:53 [PATCH] selftests: ublk: fix starting ublk device Ming Lei
@ 2025-03-21 19:47 ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2025-03-21 19:47 UTC (permalink / raw)
  To: linux-block, Ming Lei


On Fri, 21 Mar 2025 21:53:24 +0800, Ming Lei wrote:
> Firstly ublk char device node may not be created by udev yet, so wait
> a while until it can be opened or timeout.
> 
> Secondly delete created ublk device in case of start failure, otherwise
> the device becomes zombie.
> 
> 
> [...]

Applied, thanks!

[1/1] selftests: ublk: fix starting ublk device
      commit: 2b17a89292b98c27ffacc7966e2e42bfe0465242

Best regards,
-- 
Jens Axboe




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

end of thread, other threads:[~2025-03-21 19:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-21 13:53 [PATCH] selftests: ublk: fix starting ublk device Ming Lei
2025-03-21 19:47 ` Jens Axboe

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