[PATCH v2] x86: Avoid pr_cont() in show_opcodes()

From: Tetsuo Handa
Date: Sat Jul 07 2018 - 09:45:30 EST


Since syzbot is confused by concurrent printk() messages [1], this patch
changes show_opcodes() to use snprintf(). By this change, the Code: line
will always be printed as one line even if multiple threads concurrently
called printk().

To save on-stack footprint, this patch shares opcodes[] and buf[] because
we sequentially reads from opcodes[] and sequentially writes to buf[].

When we start adding prefix to each line of printk() output,
we will be able to handle concurrent printk() messages.

[1] https://syzkaller.appspot.com/text?tag=CrashReport&x=139d342c400000

Signed-off-by: Tetsuo Handa <penguin-kernel@xxxxxxxxxxxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
---
arch/x86/kernel/dumpstack.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/dumpstack.c b/arch/x86/kernel/dumpstack.c
index 666a284..6408123 100644
--- a/arch/x86/kernel/dumpstack.c
+++ b/arch/x86/kernel/dumpstack.c
@@ -94,25 +94,27 @@ static void printk_stack_address(unsigned long address, int reliable,
void show_opcodes(u8 *rip, const char *loglvl)
{
unsigned int code_prologue = OPCODE_BUFSIZE * 2 / 3;
- u8 opcodes[OPCODE_BUFSIZE];
u8 *ip;
int i;
-
- printk("%sCode: ", loglvl);
+ int pos = 0;
+ char buf[(3 * OPCODE_BUFSIZE + 2) + 1];
+ u8 *opcodes = (u8 *) buf + sizeof(buf) - OPCODE_BUFSIZE;

ip = (u8 *)rip - code_prologue;
if (probe_kernel_read(opcodes, ip, OPCODE_BUFSIZE)) {
- pr_cont("Bad RIP value.\n");
+ printk("%sCode: Bad RIP value.\n", loglvl);
return;
}

for (i = 0; i < OPCODE_BUFSIZE; i++, ip++) {
if (ip == rip)
- pr_cont("<%02x> ", opcodes[i]);
+ pos += snprintf(buf + pos, sizeof(buf) - pos,
+ "<%02x> ", opcodes[i]);
else
- pr_cont("%02x ", opcodes[i]);
+ pos += snprintf(buf + pos, sizeof(buf) - pos,
+ "%02x ", opcodes[i]);
}
- pr_cont("\n");
+ printk("%sCode: %s\n", loglvl, buf);
}

void show_ip(struct pt_regs *regs, const char *loglvl)
--
1.8.3.1