From: Sergey Shtylyov <s.shtylyov@auroraos.dev>
To: Michal Pecio <michal.pecio@gmail.com>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
<linux-usb@vger.kernel.org>, <netdev@vger.kernel.org>
Subject: Re: [PATCH] r8152: simplify loops in generic_ocp_{read,write}()
Date: Mon, 24 Aug 2026 19:00:09 +0300 [thread overview]
Message-ID: <eff53efb-9838-418b-ac9d-f592a15ea76a@auroraos.dev> (raw)
In-Reply-To: <20260822230326.5d642593.michal.pecio@gmail.com>
On 8/23/26 12:03 AM, Michal Pecio wrote:
[...]
>> In generic_ocp_{read,write}(), the *while* loops look very strange:
>> the last iteration is executed differently to the prior ones, doing
>> some useless assignments before *break*. Move the code for the last
>> iteration out of the loop bodies, dropping the pointless statements
>> as well...
>>
>> Found by Linux Verification Center (linuxtesting.org) with the Svace
>> static analysis tool.
>>
>> Signed-off-by: Sergey Shtylyov <s.shtylyov@auroraos.dev>
>>
>> ---
>> drivers/net/usb/r8152.c | 67 ++++++++++++++++-------------------------
>> 1 file changed, 26 insertions(+), 41 deletions(-)
>>
>> diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
>> index f61686433031..de9738bdce85 100644
>> --- a/drivers/net/usb/r8152.c
>> +++ b/drivers/net/usb/r8152.c
>> @@ -1431,27 +1431,19 @@ static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
>> if ((u32)index + (u32)size > 0xffff)
>> return -EPERM;
>>
>> - while (size) {
>> - if (size > limit) {
>> - ret = get_registers(tp, index, type, limit, data);
>> - if (ret < 0)
>> - break;
>> -
>> - index += limit;
>> - data += limit;
>> - size -= limit;
>> - } else {
>> - ret = get_registers(tp, index, type, size, data);
>> - if (ret < 0)
>> - break;
>> + while (size > limit) {
>> + ret = get_registers(tp, index, type, limit, data);
>> + if (ret < 0)
>> + goto error1;
>>
>> - index += size;
>> - data += size;
>> - size = 0;
>> - break;
>> - }
>> + index += limit;
>> + data += limit;
>> + size -= limit;
>> }
>>
>> + ret = get_registers(tp, index, type, size, data);
>> +
>> +error1:
>> if (ret == -ENODEV)
>> rtl_set_unplug(tp);
>
> Looks like it could be shorter still.
>
> s/limit/chunk/
>
> while (size) {
> if (size < chunk)
> chunk = size;
> ret = get_registers(tp, index, type, chunk, data);
> if (ret < 0)
> break;
> index += chunk;
> data += chunk;
> size -= chunk;
> }
That's definitely better, thank you! :-)
I'll rewrite the patch along these lines, mentioning you in the
Suggested-by tag...
> Then it could be do-while, because we know size > 0, though
> I suppose compilers may figure it out themselves anyway.
I'd prefer to leave handling of !size as it is now, so I'll
keep using the *while* loop...
> Regards,
> Michal
MBR, Sergey
next prev parent reply other threads:[~2026-08-24 16:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-22 20:21 [PATCH] r8152: simplify loops in generic_ocp_{read,write}() Sergey Shtylyov
2026-08-22 20:26 ` Sergey Shtylyov
2026-08-22 20:58 ` Jakub Kicinski
2026-08-23 8:37 ` Sergey Shtylyov
2026-08-24 17:59 ` Jakub Kicinski
2026-08-22 21:03 ` Michal Pecio
2026-08-23 8:16 ` David Laight
2026-08-24 16:00 ` Sergey Shtylyov [this message]
2026-08-23 7:52 ` Sergey Shtylyov
2026-08-23 8:11 ` David Laight
2026-08-23 8:20 ` Sergey Shtylyov
2026-08-23 9:51 ` David Laight
2026-08-23 18:00 ` Sergey Shtylyov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eff53efb-9838-418b-ac9d-f592a15ea76a@auroraos.dev \
--to=s.shtylyov@auroraos.dev \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=michal.pecio@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).