2023-02-09 10:39:18

by Daniil Tatianin

[permalink] [raw]
Subject: [PATCH v0] qed/qed_dev: guard against a possible division by zero

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 explicitly checking for
zero when doing the division.

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 <[email protected]>
---
drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c b/drivers/net/ethernet/qlogic/qed/qed_dev.c
index d61cd32ec3b6..90927f68c459 100644
--- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
+++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
@@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,

total_left_rate = min_pf_rate - total_req_min_rate;

- left_rate_per_vp = total_left_rate / non_requested_count;
+ left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);
if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
"Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
--
2.25.1



2023-02-09 11:14:04

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking for
> zero when doing the division.
>
> 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 <[email protected]>
> ---
> drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> index d61cd32ec3b6..90927f68c459 100644
> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
>
> total_left_rate = min_pf_rate - total_req_min_rate;
>
> - left_rate_per_vp = total_left_rate / non_requested_count;
> + left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);

I don't know if num_vports can be 1.
But if it is then I agree that the above will be a divide by zero.

I do, however, wonder if it would be better to either:

* Treat this case as invalid and return with -EINVAL if num_vports is 1; or

* Skip both the calculation immediately above and the code
in the if condition below, which is the only place where
the calculated value is used, if num_vports is 1.
I don't think the if clause makes much sense if num_vports is one.

> if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
> DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
> "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
> --
> 2.25.1
>

2023-02-14 07:23:24

by Daniil Tatianin

[permalink] [raw]
Subject: Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero



On 2/9/23 2:13 PM, Simon Horman wrote:
> On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking for
>> zero when doing the division.
>>
>> 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 <[email protected]>
>> ---
>> drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>> index d61cd32ec3b6..90927f68c459 100644
>> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
>>
>> total_left_rate = min_pf_rate - total_req_min_rate;
>>
>> - left_rate_per_vp = total_left_rate / non_requested_count;
>> + left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);
>
> I don't know if num_vports can be 1.
> But if it is then I agree that the above will be a divide by zero.
>
> I do, however, wonder if it would be better to either:
>
> * Treat this case as invalid and return with -EINVAL if num_vports is 1; or
I think that's a good idea considering num_vports == 1 is indeed an
invalid value.
I'd like to hear a maintainer's opinion on this.
> * Skip both the calculation immediately above and the code
> in the if condition below, which is the only place where
> the calculated value is used, if num_vports is 1.
> I don't think the if clause makes much sense if num_vports is one.left_rate_per_vp is also used below the if clause, it is assigned to
.min_speed in a for loop. Looking at that code division by 1 seems to
make sense to me in this case.
>
>> if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
>> DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
>> "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
>> --
>> 2.25.1
>>

2023-02-15 21:21:05

by Manish Chopra

[permalink] [raw]
Subject: RE: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

> -----Original Message-----
> From: Daniil Tatianin <[email protected]>
> Sent: Tuesday, February 14, 2023 12:53 PM
> To: Simon Horman <[email protected]>
> Cc: Ariel Elior <[email protected]>; Manish Chopra
> <[email protected]>; David S. Miller <[email protected]>; Eric
> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo
> Abeni <[email protected]>; Yuval Mintz <[email protected]>;
> [email protected]; [email protected]
> Subject: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division
> by zero
>
> External Email
>
> ----------------------------------------------------------------------
>
>
> On 2/9/23 2:13 PM, Simon Horman wrote:
> > On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking for
> >> zero when doing the division.
> >>
> >> 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 <[email protected]>
> >> ---
> >> drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
> >> 1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >> b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >> index d61cd32ec3b6..90927f68c459 100644
> >> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn
> >> *p_hwfn,
> >>
> >> total_left_rate = min_pf_rate - total_req_min_rate;
> >>
> >> - left_rate_per_vp = total_left_rate / non_requested_count;
> >> + left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);
> >
> > I don't know if num_vports can be 1.
> > But if it is then I agree that the above will be a divide by zero.
> >
> > I do, however, wonder if it would be better to either:
> >
> > * Treat this case as invalid and return with -EINVAL if num_vports is
> > 1; or
> I think that's a good idea considering num_vports == 1 is indeed an invalid
> value.
> I'd like to hear a maintainer's opinion on this.

Practically, this flow will only hit with presence of SR-IOV VFs. In that case it's
always expected to have num_vports > 1.

> > * Skip both the calculation immediately above and the code
> > in the if condition below, which is the only place where
> > the calculated value is used, if num_vports is 1.
> > I don't think the if clause makes much sense if num_vports is
> > one.left_rate_per_vp is also used below the if clause, it is assigned
> > to
> .min_speed in a for loop. Looking at that code division by 1 seems to make
> sense to me in this case.
> >
> >> if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
> >> DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
> >> "Non WFQ configured vports rate [%d Mbps] is less
> than one
> >> percent of configured PF min rate[%d Mbps]\n",
> >> --
> >> 2.25.1
> >>

2023-02-16 06:42:29

by Daniil Tatianin

[permalink] [raw]
Subject: Re: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

On 2/16/23 12:20 AM, Manish Chopra wrote:
>> -----Original Message-----
>> From: Daniil Tatianin <[email protected]>
>> Sent: Tuesday, February 14, 2023 12:53 PM
>> To: Simon Horman <[email protected]>
>> Cc: Ariel Elior <[email protected]>; Manish Chopra
>> <[email protected]>; David S. Miller <[email protected]>; Eric
>> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo
>> Abeni <[email protected]>; Yuval Mintz <[email protected]>;
>> [email protected]; [email protected]
>> Subject: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division
>> by zero
>>
>> External Email
>>
>> ----------------------------------------------------------------------
>>
>>
>> On 2/9/23 2:13 PM, Simon Horman wrote:
>>> On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking for
>>>> zero when doing the division.
>>>>
>>>> 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 <[email protected]>
>>>> ---
>>>> drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
>>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>> b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>> index d61cd32ec3b6..90927f68c459 100644
>>>> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn
>>>> *p_hwfn,
>>>>
>>>> total_left_rate = min_pf_rate - total_req_min_rate;
>>>>
>>>> - left_rate_per_vp = total_left_rate / non_requested_count;
>>>> + left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);
>>>
>>> I don't know if num_vports can be 1.
>>> But if it is then I agree that the above will be a divide by zero.
>>>
>>> I do, however, wonder if it would be better to either:
>>>
>>> * Treat this case as invalid and return with -EINVAL if num_vports is
>>> 1; or
>> I think that's a good idea considering num_vports == 1 is indeed an invalid
>> value.
>> I'd like to hear a maintainer's opinion on this.
>
> Practically, this flow will only hit with presence of SR-IOV VFs. In that case it's
> always expected to have num_vports > 1.

In that case, should we add a check and return with -EINVAL otherwise?
Thank you!

>>> * Skip both the calculation immediately above and the code
>>> in the if condition below, which is the only place where
>>> the calculated value is used, if num_vports is 1.
>>> I don't think the if clause makes much sense if num_vports is
>>> one.left_rate_per_vp is also used below the if clause, it is assigned
>>> to
>> .min_speed in a for loop. Looking at that code division by 1 seems to make
>> sense to me in this case.
>>>
>>>> if (left_rate_per_vp < min_pf_rate / QED_WFQ_UNIT) {
>>>> DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
>>>> "Non WFQ configured vports rate [%d Mbps] is less
>> than one
>>>> percent of configured PF min rate[%d Mbps]\n",
>>>> --
>>>> 2.25.1
>>>>

2023-03-03 12:18:32

by Daniil Tatianin

[permalink] [raw]
Subject: Re: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

On 2/16/23 9:42 AM, Daniil Tatianin wrote:
> On 2/16/23 12:20 AM, Manish Chopra wrote:
>>> -----Original Message-----
>>> From: Daniil Tatianin <[email protected]>
>>> Sent: Tuesday, February 14, 2023 12:53 PM
>>> To: Simon Horman <[email protected]>
>>> Cc: Ariel Elior <[email protected]>; Manish Chopra
>>> <[email protected]>; David S. Miller <[email protected]>; Eric
>>> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo
>>> Abeni <[email protected]>; Yuval Mintz <[email protected]>;
>>> [email protected]; [email protected]
>>> Subject: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible
>>> division
>>> by zero
>>>
>>> External Email
>>>
>>> ----------------------------------------------------------------------
>>>
>>>
>>> On 2/9/23 2:13 PM, Simon Horman wrote:
>>>> On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking for
>>>>> zero when doing the division.
>>>>>
>>>>> 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 <[email protected]>
>>>>> ---
>>>>>    drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
>>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>>
>>>>> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>> b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>> index d61cd32ec3b6..90927f68c459 100644
>>>>> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct qed_hwfn
>>>>> *p_hwfn,
>>>>>
>>>>>        total_left_rate    = min_pf_rate - total_req_min_rate;
>>>>>
>>>>> -    left_rate_per_vp = total_left_rate / non_requested_count;
>>>>> +    left_rate_per_vp = total_left_rate / (non_requested_count ?: 1);
>>>>
>>>> I don't know if num_vports can be 1.
>>>> But if it is then I agree that the above will be a divide by zero.
>>>>
>>>> I do, however, wonder if it would be better to either:
>>>>
>>>> * Treat this case as invalid and return with -EINVAL if num_vports is
>>>> 1; or
>>> I think that's a good idea considering num_vports == 1 is indeed an
>>> invalid
>>> value.
>>> I'd like to hear a maintainer's opinion on this.
>> Practically, this flow will only hit with presence of SR-IOV VFs. In
>> that case it's
>> always expected to have num_vports > 1.
>
> In that case, should we add a check and return with -EINVAL otherwise?
> Thank you!
>

Ping

>>>> * Skip both the calculation immediately above and the code
>>>>     in the if condition below, which is the only place where
>>>>     the calculated value is used, if num_vports is 1.
>>>>     I don't think the if clause makes much sense if num_vports is
>>>> one.left_rate_per_vp is also used below the if clause, it is assigned
>>>> to
>>> .min_speed in a for loop. Looking at that code division by 1 seems to
>>> make
>>> sense to me in this case.
>>>>
>>>>>        if (left_rate_per_vp <  min_pf_rate / QED_WFQ_UNIT) {
>>>>>            DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
>>>>>                   "Non WFQ configured vports rate [%d Mbps] is less
>>> than one
>>>>> percent of configured PF min rate[%d Mbps]\n",
>>>>> --
>>>>> 2.25.1
>>>>>

2023-03-07 17:56:59

by Manish Chopra

[permalink] [raw]
Subject: RE: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

> -----Original Message-----
> From: Daniil Tatianin <[email protected]>
> Sent: Friday, March 3, 2023 5:48 PM
> To: Manish Chopra <[email protected]>; Simon Horman
> <[email protected]>
> Cc: Ariel Elior <[email protected]>; David S. Miller
> <[email protected]>; Eric Dumazet <[email protected]>; Jakub
> Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Yuval Mintz
> <[email protected]>; [email protected]; linux-
> [email protected]
> Subject: Re: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible
> division by zero
>
> On 2/16/23 9:42 AM, Daniil Tatianin wrote:
> > On 2/16/23 12:20 AM, Manish Chopra wrote:
> >>> -----Original Message-----
> >>> From: Daniil Tatianin <[email protected]>
> >>> Sent: Tuesday, February 14, 2023 12:53 PM
> >>> To: Simon Horman <[email protected]>
> >>> Cc: Ariel Elior <[email protected]>; Manish Chopra
> >>> <[email protected]>; David S. Miller <[email protected]>; Eric
> >>> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>;
> >>> Paolo Abeni <[email protected]>; Yuval Mintz
> >>> <[email protected]>; [email protected];
> >>> [email protected]
> >>> Subject: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible
> >>> division by zero
> >>>
> >>> External Email
> >>>
> >>> --------------------------------------------------------------------
> >>> --
> >>>
> >>>
> >>> On 2/9/23 2:13 PM, Simon Horman wrote:
> >>>> On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking
> >>>>> for zero when doing the division.
> >>>>>
> >>>>> 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 <[email protected]>
> >>>>> ---
> >>>>>    drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
> >>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
> >>>>>
> >>>>> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >>>>> b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >>>>> index d61cd32ec3b6..90927f68c459 100644
> >>>>> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >>>>> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
> >>>>> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct
> >>>>> qed_hwfn *p_hwfn,
> >>>>>
> >>>>>        total_left_rate    = min_pf_rate - total_req_min_rate;
> >>>>>
> >>>>> -    left_rate_per_vp = total_left_rate / non_requested_count;
> >>>>> +    left_rate_per_vp = total_left_rate / (non_requested_count ?:
> >>>>> +1);
> >>>>
> >>>> I don't know if num_vports can be 1.
> >>>> But if it is then I agree that the above will be a divide by zero.
> >>>>
> >>>> I do, however, wonder if it would be better to either:
> >>>>
> >>>> * Treat this case as invalid and return with -EINVAL if num_vports
> >>>> is 1; or
> >>> I think that's a good idea considering num_vports == 1 is indeed an
> >>> invalid value.
> >>> I'd like to hear a maintainer's opinion on this.
> >> Practically, this flow will only hit with presence of SR-IOV VFs. In
> >> that case it's always expected to have num_vports > 1.
> >
> > In that case, should we add a check and return with -EINVAL otherwise?
> > Thank you!
> >
>
> Ping
>
It should be fine, please add some log indicating "Unexpected num_vports" before returning error.

Thanks,
Manish

> >>>> * Skip both the calculation immediately above and the code
> >>>>     in the if condition below, which is the only place where
> >>>>     the calculated value is used, if num_vports is 1.
> >>>>     I don't think the if clause makes much sense if num_vports is
> >>>> one.left_rate_per_vp is also used below the if clause, it is
> >>>> assigned to
> >>> .min_speed in a for loop. Looking at that code division by 1 seems
> >>> to make sense to me in this case.
> >>>>
> >>>>>        if (left_rate_per_vp <  min_pf_rate / QED_WFQ_UNIT) {
> >>>>>            DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
> >>>>>                   "Non WFQ configured vports rate [%d Mbps] is
> >>>>> less
> >>> than one
> >>>>> percent of configured PF min rate[%d Mbps]\n",
> >>>>> --
> >>>>> 2.25.1
> >>>>>

2023-03-07 19:41:51

by Daniil Tatianin

[permalink] [raw]
Subject: Re: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible division by zero

On 3/7/23 8:50 PM, Manish Chopra wrote:
>> -----Original Message-----
>> From: Daniil Tatianin <[email protected]>
>> Sent: Friday, March 3, 2023 5:48 PM
>> To: Manish Chopra <[email protected]>; Simon Horman
>> <[email protected]>
>> Cc: Ariel Elior <[email protected]>; David S. Miller
>> <[email protected]>; Eric Dumazet <[email protected]>; Jakub
>> Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Yuval Mintz
>> <[email protected]>; [email protected]; linux-
>> [email protected]
>> Subject: Re: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible
>> division by zero
>>
>> On 2/16/23 9:42 AM, Daniil Tatianin wrote:
>>> On 2/16/23 12:20 AM, Manish Chopra wrote:
>>>>> -----Original Message-----
>>>>> From: Daniil Tatianin <[email protected]>
>>>>> Sent: Tuesday, February 14, 2023 12:53 PM
>>>>> To: Simon Horman <[email protected]>
>>>>> Cc: Ariel Elior <[email protected]>; Manish Chopra
>>>>> <[email protected]>; David S. Miller <[email protected]>; Eric
>>>>> Dumazet <[email protected]>; Jakub Kicinski <[email protected]>;
>>>>> Paolo Abeni <[email protected]>; Yuval Mintz
>>>>> <[email protected]>; [email protected];
>>>>> [email protected]
>>>>> Subject: [EXT] Re: [PATCH v0] qed/qed_dev: guard against a possible
>>>>> division by zero
>>>>>
>>>>> External Email
>>>>>
>>>>> --------------------------------------------------------------------
>>>>> --
>>>>>
>>>>>
>>>>> On 2/9/23 2:13 PM, Simon Horman wrote:
>>>>>> On Thu, Feb 09, 2023 at 01:38:13PM +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 explicitly checking
>>>>>>> for zero when doing the division.
>>>>>>>
>>>>>>> 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 <[email protected]>
>>>>>>> ---
>>>>>>>    drivers/net/ethernet/qlogic/qed/qed_dev.c | 2 +-
>>>>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>>>>
>>>>>>> diff --git a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>>>> b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>>>> index d61cd32ec3b6..90927f68c459 100644
>>>>>>> --- a/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>>>> +++ b/drivers/net/ethernet/qlogic/qed/qed_dev.c
>>>>>>> @@ -5123,7 +5123,7 @@ static int qed_init_wfq_param(struct
>>>>>>> qed_hwfn *p_hwfn,
>>>>>>>
>>>>>>>        total_left_rate    = min_pf_rate - total_req_min_rate;
>>>>>>>
>>>>>>> -    left_rate_per_vp = total_left_rate / non_requested_count;
>>>>>>> +    left_rate_per_vp = total_left_rate / (non_requested_count ?:
>>>>>>> +1);
>>>>>>
>>>>>> I don't know if num_vports can be 1.
>>>>>> But if it is then I agree that the above will be a divide by zero.
>>>>>>
>>>>>> I do, however, wonder if it would be better to either:
>>>>>>
>>>>>> * Treat this case as invalid and return with -EINVAL if num_vports
>>>>>> is 1; or
>>>>> I think that's a good idea considering num_vports == 1 is indeed an
>>>>> invalid value.
>>>>> I'd like to hear a maintainer's opinion on this.
>>>> Practically, this flow will only hit with presence of SR-IOV VFs. In
>>>> that case it's always expected to have num_vports > 1.
>>>
>>> In that case, should we add a check and return with -EINVAL otherwise?
>>> Thank you!
>>>
>>
>> Ping
>>
> It should be fine, please add some log indicating "Unexpected num_vports" before returning error.
>
> Thanks,
> Manish

Will do. Thank you!

>>>>>> * Skip both the calculation immediately above and the code
>>>>>>     in the if condition below, which is the only place where
>>>>>>     the calculated value is used, if num_vports is 1.
>>>>>>     I don't think the if clause makes much sense if num_vports is
>>>>>> one.left_rate_per_vp is also used below the if clause, it is
>>>>>> assigned to
>>>>> .min_speed in a for loop. Looking at that code division by 1 seems
>>>>> to make sense to me in this case.
>>>>>>
>>>>>>>        if (left_rate_per_vp <  min_pf_rate / QED_WFQ_UNIT) {
>>>>>>>            DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
>>>>>>>                   "Non WFQ configured vports rate [%d Mbps] is
>>>>>>> less
>>>>> than one
>>>>>>> percent of configured PF min rate[%d Mbps]\n",
>>>>>>> --
>>>>>>> 2.25.1
>>>>>>>