bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link
@ 2019-08-06 23:42 Daniel Xu
  2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Daniel Xu @ 2019-08-06 23:42 UTC (permalink / raw)
  To: songliubraving, yhs, andriin; +Cc: Daniel Xu, bpf, kernel-team

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
helper for it.
---
 tools/lib/bpf/libbpf.c   | 13 +++++++++++++
 tools/lib/bpf/libbpf.h   |  1 +
 tools/lib/bpf/libbpf.map |  5 +++++
 3 files changed, 19 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ead915aec349..8469d69448ae 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4004,6 +4004,19 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link)
 	return err;
 }
 
+int bpf_link__get_perf_fd(struct bpf_link *link)
+{
+	struct bpf_link_fd *l = (void *)link;
+
+	if (!link)
+		return -1;
+
+	if (link->destroy != &bpf_link__destroy_perf_event)
+		return -1;
+
+	return l->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..5391ac95e4fa 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -168,6 +168,7 @@ LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
 struct bpf_link;
 
 LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
+LIBBPF_API int bpf_link__get_perf_fd(struct bpf_link *link);
 
 LIBBPF_API struct bpf_link *
 bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index f9d316e873d8..0f844ce29b04 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -184,3 +184,8 @@ LIBBPF_0.0.4 {
 		perf_buffer__new_raw;
 		perf_buffer__poll;
 } LIBBPF_0.0.3;
+
+LIBBPF_0.0.5 {
+	global:
+		bpf_link__get_perf_fd;
+} LIBBPF_0.0.4;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE
  2019-08-06 23:42 [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Daniel Xu
@ 2019-08-06 23:42 ` Daniel Xu
  2019-08-07  6:01   ` Yonghong Song
  2019-08-07 21:49   ` Andrii Nakryiko
  2019-08-07 18:19 ` [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Song Liu
  2019-08-07 18:57 ` Andrii Nakryiko
  2 siblings, 2 replies; 7+ messages in thread
From: Daniel Xu @ 2019-08-06 23:42 UTC (permalink / raw)
  To: songliubraving, yhs, andriin; +Cc: Daniel Xu, bpf, kernel-team

---
 tools/include/uapi/linux/perf_event.h         | 23 ++++++++++
 .../selftests/bpf/prog_tests/attach_probe.c   | 43 +++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index 7198ddd0c6b1..4a5e18606baf 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -447,6 +447,28 @@ struct perf_event_query_bpf {
 	__u32	ids[0];
 };
 
+/*
+ * Structure used by below PERF_EVENT_IOC_QUERY_KPROE command
+ * to query information about the kprobe attached to the perf
+ * event.
+ */
+struct perf_event_query_kprobe {
+       /*
+        * Size of structure for forward/backward compatibility
+        */
+       __u32   size;
+       /*
+        * Set by the kernel to indicate number of times this kprobe
+        * was temporarily disabled
+        */
+       __u64   nmissed;
+       /*
+        * Set by the kernel to indicate number of times this kprobe
+        * was hit
+        */
+       __u64   nhit;
+};
+
 /*
  * Ioctls that can be done on a perf event fd:
  */
@@ -462,6 +484,7 @@ struct perf_event_query_bpf {
 #define PERF_EVENT_IOC_PAUSE_OUTPUT		_IOW('$', 9, __u32)
 #define PERF_EVENT_IOC_QUERY_BPF		_IOWR('$', 10, struct perf_event_query_bpf *)
 #define PERF_EVENT_IOC_MODIFY_ATTRIBUTES	_IOW('$', 11, struct perf_event_attr *)
+#define PERF_EVENT_IOC_QUERY_KPROBE		_IOWR('$', 12, struct perf_event_query_kprobe *)
 
 enum perf_event_ioc_flags {
 	PERF_IOC_FLAG_GROUP		= 1U << 0,
diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
index 5ecc267d98b0..5f118e9a1469 100644
--- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c
+++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
@@ -38,9 +38,12 @@ void test_attach_probe(void)
 	struct bpf_link *kretprobe_link = NULL;
 	struct bpf_link *uprobe_link = NULL;
 	struct bpf_link *uretprobe_link = NULL;
+	int kprobe_fd, kretprobe_fd;
 	int results_map_fd;
 	size_t uprobe_offset;
 	ssize_t base_addr;
+	struct perf_event_query_kprobe kprobe_query;
+	struct perf_event_query_kprobe kretprobe_query;
 
 	base_addr = get_base_addr();
 	if (CHECK(base_addr < 0, "get_base_addr",
@@ -116,6 +119,46 @@ void test_attach_probe(void)
 	/* trigger & validate kprobe && kretprobe */
 	usleep(1);
 
+	kprobe_fd = bpf_link__get_perf_fd(kprobe_link);
+	if (CHECK(kprobe_fd < 0, "kprobe_get_perf_fd",
+	    "failed to get perf fd from kprobe link\n"))
+		goto cleanup;
+
+	kretprobe_fd = bpf_link__get_perf_fd(kretprobe_link);
+	if (CHECK(kprobe_fd < 0, "kprobe_get_perf_fd",
+	    "failed to get perf fd from kprobe link\n"))
+		goto cleanup;
+
+	memset(&kprobe_query, 0, sizeof(kprobe_query));
+	kprobe_query.size = sizeof(kprobe_query);
+	err = ioctl(kprobe_fd, PERF_EVENT_IOC_QUERY_KPROBE, &kprobe_query);
+	if (CHECK(err, "get_kprobe_ioctl",
+		  "failed to issue kprobe query ioctl\n"))
+		goto cleanup;
+	if (CHECK(kprobe_query.nmissed > 0, "get_kprobe_ioctl",
+		  "read incorect nmissed from kprobe_ioctl: %llu\n",
+		  kprobe_query.nmissed))
+		goto cleanup;
+	if (CHECK(kprobe_query.nhit <= 0, "get_kprobe_ioctl",
+		  "read incorect nhit from kprobe_ioctl: %llu\n",
+		  kprobe_query.nhit))
+		goto cleanup;
+
+	memset(&kretprobe_query, 0, sizeof(kretprobe_query));
+	kretprobe_query.size = sizeof(kretprobe_query);
+	err = ioctl(kretprobe_fd, PERF_EVENT_IOC_QUERY_KPROBE, &kretprobe_query);
+	if (CHECK(err, "get_kretprobe_ioctl",
+		  "failed to issue kprobe query ioctl\n"))
+		goto cleanup;
+	if (CHECK(kretprobe_query.nmissed > 0, "get_kretprobe_ioctl",
+		  "read incorect nmissed from kretprobe_ioctl: %llu\n",
+		  kretprobe_query.nmissed))
+		goto cleanup;
+	if (CHECK(kretprobe_query.nhit <= 0, "get_kretprobe_ioctl",
+		  "read incorect nhit from kretprobe_ioctl: %llu\n",
+		  kretprobe_query.nhit))
+		goto cleanup;
+
 	err = bpf_map_lookup_elem(results_map_fd, &kprobe_idx, &res);
 	if (CHECK(err, "get_kprobe_res",
 		  "failed to get kprobe res: %d\n", err))
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE
  2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
@ 2019-08-07  6:01   ` Yonghong Song
  2019-08-07 21:49   ` Andrii Nakryiko
  1 sibling, 0 replies; 7+ messages in thread
From: Yonghong Song @ 2019-08-07  6:01 UTC (permalink / raw)
  To: Daniel Xu, Song Liu, Andrii Nakryiko; +Cc: bpf, Kernel Team



On 8/6/19 4:42 PM, Daniel Xu wrote:
> ---
>   tools/include/uapi/linux/perf_event.h         | 23 ++++++++++
>   .../selftests/bpf/prog_tests/attach_probe.c   | 43 +++++++++++++++++++

Could you separate this into two patches?
    one patch for uapi update tools/include/uapi/linux/perf_event.h
    and the other for .../selftests/bpf/prog_tests/attach_probe.c
The reason is to help libbpf mirror sync'ing.

>   2 files changed, 66 insertions(+)
> 
> diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
> index 7198ddd0c6b1..4a5e18606baf 100644
> --- a/tools/include/uapi/linux/perf_event.h
> +++ b/tools/include/uapi/linux/perf_event.h
> @@ -447,6 +447,28 @@ struct perf_event_query_bpf {
>   	__u32	ids[0];
>   };
>   
> +/*
> + * Structure used by below PERF_EVENT_IOC_QUERY_KPROE command
> + * to query information about the kprobe attached to the perf
> + * event.
> + */
> +struct perf_event_query_kprobe {
> +       /*
> +        * Size of structure for forward/backward compatibility
> +        */
> +       __u32   size;
> +       /*
> +        * Set by the kernel to indicate number of times this kprobe
> +        * was temporarily disabled
> +        */
> +       __u64   nmissed;
> +       /*
> +        * Set by the kernel to indicate number of times this kprobe
> +        * was hit
> +        */
> +       __u64   nhit;
> +};
> +
>   /*
>    * Ioctls that can be done on a perf event fd:
>    */
> @@ -462,6 +484,7 @@ struct perf_event_query_bpf {
>   #define PERF_EVENT_IOC_PAUSE_OUTPUT		_IOW('$', 9, __u32)
>   #define PERF_EVENT_IOC_QUERY_BPF		_IOWR('$', 10, struct perf_event_query_bpf *)
>   #define PERF_EVENT_IOC_MODIFY_ATTRIBUTES	_IOW('$', 11, struct perf_event_attr *)
> +#define PERF_EVENT_IOC_QUERY_KPROBE		_IOWR('$', 12, struct perf_event_query_kprobe *)
>   
>   enum perf_event_ioc_flags {
>   	PERF_IOC_FLAG_GROUP		= 1U << 0,
> diff --git a/tools/testing/selftests/bpf/prog_tests/attach_probe.c b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
> index 5ecc267d98b0..5f118e9a1469 100644
> --- a/tools/testing/selftests/bpf/prog_tests/attach_probe.c
> +++ b/tools/testing/selftests/bpf/prog_tests/attach_probe.c
> @@ -38,9 +38,12 @@ void test_attach_probe(void)
>   	struct bpf_link *kretprobe_link = NULL;
>   	struct bpf_link *uprobe_link = NULL;
>   	struct bpf_link *uretprobe_link = NULL;
> +	int kprobe_fd, kretprobe_fd;
>   	int results_map_fd;
>   	size_t uprobe_offset;
>   	ssize_t base_addr;
> +	struct perf_event_query_kprobe kprobe_query;
> +	struct perf_event_query_kprobe kretprobe_query;

Since you are adding new fields, could you try to maintain reverse
Christmas coding style?

>   
>   	base_addr = get_base_addr();
>   	if (CHECK(base_addr < 0, "get_base_addr",
> @@ -116,6 +119,46 @@ void test_attach_probe(void)
>   	/* trigger & validate kprobe && kretprobe */
>   	usleep(1);
>   
> +	kprobe_fd = bpf_link__get_perf_fd(kprobe_link);
> +	if (CHECK(kprobe_fd < 0, "kprobe_get_perf_fd",
> +	    "failed to get perf fd from kprobe link\n"))
> +		goto cleanup;
> +
> +	kretprobe_fd = bpf_link__get_perf_fd(kretprobe_link);
> +	if (CHECK(kprobe_fd < 0, "kprobe_get_perf_fd",

typo: kprobe_fd => kretprobe_fd, kretprobe_get_perf_fd?

> +	    "failed to get perf fd from kprobe link\n"))

kretprobe link?

> +		goto cleanup;
> +
> +	memset(&kprobe_query, 0, sizeof(kprobe_query));
> +	kprobe_query.size = sizeof(kprobe_query);
> +	err = ioctl(kprobe_fd, PERF_EVENT_IOC_QUERY_KPROBE, &kprobe_query);
> +	if (CHECK(err, "get_kprobe_ioctl",
> +		  "failed to issue kprobe query ioctl\n"))
> +		goto cleanup;
> +	if (CHECK(kprobe_query.nmissed > 0, "get_kprobe_ioctl",
> +		  "read incorect nmissed from kprobe_ioctl: %llu\n",
> +		  kprobe_query.nmissed))
> +		goto cleanup;
> +	if (CHECK(kprobe_query.nhit <= 0, "get_kprobe_ioctl",

nhit is __u64, it cannot be less than 0.

> +		  "read incorect nhit from kprobe_ioctl: %llu\n",
> +		  kprobe_query.nhit))
> +		goto cleanup;
> +
> +	memset(&kretprobe_query, 0, sizeof(kretprobe_query));
> +	kretprobe_query.size = sizeof(kretprobe_query);
> +	err = ioctl(kretprobe_fd, PERF_EVENT_IOC_QUERY_KPROBE, &kretprobe_query);
> +	if (CHECK(err, "get_kretprobe_ioctl",
> +		  "failed to issue kprobe query ioctl\n"))
> +		goto cleanup;
> +	if (CHECK(kretprobe_query.nmissed > 0, "get_kretprobe_ioctl",
> +		  "read incorect nmissed from kretprobe_ioctl: %llu\n",
> +		  kretprobe_query.nmissed))
> +		goto cleanup;
> +	if (CHECK(kretprobe_query.nhit <= 0, "get_kretprobe_ioctl",

<= 0 => == 0?

> +		  "read incorect nhit from kretprobe_ioctl: %llu\n",
> +		  kretprobe_query.nhit))
> +		goto cleanup;
> +
>   	err = bpf_map_lookup_elem(results_map_fd, &kprobe_idx, &res);
>   	if (CHECK(err, "get_kprobe_res",
>   		  "failed to get kprobe res: %d\n", err))
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link
  2019-08-06 23:42 [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Daniel Xu
  2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
@ 2019-08-07 18:19 ` Song Liu
  2019-08-07 18:57 ` Andrii Nakryiko
  2 siblings, 0 replies; 7+ messages in thread
From: Song Liu @ 2019-08-07 18:19 UTC (permalink / raw)
  To: Daniel Xu; +Cc: Song Liu, Yonghong Song, Andrii Nakryiko, bpf, Kernel Team

On Tue, Aug 6, 2019 at 4:42 PM Daniel Xu <dxu@dxuuu.xyz> 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
> helper for it.

Missing "Signed-off-by" tag. Please run scripts/checkpatch.pl on the patches.

Otherwise, looks good to me.

Acked-by: Song Liu <songliubraving@fb.com>

> ---
>  tools/lib/bpf/libbpf.c   | 13 +++++++++++++
>  tools/lib/bpf/libbpf.h   |  1 +
>  tools/lib/bpf/libbpf.map |  5 +++++
>  3 files changed, 19 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index ead915aec349..8469d69448ae 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -4004,6 +4004,19 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link)
>         return err;
>  }
>
> +int bpf_link__get_perf_fd(struct bpf_link *link)
> +{
> +       struct bpf_link_fd *l = (void *)link;
> +
> +       if (!link)
> +               return -1;
> +
> +       if (link->destroy != &bpf_link__destroy_perf_event)
> +               return -1;
> +
> +       return l->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..5391ac95e4fa 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -168,6 +168,7 @@ LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
>  struct bpf_link;
>
>  LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
> +LIBBPF_API int bpf_link__get_perf_fd(struct bpf_link *link);
>
>  LIBBPF_API struct bpf_link *
>  bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index f9d316e873d8..0f844ce29b04 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -184,3 +184,8 @@ LIBBPF_0.0.4 {
>                 perf_buffer__new_raw;
>                 perf_buffer__poll;
>  } LIBBPF_0.0.3;
> +
> +LIBBPF_0.0.5 {
> +       global:
> +               bpf_link__get_perf_fd;
> +} LIBBPF_0.0.4;
> --
> 2.20.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link
  2019-08-06 23:42 [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Daniel Xu
  2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
  2019-08-07 18:19 ` [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Song Liu
@ 2019-08-07 18:57 ` Andrii Nakryiko
  2019-08-09  1:31   ` Daniel Xu
  2 siblings, 1 reply; 7+ messages in thread
From: Andrii Nakryiko @ 2019-08-07 18:57 UTC (permalink / raw)
  To: Daniel Xu; +Cc: Song Liu, Yonghong Song, Andrii Nakryiko, bpf, Kernel Team

On Tue, Aug 6, 2019 at 4:42 PM Daniel Xu <dxu@dxuuu.xyz> 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
> helper for it.
> ---

So I've been going back and forth with this approach and the
alternative one, and I think I'm leaning towards the alternative one
still.

I think it's better to have a broad "categories" of bpf_links, e.g.:

- FD-based bpf_link (which is the only one we have right now):
bpf_link_fd. It's not just for perf FD-based ones, raw tracepoint is
not, but it's still FD-based;
- for cgroup-related links (once they are added), it will be
bpf_link_cg (or something along the lines);
- there probably should be separate XDP-related bpf_link with device
ID/name inside;
- etc, whatever we'll need.

Then we can have a set of casting APIs and getter APIs that extract
useful information from specific type of bpf_link. We can also add
direct bpf_link creation API (e.g., from known FD), for cases where it
makes sense.

So something like (in libbpf.h):

struct bpf_link_fd;
struct bpf_link_cg;

/* casting APIs */
const struct bpf_link_fd *bpf_link__as_fd(const struct bpf_link *link);
const struct bpf_link_cg *bpf_link__as_cg(const struct bpf_link *link);

/* getters APIs */
int bpf_link_fd__fd(const struct bpf_link_fd *link);
int bpf_link_cg__cgroup_fd(const struct bpf_link_cg *link);

/* link factories (in addition to attach APIs) */
const struct bpf_link_fd *bpf_link__from_fd(int fd);
const struct bpf_link_cg *bpf_link__from_cg(int cg_fd, /* whatever
else necessary */);

I think this way it becomes obvious what you can expect to get of each
possible type of bpf_link and you'll have to explicitly cast to the
right type. Yet we still hide implementation details, allow no-brainer
bpf_link__destroy regardless of specific type of link (which probably
will be a common case).

Thoughts?

>  tools/lib/bpf/libbpf.c   | 13 +++++++++++++
>  tools/lib/bpf/libbpf.h   |  1 +
>  tools/lib/bpf/libbpf.map |  5 +++++
>  3 files changed, 19 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index ead915aec349..8469d69448ae 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -4004,6 +4004,19 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link)
>         return err;
>  }
>
> +int bpf_link__get_perf_fd(struct bpf_link *link)

this seems like a bit too specific name (and we should avoid "get"
words, as we do in a bunch of other libbpf APIs for getters). Maybe
just `bpf_link__fd`? This especially makes sense with a "file-based
bpf_link" abstraction I proposed above.

> +{
> +       struct bpf_link_fd *l = (void *)link;
> +
> +       if (!link)
> +               return -1;
> +
> +       if (link->destroy != &bpf_link__destroy_perf_event)
> +               return -1;
> +
> +       return l->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..5391ac95e4fa 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -168,6 +168,7 @@ LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
>  struct bpf_link;
>
>  LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
> +LIBBPF_API int bpf_link__get_perf_fd(struct bpf_link *link);
>
>  LIBBPF_API struct bpf_link *
>  bpf_program__attach_perf_event(struct bpf_program *prog, int pfd);
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index f9d316e873d8..0f844ce29b04 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -184,3 +184,8 @@ LIBBPF_0.0.4 {
>                 perf_buffer__new_raw;
>                 perf_buffer__poll;
>  } LIBBPF_0.0.3;
> +
> +LIBBPF_0.0.5 {
> +       global:
> +               bpf_link__get_perf_fd;
> +} LIBBPF_0.0.4;
> --
> 2.20.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE
  2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
  2019-08-07  6:01   ` Yonghong Song
@ 2019-08-07 21:49   ` Andrii Nakryiko
  1 sibling, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-08-07 21:49 UTC (permalink / raw)
  To: Daniel Xu; +Cc: Song Liu, Yonghong Song, Andrii Nakryiko, bpf, Kernel Team

On Tue, Aug 6, 2019 at 4:42 PM Daniel Xu <dxu@dxuuu.xyz> wrote:
>
> ---
>  tools/include/uapi/linux/perf_event.h         | 23 ++++++++++
>  .../selftests/bpf/prog_tests/attach_probe.c   | 43 +++++++++++++++++++
>  2 files changed, 66 insertions(+)
>
> diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
> index 7198ddd0c6b1..4a5e18606baf 100644
> --- a/tools/include/uapi/linux/perf_event.h
> +++ b/tools/include/uapi/linux/perf_event.h

Please split header sync into separate patch, this will cause problems
syncing libbpf to Github.

> @@ -447,6 +447,28 @@ struct perf_event_query_bpf {
>         __u32   ids[0];
>  };
>

[...]

>         ssize_t base_addr;
> +       struct perf_event_query_kprobe kprobe_query;
> +       struct perf_event_query_kprobe kretprobe_query;

Add = {} and avoid using memsets below.

>
>         base_addr = get_base_addr();
>         if (CHECK(base_addr < 0, "get_base_addr",
> @@ -116,6 +119,46 @@ void test_attach_probe(void)
>         /* trigger & validate kprobe && kretprobe */
>         usleep(1);
>

[...]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link
  2019-08-07 18:57 ` Andrii Nakryiko
@ 2019-08-09  1:31   ` Daniel Xu
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Xu @ 2019-08-09  1:31 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Song Liu, Yonghong Song, Andrii Nakryiko, bpf, Kernel Team

On Wed, Aug 7, 2019, at 11:57 AM, Andrii Nakryiko wrote:
> On Tue, Aug 6, 2019 at 4:42 PM Daniel Xu <dxu@dxuuu.xyz> 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
> > helper for it.
> > ---
> 
> So I've been going back and forth with this approach and the
> alternative one, and I think I'm leaning towards the alternative one
> still.
> 
> I think it's better to have a broad "categories" of bpf_links, e.g.:
> 
> - FD-based bpf_link (which is the only one we have right now):
> bpf_link_fd. It's not just for perf FD-based ones, raw tracepoint is
> not, but it's still FD-based;
> - for cgroup-related links (once they are added), it will be
> bpf_link_cg (or something along the lines);
> - there probably should be separate XDP-related bpf_link with device
> ID/name inside;
> - etc, whatever we'll need.
> 
> Then we can have a set of casting APIs and getter APIs that extract
> useful information from specific type of bpf_link. We can also add
> direct bpf_link creation API (e.g., from known FD), for cases where it
> makes sense.
> 
> So something like (in libbpf.h):
> 
> struct bpf_link_fd;
> struct bpf_link_cg;
> 
> /* casting APIs */
> const struct bpf_link_fd *bpf_link__as_fd(const struct bpf_link *link);
> const struct bpf_link_cg *bpf_link__as_cg(const struct bpf_link *link);
> 
> /* getters APIs */
> int bpf_link_fd__fd(const struct bpf_link_fd *link);
> int bpf_link_cg__cgroup_fd(const struct bpf_link_cg *link);
> 
> /* link factories (in addition to attach APIs) */
> const struct bpf_link_fd *bpf_link__from_fd(int fd);
> const struct bpf_link_cg *bpf_link__from_cg(int cg_fd, /* whatever
> else necessary */);
> 
> I think this way it becomes obvious what you can expect to get of each
> possible type of bpf_link and you'll have to explicitly cast to the
> right type. Yet we still hide implementation details, allow no-brainer
> bpf_link__destroy regardless of specific type of link (which probably
> will be a common case).
> 
> Thoughts?

Makes sense to me. This would probably result in a more predictable API when
new types are added. I'll make it this way in V2.

> 
> >  tools/lib/bpf/libbpf.c   | 13 +++++++++++++
> >  tools/lib/bpf/libbpf.h   |  1 +
> >  tools/lib/bpf/libbpf.map |  5 +++++
> >  3 files changed, 19 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index ead915aec349..8469d69448ae 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -4004,6 +4004,19 @@ static int bpf_link__destroy_perf_event(struct bpf_link *link)
> >         return err;
> >  }
> >
> > +int bpf_link__get_perf_fd(struct bpf_link *link)
> 
> this seems like a bit too specific name (and we should avoid "get"
> words, as we do in a bunch of other libbpf APIs for getters). Maybe
> just `bpf_link__fd`? This especially makes sense with a "file-based
> bpf_link" abstraction I proposed above.

Ok.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-08-09  1:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-06 23:42 [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Daniel Xu
2019-08-06 23:42 ` [PATCH 3/3] tracing/kprobe: Add self test for PERF_EVENT_IOC_QUERY_KPROBE Daniel Xu
2019-08-07  6:01   ` Yonghong Song
2019-08-07 21:49   ` Andrii Nakryiko
2019-08-07 18:19 ` [PATCH 2/3] libbpf: Add helper to extract perf fd from bpf_link Song Liu
2019-08-07 18:57 ` Andrii Nakryiko
2019-08-09  1:31   ` Daniel Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).