objtool/klp: Reject changed functions without an ftrace call site
Subsystems (2)
v2 cover objtool/klp: Reject changed functions without an ftrace call site
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- test-special-sections: check __mcount_loc in its own block instead of inside the section loop (Joe)
- Collected Joe's Acked-by on patches 2 and 3
A changed function without an ftrace call site makes klp-build produce a patch module which fails to load. The kernel's 'inline' implies notrace, so this happens whenever the compiler emits an inline function out of line. Patch 2 makes klp diff reject such functions, patch 3 tests it. Patch 1 builds the test fixtures with ftrace, which patch 2 needs.
Tested on x86_64 with GCC 11 and Clang 22: the objtool klp tests, and klp-build with a vng boot, where a patch module built without the check fails to load with "failed to find location for function".
Note for the arm64 klp-build series: arm64 has no __mcount_loc, so on top of that series this would reject every changed function. It needs mark_traceable_functions() to also handle __patchable_function_entries, using pfe_offset. I can send that on top of the arm64 series.
Puranjay Mohan (3):
objtool/klp: Build test fixtures with ftrace objtool/klp: Reject changed functions without an ftrace call site objtool/klp: Add test for changed functions without an ftrace call
site
.../objtool/Documentation/klp-write-tests.txt | 9 ++- tools/objtool/include/objtool/elf.h | 1 + tools/objtool/klp-diff.c | 47 ++++++++++++ .../tests/generic/test-thinlto-ambiguity.sh | 3 +- .../tests/generic/test-thinlto-local.sh | 5 +- tools/objtool/tests/lib.sh | 42 ++++++++++- .../tests/x86/fixtures/notrace_target.c | 71 ++++++++++++++++++ tools/objtool/tests/x86/test-checksum-alt.sh | 4 - .../objtool/tests/x86/test-notrace-target.sh | 74 +++++++++++++++++++ .../tests/x86/test-special-sections.sh | 12 ++- 10 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 tools/objtool/tests/x86/fixtures/notrace_target.c create mode 100755 tools/objtool/tests/x86/test-notrace-target.sh
base-commit: 6ecc854643e6fd6a2d07bc8d83015c7c34a951eb
Signature · 1 line
2.53.0-Meta
PATCH v2 1/3 objtool/klp: Build test fixtures with ftrace
Livepatch depends on ftrace, so the kernel klp-build operates on always has __mcount_loc, but the test fixtures are built without it.
On x86, build them with -pg -mfentry and generate __mcount_loc like the kernel does: with -mrecord-mcount -mnop-mcount if the compiler supports it (FTRACE_MCOUNT_USE_CC), otherwise with objtool --mcount --mnop (FTRACE_MCOUNT_USE_OBJTOOL).
test-checksum-alt ran objtool --mcount only to get insn->alts, which "klp checksum" builds by itself, and that now warns about the existing __mcount_loc. Remove it.
test-special-sections can now check the real __mcount_loc instead of a hand-written one.
--- .../objtool/Documentation/klp-write-tests.txt | 9 ++-- .../tests/generic/test-thinlto-ambiguity.sh | 3 +- .../tests/generic/test-thinlto-local.sh | 5 ++- tools/objtool/tests/lib.sh | 42 +++++++++++++++++-- tools/objtool/tests/x86/test-checksum-alt.sh | 4 -- .../tests/x86/test-special-sections.sh | 12 +++++- 6 files changed, 61 insertions(+), 14 deletions(-)
tools/objtool/Documentation/klp-write-tests.txt +6 -3
@@ -116,9 +116,12 @@ Traps that have bitten before section entsize or annotations". Their targets need real (global) symbols, or it reports "failed to convert reloc sym". * **Prefer letting objtool generate what objtool generates.** - ``.static_call_sites``, ``.mcount_loc``, ``.ibt_endbr_seal`` and ORC come - from its check pass. Call ``run_objtool_check --mcount`` and let it build - them; a hand-written copy tests your reading of the format, not the format. + ``.static_call_sites``, ``.ibt_endbr_seal`` and ORC come from its check + pass. Call ``run_objtool_check`` with the option for what you need and let + it build them; a hand-written copy tests your reading of the format, not the + format. +* **Fixtures are built with ftrace.** Objects not built with ``build_one`` + need ``$FTRACE_CFLAGS`` and ``record_mcount`` too. Write one by hand only when the test needs a shape objtool will not produce, and say so in the fixture. generic/fixtures/static_call.c does: objtool
tools/objtool/tests/generic/test-thinlto-ambiguity.sh +2 -1
@@ -38,12 +38,13 @@ build_thinlto() # $1 output object, $2 extra flags { local t for t in "" -DTU_B -DTU_C; do - $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections \ + $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $FTRACE_CFLAGS \ $2 $t -c "$FIXTURES_DIR/thinlto_ambiguity.c" \ -o "$workdir/tu$t.o" 2>/dev/null || return 1 done "$THIN_LD" -r "$workdir/tu.o" "$workdir/tu-DTU_B.o" "$workdir/tu-DTU_C.o" \ -o "$1" 2>/dev/null || return 1 + record_mcount "$1" } build_thinlto "$workdir/orig.o" "" ||
tools/objtool/tests/generic/test-thinlto-local.sh +3 -2
@@ -15,11 +15,12 @@ find_thinlto_toolchain || build_thinlto() # $1 output object, $2 extra flags { - $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 \ + $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $FTRACE_CFLAGS $2 \ -c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_a.o" 2>/dev/null || return 1 - $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 -DTU_B \ + $THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $FTRACE_CFLAGS $2 -DTU_B \ -c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_b.o" 2>/dev/null || return 1 "$THIN_LD" -r "$workdir/tu_a.o" "$workdir/tu_b.o" -o "$1" 2>/dev/null || return 1 + record_mcount "$1" } build_thinlto "$workdir/orig.o" "" ||
tools/objtool/tests/lib.sh +39 -3
85 lines not on this page · raw message
tools/objtool/tests/x86/test-checksum-alt.sh +0 -4
@@ -16,9 +16,6 @@ # the feature bit, so the failure looks like a machine-specific bug rather than # a missing patch. # -# insn->alts exists only after objtool's check pass, so the pair goes through -# that first -- the compiler emits none of this structure itself. -# # Covers the same ground as corpus/x86_64/checksum-alt-group, # checksum-alt-no-group and checksum-alt-recursion-guard in Joe Lawrence's # klp-build unit test corpus.
@@ -31,7 +28,6 @@ check() { build_pair checksum_alt.c "-D$1" assert_input_section .altinstructions - run_objtool_check --mcount run_checksum assert_checksum_differs target
tools/objtool/tests/x86/test-special-sections.sh +11 -1
@@ -15,7 +15,7 @@ # section, entry size, relocations per entry -for spec in "__bug_table 12 1" "__ex_table 12 2" "__mcount_loc 8 1"; do +for spec in "__bug_table 12 1" "__ex_table 12 2"; do set -- $spec sec=$1
@@ -39,4 +39,14 @@ for spec in "__bug_table 12 1" "__ex_table 12 2" "__mcount_loc 8 1"; do cleanup done +# __mcount_loc comes from FTRACE_CFLAGS, not from the fixture. +setup +build_pair special_sections.c +assert_input_section __mcount_loc +run_diff + +assert_section __mcount_loc +assert_reloc_sym __mcount_loc target +assert_no_reloc_sym __mcount_loc other + pass "__bug_table, __ex_table and __mcount_loc entries extracted"
Signature · 1 line
2.53.0-Meta
PATCH v2 2/3 objtool/klp: Reject changed functions without an ftrace call site
In reply to Puranjay MohanLivepatch redirects a function through its ftrace call site. If a changed function doesn't have one, klp-build succeeds but the patch module fails to load:
livepatch: failed to find location for function 'nsproxy_free'
livepatch: failed to patch object 'vmlinux'
livepatch: failed to enable patch 'livepatch_nsproxy'
This is easy to hit because the kernel's 'inline' implies notrace, so any inline function which the compiler emits out of line can't be patched.
Make klp diff fail for any changed function which has no __mcount_loc entry in the original object.
--- tools/objtool/include/objtool/elf.h | 1 + tools/objtool/klp-diff.c | 47 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
tools/objtool/include/objtool/elf.h +1 -0
@@ -98,6 +98,7 @@ struct symbol { u8 klp : 1; u8 dont_correlate : 1; u8 fake : 1; + u8 traceable : 1;struct list_head pv_target;struct reloc *relocs;struct section *group_sec;
tools/objtool/klp-diff.c +47 -0
59 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v2 3/3 objtool/klp: Add test for changed functions without an ftrace call site
In reply to Puranjay MohanTest that klp diff rejects a changed function without an ftrace call site, both a notrace one and an inline one emitted out of line, and reports each of them. Also test that a fully inlined function and an ordinary function can still be patched.
--- .../tests/x86/fixtures/notrace_target.c | 71 ++++++++++++++++++ .../objtool/tests/x86/test-notrace-target.sh | 74 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 tools/objtool/tests/x86/fixtures/notrace_target.c create mode 100755 tools/objtool/tests/x86/test-notrace-target.sh
tools/objtool/tests/x86/fixtures/notrace_target.c new file +71 -0
@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +/* PATCH_<name> changes <name> in the patched build. */ +#define notrace __attribute__((__no_instrument_function__)) + +static const char __modinfo[] + __attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux"; + +#ifndef PATCHED +#undef PATCH_OUTLINED +#undef PATCH_NOTRACE +#undef PATCH_INLINED +#undef PATCH_TRACED +#endif + +#ifdef PATCH_OUTLINED +#define OUTLINED_K 2 +#else +#define OUTLINED_K 1 +#endif + +#ifdef PATCH_NOTRACE +#define NOTRACE_K 2 +#else +#define NOTRACE_K 1 +#endif + +#ifdef PATCH_INLINED +#define INLINED_K 2 +#else +#define INLINED_K 1 +#endif + +#ifdef PATCH_TRACED +#define TRACED_K 2 +#else +#define TRACED_K 1 +#endif + +int sink; + +/* Address taken, so emitted out of line. */ +static inline notrace int outlined(int x) +{ +int i; + +for (i =0 ; i < x; i++) + sink += i *OUTLINED_K ; +return sink; +} + +int (*outlined_ptr)(int ) = outlined; + +notrace int notrace_fn(int x) +{ +return x *3 +NOTRACE_K ; +} + +static inline int inlined (int x) +{ +return x +INLINED_K ; +} + +int inlined_user (int x) +{ +return inlined (x) *5 ; +} + +int traced (int x) +{ +return x *7 +TRACED_K ; +}
tools/objtool/tests/x86/test-notrace-target.sh new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Quoted 43 lines · Puranjay Mohan
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- test-special-sections: check__mcount_locin its own block instead of inside the section loop (Joe)
- Collected Joe's Acked-by on patches 2 and 3A changed function without an ftrace call site makes klp-build produce a patch module which fails to load. The kernel's 'inline' implies notrace, so this happens whenever the compiler emits an inline function out of line. Patch 2 makes klp diff reject such functions, patch 3 tests it. Patch 1 builds the test fixtures with ftrace, which patch 2 needs.
Tested on
x86_64with GCC 11 and Clang 22: the objtool klp tests, and klp-build with a vng boot, where a patch module built without the check fails to load with "failed to find location for function".Note for the arm64 klp-build series: arm64 has no
__mcount_loc, so on top of that series this would reject every changed function. It needsmark_traceable_functions()to also handle__patchable_function_entries, usingpfe_offset. I can send that on top of the arm64 series.Puranjay Mohan (3):
objtool/klp: Build test fixtures with ftrace objtool/klp: Reject changed functions without an ftrace call site objtool/klp: Add test for changed functions without an ftrace call
site.../objtool/Documentation/klp-write-tests.txt | 9 ++- tools/objtool/include/objtool/elf.h | 1 + tools/objtool/klp-diff.c | 47 ++++++++++++ .../tests/generic/test-thinlto-ambiguity.sh | 3 +- .../tests/generic/test-thinlto-local.sh | 5 +- tools/objtool/tests/lib.sh | 42 ++++++++++- .../tests/x86/fixtures/notrace_target.c | 71 ++++++++++++++++++ tools/objtool/tests/x86/test-checksum-alt.sh | 4 - .../objtool/tests/x86/test-notrace-target.sh | 74 +++++++++++++++++++ .../tests/x86/test-special-sections.sh | 12 ++- 10 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 tools/objtool/tests/x86/fixtures/notrace_target.c create mode 100755 tools/objtool/tests/x86/test-notrace-target.sh
Quoted 1 line
base-commit:
6ecc854643e6fd6a2d07bc8d83015c7c34a951eb
Thanks, Puranjay!
For the set:
Signature · 1 line
Joe
Quoted 23 lines · Puranjay Mohan
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- test-special-sections: check__mcount_locin its own block instead of inside the section loop (Joe)
- Collected Joe's Acked-by on patches 2 and 3A changed function without an ftrace call site makes klp-build produce a patch module which fails to load. The kernel's 'inline' implies notrace, so this happens whenever the compiler emits an inline function out of line. Patch 2 makes klp diff reject such functions, patch 3 tests it. Patch 1 builds the test fixtures with ftrace, which patch 2 needs.
Tested on
x86_64with GCC 11 and Clang 22: the objtool klp tests, and klp-build with a vng boot, where a patch module built without the check fails to load with "failed to find location for function".Note for the arm64 klp-build series: arm64 has no
__mcount_loc, so on top of that series this would reject every changed function. It needsmark_traceable_functions()to also handle__patchable_function_entries, usingpfe_offset. I can send that on top of the arm64 series.
Quoted 5 lines
Puranjay Mohan (3):
objtool/klp: Build test fixtures with ftrace objtool/klp: Reject changed functions without an ftrace call site objtool/klp: Add test for changed functions without an ftrace call
site
LGTM. For the set:
Quoted 21 lines
.../objtool/Documentation/klp-write-tests.txt | 9 ++- tools/objtool/include/objtool/elf.h | 1 + tools/objtool/klp-diff.c | 47 ++++++++++++ .../tests/generic/test-thinlto-ambiguity.sh | 3 +- .../tests/generic/test-thinlto-local.sh | 5 +- tools/objtool/tests/lib.sh | 42 ++++++++++- .../tests/x86/fixtures/notrace_target.c | 71 ++++++++++++++++++ tools/objtool/tests/x86/test-checksum-alt.sh | 4 - .../objtool/tests/x86/test-notrace-target.sh | 74 +++++++++++++++++++ .../tests/x86/test-special-sections.sh | 12 ++- 10 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 tools/objtool/tests/x86/fixtures/notrace_target.c create mode 100755 tools/objtool/tests/x86/test-notrace-target.shbase-commit:
6ecc854643e6fd6a2d07bc8d83015c7c34a951eb--
2.53.0-Meta
Quoted 6 lines · Puranjay Mohan
Changelog:
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- test-special-sections: check__mcount_locin its own block instead of inside the section loop (Joe)
- Collected Joe's Acked-by on patches 2 and 3
Queued, thanks!
Signature · 1 line
Josh