Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp835449pxb; Tue, 12 Apr 2022 14:49:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHQZFxQex2BKShATtlIeVr9emKp41Ndt0g5Ap93LfCXtGs87H/emssu9jKCw7TZ2adFayV X-Received: by 2002:a17:902:7045:b0:157:144:57c5 with SMTP id h5-20020a170902704500b00157014457c5mr29805879plt.86.1649800159123; Tue, 12 Apr 2022 14:49:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649800159; cv=none; d=google.com; s=arc-20160816; b=IPfFNkOP9BDc+Zk0ucoEuXO4a8I0drxz7VVr7gXicEVbFPqbm/TJV9OnohfS/PMhhH lGv/goQ3GOrscYh3Qv3lpJQZ0820zIVmaEbeCtWcf9FBBzk3BKJOCXConSQv0qt5iNcC U67UiNS+3CJO3RiWuAEu072ZJBjuiI/9V9vuO1yRfLUb6D14Za+N0sUYGikt04eOAbRP KMWPmdse0EUPXwe2vqXVBQ2/5SjnanI9cqC1l875rmZtmdDTRR3Kr9tyX/+3aC/xktin LZZ7qyz0Iy/lkMpZJ5egP0ROns1wkvXDc/fk6ApBnvr+kVvH4HDBbYNJXV+ejlX1r9E4 XN+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=RjLMMMJSK9o4n2QWGsbdtdS98ll1ncXkFKjZkk+c5OE=; b=AWKP4LBm1QcF1T5AWVFrsZBhHEJHZKSHX+4JdO8xQh9Ficto7rjZmFzHEMqhkWTh/a CdknkIrm4O4TpfYRGOVofU5Iu4mpcMhcbYxRMdABPM5kKuxzX7waoi8yFR8eeXj1mX0Z KLN4k4/qFHWQpXmC2vKyHtBdkxKtKsIJKalGTKI7L7wSNbJxFm59p01HOono+EF5IISb wZqkma1EwxlecXK2u6lhS354jxphgsXbRnbaph/UmqdGmmrJNta0uE3gqqmBeAR5o15n AjsgtA2iU20/UKygIfQ2yZCayZXq0sV/7/CMNRbRHOy0KyVqukGq4+WQBYxb6Q+4XYL8 ok6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KwWZnyna; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id w141-20020a627b93000000b004fa88829324si12344531pfc.158.2022.04.12.14.49.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 14:49:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KwWZnyna; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0912976674; Tue, 12 Apr 2022 13:47:18 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238485AbiDIU0u (ORCPT + 99 others); Sat, 9 Apr 2022 16:26:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235417AbiDIU0o (ORCPT ); Sat, 9 Apr 2022 16:26:44 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86EE011D7B3; Sat, 9 Apr 2022 13:24:36 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id v2so3923892wrv.6; Sat, 09 Apr 2022 13:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=RjLMMMJSK9o4n2QWGsbdtdS98ll1ncXkFKjZkk+c5OE=; b=KwWZnyna7HuqcLi4U7VN2do1KHom7HabBHp9GqTZOTF4RyVSXNNkcSH2foUhxQveOB BGs45kJUGdre8uUWcQ017A0WBKoOLVWYMPhJQTRxOWw0JCFnftHULMwBUKPhJ/lBXwZU WlfXbQ3b0b/SVO33b7GvY2ZCJtgk4jjQpi69jGkzlVmV7029FM7Pf4FwkI/Le4NTBxnT nekz2DX3k9klbpZE9cqQy3FmBEhcA9rV26aGLSzAkTRVFeGD8lSF27N9mObXYEt6SK69 HL5JhRt189gnpiPHFTmhiHgMP7vB1YUDCT54mD4lFztLE9ozhGjyeZKsaY0H8cISxAaw n2rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=RjLMMMJSK9o4n2QWGsbdtdS98ll1ncXkFKjZkk+c5OE=; b=pJ1hWWqoX89vEgOy05WNdbhlA0I2yRXDvoUMoMua1Cvof2VoSMSiiVS1yvrbSklYLa wrQzfkS6EGsT8nWS/s6p+Rt9dYUZ/eKPfGu6xG/b1J+6ruZLOt94lkVpyCzE8uIRtWQn kALmlq4paYrdIu7MhwWEJy4aSQJhv3ufp1864y5C4W6aUpPSRFioOifL/ToYBpNh56iS /gbYb6NWtwJivGVmPZVVDd0ArQAKYmBGQKBIQhGOER2rCr5FjuLzl0i8wx4EFUx1OPSc HCUrxZZ1JFjrUZhX7CF2AN+rqkmZHFI4w2JwQPa/l7lj+FrUbEHUD+86xz1rllnDNrQO gdnA== X-Gm-Message-State: AOAM530CzyNRN+NJmmuEFrcRbOmTPSFhimDsVSbmk9PR6K+qiWMxLV8W TpE3S6HQZb0edvYuMN4LerQ= X-Received: by 2002:a5d:5981:0:b0:204:1da7:93d7 with SMTP id n1-20020a5d5981000000b002041da793d7mr20125083wri.621.1649535875063; Sat, 09 Apr 2022 13:24:35 -0700 (PDT) Received: from krava (94.113.247.30.static.b2b.upcbusiness.cz. [94.113.247.30]) by smtp.gmail.com with ESMTPSA id y15-20020a05600015cf00b00203e324347bsm26840249wry.102.2022.04.09.13.24.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Apr 2022 13:24:34 -0700 (PDT) Date: Sat, 9 Apr 2022 22:24:32 +0200 From: Jiri Olsa To: Alexei Starovoitov Cc: Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , netdev@vger.kernel.org, bpf@vger.kernel.org, lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh Subject: Re: [RFC bpf-next 1/4] kallsyms: Add kallsyms_lookup_names function Message-ID: References: <20220407125224.310255-1-jolsa@kernel.org> <20220407125224.310255-2-jolsa@kernel.org> <20220408231925.uc2cfeev7p6nzfww@MBP-98dd607d3435.dhcp.thefacebook.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220408231925.uc2cfeev7p6nzfww@MBP-98dd607d3435.dhcp.thefacebook.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Fri, Apr 08, 2022 at 04:19:25PM -0700, Alexei Starovoitov wrote: > On Thu, Apr 07, 2022 at 02:52:21PM +0200, Jiri Olsa wrote: > > Adding kallsyms_lookup_names 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 kallsyms_lookup_names(const char **syms, u32 cnt, > > unsigned long *addrs) > > > > Before we iterate kallsyms we sort user provided symbols by name and > > then use that in kalsyms iteration to find each kallsyms symbol in > > user provided symbols. > > > > We also check each symbol to pass ftrace_location, because this API > > will be used for fprobe symbols resolving. This can be optional in > > future if there's a need. > > > > Suggested-by: Andrii Nakryiko > > Signed-off-by: Jiri Olsa > > --- > > include/linux/kallsyms.h | 6 +++++ > > kernel/kallsyms.c | 48 ++++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 54 insertions(+) > > > > diff --git a/include/linux/kallsyms.h b/include/linux/kallsyms.h > > index ce1bd2fbf23e..5320a5e77f61 100644 > > --- a/include/linux/kallsyms.h > > +++ b/include/linux/kallsyms.h > > @@ -72,6 +72,7 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, > > #ifdef CONFIG_KALLSYMS > > /* Lookup the address for a symbol. Returns 0 if not found. */ > > unsigned long kallsyms_lookup_name(const char *name); > > +int kallsyms_lookup_names(const char **syms, u32 cnt, unsigned long *addrs); > > > > extern int kallsyms_lookup_size_offset(unsigned long addr, > > unsigned long *symbolsize, > > @@ -103,6 +104,11 @@ static inline unsigned long kallsyms_lookup_name(const char *name) > > return 0; > > } > > > > +int kallsyms_lookup_names(const char **syms, u32 cnt, unsigned long *addrs) > > +{ > > + return -ERANGE; > > +} > > + > > static inline int kallsyms_lookup_size_offset(unsigned long addr, > > unsigned long *symbolsize, > > unsigned long *offset) > > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c > > index 79f2eb617a62..a3738ddf9e87 100644 > > --- a/kernel/kallsyms.c > > +++ b/kernel/kallsyms.c > > @@ -29,6 +29,8 @@ > > #include > > #include > > #include > > +#include > > +#include > > > > /* > > * These will be re-linked against their real values > > @@ -572,6 +574,52 @@ int sprint_backtrace_build_id(char *buffer, unsigned long address) > > return __sprint_symbol(buffer, address, -1, 1, 1); > > } > > > > +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; > > + u32 cnt; > > + u32 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; > > +} > > + > > +int kallsyms_lookup_names(const char **syms, u32 cnt, unsigned long *addrs) > > +{ > > + struct kallsyms_data args; > > + > > + sort(syms, cnt, sizeof(*syms), symbols_cmp, NULL); > > It's nice to share symbols_cmp for sort and bsearch, > but messing technically input argument 'syms' like this will cause > issues sooner or later. > Lets make caller do the sort. > Unordered input will cause issue with bsearch, of course, > but it's a lesser evil. imo. ok, will move it out and make some proper comment for the function mentioning the sort requirement for syms thanks, jirka > > > + > > + args.addrs = addrs; > > + args.syms = syms; > > + args.cnt = cnt; > > + args.found = 0; > > + kallsyms_on_each_symbol(kallsyms_callback, &args); > > + > > + return args.found == args.cnt ? 0 : -EINVAL; > > +} > > + > > /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ > > struct kallsym_iter { > > loff_t pos; > > -- > > 2.35.1 > >