Commit 50cdb7c6 authored by Christoph Hellwig's avatar Christoph Hellwig

nvme-pci: fix HMB size calculation

It's possible the preferred HMB size may not be a multiple of the
chunk_size. This patch moves len to function scope and uses that in
the for loop increment so the last iteration doesn't cause the total
size to exceed the allocated HMB size.

Based on an earlier patch from Keith Busch.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Reported-by: default avatarDan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: default avatarKeith Busch <keith.busch@intel.com>
Fixes: 87ad72a5 ("nvme-pci: implement host memory buffer support")
parent 9c5358e1
...@@ -1619,7 +1619,7 @@ static void nvme_free_host_mem(struct nvme_dev *dev) ...@@ -1619,7 +1619,7 @@ static void nvme_free_host_mem(struct nvme_dev *dev)
static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred) static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
{ {
struct nvme_host_mem_buf_desc *descs; struct nvme_host_mem_buf_desc *descs;
u32 chunk_size, max_entries; u32 chunk_size, max_entries, len;
int i = 0; int i = 0;
void **bufs; void **bufs;
u64 size = 0, tmp; u64 size = 0, tmp;
...@@ -1638,10 +1638,10 @@ static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred) ...@@ -1638,10 +1638,10 @@ static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
if (!bufs) if (!bufs)
goto out_free_descs; goto out_free_descs;
for (size = 0; size < preferred; size += chunk_size) { for (size = 0; size < preferred; size += len) {
u32 len = min_t(u64, chunk_size, preferred - size);
dma_addr_t dma_addr; dma_addr_t dma_addr;
len = min_t(u64, chunk_size, preferred - size);
bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL, bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN); DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
if (!bufs[i]) if (!bufs[i])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment