Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1695591pxb; Wed, 9 Feb 2022 02:25:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJzjRfcJq9SsDJ4nSb6wLv6iJoKJQjGW3wa+GpwXUONgp9AKWeGdkJM55lhSM8BShQv7+61t X-Received: by 2002:a63:f15:: with SMTP id e21mr1318649pgl.571.1644402337151; Wed, 09 Feb 2022 02:25:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644402337; cv=none; d=google.com; s=arc-20160816; b=mT2isLCVyW36weFKV+vss2Up7WtLVOJTKf+Ol6Vz77ddDFaSJO0AZUa7WviH1+NKM8 BWwjOfjsU3MzjWdchB3ayGuwwwYUX9jh1cJq3ZKgn5Tbq4fQAYLYD+bA+WCaskn5pG2B yVJGUQwwmUWTgHKDzfrsUlLreebyLt+U/4AGQQQOCEYVy36EJrrJEwhBMeMH8jugZgdR XUrmWyfaN/5BrhTI6Fpir6oI73CkCMq6dlZ1lgU7O572nZgWhyxr/PW9VXpXRoeaAuGY thBXDwoOJppSbzsIQfmm6oIyXVPnQD7ByX6x4jeclRDloG+ICpCIak6+OICo/gEUYhoB iXWA== 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=F+Wu/sGGNFwRVA6o9CxafTUWjc6Pw1cGgpDABPbC3kU=; b=NJf2eOnzI/Ggc7bwGUfLB30eCp3lbf2ZWmRmoW317jV0FQpqkiIlCdDhZ2PLIF1uU2 y4MaqaUyq3TYbebr879Tqc+e+zLdiThrLj6GCuA8p6kJ66pvxvm1qzcguAZZ5xVqWwNG s5wX/QoMbU5PIIc8BUVN+ztHGGdVeiMtsRt+RuvCgTor41tXEZ8GydcKPDMVQsILlapk FGGSgirR7RgsB4FJ9Qya8wkI9SglAy3Ivn8D2qD5hxAz9avMvpr8+1JWoU4LyOUYf97U JAntTBvhSGBShTQowvh51ntzRI4gAmGjupURTEm5goVGqy3PFUFcuDikR+rUoDFaMD9M 7mzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gH5poQNA; 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=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id q6si15657029plx.478.2022.02.09.02.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 02:25:37 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=gH5poQNA; 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=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AAFF9C1DC2D2; Wed, 9 Feb 2022 01:20:11 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353164AbiBHJNF (ORCPT + 99 others); Tue, 8 Feb 2022 04:13:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240331AbiBHJNB (ORCPT ); Tue, 8 Feb 2022 04:13:01 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0F75CC03FEC0 for ; Tue, 8 Feb 2022 01:12:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644311576; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=F+Wu/sGGNFwRVA6o9CxafTUWjc6Pw1cGgpDABPbC3kU=; b=gH5poQNAp+psXERXpBdlDDmVN7Wi5FwHHIVbsZJRTY/eKGQVeEI4KMfF5Q9IsfuGexHBsK J0gzp7tYhAtAf+XuZhMqjZkvvOLuFS+jCLl9/JI4xhbcPImz8xEf8gvg0wdHLGUp5BO/2c oFoxtvJhYAM/sneCGR5/uhPt3ooK+OY= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-avGnWjRdOimiTs_D6DsVVg-1; Tue, 08 Feb 2022 04:12:54 -0500 X-MC-Unique: avGnWjRdOimiTs_D6DsVVg-1 Received: by mail-ed1-f70.google.com with SMTP id f9-20020a056402354900b0040fb9c35a02so228204edd.18 for ; Tue, 08 Feb 2022 01:12:54 -0800 (PST) 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=F+Wu/sGGNFwRVA6o9CxafTUWjc6Pw1cGgpDABPbC3kU=; b=1xQ87bFMAVKnBGrBGQSohdX2sQwjUuLhpwwkSZE9962yg4Ax0nORYKwKV/98tyXOIz LM8u2WZWDzwp0mPLdKSghYRoXzJiHhh9VEgg4oQHCnQUnORIrEynmhw9MFhRhjLBf5XI kpnsSLo7IYP72MbpGD++KzdnuK1oLDq9wyvssnP2p5C5B1CxMU5qm+0CuFRZekoX9Dq4 cu1gTcVLdJNh0L5C1EVGP6cF0QXu2mBAyrr5vyYpAbygan2HXneYGxdivwbxLMvWJcd4 vMKPiJoCQsRMGR7Y6IEJ8mhVZA49n8vK8jsbVep1O0IKdElht888duHXydY8yeMOHJsy gvTg== X-Gm-Message-State: AOAM5301k6oUccnGtOLRoeEQ0CzhhUgIrJbBMqY6wm8fyw1j2CWDRXYp hNZBcb9i4QOpf+m4LPgfJV3x6t44bFSFXJGTPRzug3M4gbb5cVOrzruPy8Ydc3AB0npIgWnzwuX +N3wErqAncAHRsmBxzlh4OClI X-Received: by 2002:a17:906:4fc1:: with SMTP id i1mr2822062ejw.248.1644311573609; Tue, 08 Feb 2022 01:12:53 -0800 (PST) X-Received: by 2002:a17:906:4fc1:: with SMTP id i1mr2822039ejw.248.1644311573396; Tue, 08 Feb 2022 01:12:53 -0800 (PST) Received: from krava (nat-pool-brq-u.redhat.com. [213.175.37.12]) by smtp.gmail.com with ESMTPSA id l7sm5760193edb.53.2022.02.08.01.12.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Feb 2022 01:12:52 -0800 (PST) Date: Tue, 8 Feb 2022 10:12:51 +0100 From: Jiri Olsa To: Andrii Nakryiko Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt , Jiri Olsa Subject: Re: [PATCH 6/8] libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes Message-ID: References: <20220202135333.190761-1-jolsa@kernel.org> <20220202135333.190761-7-jolsa@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,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 Mon, Feb 07, 2022 at 10:59:29AM -0800, Andrii Nakryiko wrote: SNIP > > +struct fprobe_resolve { > > + const char *name; > > + __u64 *addrs; > > + __u32 alloc; > > + __u32 cnt; > > +}; > > + > > +static bool glob_matches(const char *glob, const char *s) > > we've since added more generic glob_match() implementation (see > test_progs.c), let's copy/paste that one (it's actually shorter and > doesn't do hacky input args modification). Let's maybe also add '?' > handling (it's trivial). Both original code in perf and the one in > test_progs.c are GPL-2.0-only, so let's also get acks from original > authors. ok, will check > > > +{ > > + int n = strlen(glob); > > + > > + if (n == 1 && glob[0] == '*') > > + return true; > > + > > + if (glob[0] == '*' && glob[n - 1] == '*') { > > + const char *subs; > > + /* substring match */ > > + > > + /* this is hacky, but we don't want to allocate > > + * for no good reason > > + */ > > + ((char *)glob)[n - 1] = '\0'; > > + subs = strstr(s, glob + 1); > > + ((char *)glob)[n - 1] = '*'; > > + > > + return subs != NULL; > > + } else if (glob[0] == '*') { > > + size_t nn = strlen(s); > > + /* suffix match */ > > + > > + /* too short for a given suffix */ > > + if (nn < n - 1) > > + return false; > > + return strcmp(s + nn - (n - 1), glob + 1) == 0; > > + } else if (glob[n - 1] == '*') { > > + /* prefix match */ > > + return strncmp(s, glob, n - 1) == 0; > > + } else { > > + /* exact match */ > > + return strcmp(glob, s) == 0; > > + } > > +} > > + > > +static int resolve_fprobe_cb(void *arg, unsigned long long sym_addr, > > + char sym_type, const char *sym_name) > > +{ > > + struct fprobe_resolve *res = arg; > > + __u64 *p; > > + > > + if (!glob_matches(res->name, sym_name)) > > + return 0; > > + > > + if (res->cnt == res->alloc) { > > + res->alloc = max((__u32) 16, res->alloc * 3 / 2); > > + p = libbpf_reallocarray(res->addrs, res->alloc, sizeof(__u32)); > > + if (!p) > > + return -ENOMEM; > > + res->addrs = p; > > + } > > please use libbpf_ensure_mem() instead ok > > > > + res->addrs[res->cnt++] = sym_addr; > > + return 0; > > +} > > + > > +static struct bpf_link * > > +attach_fprobe_opts(const struct bpf_program *prog, > > + const char *func_name, > > func_glob or func_pattern? ok > > > + const struct bpf_kprobe_opts *kopts) > > +{ > > + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts); > > + struct fprobe_resolve res = { > > + .name = func_name, > > + }; > > + struct bpf_link *link = NULL; > > + char errmsg[STRERR_BUFSIZE]; > > + int err, link_fd, prog_fd; > > + bool retprobe; > > + > > + err = libbpf__kallsyms_parse(&res, resolve_fprobe_cb); > > + if (err) > > + goto error; > > + if (!res.cnt) { > > + err = -ENOENT; > > + goto error; > > + } > > + > > + retprobe = OPTS_GET(kopts, retprobe, false); > > + > > + opts.fprobe.addrs = (__u64) res.addrs; > > ptr_to_u64() ok > > > + opts.fprobe.cnt = res.cnt; > > + opts.flags = retprobe ? BPF_F_FPROBE_RETURN : 0; > > + > > + link = calloc(1, sizeof(*link)); > > + if (!link) { > > + err = -ENOMEM; > > + goto error; > > + } > > + link->detach = &bpf_link__detach_fd; > > + > > + prog_fd = bpf_program__fd(prog); > > + link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_FPROBE, &opts); > > + if (link_fd < 0) { > > + err = -errno; > > + pr_warn("prog '%s': failed to attach to %s: %s\n", > > + prog->name, res.name, > > + libbpf_strerror_r(err, errmsg, sizeof(errmsg))); > > + goto error; > > + } > > + link->fd = link_fd; > > + free(res.addrs); > > + return link; > > + > > +error: > > + free(link); > > + free(res.addrs); > > + return libbpf_err_ptr(err); > > +} > > + > > struct bpf_link * > > bpf_program__attach_kprobe_opts(const struct bpf_program *prog, > > const char *func_name, > > @@ -10047,6 +10166,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, > > if (!OPTS_VALID(opts, bpf_kprobe_opts)) > > return libbpf_err_ptr(-EINVAL); > > > > + if (prog->expected_attach_type == BPF_TRACE_FPROBE) > > + return attach_fprobe_opts(prog, func_name, opts); > > + > > retprobe = OPTS_GET(opts, retprobe, false); > > offset = OPTS_GET(opts, offset, 0); > > pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0); > > @@ -10112,6 +10234,14 @@ struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, > > return bpf_program__attach_kprobe_opts(prog, func_name, &opts); > > } > > > > +static int init_kprobe(struct bpf_program *prog, long cookie) > > +{ > > + /* If we have wildcard, switch to fprobe link. */ > > + if (strchr(prog->sec_name, '*')) > > ugh... :( maybe let's have a separate SEC("kprobe.multi/") and > same for kretprobe? I agree new SEC type is more clear ;-) ok thanks, jirka