Received: by 2002:a19:f614:0:0:0:0:0 with SMTP id x20csp43573lfe; Fri, 15 Apr 2022 18:35:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyZv8X4gyXYsWXkateYDC7JEDaPQI8yS5PbTBogzYKO9eW6Q1wlRZs7tb/pLio2gJwcFyQ X-Received: by 2002:a63:fd4e:0:b0:399:3005:d0e0 with SMTP id m14-20020a63fd4e000000b003993005d0e0mr1306819pgj.276.1650072900552; Fri, 15 Apr 2022 18:35:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650072900; cv=none; d=google.com; s=arc-20160816; b=Y69Bi2Q0KCsG5UTwfzZwF+kU08DB66cycY8mxHsu9PgJ9llN+37hKUXDUwANQPNxlM jjeNxhl4YrbeCuVar141LCBdSkJ6yH4WZf41EasZCyTJ1o12HvmOQM+2acb9lcy/LYAb Je3WNEP/9BSxgtSC42FCHAV1WExZnXt/W0w91U94F+bQCk18+KkZFaXIc/9U6lgQ6/Qs nnJvw59KMrl82ovJCKW4FyKHTlsOfi6UYCIO5gRW79yMS5NkXWkYLsfWQGS3IHGiVWmd FmTM55f8Ib/EQ0hF6Icz9AV2+eSlCwpciRPFd6Opdnhat/3tc4wINwnzfGUkuWGzwU3m DzlA== 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=ISjAqbQ1dJCEYYTH9+nv8DcaaLotl3k23XBtLrbqNWg=; b=mMSBSxv3sVC0XQjqoBuOL/U1UYL+CDedgBkiJPn5GxsxSxe0cwaclDUNAfpHRknmNV vnbS1NZlaJ+F2NGhoS9CiDxcss10h5LACCIMi7sHBOpUK2CAy16s1FCA087ipDqzCzSX 42Hs+Zu3E0tsqIctbidaCW0qPMMzLsPrSbD5A+nLhNTZOLlT9KiG1ArKnCv3orTh5IAM fntvfqlNdMUP1kYukLya8QFsuesT1iqzxczuNFLwtyHRGRLbaUIndX1YTOp7Tpu1HF/K Kltia9myOwz2DXaKOm5z/mTpvsODI92KMOBy+DBkLeBt9BhzU8A7HrAZZhRCZc8TTzx4 NZow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ghfquUVY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id nv4-20020a17090b1b4400b001cb8b9109e9si3336856pjb.45.2022.04.15.18.34.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 18:35:00 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ghfquUVY; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 15E05158D97; Fri, 15 Apr 2022 18:01:36 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348173AbiDOAuE (ORCPT + 99 others); Thu, 14 Apr 2022 20:50:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234201AbiDOAuD (ORCPT ); Thu, 14 Apr 2022 20:50:03 -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 71485B822C; Thu, 14 Apr 2022 17:47:35 -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 230BAB82BF3; Fri, 15 Apr 2022 00:47:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC692C385A5; Fri, 15 Apr 2022 00:47:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649983652; bh=5hEZI1ItFtx5BzKjBL/+be3fKdvz/RR0aOlO6S3STIw=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ghfquUVYzlp3tI5eaJrYnY83EbNVIzh6Slk4tawd9fdbhgffxm7wM/iPgj/iBGLvF 5FAbHbGpeXLuOF+hc0m+zONvkP2QpIB+QogO3+DJr5vJSHl40UrqWQKEcrvC5ScT+K OdSj8sSF3C2R60qN7rFGEnmiz1Ji+9IGBe2cQzCcnrpp/VwcNCUVS0jpLgY2PRt6ax yNsqa8T3XumrOrM+fTdlpEmGhwwHRvN6Lvscrcz1OayM5MFC6mRKQSWi5ZLf1/Xocs 5n2PvEckLA9GNjRRgmy4Kn14X+b4ZZBZgC4sCl28EuE1Slnxtgg4RN/KYVPqNuvkEV t07bzDCQiPyHA== Date: Fri, 15 Apr 2022 09:47:27 +0900 From: Masami Hiramatsu To: Jiri Olsa Cc: Alexei Starovoitov , Jiri Olsa , 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: <20220415094727.2880a321bb6674d94e104110@kernel.org> In-Reply-To: References: <20220407125224.310255-1-jolsa@kernel.org> <20220407125224.310255-2-jolsa@kernel.org> <20220408231925.uc2cfeev7p6nzfww@MBP-98dd607d3435.dhcp.thefacebook.com> 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=-6.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 Hi Jiri, Sorry for replying later. On Tue, 12 Apr 2022 22:46:15 +0200 Jiri Olsa wrote: > 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. > > > > Masami, > this logic bubbles up to the register_fprobe_syms, because user > provides symbols as its argument. Can we still force this assumption > to the 'syms' array, like with the comment change below? > > FYI the bpf side does not use register_fprobe_syms, it uses > register_fprobe_ips, because it always needs ips as search > base for cookie values Hmm, in that case fprobe can call sort() in the register function. That will be much easier and safer. The bpf case, the input array will be generated by the bpftool (not by manual), so it can ensure the syms is sorted. But we don't know how fprobe user passes syms array. Then register_fprobe_syms() will always requires sort(). I don't like such redundant requirements. Thank you, -- Masami Hiramatsu