bpf: Add syscall lookup support for fd array and htab
authorMartin KaFai Lau <kafai@fb.com>
Wed, 28 Jun 2017 06:08:34 +0000 (23:08 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 29 Jun 2017 17:13:25 +0000 (13:13 -0400)
This patch allows userspace to do BPF_MAP_LOOKUP_ELEM on
BPF_MAP_TYPE_PROG_ARRAY,
BPF_MAP_TYPE_ARRAY_OF_MAPS and
BPF_MAP_TYPE_HASH_OF_MAPS.

The lookup returns a prog-id or map-id to the userspace.
The userspace can then use the BPF_PROG_GET_FD_BY_ID
or BPF_MAP_GET_FD_BY_ID to get a fd.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/bpf.h
kernel/bpf/arraymap.c
kernel/bpf/hashtab.c
kernel/bpf/map_in_map.c
kernel/bpf/map_in_map.h
kernel/bpf/syscall.c

index deca4e7f28451f95bf566a201271fd94633f42e3..5175729270d717fbcffc9f1b5b58081e0a941d57 100644 (file)
@@ -36,6 +36,7 @@ struct bpf_map_ops {
                                int fd);
        void (*map_fd_put_ptr)(void *ptr);
        u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
+       u32 (*map_fd_sys_lookup_elem)(void *ptr);
 };
 
 struct bpf_map {
@@ -288,9 +289,11 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
 
 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
                                 void *key, void *value, u64 map_flags);
+int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
 void bpf_fd_array_map_clear(struct bpf_map *map);
 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
                                void *key, void *value, u64 map_flags);
+int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
 
 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
  * forced to use 'long' read/writes to try to atomically copy long counters.
index ecb43542246e4b89d0e10ab77de4da168789040e..d771a38725006379315cc22782efedb3775b4569 100644 (file)
@@ -334,6 +334,26 @@ static void *fd_array_map_lookup_elem(struct bpf_map *map, void *key)
        return NULL;
 }
 
+/* only called from syscall */
+int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
+{
+       void **elem, *ptr;
+       int ret =  0;
+
+       if (!map->ops->map_fd_sys_lookup_elem)
+               return -ENOTSUPP;
+
+       rcu_read_lock();
+       elem = array_map_lookup_elem(map, key);
+       if (elem && (ptr = READ_ONCE(*elem)))
+               *value = map->ops->map_fd_sys_lookup_elem(ptr);
+       else
+               ret = -ENOENT;
+       rcu_read_unlock();
+
+       return ret;
+}
+
 /* only called from syscall */
 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
                                 void *key, void *value, u64 map_flags)
@@ -400,6 +420,11 @@ static void prog_fd_array_put_ptr(void *ptr)
        bpf_prog_put(ptr);
 }
 
+static u32 prog_fd_array_sys_lookup_elem(void *ptr)
+{
+       return ((struct bpf_prog *)ptr)->aux->id;
+}
+
 /* decrement refcnt of all bpf_progs that are stored in this map */
 void bpf_fd_array_map_clear(struct bpf_map *map)
 {
@@ -418,6 +443,7 @@ const struct bpf_map_ops prog_array_map_ops = {
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = prog_fd_array_get_ptr,
        .map_fd_put_ptr = prog_fd_array_put_ptr,
+       .map_fd_sys_lookup_elem = prog_fd_array_sys_lookup_elem,
 };
 
 static struct bpf_event_entry *bpf_event_entry_gen(struct file *perf_file,
@@ -585,4 +611,5 @@ const struct bpf_map_ops array_of_maps_map_ops = {
        .map_delete_elem = fd_array_map_delete_elem,
        .map_fd_get_ptr = bpf_map_fd_get_ptr,
        .map_fd_put_ptr = bpf_map_fd_put_ptr,
+       .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
 };
index 004334ea13ba3f56f10b33e9a1a50a97906000e4..4fb463172aa88e81d9caaaee271d8d8c8a9db978 100644 (file)
@@ -1243,6 +1243,26 @@ static void fd_htab_map_free(struct bpf_map *map)
        htab_map_free(map);
 }
 
+/* only called from syscall */
+int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value)
+{
+       void **ptr;
+       int ret = 0;
+
+       if (!map->ops->map_fd_sys_lookup_elem)
+               return -ENOTSUPP;
+
+       rcu_read_lock();
+       ptr = htab_map_lookup_elem(map, key);
+       if (ptr)
+               *value = map->ops->map_fd_sys_lookup_elem(READ_ONCE(*ptr));
+       else
+               ret = -ENOENT;
+       rcu_read_unlock();
+
+       return ret;
+}
+
 /* only called from syscall */
 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
                                void *key, void *value, u64 map_flags)
@@ -1305,4 +1325,5 @@ const struct bpf_map_ops htab_of_maps_map_ops = {
        .map_delete_elem = htab_map_delete_elem,
        .map_fd_get_ptr = bpf_map_fd_get_ptr,
        .map_fd_put_ptr = bpf_map_fd_put_ptr,
+       .map_fd_sys_lookup_elem = bpf_map_fd_sys_lookup_elem,
 };
index 59bcdf821ae47477f2a058af3e6918f0ce222325..1da574612bea75dabfe60c6967dce9c55a5e662b 100644 (file)
@@ -95,3 +95,8 @@ void bpf_map_fd_put_ptr(void *ptr)
         */
        bpf_map_put(ptr);
 }
+
+u32 bpf_map_fd_sys_lookup_elem(void *ptr)
+{
+       return ((struct bpf_map *)ptr)->id;
+}
index 177fadb689dca9075ff9c43055d392821908ba6d..6183db9ec08cce0ba6aaa656cffdcb710bb8c797 100644 (file)
@@ -19,5 +19,6 @@ bool bpf_map_meta_equal(const struct bpf_map *meta0,
 void *bpf_map_fd_get_ptr(struct bpf_map *map, struct file *map_file,
                         int ufd);
 void bpf_map_fd_put_ptr(void *ptr);
+u32 bpf_map_fd_sys_lookup_elem(void *ptr);
 
 #endif
index 8942c820d620a15ef38ff6904c0a2f647cdab7be..4409ccca88310622d74ab57b21b63ae3469aaebe 100644 (file)
 #include <linux/kernel.h>
 #include <linux/idr.h>
 
+#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
+                          (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
+                          (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
+                          (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
+#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
+#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
+
 DEFINE_PER_CPU(int, bpf_prog_active);
 static DEFINE_IDR(prog_idr);
 static DEFINE_SPINLOCK(prog_idr_lock);
@@ -411,6 +418,8 @@ static int map_lookup_elem(union bpf_attr *attr)
            map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
            map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
                value_size = round_up(map->value_size, 8) * num_possible_cpus();
+       else if (IS_FD_MAP(map))
+               value_size = sizeof(u32);
        else
                value_size = map->value_size;
 
@@ -426,9 +435,10 @@ static int map_lookup_elem(union bpf_attr *attr)
                err = bpf_percpu_array_copy(map, key, value);
        } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
                err = bpf_stackmap_copy(map, key, value);
-       } else if (map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
-                  map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
-               err = -ENOTSUPP;
+       } else if (IS_FD_ARRAY(map)) {
+               err = bpf_fd_array_map_lookup_elem(map, key, value);
+       } else if (IS_FD_HASH(map)) {
+               err = bpf_fd_htab_map_lookup_elem(map, key, value);
        } else {
                rcu_read_lock();
                ptr = map->ops->map_lookup_elem(map, key);