Skip to content

Commit 92dc689

Browse files
author
Christoph Hellwig
committed
nvme-pci: fix host memory buffer allocation fallback
nvme_alloc_host_mem currently contains two loops that are interwinded, and the outer retry loop turns out to be broken. Fix this by untangling the two. Based on a report an initial patch from Akinobu Mita. Signed-off-by: Christoph Hellwig <[email protected]> Reported-by: Akinobu Mita <[email protected]> Tested-by: Akinobu Mita <[email protected]> Reviewed-by: Keith Busch <[email protected]> Cc: [email protected]
1 parent 608cc4b commit 92dc689

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

drivers/nvme/host/pci.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,18 +1612,16 @@ static void nvme_free_host_mem(struct nvme_dev *dev)
16121612
dev->host_mem_descs = NULL;
16131613
}
16141614

1615-
static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1615+
static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
1616+
u32 chunk_size)
16161617
{
16171618
struct nvme_host_mem_buf_desc *descs;
1618-
u32 chunk_size, max_entries, len;
1619+
u32 max_entries, len;
16191620
dma_addr_t descs_dma;
16201621
int i = 0;
16211622
void **bufs;
16221623
u64 size = 0, tmp;
16231624

1624-
/* start big and work our way down */
1625-
chunk_size = min(preferred, (u64)PAGE_SIZE << MAX_ORDER);
1626-
retry:
16271625
tmp = (preferred + chunk_size - 1);
16281626
do_div(tmp, chunk_size);
16291627
max_entries = tmp;
@@ -1650,15 +1648,9 @@ static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
16501648
i++;
16511649
}
16521650

1653-
if (!size || (min && size < min)) {
1654-
dev_warn(dev->ctrl.device,
1655-
"failed to allocate host memory buffer.\n");
1651+
if (!size)
16561652
goto out_free_bufs;
1657-
}
16581653

1659-
dev_info(dev->ctrl.device,
1660-
"allocated %lld MiB host memory buffer.\n",
1661-
size >> ilog2(SZ_1M));
16621654
dev->nr_host_mem_descs = i;
16631655
dev->host_mem_size = size;
16641656
dev->host_mem_descs = descs;
@@ -1679,15 +1671,28 @@ static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
16791671
dma_free_coherent(dev->dev, max_entries * sizeof(*descs), descs,
16801672
descs_dma);
16811673
out:
1682-
/* try a smaller chunk size if we failed early */
1683-
if (chunk_size >= PAGE_SIZE * 2 && (i == 0 || size < min)) {
1684-
chunk_size /= 2;
1685-
goto retry;
1686-
}
16871674
dev->host_mem_descs = NULL;
16881675
return -ENOMEM;
16891676
}
16901677

1678+
static int nvme_alloc_host_mem(struct nvme_dev *dev, u64 min, u64 preferred)
1679+
{
1680+
u32 chunk_size;
1681+
1682+
/* start big and work our way down */
1683+
for (chunk_size = min_t(u64, preferred, PAGE_SIZE << MAX_ORDER);
1684+
chunk_size >= PAGE_SIZE * 2;
1685+
chunk_size /= 2) {
1686+
if (!__nvme_alloc_host_mem(dev, preferred, chunk_size)) {
1687+
if (!min || dev->host_mem_size >= min)
1688+
return 0;
1689+
nvme_free_host_mem(dev);
1690+
}
1691+
}
1692+
1693+
return -ENOMEM;
1694+
}
1695+
16911696
static void nvme_setup_host_mem(struct nvme_dev *dev)
16921697
{
16931698
u64 max = (u64)max_host_mem_size_mb * SZ_1M;
@@ -1715,8 +1720,15 @@ static void nvme_setup_host_mem(struct nvme_dev *dev)
17151720
}
17161721

17171722
if (!dev->host_mem_descs) {
1718-
if (nvme_alloc_host_mem(dev, min, preferred))
1723+
if (nvme_alloc_host_mem(dev, min, preferred)) {
1724+
dev_warn(dev->ctrl.device,
1725+
"failed to allocate host memory buffer.\n");
17191726
return;
1727+
}
1728+
1729+
dev_info(dev->ctrl.device,
1730+
"allocated %lld MiB host memory buffer.\n",
1731+
dev->host_mem_size >> ilog2(SZ_1M));
17201732
}
17211733

17221734
if (nvme_set_host_mem(dev, enable_bits))

0 commit comments

Comments
 (0)