Skip to content

Commit 50cdb7c

Browse files
author
Christoph Hellwig
committed
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: Christoph Hellwig <[email protected]> Reported-by: Dan Carpenter <[email protected]> Reviewed-by: Keith Busch <[email protected]> Fixes: 87ad72a ("nvme-pci: implement host memory buffer support")
1 parent 9c5358e commit 50cdb7c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/nvme/host/pci.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ static void nvme_free_host_mem(struct nvme_dev *dev)
16191619
static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
16201620
{
16211621
struct nvme_host_mem_buf_desc *descs;
1622-
u32 chunk_size, max_entries;
1622+
u32 chunk_size, max_entries, len;
16231623
int i = 0;
16241624
void **bufs;
16251625
u64 size = 0, tmp;
@@ -1638,10 +1638,10 @@ static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
16381638
if (!bufs)
16391639
goto out_free_descs;
16401640

1641-
for (size = 0; size < preferred; size += chunk_size) {
1642-
u32 len = min_t(u64, chunk_size, preferred - size);
1641+
for (size = 0; size < preferred; size += len) {
16431642
dma_addr_t dma_addr;
16441643

1644+
len = min_t(u64, chunk_size, preferred - size);
16451645
bufs[i] = dma_alloc_attrs(dev->dev, len, &dma_addr, GFP_KERNEL,
16461646
DMA_ATTR_NO_KERNEL_MAPPING | DMA_ATTR_NO_WARN);
16471647
if (!bufs[i])

0 commit comments

Comments
 (0)