nvme-pci: factor the iod mempool creation into a helper
authorChristoph Hellwig <hch@lst.de>
Tue, 8 Nov 2022 08:41:41 +0000 (09:41 +0100)
committerChristoph Hellwig <hch@lst.de>
Tue, 15 Nov 2022 09:56:06 +0000 (10:56 +0100)
Add a helper to create the iod mempool.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
Tested-by Gerd Bayer <gbayer@linxu.ibm.com>

drivers/nvme/host/pci.c

index b638f43..f7dab65 100644 (file)
@@ -390,14 +390,6 @@ static int nvme_pci_npages_sgl(void)
                        PAGE_SIZE);
 }
 
-static size_t nvme_pci_iod_alloc_size(void)
-{
-       size_t npages = max(nvme_pci_npages_prp(), nvme_pci_npages_sgl());
-
-       return sizeof(__le64 *) * npages +
-               sizeof(struct scatterlist) * NVME_MAX_SEGS;
-}
-
 static int nvme_admin_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
                                unsigned int hctx_idx)
 {
@@ -2762,6 +2754,22 @@ static void nvme_release_prp_pools(struct nvme_dev *dev)
        dma_pool_destroy(dev->prp_small_pool);
 }
 
+static int nvme_pci_alloc_iod_mempool(struct nvme_dev *dev)
+{
+       size_t npages = max(nvme_pci_npages_prp(), nvme_pci_npages_sgl());
+       size_t alloc_size = sizeof(__le64 *) * npages +
+                           sizeof(struct scatterlist) * NVME_MAX_SEGS;
+
+       WARN_ON_ONCE(alloc_size > PAGE_SIZE);
+       dev->iod_mempool = mempool_create_node(1,
+                       mempool_kmalloc, mempool_kfree,
+                       (void *)alloc_size, GFP_KERNEL,
+                       dev_to_node(dev->dev));
+       if (!dev->iod_mempool)
+               return -ENOMEM;
+       return 0;
+}
+
 static void nvme_free_tagset(struct nvme_dev *dev)
 {
        if (dev->tagset.tags)
@@ -3087,7 +3095,6 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        int node, result = -ENOMEM;
        struct nvme_dev *dev;
        unsigned long quirks = id->driver_data;
-       size_t alloc_size;
 
        node = dev_to_node(&pdev->dev);
        if (node == NUMA_NO_NODE)
@@ -3132,21 +3139,9 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
                quirks |= NVME_QUIRK_SIMPLE_SUSPEND;
        }
 
-       /*
-        * Double check that our mempool alloc size will cover the biggest
-        * command we support.
-        */
-       alloc_size = nvme_pci_iod_alloc_size();
-       WARN_ON_ONCE(alloc_size > PAGE_SIZE);
-
-       dev->iod_mempool = mempool_create_node(1, mempool_kmalloc,
-                                               mempool_kfree,
-                                               (void *) alloc_size,
-                                               GFP_KERNEL, node);
-       if (!dev->iod_mempool) {
-               result = -ENOMEM;
+       result = nvme_pci_alloc_iod_mempool(dev);
+       if (result)
                goto release_pools;
-       }
 
        result = nvme_init_ctrl(&dev->ctrl, &pdev->dev, &nvme_pci_ctrl_ops,
                        quirks);