bpf: Add new bpf map type to store the pointer to struct perf_event
authorKaixu Xia <xiakaixu@huawei.com>
Thu, 6 Aug 2015 07:02:34 +0000 (07:02 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 10 Aug 2015 05:50:05 +0000 (22:50 -0700)
Introduce a new bpf map type 'BPF_MAP_TYPE_PERF_EVENT_ARRAY'.
This map only stores the pointer to struct perf_event. The
user space event FDs from perf_event_open() syscall are converted
to the pointer to struct perf_event and stored in map.

Signed-off-by: Kaixu Xia <xiakaixu@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/arraymap.c

index d495211d63d1f011c8c05d78a440419b87ae3116..4fc1f40707890eb1e33f2ecdf8976a7bd938d195 100644 (file)
@@ -10,6 +10,7 @@
 #include <uapi/linux/bpf.h>
 #include <linux/workqueue.h>
 #include <linux/file.h>
+#include <linux/perf_event.h>
 
 struct bpf_map;
 
index 2ce13c109b006a5504946d785adaabe563a57a5e..a1814e8e53a7a7dc20673f0e3941115f8fc4214e 100644 (file)
@@ -114,6 +114,7 @@ enum bpf_map_type {
        BPF_MAP_TYPE_HASH,
        BPF_MAP_TYPE_ARRAY,
        BPF_MAP_TYPE_PROG_ARRAY,
+       BPF_MAP_TYPE_PERF_EVENT_ARRAY,
 };
 
 enum bpf_prog_type {
index 45df6572ecfdac697c1331547335fe8c1ed2a71e..29ace107f2365c05b97f8f8aba54defeb59a6a78 100644 (file)
@@ -273,3 +273,60 @@ static int __init register_prog_array_map(void)
        return 0;
 }
 late_initcall(register_prog_array_map);
+
+static void perf_event_array_map_free(struct bpf_map *map)
+{
+       bpf_fd_array_map_clear(map);
+       fd_array_map_free(map);
+}
+
+static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
+{
+       struct perf_event *event;
+       const struct perf_event_attr *attr;
+
+       event = perf_event_get(fd);
+       if (IS_ERR(event))
+               return event;
+
+       attr = perf_event_attrs(event);
+       if (IS_ERR(attr))
+               return (void *)attr;
+
+       if (attr->type != PERF_TYPE_RAW &&
+           attr->type != PERF_TYPE_HARDWARE) {
+               perf_event_release_kernel(event);
+               return ERR_PTR(-EINVAL);
+       }
+       return event;
+}
+
+static void perf_event_fd_array_put_ptr(void *ptr)
+{
+       struct perf_event *event = ptr;
+
+       perf_event_release_kernel(event);
+}
+
+static const struct bpf_map_ops perf_event_array_ops = {
+       .map_alloc = fd_array_map_alloc,
+       .map_free = perf_event_array_map_free,
+       .map_get_next_key = array_map_get_next_key,
+       .map_lookup_elem = fd_array_map_lookup_elem,
+       .map_update_elem = fd_array_map_update_elem,
+       .map_delete_elem = fd_array_map_delete_elem,
+       .map_fd_get_ptr = perf_event_fd_array_get_ptr,
+       .map_fd_put_ptr = perf_event_fd_array_put_ptr,
+};
+
+static struct bpf_map_type_list perf_event_array_type __read_mostly = {
+       .ops = &perf_event_array_ops,
+       .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
+};
+
+static int __init register_perf_event_array_map(void)
+{
+       bpf_register_map_type(&perf_event_array_type);
+       return 0;
+}
+late_initcall(register_perf_event_array_map);