All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Himanshu Chauhan" <himanshu.chauhan@oss.qualcomm.com>,
	"Paul Walmsley" <pjw@kernel.org>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>
Cc: "Alexandre Ghiti" <alex@ghiti.fr>,
	"Jesse Taube" <jtaubepe@redhat.com>,
	"Oleg Nesterov" <oleg@redhat.com>, "Kees Cook" <kees@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>, "Ian Rogers" <irogers@google.com>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"James Clark" <james.clark@linaro.org>,
	"Shuah Khan" <shuah@kernel.org>,
	linux-mm@kvack.org, linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v8 6/8] riscv: Add shared instruction fetch and decode helpers
Date: Sun, 08 Nov 2026 02:41:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-203527@kernel.org> (raw)
In-Reply-To: <20261107023917.40606-7-himanshu.chauhan@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] riscv: missing s32 sign extension of offsets in
  get_next_insn_address()
- [Low] riscv: duplicate include of asm/insn.h in traps_misaligned.c
--

commit c0540231e3e0c755203ccef775001d61bb9b1fb4
Author: Himanshu Chauhan <himanshu.chauhan@oss.qualcomm.com>

riscv: Add shared instruction fetch and decode helpers

This commit moves get_insn() into arch/riscv/lib/insn.c and adds helpers to
fetch instructions and compute the next PC for control-flow instructions
during software single-stepping.

> diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c
> index 6e8ae6c66322c..541e6a5b80394 100644
> --- a/arch/riscv/kernel/traps_misaligned.c
> +++ b/arch/riscv/kernel/traps_misaligned.c
> @@ -10,6 +10,7 @@
>  #include <linux/irq.h>
>  #include <linux/stringify.h>
>  
> +#include <asm/insn.h>
>  #include <asm/processor.h>
>  #include <asm/ptrace.h>
>  #include <asm/csr.h>

[Severity: Low]
This isn't a bug, but arch/riscv/kernel/traps_misaligned.c already includes
asm/insn.h a few lines below.

[ ... ]

> diff --git a/arch/riscv/lib/insn.c b/arch/riscv/lib/insn.c
> new file mode 100644
> index 0000000000000..fc44e2aa6f5f0
> --- /dev/null
> +++ b/arch/riscv/lib/insn.c

[ ... ]

> +static unsigned long get_next_insn_address_compressed(struct pt_regs *regs, u32 insn,
> +						      unsigned long pc)
> +{
> +	unsigned int rs1_num;
> +
> +	if (riscv_insn_is_c_jalr(insn) || riscv_insn_is_c_jr(insn)) {
> +		rs1_num = RV_X(insn, RVC_C2_RS1_OPOFF, 5);
> +		return riscv_jalr_target(riscv_get_reg_value(regs, rs1_num), 0);
> +	}
> +
> +	if (riscv_insn_is_c_j(insn) || riscv_insn_is_c_jal(insn))
> +		return RVC_EXTRACT_JTYPE_IMM(insn) + pc;
> +
> +	if (riscv_insn_is_c_beqz(insn)) {
> +		rs1_num = RV_X(insn, RVC_C1_RS1_OPOFF, 3) + 8;
> +		if (!rs1_num || riscv_get_reg_value(regs, rs1_num) == 0)
> +			return RVC_EXTRACT_BTYPE_IMM(insn) + pc;
> +		return pc + 2;
> +	}
> +
> +	if (riscv_insn_is_c_bnez(insn)) {
> +		rs1_num = RV_X(insn, RVC_C1_RS1_OPOFF, 3) + 8;
> +		if (rs1_num && riscv_get_reg_value(regs, rs1_num) != 0)
> +			return RVC_EXTRACT_BTYPE_IMM(insn) + pc;
> +		return pc + 2;
> +	}
> +
> +	return pc + 2;
> +}

[ ... ]

> +static unsigned long get_next_insn_address_standard(struct pt_regs *regs, u32 insn,
> +						    unsigned long pc)
> +{
> +	unsigned int rs1_num;
> +
> +	if ((insn & __INSN_OPCODE_MASK) == __INSN_BRANCH_OPCODE)
> +		return riscv_branch_taken(regs, insn) ?
> +			RV_EXTRACT_BTYPE_IMM(insn) + pc : pc + 4;
> +
> +	if (riscv_insn_is_jal(insn))
> +		return RV_EXTRACT_JTYPE_IMM(insn) + pc;
> +
> +	if (riscv_insn_is_jalr(insn)) {
> +		rs1_num = RV_X(insn, RVG_RS1_OPOFF, 5);
> +		return riscv_jalr_target(riscv_get_reg_value(regs, rs1_num),
> +					 RV_EXTRACT_ITYPE_IMM(insn));
> +	}

[Severity: High]
On 64-bit RISC-V, do RVC_EXTRACT_*_IMM(insn) and RV_EXTRACT_*_IMM(insn)
zero-extend negative branch and jump offsets to 64 bits instead of
sign-extending them?

Because insn is u32, RV_IMM_SIGN(x_) and RVC_IMM_SIGN(x_) evaluate in 32-bit
unsigned arithmetic (producing a 32-bit unsigned mask such as 0xfffff000U),
and bitwise-ORing that with the GENMASK()-based unsigned long bitfields
zero-extends the result to 64-bit unsigned long. Any backward branch,
backward jump, or negative-offset JALR in get_next_insn_address_compressed()
and get_next_insn_address_standard() will miscompute the next PC by +4 GiB,
arming software single-step breakpoints at the wrong address.

Casting the extracted immediates to s32 before adding pc or passing the
offset to riscv_jalr_target() (as done in simulate-insn.c) would sign-extend
them properly.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261107023917.40606-1-himanshu.chauhan@oss.qualcomm.com?part=6

  reply	other threads:[~2026-11-08  2:41 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-07  2:39 [PATCH v8 0/8] riscv: Introduce support for hardware break/watchpoints Himanshu Chauhan
2026-11-07  2:39 ` Himanshu Chauhan
2026-11-07  2:39 ` [PATCH v8 1/8] " Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 2/8] riscv: Add breakpoint and watchpoint test for riscv Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 3/8] riscv: add ptrace requests for hardware breakpoints Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-07  7:04   ` kernel test robot
2026-11-07  7:04     ` kernel test robot
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 4/8] riscv: add regset access " Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 5/8] selftests/breakpoints: extend riscv test for ptrace hw break/watchpoints Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 6/8] riscv: Add shared instruction fetch and decode helpers Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot [this message]
2026-11-07  2:39 ` [PATCH v8 7/8] riscv: Add software supported single stepping with mc/mc6 triggers Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot
2026-11-07  2:39 ` [PATCH v8 8/8] perf tests: keep bp_signal memory accesses visible Himanshu Chauhan
2026-11-07  2:39   ` Himanshu Chauhan
2026-11-08  2:41   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-203527@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alex@ghiti.fr \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=himanshu.chauhan@oss.qualcomm.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=jtaubepe@redhat.com \
    --cc=kees@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=oleg@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.