From nobody Mon Jun 15 12:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA545C433F5 for ; Thu, 28 Apr 2022 20:12:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351696AbiD1UPu (ORCPT ); Thu, 28 Apr 2022 16:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351675AbiD1UPq (ORCPT ); Thu, 28 Apr 2022 16:15:46 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CB52BF953; Thu, 28 Apr 2022 13:12:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E3697B83025; Thu, 28 Apr 2022 20:12:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC7F7C385A9; Thu, 28 Apr 2022 20:12:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651176743; bh=yLdMRkQj4wpHAGTn9X8WvbNIB2gNomm0DQat/BHFt5M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Qvte3OBdcL5j3h280muHWouj15ATKEQNdHFAkeIu4zov3zXjRjTAOCXyZF+ga1PGz bKnAeE09Gm3OOUXDWwuSWiRaJ23cpaZIeNRGj5Fs2JomttOqCOihhdfaoGIDUXJI53 PIPwE7Xq7atdulrVNCAMAEkZZOPNtuwyFC2+0frGt7CjuVGyIypoOst1+jt4qjCxiN HkCYyry2GRASHphziLakFiREYGua7A3QYgJbhGWsBgTYrIohDIWoO3uxyQ8mZX3x/m xJOquZM2gDnPOY481XrtZ3FHaC52+qkp9bMnlA+yob6ClwHqdOxOP8VG5zuidUR/Hr IHOiPtPSnohBw== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: Christoph Hellwig , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv4 bpf-next 1/5] kallsyms: Fully export kallsyms_on_each_symbol function Date: Thu, 28 Apr 2022 22:12:03 +0200 Message-Id: <20220428201207.954552-2-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428201207.954552-1-jolsa@kernel.org> References: <20220428201207.954552-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Fully exporting kallsyms_on_each_symbol function, so it can be used in following changes. Rather than adding another ifdef option let's export the function completely (when CONFIG_KALLSYMS option is defined). Cc: Christoph Hellwig Signed-off-by: Jiri Olsa Reviewed-by: Masami Hiramatsu --- include/linux/kallsyms.h | 7 ++++++- kernel/kallsyms.c | 2 -- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h index ce1bd2fbf23e..89f063651192 100644 --- a/include/linux/kallsyms.h +++ b/include/linux/kallsyms.h @@ -65,11 +65,11 @@ static inline void *dereference_symbol_descriptor(void = *ptr) return ptr; } =20 +#ifdef CONFIG_KALLSYMS int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module = *, unsigned long), void *data); =20 -#ifdef CONFIG_KALLSYMS /* Lookup the address for a symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name); =20 @@ -163,6 +163,11 @@ static inline bool kallsyms_show_value(const struct cr= ed *cred) return false; } =20 +static inline int kallsyms_on_each_symbol(int (*fn)(void *, const char *, = struct module *, unsigned long), + void *data) +{ + return -EOPNOTSUPP; +} #endif /*CONFIG_KALLSYMS*/ =20 static inline void print_ip_sym(const char *loglvl, unsigned long ip) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 79f2eb617a62..fdfd308bebc4 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -228,7 +228,6 @@ unsigned long kallsyms_lookup_name(const char *name) return module_kallsyms_lookup_name(name); } =20 -#ifdef CONFIG_LIVEPATCH /* * Iterate over all symbols in vmlinux. For symbols from modules use * module_kallsyms_on_each_symbol instead. @@ -251,7 +250,6 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const cha= r *, struct module *, } return 0; } -#endif /* CONFIG_LIVEPATCH */ =20 static unsigned long get_symbol_pos(unsigned long addr, unsigned long *symbolsize, --=20 2.35.1 From nobody Mon Jun 15 12:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 659F2C433EF for ; Thu, 28 Apr 2022 20:12:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351762AbiD1UQB (ORCPT ); Thu, 28 Apr 2022 16:16:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351701AbiD1UPu (ORCPT ); Thu, 28 Apr 2022 16:15:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5D8DBF958; Thu, 28 Apr 2022 13:12:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5B35961E71; Thu, 28 Apr 2022 20:12:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF03CC385AE; Thu, 28 Apr 2022 20:12:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651176753; bh=T76x8sAnMa9QYAi+4HM2HG5atz742DfkdDvuDq9jtVY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qU25aXDefjDTCiJXG9eTlO5Gsw75CqRnqnB80Tqrxl2XfAp6dM2AnlwHwrG+QifUA 7Rn4nmot81H9XBPz/zf7uzTiouKG4wfeOGvy8H5gHJ8OlFauYTmQzT8341Qs8QdoJw Gu9wLih0fQVFQkxu31TPbXwFw58hevGzPHlK2BLVFyzctI8kfbX9D7O3LWNNVbWVbb NMDxO5O7eGyGpAcyKPD18RKFW8n9q21cfJ41SkVEd+lqRsOO9++pW2AXZJ6DTx3oXN T7UVuYm7V7WT+KYzam1JwCU+PXMu9SSklJygBbMAkNLPUZwbJmNFVI1gwy2ovkvaPo Qsd1uzuwfv9Bw== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv4 bpf-next 2/5] ftrace: Add ftrace_lookup_symbols function Date: Thu, 28 Apr 2022 22:12:04 +0200 Message-Id: <20220428201207.954552-3-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428201207.954552-1-jolsa@kernel.org> References: <20220428201207.954552-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding ftrace_lookup_symbols function that resolves array of symbols with single pass over kallsyms. The user provides array of string pointers with count and pointer to allocated array for resolved values. int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs) It iterates all kallsyms symbols and tries to loop up each in provided symbols array with bsearch. The symbols array needs to be sorted by name for this reason. We also check each symbol to pass ftrace_location, because this API will be used for fprobe symbols resolving. Suggested-by: Andrii Nakryiko Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa Reviewed-by: Masami Hiramatsu --- include/linux/ftrace.h | 6 ++++ kernel/kallsyms.c | 1 + kernel/trace/ftrace.c | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 4816b7e11047..820500430eae 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -303,6 +303,8 @@ int unregister_ftrace_function(struct ftrace_ops *ops); extern void ftrace_stub(unsigned long a0, unsigned long a1, struct ftrace_ops *op, struct ftrace_regs *fregs); =20 + +int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned l= ong *addrs); #else /* !CONFIG_FUNCTION_TRACER */ /* * (un)register_ftrace_function must be a macro since the ops parameter @@ -313,6 +315,10 @@ extern void ftrace_stub(unsigned long a0, unsigned lon= g a1, static inline void ftrace_kill(void) { } static inline void ftrace_free_init_mem(void) { } static inline void ftrace_free_mem(struct module *mod, void *start, void *= end) { } +static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t c= nt, unsigned long *addrs) +{ + return -EOPNOTSUPP; +} #endif /* CONFIG_FUNCTION_TRACER */ =20 struct ftrace_func_entry { diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index fdfd308bebc4..fbdf8d3279ac 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -29,6 +29,7 @@ #include #include #include +#include =20 /* * These will be re-linked against their real values diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 4f1d2f5e7263..07d87c7a525d 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -7964,3 +7964,65 @@ ftrace_enable_sysctl(struct ctl_table *table, int wr= ite, mutex_unlock(&ftrace_lock); return ret; } + +static int symbols_cmp(const void *a, const void *b) +{ + const char **str_a =3D (const char **) a; + const char **str_b =3D (const char **) b; + + return strcmp(*str_a, *str_b); +} + +struct kallsyms_data { + unsigned long *addrs; + const char **syms; + size_t cnt; + size_t found; +}; + +static int kallsyms_callback(void *data, const char *name, + struct module *mod, unsigned long addr) +{ + struct kallsyms_data *args =3D data; + + if (!bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_c= mp)) + return 0; + + addr =3D ftrace_location(addr); + if (!addr) + return 0; + + args->addrs[args->found++] =3D addr; + return args->found =3D=3D args->cnt ? 1 : 0; +} + +/** + * ftrace_lookup_symbols - Lookup addresses for array of symbols + * + * @sorted_syms: array of symbols pointers symbols to resolve, + * must be alphabetically sorted + * @cnt: number of symbols/addresses in @syms/@addrs arrays + * @addrs: array for storing resulting addresses + * + * This function looks up addresses for array of symbols provided in + * @syms array (must be alphabetically sorted) and stores them in + * @addrs array, which needs to be big enough to store at least @cnt + * addresses. + * + * This function returns 0 if all provided symbols are found, + * -ESRCH otherwise. + */ +int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned l= ong *addrs) +{ + struct kallsyms_data args; + int err; + + args.addrs =3D addrs; + args.syms =3D sorted_syms; + args.cnt =3D cnt; + args.found =3D 0; + err =3D kallsyms_on_each_symbol(kallsyms_callback, &args); + if (err < 0) + return err; + return args.found =3D=3D args.cnt ? 0 : -ESRCH; +} --=20 2.35.1 From nobody Mon Jun 15 12:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7AED9C433FE for ; Thu, 28 Apr 2022 20:12:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351774AbiD1UQM (ORCPT ); Thu, 28 Apr 2022 16:16:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351782AbiD1UQE (ORCPT ); Thu, 28 Apr 2022 16:16:04 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AA5DC0D30; Thu, 28 Apr 2022 13:12:45 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9AD4361E71; Thu, 28 Apr 2022 20:12:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 412F9C385AD; Thu, 28 Apr 2022 20:12:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651176764; bh=5FwLa1xFrkFZPWtTKRF4rYoUZEPf40so8YMj+Ov7/5U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ui53yeWEqn1BfSk6TKCtdb3KclCnYH8PZThCpMa9p3gD5kST7ZRjv7X6qB1dFlam6 HVrWH3+8+KfKRfpTsQ1bEeLRFHWf92HoldoACJCyq7wyZcbnxPsahUY86OIZ7TYbC6 elzonHAtLz707i/7HkIpl+yGXqOHFQzd3nqSU4aT6g9rL9NWYwdniOYetfw5NG8byQ OqHquxdJo3n9ZHDI+FPq6id3bCKRcrvWJO/90TntDVvZTJGZWwKDvkSpffSR0yB75i VEnoL/X1WY5F8Lr0zxy9hwD1V5WXyDyj13WuH3WFVk7ZEkMwiGCSeiUj4hZt+Ai719 wjK4vvo052Bfw== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv4 bpf-next 3/5] fprobe: Resolve symbols with ftrace_lookup_symbols Date: Thu, 28 Apr 2022 22:12:05 +0200 Message-Id: <20220428201207.954552-4-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428201207.954552-1-jolsa@kernel.org> References: <20220428201207.954552-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Using ftrace_lookup_symbols to speed up symbols lookup in register_fprobe_syms API. This requires syms array to be alphabetically sorted. Signed-off-by: Jiri Olsa Acked-by: Masami Hiramatsu --- kernel/trace/fprobe.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c index 89d9f994ebb0..aac63ca9c3d1 100644 --- a/kernel/trace/fprobe.c +++ b/kernel/trace/fprobe.c @@ -85,39 +85,31 @@ static void fprobe_exit_handler(struct rethook_node *rh= , void *data, } NOKPROBE_SYMBOL(fprobe_exit_handler); =20 +static int symbols_cmp(const void *a, const void *b) +{ + const char **str_a =3D (const char **) a; + const char **str_b =3D (const char **) b; + + return strcmp(*str_a, *str_b); +} + /* Convert ftrace location address from symbols */ static unsigned long *get_ftrace_locations(const char **syms, int num) { - unsigned long addr, size; unsigned long *addrs; - int i; =20 /* Convert symbols to symbol address */ addrs =3D kcalloc(num, sizeof(*addrs), GFP_KERNEL); if (!addrs) return ERR_PTR(-ENOMEM); =20 - for (i =3D 0; i < num; i++) { - addr =3D kallsyms_lookup_name(syms[i]); - if (!addr) /* Maybe wrong symbol */ - goto error; - - /* Convert symbol address to ftrace location. */ - if (!kallsyms_lookup_size_offset(addr, &size, NULL) || !size) - goto error; + /* ftrace_lookup_symbols expects sorted symbols */ + sort(syms, num, sizeof(*syms), symbols_cmp, NULL); =20 - addr =3D ftrace_location_range(addr, addr + size - 1); - if (!addr) /* No dynamic ftrace there. */ - goto error; + if (!ftrace_lookup_symbols(syms, num, addrs)) + return addrs; =20 - addrs[i] =3D addr; - } - - return addrs; - -error: kfree(addrs); - return ERR_PTR(-ENOENT); } =20 --=20 2.35.1 From nobody Mon Jun 15 12:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9C3DC433EF for ; Thu, 28 Apr 2022 20:13:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351781AbiD1UQZ (ORCPT ); Thu, 28 Apr 2022 16:16:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351767AbiD1UQL (ORCPT ); Thu, 28 Apr 2022 16:16:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BFF0BAB83; Thu, 28 Apr 2022 13:12:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9267D61D2B; Thu, 28 Apr 2022 20:12:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B6B3C385A9; Thu, 28 Apr 2022 20:12:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651176774; bh=+LByHrrtf81CwdaFC5DHRS/xfMmBXs2eEoWjFB8FU1Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KL0qBMnjYs+YPatP0fRE+24L9aD9z5g1AlN7IXWiXuaKtxyZt3flxvRW4U9teNgfS YaTJ10H+bmwDcViUrxaXwe9WK8ewMlv4M8l8kSAzzYaJP9cFgK8Z/fxgnuXTvbgq6L cemCnJopW176G2XZ7dLoDA0ABMLHegxshno2TLkoo0GnRbrTjIfzlZ+I6khEO3t+GB xjabzE3pcmbFc1l8NEou7y5XPUkFCa+AqCh+g3tdyd88DdJBC4I6P6NrUf+iM+yXk7 KgOzvOfS3QBYPJax6HrrUhK+4skHVxtvF86zKx/Ya3GGXOT24GYfblf+Z+7ogxRzNO wUqrVTVDSEo/Q== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv4 bpf-next 4/5] bpf: Resolve symbols with ftrace_lookup_symbols for kprobe multi link Date: Thu, 28 Apr 2022 22:12:06 +0200 Message-Id: <20220428201207.954552-5-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428201207.954552-1-jolsa@kernel.org> References: <20220428201207.954552-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Using kallsyms_lookup_names function to speed up symbols lookup in kprobe multi link attachment and replacing with it the current kprobe_multi_resolve_syms function. This speeds up bpftrace kprobe attachment: # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exi= t(); }' ... 6.5681 +- 0.0225 seconds time elapsed ( +- 0.34% ) After: # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exi= t(); }' ... 0.5661 +- 0.0275 seconds time elapsed ( +- 4.85% ) Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- kernel/trace/bpf_trace.c | 112 +++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 46 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index f15b826f9899..7fd11c17558d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2229,6 +2229,59 @@ struct bpf_kprobe_multi_run_ctx { unsigned long entry_ip; }; =20 +struct user_syms { + const char **syms; + char *buf; +}; + +static int copy_user_syms(struct user_syms *us, unsigned long __user *usym= s, u32 cnt) +{ + unsigned long __user usymbol; + const char **syms =3D NULL; + char *buf =3D NULL, *p; + int err =3D -ENOMEM; + unsigned int i; + + syms =3D kvmalloc(cnt * sizeof(*syms), GFP_KERNEL); + if (!syms) + goto error; + + buf =3D kvmalloc(cnt * KSYM_NAME_LEN, GFP_KERNEL); + if (!buf) + goto error; + + for (p =3D buf, i =3D 0; i < cnt; i++) { + if (__get_user(usymbol, usyms + i)) { + err =3D -EFAULT; + goto error; + } + err =3D strncpy_from_user(p, (const char __user *) usymbol, KSYM_NAME_LE= N); + if (err =3D=3D KSYM_NAME_LEN) + err =3D -E2BIG; + if (err < 0) + goto error; + syms[i] =3D p; + p +=3D err + 1; + } + + us->syms =3D syms; + us->buf =3D buf; + return 0; + +error: + if (err) { + kvfree(syms); + kvfree(buf); + } + return err; +} + +static void free_user_syms(struct user_syms *us) +{ + kvfree(us->syms); + kvfree(us->buf); +} + static void bpf_kprobe_multi_link_release(struct bpf_link *link) { struct bpf_kprobe_multi_link *kmulti_link; @@ -2349,53 +2402,12 @@ kprobe_multi_link_handler(struct fprobe *fp, unsign= ed long entry_ip, kprobe_multi_link_prog_run(link, entry_ip, regs); } =20 -static int -kprobe_multi_resolve_syms(const void __user *usyms, u32 cnt, - unsigned long *addrs) +static int symbols_cmp(const void *a, const void *b) { - unsigned long addr, size; - const char __user **syms; - int err =3D -ENOMEM; - unsigned int i; - char *func; - - size =3D cnt * sizeof(*syms); - syms =3D kvzalloc(size, GFP_KERNEL); - if (!syms) - return -ENOMEM; + const char **str_a =3D (const char **) a; + const char **str_b =3D (const char **) b; =20 - func =3D kmalloc(KSYM_NAME_LEN, GFP_KERNEL); - if (!func) - goto error; - - if (copy_from_user(syms, usyms, size)) { - err =3D -EFAULT; - goto error; - } - - for (i =3D 0; i < cnt; i++) { - err =3D strncpy_from_user(func, syms[i], KSYM_NAME_LEN); - if (err =3D=3D KSYM_NAME_LEN) - err =3D -E2BIG; - if (err < 0) - goto error; - err =3D -EINVAL; - addr =3D kallsyms_lookup_name(func); - if (!addr) - goto error; - if (!kallsyms_lookup_size_offset(addr, &size, NULL)) - goto error; - addr =3D ftrace_location_range(addr, addr + size - 1); - if (!addr) - goto error; - addrs[i] =3D addr; - } - - err =3D 0; -error: - kvfree(syms); - kfree(func); - return err; + return strcmp(*str_a, *str_b); } =20 int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_pr= og *prog) @@ -2441,7 +2453,15 @@ int bpf_kprobe_multi_link_attach(const union bpf_att= r *attr, struct bpf_prog *pr goto error; } } else { - err =3D kprobe_multi_resolve_syms(usyms, cnt, addrs); + struct user_syms us; + + err =3D copy_user_syms(&us, usyms, cnt); + if (err) + goto error; + + sort(us.syms, cnt, sizeof(*us.syms), symbols_cmp, NULL); + err =3D ftrace_lookup_symbols(us.syms, cnt, addrs); + free_user_syms(&us); if (err) goto error; } --=20 2.35.1 From nobody Mon Jun 15 12:32:48 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1E4FC433FE for ; Thu, 28 Apr 2022 20:13:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351809AbiD1UQf (ORCPT ); Thu, 28 Apr 2022 16:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351719AbiD1UQW (ORCPT ); Thu, 28 Apr 2022 16:16:22 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23811C0D3F; Thu, 28 Apr 2022 13:13:05 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 965CA61E62; Thu, 28 Apr 2022 20:13:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DE14C385AE; Thu, 28 Apr 2022 20:13:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651176784; bh=0HUrX5uwoHexRp/96k+cwZbo2S2VgOSv0AfuyEVuAJc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ABcUqQXRmu0KBMm6w3m1Lr3ZY6EY0Oa+oRXBOUYf2xfScbDczGF5Snbouy6kvLgAo szwIVDTxf+iXsi8enwHQ0jfZATgk7kYEe1Lgh2s+g3yqCt6HacW9ZFYVp0L/GFoUvA 7H1RhH5Gb67ASB86Th26tN2AomDjmugbIIj40O1tMIysW+bSaog/KB3Q0UFUFcrbFq cPbDKNzbloz79j9+dnn0VXPzUOkjpaRhQnkNF2b7D2ueFwQAKYemNxLZHIZmozxyoh xc2rKBUfFt1aNUN1ZhL7rd1zqvUOAoXaAxG5Wqhwj5T5x81CIt2ncpqWyKqPDjU5xw IGJ/xzGqFNU6g== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: [PATCHv4 bpf-next 5/5] selftests/bpf: Add attach bench test Date: Thu, 28 Apr 2022 22:12:07 +0200 Message-Id: <20220428201207.954552-6-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220428201207.954552-1-jolsa@kernel.org> References: <20220428201207.954552-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adding test that reads all functions from ftrace available_filter_functions file and attach them all through kprobe_multi API. It also prints stats info with -v option, like on my setup: test_bench_attach: found 48712 functions test_bench_attach: attached in 1.069s test_bench_attach: detached in 0.373s Acked-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- .../bpf/prog_tests/kprobe_multi_test.c | 133 ++++++++++++++++++ .../selftests/bpf/progs/kprobe_multi_empty.c | 12 ++ 2 files changed, 145 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_empty.c diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c b/t= ools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c index b9876b55fc0c..a0664c5c15ee 100644 --- a/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c @@ -2,6 +2,9 @@ #include #include "kprobe_multi.skel.h" #include "trace_helpers.h" +#include "kprobe_multi_empty.skel.h" +#include "bpf/libbpf_internal.h" +#include "bpf/hashmap.h" =20 static void kprobe_multi_test_run(struct kprobe_multi *skel, bool test_ret= urn) { @@ -301,6 +304,134 @@ static void test_attach_api_fails(void) kprobe_multi__destroy(skel); } =20 +static inline __u64 get_time_ns(void) +{ + struct timespec t; + + clock_gettime(CLOCK_MONOTONIC, &t); + return (__u64) t.tv_sec * 1000000000 + t.tv_nsec; +} + +static size_t symbol_hash(const void *key, void *ctx __maybe_unused) +{ + return str_hash((const char *) key); +} + +static bool symbol_equal(const void *key1, const void *key2, void *ctx __m= aybe_unused) +{ + return strcmp((const char *) key1, (const char *) key2) =3D=3D 0; +} + +static int get_syms(char ***symsp, size_t *cntp) +{ + size_t cap =3D 0, cnt =3D 0, i; + char *name, **syms =3D NULL; + struct hashmap *map; + char buf[256]; + FILE *f; + int err; + + /* + * The available_filter_functions contains many duplicates, + * but other than that all symbols are usable in kprobe multi + * interface. + * Filtering out duplicates by using hashmap__add, which won't + * add existing entry. + */ + f =3D fopen("/sys/kernel/debug/tracing/available_filter_functions", "r"); + if (!f) + return -EINVAL; + + map =3D hashmap__new(symbol_hash, symbol_equal, NULL); + if (IS_ERR(map)) + goto error; + + while (fgets(buf, sizeof(buf), f)) { + /* skip modules */ + if (strchr(buf, '[')) + continue; + if (sscanf(buf, "%ms$*[^\n]\n", &name) !=3D 1) + continue; + err =3D hashmap__add(map, name, NULL); + if (err) { + free(name); + if (err =3D=3D -EEXIST) + continue; + goto error; + } + err =3D libbpf_ensure_mem((void **) &syms, &cap, + sizeof(*syms), cnt + 1); + if (err) { + free(name); + goto error; + } + syms[cnt] =3D name; + cnt++; + } + + *symsp =3D syms; + *cntp =3D cnt; + +error: + fclose(f); + hashmap__free(map); + if (err) { + for (i =3D 0; i < cnt; i++) + free(syms[cnt]); + free(syms); + } + return err; +} + +static void test_bench_attach(void) +{ + LIBBPF_OPTS(bpf_kprobe_multi_opts, opts); + struct kprobe_multi_empty *skel =3D NULL; + long attach_start_ns, attach_end_ns; + long detach_start_ns, detach_end_ns; + double attach_delta, detach_delta; + struct bpf_link *link =3D NULL; + char **syms =3D NULL; + size_t cnt, i; + + if (!ASSERT_OK(get_syms(&syms, &cnt), "get_syms")) + return; + + skel =3D kprobe_multi_empty__open_and_load(); + if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load")) + goto cleanup; + + opts.syms =3D (const char **) syms; + opts.cnt =3D cnt; + + attach_start_ns =3D get_time_ns(); + link =3D bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_em= pty, + NULL, &opts); + attach_end_ns =3D get_time_ns(); + + if (!ASSERT_OK_PTR(link, "bpf_program__attach_kprobe_multi_opts")) + goto cleanup; + + detach_start_ns =3D get_time_ns(); + bpf_link__destroy(link); + detach_end_ns =3D get_time_ns(); + + attach_delta =3D (attach_end_ns - attach_start_ns) / 1000000000.0; + detach_delta =3D (detach_end_ns - detach_start_ns) / 1000000000.0; + + printf("%s: found %lu functions\n", __func__, cnt); + printf("%s: attached in %7.3lfs\n", __func__, attach_delta); + printf("%s: detached in %7.3lfs\n", __func__, detach_delta); + +cleanup: + kprobe_multi_empty__destroy(skel); + if (syms) { + for (i =3D 0; i < cnt; i++) + free(syms[i]); + free(syms); + } +} + void test_kprobe_multi_test(void) { if (!ASSERT_OK(load_kallsyms(), "load_kallsyms")) @@ -320,4 +451,6 @@ void test_kprobe_multi_test(void) test_attach_api_syms(); if (test__start_subtest("attach_api_fails")) test_attach_api_fails(); + if (test__start_subtest("bench_attach")) + test_bench_attach(); } diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_empty.c b/tools= /testing/selftests/bpf/progs/kprobe_multi_empty.c new file mode 100644 index 000000000000..e76e499aca39 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_empty.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +SEC("kprobe.multi/") +int test_kprobe_empty(struct pt_regs *ctx) +{ + return 0; +} --=20 2.35.1