Skip to contentSkip to conversation

nvme-pci: Fix mempool alloc size

PATCH Applied Keith Busch 5 participants 5 messages Dec 19, 2022 – Dec 21, 2022 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-pci: Fix mempool alloc size

From: Keith Busch <[email protected]>

Convert the max size to bytes to match the units of the divisor that calculates the worst-case number of PRP entries.

The result is used to determine how many PRP Lists are required. The code was previously rounding this to 1 list, but we can require 2 in the worst case. In that scenario, the driver would corrupt memory beyond the size provided by the mempool.

While unlikely to occur (you'd need a 4MB in exactly 127 phys segments on a queue that doesn't support SGLs), this memory corruption has been observed by kfence.

Cc Jens Axboe <[email protected]>
Fixes 943e942e6266f ("nvme-pci: limit max IO size and segments to avoid high order allocations")
---
 drivers/nvme/host/pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/pci.c +2 -2
index f0f8027644bbf..fa182fcd4c3e8 100644
@@ -380,8 +380,8 @@ static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
  */
 static int nvme_pci_npages_prp(void)
 {
-	unsigned nprps = DIV_ROUND_UP(NVME_MAX_KB_SZ + NVME_CTRL_PAGE_SIZE,
-				      NVME_CTRL_PAGE_SIZE);
+	unsigned max_bytes = (NVME_MAX_KB_SZ * 1024) + NVME_CTRL_PAGE_SIZE;
+	unsigned nprps = DIV_ROUND_UP(max_bytes, NVME_CTRL_PAGE_SIZE);
 	return DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
 }
 
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Keith Busch

From: Keith Busch <[email protected]>

Convert the max size to bytes to match the units of the divisor that calculates the worst-case number of PRP entries.

Quoted 8 lines

The result is used to determine how many PRP Lists are required. The code was previously rounding this to 1 list, but we can require 2 in the worst case. In that scenario, the driver would corrupt memory beyond the size provided by the mempool.

While unlikely to occur (you'd need a 4MB in exactly 127 phys segments on a queue that doesn't support SGLs), this memory corruption has been observed by kfence.

Good catch!

Signature · 1 line
Jens Axboe
Reviewed-by on v1 1/1
#2 lore raw
In reply to Keith Busch
Quoted 10 lines · Keith Busch

From: Keith Busch <[email protected]>

Convert the max size to bytes to match the units of the divisor that calculates the worst-case number of PRP entries.

The result is used to determine how many PRP Lists are required. The code was previously rounding this to 1 list, but we can require 2 in the worst case. In that scenario, the driver would corrupt memory beyond the size provided by the mempool.

Quoted 8 lines

While unlikely to occur (you'd need a 4MB in exactly 127 phys segments on a queue that doesn't support SGLs), this memory corruption has been observed by kfence.

Cc Jens Axboe <[email protected]>
Fixes 943e942e6266f ("nvme-pci: limit max IO size and segments to avoid high order allocations")

---

hmm, surprising to see that we never caught this until today...

-ck

Reviewed-by on v1 1/1
#3 lore raw
In reply to Keith Busch
Quoted 26 lines · Keith Busch

From: Keith Busch <[email protected]>

Convert the max size to bytes to match the units of the divisor that calculates the worst-case number of PRP entries.

The result is used to determine how many PRP Lists are required. The code was previously rounding this to 1 list, but we can require 2 in the worst case. In that scenario, the driver would corrupt memory beyond the size provided by the mempool.

While unlikely to occur (you'd need a 4MB in exactly 127 phys segments on a queue that doesn't support SGLs), this memory corruption has been observed by kfence.

Cc Jens Axboe <[email protected]>
Fixes 943e942e6266f ("nvme-pci: limit max IO size and segments to avoid high order allocations")

---
drivers/nvme/host/pci.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/pci.c +0 -0
index f0f8027644bbf..fa182fcd4c3e8 100644

drivers/nvme/host/pci.c static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db, · line 384

Quoted 5 lines
drivers/nvme/host/pci.c +0 -2
@@ -380,8 +380,8 @@ static bool nvme_dbbuf_update_and_check_event(u16 value, u32 *dbbuf_db,
 */
 static int nvme_pci_npages_prp(void)
 {
-	unsigned nprps = DIV_ROUND_UP(NVME_MAX_KB_SZ + NVME_CTRL_PAGE_SIZE,
-				      NVME_CTRL_PAGE_SIZE);

Similar calculation is present in apple.c too.
Regardless, this looks good.