All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: rmd320 - use swap macro in rmd320_transform
@ 2018-07-18 17:19 Gustavo A. R. Silva
  2018-07-18 18:31 ` make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform) Joe Perches
  2018-07-27 16:05 ` [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Herbert Xu
  0 siblings, 2 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2018-07-18 17:19 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller
  Cc: linux-crypto, linux-kernel, Gustavo A. R. Silva

Make use of the swap macro and remove unnecessary variable *tmp*.
This makes the code easier to read and maintain.

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 crypto/rmd320.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/crypto/rmd320.c b/crypto/rmd320.c
index ab3cf93..3ae1df5 100644
--- a/crypto/rmd320.c
+++ b/crypto/rmd320.c
@@ -53,7 +53,7 @@ struct rmd320_ctx {
 
 static void rmd320_transform(u32 *state, const __le32 *in)
 {
-	u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
+	u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
 
 	/* Initialize left lane */
 	aa = state[0];
@@ -106,7 +106,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12],  6);
 
 	/* Swap contents of "a" registers */
-	tmp = aa; aa = aaa; aaa = tmp;
+	swap(aa, aaa);
 
 	/* round 2: left lane" */
 	ROUND(ee, aa, bb, cc, dd, F2, K2, in[7],   7);
@@ -145,7 +145,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2],  11);
 
 	/* Swap contents of "b" registers */
-	tmp = bb; bb = bbb; bbb = tmp;
+	swap(bb, bbb);
 
 	/* round 3: left lane" */
 	ROUND(dd, ee, aa, bb, cc, F3, K3, in[3],  11);
@@ -184,7 +184,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13],  5);
 
 	/* Swap contents of "c" registers */
-	tmp = cc; cc = ccc; ccc = tmp;
+	swap(cc, ccc);
 
 	/* round 4: left lane" */
 	ROUND(cc, dd, ee, aa, bb, F4, K4, in[1],  11);
@@ -223,7 +223,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14],  8);
 
 	/* Swap contents of "d" registers */
-	tmp = dd; dd = ddd; ddd = tmp;
+	swap(dd, ddd);
 
 	/* round 5: left lane" */
 	ROUND(bb, cc, dd, ee, aa, F5, K5, in[4],   9);
@@ -262,7 +262,7 @@ static void rmd320_transform(u32 *state, const __le32 *in)
 	ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
 
 	/* Swap contents of "e" registers */
-	tmp = ee; ee = eee; eee = tmp;
+	swap(ee, eee);
 
 	/* combine results */
 	state[0] += aa;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)
  2018-07-18 17:19 [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Gustavo A. R. Silva
@ 2018-07-18 18:31 ` Joe Perches
  2018-07-20 15:43   ` Gustavo A. R. Silva
  2018-07-27 16:05 ` [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Herbert Xu
  1 sibling, 1 reply; 5+ messages in thread
From: Joe Perches @ 2018-07-18 18:31 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Herbert Xu, David S. Miller, Andrew Morton
  Cc: linux-crypto, linux-kernel, Pablo Neira Ayuso

On Wed, 2018-07-18 at 12:19 -0500, Gustavo A. R. Silva wrote:
> swap

Perhaps the swap macro should verify that the
swap(a, b) arguments are the same type.

Something like the patch below, but this patch
causes a compilation failure on at least a couple
cases that aren't obviously correct in

net/netfilter/ipset/ip_set_bitmap_port.c
	where a u16 is swapped with a u32.

and

net/netfilter/ipset/ip_set_hash_netport.c
	where a single bit bitfield is swapped with a u8
---
 include/linux/kernel.h | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d6aac75b51ba..506b59e0da24 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -969,14 +969,19 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  */
 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
-
 /**
  * swap - swap values of @a and @b
  * @a: first value
  * @b: second value
+ * @a and @b must be the same type
  */
-#define swap(a, b) \
-	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
+#define swap(a, b)							\
+do {									\
+	typeof(a) __tmp = (a);						\
+	BUILD_BUG_ON(!__same_type(typeof(a), typeof(b)));		\
+	(a) = (b);							\
+	(b) = __tmp;							\
+} while (0)
 
 /* This counts to 12. Any more, it will return 13th argument. */
 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)
  2018-07-18 18:31 ` make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform) Joe Perches
@ 2018-07-20 15:43   ` Gustavo A. R. Silva
  2018-07-21  5:34     ` Joe Perches
  0 siblings, 1 reply; 5+ messages in thread
From: Gustavo A. R. Silva @ 2018-07-20 15:43 UTC (permalink / raw)
  To: Joe Perches, Herbert Xu, David S. Miller, Andrew Morton
  Cc: linux-crypto, linux-kernel, Pablo Neira Ayuso

Hi Joe,

I like the idea. I'll take a look at the cases you mention and see
if I can make them work with your version of swap.

BTW, I wonder if it would be more convenient to trigger a build warning
instead of an error.

What do you think?

Thanks
--
Gustavo

On 07/18/2018 01:31 PM, Joe Perches wrote:
> On Wed, 2018-07-18 at 12:19 -0500, Gustavo A. R. Silva wrote:
>> swap
> 
> Perhaps the swap macro should verify that the
> swap(a, b) arguments are the same type.
> 
> Something like the patch below, but this patch
> causes a compilation failure on at least a couple
> cases that aren't obviously correct in
> 
> net/netfilter/ipset/ip_set_bitmap_port.c
> 	where a u16 is swapped with a u32.
> 
> and
> 
> net/netfilter/ipset/ip_set_hash_netport.c
> 	where a single bit bitfield is swapped with a u8
> ---
>  include/linux/kernel.h | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6aac75b51ba..506b59e0da24 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -969,14 +969,19 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
>   */
>  #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
>  
> -
>  /**
>   * swap - swap values of @a and @b
>   * @a: first value
>   * @b: second value
> + * @a and @b must be the same type
>   */
> -#define swap(a, b) \
> -	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
> +#define swap(a, b)							\
> +do {									\
> +	typeof(a) __tmp = (a);						\
> +	BUILD_BUG_ON(!__same_type(typeof(a), typeof(b)));		\
> +	(a) = (b);							\
> +	(b) = __tmp;							\
> +} while (0)
>  
>  /* This counts to 12. Any more, it will return 13th argument. */
>  #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform)
  2018-07-20 15:43   ` Gustavo A. R. Silva
@ 2018-07-21  5:34     ` Joe Perches
  0 siblings, 0 replies; 5+ messages in thread
From: Joe Perches @ 2018-07-21  5:34 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Herbert Xu, David S. Miller, Andrew Morton
  Cc: linux-crypto, linux-kernel, Pablo Neira Ayuso

On Fri, 2018-07-20 at 10:43 -0500, Gustavo A. R. Silva wrote:
> Hi Joe,
> 
> I like the idea. I'll take a look at the cases you mention and see
> if I can make them work with your version of swap.
> 
> BTW, I wonder if it would be more convenient to trigger a build warning
> instead of an error.
> 
> What do you think?

I can't think of a way to have a #pragma (or _Pragma equivalent)
emit a warning on a condition.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform
  2018-07-18 17:19 [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Gustavo A. R. Silva
  2018-07-18 18:31 ` make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform) Joe Perches
@ 2018-07-27 16:05 ` Herbert Xu
  1 sibling, 0 replies; 5+ messages in thread
From: Herbert Xu @ 2018-07-27 16:05 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: David S. Miller, linux-crypto, linux-kernel

On Wed, Jul 18, 2018 at 12:19:08PM -0500, Gustavo A. R. Silva wrote:
> Make use of the swap macro and remove unnecessary variable *tmp*.
> This makes the code easier to read and maintain.
> 
> This code was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-07-27 16:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-18 17:19 [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Gustavo A. R. Silva
2018-07-18 18:31 ` make sure swap arguments are the same type? (was: Re: [PATCH] crypto: rmd320 - use swap macro in rmd320_transform) Joe Perches
2018-07-20 15:43   ` Gustavo A. R. Silva
2018-07-21  5:34     ` Joe Perches
2018-07-27 16:05 ` [PATCH] crypto: rmd320 - use swap macro in rmd320_transform Herbert Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.