ublk: introduce UBLK_CMD_TRY_STOP_DEV
Subsystems (3)
v5 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
Change since v4:
- Selftest added by Ming
- Move kublk.c change to new selftest commit
Ming Lei (1):
selftests: ublk: add stop command with --safe option
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/Makefile | 1 + tools/testing/selftests/ublk/kublk.c | 53 +++++++++++++++++ tools/testing/selftests/ublk/kublk.h | 1 + .../testing/selftests/ublk/test_generic_16.sh | 57 +++++++++++++++++++ 6 files changed, 165 insertions(+), 6 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH v5 1/3 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
@@ -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)
PATCH v5 2/3 ublk: add UBLK_CMD_TRY_STOP_DEV command
In reply to Yoav CohenThis 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 ++++++- 2 files changed, 50 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +42 -2
@@ -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
@@ -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
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH v5 2/3] ublk: add UBLK_CMD_TRY_STOP_DEV command
Hello Yoav,
Quoted 4 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-EBUSYinstead of disrupting active clients. This allows safe, non-destructive stopping.
This description is either wrong or the implementation is wrong.
It is missing that fact that after try fails any further opens will
fail with EBUSY, what if an active opener wants to open one more time or reopen - it will be disrupted - is this intended behaviour?
A beleive that a TRY interface is not expected to have side effects,
so might be ublk_ctrl_graceful_stop_dev would be a better name in that case. But in anyway it must be specified in the commit message what it does and why.
Quoted 17 lines
Advertise
UBLK_CMD_TRY_STOP_DEVsupport viaUBLK_F_SAFE_STOP_DEVfeature flag.Signed-off-by Yoav Cohen <[email protected]>Reviewed-by Ming Lei <[email protected]>--- drivers/block/ublk_drv.c | 44 +++++++++++++++++++++++++++++++++-- include/uapi/linux/ublk_cmd.h | 9 ++++++- 2 files changed, 50 insertions(+), 3 deletions(-)drivers/block/ublk_drv.c +0
-0@@ -54,6 +54,7 @@#define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)
[snip]
Quoted 11 lines
drivers/block/ublk_drv.c +3
-0@@ -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 ; }
[snip]
Quoted 28 lines
drivers/block/ublk_drv.c +28
-0+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; +} +
[snip]
Signature · 2 lines
have fun, alex
Re: [PATCH v5 2/3] ublk: add UBLK_CMD_TRY_STOP_DEV command
Quoted 81 lines · Alexander Atanasov
External email: Use caution opening links or attachments
Hello Yoav,
Quoted 4 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-EBUSYinstead of disrupting active clients. This allows safe, non-destructive stopping.This description is either wrong or the implementation is wrong.
It is missing that fact that after try fails any further opens will fail with EBUSY, what if an active opener wants to open one more time or reopen - it will be disrupted - is this intended behaviour?
A beleive that a TRY interface is not expected to have side effects, so might be
ublk_ctrl_graceful_stop_devwould be a better name in that case. But in anyway it must be specified in the commit message what it does and why.Quoted 17 lines
Advertise
UBLK_CMD_TRY_STOP_DEVsupport viaUBLK_F_SAFE_STOP_DEVfeature flag.Signed-off-by Yoav Cohen <[email protected]>Reviewed-by Ming Lei <[email protected]>---
drivers/block/ublk_drv.c | 44 +++++++++++++++++++++++++++++++++--include/uapi/linux/ublk_cmd.h| 9 ++++++-2 files changed, 50 insertions(+), 3 deletions(-)drivers/block/ublk_drv.c +0
-0@@ -54,6 +54,7 @@ #define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC)[snip]
Quoted 12 lines
drivers/block/ublk_drv.c +3
-0@@ -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; }[snip]
Quoted 22 lines
drivers/block/ublk_drv.c +22
-0+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:
Quoted 13 lines
Quoted 6 lines
+
mutex_unlock(&disk->open_mutex);drivers/block/ublk_drv.c +5
-0+out: + ublk_put_disk(disk); + return ret; +} +[snip]
Signature · 2 lines
have fun, alex
Thank you for the reply, but I’m not sure I’m following.
Please note that this commit only prevents new opens if disk_openers was already zeroed. In that case, ublk_ctrl_stop_dev() will be called later.
The ub->block_open flag is used solely to prevent a race with a task that has already started opening the device but has not yet managed to acquire open_mutex.
The test Ming added demonstrates this behavior as well.
There are no side effects in this change; it is effectively a best-effort (try) mechanism only.
Thanks,
Yoav Cohen
PATCH v5 3/3 selftests: ublk: add stop command with --safe option
In reply to Yoav CohenFrom: Ming Lei <[email protected]>
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 | 53 +++++++++++++++++ tools/testing/selftests/ublk/kublk.h | 1 + .../testing/selftests/ublk/test_generic_16.sh | 57 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh
tools/testing/selftests/ublk/Makefile +1 -0
@@ -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 +53 -0
95 lines not on this page · raw message
tools/testing/selftests/ublk/kublk.h +1 -0
@@ -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.39.5 (Apple Git-154)