From patchwork Tue Sep 17 00:21:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uday Shankar X-Patchwork-Id: 13805837 Received: from mail-lf1-f104.google.com (mail-lf1-f104.google.com [209.85.167.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A0115672 for ; Tue, 17 Sep 2024 00:22:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.104 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532538; cv=none; b=LWHPHnVLjRlXrxJjx961eX1TLKjZxIyT+eAm42Qne9CO1WmXd+0CWsilmUu0E27OWw0zEOBo3XvNZxl6dzDiyDgrYICG88c0xg2+4r8KOut2A73bvKdMI07P/sThCD2hCMsvYaKi3l2NE96yY+ACmN4GD5ngPve507W0AQA4hEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532538; c=relaxed/simple; bh=tQ2AIiOzt79oPoquQ578qjHgc8IqkC2IkDtDmyCRvaE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cJVNqHTSW+4AhIml+CsN1mr0K3WF45aNxW8Esyq7+3tW5LWJYtlB/bxAXi7DVhBRPpNRQAHh8YR+mxNHXzMiyectihGZEcj8V/68dikBy9F/mIigLupivaAyaydUFJwD9m7BeA3SMxvON/liVIj2gsjXe41WLGilQDsYCmGNqw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=a6nOJEa+; arc=none smtp.client-ip=209.85.167.104 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="a6nOJEa+" Received: by mail-lf1-f104.google.com with SMTP id 2adb3069b0e04-5365392cfafso3680460e87.0 for ; Mon, 16 Sep 2024 17:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1726532534; x=1727137334; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IF+IrPzfd801zBWUyZuqeid0cQ3AF0F6yKbHsEmtcdA=; b=a6nOJEa+DBNq7hP9JgRExC3r+Y4rkKLldV0CD+dyk/pv4jazhczZT/mS1Weagx+ema OGshhXhNNbCBuQWD+jVL4FxoQms/oXI6cGJAjR/QJRhhF1tISWAbYUXsLokaFNuBdlww eNjpgXWOJanANn6NUaCT3g6H6CqDYCGoFKzUtR07YJbPXeWD/DrnElJqTcwoMJbMioHx hJ40RFiiep+xw2vhpKSxJy4Vajke81Iuv4uJatLAmvngBGj7Pa3hJm+yPcJGpXBR9/lT J/mxlNELJSx+wFrSgi+pcCnzl7UJmW1xf+Q7oKtCXCDwel3Kp/+LY6H9DVW3+KaFODte sfMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726532534; x=1727137334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IF+IrPzfd801zBWUyZuqeid0cQ3AF0F6yKbHsEmtcdA=; b=l8NzYkrP+/oXTHqpqp0maWywB0pBu8h5c7e+hMXAE7aFfl+8F5kXDgOw+kezngtR1L GkqKTdqUGDUQUAL8s5//M4ptRZXZ7ckZpSqYTenFU1fviQboOk0X5+DlYnD4+dca2C8A niksxCC2aySgiP8IApJnx8FgMz00/YQ3RtnmtbF37LP9k/5i6WXrTK9uGLqxYt9r1q19 K+6FVKEfS2/URsed6m/yX2mlxoLDEttQBhigUTtGlroXTsv9MIUTN/adX6JX8jA8rc4V gz0HyLD17GN6FmdAJXeRluwA1zzoT3e3cBi7Ppw6M0bsV8IOsGsaVQcdc1xByj8D06Zd SDYQ== X-Forwarded-Encrypted: i=1; AJvYcCXpNXQwUcdCSx1HnaaCe6oqE+chDOXmg+u+fY41J+rsTN7KsZTMoUDbUL48OgGcUjsUI4XSq8Fs+WCVYQ==@vger.kernel.org X-Gm-Message-State: AOJu0YzVq0J4V683ysJ4BGiEDyJdIke9Lvltm6rBIVB+IZHQgbbUFyE7 LAhOv25Ml1cV24mNr7Uz3wosE4h05BzYpIQ9eVt2x9VO/scHD1JcHg0hxFGw8mE/eSfeiAfW91E jK5X3d/2FDDY5ZE7WOutb6x85xKxKq4xS X-Google-Smtp-Source: AGHT+IFlk3dxfHFme9y2L3ACC2yqLxEqSa/GXqoGTe1j6E8U1s4EaY2V7oubJSmym7RCTTzbW33npIFvaSFl X-Received: by 2002:a05:6512:1114:b0:533:4620:ebfb with SMTP id 2adb3069b0e04-5367fed70b1mr7071557e87.21.1726532534008; Mon, 16 Sep 2024 17:22:14 -0700 (PDT) Received: from c7-smtp-2023.dev.purestorage.com ([208.88.159.128]) by smtp-relay.gmail.com with ESMTPS id 2adb3069b0e04-536870b239esm79997e87.137.2024.09.16.17.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 17:22:13 -0700 (PDT) X-Relaying-Domain: purestorage.com Received: from dev-ushankar.dev.purestorage.com (dev-ushankar.dev.purestorage.com [IPv6:2620:125:9007:640:7:70:36:0]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 68B813421DE; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) Received: by dev-ushankar.dev.purestorage.com (Postfix, from userid 1557716368) id 58137E40F90; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) From: Uday Shankar To: Ming Lei , Jens Axboe Cc: Uday Shankar , linux-block@vger.kernel.org Subject: [PATCH v2 1/4] ublk: check recovery flags for validity Date: Mon, 16 Sep 2024 18:21:52 -0600 Message-Id: <20240917002155.2044225-2-ushankar@purestorage.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240917002155.2044225-1-ushankar@purestorage.com> References: <20240917002155.2044225-1-ushankar@purestorage.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Setting UBLK_F_USER_RECOVERY_REISSUE without also setting UBLK_F_USER_RECOVERY is currently silently equivalent to not setting any recovery flags at all, even though that's obviously not intended. Check for this case and fail add_dev (with a paranoid warning to aid debugging any program which might rely on the old behavior) with EINVAL if it is detected. Signed-off-by: Uday Shankar Reviewed-by: Ming Lei --- Changes since v1 (https://lore.kernel.org/linux-block/20240617194451.435445-2-ushankar@purestorage.com/): - Replace switch statement with if statement drivers/block/ublk_drv.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index bca06bfb4bc3..5e04a0fcd0b7 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -62,6 +62,9 @@ | UBLK_F_USER_COPY \ | UBLK_F_ZONED) +#define UBLK_F_ALL_RECOVERY_FLAGS (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 | \ @@ -2373,6 +2376,14 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) else if (!(info.flags & UBLK_F_UNPRIVILEGED_DEV)) return -EPERM; + /* forbid nonsense combinations of recovery flags */ + if ((info.flags & UBLK_F_USER_RECOVERY_REISSUE) && + !(info.flags & UBLK_F_USER_RECOVERY)) { + pr_warn("%s: invalid recovery flags %llx\n", __func__, + info.flags & UBLK_F_ALL_RECOVERY_FLAGS); + return -EINVAL; + } + /* * unprivileged device can't be trusted, but RECOVERY and * RECOVERY_REISSUE still may hang error handling, so can't From patchwork Tue Sep 17 00:21:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uday Shankar X-Patchwork-Id: 13805838 Received: from mail-lf1-f101.google.com (mail-lf1-f101.google.com [209.85.167.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6900DA31 for ; Tue, 17 Sep 2024 00:22:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532538; cv=none; b=ETTIL8E7XPCIliGXNb/XRKW7xX5T4GJ11aMI5SylKOebJHQXeA91Ld1a2RNz/EJCzE4qwTA0QEJGxTDPO/38BH+BrSQEEnhl3PnsguLOKCjxNSBHwJpmTldqcNCeLyxRewQ0ok1b0vAXk1SVuFPUM83RIc9BbBzbMHG1QnY8TJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532538; c=relaxed/simple; bh=ijDkR0tL6MfX4Xp6ndHHX70zEo5pmMWmV9NCOATO9qQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HQc0DyobnBMLkpXDvSSWzZ+w5XguorxNHdtLZ4ZtfLU4VRDbDY3T9bIRKgB9b8a8jfwqmPWZaf8osZdz7Ap3Pby4Ob3K0O/YxXcOyWBaV6eMKXxKTZXXv2uNS0pn/v0T885ytcGrelkcInGNlnMReAS7DTvAfmul1hrqx121abs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=f+KlKB7y; arc=none smtp.client-ip=209.85.167.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="f+KlKB7y" Received: by mail-lf1-f101.google.com with SMTP id 2adb3069b0e04-53659867cbdso4902276e87.3 for ; Mon, 16 Sep 2024 17:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1726532534; x=1727137334; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=6JdKX56b+gpWqIVvOt/qDFoHswvHnCn8Q0Srw3ShBnw=; b=f+KlKB7yfLcG7Mqcogvhx6RXAC950h02wXsxjukY8r8votQLF67ZjAwKy32VOb2/aY R+i07T6YiA/ortQ+mu2UinPwB+Dz07U1Zgl9fv4lpGOvqaCyLTHc1zlN1B68ViG+sbZE bIsyf/ngWZku8ykXpWdf5zGBbcgwdb/LpkPYutMYCaMo74V8O55wFRIUpLTkTdsi1Ve1 t1DRs2JuNNeybbkBsC36AmNqYgtjGYAWYldEDe4247uGG2YVe9COkcKGQcQ0NS3mLWon bnsG7TSi5ix8sttF6Z4Kux9r/ZJgyF/pDwUgXrOYYB8FKKbijcE1VqQrWnJrYtXdhTOh gmbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726532534; x=1727137334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6JdKX56b+gpWqIVvOt/qDFoHswvHnCn8Q0Srw3ShBnw=; b=M0LrNIEN8VOm8ZoqwI53tgxbxhPeFQpx0JDXGqan94/DUMiNEtbJIRS+6PHrYc32+N OFKNYf829gT0P9+195C/ALqq1qa4AohW5l4ZM5/Ow1IlpF+QMXcwrRu9xd0wzF2nBsx/ cTtwxQ/lWz3pp4ZPwV4fRsxzy9h3oDFjzxBfflgoZxS4e90t7a4n+/Jw806OMIXyzNGP MAO7iQEEsFyVBLMgbzuH6w31OQ645hDvQX7gL98b9emgBeebobRXfPBg2q1ECXnK93wT lPlIe8lNTdQ2mG7EoqN7TCBsg/cRTHsMeNaEyi7ephPnHcrYKqXlNi1GP0RSG2hnWIXH vPZA== X-Forwarded-Encrypted: i=1; AJvYcCVsoC6YaxFbq7PYVJq8dqGbtGcbd3vmdgoBSDBzr4gEnwpMudSa9dnYb3mw7+yCreyr3KGlc2ZNJnX5QA==@vger.kernel.org X-Gm-Message-State: AOJu0Yx1R+83JlDj8yf8WWaI7+x5J7pBes5ZeA0kfpdxwI48JIeGd5v7 yzDMQs6YtrV7O7odavqHhVdibY4zUkkpQr3FaJcSBFfA0SRvqLAGXv1CzYqsxadtm1tbbaZw3qj TcjoP7UBcCGUqzpMM8N/BiPjINdCncFR9 X-Google-Smtp-Source: AGHT+IEvMU6bTqy/vFHh4GiEaQaaknjHWGgZHUang16j2QBZWJeQMLotSiRhfSci/ciCQPnVL/CjoUUXPPB1 X-Received: by 2002:a05:6512:31cd:b0:536:553f:a6e7 with SMTP id 2adb3069b0e04-5367fee95d4mr8295993e87.32.1726532534059; Mon, 16 Sep 2024 17:22:14 -0700 (PDT) Received: from c7-smtp-2023.dev.purestorage.com ([208.88.159.128]) by smtp-relay.gmail.com with ESMTPS id 2adb3069b0e04-5368709662esm80736e87.78.2024.09.16.17.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 17:22:14 -0700 (PDT) X-Relaying-Domain: purestorage.com Received: from dev-ushankar.dev.purestorage.com (dev-ushankar.dev.purestorage.com [IPv6:2620:125:9007:640:7:70:36:0]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 69DDB34223E; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) Received: by dev-ushankar.dev.purestorage.com (Postfix, from userid 1557716368) id 61923E40F10; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) From: Uday Shankar To: Ming Lei , Jens Axboe Cc: Uday Shankar , linux-block@vger.kernel.org Subject: [PATCH v2 2/4] ublk: refactor recovery configuration flag helpers Date: Mon, 16 Sep 2024 18:21:53 -0600 Message-Id: <20240917002155.2044225-3-ushankar@purestorage.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240917002155.2044225-1-ushankar@purestorage.com> References: <20240917002155.2044225-1-ushankar@purestorage.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 ublk currently supports the following behaviors on ublk server exit: A: outstanding I/Os get errors, subsequently issued I/Os get errors B: outstanding I/Os get errors, subsequently issued I/Os queue C: outstanding I/Os get reissued, subsequently issued I/Os queue and the following behaviors for recovery of preexisting block devices by a future incarnation of the ublk server: 1: ublk devices stopped on ublk server exit (no recovery possible) 2: ublk devices are recoverable using start/end_recovery commands The userspace interface allows selection of combinations of these behaviors using flags specified at device creation time, namely: default behavior: A + 1 UBLK_F_USER_RECOVERY: B + 2 UBLK_F_USER_RECOVERY|UBLK_F_USER_RECOVERY_REISSUE: C + 2 We can't easily change the userspace interface to allow independent selection of one of {A, B, C} and one of {1, 2}, but we can refactor the internal helpers which test for the flags. Replace the existing helpers with the following set: ublk_nosrv_should_reissue_outstanding: tests for behavior C ublk_nosrv_[dev_]should_queue_io: tests for behavior B ublk_nosrv_should_stop_dev: tests for behavior 1 Signed-off-by: Uday Shankar --- Changes since v1 (https://lore.kernel.org/linux-block/20240617194451.435445-3-ushankar@purestorage.com/): - Make the fast-path test in ublk_queue_rq access the queue-local copy of the device flags. drivers/block/ublk_drv.c | 63 +++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 5e04a0fcd0b7..b069f4d2b9d2 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -675,22 +675,45 @@ static inline int ublk_queue_cmd_buf_size(struct ublk_device *ub, int q_id) PAGE_SIZE); } -static inline bool ublk_queue_can_use_recovery_reissue( - struct ublk_queue *ubq) +/* + * Should I/O outstanding to the ublk server when it exits be reissued? + * If not, outstanding I/O will get errors. + */ +static inline bool ublk_nosrv_should_reissue_outstanding(struct ublk_device *ub) { - return (ubq->flags & UBLK_F_USER_RECOVERY) && - (ubq->flags & UBLK_F_USER_RECOVERY_REISSUE); + return (ub->dev_info.flags & UBLK_F_USER_RECOVERY) && + (ub->dev_info.flags & UBLK_F_USER_RECOVERY_REISSUE); } -static inline bool ublk_queue_can_use_recovery( - struct ublk_queue *ubq) +/* + * Should I/O issued while there is no ublk server queue? If not, I/O + * issued while there is no ublk server will get errors. + */ +static inline bool ublk_nosrv_dev_should_queue_io(struct ublk_device *ub) +{ + return ub->dev_info.flags & UBLK_F_USER_RECOVERY; +} + +/* + * Same as ublk_nosrv_dev_should_queue_io, but uses a queue-local copy + * of the device flags for smaller cache footprint - better for fast + * paths. + */ +static inline bool ublk_nosrv_should_queue_io(struct ublk_queue *ubq) { return ubq->flags & UBLK_F_USER_RECOVERY; } -static inline bool ublk_can_use_recovery(struct ublk_device *ub) +/* + * Should ublk devices be stopped (i.e. no recovery possible) when the + * ublk server exits? If not, devices can be used again by a future + * incarnation of a ublk server via the start_recovery/end_recovery + * commands. + */ +static inline bool ublk_nosrv_should_stop_dev(struct ublk_device *ub) { - return ub->dev_info.flags & UBLK_F_USER_RECOVERY; + return (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY)) && + (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY_REISSUE)); } static void ublk_free_disk(struct gendisk *disk) @@ -1066,7 +1089,7 @@ static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io, { WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE); - if (ublk_queue_can_use_recovery_reissue(ubq)) + if (ublk_nosrv_should_reissue_outstanding(ubq->dev)) blk_mq_requeue_request(req, false); else ublk_put_req_ref(ubq, req); @@ -1094,7 +1117,7 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq, struct request *rq) { /* We cannot process this rq so just requeue it. */ - if (ublk_queue_can_use_recovery(ubq)) + if (ublk_nosrv_dev_should_queue_io(ubq->dev)) blk_mq_requeue_request(rq, false); else blk_mq_end_request(rq, BLK_STS_IOERR); @@ -1239,10 +1262,10 @@ static enum blk_eh_timer_return ublk_timeout(struct request *rq) struct ublk_device *ub = ubq->dev; if (ublk_abort_requests(ub, ubq)) { - if (ublk_can_use_recovery(ub)) - schedule_work(&ub->quiesce_work); - else + if (ublk_nosrv_should_stop_dev(ub)) schedule_work(&ub->stop_work); + else + schedule_work(&ub->quiesce_work); } return BLK_EH_DONE; } @@ -1271,7 +1294,7 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, * Note: force_abort is guaranteed to be seen because it is set * before request queue is unqiuesced. */ - if (ublk_queue_can_use_recovery(ubq) && unlikely(ubq->force_abort)) + if (ublk_nosrv_should_queue_io(ubq) && unlikely(ubq->force_abort)) return BLK_STS_IOERR; if (unlikely(ubq->canceling)) { @@ -1492,10 +1515,10 @@ static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd *cmd, ublk_cancel_cmd(ubq, io, issue_flags); if (need_schedule) { - if (ublk_can_use_recovery(ub)) - schedule_work(&ub->quiesce_work); - else + if (ublk_nosrv_should_stop_dev(ub)) schedule_work(&ub->stop_work); + else + schedule_work(&ub->quiesce_work); } } @@ -1600,7 +1623,7 @@ static void ublk_stop_dev(struct ublk_device *ub) mutex_lock(&ub->mutex); if (ub->dev_info.state == UBLK_S_DEV_DEAD) goto unlock; - if (ublk_can_use_recovery(ub)) { + if (ublk_nosrv_dev_should_queue_io(ub)) { if (ub->dev_info.state == UBLK_S_DEV_LIVE) __ublk_quiesce_dev(ub); ublk_unquiesce_dev(ub); @@ -2702,7 +2725,7 @@ static int ublk_ctrl_start_recovery(struct ublk_device *ub, int i; mutex_lock(&ub->mutex); - if (!ublk_can_use_recovery(ub)) + if (ublk_nosrv_should_stop_dev(ub)) goto out_unlock; if (!ub->nr_queues_ready) goto out_unlock; @@ -2755,7 +2778,7 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub, __func__, ub->dev_info.nr_hw_queues, header->dev_id); mutex_lock(&ub->mutex); - if (!ublk_can_use_recovery(ub)) + if (ublk_nosrv_should_stop_dev(ub)) goto out_unlock; if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) { From patchwork Tue Sep 17 00:21:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uday Shankar X-Patchwork-Id: 13805834 Received: from mail-yb1-f228.google.com (mail-yb1-f228.google.com [209.85.219.228]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C01B24C7C for ; Tue, 17 Sep 2024 00:22:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.228 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532536; cv=none; b=B6At0j4GQrM/pV8xQoVUMk1GU31AJlXEW/P4O34PUKisVeY9UI+5z4+gwcQNKkNub7FE+s4OMqTcgR1yboQ07EvHOmIcLoOxVcIouv/+LW1o3wQXRKGI9eqdF3pmsNGbLL0+s632qHOOM6CZUjsjEy7md0hIzRIoCG9SmNwiD7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532536; c=relaxed/simple; bh=+PaiOJ2ZWWP0cyowrwmAS/YacNa41wjh6wut2sqDk8A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jU2C0SWHdtognpYgEd5X6YbYvpMuW/OFc35d1/hqJ1e8F1E/P1XSxyhfwX0XlMAbw5PmcRqyGw0lRFPMzDkSfbwIkaDPrxLYRCtB1L29+XyF9s934ozk7Nagejq/YUZJF93YrAi3SnkPyMz6+wBm8fJexmExq+wrbfqzm9cxjys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=A94+vRA3; arc=none smtp.client-ip=209.85.219.228 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="A94+vRA3" Received: by mail-yb1-f228.google.com with SMTP id 3f1490d57ef6-e05f25fb96eso3330501276.1 for ; Mon, 16 Sep 2024 17:22:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1726532534; x=1727137334; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Pmkb4TQvPiWQD4+HIkIf+BpaSPlDYzwxRM06LpNqV00=; b=A94+vRA366kviVxZeNqeCLQsdCSe+DNeaLJOzjjhijvZjxiR5M7C15YSCprKwc/EVw OPwi5jBpfqnAFAzatL9eKCEa3jX7s5FQ4frhmOX1F5yaSU3+9TvE1pWGGfHDS/jNqWNa mHUDedMc6xXpdI4KacfMKgRPD6OsedQBHexzIcvdyt/1WEjaSQIOZBuzZPA7MIxH8Yo+ /JnNCPMHZF0+pfHfcXml/qBUt+RUJlVl2poKYdjHLsb3omjKzzhTd42oWYkvE7IlZLzQ 21mLIiWm/2bPbx/ue33ciQarHgXh3n56M5azyvRz/E/KrMDVupqgVhoqC9NNzXTajTYy pl9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726532534; x=1727137334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pmkb4TQvPiWQD4+HIkIf+BpaSPlDYzwxRM06LpNqV00=; b=TejwysfKyjgAHUa3sMC637pK/dFGKBP+fz4RSuOAW3nsYSEG9vIMam3FGfVRTVIrKC CT5VAdrTCwhkCrU1ZtD79CF4Y7lZA7Jl4DdXbL4vjxbwtcmmADlHMzaWOOyA2epeJX7c RU76JnAr1ng+q5v/C7jwyHefiLlqcgKFIV+PUQM7+6eZvx7QFAZt8KceIFvTBB6kyav4 pc8Dw69+PYfdqmmwle8bQk2donnhUmtbvyWeoM8dKnZYzaMF+b2Bgse0MMaYUsBnj8Cm 1x7dhDGRXdA/wu5rrdNeIujewGOEOMW8hxJvR3Y70xGsvPZxnMbSUE8IWkkieLT05+a9 OL/g== X-Forwarded-Encrypted: i=1; AJvYcCVe13j63yb23yOqHgEfi3NcpU91ANHilFKvwkTDzZQmn6dLO7rS5DO3zSNnwaKY2EK3l919oaf0VpUl7Q==@vger.kernel.org X-Gm-Message-State: AOJu0YyaQ73kOguaW1HrpF4Bd3jzNnvtCHVzH47lYELn//nktwtYD0+l Yq8fREzNj1Nk66H9AKpKo5C1SlLIGX6zZtsoVdf5vF5wqZUGyb6d2a/+YXo3WP2riGSdZt8q7rU 1iSWNywXRCv6ivFlZ8l9YyOeFWjKnayOUY5xjwN8YsVvPSk0t X-Google-Smtp-Source: AGHT+IHb8ySTGmOlmQuqd84mgsM8TfFnqyG0L9ra5kd50Uj66OqTCohoSrkg8V34iu6lgEN6EKOCZIygRglx X-Received: by 2002:a05:6902:268a:b0:e1d:436c:3b4f with SMTP id 3f1490d57ef6-e1db00ffb23mr8250377276.50.1726532533578; Mon, 16 Sep 2024 17:22:13 -0700 (PDT) Received: from c7-smtp-2023.dev.purestorage.com ([2620:125:9017:12:36:3:5:0]) by smtp-relay.gmail.com with ESMTPS id 3f1490d57ef6-e1dc1121f5dsm279444276.9.2024.09.16.17.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 17:22:13 -0700 (PDT) X-Relaying-Domain: purestorage.com Received: from dev-ushankar.dev.purestorage.com (dev-ushankar.dev.purestorage.com [IPv6:2620:125:9007:640:7:70:36:0]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 719F9342243; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) Received: by dev-ushankar.dev.purestorage.com (Postfix, from userid 1557716368) id 6B17AE40F10; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) From: Uday Shankar To: Ming Lei , Jens Axboe Cc: Uday Shankar , linux-block@vger.kernel.org Subject: [PATCH v2 3/4] ublk: merge stop_work and quiesce_work Date: Mon, 16 Sep 2024 18:21:54 -0600 Message-Id: <20240917002155.2044225-4-ushankar@purestorage.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240917002155.2044225-1-ushankar@purestorage.com> References: <20240917002155.2044225-1-ushankar@purestorage.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Save some lines by merging stop_work and quiesce_work into nosrv_work, which looks at the recovery flags and does the right thing when the "no ublk server" condition is detected. Signed-off-by: Uday Shankar Reviewed-by: Ming Lei --- drivers/block/ublk_drv.c | 64 ++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index b069f4d2b9d2..c7a0493b3545 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -182,8 +182,7 @@ struct ublk_device { unsigned int nr_queues_ready; unsigned int nr_privileged_daemon; - struct work_struct quiesce_work; - struct work_struct stop_work; + struct work_struct nosrv_work; }; /* header of ublk_params */ @@ -1262,10 +1261,7 @@ static enum blk_eh_timer_return ublk_timeout(struct request *rq) struct ublk_device *ub = ubq->dev; if (ublk_abort_requests(ub, ubq)) { - if (ublk_nosrv_should_stop_dev(ub)) - schedule_work(&ub->stop_work); - else - schedule_work(&ub->quiesce_work); + schedule_work(&ub->nosrv_work); } return BLK_EH_DONE; } @@ -1515,10 +1511,7 @@ static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd *cmd, ublk_cancel_cmd(ubq, io, issue_flags); if (need_schedule) { - if (ublk_nosrv_should_stop_dev(ub)) - schedule_work(&ub->stop_work); - else - schedule_work(&ub->quiesce_work); + schedule_work(&ub->nosrv_work); } } @@ -1581,20 +1574,6 @@ static void __ublk_quiesce_dev(struct ublk_device *ub) ub->dev_info.state = UBLK_S_DEV_QUIESCED; } -static void ublk_quiesce_work_fn(struct work_struct *work) -{ - struct ublk_device *ub = - container_of(work, struct ublk_device, quiesce_work); - - mutex_lock(&ub->mutex); - if (ub->dev_info.state != UBLK_S_DEV_LIVE) - goto unlock; - __ublk_quiesce_dev(ub); - unlock: - mutex_unlock(&ub->mutex); - ublk_cancel_dev(ub); -} - static void ublk_unquiesce_dev(struct ublk_device *ub) { int i; @@ -1643,6 +1622,25 @@ static void ublk_stop_dev(struct ublk_device *ub) ublk_cancel_dev(ub); } +static void ublk_nosrv_work(struct work_struct *work) +{ + struct ublk_device *ub = + container_of(work, struct ublk_device, nosrv_work); + + if (ublk_nosrv_should_stop_dev(ub)) { + ublk_stop_dev(ub); + return; + } + + mutex_lock(&ub->mutex); + if (ub->dev_info.state != UBLK_S_DEV_LIVE) + goto unlock; + __ublk_quiesce_dev(ub); + unlock: + mutex_unlock(&ub->mutex); + ublk_cancel_dev(ub); +} + /* device can only be started after all IOs are ready */ static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq) { @@ -2157,14 +2155,6 @@ static int ublk_add_chdev(struct ublk_device *ub) return ret; } -static void ublk_stop_work_fn(struct work_struct *work) -{ - struct ublk_device *ub = - container_of(work, struct ublk_device, stop_work); - - ublk_stop_dev(ub); -} - /* align max io buffer size with PAGE_SIZE */ static void ublk_align_max_io_size(struct ublk_device *ub) { @@ -2189,8 +2179,7 @@ static int ublk_add_tag_set(struct ublk_device *ub) static void ublk_remove(struct ublk_device *ub) { ublk_stop_dev(ub); - cancel_work_sync(&ub->stop_work); - cancel_work_sync(&ub->quiesce_work); + cancel_work_sync(&ub->nosrv_work); cdev_device_del(&ub->cdev, &ub->cdev_dev); ublk_put_device(ub); ublks_added--; @@ -2450,8 +2439,7 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) goto out_unlock; mutex_init(&ub->mutex); spin_lock_init(&ub->lock); - INIT_WORK(&ub->quiesce_work, ublk_quiesce_work_fn); - INIT_WORK(&ub->stop_work, ublk_stop_work_fn); + INIT_WORK(&ub->nosrv_work, ublk_nosrv_work); ret = ublk_alloc_dev_number(ub, header->dev_id); if (ret < 0) @@ -2586,9 +2574,7 @@ static inline void ublk_ctrl_cmd_dump(struct io_uring_cmd *cmd) static int ublk_ctrl_stop_dev(struct ublk_device *ub) { ublk_stop_dev(ub); - cancel_work_sync(&ub->stop_work); - cancel_work_sync(&ub->quiesce_work); - + cancel_work_sync(&ub->nosrv_work); return 0; } From patchwork Tue Sep 17 00:21:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uday Shankar X-Patchwork-Id: 13805835 Received: from mail-ot1-f101.google.com (mail-ot1-f101.google.com [209.85.210.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05E884C7E for ; Tue, 17 Sep 2024 00:22:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532537; cv=none; b=WJL6dMRi4OpQ1q+KvRSmltesaeEs93BlkQlKfgio3RprGVUqFTgYBAV1gnfC4riBlMPdAURh4XBMNFOiB3MVoPyHQGL8wSPrP8ty+f8GGHfrJ4LP7q49aw9dA2HtLRY6t/IQnF91AMTPNoW4+c1MPTIxGrKazXOV/G5y3d7SUGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726532537; c=relaxed/simple; bh=denZ3I4baDt0qOPYcBDwVMkE8nOZHdysFCSIBVHg+DA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b2mbmB8Oq+vaHVTUEadtQKI1s0CTWwX1grXr5K9gV04uIxnZBB/kdrkdwlPmtJFdotTe0jS2OjzxQTrq1zeESNeQ2vqN9ridN8Cd30VYom9JvO5cmonQgK89VGyJV71L7Q3dvdeEsxT253RIm35OByKz9KCkPw8Yaqy5qqCb4YU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com; spf=fail smtp.mailfrom=purestorage.com; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b=fkzK0+13; arc=none smtp.client-ip=209.85.210.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=purestorage.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=purestorage.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=purestorage.com header.i=@purestorage.com header.b="fkzK0+13" Received: by mail-ot1-f101.google.com with SMTP id 46e09a7af769-712422564aaso854051a34.0 for ; Mon, 16 Sep 2024 17:22:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=purestorage.com; s=google2022; t=1726532534; x=1727137334; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BY20McgVt8zeEokl2t4x+9nKjS7C2srtz41PtEnI54U=; b=fkzK0+13Vvzi/nzQr09lYAGjqotXT86p4TcZZG7GrzPXkBqdQhY56NaIGLj7SNRsPj uG3GnhtJq2bsC+mtuYeGX++39GXbPLpa6Cg1oh5rONVO83Hap3MGeFXoYT/8zrgQ3sqU RVmhUAXP/Va4wLtr1zba7TBvcDpGwsEwdJpODm6SmLo7HqRu692NM/oSwXOP2AdpuGL4 Qo+tt6w7qxBVBXyFj3ui5nuxIp7pXM7TxtNTkkfPJ2Q4V37+AcHViKmh+iefecFIF4Lz 8XTayHv4nKT7I8HJjgt+eYqmJh/zSqWyv7tYJEn5xVLgc6QSkr45k7XqAG+7/VsASFID V2Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726532534; x=1727137334; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BY20McgVt8zeEokl2t4x+9nKjS7C2srtz41PtEnI54U=; b=IEg5enu93fwGQ53ueK3zHLQ4H93TNVpzIK57dVblpiFNXLRwCfDkK2ldtNDuEgIDS9 MaBwLy03D0LF2/GiRyLza/Ni2xeov35/ShcJYtaAbn2S3HghI51e8JOBoAqnnLRrRQsW ORZw+re6AaM2kF3zGjyGmXuiL4A24r7A+yLjlFrZNbluxD5CXxsfsHsjdLfNBQ7Q8yES 3TgrCMC2p3kH/cmd5qZPO9u0S3M7eavD7owbvj3DHianQ2eKwyT7F5v+kVo15TOzP19t n4OTGuRkG9DiWcjJrizoKQXsviQKSe+ldbpWJJ2Q2otsSxB+ZLT8k42pQ8imHqpkj5XM F73A== X-Forwarded-Encrypted: i=1; AJvYcCWvMajeBJGcAhjfESEZ85oDsjfrGxDeW6xS/eo2rNZ/T7rg4eG6qqCTynt6frFwlbmX8iiZtg2zLVTGzg==@vger.kernel.org X-Gm-Message-State: AOJu0YyP5D6hroLqr+rrzXCGV5Gy+nlKMpyej5/8573wNNSbzH32TnAj URap7spMpEJSRLTIPCmwCpjFDuFrmr3H7IElVrhp3yUrniB+WJlwaSO8cNAyWIKdlpV2CaQtlZ8 BW0OBrTK4dZS7svD+bIbtar3PN6B8cUpZ2Ua/hIbXeLbGZPdq X-Google-Smtp-Source: AGHT+IHwbZX3irZ9m7OfbpFZ+PKysC3F0tonUD/m9v/FWeMtAJuh26gZyQaVX1n3QlpRMsk4HO5fsDKU0Yqy X-Received: by 2002:a05:6870:219b:b0:260:25ef:c5a0 with SMTP id 586e51a60fabf-27c3e88dc51mr8626114fac.5.1726532534006; Mon, 16 Sep 2024 17:22:14 -0700 (PDT) Received: from c7-smtp-2023.dev.purestorage.com ([2620:125:9017:12:36:3:5:0]) by smtp-relay.gmail.com with ESMTPS id 586e51a60fabf-27c9598f99fsm268567fac.48.2024.09.16.17.22.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 17:22:13 -0700 (PDT) X-Relaying-Domain: purestorage.com Received: from dev-ushankar.dev.purestorage.com (dev-ushankar.dev.purestorage.com [IPv6:2620:125:9007:640:7:70:36:0]) by c7-smtp-2023.dev.purestorage.com (Postfix) with ESMTP id 7B633342244; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) Received: by dev-ushankar.dev.purestorage.com (Postfix, from userid 1557716368) id 74A18E40F10; Mon, 16 Sep 2024 18:22:12 -0600 (MDT) From: Uday Shankar To: Ming Lei , Jens Axboe Cc: Uday Shankar , linux-block@vger.kernel.org Subject: [PATCH v2 4/4] ublk: support device recovery without I/O queueing Date: Mon, 16 Sep 2024 18:21:55 -0600 Message-Id: <20240917002155.2044225-5-ushankar@purestorage.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240917002155.2044225-1-ushankar@purestorage.com> References: <20240917002155.2044225-1-ushankar@purestorage.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 ublk currently supports the following behaviors on ublk server exit: A: outstanding I/Os get errors, subsequently issued I/Os get errors B: outstanding I/Os get errors, subsequently issued I/Os queue C: outstanding I/Os get reissued, subsequently issued I/Os queue and the following behaviors for recovery of preexisting block devices by a future incarnation of the ublk server: 1: ublk devices stopped on ublk server exit (no recovery possible) 2: ublk devices are recoverable using start/end_recovery commands The userspace interface allows selection of combinations of these behaviors using flags specified at device creation time, namely: default behavior: A + 1 UBLK_F_USER_RECOVERY: B + 2 UBLK_F_USER_RECOVERY|UBLK_F_USER_RECOVERY_REISSUE: C + 2 The behavior A + 2 is currently unsupported. Add support for this behavior under the new flag combination UBLK_F_USER_RECOVERY|UBLK_F_USER_RECOVERY_FAIL_IO. Signed-off-by: Uday Shankar --- Changes since v1 (https://lore.kernel.org/linux-block/20240617194451.435445-5-ushankar@purestorage.com/): - Change flag name from UBLK_F_USER_RECOVERY_NOQUEUE to UBLK_F_USER_RECOVERY_FAIL_IO - Require UBLK_F_USER_RECOVERY to be set along with the new flag for it to be effective. This makes more sense, as UBLK_F_USER_RECOVERY essentially selects behavior 2 above (and not setting UBLK_F_USER_RECOVERY selects behavior 1). - Add per-ublk-queue flag which is true iff device state is UBLK_S_DEV_FAIL_IO. This lets us avoid fetching the device in the fast path. drivers/block/ublk_drv.c | 75 ++++++++++++++++++++++++++++------- include/uapi/linux/ublk_cmd.h | 18 +++++++++ 2 files changed, 79 insertions(+), 14 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index c7a0493b3545..548043eeefb9 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -60,10 +60,12 @@ | UBLK_F_UNPRIVILEGED_DEV \ | UBLK_F_CMD_IOCTL_ENCODE \ | UBLK_F_USER_COPY \ - | UBLK_F_ZONED) + | UBLK_F_ZONED \ + | UBLK_F_USER_RECOVERY_FAIL_IO) #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \ - | UBLK_F_USER_RECOVERY_REISSUE) + | UBLK_F_USER_RECOVERY_REISSUE \ + | UBLK_F_USER_RECOVERY_FAIL_IO) /* All UBLK_PARAM_TYPE_* should be included here */ #define UBLK_PARAM_TYPE_ALL \ @@ -146,6 +148,7 @@ struct ublk_queue { bool force_abort; bool timeout; bool canceling; + bool fail_io; /* copy of dev->state == UBLK_S_DEV_FAIL_IO */ unsigned short nr_io_ready; /* how many ios setup */ spinlock_t cancel_lock; struct ublk_device *dev; @@ -690,7 +693,8 @@ static inline bool ublk_nosrv_should_reissue_outstanding(struct ublk_device *ub) */ static inline bool ublk_nosrv_dev_should_queue_io(struct ublk_device *ub) { - return ub->dev_info.flags & UBLK_F_USER_RECOVERY; + return (ub->dev_info.flags & UBLK_F_USER_RECOVERY) && + !(ub->dev_info.flags & UBLK_F_USER_RECOVERY_FAIL_IO); } /* @@ -700,7 +704,8 @@ static inline bool ublk_nosrv_dev_should_queue_io(struct ublk_device *ub) */ static inline bool ublk_nosrv_should_queue_io(struct ublk_queue *ubq) { - return ubq->flags & UBLK_F_USER_RECOVERY; + return (ubq->flags & UBLK_F_USER_RECOVERY) && + !(ubq->flags & UBLK_F_USER_RECOVERY_FAIL_IO); } /* @@ -712,7 +717,14 @@ static inline bool ublk_nosrv_should_queue_io(struct ublk_queue *ubq) static inline bool ublk_nosrv_should_stop_dev(struct ublk_device *ub) { return (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY)) && - (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY_REISSUE)); + (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY_REISSUE)) && + (!(ub->dev_info.flags & UBLK_F_USER_RECOVERY_FAIL_IO)); +} + +static inline bool ublk_dev_in_recoverable_state(struct ublk_device *ub) +{ + return ub->dev_info.state == UBLK_S_DEV_QUIESCED || + ub->dev_info.state == UBLK_S_DEV_FAIL_IO; } static void ublk_free_disk(struct gendisk *disk) @@ -1276,6 +1288,10 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq = bd->rq; blk_status_t res; + if (unlikely(ubq->fail_io)) { + return BLK_STS_TARGET; + } + /* fill iod to slot in io cmd buffer */ res = ublk_setup_iod(ubq, rq); if (unlikely(res != BLK_STS_OK)) @@ -1626,6 +1642,7 @@ static void ublk_nosrv_work(struct work_struct *work) { struct ublk_device *ub = container_of(work, struct ublk_device, nosrv_work); + int i; if (ublk_nosrv_should_stop_dev(ub)) { ublk_stop_dev(ub); @@ -1635,7 +1652,18 @@ static void ublk_nosrv_work(struct work_struct *work) mutex_lock(&ub->mutex); if (ub->dev_info.state != UBLK_S_DEV_LIVE) goto unlock; - __ublk_quiesce_dev(ub); + + if (ublk_nosrv_dev_should_queue_io(ub)) { + __ublk_quiesce_dev(ub); + } else { + blk_mq_quiesce_queue(ub->ub_disk->queue); + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + ublk_get_queue(ub, i)->fail_io = true; + } + blk_mq_unquiesce_queue(ub->ub_disk->queue); + ub->dev_info.state = UBLK_S_DEV_FAIL_IO; + } + unlock: mutex_unlock(&ub->mutex); ublk_cancel_dev(ub); @@ -2389,8 +2417,13 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) return -EPERM; /* forbid nonsense combinations of recovery flags */ - if ((info.flags & UBLK_F_USER_RECOVERY_REISSUE) && - !(info.flags & UBLK_F_USER_RECOVERY)) { + switch (info.flags & UBLK_F_ALL_RECOVERY_FLAGS) { + case 0: + case UBLK_F_USER_RECOVERY: + case (UBLK_F_USER_RECOVERY | UBLK_F_USER_RECOVERY_REISSUE): + case (UBLK_F_USER_RECOVERY | UBLK_F_USER_RECOVERY_FAIL_IO): + break; + default: pr_warn("%s: invalid recovery flags %llx\n", __func__, info.flags & UBLK_F_ALL_RECOVERY_FLAGS); return -EINVAL; @@ -2722,14 +2755,18 @@ static int ublk_ctrl_start_recovery(struct ublk_device *ub, * and related io_uring ctx is freed so file struct of /dev/ublkcX is * released. * + * and one of the following holds + * * (2) UBLK_S_DEV_QUIESCED is set, which means the quiesce_work: * (a)has quiesced request queue * (b)has requeued every inflight rqs whose io_flags is ACTIVE * (c)has requeued/aborted every inflight rqs whose io_flags is NOT ACTIVE * (d)has completed/camceled all ioucmds owned by ther dying process + * + * (3) UBLK_S_DEV_FAIL_IO is set, which means the queue is not + * quiesced, but all I/O is being immediately errored */ - if (test_bit(UB_STATE_OPEN, &ub->state) || - ub->dev_info.state != UBLK_S_DEV_QUIESCED) { + if (test_bit(UB_STATE_OPEN, &ub->state) || !ublk_dev_in_recoverable_state(ub)) { ret = -EBUSY; goto out_unlock; } @@ -2753,6 +2790,7 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub, const struct ublksrv_ctrl_cmd *header = io_uring_sqe_cmd(cmd->sqe); int ublksrv_pid = (int)header->data[0]; int ret = -EINVAL; + int i; 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); @@ -2767,18 +2805,27 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub, if (ublk_nosrv_should_stop_dev(ub)) goto out_unlock; - if (ub->dev_info.state != UBLK_S_DEV_QUIESCED) { + if (!ublk_dev_in_recoverable_state(ub)) { 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_quiesce_queue(ub->ub_disk->queue); + for (i = 0; i < ub->dev_info.nr_hw_queues; i++) { + ublk_get_queue(ub, i)->fail_io = false; + } blk_mq_unquiesce_queue(ub->ub_disk->queue); - pr_devel("%s: queue unquiesced, dev id %d.\n", - __func__, header->dev_id); - blk_mq_kick_requeue_list(ub->ub_disk->queue); ub->dev_info.state = UBLK_S_DEV_LIVE; + if (ublk_nosrv_dev_should_queue_io(ub)) { + blk_mq_unquiesce_queue(ub->ub_disk->queue); + pr_devel("%s: queue unquiesced, dev id %d.\n", + __func__, header->dev_id); + blk_mq_kick_requeue_list(ub->ub_disk->queue); + } + ret = 0; out_unlock: mutex_unlock(&ub->mutex); diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h index c8dc5f8ea699..a2b3ea344639 100644 --- a/include/uapi/linux/ublk_cmd.h +++ b/include/uapi/linux/ublk_cmd.h @@ -147,8 +147,18 @@ */ #define UBLK_F_NEED_GET_DATA (1UL << 2) +/* + * - Block devices are recoverable if ublk server exits and restarts + * - Outstanding I/O when ublk server exits is met with errors + * - I/O issued while there is no ublk server queues + */ #define UBLK_F_USER_RECOVERY (1UL << 3) +/* + * - Block devices are recoverable if ublk server exits and restarts + * - Outstanding I/O when ublk server exits is reissued + * - I/O issued while there is no ublk server queues + */ #define UBLK_F_USER_RECOVERY_REISSUE (1UL << 4) /* @@ -184,10 +194,18 @@ */ #define UBLK_F_ZONED (1ULL << 8) +/* + * - Block devices are recoverable if ublk server exits and restarts + * - Outstanding I/O when ublk server exits is met with errors + * - I/O issued while there is no ublk server is met with errors + */ +#define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9) + /* device state */ #define UBLK_S_DEV_DEAD 0 #define UBLK_S_DEV_LIVE 1 #define UBLK_S_DEV_QUIESCED 2 +#define UBLK_S_DEV_FAIL_IO 3 /* shipped via sqe->cmd of io_uring command */ struct ublksrv_ctrl_cmd {