Skip to contentSkip to conversation

swiotlb: Cleanup and consistency fix

PATCH In review Florian Fainelli 3 participants 6 messages Jun 11, 2019 – Jun 14, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover swiotlb: Cleanup and consistency fix

Hi Christoph,

Still with my contrived memory layout where there is no physical memory the kernel can use below 4GB, it was possible to fail swiotlb_init(), but still not hit swiotlb_map_single() since all peripherals have a DMA_BIT_MASK() that is within the remaining addressable physical memory.

The second path could be backported to stable, but for the same reasons as the one we had just discussed before, this requires a very contrived test case that is not necessarily realistic or would warrant a stable backport IMHO.

Thanks!

Florian Fainelli (2):
swiotlb: Group identical cleanup in swiotlb_cleanup()
swiotlb: Return consistent SWIOTLB segments/nr_tbl

 kernel/dma/swiotlb.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
Signature · 6 lines
2.17.1

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

PATCH 1/2 swiotlb: Group identical cleanup in swiotlb_cleanup()

Avoid repeating the zeroing of global swiotlb variables in two locations and introduce swiotlb_cleanup() to do that.

---
 kernel/dma/swiotlb.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/dma/swiotlb.c +10 -8
index 13f0cb080a4d..b2b5c5df273c 100644
@@ -317,6 +317,14 @@ swiotlb_late_init_with_default_size(size_t default_size)
 	return rc;
 }
 
+static void swiotlb_cleanup(void)
+{
+	io_tlb_end = 0;
+	io_tlb_start = 0;
+	io_tlb_nslabs = 0;
+	max_segment = 0;
+}
+
 int
 swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
 {
@@ -367,10 +375,7 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
 	                                                 sizeof(int)));
 	io_tlb_list = NULL;
 cleanup3:
-	io_tlb_end = 0;
-	io_tlb_start = 0;
-	io_tlb_nslabs = 0;
-	max_segment = 0;
+	swiotlb_cleanup();
 	return -ENOMEM;
 }
 
@@ -394,10 +399,7 @@ void __init swiotlb_exit(void)
 		memblock_free_late(io_tlb_start,
 				   PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
 	}
-	io_tlb_start = 0;
-	io_tlb_end = 0;
-	io_tlb_nslabs = 0;
-	max_segment = 0;
+	swiotlb_cleanup();
 }
 
 /*
Signature · 1 line
2.17.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] swiotlb: Group identical cleanup in swiotlb_cleanup()

Quoted 4 lines · Florian Fainelli

Avoid repeating the zeroing of global swiotlb variables in two locations and introduce swiotlb_cleanup() to do that.

Looks good,

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

#3 lore raw

PATCH 2/2 swiotlb: Return consistent SWIOTLB segments/nr_tbl

In reply to Florian Fainelli

With a specifically contrived memory layout where there is no physical memory available to the kernel below the 4GB boundary, we will fail to perform the initial swiotlb_init() call and set no_iotlb_memory to true.

There are drivers out there that call into swiotlb_nr_tbl() to determine whether they can use the SWIOTLB. With the right DMA_BIT_MASK() value for these drivers (say 64-bit), they won't ever need to hit swiotlb_tbl_map_single() so this can go unoticed and we would be possibly lying about those drivers.

---
 kernel/dma/swiotlb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/dma/swiotlb.c +4 -4
index b2b5c5df273c..e906ef2e6315 100644
@@ -129,15 +129,17 @@ setup_io_tlb_npages(char *str)
 }
 early_param("swiotlb", setup_io_tlb_npages);
 
+static bool no_iotlb_memory;
+
 unsigned long swiotlb_nr_tbl(void)
 {
-	return io_tlb_nslabs;
+	return unlikely(no_iotlb_memory) ? 0 : io_tlb_nslabs;
 }
 EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
 
 unsigned int swiotlb_max_segment(void)
 {
-	return max_segment;
+	return unlikely(no_iotlb_memory) ? 0 : max_segment;
 }
 EXPORT_SYMBOL_GPL(swiotlb_max_segment);
 
@@ -160,8 +162,6 @@ unsigned long swiotlb_size_or_default(void)
 	return size ? size : (IO_TLB_DEFAULT_SIZE);
 }
 
-static bool no_iotlb_memory;
-
 void swiotlb_print_info(void)
 {
 	unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] swiotlb: Return consistent SWIOTLB segments/nr_tbl

Quoted 26 lines · Florian Fainelli

With a specifically contrived memory layout where there is no physical memory available to the kernel below the 4GB boundary, we will fail to perform the initial swiotlb_init() call and set no_iotlb_memory to true.

There are drivers out there that call into swiotlb_nr_tbl() to determine whether they can use the SWIOTLB. With the right DMA_BIT_MASK() value for these drivers (say 64-bit), they won't ever need to hit swiotlb_tbl_map_single() so this can go unoticed and we would be possibly lying about those drivers.

---
 kernel/dma/swiotlb.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/dma/swiotlb.c +2 -0
index b2b5c5df273c..e906ef2e6315 100644
@@ -129,15 +129,17 @@ setup_io_tlb_npages(char *str)
 }
 early_param("swiotlb", setup_io_tlb_npages);
 
+static bool no_iotlb_memory;
+
 unsigned long swiotlb_nr_tbl(void)

kernel/dma/swiotlb.c setup_io_tlb_npages(char *str) · line 142

Quoted 10 lines
kernel/dma/swiotlb.c +2 -2
 {
-	return io_tlb_nslabs;
+	return unlikely(no_iotlb_memory) ? 0 : io_tlb_nslabs;
 }
 EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
 
 unsigned int swiotlb_max_segment(void)
 {
-	return max_segment;
+	return unlikely(no_iotlb_memory) ? 0 : max_segment;

I wouldn't bother with the unlikely here as anythign querying swiotlb details should pretty much be a slow path already.

Otherwise looks good:

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

#5 lore raw
In reply to Florian Fainelli
Quoted 1 line · Florian Fainelli

Hi Christoph,

I pulled the patches in my tree.

Quoted 11 lines

Still with my contrived memory layout where there is no physical memory the kernel can use below 4GB, it was possible to fail swiotlb_init(), but still not hit swiotlb_map_single() since all peripherals have a DMA_BIT_MASK() that is within the remaining addressable physical memory.

The second path could be backported to stable, but for the same reasons as the one we had just discussed before, this requires a very contrived test case that is not necessarily realistic or would warrant a stable backport IMHO.

Quoted 12 lines

Thanks!

Florian Fainelli (2):
swiotlb: Group identical cleanup in swiotlb_cleanup()
swiotlb: Return consistent SWIOTLB segments/nr_tbl

 kernel/dma/swiotlb.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1

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