Skip to content

Commit ae24345

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Implement an interface to register bpf_iter targets
The target can call bpf_iter_reg_target() to register itself. The needed information: target: target name seq_ops: the seq_file operations for the target init_seq_private target callback to initialize seq_priv during file open fini_seq_private target callback to clean up seq_priv during file release seq_priv_size: the private_data size needed by the seq_file operations The target name represents a target which provides a seq_ops for iterating objects. The target can provide two callback functions, init_seq_private and fini_seq_private, called during file open/release time. For example, /proc/net/{tcp6, ipv6_route, netlink, ...}, net name space needs to be setup properly during file open and released properly during file release. Function bpf_iter_unreg_target() is also implemented to unregister a particular target. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200509175859.2474669-1-yhs@fb.com
1 parent 8086fba commit ae24345

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎include/linux/bpf.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct seq_file;
3131
struct btf;
3232
struct btf_type;
3333
struct exception_table_entry;
34+
struct seq_operations;
3435

3536
extern struct idr btf_idr;
3637
extern spinlock_t btf_idr_lock;
@@ -1126,6 +1127,20 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd);
11261127
int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
11271128
int bpf_obj_get_user(const char __user *pathname, int flags);
11281129

1130+
typedef int (*bpf_iter_init_seq_priv_t)(void *private_data);
1131+
typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
1132+
1133+
struct bpf_iter_reg {
1134+
const char *target;
1135+
const struct seq_operations *seq_ops;
1136+
bpf_iter_init_seq_priv_t init_seq_private;
1137+
bpf_iter_fini_seq_priv_t fini_seq_private;
1138+
u32 seq_priv_size;
1139+
};
1140+
1141+
int bpf_iter_reg_target(struct bpf_iter_reg *reg_info);
1142+
void bpf_iter_unreg_target(const char *target);
1143+
11291144
int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
11301145
int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
11311146
int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,

‎kernel/bpf/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
obj-y := core.o
33
CFLAGS_core.o += $(call cc-disable-warning, override-init)
44

5-
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o
5+
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o
66
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o
77
obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o
88
obj-$(CONFIG_BPF_SYSCALL) += disasm.o

‎kernel/bpf/bpf_iter.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Copyright (c) 2020 Facebook */
3+
4+
#include <linux/fs.h>
5+
#include <linux/filter.h>
6+
#include <linux/bpf.h>
7+
8+
struct bpf_iter_target_info {
9+
struct list_head list;
10+
const char *target;
11+
const struct seq_operations *seq_ops;
12+
bpf_iter_init_seq_priv_t init_seq_private;
13+
bpf_iter_fini_seq_priv_t fini_seq_private;
14+
u32 seq_priv_size;
15+
};
16+
17+
static struct list_head targets = LIST_HEAD_INIT(targets);
18+
static DEFINE_MUTEX(targets_mutex);
19+
20+
int bpf_iter_reg_target(struct bpf_iter_reg *reg_info)
21+
{
22+
struct bpf_iter_target_info *tinfo;
23+
24+
tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
25+
if (!tinfo)
26+
return -ENOMEM;
27+
28+
tinfo->target = reg_info->target;
29+
tinfo->seq_ops = reg_info->seq_ops;
30+
tinfo->init_seq_private = reg_info->init_seq_private;
31+
tinfo->fini_seq_private = reg_info->fini_seq_private;
32+
tinfo->seq_priv_size = reg_info->seq_priv_size;
33+
INIT_LIST_HEAD(&tinfo->list);
34+
35+
mutex_lock(&targets_mutex);
36+
list_add(&tinfo->list, &targets);
37+
mutex_unlock(&targets_mutex);
38+
39+
return 0;
40+
}
41+
42+
void bpf_iter_unreg_target(const char *target)
43+
{
44+
struct bpf_iter_target_info *tinfo;
45+
bool found = false;
46+
47+
mutex_lock(&targets_mutex);
48+
list_for_each_entry(tinfo, &targets, list) {
49+
if (!strcmp(target, tinfo->target)) {
50+
list_del(&tinfo->list);
51+
kfree(tinfo);
52+
found = true;
53+
break;
54+
}
55+
}
56+
mutex_unlock(&targets_mutex);
57+
58+
WARN_ON(found == false);
59+
}

0 commit comments

Comments
 (0)