From: Marc Zyngier <maz@kernel.org>
To: Ada Couprie Diaz <ada.coupriediaz@arm.com>
Cc: linux-arm-kernel@lists.infradead.org,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Oliver Upton <oliver.upton@linux.dev>,
Ard Biesheuvel <ardb@kernel.org>, Joey Gouly <joey.gouly@arm.com>,
Suzuki K Poulose <suzuki.poulose@arm.com>,
Zenghui Yu <yuzenghui@huawei.com>,
Andrey Ryabinin <ryabinin.a.a@gmail.com>,
Alexander Potapenko <glider@google.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
Dmitry Vyukov <dvyukov@google.com>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev,
kasan-dev@googlegroups.com, Mark Rutland <mark.rutland@arm.com>
Subject: Re: [RFC PATCH 12/16] kvm/arm64: make alternative callbacks safe
Date: Mon, 20 Oct 2025 18:05:03 +0100 [thread overview]
Message-ID: <86ikg9wlkw.wl-maz@kernel.org> (raw)
In-Reply-To: <20250923174903.76283-13-ada.coupriediaz@arm.com>
nit: please keep $SUBJECT in keeping with the subsystem you are
patching: "KVM: arm64: Make alternative callbacks safe"
On Tue, 23 Sep 2025 18:48:59 +0100,
Ada Couprie Diaz <ada.coupriediaz@arm.com> wrote:
>
> Alternative callback functions are regular functions, which means they
> or any function they call could get patched or instrumented
> by alternatives or other parts of the kernel.
> Given that applying alternatives does not guarantee a consistent state
> while patching, only once done, and handles cache maintenance manually,
> it could lead to nasty corruptions and execution of bogus code.
>
> Make the KVM alternative callbacks safe by marking them `noinstr` and
> `__always_inline`'ing their helpers.
> This is possible thanks to previous commits making `aarch64_insn_...`
> functions used in the callbacks safe to inline.
>
> `kvm_update_va_mask()` is already marked as `__init`, which has its own
> text section conflicting with the `noinstr` one.
> Instead, use `__no_instr_section(".noinstr.text")` to add
> all the function attributes added by `noinstr`, without the section
> conflict.
> This can be an issue, as kprobes seems to only block the text sections,
> not based on function attributes.
>
> Signed-off-by: Ada Couprie Diaz <ada.coupriediaz@arm.com>
> ---
> This is missing `kvm_patch_vector_branch()`, which could receive the same
> treatment, but the `WARN_ON_ONCE` in the early-exit check would make it
> call into instrumentable code.
> I do not currently know if this `WARN` can safely be removed or if it
> has some importance.
This is only debug code, which could be easily removed. However, I'd
like to suggest that we add a method to indicate that a patching
callback has failed for whatever reason. This doesn't have to be a
complex piece of infrastructure, and can be best effort (you can only
fail a single callback in a single location).
But it would certainly help catching unexpected situations (been
there, done that, ended up with visible scars...).
> ---
> arch/arm64/kvm/va_layout.c | 12 +++++++-----
> 1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/arch/arm64/kvm/va_layout.c b/arch/arm64/kvm/va_layout.c
> index 91b22a014610..3ebb7e0074f6 100644
> --- a/arch/arm64/kvm/va_layout.c
> +++ b/arch/arm64/kvm/va_layout.c
> @@ -109,7 +109,7 @@ __init void kvm_apply_hyp_relocations(void)
> }
> }
>
> -static u32 compute_instruction(int n, u32 rd, u32 rn)
> +static __always_inline u32 compute_instruction(int n, u32 rd, u32 rn)
> {
> u32 insn = AARCH64_BREAK_FAULT;
>
> @@ -151,6 +151,7 @@ static u32 compute_instruction(int n, u32 rd, u32 rn)
> return insn;
> }
>
> +__noinstr_section(".init.text")
> void __init kvm_update_va_mask(struct alt_instr *alt,
> __le32 *origptr, __le32 *updptr, int nr_inst)
> {
> @@ -241,7 +242,8 @@ void kvm_patch_vector_branch(struct alt_instr *alt,
> *updptr++ = cpu_to_le32(insn);
> }
>
> -static void generate_mov_q(u64 val, __le32 *origptr, __le32 *updptr, int nr_inst)
> +static __always_inline void generate_mov_q(u64 val, __le32 *origptr,
> + __le32 *updptr, int nr_inst)
> {
> u32 insn, oinsn, rd;
>
generate_mov_q() (and others) start with a BUG_ON(), which may not be
recoverable, just like WARN_ON() above. That's where we should be able
to fail (more or less) gracefully and at least indicate the failure.
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
next prev parent reply other threads:[~2025-10-20 17:05 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-23 17:48 [RFC PATCH 00/16] arm64: make alternative patching callbacks safe Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 01/16] kasan: mark kasan_(hw_)tags_enabled() __always_inline Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 02/16] arm64: kasan: make kasan_hw_tags_enable() callback safe Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 03/16] arm64/insn: always inline aarch64_insn_decode_register() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-10-20 16:39 ` Marc Zyngier
2025-09-23 17:48 ` [RFC PATCH 04/16] arm64/insn: always inline aarch64_insn_encode_register() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 05/16] arm64/insn: always inline aarch64_insn_encode_immediate() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 06/16] arm64/insn: always inline aarch64_insn_gen_movewide() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-10-20 16:48 ` Marc Zyngier
2025-09-23 17:48 ` [RFC PATCH 07/16] arm64/proton-pack: make alternative callbacks safe Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 08/16] arm64/insn: always inline aarch64_insn_gen_logical_immediate() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 09/16] arm64/insn: always inline aarch64_insn_gen_add_sub_imm() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 10/16] arm64/insn: always inline aarch64_insn_gen_branch_reg() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 11/16] arm64/insn: always inline aarch64_insn_gen_extr() Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-09-23 17:48 ` [RFC PATCH 12/16] kvm/arm64: make alternative callbacks safe Ada Couprie Diaz
2025-09-23 17:48 ` Ada Couprie Diaz
2025-10-20 17:05 ` Marc Zyngier [this message]
2025-09-23 17:49 ` [RFC PATCH 13/16] arm64/insn: introduce missing is_store/is_load helpers Ada Couprie Diaz
2025-09-23 17:49 ` Ada Couprie Diaz
2025-09-23 17:49 ` [RFC PATCH 14/16] arm64/insn: always inline aarch64_insn_encode_ldst_size() Ada Couprie Diaz
2025-09-23 17:49 ` Ada Couprie Diaz
2025-10-20 17:12 ` Marc Zyngier
2025-09-23 17:49 ` [RFC PATCH 15/16] arm64/insn: always inline aarch64_insn_gen_load_acq_store_rel() Ada Couprie Diaz
2025-09-23 17:49 ` Ada Couprie Diaz
2025-09-23 17:49 ` [RFC PATCH 16/16] arm64/io: rework Cortex-A57 erratum 832075 to use callback Ada Couprie Diaz
2025-09-23 17:49 ` Ada Couprie Diaz
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=86ikg9wlkw.wl-maz@kernel.org \
--to=maz@kernel.org \
--cc=ada.coupriediaz@arm.com \
--cc=andreyknvl@gmail.com \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dvyukov@google.com \
--cc=glider@google.com \
--cc=joey.gouly@arm.com \
--cc=kasan-dev@googlegroups.com \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=oliver.upton@linux.dev \
--cc=ryabinin.a.a@gmail.com \
--cc=suzuki.poulose@arm.com \
--cc=vincenzo.frascino@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.