Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp190643pxh; Thu, 7 Apr 2022 18:26:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEj4zOKnk0HTKH2XW2zFBow6OPqc6LG97mNi2+NY4m0Zt2b/aShPUOLrncV4/mJ8WaAk2B X-Received: by 2002:a05:6a02:106:b0:36c:96dd:8c17 with SMTP id bg6-20020a056a02010600b0036c96dd8c17mr13966178pgb.190.1649381182360; Thu, 07 Apr 2022 18:26:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649381182; cv=none; d=google.com; s=arc-20160816; b=cUpzs/rMrdRfGo4pMaghvvu5l9E1HFinJ8jI1nOfrZonpNQmriwPVZtfsev6fUY6pV j5xC7jl7ZKqv8OaUUAW+IfqEnvhscQ6OiLh+ALjZfDabTpPTQzNOHdM7/z1Uk6R3HwyD xRErXDFxLVZ+7hELqI/qJSLvRnNEyLwBDD6ZTRUKM3f8XON7aql639kSlB4I4H9vLmDm zzxyv323rtXie7+USJiq19DYV2XDuosxVrNHdvAUb3I5TfVzkxI5IgET8mqIY2+uLzYU vuU597f4g5ZfliDsnCm3HBFr4j+ZAL0Z4VBxOBARSGTTF95VQbqJltJRIp0HUL5IA1Ww I0dw== 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:subject:cc:to:from:date :dkim-signature; bh=CJqB8CwiOnXNsHP7dFFagMG7kcy1bIyDjOXVsyQRxYw=; b=eNaVoocKP2r5lD7IAuWvpeOQYm/krvmbGxRpAAdBT7tYZWhph0PWhexpUzxbBv4xPH tYpiToUQxwtJVeJDykwSjWzH0Ll580PzgUpIYlTAR/2wpp8lzIlJ7zlHJ8C3hG/SggQJ 2V5ycqaiOqwFoWChsBnNjx7t5uVjoU0OWGyGBO/FxHLL0+dnbrpwkdt8SYJvVQbZJAHc Y5Rbk3OELIMN0p1v0RXXfEtCozRfSpUJK7aYVirj1nBITTMs/curQi14gcx26NHJh1z5 dDsJXue0H07l+jjNos95ncGVDud8Yo9MA43mIBBkvNxUNukqgDqlg7TnN9mJDmBsd8qa 5cgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bR3dbSBu; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c6-20020a170902aa4600b00153b2d1655csi1129597plr.356.2022.04.07.18.26.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 18:26:22 -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=@kernel.org header.s=k20201202 header.b=bR3dbSBu; 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=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AC59C2E75AD; Thu, 7 Apr 2022 17:57:24 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233139AbiDHA7N (ORCPT + 99 others); Thu, 7 Apr 2022 20:59:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233115AbiDHA7K (ORCPT ); Thu, 7 Apr 2022 20:59:10 -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 2E79C21A0C5; Thu, 7 Apr 2022 17:57:08 -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 B26546191A; Fri, 8 Apr 2022 00:57:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15C8EC385A0; Fri, 8 Apr 2022 00:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649379427; bh=Zb8Ic9D/QGxYAR4I8vqLiookiKbN5j2NfKA35yV3Y4g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=bR3dbSBunB2p3hCsRHMiS+KD/yImN50pTPIlRWD7cGnLF4JMZm2tE4qOQPAEdj8Lt 49W4CwsvIftzvfWF8cux5e38yanGKWkAZXAq6JE6qYQYb5a6tkwgp8AfE+4+/+DRur CVh8hx4XzPj2wJqtIMKePNs+dkQNdr/JVlw6bgkknX+0IZ69Qnp4vvHPTQvAJMS4uS 0KrcVKZJdADIc0RQ7zAddqsDlKrYM2SRKclPc4YAGb8kWKQQm9N6+SeXokyHlIhC4c AvtMxJ54VVHZNnk6I3wkug/Yos+9HA4e1Scg7PcdbY6cmLCd3OIwxmMDlmQWRMNv0z Z4iaym/DSKMZw== Date: Fri, 8 Apr 2022 09:57:01 +0900 From: Masami Hiramatsu To: Jiri Olsa Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , 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: <20220408095701.54aea15c3cafcf66dd628a95@kernel.org> In-Reply-To: <20220407125224.310255-2-jolsa@kernel.org> References: <20220407125224.310255-1-jolsa@kernel.org> <20220407125224.310255-2-jolsa@kernel.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Thu, 7 Apr 2022 14:52:21 +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. I like this idea very much :-) > > 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; BTW, why do you use 'u32' for this arch independent code? I think 'size_t' will make its role clearer. > +}; > + > +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) Ditto. I think 'size_t cnt' is better. Thank you, > +{ > + struct kallsyms_data args; > + > + sort(syms, cnt, sizeof(*syms), symbols_cmp, NULL); > + > + 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 > -- Masami Hiramatsu