Skip to content

Commit d46edd6

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Sharing bpf runtime stats with BPF_ENABLE_STATS
Currently, sysctl kernel.bpf_stats_enabled controls BPF runtime stats. Typical userspace tools use kernel.bpf_stats_enabled as follows: 1. Enable kernel.bpf_stats_enabled; 2. Check program run_time_ns; 3. Sleep for the monitoring period; 4. Check program run_time_ns again, calculate the difference; 5. Disable kernel.bpf_stats_enabled. The problem with this approach is that only one userspace tool can toggle this sysctl. If multiple tools toggle the sysctl at the same time, the measurement may be inaccurate. To fix this problem while keep backward compatibility, introduce a new bpf command BPF_ENABLE_STATS. On success, this command enables stats and returns a valid fd. BPF_ENABLE_STATS takes argument "type". Currently, only one type, BPF_STATS_RUN_TIME, is supported. We can extend the command to support other types of stats in the future. With BPF_ENABLE_STATS, user space tool would have the following flow: 1. Get a fd with BPF_ENABLE_STATS, and make sure it is valid; 2. Check program run_time_ns; 3. Sleep for the monitoring period; 4. Check program run_time_ns again, calculate the difference; 5. Close the fd. Signed-off-by: Song Liu <songliubraving@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200430071506.1408910-2-songliubraving@fb.com
1 parent c321022 commit d46edd6

5 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ _out: \
987987

988988
#ifdef CONFIG_BPF_SYSCALL
989989
DECLARE_PER_CPU(int, bpf_prog_active);
990+
extern struct mutex bpf_stats_enabled_mutex;
990991

991992
/*
992993
* Block execution of BPF programs attached to instrumentation (perf,

‎include/uapi/linux/bpf.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum bpf_cmd {
115115
BPF_LINK_UPDATE,
116116
BPF_LINK_GET_FD_BY_ID,
117117
BPF_LINK_GET_NEXT_ID,
118+
BPF_ENABLE_STATS,
118119
};
119120

120121
enum bpf_map_type {
@@ -390,6 +391,12 @@ enum {
390391
*/
391392
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
392393

394+
/* type for BPF_ENABLE_STATS */
395+
enum bpf_stats_type {
396+
/* enabled run_time_ns and run_cnt */
397+
BPF_STATS_RUN_TIME = 0,
398+
};
399+
393400
enum bpf_stack_build_id_status {
394401
/* user space need an empty entry to identify end of a trace */
395402
BPF_STACK_BUILD_ID_EMPTY = 0,
@@ -601,6 +608,10 @@ union bpf_attr {
601608
__u32 old_prog_fd;
602609
} link_update;
603610

611+
struct { /* struct used by BPF_ENABLE_STATS command */
612+
__u32 type;
613+
} enable_stats;
614+
604615
} __attribute__((aligned(8)));
605616

606617
/* The description below is an attempt at providing documentation to eBPF

‎kernel/bpf/syscall.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3872,6 +3872,60 @@ static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
38723872
return fd;
38733873
}
38743874

3875+
DEFINE_MUTEX(bpf_stats_enabled_mutex);
3876+
3877+
static int bpf_stats_release(struct inode *inode, struct file *file)
3878+
{
3879+
mutex_lock(&bpf_stats_enabled_mutex);
3880+
static_key_slow_dec(&bpf_stats_enabled_key.key);
3881+
mutex_unlock(&bpf_stats_enabled_mutex);
3882+
return 0;
3883+
}
3884+
3885+
static const struct file_operations bpf_stats_fops = {
3886+
.release = bpf_stats_release,
3887+
};
3888+
3889+
static int bpf_enable_runtime_stats(void)
3890+
{
3891+
int fd;
3892+
3893+
mutex_lock(&bpf_stats_enabled_mutex);
3894+
3895+
/* Set a very high limit to avoid overflow */
3896+
if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
3897+
mutex_unlock(&bpf_stats_enabled_mutex);
3898+
return -EBUSY;
3899+
}
3900+
3901+
fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
3902+
if (fd >= 0)
3903+
static_key_slow_inc(&bpf_stats_enabled_key.key);
3904+
3905+
mutex_unlock(&bpf_stats_enabled_mutex);
3906+
return fd;
3907+
}
3908+
3909+
#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
3910+
3911+
static int bpf_enable_stats(union bpf_attr *attr)
3912+
{
3913+
3914+
if (CHECK_ATTR(BPF_ENABLE_STATS))
3915+
return -EINVAL;
3916+
3917+
if (!capable(CAP_SYS_ADMIN))
3918+
return -EPERM;
3919+
3920+
switch (attr->enable_stats.type) {
3921+
case BPF_STATS_RUN_TIME:
3922+
return bpf_enable_runtime_stats();
3923+
default:
3924+
break;
3925+
}
3926+
return -EINVAL;
3927+
}
3928+
38753929
SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
38763930
{
38773931
union bpf_attr attr;
@@ -3996,6 +4050,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz
39964050
err = bpf_obj_get_next_id(&attr, uattr,
39974051
&link_idr, &link_idr_lock);
39984052
break;
4053+
case BPF_ENABLE_STATS:
4054+
err = bpf_enable_stats(&attr);
4055+
break;
39994056
default:
40004057
err = -EINVAL;
40014058
break;

‎kernel/sysctl.c‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,40 @@ static int max_extfrag_threshold = 1000;
201201

202202
#endif /* CONFIG_SYSCTL */
203203

204+
#ifdef CONFIG_BPF_SYSCALL
205+
static int bpf_stats_handler(struct ctl_table *table, int write,
206+
void __user *buffer, size_t *lenp,
207+
loff_t *ppos)
208+
{
209+
struct static_key *key = (struct static_key *)table->data;
210+
static int saved_val;
211+
int val, ret;
212+
struct ctl_table tmp = {
213+
.data = &val,
214+
.maxlen = sizeof(val),
215+
.mode = table->mode,
216+
.extra1 = SYSCTL_ZERO,
217+
.extra2 = SYSCTL_ONE,
218+
};
219+
220+
if (write && !capable(CAP_SYS_ADMIN))
221+
return -EPERM;
222+
223+
mutex_lock(&bpf_stats_enabled_mutex);
224+
val = saved_val;
225+
ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
226+
if (write && !ret && val != saved_val) {
227+
if (val)
228+
static_key_slow_inc(key);
229+
else
230+
static_key_slow_dec(key);
231+
saved_val = val;
232+
}
233+
mutex_unlock(&bpf_stats_enabled_mutex);
234+
return ret;
235+
}
236+
#endif
237+
204238
/*
205239
* /proc/sys support
206240
*/
@@ -2549,7 +2583,7 @@ static struct ctl_table kern_table[] = {
25492583
.data = &bpf_stats_enabled_key.key,
25502584
.maxlen = sizeof(bpf_stats_enabled_key),
25512585
.mode = 0644,
2552-
.proc_handler = proc_do_static_key,
2586+
.proc_handler = bpf_stats_handler,
25532587
},
25542588
#endif
25552589
#if defined(CONFIG_TREE_RCU)

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ enum bpf_cmd {
115115
BPF_LINK_UPDATE,
116116
BPF_LINK_GET_FD_BY_ID,
117117
BPF_LINK_GET_NEXT_ID,
118+
BPF_ENABLE_STATS,
118119
};
119120

120121
enum bpf_map_type {
@@ -390,6 +391,12 @@ enum {
390391
*/
391392
#define BPF_F_QUERY_EFFECTIVE (1U << 0)
392393

394+
/* type for BPF_ENABLE_STATS */
395+
enum bpf_stats_type {
396+
/* enabled run_time_ns and run_cnt */
397+
BPF_STATS_RUN_TIME = 0,
398+
};
399+
393400
enum bpf_stack_build_id_status {
394401
/* user space need an empty entry to identify end of a trace */
395402
BPF_STACK_BUILD_ID_EMPTY = 0,
@@ -601,6 +608,10 @@ union bpf_attr {
601608
__u32 old_prog_fd;
602609
} link_update;
603610

611+
struct { /* struct used by BPF_ENABLE_STATS command */
612+
__u32 type;
613+
} enable_stats;
614+
604615
} __attribute__((aligned(8)));
605616

606617
/* The description below is an attempt at providing documentation to eBPF

0 commit comments

Comments
 (0)