Skip to content

Commit ecca390

Browse files
sagigrimbergChristoph Hellwig
authored andcommitted
nvme: fix deadlock in disconnect during scan_work and/or ana_work
A deadlock happens in the following scenario with multipath: 1) scan_work(nvme0) detects a new nsid while nvme0 is an optimized path to it, path nvme1 happens to be inaccessible. 2) Before scan_work is complete nvme0 disconnect is initiated nvme_delete_ctrl_sync() sets nvme0 state to NVME_CTRL_DELETING 3) scan_work(1) attempts to submit IO, but nvme_path_is_optimized() observes nvme0 is not LIVE. Since nvme1 is a possible path IO is requeued and scan_work hangs. -- Workqueue: nvme-wq nvme_scan_work [nvme_core] kernel: Call Trace: kernel: __schedule+0x2b9/0x6c0 kernel: schedule+0x42/0xb0 kernel: io_schedule+0x16/0x40 kernel: do_read_cache_page+0x438/0x830 kernel: read_cache_page+0x12/0x20 kernel: read_dev_sector+0x27/0xc0 kernel: read_lba+0xc1/0x220 kernel: efi_partition+0x1e6/0x708 kernel: check_partition+0x154/0x244 kernel: rescan_partitions+0xae/0x280 kernel: __blkdev_get+0x40f/0x560 kernel: blkdev_get+0x3d/0x140 kernel: __device_add_disk+0x388/0x480 kernel: device_add_disk+0x13/0x20 kernel: nvme_mpath_set_live+0x119/0x140 [nvme_core] kernel: nvme_update_ns_ana_state+0x5c/0x60 [nvme_core] kernel: nvme_set_ns_ana_state+0x1e/0x30 [nvme_core] kernel: nvme_parse_ana_log+0xa1/0x180 [nvme_core] kernel: nvme_mpath_add_disk+0x47/0x90 [nvme_core] kernel: nvme_validate_ns+0x396/0x940 [nvme_core] kernel: nvme_scan_work+0x24f/0x380 [nvme_core] kernel: process_one_work+0x1db/0x380 kernel: worker_thread+0x249/0x400 kernel: kthread+0x104/0x140 -- 4) Delete also hangs in flush_work(ctrl->scan_work) from nvme_remove_namespaces(). Similiarly a deadlock with ana_work may happen: if ana_work has started and calls nvme_mpath_set_live and device_add_disk, it will trigger I/O. When we trigger disconnect I/O will block because our accessible (optimized) path is disconnecting, but the alternate path is inaccessible, so I/O blocks. Then disconnect tries to flush the ana_work and hangs. [ 605.550896] Workqueue: nvme-wq nvme_ana_work [nvme_core] [ 605.552087] Call Trace: [ 605.552683] __schedule+0x2b9/0x6c0 [ 605.553507] schedule+0x42/0xb0 [ 605.554201] io_schedule+0x16/0x40 [ 605.555012] do_read_cache_page+0x438/0x830 [ 605.556925] read_cache_page+0x12/0x20 [ 605.557757] read_dev_sector+0x27/0xc0 [ 605.558587] amiga_partition+0x4d/0x4c5 [ 605.561278] check_partition+0x154/0x244 [ 605.562138] rescan_partitions+0xae/0x280 [ 605.563076] __blkdev_get+0x40f/0x560 [ 605.563830] blkdev_get+0x3d/0x140 [ 605.564500] __device_add_disk+0x388/0x480 [ 605.565316] device_add_disk+0x13/0x20 [ 605.566070] nvme_mpath_set_live+0x5e/0x130 [nvme_core] [ 605.567114] nvme_update_ns_ana_state+0x2c/0x30 [nvme_core] [ 605.568197] nvme_update_ana_state+0xca/0xe0 [nvme_core] [ 605.569360] nvme_parse_ana_log+0xa1/0x180 [nvme_core] [ 605.571385] nvme_read_ana_log+0x76/0x100 [nvme_core] [ 605.572376] nvme_ana_work+0x15/0x20 [nvme_core] [ 605.573330] process_one_work+0x1db/0x380 [ 605.574144] worker_thread+0x4d/0x400 [ 605.574896] kthread+0x104/0x140 [ 605.577205] ret_from_fork+0x35/0x40 [ 605.577955] INFO: task nvme:14044 blocked for more than 120 seconds. [ 605.579239] Tainted: G OE 5.3.5-050305-generic #201910071830 [ 605.580712] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 605.582320] nvme D 0 14044 14043 0x00000000 [ 605.583424] Call Trace: [ 605.583935] __schedule+0x2b9/0x6c0 [ 605.584625] schedule+0x42/0xb0 [ 605.585290] schedule_timeout+0x203/0x2f0 [ 605.588493] wait_for_completion+0xb1/0x120 [ 605.590066] __flush_work+0x123/0x1d0 [ 605.591758] __cancel_work_timer+0x10e/0x190 [ 605.593542] cancel_work_sync+0x10/0x20 [ 605.594347] nvme_mpath_stop+0x2f/0x40 [nvme_core] [ 605.595328] nvme_stop_ctrl+0x12/0x50 [nvme_core] [ 605.596262] nvme_do_delete_ctrl+0x3f/0x90 [nvme_core] [ 605.597333] nvme_sysfs_delete+0x5c/0x70 [nvme_core] [ 605.598320] dev_attr_store+0x17/0x30 Fix this by introducing a new state: NVME_CTRL_DELETE_NOIO, which will indicate the phase of controller deletion where I/O cannot be allowed to access the namespace. NVME_CTRL_DELETING still allows mpath I/O to be issued to the bottom device, and only after we flush the ana_work and scan_work (after nvme_stop_ctrl and nvme_prep_remove_namespaces) we change the state to NVME_CTRL_DELETING_NOIO. Also we prevent ana_work from re-firing by aborting early if we are not LIVE, so we should be safe here. In addition, change the transport drivers to follow the updated state machine. Fixes: 0d0b660 ("nvme: add ANA support") Reported-by: Anton Eidelman <[email protected]> Signed-off-by: Sagi Grimberg <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 4212f4e commit ecca390

File tree

8 files changed

+55
-15
lines changed

8 files changed

+55
-15
lines changed

drivers/nvme/host/core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
366366
break;
367367
}
368368
break;
369+
case NVME_CTRL_DELETING_NOIO:
370+
switch (old_state) {
371+
case NVME_CTRL_DELETING:
372+
case NVME_CTRL_DEAD:
373+
changed = true;
374+
/* FALLTHRU */
375+
default:
376+
break;
377+
}
378+
break;
369379
case NVME_CTRL_DEAD:
370380
switch (old_state) {
371381
case NVME_CTRL_DELETING:
@@ -403,6 +413,7 @@ static bool nvme_state_terminal(struct nvme_ctrl *ctrl)
403413
case NVME_CTRL_CONNECTING:
404414
return false;
405415
case NVME_CTRL_DELETING:
416+
case NVME_CTRL_DELETING_NOIO:
406417
case NVME_CTRL_DEAD:
407418
return true;
408419
default:
@@ -3476,6 +3487,7 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
34763487
[NVME_CTRL_RESETTING] = "resetting",
34773488
[NVME_CTRL_CONNECTING] = "connecting",
34783489
[NVME_CTRL_DELETING] = "deleting",
3490+
[NVME_CTRL_DELETING_NOIO]= "deleting (no IO)",
34793491
[NVME_CTRL_DEAD] = "dead",
34803492
};
34813493

@@ -4112,6 +4124,9 @@ void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
41124124
if (ctrl->state == NVME_CTRL_DEAD)
41134125
nvme_kill_queues(ctrl);
41144126

4127+
/* this is a no-op when called from the controller reset handler */
4128+
nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING_NOIO);
4129+
41154130
down_write(&ctrl->namespaces_rwsem);
41164131
list_splice_init(&ctrl->namespaces, &ns_list);
41174132
up_write(&ctrl->namespaces_rwsem);

drivers/nvme/host/fabrics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static struct nvmf_transport_ops *nvmf_lookup_transport(
547547
blk_status_t nvmf_fail_nonready_command(struct nvme_ctrl *ctrl,
548548
struct request *rq)
549549
{
550-
if (ctrl->state != NVME_CTRL_DELETING &&
550+
if (ctrl->state != NVME_CTRL_DELETING_NOIO &&
551551
ctrl->state != NVME_CTRL_DEAD &&
552552
!blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
553553
return BLK_STS_RESOURCE;

drivers/nvme/host/fabrics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ bool nvmf_ip_options_match(struct nvme_ctrl *ctrl,
182182
static inline bool nvmf_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
183183
bool queue_live)
184184
{
185-
if (likely(ctrl->state == NVME_CTRL_LIVE))
185+
if (likely(ctrl->state == NVME_CTRL_LIVE ||
186+
ctrl->state == NVME_CTRL_DELETING))
186187
return true;
187188
return __nvmf_check_ready(ctrl, rq, queue_live);
188189
}

drivers/nvme/host/fc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
826826
break;
827827

828828
case NVME_CTRL_DELETING:
829+
case NVME_CTRL_DELETING_NOIO:
829830
default:
830831
/* no action to take - let it delete */
831832
break;

drivers/nvme/host/multipath.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,18 @@ void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
167167

168168
static bool nvme_path_is_disabled(struct nvme_ns *ns)
169169
{
170-
return ns->ctrl->state != NVME_CTRL_LIVE ||
171-
test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
172-
test_bit(NVME_NS_REMOVING, &ns->flags);
170+
/*
171+
* We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
172+
* still be able to complete assuming that the controller is connected.
173+
* Otherwise it will fail immediately and return to the requeue list.
174+
*/
175+
if (ns->ctrl->state != NVME_CTRL_LIVE &&
176+
ns->ctrl->state != NVME_CTRL_DELETING)
177+
return true;
178+
if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
179+
test_bit(NVME_NS_REMOVING, &ns->flags))
180+
return true;
181+
return false;
173182
}
174183

175184
static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
@@ -563,6 +572,9 @@ static void nvme_ana_work(struct work_struct *work)
563572
{
564573
struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
565574

575+
if (ctrl->state != NVME_CTRL_LIVE)
576+
return;
577+
566578
nvme_read_ana_log(ctrl);
567579
}
568580

drivers/nvme/host/nvme.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ static inline u16 nvme_req_qid(struct request *req)
190190
* @NVME_CTRL_CONNECTING: Controller is disconnected, now connecting the
191191
* transport
192192
* @NVME_CTRL_DELETING: Controller is deleting (or scheduled deletion)
193+
* @NVME_CTRL_DELETING_NOIO: Controller is deleting and I/O is not
194+
* disabled/failed immediately. This state comes
195+
* after all async event processing took place and
196+
* before ns removal and the controller deletion
197+
* progress
193198
* @NVME_CTRL_DEAD: Controller is non-present/unresponsive during
194199
* shutdown or removal. In this case we forcibly
195200
* kill all inflight I/O as they have no chance to
@@ -201,6 +206,7 @@ enum nvme_ctrl_state {
201206
NVME_CTRL_RESETTING,
202207
NVME_CTRL_CONNECTING,
203208
NVME_CTRL_DELETING,
209+
NVME_CTRL_DELETING_NOIO,
204210
NVME_CTRL_DEAD,
205211
};
206212

drivers/nvme/host/rdma.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,11 +1102,12 @@ static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
11021102
changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
11031103
if (!changed) {
11041104
/*
1105-
* state change failure is ok if we're in DELETING state,
1105+
* state change failure is ok if we started ctrl delete,
11061106
* unless we're during creation of a new controller to
11071107
* avoid races with teardown flow.
11081108
*/
1109-
WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1109+
WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1110+
ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
11101111
WARN_ON_ONCE(new);
11111112
ret = -EINVAL;
11121113
goto destroy_io;
@@ -1159,8 +1160,9 @@ static void nvme_rdma_error_recovery_work(struct work_struct *work)
11591160
blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
11601161

11611162
if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
1162-
/* state change failure is ok if we're in DELETING state */
1163-
WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING);
1163+
/* state change failure is ok if we started ctrl delete */
1164+
WARN_ON_ONCE(ctrl->ctrl.state != NVME_CTRL_DELETING &&
1165+
ctrl->ctrl.state != NVME_CTRL_DELETING_NOIO);
11641166
return;
11651167
}
11661168

drivers/nvme/host/tcp.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,11 +1950,12 @@ static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
19501950

19511951
if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
19521952
/*
1953-
* state change failure is ok if we're in DELETING state,
1953+
* state change failure is ok if we started ctrl delete,
19541954
* unless we're during creation of a new controller to
19551955
* avoid races with teardown flow.
19561956
*/
1957-
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1957+
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
1958+
ctrl->state != NVME_CTRL_DELETING_NOIO);
19581959
WARN_ON_ONCE(new);
19591960
ret = -EINVAL;
19601961
goto destroy_io;
@@ -2010,8 +2011,9 @@ static void nvme_tcp_error_recovery_work(struct work_struct *work)
20102011
blk_mq_unquiesce_queue(ctrl->admin_q);
20112012

20122013
if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2013-
/* state change failure is ok if we're in DELETING state */
2014-
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
2014+
/* state change failure is ok if we started ctrl delete */
2015+
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2016+
ctrl->state != NVME_CTRL_DELETING_NOIO);
20152017
return;
20162018
}
20172019

@@ -2046,8 +2048,9 @@ static void nvme_reset_ctrl_work(struct work_struct *work)
20462048
nvme_tcp_teardown_ctrl(ctrl, false);
20472049

20482050
if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2049-
/* state change failure is ok if we're in DELETING state */
2050-
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
2051+
/* state change failure is ok if we started ctrl delete */
2052+
WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2053+
ctrl->state != NVME_CTRL_DELETING_NOIO);
20512054
return;
20522055
}
20532056

0 commit comments

Comments
 (0)