public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb•com>
To: Andrii Nakryiko <andrii.nakryiko@gmail•com>
Cc: Andrii Nakryiko <andriin@fb•com>,
	"bpf@vger•kernel.org" <bpf@vger•kernel.org>,
	"netdev@vger•kernel.org" <netdev@vger•kernel.org>,
	Alexei Starovoitov <ast@fb•com>,
	"daniel@iogearbox•net" <daniel@iogearbox•net>,
	Kernel Team <Kernel-team@fb•com>,
	"sdf@fomichev•me" <sdf@fomichev•me>,
	Song Liu <songliubraving@fb•com>
Subject: Re: [PATCH v4 bpf-next 6/9] libbpf: add raw tracepoint attach API
Date: Mon, 1 Jul 2019 23:04:43 +0000	[thread overview]
Message-ID: <cf314380-a96e-eae6-6fb5-b136c50cec71@fb.com> (raw)
In-Reply-To: <CAEf4BzYRpgE5VPwuv2zkXZ2N9BQVPASvYbtsZDM10C1kwdX3eg@mail.gmail.com>



On 7/1/19 3:26 PM, Andrii Nakryiko wrote:
> On Mon, Jul 1, 2019 at 10:13 AM Yonghong Song <yhs@fb•com> wrote:
>>
>>
>>
>> On 6/28/19 8:49 PM, Andrii Nakryiko wrote:
>>> Add a wrapper utilizing bpf_link "infrastructure" to allow attaching BPF
>>> programs to raw tracepoints.
>>>
>>> Signed-off-by: Andrii Nakryiko <andriin@fb•com>
>>> Acked-by: Song Liu <songliubraving@fb•com>
>>> ---
>>>    tools/lib/bpf/libbpf.c   | 37 +++++++++++++++++++++++++++++++++++++
>>>    tools/lib/bpf/libbpf.h   |  3 +++
>>>    tools/lib/bpf/libbpf.map |  1 +
>>>    3 files changed, 41 insertions(+)
>>>
>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>> index 8ad4f915df38..f8c7a7ecb35e 100644
>>> --- a/tools/lib/bpf/libbpf.c
>>> +++ b/tools/lib/bpf/libbpf.c
>>> @@ -4263,6 +4263,43 @@ struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog,
>>>        return link;
>>>    }
>>>
>>> +static int bpf_link__destroy_fd(struct bpf_link *link)
>>> +{
>>> +     struct bpf_link_fd *l = (void *)link;
>>> +
>>> +     return close(l->fd);
>>> +}
>>> +
>>> +struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
>>> +                                                 const char *tp_name)
>>> +{
>>> +     char errmsg[STRERR_BUFSIZE];
>>> +     struct bpf_link_fd *link;
>>> +     int prog_fd, pfd;
>>> +
>>> +     prog_fd = bpf_program__fd(prog);
>>> +     if (prog_fd < 0) {
>>> +             pr_warning("program '%s': can't attach before loaded\n",
>>> +                        bpf_program__title(prog, false));
>>> +             return ERR_PTR(-EINVAL);
>>> +     }
>>> +
>>> +     link = malloc(sizeof(*link));
>>> +     link->link.destroy = &bpf_link__destroy_fd;
>>
>> You can move the "link = malloc(...)" etc. after
>> bpf_raw_tracepoint_open(). That way, you do not need to free(link)
>> in the error case.
> 
> It's either `free(link)` here, or `close(pfd)` if malloc fails after
> we attached program. Either way extra clean up is needed. I went with
> the first one.

Okay with me.
BTW, do you want to check whether malloc() may failure and link may be NULL?

>>
>>> +
>>> +     pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
>>> +     if (pfd < 0) {
>>> +             pfd = -errno;
>>> +             free(link);
>>> +             pr_warning("program '%s': failed to attach to raw tracepoint '%s': %s\n",
>>> +                        bpf_program__title(prog, false), tp_name,
>>> +                        libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
>>> +             return ERR_PTR(pfd);
>>> +     }
>>> +     link->fd = pfd;
>>> +     return (struct bpf_link *)link;
>>> +}
>>> +
>>>    enum bpf_perf_event_ret
>>>    bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
>>>                           void **copy_mem, size_t *copy_size,
>>> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
>>> index 60611f4b4e1d..f55933784f95 100644
>>> --- a/tools/lib/bpf/libbpf.h
>>> +++ b/tools/lib/bpf/libbpf.h
>>> @@ -182,6 +182,9 @@ LIBBPF_API struct bpf_link *
>>>    bpf_program__attach_tracepoint(struct bpf_program *prog,
>>>                               const char *tp_category,
>>>                               const char *tp_name);
>>> +LIBBPF_API struct bpf_link *
>>> +bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
>>> +                                const char *tp_name);
>>>
>>>    struct bpf_insn;
>>>
>>> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
>>> index 3c618b75ef65..e6b7d4edbc93 100644
>>> --- a/tools/lib/bpf/libbpf.map
>>> +++ b/tools/lib/bpf/libbpf.map
>>> @@ -171,6 +171,7 @@ LIBBPF_0.0.4 {
>>>                bpf_object__load_xattr;
>>>                bpf_program__attach_kprobe;
>>>                bpf_program__attach_perf_event;
>>> +             bpf_program__attach_raw_tracepoint;
>>>                bpf_program__attach_tracepoint;
>>>                bpf_program__attach_uprobe;
>>>                btf_dump__dump_type;
>>>

  reply	other threads:[~2019-07-01 23:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-29  3:48 [PATCH v4 bpf-next 0/9] libbpf: add bpf_link and tracing attach APIs Andrii Nakryiko
2019-06-29  3:48 ` [PATCH v4 bpf-next 1/9] libbpf: make libbpf_strerror_r agnostic to sign of error Andrii Nakryiko
2019-06-29  3:48 ` [PATCH v4 bpf-next 2/9] libbpf: introduce concept of bpf_link Andrii Nakryiko
2019-07-01 17:01   ` Yonghong Song
2019-06-29  3:49 ` [PATCH v4 bpf-next 3/9] libbpf: add ability to attach/detach BPF program to perf event Andrii Nakryiko
2019-06-29  8:48   ` Song Liu
2019-07-01 17:03   ` Yonghong Song
2019-07-01 21:57     ` Andrii Nakryiko
2019-07-01 23:02       ` Yonghong Song
2019-06-29  3:49 ` [PATCH v4 bpf-next 4/9] libbpf: add kprobe/uprobe attach API Andrii Nakryiko
2019-06-29  8:50   ` Song Liu
2019-07-01 17:09   ` Yonghong Song
2019-07-01 22:08     ` Andrii Nakryiko
2019-06-29  3:49 ` [PATCH v4 bpf-next 5/9] libbpf: add tracepoint " Andrii Nakryiko
2019-07-01 17:10   ` Yonghong Song
2019-06-29  3:49 ` [PATCH v4 bpf-next 6/9] libbpf: add raw " Andrii Nakryiko
2019-07-01 17:13   ` Yonghong Song
2019-07-01 22:26     ` Andrii Nakryiko
2019-07-01 23:04       ` Yonghong Song [this message]
2019-07-01 23:09         ` Andrii Nakryiko
2019-06-29  3:49 ` [PATCH v4 bpf-next 7/9] selftests/bpf: switch test to new attach_perf_event API Andrii Nakryiko
2019-07-01 17:16   ` Yonghong Song
2019-07-01 22:32     ` Andrii Nakryiko
2019-07-01 23:05       ` Yonghong Song
2019-06-29  3:49 ` [PATCH v4 bpf-next 8/9] selftests/bpf: add kprobe/uprobe selftests Andrii Nakryiko
2019-07-01 17:18   ` Yonghong Song
2019-07-01 23:02     ` Andrii Nakryiko
2019-07-01 17:22   ` Yonghong Song
2019-06-29  3:49 ` [PATCH v4 bpf-next 9/9] selftests/bpf: convert existing tracepoint tests to new APIs Andrii Nakryiko
2019-07-01 16:10 ` [PATCH v4 bpf-next 0/9] libbpf: add bpf_link and tracing attach APIs Stanislav Fomichev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf314380-a96e-eae6-6fb5-b136c50cec71@fb.com \
    --to=yhs@fb$(echo .)com \
    --cc=Kernel-team@fb$(echo .)com \
    --cc=andrii.nakryiko@gmail$(echo .)com \
    --cc=andriin@fb$(echo .)com \
    --cc=ast@fb$(echo .)com \
    --cc=bpf@vger$(echo .)kernel.org \
    --cc=daniel@iogearbox$(echo .)net \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=sdf@fomichev$(echo .)me \
    --cc=songliubraving@fb$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox