diff mbox series

[v5] riscv: add userland instruction dump to RISC-V splats

Message ID 20230901123043.73700-1-cuiyunhui@bytedance.com (mailing list archive)
State Superseded
Headers show
Series [v5] riscv: add userland instruction dump to RISC-V splats | expand

Checks

Context Check Description
conchuod/cover_letter success Single patches do not need cover letters
conchuod/tree_selection success Guessed tree name to be for-next at HEAD 9a1d204f5c57
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 2 and now 2
conchuod/verify_signedoff success Signed-off-by tag matches author and committer
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 9 this patch: 9
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 9 this patch: 9
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 39 this patch: 39
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch success total: 0 errors, 0 warnings, 0 checks, 43 lines checked
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Yunhui Cui Sept. 1, 2023, 12:30 p.m. UTC
Add userland instruction dump and rename dump_kernel_instr()
to dump_instr().

An example:
[    0.822439] Freeing unused kernel image (initmem) memory: 6916K
[    0.823817] Run /init as init process
[    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
[    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
[    0.841373] Hardware name:  , BIOS
[    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
[    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
[    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
[    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
[    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
[    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
[    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
[    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
[    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
[    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
[    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
[    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
[    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
[    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004

Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
 arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

Comments

Yunhui Cui Sept. 7, 2023, 11:43 a.m. UTC | #1
Hi Björn,


On Fri, Sep 1, 2023 at 8:30 PM Yunhui Cui <cuiyunhui@bytedance.com> wrote:
>
> Add userland instruction dump and rename dump_kernel_instr()
> to dump_instr().
>
> An example:
> [    0.822439] Freeing unused kernel image (initmem) memory: 6916K
> [    0.823817] Run /init as init process
> [    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
> [    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
> [    0.841373] Hardware name:  , BIOS
> [    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
> [    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
> [    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
> [    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
> [    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
> [    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
> [    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
> [    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
> [    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
> [    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
> [    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
> [    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
> [    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
> [    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
>
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> ---
>  arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index f798c853bede..923b49c38985 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -33,7 +33,19 @@ int show_unhandled_signals = 1;
>
>  static DEFINE_SPINLOCK(die_lock);
>
> -static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
> +static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
> +{
> +       if (!user_mode(regs))
> +               return get_kernel_nofault(*val, insns);
> +
> +       /* The user space code from other tasks cannot be accessed. */
> +       if (regs != task_pt_regs(current))
> +               return -EPERM;
> +
> +       return copy_from_user_nofault(val, insns, sizeof(*val));
> +}
> +
> +static void dump_instr(const char *loglvl, struct pt_regs *regs)
>  {
>         char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
>         const u16 *insns = (u16 *)instruction_pointer(regs);
> @@ -42,7 +54,7 @@ static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
>         int i;
>
>         for (i = -10; i < 2; i++) {
> -               bad = get_kernel_nofault(val, &insns[i]);
> +               bad = copy_code(regs, &val, &insns[i]);
>                 if (!bad) {
>                         p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
>                 } else {
> @@ -71,7 +83,7 @@ void die(struct pt_regs *regs, const char *str)
>         print_modules();
>         if (regs) {
>                 show_regs(regs);
> -               dump_kernel_instr(KERN_EMERG, regs);
> +               dump_instr(KERN_EMERG, regs);
>         }
>
>         cause = regs ? regs->cause : -1;
> @@ -104,6 +116,7 @@ void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
>                 print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
>                 pr_cont("\n");
>                 __show_regs(regs);
> +               dump_instr(KERN_EMERG, regs);
>         }
>
>         force_sig_fault(signo, code, (void __user *)addr);
> --
> 2.20.1
>

This patch has been passed:
https://patchwork.kernel.org/project/linux-riscv/patch/20230901123043.73700-1-cuiyunhui@bytedance.com/

Could you help merge it to linux-next?

Thanks,
Yunhui
Björn Töpel Sept. 7, 2023, 6:45 p.m. UTC | #2
Yunhui Cui <cuiyunhui@bytedance.com> writes:

> Add userland instruction dump and rename dump_kernel_instr()
> to dump_instr().
>
> An example:
> [    0.822439] Freeing unused kernel image (initmem) memory: 6916K
> [    0.823817] Run /init as init process
> [    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
> [    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
> [    0.841373] Hardware name:  , BIOS
> [    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
> [    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
> [    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
> [    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
> [    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
> [    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
> [    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
> [    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
> [    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
> [    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
> [    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
> [    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
> [    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
> [    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
>
> Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> ---
>  arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> index f798c853bede..923b49c38985 100644
> --- a/arch/riscv/kernel/traps.c
> +++ b/arch/riscv/kernel/traps.c
> @@ -33,7 +33,19 @@ int show_unhandled_signals = 1;
>  
>  static DEFINE_SPINLOCK(die_lock);
>  
> -static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
> +static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
> +{
> +	if (!user_mode(regs))
> +		return get_kernel_nofault(*val, insns);
> +
> +	/* The user space code from other tasks cannot be accessed. */
> +	if (regs != task_pt_regs(current))
> +		return -EPERM;
> +
> +	return copy_from_user_nofault(val, insns, sizeof(*val));

Hmm, I think you missed the actual problem in [1]. I'm still getting:

  |   CHECK   arch/riscv/kernel/traps.c
  | arch/riscv/kernel/traps.c:46:44: warning: incorrect type in argument 2 (different address spaces)
  | arch/riscv/kernel/traps.c:46:44:    expected void const [noderef] __user *src
  | arch/riscv/kernel/traps.c:46:44:    got unsigned short const [usertype] *insns

This only moves the problem. You needs to cast the "insn" with something
like:

  | const void __user *uaddr =  (__force const void __user *)insn;

...and pass uaddr to copy_from_user_nofault().

  | long copy_from_user_nofault(void *dst, const void __user *src, size_t size);

Notice the "__user" tag to src.


Björn

[1] https://lore.kernel.org/linux-riscv/87msy6p8k3.fsf@all.your.base.are.belong.to.us/
Yunhui Cui Sept. 8, 2023, 1:15 p.m. UTC | #3
Hi Björn,

On Fri, Sep 8, 2023 at 2:45 AM Björn Töpel <bjorn@kernel.org> wrote:
>
> Yunhui Cui <cuiyunhui@bytedance.com> writes:
>
> > Add userland instruction dump and rename dump_kernel_instr()
> > to dump_instr().
> >
> > An example:
> > [    0.822439] Freeing unused kernel image (initmem) memory: 6916K
> > [    0.823817] Run /init as init process
> > [    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
> > [    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
> > [    0.841373] Hardware name:  , BIOS
> > [    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
> > [    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
> > [    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
> > [    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
> > [    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
> > [    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
> > [    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
> > [    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
> > [    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
> > [    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
> > [    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
> > [    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
> > [    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
> > [    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
> >
> > Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> > ---
> >  arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
> >  1 file changed, 16 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> > index f798c853bede..923b49c38985 100644
> > --- a/arch/riscv/kernel/traps.c
> > +++ b/arch/riscv/kernel/traps.c
> > @@ -33,7 +33,19 @@ int show_unhandled_signals = 1;
> >
> >  static DEFINE_SPINLOCK(die_lock);
> >
> > -static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
> > +static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
> > +{
> > +     if (!user_mode(regs))
> > +             return get_kernel_nofault(*val, insns);
> > +
> > +     /* The user space code from other tasks cannot be accessed. */
> > +     if (regs != task_pt_regs(current))
> > +             return -EPERM;
> > +
> > +     return copy_from_user_nofault(val, insns, sizeof(*val));
>
> Hmm, I think you missed the actual problem in [1]. I'm still getting:
>
>   |   CHECK   arch/riscv/kernel/traps.c
>   | arch/riscv/kernel/traps.c:46:44: warning: incorrect type in argument 2 (different address spaces)
>   | arch/riscv/kernel/traps.c:46:44:    expected void const [noderef] __user *src
>   | arch/riscv/kernel/traps.c:46:44:    got unsigned short const [usertype] *insns
>

How did the warnings above come about? I don't have one locally. What
is your risc-v gcc version?

This is mine:

/opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/opt/riscv/bin/riscv64-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/riscv/libexec/gcc/riscv64-unknown-linux-gnu/12.2.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: .../riscv-gnu-toolchain/gcc/configure
--target=riscv64-unknown-linux-gnu --prefix=/opt/riscv
--with-sysroot=/opt/riscv/sysroot --with-pkgversion=g2ee5e430018
--with-system-zlib --enable-shared --enable-tls
--enable-languages=c,c++,fortran --disable-libmudflap --disable-libssp
--disable-libquadmath --disable-libsanitizer --disable-nls
--disable-bootstrap --src=.../riscv_acpi/riscv-gnu-toolchain/gcc
--disable-multilib --with-abi=lp64d --with-arch=rv64imafdc
--with-tune=rocket --with-isa-spec=2.2 'CFLAGS_FOR_TARGET=-O2
-mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2   -mcmodel=medlow'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (g2ee5e430018)

Thanks,
Yunhui
Björn Töpel Sept. 8, 2023, 6 p.m. UTC | #4
yunhui cui <cuiyunhui@bytedance.com> writes:

> Hi Björn,
>
> On Fri, Sep 8, 2023 at 2:45 AM Björn Töpel <bjorn@kernel.org> wrote:
>>
>> Yunhui Cui <cuiyunhui@bytedance.com> writes:
>>
>> > Add userland instruction dump and rename dump_kernel_instr()
>> > to dump_instr().
>> >
>> > An example:
>> > [    0.822439] Freeing unused kernel image (initmem) memory: 6916K
>> > [    0.823817] Run /init as init process
>> > [    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
>> > [    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
>> > [    0.841373] Hardware name:  , BIOS
>> > [    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
>> > [    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
>> > [    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
>> > [    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
>> > [    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
>> > [    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
>> > [    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
>> > [    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
>> > [    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
>> > [    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
>> > [    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
>> > [    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
>> > [    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
>> > [    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
>> >
>> > Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
>> > ---
>> >  arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
>> >  1 file changed, 16 insertions(+), 3 deletions(-)
>> >
>> > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
>> > index f798c853bede..923b49c38985 100644
>> > --- a/arch/riscv/kernel/traps.c
>> > +++ b/arch/riscv/kernel/traps.c
>> > @@ -33,7 +33,19 @@ int show_unhandled_signals = 1;
>> >
>> >  static DEFINE_SPINLOCK(die_lock);
>> >
>> > -static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
>> > +static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
>> > +{
>> > +     if (!user_mode(regs))
>> > +             return get_kernel_nofault(*val, insns);
>> > +
>> > +     /* The user space code from other tasks cannot be accessed. */
>> > +     if (regs != task_pt_regs(current))
>> > +             return -EPERM;
>> > +
>> > +     return copy_from_user_nofault(val, insns, sizeof(*val));
>>
>> Hmm, I think you missed the actual problem in [1]. I'm still getting:
>>
>>   |   CHECK   arch/riscv/kernel/traps.c
>>   | arch/riscv/kernel/traps.c:46:44: warning: incorrect type in argument 2 (different address spaces)
>>   | arch/riscv/kernel/traps.c:46:44:    expected void const [noderef] __user *src
>>   | arch/riscv/kernel/traps.c:46:44:    got unsigned short const [usertype] *insns
>>
>
> How did the warnings above come about? I don't have one locally. What
> is your risc-v gcc version?

It's from the "sparse" tool. Pass "C=1" to make. Unfortunately RV sparse
needs to be built manually: https://github.com/ConchuOD/sparse

 | make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- C=1 W=1 arch/riscv/kernel/traps.o


Thanks!
Björn
Yunhui Cui Sept. 12, 2023, 2:13 a.m. UTC | #5
Hi Björn,

On Sat, Sep 9, 2023 at 2:01 AM Björn Töpel <bjorn@kernel.org> wrote:
>
> yunhui cui <cuiyunhui@bytedance.com> writes:
>
> > Hi Björn,
> >
> > On Fri, Sep 8, 2023 at 2:45 AM Björn Töpel <bjorn@kernel.org> wrote:
> >>
> >> Yunhui Cui <cuiyunhui@bytedance.com> writes:
> >>
> >> > Add userland instruction dump and rename dump_kernel_instr()
> >> > to dump_instr().
> >> >
> >> > An example:
> >> > [    0.822439] Freeing unused kernel image (initmem) memory: 6916K
> >> > [    0.823817] Run /init as init process
> >> > [    0.839411] init[1]: unhandled signal 4 code 0x1 at 0x000000000005be18 in bb[10000+5fb000]
> >> > [    0.840751] CPU: 0 PID: 1 Comm: init Not tainted 5.14.0-rc4-00049-gbd644290aa72-dirty #187
> >> > [    0.841373] Hardware name:  , BIOS
> >> > [    0.841743] epc : 000000000005be18 ra : 0000000000079e74 sp : 0000003fffcafda0
> >> > [    0.842271]  gp : ffffffff816e9dc8 tp : 0000000000000000 t0 : 0000000000000000
> >> > [    0.842947]  t1 : 0000003fffc9fdf0 t2 : 0000000000000000 s0 : 0000000000000000
> >> > [    0.843434]  s1 : 0000000000000000 a0 : 0000003fffca0190 a1 : 0000003fffcafe18
> >> > [    0.843891]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
> >> > [    0.844357]  a5 : 0000000000000000 a6 : 0000000000000000 a7 : 0000000000000000
> >> > [    0.844803]  s2 : 0000000000000000 s3 : 0000000000000000 s4 : 0000000000000000
> >> > [    0.845253]  s5 : 0000000000000000 s6 : 0000000000000000 s7 : 0000000000000000
> >> > [    0.845722]  s8 : 0000000000000000 s9 : 0000000000000000 s10: 0000000000000000
> >> > [    0.846180]  s11: 0000000000d144e0 t3 : 0000000000000000 t4 : 0000000000000000
> >> > [    0.846616]  t5 : 0000000000000000 t6 : 0000000000000000
> >> > [    0.847204] status: 0000000200000020 badaddr: 00000000f0028053 cause: 0000000000000002
> >> > [    0.848219] Code: f06f ff5f 3823 fa11 0113 fb01 2e23 0201 0293 0000 (8053) f002
> >> > [    0.851016] Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
> >> >
> >> > Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
> >> > ---
> >> >  arch/riscv/kernel/traps.c | 19 ++++++++++++++++---
> >> >  1 file changed, 16 insertions(+), 3 deletions(-)
> >> >
> >> > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
> >> > index f798c853bede..923b49c38985 100644
> >> > --- a/arch/riscv/kernel/traps.c
> >> > +++ b/arch/riscv/kernel/traps.c
> >> > @@ -33,7 +33,19 @@ int show_unhandled_signals = 1;
> >> >
> >> >  static DEFINE_SPINLOCK(die_lock);
> >> >
> >> > -static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
> >> > +static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
> >> > +{
> >> > +     if (!user_mode(regs))
> >> > +             return get_kernel_nofault(*val, insns);
> >> > +
> >> > +     /* The user space code from other tasks cannot be accessed. */
> >> > +     if (regs != task_pt_regs(current))
> >> > +             return -EPERM;
> >> > +
> >> > +     return copy_from_user_nofault(val, insns, sizeof(*val));
> >>
> >> Hmm, I think you missed the actual problem in [1]. I'm still getting:
> >>
> >>   |   CHECK   arch/riscv/kernel/traps.c
> >>   | arch/riscv/kernel/traps.c:46:44: warning: incorrect type in argument 2 (different address spaces)
> >>   | arch/riscv/kernel/traps.c:46:44:    expected void const [noderef] __user *src
> >>   | arch/riscv/kernel/traps.c:46:44:    got unsigned short const [usertype] *insns
> >>
> >
> > How did the warnings above come about? I don't have one locally. What
> > is your risc-v gcc version?
>
> It's from the "sparse" tool. Pass "C=1" to make. Unfortunately RV sparse
> needs to be built manually: https://github.com/ConchuOD/sparse
>
>  | make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- C=1 W=1 arch/riscv/kernel/traps.o

I used sparse to check locally and found that there were indeed
warnings. I'll post v6 to solve it.


Thanks,
Yunhui
diff mbox series

Patch

diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index f798c853bede..923b49c38985 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -33,7 +33,19 @@  int show_unhandled_signals = 1;
 
 static DEFINE_SPINLOCK(die_lock);
 
-static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
+static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
+{
+	if (!user_mode(regs))
+		return get_kernel_nofault(*val, insns);
+
+	/* The user space code from other tasks cannot be accessed. */
+	if (regs != task_pt_regs(current))
+		return -EPERM;
+
+	return copy_from_user_nofault(val, insns, sizeof(*val));
+}
+
+static void dump_instr(const char *loglvl, struct pt_regs *regs)
 {
 	char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
 	const u16 *insns = (u16 *)instruction_pointer(regs);
@@ -42,7 +54,7 @@  static void dump_kernel_instr(const char *loglvl, struct pt_regs *regs)
 	int i;
 
 	for (i = -10; i < 2; i++) {
-		bad = get_kernel_nofault(val, &insns[i]);
+		bad = copy_code(regs, &val, &insns[i]);
 		if (!bad) {
 			p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
 		} else {
@@ -71,7 +83,7 @@  void die(struct pt_regs *regs, const char *str)
 	print_modules();
 	if (regs) {
 		show_regs(regs);
-		dump_kernel_instr(KERN_EMERG, regs);
+		dump_instr(KERN_EMERG, regs);
 	}
 
 	cause = regs ? regs->cause : -1;
@@ -104,6 +116,7 @@  void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
 		print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
 		pr_cont("\n");
 		__show_regs(regs);
+		dump_instr(KERN_EMERG, regs);
 	}
 
 	force_sig_fault(signo, code, (void __user *)addr);