Re: [PATCH -tip v2 2/3] x86/callthunks: Handle %rip-relative relocations in call thunk template

From: Nathan Chancellor
Date: Thu Nov 30 2023 - 22:55:04 EST


Hi Uros,

On Sun, Nov 05, 2023 at 10:34:36PM +0100, Uros Bizjak wrote:
> Contrary to alternatives, relocations are currently not supported in
> call thunk templates. Re-use the existing infrastructure from
> alternative.c to allow %rip-relative relocations when copying call
> thunk template from its storage location.
>
> The patch allows unification of ASM_INCREMENT_CALL_DEPTH, which already
> uses PER_CPU_VAR macro, with INCREMENT_CALL_DEPTH, used in call thunk
> template, which is currently limited to use absolute address.
>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Cc: Ingo Molnar <mingo@xxxxxxxxxx>
> Cc: Borislav Petkov <bp@xxxxxxxxx>
> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
...
> diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c
> index e9ad518a5003..ef9c04707b3c 100644
> --- a/arch/x86/kernel/callthunks.c
> +++ b/arch/x86/kernel/callthunks.c
...
> @@ -291,20 +298,27 @@ void *callthunks_translate_call_dest(void *dest)
> static bool is_callthunk(void *addr)
> {
> unsigned int tmpl_size = SKL_TMPL_SIZE;
> - void *tmpl = skl_call_thunk_template;
> + u8 insn_buff[MAX_PATCH_LEN];
> unsigned long dest;
> + u8 *pad;
>
> dest = roundup((unsigned long)addr, CONFIG_FUNCTION_ALIGNMENT);
> if (!thunks_initialized || skip_addr((void *)dest))
> return false;
>
> - return !bcmp((void *)(dest - tmpl_size), tmpl, tmpl_size);
> + *pad = dest - tmpl_size;

Clang warns (or errors with CONFIG_WERROR=y):

arch/x86/kernel/callthunks.c:315:3: error: variable 'pad' is uninitialized when used here [-Werror,-Wuninitialized]
315 | *pad = dest - tmpl_size;
| ^~~
arch/x86/kernel/callthunks.c:309:9: note: initialize the variable 'pad' to silence this warning
309 | u8 *pad;
| ^
| = NULL
1 error generated.

which came from our continuous integration:

https://github.com/ClangBuiltLinux/continuous-integration2/actions/runs/7054081453/job/19205345548
https://storage.tuxsuite.com/public/clangbuiltlinux/continuous-integration2/builds/2Yv1FATZZIeD3P7S57ZkHYhyZ8A/build.log

> +
> + memcpy(insn_buff, skl_call_thunk_template, tmpl_size);
> + apply_relocation(insn_buff, tmpl_size, pad,
> + skl_call_thunk_template, tmpl_size);
> +
> + return !bcmp(pad, insn_buff, tmpl_size);
> }

Cheers,
Nathan