Skip to contentSkip to conversation

net/mlx5e: Return in default case statement in tx_post_resync_params

PATCH In review Nathan Chancellor 4 participants 11 messages Jul 8, 2019 – Jul 12, 2019

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

PATCH 1/1 net/mlx5e: Return in default case statement in tx_post_resync_params

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

This case statement was clearly designed to be one that should not be hit during runtime because of the WARN_ON statement so just return early to prevent copying uninitialized memory up into rn_be.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +1 -0
index 3f5f4317a22b..5c08891806f0 100644
@@ -250,6 +250,7 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
 	}
 	default:
 		WARN_ON(1);
+		return;
 	}
 
 	skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
Signature · 1 line
2.22.0
#1 lore raw
Quoted 35 lines · Nathan Chancellor

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

This case statement was clearly designed to be one that should not be hit during runtime because of the WARN_ON statement so just return early to prevent copying uninitialized memory up into rn_be.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +0 -0
index 3f5f4317a22b..5c08891806f0 100644

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c tx_post_resync_params(struct mlx5e_txqsq *sq, · line 254

Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +1 -0
@@ -250,6 +250,7 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
        }
        default:
                WARN_ON(1);
+               return;
        }

hmm...a switch statement with a single case is a code smell. How about a single conditional with early return? Then the "meat" of the happy path doesn't need an additional level of indentation.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 32 lines · Nick Desaulniers
Quoted 30 lines · Nathan Chancellor

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

This case statement was clearly designed to be one that should not be hit during runtime because of the WARN_ON statement so just return early to prevent copying uninitialized memory up into rn_be.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 17 lines

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c tx_post_resync_params(struct mlx5e_txqsq *sq, · line 254

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +1 -0
index 3f5f4317a22b..5c08891806f0 100644
@@ -250,6 +250,7 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
        }
        default:
                WARN_ON(1);
+               return;
        }

hmm...a switch statement with a single case is a code smell. How about a single conditional with early return? Then the "meat" of the happy path doesn't need an additional level of indentation.

Signature · 2 lines
Thanks,
~Nick Desaulniers

I assume that the reason for this is there may be other cipher types added in the future? I suppose the maintainers can give more clarity to that.

Furthermore, if they want the switch statements to remain, it looks like fill_static_params_ctx also returns in the default statement so it seems like this is the right fix.

Cheers,
Nathan

#3 lore raw
Quoted 51 lines · Nathan Chancellor
Quoted 41 lines · Nick Desaulniers
Quoted 39 lines · Nathan Chancellor

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

This case statement was clearly designed to be one that should not be hit during runtime because of the WARN_ON statement so just return early to prevent copying uninitialized memory up into rn_be.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")
---
 drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +1 -0
index 3f5f4317a22b..5c08891806f0 100644
@@ -250,6 +250,7 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
        }
        default:
                WARN_ON(1);
+               return;
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c tx_post_resync_params(struct mlx5e_txqsq *sq, · line 254

Quoted 1 line
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +0 -0
        }

hmm...a switch statement with a single case is a code smell. How about a single conditional with early return? Then the "meat" of the happy path doesn't need an additional level of indentation. --
Thanks,
~Nick Desaulniers

Quoted 3 lines

I assume that the reason for this is there may be other cipher types added in the future? I suppose the maintainers can give more clarity to that.

Our devices supports extra ciphers, for example TLS_CIPHER_AES_GCM_256. So I assume this was the reason for switch<->case, but because such implementation doesn't exist in any driver, I recommend to rewrite the code to have "if" statement and return early.

Quoted 7 lines

Furthermore, if they want the switch statements to remain, it looks like fill_static_params_ctx also returns in the default statement so it seems like this is the right fix.

Cheers,
Nathan

#4 lore raw

PATCH v2 1/1 net/mlx5e: Refactor switch statements to avoid using uninitialized variables

In reply to Nathan Chancellor

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

The default case statement should return in tx_post_resync_params like in fill_static_params_ctx. However, as Nick and Leon point out, the switch statements converted into if statements to clean up the code a bit since there is only one cipher supported. Do that to clear up the code.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")

---

v1 -> v2:

* Refactor switch statements into if statements

 .../mellanox/mlx5/core/en_accel/ktls_tx.c     | 33 +++++++------------
 1 file changed, 11 insertions(+), 22 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +11 -22
index 3f5f4317a22b..ea032f54197e 100644
@@ -25,23 +25,17 @@ static void
 fill_static_params_ctx(void *ctx, struct mlx5e_ktls_offload_context_tx *priv_tx)
 {
 	struct tls_crypto_info *crypto_info = priv_tx->crypto_info;
+	struct tls12_crypto_info_aes_gcm_128 *info;
 	char *initial_rn, *gcm_iv;
 	u16 salt_sz, rec_seq_sz;
 	char *salt, *rec_seq;
 	u8 tls_version;
 
-	switch (crypto_info->cipher_type) {
-	case TLS_CIPHER_AES_GCM_128: {
-		struct tls12_crypto_info_aes_gcm_128 *info =
-			(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
-
-		EXTRACT_INFO_FIELDS;
-		break;
-	}
-	default:
-		WARN_ON(1);
+	if (WARN_ON(crypto_info->cipher_type != TLS_CIPHER_AES_GCM_128))
 		return;
-	}
+
+	info = (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
+	EXTRACT_INFO_FIELDS;
 
 	gcm_iv      = MLX5_ADDR_OF(tls_static_params, ctx, gcm_iv);
 	initial_rn  = MLX5_ADDR_OF(tls_static_params, ctx, initial_record_number);
@@ -234,23 +228,18 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
 		      u64 rcd_sn)
 {
 	struct tls_crypto_info *crypto_info = priv_tx->crypto_info;
+	struct tls12_crypto_info_aes_gcm_128 *info;
 	__be64 rn_be = cpu_to_be64(rcd_sn);
 	bool skip_static_post;
 	u16 rec_seq_sz;
 	char *rec_seq;
 
-	switch (crypto_info->cipher_type) {
-	case TLS_CIPHER_AES_GCM_128: {
-		struct tls12_crypto_info_aes_gcm_128 *info =
-			(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
+	if (WARN_ON(crypto_info->cipher_type != TLS_CIPHER_AES_GCM_128))
+		return;
 
-		rec_seq = info->rec_seq;
-		rec_seq_sz = sizeof(info->rec_seq);
-		break;
-	}
-	default:
-		WARN_ON(1);
-	}
+	info = (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
+	rec_seq = info->rec_seq;
+	rec_seq_sz = sizeof(info->rec_seq);
 
 	skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
 	if (!skip_static_post)
Signature · 1 line
2.22.0
Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH v2] net/mlx5e: Refactor switch statements to avoid using uninitialized variables

Quoted 29 lines · Nathan Chancellor

clang warns:

drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:251:2:
warning: variable 'rec_seq_sz' is used uninitialized whenever switch default is taken [-Wsometimes-uninitialized]

        default:
        ^~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:255:46: note:
uninitialized use occurs here
        skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
                                                    ^~~~~~~~~~
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c:239:16: note:
initialize the variable 'rec_seq_sz' to silence this warning
        u16 rec_seq_sz;
                      ^
                       = 0

1 warning generated.

The default case statement should return in tx_post_resync_params like in fill_static_params_ctx. However, as Nick and Leon point out, the switch statements converted into if statements to clean up the code a bit since there is only one cipher supported. Do that to clear up the code.

Fixes d2ead1f360e8 ("net/mlx5e: Add kTLS TX HW offload support")
Quoted 9 lines

---

v1 -> v2:

* Refactor switch statements into if statements

 .../mellanox/mlx5/core/en_accel/ktls_tx.c     | 33 +++++++------------
 1 file changed, 11 insertions(+), 22 deletions(-)

Thanks,

maintainer
#6 lore raw

Re: [PATCH v2] net/mlx5e: Refactor switch statements to avoid using uninitialized variables

In reply to Nathan Chancellor

I applied your simpler addition of the return statement so that I could get the net-next pull request out tonight, just FYI...

#7 lore raw

Re: [PATCH v2] net/mlx5e: Refactor switch statements to avoid using uninitialized variables

Quoted 3 lines · David Miller

I applied your simpler addition of the return statement so that I could get the net-next pull request out tonight, just FYI...

Thanks for the heads up, I'll spin up a v3 just focusing on the refactoring.

Cheers,
Nathan

#8 lore raw

PATCH v3 1/1 net/mlx5e: Convert single case statement switch statements into if statements

In reply to Nathan Chancellor

During the review of commit 1ff2f0fa450e ("net/mlx5e: Return in default case statement in tx_post_resync_params"), Leon and Nick pointed out that the switch statements can be converted to single if statements that return early so that the code is easier to follow.

---

v1 -> v2:

* Refactor switch statements into if statements

v2 -> v3:

* Rebase on net-next after v1 was already applied, patch just refactors switch statements to if statements.

 .../mellanox/mlx5/core/en_accel/ktls_tx.c     | 34 ++++++-------------
 1 file changed, 11 insertions(+), 23 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_accel/ktls_tx.c +11 -23
index 5c08891806f0..ea032f54197e 100644
@@ -25,23 +25,17 @@ static void
 fill_static_params_ctx(void *ctx, struct mlx5e_ktls_offload_context_tx *priv_tx)
 {
 	struct tls_crypto_info *crypto_info = priv_tx->crypto_info;
+	struct tls12_crypto_info_aes_gcm_128 *info;
 	char *initial_rn, *gcm_iv;
 	u16 salt_sz, rec_seq_sz;
 	char *salt, *rec_seq;
 	u8 tls_version;
 
-	switch (crypto_info->cipher_type) {
-	case TLS_CIPHER_AES_GCM_128: {
-		struct tls12_crypto_info_aes_gcm_128 *info =
-			(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
-
-		EXTRACT_INFO_FIELDS;
-		break;
-	}
-	default:
-		WARN_ON(1);
+	if (WARN_ON(crypto_info->cipher_type != TLS_CIPHER_AES_GCM_128))
 		return;
-	}
+
+	info = (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
+	EXTRACT_INFO_FIELDS;
 
 	gcm_iv      = MLX5_ADDR_OF(tls_static_params, ctx, gcm_iv);
 	initial_rn  = MLX5_ADDR_OF(tls_static_params, ctx, initial_record_number);
@@ -234,24 +228,18 @@ tx_post_resync_params(struct mlx5e_txqsq *sq,
 		      u64 rcd_sn)
 {
 	struct tls_crypto_info *crypto_info = priv_tx->crypto_info;
+	struct tls12_crypto_info_aes_gcm_128 *info;
 	__be64 rn_be = cpu_to_be64(rcd_sn);
 	bool skip_static_post;
 	u16 rec_seq_sz;
 	char *rec_seq;
 
-	switch (crypto_info->cipher_type) {
-	case TLS_CIPHER_AES_GCM_128: {
-		struct tls12_crypto_info_aes_gcm_128 *info =
-			(struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
-
-		rec_seq = info->rec_seq;
-		rec_seq_sz = sizeof(info->rec_seq);
-		break;
-	}
-	default:
-		WARN_ON(1);
+	if (WARN_ON(crypto_info->cipher_type != TLS_CIPHER_AES_GCM_128))
 		return;
-	}
+
+	info = (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
+	rec_seq = info->rec_seq;
+	rec_seq_sz = sizeof(info->rec_seq);
 
 	skip_static_post = !memcmp(rec_seq, &rn_be, rec_seq_sz);
 	if (!skip_static_post)
Signature · 1 line
2.22.0
Reviewed-by on v3 1/1
#9 lore raw

Re: [PATCH net-next v3] net/mlx5e: Convert single case statement switch statements into if statements

Quoted 9 lines · Nathan Chancellor

During the review of commit 1ff2f0fa450e ("net/mlx5e: Return in default case statement in tx_post_resync_params"), Leon and Nick pointed out that the switch statements can be converted to single if statements that return early so that the code is easier to follow.

---

Thanks again,

maintainer Applied
#10 lore raw

Re: [PATCH net-next v3] net/mlx5e: Convert single case statement switch statements into if statements

In reply to Nathan Chancellor

From: Nathan Chancellor <[email protected]>
Date: Tue, 9 Jul 2019 23:06:15 -0700

Quoted 8 lines

During the review of commit 1ff2f0fa450e ("net/mlx5e: Return in default case statement in tx_post_resync_params"), Leon and Nick pointed out that the switch statements can be converted to single if statements that return early so that the code is easier to follow.

Applied, thanks for following up Nathan.