All of lore.kernel.org
 help / color / mirror / Atom feed
From: Max Gurtovoy <maxg@mellanox.com>
To: Weiping Zhang <zhangweiping@didiglobal.com>,
	hch@infradead.org, axboe@kernel.dk, kbusch@kernel.org,
	sagi@grimberg.me
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH] nvme: align io queue count with allocted nvme_queue in nvme_probe
Date: Sun, 12 Apr 2020 15:38:13 +0300	[thread overview]
Message-ID: <188ad279-9211-9dca-3e6c-b5718ae6fc80@mellanox.com> (raw)
In-Reply-To: <20200410095719.GA4393@192.168.3.9>

hi,

how about the following minor update:

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 4e79e41..46ab28b 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -89,6 +89,7 @@
   */
  struct nvme_dev {
         struct nvme_queue *queues;
+       int nr_allocated_queue;
         struct blk_mq_tag_set tagset;
         struct blk_mq_tag_set admin_tagset;
         u32 __iomem *dbs;
@@ -209,15 +210,15 @@ struct nvme_iod {
         struct scatterlist *sg;
  };

-static unsigned int max_io_queues(void)
+static unsigned int nr_dev_io_queues(struct nvme_dev *dev)
  {
-       return num_possible_cpus() + write_queues + poll_queues;
+       return dev->nr_allocated_queue - 1;
  }

  static unsigned int max_queue_count(void)
  {
         /* IO queues + admin queue */
-       return 1 + max_io_queues();
+       return 1 + num_possible_cpus() + write_queues + poll_queues;
  }

  static inline unsigned int nvme_dbbuf_size(u32 stride)
@@ -2073,7 +2074,7 @@ static int nvme_setup_io_queues(struct nvme_dev *dev)
         int result, nr_io_queues;
         unsigned long size;

-       nr_io_queues = max_io_queues();
+       nr_io_queues = nr_dev_io_queues(dev);

         /*
          * If tags are shared with admin queue (Apple bug), then
@@ -2742,7 +2743,7 @@ static void nvme_async_probe(void *data, 
async_cookie_t cookie)

  static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id 
*id)
  {
-       int node, result = -ENOMEM;
+       int node, nr_queues, result = -ENOMEM;
         struct nvme_dev *dev;
         unsigned long quirks = id->driver_data;
         size_t alloc_size;
@@ -2755,11 +2756,14 @@ static int nvme_probe(struct pci_dev *pdev, 
const struct pci_device_id *id)
         if (!dev)
                 return -ENOMEM;

-       dev->queues = kcalloc_node(max_queue_count(), sizeof(struct 
nvme_queue),
-                                       GFP_KERNEL, node);
+       nr_queues =  max_queue_count();
+       dev->queues = kcalloc_node(nr_queues, sizeof(struct nvme_queue),
+                                  GFP_KERNEL, node);
         if (!dev->queues)
                 goto free;

+       dev->nr_allocated_queue = nr_queues;
+
         dev->dev = get_device(&pdev->dev);
         pci_set_drvdata(pdev, dev);


-Max

On 4/10/2020 12:57 PM, Weiping Zhang wrote:
> Since the commit 147b27e4bd0 "nvme-pci: allocate device queues storage space at probe"
> nvme_alloc_queue will not alloc struct nvme_queue any more.
> If user change write/poll_queues to larger than the number of
> allocated queue in nvme_probe, nvme_alloc_queue will touch
> the memory out of boundary.
>
> Signed-off-by: Weiping Zhang <zhangweiping@didiglobal.com>
> ---
>   drivers/nvme/host/pci.c | 10 ++++++++--
>   1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
> index 4e79e412b276..cc10258e578e 100644
> --- a/drivers/nvme/host/pci.c
> +++ b/drivers/nvme/host/pci.c
> @@ -89,6 +89,7 @@ static bool __nvme_disable_io_queues(struct nvme_dev *dev, u8 opcode);
>    */
>   struct nvme_dev {
>   	struct nvme_queue *queues;
> +	int nr_allocated_queue;
>   	struct blk_mq_tag_set tagset;
>   	struct blk_mq_tag_set admin_tagset;
>   	u32 __iomem *dbs;
> @@ -2074,6 +2075,8 @@ static int nvme_setup_io_queues(struct nvme_dev *dev)
>   	unsigned long size;
>   
>   	nr_io_queues = max_io_queues();
> +	if (nr_io_queues > dev->nr_allocated_queue - 1)
> +		nr_io_queues = dev->nr_allocated_queue - 1;
>   
>   	/*
>   	 * If tags are shared with admin queue (Apple bug), then
> @@ -2742,7 +2745,7 @@ static void nvme_async_probe(void *data, async_cookie_t cookie)
>   
>   static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
>   {
> -	int node, result = -ENOMEM;
> +	int node, nr_queue, result = -ENOMEM;
>   	struct nvme_dev *dev;
>   	unsigned long quirks = id->driver_data;
>   	size_t alloc_size;
> @@ -2755,11 +2758,14 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
>   	if (!dev)
>   		return -ENOMEM;
>   
> -	dev->queues = kcalloc_node(max_queue_count(), sizeof(struct nvme_queue),
> +	nr_queue = max_queue_count();
> +	dev->queues = kcalloc_node(nr_queue, sizeof(struct nvme_queue),
>   					GFP_KERNEL, node);
>   	if (!dev->queues)
>   		goto free;
>   
> +	dev->nr_allocated_queue = nr_queue;
> +
>   	dev->dev = get_device(&pdev->dev);
>   	pci_set_drvdata(pdev, dev);
>   

_______________________________________________
linux-nvme mailing list
linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  reply	other threads:[~2020-04-12 12:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-10  9:57 [PATCH] nvme: align io queue count with allocted nvme_queue in nvme_probe Weiping Zhang
2020-04-12 12:38 ` Max Gurtovoy [this message]
2020-04-13  1:01   ` Weiping Zhang
2020-04-13  9:37     ` Max Gurtovoy
2020-04-13 12:00       ` Weiping Zhang
2020-04-14 12:59         ` Max Gurtovoy
2020-04-22  8:37           ` Christoph Hellwig
2020-04-22  9:24             ` weiping zhang
2020-04-22 16:57               ` Christoph Hellwig
2020-04-23  7:59                 ` Weiping Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=188ad279-9211-9dca-3e6c-b5718ae6fc80@mellanox.com \
    --to=maxg@mellanox.com \
    --cc=axboe@kernel.dk \
    --cc=hch@infradead.org \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    --cc=zhangweiping@didiglobal.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.