From patchwork Fri Aug 2 21:08:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13751998 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69AF1219FC; Fri, 2 Aug 2024 21:08:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632936; cv=none; b=YIMnn4X0glHzZDhTHi1ZjstGJ+NSX0m1FRXYXY5eqRf76Yh1nvrvYSUSXQIzKpcIurq3mEn4Ij1ZPe+wZaHsnEhTVWaLSqq4jPWNp6fzq5779qEztl5QPubeR8/AuJ3NWUFX9osr6BZw0tTIfDIqXjWPV/BiVAo7BoNA3RrpYdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632936; c=relaxed/simple; bh=pyBJdtxBPz3GxnxSNiZhE22PkJY5jWrFTCpJgLxoSPI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ekS4gWAMTtOcJV++ItKGYKFLf3E7mMupAj4Nricja+hqCb5LqoGLdr8N/GMz5RxLKvBbl+EyaU09ga/wwiHWQ/vCIF4krJbmHRY5DJ4i6yxgI0FyRDJAKNSv7SCM9LdFBeQQW+XKddWw5cks4d9ZM6eNn39C0YOyuLGTcPcTgag= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M/ooE/aV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="M/ooE/aV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2E30C32782; Fri, 2 Aug 2024 21:08:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722632936; bh=pyBJdtxBPz3GxnxSNiZhE22PkJY5jWrFTCpJgLxoSPI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M/ooE/aVl1Ul56u1Bl0w7qHeIpO4lGmqtYeRlqbCwKf/g4qjEOfo8b6awMTBNu8mB L5cGti5vcL9Tk1M3SKK6WTrhjtOJAtMN7Dzu+z98cNMe/SahEXw5O+J2f7DOJtk+CZ cp9RBZRFYWwPEalXRuiA/P22ioFAofv/q9Gcd1L57oIxCbFYbYAmrIEOi8s69e7FL7 Ll5/37Jh2xf1Bnt4iItOJPwQMzTb1amxubkIyLTPTtM1f1fle7NAlCNolOomgzrfo6 rv5wJBAEv1rqIsAnvXvfksoVNF/yFouEpKv/xOgHX9CNJ9EYXUs+Wtb/T0WF0WSE9r PJI/Ao2MjGj/g== From: Song Liu To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, nathan@kernel.org, morbo@google.com, justinstitt@google.com, mcgrof@kernel.org, thunder.leizhen@huawei.com, kees@kernel.org, kernel-team@meta.com, song@kernel.org, mmaurer@google.com, samitolvanen@google.com, mhiramat@kernel.org, rostedt@goodmis.org Subject: [PATCH v2 1/3] kallsyms: Do not cleanup .llvm. suffix before sorting symbols Date: Fri, 2 Aug 2024 14:08:33 -0700 Message-ID: <20240802210836.2210140-2-song@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240802210836.2210140-1-song@kernel.org> References: <20240802210836.2210140-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Cleaning up the symbols causes various issues afterwards. Let's sort the list based on original name and handle suffix later during the symbol lookup. Signed-off-by: Song Liu --- scripts/kallsyms.c | 31 ++----------------------------- scripts/link-vmlinux.sh | 4 ---- 2 files changed, 2 insertions(+), 33 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 0ed873491bf5..123dab0572f8 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,8 +5,7 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: kallsyms [--all-symbols] [--absolute-percpu] - * [--lto-clang] in.map > out.S + * Usage: kallsyms [--all-symbols] [--absolute-percpu] in.map > out.S * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -62,7 +61,6 @@ static struct sym_entry **table; static unsigned int table_size, table_cnt; static int all_symbols; static int absolute_percpu; -static int lto_clang; static int token_profit[0x10000]; @@ -73,8 +71,7 @@ static unsigned char best_table_len[256]; static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] " - "[--lto-clang] in.map > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] in.map > out.S\n"); exit(1); } @@ -344,25 +341,6 @@ static bool symbol_absolute(const struct sym_entry *s) return s->percpu_absolute; } -static void cleanup_symbol_name(char *s) -{ - char *p; - - /* - * ASCII[.] = 2e - * ASCII[0-9] = 30,39 - * ASCII[A-Z] = 41,5a - * ASCII[_] = 5f - * ASCII[a-z] = 61,7a - * - * As above, replacing the first '.' in ".llvm." with '\0' does not - * affect the main sorting, but it helps us with subsorting. - */ - p = strstr(s, ".llvm."); - if (p) - *p = '\0'; -} - static int compare_names(const void *a, const void *b) { int ret; @@ -526,10 +504,6 @@ static void write_src(void) output_address(relative_base); printf("\n"); - if (lto_clang) - for (i = 0; i < table_cnt; i++) - cleanup_symbol_name((char *)table[i]->sym); - sort_symbols_by_name(); output_label("kallsyms_seqs_of_names"); for (i = 0; i < table_cnt; i++) @@ -807,7 +781,6 @@ int main(int argc, char **argv) static const struct option long_options[] = { {"all-symbols", no_argument, &all_symbols, 1}, {"absolute-percpu", no_argument, &absolute_percpu, 1}, - {"lto-clang", no_argument, <o_clang, 1}, {}, }; diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index f7b2503cdba9..22d0bc843986 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -156,10 +156,6 @@ kallsyms() kallsymopt="${kallsymopt} --absolute-percpu" fi - if is_enabled CONFIG_LTO_CLANG; then - kallsymopt="${kallsymopt} --lto-clang" - fi - info KSYMS "${2}.S" scripts/kallsyms ${kallsymopt} "${1}" > "${2}.S" From patchwork Fri Aug 2 21:08:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13751999 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC2F174BED; Fri, 2 Aug 2024 21:09:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632943; cv=none; b=QFInGaLJbxIX3zksbLhCkPmSw1MQwJ0J2CiRyOE9pr5ukI/ffc8FCPc5REcygteBcrTuwEjFMaLSzbhM+G5cWL8rr+vN7/Est5woLeZfVGM0W7aXvCURP2YJUZgmN7yukdH5E9uDK8RdmVI39kOEUcWWEGsXdoI3b620teTrCxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632943; c=relaxed/simple; bh=mPOYboxDlB+DBh4vNdMxSRk3eUhDf8x+e+4dj7v+24o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aiu6BmzzV+380ckCS/uq5GErh9u4CbBlp6gpLdpOHv5kHVStCEO2gz0K2bWtx7fe9Lrw9/ZAS9+3uNG+4zVnarHLoD5VYpMTbnGsebjE4mYrEG2uDSjZVm2OiKAnc8VWJ9Yqy6f/Rx/MRUZG3rNK2oQvpbh3+59UNMJXI6byLXU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iXyuW2hA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iXyuW2hA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56390C32782; Fri, 2 Aug 2024 21:09:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722632943; bh=mPOYboxDlB+DBh4vNdMxSRk3eUhDf8x+e+4dj7v+24o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iXyuW2hAXSCTQiEzJGj6lNASsTGFAbbJBsbc6lOMrRQyRdrUfpPghy6D2CC4K863C WB30HQIeKFEnvCh8QjTpGsHUsaJd+x1xEEYZXuB2Gkk/UnDFUpFgl3X5cvJxxQ9Vit euhDjMaIRWcJhMR3AvLvFH7ajN+4EByDh2Kqqe4V+fziwJICt9duOSOed6vfG7xUpM XvkIfzH2pKCmAPUvq0YdHQ/k/3H+BZFWfj2eDo2m2pAtv3GzJWzRFrJcd67n+E1/qr DPjQVCy9EEwY30NAa+lwU9NBkF22lInHDf2qOR28+7HkyOarpAG/z3O1Ws2u3Z8etk L/JSrFPP2OgWg== From: Song Liu To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, nathan@kernel.org, morbo@google.com, justinstitt@google.com, mcgrof@kernel.org, thunder.leizhen@huawei.com, kees@kernel.org, kernel-team@meta.com, song@kernel.org, mmaurer@google.com, samitolvanen@google.com, mhiramat@kernel.org, rostedt@goodmis.org Subject: [PATCH v2 2/3] kallsyms: Add APIs to match symbol without .XXXX suffix. Date: Fri, 2 Aug 2024 14:08:34 -0700 Message-ID: <20240802210836.2210140-3-song@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240802210836.2210140-1-song@kernel.org> References: <20240802210836.2210140-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 With CONFIG_LTO_CLANG=y, the compiler may add suffix to function names to avoid duplication. This causes confusion with users of kallsyms. On one hand, users like livepatch are required to match the symbols exactly. On the other hand, users like kprobe would like to match to original function names. Solve this by splitting kallsyms APIs. Specifically, existing APIs now should match the symbols exactly. Add two APIs that match only the part without .XXXX suffix. Specifically, the following two APIs are added. 1. kallsyms_lookup_name_without_suffix() 2. kallsyms_on_each_match_symbol_without_suffix() These APIs will be used by kprobe. Also cleanup some code and update kallsyms_selftests accordingly. Signed-off-by: Song Liu Reviewed-by: Masami Hiramatsu (Google) --- include/linux/kallsyms.h | 14 ++++++ kernel/kallsyms.c | 88 ++++++++++++++++++++++++++------------ kernel/kallsyms_selftest.c | 75 +++++++++++++++++++++++--------- 3 files changed, 128 insertions(+), 49 deletions(-) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index c3f075e8f60c..9ef2a944a993 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -74,9 +74,12 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, unsigned long), void *data); int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long), const char *name, void *data); +int kallsyms_on_each_match_symbol_without_suffix(int (*fn)(void *, unsigned long), + const char *name, void *data); /* Lookup the address for a symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name); +unsigned long kallsyms_lookup_name_without_suffix(const char *name); extern int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, @@ -104,6 +107,11 @@ static inline unsigned long kallsyms_lookup_name(const char *name) return 0; } +static inline unsigned long kallsyms_lookup_name_without_suffix(const char *name) +{ + return 0; +} + static inline int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize, unsigned long *offset) @@ -165,6 +173,12 @@ static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long) { return -EOPNOTSUPP; } + +static inline int kallsyms_on_each_match_symbol_without_suffix(int (*fn)(void *, unsigned long), + const char *name, void *data) +{ + return -EOPNOTSUPP; +} #endif /*CONFIG_KALLSYMS*/ static inline void print_ip_sym(const char *loglvl, unsigned long ip) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index fb2c77368d18..64fdff6cde85 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -164,30 +164,27 @@ static void cleanup_symbol_name(char *s) { char *res; - if (!IS_ENABLED(CONFIG_LTO_CLANG)) - return; - /* * 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 only in LLVM LTO observed: - * - foo.llvm.[0-9a-f]+ + * character in an identifier in C, so we can just remove the + * suffix. */ - res = strstr(s, ".llvm."); + res = strstr(s, "."); if (res) *res = '\0'; return; } -static int compare_symbol_name(const char *name, char *namebuf) +static int compare_symbol_name(const char *name, char *namebuf, bool exact_match) { - /* The kallsyms_seqs_of_names is sorted based on names after - * cleanup_symbol_name() (see scripts/kallsyms.c) if clang lto is enabled. - * To ensure correct bisection in kallsyms_lookup_names(), do - * cleanup_symbol_name(namebuf) before comparing name and namebuf. - */ + int ret = strcmp(name, namebuf); + + if (exact_match || !ret) + return ret; + cleanup_symbol_name(namebuf); return strcmp(name, namebuf); } @@ -204,13 +201,17 @@ static unsigned int get_symbol_seq(int index) static int kallsyms_lookup_names(const char *name, unsigned int *start, - unsigned int *end) + unsigned int *end, + bool exact_match) { int ret; int low, mid, high; unsigned int seq, off; char namebuf[KSYM_NAME_LEN]; + if (!IS_ENABLED(CONFIG_LTO_CLANG)) + exact_match = true; + low = 0; high = kallsyms_num_syms - 1; @@ -219,7 +220,7 @@ static int kallsyms_lookup_names(const char *name, seq = get_symbol_seq(mid); off = get_symbol_offset(seq); kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); - ret = compare_symbol_name(name, namebuf); + ret = compare_symbol_name(name, namebuf, exact_match); if (ret > 0) low = mid + 1; else if (ret < 0) @@ -236,7 +237,7 @@ static int kallsyms_lookup_names(const char *name, seq = get_symbol_seq(low - 1); off = get_symbol_offset(seq); kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); - if (compare_symbol_name(name, namebuf)) + if (compare_symbol_name(name, namebuf, exact_match)) break; low--; } @@ -248,7 +249,7 @@ static int kallsyms_lookup_names(const char *name, seq = get_symbol_seq(high + 1); off = get_symbol_offset(seq); kallsyms_expand_symbol(off, namebuf, ARRAY_SIZE(namebuf)); - if (compare_symbol_name(name, namebuf)) + if (compare_symbol_name(name, namebuf, exact_match)) break; high++; } @@ -268,7 +269,28 @@ unsigned long kallsyms_lookup_name(const char *name) if (!*name) return 0; - ret = kallsyms_lookup_names(name, &i, NULL); + ret = kallsyms_lookup_names(name, &i, NULL, true); + if (!ret) + return kallsyms_sym_address(get_symbol_seq(i)); + + return module_kallsyms_lookup_name(name); +} + +/* + * Lookup the address for this symbol. + * Remove .XXX suffix from the symbole before comparing against + * the name to lookup. + */ +unsigned long kallsyms_lookup_name_without_suffix(const char *name) +{ + int ret; + unsigned int i; + + /* Skip the search for empty string. */ + if (!*name) + return 0; + + ret = kallsyms_lookup_names(name, &i, NULL, false); if (!ret) return kallsyms_sym_address(get_symbol_seq(i)); @@ -303,7 +325,25 @@ int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long), int ret; unsigned int i, start, end; - ret = kallsyms_lookup_names(name, &start, &end); + ret = kallsyms_lookup_names(name, &start, &end, true); + if (ret) + return 0; + + for (i = start; !ret && i <= end; i++) { + ret = fn(data, kallsyms_sym_address(get_symbol_seq(i))); + cond_resched(); + } + + return ret; +} + +int kallsyms_on_each_match_symbol_without_suffix(int (*fn)(void *, unsigned long), + const char *name, void *data) +{ + int ret; + unsigned int i, start, end; + + ret = kallsyms_lookup_names(name, &start, &end, false); if (ret) return 0; @@ -450,8 +490,6 @@ const char *kallsyms_lookup(unsigned long addr, int lookup_symbol_name(unsigned long addr, char *symname) { - int res; - symname[0] = '\0'; symname[KSYM_NAME_LEN - 1] = '\0'; @@ -462,16 +500,10 @@ int lookup_symbol_name(unsigned long addr, char *symname) /* Grab name */ kallsyms_expand_symbol(get_symbol_offset(pos), symname, KSYM_NAME_LEN); - goto found; + return 0; } /* See if it's in a module. */ - res = lookup_module_symbol_name(addr, symname); - if (res) - return res; - -found: - cleanup_symbol_name(symname); - return 0; + return lookup_module_symbol_name(addr, symname); } /* Look up a kernel symbol and return it in a text buffer. */ diff --git a/kernel/kallsyms_selftest.c b/kernel/kallsyms_selftest.c index 2f84896a7bcb..929270f4ed55 100644 --- a/kernel/kallsyms_selftest.c +++ b/kernel/kallsyms_selftest.c @@ -187,31 +187,11 @@ static void test_perf_kallsyms_lookup_name(void) stat.min, stat.max, div_u64(stat.sum, stat.real_cnt)); } -static bool match_cleanup_name(const char *s, const char *name) -{ - char *p; - int len; - - if (!IS_ENABLED(CONFIG_LTO_CLANG)) - return false; - - p = strstr(s, ".llvm."); - if (!p) - return false; - - len = strlen(name); - if (p - s != len) - return false; - - return !strncmp(s, name, len); -} - static int find_symbol(void *data, const char *name, unsigned long addr) { struct test_stat *stat = (struct test_stat *)data; - if (strcmp(name, stat->name) == 0 || - (!stat->perf && match_cleanup_name(name, stat->name))) { + if (!strcmp(name, stat->name)) { stat->real_cnt++; stat->addr = addr; @@ -327,12 +307,28 @@ static int test_kallsyms_basic_function(void) } } + prefix = "kallsyms_on_each_match_symbol() for"; + for (i = 0; i < ARRAY_SIZE(test_items); i++) { + memset(stat, 0, sizeof(*stat)); + stat->max = INT_MAX; + stat->name = test_items[i].name; + kallsyms_on_each_match_symbol(match_symbol, test_items[i].name, stat); + if (stat->addr != test_items[i].addr || stat->real_cnt != 1) { + nr_failed++; + pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n", + prefix, test_items[i].name, + stat->real_cnt, stat->addr, test_items[i].addr); + } + } + if (nr_failed) { kfree(stat); return -ESRCH; } for (i = 0; i < kallsyms_num_syms; i++) { + char *p; + addr = kallsyms_sym_address(i); if (!is_ksym_addr(addr)) continue; @@ -415,6 +411,43 @@ static int test_kallsyms_basic_function(void) goto failed; } } + + if (IS_ENABLED(CONFIG_LTO_CLANG)) + continue; + + p = strstr(namebuf, "."); + + /* + * If the symbol contains a "." and it not started with + * ".", test the no suffix lookup. + */ + if (!p || namebuf[0] != '.') + continue; + + *p = '\0'; + + /* + * kallsyms_lookup_name_without_suffix should return a + * value, but it may not match the address of this symbol, + * as the name without suffix may not be unique. + */ + addr = kallsyms_lookup_name_without_suffix(namebuf); + if (!addr) { + pr_info("%s: kallsyms_lookup_name_without_suffix cannot find the symbol\n", + namebuf); + goto failed; + } + + memset(stat, 0, sizeof(*stat)); + stat->max = INT_MAX; + stat->name = namebuf; + kallsyms_on_each_match_symbol_without_suffix( + match_symbol, namebuf, stat); + if (!stat->real_cnt) { + pr_info("%s: kallsyms_on_each_match_symbol_without_suffix cannot find the symbol\n", + namebuf); + goto failed; + } } kfree(stat); From patchwork Fri Aug 2 21:08:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13752000 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 683B4132116; Fri, 2 Aug 2024 21:09:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632951; cv=none; b=NYjXJI/UlafUmFuoDp3VRff4q++EO4TISGHlhMHXMnjrT0qJHQX9e1p4oxdsJihPm/QQAAOpKnQmVfIvLXT3mFvMfW9ARYBnrHXOz1w5k6uP1IW4hv1SfLClmUabYtGOUVTNAr4T/yj6pBZjpN1OT6DrLSKHJDPepmMmEHqs+6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722632951; c=relaxed/simple; bh=wSgBrkC0s9VfLt3cNhMld8TVeciJRDopmo/jk6FLqrA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FagvSkFaNJ5ZVjeGol8nQz1rRe00baeKRKjjDcIAa9bs+D8ym+ZZ94JUtjDydMxRXhHfg9+VgUOUasJg9XUMZ2+jju9CEKUOAwAXOvO1acwnxvtdimaEgzb8rvDbRuZT59j+bqq+D3dc2dCOfzJuwA7RDXvZasP9HoJQ21FK3JY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pnauKxwb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pnauKxwb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCEAEC4AF0A; Fri, 2 Aug 2024 21:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722632951; bh=wSgBrkC0s9VfLt3cNhMld8TVeciJRDopmo/jk6FLqrA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pnauKxwbrdbBR+VxAYGejfu2s56HQwIyB0A837TBxAWpfGkY8Bl4ygKbjUpY/4n+b tBEfjJFtLaqVJM9u0IuzifVtekyBQ7sFh1ketqs0TcEJuSSPsD2DtKM1i02qB0FhMH TGFQ5RI3+qiFsyCduGBlOW12igwNpZnH98aIaJUz+aDXrvg4bTsqYpJZBEsyuYmXRt B3pmwwAwTHKFKJoRCYuvrEdagZ8di6MpS3zoQ5ZNCXpPJ2CHCYXfV2AyS6/pOjy8ec x+V8inj+xFHuDrWGANKJHRJfjpiu+nH029ixLD3Pc7e/MMtWX+aVk/ACjt97i7MqID FrF7GO+P3WCTg== From: Song Liu To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com, nathan@kernel.org, morbo@google.com, justinstitt@google.com, mcgrof@kernel.org, thunder.leizhen@huawei.com, kees@kernel.org, kernel-team@meta.com, song@kernel.org, mmaurer@google.com, samitolvanen@google.com, mhiramat@kernel.org, rostedt@goodmis.org Subject: [PATCH v2 3/3] tracing/kprobes: Use APIs that matches symbols without .XXX suffix Date: Fri, 2 Aug 2024 14:08:35 -0700 Message-ID: <20240802210836.2210140-4-song@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240802210836.2210140-1-song@kernel.org> References: <20240802210836.2210140-1-song@kernel.org> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use the new kallsyms APIs that matches symbols name with .XXX suffix. This allows userspace tools to get kprobes on the expected function name, while the actual symbol has a .llvm. suffix. This only effects kernel compile with CONFIG_LTO_CLANG. Signed-off-by: Song Liu --- kernel/kprobes.c | 6 +++++- kernel/trace/trace_kprobe.c | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index e85de37d9e1e..99102283b076 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -70,7 +70,11 @@ static DEFINE_PER_CPU(struct kprobe *, kprobe_instance); kprobe_opcode_t * __weak kprobe_lookup_name(const char *name, unsigned int __unused) { - return ((kprobe_opcode_t *)(kallsyms_lookup_name(name))); + unsigned long addr = kallsyms_lookup_name(name); + + if (IS_ENABLED(CONFIG_LTO_CLANG) && !addr) + addr = kallsyms_lookup_name_without_suffix(name); + return ((kprobe_opcode_t *)(addr)); } /* diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index 61a6da808203..d2ad0c561c83 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -203,6 +203,10 @@ unsigned long trace_kprobe_address(struct trace_kprobe *tk) if (tk->symbol) { addr = (unsigned long) kallsyms_lookup_name(trace_kprobe_symbol(tk)); + + if (IS_ENABLED(CONFIG_LTO_CLANG) && !addr) + addr = kallsyms_lookup_name_without_suffix(trace_kprobe_symbol(tk)); + if (addr) addr += tk->rp.kp.offset; } else { @@ -766,8 +770,13 @@ static unsigned int number_of_same_symbols(const char *mod, const char *func_nam { struct sym_count_ctx ctx = { .count = 0, .name = func_name }; - if (!mod) + if (!mod) { kallsyms_on_each_match_symbol(count_symbols, func_name, &ctx.count); + if (IS_ENABLED(CONFIG_LTO_CLANG) && !ctx.count) { + kallsyms_on_each_match_symbol_without_suffix( + count_symbols, func_name, &ctx.count); + } + } module_kallsyms_on_each_symbol(mod, count_mod_symbols, &ctx);