Re: [PATCH] staging: qlge: qlge_main.c: convert do-while loops to for loops

Dan Carpenter
2022-06-09T18:35:06Z

On Thu, Jun 09, 2022 at 01:01:27PM -0400, Srivathsan Sivakumar wrote:
> On Thu, Jun 09, 2022 at 06:26:53PM +0300, Dan Carpenter wrote:
> > On Thu, Jun 09, 2022 at 11:15:51AM -0400, Srivathsan Sivakumar wrote:
> > > diff --git a/drivers/staging/qlge/qlge_main.c b/drivers/staging/qlge/qlge_main.c
> > > index 8c35d4c4b851..308e8b621185 100644
> > > --- a/drivers/staging/qlge/qlge_main.c
> > > +++ b/drivers/staging/qlge/qlge_main.c
> > > @@ -3006,13 +3006,13 @@ static int qlge_start_rx_ring(struct qlge_adapter *qdev, struct rx_ring *rx_ring
> > > cqicb->flags |= FLAGS_LL; /* Load lbq values */
> > > tmp = (u64)rx_ring->lbq.base_dma;
> > > base_indirect_ptr = rx_ring->lbq.base_indirect;
> > > - page_entries = 0;
> > > - do {
> > > - *base_indirect_ptr = cpu_to_le64(tmp);
> > > - tmp += DB_PAGE_SIZE;
> > > - base_indirect_ptr++;
> > > - page_entries++;
> > > - } while (page_entries < MAX_DB_PAGES_PER_BQ(QLGE_BQ_LEN));
> > > +
> > > + for (page_entries = 0; page_entries <
> > > + MAX_DB_PAGES_PER_BQ(QLGE_BQ_LEN); page_entries++) {
> > > + *base_indirect_ptr = cpu_to_le64(tmp);
> > > + tmp += DB_PAGE_SIZE;
> > > + base_indirect_ptr++;
> > > + }
> >
> > It's better than the original, but wouldn't it be better yet to write
> > something like this (untested):
> >
> > for (i = 0; i < MAX_DB_PAGES_PER_BQ(QLGE_BQ_LEN); i++)
> > base_indirect_ptr[i] = cpu_to_le64(tmp + (i * DB_PAGE_SIZE));
> >
> > Same with the other as well, obviously.
> >
> > regards,
> > dan carpenter
> >
>
> Hello Dan,
>
> Thanks for your input
>
> wouldn't base_indirect_ptr point at a different endian value if tmp is
> added with (i * DB_PAGE_SIZE)?

tmp is cpu endian so we can do math on it. Then we convert the result
to le64. This is how it works before and after. What isn't allowed
(doesn't make sense) is to do math on endian data so "cpu_to_le64(tmp) +
i * DB_PAGE_SIZE" is wrong.

Sparse can detect endian bugs like that:
https://lwn.net/Articles/205624/

regards,
dan carpenter