2018-09-07 15:46:56

by Christophe Leroy

[permalink] [raw]
Subject: [PATCH v3 1/3] powerpc/process: fix casting and missing header

This patch fixes the following warnings. The first ones are leftovers
from when __get_user() was replaced by probe_kernel_address().

The last one is from when show_user_instructions() was added.

arch/powerpc/kernel/process.c:1287:22: warning: incorrect type in argument 2 (different address spaces)
arch/powerpc/kernel/process.c:1287:22: expected void const *src
arch/powerpc/kernel/process.c:1287:22: got unsigned int [noderef] <asn:1>*<noident>
arch/powerpc/kernel/process.c:1319:21: warning: incorrect type in argument 2 (different address spaces)
arch/powerpc/kernel/process.c:1319:21: expected void const *src
arch/powerpc/kernel/process.c:1319:21: got unsigned int [noderef] <asn:1>*<noident>
arch/powerpc/kernel/process.c:1302:6: warning: symbol 'show_user_instructions' was not declared. Should it be static?

Fixes: 7b051f665c32d ("powerpc: Use probe_kernel_address in show_instructions")
Fixes: 88b0fe1757359 ("powerpc: Add show_user_instructions()")
Signed-off-by: Christophe Leroy <[email protected]>
---
v3: new in v3 to fix sparse warnings reported by snowpatch on the serie

arch/powerpc/kernel/process.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 913c5725cdb2..e108e1ef2b85 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -65,6 +65,7 @@
#include <asm/livepatch.h>
#include <asm/cpu_has_feature.h>
#include <asm/asm-prototypes.h>
+#include <asm/stacktrace.h>

#include <linux/kprobes.h>
#include <linux/kdebug.h>
@@ -1284,7 +1285,7 @@ static void show_instructions(struct pt_regs *regs)
#endif

if (!__kernel_text_address(pc) ||
- probe_kernel_address((unsigned int __user *)pc, instr)) {
+ probe_kernel_address((const void *)pc, instr)) {
pr_cont("XXXXXXXX ");
} else {
if (regs->nip == pc)
@@ -1316,7 +1317,7 @@ void show_user_instructions(struct pt_regs *regs)
pr_info("%s[%d]: code: ", current->comm, current->pid);
}

- if (probe_kernel_address((unsigned int __user *)pc, instr)) {
+ if (probe_kernel_address((const void *)pc, instr)) {
pr_cont("XXXXXXXX ");
} else {
if (regs->nip == pc)
--
2.13.3



2018-09-07 15:46:25

by Christophe Leroy

[permalink] [raw]
Subject: [PATCH v3 2/3] powerpc/process: fix interleaved output in show_user_instructions()

When two processes crash at the same time, we sometimes encounter
interleaving in the middle of a line:

[ 4.365317] init[1]: segfault (11) at 0 nip 0 lr 0 code 1
[ 4.370452] init[1]: code: XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
[ 4.372042] init[74]: segfault (11) at 10a74 nip 1000c198 lr 100078c8 code 1 in sh[10000000+14000]
[ 4.386829] XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
[ 4.391542] init[1]: code: XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
[ 4.400863] init[74]: code: 90010024 bf61000c 91490a7c 3fa01002 3be00000 7d3e4b78 3bbd0c20 3b600000
[ 4.409867] init[74]: code: 3b9d0040 7c7fe02e 2f830000 419e0028 <89230000> 2f890000 41be001c 4b7f6e79

This patch fixes it by preparing complete lines in a buffer and
printing it at once.

Fixes: 88b0fe1757359 ("powerpc: Add show_user_instructions()")
Cc: Murilo Opsfelder Araujo <[email protected]>
Signed-off-by: Christophe Leroy <[email protected]>
---
v3: no change
v2: Using seq_buf and reworked the loop to avoid redundant prints.

arch/powerpc/kernel/process.c | 37 +++++++++++++++++++------------------
1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index e108e1ef2b85..2a39f7aca846 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -43,6 +43,7 @@
#include <linux/uaccess.h>
#include <linux/elf-randomize.h>
#include <linux/pkeys.h>
+#include <linux/seq_buf.h>

#include <asm/pgtable.h>
#include <asm/io.h>
@@ -1303,33 +1304,33 @@ static void show_instructions(struct pt_regs *regs)
void show_user_instructions(struct pt_regs *regs)
{
unsigned long pc;
- int i;
+ int n = instructions_to_print;
+ struct seq_buf s;
+ char buf[96]; /* enough for 8 times 9 + 2 chars */

pc = regs->nip - (instructions_to_print * 3 / 4 * sizeof(int));

- pr_info("%s[%d]: code: ", current->comm, current->pid);
+ seq_buf_init(&s, buf, sizeof(buf));

- for (i = 0; i < instructions_to_print; i++) {
- int instr;
+ while (n) {
+ int i;

- if (!(i % 8) && (i > 0)) {
- pr_cont("\n");
- pr_info("%s[%d]: code: ", current->comm, current->pid);
- }
+ seq_buf_clear(&s);

- if (probe_kernel_address((const void *)pc, instr)) {
- pr_cont("XXXXXXXX ");
- } else {
- if (regs->nip == pc)
- pr_cont("<%08x> ", instr);
- else
- pr_cont("%08x ", instr);
+ for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
+ int instr;
+
+ if (probe_kernel_address((const void *)pc, instr)) {
+ seq_buf_puts(&s, "XXXXXXXX ");
+ continue;
+ }
+ seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
}

- pc += sizeof(int);
+ if (!seq_buf_has_overflowed(&s))
+ pr_info("%s[%d]: code: %s\n", current->comm,
+ current->pid, s.buffer);
}
-
- pr_cont("\n");
}

struct regbit {
--
2.13.3


2018-09-07 15:46:50

by Christophe Leroy

[permalink] [raw]
Subject: [PATCH v3 3/3] powerpc/process: Constify the number of insns printed by show instructions functions.

instructions_to_print var is assigned value 16 and there is no
way to change it.

This patch replaces it by a constant.

Signed-off-by: Christophe Leroy <[email protected]>
---
v3: no change
v2: no change

arch/powerpc/kernel/process.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 2a39f7aca846..7d86b4f7949e 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -1261,17 +1261,16 @@ struct task_struct *__switch_to(struct task_struct *prev,
return last;
}

-static int instructions_to_print = 16;
+#define NR_INSN_TO_PRINT 16

static void show_instructions(struct pt_regs *regs)
{
int i;
- unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
- sizeof(int));
+ unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));

printk("Instruction dump:");

- for (i = 0; i < instructions_to_print; i++) {
+ for (i = 0; i < NR_INSN_TO_PRINT; i++) {
int instr;

if (!(i % 8))
@@ -1304,11 +1303,11 @@ static void show_instructions(struct pt_regs *regs)
void show_user_instructions(struct pt_regs *regs)
{
unsigned long pc;
- int n = instructions_to_print;
+ int n = NR_INSN_TO_PRINT;
struct seq_buf s;
char buf[96]; /* enough for 8 times 9 + 2 chars */

- pc = regs->nip - (instructions_to_print * 3 / 4 * sizeof(int));
+ pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));

seq_buf_init(&s, buf, sizeof(buf));

--
2.13.3


2018-09-21 20:25:37

by Murilo Opsfelder Araujo

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] powerpc/process: fix casting and missing header

On Fri, Sep 07, 2018 at 01:47:29PM +0000, Christophe Leroy wrote:
> This patch fixes the following warnings. The first ones are leftovers
> from when __get_user() was replaced by probe_kernel_address().
>
> The last one is from when show_user_instructions() was added.
>
> arch/powerpc/kernel/process.c:1287:22: warning: incorrect type in argument 2 (different address spaces)
> arch/powerpc/kernel/process.c:1287:22: expected void const *src
> arch/powerpc/kernel/process.c:1287:22: got unsigned int [noderef] <asn:1>*<noident>
> arch/powerpc/kernel/process.c:1319:21: warning: incorrect type in argument 2 (different address spaces)
> arch/powerpc/kernel/process.c:1319:21: expected void const *src
> arch/powerpc/kernel/process.c:1319:21: got unsigned int [noderef] <asn:1>*<noident>
> arch/powerpc/kernel/process.c:1302:6: warning: symbol 'show_user_instructions' was not declared. Should it be static?
>
> Fixes: 7b051f665c32d ("powerpc: Use probe_kernel_address in show_instructions")
> Fixes: 88b0fe1757359 ("powerpc: Add show_user_instructions()")
> Signed-off-by: Christophe Leroy <[email protected]>

Smoke test passed. Thank you.

Reviewed-by: Murilo Opsfelder Araujo <[email protected]>

> ---
> v3: new in v3 to fix sparse warnings reported by snowpatch on the serie
>
> arch/powerpc/kernel/process.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> index 913c5725cdb2..e108e1ef2b85 100644
> --- a/arch/powerpc/kernel/process.c
> +++ b/arch/powerpc/kernel/process.c
> @@ -65,6 +65,7 @@
> #include <asm/livepatch.h>
> #include <asm/cpu_has_feature.h>
> #include <asm/asm-prototypes.h>
> +#include <asm/stacktrace.h>
>
> #include <linux/kprobes.h>
> #include <linux/kdebug.h>
> @@ -1284,7 +1285,7 @@ static void show_instructions(struct pt_regs *regs)
> #endif
>
> if (!__kernel_text_address(pc) ||
> - probe_kernel_address((unsigned int __user *)pc, instr)) {
> + probe_kernel_address((const void *)pc, instr)) {
> pr_cont("XXXXXXXX ");
> } else {
> if (regs->nip == pc)
> @@ -1316,7 +1317,7 @@ void show_user_instructions(struct pt_regs *regs)
> pr_info("%s[%d]: code: ", current->comm, current->pid);
> }
>
> - if (probe_kernel_address((unsigned int __user *)pc, instr)) {
> + if (probe_kernel_address((const void *)pc, instr)) {
> pr_cont("XXXXXXXX ");
> } else {
> if (regs->nip == pc)
> --
> 2.13.3
>

--
Murilo


2018-09-21 20:27:10

by Murilo Opsfelder Araujo

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] powerpc/process: fix interleaved output in show_user_instructions()

On Fri, Sep 07, 2018 at 01:47:31PM +0000, Christophe Leroy wrote:
> When two processes crash at the same time, we sometimes encounter
> interleaving in the middle of a line:
>
> [ 4.365317] init[1]: segfault (11) at 0 nip 0 lr 0 code 1
> [ 4.370452] init[1]: code: XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
> [ 4.372042] init[74]: segfault (11) at 10a74 nip 1000c198 lr 100078c8 code 1 in sh[10000000+14000]
> [ 4.386829] XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
> [ 4.391542] init[1]: code: XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
> [ 4.400863] init[74]: code: 90010024 bf61000c 91490a7c 3fa01002 3be00000 7d3e4b78 3bbd0c20 3b600000
> [ 4.409867] init[74]: code: 3b9d0040 7c7fe02e 2f830000 419e0028 <89230000> 2f890000 41be001c 4b7f6e79
>
> This patch fixes it by preparing complete lines in a buffer and
> printing it at once.
>
> Fixes: 88b0fe1757359 ("powerpc: Add show_user_instructions()")
> Cc: Murilo Opsfelder Araujo <[email protected]>
> Signed-off-by: Christophe Leroy <[email protected]>

Reviewed-by: Murilo Opsfelder Araujo <[email protected]>

> ---
> v3: no change
> v2: Using seq_buf and reworked the loop to avoid redundant prints.
>
> arch/powerpc/kernel/process.c | 37 +++++++++++++++++++------------------
> 1 file changed, 19 insertions(+), 18 deletions(-)
>
> diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> index e108e1ef2b85..2a39f7aca846 100644
> --- a/arch/powerpc/kernel/process.c
> +++ b/arch/powerpc/kernel/process.c
> @@ -43,6 +43,7 @@
> #include <linux/uaccess.h>
> #include <linux/elf-randomize.h>
> #include <linux/pkeys.h>
> +#include <linux/seq_buf.h>
>
> #include <asm/pgtable.h>
> #include <asm/io.h>
> @@ -1303,33 +1304,33 @@ static void show_instructions(struct pt_regs *regs)
> void show_user_instructions(struct pt_regs *regs)
> {
> unsigned long pc;
> - int i;
> + int n = instructions_to_print;
> + struct seq_buf s;
> + char buf[96]; /* enough for 8 times 9 + 2 chars */
>
> pc = regs->nip - (instructions_to_print * 3 / 4 * sizeof(int));
>
> - pr_info("%s[%d]: code: ", current->comm, current->pid);
> + seq_buf_init(&s, buf, sizeof(buf));
>
> - for (i = 0; i < instructions_to_print; i++) {
> - int instr;
> + while (n) {
> + int i;
>
> - if (!(i % 8) && (i > 0)) {
> - pr_cont("\n");
> - pr_info("%s[%d]: code: ", current->comm, current->pid);
> - }
> + seq_buf_clear(&s);
>
> - if (probe_kernel_address((const void *)pc, instr)) {
> - pr_cont("XXXXXXXX ");
> - } else {
> - if (regs->nip == pc)
> - pr_cont("<%08x> ", instr);
> - else
> - pr_cont("%08x ", instr);
> + for (i = 0; i < 8 && n; i++, n--, pc += sizeof(int)) {
> + int instr;
> +
> + if (probe_kernel_address((const void *)pc, instr)) {
> + seq_buf_puts(&s, "XXXXXXXX ");
> + continue;
> + }
> + seq_buf_printf(&s, regs->nip == pc ? "<%08x> " : "%08x ", instr);
> }
>
> - pc += sizeof(int);
> + if (!seq_buf_has_overflowed(&s))
> + pr_info("%s[%d]: code: %s\n", current->comm,
> + current->pid, s.buffer);
> }
> -
> - pr_cont("\n");
> }
>
> struct regbit {
> --
> 2.13.3
>

--
Murilo


2018-09-21 20:28:04

by Murilo Opsfelder Araujo

[permalink] [raw]
Subject: Re: [PATCH v3 3/3] powerpc/process: Constify the number of insns printed by show instructions functions.

On Fri, Sep 07, 2018 at 01:47:33PM +0000, Christophe Leroy wrote:
> instructions_to_print var is assigned value 16 and there is no
> way to change it.
>
> This patch replaces it by a constant.
>
> Signed-off-by: Christophe Leroy <[email protected]>

Reviewed-by: Murilo Opsfelder Araujo <[email protected]>

> ---
> v3: no change
> v2: no change
>
> arch/powerpc/kernel/process.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
> index 2a39f7aca846..7d86b4f7949e 100644
> --- a/arch/powerpc/kernel/process.c
> +++ b/arch/powerpc/kernel/process.c
> @@ -1261,17 +1261,16 @@ struct task_struct *__switch_to(struct task_struct *prev,
> return last;
> }
>
> -static int instructions_to_print = 16;
> +#define NR_INSN_TO_PRINT 16
>
> static void show_instructions(struct pt_regs *regs)
> {
> int i;
> - unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
> - sizeof(int));
> + unsigned long pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
>
> printk("Instruction dump:");
>
> - for (i = 0; i < instructions_to_print; i++) {
> + for (i = 0; i < NR_INSN_TO_PRINT; i++) {
> int instr;
>
> if (!(i % 8))
> @@ -1304,11 +1303,11 @@ static void show_instructions(struct pt_regs *regs)
> void show_user_instructions(struct pt_regs *regs)
> {
> unsigned long pc;
> - int n = instructions_to_print;
> + int n = NR_INSN_TO_PRINT;
> struct seq_buf s;
> char buf[96]; /* enough for 8 times 9 + 2 chars */
>
> - pc = regs->nip - (instructions_to_print * 3 / 4 * sizeof(int));
> + pc = regs->nip - (NR_INSN_TO_PRINT * 3 / 4 * sizeof(int));
>
> seq_buf_init(&s, buf, sizeof(buf));
>
> --
> 2.13.3
>

--
Murilo