]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
regmap: avoid -Wint-in-bool-context warning
authorArnd Bergmann <arnd@arndb.de>
Fri, 13 Oct 2017 12:12:45 +0000 (14:12 +0200)
committerMark Brown <broonie@kernel.org>
Fri, 13 Oct 2017 17:22:11 +0000 (18:22 +0100)
When we pass the result of a multiplication as the timeout or the delay,
we can get a warning from gcc-7:

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 two regmap 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
Link: http://lkml.kernel.org/r/20170726133756.2161367-1-arnd@arndb.de
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Mark Brown <broonie@kernel.org>
include/linux/regmap.h

index 1e369f0e921daffd720f8c9e1aa7fe7a93260329..edad98890b9b69833bd331bf983171675da1d6be 100644 (file)
@@ -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); \
 })
@@ -160,21 +162,23 @@ struct reg_sequence {
  */
 #define regmap_field_read_poll_timeout(field, 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 pollret; \
-       might_sleep_if(sleep_us); \
+       might_sleep_if(__sleep_us); \
        for (;;) { \
                pollret = regmap_field_read((field), &(val)); \
                if (pollret) \
                        break; \
                if (cond) \
                        break; \
-               if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
+               if (__timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
                        pollret = regmap_field_read((field), &(val)); \
                        break; \
                } \
-               if (sleep_us) \
-                       usleep_range((sleep_us >> 2) + 1, sleep_us); \
+               if (__sleep_us) \
+                       usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
        } \
        pollret ?: ((cond) ? 0 : -ETIMEDOUT); \
 })