All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
@ 2017-07-26 13:37 Arnd Bergmann
  2017-07-26 14:28 ` Mark Brown
  2017-07-26 21:38 ` Andrew Morton
  0 siblings, 2 replies; 6+ messages in thread
From: Arnd Bergmann @ 2017-07-26 13:37 UTC (permalink / raw)
  To: Andrew Morton, Mark Brown, Arnd Bergmann, Masahiro Yamada
  Cc: Charles Keepax, Tony Lindgren, Michael Grzeschik,
	Ramesh Shanmugasundaram, Alex A. Mihaylov, linux-kernel

When we pass the result of a multiplication as the timeout or
the delay, we can get a warning:

drivers/mmc/host/bcm2835.c:596:149: error: '*' in boolean context, suggest '&&' instead [-Werror=int-in-bool-context]
drivers/mfd/arizona-core.c:247:195: error: '*' in boolean context, suggest '&&' instead [-Werror=int-in-bool-context]
drivers/gpu/drm/sun4i/sun4i_hdmi_i2c.c:49:27: error: '*' in boolean context, suggest '&&' instead [-Werror=int-in-bool-context]

The warning is a bit questionable inside of a macro, but this
is intentional on the side of the gcc developers. It is also
an indication of another problem: we evaluate the timeout
and sleep arguments multiple times, which can have undesired
side-effects when those are complex expressions.

This changes the three iopoll variants to use local variables
for storing copies of the timeouts. This adds some more type
safety, and avoids both the double-evaluation and the gcc
warning.

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81484
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
v2: - use temporary variables instead of zero-comparison, to
      avoid double evaluation
    - also address the delay, not just timout handling
---
 include/linux/iopoll.h | 24 +++++++++++++++---------
 include/linux/regmap.h | 12 +++++++-----
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h
index d29e1e21bf3f..b1d861caca16 100644
--- a/include/linux/iopoll.h
+++ b/include/linux/iopoll.h
@@ -42,18 +42,21 @@
  */
 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)	\
 ({ \
-	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
-	might_sleep_if(sleep_us); \
+	u64 __timeout_us = (timeout_us); \
+	unsigned long __sleep_us = (sleep_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
+	might_sleep_if((__sleep_us) != 0); \
 	for (;;) { \
 		(val) = op(addr); \
 		if (cond) \
 			break; \
-		if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
+		if (__timeout_us && \
+		    ktime_compare(ktime_get(), __timeout) > 0) { \
 			(val) = op(addr); \
 			break; \
 		} \
-		if (sleep_us) \
-			usleep_range((sleep_us >> 2) + 1, sleep_us); \
+		if (__sleep_us) \
+			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
 	} \
 	(cond) ? 0 : -ETIMEDOUT; \
 })
@@ -77,17 +80,20 @@
  */
 #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
 ({ \
-	ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
+	u64 __timeout_us = (timeout_us); \
+	unsigned long __delay_us = (delay_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
 	for (;;) { \
 		(val) = op(addr); \
 		if (cond) \
 			break; \
-		if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
+		if (__timeout_us && \
+		    ktime_compare(ktime_get(), __timeout) > 0) { \
 			(val) = op(addr); \
 			break; \
 		} \
-		if (delay_us) \
-			udelay(delay_us);	\
+		if (__delay_us) \
+			udelay(__delay_us);	\
 	} \
 	(cond) ? 0 : -ETIMEDOUT; \
 })
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index 1474ab0a3922..a4d30c877f6b 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -120,22 +120,24 @@ struct reg_sequence {
  */
 #define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_us) \
 ({ \
-	ktime_t __timeout = ktime_add_us(ktime_get(), timeout_us); \
+	u64 __timeout_us = (timeout_us); \
+	unsigned long __sleep_us = (sleep_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
 	int __ret; \
-	might_sleep_if(sleep_us); \
+	might_sleep_if(__sleep_us); \
 	for (;;) { \
 		__ret = regmap_read((map), (addr), &(val)); \
 		if (__ret) \
 			break; \
 		if (cond) \
 			break; \
-		if ((timeout_us) && \
+		if (__timeout_us && \
 		    ktime_compare(ktime_get(), __timeout) > 0) { \
 			__ret = regmap_read((map), (addr), &(val)); \
 			break; \
 		} \
-		if (sleep_us) \
-			usleep_range(((sleep_us) >> 2) + 1, sleep_us); \
+		if (__sleep_us) \
+			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
 	} \
 	__ret ?: ((cond) ? 0 : -ETIMEDOUT); \
 })
-- 
2.9.0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
  2017-07-26 13:37 [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning Arnd Bergmann
@ 2017-07-26 14:28 ` Mark Brown
  2017-07-26 21:39   ` Andrew Morton
  2017-07-26 21:38 ` Andrew Morton
  1 sibling, 1 reply; 6+ messages in thread
From: Mark Brown @ 2017-07-26 14:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Andrew Morton, Masahiro Yamada, Charles Keepax, Tony Lindgren,
	Michael Grzeschik, Ramesh Shanmugasundaram, Alex A. Mihaylov,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 381 bytes --]

On Wed, Jul 26, 2017 at 03:37:32PM +0200, Arnd Bergmann wrote:
> When we pass the result of a multiplication as the timeout or
> the delay, we can get a warning:

This'd be easier if you sent each of the changes separately...

>  include/linux/iopoll.h | 24 +++++++++++++++---------
>  include/linux/regmap.h | 12 +++++++-----
>  2 files changed, 22 insertions(+), 14 deletions(-)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
  2017-07-26 13:37 [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning Arnd Bergmann
  2017-07-26 14:28 ` Mark Brown
@ 2017-07-26 21:38 ` Andrew Morton
  2017-07-26 21:41   ` Arnd Bergmann
  1 sibling, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2017-07-26 21:38 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Mark Brown, Masahiro Yamada, Charles Keepax, Tony Lindgren,
	Michael Grzeschik, Ramesh Shanmugasundaram, Alex A. Mihaylov,
	linux-kernel

On Wed, 26 Jul 2017 15:37:32 +0200 Arnd Bergmann <arnd@arndb.de> wrote:

> When we pass the result of a multiplication as the timeout or
> the delay, we can get a warning:

from?  gcc-7 I assume?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
  2017-07-26 14:28 ` Mark Brown
@ 2017-07-26 21:39   ` Andrew Morton
  2017-07-27  9:53     ` Mark Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2017-07-26 21:39 UTC (permalink / raw)
  To: Mark Brown
  Cc: Arnd Bergmann, Masahiro Yamada, Charles Keepax, Tony Lindgren,
	Michael Grzeschik, Ramesh Shanmugasundaram, Alex A. Mihaylov,
	linux-kernel

On Wed, 26 Jul 2017 15:28:06 +0100 Mark Brown <broonie@kernel.org> wrote:

> On Wed, Jul 26, 2017 at 03:37:32PM +0200, Arnd Bergmann wrote:
> > When we pass the result of a multiplication as the timeout or
> > the delay, we can get a warning:
> 
> This'd be easier if you sent each of the changes separately...

I think it's OK - the patch encapsulates a single concept.  Doesn't
matter much either way, unless someone wants to take one part only into
a particular tree(?).

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
  2017-07-26 21:38 ` Andrew Morton
@ 2017-07-26 21:41   ` Arnd Bergmann
  0 siblings, 0 replies; 6+ messages in thread
From: Arnd Bergmann @ 2017-07-26 21:41 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Mark Brown, Masahiro Yamada, Charles Keepax, Tony Lindgren,
	Michael Grzeschik, Ramesh Shanmugasundaram, Alex A. Mihaylov,
	Linux Kernel Mailing List

On Wed, Jul 26, 2017 at 11:38 PM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> On Wed, 26 Jul 2017 15:37:32 +0200 Arnd Bergmann <arnd@arndb.de> wrote:
>
>> When we pass the result of a multiplication as the timeout or
>> the delay, we can get a warning:
>
> from?  gcc-7 I assume?

Correct, sorry for missing this. In fact, the warning is currently disabled
on mainline kernels, but there are only a couple of instance remaining,
so I expect to be able to turn it back on in the future.

       Arnd

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning
  2017-07-26 21:39   ` Andrew Morton
@ 2017-07-27  9:53     ` Mark Brown
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Brown @ 2017-07-27  9:53 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Arnd Bergmann, Masahiro Yamada, Charles Keepax, Tony Lindgren,
	Michael Grzeschik, Ramesh Shanmugasundaram, Alex A. Mihaylov,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 633 bytes --]

On Wed, Jul 26, 2017 at 02:39:11PM -0700, Andrew Morton wrote:
> On Wed, 26 Jul 2017 15:28:06 +0100 Mark Brown <broonie@kernel.org> wrote:

> > On Wed, Jul 26, 2017 at 03:37:32PM +0200, Arnd Bergmann wrote:
> > > When we pass the result of a multiplication as the timeout or
> > > the delay, we can get a warning:

> > This'd be easier if you sent each of the changes separately...

> I think it's OK - the patch encapsulates a single concept.  Doesn't
> matter much either way, unless someone wants to take one part only into
> a particular tree(?).

I'd have applied the regmap bit if it weren't wrapped up with the iopoll
change.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-07-27  9:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-26 13:37 [PATCH] [v2] iopoll: avoid -Wint-in-bool-context warning Arnd Bergmann
2017-07-26 14:28 ` Mark Brown
2017-07-26 21:39   ` Andrew Morton
2017-07-27  9:53     ` Mark Brown
2017-07-26 21:38 ` Andrew Morton
2017-07-26 21:41   ` Arnd Bergmann

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.