bpf: Remove bpf_image tree
[linux-2.6-microblaze.git] / kernel / bpf / trampoline.c
index 221a17a..f42f700 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/filter.h>
 #include <linux/ftrace.h>
 #include <linux/rbtree_latch.h>
+#include <linux/perf_event.h>
 
 /* dummy _ops. The verifier will operate on target program's ops. */
 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
@@ -17,12 +18,11 @@ const struct bpf_prog_ops bpf_extension_prog_ops = {
 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
 
 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
-static struct latch_tree_root image_tree __cacheline_aligned;
 
-/* serializes access to trampoline_table and image_tree */
+/* serializes access to trampoline_table */
 static DEFINE_MUTEX(trampoline_mutex);
 
-static void *bpf_jit_alloc_exec_page(void)
+void *bpf_jit_alloc_exec_page(void)
 {
        void *image;
 
@@ -38,62 +38,28 @@ static void *bpf_jit_alloc_exec_page(void)
        return image;
 }
 
-static __always_inline bool image_tree_less(struct latch_tree_node *a,
-                                     struct latch_tree_node *b)
+void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
 {
-       struct bpf_image *ia = container_of(a, struct bpf_image, tnode);
-       struct bpf_image *ib = container_of(b, struct bpf_image, tnode);
-
-       return ia < ib;
+       ksym->start = (unsigned long) data;
+       ksym->end = ksym->start + PAGE_SIZE;
+       bpf_ksym_add(ksym);
+       perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
+                          PAGE_SIZE, false, ksym->name);
 }
 
-static __always_inline int image_tree_comp(void *addr, struct latch_tree_node *n)
+void bpf_image_ksym_del(struct bpf_ksym *ksym)
 {
-       void *image = container_of(n, struct bpf_image, tnode);
-
-       if (addr < image)
-               return -1;
-       if (addr >= image + PAGE_SIZE)
-               return 1;
-
-       return 0;
+       bpf_ksym_del(ksym);
+       perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
+                          PAGE_SIZE, true, ksym->name);
 }
 
-static const struct latch_tree_ops image_tree_ops = {
-       .less   = image_tree_less,
-       .comp   = image_tree_comp,
-};
-
-static void *__bpf_image_alloc(bool lock)
+static void bpf_trampoline_ksym_add(struct bpf_trampoline *tr)
 {
-       struct bpf_image *image;
+       struct bpf_ksym *ksym = &tr->ksym;
 
-       image = bpf_jit_alloc_exec_page();
-       if (!image)
-               return NULL;
-
-       if (lock)
-               mutex_lock(&trampoline_mutex);
-       latch_tree_insert(&image->tnode, &image_tree, &image_tree_ops);
-       if (lock)
-               mutex_unlock(&trampoline_mutex);
-       return image->data;
-}
-
-void *bpf_image_alloc(void)
-{
-       return __bpf_image_alloc(true);
-}
-
-bool is_bpf_image_address(unsigned long addr)
-{
-       bool ret;
-
-       rcu_read_lock();
-       ret = latch_tree_find((void *) addr, &image_tree, &image_tree_ops) != NULL;
-       rcu_read_unlock();
-
-       return ret;
+       snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", tr->key);
+       bpf_image_ksym_add(tr->image, ksym);
 }
 
 struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
@@ -116,7 +82,7 @@ struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
                goto out;
 
        /* is_root was checked earlier. No need for bpf_jit_charge_modmem() */
-       image = __bpf_image_alloc(false);
+       image = bpf_jit_alloc_exec_page();
        if (!image) {
                kfree(tr);
                tr = NULL;
@@ -131,6 +97,8 @@ struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
        for (i = 0; i < BPF_TRAMP_MAX; i++)
                INIT_HLIST_HEAD(&tr->progs_hlist[i]);
        tr->image = image;
+       INIT_LIST_HEAD_RCU(&tr->ksym.lnode);
+       bpf_trampoline_ksym_add(tr);
 out:
        mutex_unlock(&trampoline_mutex);
        return tr;
@@ -216,8 +184,8 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total)
 
 static int bpf_trampoline_update(struct bpf_trampoline *tr)
 {
-       void *old_image = tr->image + ((tr->selector + 1) & 1) * BPF_IMAGE_SIZE/2;
-       void *new_image = tr->image + (tr->selector & 1) * BPF_IMAGE_SIZE/2;
+       void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE/2;
+       void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE/2;
        struct bpf_tramp_progs *tprogs;
        u32 flags = BPF_TRAMP_F_RESTORE_REGS;
        int err, total;
@@ -245,7 +213,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)
 
        synchronize_rcu_tasks();
 
-       err = arch_prepare_bpf_trampoline(new_image, new_image + BPF_IMAGE_SIZE / 2,
+       err = arch_prepare_bpf_trampoline(new_image, new_image + PAGE_SIZE / 2,
                                          &tr->func.model, flags, tprogs,
                                          tr->func.addr);
        if (err < 0)
@@ -356,8 +324,6 @@ out:
 
 void bpf_trampoline_put(struct bpf_trampoline *tr)
 {
-       struct bpf_image *image;
-
        if (!tr)
                return;
        mutex_lock(&trampoline_mutex);
@@ -368,11 +334,10 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
                goto out;
        if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
                goto out;
-       image = container_of(tr->image, struct bpf_image, data);
-       latch_tree_erase(&image->tnode, &image_tree, &image_tree_ops);
+       bpf_image_ksym_del(&tr->ksym);
        /* wait for tasks to get out of trampoline before freeing it */
        synchronize_rcu_tasks();
-       bpf_jit_free_exec(image);
+       bpf_jit_free_exec(tr->image);
        hlist_del(&tr->hlist);
        kfree(tr);
 out: