Skip to contentSkip to conversation

PCI: hv: Fix ring buffer size calculation

PATCH Superseded [email protected] 4 participants 4 messages Feb 13, 2024 – Feb 15, 2024

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: hv: Fix ring buffer size calculation

From: Michael Kelley <[email protected]>

For a physical PCI device that is passed through to a Hyper-V guest VM, current code specifies the VMBus ring buffer size as 4 pages. But this is an inappropriate dependency, since the amount of ring buffer space needed is unrelated to PAGE_SIZE. For example, on x86 the ring buffer size ends up as 16 Kbytes, while on ARM64 with 64 Kbyte pages, the ring size bloats to 256 Kbytes. The ring buffer for PCI pass-thru devices is used for only a few messages during device setup and removal, so any space above a few Kbytes is wasted.

Fix this by declaring the ring buffer size to be a fixed 16 Kbytes. Furthermore, use the VMBUS_RING_SIZE() macro so that the ring buffer header is properly accounted for, and so the size is rounded up to a page boundary, using the page size for which the kernel is built. While w/64 Kbyte pages this results in a 64 Kbyte ring buffer header plus a 64 Kbyte ring buffer, that's the smallest possible with that page size. It's still 128 Kbytes better than the current code.

---
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +1 -1
index 1eaffff40b8d..5f22ad38bb98 100644
@@ -465,7 +465,7 @@ struct pci_eject_response {
 	u32 status;
 } __packed;
 
-static int pci_ring_size = (4 * PAGE_SIZE);
+static int pci_ring_size = VMBUS_RING_SIZE(16 * 1024);
 
 /*
  * Driver specific state.
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · On 2/12/24 10:19 PM, [email protected] wrote:

From: Michael Kelley <[email protected]>

For a physical PCI device that is passed through to a Hyper-V guest VM, current code specifies the VMBus ring buffer size as 4 pages. But this is an inappropriate dependency, since the amount of ring buffer space needed is unrelated to PAGE_SIZE. For example, on x86 the ring buffer size ends up as 16 Kbytes, while on ARM64 with 64 Kbyte pages, the ring size bloats to 256 Kbytes. The ring buffer for PCI pass-thru devices is used for only a few messages during device setup and removal, so any space above a few Kbytes is wasted.

Fix this by declaring the ring buffer size to be a fixed 16 Kbytes. Furthermore, use the VMBUS_RING_SIZE() macro so that the ring buffer header is properly accounted for, and so the size is rounded up to a page boundary, using the page size for which the kernel is built. While w/64 Kbyte pages this results in a 64 Kbyte ring buffer header plus a 64 Kbyte ring buffer, that's the smallest possible with that page size. It's still 128 Kbytes better than the current code.

Quoted 3 lines

---

Looks good to me.

Quoted 8 lines
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +0 -0
index 1eaffff40b8d..5f22ad38bb98 100644

drivers/pci/controller/pci-hyperv.c struct pci_eject_response { · line 469

Quoted 6 lines
drivers/pci/controller/pci-hyperv.c +1 -1
@@ -465,7 +465,7 @@ struct pci_eject_response {
 	u32 status;
 } __packed;
 
-static int pci_ring_size = (4 * PAGE_SIZE);
+static int pci_ring_size = VMBUS_RING_SIZE(16 * 1024);
 

Nit: I think you can use SZ_16K to make it more readable.

Quoted 2 lines
drivers/pci/controller/pci-hyperv.c +0 -0
 /*
  * Driver specific state.
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#2 lore raw

From: Kuppuswamy Sathyanarayanan <[email protected]>

Quoted 38 lines · On 2/12/24 10:19 PM, [email protected] wrote:
Quoted 19 lines

From: Michael Kelley <[email protected]>

For a physical PCI device that is passed through to a Hyper-V guest VM, current code specifies the VMBus ring buffer size as 4 pages. But this is an inappropriate dependency, since the amount of ring buffer space needed is unrelated to PAGE_SIZE. For example, on x86 the ring buffer size ends up as 16 Kbytes, while on ARM64 with 64 Kbyte pages, the ring size bloats to 256 Kbytes. The ring buffer for PCI pass-thru devices is used for only a few messages during device setup and removal, so any space above a few Kbytes is wasted.

Fix this by declaring the ring buffer size to be a fixed 16 Kbytes. Furthermore, use the VMBUS_RING_SIZE() macro so that the ring buffer header is properly accounted for, and so the size is rounded up to a page boundary, using the page size for which the kernel is built. While w/64 Kbyte pages this results in a 64 Kbyte ring buffer header plus a 64 Kbyte ring buffer, that's the smallest possible with that page size. It's still 128 Kbytes better than the current code.

Quoted 3 lines

---

Looks good to me.

Quoted 11 lines
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +0 -0
index 1eaffff40b8d..5f22ad38bb98 100644
@@ -465,7 +465,7 @@ struct pci_eject_response {
 	u32 status;
 } __packed;
 
Quoted 4 lines

drivers/pci/controller/pci-hyperv.c struct pci_eject_response { · line 469

Quoted 3 lines
drivers/pci/controller/pci-hyperv.c +1 -1
-static int pci_ring_size = (4 * PAGE_SIZE);
+static int pci_ring_size = VMBUS_RING_SIZE(16 * 1024);
 

Nit: I think you can use SZ_16K to make it more readable.

Thanks for the review. I'll send a v2 that uses SZ_16K per your and Ilpo Järvinen's comment.

Michael

#3 lore raw
In reply to [email protected]
Quoted 30 lines · On Mon, 12 Feb 2024, [email protected] wrote:

From: Michael Kelley <[email protected]>

For a physical PCI device that is passed through to a Hyper-V guest VM, current code specifies the VMBus ring buffer size as 4 pages. But this is an inappropriate dependency, since the amount of ring buffer space needed is unrelated to PAGE_SIZE. For example, on x86 the ring buffer size ends up as 16 Kbytes, while on ARM64 with 64 Kbyte pages, the ring size bloats to 256 Kbytes. The ring buffer for PCI pass-thru devices is used for only a few messages during device setup and removal, so any space above a few Kbytes is wasted.

Fix this by declaring the ring buffer size to be a fixed 16 Kbytes. Furthermore, use the VMBUS_RING_SIZE() macro so that the ring buffer header is properly accounted for, and so the size is rounded up to a page boundary, using the page size for which the kernel is built. While w/64 Kbyte pages this results in a 64 Kbyte ring buffer header plus a 64 Kbyte ring buffer, that's the smallest possible with that page size. It's still 128 Kbytes better than the current code.

---
 drivers/pci/controller/pci-hyperv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/pci/controller/pci-hyperv.c +0 -0
index 1eaffff40b8d..5f22ad38bb98 100644

drivers/pci/controller/pci-hyperv.c struct pci_eject_response { · line 468

Quoted 5 lines
drivers/pci/controller/pci-hyperv.c +1 -1
@@ -465,7 +465,7 @@ struct pci_eject_response {
 	u32 status;
 } __packed;
 
-static int pci_ring_size = (4 * PAGE_SIZE);
+static int pci_ring_size = VMBUS_RING_SIZE(16 * 1024);

SZ_16K (and add the relevant #include if needed).

Signature · 1 line
 i.