Skip to contentSkip to conversation

selftests: ublk: fix starting ublk device

PATCH Applied Ming Lei 2 participants 2 messages Mar 21, 2025

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 selftests: ublk: fix starting ublk device

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.

---
 tools/testing/selftests/ublk/file_backed.c |  4 ++-
 tools/testing/selftests/ublk/kublk.c       | 30 ++++++++++++++--------
 2 files changed, 23 insertions(+), 11 deletions(-)
tools/testing/selftests/ublk/file_backed.c +3 -1
index 8a07356eccaf..570a5158b665 100644
@@ -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;
tools/testing/selftests/ublk/kublk.c +20 -10
index 148355717ee7..11005a87bcfa 100644
@@ -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)
Signature · 1 line
2.47.1
maintainer Applied
#1 lore raw
Quoted 8 lines · 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.

[...]

Applied, thanks!

[1/1] selftests: ublk: fix starting ublk device

      commit: 2b17a89292b98c27ffacc7966e2e42bfe0465242

Best regards,

Signature · 1 line
Jens Axboe