linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mark Rutland <mark.rutland@arm.com>
To: guoren@kernel.org
Cc: anup@brainfault.org, paul.walmsley@sifive.com,
	palmer@dabbelt.com, conor.dooley@microchip.com, heiko@sntech.de,
	rostedt@goodmis.org, mhiramat@kernel.org, jolsa@redhat.com,
	bp@suse.de, jpoimboe@kernel.org, suagrfillet@gmail.com,
	andy.chiu@sifive.com, e.shatokhin@yadro.com,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH -next V7 1/7] riscv: ftrace: Fixup panic by disabling preemption
Date: Thu, 12 Jan 2023 12:57:27 +0000	[thread overview]
Message-ID: <Y8ADt98oowRZJdjw@FVFF77S0Q05N> (raw)
In-Reply-To: <Y7/6AtX5X0+5qF6Y@FVFF77S0Q05N>

On Thu, Jan 12, 2023 at 12:16:02PM +0000, Mark Rutland wrote:
> Hi Guo,
> 
> On Thu, Jan 12, 2023 at 04:05:57AM -0500, guoren@kernel.org wrote:
> > From: Andy Chiu <andy.chiu@sifive.com>
> > 
> > In RISCV, we must use an AUIPC + JALR pair to encode an immediate,
> > forming a jump that jumps to an address over 4K. This may cause errors
> > if we want to enable kernel preemption and remove dependency from
> > patching code with stop_machine(). For example, if a task was switched
> > out on auipc. And, if we changed the ftrace function before it was
> > switched back, then it would jump to an address that has updated 11:0
> > bits mixing with previous XLEN:12 part.
> > 
> > p: patched area performed by dynamic ftrace
> > ftrace_prologue:
> > p|      REG_S   ra, -SZREG(sp)
> > p|      auipc   ra, 0x? ------------> preempted
> > 					...
> > 				change ftrace function
> > 					...
> > p|      jalr    -?(ra) <------------- switched back
> > p|      REG_L   ra, -SZREG(sp)
> > func:
> > 	xxx
> > 	ret
> 
> As mentioned on the last posting, I don't think this is sufficient to fix the
> issue. I've replied with more detail there:
> 
>   https://lore.kernel.org/lkml/Y7%2F3hoFjS49yy52W@FVFF77S0Q05N/
> 
> Even in a non-preemptible SMP kernel, if one CPU can be in the middle of
> executing the ftrace_prologue while another CPU is patching the
> ftrace_prologue, you have the exact same issue.
> 
> For example, if CPU X is in the prologue fetches the old AUIPC and the new
> JALR (because it races with CPU Y modifying those), CPU X will branch to the
> wrong address. The race window is much smaller in the absence of preemption,
> but it's still there (and will be exacerbated in virtual machines since the
> hypervisor can preempt a vCPU at any time).

With that in mind, I think your current implementation of ftrace_make_call()
and ftrace_make_nop() have a simlar bug. A caller might execute:

	NOP	// not yet patched to AUIPC

				< AUIPC and JALR instructions both patched >

	JALR

... and go to the wrong place.

Assuming individual instruction fetches are atomic, and that you only ever
branch to the same trampoline, you could fix that by always leaving the AUIPC
in place, so that you only patch the JALR to enable/disable the callsite.

Depending on your calling convention, if you have two free GPRs, you might be
able to avoid the stacking of RA by always saving it to a GPR in the callsite,
using a different GPR for the address generation, and having the ftrace
trampoline restore the original RA value, e.g.

	MV	GPR1, ra
	AUIPC	GPR2, high_bits_of(ftrace_caller)
	JALR	ra, high_bits(GPR2)			// only patch this

... which'd save an instruction per callsite.

Thanks,
Mark.

  reply	other threads:[~2023-01-12 12:59 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-12  9:05 [PATCH -next V7 0/7] riscv: Optimize function trace guoren
2023-01-12  9:05 ` [PATCH -next V7 1/7] riscv: ftrace: Fixup panic by disabling preemption guoren
2023-01-12 12:16   ` Mark Rutland
2023-01-12 12:57     ` Mark Rutland [this message]
2023-01-28  9:45       ` Guo Ren
2023-01-28  9:37     ` Guo Ren
2023-01-30 10:54       ` Mark Rutland
2023-02-04  1:19         ` Guo Ren
2023-01-12  9:05 ` [PATCH -next V7 2/7] riscv: ftrace: Remove wasted nops for !RISCV_ISA_C guoren
2023-01-12  9:05 ` [PATCH -next V7 3/7] riscv: ftrace: Reduce the detour code size to half guoren
2023-01-16 14:11   ` Evgenii Shatokhin
2023-01-12  9:06 ` [PATCH -next V7 4/7] riscv: ftrace: Add ftrace_graph_func guoren
2023-01-12  9:06 ` [PATCH -next V7 5/7] riscv: ftrace: Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support guoren
2023-01-12  9:06 ` [PATCH -next V7 6/7] samples: ftrace: Add riscv support for SAMPLE_FTRACE_DIRECT[_MULTI] guoren
2023-01-16 14:30   ` Evgenii Shatokhin
2023-01-17  9:32     ` Song Shuai
2023-01-17 13:16       ` Evgenii Shatokhin
2023-01-17 16:22         ` Evgenii Shatokhin
2023-01-18  2:37           ` Song Shuai
2023-01-18 15:19             ` Evgenii Shatokhin
2023-01-19  6:05               ` Guo Ren
2023-02-18 21:30                 ` Palmer Dabbelt
2023-02-20  2:46                   ` Song Shuai
2023-02-21  3:56                     ` Guo Ren
2023-02-21  4:02                   ` Guo Ren
2023-01-12  9:06 ` [PATCH -next V7 7/7] riscv : select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY guoren
2023-01-16 15:02 ` [PATCH -next V7 0/7] riscv: Optimize function trace Evgenii Shatokhin
2023-02-04  6:40   ` Guo Ren
2023-02-06  9:56     ` Mark Rutland
2023-02-07  3:57       ` Guo Ren
2023-02-07  9:16         ` Mark Rutland
2023-02-08  2:30           ` Guo Ren
2023-02-08 14:46             ` Mark Rutland
2023-02-09  1:31               ` Guo Ren
2023-02-09 22:46                 ` David Laight
2023-02-10  2:18                   ` Guo Ren
2023-02-08 22:29             ` David Laight
2023-02-09  1:51               ` Guo Ren
2023-02-09  1:59                 ` Guo Ren
2023-02-09  9:54                   ` Mark Rutland
2023-02-10  2:21                     ` Guo Ren
2023-02-09  9:00                 ` David Laight
2023-02-09  9:11                   ` Guo Ren
2023-02-18 21:42 ` patchwork-bot+linux-riscv

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=Y8ADt98oowRZJdjw@FVFF77S0Q05N \
    --to=mark.rutland@arm.com \
    --cc=andy.chiu@sifive.com \
    --cc=anup@brainfault.org \
    --cc=bp@suse.de \
    --cc=conor.dooley@microchip.com \
    --cc=e.shatokhin@yadro.com \
    --cc=guoren@kernel.org \
    --cc=heiko@sntech.de \
    --cc=jolsa@redhat.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mhiramat@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=rostedt@goodmis.org \
    --cc=suagrfillet@gmail.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;
as well as URLs for NNTP newsgroup(s).