Skip to content

Commit dfbf289

Browse files
rostedttorvalds
authored andcommitted
bug: set warn variable before calling WARN()
This has hit me a couple of times already. I would be debugging code and the system would simply hang and then reboot. Finally, I found that the problem was caused by WARN_ON_ONCE() and friends. The macro WARN_ON_ONCE(condition) is defined as: static bool __section(.data.unlikely) __warned; int __ret_warn_once = !!(condition); if (unlikely(__ret_warn_once)) if (WARN_ON(!__warned)) __warned = true; unlikely(__ret_warn_once); Which looks great and all. But what I have hit, is an issue when WARN_ON() itself hits the same WARN_ON_ONCE() code. Because, the variable __warned is not yet set. Then it too calls WARN_ON() and that triggers the warning again. It keeps doing this until the stack is overflowed and the system crashes. By setting __warned first before calling WARN_ON() makes the original WARN_ON_ONCE() really only warn once, and not an infinite amount of times if the WARN_ON() also triggers the warning. Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c60f169 commit dfbf289

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

‎include/asm-generic/bug.h‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,32 @@ extern void warn_slowpath_null(const char *file, const int line);
110110
static bool __section(.data.unlikely) __warned; \
111111
int __ret_warn_once = !!(condition); \
112112
\
113-
if (unlikely(__ret_warn_once)) \
114-
if (WARN_ON(!__warned)) \
115-
__warned = true; \
113+
if (unlikely(__ret_warn_once && !__warned)) { \
114+
__warned = true; \
115+
WARN_ON(1); \
116+
} \
116117
unlikely(__ret_warn_once); \
117118
})
118119

119120
#define WARN_ONCE(condition, format...) ({ \
120121
static bool __section(.data.unlikely) __warned; \
121122
int __ret_warn_once = !!(condition); \
122123
\
123-
if (unlikely(__ret_warn_once)) \
124-
if (WARN(!__warned, format)) \
125-
__warned = true; \
124+
if (unlikely(__ret_warn_once && !__warned)) { \
125+
__warned = true; \
126+
WARN(1, format); \
127+
} \
126128
unlikely(__ret_warn_once); \
127129
})
128130

129131
#define WARN_TAINT_ONCE(condition, taint, format...) ({ \
130132
static bool __section(.data.unlikely) __warned; \
131133
int __ret_warn_once = !!(condition); \
132134
\
133-
if (unlikely(__ret_warn_once)) \
134-
if (WARN_TAINT(!__warned, taint, format)) \
135-
__warned = true; \
135+
if (unlikely(__ret_warn_once && !__warned)) { \
136+
__warned = true; \
137+
WARN_TAINT(1, taint, format); \
138+
} \
136139
unlikely(__ret_warn_once); \
137140
})
138141

0 commit comments

Comments
 (0)