nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
PATCH 1/1 nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
There may be a race between nvme_reap_pending_cqes() and nvme_poll(), e.g., when doing live reset while polling the nvme device.
CPU X CPU Y
nvme_poll()nvme_dev_disable()
-> nvme_stop_queues()
-> nvme_suspend_io_queues()
-> nvme_suspend_queue()
-> spin_lock(&nvmeq->cq_poll_lock);
-> nvme_reap_pending_cqes()
-> nvme_process_cq() -> nvme_process_cq()
In the above scenario, the nvme_process_cq() for the same queue may be running on both CPU X and CPU Y concurrently.
It is much more easier to reproduce the issue when CONFIG_PREEMPT is enabled in kernel. When CONFIG_PREEMPT is disabled, it would take longer time for nvme_stop_queues()-->blk_mq_quiesce_queue() to wait for grace period.
This patch protects nvme_process_cq() with nvmeq->cq_poll_lock in nvme_reap_pending_cqes().
--- drivers/nvme/host/pci.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/nvme/host/pci.c +7 -4
@@ -1382,16 +1382,19 @@ static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)/* * Called only on a device that has been disabled and after all other threads -* that can check this device's completion queues have synced. This is the -* last chance for the driver to see a natural completion before -* nvme_cancel_request() terminates all incomplete requests. +* that can check this device's completion queues have synced, except +* nvme_poll(). This is the last chance for the driver to see a natural +* completion before nvme_cancel_request() terminates all incomplete requests. */ static void nvme_reap_pending_cqes (struct nvme_dev *dev) {int i; -for (i = dev->ctrl.queue_count -1 ; i >0 ; i--) +for (i = dev->ctrl.queue_count -1 ; i >0 ; i--) { +spin_lock (&dev->queues[i].cq_poll_lock);nvme_process_cq (&dev->queues[i]); +spin_unlock (&dev->queues[i].cq_poll_lock); + } } static int nvme_cmb_qdepth(struct nvme_dev *dev, int nr_io_queues,
Signature · 7 lines
2.17.1 _______________________________________________ linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 46 lines · Dongli Zhang
There may be a race between
nvme_reap_pending_cqes()andnvme_poll(), e.g., when doing live reset while polling the nvme device.CPU X CPU Y
nvme_poll()nvme_dev_disable()
->nvme_stop_queues()
->nvme_suspend_io_queues()
->nvme_suspend_queue()-> spin_lock(&nvmeq->cq_poll_lock);->
nvme_reap_pending_cqes()
->nvme_process_cq()->nvme_process_cq()In the above scenario, the
nvme_process_cq()for the same queue may be running on both CPU X and CPU Y concurrently.It is much more easier to reproduce the issue when
CONFIG_PREEMPTis enabled in kernel. WhenCONFIG_PREEMPTis disabled, it would take longer time for nvme_stop_queues()-->blk_mq_quiesce_queue() to wait for grace period.This patch protects
nvme_process_cq()withnvmeq->cq_poll_lockinnvme_reap_pending_cqes().Signed-off-by Dongli Zhang <[email protected]>--- drivers/nvme/host/pci.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)drivers/nvme/host/pci.c +3
-3@@ -1382,16 +1382,19 @@ static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown)/* * Called only on a device that has been disabled and after all other threads -* that can check this device's completion queues have synced. This is the -* last chance for the driver to see a natural completion before -* nvme_cancel_request() terminates all incomplete requests. +* that can check this device's completion queues have synced, except +* nvme_poll(). This is the last chance for the driver to see a natural +* completion before nvme_cancel_request() terminates all incomplete requests. */ static void nvme_reap_pending_cqes (struct nvme_dev *dev)
drivers/nvme/host/pci.c static void nvme_disable_admin_queue(struct nvme_dev *dev, bool shutdown) · line 1398
Quoted 10 lines
drivers/nvme/host/pci.c +4
-1{int i; -for (i = dev->ctrl.queue_count -1 ; i >0 ; i--) +for (i = dev->ctrl.queue_count -1 ; i >0 ; i--) { +spin_lock (&dev->queues[i].cq_poll_lock);nvme_process_cq (&dev->queues[i]); +spin_unlock (&dev->queues[i].cq_poll_lock); + } }
Looks a real race, and the fix is fine:
thanks,
Ming Lei
Looks good to me.
Christoph Hellwig
maintainerCan you add a Fixes tag so that this gets queued up for -stable properly?