Skip to contentSkip to conversation

ublk: introduce UBLK_CMD_TRY_STOP_DEV

PATCH Superseded Yoav Cohen 2 participants 6 messages Jan 6

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Yoav Cohen

author
#0 lore raw

v3 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 v1:
- Address Ming Lei’s comments in patch 2:
- Add a feature flag and some minor comments.
- Patch 1 unchanged, keeps Reviewed-by

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

 drivers/block/ublk_drv.c      | 48 ++++++++++++++++++++++++++++++++---
 include/uapi/linux/ublk_cmd.h |  9 ++++++-
 2 files changed, 52 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
author
#1 lore raw

PATCH v3 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 v3 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      | 42 ++++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  9 +++++++-
 2 files changed, 49 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +41 -1
index 2d5602ef05cc..9291eab4c31f 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;
 }
 
@@ -3309,6 +3316,35 @@ 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..2b48c172542d 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
@@ -241,6 +242,12 @@
  */
 #define UBLK_F_UPDATE_SIZE		 (1ULL << 10)
 
+/*
+ * 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 << 11)
+
 /*
  * request buffer is registered automatically to uring_cmd's io_uring
  * context before delivering this io command to ublk server, meantime
Signature · 1 line
2.39.5 (Apple Git-154)
#3 lore raw

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

Quoted 27 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      | 42 ++++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  9 +++++++-
 2 files changed, 49 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +1 -0
index 2d5602ef05cc..9291eab4c31f 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)

drivers/block/ublk_drv.c line 78

Quoted 6 lines
drivers/block/ublk_drv.c +2 -1
@@ -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)

Should also be added to the list of automatic feature flags in ublk_ctrl_add_dev() (UBLK_F_CMD_IOCTL_ENCODE, ..., UBLK_F_BUF_REG_OFF_DAEMON).

Quoted 91 lines
drivers/block/ublk_drv.c +38 -0
 #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;
 }
 
@@ -3309,6 +3316,35 @@ 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 +2 -1
index ec77dabba45b..2b48c172542d 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

include/uapi/linux/ublk_cmd.h line 249

Quoted 8 lines
include/uapi/linux/ublk_cmd.h +5 -0
@@ -241,6 +242,12 @@
  */
 #define UBLK_F_UPDATE_SIZE              (1ULL << 10)
 
+/*
+ * 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 << 11)

This feature flag is already used for UBLK_F_AUTO_BUF_REG. Please use bit 17 or higher, as bits up to 14 are already assigned and bits 15 and 16 are allocated by in-flight patch sets.

The feature flag should also be added to the list of known ublk feature flags in cmd_dev_get_features() in tools/testing/selftests/ublk/kublk.c.

Best,
Caleb

Quoted 7 lines
include/uapi/linux/ublk_cmd.h +1 -1
+
 /*
  * request buffer is registered automatically to uring_cmd's io_uring
  * context before delivering this io command to ublk server, meantime
--

2.39.5 (Apple Git-154)

author
#4 lore raw

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

Quoted 146 lines · Caleb Sander Mateos

External email: Use caution opening links or attachments

Quoted 27 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      | 42 ++++++++++++++++++++++++++++++++++-
  include/uapi/linux/ublk_cmd.h |  9 +++++++-
  2 files changed, 49 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +1 -0
index 2d5602ef05cc..9291eab4c31f 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)

drivers/block/ublk_drv.c line 78

Quoted 6 lines
drivers/block/ublk_drv.c +2 -1
@@ -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)

Should also be added to the list of automatic feature flags in ublk_ctrl_add_dev() (UBLK_F_CMD_IOCTL_ENCODE, ..., UBLK_F_BUF_REG_OFF_DAEMON).

Quoted 91 lines
drivers/block/ublk_drv.c +38 -0
  #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;
  }
 
@@ -3309,6 +3316,35 @@ 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 +2 -1
index ec77dabba45b..2b48c172542d 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

include/uapi/linux/ublk_cmd.h line 249

Quoted 8 lines
include/uapi/linux/ublk_cmd.h +5 -0
@@ -241,6 +242,12 @@
   */
  #define UBLK_F_UPDATE_SIZE              (1ULL << 10)
 
+/*
+ * 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 << 11)

This feature flag is already used for UBLK_F_AUTO_BUF_REG. Please use bit 17 or higher, as bits up to 14 are already assigned and bits 15 and 16 are allocated by in-flight patch sets.

Quoted 14 lines

The feature flag should also be added to the list of known ublk feature flags in cmd_dev_get_features() in tools/testing/selftests/ublk/kublk.c.

Best,
Caleb

Quoted 7 lines
include/uapi/linux/ublk_cmd.h +1 -1
+
  /*
   * request buffer is registered automatically to uring_cmd's io_uring
   * context before delivering this io command to ublk server, meantime
--

2.39.5 (Apple Git-154)

Thank you for the comments, on which branch should I rebased my changes on?

Thank you,
Yoav

#5 lore raw

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

Quoted 155 lines · Yoav Cohen
Quoted 153 lines · Caleb Sander Mateos

External email: Use caution opening links or attachments

Quoted 27 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      | 42 ++++++++++++++++++++++++++++++++++-
  include/uapi/linux/ublk_cmd.h |  9 +++++++-
  2 files changed, 49 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +1 -0
index 2d5602ef05cc..9291eab4c31f 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)

drivers/block/ublk_drv.c line 78

Quoted 6 lines
drivers/block/ublk_drv.c +2 -1
@@ -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)

Should also be added to the list of automatic feature flags in ublk_ctrl_add_dev() (UBLK_F_CMD_IOCTL_ENCODE, ..., UBLK_F_BUF_REG_OFF_DAEMON).

Quoted 91 lines
drivers/block/ublk_drv.c +38 -0
  #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;
  }
 
@@ -3309,6 +3316,35 @@ 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 +2 -1
index ec77dabba45b..2b48c172542d 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

include/uapi/linux/ublk_cmd.h line 249

Quoted 8 lines
include/uapi/linux/ublk_cmd.h +5 -0
@@ -241,6 +242,12 @@
   */
  #define UBLK_F_UPDATE_SIZE              (1ULL << 10)
 
+/*
+ * 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 << 11)

This feature flag is already used for UBLK_F_AUTO_BUF_REG. Please use bit 17 or higher, as bits up to 14 are already assigned and bits 15 and 16 are allocated by in-flight patch sets.

The feature flag should also be added to the list of known ublk feature flags in cmd_dev_get_features() in tools/testing/selftests/ublk/kublk.c.

Best,
Caleb

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
include/uapi/linux/ublk_cmd.h +1 -1
+
  /*
   * request buffer is registered automatically to uring_cmd's io_uring
   * context before delivering this io command to ublk server, meantime
--

2.39.5 (Apple Git-154)

Thank you for the comments, on which branch should I rebased my changes on?

I would use block-6.19. It looks like it has 1 additional ublk commit on top of for-7.0/block. Feature flag bits 15 and 16 are used by the following patch series, but they can be rebased on yours once it lands:
UBLK_F_BATCH_IO:
https://lore.kernel.org/linux-block/[email protected]/T/#u
UBLK_F_NO_AUTO_PART_SCAN:
https://lore.kernel.org/linux-block/[email protected]/T/#u
UBLK_F_INTEGRITY:
https://lore.kernel.org/linux-block/[email protected]/T/#u

Best,
Caleb