linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
@ 2020-05-27  0:49 Dongli Zhang
  2020-05-27  2:37 ` Ming Lei
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Dongli Zhang @ 2020-05-27  0:49 UTC (permalink / raw)
  To: linux-nvme; +Cc: linux-kernel, kbusch, axboe, hch, sagi

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().

Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
---
 drivers/nvme/host/pci.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 3726dc780d15..cc46e250fcac 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -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,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
  2020-05-27  0:49 [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll() Dongli Zhang
@ 2020-05-27  2:37 ` Ming Lei
  2020-05-27  3:27 ` Keith Busch
  2020-05-27  5:05 ` Christoph Hellwig
  2 siblings, 0 replies; 5+ messages in thread
From: Ming Lei @ 2020-05-27  2:37 UTC (permalink / raw)
  To: Dongli Zhang
  Cc: linux-nvme, Keith Busch, Jens Axboe, Sagi Grimberg,
	Linux Kernel Mailing List, Christoph Hellwig

On Wed, May 27, 2020 at 8:57 AM Dongli Zhang <dongli.zhang@oracle.com> wrote:
>
> 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().
>
> Signed-off-by: Dongli Zhang <dongli.zhang@oracle.com>
> ---
>  drivers/nvme/host/pci.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
> index 3726dc780d15..cc46e250fcac 100644
> --- a/drivers/nvme/host/pci.c
> +++ b/drivers/nvme/host/pci.c
> @@ -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);
> +       }
>  }

Looks a real race, and the fix is fine:

Reviewed-by: Ming Lei <ming.lei@redhat.com>


thanks,
Ming Lei

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
  2020-05-27  0:49 [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll() Dongli Zhang
  2020-05-27  2:37 ` Ming Lei
@ 2020-05-27  3:27 ` Keith Busch
  2020-05-27  5:05 ` Christoph Hellwig
  2 siblings, 0 replies; 5+ messages in thread
From: Keith Busch @ 2020-05-27  3:27 UTC (permalink / raw)
  To: Dongli Zhang; +Cc: linux-nvme, linux-kernel, axboe, hch, sagi

Looks good to me.

Reviewed-by: Keith Busch <kbusch@kernel.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
  2020-05-27  0:49 [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll() Dongli Zhang
  2020-05-27  2:37 ` Ming Lei
  2020-05-27  3:27 ` Keith Busch
@ 2020-05-27  5:05 ` Christoph Hellwig
  2020-05-27  5:18   ` Keith Busch
  2 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2020-05-27  5:05 UTC (permalink / raw)
  To: Dongli Zhang; +Cc: linux-nvme, linux-kernel, kbusch, axboe, hch, sagi

Can you add a Fixes tag so that this gets queued up for -stable properly?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll()
  2020-05-27  5:05 ` Christoph Hellwig
@ 2020-05-27  5:18   ` Keith Busch
  0 siblings, 0 replies; 5+ messages in thread
From: Keith Busch @ 2020-05-27  5:18 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Dongli Zhang, linux-nvme, linux-kernel, axboe, sagi

Fixes: fa46c6fb5d61 ("nvme/pci: move cqe check after device shutdown")

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-05-27  5:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-27  0:49 [PATCH 1/1] nvme-pci: avoid race between nvme_reap_pending_cqes() and nvme_poll() Dongli Zhang
2020-05-27  2:37 ` Ming Lei
2020-05-27  3:27 ` Keith Busch
2020-05-27  5:05 ` Christoph Hellwig
2020-05-27  5:18   ` Keith Busch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).