Skip to contentSkip to conversation

dm: writecache: fix format string warning

PATCH In review Arnd Bergmann 4 participants 6 messages May 28, 2018 – Jun 4, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dm: writecache: fix format string warning

The return type of ACCESS_ONCE is configuration dependent and may be either 'int' or 'long int' for the writecache_has_error() macro, so we get a warning like this for either format string:

In file included from drivers/md/dm-writecache.c:8:
drivers/md/dm-writecache.c: In function 'writecache_status':
drivers/md/dm-writecache.c:2227:10: error: format '%ld' expects argument of type 'long int', but argument 4 has type 'int' [-Werror=format=]
DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),

          ^~~~~~~~~~~~~~~~~~~~

include/linux/device-mapper.h:549:46: note: in definition of macro 'DMEMIT'
0 : scnprintf(result + sz, maxlen - sz, x))
^

The code is otherwise correct, so we just need to shut up the warning, which can be done using an extra type cast.

Fixes bb15b431d650 ("dm: add writecache target")
---
 drivers/md/dm-writecache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-writecache.c +1 -1
index 1ef06e738eb6..772ac3a57287 100644
@@ -2224,7 +2224,7 @@ static void writecache_status(struct dm_target *ti, status_type_t type,
 
 	switch (type) {
 	case STATUSTYPE_INFO:
-		DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
+		DMEMIT("%ld %llu %llu %llu", (long)writecache_has_error(wc),
 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
 		       (unsigned long long)wc->writeback_size);
 		break;
Signature · 1 line
2.9.0
#1 lore raw
Quoted 13 lines · Arnd Bergmann

The return type of ACCESS_ONCE is configuration dependent and may be either 'int' or 'long int' for the writecache_has_error() macro, so we get a warning like this for either format string:

In file included from drivers/md/dm-writecache.c:8:
drivers/md/dm-writecache.c: In function 'writecache_status':
drivers/md/dm-writecache.c:2227:10: error: format '%ld' expects argument of type 'long int', but argument 4 has type 'int' [-Werror=format=]
DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),

          ^~~~~~~~~~~~~~~~~~~~

include/linux/device-mapper.h:549:46: note: in definition of macro 'DMEMIT'
0 : scnprintf(result + sz, maxlen - sz, x))
^

Quoted 5 lines

The code is otherwise correct, so we just need to shut up the warning, which can be done using an extra type cast.

Fixes bb15b431d650 ("dm: add writecache target")

Thanks, I've picked this up.

maintainer
#2 lore raw
In reply to Arnd Bergmann
Quoted 3 lines · Arnd Bergmann

The return type of ACCESS_ONCE is configuration dependent and may be either 'int' or 'long int' for the writecache_has_error() macro, so we get a warning like this for either format string:

__builtin_expect returns always long, see the GCC documentation (it used to return int in very old gcc versions such as 2.96).

I think this is a bug in the macro __branch_check__. The variable ______r should be long, but it is int. This bug may cause misbehavior of other kernel parts (i.e. truncation of long value to int), so it should be fixed in __branch_check__ - not in dm-writecache.

Mikulas

Quoted 34 lines

In file included from drivers/md/dm-writecache.c:8:
drivers/md/dm-writecache.c: In function 'writecache_status':
drivers/md/dm-writecache.c:2227:10: error: format '%ld' expects argument of type 'long int', but argument 4 has type 'int' [-Werror=format=]
DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),

          ^~~~~~~~~~~~~~~~~~~~

include/linux/device-mapper.h:549:46: note: in definition of macro 'DMEMIT'
0 : scnprintf(result + sz, maxlen - sz, x))
^

The code is otherwise correct, so we just need to shut up the warning, which can be done using an extra type cast.

Fixes bb15b431d650 ("dm: add writecache target")
---
 drivers/md/dm-writecache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-writecache.c +1 -1
index 1ef06e738eb6..772ac3a57287 100644
@@ -2224,7 +2224,7 @@ static void writecache_status(struct dm_target *ti, status_type_t type,
 
 	switch (type) {
 	case STATUSTYPE_INFO:
-		DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
+		DMEMIT("%ld %llu %llu %llu", (long)writecache_has_error(wc),
 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
 		       (unsigned long long)wc->writeback_size);
 		break;
Signature · 1 line
2.9.0
maintainer
#3 lore raw

PATCH 1/1 branch-check: fix long->int truncation when profiling branches

In reply to Arnd Bergmann

The function __builtin_expect returns long type (see the gcc documentation), and so do macros likely and unlikely. Unfortunatelly, when CONFIG_PROFILE_ANNOTATED_BRANCHES is selected, the macros likely and unlikely expand to __branch_check__ and __branch_check__ truncates the long type to int. This unintended truncation may cause bugs in various kernel code (we found a bug in dm-writecache because of it), so it's better to fix __branch_check__ to return long.

---
 include/linux/compiler.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/compiler.h +1 -1
Index: linux-2.6/include/linux/compiler.h
===================================================================
@@ -21,7 +21,7 @@ void ftrace_likely_update(struct ftrace_
 #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
 
 #define __branch_check__(x, expect, is_constant) ({			\
-			int ______r;					\
+			long ______r;					\
 			static struct ftrace_likely_data		\
 				__attribute__((__aligned__(4)))		\
 				__attribute__((section("_ftrace_annotated_branch"))) \
#4 lore raw

Re: [PATCH] branch-check: fix long->int truncation when profiling branches

Quoted 4 lines · On Wed, 30 May 2018 08:19:22 -0400 (EDT) Mikulas Patocka <[email protected]>…

The function __builtin_expect returns long type (see the gcc documentation), and so do macros likely and unlikely. Unfortunatelly, when CONFIG_PROFILE_ANNOTATED_BRANCHES is selected, the macros likely and unlikely expand to __branch_check__ and __branch_check__ truncates the

Nice catch.

Quoted 2 lines

long type to int. This unintended truncation may cause bugs in various kernel code (we found a bug in dm-writecache because of it), so it's

I'm curious to what that bug was.

Quoted 4 lines

better to fix __branch_check__ to return long.

Anyway, I can pull this in my tree and test it.

-- Steve

Quoted 18 lines
---
 include/linux/compiler.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: linux-2.6/include/linux/compiler.h
===================================================================

include/linux/compiler.h +1 -1
@@ -21,7 +21,7 @@ void ftrace_likely_update(struct ftrace_
 #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
 
 #define __branch_check__(x, expect, is_constant) ({			\
-			int ______r;					\
+			long ______r;					\
 			static struct ftrace_likely_data		\
 				__attribute__((__aligned__(4)))		\
 				__attribute__((section("_ftrace_annotated_branch"))) \
maintainer
#5 lore raw

Re: [PATCH] branch-check: fix long->int truncation when profiling branches

Quoted 8 lines · Steven Rostedt
Quoted 4 lines · On Wed, 30 May 2018 08:19:22 -0400 (EDT) Mikulas Patocka <[email protected]>…

The function __builtin_expect returns long type (see the gcc documentation), and so do macros likely and unlikely. Unfortunatelly, when CONFIG_PROFILE_ANNOTATED_BRANCHES is selected, the macros likely and unlikely expand to __branch_check__ and __branch_check__ truncates the

Quoted 6 lines

Nice catch.

Quoted 2 lines

long type to int. This unintended truncation may cause bugs in various kernel code (we found a bug in dm-writecache because of it), so it's

I'm curious to what that bug was.

printk("%ld", writecache_has_error(wc))

... and writecache_has_error was defined as
#define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))

Mikulas