Skip to content

Commit 44eebf9

Browse files
dylanbhatchpuranjaymohan
authored andcommitted
create-diff-object: Add support for arm64 DYNAMIC_FTRACE_WITH_CALL_OPS
For arm64 this option uses -fpatchable-function-entry=M,2, so 2 NOPs are placed before the function entry point (in order to store a pointer to ftrace_ops). When calculating function padding, check for the presence of the two NOPs, and adjust the padding size by 8 if they are found. This was merged in the upstream kernel in v6.8 with: baaf553d3bc3 ("arm64: Implement HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS") With this into the equation, the entry of a function can look like one of: 1. Without DYNAMIC_FTRACE_WITH_CALL_OPS and CONFIG_ARM64_BTI_KERNEL ------------------------------------------------------------------- Disassembly of section .text.cmdline_proc_show: 0000000000000008 <cmdline_proc_show>: 8: d503201f nop c: d503201f nop 2. Without DYNAMIC_FTRACE_WITH_CALL_OPS and with CONFIG_ARM64_BTI_KERNEL ------------------------------------------------------------------------ Disassembly of section .text.cmdline_proc_show: 0000000000000008 <cmdline_proc_show>: 0: d503245f bti c 4: d503201f nop 8: d503201f nop 3. With DYNAMIC_FTRACE_WITH_CALL_OPS and without CONFIG_ARM64_BTI_KERNEL ------------------------------------------------------------------------ Disassembly of section .text.cmdline_proc_show: 0000000000000000 <cmdline_proc_show-0x8>: 0: d503201f nop 4: d503201f nop 0000000000000008 <cmdline_proc_show>: 8: d503201f nop c: d503201f nop 4. With DYNAMIC_FTRACE_WITH_CALL_OPS and with CONFIG_ARM64_BTI_KERNEL --------------------------------------------------------------------- Disassembly of section .text.cmdline_proc_show: 0000000000000000 <cmdline_proc_show-0x8>: 0: d503201f nop 4: d503201f nop 0000000000000008 <cmdline_proc_show>: 8: d503245f bti c c: d503201f nop 10: d503201f nop make create-diff-object aware of DYNAMIC_FTRACE_WITH_CALL_OPS and its quirks. Signed-off-by: Dylan Hatch <dylanbhatch@google.com> Signed-off-by: Puranjay Mohan <pjy@amazon.com>
1 parent 0db9999 commit 44eebf9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

kpatch-build/create-diff-object.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,37 @@ static bool kpatch_is_mapping_symbol(struct kpatch_elf *kelf, struct symbol *sym
255255
return false;
256256
}
257257

258+
static unsigned int function_padding_size(struct kpatch_elf *kelf, struct symbol *sym)
259+
{
260+
unsigned int size = 0;
261+
262+
switch (kelf->arch) {
263+
case AARCH64:
264+
{
265+
uint32_t *insn = sym->sec->data->d_buf;
266+
unsigned int i;
267+
void *insn_end = sym->sec->data->d_buf + sym->sym.st_value;
268+
269+
/*
270+
* If the arm64 kernel is compiled with CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS
271+
* then there are two NOPs before the function and a `BTI C` + 2 NOPs at the
272+
* start of the function. Verify the presence of the two NOPs before the
273+
* function entry.
274+
*/
275+
for (i = 0; (void *)insn < insn_end && *insn == 0xd503201f; i++, insn++)
276+
;
277+
278+
if (i == 2)
279+
size = 8;
280+
break;
281+
}
282+
default:
283+
break;
284+
}
285+
286+
return size;
287+
}
288+
258289
/*
259290
* When compiling with -ffunction-sections and -fdata-sections, almost every
260291
* symbol gets its own dedicated section. We call such symbols "bundled"
@@ -271,6 +302,8 @@ static void kpatch_bundle_symbols(struct kpatch_elf *kelf)
271302
expected_offset = sym->pfx->sym.st_size;
272303
else if (is_gcc6_localentry_bundled_sym(kelf, sym))
273304
expected_offset = 8;
305+
else if (sym->type == STT_FUNC)
306+
expected_offset = function_padding_size(kelf, sym);
274307
else
275308
expected_offset = 0;
276309

@@ -3808,6 +3841,11 @@ static void kpatch_create_ftrace_callsite_sections(struct kpatch_elf *kelf, bool
38083841
unsigned char *insn = sym->sec->data->d_buf;
38093842
int i;
38103843

3844+
/*
3845+
* Skip the padding NOPs added by CALL_OPS.
3846+
*/
3847+
insn += function_padding_size(kelf, sym);
3848+
38113849
/*
38123850
* If BTI (Branch Target Identification) is enabled then there
38133851
* might be an additional 'BTI C' instruction before the two

0 commit comments

Comments
 (0)