Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc64/ftrace: Support .text larger than 32MB with out-of-line stubs

We are restricted to a .text size of ~32MB when using out-of-line
function profile sequence. Allow this to be extended up to the previous
limit of ~64MB by reserving space in the middle of .text.

A new config option CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE is
introduced to specify the number of function stubs that are reserved in
.text. On boot, ftrace utilizes stubs from this area first before using
the stub area at the end of .text.

A ppc64le defconfig has ~44k functions that can be traced. A more
conservative value of 32k functions is chosen as the default value of
PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE so that we do not allot more space
than necessary by default. If building a kernel that only has 32k
trace-able functions, we won't allot any more space at the end of .text
during the pass on vmlinux.o. Otherwise, only the remaining functions
get space for stubs at the end of .text. This default value should help
cover a .text size of ~48MB in total (including space reserved at the
end of .text which can cover up to 32MB), which should be sufficient for
most common builds. For a very small kernel build, this can be set to 0.
Or, this can be bumped up to a larger value to support vmlinux .text
size up to ~64MB.

Signed-off-by: Naveen N Rao <naveen@kernel.org>
Signed-off-by: Hari Bathini <hbathini@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://patch.msgid.link/20241030070850.1361304-14-hbathini@linux.ibm.com

authored by

Naveen N Rao and committed by
Michael Ellerman
cf9bc0ef eec37961

+58 -13
+12
arch/powerpc/Kconfig
··· 573 573 def_bool PPC64 && ARCH_USING_PATCHABLE_FUNCTION_ENTRY 574 574 select ARCH_WANTS_PRE_LINK_VMLINUX 575 575 576 + config PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE 577 + int "Number of ftrace out-of-line stubs to reserve within .text" 578 + depends on PPC_FTRACE_OUT_OF_LINE 579 + default 32768 580 + help 581 + Number of stubs to reserve for use by ftrace. This space is 582 + reserved within .text, and is distinct from any additional space 583 + added at the end of .text before the final vmlinux link. Set to 584 + zero to have stubs only be generated at the end of vmlinux (only 585 + if the size of vmlinux is less than 32MB). Set to a higher value 586 + if building vmlinux larger than 48MB. 587 + 576 588 config HOTPLUG_CPU 577 589 bool "Support for enabling/disabling CPUs" 578 590 depends on SMP && (PPC_PSERIES || \
+4 -2
arch/powerpc/include/asm/ftrace.h
··· 138 138 struct ftrace_ool_stub { 139 139 u32 insn[4]; 140 140 }; 141 - extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_inittext[]; 142 - extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_inittext_count; 141 + extern struct ftrace_ool_stub ftrace_ool_stub_text_end[], ftrace_ool_stub_text[], 142 + ftrace_ool_stub_inittext[]; 143 + extern unsigned int ftrace_ool_stub_text_end_count, ftrace_ool_stub_text_count, 144 + ftrace_ool_stub_inittext_count; 143 145 #endif 144 146 void ftrace_free_init_tramp(void); 145 147 unsigned long ftrace_call_adjust(unsigned long addr);
+17 -4
arch/powerpc/kernel/trace/ftrace.c
··· 168 168 static int ftrace_init_ool_stub(struct module *mod, struct dyn_ftrace *rec) 169 169 { 170 170 #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE 171 - static int ool_stub_text_end_index, ool_stub_inittext_index; 171 + static int ool_stub_text_index, ool_stub_text_end_index, ool_stub_inittext_index; 172 172 int ret = 0, ool_stub_count, *ool_stub_index; 173 173 ppc_inst_t inst; 174 174 /* ··· 191 191 ool_stub_index = &ool_stub_inittext_index; 192 192 ool_stub_count = ftrace_ool_stub_inittext_count; 193 193 } else if (is_kernel_text(rec->ip)) { 194 - ool_stub = ftrace_ool_stub_text_end; 195 - ool_stub_index = &ool_stub_text_end_index; 196 - ool_stub_count = ftrace_ool_stub_text_end_count; 194 + /* 195 + * ftrace records are sorted, so we first use up the stub area within .text 196 + * (ftrace_ool_stub_text) before using the area at the end of .text 197 + * (ftrace_ool_stub_text_end), unless the stub is out of range of the record. 198 + */ 199 + if (ool_stub_text_index >= ftrace_ool_stub_text_count || 200 + !is_offset_in_branch_range((long)rec->ip - 201 + (long)&ftrace_ool_stub_text[ool_stub_text_index])) { 202 + ool_stub = ftrace_ool_stub_text_end; 203 + ool_stub_index = &ool_stub_text_end_index; 204 + ool_stub_count = ftrace_ool_stub_text_end_count; 205 + } else { 206 + ool_stub = ftrace_ool_stub_text; 207 + ool_stub_index = &ool_stub_text_index; 208 + ool_stub_count = ftrace_ool_stub_text_count; 209 + } 197 210 #ifdef CONFIG_MODULES 198 211 } else if (mod) { 199 212 ool_stub = mod->arch.ool_stubs;
+8
arch/powerpc/kernel/trace/ftrace_entry.S
··· 374 374 blr 375 375 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 376 376 377 + #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE 378 + SYM_DATA(ftrace_ool_stub_text_count, .long CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE) 379 + 380 + SYM_CODE_START(ftrace_ool_stub_text) 381 + .space CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE * FTRACE_OOL_STUB_SIZE 382 + SYM_CODE_END(ftrace_ool_stub_text) 383 + #endif 384 + 377 385 .pushsection ".tramp.ftrace.text","aw",@progbits; 378 386 .globl ftrace_tramp_text 379 387 ftrace_tramp_text:
+2 -1
arch/powerpc/tools/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 3 quiet_cmd_gen_ftrace_ool_stubs = GEN $@ 4 - cmd_gen_ftrace_ool_stubs = $< "$(CONFIG_64BIT)" "$(OBJDUMP)" vmlinux.o $@ 4 + cmd_gen_ftrace_ool_stubs = $< "$(CONFIG_PPC_FTRACE_OUT_OF_LINE_NUM_RESERVE)" "$(CONFIG_64BIT)" \ 5 + "$(OBJDUMP)" vmlinux.o $@ 5 6 6 7 $(obj)/vmlinux.arch.S: $(src)/ftrace-gen-ool-stubs.sh vmlinux.o FORCE 7 8 $(call if_changed,gen_ftrace_ool_stubs)
+15 -6
arch/powerpc/tools/ftrace-gen-ool-stubs.sh
··· 4 4 # Error out on error 5 5 set -e 6 6 7 - is_64bit="$1" 8 - objdump="$2" 9 - vmlinux_o="$3" 10 - arch_vmlinux_S="$4" 7 + num_ool_stubs_text_builtin="$1" 8 + is_64bit="$2" 9 + objdump="$3" 10 + vmlinux_o="$4" 11 + arch_vmlinux_S="$5" 11 12 12 13 RELOCATION=R_PPC64_ADDR64 13 14 if [ -z "$is_64bit" ]; then ··· 20 19 num_ool_stubs_inittext=$($objdump -r -j __patchable_function_entries "$vmlinux_o" | 21 20 grep ".init.text" | grep -c "$RELOCATION") 22 21 22 + if [ "$num_ool_stubs_text" -gt "$num_ool_stubs_text_builtin" ]; then 23 + num_ool_stubs_text_end=$((num_ool_stubs_text - num_ool_stubs_text_builtin)) 24 + else 25 + num_ool_stubs_text_end=0 26 + fi 27 + 23 28 cat > "$arch_vmlinux_S" <<EOF 24 29 #include <asm/asm-offsets.h> 25 30 #include <linux/linkage.h> 26 31 27 32 .pushsection .tramp.ftrace.text,"aw" 28 - SYM_DATA(ftrace_ool_stub_text_end_count, .long $num_ool_stubs_text) 33 + SYM_DATA(ftrace_ool_stub_text_end_count, .long $num_ool_stubs_text_end) 29 34 30 35 SYM_CODE_START(ftrace_ool_stub_text_end) 31 - .space $num_ool_stubs_text * FTRACE_OOL_STUB_SIZE 36 + #if $num_ool_stubs_text_end 37 + .space $num_ool_stubs_text_end * FTRACE_OOL_STUB_SIZE 38 + #endif 32 39 SYM_CODE_END(ftrace_ool_stub_text_end) 33 40 .popsection 34 41