Skip to contentSkip to conversation

dmaengine: Refactor dmaengine_check_align() to be bit operations only

PATCH Applied Andy Shevchenko 3 participants 10 messages Feb 26, 2020 – Mar 2, 2020 dmaengine

lore
Share & tools
lore
#1 lore raw

PATCH 2/4 dmaengine: Use negative condition for better readability

In reply to Andy Shevchenko

When negative condition is in use we may decrease indentation level and make the main part of logic better visible.

---
 include/linux/dmaengine.h | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
include/linux/dmaengine.h +18 -17
index 9f3f5582816a..ae56a91c2a05 100644
@@ -618,10 +618,11 @@ static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
 
 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
 {
-	if (tx->unmap) {
-		dmaengine_unmap_put(tx->unmap);
-		tx->unmap = NULL;
-	}
+	if (!tx->unmap)
+		return;
+
+	dmaengine_unmap_put(tx->unmap);
+	tx->unmap = NULL;
 }
 
 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
@@ -1408,11 +1409,12 @@ static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
 static inline void
 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
 {
-	if (st) {
-		st->last = last;
-		st->used = used;
-		st->residue = residue;
-	}
+	if (!st)
+		return;
+
+	st->last = last;
+	st->used = used;
+	st->residue = residue;
 }
 
 #ifdef CONFIG_DMA_ENGINE
@@ -1489,12 +1491,11 @@ static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
 	if (ret)
 		return ret;
 
-	if (caps.descriptor_reuse) {
-		tx->flags |= DMA_CTRL_REUSE;
-		return 0;
-	} else {
+	if (!caps.descriptor_reuse)
 		return -EPERM;
-	}
+
+	tx->flags |= DMA_CTRL_REUSE;
+	return 0;
 }
 
 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
@@ -1510,10 +1511,10 @@ static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
 {
 	/* this is supported for reusable desc, so check that */
-	if (dmaengine_desc_test_reuse(desc))
-		return desc->desc_free(desc);
-	else
+	if (!dmaengine_desc_test_reuse(desc))
 		return -EPERM;
+
+	return desc->desc_free(desc);
 }
 
 /* --- DMA device --- */
Signature · 1 line
2.25.0
#0 lore raw

PATCH 1/4 dmaengine: Refactor dmaengine_check_align() to be bit operations only

There is no need to have branch and temporary variable in the function. Simple convert it to be a set of bit and arithmetic operations.

---
 include/linux/dmaengine.h | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
include/linux/dmaengine.h +1 -8
index 64461fc64e1b..9f3f5582816a 100644
@@ -1155,14 +1155,7 @@ static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc
 static inline bool dmaengine_check_align(enum dmaengine_alignment align,
 					 size_t off1, size_t off2, size_t len)
 {
-	size_t mask;
-
-	if (!align)
-		return true;
-	mask = (1 << align) - 1;
-	if (mask & (off1 | off2 | len))
-		return false;
-	return true;
+	return !(((1 << align) - 1) & (off1 | off2 | len));
 }
 
 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
Signature · 1 line
2.25.0
#3 lore raw

PATCH 3/4 dmaengine: Drop redundant 'else' keyword

It's obvious that 'else' keyword is redundant in the code like

	if (foo)
		return bar;
	else if (baz)
		...

Drop it for good.

---
 include/linux/dmaengine.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/dmaengine.h +3 -3
index ae56a91c2a05..1bb5477ef7ec 100644
@@ -1230,9 +1230,9 @@ static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
 {
 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma);
-	else if (dmaf_p_disabled_continue(flags))
+	if (dmaf_p_disabled_continue(flags))
 		return dma_dev_to_maxpq(dma) - 1;
-	else if (dmaf_continue(flags))
+	if (dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma) - 3;
 	BUG();
 }
@@ -1243,7 +1243,7 @@ static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
 	if (inc) {
 		if (dir_icg)
 			return dir_icg;
-		else if (sgl)
+		if (sgl)
 			return icg;
 	}
 
Signature · 1 line
2.25.0
#6 lore raw

PATCH 4/4 dmaengine: consistently return string literal from switch-case

In reply to Andy Shevchenko

There is no need to have 'break;' statement in the default case followed by return certain string literal when all other cases have returned the string literals. So, refactor it accordingly.

---
 include/linux/dmaengine.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/dmaengine.h +1 -3
index 1bb5477ef7ec..d3672f065a64 100644
@@ -1560,9 +1560,7 @@ dmaengine_get_direction_text(enum dma_transfer_direction dir)
 	case DMA_DEV_TO_DEV:
 		return "DEV_TO_DEV";
 	default:
-		break;
+		return "invalid";
 	}
-
-	return "invalid";
 }
 #endif /* DMAENGINE_H */
Signature · 1 line
2.25.0
Reviewed-by on v1 4/4
#7 lore raw

Re: [PATCH v1 4/4] dmaengine: consistently return string literal from switch-case

Hi Andy,

Quoted 3 lines · Andy Shevchenko

There is no need to have 'break;' statement in the default case followed by return certain string literal when all other cases have returned the string literals. So, refactor it accordingly.

Quoted 11 lines
---
 include/linux/dmaengine.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/dmaengine.h +0 -0
index 1bb5477ef7ec..d3672f065a64 100644

include/linux/dmaengine.h dmaengine_get_direction_text(enum dma_transfer_direction dir) · line 1566

Quoted 10 lines
include/linux/dmaengine.h +1 -3
@@ -1560,9 +1560,7 @@ dmaengine_get_direction_text(enum dma_transfer_direction dir)
 	case DMA_DEV_TO_DEV:
 		return "DEV_TO_DEV";
 	default:
-		break;
+		return "invalid";
 	}
-
-	return "invalid";
 }
 #endif /* DMAENGINE_H */

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Reviewed-by on v1 1/4
#8 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

There is no need to have branch and temporary variable in the function. Simple convert it to be a set of bit and arithmetic operations.

Quoted 16 lines
---
 include/linux/dmaengine.h | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
include/linux/dmaengine.h +0 -2
index 64461fc64e1b..9f3f5582816a 100644
@@ -1155,14 +1155,7 @@ static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc
 static inline bool dmaengine_check_align(enum dmaengine_alignment align,
 					 size_t off1, size_t off2, size_t len)
 {
-	size_t mask;
-

include/linux/dmaengine.h static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc · line 1161

Quoted 10 lines
include/linux/dmaengine.h +1 -6
-	if (!align)
-		return true;
-	mask = (1 << align) - 1;
-	if (mask & (off1 | off2 | len))
-		return false;
-	return true;
+	return !(((1 << align) - 1) & (off1 | off2 | len));
 }
 
 static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Reviewed-by on v1 2/4
#2 lore raw

Re: [PATCH v1 2/4] dmaengine: Use negative condition for better readability

In reply to Andy Shevchenko

Hi Andy,

Quoted 2 lines · Andy Shevchenko

When negative condition is in use we may decrease indentation level and make the main part of logic better visible.

It makes the code a bit nicer, I agree.

Quoted 65 lines
---
 include/linux/dmaengine.h | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
include/linux/dmaengine.h +15 -14
index 9f3f5582816a..ae56a91c2a05 100644
@@ -618,10 +618,11 @@ static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
 
 static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
 {
-	if (tx->unmap) {
-		dmaengine_unmap_put(tx->unmap);
-		tx->unmap = NULL;
-	}
+	if (!tx->unmap)
+		return;
+
+	dmaengine_unmap_put(tx->unmap);
+	tx->unmap = NULL;
 }
 
 #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
@@ -1408,11 +1409,12 @@ static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
 static inline void
 dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
 {
-	if (st) {
-		st->last = last;
-		st->used = used;
-		st->residue = residue;
-	}
+	if (!st)
+		return;
+
+	st->last = last;
+	st->used = used;
+	st->residue = residue;
 }
 
 #ifdef CONFIG_DMA_ENGINE
@@ -1489,12 +1491,11 @@ static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
 	if (ret)
 		return ret;
 
-	if (caps.descriptor_reuse) {
-		tx->flags |= DMA_CTRL_REUSE;
-		return 0;
-	} else {
+	if (!caps.descriptor_reuse)
 		return -EPERM;
-	}
+
+	tx->flags |= DMA_CTRL_REUSE;
+	return 0;
 }
 
 static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
@@ -1510,10 +1511,10 @@ static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
 static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
 {
 	/* this is supported for reusable desc, so check that */

include/linux/dmaengine.h static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx) · line 1520

Quoted 10 lines
include/linux/dmaengine.h +3 -3
-	if (dmaengine_desc_test_reuse(desc))
-		return desc->desc_free(desc);
-	else
+	if (!dmaengine_desc_test_reuse(desc))
 		return -EPERM;
+
+	return desc->desc_free(desc);
 }
 
 /* --- DMA device --- */

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

#4 lore raw

Re: [PATCH v1 3/4] dmaengine: Drop redundant 'else' keyword

In reply to Andy Shevchenko

Hi Andy,

Quoted 19 lines · Andy Shevchenko

It's obvious that 'else' keyword is redundant in the code like

	if (foo)
		return bar;
	else if (baz)
		...

Drop it for good.

---
 include/linux/dmaengine.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/dmaengine.h +0 -0
index ae56a91c2a05..1bb5477ef7ec 100644

include/linux/dmaengine.h static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags) · line 1233

Quoted 5 lines
include/linux/dmaengine.h +1 -1
@@ -1230,9 +1230,9 @@ static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
 {
 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma);
-	else if (dmaf_p_disabled_continue(flags))
+	if (dmaf_p_disabled_continue(flags))

I would add blank line in between the - new - if cases for better readability.

Quoted 8 lines
include/linux/dmaengine.h +1 -1
 		return dma_dev_to_maxpq(dma) - 1;
-	else if (dmaf_continue(flags))
+	if (dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma) - 3;
 	BUG();
 }

include/linux/dmaengine.h static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg, · line 1249

Quoted 8 lines
include/linux/dmaengine.h +1 -1
@@ -1243,7 +1243,7 @@ static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
 	if (inc) {
 		if (dir_icg)
 			return dir_icg;
-		else if (sgl)
+		if (sgl)
 			return icg;
 	}
 

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

#5 lore raw

Re: [PATCH v1 3/4] dmaengine: Drop redundant 'else' keyword

Quoted 15 lines · Peter Ujfalusi
Quoted 14 lines · Andy Shevchenko

It's obvious that 'else' keyword is redundant in the code like

	if (foo)
		return bar;
	else if (baz)
		...

Drop it for good.

---
 include/linux/dmaengine.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 13 lines

include/linux/dmaengine.h static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags) · line 1233

Quoted 10 lines
include/linux/dmaengine.h +1 -1
index ae56a91c2a05..1bb5477ef7ec 100644
@@ -1230,9 +1230,9 @@ static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
 {
 	if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma);
-	else if (dmaf_p_disabled_continue(flags))
+	if (dmaf_p_disabled_continue(flags))

I would add blank line in between the - new - if cases for better readability.

Thank you for review and comment.

Here I have opposite opinion, but let Vinod and Dan, as maintainers, to decide. I'll be not against it if it's preferred way.

Quoted 21 lines
Quoted 8 lines
include/linux/dmaengine.h +1 -1
 		return dma_dev_to_maxpq(dma) - 1;
-	else if (dmaf_continue(flags))
+	if (dmaf_continue(flags))
 		return dma_dev_to_maxpq(dma) - 3;
 	BUG();
 }

include/linux/dmaengine.h static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg, · line 1249

Quoted 8 lines
include/linux/dmaengine.h +1 -1
@@ -1243,7 +1243,7 @@ static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
 	if (inc) {
 		if (dir_icg)
 			return dir_icg;
-		else if (sgl)
+		if (sgl)
 			return icg;
 	}
 

- Péter

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Applied
#9 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

There is no need to have branch and temporary variable in the function. Simple convert it to be a set of bit and arithmetic operations.

Applied, thanks

Signature · 1 line
~Vinod