Skip to content

Commit 0515e59

Browse files
4astdavem330
authored andcommitted
bpf: introduce BPF_PROG_TYPE_PERF_EVENT program type
Introduce BPF_PROG_TYPE_PERF_EVENT programs that can be attached to HW and SW perf events (PERF_TYPE_HARDWARE and PERF_TYPE_SOFTWARE correspondingly in uapi/linux/perf_event.h) The program visible context meta structure is struct bpf_perf_event_data { struct pt_regs regs; __u64 sample_period; }; which is accessible directly from the program: int bpf_prog(struct bpf_perf_event_data *ctx) { ... ctx->sample_period ... ... ctx->regs.ip ... } The bpf verifier rewrites the accesses into kernel internal struct bpf_perf_event_data_kern which allows changing struct perf_sample_data without affecting bpf programs. New fields can be added to the end of struct bpf_perf_event_data in the future. Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ea2e7ce commit 0515e59

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

include/linux/perf_event.h

+5
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,11 @@ struct perf_output_handle {
788788
int page;
789789
};
790790

791+
struct bpf_perf_event_data_kern {
792+
struct pt_regs *regs;
793+
struct perf_sample_data *data;
794+
};
795+
791796
#ifdef CONFIG_CGROUP_PERF
792797

793798
/*

include/uapi/linux/Kbuild

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ header-y += binfmts.h
7171
header-y += blkpg.h
7272
header-y += blktrace_api.h
7373
header-y += bpf_common.h
74+
header-y += bpf_perf_event.h
7475
header-y += bpf.h
7576
header-y += bpqether.h
7677
header-y += bsg.h

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ enum bpf_prog_type {
9595
BPF_PROG_TYPE_SCHED_ACT,
9696
BPF_PROG_TYPE_TRACEPOINT,
9797
BPF_PROG_TYPE_XDP,
98+
BPF_PROG_TYPE_PERF_EVENT,
9899
};
99100

100101
#define BPF_PSEUDO_MAP_FD 1

include/uapi/linux/bpf_perf_event.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2016 Facebook
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*/
7+
#ifndef _UAPI__LINUX_BPF_PERF_EVENT_H__
8+
#define _UAPI__LINUX_BPF_PERF_EVENT_H__
9+
10+
#include <linux/types.h>
11+
#include <linux/ptrace.h>
12+
13+
struct bpf_perf_event_data {
14+
struct pt_regs regs;
15+
__u64 sample_period;
16+
};
17+
18+
#endif /* _UAPI__LINUX_BPF_PERF_EVENT_H__ */

kernel/trace/bpf_trace.c

+61
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Copyright (c) 2011-2015 PLUMgrid, http://plumgrid.com
2+
* Copyright (c) 2016 Facebook
23
*
34
* This program is free software; you can redistribute it and/or
45
* modify it under the terms of version 2 of the GNU General Public
@@ -8,6 +9,7 @@
89
#include <linux/types.h>
910
#include <linux/slab.h>
1011
#include <linux/bpf.h>
12+
#include <linux/bpf_perf_event.h>
1113
#include <linux/filter.h>
1214
#include <linux/uaccess.h>
1315
#include <linux/ctype.h>
@@ -552,10 +554,69 @@ static struct bpf_prog_type_list tracepoint_tl = {
552554
.type = BPF_PROG_TYPE_TRACEPOINT,
553555
};
554556

557+
static bool pe_prog_is_valid_access(int off, int size, enum bpf_access_type type,
558+
enum bpf_reg_type *reg_type)
559+
{
560+
if (off < 0 || off >= sizeof(struct bpf_perf_event_data))
561+
return false;
562+
if (type != BPF_READ)
563+
return false;
564+
if (off % size != 0)
565+
return false;
566+
if (off == offsetof(struct bpf_perf_event_data, sample_period)) {
567+
if (size != sizeof(u64))
568+
return false;
569+
} else {
570+
if (size != sizeof(long))
571+
return false;
572+
}
573+
return true;
574+
}
575+
576+
static u32 pe_prog_convert_ctx_access(enum bpf_access_type type, int dst_reg,
577+
int src_reg, int ctx_off,
578+
struct bpf_insn *insn_buf,
579+
struct bpf_prog *prog)
580+
{
581+
struct bpf_insn *insn = insn_buf;
582+
583+
switch (ctx_off) {
584+
case offsetof(struct bpf_perf_event_data, sample_period):
585+
BUILD_BUG_ON(FIELD_SIZEOF(struct perf_sample_data, period) != sizeof(u64));
586+
*insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct bpf_perf_event_data_kern, data)),
587+
dst_reg, src_reg,
588+
offsetof(struct bpf_perf_event_data_kern, data));
589+
*insn++ = BPF_LDX_MEM(BPF_DW, dst_reg, dst_reg,
590+
offsetof(struct perf_sample_data, period));
591+
break;
592+
default:
593+
*insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct bpf_perf_event_data_kern, regs)),
594+
dst_reg, src_reg,
595+
offsetof(struct bpf_perf_event_data_kern, regs));
596+
*insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(long)),
597+
dst_reg, dst_reg, ctx_off);
598+
break;
599+
}
600+
601+
return insn - insn_buf;
602+
}
603+
604+
static const struct bpf_verifier_ops perf_event_prog_ops = {
605+
.get_func_proto = tp_prog_func_proto,
606+
.is_valid_access = pe_prog_is_valid_access,
607+
.convert_ctx_access = pe_prog_convert_ctx_access,
608+
};
609+
610+
static struct bpf_prog_type_list perf_event_tl = {
611+
.ops = &perf_event_prog_ops,
612+
.type = BPF_PROG_TYPE_PERF_EVENT,
613+
};
614+
555615
static int __init register_kprobe_prog_ops(void)
556616
{
557617
bpf_register_prog_type(&kprobe_tl);
558618
bpf_register_prog_type(&tracepoint_tl);
619+
bpf_register_prog_type(&perf_event_tl);
559620
return 0;
560621
}
561622
late_initcall(register_kprobe_prog_ops);

0 commit comments

Comments
 (0)