Skip to contentSkip to conversation

ublk: fail to start/recover/del device if interrupted by signal

PATCH Applied Ming Lei 3 participants 7 messages Jul 26, 2023 – Jul 27, 2023

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v2 cover ublk: fail to start/recover/del device if interrupted by signal

Hi

The 1st & 2nd patch fixes kernel oops if user interrupts the current ublk task.

The 3rd patch returns -EINTR if user interrupts the device deletion.

V2:
- add patch 2&3, as reported by Stefano

Ming Lei (3):
ublk: fail to start device if queue setup is interrupted
ublk: fail to recover device if queue setup is interrupted
ublk: return -EINTR if breaking from waiting for existed users in
DEL_DEV

 drivers/block/ublk_drv.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.40.1
author
#1 lore raw

PATCH v2 1/3 ublk: fail to start device if queue setup is interrupted

In ublk_ctrl_start_dev(), if wait_for_completion_interruptible() is interrupted by signal, queues aren't setup successfully yet, so we have to fail UBLK_CMD_START_DEV, otherwise kernel oops can be triggered.

Reported by German when working on qemu-storage-deamon which requires single thread ublk daemon.

Fixes 71f28f3136af ("ublk_drv: add io_uring based userspace block driver")
---
 drivers/block/ublk_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +2 -1
index 1c823750c95a..7938221f4f7e 100644
@@ -1847,7 +1847,8 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)
 	if (ublksrv_pid <= 0)
 		return -EINVAL;
 
-	wait_for_completion_interruptible(&ub->completion);
+	if (wait_for_completion_interruptible(&ub->completion) != 0)
+		return -EINTR;
 
 	schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD);
 
Signature · 1 line
2.40.1
author
#2 lore raw

PATCH v2 2/3 ublk: fail to recover device if queue setup is interrupted

In reply to Ming Lei

In ublk_ctrl_end_recovery(), if wait_for_completion_interruptible() is interrupted by signal, queues aren't setup successfully yet, so we have to fail UBLK_CMD_END_USER_RECOVERY, otherwise kernel oops can be triggered.

Fixes c732a852b419 ("ublk_drv: add START_USER_RECOVERY and END_USER_RECOVERY support")
---
 drivers/block/ublk_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +3 -1
index 7938221f4f7e..9fcba3834e8d 100644
@@ -2324,7 +2324,9 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub,
 	pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
 	/* wait until new ubq_daemon sending all FETCH_REQ */
-	wait_for_completion_interruptible(&ub->completion);
+	if (wait_for_completion_interruptible(&ub->completion))
+		return -EINTR;
+
 	pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
 
Signature · 1 line
2.40.1
Reviewed-by on v2 2/3
#3 lore raw

Re: [PATCH V2 2/3] ublk: fail to recover device if queue setup is interrupted

Quoted 11 lines · Ming Lei

In ublk_ctrl_end_recovery(), if wait_for_completion_interruptible() is interrupted by signal, queues aren't setup successfully yet, so we have to fail UBLK_CMD_END_USER_RECOVERY, otherwise kernel oops can be triggered.

Fixes c732a852b419 ("ublk_drv: add START_USER_RECOVERY and END_USER_RECOVERY support")

---
drivers/block/ublk_drv.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 19 lines
drivers/block/ublk_drv.c +3 -1
index 7938221f4f7e..9fcba3834e8d 100644
@@ -2324,7 +2324,9 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub,
 	pr_devel("%s: Waiting for new ubq_daemons(nr: %d) are ready, dev id %d...\n",
 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
 	/* wait until new ubq_daemon sending all FETCH_REQ */
-	wait_for_completion_interruptible(&ub->completion);
+	if (wait_for_completion_interruptible(&ub->completion))
+		return -EINTR;
+
 	pr_devel("%s: All new ubq_daemons(nr: %d) are ready, dev id %d\n",
 			__func__, ub->dev_info.nr_hw_queues, header->dev_id);
 
Signature · 1 line
2.40.1
author
#4 lore raw

PATCH v2 3/3 ublk: return -EINTR if breaking from waiting for existed users in DEL_DEV

In reply to Ming Lei

If user interrupts wait_event_interruptible() in ublk_ctrl_del_dev(), return -EINTR and let user know what happens.

Fixes 0abe39dec065 ("block: ublk: improve handling device deletion")
---
 drivers/block/ublk_drv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +2 -2
index 9fcba3834e8d..21d2e71c5514 100644
@@ -2126,8 +2126,8 @@ static int ublk_ctrl_del_dev(struct ublk_device **p_ub)
 	 * - the device number is freed already, we will not find this
 	 *   device via ublk_get_device_from_id()
 	 */
-	wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx));
-
+	if (wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx)))
+		return -EINTR;
 	return 0;
 }
 
Signature · 1 line
2.40.1
Reviewed-by on v2 3/3
#5 lore raw

Re: [PATCH V2 3/3] ublk: return -EINTR if breaking from waiting for existed users in DEL_DEV

Quoted 9 lines · Ming Lei

If user interrupts wait_event_interruptible() in ublk_ctrl_del_dev(), return -EINTR and let user know what happens.

Fixes 0abe39dec065 ("block: ublk: improve handling device deletion")

---
drivers/block/ublk_drv.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 19 lines
drivers/block/ublk_drv.c +2 -2
index 9fcba3834e8d..21d2e71c5514 100644
@@ -2126,8 +2126,8 @@ static int ublk_ctrl_del_dev(struct ublk_device **p_ub)
 	 * - the device number is freed already, we will not find this
 	 *   device via ublk_get_device_from_id()
 	 */
-	wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx));
-
+	if (wait_event_interruptible(ublk_idr_wq, ublk_idr_freed(idx)))
+		return -EINTR;
 	return 0;
 }
 
Signature · 1 line
2.40.1
maintainer Applied
#6 lore raw
In reply to Ming Lei
Quoted 9 lines · Ming Lei

The 1st & 2nd patch fixes kernel oops if user interrupts the current ublk task.

The 3rd patch returns -EINTR if user interrupts the device deletion.

V2:
- add patch 2&3, as reported by Stefano

[...]

Applied, thanks!

[1/3] ublk: fail to start device if queue setup is interrupted

      commit: 53e7d08f6d6e214c40db1f51291bb2975c789dc2
[2/3] ublk: fail to recover device if queue setup is interrupted
      commit: 0c0cbd4ebc375ceebc75c89df04b74f215fab23a
[3/3] ublk: return -EINTR if breaking from waiting for existed users in DEL_DEV
      commit: 3e9dce80dbf91972aed972c743f539c396a34312

Best regards,

Signature · 1 line
Jens Axboe