From: "Clément Léger" <cleger@rivosinc.com>
To: "Maciej W. Rozycki" <macro@orcam.me.uk>
Cc: Alexandre Ghiti <alex@ghiti.fr>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
"open list:RISC-V ARCHITECTURE" <linux-riscv@lists.infradead.org>,
open list <linux-kernel@vger.kernel.org>,
Samuel Holland <samuel.holland@sifive.com>
Subject: Re: [PATCH] riscv: misaligned: disable pagefault before accessing user memory
Date: Tue, 7 Jan 2025 09:32:29 +0100 [thread overview]
Message-ID: <af6dde21-39de-4142-acd6-0f2db931b257@rivosinc.com> (raw)
In-Reply-To: <alpine.DEB.2.21.2501070143250.18889@angie.orcam.me.uk>
On 07/01/2025 03:33, Maciej W. Rozycki wrote:
> On Mon, 6 Jan 2025, Clément Léger wrote:
>
>>> I'm wondering why the irqs are disabled here? What prevents us from
>>> enabling them? IIUC, if for some reason the user page is not in memory,
>>> we will fail the misaligned access and kill the process, so such
>>> failures could be completely random right?
>>
>> Hi Alex,
>>
>> This code is called from interrupt context as a result from an
>> exception. AFAIK, interrupts are disabled during the whole exception
>> handling and re-enabled when returning to user mode in
>> do_notify_resume(). So that's why it triggers this warning.
>
> This feels outright wrong. We have analogous emulation code in Alpha and
> MIPS ports and we stay away from quick sands by enabling interrupts in the
> relevant exception handlers as soon as it is safe to do so; in particular
> when emulating user-mode unaligned accesses. What causes the RISC-V port
> to be different here?
Hi Maciej,
I probably did not clarify that enough. The current path for misaligned
accesses does not re-enable irqs (whatever the mode we enter from). Some
other path does (page_fault) as it's indeed safe.
>
> See e.g. the MIPS exception dispatcher in arch/mips/kernel/genex.S where
> BUILD_HANDLER invocations explicitly enable interrupts for most of the
> handlers (via `sti' -> `__build_clear_sti'). Then unaligned emulation is
> run with the interrupt enable setting preserved from the context the fault
> happened in (via `ade' -> `__build_clear_ade'), clearly because it can in
> principle trigger from kernel code run with interrupts disabled. But such
> code is not supposed to access user memory in the first place and traps
> from the user mode will have had interrupts enabled.
>
> I think the RISC-V port has to do the same, i.e. run the handler with the
> supervisor interrupt enable bit set according to whether interrupts were
> enabled or not for the mode the fault has triggered in.
It seems like as you say, it would actually be safe to re-enable
interrupts in the misaligned load/store path as well as
do_trap()/do_trap_break() which might also benefit from this improvement.
After reviewing, the code, there is also a bunch of irqentry_nmi_enter()
that seems a bit weird, maybe they could be replaced with irqentry_enter().
Thanks for the review,
Clément
>
> Maciej
next prev parent reply other threads:[~2025-01-07 8:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-03 16:02 Clément Léger
2025-01-06 14:37 ` Alexandre Ghiti
2025-01-06 14:47 ` Clément Léger
2025-01-07 2:33 ` Maciej W. Rozycki
2025-01-07 8:32 ` Clément Léger [this message]
2025-01-06 14:51 ` Clément Léger
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=af6dde21-39de-4142-acd6-0f2db931b257@rivosinc.com \
--to=cleger@rivosinc.com \
--cc=alex@ghiti.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=macro@orcam.me.uk \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=samuel.holland@sifive.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
all inboxes | Powered by JetHome®