Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp3957884ybl; Mon, 12 Aug 2019 09:05:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqwlcIXYTNuh6qfaJCeCnCDyJ8tDq6LkZ3i2IyN5o7XWLdjXdvMJtBnm8IsGaPOCbpzftTp3 X-Received: by 2002:a17:902:e582:: with SMTP id cl2mr34059054plb.60.1565625911052; Mon, 12 Aug 2019 09:05:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565625911; cv=none; d=google.com; s=arc-20160816; b=wnRS7MixRWfjZdcMmrq1kUPyyqz3BR2scKj3wFKDsTBUfcw5FMNTcK4nkO9EAUFhaA Q+QtA/C3vnbvbarnSf4YZaWOiGdm3r/Zfiy62IcL/u75MSGJZTi0OpzdZUYUBL+JEMct sLKImGCJ5dGkuiDwcF4LzOCzeWmcpx5fQq7MxFZJrLRWoJc9X7zGWbyr2u4ULzf+8cF4 xFnYvP3RTj5IADoYyjOLw91CjkZp55FTXRytxKl18OvILds4EQ/22sCYaP37sggr9P5y fDUGppOI6kwtGgUFDxhmrOAdVkm1vesKbHvsFwTEr3r9/g9AI9T+Z+PLv3zzIPRaP0bI ifIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=1rJ0SsKCAn4Ps4kvl42htxRnl/ptd0icgYCU1kjPkA8=; b=HajqXkM0KSWilfHXsgU7II6PDx725svxfJM2RSvJQ+dpCvQW+Hm5Z2DLDeX8rzwREQ 4tU/KPx4PkwMXLyGNy09RqdRDZ5Cv0tEUeJEAdOyogO0CGd7tCWnUIeYKKI4m0aPoBnC R3VZ4p+iSoADzhxc8xIyJZu0JEsnE5Gu++rpCwwIABdur94fvzSr0BUeUTNPUtGVo22P oqBdmIYljPZTtF12DLGVzJCYZ8MkHrEqJ28a8hlZf8dGgHHySj/Iaa8PBlKDenCaYvFF WFvma6UEpMLGLPSFJ8yUQWCDSRF5w4gGZijTj9eFsjGoO5ljMkWFVC8filmvFSpVahym sjhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=eh4lij5R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h96si12449209plb.51.2019.08.12.09.04.54; Mon, 12 Aug 2019 09:05:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=eh4lij5R; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726568AbfHLQCp (ORCPT + 99 others); Mon, 12 Aug 2019 12:02:45 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:38409 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725822AbfHLQCp (ORCPT ); Mon, 12 Aug 2019 12:02:45 -0400 Received: by mail-qk1-f194.google.com with SMTP id u190so13668277qkh.5 for ; Mon, 12 Aug 2019 09:02:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1rJ0SsKCAn4Ps4kvl42htxRnl/ptd0icgYCU1kjPkA8=; b=eh4lij5RQuha4+CgRVyoKGjqU7Vl9JdikQ8hoI1bTsZSDgh41YBd9LEtcO2luBc9m+ vFKMLEn8LQ7cbvTk0YcQkXU0+jVwp2Q6Wk/t6LHv7C8wxZO/BHDa4TUb5qZutTp7/1w+ j8xfkx3kVqGmMZ5fNXqeryFH7ArCQwt+tktFrqYc6SVmNAC7HQhjF8ptHv8CZnanBtiX WzUTzd+Gk+LxSiiZFfhQoeBTzcp3qG/K1C/iEpRtmzEsFC3vO4NTXq6GgXgQGe8VZxjV CvvRMWFRQD3l1RPsdrFINiY8J0fHFEbNou03AjCnfzDX5NQ7yHojXH8DS+FMrJl3yLJ5 HVyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1rJ0SsKCAn4Ps4kvl42htxRnl/ptd0icgYCU1kjPkA8=; b=HgI2kpFZVjVpoHC1NKSUAIFdWdgMuMGekXga1bZwRRmjjPzRH7joanSkoL3p4CIjVB NswUzyHcUe17umt/I93U1mbQdRJpB3NMemhLCQH7QMQi/footRW24JNCxlajXM7OLnJD JcnGOoJaFANr/tQR7XIlyq/eKNLvXAVzG215f2eJLwLruNif22ZJlDY4SmjSZ2ow/x1l 1z5QeVJcdbMIwkIaZkQUEwxNHLbJ0VLcffU5Kc0IAEdTt5FdZApyHq0A5WQMlFF7U0wT SRI/aqBfpFq24ycgsKJ0xcRAfIYn4WKDFNm04hiXoAu/ajK+MY9gt51JmMO2ZIHwVmGV fpUQ== X-Gm-Message-State: APjAAAW10LCnou2gny2W6RTOVYyCPR+DdU8EmsEXv9cdgQia0kLKRGOs YXPmKlfsIFJCAtETfCer5GHrnhZ5sF6Hv04dhUHyhBU6ydHgPg== X-Received: by 2002:a37:b646:: with SMTP id g67mr30240549qkf.92.1565625764674; Mon, 12 Aug 2019 09:02:44 -0700 (PDT) MIME-Version: 1.0 References: <20190809214642.12078-1-dxu@dxuuu.xyz> <20190809214642.12078-3-dxu@dxuuu.xyz> In-Reply-To: <20190809214642.12078-3-dxu@dxuuu.xyz> From: Andrii Nakryiko Date: Mon, 12 Aug 2019 09:02:33 -0700 Message-ID: Subject: Re: [PATCH v2 bpf-next 2/4] libbpf: Add helpers to extract perf fd from bpf_link To: Daniel Xu Cc: Song Liu , Yonghong Song , Andrii Nakryiko , peterz@infraded.org, Ingo Molnar , Arnaldo Carvalho de Melo , Alexei Starovoitov , alexander.shishkin@linux.intel.com, Jiri Olsa , Namhyung Kim , open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 9, 2019 at 2:48 PM Daniel Xu wrote: > > It is sometimes necessary to perform ioctl's on the underlying perf fd. > There is not currently a way to extract the fd given a bpf_link, so add a > a pair of casting and getting helpers. > > The casting and getting helpers are nice because they let us define > broad categories of links that makes it clear to users what they can > expect to extract from what type of link. > > Signed-off-by: Daniel Xu > --- > tools/lib/bpf/libbpf.c | 19 +++++++++++++++++++ > tools/lib/bpf/libbpf.h | 8 ++++++++ > tools/lib/bpf/libbpf.map | 6 ++++++ > 3 files changed, 33 insertions(+) > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > index ead915aec349..f4d750863abd 100644 > --- a/tools/lib/bpf/libbpf.c > +++ b/tools/lib/bpf/libbpf.c > @@ -4004,6 +4004,25 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link) > return err; > } > > +const struct bpf_link_fd *bpf_link__as_fd(const struct bpf_link *link) > +{ > + if (!link) > + return NULL; I'm not sure about all those NULL checks everywhere. It's not really a Java, passing NULL is ok only to xxx__free() kind of functions, everything else shouldn't expect to deal with NULLs correctly, IMO. > + > + if (link->destroy != &bpf_link__destroy_perf_event) While this is clever, it doesn't handle case for raw tracepoint already. It is also anti-future proof, as when we add another use case for bpf_link__fd with different destroy callback, we'll most probably forget to update this function. So let's instead introduce enum bpf_link_type and make it standard part of "abstract" bpf_link. Please also add getter enum bpf_link_type bpf_link__type(const struct bpf_link *link) { return link->type; } to fetch it. That way users will also be able to write generic functions potentially handling multiple kinds of bpf_links (and there won't be a need to just "guess" the type of bpf_link). > + return NULL; > + > + return (struct bpf_link_fd *)link; > +} > + > +int bpf_link_fd__fd(const struct bpf_link_fd *link) > +{ > + if (!link) > + return -1; > + > + return link->fd; > +} > + > struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, > int pfd) > { > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h > index 8a9d462a6f6d..4498b6ae459a 100644 > --- a/tools/lib/bpf/libbpf.h > +++ b/tools/lib/bpf/libbpf.h > @@ -166,6 +166,14 @@ LIBBPF_API int bpf_program__unpin(struct bpf_program *prog, const char *path); > LIBBPF_API void bpf_program__unload(struct bpf_program *prog); > > struct bpf_link; > +struct bpf_link_fd; > + > +/* casting APIs */ > +LIBBPF_API const struct bpf_link_fd * > +bpf_link__as_fd(const struct bpf_link *link); > + > +/* getters APIs */ > +LIBBPF_API int bpf_link_fd__fd(const struct bpf_link_fd *link); But otherwise these new APIs look great, thanks! > > LIBBPF_API int bpf_link__destroy(struct bpf_link *link); > > diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map > index f9d316e873d8..b58dd0f0259c 100644 > --- a/tools/lib/bpf/libbpf.map > +++ b/tools/lib/bpf/libbpf.map > @@ -184,3 +184,9 @@ LIBBPF_0.0.4 { > perf_buffer__new_raw; > perf_buffer__poll; > } LIBBPF_0.0.3; > + > +LIBBPF_0.0.5 { > + global: > + bpf_link__as_fd; > + bpf_link_fd__fd; > +} LIBBPF_0.0.4; > -- > 2.20.1 >