Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE344C2B9F4 for ; Tue, 22 Jun 2021 07:02:06 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 58F546044F for ; Tue, 22 Jun 2021 07:02:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 58F546044F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=HyoYTFNJdue+A/jtR5Hl8oahO0pDHPWXj5yl2b3qRgs=; b=01niDR1+iDfw56 x7CQ1b5/j/GRhUD/UlP31qSJTxhTFGJIxQ8tB2b/G5MTNj+lGvVFXx7DqXtkhohKG7gtH0ZPY6GU0 cYyGcNf45o6pNqZjUxiJfB+gBFHSQroO5RVaFommeK57mUcGfMKl9wVsZTCviBOHVXw8sZrnCz5rs niozsZewk146GWslGBXs8P3SqGfXwQXGGpPafQWAvgIL+5ZpStjC7eL25EF1OEPDlywjyio6t8rVh ENz5Xs5Voq9Uc/UbPxBGN17jkksRFipeXRLKFPq+d2t8GwvQBbEaJdOi/e0Ap17SkFh5WpanirH4x Lbp1FSOBE14oqYAaniMw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lvaPu-005ve5-KI; Tue, 22 Jun 2021 07:01:50 +0000 Received: from smtp-out2.suse.de ([195.135.220.29]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lvaPr-005vda-JB for linux-nvme@lists.infradead.org; Tue, 22 Jun 2021 07:01:49 +0000 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 44A441FD45; Tue, 22 Jun 2021 07:01:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1624345306; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=jUeS9MHCquKCFE1u4gUHKXBKGK2DtieQfYLKX6L/YEg=; b=xz+im6Q4C1RdfF3vi+JIOFg9zCkVL88LaoZnE9g9fn+6pnwgqEp62FqPbTRMwUwGaxQo68 pjsBRMkl8MA8vf5NeHJRX4LoyXQNV8yciDSaiChnCpf4KBIGJPxVwyXtj0EFIum5hGr/Ki YDuOwNnVMlqNAPfsH/PUvQCXdNwPwsM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1624345306; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=jUeS9MHCquKCFE1u4gUHKXBKGK2DtieQfYLKX6L/YEg=; b=kIuyl69W2G8pX6mjF9ZQVN1oqNdOs1/anZgqEO8x8pJENZCi0uuHl/yCkn6lM9u4oaCrbu 7aVF50kzes4wQgAg== Received: from adalid.arch.suse.de (adalid.arch.suse.de [10.161.8.13]) by relay2.suse.de (Postfix) with ESMTP id 174B3A3B84; Tue, 22 Jun 2021 07:01:45 +0000 (UTC) Received: by adalid.arch.suse.de (Postfix, from userid 16045) id 3FB9A5170795; Tue, 22 Jun 2021 09:01:45 +0200 (CEST) From: Hannes Reinecke To: Christoph Hellwig Cc: Sagi Grimberg , Keith Busch , linux-nvme@lists.infradead.org, Hannes Reinecke Subject: [PATCHv7] nvme: fix refcounting imbalance when all paths are down Date: Tue, 22 Jun 2021 09:01:40 +0200 Message-Id: <20210622070140.22488-1-hare@suse.de> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210622_000147_838935_738B44F0 X-CRM114-Status: GOOD ( 17.76 ) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org When the last path to a ns_head drops the current code removes the ns_head from the subsystem list, but will only delete the disk itself if the last reference to the ns_head drops. This is causing an refcounting imbalance eg when applications have a reference to the disk, as then they'll never get notified that the disk is in fact dead. With this patch we are calling 'del_gendisk' and remove it from the list of ns_heads at the same time, ensuring that the disk can be properly removed and applications get the appropriate notifications. Changes to v6: - Move the list_del() into nvme_mpath_check_last_path() - Drop the tests for GENHD_FL_UP Changes to v5: - Synchronize between nvme_init_ns_head() and nvme_mpath_check_last_path() - Check for removed gendisk in nvme_ns_head_submit_bio() Changes to v4: - Call del_gendisk() in nvme_mpath_check_last_path() to avoid deadlock Changes to v3: - Simplify if() clause to detect duplicate namespaces Changes to v2: - Drop memcpy() statement Changes to v1: - Always check NSIDs after reattach Signed-off-by: Hannes Reinecke --- drivers/nvme/host/core.c | 6 +----- drivers/nvme/host/multipath.c | 28 +++++++++++++++++++++++----- drivers/nvme/host/nvme.h | 11 ++--------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index c7ef0b6684b5..106322dafe45 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -3799,8 +3799,6 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid, out_unlink_ns: mutex_lock(&ctrl->subsys->lock); list_del_rcu(&ns->siblings); - if (list_empty(&ns->head->list)) - list_del_init(&ns->head->entry); mutex_unlock(&ctrl->subsys->lock); nvme_put_ns_head(ns->head); out_free_queue: @@ -3821,8 +3819,6 @@ static void nvme_ns_remove(struct nvme_ns *ns) mutex_lock(&ns->ctrl->subsys->lock); list_del_rcu(&ns->siblings); - if (list_empty(&ns->head->list)) - list_del_init(&ns->head->entry); mutex_unlock(&ns->ctrl->subsys->lock); synchronize_rcu(); /* guarantee not available in head->list */ @@ -3842,7 +3838,7 @@ static void nvme_ns_remove(struct nvme_ns *ns) list_del_init(&ns->list); up_write(&ns->ctrl->namespaces_rwsem); - nvme_mpath_check_last_path(ns); + nvme_mpath_check_last_path(ns->head); nvme_put_ns(ns); } diff --git a/drivers/nvme/host/multipath.c b/drivers/nvme/host/multipath.c index 23573fe3fc7d..bf9dfaefa77d 100644 --- a/drivers/nvme/host/multipath.c +++ b/drivers/nvme/host/multipath.c @@ -771,20 +771,38 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id) #endif } -void nvme_mpath_remove_disk(struct nvme_ns_head *head) +void nvme_mpath_check_last_path(struct nvme_ns_head *head) { + bool last_path = false; if (!head->disk) return; - if (head->disk->flags & GENHD_FL_UP) { - nvme_cdev_del(&head->cdev, &head->cdev_device); - del_gendisk(head->disk); + + /* Synchronize with nvme_init_ns_head() */ + mutex_lock(&head->subsys->lock); + if (list_empty(&head->list)) { + list_del_init(&head->entry); + last_path = true; } + mutex_unlock(&head->subsys->lock); + if (last_path) { + kblockd_schedule_work(&head->requeue_work); + if (head->disk->flags & GENHD_FL_UP) { + nvme_cdev_del(&head->cdev, &head->cdev_device); + del_gendisk(head->disk); + } + } +} + +void nvme_mpath_remove_disk(struct nvme_ns_head *head) +{ + if (!head->disk) + return; blk_set_queue_dying(head->disk->queue); /* make sure all pending bios are cleaned up */ kblockd_schedule_work(&head->requeue_work); flush_work(&head->requeue_work); blk_cleanup_queue(head->disk->queue); - if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { + if (!test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) { /* * if device_add_disk wasn't called, prevent * disk release to put a bogus reference on the diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index 75420ceacc10..6b1caabe861b 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -716,14 +716,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl); void nvme_mpath_stop(struct nvme_ctrl *ctrl); bool nvme_mpath_clear_current_path(struct nvme_ns *ns); void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl); - -static inline void nvme_mpath_check_last_path(struct nvme_ns *ns) -{ - struct nvme_ns_head *head = ns->head; - - if (head->disk && list_empty(&head->list)) - kblockd_schedule_work(&head->requeue_work); -} +void nvme_mpath_check_last_path(struct nvme_ns_head *head); static inline void nvme_trace_bio_complete(struct request *req) { @@ -772,7 +765,7 @@ static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns) static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl) { } -static inline void nvme_mpath_check_last_path(struct nvme_ns *ns) +static inline void nvme_mpath_check_last_path(struct nvme_ns_head *head) { } static inline void nvme_trace_bio_complete(struct request *req) -- 2.26.2 _______________________________________________ Linux-nvme mailing list Linux-nvme@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-nvme