From: Mark Rutland <mark.rutland@arm.com>
To: Guo Ren <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 V6 1/7] riscv: ftrace: Fixup panic by disabling preemption
Date: Thu, 12 Jan 2023 12:05:26 +0000 [thread overview]
Message-ID: <Y7/3hoFjS49yy52W@FVFF77S0Q05N> (raw)
In-Reply-To: <CAJF2gTRzS0hBdqBUNbijvKKx3Kf_mY55XSkUyPJsfOK8p15_Mw@mail.gmail.com>
On Wed, Jan 11, 2023 at 09:22:09PM +0800, Guo Ren wrote:
> On Tue, Jan 10, 2023 at 1:20 AM Mark Rutland <mark.rutland@arm.com> wrote:
> >
> > On Sat, Jan 07, 2023 at 08:35:43AM -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
> >
> > What happens on SMP but not !PREEMPTION; can't a CPU be in the middle of this
> > while you're patching the sequence?
> Yes, when PREEMPTION, a timer interrupt between auipc & jalr may cause
> context_switch. And riscv uses stop_machine for patch_text. Then, we
> may modify auipc part, but only execute the jalr part when return.
Please re-read my question; "!PREEMPTION" means "NOT PREEMPTION".
Ignore preeemption entirely and assume two CPUs X and Y are running code
concurrently. Assume CPU X is in the ftrace prologue, and CPU Y is patching
that prologue while CPU X is executing it.
Is that prevented somehow? If not, what happens in that case?
At the very least you can have exactly the same case as on a preemptible kernel
(and in a VM, the hypervisor can preempt the guest ata arbitrary times),
becuase CPU X could end up executing a mixture of the old and new instructions.
More generally, if you don't have strong rules about concurrent modification
and execution of instructions, it may not be safe to modify and instruction as
it is being executed (e.g. if the CPU's instruction fetches aren't atomic).
> > Do you have any guarantee as to the atomicity and ordering of instruction
> > fetches?
> Not yet. If the region is short, we could use nop + jalr pair instead.
Ok, so as above I do not understand how this is safe. Maybe I am missing
something, but if you don't have a guarantee as to ordering I don't see how you
can safely patch this even if you have atomicity of each instruction update.
Note that if you don't have atomicity of instruction fetches you *cannot*
safely concurrently modify and execute instructions.
> Only one jalr instruction makes the entry atomicity.
I'll have to take your word for that.
As above, I don't think this sequence is safe regardless.
> There are already several proposed solutions:
> 1. Make stop_machine guarantee all CPU out of preemption point.
> 2. Expand -fpatchable-function-entry from 4 to 24, and make detour
> codes atomicity.
> 3. We want to propose a solution to make auipc by hardware mask_irq.
> For more details, see:
> https://www.youtube.com/watch?v=4JkkkXuEvCw
Ignoring things which require HW changes, you could consider doing something
like what I'm doing for arm64 with DYNAMIC_FTRACE_WITH_CALL_OPS:
https://lore.kernel.org/lkml/20230109135828.879136-1-mark.rutland@arm.com/
... which would replace the address generation with a load, which can be
atomic, and would give you a number of other benefits (e.g. avoiding branch
range limitations, performance benefits as in the cover letter).
Thanks,
Mark.
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2023-01-12 12:05 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-07 13:35 [PATCH -next V6 0/7] riscv: Optimize function trace guoren
2023-01-07 13:35 ` [PATCH -next V6 1/7] riscv: ftrace: Fixup panic by disabling preemption guoren
2023-01-09 17:19 ` Mark Rutland
2023-01-11 13:22 ` Guo Ren
2023-01-12 12:05 ` Mark Rutland [this message]
2023-01-28 10:00 ` Guo Ren
2023-01-29 5:36 ` Guo Ren
2023-01-30 11:17 ` Mark Rutland
2023-02-07 2:31 ` Guo Ren
2023-01-07 13:35 ` [PATCH -next V6 2/7] riscv: ftrace: Remove wasted nops for !RISCV_ISA_C guoren
2023-01-07 13:35 ` [PATCH -next V6 3/7] riscv: ftrace: Reduce the detour code size to half guoren
2023-01-10 17:13 ` Evgenii Shatokhin
2023-01-11 9:58 ` Guo Ren
2023-01-07 13:35 ` [PATCH -next V6 4/7] riscv: ftrace: Add ftrace_graph_func guoren
2023-01-07 13:35 ` [PATCH -next V6 5/7] riscv: ftrace: Make ftrace_caller call ftrace_graph_func guoren
2023-01-10 17:16 ` Evgenii Shatokhin
2023-01-11 8:23 ` Guo Ren
2023-01-11 8:41 ` Guo Ren
2023-01-07 13:35 ` [PATCH -next V6 6/7] riscv: ftrace: Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support guoren
2023-01-07 13:35 ` [PATCH -next V6 7/7] samples: ftrace: Add riscv support for SAMPLE_FTRACE_DIRECT[_MULTI] guoren
2023-01-10 13:08 ` Evgenii Shatokhin
2023-01-10 13:50 ` Evgenii Shatokhin
2023-01-11 9:50 ` Song Shuai
2023-01-11 9:59 ` Guo Ren
2023-01-13 10:48 ` Song Shuai
2023-01-17 3:20 ` Guo Ren
2023-01-11 10:11 ` [PATCH -next V6 0/7] riscv: Optimize function trace Song Shuai
2023-01-11 14:03 ` Guo Ren
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=Y7/3hoFjS49yy52W@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