Skip to contentSkip to conversation

ublk: introduce UBLK_CMD_TRY_STOP_DEV

PATCH Superseded Yoav Cohen 2 participants 8 messages Jan 6 – Jan 18 1 message not in the archive

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Yoav Cohen

author
#0 lore raw

v4 cover ublk: introduce UBLK_CMD_TRY_STOP_DEV

Hello,

This patch series introduces a new command for ublk device management.

The first patch changes ublk_ctrl_stop_dev() to return void, since it always returned 0. This simplifies the API.

The second patch introduces UBLK_CMD_TRY_STOP_DEV, which stops the device only if there are no active openers. Unlike the existing stop command (UBLK_CMD_STOP_DEV), this command avoids disrupting active users by returning -EBUSY if the device is busy.

These patches only introduce the new command and API simplification without altering existing behavior for active users

Changes since v2:
- Address Ming Lei’s comments in patch 2:
- Add a feature flag and some minor comments.
- Patch 1 unchanged, keeps Reviewed-by

Changes since v3:
- Address Caleb's comments in patch 2
- add to kublk.c
- add to auto features
- set feature flag to 17

Yoav Cohen (2):
ublk: make ublk_ctrl_stop_dev return void
ublk: add UBLK_CMD_TRY_STOP_DEV command

 drivers/block/ublk_drv.c             | 50 +++++++++++++++++++++++++---
 include/uapi/linux/ublk_cmd.h        |  9 ++++-
 tools/testing/selftests/ublk/kublk.c |  1 +
 3 files changed, 54 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
author
#1 lore raw

PATCH v4 1/2 ublk: make ublk_ctrl_stop_dev return void

This function always returns 0, so there is no need to return a value.

---
 drivers/block/ublk_drv.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +3 -3
index 837fedb02e0d..2d5602ef05cc 100644
@@ -3304,10 +3304,9 @@ static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd)
 			header->data[0], header->addr, header->len);
 }
 
-static int ublk_ctrl_stop_dev(struct ublk_device *ub)
+static void ublk_ctrl_stop_dev(struct ublk_device *ub)
 {
 	ublk_stop_dev(ub);
-	return 0;
 }
 
 static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
@@ -3780,7 +3779,8 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 		ret = ublk_ctrl_start_dev(ub, header);
 		break;
 	case UBLK_CMD_STOP_DEV:
-		ret = ublk_ctrl_stop_dev(ub);
+		ublk_ctrl_stop_dev(ub);
+		ret = 0;
 		break;
 	case UBLK_CMD_GET_DEV_INFO:
 	case UBLK_CMD_GET_DEV_INFO2:
Signature · 1 line
2.39.5 (Apple Git-154)
author
#2 lore raw

PATCH v4 2/2 ublk: add UBLK_CMD_TRY_STOP_DEV command

In reply to Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
 drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++--
 include/uapi/linux/ublk_cmd.h        |  9 +++++-
 tools/testing/selftests/ublk/kublk.c |  1 +
 3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
 #define UBLK_CMD_DEL_DEV_ASYNC	_IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
 #define UBLK_CMD_UPDATE_SIZE	_IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
 #define UBLK_CMD_QUIESCE_DEV	_IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
+#define UBLK_CMD_TRY_STOP_DEV	_IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
 
 #define UBLK_IO_REGISTER_IO_BUF		_IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
 #define UBLK_IO_UNREGISTER_IO_BUF	_IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -73,7 +74,8 @@
 		| UBLK_F_AUTO_BUF_REG \
 		| UBLK_F_QUIESCE \
 		| UBLK_F_PER_IO_DAEMON \
-		| UBLK_F_BUF_REG_OFF_DAEMON)
+		| UBLK_F_BUF_REG_OFF_DAEMON \
+		| UBLK_F_SAFE_STOP_DEV)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
 		| UBLK_F_USER_RECOVERY_REISSUE \
@@ -239,6 +241,8 @@ struct ublk_device {
 	struct delayed_work	exit_work;
 	struct work_struct	partition_scan_work;
 
+	bool			block_open; /* protected by open_mutex */
+
 	struct ublk_queue       *queues[];
 };
 
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
 			return -EPERM;
 	}
 
+	if (ub->block_open)
+		return -EBUSY;
+
 	return 0;
 }
 
@@ -3188,7 +3195,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
 	ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
 		UBLK_F_URING_CMD_COMP_IN_TASK |
 		UBLK_F_PER_IO_DAEMON |
-		UBLK_F_BUF_REG_OFF_DAEMON;
+		UBLK_F_BUF_REG_OFF_DAEMON |
+		UBLK_F_SAFE_STOP_DEV;
 
 	/* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
 	if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3309,6 +3317,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
 	ublk_stop_dev(ub);
 }
 
+static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
+{
+	struct gendisk *disk;
+	int ret = 0;
+
+	disk = ublk_get_disk(ub);
+	if (!disk)
+		return -ENODEV;
+
+	mutex_lock(&disk->open_mutex);
+	if (disk_openers(disk) > 0) {
+		ret = -EBUSY;
+		goto unlock;
+	}
+	ub->block_open = true;
+	/* release open_mutex as del_gendisk() will reacquire it */
+	mutex_unlock(&disk->open_mutex);
+
+	ublk_ctrl_stop_dev(ub);
+	goto out;
+
+unlock:
+	mutex_unlock(&disk->open_mutex);
+out:
+	ublk_put_disk(disk);
+	return ret;
+}
+
 static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
 		const struct ublksrv_ctrl_cmd *header)
 {
@@ -3704,6 +3740,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
 	case UBLK_CMD_END_USER_RECOVERY:
 	case UBLK_CMD_UPDATE_SIZE:
 	case UBLK_CMD_QUIESCE_DEV:
+	case UBLK_CMD_TRY_STOP_DEV:
 		mask = MAY_READ | MAY_WRITE;
 		break;
 	default:
@@ -3817,6 +3854,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 	case UBLK_CMD_QUIESCE_DEV:
 		ret = ublk_ctrl_quiesce_dev(ub, header);
 		break;
+	case UBLK_CMD_TRY_STOP_DEV:
+		ret = ublk_ctrl_try_stop_dev(ub);
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
include/uapi/linux/ublk_cmd.h +8 -1
index ec77dabba45b..9daa8ab372f0 100644
@@ -55,7 +55,8 @@
 	_IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
 #define UBLK_U_CMD_QUIESCE_DEV		\
 	_IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
-
+#define UBLK_U_CMD_TRY_STOP_DEV		\
+	_IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
 /*
  * 64bits are enough now, and it should be easy to extend in case of
  * running out of feature flags
@@ -311,6 +312,12 @@
  */
 #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+/*
+ * The device supports the UBLK_CMD_TRY_STOP_DEV command, which
+ * allows stopping the device only if there are no openers.
+ */
+#define UBLK_F_SAFE_STOP_DEV	(1ULL << 17)
+
 /* device state */
 #define UBLK_S_DEV_DEAD	0
 #define UBLK_S_DEV_LIVE	1
tools/testing/selftests/ublk/kublk.c +1 -0
index 185ba553686a..6739e28c4059 100644
@@ -1454,6 +1454,7 @@ static int cmd_dev_get_features(void)
 		FEAT_NAME(UBLK_F_QUIESCE),
 		FEAT_NAME(UBLK_F_PER_IO_DAEMON),
 		FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+		FEAT_NAME(UBLK_F_SAFE_STOP_DEV),
 	};
 	struct ublk_dev *dev;
 	__u64 features = 0;
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v4 2/2
#3 lore raw

Re: [PATCH v4 2/2] ublk: add UBLK_CMD_TRY_STOP_DEV command

Quoted 151 lines · Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
 drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++--
 include/uapi/linux/ublk_cmd.h        |  9 +++++-
 tools/testing/selftests/ublk/kublk.c |  1 +
 3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
 #define UBLK_CMD_DEL_DEV_ASYNC	_IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
 #define UBLK_CMD_UPDATE_SIZE	_IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
 #define UBLK_CMD_QUIESCE_DEV	_IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
+#define UBLK_CMD_TRY_STOP_DEV	_IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
 
 #define UBLK_IO_REGISTER_IO_BUF		_IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
 #define UBLK_IO_UNREGISTER_IO_BUF	_IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -73,7 +74,8 @@
 		| UBLK_F_AUTO_BUF_REG \
 		| UBLK_F_QUIESCE \
 		| UBLK_F_PER_IO_DAEMON \
-		| UBLK_F_BUF_REG_OFF_DAEMON)
+		| UBLK_F_BUF_REG_OFF_DAEMON \
+		| UBLK_F_SAFE_STOP_DEV)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
 		| UBLK_F_USER_RECOVERY_REISSUE \
@@ -239,6 +241,8 @@ struct ublk_device {
 	struct delayed_work	exit_work;
 	struct work_struct	partition_scan_work;
 
+	bool			block_open; /* protected by open_mutex */
+
 	struct ublk_queue       *queues[];
 };
 
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
 			return -EPERM;
 	}
 
+	if (ub->block_open)
+		return -EBUSY;
+
 	return 0;
 }
 
@@ -3188,7 +3195,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
 	ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
 		UBLK_F_URING_CMD_COMP_IN_TASK |
 		UBLK_F_PER_IO_DAEMON |
-		UBLK_F_BUF_REG_OFF_DAEMON;
+		UBLK_F_BUF_REG_OFF_DAEMON |
+		UBLK_F_SAFE_STOP_DEV;
 
 	/* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
 	if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3309,6 +3317,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
 	ublk_stop_dev(ub);
 }
 
+static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
+{
+	struct gendisk *disk;
+	int ret = 0;
+
+	disk = ublk_get_disk(ub);
+	if (!disk)
+		return -ENODEV;
+
+	mutex_lock(&disk->open_mutex);
+	if (disk_openers(disk) > 0) {
+		ret = -EBUSY;
+		goto unlock;
+	}
+	ub->block_open = true;
+	/* release open_mutex as del_gendisk() will reacquire it */
+	mutex_unlock(&disk->open_mutex);
+
+	ublk_ctrl_stop_dev(ub);
+	goto out;
+
+unlock:
+	mutex_unlock(&disk->open_mutex);
+out:
+	ublk_put_disk(disk);
+	return ret;
+}
+
 static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
 		const struct ublksrv_ctrl_cmd *header)
 {
@@ -3704,6 +3740,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
 	case UBLK_CMD_END_USER_RECOVERY:
 	case UBLK_CMD_UPDATE_SIZE:
 	case UBLK_CMD_QUIESCE_DEV:
+	case UBLK_CMD_TRY_STOP_DEV:
 		mask = MAY_READ | MAY_WRITE;
 		break;
 	default:
@@ -3817,6 +3854,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 	case UBLK_CMD_QUIESCE_DEV:
 		ret = ublk_ctrl_quiesce_dev(ub, header);
 		break;
+	case UBLK_CMD_TRY_STOP_DEV:
+		ret = ublk_ctrl_try_stop_dev(ub);
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 		break;
include/uapi/linux/ublk_cmd.h +8 -1
index ec77dabba45b..9daa8ab372f0 100644
@@ -55,7 +55,8 @@
 	_IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
 #define UBLK_U_CMD_QUIESCE_DEV		\
 	_IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
-
+#define UBLK_U_CMD_TRY_STOP_DEV		\
+	_IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
 /*
  * 64bits are enough now, and it should be easy to extend in case of
  * running out of feature flags
@@ -311,6 +312,12 @@
  */
 #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+/*
+ * The device supports the UBLK_CMD_TRY_STOP_DEV command, which
+ * allows stopping the device only if there are no openers.
+ */
+#define UBLK_F_SAFE_STOP_DEV	(1ULL << 17)
+
 /* device state */
 #define UBLK_S_DEV_DEAD	0
 #define UBLK_S_DEV_LIVE	1
tools/testing/selftests/ublk/kublk.c +0 -0
index 185ba553686a..6739e28c4059 100644

tools/testing/selftests/ublk/kublk.c static int cmd_dev_get_features(void) · line 1457

Quoted 4 lines
tools/testing/selftests/ublk/kublk.c +1 -0
@@ -1454,6 +1454,7 @@ static int cmd_dev_get_features(void)
 		FEAT_NAME(UBLK_F_QUIESCE),
 		FEAT_NAME(UBLK_F_PER_IO_DAEMON),
 		FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+		FEAT_NAME(UBLK_F_SAFE_STOP_DEV),

We usually split driver and test code into different patches, and the patch looks fine:

But we usually separate test code from driver change, also new feature needs selftest code for verifying, can you move +FEAT_NAME(UBLK_F_SAFE_STOP_DEV) into the attached selftest patch and post all 3 in v5?

Thanks,
Ming

From 3574f73d3098dc56f63b7c2b1097c0f090c4ad3c Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Wed, 7 Jan 2026 13:01:39 +0000
Subject: [PATCH] selftests: ublk: add stop command with --safe option

Add 'stop' subcommand to kublk utility that uses the new UBLK_CMD_TRY_STOP_DEV command when --safe option is specified. This allows stopping a device only if it has no active openers, returning -EBUSY otherwise.

Also add test_generic_16.sh to test the new functionality.

---
 tools/testing/selftests/ublk/Makefile         |  1 +
 tools/testing/selftests/ublk/kublk.c          | 52 +++++++++++++++++
 tools/testing/selftests/ublk/kublk.h          |  1 +
 .../testing/selftests/ublk/test_generic_16.sh | 57 +++++++++++++++++++
 4 files changed, 111 insertions(+)
 create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh
tools/testing/selftests/ublk/Makefile +1 -0
index 06ba6fde098d..e9614f77b809 100644
@@ -23,6 +23,7 @@ TEST_PROGS += test_generic_12.sh
 TEST_PROGS += test_generic_13.sh
 TEST_PROGS += test_generic_14.sh
 TEST_PROGS += test_generic_15.sh
+TEST_PROGS += test_generic_16.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/kublk.c +52 -0

88 lines not on this page · raw message

tools/testing/selftests/ublk/kublk.h +1 -0
index 8a83b90ec603..4b6b4992f78e 100644
@@ -78,6 +78,7 @@ struct dev_ctx {
 	unsigned int	auto_zc_fallback:1;
 	unsigned int	per_io_tasks:1;
 	unsigned int	no_ublk_fixed_fd:1;
+	unsigned int	safe_stop:1;
 
 	int _evtfd;
 	int _shmid;
tools/testing/selftests/ublk/test_generic_16.sh new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.47.1
author
#4 lore raw

Re: [PATCH v4 2/2] ublk: add UBLK_CMD_TRY_STOP_DEV command

Quoted 165 lines · Ming Lei

External email: Use caution opening links or attachments

Quoted 151 lines · Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
  drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++--
  include/uapi/linux/ublk_cmd.h        |  9 +++++-
  tools/testing/selftests/ublk/kublk.c |  1 +
  3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
  #define UBLK_CMD_DEL_DEV_ASYNC       _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
  #define UBLK_CMD_UPDATE_SIZE _IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
  #define UBLK_CMD_QUIESCE_DEV _IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
+#define UBLK_CMD_TRY_STOP_DEV        _IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
 
  #define UBLK_IO_REGISTER_IO_BUF              _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
  #define UBLK_IO_UNREGISTER_IO_BUF    _IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -73,7 +74,8 @@
               | UBLK_F_AUTO_BUF_REG \
               | UBLK_F_QUIESCE \
               | UBLK_F_PER_IO_DAEMON \
-             | UBLK_F_BUF_REG_OFF_DAEMON)
+             | UBLK_F_BUF_REG_OFF_DAEMON \
+             | UBLK_F_SAFE_STOP_DEV)
 
  #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
               | UBLK_F_USER_RECOVERY_REISSUE \
@@ -239,6 +241,8 @@ struct ublk_device {
       struct delayed_work     exit_work;
       struct work_struct      partition_scan_work;
 
+     bool                    block_open; /* protected by open_mutex */
+
       struct ublk_queue       *queues[];
  };
 
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
                       return -EPERM;
       }
 
+     if (ub->block_open)
+             return -EBUSY;
+
       return 0;
  }
 
@@ -3188,7 +3195,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
       ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
               UBLK_F_URING_CMD_COMP_IN_TASK |
               UBLK_F_PER_IO_DAEMON |
-             UBLK_F_BUF_REG_OFF_DAEMON;
+             UBLK_F_BUF_REG_OFF_DAEMON |
+             UBLK_F_SAFE_STOP_DEV;
 
       /* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
       if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3309,6 +3317,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
       ublk_stop_dev(ub);
  }
 
+static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
+{
+     struct gendisk *disk;
+     int ret = 0;
+
+     disk = ublk_get_disk(ub);
+     if (!disk)
+             return -ENODEV;
+
+     mutex_lock(&disk->open_mutex);
+     if (disk_openers(disk) > 0) {
+             ret = -EBUSY;
+             goto unlock;
+     }
+     ub->block_open = true;
+     /* release open_mutex as del_gendisk() will reacquire it */
+     mutex_unlock(&disk->open_mutex);
+
+     ublk_ctrl_stop_dev(ub);
+     goto out;
+
+unlock:
+     mutex_unlock(&disk->open_mutex);
+out:
+     ublk_put_disk(disk);
+     return ret;
+}
+
  static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
               const struct ublksrv_ctrl_cmd *header)
  {
@@ -3704,6 +3740,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
       case UBLK_CMD_END_USER_RECOVERY:
       case UBLK_CMD_UPDATE_SIZE:
       case UBLK_CMD_QUIESCE_DEV:
+     case UBLK_CMD_TRY_STOP_DEV:
               mask = MAY_READ | MAY_WRITE;
               break;
       default:
@@ -3817,6 +3854,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
       case UBLK_CMD_QUIESCE_DEV:
               ret = ublk_ctrl_quiesce_dev(ub, header);
               break;
+     case UBLK_CMD_TRY_STOP_DEV:
+             ret = ublk_ctrl_try_stop_dev(ub);
+             break;
       default:
               ret = -EOPNOTSUPP;
               break;
include/uapi/linux/ublk_cmd.h +8 -1
index ec77dabba45b..9daa8ab372f0 100644
@@ -55,7 +55,8 @@
       _IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
  #define UBLK_U_CMD_QUIESCE_DEV               \
       _IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
-
+#define UBLK_U_CMD_TRY_STOP_DEV              \
+     _IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
  /*
   * 64bits are enough now, and it should be easy to extend in case of
   * running out of feature flags
@@ -311,6 +312,12 @@
   */
  #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+/*
+ * The device supports the UBLK_CMD_TRY_STOP_DEV command, which
+ * allows stopping the device only if there are no openers.
+ */
+#define UBLK_F_SAFE_STOP_DEV (1ULL << 17)
+
  /* device state */
  #define UBLK_S_DEV_DEAD      0
  #define UBLK_S_DEV_LIVE      1
tools/testing/selftests/ublk/kublk.c +0 -0
index 185ba553686a..6739e28c4059 100644

tools/testing/selftests/ublk/kublk.c static int cmd_dev_get_features(void) · line 1457

Quoted 4 lines
tools/testing/selftests/ublk/kublk.c +1 -0
@@ -1454,6 +1454,7 @@ static int cmd_dev_get_features(void)
               FEAT_NAME(UBLK_F_QUIESCE),
               FEAT_NAME(UBLK_F_PER_IO_DAEMON),
               FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+             FEAT_NAME(UBLK_F_SAFE_STOP_DEV),

We usually split driver and test code into different patches, and the patch looks fine:

Quoted 8 lines

But we usually separate test code from driver change, also new feature needs selftest code for verifying, can you move +FEAT_NAME(UBLK_F_SAFE_STOP_DEV) into the attached selftest patch and post all 3 in v5?

Thanks,
Ming

Hi,

Thank you Ming, just to be sure:
I'll split the kublk change to a 3rd patch - that's ok.
But do you also want me to write a selftest for this feature as part of the 3rd patch?

Thank you

#5 lore raw

Re: [PATCH v4 2/2] ublk: add UBLK_CMD_TRY_STOP_DEV command

Quoted 175 lines · Yoav Cohen
Quoted 165 lines · Ming Lei

External email: Use caution opening links or attachments

Quoted 151 lines · Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
  drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++--
  include/uapi/linux/ublk_cmd.h        |  9 +++++-
  tools/testing/selftests/ublk/kublk.c |  1 +
  3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
  #define UBLK_CMD_DEL_DEV_ASYNC       _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
  #define UBLK_CMD_UPDATE_SIZE _IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
  #define UBLK_CMD_QUIESCE_DEV _IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
+#define UBLK_CMD_TRY_STOP_DEV        _IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
 
  #define UBLK_IO_REGISTER_IO_BUF              _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
  #define UBLK_IO_UNREGISTER_IO_BUF    _IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -73,7 +74,8 @@
               | UBLK_F_AUTO_BUF_REG \
               | UBLK_F_QUIESCE \
               | UBLK_F_PER_IO_DAEMON \
-             | UBLK_F_BUF_REG_OFF_DAEMON)
+             | UBLK_F_BUF_REG_OFF_DAEMON \
+             | UBLK_F_SAFE_STOP_DEV)
 
  #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
               | UBLK_F_USER_RECOVERY_REISSUE \
@@ -239,6 +241,8 @@ struct ublk_device {
       struct delayed_work     exit_work;
       struct work_struct      partition_scan_work;
 
+     bool                    block_open; /* protected by open_mutex */
+
       struct ublk_queue       *queues[];
  };
 
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
                       return -EPERM;
       }
 
+     if (ub->block_open)
+             return -EBUSY;
+
       return 0;
  }
 
@@ -3188,7 +3195,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
       ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
               UBLK_F_URING_CMD_COMP_IN_TASK |
               UBLK_F_PER_IO_DAEMON |
-             UBLK_F_BUF_REG_OFF_DAEMON;
+             UBLK_F_BUF_REG_OFF_DAEMON |
+             UBLK_F_SAFE_STOP_DEV;
 
       /* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
       if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3309,6 +3317,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
       ublk_stop_dev(ub);
  }
 
+static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
+{
+     struct gendisk *disk;
+     int ret = 0;
+
+     disk = ublk_get_disk(ub);
+     if (!disk)
+             return -ENODEV;
+
+     mutex_lock(&disk->open_mutex);
+     if (disk_openers(disk) > 0) {
+             ret = -EBUSY;
+             goto unlock;
+     }
+     ub->block_open = true;
+     /* release open_mutex as del_gendisk() will reacquire it */
+     mutex_unlock(&disk->open_mutex);
+
+     ublk_ctrl_stop_dev(ub);
+     goto out;
+
+unlock:
+     mutex_unlock(&disk->open_mutex);
+out:
+     ublk_put_disk(disk);
+     return ret;
+}
+
  static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
               const struct ublksrv_ctrl_cmd *header)
  {
@@ -3704,6 +3740,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
       case UBLK_CMD_END_USER_RECOVERY:
       case UBLK_CMD_UPDATE_SIZE:
       case UBLK_CMD_QUIESCE_DEV:
+     case UBLK_CMD_TRY_STOP_DEV:
               mask = MAY_READ | MAY_WRITE;
               break;
       default:
@@ -3817,6 +3854,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
       case UBLK_CMD_QUIESCE_DEV:
               ret = ublk_ctrl_quiesce_dev(ub, header);
               break;
+     case UBLK_CMD_TRY_STOP_DEV:
+             ret = ublk_ctrl_try_stop_dev(ub);
+             break;
       default:
               ret = -EOPNOTSUPP;
               break;
include/uapi/linux/ublk_cmd.h +8 -1
index ec77dabba45b..9daa8ab372f0 100644
@@ -55,7 +55,8 @@
       _IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
  #define UBLK_U_CMD_QUIESCE_DEV               \
       _IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
-
+#define UBLK_U_CMD_TRY_STOP_DEV              \
+     _IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
  /*
   * 64bits are enough now, and it should be easy to extend in case of
   * running out of feature flags
@@ -311,6 +312,12 @@
   */
  #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+/*
+ * The device supports the UBLK_CMD_TRY_STOP_DEV command, which
+ * allows stopping the device only if there are no openers.
+ */
+#define UBLK_F_SAFE_STOP_DEV (1ULL << 17)
+
  /* device state */
  #define UBLK_S_DEV_DEAD      0
  #define UBLK_S_DEV_LIVE      1
tools/testing/selftests/ublk/kublk.c +0 -0
index 185ba553686a..6739e28c4059 100644

tools/testing/selftests/ublk/kublk.c static int cmd_dev_get_features(void) · line 1457

Quoted 4 lines
tools/testing/selftests/ublk/kublk.c +1 -0
@@ -1454,6 +1454,7 @@ static int cmd_dev_get_features(void)
               FEAT_NAME(UBLK_F_QUIESCE),
               FEAT_NAME(UBLK_F_PER_IO_DAEMON),
               FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+             FEAT_NAME(UBLK_F_SAFE_STOP_DEV),

We usually split driver and test code into different patches, and the patch looks fine:

Quoted 8 lines

But we usually separate test code from driver change, also new feature needs selftest code for verifying, can you move +FEAT_NAME(UBLK_F_SAFE_STOP_DEV) into the attached selftest patch and post all 3 in v5?

Thanks,
Ming

Quoted 6 lines

Hi,

Thank you Ming, just to be sure:
I'll split the kublk change to a 3rd patch - that's ok.
But do you also want me to write a selftest for this feature as part of the 3rd patch?

The 3rd patch has included one complete test case, but feel free to fix/improve, or even rewrite it if you think it isn't good.

Thanks,
Ming

author
#6 lore raw

Re: [PATCH v4 2/2] ublk: add UBLK_CMD_TRY_STOP_DEV command

Quoted 186 lines · Ming Lei

External email: Use caution opening links or attachments

Quoted 175 lines · Yoav Cohen
Quoted 165 lines · Ming Lei

External email: Use caution opening links or attachments

Quoted 151 lines · Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
   drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++--
   include/uapi/linux/ublk_cmd.h        |  9 +++++-
   tools/testing/selftests/ublk/kublk.c |  1 +
   3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
   #define UBLK_CMD_DEL_DEV_ASYNC       _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
   #define UBLK_CMD_UPDATE_SIZE _IOC_NR(UBLK_U_CMD_UPDATE_SIZE)
   #define UBLK_CMD_QUIESCE_DEV _IOC_NR(UBLK_U_CMD_QUIESCE_DEV)
+#define UBLK_CMD_TRY_STOP_DEV        _IOC_NR(UBLK_U_CMD_TRY_STOP_DEV)
 
   #define UBLK_IO_REGISTER_IO_BUF              _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
   #define UBLK_IO_UNREGISTER_IO_BUF    _IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
@@ -73,7 +74,8 @@
                | UBLK_F_AUTO_BUF_REG \
                | UBLK_F_QUIESCE \
                | UBLK_F_PER_IO_DAEMON \
-             | UBLK_F_BUF_REG_OFF_DAEMON)
+             | UBLK_F_BUF_REG_OFF_DAEMON \
+             | UBLK_F_SAFE_STOP_DEV)
 
   #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
@@ -239,6 +241,8 @@ struct ublk_device {
        struct delayed_work     exit_work;
        struct work_struct      partition_scan_work;
 
+     bool                    block_open; /* protected by open_mutex */
+
        struct ublk_queue       *queues[];
   };
 
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, blk_mode_t mode)
                        return -EPERM;
        }
 
+     if (ub->block_open)
+             return -EBUSY;
+
        return 0;
   }
 
@@ -3188,7 +3195,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
        ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
                UBLK_F_URING_CMD_COMP_IN_TASK |
                UBLK_F_PER_IO_DAEMON |
-             UBLK_F_BUF_REG_OFF_DAEMON;
+             UBLK_F_BUF_REG_OFF_DAEMON |
+             UBLK_F_SAFE_STOP_DEV;
 
        /* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
        if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
@@ -3309,6 +3317,34 @@ static void ublk_ctrl_stop_dev(struct ublk_device *ub)
        ublk_stop_dev(ub);
   }
 
+static int ublk_ctrl_try_stop_dev(struct ublk_device *ub)
+{
+     struct gendisk *disk;
+     int ret = 0;
+
+     disk = ublk_get_disk(ub);
+     if (!disk)
+             return -ENODEV;
+
+     mutex_lock(&disk->open_mutex);
+     if (disk_openers(disk) > 0) {
+             ret = -EBUSY;
+             goto unlock;
+     }
+     ub->block_open = true;
+     /* release open_mutex as del_gendisk() will reacquire it */
+     mutex_unlock(&disk->open_mutex);
+
+     ublk_ctrl_stop_dev(ub);
+     goto out;
+
+unlock:
+     mutex_unlock(&disk->open_mutex);
+out:
+     ublk_put_disk(disk);
+     return ret;
+}
+
   static int ublk_ctrl_get_dev_info(struct ublk_device *ub,
                const struct ublksrv_ctrl_cmd *header)
   {
@@ -3704,6 +3740,7 @@ static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
        case UBLK_CMD_END_USER_RECOVERY:
        case UBLK_CMD_UPDATE_SIZE:
        case UBLK_CMD_QUIESCE_DEV:
+     case UBLK_CMD_TRY_STOP_DEV:
                mask = MAY_READ | MAY_WRITE;
                break;
        default:
@@ -3817,6 +3854,9 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
        case UBLK_CMD_QUIESCE_DEV:
                ret = ublk_ctrl_quiesce_dev(ub, header);
                break;
+     case UBLK_CMD_TRY_STOP_DEV:
+             ret = ublk_ctrl_try_stop_dev(ub);
+             break;
        default:
                ret = -EOPNOTSUPP;
                break;
include/uapi/linux/ublk_cmd.h +8 -1
index ec77dabba45b..9daa8ab372f0 100644
@@ -55,7 +55,8 @@
        _IOWR('u', 0x15, struct ublksrv_ctrl_cmd)
   #define UBLK_U_CMD_QUIESCE_DEV               \
        _IOWR('u', 0x16, struct ublksrv_ctrl_cmd)
-
+#define UBLK_U_CMD_TRY_STOP_DEV              \
+     _IOWR('u', 0x17, struct ublksrv_ctrl_cmd)
   /*
    * 64bits are enough now, and it should be easy to extend in case of
    * running out of feature flags
@@ -311,6 +312,12 @@
    */
   #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+/*
+ * The device supports the UBLK_CMD_TRY_STOP_DEV command, which
+ * allows stopping the device only if there are no openers.
+ */
+#define UBLK_F_SAFE_STOP_DEV (1ULL << 17)
+
   /* device state */
   #define UBLK_S_DEV_DEAD      0
   #define UBLK_S_DEV_LIVE      1
tools/testing/selftests/ublk/kublk.c +0 -0
index 185ba553686a..6739e28c4059 100644

tools/testing/selftests/ublk/kublk.c static int cmd_dev_get_features(void) · line 1457

Quoted 4 lines
tools/testing/selftests/ublk/kublk.c +1 -0
@@ -1454,6 +1454,7 @@ static int cmd_dev_get_features(void)
                FEAT_NAME(UBLK_F_QUIESCE),
                FEAT_NAME(UBLK_F_PER_IO_DAEMON),
                FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+             FEAT_NAME(UBLK_F_SAFE_STOP_DEV),

We usually split driver and test code into different patches, and the patch looks fine:

Quoted 8 lines

But we usually separate test code from driver change, also new feature needs selftest code for verifying, can you move +FEAT_NAME(UBLK_F_SAFE_STOP_DEV) into the attached selftest patch and post all 3 in v5?

Thanks,
Ming

Quoted 6 lines

Hi,

Thank you Ming, just to be sure:
I'll split the kublk change to a 3rd patch - that's ok.
But do you also want me to write a selftest for this feature as part of the 3rd patch?

Quoted 7 lines

The 3rd patch has included one complete test case, but feel free to fix/improve, or even rewrite it if you think it isn't good.

Thanks,
Ming

No problem.
Will do.

Thank you

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#8 lore raw

Re: [PATCH v4 2/2] ublk: add UBLK_CMD_TRY_STOP_DEV command

Quoted 33 lines · Alexander Atanasov

External email: Use caution opening links or attachments

Quoted 21 lines · Yoav Cohen

This command is similar to UBLK_CMD_STOP_DEV, but it only stops the device if there are no active openers for the ublk block device. If the device is busy, the command returns -EBUSY instead of disrupting active clients. This allows safe, non-destructive stopping.

Advertise UBLK_CMD_TRY_STOP_DEV support via UBLK_F_SAFE_STOP_DEV feature flag.

---
  drivers/block/ublk_drv.c             | 44 ++++++++++++++++++++++++++-- include/uapi/linux/ublk_cmd.h        |  9 +++++- tools/testing/selftests/ublk/kublk.c |  1 +

  3 files changed, 51 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +0 -0
index 2d5602ef05cc..fc8b87902f8f 100644
@@ -54,6 +54,7 @@
   #define UBLK_CMD_DEL_DEV_ASYNC      _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)

[snip]

Quoted 5 lines
drivers/block/ublk_drv.c +0 -0
@@ -919,6 +923,9 @@ static int ublk_open(struct gendisk *disk, 
 blk_mode_t mode)
                       return -EPERM;
       }
 
Quoted 7 lines

drivers/block/ublk_drv.c static int ublk_open(struct gendisk *disk, blk_mode_t mode) · line 928

Quoted 3 lines
drivers/block/ublk_drv.c +3 -0
+     if (ub->block_open)
+             return -EBUSY;
+

Why back to EBUSY?

This is an old patch version - v7 was already pushed with your comments applied(-ENXIO)