From: Mark Rutland <mark.rutland@arm.com>
To: Joey Gouly <joey.gouly@arm.com>
Cc: linux-arm-kernel@lists.infradead.org, nd@arm.com,
andre.przywara@arm.com, catalin.marinas@arm.com,
vincenzo.frascino@arm.com, will@kernel.org
Subject: Re: [PATCH v1 2/3] arm64: alternative: patch alternatives in the vDSO
Date: Thu, 25 Aug 2022 14:19:32 +0100 [thread overview]
Message-ID: <Ywd25KlBOabxvWHs@FVFF77S0Q05N> (raw)
In-Reply-To: <20220825102025.53916-3-joey.gouly@arm.com>
On Thu, Aug 25, 2022 at 11:20:24AM +0100, Joey Gouly wrote:
> Make it possible to use alternatives in the vDSO, so that better
> implementations can be used if possible.
>
> Signed-off-by: Joey Gouly <joey.gouly@arm.com>
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
> Cc: Mark Rutland <mark.rutland@arm.com>
> ---
> arch/arm64/include/asm/vdso.h | 3 +++
> arch/arm64/kernel/alternative.c | 25 +++++++++++++++++++++++++
> arch/arm64/kernel/vdso.c | 3 ---
> arch/arm64/kernel/vdso/vdso.lds.S | 7 +++++++
> 4 files changed, 35 insertions(+), 3 deletions(-)
>
> diff --git a/arch/arm64/include/asm/vdso.h b/arch/arm64/include/asm/vdso.h
> index f99dcb94b438..b4ae32109932 100644
> --- a/arch/arm64/include/asm/vdso.h
> +++ b/arch/arm64/include/asm/vdso.h
> @@ -26,6 +26,9 @@
> (void *)(vdso_offset_##name - VDSO_LBASE + (unsigned long)(base)); \
> })
>
> +extern char vdso_start[], vdso_end[];
> +extern char vdso32_start[], vdso32_end[];
> +
> #endif /* !__ASSEMBLY__ */
>
> #endif /* __ASM_VDSO_H */
> diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c
> index 9bcaa5eacf16..a4036194a5cd 100644
> --- a/arch/arm64/kernel/alternative.c
> +++ b/arch/arm64/kernel/alternative.c
> @@ -10,11 +10,14 @@
>
> #include <linux/init.h>
> #include <linux/cpu.h>
> +#include <linux/elf.h>
> #include <asm/cacheflush.h>
> #include <asm/alternative.h>
> #include <asm/cpufeature.h>
> #include <asm/insn.h>
> +#include <asm/module.h>
> #include <asm/sections.h>
> +#include <asm/vdso.h>
> #include <linux/stop_machine.h>
>
> #define __ALT_PTR(a, f) ((void *)&(a)->f + (a)->f)
> @@ -192,6 +195,27 @@ static void __nocfi __apply_alternatives(struct alt_region *region, bool is_modu
> }
> }
>
> +void apply_alternatives_vdso(unsigned long *feature_mask)
> +{
> + struct alt_region region;
> + const struct elf64_hdr *hdr;
> + const struct elf64_shdr *shdr;
> + const struct elf64_shdr *alt;
> +
> + hdr = (struct elf64_hdr *)vdso_start;
> + shdr = (void *)hdr + hdr->e_shoff;
> + alt = find_section(hdr, shdr, ".altinstructions");
> + if (!alt)
> + return;
> +
> + region = (struct alt_region){
> + .begin = (void *)hdr + alt->sh_offset,
> + .end = (void *)hdr + alt->sh_offset + alt->sh_size,
> + };
> +
> + __apply_alternatives(®ion, false, feature_mask);
> +}
> +
> /*
> * We might be patching the stop_machine state machine, so implement a
> * really simple polling protocol here.
> @@ -216,6 +240,7 @@ static int __apply_alternatives_multi_stop(void *unused)
>
> BUG_ON(all_alternatives_applied);
> __apply_alternatives(®ion, false, remaining_capabilities);
> + apply_alternatives_vdso(remaining_capabilities);
Since we didn't patch the vdso in apply_boot_alternatives(), using
`remaining_capabilities` means that we could in theory miss alternatives for
features which were detected on the boot CPU.
Since the VDSO cannot be concurrently executed within the kernel, we could
hoist the call to apply_alternatives_vdso() out of
__apply_alternatives_multi_stop(), and call it before the stop_machine in
apply_alternatives_all().
That would keep __apply_alternatives_multi_stop() simple (and easier to make
noinstr-safe), and we could use the same mask logic as
apply_alternatives_module(), e.g.
void apply_alternatives_vdso(void)
{
DECLARE_BITMAP(all_capabilities, ARM64_NPATCHABLE);
bitmap_fill(all_capabilities, ARM64_NPATCHABLE);
struct alt_region region;
const struct elf64_hdr *hdr;
const struct elf64_shdr *shdr;
const struct elf64_shdr *alt;
hdr = (struct elf64_hdr *)vdso_start;
shdr = (void *)hdr + hdr->e_shoff;
alt = find_section(hdr, shdr, ".altinstructions");
if (!alt)
return;
region = (struct alt_region){
.begin = (void *)hdr + alt->sh_offset,
.end = (void *)hdr + alt->sh_offset + alt->sh_size,
};
__apply_alternatives(®ion, false, &all_capabilities[0]);
}
... does that sound ok to you?
Thanks,
Mark.
> /* Barriers provided by the cache flushing */
> all_alternatives_applied = 1;
> }
> diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c
> index a61fc4f989b3..ac93a2ee9c07 100644
> --- a/arch/arm64/kernel/vdso.c
> +++ b/arch/arm64/kernel/vdso.c
> @@ -29,9 +29,6 @@
> #include <asm/signal32.h>
> #include <asm/vdso.h>
>
> -extern char vdso_start[], vdso_end[];
> -extern char vdso32_start[], vdso32_end[];
> -
> enum vdso_abi {
> VDSO_ABI_AA64,
> VDSO_ABI_AA32,
> diff --git a/arch/arm64/kernel/vdso/vdso.lds.S b/arch/arm64/kernel/vdso/vdso.lds.S
> index e69fb4aaaf3e..6028f1fe2d1c 100644
> --- a/arch/arm64/kernel/vdso/vdso.lds.S
> +++ b/arch/arm64/kernel/vdso/vdso.lds.S
> @@ -48,6 +48,13 @@ SECTIONS
> PROVIDE (_etext = .);
> PROVIDE (etext = .);
>
> + . = ALIGN(4);
> + .altinstructions : {
> + __alt_instructions = .;
> + *(.altinstructions)
> + __alt_instructions_end = .;
> + }
> +
> .dynamic : { *(.dynamic) } :text :dynamic
>
> .rela.dyn : ALIGN(8) { *(.rela .rela*) }
> --
> 2.17.1
>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-08-25 13:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-25 10:20 [PATCH v1 0/3] Use CNTVCTSS_EL0 in gettimeofday() Joey Gouly
2022-08-25 10:20 ` [PATCH v1 1/3] arm64: module: move find_section to header Joey Gouly
2022-08-25 13:19 ` Mark Rutland
2022-08-25 10:20 ` [PATCH v1 2/3] arm64: alternative: patch alternatives in the vDSO Joey Gouly
2022-08-25 13:19 ` Mark Rutland [this message]
2022-08-25 14:57 ` Joey Gouly
2022-08-25 10:20 ` [PATCH v1 3/3] arm64: vdso: use SYS_CNTVCTSS_EL0 for gettimeofday Joey Gouly
2022-08-25 13:24 ` Mark Rutland
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=Ywd25KlBOabxvWHs@FVFF77S0Q05N \
--to=mark.rutland@arm.com \
--cc=andre.przywara@arm.com \
--cc=catalin.marinas@arm.com \
--cc=joey.gouly@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=nd@arm.com \
--cc=vincenzo.frascino@arm.com \
--cc=will@kernel.org \
/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