Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/pkeys/selftests: Avoid printf-in-signal deadlocks

printf() and friends are unusable in signal handlers. They deadlock.
The pkey selftest does not do any normal printing in signal handlers,
only extra debugging. So, just print the format string so we get
*some* output when debugging.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michael Ellermen <mpe@ellerman.id.au>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ram Pai <linuxram@us.ibm.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-mm@kvack.org
Link: http://lkml.kernel.org/r/20180509171344.C53FD2F3@viggo.jf.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Dave Hansen and committed by
Ingo Molnar
caf9eb6b a50093d6

+8 -12
+8 -12
tools/testing/selftests/x86/pkey-helpers.h
··· 26 26 { 27 27 va_list ap; 28 28 29 - va_start(ap, format); 30 29 if (!dprint_in_signal) { 30 + va_start(ap, format); 31 31 vprintf(format, ap); 32 + va_end(ap); 32 33 } else { 33 34 int ret; 34 - int len = vsnprintf(dprint_in_signal_buffer, 35 - DPRINT_IN_SIGNAL_BUF_SIZE, 36 - format, ap); 37 35 /* 38 - * len is amount that would have been printed, 39 - * but actual write is truncated at BUF_SIZE. 36 + * No printf() functions are signal-safe. 37 + * They deadlock easily. Write the format 38 + * string to get some output, even if 39 + * incomplete. 40 40 */ 41 - if (len > DPRINT_IN_SIGNAL_BUF_SIZE) 42 - len = DPRINT_IN_SIGNAL_BUF_SIZE; 43 - ret = write(1, dprint_in_signal_buffer, len); 41 + ret = write(1, format, strlen(format)); 44 42 if (ret < 0) 45 - abort(); 43 + exit(1); 46 44 } 47 - va_end(ap); 48 45 } 49 46 #define dprintf_level(level, args...) do { \ 50 47 if (level <= DEBUG_LEVEL) \ 51 48 sigsafe_printf(args); \ 52 - fflush(NULL); \ 53 49 } while (0) 54 50 #define dprintf0(args...) dprintf_level(0, args) 55 51 #define dprintf1(args...) dprintf_level(1, args)