| From 13ef9d57af95de5aadee236451cac7997325c2bf Mon Sep 17 00:00:00 2001 |
| From: Dongli Zhang <dongli.zhang@oracle.com> |
| Date: Wed, 27 May 2020 09:13:52 -0700 |
| Subject: [PATCH] nvme-pci: avoid race between nvme_reap_pending_cqes() and |
| nvme_poll() |
| |
| commit 9210c075cef29c1f764b4252f93105103bdfb292 upstream. |
| |
| 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(). |
| |
| Fixes: fa46c6fb5d61 ("nvme/pci: move cqe check after device shutdown") |
| Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com> |
| Reviewed-by: Ming Lei <ming.lei@redhat.com> |
| Reviewed-by: Keith Busch <kbusch@kernel.org> |
| Signed-off-by: Christoph Hellwig <hch@lst.de> |
| [PG: v5.2 has open/close {} still, since v5.2 doesn't have 324b494c2862] |
| Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com> |
| |
| diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c |
| index 27eabb53cd00..a27cc8df10af 100644 |
| --- a/drivers/nvme/host/pci.c |
| +++ b/drivers/nvme/host/pci.c |
| @@ -1422,9 +1422,9 @@ 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) |
| { |
| @@ -1432,7 +1432,9 @@ static void nvme_reap_pending_cqes(struct nvme_dev *dev) |
| int 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], &start, &end, -1); |
| + spin_unlock(&dev->queues[i].cq_poll_lock); |
| nvme_complete_cqes(&dev->queues[i], start, end); |
| } |
| } |
| -- |
| 2.7.4 |
| |