Skip to content

Commit 17bce3b

Browse files
ubizjakingomolnar
authored andcommitted
x86/callthunks: Handle %rip-relative relocations in call thunk template
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. Reuse existing relocation infrastructure from alternative.c., as suggested by Peter Zijlstra. Signed-off-by: Uros Bizjak <ubizjak@gmail.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Link: https://lore.kernel.org/r/20231105213731.1878100-3-ubizjak@gmail.com
1 parent 43bda69 commit 17bce3b

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

arch/x86/include/asm/text-patching.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ static inline void apply_paravirt(struct paravirt_patch_site *start,
1818
#define __parainstructions_end NULL
1919
#endif
2020

21+
void apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len);
22+
2123
/*
2224
* Currently, the max observed size in the kernel code is
2325
* JUMP_LABEL_NOP_SIZE/RELATIVEJUMP_SIZE, which are 5.

arch/x86/kernel/alternative.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ bool need_reloc(unsigned long offset, u8 *src, size_t src_len)
325325
return (target < src || target > src + src_len);
326326
}
327327

328-
static void __init_or_module noinline
329-
apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len)
328+
void apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src, size_t src_len)
330329
{
331330
int prev, target = 0;
332331

arch/x86/kernel/callthunks.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
static int __initdata_or_module debug_callthunks;
2626

27+
#define MAX_PATCH_LEN (255-1)
28+
2729
#define prdbg(fmt, args...) \
2830
do { \
2931
if (debug_callthunks) \
@@ -184,10 +186,15 @@ static const u8 nops[] = {
184186
static void *patch_dest(void *dest, bool direct)
185187
{
186188
unsigned int tsize = SKL_TMPL_SIZE;
189+
u8 insn_buff[MAX_PATCH_LEN];
187190
u8 *pad = dest - tsize;
188191

192+
memcpy(insn_buff, skl_call_thunk_template, tsize);
193+
apply_relocation(insn_buff, tsize, pad,
194+
skl_call_thunk_template, tsize);
195+
189196
/* Already patched? */
190-
if (!bcmp(pad, skl_call_thunk_template, tsize))
197+
if (!bcmp(pad, insn_buff, tsize))
191198
return pad;
192199

193200
/* Ensure there are nops */
@@ -197,9 +204,9 @@ static void *patch_dest(void *dest, bool direct)
197204
}
198205

199206
if (direct)
200-
memcpy(pad, skl_call_thunk_template, tsize);
207+
memcpy(pad, insn_buff, tsize);
201208
else
202-
text_poke_copy_locked(pad, skl_call_thunk_template, tsize, true);
209+
text_poke_copy_locked(pad, insn_buff, tsize, true);
203210
return pad;
204211
}
205212

@@ -297,20 +304,27 @@ void *callthunks_translate_call_dest(void *dest)
297304
static bool is_callthunk(void *addr)
298305
{
299306
unsigned int tmpl_size = SKL_TMPL_SIZE;
300-
void *tmpl = skl_call_thunk_template;
307+
u8 insn_buff[MAX_PATCH_LEN];
301308
unsigned long dest;
309+
u8 *pad;
302310

303311
dest = roundup((unsigned long)addr, CONFIG_FUNCTION_ALIGNMENT);
304312
if (!thunks_initialized || skip_addr((void *)dest))
305313
return false;
306314

307-
return !bcmp((void *)(dest - tmpl_size), tmpl, tmpl_size);
315+
*pad = dest - tmpl_size;
316+
317+
memcpy(insn_buff, skl_call_thunk_template, tmpl_size);
318+
apply_relocation(insn_buff, tmpl_size, pad,
319+
skl_call_thunk_template, tmpl_size);
320+
321+
return !bcmp(pad, insn_buff, tmpl_size);
308322
}
309323

310324
int x86_call_depth_emit_accounting(u8 **pprog, void *func)
311325
{
312326
unsigned int tmpl_size = SKL_TMPL_SIZE;
313-
void *tmpl = skl_call_thunk_template;
327+
u8 insn_buff[MAX_PATCH_LEN];
314328

315329
if (!thunks_initialized)
316330
return 0;
@@ -319,7 +333,11 @@ int x86_call_depth_emit_accounting(u8 **pprog, void *func)
319333
if (func && is_callthunk(func))
320334
return 0;
321335

322-
memcpy(*pprog, tmpl, tmpl_size);
336+
memcpy(insn_buff, skl_call_thunk_template, tmpl_size);
337+
apply_relocation(insn_buff, tmpl_size, *pprog,
338+
skl_call_thunk_template, tmpl_size);
339+
340+
memcpy(*pprog, insn_buff, tmpl_size);
323341
*pprog += tmpl_size;
324342
return tmpl_size;
325343
}

0 commit comments

Comments
 (0)