Skip to contentSkip to conversation

Restricted DMA

RFC PATCH Superseded Claire Chang 11 participants 62 messages Jan 6, 2021 – Feb 9, 2021

Subsystems (9)

v3 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v3 cover Restricted DMA

This series implements mitigations for lack of DMA access control on systems without an IOMMU, which could result in the DMA accessing the system memory at unexpected times and/or unexpected addresses, possibly leading to data leakage or corruption.

For example, we plan to use the PCI-e bus for Wi-Fi and that PCI-e bus is not behind an IOMMU. As PCI-e, by design, gives the device full access to system memory, a vulnerability in the Wi-Fi firmware could easily escalate to a full system exploit (remote wifi exploits: [1a], [1b] that shows a full chain of exploits; [2], [3]).

To mitigate the security concerns, we introduce restricted DMA. Restricted DMA utilizes the existing swiotlb to bounce streaming DMA in and out of a specially allocated region and does memory allocation from the same region. The feature on its own provides a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to restrict the DMA to a predefined memory region (this is usually done at firmware level, e.g. in ATF on some ARM platforms).

[1a] https://googleprojectzero.blogspot.com/2017/04/over-air-exploiting-broadcoms-wi-fi_4.html
[1b] https://googleprojectzero.blogspot.com/2017/04/over-air-exploiting-broadcoms-wi-fi_11.html
[2] https://blade.tencent.com/en/advisories/qualpwn/
[3] https://www.bleepingcomputer.com/news/security/vulnerabilities-found-in-highly-popular-firmware-for-wifi-chips/

Claire Chang (6):
swiotlb: Add io_tlb_mem struct
swiotlb: Add restricted DMA pool
swiotlb: Use restricted DMA pool if available
swiotlb: Add restricted DMA alloc/free support.
dt-bindings: of: Add restricted DMA pool
of: Add plumbing for restricted DMA pool

 .../reserved-memory/reserved-memory.txt       |  24 +
 arch/powerpc/platforms/pseries/svm.c          |   4 +-
 drivers/iommu/dma-iommu.c                     |  12 +-
 drivers/of/address.c                          |  21 +
 drivers/of/device.c                           |   4 +
 drivers/of/of_private.h                       |   5 +
 drivers/xen/swiotlb-xen.c                     |   4 +-
 include/linux/device.h                        |   4 +
 include/linux/swiotlb.h                       |  61 +-
 kernel/dma/Kconfig                            |   1 +
 kernel/dma/direct.c                           |  20 +-
 kernel/dma/direct.h                           |  10 +-
 kernel/dma/swiotlb.c                          | 576 +++++++++++-------
 13 files changed, 514 insertions(+), 232 deletions(-)
Signature · 17 lines
2.29.2.729.g45daf8777d-goog

v3: 
  Using only one reserved memory region for both streaming DMA and memory
  allocation.

v2:
  Building on top of swiotlb.
  https://lore.kernel.org/patchwork/cover/1280705/

v1:
  Using dma_map_ops.
  https://lore.kernel.org/patchwork/cover/1271660/
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw

RFC PATCH v3 1/6 swiotlb: Add io_tlb_mem struct

Added a new struct, io_tlb_mem, as the IO TLB memory pool descriptor and moved relevant global variables into that struct.
This will be useful later to allow for restricted DMA pool.

---
 arch/powerpc/platforms/pseries/svm.c |   4 +-
 drivers/xen/swiotlb-xen.c            |   4 +-
 include/linux/swiotlb.h              |  39 +++-
 kernel/dma/swiotlb.c                 | 292 +++++++++++++--------------
 4 files changed, 178 insertions(+), 161 deletions(-)
arch/powerpc/platforms/pseries/svm.c +2 -2
index 7b739cc7a8a9..2b767f1ca5fd 100644
@@ -55,8 +55,8 @@ void __init svm_swiotlb_init(void)
 	if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, false))
 		return;
 
-	if (io_tlb_start)
-		memblock_free_early(io_tlb_start,
+	if (io_tlb_default_mem.start)
+		memblock_free_early(io_tlb_default_mem.start,
 				    PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
 	panic("SVM: Cannot allocate SWIOTLB buffer");
 }
drivers/xen/swiotlb-xen.c +2 -2
index 2b385c1b4a99..4d17dff7ffd2 100644
@@ -192,8 +192,8 @@ int __ref xen_swiotlb_init(int verbose, bool early)
 	/*
 	 * IO TLB memory already allocated. Just use it.
 	 */
-	if (io_tlb_start != 0) {
-		xen_io_tlb_start = phys_to_virt(io_tlb_start);
+	if (io_tlb_default_mem.start != 0) {
+		xen_io_tlb_start = phys_to_virt(io_tlb_default_mem.start);
 		goto end;
 	}
 
include/linux/swiotlb.h +37 -2
index d9c9fc9ca5d2..dd8eb57cbb8f 100644
@@ -70,11 +70,46 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
 
 #ifdef CONFIG_SWIOTLB
 extern enum swiotlb_force swiotlb_force;
-extern phys_addr_t io_tlb_start, io_tlb_end;
+
+/**
+ * struct io_tlb_mem - IO TLB Memory Pool Descriptor
+ *
+ * @start:	The start address of the swiotlb memory pool. Used to do a quick
+ *		range check to see if the memory was in fact allocated by this
+ *		API.
+ * @end:	The end address of the swiotlb memory pool. Used to do a quick
+ *		range check to see if the memory was in fact allocated by this
+ *		API.
+ * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
+ *		@end. This is command line adjustable via setup_io_tlb_npages.
+ * @used:	The number of used IO TLB block.
+ * @list:	The free list describing the number of free entries available
+ *		from each index.
+ * @index:	The index to start searching in the next round.
+ * @orig_addr:	The original address corresponding to a mapped entry for the
+ *		sync operations.
+ * @lock:	The lock to protect the above data structures in the map and
+ *		unmap calls.
+ * @debugfs:	The dentry to debugfs.
+ */
+struct io_tlb_mem {
+	phys_addr_t start;
+	phys_addr_t end;
+	unsigned long nslabs;
+	unsigned long used;
+	unsigned int *list;
+	unsigned int index;
+	phys_addr_t *orig_addr;
+	spinlock_t lock;
+	struct dentry *debugfs;
+};
+extern struct io_tlb_mem io_tlb_default_mem;
 
 static inline bool is_swiotlb_buffer(phys_addr_t paddr)
 {
-	return paddr >= io_tlb_start && paddr < io_tlb_end;
+	struct io_tlb_mem *mem = &io_tlb_default_mem;
+
+	return paddr >= mem->start && paddr < mem->end;
 }
 
 void __init swiotlb_exit(void);
kernel/dma/swiotlb.c +137 -155

593 lines not on this page · raw message

Signature · 8 lines
2.29.2.729.g45daf8777d-goog

To make this change as mechanical as possible, I didn't fix any
checkpatch.pl ERROR/WARNING.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#2 lore raw

Re: [RFC PATCH v3 1/6] swiotlb: Add io_tlb_mem struct

Quoted 3 lines · Claire Chang

Added a new struct, io_tlb_mem, as the IO TLB memory pool descriptor and moved relevant global variables into that struct.
This will be useful later to allow for restricted DMA pool.

I like where this is going, but a few comments.

Mostly I'd love to be able to entirely hide io_tlb_default_mem and struct io_tlb_mem inside of swiotlb.c.

arch/powerpc/platforms/pseries/svm.c void __init svm_swiotlb_init(void) · line 60

Quoted 11 lines
arch/powerpc/platforms/pseries/svm.c +2 -2
@@ -55,8 +55,8 @@ void __init svm_swiotlb_init(void)
 	if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, false))
 		return;
 
-	if (io_tlb_start)
-		memblock_free_early(io_tlb_start,
+	if (io_tlb_default_mem.start)
+		memblock_free_early(io_tlb_default_mem.start,
 				    PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));

I think this should switch to use the local vstart variable in prep patch.

Quoted 7 lines
arch/powerpc/platforms/pseries/svm.c +0 -0
 	panic("SVM: Cannot allocate SWIOTLB buffer");
 }
drivers/xen/swiotlb-xen.c +0 -0
index 2b385c1b4a99..4d17dff7ffd2 100644

drivers/xen/swiotlb-xen.c int __ref xen_swiotlb_init(int verbose, bool early) · line 197

Quoted 8 lines
drivers/xen/swiotlb-xen.c +2 -2
@@ -192,8 +192,8 @@ int __ref xen_swiotlb_init(int verbose, bool early)
 	/*
 	 * IO TLB memory already allocated. Just use it.
 	 */
-	if (io_tlb_start != 0) {
-		xen_io_tlb_start = phys_to_virt(io_tlb_start);
+	if (io_tlb_default_mem.start != 0) {
+		xen_io_tlb_start = phys_to_virt(io_tlb_default_mem.start);
 		goto end;

xen_io_tlb_start is interesting. It is used only in two functions:

1) is_xen_swiotlb_buffer, where I think we should be able to just use is_swiotlb_buffer instead of open coding it with the extra phys_to_virt/virt_to_phys cycle.
2) xen_swiotlb_init, where except for the assignment it only is used locally for the case not touched above and could this be replaced with a local variable.

Konrad, does this make sense to you?

drivers/xen/swiotlb-xen.c int __ref xen_swiotlb_init(int verbose, bool early)

Quoted 6 lines
drivers/xen/swiotlb-xen.c +3 -1
 static inline bool is_swiotlb_buffer(phys_addr_t paddr)
 {
-	return paddr >= io_tlb_start && paddr < io_tlb_end;
+	struct io_tlb_mem *mem = &io_tlb_default_mem;
+
+	return paddr >= mem->start && paddr < mem->end;

We'd then have to move this out of line as well.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw

RFC PATCH v3 2/6 swiotlb: Add restricted DMA pool

In reply to Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
 include/linux/device.h  |   4 ++
 include/linux/swiotlb.h |   7 +-
 kernel/dma/Kconfig      |   1 +
 kernel/dma/swiotlb.c    | 144 ++++++++++++++++++++++++++++++++++------
 4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +4 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
  * @dma_pools:	Dma pools (if dma'ble device).
  * @dma_mem:	Internal for coherent mem override.
  * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
  * @archdata:	For arch-specific additions.
  * @of_node:	Associated device tree node.
  * @fwnode:	Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
 #ifdef CONFIG_DMA_CMA
 	struct cma *cma_area;		/* contiguous memory area for dma
 					   allocations */
+#endif
+#ifdef CONFIG_SWIOTLB
+	struct io_tlb_mem	*dma_io_tlb_mem;
 #endif
 	/* arch specific additions */
 	struct dev_archdata	archdata;
include/linux/swiotlb.h +4 -3
index dd8eb57cbb8f..a1bbd7788885 100644
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
  *
  * @start:	The start address of the swiotlb memory pool. Used to do a quick
  *		range check to see if the memory was in fact allocated by this
- *		API.
+ *		API. For restricted DMA pool, this is device tree adjustable.
  * @end:	The end address of the swiotlb memory pool. Used to do a quick
  *		range check to see if the memory was in fact allocated by this
- *		API.
+ *		API. For restricted DMA pool, this is device tree adjustable.
  * @nslabs:	The number of IO TLB blocks (in groups of 64) between @start and
- *		@end. This is command line adjustable via setup_io_tlb_npages.
+ *		@end. For default swiotlb, this is command line adjustable via
+ *		setup_io_tlb_npages.
  * @used:	The number of used IO TLB block.
  * @list:	The free list describing the number of free entries available
  *		from each index.
kernel/dma/Kconfig +1 -0
index 479fc145acfc..131a0a66781b 100644
@@ -82,6 +82,7 @@ config ARCH_HAS_FORCE_DMA_UNENCRYPTED
 config SWIOTLB
 	bool
 	select NEED_DMA_MAP_STATE
+	select OF_EARLY_FLATTREE
 
 #
 # Should be selected if we can mmap non-coherent mappings to userspace.
kernel/dma/swiotlb.c +122 -22

183 lines not on this page · raw message

Signature · 6 lines
2.29.2.729.g45daf8777d-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#4 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 16 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
 include/linux/device.h  |   4 ++
 include/linux/swiotlb.h |   7 +-
 kernel/dma/Kconfig      |   1 +
 kernel/dma/swiotlb.c    | 144 ++++++++++++++++++++++++++++++++++------
 4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +0 -0
index 89bb8b84173e..ca6f71ec8871 100644

include/linux/device.h struct dev_links_info { · line 416

Quoted 4 lines
include/linux/device.h +1 -0
@@ -413,6 +413,7 @@ struct dev_links_info {
  * @dma_pools:	Dma pools (if dma'ble device).
  * @dma_mem:	Internal for coherent mem override.
  * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.

Why does this have to be added here? Shouldn't the platform-specific code handle it instead?

thanks,

greg k-h
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#5 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 10 lines · Greg KH

include/linux/device.h struct dev_links_info { · line 416

Quoted 7 lines
include/linux/device.h +1 -0
@@ -413,6 +413,7 @@ struct dev_links_info {
  * @dma_pools:	Dma pools (if dma'ble device).
  * @dma_mem:	Internal for coherent mem override.
  * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.

Why does this have to be added here? Shouldn't the platform-specific code handle it instead?

The whole code added here is pretty generic. What we need to eventually do, though is to add a separate dma_device instead of adding more and more bloat to struct device.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#6 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Greg KH

include/linux/device.h struct dev_links_info { · line 416

Quoted 7 lines
include/linux/device.h +1 -0
@@ -413,6 +413,7 @@ struct dev_links_info {
  * @dma_pools:	Dma pools (if dma'ble device).
  * @dma_mem:	Internal for coherent mem override.
  * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.

Why does this have to be added here? Shouldn't the platform-specific code handle it instead?

Quoted 3 lines

The whole code added here is pretty generic. What we need to eventually do, though is to add a separate dma_device instead of adding more and more bloat to struct device.

I have no objections for that happening!
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#7 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 8 lines · Greg KH
Quoted 6 lines
Quoted 2 lines

Why does this have to be added here? Shouldn't the platform-specific code handle it instead?

The whole code added here is pretty generic. What we need to eventually do, though is to add a separate dma_device instead of adding more and more bloat to struct device.

I have no objections for that happening!

I'm pretty sure you agreed to it before in fact. Now someone just needs to find the time to do this heavy lifting, where "someone" probably means me.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#8 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Claire Chang

Hello!

In this file:

Quoted 4 lines
kernel/dma/swiotlb.c +0 -0
index e4368159f88a..7fb2ac087d23 100644

..

Quoted 12 lines
kernel/dma/swiotlb.c +12 -0
+static const struct reserved_mem_ops rmem_swiotlb_ops = {
+	.device_init	= rmem_swiotlb_device_init,
+	.device_release	= rmem_swiotlb_device_release,
+};
+
+static int __init rmem_swiotlb_setup(struct reserved_mem *rmem)
+{
+	unsigned long node = rmem->fdt_node;
+
+	if (of_get_flat_dt_prop(node, "reusable", NULL) ||
+	    of_get_flat_dt_prop(node, "linux,cma-default", NULL) ||
+	    of_get_flat_dt_prop(node, "linux,dma-default", NULL) ||

kernel/dma/swiotlb.c bool is_swiotlb_active(void) · line 818

Quoted 10 lines
kernel/dma/swiotlb.c +10 -0
+	    of_get_flat_dt_prop(node, "no-map", NULL))
+		return -EINVAL;
+
+	rmem->ops = &rmem_swiotlb_ops;
+	pr_info("Reserved memory: created device swiotlb memory pool at %pa, size %ld MiB\n",
+		&rmem->base, (unsigned long)rmem->size / SZ_1M);
+	return 0;
+}
+
+RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup);

The code should be as much as possible arch-agnostic. That is why there are multiple -swiotlb files scattered in arch directories that own the architecture specific code.

Would it be possible to move the code there and perhaps have a ARM specific front-end for this DMA restricted pool there? See for example the xen-swiotlb code.

Cheers!

Konrad
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#9 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

Thanks!

#10 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

Quoted 2 lines

Thanks!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#11 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

In premise the same code could be used with an ACPI enabled system with an appropriate service to identify the restricted DMA regions and unlock them.

More than 1 architecture requiring this function (ARM and ARM64 are the two I can think of needing this immediately) sort of calls for making the code architecture agnostic since past 2, you need something that scales.

There is already code today under kernel/dma/contiguous.c that is only activated on a CONFIG_OF=y && CONFIG_OF_RESERVED_MEM=y system, this is no different.

Signature · 1 line
Florian
#12 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 8 lines · Florian Fainelli
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

Quoted 7 lines
Quoted 3 lines

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

In premise the same code could be used with an ACPI enabled system with an appropriate service to identify the restricted DMA regions and unlock them.

Which this patchset is not.

Quoted 4 lines

More than 1 architecture requiring this function (ARM and ARM64 are the two I can think of needing this immediately) sort of calls for making the code architecture agnostic since past 2, you need something that scales.

I believe the use-case is for ARM64 at this moment.

Quoted 6 lines

There is already code today under kernel/dma/contiguous.c that is only activated on a CONFIG_OF=y && CONFIG_OF_RESERVED_MEM=y system, this is no different.

Signature · 1 line
Florian

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#13 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 13 lines · Konrad Rzeszutek Wilk
Quoted 12 lines · Florian Fainelli
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

Quoted 5 lines
Quoted 3 lines

In premise the same code could be used with an ACPI enabled system with an appropriate service to identify the restricted DMA regions and unlock them.

Which this patchset is not.

ACPI is not included, but the comment about Device Tree being specific to PowerPC, SPARC and ARM is x86 is not quite correct. There is an architecture specific part to obtaining where the Device Tree lives in memory, but the implementation itself is architecture agnostic (with some early SPARC/OpenFirmware shenanigans), and x86 does, or rather did support Device Tree to a very small extent with the CE4100 platform.

Would you prefer that an swiotlb_of.c file be created instead or something along those lines to better encapsulate where the OF specific code lives?

Quoted 7 lines
Quoted 5 lines

More than 1 architecture requiring this function (ARM and ARM64 are the two I can think of needing this immediately) sort of calls for making the code architecture agnostic since past 2, you need something that scales.

I believe the use-case is for ARM64 at this moment.

For the platforms that Claire uses, certainly for the ones we use, ARM and ARM64 are in scope.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#14 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Florian Fainelli
Quoted 16 lines · Florian Fainelli
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

In premise the same code could be used with an ACPI enabled system with an appropriate service to identify the restricted DMA regions and unlock them.

Quoted 7 lines

More than 1 architecture requiring this function (ARM and ARM64 are the two I can think of needing this immediately) sort of calls for making the code architecture agnostic since past 2, you need something that scales.

There is already code today under kernel/dma/contiguous.c that is only activated on a CONFIG_OF=y && CONFIG_OF_RESERVED_MEM=y system, this is no different.

<unrelated to these patches, which are useful for the case cited>

Just a note for history/archives that this approach would not be appropriate on general purpose Arm systems, such as SystemReady-ES edge/non-server platforms seeking to run general purpose distros. I want to have that in the record before someone at Arm (or NVidia, or a bunch of others that come to mind who have memory firewalls) gets an idea.

If you're working at an Arm vendor and come looking at this later thinking "wow, what a great idea!", please fix your hardware to have a real IOMMU/SMMU and real PCIe. You'll be pointed at this reply.

Jon.

Signature · 1 line
Computer Architect
maintainer
#15 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Konrad Rzeszutek Wilk
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Claire Chang

Hi Greg and Konrad,

This change is intended to be non-arch specific. Any arch that lacks DMA access control and has devices not behind an IOMMU can make use of it. Could you share why you think this should be arch specific?

The idea behind non-arch specific code is it to be generic. The devicetree is specific to PowerPC, Sparc, and ARM, and not to x86 - hence it should be in arch specific code.

Sorry, but that's an absurd argument. By the same token you'd equally have to claim that bits of, say, the Broadcom WiFi driver (not to mention dozens of others) should be split out into arch code, since not all platforms use the devicetree parts, nor the ACPI parts, nor the PCI parts...

There is nothing architecture-specific about using devicetree as a system description - AFAIK there *are* a handful of x86 platforms that use it, besides even more architectures than you've listed above. It has long been the policy that devicetree-related code for a particular subsystem should just live within that subsystem. Sometimes if there's enough of it it gets collected together into its own file - e.g. drivers/pci/of.c - otherwise it tends to just get #ifdef'ed - e.g. of_spi_parse_dt(), or the other DMA reserved-memory consumers that already exist as Florian points out.

Besides, there are far more platforms that enable CONFIG_OF than enable CONFIG_SWIOTLB, so by that metric the whole of the SWIOTLB code itself is even less "generic" than any DT parsing :P

Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#16 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Claire Chang
Quoted 38 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
 include/linux/device.h  |   4 ++
 include/linux/swiotlb.h |   7 +-
 kernel/dma/Kconfig      |   1 +
 kernel/dma/swiotlb.c    | 144 ++++++++++++++++++++++++++++++++++------
 4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +4 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
  * @dma_pools:	Dma pools (if dma'ble device).
  * @dma_mem:	Internal for coherent mem override.
  * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
  * @archdata:	For arch-specific additions.
  * @of_node:	Associated device tree node.
  * @fwnode:	Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
 #ifdef CONFIG_DMA_CMA
 	struct cma *cma_area;		/* contiguous memory area for dma
 					   allocations */
+#endif
+#ifdef CONFIG_SWIOTLB
+	struct io_tlb_mem	*dma_io_tlb_mem;
 #endif
 	/* arch specific additions */
 	struct dev_archdata	archdata;
include/linux/swiotlb.h +0 -0
index dd8eb57cbb8f..a1bbd7788885 100644

include/linux/swiotlb.h extern enum swiotlb_force swiotlb_force; · line 79

Quoted 5 lines
include/linux/swiotlb.h +1 -1
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
  *
  * @start:	The start address of the swiotlb memory pool. Used to do a quick
  *		range check to see if the memory was in fact allocated by this
- *		API.
+ *		API. For restricted DMA pool, this is device tree adjustable.

Maybe write it as this is "firmware adjustable" such that when/if ACPI needs something like this, the description does not need updating.

[snip]

Quoted 5 lines
include/linux/swiotlb.h +5 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+				    struct device *dev)
+{
+	struct io_tlb_mem *mem = rmem->priv;
+	int ret;

include/linux/swiotlb.h extern enum swiotlb_force swiotlb_force;

Quoted 10 lines
include/linux/swiotlb.h +10 -0
+
+	if (dev->dma_io_tlb_mem)
+		return -EBUSY;
+
+	if (!mem) {
+		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+		if (!mem)
+			return -ENOMEM;
+
+		if (!memremap(rmem->base, rmem->size, MEMREMAP_WB)) {

MEMREMAP_WB sounds appropriate as a default. Documentation/devicetree/bindings/reserved-memory/ramoops.txt does define an "unbuffered" property which in premise could be applied to the generic reserved memory binding as well and that we may have to be honoring here, if we were to make it more generic. Oh well, this does not need to be addressed right now I guess.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#17 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Hi All,

Quoted 59 lines · Florian Fainelli
Quoted 38 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---

 include/linux/device.h  |   4 ++
 include/linux/swiotlb.h |   7 +-
 kernel/dma/Kconfig      |   1 +
 kernel/dma/swiotlb.c    | 144 ++++++++++++++++++++++++++++++++++------
 4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +1 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
   * @dma_pools:	Dma pools (if dma'ble device).
   * @dma_mem:	Internal for coherent mem override.
   * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
   * @archdata:	For arch-specific additions.
   * @of_node:	Associated device tree node.
   * @fwnode:	Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
  #ifdef CONFIG_DMA_CMA

  struct cma *cma_area; /* contiguous memory area for dma
  allocations */

include/linux/device.h +3 -0
+#endif
+#ifdef CONFIG_SWIOTLB
+	struct io_tlb_mem	*dma_io_tlb_mem;

 #endif
  /* arch specific additions */
  struct dev_archdata archdata;

include/linux/swiotlb.h +0 -0
index dd8eb57cbb8f..a1bbd7788885 100644

include/linux/swiotlb.h extern enum swiotlb_force swiotlb_force; · line 79

Quoted 5 lines
include/linux/swiotlb.h +1 -1
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
   *
   * @start:	The start address of the swiotlb memory pool. Used to do a quick
   *		range check to see if the memory was in fact allocated by this
- *		API.
+ *		API. For restricted DMA pool, this is device tree adjustable.

Maybe write it as this is "firmware adjustable" such that when/if ACPI needs something like this, the description does not need updating.

[snip]

Quoted 9 lines
include/linux/swiotlb.h +9 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+				    struct device *dev)
+{
+	struct io_tlb_mem *mem = rmem->priv;
+	int ret;
+
+	if (dev->dma_io_tlb_mem)
+		return -EBUSY;
+
Quoted 8 lines

include/linux/swiotlb.h extern enum swiotlb_force swiotlb_force;

Quoted 6 lines
include/linux/swiotlb.h +6 -0
+	if (!mem) {
+		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+		if (!mem)
+			return -ENOMEM;
+
+		if (!memremap(rmem->base, rmem->size, MEMREMAP_WB)) {

MEMREMAP_WB sounds appropriate as a default.

As per the binding 'no-map' has to be disabled here. So AFAIU, this memory will be part of the linear mapping. Is this really needed then?

Quoted 5 lines

Documentation/devicetree/bindings/reserved-memory/ramoops.txt does define an "unbuffered" property which in premise could be applied to the generic reserved memory binding as well and that we may have to be honoring here, if we were to make it more generic. Oh well, this does not need to be addressed right now I guess.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#18 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 50 lines · Nicolas Saenz Julienne

Hi All,

Quoted 47 lines · Florian Fainelli
Quoted 38 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
  include/linux/device.h  |   4 ++
  include/linux/swiotlb.h |   7 +-
  kernel/dma/Kconfig      |   1 +
  kernel/dma/swiotlb.c    | 144 ++++++++++++++++++++++++++++++++++------
  4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +4 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
   * @dma_pools:	Dma pools (if dma'ble device).
   * @dma_mem:	Internal for coherent mem override.
   * @cma_area:	Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
   * @archdata:	For arch-specific additions.
   * @of_node:	Associated device tree node.
   * @fwnode:	Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
  #ifdef CONFIG_DMA_CMA
  	struct cma *cma_area;		/* contiguous memory area for dma
  					   allocations */
+#endif
+#ifdef CONFIG_SWIOTLB
+	struct io_tlb_mem	*dma_io_tlb_mem;
  #endif
  	/* arch specific additions */
  	struct dev_archdata	archdata;
include/linux/swiotlb.h +0 -0
index dd8eb57cbb8f..a1bbd7788885 100644

include/linux/swiotlb.h extern enum swiotlb_force swiotlb_force; · line 79

Quoted 5 lines
include/linux/swiotlb.h +1 -1
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
   *
   * @start:	The start address of the swiotlb memory pool. Used to do a quick
   *		range check to see if the memory was in fact allocated by this
- *		API.
+ *		API. For restricted DMA pool, this is device tree adjustable.

Maybe write it as this is "firmware adjustable" such that when/if ACPI needs something like this, the description does not need updating.

TBH I really don't think this needs calling out at all. Even in the regular case, the details of exactly how and where the pool is allocated are beyond the scope of this code - architectures already have several ways to control that and make their own decisions.

Quoted 21 lines
Quoted 12 lines

[snip]

Quoted 9 lines
include/linux/swiotlb.h +9 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+				    struct device *dev)
+{
+	struct io_tlb_mem *mem = rmem->priv;
+	int ret;
+
+	if (dev->dma_io_tlb_mem)
+		return -EBUSY;
+
Quoted 8 lines

include/linux/swiotlb.h

Quoted 6 lines
include/linux/swiotlb.h +6 -0
+	if (!mem) {
+		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+		if (!mem)
+			return -ENOMEM;
+
+		if (!memremap(rmem->base, rmem->size, MEMREMAP_WB)) {

MEMREMAP_WB sounds appropriate as a default.

Quoted 2 lines

As per the binding 'no-map' has to be disabled here. So AFAIU, this memory will be part of the linear mapping. Is this really needed then?

More than that, I'd assume that we *have* to use the linear/direct map address rather than anything that has any possibility of being a vmalloc remap, otherwise we can no longer safely rely on phys_to_dma/dma_to_phys, no?

That said, given that we're not actually using the returned address, I'm not entirely sure what the point of this call is anyway. If we can assume it's always going to return the linear map address via try_ram_remap() then we can equally just go ahead and use the linear map address straight away. I don't really see how we could ever hit the "is_ram == REGION_MIXED" case in memremap() that would return NULL, if we passed the memblock check earlier in __reserved_mem_alloc_size() such that this rmem node ever got to be initialised at all.

Robin.

Quoted 8 lines
Quoted 5 lines

Documentation/devicetree/bindings/reserved-memory/ramoops.txt does define an "unbuffered" property which in premise could be applied to the generic reserved memory binding as well and that we may have to be honoring here, if we were to make it more generic. Oh well, this does not need to be addressed right now I guess.

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#19 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 82 lines · Robin Murphy
Quoted 54 lines · Nicolas Saenz Julienne

Hi All,

Quoted 51 lines · Florian Fainelli
Quoted 41 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
  include/linux/device.h  |   4 ++
  include/linux/swiotlb.h |   7 +-
  kernel/dma/Kconfig      |   1 +
  kernel/dma/swiotlb.c    | 144
++++++++++++++++++++++++++++++++++------

  4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +1 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
    * @dma_pools:    Dma pools (if dma'ble device).
    * @dma_mem:    Internal for coherent mem override.
    * @cma_area:    Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
    * @archdata:    For arch-specific additions.
    * @of_node:    Associated device tree node.
    * @fwnode:    Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
   #ifdef CONFIG_DMA_CMA

      struct cma *cma_area;        /* contiguous memory area for dma allocations */

include/linux/device.h +3 -0
+#endif
+#ifdef CONFIG_SWIOTLB
+    struct io_tlb_mem    *dma_io_tlb_mem;

  #endif
      /* arch specific additions */
      struct dev_archdata    archdata;

include/linux/swiotlb.h +0 -0
index dd8eb57cbb8f..a1bbd7788885 100644
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
    *
    * @start:    The start address of the swiotlb memory pool. Used
Quoted 6 lines

to do a quick
   *        range check to see if the memory was in fact allocated by this
- *        API.
+ *        API. For restricted DMA pool, this is device tree adjustable.

Maybe write it as this is "firmware adjustable" such that when/if ACPI needs something like this, the description does not need updating.

TBH I really don't think this needs calling out at all. Even in the regular case, the details of exactly how and where the pool is allocated are beyond the scope of this code - architectures already have several ways to control that and make their own decisions.

Quoted 21 lines
Quoted 20 lines

[snip]

Quoted 5 lines
include/linux/swiotlb.h +5 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+                    struct device *dev)
+{
+    struct io_tlb_mem *mem = rmem->priv;
+    int ret;

include/linux/swiotlb.h

Quoted 10 lines
include/linux/swiotlb.h +10 -0
+
+    if (dev->dma_io_tlb_mem)
+        return -EBUSY;
+
+    if (!mem) {
+        mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+        if (!mem)
+            return -ENOMEM;
+
+        if (!memremap(rmem->base, rmem->size, MEMREMAP_WB)) {

MEMREMAP_WB sounds appropriate as a default.

Quoted 8 lines
Quoted 3 lines

As per the binding 'no-map' has to be disabled here. So AFAIU, this memory will
be part of the linear mapping. Is this really needed then?

More than that, I'd assume that we *have* to use the linear/direct map address rather than anything that has any possibility of being a vmalloc remap, otherwise we can no longer safely rely on phys_to_dma/dma_to_phys, no?

I believe you are right, which means that if we want to make use of the restricted DMA pool on a 32-bit architecture (and we do, at least, I do) we should probably add some error checking/warning to ensure the restricted DMA pool falls within the linear map.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
maintainer
#20 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 92 lines · Florian Fainelli
Quoted 82 lines · Robin Murphy
Quoted 54 lines · Nicolas Saenz Julienne

Hi All,

Quoted 51 lines · Florian Fainelli
Quoted 41 lines · Claire Chang

Add the initialization function to create restricted DMA pools from matching reserved-memory nodes in the device tree.

---
   include/linux/device.h  |   4 ++
   include/linux/swiotlb.h |   7 +-
   kernel/dma/Kconfig      |   1 +
   kernel/dma/swiotlb.c    | 144
++++++++++++++++++++++++++++++++++------
   4 files changed, 131 insertions(+), 25 deletions(-)
include/linux/device.h +4 -0
index 89bb8b84173e..ca6f71ec8871 100644
@@ -413,6 +413,7 @@ struct dev_links_info {
    * @dma_pools:    Dma pools (if dma'ble device).
    * @dma_mem:    Internal for coherent mem override.
    * @cma_area:    Contiguous memory area for dma allocations
+ * @dma_io_tlb_mem: Internal for swiotlb io_tlb_mem override.
    * @archdata:    For arch-specific additions.
    * @of_node:    Associated device tree node.
    * @fwnode:    Associated device node supplied by platform firmware.
@@ -515,6 +516,9 @@ struct device {
   #ifdef CONFIG_DMA_CMA
       struct cma *cma_area;        /* contiguous memory area for dma
                          allocations */
+#endif
+#ifdef CONFIG_SWIOTLB
+    struct io_tlb_mem    *dma_io_tlb_mem;
   #endif
       /* arch specific additions */
       struct dev_archdata    archdata;
include/linux/swiotlb.h +0 -0
index dd8eb57cbb8f..a1bbd7788885 100644
@@ -76,12 +76,13 @@ extern enum swiotlb_force swiotlb_force;
    *
    * @start:    The start address of the swiotlb memory pool. Used
Quoted 6 lines

to do a quick
   *        range check to see if the memory was in fact allocated
by this
- *        API.
+ *        API. For restricted DMA pool, this is device tree adjustable.

Maybe write it as this is "firmware adjustable" such that when/if ACPI needs something like this, the description does not need updating.

TBH I really don't think this needs calling out at all. Even in the regular case, the details of exactly how and where the pool is allocated are beyond the scope of this code - architectures already have several ways to control that and make their own decisions.

Quoted 21 lines
Quoted 20 lines

[snip]

Quoted 5 lines
include/linux/swiotlb.h +5 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+                    struct device *dev)
+{
+    struct io_tlb_mem *mem = rmem->priv;
+    int ret;

include/linux/swiotlb.h

Quoted 10 lines
include/linux/swiotlb.h +10 -0
+
+    if (dev->dma_io_tlb_mem)
+        return -EBUSY;
+
+    if (!mem) {
+        mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+        if (!mem)
+            return -ENOMEM;
+
+        if (!memremap(rmem->base, rmem->size, MEMREMAP_WB)) {

MEMREMAP_WB sounds appropriate as a default.

Quoted 8 lines
Quoted 3 lines

As per the binding 'no-map' has to be disabled here. So AFAIU, this memory will
be part of the linear mapping. Is this really needed then?

More than that, I'd assume that we *have* to use the linear/direct map address rather than anything that has any possibility of being a vmalloc remap, otherwise we can no longer safely rely on phys_to_dma/dma_to_phys, no?

Quoted 4 lines

I believe you are right, which means that if we want to make use of the restricted DMA pool on a 32-bit architecture (and we do, at least, I do) we should probably add some error checking/warning to ensure the restricted DMA pool falls within the linear map.

Oh, good point - I'm so used to 64-bit that I instinctively just blanked out the !PageHighMem() condition in try_ram_remap(). So maybe the original intent here *was* to effectively just implement that check, but if so it could still do with being a lot more explicit.

Cheers,
Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#21 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Claire Chang

include/linux/device.h struct device { · line 522

Quoted 3 lines
include/linux/device.h +2 -0
+#ifdef CONFIG_SWIOTLB
+	struct io_tlb_mem	*dma_io_tlb_mem;
 #endif

Please add a new config option for this code instead of always building it when swiotlb is enabled.

kernel/dma/swiotlb.c static void swiotlb_cleanup(void) · line 328

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+static int swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
+				   size_t size)

Can you split the refactoring in swiotlb.c into one or more prep patches?

kernel/dma/swiotlb.c bool is_swiotlb_active(void) · line 757

Quoted 13 lines
kernel/dma/swiotlb.c +13 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+				    struct device *dev)
+{
+	struct io_tlb_mem *mem = rmem->priv;
+	int ret;
+
+	if (dev->dma_io_tlb_mem)
+		return -EBUSY;
+
+	if (!mem) {
+		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+		if (!mem)
+			return -ENOMEM;

What is the calling convention here that allows for a NULL and non-NULL private data?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#22 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

Quoted 14 lines · Christoph Hellwig

include/linux/device.h struct device { · line 522

Quoted 4 lines
include/linux/device.h +2 -0
+#ifdef CONFIG_SWIOTLB
+     struct io_tlb_mem       *dma_io_tlb_mem;
 #endif

Please add a new config option for this code instead of always building it when swiotlb is enabled.

kernel/dma/swiotlb.c static void swiotlb_cleanup(void) · line 328

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+static int swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
+                                size_t size)

Can you split the refactoring in swiotlb.c into one or more prep patches?

Quoted 16 lines

kernel/dma/swiotlb.c bool is_swiotlb_active(void) · line 757

Quoted 13 lines
kernel/dma/swiotlb.c +13 -0
+static int rmem_swiotlb_device_init(struct reserved_mem *rmem,
+                                 struct device *dev)
+{
+     struct io_tlb_mem *mem = rmem->priv;
+     int ret;
+
+     if (dev->dma_io_tlb_mem)
+             return -EBUSY;
+
+     if (!mem) {
+             mem = kzalloc(sizeof(*mem), GFP_KERNEL);
+             if (!mem)
+                     return -ENOMEM;

What is the calling convention here that allows for a NULL and non-NULL private data?

Since multiple devices can share the same pool, the private data, io_tlb_mem struct, will be initialized by the first device attached to it.
This is similar to rmem_dma_device_init() in kernel/dma/coherent.c. I'll add a comment for it in next version.

#23 lore raw

Re: [RFC PATCH v3 2/6] swiotlb: Add restricted DMA pool

In reply to Claire Chang
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Claire,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on powerpc/next]
[also build test ERROR on linus/master v5.11-rc3 next-20210115] [cannot apply to swiotlb/linux-next robh/for-next xen-tip/linux-next iommu/next] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Claire-Chang/Restricted-DMA/20210106-114544
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: x86_64-rhel (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/9c54685703ccb9e53d6a2558cac472b053fb5470
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Claire-Chang/Restricted-DMA/20210106-114544
        git checkout 9c54685703ccb9e53d6a2558cac472b053fb5470
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

drivers/tty/serial/earlycon.c: In function 'of_setup_earlycon':

>> drivers/tty/serial/earlycon.c:258:9: error: implicit declaration of function 'of_flat_dt_translate_address' [-Werror=implicit-function-declaration]
     258 |  addr = of_flat_dt_translate_address(node);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for OF_EARLY_FLATTREE Depends on OF
Selected by
- SWIOTLB

vim +/of_flat_dt_translate_address +258 drivers/tty/serial/earlycon.c

8477614d9f7c5ce0 Peter Hurley       2016-01-16  245  
c90fe9c0394b068c Peter Hurley       2016-01-16  246  int __init of_setup_earlycon(const struct earlycon_id *match,
088da2a17619cf01 Peter Hurley       2016-01-16  247  			     unsigned long node,
4d118c9a86659085 Peter Hurley       2016-01-16  248  			     const char *options)
b0b6abd34c1b508d Rob Herring        2014-03-27  249  {
b0b6abd34c1b508d Rob Herring        2014-03-27  250  	int err;
b0b6abd34c1b508d Rob Herring        2014-03-27  251  	struct uart_port *port = &early_console_dev.port;
088da2a17619cf01 Peter Hurley       2016-01-16  252  	const __be32 *val;
088da2a17619cf01 Peter Hurley       2016-01-16  253  	bool big_endian;
c90fe9c0394b068c Peter Hurley       2016-01-16  254  	u64 addr;
b0b6abd34c1b508d Rob Herring        2014-03-27  255  
e1dd3bef6d03c908 Geert Uytterhoeven 2015-11-27  256  	spin_lock_init(&port->lock);
b0b6abd34c1b508d Rob Herring        2014-03-27  257  	port->iotype = UPIO_MEM;
c90fe9c0394b068c Peter Hurley       2016-01-16 @258  	addr = of_flat_dt_translate_address(node);
c90fe9c0394b068c Peter Hurley       2016-01-16  259  	if (addr == OF_BAD_ADDR) {
c90fe9c0394b068c Peter Hurley       2016-01-16  260  		pr_warn("[%s] bad address\n", match->name);
c90fe9c0394b068c Peter Hurley       2016-01-16  261  		return -ENXIO;
c90fe9c0394b068c Peter Hurley       2016-01-16  262  	}
b0b6abd34c1b508d Rob Herring        2014-03-27  263  	port->mapbase = addr;
b0b6abd34c1b508d Rob Herring        2014-03-27  264  
088da2a17619cf01 Peter Hurley       2016-01-16  265  	val = of_get_flat_dt_prop(node, "reg-offset", NULL);
088da2a17619cf01 Peter Hurley       2016-01-16  266  	if (val)
088da2a17619cf01 Peter Hurley       2016-01-16  267  		port->mapbase += be32_to_cpu(*val);
1f66dd36bb184373 Greentime Hu       2018-02-13  268  	port->membase = earlycon_map(port->mapbase, SZ_4K);
1f66dd36bb184373 Greentime Hu       2018-02-13  269  
088da2a17619cf01 Peter Hurley       2016-01-16  270  	val = of_get_flat_dt_prop(node, "reg-shift", NULL);
088da2a17619cf01 Peter Hurley       2016-01-16  271  	if (val)
088da2a17619cf01 Peter Hurley       2016-01-16  272  		port->regshift = be32_to_cpu(*val);
088da2a17619cf01 Peter Hurley       2016-01-16  273  	big_endian = of_get_flat_dt_prop(node, "big-endian", NULL) != NULL ||
088da2a17619cf01 Peter Hurley       2016-01-16  274  		(IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
088da2a17619cf01 Peter Hurley       2016-01-16  275  		 of_get_flat_dt_prop(node, "native-endian", NULL) != NULL);
088da2a17619cf01 Peter Hurley       2016-01-16  276  	val = of_get_flat_dt_prop(node, "reg-io-width", NULL);
088da2a17619cf01 Peter Hurley       2016-01-16  277  	if (val) {
088da2a17619cf01 Peter Hurley       2016-01-16  278  		switch (be32_to_cpu(*val)) {
088da2a17619cf01 Peter Hurley       2016-01-16  279  		case 1:
088da2a17619cf01 Peter Hurley       2016-01-16  280  			port->iotype = UPIO_MEM;
088da2a17619cf01 Peter Hurley       2016-01-16  281  			break;
088da2a17619cf01 Peter Hurley       2016-01-16  282  		case 2:
088da2a17619cf01 Peter Hurley       2016-01-16  283  			port->iotype = UPIO_MEM16;
088da2a17619cf01 Peter Hurley       2016-01-16  284  			break;
088da2a17619cf01 Peter Hurley       2016-01-16  285  		case 4:
088da2a17619cf01 Peter Hurley       2016-01-16  286  			port->iotype = (big_endian) ? UPIO_MEM32BE : UPIO_MEM32;
088da2a17619cf01 Peter Hurley       2016-01-16  287  			break;
088da2a17619cf01 Peter Hurley       2016-01-16  288  		default:
088da2a17619cf01 Peter Hurley       2016-01-16  289  			pr_warn("[%s] unsupported reg-io-width\n", match->name);
088da2a17619cf01 Peter Hurley       2016-01-16  290  			return -EINVAL;
088da2a17619cf01 Peter Hurley       2016-01-16  291  		}
088da2a17619cf01 Peter Hurley       2016-01-16  292  	}
088da2a17619cf01 Peter Hurley       2016-01-16  293  
31cb9a8575ca04f4 Eugeniy Paltsev    2017-08-21  294  	val = of_get_flat_dt_prop(node, "current-speed", NULL);
31cb9a8575ca04f4 Eugeniy Paltsev    2017-08-21  295  	if (val)
31cb9a8575ca04f4 Eugeniy Paltsev    2017-08-21  296  		early_console_dev.baud = be32_to_cpu(*val);
31cb9a8575ca04f4 Eugeniy Paltsev    2017-08-21  297  
814453adea7d081a Michal Simek       2018-04-10  298  	val = of_get_flat_dt_prop(node, "clock-frequency", NULL);
814453adea7d081a Michal Simek       2018-04-10  299  	if (val)
814453adea7d081a Michal Simek       2018-04-10  300  		port->uartclk = be32_to_cpu(*val);
814453adea7d081a Michal Simek       2018-04-10  301  
4d118c9a86659085 Peter Hurley       2016-01-16  302  	if (options) {
31cb9a8575ca04f4 Eugeniy Paltsev    2017-08-21  303  		early_console_dev.baud = simple_strtoul(options, NULL, 0);
4d118c9a86659085 Peter Hurley       2016-01-16  304  		strlcpy(early_console_dev.options, options,
4d118c9a86659085 Peter Hurley       2016-01-16  305  			sizeof(early_console_dev.options));
4d118c9a86659085 Peter Hurley       2016-01-16  306  	}
05d961320ba624c9 Peter Hurley       2016-01-16  307  	earlycon_init(&early_console_dev, match->name);
4d118c9a86659085 Peter Hurley       2016-01-16  308  	err = match->setup(&early_console_dev, options);
f28295cc8ce14b7a Hsin-Yi Wang       2020-09-15  309  	earlycon_print_info(&early_console_dev);
b0b6abd34c1b508d Rob Herring        2014-03-27  310  	if (err < 0)
b0b6abd34c1b508d Rob Herring        2014-03-27  311  		return err;
b0b6abd34c1b508d Rob Herring        2014-03-27  312  	if (!early_console_dev.con->write)
b0b6abd34c1b508d Rob Herring        2014-03-27  313  		return -ENODEV;
b0b6abd34c1b508d Rob Herring        2014-03-27  314  
b0b6abd34c1b508d Rob Herring        2014-03-27  315  
b0b6abd34c1b508d Rob Herring        2014-03-27  316  	register_console(early_console_dev.con);
b0b6abd34c1b508d Rob Herring        2014-03-27  317  	return 0;
b0b6abd34c1b508d Rob Herring        2014-03-27  318  }
8477614d9f7c5ce0 Peter Hurley       2016-01-16  319  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#24 lore raw

RFC PATCH v3 3/6 swiotlb: Use restricted DMA pool if available

In reply to Claire Chang

Regardless of swiotlb setting, the restricted DMA pool is preferred if available.

The restricted DMA pools provide a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to restrict the DMA to a predefined memory region.

---
 drivers/iommu/dma-iommu.c | 12 ++++++------
 include/linux/swiotlb.h   | 17 +++++++++++------
 kernel/dma/direct.c       |  8 ++++----
 kernel/dma/direct.h       | 10 ++++++----
 kernel/dma/swiotlb.c      | 13 ++++++-------
 5 files changed, 33 insertions(+), 27 deletions(-)
drivers/iommu/dma-iommu.c +6 -6
index f0305e6aac1b..1343cc2ef27a 100644
@@ -516,7 +516,7 @@ static void __iommu_dma_unmap_swiotlb(struct device *dev, dma_addr_t dma_addr,
 
 	__iommu_dma_unmap(dev, dma_addr, size);
 
-	if (unlikely(is_swiotlb_buffer(phys)))
+	if (unlikely(is_swiotlb_buffer(dev, phys)))
 		swiotlb_tbl_unmap_single(dev, phys, size,
 				iova_align(iovad, size), dir, attrs);
 }
@@ -592,7 +592,7 @@ static dma_addr_t __iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,
 	}
 
 	iova = __iommu_dma_map(dev, phys, aligned_size, prot, dma_mask);
-	if ((iova == DMA_MAPPING_ERROR) && is_swiotlb_buffer(phys))
+	if ((iova == DMA_MAPPING_ERROR) && is_swiotlb_buffer(dev, phys))
 		swiotlb_tbl_unmap_single(dev, phys, org_size,
 				aligned_size, dir, attrs);
 
@@ -764,7 +764,7 @@ static void iommu_dma_sync_single_for_cpu(struct device *dev,
 	if (!dev_is_dma_coherent(dev))
 		arch_sync_dma_for_cpu(phys, size, dir);
 
-	if (is_swiotlb_buffer(phys))
+	if (is_swiotlb_buffer(dev, phys))
 		swiotlb_tbl_sync_single(dev, phys, size, dir, SYNC_FOR_CPU);
 }
 
@@ -777,7 +777,7 @@ static void iommu_dma_sync_single_for_device(struct device *dev,
 		return;
 
 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
-	if (is_swiotlb_buffer(phys))
+	if (is_swiotlb_buffer(dev, phys))
 		swiotlb_tbl_sync_single(dev, phys, size, dir, SYNC_FOR_DEVICE);
 
 	if (!dev_is_dma_coherent(dev))
@@ -798,7 +798,7 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev,
 		if (!dev_is_dma_coherent(dev))
 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
 
-		if (is_swiotlb_buffer(sg_phys(sg)))
+		if (is_swiotlb_buffer(dev, sg_phys(sg)))
 			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
 						dir, SYNC_FOR_CPU);
 	}
@@ -815,7 +815,7 @@ static void iommu_dma_sync_sg_for_device(struct device *dev,
 		return;
 
 	for_each_sg(sgl, sg, nelems, i) {
-		if (is_swiotlb_buffer(sg_phys(sg)))
+		if (is_swiotlb_buffer(dev, sg_phys(sg)))
 			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
 						dir, SYNC_FOR_DEVICE);
 
include/linux/swiotlb.h +11 -6
index a1bbd7788885..5135e5636042 100644
@@ -2,12 +2,12 @@
 #ifndef __LINUX_SWIOTLB_H
 #define __LINUX_SWIOTLB_H
 
+#include <linux/device.h>
 #include <linux/dma-direction.h>
 #include <linux/init.h>
 #include <linux/types.h>
 #include <linux/limits.h>
 
-struct device;
 struct page;
 struct scatterlist;
 
@@ -106,9 +106,14 @@ struct io_tlb_mem {
 };
 extern struct io_tlb_mem io_tlb_default_mem;
 
-static inline bool is_swiotlb_buffer(phys_addr_t paddr)
+static inline struct io_tlb_mem *get_io_tlb_mem(struct device *dev)
 {
-	struct io_tlb_mem *mem = &io_tlb_default_mem;
+	return dev->dma_io_tlb_mem ? dev->dma_io_tlb_mem : &io_tlb_default_mem;
+}
+
+static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
+{
+	struct io_tlb_mem *mem = get_io_tlb_mem(dev);
 
 	return paddr >= mem->start && paddr < mem->end;
 }
@@ -116,11 +121,11 @@ static inline bool is_swiotlb_buffer(phys_addr_t paddr)
 void __init swiotlb_exit(void);
 unsigned int swiotlb_max_segment(void);
 size_t swiotlb_max_mapping_size(struct device *dev);
-bool is_swiotlb_active(void);
+bool is_swiotlb_active(struct device *dev);
 void __init swiotlb_adjust_size(unsigned long new_size);
 #else
 #define swiotlb_force SWIOTLB_NO_FORCE
-static inline bool is_swiotlb_buffer(phys_addr_t paddr)
+static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
 {
 	return false;
 }
@@ -136,7 +141,7 @@ static inline size_t swiotlb_max_mapping_size(struct device *dev)
 	return SIZE_MAX;
 }
 
-static inline bool is_swiotlb_active(void)
+static inline bool is_swiotlb_active(struct device *dev)
 {
 	return false;
 }
kernel/dma/direct.c +4 -4
index 002268262c9a..30ccbc08e229 100644
@@ -343,7 +343,7 @@ void dma_direct_sync_sg_for_device(struct device *dev,
 	for_each_sg(sgl, sg, nents, i) {
 		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-		if (unlikely(is_swiotlb_buffer(paddr)))
+		if (unlikely(is_swiotlb_buffer(dev, paddr)))
 			swiotlb_tbl_sync_single(dev, paddr, sg->length,
 					dir, SYNC_FOR_DEVICE);
 
@@ -369,7 +369,7 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
 		if (!dev_is_dma_coherent(dev))
 			arch_sync_dma_for_cpu(paddr, sg->length, dir);
 
-		if (unlikely(is_swiotlb_buffer(paddr)))
+		if (unlikely(is_swiotlb_buffer(dev, paddr)))
 			swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
 					SYNC_FOR_CPU);
 
@@ -495,7 +495,7 @@ int dma_direct_supported(struct device *dev, u64 mask)
 size_t dma_direct_max_mapping_size(struct device *dev)
 {
 	/* If SWIOTLB is active, use its maximum mapping size */
-	if (is_swiotlb_active() &&
+	if (is_swiotlb_active(dev) &&
 	    (dma_addressing_limited(dev) || swiotlb_force == SWIOTLB_FORCE))
 		return swiotlb_max_mapping_size(dev);
 	return SIZE_MAX;
@@ -504,7 +504,7 @@ size_t dma_direct_max_mapping_size(struct device *dev)
 bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr)
 {
 	return !dev_is_dma_coherent(dev) ||
-		is_swiotlb_buffer(dma_to_phys(dev, dma_addr));
+		is_swiotlb_buffer(dev, dma_to_phys(dev, dma_addr));
 }
 
 /**
kernel/dma/direct.h +6 -4
index b98615578737..7188834cc4c7 100644
@@ -56,7 +56,7 @@ static inline void dma_direct_sync_single_for_device(struct device *dev,
 {
 	phys_addr_t paddr = dma_to_phys(dev, addr);
 
-	if (unlikely(is_swiotlb_buffer(paddr)))
+	if (unlikely(is_swiotlb_buffer(dev, paddr)))
 		swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
 
 	if (!dev_is_dma_coherent(dev))
@@ -73,7 +73,7 @@ static inline void dma_direct_sync_single_for_cpu(struct device *dev,
 		arch_sync_dma_for_cpu_all();
 	}
 
-	if (unlikely(is_swiotlb_buffer(paddr)))
+	if (unlikely(is_swiotlb_buffer(dev, paddr)))
 		swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);
 
 	if (dir == DMA_FROM_DEVICE)
@@ -87,8 +87,10 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev,
 	phys_addr_t phys = page_to_phys(page) + offset;
 	dma_addr_t dma_addr = phys_to_dma(dev, phys);
 
-	if (unlikely(swiotlb_force == SWIOTLB_FORCE))
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(swiotlb_force == SWIOTLB_FORCE) || dev->dma_io_tlb_mem)
 		return swiotlb_map(dev, phys, size, dir, attrs);
+#endif
 
 	if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
 		if (swiotlb_force != SWIOTLB_NO_FORCE)
@@ -113,7 +115,7 @@ static inline void dma_direct_unmap_page(struct device *dev, dma_addr_t addr,
 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
 		dma_direct_sync_single_for_cpu(dev, addr, size, dir);
 
-	if (unlikely(is_swiotlb_buffer(phys)))
+	if (unlikely(is_swiotlb_buffer(dev, phys)))
 		swiotlb_tbl_unmap_single(dev, phys, size, size, dir, attrs);
 }
 #endif /* _KERNEL_DMA_DIRECT_H */
kernel/dma/swiotlb.c +6 -7
index 7fb2ac087d23..1f05af09e61a 100644
@@ -222,7 +222,6 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
 		mem->orig_addr[i] = INVALID_PHYS_ADDR;
 	}
 	mem->index = 0;
-	no_iotlb_memory = false;
 
 	if (verbose)
 		swiotlb_print_info();
@@ -464,7 +463,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
 		size_t mapping_size, size_t alloc_size,
 		enum dma_data_direction dir, unsigned long attrs)
 {
-	struct io_tlb_mem *mem = &io_tlb_default_mem;
+	struct io_tlb_mem *mem = get_io_tlb_mem(hwdev);
 	dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, mem->start);
 	unsigned long flags;
 	phys_addr_t tlb_addr;
@@ -475,7 +474,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
 	unsigned long max_slots;
 	unsigned long tmp_io_tlb_used;
 
-	if (no_iotlb_memory)
+	if (no_iotlb_memory && !hwdev->dma_io_tlb_mem)
 		panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
 
 	if (mem_encrypt_active())
@@ -597,7 +596,7 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
 			      size_t mapping_size, size_t alloc_size,
 			      enum dma_data_direction dir, unsigned long attrs)
 {
-	struct io_tlb_mem *mem = &io_tlb_default_mem;
+	struct io_tlb_mem *mem = get_io_tlb_mem(hwdev);
 	unsigned long flags;
 	int i, count, nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
 	int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
@@ -645,7 +644,7 @@ void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr,
 			     size_t size, enum dma_data_direction dir,
 			     enum dma_sync_target target)
 {
-	struct io_tlb_mem *mem = &io_tlb_default_mem;
+	struct io_tlb_mem *mem = get_io_tlb_mem(hwdev);
 	int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT;
 	phys_addr_t orig_addr = mem->orig_addr[index];
 
@@ -712,13 +711,13 @@ size_t swiotlb_max_mapping_size(struct device *dev)
 	return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
 }
 
-bool is_swiotlb_active(void)
+bool is_swiotlb_active(struct device *dev)
 {
 	/*
 	 * When SWIOTLB is initialized, even if mem->start points to physical
 	 * address zero, mem->end surely doesn't.
 	 */
-	return io_tlb_default_mem.end != 0;
+	return io_tlb_default_mem.end != 0 || dev->dma_io_tlb_mem;
 }
 
 #ifdef CONFIG_DEBUG_FS
Signature · 6 lines
2.29.2.729.g45daf8777d-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#25 lore raw

Re: [RFC PATCH v3 3/6] swiotlb: Use restricted DMA pool if available

Quoted 10 lines · Claire Chang

Regardless of swiotlb setting, the restricted DMA pool is preferred if available.

The restricted DMA pools provide a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to restrict the DMA to a predefined memory region.

You could probably split this patch into two:

- one that introduces the get_io_tlb_mem() getter, updates all callers of is_swiotlb_buffer() to gain a 'struct device' argument
- another one that does add support for a non-default swiotlb pool and adds dev->dma_io_tlb_mem

Other than that, LGTM!

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#26 lore raw

Re: [RFC PATCH v3 3/6] swiotlb: Use restricted DMA pool if available

In reply to Claire Chang

kernel/dma/direct.h static inline dma_addr_t dma_direct_map_page(struct device *dev, · line 93

Quoted 4 lines
kernel/dma/direct.h +3 -0
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(swiotlb_force == SWIOTLB_FORCE) || dev->dma_io_tlb_mem)
 		return swiotlb_map(dev, phys, size, dir, attrs);
+#endif

Please provide a wrapper for the dev->dma_io_tlb_mem check that always returns false if the per-device swiotlb support is not enabled.

kernel/dma/swiotlb.c int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) · line 225

Quoted 8 lines

index 7fb2ac087d23..1f05af09e61a 100644

kernel/dma/swiotlb.c +0 -1
@@ -222,7 +222,6 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
 		mem->orig_addr[i] = INVALID_PHYS_ADDR;
 	}
 	mem->index = 0;
-	no_iotlb_memory = false;

How does this fit in here?

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#27 lore raw

RFC PATCH v3 4/6 swiotlb: Add restricted DMA alloc/free support.

In reply to Claire Chang

Add the functions, swiotlb_alloc and swiotlb_free to support the memory allocation from restricted DMA pool.

---
 include/linux/swiotlb.h |   6 ++
 kernel/dma/direct.c     |  12 +++
 kernel/dma/swiotlb.c    | 171 +++++++++++++++++++++++++++++-----------
 3 files changed, 144 insertions(+), 45 deletions(-)
include/linux/swiotlb.h +6 -0
index 5135e5636042..84fe96e40685 100644
@@ -68,6 +68,12 @@ extern void swiotlb_tbl_sync_single(struct device *hwdev,
 dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys,
 		size_t size, enum dma_data_direction dir, unsigned long attrs);
 
+void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
+		    unsigned long attrs);
+
+void swiotlb_free(struct device *dev, size_t size, void *vaddr,
+		  dma_addr_t dma_addr, unsigned long attrs);
+
 #ifdef CONFIG_SWIOTLB
 extern enum swiotlb_force swiotlb_force;
 
kernel/dma/direct.c +12 -0
index 30ccbc08e229..126e9b3354d6 100644
@@ -137,6 +137,11 @@ void *dma_direct_alloc(struct device *dev, size_t size,
 	void *ret;
 	int err;
 
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(dev->dma_io_tlb_mem))
+		return swiotlb_alloc(dev, size, dma_handle, attrs);
+#endif
+
 	size = PAGE_ALIGN(size);
 	if (attrs & DMA_ATTR_NO_WARN)
 		gfp |= __GFP_NOWARN;
@@ -246,6 +251,13 @@ void dma_direct_free(struct device *dev, size_t size,
 {
 	unsigned int page_order = get_order(size);
 
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(dev->dma_io_tlb_mem)) {
+		swiotlb_free(dev, size, cpu_addr, dma_addr, attrs);
+		return;
+	}
+#endif
+
 	if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
 	    !force_dma_unencrypted(dev)) {
 		/* cpu_addr is a struct page cookie, not a kernel address */
kernel/dma/swiotlb.c +126 -45

219 lines not on this page · raw message

Signature · 6 lines
2.29.2.729.g45daf8777d-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#28 lore raw

Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support.

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Claire,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on powerpc/next]
[also build test WARNING on linus/master v5.11-rc2 next-20210104] [cannot apply to swiotlb/linux-next robh/for-next xen-tip/linux-next iommu/next] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Claire-Chang/Restricted-DMA/20210106-114544
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: x86_64-randconfig-m001-20210106 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate

smatch warnings:
kernel/dma/swiotlb.c:629 swiotlb_tbl_map_single() warn: unsigned 'index' is never less than zero.

vim +/index +629 kernel/dma/swiotlb.c

607
608 phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
609 size_t mapping_size, size_t alloc_size,
610 enum dma_data_direction dir, unsigned long attrs)
611 {
612 struct io_tlb_mem *mem = get_io_tlb_mem(hwdev);
613 dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, mem->start);
614 phys_addr_t tlb_addr;
615 unsigned int nslots, index;
616 int i;
617
618 if (mem_encrypt_active())
619 pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
620
621 if (mapping_size > alloc_size) {
622 dev_warn_once(hwdev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
623 mapping_size, alloc_size);
624 return (phys_addr_t)DMA_MAPPING_ERROR;
625 }
626
627 index = swiotlb_tbl_find_free_region(hwdev, tbl_dma_addr, alloc_size,
628 attrs);

 > 629		if (index < 0)

630 return (phys_addr_t)DMA_MAPPING_ERROR;
631
632 tlb_addr = mem->start + (index << IO_TLB_SHIFT);
633
634 /*
635 * Save away the mapping from the original address to the DMA address.
636 * This is needed when we sync the memory. Then we sync the buffer if
637 * needed.
638 */
639 nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
640 for (i = 0; i < nslots; i++)
641 mem->orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
642 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
643 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
644 swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
645
646 return tlb_addr;
647 }
648

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#29 lore raw

Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support.

In reply to Claire Chang
Quoted 5 lines · Claire Chang

Add the functions, swiotlb_alloc and swiotlb_free to support the memory allocation from restricted DMA pool.

---

[snip]

kernel/dma/direct.c void *dma_direct_alloc(struct device *dev, size_t size, · line 143

Quoted 12 lines
kernel/dma/direct.c +4 -0
index 30ccbc08e229..126e9b3354d6 100644
@@ -137,6 +137,11 @@ void *dma_direct_alloc(struct device *dev, size_t size,
 	void *ret;
 	int err;
 
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(dev->dma_io_tlb_mem))
+		return swiotlb_alloc(dev, size, dma_handle, attrs);
+#endif

While this is potentially a hot path, I am not sure of the unkikely is warranted, maybe best left as a plain conditional.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#30 lore raw

Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support.

In reply to Claire Chang

kernel/dma/direct.c void *dma_direct_alloc(struct device *dev, size_t size, · line 143

Quoted 4 lines
kernel/dma/direct.c +4 -0
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(dev->dma_io_tlb_mem))
+		return swiotlb_alloc(dev, size, dma_handle, attrs);
+#endif

Another place where the dma_io_tlb_mem is useful to avoid the ifdef.

kernel/dma/swiotlb.c static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,

Quoted 10 lines
kernel/dma/swiotlb.c +6 -3
-phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
-		size_t mapping_size, size_t alloc_size,
-		enum dma_data_direction dir, unsigned long attrs)
+static int swiotlb_tbl_find_free_region(struct device *hwdev,
+					dma_addr_t tbl_dma_addr,
+					size_t alloc_size,
+					unsigned long attrs)
 
+static void swiotlb_tbl_release_region(struct device *hwdev, int index,
+				       size_t size)

This refactoring should be another prep patch.

kernel/dma/swiotlb.c dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, · line 738

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
+		    unsigned long attrs)

I'd rather have the names convey there are for the per-device bounce buffer in some form.

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;

While we're at it I wonder if the io_tlb is something we could change while we're at it. Maybe replace io_tlb_mem with struct swiotlb and rename the field in struct device to dev_swiotlb?

Quoted 7 lines
kernel/dma/swiotlb.c +7 -0
+	int index;
+	void *vaddr;
+	phys_addr_t tlb_addr;
+
+	size = PAGE_ALIGN(size);
+	index = swiotlb_tbl_find_free_region(dev, mem->start, size, attrs);
+	if (index < 0)

kernel/dma/swiotlb.c dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, · line 757

Quoted 10 lines
kernel/dma/swiotlb.c +10 -0
+		return NULL;
+
+	tlb_addr = mem->start + (index << IO_TLB_SHIFT);
+	*dma_handle = phys_to_dma_unencrypted(dev, tlb_addr);
+
+	if (!dev_is_dma_coherent(dev)) {
+		unsigned long pfn = PFN_DOWN(tlb_addr);
+
+		/* remove any dirty cache lines on the kernel alias */
+		arch_dma_prep_coherent(pfn_to_page(pfn), size);

Can we hook in somewhat lower level in the dma-direct code so that all the remapping in dma-direct can be reused instead of duplicated? That also becomes important if we want to use non-remapping uncached support, e.g. on mips or x86, or the direct changing of the attributes that Will planned to look into for arm64.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#31 lore raw

Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support.

Quoted 48 lines · Christoph Hellwig

kernel/dma/direct.c void *dma_direct_alloc(struct device *dev, size_t size, · line 143

Quoted 4 lines
kernel/dma/direct.c +4 -0
+#ifdef CONFIG_SWIOTLB
+	if (unlikely(dev->dma_io_tlb_mem))
+		return swiotlb_alloc(dev, size, dma_handle, attrs);
+#endif

Another place where the dma_io_tlb_mem is useful to avoid the ifdef.

kernel/dma/swiotlb.c static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,

Quoted 10 lines
kernel/dma/swiotlb.c +6 -3
-phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
-		size_t mapping_size, size_t alloc_size,
-		enum dma_data_direction dir, unsigned long attrs)
+static int swiotlb_tbl_find_free_region(struct device *hwdev,
+					dma_addr_t tbl_dma_addr,
+					size_t alloc_size,
+					unsigned long attrs)
 
+static void swiotlb_tbl_release_region(struct device *hwdev, int index,
+				       size_t size)

This refactoring should be another prep patch.

kernel/dma/swiotlb.c dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, · line 738

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+void *swiotlb_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
+		    unsigned long attrs)

I'd rather have the names convey there are for the per-device bounce buffer in some form.

Review

Quoted 1 line
(quoted diff) +1 -0
+	struct io_tlb_mem *mem = dev->dma_io_tlb_mem;

While we're at it I wonder if the io_tlb is something we could change while we're at it. Maybe replace io_tlb_mem with struct swiotlb and rename the field in struct device to dev_swiotlb?

Quoted 15 lines
kernel/dma/swiotlb.c +15 -0
+	int index;
+	void *vaddr;
+	phys_addr_t tlb_addr;
+
+	size = PAGE_ALIGN(size);
+	index = swiotlb_tbl_find_free_region(dev, mem->start, size, attrs);
+	if (index < 0)
+		return NULL;
+
+	tlb_addr = mem->start + (index << IO_TLB_SHIFT);
+	*dma_handle = phys_to_dma_unencrypted(dev, tlb_addr);
+
+	if (!dev_is_dma_coherent(dev)) {
+		unsigned long pfn = PFN_DOWN(tlb_addr);
+
Quoted 8 lines

kernel/dma/swiotlb.c dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size, · line 757

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+		/* remove any dirty cache lines on the kernel alias */
+		arch_dma_prep_coherent(pfn_to_page(pfn), size);

Can we hook in somewhat lower level in the dma-direct code so that all the remapping in dma-direct can be reused instead of duplicated? That also becomes important if we want to use non-remapping uncached support, e.g. on mips or x86, or the direct changing of the attributes that Will planned to look into for arm64.

Indeed, AFAICS this ought to boil down to a direct equivalent of __dma_direct_alloc_pages() - other than the address there should be no conceptual difference between pages from the restricted pool and those from the regular page allocator, so this probably deserves to be plumbed in as an alternative to that.

Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#32 lore raw

Re: [RFC PATCH v3 4/6] swiotlb: Add restricted DMA alloc/free support.

Quoted 11 lines · Robin Murphy
Quoted 5 lines

Can we hook in somewhat lower level in the dma-direct code so that all the remapping in dma-direct can be reused instead of duplicated? That also becomes important if we want to use non-remapping uncached support, e.g. on mips or x86, or the direct changing of the attributes that Will planned to look into for arm64.

Indeed, AFAICS this ought to boil down to a direct equivalent of __dma_direct_alloc_pages() - other than the address there should be no conceptual difference between pages from the restricted pool and those from the regular page allocator, so this probably deserves to be plumbed in as an alternative to that.

Yes, that's what I mean. You managed to word it much better, though.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#33 lore raw

RFC PATCH v3 5/6 dt-bindings: of: Add restricted DMA pool

In reply to Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

---
 .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +24 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
           used as a shared pool of DMA buffers for a set of devices. It can
           be used by an operating system to instantiate the necessary pool
           management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.
         - vendor specific string in the form <vendor>,[<device>-]<usage>
 no-map (optional) - empty property
     - Indicates the operating system must not create a virtual mapping
@@ -120,6 +134,11 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
 			compatible = "acme,multimedia-memory";
 			reg = <0x77000000 0x4000000>;
 		};
+
+		restricted_dma_mem_reserved: restricted_dma_mem_reserved {
+			compatible = "restricted-dma-pool";
+			reg = <0x50000000 0x400000>;
+		};
 	};
 
 	/* ... */
@@ -138,4 +157,9 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
 		memory-region = <&multimedia_reserved>;
 		/* ... */
 	};
+
+	pcie_device: pcie_device@0,0 {
+		memory-region = <&restricted_dma_mem_reserved>;
+		/* ... */
+	};
 };
Signature · 6 lines
2.29.2.729.g45daf8777d-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#34 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 21 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

---
 .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +4 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
           used as a shared pool of DMA buffers for a set of devices. It can
           be used by an operating system to instantiate the necessary pool
           management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt compatible (optional) - standard definition · line 67

Quoted 10 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +10 -0
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.

Heya!

I think I am missing something obvious here so please bear with my questions:

- This code adds the means of having the SWIOTLB pool tied to a specific memory correct?

- Nothing stops the physical device from bypassing the SWIOTLB buffer. That is if an errant device screwed up the length or DMA address, the SWIOTLB would gladly do what the device told it do?

- This has to be combined with SWIOTLB-force-ish to always use the bounce buffer, otherwise you could still do DMA without using SWIOTLB (by not hitting the criteria for needing to use SWIOTLB)?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#35 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 36 lines · Konrad Rzeszutek Wilk
Quoted 21 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

---
 .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +4 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
           used as a shared pool of DMA buffers for a set of devices. It can
           be used by an operating system to instantiate the necessary pool
           management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt compatible (optional) - standard definition · line 67

Quoted 10 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +10 -0
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.

Heya!

Quoted 5 lines

I think I am missing something obvious here so please bear with my questions:

- This code adds the means of having the SWIOTLB pool tied to a specific memory correct?

It doesn't affect the existing SWIOTLB. It just utilizes the existing SWIOTLB code to create another DMA pool tied to a specific memory region for a given set of devices. It bounces the streaming DMA (map/unmap) in and out of that region and does the memory allocation (dma_direct_alloc) from the same region.

Quoted 5 lines

- Nothing stops the physical device from bypassing the SWIOTLB buffer. That is if an errant device screwed up the length or DMA address, the SWIOTLB would gladly do what the device told it do?

So the system needs to provide a way to lock down the memory access, e.g. MPU.

Quoted 4 lines

- This has to be combined with SWIOTLB-force-ish to always use the bounce buffer, otherwise you could still do DMA without using SWIOTLB (by not hitting the criteria for needing to use SWIOTLB)?

Since restricted DMA is for the devices that are not behind an IOMMU, I change the criteria
if (unlikely(swiotlb_force == SWIOTLB_FORCE))
to
if (unlikely(swiotlb_force == SWIOTLB_FORCE) || dev->dma_io_tlb_mem) in dma_direct_map_page().

Also, even if SWIOTLB=force, the restricted DMA pool is preferred if available (get_io_tlb_mem in https://lore.kernel.org/patchwork/patch/1360995/).

Thanks!

#36 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 41 lines · Claire Chang
Quoted 39 lines · Konrad Rzeszutek Wilk
Quoted 21 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

---
 .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +4 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
           used as a shared pool of DMA buffers for a set of devices. It can
           be used by an operating system to instantiate the necessary pool
           management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt compatible (optional) - standard definition · line 67

Quoted 10 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +10 -0
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.

Heya!

I think I am missing something obvious here so please bear with my questions:

Quoted 7 lines
Quoted 2 lines

- This code adds the means of having the SWIOTLB pool tied to a specific memory correct?

It doesn't affect the existing SWIOTLB. It just utilizes the existing SWIOTLB code to create another DMA pool tied to a specific memory region for a given set of devices. It bounces the streaming DMA (map/unmap) in and out of that region and does the memory allocation (dma_direct_alloc) from the same region.

Right, so why can't it follow the same mechanism that Xen SWIOTLB does - which had exactly the same problem (needed special handling on the pool) - and do a similar code?

Quoted 8 lines
Quoted 6 lines

- Nothing stops the physical device from bypassing the SWIOTLB buffer. That is if an errant device screwed up the length or DMA address, the SWIOTLB would gladly do what the device told it do?

So the system needs to provide a way to lock down the memory access, e.g. MPU.

OK! Would it be prudent to have this in the description above perhaps?

Quoted 13 lines
Quoted 5 lines

- This has to be combined with SWIOTLB-force-ish to always use the bounce buffer, otherwise you could still do DMA without using SWIOTLB (by not hitting the criteria for needing to use SWIOTLB)?

Since restricted DMA is for the devices that are not behind an IOMMU, I change the criteria
if (unlikely(swiotlb_force == SWIOTLB_FORCE))
to
if (unlikely(swiotlb_force == SWIOTLB_FORCE) || dev->dma_io_tlb_mem) in dma_direct_map_page().

Quoted 4 lines

Also, even if SWIOTLB=force, the restricted DMA pool is preferred if available (get_io_tlb_mem in https://lore.kernel.org/patchwork/patch/1360995/).

Thanks!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#37 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 9 lines · Konrad Rzeszutek Wilk
Quoted 7 lines
Quoted 5 lines

- Nothing stops the physical device from bypassing the SWIOTLB buffer. That is if an errant device screwed up the length or DMA address, the SWIOTLB would gladly do what the device told it do?

So the system needs to provide a way to lock down the memory access, e.g. MPU.

OK! Would it be prudent to have this in the description above perhaps?

Yes this is something that must be documented as a requirement for the restricted DMA pool users, otherwise attempting to do restricted DMA pool is no different than say, using a device private CMA region. Without the enforcement, this is just a best effort.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#38 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 8 lines · Florian Fainelli
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 6 lines
Quoted 5 lines

- Nothing stops the physical device from bypassing the SWIOTLB buffer. That is if an errant device screwed up the length or DMA address, the SWIOTLB would gladly do what the device told it do?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

So the system needs to provide a way to lock down the memory access, e.g. MPU.

OK! Would it be prudent to have this in the description above perhaps?

Yes this is something that must be documented as a requirement for the restricted DMA pool users, otherwise attempting to do restricted DMA pool is no different than say, using a device private CMA region. Without the enforcement, this is just a best effort.

Will add in the next version.

Quoted 2 lines

--
Florian

maintainer
#39 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

In reply to Claire Chang
Quoted 3 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

Quoted 43 lines
---
 .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +19 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
           used as a shared pool of DMA buffers for a set of devices. It can
           be used by an operating system to instantiate the necessary pool
           management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.
         - vendor specific string in the form <vendor>,[<device>-]<usage>
 no-map (optional) - empty property
     - Indicates the operating system must not create a virtual mapping
@@ -120,6 +134,11 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
 			compatible = "acme,multimedia-memory";
 			reg = <0x77000000 0x4000000>;
 		};
+
+		restricted_dma_mem_reserved: restricted_dma_mem_reserved {
+			compatible = "restricted-dma-pool";
+			reg = <0x50000000 0x400000>;
+		};
 	};
 
 	/* ... */

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt one for multimedia processing (named multimedia-memory@77000000, 64MiB). · line 162

Quoted 6 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +3 -0
@@ -138,4 +157,9 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
 		memory-region = <&multimedia_reserved>;
 		/* ... */
 	};
+
+	pcie_device: pcie_device@0,0 {
+		memory-region = <&restricted_dma_mem_reserved>;

PCI hosts often have inbound window configurations that limit the address range and translate PCI to bus addresses. Those windows happen to be configured by dma-ranges. In any case, wouldn't you want to put the configuration in the PCI host node? Is there a usecase of restricting one PCIe device and not another?

Rob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#40 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 9 lines · Rob Herring
Quoted 3 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

There would still need to be some way to tell SWIOTLB to pick up the corresponding chunk of memory and to prevent the kernel from using it for anything else, though.

Quoted 41 lines
Quoted 41 lines
---
  .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
  1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +19 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
            used as a shared pool of DMA buffers for a set of devices. It can
            be used by an operating system to instantiate the necessary pool
            management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.
          - vendor specific string in the form <vendor>,[<device>-]<usage>
  no-map (optional) - empty property
      - Indicates the operating system must not create a virtual mapping
@@ -120,6 +134,11 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
  			compatible = "acme,multimedia-memory";
  			reg = <0x77000000 0x4000000>;
  		};
+
+		restricted_dma_mem_reserved: restricted_dma_mem_reserved {
+			compatible = "restricted-dma-pool";
+			reg = <0x50000000 0x400000>;
+		};
  	};
 
Quoted 14 lines

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt one for multimedia processing (named multimedia-memory@77000000, 64MiB). · line 162

Quoted 8 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +3 -0
  	/* ... */
@@ -138,4 +157,9 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
  		memory-region = <&multimedia_reserved>;
  		/* ... */
  	};
+
+	pcie_device: pcie_device@0,0 {
+		memory-region = <&restricted_dma_mem_reserved>;

PCI hosts often have inbound window configurations that limit the address range and translate PCI to bus addresses. Those windows happen to be configured by dma-ranges. In any case, wouldn't you want to put the configuration in the PCI host node? Is there a usecase of restricting one PCIe device and not another?

The general design seems to accommodate devices having their own pools such that they can't even snoop on each others' transient DMA data. If the interconnect had a way of wiring up, say, PCI RIDs to AMBA NSAIDs, then in principle you could certainly apply that to PCI endpoints too (presumably you'd also disallow them from peer-to-peer transactions at the PCI level too).

Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#41 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 7 lines · Robin Murphy
Quoted 5 lines · Rob Herring
Quoted 3 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

Quoted 8 lines
Quoted 4 lines

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

There would still need to be some way to tell SWIOTLB to pick up the corresponding chunk of memory and to prevent the kernel from using it for anything else, though.

Don't we already have that problem if dma-ranges had a very small range? We just get lucky because the restriction is generally much more RAM than needed.

In any case, wouldn't finding all the dma-ranges do this? We're already walking the tree to find the max DMA address now.

Quoted 56 lines
Quoted 41 lines
Quoted 41 lines
---
  .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
  1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +19 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
            used as a shared pool of DMA buffers for a set of devices. It can
            be used by an operating system to instantiate the necessary pool
            management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.
          - vendor specific string in the form <vendor>,[<device>-]<usage>
  no-map (optional) - empty property
      - Indicates the operating system must not create a virtual mapping
@@ -120,6 +134,11 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
                     compatible = "acme,multimedia-memory";
                     reg = <0x77000000 0x4000000>;
             };
+
+            restricted_dma_mem_reserved: restricted_dma_mem_reserved {
+                    compatible = "restricted-dma-pool";
+                    reg = <0x50000000 0x400000>;
+            };
     };
 
Quoted 14 lines

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt one for multimedia processing (named multimedia-memory@77000000, 64MiB). · line 162

Quoted 8 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +3 -0
     /* ... */
@@ -138,4 +157,9 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
             memory-region = <&multimedia_reserved>;
             /* ... */
     };
+
+    pcie_device: pcie_device@0,0 {
+            memory-region = <&restricted_dma_mem_reserved>;

PCI hosts often have inbound window configurations that limit the address range and translate PCI to bus addresses. Those windows happen to be configured by dma-ranges. In any case, wouldn't you want to put the configuration in the PCI host node? Is there a usecase of restricting one PCIe device and not another?

Quoted 6 lines

The general design seems to accommodate devices having their own pools such that they can't even snoop on each others' transient DMA data. If the interconnect had a way of wiring up, say, PCI RIDs to AMBA NSAIDs, then in principle you could certainly apply that to PCI endpoints too (presumably you'd also disallow them from peer-to-peer transactions at the PCI level too).

At least for PCI, I think we can handle this. We have the BDF in the 3rd address cell in dma-ranges. The Openfirmware spec says those are 0 in the case of ranges. It doesn't talk about dma-ranges though. But I think we could extend it to allow for BDF. Though typically with PCIe every device is behind its own bridge and each bridge node can have a dma-ranges.

Rob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#42 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 13 lines · Rob Herring
Quoted 12 lines · Robin Murphy
Quoted 9 lines · Rob Herring
Quoted 3 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

Quoted 7 lines
Quoted 3 lines

There would still need to be some way to tell SWIOTLB to pick up the corresponding chunk of memory and to prevent the kernel from using it for anything else, though.

Don't we already have that problem if dma-ranges had a very small range? We just get lucky because the restriction is generally much more RAM than needed.

Not really - if a device has a naturally tiny addressing capability that doesn't even cover ZONE_DMA32 where the regular SWIOTLB buffer will be allocated then it's unlikely to work well, but that's just crap system design. Yes, memory pressure in ZONE_DMA{32} is particularly problematic for such limited devices, but it's irrelevant to the issue at hand here.

What we have here is a device that's not allowed to see *kernel* memory at all. It's been artificially constrained to a particular region by a TZASC or similar, and the only data which should ever be placed in that region is data intended for that device to see. That way if it tries to go rogue it physically can't start slurping data intended for other devices or not mapped for DMA at all. The bouncing is an important part of this - I forget the title off-hand but there was an interesting paper a few years ago which demonstrated that even with an IOMMU, streaming DMA of in-place buffers could reveal enough adjacent data from the same page to mount an attack on the system. Memory pressure should be immaterial since the size of each bounce pool carveout will presumably be tuned for the needs of the given device.

Quoted 2 lines

In any case, wouldn't finding all the dma-ranges do this? We're already walking the tree to find the max DMA address now.

If all you can see are two "dma-ranges" properties, how do you propose to tell that one means "this is the extent of what I can address, please set my masks and dma-range-map accordingly and try to allocate things where I can reach them" while the other means "take this output range away from the page allocator and hook it up as my dedicated bounce pool, because it is Serious Security Time"? Especially since getting that choice wrong either way would be a Bad Thing.

Robin.

Quoted 63 lines
Quoted 56 lines
Quoted 41 lines
Quoted 41 lines
---
   .../reserved-memory/reserved-memory.txt       | 24 +++++++++++++++++++
   1 file changed, 24 insertions(+)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +19 -0
index e8d3096d922c..44975e2a1fd2 100644
@@ -51,6 +51,20 @@ compatible (optional) - standard definition
             used as a shared pool of DMA buffers for a set of devices. It can
             be used by an operating system to instantiate the necessary pool
             management subsystem if necessary.
+        - restricted-dma-pool: This indicates a region of memory meant to be
+          used as a pool of restricted DMA buffers for a set of devices. The
+          memory region would be the only region accessible to those devices.
+          When using this, the no-map and reusable properties must not be set,
+          so the operating system can create a virtual mapping that will be used
+          for synchronization. The main purpose for restricted DMA is to
+          mitigate the lack of DMA access control on systems without an IOMMU,
+          which could result in the DMA accessing the system memory at
+          unexpected times and/or unexpected addresses, possibly leading to data
+          leakage or corruption. The feature on its own provides a basic level
+          of protection against the DMA overwriting buffer contents at
+          unexpected times. However, to protect against general data leakage and
+          system memory corruption, the system needs to provide way to restrict
+          the DMA to a predefined memory region.
           - vendor specific string in the form <vendor>,[<device>-]<usage>
   no-map (optional) - empty property
       - Indicates the operating system must not create a virtual mapping
@@ -120,6 +134,11 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
                      compatible = "acme,multimedia-memory";
                      reg = <0x77000000 0x4000000>;
              };
+
+            restricted_dma_mem_reserved: restricted_dma_mem_reserved {
+                    compatible = "restricted-dma-pool";
+                    reg = <0x50000000 0x400000>;
+            };
      };
 
Quoted 14 lines

Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt one for multimedia processing (named multimedia-memory@77000000, 64MiB). · line 162

Quoted 8 lines
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +3 -0
      /* ... */
@@ -138,4 +157,9 @@ one for multimedia processing (named multimedia-memory@77000000, 64MiB).
              memory-region = <&multimedia_reserved>;
              /* ... */
      };
+
+    pcie_device: pcie_device@0,0 {
+            memory-region = <&restricted_dma_mem_reserved>;

PCI hosts often have inbound window configurations that limit the address range and translate PCI to bus addresses. Those windows happen to be configured by dma-ranges. In any case, wouldn't you want to put the configuration in the PCI host node? Is there a usecase of restricting one PCIe device and not another?

Quoted 6 lines

The general design seems to accommodate devices having their own pools such that they can't even snoop on each others' transient DMA data. If the interconnect had a way of wiring up, say, PCI RIDs to AMBA NSAIDs, then in principle you could certainly apply that to PCI endpoints too (presumably you'd also disallow them from peer-to-peer transactions at the PCI level too).

Quoted 9 lines

At least for PCI, I think we can handle this. We have the BDF in the 3rd address cell in dma-ranges. The Openfirmware spec says those are 0 in the case of ranges. It doesn't talk about dma-ranges though. But I think we could extend it to allow for BDF. Though typically with PCIe every device is behind its own bridge and each bridge node can have a dma-ranges.

Rob

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#43 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 23 lines · Robin Murphy
Quoted 13 lines · Rob Herring
Quoted 12 lines · Robin Murphy
Quoted 9 lines · Rob Herring
Quoted 3 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

Quoted 7 lines
Quoted 3 lines

There would still need to be some way to tell SWIOTLB to pick up the corresponding chunk of memory and to prevent the kernel from using it for anything else, though.

Don't we already have that problem if dma-ranges had a very small range? We just get lucky because the restriction is generally much more RAM than needed.

Quoted 5 lines

Not really - if a device has a naturally tiny addressing capability that doesn't even cover ZONE_DMA32 where the regular SWIOTLB buffer will be allocated then it's unlikely to work well, but that's just crap system design. Yes, memory pressure in ZONE_DMA{32} is particularly problematic for such limited devices, but it's irrelevant to the issue at hand here.

Yesterday's crap system design is today's security feature. Couldn't this feature make crap system design work better?

Quoted 3 lines

What we have here is a device that's not allowed to see *kernel* memory at all. It's been artificially constrained to a particular region by a TZASC or similar, and the only data which should ever be placed in that

May have been constrained, but that's entirely optional.

In the optional case where the setup is entirely up to the OS, I don't think this belongs in the DT at all. Perhaps that should be solved first.

Quoted 13 lines

region is data intended for that device to see. That way if it tries to go rogue it physically can't start slurping data intended for other devices or not mapped for DMA at all. The bouncing is an important part of this - I forget the title off-hand but there was an interesting paper a few years ago which demonstrated that even with an IOMMU, streaming DMA of in-place buffers could reveal enough adjacent data from the same page to mount an attack on the system. Memory pressure should be immaterial since the size of each bounce pool carveout will presumably be tuned for the needs of the given device.

Quoted 2 lines

In any case, wouldn't finding all the dma-ranges do this? We're already walking the tree to find the max DMA address now.

Quoted 7 lines

If all you can see are two "dma-ranges" properties, how do you propose to tell that one means "this is the extent of what I can address, please set my masks and dma-range-map accordingly and try to allocate things where I can reach them" while the other means "take this output range away from the page allocator and hook it up as my dedicated bounce pool, because it is Serious Security Time"? Especially since getting that choice wrong either way would be a Bad Thing.

Either we have some heuristic based on the size or we add some hint. The point is let's build on what we already have for defining DMA accessible memory in DT rather than some parallel mechanism.

Rob
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#44 lore raw

Re: [RFC PATCH v3 5/6] dt-bindings: of: Add restricted DMA pool

Quoted 29 lines · Rob Herring
Quoted 27 lines · Robin Murphy
Quoted 24 lines · Rob Herring
Quoted 15 lines · Robin Murphy
Quoted 7 lines · Rob Herring
Quoted 4 lines · Claire Chang

Introduce the new compatible string, restricted-dma-pool, for restricted DMA. One can specify the address and length of the restricted DMA memory region by restricted-dma-pool in the device tree.

Quoted 5 lines

If this goes into DT, I think we should be able to use dma-ranges for this purpose instead. Normally, 'dma-ranges' is for physical bus restrictions, but there's no reason it can't be used for policy or to express restrictions the firmware has enabled.

Quoted 3 lines

There would still need to be some way to tell SWIOTLB to pick up the corresponding chunk of memory and to prevent the kernel from using it for anything else, though.

Don't we already have that problem if dma-ranges had a very small range? We just get lucky because the restriction is generally much more RAM than needed.

Quoted 8 lines
Quoted 6 lines

Not really - if a device has a naturally tiny addressing capability that doesn't even cover ZONE_DMA32 where the regular SWIOTLB buffer will be allocated then it's unlikely to work well, but that's just crap system design. Yes, memory pressure in ZONE_DMA{32} is particularly problematic for such limited devices, but it's irrelevant to the issue at hand here.

Yesterday's crap system design is today's security feature. Couldn't this feature make crap system design work better?

Indeed! Say you bring out your shiny new "Strawberry Flan 4" machine
with all the latest connectivity, but tragically its PCIe can only
address 25% of the RAM. So you decide to support deploying it in two
configurations: one where it runs normally for best performance, and
another "secure" one where it dedicates that quarter of RAM as a restricted DMA pool for any PCIe devices - that way, even if that hotel projector you plug in turns out to be a rogue Thunderbolt endpoint, it can never snarf your private keys off your eMMC out of the page cache.

(Yes, is is the thinnest of strawmen, but it sets the scene for the point you raised...)

...which is that in both cases the dma-ranges will still be identical. So how is the kernel going to know whether to steal that whole area from memblock before anything else can allocate from it, or not?

I don't disagree that even in Claire's original intended case it would be semantically correct to describe the hardware-firewalled region with dma-ranges. It just turns out not to be necessary, and you're already arguing for not adding anything in DT that doesn't need to be.

Quoted 10 lines
Quoted 4 lines

What we have here is a device that's not allowed to see *kernel* memory at all. It's been artificially constrained to a particular region by a TZASC or similar, and the only data which should ever be placed in that

May have been constrained, but that's entirely optional.

In the optional case where the setup is entirely up to the OS, I don't think this belongs in the DT at all. Perhaps that should be solved first.

Yes! Let's definitely consider that case! Say you don't have any security or physical limitations but want to use a bounce pool for some device anyway because reasons (perhaps copying streaming DMA data to a better guaranteed alignment gives an overall performance win). Now the *only* relevant thing to communicate to the kernel is to, ahem, reserve a large chunk of memory, and use it for this special purpose. Isn't that literally what reserved-memory bindings are for?

Quoted 22 lines
Quoted 13 lines

region is data intended for that device to see. That way if it tries to go rogue it physically can't start slurping data intended for other devices or not mapped for DMA at all. The bouncing is an important part of this - I forget the title off-hand but there was an interesting paper a few years ago which demonstrated that even with an IOMMU, streaming DMA of in-place buffers could reveal enough adjacent data from the same page to mount an attack on the system. Memory pressure should be immaterial since the size of each bounce pool carveout will presumably be tuned for the needs of the given device.

Quoted 1 line

In any case, wouldn't finding all the dma-ranges do this? We're already walking the tree to find the max DMA address now.

Quoted 8 lines

If all you can see are two "dma-ranges" properties, how do you propose to tell that one means "this is the extent of what I can address, please set my masks and dma-range-map accordingly and try to allocate things where I can reach them" while the other means "take this output range away from the page allocator and hook it up as my dedicated bounce pool, because it is Serious Security Time"? Especially since getting that choice wrong either way would be a Bad Thing.

Quoted 1 line

Either we have some heuristic based on the size or we add some hint. The point is let's build on what we already have for defining DMA accessible memory in DT rather than some parallel mechanism.

The point I'm trying to bang home is that it's really not about the DMA accessibility, it's about the purpose of the memory itself. Even when DMA accessibility *is* relevant it's already implied by that purpose, from the point of view of the implementation. The only difference it might make is to the end user if they want to ascertain whether the presence of such a pool represents protection against an untrusted device or just some DMA optimisation tweak.

Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#45 lore raw

RFC PATCH v3 6/6 of: Add plumbing for restricted DMA pool

In reply to Claire Chang

If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented.

---
 drivers/of/address.c    | 21 +++++++++++++++++++++
 drivers/of/device.c     |  4 ++++
 drivers/of/of_private.h |  5 +++++
 3 files changed, 30 insertions(+)
drivers/of/address.c +21 -0
index 73ddf2540f3f..94eca8249854 100644
@@ -8,6 +8,7 @@
 #include <linux/logic_pio.h>
 #include <linux/module.h>
 #include <linux/of_address.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/pci.h>
 #include <linux/pci_regs.h>
 #include <linux/sizes.h>
@@ -1094,3 +1095,23 @@ bool of_dma_is_coherent(struct device_node *np)
 	return false;
 }
 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
+
+int of_dma_set_restricted_buffer(struct device *dev)
+{
+	struct device_node *node;
+	int count, i;
+
+	if (!dev->of_node)
+		return 0;
+
+	count = of_property_count_elems_of_size(dev->of_node, "memory-region",
+						sizeof(phandle));
+	for (i = 0; i < count; i++) {
+		node = of_parse_phandle(dev->of_node, "memory-region", i);
+		if (of_device_is_compatible(node, "restricted-dma-pool"))
+			return of_reserved_mem_device_init_by_idx(
+				dev, dev->of_node, i);
+	}
+
+	return 0;
+}
drivers/of/device.c +4 -0
index aedfaaafd3e7..e2c7409956ab 100644
@@ -182,6 +182,10 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
 	arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
 
 	dev->dma_range_map = map;
+
+	if (!iommu)
+		return of_dma_set_restricted_buffer(dev);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(of_dma_configure_id);
drivers/of/of_private.h +5 -0
index d9e6a324de0a..28a2dfa197ba 100644
@@ -161,12 +161,17 @@ struct bus_dma_region;
 #if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_HAS_DMA)
 int of_dma_get_range(struct device_node *np,
 		const struct bus_dma_region **map);
+int of_dma_set_restricted_buffer(struct device *dev);
 #else
 static inline int of_dma_get_range(struct device_node *np,
 		const struct bus_dma_region **map)
 {
 	return -ENODEV;
 }
+static inline int of_dma_get_restricted_buffer(struct device *dev)
+{
+	return -ENODEV;
+}
 #endif
 
 #endif /* _LINUX_OF_PRIVATE_H */
Signature · 6 lines
2.29.2.729.g45daf8777d-goog

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#46 lore raw

Re: [RFC PATCH v3 6/6] of: Add plumbing for restricted DMA pool

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Claire,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on powerpc/next]
[also build test WARNING on linus/master v5.11-rc2 next-20210104] [cannot apply to swiotlb/linux-next robh/for-next xen-tip/linux-next iommu/next] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Claire-Chang/Restricted-DMA/20210106-114544
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: sh-allmodconfig (attached as .config)
compiler: sh4-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/1c220b845aea057b79a1a0a33ebf1204866fb2a1
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Claire-Chang/Restricted-DMA/20210106-114544
git checkout 1c220b845aea057b79a1a0a33ebf1204866fb2a1
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> drivers/of/address.c:1099:5: warning: no previous prototype for 'of_dma_set_restricted_buffer' [-Wmissing-prototypes]

1099 | int of_dma_set_restricted_buffer(struct device *dev)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for SND_ATMEL_SOC_PDC
Depends on SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && HAS_DMA Selected by
- SND_ATMEL_SOC_SSC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC
- SND_ATMEL_SOC_SSC_PDC && SOUND && !UML && SND && SND_SOC && SND_ATMEL_SOC && ATMEL_SSC

vim +/of_dma_set_restricted_buffer +1099 drivers/of/address.c

1098

> 1099	int of_dma_set_restricted_buffer(struct device *dev)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#47 lore raw

Re: [RFC PATCH v3 6/6] of: Add plumbing for restricted DMA pool

In reply to Claire Chang
Quoted 5 lines · Claire Chang

If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented.

---

[snip]

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1108

Quoted 10 lines
drivers/of/address.c +10 -0
+int of_dma_set_restricted_buffer(struct device *dev)
+{
+	struct device_node *node;
+	int count, i;
+
+	if (!dev->of_node)
+		return 0;
+
+	count = of_property_count_elems_of_size(dev->of_node, "memory-region",
+						sizeof(phandle));

You could have an early check for count < 0, along with an error message, if that is deemed useful.

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1111

Quoted 3 lines
drivers/of/address.c +3 -0
+	for (i = 0; i < count; i++) {
+		node = of_parse_phandle(dev->of_node, "memory-region", i);
+		if (of_device_is_compatible(node, "restricted-dma-pool"))

And you may want to add here an of_device_is_available(node). A platform that provides the Device Tree firmware and try to support multiple different SoCs may try to determine if an IOMMU is present, and if it is, it could be marking the restriced-dma-pool region with a 'status = "disabled"' property, or any variant of that scheme.

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1113

Quoted 2 lines
drivers/of/address.c +2 -0
+			return of_reserved_mem_device_init_by_idx(
+				dev, dev->of_node, i);

This does not seem to be supporting more than one memory region, did not you want something like instead:

		ret = of_reserved_mem_device_init_by_idx(...);
		if (ret)
			return ret;
Quoted 42 lines
drivers/of/address.c +4 -0
+	}
+
+	return 0;
+}
drivers/of/device.c +4 -0
index aedfaaafd3e7..e2c7409956ab 100644
@@ -182,6 +182,10 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
 	arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
 
 	dev->dma_range_map = map;
+
+	if (!iommu)
+		return of_dma_set_restricted_buffer(dev);
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(of_dma_configure_id);
drivers/of/of_private.h +5 -0
index d9e6a324de0a..28a2dfa197ba 100644
@@ -161,12 +161,17 @@ struct bus_dma_region;
 #if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_HAS_DMA)
 int of_dma_get_range(struct device_node *np,
 		const struct bus_dma_region **map);
+int of_dma_set_restricted_buffer(struct device *dev);
 #else
 static inline int of_dma_get_range(struct device_node *np,
 		const struct bus_dma_region **map)
 {
 	return -ENODEV;
 }
+static inline int of_dma_get_restricted_buffer(struct device *dev)
+{
+	return -ENODEV;
+}
 #endif
 
 #endif /* _LINUX_OF_PRIVATE_H */
Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#48 lore raw

Re: [RFC PATCH v3 6/6] of: Add plumbing for restricted DMA pool

Quoted 28 lines · Florian Fainelli
Quoted 5 lines · Claire Chang

If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented.

---

[snip]

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1108

Quoted 10 lines
drivers/of/address.c +10 -0
+int of_dma_set_restricted_buffer(struct device *dev)
+{
+     struct device_node *node;
+     int count, i;
+
+     if (!dev->of_node)
+             return 0;
+
+     count = of_property_count_elems_of_size(dev->of_node, "memory-region",
+                                             sizeof(phandle));

You could have an early check for count < 0, along with an error message, if that is deemed useful.

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1111

Quoted 3 lines
drivers/of/address.c +3 -0
+     for (i = 0; i < count; i++) {
+             node = of_parse_phandle(dev->of_node, "memory-region", i);
+             if (of_device_is_compatible(node, "restricted-dma-pool"))
Quoted 5 lines

And you may want to add here an of_device_is_available(node). A platform that provides the Device Tree firmware and try to support multiple different SoCs may try to determine if an IOMMU is present, and if it is, it could be marking the restriced-dma-pool region with a 'status = "disabled"' property, or any variant of that scheme.

This function is called only when there is no IOMMU present (check in drivers/of/device.c). I can still add of_device_is_available(node) here if you think it's helpful.

Quoted 11 lines

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1113

Quoted 3 lines
drivers/of/address.c +2 -0
+                     return of_reserved_mem_device_init_by_idx(
+                             dev, dev->of_node, i);

This does not seem to be supporting more than one memory region, did not you want something like instead:

                ret = of_reserved_mem_device_init_by_idx(...);
                if (ret)
                        return ret;

Yes. This implement only supports one restriced-dma-pool memory region with the assumption that there is only one memory region with the compatible string, restricted-dma-pool, in the dts. IIUC, it's similar to shared-dma-pool.

Quoted 40 lines
Quoted 40 lines
drivers/of/address.c +4 -0
+     }
+
+     return 0;
+}
drivers/of/device.c +4 -0
index aedfaaafd3e7..e2c7409956ab 100644
@@ -182,6 +182,10 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
      arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
 
      dev->dma_range_map = map;
+
+     if (!iommu)
+             return of_dma_set_restricted_buffer(dev);
+
      return 0;
 }
 EXPORT_SYMBOL_GPL(of_dma_configure_id);
drivers/of/of_private.h +5 -0
index d9e6a324de0a..28a2dfa197ba 100644
@@ -161,12 +161,17 @@ struct bus_dma_region;
 #if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_HAS_DMA)
 int of_dma_get_range(struct device_node *np,
              const struct bus_dma_region **map);
+int of_dma_set_restricted_buffer(struct device *dev);
 #else
 static inline int of_dma_get_range(struct device_node *np,
              const struct bus_dma_region **map)
 {
      return -ENODEV;
 }
+static inline int of_dma_get_restricted_buffer(struct device *dev)
+{
+     return -ENODEV;
+}
 #endif
 
Quoted 6 lines

drivers/of/of_private.h struct bus_dma_region; · line 177

Quoted 2 lines
drivers/of/of_private.h +0 -0
 #endif /* _LINUX_OF_PRIVATE_H */

--
Florian

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#49 lore raw

Re: [RFC PATCH v3 6/6] of: Add plumbing for restricted DMA pool

Quoted 35 lines · Claire Chang
Quoted 28 lines · Florian Fainelli
Quoted 5 lines · Claire Chang

If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented.

---

[snip]

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1108

Quoted 10 lines
drivers/of/address.c +10 -0
+int of_dma_set_restricted_buffer(struct device *dev)
+{
+     struct device_node *node;
+     int count, i;
+
+     if (!dev->of_node)
+             return 0;
+
+     count = of_property_count_elems_of_size(dev->of_node, "memory-region",
+                                             sizeof(phandle));

You could have an early check for count < 0, along with an error message, if that is deemed useful.

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1111

Quoted 3 lines
drivers/of/address.c +3 -0
+     for (i = 0; i < count; i++) {
+             node = of_parse_phandle(dev->of_node, "memory-region", i);
+             if (of_device_is_compatible(node, "restricted-dma-pool"))
Quoted 5 lines

And you may want to add here an of_device_is_available(node). A platform that provides the Device Tree firmware and try to support multiple different SoCs may try to determine if an IOMMU is present, and if it is, it could be marking the restriced-dma-pool region with a 'status = "disabled"' property, or any variant of that scheme.

Quoted 3 lines

This function is called only when there is no IOMMU present (check in drivers/of/device.c). I can still add of_device_is_available(node) here if you think it's helpful.

I believe it is, since boot loader can have a shared Device Tree blob skeleton and do various adaptations based on the chip (that's what we do) and adding a status property is much simpler than insertion new nodes are run time.

Quoted 13 lines
Quoted 5 lines

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1113

Quoted 4 lines
drivers/of/address.c +2 -0
+                     return of_reserved_mem_device_init_by_idx(
+                             dev, dev->of_node, i);
Quoted 7 lines

This does not seem to be supporting more than one memory region, did not you want something like instead:

                ret = of_reserved_mem_device_init_by_idx(...);
                if (ret)
                        return ret;
Quoted 4 lines

Yes. This implement only supports one restriced-dma-pool memory region with the assumption that there is only one memory region with the compatible string, restricted-dma-pool, in the dts. IIUC, it's similar to shared-dma-pool.

Then if here is such a known limitation it should be both documented and enforced here, you shouldn ot be iterating over all of the phandles that you find, stop at the first one and issue a warning if count > 1?

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#50 lore raw

Re: [RFC PATCH v3 6/6] of: Add plumbing for restricted DMA pool

Quoted 59 lines · Florian Fainelli
Quoted 35 lines · Claire Chang
Quoted 28 lines · Florian Fainelli
Quoted 5 lines · Claire Chang

If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented.

---

[snip]

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1108

Quoted 10 lines
drivers/of/address.c +10 -0
+int of_dma_set_restricted_buffer(struct device *dev)
+{
+     struct device_node *node;
+     int count, i;
+
+     if (!dev->of_node)
+             return 0;
+
+     count = of_property_count_elems_of_size(dev->of_node, "memory-region",
+                                             sizeof(phandle));

You could have an early check for count < 0, along with an error message, if that is deemed useful.

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1111

Quoted 3 lines
drivers/of/address.c +3 -0
+     for (i = 0; i < count; i++) {
+             node = of_parse_phandle(dev->of_node, "memory-region", i);
+             if (of_device_is_compatible(node, "restricted-dma-pool"))
Quoted 5 lines

And you may want to add here an of_device_is_available(node). A platform that provides the Device Tree firmware and try to support multiple different SoCs may try to determine if an IOMMU is present, and if it is, it could be marking the restriced-dma-pool region with a 'status = "disabled"' property, or any variant of that scheme.

Quoted 3 lines

This function is called only when there is no IOMMU present (check in drivers/of/device.c). I can still add of_device_is_available(node) here if you think it's helpful.

I believe it is, since boot loader can have a shared Device Tree blob skeleton and do various adaptations based on the chip (that's what we do) and adding a status property is much simpler than insertion new nodes are run time.

Quoted 13 lines
Quoted 12 lines

drivers/of/address.c bool of_dma_is_coherent(struct device_node *np) · line 1113

Quoted 4 lines
drivers/of/address.c +2 -0
+                     return of_reserved_mem_device_init_by_idx(
+                             dev, dev->of_node, i);

This does not seem to be supporting more than one memory region, did not you want something like instead:

                ret = of_reserved_mem_device_init_by_idx(...);
                if (ret)
                        return ret;
Quoted 8 lines
Quoted 4 lines

Yes. This implement only supports one restriced-dma-pool memory region with the assumption that there is only one memory region with the compatible string, restricted-dma-pool, in the dts. IIUC, it's similar to shared-dma-pool.

Then if here is such a known limitation it should be both documented and enforced here, you shouldn ot be iterating over all of the phandles that you find, stop at the first one and issue a warning if count > 1?

What I have in mind is there might be multiple memory regions, but only one is for restriced-dma-pool.
Say, if you want a separated region for coherent DMA and only do streaming DMA in this restriced-dma-pool region, you can add another reserved-memory node with shared-dma-pool in dts and the current implementation will try to allocate the memory via dma_alloc_from_dev_coherent() first (see dma_alloc_attrs() in /kernel/dma/mapping.c).
Or if you have vendor specific memory region, you can still set up restriced-dma-pool by adding another reserved-memory node in dts. Dose this make sense to you? I'll document this for sure.

Quoted 2 lines

--
Florian

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#51 lore raw
In reply to Claire Chang

Hi,

First of all let me say that I am glad that someone is working on a upstream solution for this issue, would appreciate if you could CC and Jim Quinlan on subsequent submissions.

Quoted 11 lines · Claire Chang

This series implements mitigations for lack of DMA access control on systems without an IOMMU, which could result in the DMA accessing the system memory at unexpected times and/or unexpected addresses, possibly leading to data leakage or corruption.

For example, we plan to use the PCI-e bus for Wi-Fi and that PCI-e bus is not behind an IOMMU. As PCI-e, by design, gives the device full access to system memory, a vulnerability in the Wi-Fi firmware could easily escalate to a full system exploit (remote wifi exploits: [1a], [1b] that shows a full chain of exploits; [2], [3]).

Quoted 8 lines

To mitigate the security concerns, we introduce restricted DMA. Restricted DMA utilizes the existing swiotlb to bounce streaming DMA in and out of a specially allocated region and does memory allocation from the same region. The feature on its own provides a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to restrict the DMA to a predefined memory region (this is usually done at firmware level, e.g. in ATF on some ARM platforms).

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Thanks!

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#52 lore raw
Quoted 6 lines · Florian Fainelli

Hi,

First of all let me say that I am glad that someone is working on a upstream solution for this issue, would appreciate if you could CC and Jim Quinlan on subsequent submissions.

Sure!

Quoted 9 lines · Claire Chang
Quoted 6 lines

This series implements mitigations for lack of DMA access control on systems without an IOMMU, which could result in the DMA accessing the system memory at unexpected times and/or unexpected addresses, possibly leading to data leakage or corruption.

For example, we plan to use the PCI-e bus for Wi-Fi and that PCI-e bus

is

Quoted 1 line
Quoted 1 line

not behind an IOMMU. As PCI-e, by design, gives the device full access

to

Quoted 1 line
Quoted 1 line

system memory, a vulnerability in the Wi-Fi firmware could easily

escalate

Quoted 4 lines
Quoted 4 lines

to a full system exploit (remote wifi exploits: [1a], [1b] that shows a full chain of exploits; [2], [3]).

To mitigate the security concerns, we introduce restricted DMA.

Restricted

Quoted 1 line
Quoted 1 line

DMA utilizes the existing swiotlb to bounce streaming DMA in and out of

a

Quoted 1 line
Quoted 1 line

specially allocated region and does memory allocation from the same

region.

Quoted 1 line
Quoted 1 line

The feature on its own provides a basic level of protection against the

DMA

Quoted 2 lines
Quoted 2 lines

overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system

needs

Quoted 1 line
Quoted 1 line

to provide a way to restrict the DMA to a predefined memory region

(this is

Quoted 7 lines
Quoted 1 line

usually done at firmware level, e.g. in ATF on some ARM platforms).

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory
region is for PCIe DMA only, but we still need MPU to locks down PCIe access to
that specific regions.

Quoted 9 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

Quoted 11 lines

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Only the streaming DMA (map/unmap) needs bounce buffering.
I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will
try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

The CPU usage doesn't increase too much either.
Although I didn't measure the CPU usage very precisely, it's ~3% with a single
big core (Cortex-A72) and ~5% with a single small core (Cortex-A53).

Thanks!

Quoted 2 lines

Thanks!

Quoted 63 lines · Florian Fainelli

Hi,

First of all let me say that I am glad that someone is working on a upstream solution for this issue, would appreciate if you could CC and Jim Quinlan on subsequent submissions.

Quoted 8 lines · Claire Chang

This series implements mitigations for lack of DMA access control on systems without an IOMMU, which could result in the DMA accessing the system memory at unexpected times and/or unexpected addresses, possibly leading to data leakage or corruption.

For example, we plan to use the PCI-e bus for Wi-Fi and that PCI-e bus is not behind an IOMMU. As PCI-e, by design, gives the device full access to system memory, a vulnerability in the Wi-Fi firmware could easily

escalate

Quoted 4 lines

to a full system exploit (remote wifi exploits: [1a], [1b] that shows a full chain of exploits; [2], [3]).

To mitigate the security concerns, we introduce restricted DMA.

Restricted

Quoted 2 lines

DMA utilizes the existing swiotlb to bounce streaming DMA in and out of a specially allocated region and does memory allocation from the same

region.

Quoted 1 line

The feature on its own provides a basic level of protection against the

DMA

Quoted 2 lines

overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system

needs

Quoted 1 line

to provide a way to restrict the DMA to a predefined memory region (this

is

Quoted 1 line

usually done at firmware level, e.g. in ATF on some ARM platforms).

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Thanks!
--
Florian

#53 lore raw
In reply to Florian Fainelli
Quoted 6 lines · Florian Fainelli

Hi,

First of all let me say that I am glad that someone is working on a upstream solution for this issue, would appreciate if you could CC and Jim Quinlan on subsequent submissions.

Sure!

Quoted 22 lines · Claire Chang
Quoted 11 lines

This series implements mitigations for lack of DMA access control on systems without an IOMMU, which could result in the DMA accessing the system memory at unexpected times and/or unexpected addresses, possibly leading to data leakage or corruption.

For example, we plan to use the PCI-e bus for Wi-Fi and that PCI-e bus is not behind an IOMMU. As PCI-e, by design, gives the device full access to system memory, a vulnerability in the Wi-Fi firmware could easily escalate to a full system exploit (remote wifi exploits: [1a], [1b] that shows a full chain of exploits; [2], [3]).

Quoted 8 lines

To mitigate the security concerns, we introduce restricted DMA. Restricted DMA utilizes the existing swiotlb to bounce streaming DMA in and out of a specially allocated region and does memory allocation from the same region. The feature on its own provides a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to restrict the DMA to a predefined memory region (this is usually done at firmware level, e.g. in ATF on some ARM platforms).

Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 9 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

Quoted 11 lines

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Only the streaming DMA (map/unmap) needs bounce buffering.
I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

The CPU usage doesn't increase too much either.
Although I didn't measure the CPU usage very precisely, it's ~3% with a single big core (Cortex-A72) and ~5% with a single small core (Cortex-A53).

Thanks!

Quoted 4 lines

Thanks!
--
Florian

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#54 lore raw
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 10 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

Quoted 11 lines

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 9 lines

I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

Quoted 7 lines

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

How come you get better throughput with restricted DMA? Is it because doing DMA to/from a contiguous region allows for better grouping of transactions from the DRAM controller's perspective somehow?

Quoted 11 lines

The CPU usage doesn't increase too much either.
Although I didn't measure the CPU usage very precisely, it's ~3% with a single big core (Cortex-A72) and ~5% with a single small core (Cortex-A53).

Thanks!

Quoted 4 lines

Thanks!
--
Florian

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#55 lore raw
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Quoted 13 lines
Quoted 13 lines

I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99
Quoted 7 lines
Quoted 3 lines

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

How come you get better throughput with restricted DMA? Is it because doing DMA to/from a contiguous region allows for better grouping of transactions from the DRAM controller's perspective somehow?

I'm not sure, but actually, enabling the default swiotlb for wifi also helps the throughput a little bit for me.

Quoted 16 lines
Quoted 12 lines

The CPU usage doesn't increase too much either.
Although I didn't measure the CPU usage very precisely, it's ~3% with a single big core (Cortex-A72) and ~5% with a single small core (Cortex-A53).

Thanks!

Quoted 4 lines

Thanks!
--
Florian

--
Florian

#56 lore raw
Quoted 57 lines · Claire Chang
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 3 lines

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Sure, however the goals of using bounce buffering equally applies to RX and TX in that this is the only layer sitting between a stack (block, networking, USB, etc.) and the underlying device driver that scales well in order to massage a dma_addr_t to be within a particular physical range.

There is however room for improvement if the drivers are willing to change their buffer allocation strategy. When you receive Wi-Fi frames you need to allocate buffers for the Wi-Fi device to DMA into, and that happens ahead of the DMA transfers by the Wi-Fi device. At buffer allocation time you could very well allocate these frames from the restricted DMA region without having to bounce buffer them since the host CPU is in control over where and when to DMA into.

The issue is that each network driver may implement its own buffer allocation strategy, some may simply call netdev_alloc_skb() which gives zero control over where the buffer comes from unless you play tricks with NUMA node allocations and somehow declare that your restricted DMA region is a different NUMA node. If the driver allocates pages and then attaches a SKB to that page using build_skb(), then you have much more control over where that page comes from, and this is where using a device private CMA are helps, because you can just do dma_alloc_from_contiguous() and that will ensure that the pages are coming from your specific CMA area.

Few questions on the implementation:

- is there any warning or error being printed if the restricted DMA region is outside of a device's DMA addressable range?

- are there are any helpful statistics that could be shown to indicate that the restricted DMA region was sized too small, e.g.: that allocation of a DMA buffer failed because we ran out of space in the swiotlb pool?

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines

I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

Quoted 6 lines
Quoted 3 lines

How come you get better throughput with restricted DMA? Is it because doing DMA to/from a contiguous region allows for better grouping of transactions from the DRAM controller's perspective somehow?

I'm not sure, but actually, enabling the default swiotlb for wifi also helps the throughput a little bit for me.

OK, it would be interesting if you could get to the bottom of why performance does increase with swiotlb.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#57 lore raw

Hi Florian,

Quoted 68 lines · Florian Fainelli
Quoted 57 lines · Claire Chang
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 3 lines

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Sure, however the goals of using bounce buffering equally applies to RX and TX in that this is the only layer sitting between a stack (block, networking, USB, etc.) and the underlying device driver that scales well in order to massage a dma_addr_t to be within a particular physical range.

Quoted 8 lines

There is however room for improvement if the drivers are willing to change their buffer allocation strategy. When you receive Wi-Fi frames you need to allocate buffers for the Wi-Fi device to DMA into, and that happens ahead of the DMA transfers by the Wi-Fi device. At buffer allocation time you could very well allocate these frames from the restricted DMA region without having to bounce buffer them since the host CPU is in control over where and when to DMA into.

That is, however, still a trade-off between saving that one copy and protection from the DMA tampering with the packet contents when the kernel is reading them. Notice how the copy effectively makes a snapshot of the contents, guaranteeing that the kernel has a consistent view of the packet, which is not true if the DMA could modify the buffer contents in the middle of CPU accesses.

Best regards,
Tomasz

Quoted 43 lines

The issue is that each network driver may implement its own buffer allocation strategy, some may simply call netdev_alloc_skb() which gives zero control over where the buffer comes from unless you play tricks with NUMA node allocations and somehow declare that your restricted DMA region is a different NUMA node. If the driver allocates pages and then attaches a SKB to that page using build_skb(), then you have much more control over where that page comes from, and this is where using a device private CMA are helps, because you can just do dma_alloc_from_contiguous() and that will ensure that the pages are coming from your specific CMA area.

Few questions on the implementation:

- is there any warning or error being printed if the restricted DMA region is outside of a device's DMA addressable range?

- are there are any helpful statistics that could be shown to indicate that the restricted DMA region was sized too small, e.g.: that allocation of a DMA buffer failed because we ran out of space in the swiotlb pool?

Quoted 22 lines
Quoted 21 lines
Quoted 10 lines

I also added alloc/free support in this series
(https://lore.kernel.org/patchwork/patch/1360995/), so dma_direct_alloc() will try to allocate memory from the predefined memory region.

As for the performance hit, it should be similar to the default swiotlb. Here are my experiment results. Both SoCs lack IOMMU for PCIe.

PCIe wifi vht80 throughput -

Quoted 7 lines

MTK SoC tcp_tx tcp_rx udp_tx udp_rx

  w/o Restricted DMA  244.1     134.66   312.56   350.79
  w/ Restricted DMA    246.95   136.59   363.21   351.99

Rockchip SoC tcp_tx tcp_rx udp_tx udp_rx
w/o Restricted DMA 237.87 133.86 288.28 361.88
w/ Restricted DMA 256.01 130.95 292.28 353.19

How come you get better throughput with restricted DMA? Is it because doing DMA to/from a contiguous region allows for better grouping of transactions from the DRAM controller's perspective somehow?

Quoted 7 lines
Quoted 2 lines

I'm not sure, but actually, enabling the default swiotlb for wifi also helps the throughput a little bit for me.

OK, it would be interesting if you could get to the bottom of why performance does increase with swiotlb.
--
Florian

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#58 lore raw
Quoted 80 lines · Tomasz Figa

Hi Florian,

Quoted 68 lines · Florian Fainelli
Quoted 57 lines · Claire Chang
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 3 lines

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Sure, however the goals of using bounce buffering equally applies to RX and TX in that this is the only layer sitting between a stack (block, networking, USB, etc.) and the underlying device driver that scales well in order to massage a dma_addr_t to be within a particular physical range.

Quoted 8 lines

There is however room for improvement if the drivers are willing to change their buffer allocation strategy. When you receive Wi-Fi frames you need to allocate buffers for the Wi-Fi device to DMA into, and that happens ahead of the DMA transfers by the Wi-Fi device. At buffer allocation time you could very well allocate these frames from the restricted DMA region without having to bounce buffer them since the host CPU is in control over where and when to DMA into.

Quoted 6 lines

That is, however, still a trade-off between saving that one copy and protection from the DMA tampering with the packet contents when the kernel is reading them. Notice how the copy effectively makes a snapshot of the contents, guaranteeing that the kernel has a consistent view of the packet, which is not true if the DMA could modify the buffer contents in the middle of CPU accesses.

I would say that the window just became so much narrower for the PCIe end-point to overwrite contents with the copy because it would have to happen within the dma_unmap_{page,single} time and before the copy is finished to the bounce buffer.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#59 lore raw
Quoted 91 lines · Florian Fainelli
Quoted 80 lines · Tomasz Figa

Hi Florian,

Quoted 68 lines · Florian Fainelli
Quoted 57 lines · Claire Chang
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 3 lines

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Sure, however the goals of using bounce buffering equally applies to RX and TX in that this is the only layer sitting between a stack (block, networking, USB, etc.) and the underlying device driver that scales well in order to massage a dma_addr_t to be within a particular physical range.

Quoted 8 lines

There is however room for improvement if the drivers are willing to change their buffer allocation strategy. When you receive Wi-Fi frames you need to allocate buffers for the Wi-Fi device to DMA into, and that happens ahead of the DMA transfers by the Wi-Fi device. At buffer allocation time you could very well allocate these frames from the restricted DMA region without having to bounce buffer them since the host CPU is in control over where and when to DMA into.

Quoted 6 lines

That is, however, still a trade-off between saving that one copy and protection from the DMA tampering with the packet contents when the kernel is reading them. Notice how the copy effectively makes a snapshot of the contents, guaranteeing that the kernel has a consistent view of the packet, which is not true if the DMA could modify the buffer contents in the middle of CPU accesses.

Quoted 4 lines

I would say that the window just became so much narrower for the PCIe end-point to overwrite contents with the copy because it would have to happen within the dma_unmap_{page,single} time and before the copy is finished to the bounce buffer.

Not only. Imagine this:

a) Without bouncing:

- RX interrupt
- Pass the packet to the network stack
- Network stack validates the packet
- DMA overwrites the packet
- Network stack goes boom, because the packet changed after validation

b) With bouncing:

- RX interrupt
- Copy the packet to a DMA-inaccessible buffer
- Network stack validates the packet
- Network stack is happy, because the packet is guaranteed to stay the same after validation

Best regards,
Tomasz

#60 lore raw
Quoted 107 lines · Tomasz Figa
Quoted 91 lines · Florian Fainelli
Quoted 80 lines · Tomasz Figa

Hi Florian,

Quoted 68 lines · Florian Fainelli
Quoted 57 lines · Claire Chang
Quoted 14 lines · Florian Fainelli
Quoted 10 lines · Claire Chang
Quoted 5 lines

Can you explain how ATF gets involved and to what extent it does help, besides enforcing a secure region from the ARM CPU's perpsective? Does the PCIe root complex not have an IOMMU but can somehow be denied access to a region that is marked NS=0 in the ARM CPU's MMU? If so, that is still some sort of basic protection that the HW enforces, right?

We need the ATF support for memory MPU (memory protection unit). Restricted DMA (with reserved-memory in dts) makes sure the predefined memory region is for PCIe DMA only, but we still need MPU to locks down PCIe access to that specific regions.

Quoted 4 lines

OK so you do have a protection unit of some sort to enforce which region in DRAM the PCIE bridge is allowed to access, that makes sense, otherwise the restricted DMA region would only be a hint but nothing you can really enforce. This is almost entirely analogous to our systems then.

Here is the example of setting the MPU:
https://github.com/ARM-software/arm-trusted-firmware/blob/master/plat/mediatek/mt8183/drivers/emi_mpu/emi_mpu.c#L132

Quoted 26 lines

There may be some value in standardizing on an ARM SMCCC call then since you already support two different SoC vendors.

Quoted 22 lines
Quoted 21 lines

On Broadcom STB SoCs we have had something similar for a while however and while we don't have an IOMMU for the PCIe bridge, we do have a a basic protection mechanism whereby we can configure a region in DRAM to be PCIe read/write and CPU read/write which then gets used as the PCIe inbound region for the PCIe EP. By default the PCIe bridge is not allowed access to DRAM so we must call into a security agent to allow the PCIe bridge to access the designated DRAM region.

We have done this using a private CMA area region assigned via Device Tree, assigned with a and requiring the PCIe EP driver to use dma_alloc_from_contiguous() in order to allocate from this device private CMA area. The only drawback with that approach is that it requires knowing how much memory you need up front for buffers and DMA descriptors that the PCIe EP will need to process. The problem is that it requires driver modifications and that does not scale over the number of PCIe EP drivers, some we absolutely do not control, but there is no need to bounce buffer. Your approach scales better across PCIe EP drivers however it does require bounce buffering which could be a performance hit.

Quoted 7 lines
Quoted 1 line

Only the streaming DMA (map/unmap) needs bounce buffering.

True, and typically only on transmit since you don't really control where the sk_buff are allocated from, right? On RX since you need to hand buffer addresses to the WLAN chip prior to DMA, you can allocate them from a pool that already falls within the restricted DMA region, right?

Quoted 3 lines

Right, but applying bounce buffering to RX will make it more secure. The device won't be able to modify the content after unmap. Just like what iommu_unmap does.

Sure, however the goals of using bounce buffering equally applies to RX and TX in that this is the only layer sitting between a stack (block, networking, USB, etc.) and the underlying device driver that scales well in order to massage a dma_addr_t to be within a particular physical range.

Quoted 8 lines

There is however room for improvement if the drivers are willing to change their buffer allocation strategy. When you receive Wi-Fi frames you need to allocate buffers for the Wi-Fi device to DMA into, and that happens ahead of the DMA transfers by the Wi-Fi device. At buffer allocation time you could very well allocate these frames from the restricted DMA region without having to bounce buffer them since the host CPU is in control over where and when to DMA into.

Quoted 6 lines

That is, however, still a trade-off between saving that one copy and protection from the DMA tampering with the packet contents when the kernel is reading them. Notice how the copy effectively makes a snapshot of the contents, guaranteeing that the kernel has a consistent view of the packet, which is not true if the DMA could modify the buffer contents in the middle of CPU accesses.

Quoted 4 lines

I would say that the window just became so much narrower for the PCIe end-point to overwrite contents with the copy because it would have to happen within the dma_unmap_{page,single} time and before the copy is finished to the bounce buffer.

Not only. Imagine this:

a) Without bouncing:

- RX interrupt
- Pass the packet to the network stack
- Network stack validates the packet
- DMA overwrites the packet
- Network stack goes boom, because the packet changed after validation

Quoted 7 lines

b) With bouncing:

- RX interrupt
- Copy the packet to a DMA-inaccessible buffer
- Network stack validates the packet
- Network stack is happy, because the packet is guaranteed to stay the same after validation

Yes that's a much safer set of operations, thanks for walking through a practical example.

Signature · 5 lines
Florian
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu