diff mbox series

[v1] qed/qed_dev: guard against a possible division by zero

Message ID 20230309125636.176337-1-d-tatianin@yandex-team.ru (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v1] qed/qed_dev: guard against a possible division by zero | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 20 this patch: 20
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 20 this patch: 20
netdev/checkpatch warning CHECK: Alignment should match open parenthesis
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Daniil Tatianin March 9, 2023, 12:56 p.m. UTC
Previously we would divide total_left_rate by zero if num_vports
happened to be 1 because non_requested_count is calculated as
num_vports - req_count. Guard against this by validating num_vports at
the beginning and returning an error otherwise.

Found by Linux Verification Center (linuxtesting.org) with the SVACE
static analysis tool.

Fixes: bcd197c81f63 ("qed: Add vport WFQ configuration APIs")
Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
---
 drivers/net/ethernet/qlogic/qed/qed_dev.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Simon Horman March 9, 2023, 4:01 p.m. UTC | #1
On Thu, Mar 09, 2023 at 03:56:36PM +0300, Daniil Tatianin wrote:
> Previously we would divide total_left_rate by zero if num_vports
> happened to be 1 because non_requested_count is calculated as
> num_vports - req_count. Guard against this by validating num_vports at
> the beginning and returning an error otherwise.

Thanks,

I like this approach.

Reviewed-by: Simon Horman <simon.horman@corigine.com>

> Found by Linux Verification Center (linuxtesting.org) with the SVACE
> static analysis tool.
> 
> Fixes: bcd197c81f63 ("qed: Add vport WFQ configuration APIs")
> Signed-off-by: Daniil Tatianin <d-tatianin@yandex-team.ru>
> ---
>  drivers/net/ethernet/qlogic/qed/qed_dev.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> index d61cd32ec3b6..9aaaf5ad3eb0 100644
> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> @@ -5083,6 +5083,13 @@ static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
>  
>  	num_vports = p_hwfn->qm_info.num_vports;
>  
> +	if (num_vports < 2) {
> +		DP_NOTICE(p_hwfn,
> +			   "Unexpected num_vports: %d\n",
> +			   num_vports);

nit: the lines above are not indented correctly, however,
     I think that it's better yet if the DP_NOTICE is on a single line.

> +		return -EINVAL;
> +	}
> +
>  	/* Accounting for the vports which are configured for WFQ explicitly */
>  	for (i = 0; i < num_vports; i++) {
>  		u32 tmp_speed;
> -- 
> 2.25.1
>
diff mbox series

Patch

diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c b/drivers/net/ethernet/qlogic/qed/qed_dev.c
index d61cd32ec3b6..9aaaf5ad3eb0 100644
--- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
+++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
@@ -5083,6 +5083,13 @@  static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
 
 	num_vports = p_hwfn->qm_info.num_vports;
 
+	if (num_vports < 2) {
+		DP_NOTICE(p_hwfn,
+			   "Unexpected num_vports: %d\n",
+			   num_vports);
+		return -EINVAL;
+	}
+
 	/* Accounting for the vports which are configured for WFQ explicitly */
 	for (i = 0; i < num_vports; i++) {
 		u32 tmp_speed;