Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp15364lfv; Tue, 12 Apr 2022 15:21:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2dUaqoeHwWcEdg+sEQBcuvxaFbDhiJGiSNmuaxrdacGVFrdWj2Mqe2C50kCiUemolI/++ X-Received: by 2002:a63:e617:0:b0:382:9ad9:d829 with SMTP id g23-20020a63e617000000b003829ad9d829mr32165620pgh.553.1649802076554; Tue, 12 Apr 2022 15:21:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649802076; cv=none; d=google.com; s=arc-20160816; b=OHDecChbUuhyQhE23I0cAJhUeIQlHnr711cSOpurprQ5M3giWb8FEIPsIQ2/E6w1bJ sftKGowvf06BbYYAkHlAz1KLBFwdkAaIamkBKZx/E7/LcIgbGxHWisp0cYx8j7EAFymT Vscpz+N6CXIs8xRnZDuWgPMqpnWtM7s1sslOi9veAHMdRI5yetYpF96mB+pfLSjs/M6E eONXtVvs0rGc4LEYf03C0Yz3E/snZzUZnFQTWXkwpijfDli+TlLmLBAgQxXIoy1N8C7E lkSxVTbX9bOM6ojL3ulL9g3vGm2dGI/JAot1pCtYH93U9vxwA4SkxutzDvcjQ8iDEIlm o6Bg== 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=Ghy1UswEx6W00pg1cAv/pF841qi0AmfQEmewriQWJBs=; b=r8E1jH79YkymvfZNTJA1rVET47WOK2wFni7tPFqjMoR9SUpgruZ9ItxnesfXbC6zU9 wrgZT2pEMUC3MVZFo6Drj1xo4GDScCblEA3M/It1PntmX9Emb2iHoUV+jPKe0V6X1qYY ZIi7RvgkS9zuusqZgWGW9Zy5ydCwtyQ9kCoPfxdlp9a1mS+i2B0FMmtgNTB4MexBjnak zcPGFMM1AlmoUvsrjKrpN6p8TJVWptdDIQcp+h8rNmS49901IHMr7c8d2uH50TOpjt74 H75qxckdEIC60h98Gg5AGECIMewiqNd91WxOFapN0kimUVrzflENFMhOwvURn7xGZkaP DAkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=dcVp+hrb; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d14-20020a056a00244e00b004fa3a8e000esi13261731pfj.197.2022.04.12.15.21.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:21:16 -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=@gmail.com header.s=20210112 header.b=dcVp+hrb; 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=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 E354F6D979; Tue, 12 Apr 2022 14:01:55 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350367AbiDKWSP (ORCPT + 99 others); Mon, 11 Apr 2022 18:18:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241387AbiDKWSC (ORCPT ); Mon, 11 Apr 2022 18:18:02 -0400 Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C5452126E; Mon, 11 Apr 2022 15:15:47 -0700 (PDT) Received: by mail-io1-xd31.google.com with SMTP id q11so20378445iod.6; Mon, 11 Apr 2022 15:15:47 -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=Ghy1UswEx6W00pg1cAv/pF841qi0AmfQEmewriQWJBs=; b=dcVp+hrbmD/m/P0sVNANzMYqawG4Iygh4NY+WK9XwNBErx9AcxSG/ZhU5kh4MLNyDl OLn/f+MsMEDyrIhykWCdo6BYe5sGmgJOJU/lf07fKmlPd954bGI5rF4u7UkXpO1ZeTnI bkuycLc/gCAspPercz7Seqlhf9Uj0FYEI05lT5G3zcviNCqd2LmIrtRFXnbOGHwHTVlZ n/+hSsF+D3maVmBKJAhVJvg/G7iXKUMtObKw2Zsuv002VuRlAFzOy7ugDxTtJIVyosVK yghk1fsXau1MViZvz1XLNHth82HG+6ORoNyih8E6oLj44Nbm4aWp85vDgx6dG7iL/THD CXAw== 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=Ghy1UswEx6W00pg1cAv/pF841qi0AmfQEmewriQWJBs=; b=5turyEgoUZ1BEmG91PNaVd1q74ILjS72oH9ZG0ZToz5sXAxldlPR4B6Iy+Qru08OJB R7nCb8cJH8fChdso5K9NVuPHXo4MbQ3kIRokBiqfq7O3xABWurtYvfl0oFveExWJqxj4 N9bYZ6OiBVGF/klo7Jg7XL34Ky6YeVCa6Br2wgE3DUxn7jdEisqP1fb7c7Jh3kAYXQEy RCo6/C+JH1A9X2yH2PVBnJN02KiU85n4btngEVHQnSPY/XRP0EDFbdQ7G6M65h/qru+b fz79qQqecGxys8d2Qb02Wel47HviStBwahFDy8fTGTvRtKySbIyZJqT8rpCbzMztxjKC cQ7A== X-Gm-Message-State: AOAM5338pXOrYX/awVNYxQSvgJFdPuLbELZO7/aAtUa0uWYnoXGyzUK+ VLAyim8hSsQvSBn6cKKkYKTMPfVun4+Z2h5TGDl+FQod X-Received: by 2002:a02:cc07:0:b0:326:3976:81ad with SMTP id n7-20020a02cc07000000b00326397681admr1865761jap.237.1649715346615; Mon, 11 Apr 2022 15:15:46 -0700 (PDT) MIME-Version: 1.0 References: <20220407125224.310255-1-jolsa@kernel.org> <20220407125224.310255-4-jolsa@kernel.org> <20220408232610.nwtcuectacpwh6rk@MBP-98dd607d3435.dhcp.thefacebook.com> In-Reply-To: <20220408232610.nwtcuectacpwh6rk@MBP-98dd607d3435.dhcp.thefacebook.com> From: Andrii Nakryiko Date: Mon, 11 Apr 2022 15:15:35 -0700 Message-ID: Subject: Re: [RFC bpf-next 3/4] bpf: Resolve symbols with kallsyms_lookup_names for kprobe multi link To: Alexei Starovoitov Cc: Jiri Olsa , 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 Fri, Apr 8, 2022 at 4:26 PM Alexei Starovoitov wrote: > > On Thu, Apr 07, 2022 at 02:52:23PM +0200, Jiri Olsa wrote: > > Using kallsyms_lookup_names function to speed up symbols lookup in > > kprobe multi link attachment and replacing with it the current > > kprobe_multi_resolve_syms function. > > > > This speeds up bpftrace kprobe attachment: > > > > # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exit(); }' > > ... > > 6.5681 +- 0.0225 seconds time elapsed ( +- 0.34% ) > > > > After: > > > > # perf stat -r 5 -e cycles ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exit(); }' > > ... > > 0.5661 +- 0.0275 seconds time elapsed ( +- 4.85% ) > > > > Signed-off-by: Jiri Olsa > > --- > > kernel/trace/bpf_trace.c | 123 +++++++++++++++++++++++---------------- > > 1 file changed, 73 insertions(+), 50 deletions(-) > > > > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c > > index b26f3da943de..2602957225ba 100644 > > --- a/kernel/trace/bpf_trace.c > > +++ b/kernel/trace/bpf_trace.c > > @@ -2226,6 +2226,72 @@ struct bpf_kprobe_multi_run_ctx { > > unsigned long entry_ip; > > }; > > > > +struct user_syms { > > + const char **syms; > > + char *buf; > > +}; > > + > > +static int copy_user_syms(struct user_syms *us, void __user *usyms, u32 cnt) > > +{ > > + const char __user **usyms_copy = NULL; > > + const char **syms = NULL; > > + char *buf = NULL, *p; > > + int err = -EFAULT; > > + unsigned int i; > > + size_t size; > > + > > + size = cnt * sizeof(*usyms_copy); > > + > > + usyms_copy = kvmalloc(size, GFP_KERNEL); > > + if (!usyms_copy) > > + return -ENOMEM; > > + > > + if (copy_from_user(usyms_copy, usyms, size)) > > + goto error; > > + > > + err = -ENOMEM; > > + syms = kvmalloc(size, GFP_KERNEL); > > + if (!syms) > > + goto error; > > + > > + /* TODO this potentially allocates lot of memory (~6MB in my tests > > + * with attaching ~40k functions). I haven't seen this to fail yet, > > + * but it could be changed to allocate memory gradually if needed. > > + */ > > Why would 6MB kvmalloc fail? > If we don't have such memory the kernel will be ooming soon anyway. > I don't think we'll see this kvmalloc triggering oom in practice. > The verifier allocates a lot more memory to check large programs. > > imo this approach is fine. It's simple. > Trying to do gradual alloc with realloc would be just guessing. > > Another option would be to ask user space (libbpf) to do the sort. > There are pros and cons. > This vmalloc+sort is slightly better imo. Totally agree, the simpler the better. Also when you are attaching to tens of thousands of probes, 6MB isn't a lot of memory for whatever you are trying to do, anyways :) Even if libbpf had to sort it, kernel would probably have to validate that. Also for binary search you'd still need to read in the string itself, but if you'd do this "on demand", we are adding TOCTOU and other headaches. Simple is good. > > > + size = cnt * KSYM_NAME_LEN; > > + buf = kvmalloc(size, GFP_KERNEL); > > + if (!buf) > > + goto error; > > + > > + for (p = buf, i = 0; i < cnt; i++) { > > + err = strncpy_from_user(p, usyms_copy[i], KSYM_NAME_LEN); > > + if (err == KSYM_NAME_LEN) > > + err = -E2BIG; > > + if (err < 0) > > + goto error; [...]