Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp18841lfv; Tue, 12 Apr 2022 15:29:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxeix0EF9FQ8pWryiP9WORTcj6AyFyw48q914H2ZHe3mO46O12/Iiqqwi7dB0PB+zDrVlDP X-Received: by 2002:a63:e557:0:b0:39d:2965:a7f0 with SMTP id z23-20020a63e557000000b0039d2965a7f0mr13718479pgj.158.1649802545228; Tue, 12 Apr 2022 15:29:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649802545; cv=none; d=google.com; s=arc-20160816; b=zlmX/nHuWkNb/CHSfjsUGhOXWBTIFmYeZQwyL+2wBooqwNJGHWihvgRiJ9MqUvAT7H YXjH0DBiQh3gWUwq0dvLT9u+n+r+/cKM+SvA3s5Hso6QBP8+FFJHzzEG3TinYkzffSJi qOyZfAVE8aTmdoRZWv2ZRtDMZ78JeQdRTSpIUmPqq4r+XhP+b0RgpyRaXvq+wjlRe+Cu d0XWlfUZ3tyCLDWfLN9Nteom9bs6opRyH+4E0ygVIuNG7Huk4z8cQVCK4IPsp5eTIzXl ndHewdsNrn5HBy9KujCpNjUOuaMVivjnXdtZKhVfvKAi9NhJbGhAjcB19fJzBSeIXIUu 8QjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=kL8nw+kkxRkF6850uohgHzWtw+kuxUaqTysptdnGkC8=; b=WXAKRvOmAAaTwmzA5fOBGUNbXFFFIphafdTEbk/mdgldNhgx5/iThG+uIXEBKFvxK8 RrtK46YZ5QXrMyXQxddS3Fu6H/QO/TEl4udjKs4PNRKgSTnkQDMy3KOLQJTMPS8DGeBo ckvUtphgG14qsCuZfPWmlRMQqn59owpQK28hrq4NC3QYzvsNB7FOjqQzNaKysXH8dA96 ZQLkGCLwdRjSuuqkpUhbXGKR3H4vHHSy1DSfP9GHBiN0koS+QPdPro+MXVcfORplVWSp yjvYfbNIspoXrobi/M5dso37PtB5EkCraZmpnZV//iSISrNHrAktu/lruL7N1e4/U8lZ WGPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JIbrFBlm; 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 g15-20020a63f40f000000b0039905cd17c7si3934749pgi.437.2022.04.12.15.29.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:29:05 -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=JIbrFBlm; 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 125C5186FA3; Tue, 12 Apr 2022 14:06:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350365AbiDKWSB (ORCPT + 99 others); Mon, 11 Apr 2022 18:18:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350418AbiDKWR5 (ORCPT ); Mon, 11 Apr 2022 18:17:57 -0400 Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECE9B26112; Mon, 11 Apr 2022 15:15:35 -0700 (PDT) Received: by mail-io1-xd32.google.com with SMTP id k25so20346908iok.8; Mon, 11 Apr 2022 15:15:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kL8nw+kkxRkF6850uohgHzWtw+kuxUaqTysptdnGkC8=; b=JIbrFBlm2+YqWyPQ9JmNckPFktJHytR/kSF7WkbgqY3lYq8prTVFwQOQkYwPem0DNe TkHoLBgTnD83MUICH5xJpN8xUxC6G/mSbHNAdxlv/SWx1lkfRxuYDjxsMj8TYLv9vVu3 CRb72ycRk2Tu0yW29dW5TqOj4zEe2s5J1dcuwIPoUpVW3PjDE3p6XG2kLogwyh2zN5vT hZWI7+bCcgTGJ3bnJ3jdrhn1VIIG0UL603yeVOijESZmny6QGIrR9IvtFS8qMP1+648C lHNuRNpUPpDPPNYcfv/ModIIiFq72dhnUSuTJyVs+oMVQgqQHXch9ZX1Oe6f/4ZgfYi4 1oiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kL8nw+kkxRkF6850uohgHzWtw+kuxUaqTysptdnGkC8=; b=iVLVvysR3aLMs7zYEYfzmirT4LjwMCvZ0S1k7HYMHE2Ky48QUrBsuFfAkz0WkRgUMY OoBu7BAmEEgDkmBJMDgZ9ePhlbM3WVyS6YGdJw3eFc68w36Lsd0EOE3ax3dT05YaVuBA +Ac32B8WTI533Bxea0qqw+V83S4G7yLdP1X1vKbtnTT089I9y6Ed02fNhJMQt5ak4COL G6Qbnd0kOEGg4ZSLz3ENXOfVgcNYipWnyeffKH+L3wPnoE86FbiGCxCVeDka3s5Cmnwe i7g618T8E+1oHnXLYbgY8q1X7WRVs8KhFZwi5pGg/SbSkXow++dUvwv2UdRK5njKrCxW 7PTA== X-Gm-Message-State: AOAM530CbQ5Ffn+ASf2k1n5HjAFhiiSlpDpkdf5A8lG3V+zeO4SbBvbt XsYZMLQHJOkKN2ZZDbHov+Ss4wvPl3Sw/vtaCUE= X-Received: by 2002:a05:6638:2104:b0:326:1e94:efa6 with SMTP id n4-20020a056638210400b003261e94efa6mr5719614jaj.234.1649715333863; Mon, 11 Apr 2022 15:15:33 -0700 (PDT) MIME-Version: 1.0 References: <20220407125224.310255-1-jolsa@kernel.org> <20220407125224.310255-2-jolsa@kernel.org> In-Reply-To: <20220407125224.310255-2-jolsa@kernel.org> From: Andrii Nakryiko Date: Mon, 11 Apr 2022 15:15:23 -0700 Message-ID: Subject: Re: [RFC bpf-next 1/4] kallsyms: Add kallsyms_lookup_names function To: Jiri Olsa Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh Content-Type: text/plain; charset="UTF-8" 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 Thu, Apr 7, 2022 at 5:52 AM 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) isn't this literally strcmp? Or compiler will actually complain about const void * vs const char *? > +{ > + 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); > + > + 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; ESRCH or ENOENT makes a bit more sense as an error? > +} > + > /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ > struct kallsym_iter { > loff_t pos; > -- > 2.35.1 >