Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4982406ioa; Wed, 27 Apr 2022 16:02:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzywXIxocxuVb+sth3TCuLWr3tz6KSRj7IySup4HYBUUj4rGcIspttlDX6rF1i/wsW+XZqa X-Received: by 2002:aa7:c6c3:0:b0:425:b13b:94f with SMTP id b3-20020aa7c6c3000000b00425b13b094fmr32682258eds.313.1651100562434; Wed, 27 Apr 2022 16:02:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651100562; cv=none; d=google.com; s=arc-20160816; b=XGR7D8E6EDLLNER/LrMkGzzZ7+Ch/GeT6WlQUWmgRnSsmMJiiPdIejr6LfgHt0GS4/ Zdp/+GELnGNi1EjXvpG71OTOnkkbhBmCczV2ere2WR9CmZSQmT/ZgIyf/pQmWCzYu6Sq uWa8wZ0fOnx99jmxpVdWotSRkegcxFwSzK3K2jS1ikD6YLG34oSTrFffp579DHkW4GiK uWJKpL/RUgInKS73FxQGNa8T90xhlQk/Hku3xCAH27GelfIl7D8b3wP2L21uiNyfwB9k 0Hsh+GYOfMpN8BHYoYI5hARoDY2hVgDWRDWcgD5LFeKvjht7tjp4Zq4QxcWnnug9dFaf SxpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fdJ1SgXlu80AM1s3q5h132fPmZcvoswMs37/zW8pbw4=; b=rotqOgULDCfblaXkBhYrvF926jY8SCVxp4UJrYwZVt1A+KjUpH6OrDaKHi0hs6vqKm GV+5S/V/tdURyvwRYTxkA+MG/R+2Cap/mFZgqPgag9taCNjvSArPgvNRlAuU6Sv6aTZS QaVvIuzt8OHLjCfo60xwLownoxm0xMIOKToEnc4qyvi5esPzWgBchC3D7mNDFdqXiNkm xh1lsvCDMdKCRzhqESw5W4feEiUSF43Z2kxNZyxqciAFXwaCgxyqJ165wcOsMY8u89CP UmlbG3M+MeLjZ/BA5/agKjm0FGkOypju1S3d9H5G9vs+NG2izWTfHVtAQwMxGnNKZ8iQ W30g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zjo0L2p9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d19-20020a1709063ed300b006f3b763040asi2213576ejj.141.2022.04.27.16.02.17; Wed, 27 Apr 2022 16:02:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zjo0L2p9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233639AbiD0VH1 (ORCPT + 99 others); Wed, 27 Apr 2022 17:07:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236944AbiD0VHZ (ORCPT ); Wed, 27 Apr 2022 17:07:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 111B73B55E; Wed, 27 Apr 2022 14:04:13 -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 A0E2E61D96; Wed, 27 Apr 2022 21:04:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2999CC385A7; Wed, 27 Apr 2022 21:04:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651093452; bh=NbaQaqK6bK4LYN0yFT8k61hz2Mc7Pqy8ccZi9IehvmM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zjo0L2p9Y7strrXZIHom4dECd+ItvA7YpRMkD5upN/8TYaVa5jwn+XOOpMJp75nSS 9LSPxb+N6M7OAv3qbWjOO0Oc6yATFMCs2F8DSaDBlJmelRmo/CjDHjMgt0nwp7fsDo 3JSuBQxwWbLiX8UfEyVgzFCDjB3sdsqKQPQn+PA3+e5RT3WSddQ1WE3ib3tfaOHt+b QEEBCiC6EZ2vUxViZpoWaF62dv4bw0JthSf7yGNKvEKmeYttaOVS01V+yG62UthCsp YU0VSydr1amRF6uaB14VrLiFS6b6Z3uXf8XIoFephx0g0pVu1uevvTIJC09ngoUN5/ SMgma+JmjwcrA== 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: [PATCHv3 bpf-next 2/5] ftrace: Add ftrace_lookup_symbols function Date: Wed, 27 Apr 2022 23:03:42 +0200 Message-Id: <20220427210345.455611-3-jolsa@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220427210345.455611-1-jolsa@kernel.org> References: <20220427210345.455611-1-jolsa@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 kalsyms 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 Signed-off-by: Jiri Olsa --- 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); + +int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *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 long 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 cnt, unsigned long *addrs) +{ + return -EOPNOTSUPP; +} #endif /* CONFIG_FUNCTION_TRACER */ 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 /* * 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 write, mutex_unlock(&ftrace_lock); return ret; } + +static int symbols_cmp(const void *a, const void *b) +{ + const char **str_a = (const char **) a; + const char **str_b = (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 = data; + + if (!bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp)) + return 0; + + addr = ftrace_location(addr); + if (!addr) + return 0; + + args->addrs[args->found++] = addr; + return args->found == 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 long *addrs) +{ + struct kallsyms_data args; + int err; + + args.addrs = addrs; + args.syms = sorted_syms; + args.cnt = cnt; + args.found = 0; + err = kallsyms_on_each_symbol(kallsyms_callback, &args); + if (err < 0) + return err; + return args.found == args.cnt ? 0 : -ESRCH; +} -- 2.35.1