Skip to contentSkip to conversation

unexport swiotlb_active

PATCH In review Christoph Hellwig 6 participants 22 messages May 18, 2023 – Jun 12, 2023

Subsystems (10)
DRM drivers x86 DRM misc PCI +6 more subsystems
lore
Share & tools
lore
#0 lore raw

Hi all,

this little series removes the last swiotlb API exposed to modules.

Diffstat:

 arch/x86/include/asm/xen/swiotlb-xen.h |    6 ------
 arch/x86/kernel/pci-dma.c              |   28 ++++------------------------
 drivers/gpu/drm/nouveau/nouveau_ttm.c  |   10 +++-------
 drivers/pci/xen-pcifront.c             |    6 ------
 kernel/dma/swiotlb.c                   |    1 -
 5 files changed, 7 insertions(+), 44 deletions(-)
#1 lore raw

PATCH 1/4 x86: move a check out of pci_xen_swiotlb_init

Move the exact checks when to initialize the Xen swiotlb code out of pci_xen_swiotlb_init and into the caller so that is uses readable positive checks, rather than negative ones that will get even more confusing with another addition.

---
 arch/x86/kernel/pci-dma.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/pci-dma.c +2 -3
index de6be0a3965ee4..f887b08ac5ffe4 100644
@@ -74,8 +74,6 @@ static inline void __init pci_swiotlb_detect(void)
 #ifdef CONFIG_SWIOTLB_XEN
 static void __init pci_xen_swiotlb_init(void)
 {
-	if (!xen_initial_domain() && !x86_swiotlb_enable)
-		return;
 	x86_swiotlb_enable = true;
 	x86_swiotlb_flags |= SWIOTLB_ANY;
 	swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup);
@@ -113,7 +111,8 @@ static inline void __init pci_xen_swiotlb_init(void)
 void __init pci_iommu_alloc(void)
 {
 	if (xen_pv_domain()) {
-		pci_xen_swiotlb_init();
+		if (xen_initial_domain() || x86_swiotlb_enable)
+			pci_xen_swiotlb_init();
 		return;
 	}
 	pci_swiotlb_detect();
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/4 x86: always initialize xen-swiotlb when xen-pcifront is enabling

In reply to Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

---
 arch/x86/include/asm/xen/swiotlb-xen.h |  6 ------
 arch/x86/kernel/pci-dma.c              | 25 +++----------------------
 drivers/pci/xen-pcifront.c             |  6 ------
 3 files changed, 3 insertions(+), 34 deletions(-)
arch/x86/include/asm/xen/swiotlb-xen.h +0 -6
index 77a2d19cc9909e..abde0f44df57dc 100644
@@ -2,12 +2,6 @@
 #ifndef _ASM_X86_SWIOTLB_XEN_H
 #define _ASM_X86_SWIOTLB_XEN_H
 
-#ifdef CONFIG_SWIOTLB_XEN
-extern int pci_xen_swiotlb_init_late(void);
-#else
-static inline int pci_xen_swiotlb_init_late(void) { return -ENXIO; }
-#endif
-
 int xen_swiotlb_fixup(void *buf, unsigned long nslabs);
 int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
 				unsigned int address_bits,
arch/x86/kernel/pci-dma.c +3 -22
index f887b08ac5ffe4..c4a7ead9eb674e 100644
@@ -81,27 +81,6 @@ static void __init pci_xen_swiotlb_init(void)
 	if (IS_ENABLED(CONFIG_PCI))
 		pci_request_acs();
 }
-
-int pci_xen_swiotlb_init_late(void)
-{
-	if (dma_ops == &xen_swiotlb_dma_ops)
-		return 0;
-
-	/* we can work with the default swiotlb */
-	if (!io_tlb_default_mem.nslabs) {
-		int rc = swiotlb_init_late(swiotlb_size_or_default(),
-					   GFP_KERNEL, xen_swiotlb_fixup);
-		if (rc < 0)
-			return rc;
-	}
-
-	/* XXX: this switches the dma ops under live devices! */
-	dma_ops = &xen_swiotlb_dma_ops;
-	if (IS_ENABLED(CONFIG_PCI))
-		pci_request_acs();
-	return 0;
-}
-EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
 #else
 static inline void __init pci_xen_swiotlb_init(void)
 {
@@ -111,7 +90,9 @@ static inline void __init pci_xen_swiotlb_init(void)
 void __init pci_iommu_alloc(void)
 {
 	if (xen_pv_domain()) {
-		if (xen_initial_domain() || x86_swiotlb_enable)
+		if (xen_initial_domain() ||
+		    IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND) ||
+		    x86_swiotlb_enable)
 			pci_xen_swiotlb_init();
 		return;
 	}
drivers/pci/xen-pcifront.c +0 -6
index 83c0ab50676dff..11636634ae512f 100644
@@ -22,7 +22,6 @@
 #include <linux/bitops.h>
 #include <linux/time.h>
 #include <linux/ktime.h>
-#include <linux/swiotlb.h>
 #include <xen/platform_pci.h>
 
 #include <asm/xen/swiotlb-xen.h>
@@ -669,11 +668,6 @@ static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
 
 	spin_unlock(&pcifront_dev_lock);
 
-	if (!err && !is_swiotlb_active(&pdev->xdev->dev)) {
-		err = pci_xen_swiotlb_init_late();
-		if (err)
-			dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
-	}
 	return err;
 }
 
Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 5 lines · Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

Doesn't it mean all the PV guests will basically waste 64MB of RAM by default each if they don't really have PCI devices?

Quoted 94 lines
---
 arch/x86/include/asm/xen/swiotlb-xen.h |  6 ------
 arch/x86/kernel/pci-dma.c              | 25 +++----------------------
 drivers/pci/xen-pcifront.c             |  6 ------
 3 files changed, 3 insertions(+), 34 deletions(-)
arch/x86/include/asm/xen/swiotlb-xen.h +0 -6
index 77a2d19cc9909e..abde0f44df57dc 100644
@@ -2,12 +2,6 @@
 #ifndef _ASM_X86_SWIOTLB_XEN_H
 #define _ASM_X86_SWIOTLB_XEN_H
 
-#ifdef CONFIG_SWIOTLB_XEN
-extern int pci_xen_swiotlb_init_late(void);
-#else
-static inline int pci_xen_swiotlb_init_late(void) { return -ENXIO; }
-#endif
-
 int xen_swiotlb_fixup(void *buf, unsigned long nslabs);
 int xen_create_contiguous_region(phys_addr_t pstart, unsigned int order,
 				unsigned int address_bits,
arch/x86/kernel/pci-dma.c +3 -22
index f887b08ac5ffe4..c4a7ead9eb674e 100644
@@ -81,27 +81,6 @@ static void __init pci_xen_swiotlb_init(void)
 	if (IS_ENABLED(CONFIG_PCI))
 		pci_request_acs();
 }
-
-int pci_xen_swiotlb_init_late(void)
-{
-	if (dma_ops == &xen_swiotlb_dma_ops)
-		return 0;
-
-	/* we can work with the default swiotlb */
-	if (!io_tlb_default_mem.nslabs) {
-		int rc = swiotlb_init_late(swiotlb_size_or_default(),
-					   GFP_KERNEL, xen_swiotlb_fixup);
-		if (rc < 0)
-			return rc;
-	}
-
-	/* XXX: this switches the dma ops under live devices! */
-	dma_ops = &xen_swiotlb_dma_ops;
-	if (IS_ENABLED(CONFIG_PCI))
-		pci_request_acs();
-	return 0;
-}
-EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
 #else
 static inline void __init pci_xen_swiotlb_init(void)
 {
@@ -111,7 +90,9 @@ static inline void __init pci_xen_swiotlb_init(void)
 void __init pci_iommu_alloc(void)
 {
 	if (xen_pv_domain()) {
-		if (xen_initial_domain() || x86_swiotlb_enable)
+		if (xen_initial_domain() ||
+		    IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND) ||
+		    x86_swiotlb_enable)
 			pci_xen_swiotlb_init();
 		return;
 	}
drivers/pci/xen-pcifront.c +0 -6
index 83c0ab50676dff..11636634ae512f 100644
@@ -22,7 +22,6 @@
 #include <linux/bitops.h>
 #include <linux/time.h>
 #include <linux/ktime.h>
-#include <linux/swiotlb.h>
 #include <xen/platform_pci.h>
 
 #include <asm/xen/swiotlb-xen.h>
@@ -669,11 +668,6 @@ static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
 
 	spin_unlock(&pcifront_dev_lock);
 
-	if (!err && !is_swiotlb_active(&pdev->xdev->dev)) {
-		err = pci_xen_swiotlb_init_late();
-		if (err)
-			dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
-	}
 	return err;
 }
 
Signature · 1 line
2.39.2
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#4 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 9 lines · Marek Marczykowski-Górecki
Quoted 5 lines · Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

Doesn't it mean all the PV guests will basically waste 64MB of RAM by default each if they don't really have PCI devices?

If CONFIG_XEN_PCIDEV_FRONTEND is enabled, and the kernel's isn't booted with swiotlb=noforce, yes.

#5 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Marek Marczykowski-Górecki
Quoted 4 lines · Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Doesn't it mean all the PV guests will basically waste 64MB of RAM by default each if they don't really have PCI devices?

If CONFIG_XEN_PCIDEV_FRONTEND is enabled, and the kernel's isn't booted with swiotlb=noforce, yes.

That's "a bit" unfortunate, since that might be significant part of the VM memory, or if you have a lot of VMs, a significant part of the host memory - it quickly adds up.
While I would say PCI passthrough is not very common for PV guests, can the decision about xen-swiotlb be delayed until you can enumerate xenstore to check if there are any PCI devices connected (and not allocate xen-swiotlb by default if there are none)? This would still not cover the hotplug case (in which case, you'd need to force it with a cmdline), but at least you wouldn't loose much memory just because one of your VMs may use PCI passthrough (so, you have it enabled in your kernel).
Please remember that guest kernel is not always under full control of the host admin, so making guests loose 64MB of RAM always, in default setup isn't good for customers of such VMs...

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#6 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 8 lines · Marek Marczykowski-Górecki

While I would say PCI passthrough is not very common for PV guests, can the decision about xen-swiotlb be delayed until you can enumerate xenstore to check if there are any PCI devices connected (and not allocate xen-swiotlb by default if there are none)? This would still not cover the hotplug case (in which case, you'd need to force it with a cmdline), but at least you wouldn't loose much memory just because one of your VMs may use PCI passthrough (so, you have it enabled in your kernel).

How early can we query xenstore? We'd need to do this before setting up DMA for any device.

The alternative would be to finally merge swiotlb-xen into swiotlb, in which case we might be able to do this later. Let me see what I can do there.

#7 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Marek Marczykowski-Górecki

While I would say PCI passthrough is not very common for PV guests, can the decision about xen-swiotlb be delayed until you can enumerate xenstore to check if there are any PCI devices connected (and not allocate xen-swiotlb by default if there are none)? This would still not cover the hotplug case (in which case, you'd need to force it with a cmdline), but at least you wouldn't loose much memory just because one of your VMs may use PCI passthrough (so, you have it enabled in your kernel).

How early can we query xenstore? We'd need to do this before setting up DMA for any device.

Not that early.  One supported configuration has xenstore not starting for an indefinite period of time after boot.

Quoted 3 lines

The alternative would be to finally merge swiotlb-xen into swiotlb, in which case we might be able to do this later. Let me see what I can do there.

If that is an option, it would be great to reduce the special-cashing.

~Andrew

#8 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 5 lines · Andrew Cooper
Quoted 3 lines

The alternative would be to finally merge swiotlb-xen into swiotlb, in which case we might be able to do this later. Let me see what I can do there.

If that is an option, it would be great to reduce the special-cashing.

I think it's doable, and I've been wanting it for a while. I just need motivated testers, but it seems like I just found at least two :)

#9 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Andrew Cooper
Quoted 3 lines

The alternative would be to finally merge swiotlb-xen into swiotlb, in which case we might be able to do this later. Let me see what I can do there.

If that is an option, it would be great to reduce the special-cashing.

I think it's doable, and I've been wanting it for a while. I just need motivated testers, but it seems like I just found at least two :)

So looking at swiotlb-xen it does these off things where it takes a value generated originally be xen_phys_to_dma, then only does a dma_to_phys to go back and call pfn_valid on the result. Does this make sense, or is it wrong and just works by accident? I.e. is the patch below correct?

drivers/xen/swiotlb-xen.c +5 -7
index 67aa74d201627d..3396c5766f0dd8 100644
@@ -90,9 +90,7 @@ static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
 
 static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr)
 {
-	unsigned long bfn = XEN_PFN_DOWN(dma_to_phys(dev, dma_addr));
-	unsigned long xen_pfn = bfn_to_local_pfn(bfn);
-	phys_addr_t paddr = (phys_addr_t)xen_pfn << XEN_PAGE_SHIFT;
+	phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
 
 	/* If the address is outside our domain, it CAN
 	 * have the same virtual address as another address
@@ -234,7 +232,7 @@ static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page,
 
 done:
 	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dev_addr))))
+		if (pfn_valid(PFN_DOWN(phys)))
 			arch_sync_dma_for_device(phys, size, dir);
 		else
 			xen_dma_sync_for_device(dev, dev_addr, size, dir);
@@ -258,7 +256,7 @@ static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
 	BUG_ON(dir == DMA_NONE);
 
 	if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(hwdev, dev_addr))))
+		if (pfn_valid(PFN_DOWN(paddr)))
 			arch_sync_dma_for_cpu(paddr, size, dir);
 		else
 			xen_dma_sync_for_cpu(hwdev, dev_addr, size, dir);
@@ -276,7 +274,7 @@ xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr,
 	phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
 
 	if (!dev_is_dma_coherent(dev)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
+		if (pfn_valid(PFN_DOWN(paddr)))
 			arch_sync_dma_for_cpu(paddr, size, dir);
 		else
 			xen_dma_sync_for_cpu(dev, dma_addr, size, dir);
@@ -296,7 +294,7 @@ xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr,
 		swiotlb_sync_single_for_device(dev, paddr, size, dir);
 
 	if (!dev_is_dma_coherent(dev)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
+		if (pfn_valid(PFN_DOWN(paddr)))
 			arch_sync_dma_for_device(paddr, size, dir);
 		else
 			xen_dma_sync_for_device(dev, dma_addr, size, dir);
#10 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Hi Christoph,

Quoted 22 lines · On Sat, 20 May 2023 08:21:03 +0200 Christoph Hellwig <[email protected]> wrote:
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Andrew Cooper
Quoted 3 lines

The alternative would be to finally merge swiotlb-xen into swiotlb, in which case we might be able to do this later. Let me see what I can do there.

If that is an option, it would be great to reduce the special-cashing.

I think it's doable, and I've been wanting it for a while. I just need motivated testers, but it seems like I just found at least two :)

So looking at swiotlb-xen it does these off things where it takes a value generated originally be xen_phys_to_dma, then only does a dma_to_phys to go back and call pfn_valid on the result. Does this make sense, or is it wrong and just works by accident? I.e. is the patch below correct?

drivers/xen/swiotlb-xen.c +0 -0
index 67aa74d201627d..3396c5766f0dd8 100644

drivers/xen/swiotlb-xen.c static inline int range_straddles_page_boundary(phys_addr_t p, size_t size) · line 93

Quoted 7 lines
drivers/xen/swiotlb-xen.c +1 -3
@@ -90,9 +90,7 @@ static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
 
 static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr)
 {
-	unsigned long bfn = XEN_PFN_DOWN(dma_to_phys(dev, dma_addr));
-	unsigned long xen_pfn = bfn_to_local_pfn(bfn);
-	phys_addr_t paddr = (phys_addr_t)xen_pfn << XEN_PAGE_SHIFT;
+	phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);

I'm no big Xen expert, but I think this is wrong. Let's go through it line by line:

- bfn = XEN_PFN_DOWN(dma_to_phys(dev, dma_addr));

Take the DMA address (as seen by devices on the bus), convert it to a physical address (as seen by the CPU on the bus) and shift it right by XEN_PAGE_SHIFT. The result is a Xen machine PFN.

- xen_pfn = bfn_to_local_pfn(bfn);

Take the machine PFN and converts it to a physical PFN.

- paddr = (phys_addr_t)xen_pfn << XEN_PAGE_SHIFT;

Convert the physical PFN to a physical address.

The important thing here is that Xen PV does not have auto-translated physical addresses, so physical address != machine address. Physical addresses in Xen PV domains are "artificial", used by the kernel to index the mem_map array, so a PFN can be easily converted to a struct page pointer and back. However, these addresses are never used by hardware, not even by CPU. The addresses used by the CPU are called machine addresses. There is no address translation between VCPUs and CPUs, because a PV domain runs directly on the CPU. After all, that's why it is called _para_virtualized.

HTH
Petr T

#11 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

In reply to Marek Marczykowski-Górecki
Quoted 12 lines · On Fri, 19 May 2023 12:10:26 +0200 Marek Marczykowski-Górecki…
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Marek Marczykowski-Górecki
Quoted 5 lines · Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

Doesn't it mean all the PV guests will basically waste 64MB of RAM by default each if they don't really have PCI devices?

Quoted 6 lines
Quoted 2 lines

If CONFIG_XEN_PCIDEV_FRONTEND is enabled, and the kernel's isn't booted with swiotlb=noforce, yes.

That's "a bit" unfortunate, since that might be significant part of the VM memory, or if you have a lot of VMs, a significant part of the host memory - it quickly adds up.

I wonder if dynamic swiotlb allocation might also help with this...

Petr T

maintainer
#12 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

In reply to Marek Marczykowski-Górecki
Quoted 15 lines · Marek Marczykowski-Górecki
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Marek Marczykowski-Górecki
Quoted 4 lines · Christoph Hellwig

Remove the dangerous late initialization of xen-swiotlb in pci_xen_swiotlb_init_late and instead just always initialize xen-swiotlb in the boot code if CONFIG_XEN_PCIDEV_FRONTEND is enabled.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Doesn't it mean all the PV guests will basically waste 64MB of RAM by default each if they don't really have PCI devices?

If CONFIG_XEN_PCIDEV_FRONTEND is enabled, and the kernel's isn't booted with swiotlb=noforce, yes.

Quoted 15 lines

That's "a bit" unfortunate, since that might be significant part of the VM memory, or if you have a lot of VMs, a significant part of the host memory - it quickly adds up.
While I would say PCI passthrough is not very common for PV guests, can the decision about xen-swiotlb be delayed until you can enumerate xenstore to check if there are any PCI devices connected (and not allocate xen-swiotlb by default if there are none)? This would still not cover the hotplug case (in which case, you'd need to force it with a cmdline), but at least you wouldn't loose much memory just because one of your VMs may use PCI passthrough (so, you have it enabled in your kernel).
Please remember that guest kernel is not always under full control of the host admin, so making guests loose 64MB of RAM always, in default setup isn't good for customers of such VMs...

In normal cases PCI passthrough in PV guests requires to start the guest with e820_host=1. So it should be rather easy to limit allocating the 64MB in PV guests to the cases where the memory map has non-RAM regions especially in the first 1MB of the memory.

This will cover even hotplug cases. The only case not covered would be a guest started with e820_host=1 even if no PCI passthrough was planned. But this should be rather rare (at least I hope so).

Juergen

#13 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 8 lines · Juergen Gross

In normal cases PCI passthrough in PV guests requires to start the guest with e820_host=1. So it should be rather easy to limit allocating the 64MB in PV guests to the cases where the memory map has non-RAM regions especially in the first 1MB of the memory.

This will cover even hotplug cases. The only case not covered would be a guest started with e820_host=1 even if no PCI passthrough was planned. But this should be rather rare (at least I hope so).

So is this an ACK for the patch and can we go ahead with it?

(I'd still like to merge swiotlb-xen into swiotlb eventually, but it's probably not going to happen this merge window)

maintainer
#14 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 11 lines · Christoph Hellwig
Quoted 8 lines · Juergen Gross

In normal cases PCI passthrough in PV guests requires to start the guest with e820_host=1. So it should be rather easy to limit allocating the 64MB in PV guests to the cases where the memory map has non-RAM regions especially in the first 1MB of the memory.

This will cover even hotplug cases. The only case not covered would be a guest started with e820_host=1 even if no PCI passthrough was planned. But this should be rather rare (at least I hope so).

So is this an ACK for the patch and can we go ahead with it?

As long as above mentioned check of the E820 map is done, yes.

If you want I can send a diff to be folded into your patch on Monday.

Quoted 3 lines

(I'd still like to merge swiotlb-xen into swiotlb eventually, but it's probably not going to happen this merge window)

Would be nice.

Juergen

#15 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Quoted 8 lines · Juergen Gross
Quoted 4 lines
Quoted 2 lines

guest started with e820_host=1 even if no PCI passthrough was planned. But this should be rather rare (at least I hope so).

So is this an ACK for the patch and can we go ahead with it?

As long as above mentioned check of the E820 map is done, yes.

If you want I can send a diff to be folded into your patch on Monday.

Yes, that would be great!

maintainer
#16 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

In reply to Juergen Gross
Quoted 11 lines · Juergen Gross
Quoted 10 lines · Christoph Hellwig
Quoted 8 lines · Juergen Gross

In normal cases PCI passthrough in PV guests requires to start the guest with e820_host=1. So it should be rather easy to limit allocating the 64MB in PV guests to the cases where the memory map has non-RAM regions especially in the first 1MB of the memory.

This will cover even hotplug cases. The only case not covered would be a guest started with e820_host=1 even if no PCI passthrough was planned. But this should be rather rare (at least I hope so).

Quoted 5 lines
Quoted 1 line

So is this an ACK for the patch and can we go ahead with it?

As long as above mentioned check of the E820 map is done, yes.

If you want I can send a diff to be folded into your patch on Monday.

Attached is a patch for adding a new flag xen_pv_pci_possible. You can either add the patch to your series or merge it into your patch 2.

You need to modify your patch like this:

@@ -111,7 +90,10 @@ static inline void __init pci_xen_swiotlb_init(void)
void __init pci_iommu_alloc(void)
{
if (xen_pv_domain()) {

-		if (xen_initial_domain() || x86_swiotlb_enable)
+		if (xen_initial_domain() ||

+ (IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND) &&
+ xen_pv_pci_possible) ||
+ x86_swiotlb_enable)
pci_xen_swiotlb_init();
return;
}

Juergen

From 7e84a88243b57bc90d1ef6bd42661f499886e659 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Mon, 12 Jun 2023 09:57:18 +0200
Subject: [PATCH] xen/pci: add flag for PCI passthrough being possible

When running as a Xen PV guests passed through PCI devices only have a chance to work if the Xen supplied memory map has some PCI space reserved.

Add a flag xen_pv_pci_possible which will be set in early boot in case the memory map has at least one area with the type E820_TYPE_RESERVED.

---
 arch/x86/xen/setup.c | 6 ++++++
 include/xen/xen.h    | 6 ++++++
 2 files changed, 12 insertions(+)
arch/x86/xen/setup.c +6 -0
index c2be3efb2ba0..716f76c41416 100644
@@ -43,6 +43,9 @@ struct xen_memory_region xen_extra_mem[XEN_EXTRA_MEM_MAX_REGIONS] __initdata;
 /* Number of pages released from the initial allocation. */
 unsigned long xen_released_pages;
 
+/* Memory map would allow PCI passthrough. */
+bool xen_pv_pci_possible;
+
 /* E820 map used during setting up memory. */
 static struct e820_table xen_e820_table __initdata;
 
@@ -804,6 +807,9 @@ char * __init xen_memory_setup(void)
 		chunk_size = size;
 		type = xen_e820_table.entries[i].type;
 
+		if (type == E820_TYPE_RESERVED)
+			xen_pv_pci_possible = true;
+
 		if (type == E820_TYPE_RAM) {
 			if (addr < mem_end) {
 				chunk_size = min(size, mem_end - addr);
include/xen/xen.h +6 -0
index 0efeb652f9b8..5eb0a974a11e 100644
@@ -29,6 +29,12 @@ extern bool xen_pvh;
 
 extern uint32_t xen_start_flags;
 
+#ifdef CONFIG_XEN_PV
+extern bool xen_pv_pci_possible;
+#else
+#define xen_pv_pci_possible	0
+#endif
+
 #include <xen/interface/hvm/start_info.h>
 extern struct hvm_start_info pvh_start_info;
 
Signature · 1 line
2.35.3
#17 lore raw

Re: [PATCH 2/4] x86: always initialize xen-swiotlb when xen-pcifront is enabling

Thank you. I'll queue it up as a separate patch.

#18 lore raw

PATCH 3/4 drm/nouveau: stop using is_swiotlb_active

In reply to Christoph Hellwig

Drivers have no business looking into dma-mapping internals and check what backend is used. Unfortunstely the DRM core is still broken and tries to do plain page allocations instead of using DMA API allocators by default and uses various bandaids on when to use dma_alloc_coherent.

Switch nouveau to use the same (broken) scheme as amdgpu and radeon to remove the last driver user of is_swiotlb_active.

---
 drivers/gpu/drm/nouveau/nouveau_ttm.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/gpu/drm/nouveau/nouveau_ttm.c +3 -7
index 1469a88910e45d..486f39f31a38df 100644
@@ -24,9 +24,9 @@
  */
 
 #include <linux/limits.h>
-#include <linux/swiotlb.h>
 
 #include <drm/ttm/ttm_range_manager.h>
+#include <drm/drm_cache.h>
 
 #include "nouveau_drv.h"
 #include "nouveau_gem.h"
@@ -265,7 +265,6 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 	struct nvkm_pci *pci = device->pci;
 	struct nvif_mmu *mmu = &drm->client.mmu;
 	struct drm_device *dev = drm->dev;
-	bool need_swiotlb = false;
 	int typei, ret;
 
 	ret = nouveau_ttm_init_host(drm, 0);
@@ -300,13 +299,10 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 		drm->agp.cma = pci->agp.cma;
 	}
 
-#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
-	need_swiotlb = is_swiotlb_active(dev->dev);
-#endif
-
 	ret = ttm_device_init(&drm->ttm.bdev, &nouveau_bo_driver, drm->dev->dev,
 				  dev->anon_inode->i_mapping,
-				  dev->vma_offset_manager, need_swiotlb,
+				  dev->vma_offset_manager,
+				  drm_need_swiotlb(drm->client.mmu.dmabits),
 				  drm->client.mmu.dmabits <= 32);
 	if (ret) {
 		NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
Signature · 1 line
2.39.2
maintainer Reviewed-by on v1 3/4
#19 lore raw

Re: [PATCH 3/4] drm/nouveau: stop using is_swiotlb_active

Thanks for getting to this!

Quoted 52 lines · Christoph Hellwig

Drivers have no business looking into dma-mapping internals and check what backend is used. Unfortunstely the DRM core is still broken and tries to do plain page allocations instead of using DMA API allocators by default and uses various bandaids on when to use dma_alloc_coherent.

Switch nouveau to use the same (broken) scheme as amdgpu and radeon to remove the last driver user of is_swiotlb_active.

---
 drivers/gpu/drm/nouveau/nouveau_ttm.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/gpu/drm/nouveau/nouveau_ttm.c +3 -7
index 1469a88910e45d..486f39f31a38df 100644
@@ -24,9 +24,9 @@
  */
 
 #include <linux/limits.h>
-#include <linux/swiotlb.h>
 
 #include <drm/ttm/ttm_range_manager.h>
+#include <drm/drm_cache.h>
 
 #include "nouveau_drv.h"
 #include "nouveau_gem.h"
@@ -265,7 +265,6 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 	struct nvkm_pci *pci = device->pci;
 	struct nvif_mmu *mmu = &drm->client.mmu;
 	struct drm_device *dev = drm->dev;
-	bool need_swiotlb = false;
 	int typei, ret;
 
 	ret = nouveau_ttm_init_host(drm, 0);
@@ -300,13 +299,10 @@ nouveau_ttm_init(struct nouveau_drm *drm)
 		drm->agp.cma = pci->agp.cma;
 	}
 
-#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
-	need_swiotlb = is_swiotlb_active(dev->dev);
-#endif
-
 	ret = ttm_device_init(&drm->ttm.bdev, &nouveau_bo_driver, drm->dev->dev,
 				  dev->anon_inode->i_mapping,
-				  dev->vma_offset_manager, need_swiotlb,
+				  dev->vma_offset_manager,
+				  drm_need_swiotlb(drm->client.mmu.dmabits),
 				  drm->client.mmu.dmabits <= 32);
 	if (ret) {
 		NV_ERROR(drm, "error initialising bo driver, %d\n", ret);
Signature · 3 lines
Cheers,
 Lyude Paul (she/her)
 Software Engineer at Red Hat
#20 lore raw

Re: [PATCH 3/4] drm/nouveau: stop using is_swiotlb_active

Quoted 3 lines · Lyude Paul

Thanks for getting to this!

I've tentantively queued this up in the dma-mapping for-next tree. Let me know if you'd prefer it to go through the nouveau tree.

#21 lore raw

PATCH 4/4 swiotlb: unexport is_swiotlb_active

In reply to Christoph Hellwig

Drivers have no business looking at dma-mapping or swiotlb internals.

---
 kernel/dma/swiotlb.c | 1 -
 1 file changed, 1 deletion(-)
kernel/dma/swiotlb.c +0 -1
index af2e304c672c43..9f1fd28264a067 100644
@@ -921,7 +921,6 @@ bool is_swiotlb_active(struct device *dev)
 
 	return mem && mem->nslabs;
 }
-EXPORT_SYMBOL_GPL(is_swiotlb_active);
 
 #ifdef CONFIG_DEBUG_FS
 
Signature · 1 line
2.39.2