nvme: fix deadlock caused by ana update wrong locking
Sagi Grimberg
maintainerPATCH 1/1 nvme: fix deadlock caused by ana update wrong locking
The deadlock combines 4 flows in parallel:
- ns scanning (triggered from reconnect)
- request timeout
- ANA update (triggered from reconnect)
- I/O coming into the mpath device
(1) ns scanning triggers disk revalidation -> update disk info ->
freeze queue -> but blocked, due to (2)
(2) timeout handler reference the g_usage_counter - > but blocks in the transport .timeout() handler, due to (3)
(3) the transport timeout handler (indirectly) calls nvme_stop_queue() -> which takes the (down_read) namespaces_rwsem - > but blocks, due to (4)
(4) ANA update takes the (down_write) namespaces_rwsem -> calls
nvme_mpath_set_live() -> which synchronize the ns_head srcu
(see commit 504db087aacc) -> but blocks, due to (5)(5) I/O came into nvme_mpath_make_request -> took srcu_read_lock ->
direct_make_request > blk_queue_enter -> but blocked, due to (1)
==> the request queue is under freeze -> deadlock.
The fix is making ANA update take a read lock as the namespaces list is not manipulated, it is just the ns and ns->head (which is protected with the ns->head lock.
--- drivers/nvme/host/multipath.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/multipath.c +2 -2
@@ -510,7 +510,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (!nr_nsids)return 0 ; -down_write (&ctrl->namespaces_rwsem); +down_read (&ctrl->namespaces_rwsem);list_for_each_entry (ns, &ctrl->namespaces, list) {unsigned nsid =le32_to_cpu (desc->nsids[n]);
@@ -521,7 +521,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (++n == nr_nsids)break ; } -up_write (&ctrl->namespaces_rwsem); +up_read (&ctrl->namespaces_rwsem);return 0 ; }
Signature · 7 lines
2.20.1 _______________________________________________ linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 22 lines · Sagi Grimberg
The deadlock combines 4 flows in parallel:
- ns scanning (triggered from reconnect)
- request timeout
- ANA update (triggered from reconnect)
- I/O coming into the mpath device(1) ns scanning triggers disk revalidation -> update disk info ->
freeze queue -> but blocked, due to (2)(2) timeout handler reference the
g_usage_counter- > but blocks in the transport .timeout() handler, due to (3)(3) the transport timeout handler (indirectly) calls
nvme_stop_queue()-> which takes the (down_read)namespaces_rwsem- > but blocks, due to (4)(4) ANA update takes the (
down_write)namespaces_rwsem-> callsnvme_mpath_set_live() -> which synchronize the ns_head srcu (see commit 504db087aacc) -> but blocks, due to (5)(5) I/O came into
nvme_mpath_make_request-> tooksrcu_read_lock->direct_make_request > blk_queue_enter -> but blocked, due to (1)
Quoted 7 lines
==> the request queue is under freeze -> deadlock.
The fix is making ANA update take a read lock as the namespaces list is not manipulated, it is just the ns and
ns->head(which is protected with thens->headlock.Signed-off-by Sagi Grimberg <[email protected]>
Patch looks good to me,
I think this could be a stable candidate, so may want to include a fixes tag:
Quoted 18 lines
--- drivers/nvme/host/multipath.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/nvme/host/multipath.c +1
-1@@ -510,7 +510,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (!nr_nsids)return 0 ; -down_write (&ctrl->namespaces_rwsem); +down_read (&ctrl->namespaces_rwsem);list_for_each_entry (ns, &ctrl->namespaces, list) {unsigned nsid =le32_to_cpu (desc->nsids[n]);
drivers/nvme/host/multipath.c static int nvme_update_ana_state(struct nvme_ctrl *ctrl, · line 526
Quoted 7 lines
drivers/nvme/host/multipath.c +1
-1@@ -521,7 +521,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (++n == nr_nsids)break ; } -up_write (&ctrl->namespaces_rwsem); +up_read (&ctrl->namespaces_rwsem);return 0 ; }
_______________________________________________
linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 48 lines · Sagi Grimberg
The deadlock combines 4 flows in parallel:
- ns scanning (triggered from reconnect)
- request timeout
- ANA update (triggered from reconnect)
- I/O coming into the mpath device(1) ns scanning triggers disk revalidation -> update disk info ->
freeze queue -> but blocked, due to (2)(2) timeout handler reference the
g_usage_counter- > but blocks in the transport .timeout() handler, due to (3)(3) the transport timeout handler (indirectly) calls
nvme_stop_queue()-> which takes the (down_read)namespaces_rwsem- > but blocks, due to (4)(4) ANA update takes the (
down_write)namespaces_rwsem-> callsnvme_mpath_set_live() -> which synchronize the ns_head srcu (see commit 504db087aacc) -> but blocks, due to (5)(5) I/O came into
nvme_mpath_make_request-> tooksrcu_read_lock->direct_make_request > blk_queue_enter -> but blocked, due to (1)==> the request queue is under freeze -> deadlock.
The fix is making ANA update take a read lock as the namespaces list is not manipulated, it is just the ns and
ns->head(which is protected with thens->headlock.Signed-off-by Sagi Grimberg <[email protected]>--- drivers/nvme/host/multipath.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/nvme/host/multipath.c +1
-1@@ -510,7 +510,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (!nr_nsids)return 0 ; -down_write (&ctrl->namespaces_rwsem); +down_read (&ctrl->namespaces_rwsem);list_for_each_entry (ns, &ctrl->namespaces, list) {unsigned nsid =le32_to_cpu (desc->nsids[n]);
drivers/nvme/host/multipath.c static int nvme_update_ana_state(struct nvme_ctrl *ctrl, · line 527
Quoted 8 lines
drivers/nvme/host/multipath.c +1
-1@@ -521,7 +521,7 @@ static int nvme_update_ana_state(struct nvme_ctrl *ctrl,if (++n == nr_nsids)break ; } -up_write (&ctrl->namespaces_rwsem); +up_read (&ctrl->namespaces_rwsem);return 0 ; }
Cheers,
Hannes
Signature · 9 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer _______________________________________________ linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme