From patchwork Wed Aug 31 15:51:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D5BFECAAD4 for ; Wed, 31 Aug 2022 15:53:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232156AbiHaPw7 (ORCPT ); Wed, 31 Aug 2022 11:52:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232078AbiHaPws (ORCPT ); Wed, 31 Aug 2022 11:52:48 -0400 Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55F20AB4C7; Wed, 31 Aug 2022 08:52:44 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045192;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdnqz_1661961149; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdnqz_1661961149) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:52:41 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 1/6] ublk_drv: check 'current' instead of 'ubq_daemon' Date: Wed, 31 Aug 2022 23:51:31 +0800 Message-Id: <20220831155136.23434-2-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org This check is not atomic. So with recovery feature, ubq_daemon may be modified simultaneously by recovery task. Instead, check 'current' is safe here because 'current' never changes. Also add comment explaining this check, which is really important for understanding recovery feature. Signed-off-by: ZiyangZhang Reviewed-by: Ming Lei --- drivers/block/ublk_drv.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 6a4a94b4cdf4..c39b67d7133d 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -645,14 +645,22 @@ static inline void __ublk_rq_task_work(struct request *req) struct ublk_device *ub = ubq->dev; int tag = req->tag; struct ublk_io *io = &ubq->ios[tag]; - bool task_exiting = current != ubq->ubq_daemon || ubq_daemon_is_dying(ubq); unsigned int mapped_bytes; pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n", __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags, ublk_get_iod(ubq, req->tag)->addr); - if (unlikely(task_exiting)) { + /* + * Task is exiting if either: + * + * (1) current != ubq_daemon. + * io_uring_cmd_complete_in_task() tries to run task_work + * in a workqueue if ubq_daemon(cmd's task) is PF_EXITING. + * + * (2) current->flags & PF_EXITING. + */ + if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) { blk_mq_end_request(req, BLK_STS_IOERR); mod_delayed_work(system_wq, &ub->monitor_work, 0); return; From patchwork Wed Aug 31 15:51:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960988 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1218C0502A for ; Wed, 31 Aug 2022 15:54:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232187AbiHaPyI (ORCPT ); Wed, 31 Aug 2022 11:54:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231249AbiHaPx4 (ORCPT ); Wed, 31 Aug 2022 11:53:56 -0400 Received: from out30-57.freemail.mail.aliyun.com (out30-57.freemail.mail.aliyun.com [115.124.30.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C818DAE231; Wed, 31 Aug 2022 08:53:54 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdntv_1661961162; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdntv_1661961162) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:53:49 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 2/6] ublk_drv: refactor ublk_cancel_queue() Date: Wed, 31 Aug 2022 23:51:32 +0800 Message-Id: <20220831155136.23434-3-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Assume only a few FETCH_REQ ioucmds are sent to ublk_drv, then the ubq_daemon exits, We have to call io_uring_cmd_done() for all ioucmds received so that io_uring ctx will not leak. ublk_cancel_queue() may be called before START_DEV or after STOP_DEV, we decrease ubq->nr_io_ready and clear UBLK_IO_FLAG_ACTIVE so that we won't call io_uring_cmd_done() twice for one ioucmd to avoid UAF. Also clearing UBLK_IO_FLAG_ACTIVE makes the code more reasonable. Signed-off-by: ZiyangZhang Reviewed-by: Ming Lei --- drivers/block/ublk_drv.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index c39b67d7133d..0c6db0978ed0 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -963,22 +963,39 @@ static inline bool ublk_queue_ready(struct ublk_queue *ubq) return ubq->nr_io_ready == ubq->q_depth; } +/* If ublk_cancel_queue() is called before sending START_DEV(), ->mutex + * provides protection on above update. + * + * If ublk_cancel_queue() is called after sending START_DEV(), disk is + * deleted first, UBLK_IO_RES_ABORT is returned so that any new io + * command can't be issued to driver, so updating on io flags and + * nr_io_ready is safe here. + * + * Also ->nr_io_ready is guaranteed to become zero after ublk_cance_queue() + * returns since request queue is either frozen or not present in both two + * cases. + */ static void ublk_cancel_queue(struct ublk_queue *ubq) { int i; - if (!ublk_queue_ready(ubq)) + if (!ubq->nr_io_ready) return; for (i = 0; i < ubq->q_depth; i++) { struct ublk_io *io = &ubq->ios[i]; - if (io->flags & UBLK_IO_FLAG_ACTIVE) + if (io->flags & UBLK_IO_FLAG_ACTIVE) { + pr_devel("%s: done old cmd: qid %d tag %d\n", + __func__, ubq->q_id, i); io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0); + io->flags &= ~UBLK_IO_FLAG_ACTIVE; + ubq->nr_io_ready--; + } } /* all io commands are canceled */ - ubq->nr_io_ready = 0; + WARN_ON_ONCE(ubq->nr_io_ready); } /* Cancel all pending commands, must be called after del_gendisk() returns */ From patchwork Wed Aug 31 15:51:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960989 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C52FECAAD4 for ; Wed, 31 Aug 2022 15:54:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231377AbiHaPyX (ORCPT ); Wed, 31 Aug 2022 11:54:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbiHaPyL (ORCPT ); Wed, 31 Aug 2022 11:54:11 -0400 Received: from out30-56.freemail.mail.aliyun.com (out30-56.freemail.mail.aliyun.com [115.124.30.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57C24BCC24; Wed, 31 Aug 2022 08:54:05 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R361e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdoBn_1661961229; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdoBn_1661961229) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:54:02 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 3/6] ublk_drv: define macros for recovery feature and check them Date: Wed, 31 Aug 2022 23:51:33 +0800 Message-Id: <20220831155136.23434-4-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Define some macros for recovery feature. Especially define a new state: UBLK_S_DEV_RECOVERING which implies the ublk_device is recovering. UBLK_F_USER_RECOVERY implies that: (1) ublk_drv enables recovery feature. It won't let monitor_work to automatically abort rqs and release the device. Instead, it waits for user's START_USER_RECOVERY ctrl-cmd. (2) In monitor_work after a crash, ublk_drv ends(aborts) rqs issued to userspace(ublksrv) before crash. (3) In task work and ublk_queue_rq() after a crash, ublk_drv requeues rqs dispatched after crash. UBLK_F_USER_RECOVERY_REISSUE implies that: (1) everything UBLK_F_USER_RECOVERY implies except (2) ublk_drv requeues rqs issued to userspace(ublksrv) before crash. UBLK_F_USER_RECOVERY_REISSUE is designed for backends which: (1) tolerate double-writes because we may issue the same rq twice. (2) cannot let frontend users get I/O error, such as a RDONLY system. Signed-off-by: ZiyangZhang --- drivers/block/ublk_drv.c | 31 ++++++++++++++++++++++++++++++- include/uapi/linux/ublk_cmd.h | 7 +++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 0c6db0978ed0..0c3d32e8d686 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -49,7 +49,9 @@ /* All UBLK_F_* have to be included into UBLK_F_ALL */ #define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \ | UBLK_F_URING_CMD_COMP_IN_TASK \ - | UBLK_F_NEED_GET_DATA) + | UBLK_F_NEED_GET_DATA \ + | UBLK_F_USER_RECOVERY \ + | UBLK_F_USER_RECOVERY_REISSUE) /* All UBLK_PARAM_TYPE_* should be included here */ #define UBLK_PARAM_TYPE_ALL (UBLK_PARAM_TYPE_BASIC | UBLK_PARAM_TYPE_DISCARD) @@ -323,6 +325,33 @@ static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id) PAGE_SIZE); } +/* + * TODO: UBLK_F_USER_RECOVERY should be a flag for device, not for queue, + * since "some queues are aborted while others are recoverd" is really weird. + */ +static inline bool ublk_can_use_recovery(struct ublk_device *ub) +{ + struct ublk_queue *ubq = ublk_get_queue(ub, 0); + + if (ubq->flags & UBLK_F_USER_RECOVERY) + return true; + return false; +} + +/* + * TODO: UBLK_F_USER_RECOVERY_REISSUE should be a flag for device, not for queue, + * since "some queues are aborted while others are recoverd" is really weird. + */ +static inline bool ublk_can_use_recovery_reissue(struct ublk_device *ub) +{ + struct ublk_queue *ubq = ublk_get_queue(ub, 0); + + if ((ubq->flags & UBLK_F_USER_RECOVERY) && + (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE)) + return true; + return false; +} + static void ublk_free_disk(struct gendisk *disk) { struct ublk_device *ub = disk->private_data; diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index 677edaab2b66..7f7e6f44cec5 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -17,6 +17,8 @@ #define UBLK_CMD_STOP_DEV 0x07 #define UBLK_CMD_SET_PARAMS 0x08 #define UBLK_CMD_GET_PARAMS 0x09 +#define UBLK_CMD_START_USER_RECOVERY 0x10 +#define UBLK_CMD_END_USER_RECOVERY 0x11 /* * IO commands, issued by ublk server, and handled by ublk driver. @@ -74,9 +76,14 @@ */ #define UBLK_F_NEED_GET_DATA (1UL << 2) +#define UBLK_F_USER_RECOVERY (1UL << 3) + +#define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4) + /* device state */ #define UBLK_S_DEV_DEAD 0 #define UBLK_S_DEV_LIVE 1 +#define UBLK_S_DEV_RECOVERING 2 /* shipped via sqe->cmd of io_uring command */ struct ublksrv_ctrl_cmd { From patchwork Wed Aug 31 15:51:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960990 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4C5AECAAD4 for ; Wed, 31 Aug 2022 15:54:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232200AbiHaPyk (ORCPT ); Wed, 31 Aug 2022 11:54:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232221AbiHaPy1 (ORCPT ); Wed, 31 Aug 2022 11:54:27 -0400 Received: from out30-44.freemail.mail.aliyun.com (out30-44.freemail.mail.aliyun.com [115.124.30.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF5F1B6D30; Wed, 31 Aug 2022 08:54:24 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R601e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdoG0_1661961242; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdoG0_1661961242) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:54:21 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 4/6] ublk_drv: requeue rqs with recovery feature enabled Date: Wed, 31 Aug 2022 23:51:34 +0800 Message-Id: <20220831155136.23434-5-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org With recovery feature enabled, in ublk_queue_rq or task work (in exit_task_work or fallback wq), we requeue rqs instead of ending(aborting) them. No matter recovery feature is enabled or disabled, schedule monitor work immediately after detecting a crash so it can find out the crash and do aborting/recovery mechanism. Signed-off-by: ZiyangZhang --- drivers/block/ublk_drv.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 0c3d32e8d686..296b9d80f003 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -690,7 +690,16 @@ static inline void __ublk_rq_task_work(struct request *req) * (2) current->flags & PF_EXITING. */ if (unlikely(current != ubq->ubq_daemon || current->flags & PF_EXITING)) { - blk_mq_end_request(req, BLK_STS_IOERR); + pr_devel("%s: %s q_id %d tag %d io_flags %x.\n", __func__, + (ublk_can_use_recovery(ub)) ? "requeue" : "abort", + ubq->q_id, req->tag, io->flags); + + if (ublk_can_use_recovery(ub)) { + /* We cannot process this req so just requeue it. */ + blk_mq_requeue_request(req, false); + } else { + blk_mq_end_request(req, BLK_STS_IOERR); + } mod_delayed_work(system_wq, &ub->monitor_work, 0); return; } @@ -770,6 +779,7 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, { struct ublk_queue *ubq = hctx->driver_data; struct request *rq = bd->rq; + struct ublk_io *io = &ubq->ios[rq->tag]; blk_status_t res; /* fill iod to slot in io cmd buffer */ @@ -781,8 +791,18 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, if (unlikely(ubq_daemon_is_dying(ubq))) { fail: + pr_devel("%s: %s q_id %d tag %d io_flags %x.\n", __func__, + (ublk_can_use_recovery(ubq->dev)) ? "requeue" : "abort", + ubq->q_id, rq->tag, io->flags); + mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0); - return BLK_STS_IOERR; + if (ublk_can_use_recovery(ubq->dev)) { + /* We cannot process this rq so just requeue it. */ + blk_mq_requeue_request(rq, false); + return BLK_STS_OK; + } else { + return BLK_STS_IOERR; + } } if (ublk_can_use_task_work(ubq)) { @@ -793,7 +813,6 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, if (task_work_add(ubq->ubq_daemon, &data->work, notify_mode)) goto fail; } else { - struct ublk_io *io = &ubq->ios[rq->tag]; struct io_uring_cmd *cmd = io->cmd; struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd); From patchwork Wed Aug 31 15:51:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8093EC0502A for ; Wed, 31 Aug 2022 15:54:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230151AbiHaPym (ORCPT ); Wed, 31 Aug 2022 11:54:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232259AbiHaPyc (ORCPT ); Wed, 31 Aug 2022 11:54:32 -0400 Received: from out30-43.freemail.mail.aliyun.com (out30-43.freemail.mail.aliyun.com [115.124.30.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E996CBD1D9; Wed, 31 Aug 2022 08:54:28 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045168;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdoMN_1661961261; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdoMN_1661961261) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:54:24 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 5/6] ublk_drv: consider recovery feature in aborting mechanism Date: Wed, 31 Aug 2022 23:51:35 +0800 Message-Id: <20220831155136.23434-6-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org We change the default behavior of aborting machenism. Now monitor_work will not be manually scheduled by ublk_queue_rq() or task_work after a ubq_daemon or process is dying(PF_EXITING). The monitor work should find a dying ubq_daemon or a crash process by itself. Then, it can start the aborting machenism. We do such modification is because we want to strictly separate the STOP_DEV procedure and monitor_work. More specifically, we ensure that monitor_work must not be scheduled after we start deleting gendisk and ending(aborting) all inflight rqs. In this way we are easy to consider recovery feature and unify it into existing aborting mechanism. Really we do not want too many "if can_use_recovery" checks. With recovery feature disabled and after a ubq_daemon crash: (1) monitor_work notices the crash and schedules stop_work (2) stop_work calls ublk_stop_dev() (3) In ublk_stop_dev(): (a) It sets 'force_abort', which prevents new rqs in ublk_queue_rq(); If ublk_queue_rq() does not see it, rqs can still be ended(aborted) in fallback wq. (b) Then it cancels monitor_work; (c) Then it schedules abort_work which ends(aborts) all inflight rqs. (d) At the same time del_gendisk() is called. (e) Finally, we complete all ioucmds. Note: we do not change the existing behavior with reocvery disabled. Note that STOP_DEV ctrl-cmd can be processed without reagrd to monitor_work. With recovery feature enabled and after a process crash: (1) monitor_work notices the crash and all ubq_daemon are dying. We do not consider a "single" ubq_daemon(pthread) crash. Please send STOP_DEV ctrl-cmd which calling ublk_stop_dev() for this case. (2) The monitor_work quiesces request queue. (3) The monotor_work checks if there is any inflight rq with UBLK_IO_FLAG_ACTIVE unset. If so, we give up and schedule monitor_work later to retry. This is because we have to wait these rqs requeued(IDLE) and we are safe to complete their ioucmds later. Otherwise we may cause UAF on ioucmd in fallback wq. (4) If check in (3) passes, we should requeue/abort inflight rqs issued to the crash ubq_daemon before. If UBLK_F_USER_RECOVERY_REISSUE is set, rq is requeued. Otherwise it is aborted. (5) All ioucmds are completed by calling io_uring_cmd_done(). (6) monitor_work set ub's state to UBLK_S_DEV_RECOVERING. It does not scheduled itself anymore. Now we are ready for START_USER_RECOVERY. Note: If (3) fails, monitor_work schedules itself and retires (3). We allow user to manually start STOP_DEV procedure without reagrd to monitor_work. STOP_DEV can cancel monitor_work, unquiesce request queue and drain all requeued rqs. More importantly, STOP_DEV can safely complete all ioucmds since monitor_work has been canceled at that moment. Signed-off-by: ZiyangZhang --- drivers/block/ublk_drv.c | 222 +++++++++++++++++++++++++++++++++++---- 1 file changed, 202 insertions(+), 20 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 296b9d80f003..0e185d1fa2c4 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -156,7 +156,7 @@ struct ublk_device { struct completion completion; unsigned int nr_queues_ready; - atomic_t nr_aborted_queues; + bool force_abort; /* * Our ubq->daemon may be killed without any notification, so @@ -164,6 +164,7 @@ struct ublk_device { */ struct delayed_work monitor_work; struct work_struct stop_work; + struct work_struct abort_work; }; /* header of ublk_params */ @@ -643,9 +644,13 @@ static void ublk_complete_rq(struct request *req) */ static void __ublk_fail_req(struct ublk_io *io, struct request *req) { + struct ublk_queue *ubq = req->mq_hctx->driver_data; + WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE); if (!(io->flags & UBLK_IO_FLAG_ABORTED)) { + pr_devel("%s: abort rq: qid %d tag %d io_flags %x\n", + __func__, ubq->q_id, req->tag, io->flags); io->flags |= UBLK_IO_FLAG_ABORTED; blk_mq_end_request(req, BLK_STS_IOERR); } @@ -664,6 +669,8 @@ static void ubq_complete_io_cmd(struct ublk_io *io, int res) /* tell ublksrv one io request is coming */ io_uring_cmd_done(io->cmd, res, 0); + pr_devel("%s: complete ioucmd: res %d io_flags %x\n", + __func__, res, io->flags); } #define UBLK_REQUEUE_DELAY_MS 3 @@ -675,11 +682,6 @@ static inline void __ublk_rq_task_work(struct request *req) int tag = req->tag; struct ublk_io *io = &ubq->ios[tag]; unsigned int mapped_bytes; - - pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n", - __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags, - ublk_get_iod(ubq, req->tag)->addr); - /* * Task is exiting if either: * @@ -700,10 +702,13 @@ static inline void __ublk_rq_task_work(struct request *req) } else { blk_mq_end_request(req, BLK_STS_IOERR); } - mod_delayed_work(system_wq, &ub->monitor_work, 0); return; } + pr_devel("%s: complete: op %d, qid %d tag %d io_flags %x addr %llx\n", + __func__, io->cmd->cmd_op, ubq->q_id, req->tag, io->flags, + ublk_get_iod(ubq, req->tag)->addr); + if (ublk_need_get_data(ubq) && (req_op(req) == REQ_OP_WRITE || req_op(req) == REQ_OP_FLUSH)) { @@ -782,6 +787,11 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, struct ublk_io *io = &ubq->ios[rq->tag]; blk_status_t res; + if (unlikely(ubq->dev->force_abort)) { + pr_devel("%s: abort q_id %d tag %d io_flags %x.\n", + __func__, ubq->q_id, rq->tag, io->flags); + return BLK_STS_IOERR; + } /* fill iod to slot in io cmd buffer */ res = ublk_setup_iod(ubq, rq); if (unlikely(res != BLK_STS_OK)) @@ -789,13 +799,15 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, blk_mq_start_request(bd->rq); + pr_devel("%s: start rq: q_id %d tag %d io_flags %x.\n", + __func__, ubq->q_id, rq->tag, io->flags); + if (unlikely(ubq_daemon_is_dying(ubq))) { fail: pr_devel("%s: %s q_id %d tag %d io_flags %x.\n", __func__, (ublk_can_use_recovery(ubq->dev)) ? "requeue" : "abort", ubq->q_id, rq->tag, io->flags); - mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0); if (ublk_can_use_recovery(ubq->dev)) { /* We cannot process this rq so just requeue it. */ blk_mq_requeue_request(rq, false); @@ -880,6 +892,7 @@ static int ublk_ch_open(struct inode *inode, struct file *filp) if (test_and_set_bit(UB_STATE_OPEN, &ub->state)) return -EBUSY; filp->private_data = ub; + pr_devel("%s: open /dev/ublkc%d\n", __func__, ub->dev_info.dev_id); return 0; } @@ -888,6 +901,7 @@ static int ublk_ch_release(struct inode *inode, struct file *filp) struct ublk_device *ub = filp->private_data; clear_bit(UB_STATE_OPEN, &ub->state); + pr_devel("%s: release /dev/ublkc%d\n", __func__, ub->dev_info.dev_id); return 0; } @@ -971,37 +985,180 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq) * will do it */ rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i); - if (rq) + if (rq && blk_mq_request_started(rq)) __ublk_fail_req(io, rq); } } ublk_put_device(ub); } -static void ublk_daemon_monitor_work(struct work_struct *work) + + +static void ublk_abort_work_fn(struct work_struct *work) { struct ublk_device *ub = - container_of(work, struct ublk_device, monitor_work.work); + container_of(work, struct ublk_device, abort_work); + + int i; + + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + struct ublk_queue *ubq = ublk_get_queue(ub, i); + + if (ubq_daemon_is_dying(ubq)) + ublk_abort_queue(ub, ubq); + } + + if (ub->force_abort) + schedule_work(&ub->abort_work); +} + +static void ublk_reinit_queue(struct ublk_device *ub, + struct ublk_queue *ubq) +{ + int i; + + for (i = 0; i < ubq->q_depth; i++) { + struct ublk_io *io = &ubq->ios[i]; + + if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) { + struct request *rq = blk_mq_tag_to_rq( + ub->tag_set.tags[ubq->q_id], i); + + WARN_ON_ONCE(!rq); + pr_devel("%s: %s rq: qid %d tag %d io_flags %x\n", + __func__, + ublk_can_use_recovery_reissue(ub) ? "requeue" : "abort", + ubq->q_id, i, io->flags); + if (ublk_can_use_recovery_reissue(ub)) + blk_mq_requeue_request(rq, false); + else + __ublk_fail_req(io, rq); + + } else { + io_uring_cmd_done(io->cmd, + UBLK_IO_RES_ABORT, 0); + io->flags &= ~UBLK_IO_FLAG_ACTIVE; + pr_devel("%s: send UBLK_IO_RES_ABORT: qid %d tag %d io_flags %x\n", + __func__, ubq->q_id, i, io->flags); + } + ubq->nr_io_ready--; + } + WARN_ON_ONCE(ubq->nr_io_ready); +} + +static bool ublk_check_inflight_rq(struct request *rq, void *data) +{ + struct ublk_queue *ubq = rq->mq_hctx->driver_data; + struct ublk_io *io = &ubq->ios[rq->tag]; + bool *busy = data; + + if (io->flags & UBLK_IO_FLAG_ACTIVE) { + *busy = true; + return false; + } + return true; +} + +static void ublk_reinit_dev(struct ublk_device *ub) +{ + int i; + bool busy = false; + + if (!ublk_get_device(ub)) + return; + + /* If we have quiesced q, all ubq_daemons are dying */ + if (blk_queue_quiesced(ub->ub_disk->queue)) + goto check_inflight; + + /* Recovery feature is for 'process' crash. */ + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + struct ublk_queue *ubq = ublk_get_queue(ub, i); + + if (!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq))) + goto out; + } + + pr_devel("%s: all ubq_daemons(nr: %d) are dying.\n", + __func__, ub->dev_info.nr_hw_queues); + + /* Now all ubq_daemons are PF_EXITING, let's quiesce q. */ + blk_mq_quiesce_queue(ub->ub_disk->queue); + pr_devel("%s: queue quiesced.\n", __func__); + check_inflight: + /* All rqs have to be requeued(and stay in queue now) */ + blk_mq_tagset_busy_iter(&ub->tag_set, ublk_check_inflight_rq, &busy); + if (busy) { + pr_devel("%s: still some inflight rqs, retry later...\n", + __func__); + goto out; + } + + pr_devel("%s: all inflight rqs are requeued.\n", __func__); + + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + struct ublk_queue *ubq = ublk_get_queue(ub, i); + + ublk_reinit_queue(ub, ubq); + } + /* So monitor_work won't be scheduled anymore */ + ub->dev_info.state = UBLK_S_DEV_RECOVERING; + pr_devel("%s: convert state to UBLK_S_DEV_RECOVERING\n", + __func__); + out: + ublk_put_device(ub); +} + +static void ublk_kill_dev(struct ublk_device *ub) +{ int i; for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { struct ublk_queue *ubq = ublk_get_queue(ub, i); if (ubq_daemon_is_dying(ubq)) { + pr_devel("%s: ubq(%d) is dying, schedule stop_work now\n", + __func__, i); schedule_work(&ub->stop_work); - - /* abort queue is for making forward progress */ - ublk_abort_queue(ub, ubq); } } +} + +static void ublk_daemon_monitor_work(struct work_struct *work) +{ + struct ublk_device *ub = + container_of(work, struct ublk_device, monitor_work.work); + + pr_devel("%s: mode(%s) running...\n", + __func__, + ublk_can_use_recovery(ub) ? "recovery" : "kill"); + /* + * We can't schedule monitor work if: + * (1) The state is DEAD. + * The gendisk is not alive, so either all rqs are ended + * or request queue is not created. + * + * (2) The state is RECOVERYING. + * The process crashed, all rqs were requeued and request queue + * was quiesced. + */ + WARN_ON_ONCE(ub->dev_info.state != UBLK_S_DEV_LIVE); + if (ublk_can_use_recovery(ub)) + ublk_reinit_dev(ub); + else + ublk_kill_dev(ub); /* - * We can't schedule monitor work after ublk_remove() is started. + * No need ub->mutex, monitor work are canceled after ub is marked + * as force_abort which is observed reliably. + * + * Note: + * All incoming rqs are aborted in ublk_queue_rq ASAP. Then + * we will hang on del_gendisk() and wait for all inflight rqs' + * completion. * - * No need ub->mutex, monitor work are canceled after state is marked - * as DEAD, so DEAD state is observed reliably. */ - if (ub->dev_info.state != UBLK_S_DEV_DEAD) + if (ub->dev_info.state == UBLK_S_DEV_LIVE && !ub->force_abort) schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD); } @@ -1058,10 +1215,35 @@ static void ublk_cancel_dev(struct ublk_device *ub) static void ublk_stop_dev(struct ublk_device *ub) { mutex_lock(&ub->mutex); - if (ub->dev_info.state != UBLK_S_DEV_LIVE) + if (ub->dev_info.state == UBLK_S_DEV_DEAD) goto unlock; + /* + * All incoming rqs are aborted in ublk_queue_rq ASAP. Then + * we will hang on del_gendisk() and wait for all inflight rqs' + * completion. + */ + ub->force_abort = true; + /* wait until monitor_work is not scheduled */ + cancel_delayed_work_sync(&ub->monitor_work); + pr_devel("%s: monitor work is canceled.\n", __func__); + /* unquiesce q and let all inflight rqs' be aborted */ + if (blk_queue_quiesced(ub->ub_disk->queue)) { + blk_mq_unquiesce_queue(ub->ub_disk->queue); + pr_devel("%s: queue unquiesced.\n", __func__); + } + /* requeued requests will be aborted ASAP because of 'force_abort' */ + blk_mq_kick_requeue_list(ub->ub_disk->queue); + /* forward progress */ + schedule_work(&ub->abort_work); + pr_devel("%s: abort work is scheduled, start delete gendisk...\n", + __func__); + pr_devel("%s: gendisk is deleted.\n", __func__); del_gendisk(ub->ub_disk); + pr_devel("%s: gendisk is deleted.\n", __func__); + ub->force_abort = false; + cancel_work_sync(&ub->abort_work); + pr_devel("%s: abort work is canceled.\n", __func__); ub->dev_info.state = UBLK_S_DEV_DEAD; ub->dev_info.ublksrv_pid = -1; put_disk(ub->ub_disk); @@ -1069,7 +1251,6 @@ static void ublk_stop_dev(struct ublk_device *ub) unlock: ublk_cancel_dev(ub); mutex_unlock(&ub->mutex); - cancel_delayed_work_sync(&ub->monitor_work); } /* device can only be started after all IOs are ready */ @@ -1560,6 +1741,7 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) goto out_unlock; mutex_init(&ub->mutex); spin_lock_init(&ub->mm_lock); + INIT_WORK(&ub->abort_work, ublk_abort_work_fn); INIT_WORK(&ub->stop_work, ublk_stop_work_fn); INIT_DELAYED_WORK(&ub->monitor_work, ublk_daemon_monitor_work); From patchwork Wed Aug 31 15:51:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ziyang Zhang X-Patchwork-Id: 12960992 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC2BEECAAD4 for ; Wed, 31 Aug 2022 15:54:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230181AbiHaPy6 (ORCPT ); Wed, 31 Aug 2022 11:54:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231419AbiHaPyn (ORCPT ); Wed, 31 Aug 2022 11:54:43 -0400 Received: from out30-43.freemail.mail.aliyun.com (out30-43.freemail.mail.aliyun.com [115.124.30.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C98E1AB4FE; Wed, 31 Aug 2022 08:54:41 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=ziyangzhang@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VNrdoMu_1661961264; Received: from localhost.localdomain(mailfrom:ZiyangZhang@linux.alibaba.com fp:SMTPD_---0VNrdoMu_1661961264) by smtp.aliyun-inc.com; Wed, 31 Aug 2022 23:54:37 +0800 From: ZiyangZhang To: ming.lei@redhat.com, axboe@kernel.dk Cc: xiaoguang.wang@linux.alibaba.com, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, joseph.qi@linux.alibaba.com, ZiyangZhang Subject: [RFC PATCH V2 6/6] ublk_drv: add START_USER_RECOVERY and END_USER_RECOVERY support Date: Wed, 31 Aug 2022 23:51:36 +0800 Message-Id: <20220831155136.23434-7-ZiyangZhang@linux.alibaba.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> References: <20220831155136.23434-1-ZiyangZhang@linux.alibaba.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org START_USER_RECOVERY and END_USER_RECOVERY are two new control commands to support user recovery feature. After a crash, user should send START_USER_RECOVERY, it will: (1) check if (a)current ublk_device is UBLK_S_DEV_RECOVERING which was set by monitor_work and (b)the file struct is released. We always expect crash of the ublksrv 'process', not exit of a single ubq_daemon 'pthread'. (2) reinit all ubqs, including: (a) put the dying task(thread) and reset ->ubq_daemon to NULL. (b) reset all ublk_io. (3) reset ub->mm to NULL. Then, user should start a new 'process' and send FETCH_REQ on each ubq_daemon. Finally, user should send END_USER_RECOVERY, it will: (1) wait for all new ubq_daemons getting ready. (2) unquiesce the request queue and expect incoming ublk_queue_rq() (3) convert state to UBLK_S_DEV_LIVE (4) schedule monitor_work again Signed-off-by: ZiyangZhang --- drivers/block/ublk_drv.c | 130 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 0e185d1fa2c4..2d1f3e032606 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -1964,6 +1964,130 @@ static int ublk_ctrl_set_params(struct io_uring_cmd *cmd) return ret; } +static void ublk_queue_start_recovery(struct ublk_device *ub, struct ublk_queue *ubq) +{ + int i; + + /* All old ioucmds have to be completed/canceled by io_uring_cmd_done(). */ + WARN_ON_ONCE(ubq->nr_io_ready); + + /* old daemon is PF_EXITING, put it now */ + put_task_struct(ubq->ubq_daemon); + /* have to set it to NULL, otherwise ub won't accept new FETCH_REQ */ + ubq->ubq_daemon = NULL; + + for (i = 0; i < ubq->q_depth; i++) { + struct ublk_io *io = &ubq->ios[i]; + + /* forget everything now and be ready for new FETCH_REQ */ + io->flags = 0; + io->cmd = NULL; + io->addr = 0; + } +} + +static int ublk_ctrl_start_recovery(struct io_uring_cmd *cmd) +{ + struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; + struct ublk_device *ub; + int ret = -EINVAL; + int i; + + ub = ublk_get_device_from_id(header->dev_id); + if (!ub) + return ret; + + mutex_lock(&ub->mutex); + + if (!ublk_can_use_recovery(ub)) + goto out_unlock; + + /* + * START_RECOVERY is only allowd after: + * + * (1) UB_STATE_OPEN is not set, which means the dying process is exited + * and related io_uring ctx is freed so file struct of /dev/ublkcX is + * released. + * + * (2) UBLK_S_DEV_RECOVERING is set, which means the monitor_work: + * (a)has requeued all inflight rqs whose io_flags is ACTIVE + * (b)has requeued/aborted all inflight rqs whose io_flags is NOT ACTIVE + * (c)has completed/camceled all ioucmds owned by ther dying process + */ + if (test_bit(UB_STATE_OPEN, &ub->state) || + ub->dev_info.state != UBLK_S_DEV_RECOVERING) { + ret = -EBUSY; + goto out_unlock; + } + + pr_devel("%s: start recovery for dev id %d.\n", __func__, header->dev_id); + + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + struct ublk_queue *ubq = ublk_get_queue(ub, i); + + WARN_ON_ONCE(!(ubq->ubq_daemon && ubq_daemon_is_dying(ubq))); + pr_devel("%s: prepare for recovering qid %d\n", __func__, ubq->q_id); + ublk_queue_start_recovery(ub, ubq); + } + + /* set to NULL, otherwise new ubq_daemon cannot mmap the io_cmd_buf */ + ub->mm = NULL; + ub->nr_queues_ready = 0; + init_completion(&ub->completion); + ret = 0; + + out_unlock: + mutex_unlock(&ub->mutex); + ublk_put_device(ub); + return ret; +} + +static int ublk_ctrl_end_recovery(struct io_uring_cmd *cmd) +{ + struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd; + int ublksrv_pid = (int)header->data[0]; + struct ublk_device *ub; + int ret = -EINVAL; + + ub = ublk_get_device_from_id(header->dev_id); + if (!ub) + return ret; + + pr_devel("%s: Waiting for new ubq_daemon is ready, dev id %d...\n", + __func__, header->dev_id); + /* wait until new ubq_daemon sending all FETCH_REQ */ + wait_for_completion_interruptible(&ub->completion); + pr_devel("%s: All new ubq_daemon is ready, dev id %d\n", + __func__, header->dev_id); + + mutex_lock(&ub->mutex); + + if (!ublk_can_use_recovery(ub)) + goto out_unlock; + + /* monitor_work should set UBLK_S_DEV_RECOVERING */ + if (ub->dev_info.state != UBLK_S_DEV_RECOVERING) { + ret = -EBUSY; + goto out_unlock; + } + ub->dev_info.ublksrv_pid = ublksrv_pid; + pr_devel("%s: new ublksrv_pid %d, dev id %d\n", + __func__, ublksrv_pid, header->dev_id); + blk_mq_unquiesce_queue(ub->ub_disk->queue); + pr_devel("%s: queue unquiesced, dev id %d.\n", + __func__, header->dev_id); + + ub->dev_info.state = UBLK_S_DEV_LIVE; + schedule_delayed_work(&ub->monitor_work, UBLK_DAEMON_MONITOR_PERIOD); + /* We are good to redo requests now */ + blk_mq_kick_requeue_list(ub->ub_disk->queue); + ret = 0; + out_unlock: + mutex_unlock(&ub->mutex); + ublk_put_device(ub); + return ret; +} + static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) { @@ -2005,6 +2129,12 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd, case UBLK_CMD_SET_PARAMS: ret = ublk_ctrl_set_params(cmd); break; + case UBLK_CMD_START_USER_RECOVERY: + ret = ublk_ctrl_start_recovery(cmd); + break; + case UBLK_CMD_END_USER_RECOVERY: + ret = ublk_ctrl_end_recovery(cmd); + break; default: break; }