Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Nam Cao <namcao@linutronix.de>
To: Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>,
	Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>
Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	yuanxiaofeng@eswincomputing.com
Subject: Re: [PATCH] riscv: probes: reject kprobes inside LR/SC sequences
Date: Wed, 19 Aug 2026 14:33:55 +0200	[thread overview]
Message-ID: <87tsoql26k.fsf@yellow.woof> (raw)
In-Reply-To: <20260819102922.1747-1-yuanxiaofeng@eswincomputing.com>

Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com> writes:
> +/*
> + * A trap taken in the middle of an LR/SC sequence clears the load
> + * reservation, so an SC following the probed instruction would always
> + * fail and the enclosing retry loop would re-enter the breakpoint.
> + * Reject probes inside such a sequence.
> + *
> + * A constrained LR/SC loop (Zalrsc) is limited to 16 instructions placed
> + * sequentially in memory (64 bytes in the base ISA), so scanning back
> + * that far covers every possible enclosing sequence.
> + */
> +#define MAX_ATOMIC_CONTEXT_SIZE	64

What about C extension? Does this 64 still work?

> +
> +static bool __kprobes riscv_probe_insn_in_atomic(unsigned long addr)
> +{
> +	unsigned long tmp, offset, scan_start;
> +	bool in_atomic = false;
> +
> +	if (!kallsyms_lookup_size_offset(addr, NULL, &offset))
> +		return false;
> +
> +	tmp = addr - offset;				/* function entry */
> +
> +	if (offset > MAX_ATOMIC_CONTEXT_SIZE)
> +		scan_start = addr - MAX_ATOMIC_CONTEXT_SIZE;

What guarantees that 'scan_start' points to the beginning of an instruction?

> +	else
> +		scan_start = tmp;
> +
> +	/* advance to the scan window, keeping instruction alignment */
> +	while (tmp < scan_start)
> +		tmp += GET_INSN_LENGTH(*(u16 *)tmp);
> +
> +	/* scan the window, tracking whether an LR is still outstanding */
> +	while (tmp < addr) {
> +		if (GET_INSN_LENGTH(*(u16 *)tmp) == 4) {

Why is tmp casted to (u16 *)? An instruction can be 32 bits.

It still works, I think, but looks confusing.

> +			u32 insn = *(u32 *)tmp;

Reuse this insn for the GET_INSN_LENGTH() above?

Nam

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

      reply	other threads:[~2026-08-19 12:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-19 10:29 [PATCH] riscv: probes: reject kprobes inside LR/SC sequences Xiaofeng Yuan
2026-08-19 12:33 ` Nam Cao [this message]

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=87tsoql26k.fsf@yellow.woof \
    --to=namcao@linutronix.de \
    --cc=aou@eecs.berkeley.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=yuanxiaofeng@eswincomputing.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox