History

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.

Yonghong Song committed 6 years ago

Commit ae243451 parent 8086fba3 files · +75 −1Browse files

Showing the changes to kernel (2 of 3 files). Show all

Unified · Split

2 files
  1. kernel/bpf/Makefile +1 −1
  2. kernel/bpf/bpf_iter.c +59 −0
kernel/bpf/Makefile+1−1
@@ -2,7 +2,7 @@ · 1 unmodified line
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+59−0
@@ -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+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t