Skip to contentSkip to conversation

dma-mapping: add unlikely hint for error path in dma_mapping_error

PATCH In review Heiner Kallweit 3 participants 5 messages Dec 13, 2020 – Jan 8, 2021

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

PATCH v2 1/1 dma-mapping: add unlikely hint for error path in dma_mapping_error

Zillions of drivers use the unlikely() hint when checking the result of dma_mapping_error(). This is an inline function anyway, so we can move the hint into this function and remove it from drivers.

---
v2:
Split the big patch into the change for dma-mapping.h and follow-up patches per subsystem that will go through the trees of the respective maintainers.

---
 include/linux/dma-mapping.h | 2 +-
 kernel/dma/map_benchmark.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/dma-mapping.h +1 -1
index 2e49996a8..6177e20b5 100644
@@ -95,7 +95,7 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
 {
 	debug_dma_mapping_error(dev, dma_addr);
 
-	if (dma_addr == DMA_MAPPING_ERROR)
+	if (unlikely(dma_addr == DMA_MAPPING_ERROR))
 		return -ENOMEM;
 	return 0;
 }
kernel/dma/map_benchmark.c +1 -1
index b1496e744..901420a5d 100644
@@ -78,7 +78,7 @@ static int map_benchmark_thread(void *data)
 
 		map_stime = ktime_get();
 		dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
-		if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
+		if (dma_mapping_error(map->dev, dma_addr)) {
 			pr_err("dma_map_single failed on %s\n",
 				dev_name(map->dev));
 			ret = -ENOMEM;
Signature · 6 lines
2.29.2

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

-----Original Message-----
From: Heiner Kallweit [mailto:[email protected]]
Sent: Monday, December 14, 2020 5:33 AM
To: Christoph Hellwig <[email protected]>; Marek Szyprowski <[email protected]>; Robin Murphy <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: open list:AMD IOMMU (AMD-VI) <[email protected]>; Linux Kernel Mailing List <[email protected]>
Subject: [PATCH v2] dma-mapping: add unlikely hint for error path in dma_mapping_error

Quoted 5 lines

Zillions of drivers use the unlikely() hint when checking the result of dma_mapping_error(). This is an inline function anyway, so we can move the hint into this function and remove it from drivers.

not sure if this is really necessary. It seems the original code is more readable. Readers can more easily understand we are predicting the branch based on the return value of dma_mapping_error().

Anyway, I don't object to this one. if other people like it, I am also ok with it.

Quoted 38 lines

---
v2:
Split the big patch into the change for dma-mapping.h and follow-up patches per subsystem that will go through the trees of the respective maintainers.

---
 include/linux/dma-mapping.h | 2 +-
 kernel/dma/map_benchmark.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/dma-mapping.h +1 -1
index 2e49996a8..6177e20b5 100644
@@ -95,7 +95,7 @@ static inline int dma_mapping_error(struct device *dev,
 dma_addr_t dma_addr)
 {
 	debug_dma_mapping_error(dev, dma_addr);
 
-	if (dma_addr == DMA_MAPPING_ERROR)
+	if (unlikely(dma_addr == DMA_MAPPING_ERROR))
 		return -ENOMEM;
 	return 0;

}

kernel/dma/map_benchmark.c +1 -2
index b1496e744..901420a5d 100644
@@ -78,7 +78,7 @@ static int map_benchmark_thread(void *data)
 
 		map_stime = ktime_get();
 		dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
-		if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
+		if (dma_mapping_error(map->dev, dma_addr)) {
 			pr_err("dma_map_single failed on %s\n",
 				dev_name(map->dev));
 			ret = -ENOMEM;
--
Quoted 1 line

2.29.2

Thanks
Barry

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

#2 lore raw

Am 13.12.2020 um 22:27 schrieb Song Bao Hua (Barry Song):

Quoted 17 lines
Quoted 17 lines

-----Original Message-----
From: Heiner Kallweit [mailto:[email protected]]
Sent: Monday, December 14, 2020 5:33 AM
To: Christoph Hellwig <[email protected]>; Marek Szyprowski <[email protected]>; Robin Murphy <[email protected]>; Song Bao Hua (Barry Song) <[email protected]>
Cc: open list:AMD IOMMU (AMD-VI) <[email protected]>; Linux Kernel Mailing List <[email protected]>
Subject: [PATCH v2] dma-mapping: add unlikely hint for error path in dma_mapping_error

Zillions of drivers use the unlikely() hint when checking the result of dma_mapping_error(). This is an inline function anyway, so we can move the hint into this function and remove it from drivers.

Quoted 7 lines
Quoted 1 line

not sure if this is really necessary. It seems the original code is more readable. Readers can more easily understand we are predicting the branch based on the return value of dma_mapping_error().

I basically see two points promoting the proposed change:
1. Driver authors shouldn't have to think (as far as possible) about whether a branch prediction hint could make sense for a standard core API call. I saw quite some past discussions about when something is unlikely enough so that an unlikely() makes sense. If the core can hide some more complexity from drivers, then I think it's a good thing.
2. If we ever want or have to change the use of unlikely with dma_mapping_error(), then we have to do it in just one place.

Quoted 33 lines

Anyway, I don't object to this one. if other people like it, I am also ok with it.

Quoted 30 lines

---
v2:
Split the big patch into the change for dma-mapping.h and follow-up patches per subsystem that will go through the trees of the respective maintainers.

---
 include/linux/dma-mapping.h | 2 +-
 kernel/dma/map_benchmark.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/dma-mapping.h +1 -1
index 2e49996a8..6177e20b5 100644
@@ -95,7 +95,7 @@ static inline int dma_mapping_error(struct device *dev,
 dma_addr_t dma_addr)
 {
 	debug_dma_mapping_error(dev, dma_addr);
 
-	if (dma_addr == DMA_MAPPING_ERROR)
+	if (unlikely(dma_addr == DMA_MAPPING_ERROR))
 		return -ENOMEM;
 	return 0;

}

kernel/dma/map_benchmark.c +0 -0
index b1496e744..901420a5d 100644
@@ -78,7 +78,7 @@ static int map_benchmark_thread(void *data)
 
Quoted 13 lines
Quoted 9 lines
kernel/dma/map_benchmark.c +1 -2
 		map_stime = ktime_get();
 		dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
-		if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
+		if (dma_mapping_error(map->dev, dma_addr)) {
 			pr_err("dma_map_single failed on %s\n",
 				dev_name(map->dev));
 			ret = -ENOMEM;
--

2.29.2

Thanks
Barry

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

reviewer Reviewed-by on v2 1/1
#3 lore raw
In reply to Heiner Kallweit
Quoted 3 lines · Heiner Kallweit

Zillions of drivers use the unlikely() hint when checking the result of dma_mapping_error(). This is an inline function anyway, so we can move the hint into this function and remove it from drivers.

FWIW I consider this case similar to the same hint in WARN_ON() and friends - it's a pretty severe condition that should never be expected to be hit in normal operation, so it's entirely logical for it to be implicitly unlikely. I struggle to imagine any case that would specifically *not* want that (or worse, want to hint it as likely). Some DMA API backends may spend considerable time trying as hard as possible to make a mapping work before eventually admitting defeat, so the idea of ever trying to optimise at the driver level for failure in hot paths just makes no sense.

Thanks,
Robin.

Quoted 30 lines

---
v2:
Split the big patch into the change for dma-mapping.h and follow-up patches per subsystem that will go through the trees of the respective maintainers.

---
  include/linux/dma-mapping.h | 2 +-
  kernel/dma/map_benchmark.c  | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/dma-mapping.h +1 -1
index 2e49996a8..6177e20b5 100644
@@ -95,7 +95,7 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  {
  	debug_dma_mapping_error(dev, dma_addr);
 
-	if (dma_addr == DMA_MAPPING_ERROR)
+	if (unlikely(dma_addr == DMA_MAPPING_ERROR))
  		return -ENOMEM;
  	return 0;
  }
kernel/dma/map_benchmark.c +0 -0
index b1496e744..901420a5d 100644

kernel/dma/map_benchmark.c static int map_benchmark_thread(void *data) · line 84

Quoted 8 lines
kernel/dma/map_benchmark.c +1 -1
@@ -78,7 +78,7 @@ static int map_benchmark_thread(void *data)
 
  		map_stime = ktime_get();
  		dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
-		if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
+		if (dma_mapping_error(map->dev, dma_addr)) {
  			pr_err("dma_map_single failed on %s\n",
  				dev_name(map->dev));
  			ret = -ENOMEM;

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

#4 lore raw
Quoted 40 lines · Robin Murphy
Quoted 3 lines · Heiner Kallweit

Zillions of drivers use the unlikely() hint when checking the result of dma_mapping_error(). This is an inline function anyway, so we can move the hint into this function and remove it from drivers.

FWIW I consider this case similar to the same hint in WARN_ON() and friends - it's a pretty severe condition that should never be expected to be hit in normal operation, so it's entirely logical for it to be implicitly unlikely. I struggle to imagine any case that would specifically *not* want that (or worse, want to hint it as likely). Some DMA API backends may spend considerable time trying as hard as possible to make a mapping work before eventually admitting defeat, so the idea of ever trying to optimise at the driver level for failure in hot paths just makes no sense.

Thanks,
Robin.

Quoted 28 lines

---
v2:
Split the big patch into the change for dma-mapping.h and follow-up patches per subsystem that will go through the trees of the respective maintainers.
---
  include/linux/dma-mapping.h | 2 +- kernel/dma/map_benchmark.c  | 2 +-

  2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/dma-mapping.h +1 -0
index 2e49996a8..6177e20b5 100644
@@ -95,7 +95,7 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
   {
       debug_dma_mapping_error(dev, dma_addr);
   -    if (dma_addr == DMA_MAPPING_ERROR)
+    if (unlikely(dma_addr == DMA_MAPPING_ERROR))
           return -ENOMEM;
       return 0;
   }
kernel/dma/map_benchmark.c +0 -0
index b1496e744..901420a5d 100644
@@ -78,7 +78,7 @@ static int map_benchmark_thread(void *data)
Quoted 8 lines

kernel/dma/map_benchmark.c static int map_benchmark_thread(void *data)

Quoted 8 lines
kernel/dma/map_benchmark.c +1 -1
             map_stime = ktime_get();
           dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
-        if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
+        if (dma_mapping_error(map->dev, dma_addr)) {
               pr_err("dma_map_single failed on %s\n",
                   dev_name(map->dev));
               ret = -ENOMEM;
 

Is this patch going to make it to linux-next?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu