mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jinyang He <hejinyang@loongson.cn>
To: Tiezhu Yang <yangtiezhu@loongson.cn>,
	Huacai Chen <chenhuacai@kernel.org>
Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] LoongArch: Enhance robust of kprobe
Date: Wed, 9 Apr 2025 10:17:13 +0800	[thread overview]
Message-ID: <f83d1048-93f6-6c11-2c2a-98c1e1ea7e9d@loongson.cn> (raw)
In-Reply-To: <20250408092756.22339-1-yangtiezhu@loongson.cn>

On 2025-04-08 17:27, Tiezhu Yang wrote:

> Currently, interrupts need to be disabled before single-step mode is set,
> it requires that the CSR_PRMD_PIE must be cleared in save_local_irqflag()
> which is called by setup_singlestep(), this is reasonable.
>
> But in the first kprobe breakpoint exception, if the irq is enabled at the
> beginning of do_bp(), it will not be disabled at the end of do_bp() due to
> the CSR_PRMD_PIE has been cleared in save_local_irqflag(). For this case,
> it may corrupt exception context when restoring exception after do_bp() in
> handle_bp(), this is not reasonable.
>
> Based on the above analysis, in order to make sure the irq is disabled at
> the end of do_bp() for the first kprobe breakpoint exception, it is proper
> to disable irq first before clearing CSR_PRMD_PIE in save_local_irqflag().
>
> Fixes: 6d4cc40fb5f5 ("LoongArch: Add kprobes support")
> Co-developed-by: Tianyang Zhang <zhangtianyang@loongson.cn>
> Signed-off-by: Tianyang Zhang <zhangtianyang@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>   arch/loongarch/kernel/kprobes.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/arch/loongarch/kernel/kprobes.c b/arch/loongarch/kernel/kprobes.c
> index 8ba391cfabb0..6eab97636e6b 100644
> --- a/arch/loongarch/kernel/kprobes.c
> +++ b/arch/loongarch/kernel/kprobes.c
> @@ -113,6 +113,7 @@ NOKPROBE_SYMBOL(set_current_kprobe);
>   static void save_local_irqflag(struct kprobe_ctlblk *kcb,
>   			       struct pt_regs *regs)
>   {
> +	local_irq_disable();
>   	kcb->saved_status = regs->csr_prmd;
>   	regs->csr_prmd &= ~CSR_PRMD_PIE;
>   }

Hi, Tiezhu,

I think the carsh is caused by "irq-triggered re-re-enter" clear
the previous_kprobe status. An example things like,

...
   static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
                    struct kprobe_ctlblk *kcb, int reenter)
   {
       union loongarch_instruction insn;

       if (reenter) {
           save_previous_kprobe(kcb);
  ===================   <- irq and trigger re-re-enter in its handler
           set_current_kprobe(p);
           kcb->kprobe_status = KPROBE_REENTER;
       } else {
           kcb->kprobe_status = KPROBE_HIT_SS;
       }
...

We should assure the previous_kprobe status not be changed after re-enter.
So this `local_irq_disable` should be set in reenter block begin.
And for !reenter block, `local_irq_disable` may be not needed.

Jinyang


  reply	other threads:[~2025-04-09  2:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-08  9:27 Tiezhu Yang
2025-04-09  2:17 ` Jinyang He [this message]
2025-04-11  2:48   ` Tiezhu Yang
2025-04-11  4:46     ` Jinyang He
2025-04-11 11:37       ` Tiezhu Yang
2025-04-13  2:22         ` Huacai Chen

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=f83d1048-93f6-6c11-2c2a-98c1e1ea7e9d@loongson.cn \
    --to=hejinyang@loongson.cn \
    --cc=chenhuacai@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=yangtiezhu@loongson.cn \
    /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

all inboxes | Powered by JetHome®