Skip to contentSkip to conversation

kallsyms: strip LTO-only suffixes from promoted global functions

PATCH Superseded Yonghong Song 3 participants 3 messages Jun 28, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kallsyms: strip LTO-only suffixes from promoted global functions

Commit 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions") stripped all function/variable suffixes started with '.' regardless of whether those suffixes are generated at LTO mode or not. In fact, as far as I know, in LTO mode, when a static function/variable is promoted to the global scope, '.llvm.<...>' suffix is added.

The existing mechanism breaks live patch for a LTO kernel even if no <symbol>.llvm.<...> symbols are involved. For example, for the following kernel symbols:

  $ grep bpf_verifier_vlog /proc/kallsyms
  ffffffff81549f60 t bpf_verifier_vlog
  ffffffff8268b430 d bpf_verifier_vlog._entry
  ffffffff8282a958 d bpf_verifier_vlog._entry_ptr
  ffffffff82e12a1f d bpf_verifier_vlog.__already_done
'bpf_verifier_vlog' is a static function. '_entry', '_entry_ptr' and
'__already_done' are static variables used inside 'bpf_verifier_vlog',
so llvm promotes them to file-level static with prefix 'bpf_verifier_vlog.'.
Note that the func-level to file-level static function promotion also
happens without LTO.

Given a symbol name 'bpf_verifier_vlog', with LTO kernel, current mechanism will return 4 symbols to live patch subsystem which current live patching subsystem cannot handle it. With non-LTO kernel, only one symbol
is returned.

In [1], we have a lengthy discussion, the suggestion is to separate two cases:
(1). new symbols with suffix which are generated regardless of whether
LTO is enabled or not, and
(2). new symbols with suffix generated only when LTO is enabled.

The cleanup_symbol_name() should only remove suffixes for case (2). Case (1) should not be changed so it can work uniformly with or without LTO.

This patch removed LTO-only suffix '.llvm.<...>' so live patching and tracing should work the same way for non-LTO kernel.

[1] https://lore.kernel.org/live-patching/[email protected]/T/#u

Fixes 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions")
---
 kernel/kallsyms.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/kallsyms.c +2 -3
index 77747391f49b..4874508bb950 100644
@@ -174,11 +174,10 @@ static bool cleanup_symbol_name(char *s)
 	 * LLVM appends various suffixes for local functions and variables that
 	 * must be promoted to global scope as part of LTO.  This can break
 	 * hooking of static functions with kprobes. '.' is not a valid
-	 * character in an identifier in C. Suffixes observed:
+	 * character in an identifier in C. Suffixes only in LLVM LTO observed:
 	 * - foo.llvm.[0-9a-f]+
-	 * - foo.[0-9a-f]+
 	 */
-	res = strchr(s, '.');
+	res = strstr(s, ".llvm.");
 	if (res) {
 		*res = '\0';
 		return true;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 31 lines · Yonghong Song

Commit 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions") stripped all function/variable suffixes started with '.' regardless of whether those suffixes are generated at LTO mode or not. In fact, as far as I know, in LTO mode, when a static function/variable is promoted to the global scope, '.llvm.<...>' suffix is added.

The existing mechanism breaks live patch for a LTO kernel even if no <symbol>.llvm.<...> symbols are involved. For example, for the following kernel symbols:

  $ grep bpf_verifier_vlog /proc/kallsyms
  ffffffff81549f60 t bpf_verifier_vlog
  ffffffff8268b430 d bpf_verifier_vlog._entry
  ffffffff8282a958 d bpf_verifier_vlog._entry_ptr
  ffffffff82e12a1f d bpf_verifier_vlog.__already_done
'bpf_verifier_vlog' is a static function. '_entry', '_entry_ptr' and
'__already_done' are static variables used inside 'bpf_verifier_vlog',
so llvm promotes them to file-level static with prefix 'bpf_verifier_vlog.'.
Note that the func-level to file-level static function promotion also
happens without LTO.

Given a symbol name 'bpf_verifier_vlog', with LTO kernel, current mechanism will return 4 symbols to live patch subsystem which current live patching subsystem cannot handle it. With non-LTO kernel, only one symbol
is returned.

In [1], we have a lengthy discussion, the suggestion is to separate two cases:
(1). new symbols with suffix which are generated regardless of whether
LTO is enabled or not, and
(2). new symbols with suffix generated only when LTO is enabled.

Quoted 7 lines

The cleanup_symbol_name() should only remove suffixes for case (2). Case (1) should not be changed so it can work uniformly with or without LTO.

This patch removed LTO-only suffix '.llvm.<...>' so live patching and tracing should work the same way for non-LTO kernel.

[1] https://lore.kernel.org/live-patching/[email protected]/T/#u

Missed the addition of:

Quoted 12 lines
Fixes 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions")
---
 kernel/kallsyms.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/kallsyms.c +0 -0
index 77747391f49b..4874508bb950 100644

kernel/kallsyms.c static bool cleanup_symbol_name(char *s) · line 180

Quoted 10 lines
kernel/kallsyms.c +2 -3
@@ -174,11 +174,10 @@ static bool cleanup_symbol_name(char *s)
 	 * LLVM appends various suffixes for local functions and variables that
 	 * must be promoted to global scope as part of LTO.  This can break
 	 * hooking of static functions with kprobes. '.' is not a valid
-	 * character in an identifier in C. Suffixes observed:
+	 * character in an identifier in C. Suffixes only in LLVM LTO observed:
 	 * - foo.llvm.[0-9a-f]+
-	 * - foo.[0-9a-f]+
 	 */
-	res = strchr(s, '.');
+	res = strstr(s, ".llvm.");

We'd better modify function cleanup_symbol_name() in scripts/kallsyms.c accordingly.

Quoted 4 lines
kernel/kallsyms.c +0 -0
 	if (res) {
 		*res = '\0';
 		return true;
Signature · 2 lines
Regards,
  Zhen Lei
#2 lore raw
Quoted 37 lines · Leizhen (ThunderTown)
Quoted 34 lines · Yonghong Song

Commit 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions") stripped all function/variable suffixes started with '.' regardless of whether those suffixes are generated at LTO mode or not. In fact, as far as I know, in LTO mode, when a static function/variable is promoted to the global scope, '.llvm.<...>' suffix is added.

The existing mechanism breaks live patch for a LTO kernel even if no <symbol>.llvm.<...> symbols are involved. For example, for the following kernel symbols:

   $ grep bpf_verifier_vlog /proc/kallsyms
   ffffffff81549f60 t bpf_verifier_vlog
   ffffffff8268b430 d bpf_verifier_vlog._entry
   ffffffff8282a958 d bpf_verifier_vlog._entry_ptr
   ffffffff82e12a1f d bpf_verifier_vlog.__already_done
'bpf_verifier_vlog' is a static function. '_entry', '_entry_ptr' and
'__already_done' are static variables used inside 'bpf_verifier_vlog',
so llvm promotes them to file-level static with prefix 'bpf_verifier_vlog.'.
Note that the func-level to file-level static function promotion also
happens without LTO.

Given a symbol name 'bpf_verifier_vlog', with LTO kernel, current mechanism will return 4 symbols to live patch subsystem which current live patching subsystem cannot handle it. With non-LTO kernel, only one symbol
is returned.

In [1], we have a lengthy discussion, the suggestion is to separate two cases:
(1). new symbols with suffix which are generated regardless of whether
LTO is enabled or not, and
(2). new symbols with suffix generated only when LTO is enabled.

The cleanup_symbol_name() should only remove suffixes for case (2). Case (1) should not be changed so it can work uniformly with or without LTO.

Quoted 7 lines
Quoted 4 lines

This patch removed LTO-only suffix '.llvm.<...>' so live patching and tracing should work the same way for non-LTO kernel.

[1] https://lore.kernel.org/live-patching/[email protected]/T/#u

Missed the addition of:

Will add in the next revision.

Quoted 17 lines
Quoted 17 lines
Fixes 6eb4bd92c1ce ("kallsyms: strip LTO suffixes from static functions")
---
  kernel/kallsyms.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
kernel/kallsyms.c +0 -1
index 77747391f49b..4874508bb950 100644
@@ -174,11 +174,10 @@ static bool cleanup_symbol_name(char *s)
  	 * LLVM appends various suffixes for local functions and variables that
  	 * must be promoted to global scope as part of LTO.  This can break
  	 * hooking of static functions with kprobes. '.' is not a valid
-	 * character in an identifier in C. Suffixes observed:
Quoted 8 lines

kernel/kallsyms.c static bool cleanup_symbol_name(char *s) · line 180

Quoted 6 lines
kernel/kallsyms.c +2 -2
+	 * character in an identifier in C. Suffixes only in LLVM LTO observed:
  	 * - foo.llvm.[0-9a-f]+
-	 * - foo.[0-9a-f]+
  	 */
-	res = strchr(s, '.');
+	res = strstr(s, ".llvm.");

We'd better modify function cleanup_symbol_name() in scripts/kallsyms.c accordingly.

Thanks, will make corresponding change in scripts/kallsyms.c as well.

Quoted 7 lines
Quoted 6 lines
kernel/kallsyms.c +0 -0
  	if (res) {
  		*res = '\0';
  		return true;