|
38 | 38 |
|
39 | 39 | #define SGES_PER_PAGE (PAGE_SIZE / sizeof(struct nvme_sgl_desc))
|
40 | 40 |
|
| 41 | +/* |
| 42 | + * These can be higher, but we need to ensure that any command doesn't |
| 43 | + * require an sg allocation that needs more than a page of data. |
| 44 | + */ |
| 45 | +#define NVME_MAX_KB_SZ 4096 |
| 46 | +#define NVME_MAX_SEGS 127 |
| 47 | + |
41 | 48 | static int use_threaded_interrupts;
|
42 | 49 | module_param(use_threaded_interrupts, int, 0);
|
43 | 50 |
|
@@ -100,6 +107,8 @@ struct nvme_dev {
|
100 | 107 | struct nvme_ctrl ctrl;
|
101 | 108 | struct completion ioq_wait;
|
102 | 109 |
|
| 110 | + mempool_t *iod_mempool; |
| 111 | + |
103 | 112 | /* shadow doorbell buffer support: */
|
104 | 113 | u32 *dbbuf_dbs;
|
105 | 114 | dma_addr_t dbbuf_dbs_dma_addr;
|
@@ -477,10 +486,7 @@ static blk_status_t nvme_init_iod(struct request *rq, struct nvme_dev *dev)
|
477 | 486 | iod->use_sgl = nvme_pci_use_sgls(dev, rq);
|
478 | 487 |
|
479 | 488 | if (nseg > NVME_INT_PAGES || size > NVME_INT_BYTES(dev)) {
|
480 |
| - size_t alloc_size = nvme_pci_iod_alloc_size(dev, size, nseg, |
481 |
| - iod->use_sgl); |
482 |
| - |
483 |
| - iod->sg = kmalloc(alloc_size, GFP_ATOMIC); |
| 489 | + iod->sg = mempool_alloc(dev->iod_mempool, GFP_ATOMIC); |
484 | 490 | if (!iod->sg)
|
485 | 491 | return BLK_STS_RESOURCE;
|
486 | 492 | } else {
|
@@ -526,7 +532,7 @@ static void nvme_free_iod(struct nvme_dev *dev, struct request *req)
|
526 | 532 | }
|
527 | 533 |
|
528 | 534 | if (iod->sg != iod->inline_sg)
|
529 |
| - kfree(iod->sg); |
| 535 | + mempool_free(iod->sg, dev->iod_mempool); |
530 | 536 | }
|
531 | 537 |
|
532 | 538 | #ifdef CONFIG_BLK_DEV_INTEGRITY
|
@@ -2280,6 +2286,7 @@ static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
|
2280 | 2286 | blk_put_queue(dev->ctrl.admin_q);
|
2281 | 2287 | kfree(dev->queues);
|
2282 | 2288 | free_opal_dev(dev->ctrl.opal_dev);
|
| 2289 | + mempool_destroy(dev->iod_mempool); |
2283 | 2290 | kfree(dev);
|
2284 | 2291 | }
|
2285 | 2292 |
|
@@ -2334,6 +2341,13 @@ static void nvme_reset_work(struct work_struct *work)
|
2334 | 2341 | if (result)
|
2335 | 2342 | goto out;
|
2336 | 2343 |
|
| 2344 | + /* |
| 2345 | + * Limit the max command size to prevent iod->sg allocations going |
| 2346 | + * over a single page. |
| 2347 | + */ |
| 2348 | + dev->ctrl.max_hw_sectors = NVME_MAX_KB_SZ << 1; |
| 2349 | + dev->ctrl.max_segments = NVME_MAX_SEGS; |
| 2350 | + |
2337 | 2351 | result = nvme_init_identify(&dev->ctrl);
|
2338 | 2352 | if (result)
|
2339 | 2353 | goto out;
|
@@ -2509,6 +2523,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
|
2509 | 2523 | int node, result = -ENOMEM;
|
2510 | 2524 | struct nvme_dev *dev;
|
2511 | 2525 | unsigned long quirks = id->driver_data;
|
| 2526 | + size_t alloc_size; |
2512 | 2527 |
|
2513 | 2528 | node = dev_to_node(&pdev->dev);
|
2514 | 2529 | if (node == NUMA_NO_NODE)
|
@@ -2546,6 +2561,23 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
|
2546 | 2561 | if (result)
|
2547 | 2562 | goto release_pools;
|
2548 | 2563 |
|
| 2564 | + /* |
| 2565 | + * Double check that our mempool alloc size will cover the biggest |
| 2566 | + * command we support. |
| 2567 | + */ |
| 2568 | + alloc_size = nvme_pci_iod_alloc_size(dev, NVME_MAX_KB_SZ, |
| 2569 | + NVME_MAX_SEGS, true); |
| 2570 | + WARN_ON_ONCE(alloc_size > PAGE_SIZE); |
| 2571 | + |
| 2572 | + dev->iod_mempool = mempool_create_node(1, mempool_kmalloc, |
| 2573 | + mempool_kfree, |
| 2574 | + (void *) alloc_size, |
| 2575 | + GFP_KERNEL, node); |
| 2576 | + if (!dev->iod_mempool) { |
| 2577 | + result = -ENOMEM; |
| 2578 | + goto release_pools; |
| 2579 | + } |
| 2580 | + |
2549 | 2581 | dev_info(dev->ctrl.device, "pci function %s\n", dev_name(&pdev->dev));
|
2550 | 2582 |
|
2551 | 2583 | nvme_get_ctrl(&dev->ctrl);
|
|
0 commit comments