Skip to content

Commit 5396fda

Browse files
hreineckeChristoph Hellwig
authored andcommitted
nvme: fix refcounting imbalance when all paths are down
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. This patch moves the call 'del_gendisk' into nvme_mpath_check_last_path(), ensuring that the disk can be properly removed and applications get the appropriate notifications. Signed-off-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Keith Busch <kbusch@kernel.org> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 7764656 commit 5396fda

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

drivers/nvme/host/core.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3807,6 +3807,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
38073807

38083808
static void nvme_ns_remove(struct nvme_ns *ns)
38093809
{
3810+
bool last_path = false;
3811+
38103812
if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
38113813
return;
38123814

@@ -3815,8 +3817,6 @@ static void nvme_ns_remove(struct nvme_ns *ns)
38153817

38163818
mutex_lock(&ns->ctrl->subsys->lock);
38173819
list_del_rcu(&ns->siblings);
3818-
if (list_empty(&ns->head->list))
3819-
list_del_init(&ns->head->entry);
38203820
mutex_unlock(&ns->ctrl->subsys->lock);
38213821

38223822
synchronize_rcu(); /* guarantee not available in head->list */
@@ -3836,7 +3836,15 @@ static void nvme_ns_remove(struct nvme_ns *ns)
38363836
list_del_init(&ns->list);
38373837
up_write(&ns->ctrl->namespaces_rwsem);
38383838

3839-
nvme_mpath_check_last_path(ns);
3839+
/* Synchronize with nvme_init_ns_head() */
3840+
mutex_lock(&ns->head->subsys->lock);
3841+
if (list_empty(&ns->head->list)) {
3842+
list_del_init(&ns->head->entry);
3843+
last_path = true;
3844+
}
3845+
mutex_unlock(&ns->head->subsys->lock);
3846+
if (last_path)
3847+
nvme_mpath_shutdown_disk(ns->head);
38403848
nvme_put_ns(ns);
38413849
}
38423850

drivers/nvme/host/multipath.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,21 @@ void nvme_mpath_add_disk(struct nvme_ns *ns, struct nvme_id_ns *id)
760760
#endif
761761
}
762762

763-
void nvme_mpath_remove_disk(struct nvme_ns_head *head)
763+
void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
764764
{
765765
if (!head->disk)
766766
return;
767+
kblockd_schedule_work(&head->requeue_work);
767768
if (head->disk->flags & GENHD_FL_UP) {
768769
nvme_cdev_del(&head->cdev, &head->cdev_device);
769770
del_gendisk(head->disk);
770771
}
772+
}
773+
774+
void nvme_mpath_remove_disk(struct nvme_ns_head *head)
775+
{
776+
if (!head->disk)
777+
return;
771778
blk_set_queue_dying(head->disk->queue);
772779
/* make sure all pending bios are cleaned up */
773780
kblockd_schedule_work(&head->requeue_work);

drivers/nvme/host/nvme.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,7 @@ void nvme_mpath_uninit(struct nvme_ctrl *ctrl);
716716
void nvme_mpath_stop(struct nvme_ctrl *ctrl);
717717
bool nvme_mpath_clear_current_path(struct nvme_ns *ns);
718718
void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl);
719-
720-
static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
721-
{
722-
struct nvme_ns_head *head = ns->head;
723-
724-
if (head->disk && list_empty(&head->list))
725-
kblockd_schedule_work(&head->requeue_work);
726-
}
719+
void nvme_mpath_shutdown_disk(struct nvme_ns_head *head);
727720

728721
static inline void nvme_trace_bio_complete(struct request *req)
729722
{
@@ -772,7 +765,7 @@ static inline bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
772765
static inline void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
773766
{
774767
}
775-
static inline void nvme_mpath_check_last_path(struct nvme_ns *ns)
768+
static inline void nvme_mpath_shutdown_disk(struct nvme_ns_head *head)
776769
{
777770
}
778771
static inline void nvme_trace_bio_complete(struct request *req)

0 commit comments

Comments
 (0)