bpf: net: Refactor bpf_iter target registration
authorYonghong Song <yhs@fb.com>
Wed, 13 May 2020 18:02:19 +0000 (11:02 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 13 May 2020 19:30:50 +0000 (12:30 -0700)
Currently bpf_iter_reg_target takes parameters from target
and allocates memory to save them. This is really not
necessary, esp. in the future we may grow information
passed from targets to bpf_iter manager.

The patch refactors the code so target reg_info
becomes static and bpf_iter manager can just take
a reference to it.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200513180219.2949605-1-yhs@fb.com
include/linux/bpf.h
kernel/bpf/bpf_iter.c
kernel/bpf/map_iter.c
kernel/bpf/task_iter.c
net/ipv6/route.c
net/netlink/af_netlink.c

index ab94dfd8826fabc00597cc4e64238758db4fedba..6fa773e2d1bfbf483c061893b94cf123195080dd 100644 (file)
@@ -1153,7 +1153,7 @@ struct bpf_iter_meta {
        u64 seq_num;
 };
 
-int bpf_iter_reg_target(struct bpf_iter_reg *reg_info);
+int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
 void bpf_iter_unreg_target(const char *target);
 bool bpf_iter_prog_supported(struct bpf_prog *prog);
 int bpf_iter_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
index 0a45a6cdfabdb5fde0f716825fb3b369b4e75c83..051fb8cab62a196ed27de4864e3023d0ca4fe016 100644 (file)
@@ -8,11 +8,7 @@
 
 struct bpf_iter_target_info {
        struct list_head list;
-       const char *target;
-       const struct seq_operations *seq_ops;
-       bpf_iter_init_seq_priv_t init_seq_private;
-       bpf_iter_fini_seq_priv_t fini_seq_private;
-       u32 seq_priv_size;
+       const struct bpf_iter_reg *reg_info;
        u32 btf_id;     /* cached value */
 };
 
@@ -222,8 +218,8 @@ static int iter_release(struct inode *inode, struct file *file)
        iter_priv = container_of(seq->private, struct bpf_iter_priv_data,
                                 target_private);
 
-       if (iter_priv->tinfo->fini_seq_private)
-               iter_priv->tinfo->fini_seq_private(seq->private);
+       if (iter_priv->tinfo->reg_info->fini_seq_private)
+               iter_priv->tinfo->reg_info->fini_seq_private(seq->private);
 
        bpf_prog_put(iter_priv->prog);
        seq->private = iter_priv;
@@ -238,7 +234,12 @@ const struct file_operations bpf_iter_fops = {
        .release        = iter_release,
 };
 
-int bpf_iter_reg_target(struct bpf_iter_reg *reg_info)
+/* The argument reg_info will be cached in bpf_iter_target_info.
+ * The common practice is to declare target reg_info as
+ * a const static variable and passed as an argument to
+ * bpf_iter_reg_target().
+ */
+int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info)
 {
        struct bpf_iter_target_info *tinfo;
 
@@ -246,11 +247,7 @@ int bpf_iter_reg_target(struct bpf_iter_reg *reg_info)
        if (!tinfo)
                return -ENOMEM;
 
-       tinfo->target = reg_info->target;
-       tinfo->seq_ops = reg_info->seq_ops;
-       tinfo->init_seq_private = reg_info->init_seq_private;
-       tinfo->fini_seq_private = reg_info->fini_seq_private;
-       tinfo->seq_priv_size = reg_info->seq_priv_size;
+       tinfo->reg_info = reg_info;
        INIT_LIST_HEAD(&tinfo->list);
 
        mutex_lock(&targets_mutex);
@@ -267,7 +264,7 @@ void bpf_iter_unreg_target(const char *target)
 
        mutex_lock(&targets_mutex);
        list_for_each_entry(tinfo, &targets, list) {
-               if (!strcmp(target, tinfo->target)) {
+               if (!strcmp(target, tinfo->reg_info->target)) {
                        list_del(&tinfo->list);
                        kfree(tinfo);
                        found = true;
@@ -303,7 +300,7 @@ bool bpf_iter_prog_supported(struct bpf_prog *prog)
                        supported = true;
                        break;
                }
-               if (!strcmp(attach_fname + prefix_len, tinfo->target)) {
+               if (!strcmp(attach_fname + prefix_len, tinfo->reg_info->target)) {
                        cache_btf_id(tinfo, prog);
                        supported = true;
                        break;
@@ -431,15 +428,16 @@ static int prepare_seq_file(struct file *file, struct bpf_iter_link *link)
 
        tinfo = link->tinfo;
        total_priv_dsize = offsetof(struct bpf_iter_priv_data, target_private) +
-                          tinfo->seq_priv_size;
-       priv_data = __seq_open_private(file, tinfo->seq_ops, total_priv_dsize);
+                          tinfo->reg_info->seq_priv_size;
+       priv_data = __seq_open_private(file, tinfo->reg_info->seq_ops,
+                                      total_priv_dsize);
        if (!priv_data) {
                err = -ENOMEM;
                goto release_prog;
        }
 
-       if (tinfo->init_seq_private) {
-               err = tinfo->init_seq_private(priv_data->target_private);
+       if (tinfo->reg_info->init_seq_private) {
+               err = tinfo->reg_info->init_seq_private(priv_data->target_private);
                if (err)
                        goto release_seq_file;
        }
index 8162e0c00b9f43c84797dc8206326db283444866..c6216a5fe56e779a0b15631dca729f3f3bdced6f 100644 (file)
@@ -81,17 +81,17 @@ static const struct seq_operations bpf_map_seq_ops = {
        .show   = bpf_map_seq_show,
 };
 
+static const struct bpf_iter_reg bpf_map_reg_info = {
+       .target                 = "bpf_map",
+       .seq_ops                = &bpf_map_seq_ops,
+       .init_seq_private       = NULL,
+       .fini_seq_private       = NULL,
+       .seq_priv_size          = sizeof(struct bpf_iter_seq_map_info),
+};
+
 static int __init bpf_map_iter_init(void)
 {
-       struct bpf_iter_reg reg_info = {
-               .target                 = "bpf_map",
-               .seq_ops                = &bpf_map_seq_ops,
-               .init_seq_private       = NULL,
-               .fini_seq_private       = NULL,
-               .seq_priv_size          = sizeof(struct bpf_iter_seq_map_info),
-       };
-
-       return bpf_iter_reg_target(&reg_info);
+       return bpf_iter_reg_target(&bpf_map_reg_info);
 }
 
 late_initcall(bpf_map_iter_init);
index aeed662d8451762aacb278c1d863565d3ad2a89e..bd7bfd83d9e0c23b14637a6e8d48c61db9fe7cee 100644 (file)
@@ -306,22 +306,24 @@ static const struct seq_operations task_file_seq_ops = {
        .show   = task_file_seq_show,
 };
 
+static const struct bpf_iter_reg task_reg_info = {
+       .target                 = "task",
+       .seq_ops                = &task_seq_ops,
+       .init_seq_private       = init_seq_pidns,
+       .fini_seq_private       = fini_seq_pidns,
+       .seq_priv_size          = sizeof(struct bpf_iter_seq_task_info),
+};
+
+static const struct bpf_iter_reg task_file_reg_info = {
+       .target                 = "task_file",
+       .seq_ops                = &task_file_seq_ops,
+       .init_seq_private       = init_seq_pidns,
+       .fini_seq_private       = fini_seq_pidns,
+       .seq_priv_size          = sizeof(struct bpf_iter_seq_task_file_info),
+};
+
 static int __init task_iter_init(void)
 {
-       struct bpf_iter_reg task_file_reg_info = {
-               .target                 = "task_file",
-               .seq_ops                = &task_file_seq_ops,
-               .init_seq_private       = init_seq_pidns,
-               .fini_seq_private       = fini_seq_pidns,
-               .seq_priv_size          = sizeof(struct bpf_iter_seq_task_file_info),
-       };
-       struct bpf_iter_reg task_reg_info = {
-               .target                 = "task",
-               .seq_ops                = &task_seq_ops,
-               .init_seq_private       = init_seq_pidns,
-               .fini_seq_private       = fini_seq_pidns,
-               .seq_priv_size          = sizeof(struct bpf_iter_seq_task_info),
-       };
        int ret;
 
        ret = bpf_iter_reg_target(&task_reg_info);
index 25f6d3e619d078d5bece7cb848466fd80dbdf4b1..6ad2fa51a23a1597098918789ae24dbc2613a95d 100644 (file)
@@ -6397,17 +6397,17 @@ void __init ip6_route_init_special_entries(void)
 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
 DEFINE_BPF_ITER_FUNC(ipv6_route, struct bpf_iter_meta *meta, struct fib6_info *rt)
 
+static const struct bpf_iter_reg ipv6_route_reg_info = {
+       .target                 = "ipv6_route",
+       .seq_ops                = &ipv6_route_seq_ops,
+       .init_seq_private       = bpf_iter_init_seq_net,
+       .fini_seq_private       = bpf_iter_fini_seq_net,
+       .seq_priv_size          = sizeof(struct ipv6_route_iter),
+};
+
 static int __init bpf_iter_register(void)
 {
-       struct bpf_iter_reg reg_info = {
-               .target                 = "ipv6_route",
-               .seq_ops                = &ipv6_route_seq_ops,
-               .init_seq_private       = bpf_iter_init_seq_net,
-               .fini_seq_private       = bpf_iter_fini_seq_net,
-               .seq_priv_size          = sizeof(struct ipv6_route_iter),
-       };
-
-       return bpf_iter_reg_target(&reg_info);
+       return bpf_iter_reg_target(&ipv6_route_reg_info);
 }
 
 static void bpf_iter_unregister(void)
index 33cda9baa979595999f2e2d7944f6b0307571df8..839827227e98492332c4861f8a2258b577d947b9 100644 (file)
@@ -2803,17 +2803,17 @@ static const struct rhashtable_params netlink_rhashtable_params = {
 };
 
 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
+static const struct bpf_iter_reg netlink_reg_info = {
+       .target                 = "netlink",
+       .seq_ops                = &netlink_seq_ops,
+       .init_seq_private       = bpf_iter_init_seq_net,
+       .fini_seq_private       = bpf_iter_fini_seq_net,
+       .seq_priv_size          = sizeof(struct nl_seq_iter),
+};
+
 static int __init bpf_iter_register(void)
 {
-       struct bpf_iter_reg reg_info = {
-               .target                 = "netlink",
-               .seq_ops                = &netlink_seq_ops,
-               .init_seq_private       = bpf_iter_init_seq_net,
-               .fini_seq_private       = bpf_iter_fini_seq_net,
-               .seq_priv_size          = sizeof(struct nl_seq_iter),
-       };
-
-       return bpf_iter_reg_target(&reg_info);
+       return bpf_iter_reg_target(&netlink_reg_info);
 }
 #endif