bpf: add map_lookup_elem_sys_only for lookups from syscall side
authorDaniel Borkmann <daniel@iogearbox.net>
Mon, 13 May 2019 23:18:55 +0000 (01:18 +0200)
committerCosmin Tanislav <demonsingur@gmail.com>
Thu, 16 May 2024 07:58:22 +0000 (10:58 +0300)
commit c6110222c6f49ea68169f353565eb865488a8619 upstream.

Add a callback map_lookup_elem_sys_only() that map implementations
could use over map_lookup_elem() from system call side in case the
map implementation needs to handle the latter differently than from
the BPF data path. If map_lookup_elem_sys_only() is set, this will
be preferred pick for map lookups out of user space. This hook is
used in a follow-up fix for LRU map, but once development window
opens, we can convert other map types from map_lookup_elem() (here,
the one called upon BPF_MAP_LOOKUP_ELEM cmd is meant) over to use
the callback to simplify and clean up the latter.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
include/linux/bpf.h
kernel/bpf/syscall.c

index f22171d4ca85c6280e782359b6cf311bc6bef5ac..2f6ff6e53290d851881d8fc21f000b01504c9064 100644 (file)
@@ -28,6 +28,7 @@ struct bpf_map_ops {
        void (*map_free)(struct bpf_map *map);
        int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
        void (*map_release_uref)(struct bpf_map *map);
+       void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
 
        /* funcs callable from userspace and from eBPF programs */
        void *(*map_lookup_elem)(struct bpf_map *map, void *key);
index 176170b002a35d03244d69b767aabcbfb59f65f8..e791adc6cdb7201e2976359e6ea38fe429763922 100644 (file)
@@ -579,7 +579,10 @@ static int map_lookup_elem(union bpf_attr *attr)
                err = bpf_fd_htab_map_lookup_elem(map, key, value);
        } else {
                rcu_read_lock();
-               ptr = map->ops->map_lookup_elem(map, key);
+               if (map->ops->map_lookup_elem_sys_only)
+                       ptr = map->ops->map_lookup_elem_sys_only(map, key);
+               else
+                       ptr = map->ops->map_lookup_elem(map, key);
                if (ptr)
                        memcpy(value, ptr, value_size);
                rcu_read_unlock();