All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <error27@gmail.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: oe-kbuild@lists.linux.dev, Qingtao Cao <qingtao.cao.au@gmail.com>,
	lkp@intel.com, oe-kbuild-all@lists.linux.dev,
	linux-kernel@vger.kernel.org, Bartosz Golaszewski <brgl@bgdev.pl>
Subject: Re: drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
Date: Tue, 27 Dec 2022 20:46:13 +0300	[thread overview]
Message-ID: <Y6svZX++B5TYRHBT@kadam> (raw)
In-Reply-To: <C430F7E6-723B-4EA8-BA87-00C84676BD4F@gmail.com>

On Sun, Dec 25, 2022 at 12:50:46PM +0100, Andy Shevchenko wrote:
> 
> 
> Lähetetty iPhonesta
> 
> > Andy Shevchenko <andy.shevchenko@gmail.com> kirjoitti 25.12.2022 kello 12.45:
> > 
> > 
> > 
> > Lähetetty iPhonesta
> > 
> >>> Dan Carpenter <error27@gmail.com> kirjoitti 24.12.2022 kello 20.30:
> >>> 
> >>> On Sat, Dec 24, 2022 at 05:19:27PM +0100, Andy Shevchenko wrote:
> >>> 
> >>> 
> >>> Lähetetty iPhonesta
> >>> 
> >>>> Dan Carpenter <error27@gmail.com> kirjoitti 23.12.2022 kello 11.54:
> >>>> 
> >>>> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> >>>> head:   f9ff5644bcc04221bae56f922122f2b7f5d24d62
> >>>> commit: 5134272f9f3f71d4e1f3aa15cb09321af49b3646 gpio: exar: access MPIO registers on cascaded chips
> >>>> config: ia64-randconfig-m031-20221218
> >>>> compiler: ia64-linux-gcc (GCC) 12.1.0
> >>>> 
> >>>> If you fix the issue, kindly add following tag where applicable
> >>>> | Reported-by: kernel test robot <lkp@intel.com>
> >>>> | Reported-by: Dan Carpenter <error27@gmail.com>
> >>>> 
> >>>> smatch warnings:
> >>>> drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
> >>>> drivers/gpio/gpio-exar.c:62 exar_offset_to_lvl_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8'
> >>>> 
> >>> 
> >>> 
> >>> 
> >>> I don’t think this is a good advice. If we want to limit that, we need
> >>> to check also upper limit. But. The GPIO framework does that. So,
> >>> changing / to >= is bogus.
> >> 
> >> 
> >> How is checking pin / 8 not mathematically equivalent to pin >= 8?
> > 
> > The point is that semantically the / is better in case this code will ever support more than two banks of pins.
> 
> On top of that it’s paired with pin % 8.
> 

I noticed that, but it's a common bug though that a lot of people
accidentally write if (pin / 8) when if ((pin % 8) == 0) is intended.

For example:

drivers/rtc/rtc-m48t59.c
   132          M48T59_WRITE((bin2bcd(tm->tm_mon + 1) & 0x1F), M48T59_MONTH);
   133          M48T59_WRITE(bin2bcd(year % 100), M48T59_YEAR);
   134  
   135          if (pdata->type == M48T59RTC_TYPE_M48T59 && (year / 100))
                                                             ^^^^^^^^^^
This code is pretty clearly an example of where people accidentally uses
/ to mean "divides cleanly".  (I have not patched or reported this code,
btw so if anyone wants an easy patch to send it's available).

   136                  val = (M48T59_WDAY_CEB | M48T59_WDAY_CB);
   137          val |= (bin2bcd(tm->tm_wday) & 0x07);
   138          M48T59_WRITE(val, M48T59_WDAY);

regards,
dan carpenter


  reply	other threads:[~2022-12-27 17:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-18  3:33 drivers/gpio/gpio-exar.c:52 exar_offset_to_sel_addr() warn: replace divide condition 'pin / 8' with 'pin >= 8' kernel test robot
2022-12-23 10:54 ` Dan Carpenter
2022-12-24 16:19 ` Andy Shevchenko
2022-12-24 19:30   ` Dan Carpenter
2022-12-24 19:36     ` Dan Carpenter
2022-12-25 11:45     ` Andy Shevchenko
2022-12-25 11:50       ` Andy Shevchenko
2022-12-27 17:46         ` Dan Carpenter [this message]
2022-12-27 18:30           ` Andy Shevchenko
  -- strict thread matches above, loose matches on Subject: below --
2023-04-14 14:35 kernel test robot

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=Y6svZX++B5TYRHBT@kadam \
    --to=error27@gmail.com \
    --cc=andy.shevchenko@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@intel.com \
    --cc=oe-kbuild-all@lists.linux.dev \
    --cc=oe-kbuild@lists.linux.dev \
    --cc=qingtao.cao.au@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.