Commit e03e7ee3 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Ingo Molnar

perf/bpf: Convert perf_event_array to use struct file

Robustify refcounting.
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@infradead.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vince Weaver <vincent.weaver@maine.edu>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: vince@deater.net
Link: http://lkml.kernel.org/r/20160126045947.GA40151@ast-mbp.thefacebook.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent 828b6f0e
...@@ -729,7 +729,7 @@ extern int perf_event_init_task(struct task_struct *child); ...@@ -729,7 +729,7 @@ extern int perf_event_init_task(struct task_struct *child);
extern void perf_event_exit_task(struct task_struct *child); extern void perf_event_exit_task(struct task_struct *child);
extern void perf_event_free_task(struct task_struct *task); extern void perf_event_free_task(struct task_struct *task);
extern void perf_event_delayed_put(struct task_struct *task); extern void perf_event_delayed_put(struct task_struct *task);
extern struct perf_event *perf_event_get(unsigned int fd); extern struct file *perf_event_get(unsigned int fd);
extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event); extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
extern void perf_event_print_debug(void); extern void perf_event_print_debug(void);
extern void perf_pmu_disable(struct pmu *pmu); extern void perf_pmu_disable(struct pmu *pmu);
...@@ -1070,7 +1070,7 @@ static inline int perf_event_init_task(struct task_struct *child) { return 0; } ...@@ -1070,7 +1070,7 @@ static inline int perf_event_init_task(struct task_struct *child) { return 0; }
static inline void perf_event_exit_task(struct task_struct *child) { } static inline void perf_event_exit_task(struct task_struct *child) { }
static inline void perf_event_free_task(struct task_struct *task) { } static inline void perf_event_free_task(struct task_struct *task) { }
static inline void perf_event_delayed_put(struct task_struct *task) { } static inline void perf_event_delayed_put(struct task_struct *task) { }
static inline struct perf_event *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); } static inline struct file *perf_event_get(unsigned int fd) { return ERR_PTR(-EINVAL); }
static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event) static inline const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
{ {
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
......
...@@ -291,10 +291,13 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd) ...@@ -291,10 +291,13 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
{ {
struct perf_event *event; struct perf_event *event;
const struct perf_event_attr *attr; const struct perf_event_attr *attr;
struct file *file;
event = perf_event_get(fd); file = perf_event_get(fd);
if (IS_ERR(event)) if (IS_ERR(file))
return event; return file;
event = file->private_data;
attr = perf_event_attrs(event); attr = perf_event_attrs(event);
if (IS_ERR(attr)) if (IS_ERR(attr))
...@@ -304,24 +307,22 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd) ...@@ -304,24 +307,22 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map, int fd)
goto err; goto err;
if (attr->type == PERF_TYPE_RAW) if (attr->type == PERF_TYPE_RAW)
return event; return file;
if (attr->type == PERF_TYPE_HARDWARE) if (attr->type == PERF_TYPE_HARDWARE)
return event; return file;
if (attr->type == PERF_TYPE_SOFTWARE && if (attr->type == PERF_TYPE_SOFTWARE &&
attr->config == PERF_COUNT_SW_BPF_OUTPUT) attr->config == PERF_COUNT_SW_BPF_OUTPUT)
return event; return file;
err: err:
perf_event_release_kernel(event); fput(file);
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
} }
static void perf_event_fd_array_put_ptr(void *ptr) static void perf_event_fd_array_put_ptr(void *ptr)
{ {
struct perf_event *event = ptr; fput((struct file *)ptr);
perf_event_release_kernel(event);
} }
static const struct bpf_map_ops perf_event_array_ops = { static const struct bpf_map_ops perf_event_array_ops = {
......
...@@ -8916,21 +8916,20 @@ void perf_event_delayed_put(struct task_struct *task) ...@@ -8916,21 +8916,20 @@ void perf_event_delayed_put(struct task_struct *task)
WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
} }
struct perf_event *perf_event_get(unsigned int fd) struct file *perf_event_get(unsigned int fd)
{ {
int err; struct file *file;
struct fd f;
struct perf_event *event;
err = perf_fget_light(fd, &f); file = fget_raw(fd);
if (err) if (!file)
return ERR_PTR(err); return ERR_PTR(-EBADF);
event = f.file->private_data; if (file->f_op != &perf_fops) {
atomic_long_inc(&event->refcount); fput(file);
fdput(f); return ERR_PTR(-EBADF);
}
return event; return file;
} }
const struct perf_event_attr *perf_event_attrs(struct perf_event *event) const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
......
...@@ -191,14 +191,17 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5) ...@@ -191,14 +191,17 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
struct bpf_array *array = container_of(map, struct bpf_array, map); struct bpf_array *array = container_of(map, struct bpf_array, map);
struct perf_event *event; struct perf_event *event;
struct file *file;
if (unlikely(index >= array->map.max_entries)) if (unlikely(index >= array->map.max_entries))
return -E2BIG; return -E2BIG;
event = (struct perf_event *)array->ptrs[index]; file = (struct file *)array->ptrs[index];
if (!event) if (unlikely(!file))
return -ENOENT; return -ENOENT;
event = file->private_data;
/* make sure event is local and doesn't have pmu::count */ /* make sure event is local and doesn't have pmu::count */
if (event->oncpu != smp_processor_id() || if (event->oncpu != smp_processor_id() ||
event->pmu->count) event->pmu->count)
...@@ -228,6 +231,7 @@ static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size) ...@@ -228,6 +231,7 @@ static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
void *data = (void *) (long) r4; void *data = (void *) (long) r4;
struct perf_sample_data sample_data; struct perf_sample_data sample_data;
struct perf_event *event; struct perf_event *event;
struct file *file;
struct perf_raw_record raw = { struct perf_raw_record raw = {
.size = size, .size = size,
.data = data, .data = data,
...@@ -236,10 +240,12 @@ static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size) ...@@ -236,10 +240,12 @@ static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
if (unlikely(index >= array->map.max_entries)) if (unlikely(index >= array->map.max_entries))
return -E2BIG; return -E2BIG;
event = (struct perf_event *)array->ptrs[index]; file = (struct file *)array->ptrs[index];
if (unlikely(!event)) if (unlikely(!file))
return -ENOENT; return -ENOENT;
event = file->private_data;
if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE || if (unlikely(event->attr.type != PERF_TYPE_SOFTWARE ||
event->attr.config != PERF_COUNT_SW_BPF_OUTPUT)) event->attr.config != PERF_COUNT_SW_BPF_OUTPUT))
return -EINVAL; return -EINVAL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment