Implement queue/stack maps
Subsystems (9)
v2 cover Implement queue/stack maps
In some applications this is needed have a pool of free elements, for example the list of free L4 ports in a SNAT. None of the current maps allow to do it as it is not possible to get any element without having they key it is associated to, even if it were possible, the lack of locking mecanishms in eBPF would do it almost impossible to be implemented without data races.
This patchset implements two new kind of eBPF maps: queue and stack. Those maps provide to eBPF programs the peek, push and pop operations, and for userspace applications a new bpf_map_lookup_and_delete_elem() is added.
v1 -> v2:
- Put ARG_PTR_TO_UNINIT_MAP_VALUE logic into a separated patch
- Fix missing __this_cpu_dec & preempt_enable calls in kernel/bpf/syscall.c
RFC v4 -> v1:
- Remove roundup to power of 2 in memory allocation
- Remove count and use a free slot to check if queue/stack is empty
- Use if + assigment for wrapping indexes
- Fix some minor style issues
- Squash two patches together
RFC v3 -> RFC v4:
- Revert renaming of kernel/bpf/stackmap.c
- Remove restriction on value size
- Remove len arguments from peek/pop helpers
- Add new ARG_PTR_TO_UNINIT_MAP_VALUE
RFC v2 -> RFC v3:
- Return elements by value instead that by reference
- Implement queue/stack base on array and head + tail indexes
- Rename stack trace related files to avoid confusion and conflicts
RFC v1 -> RFC v2:
- Create two separate maps instead of single one + flags
- Implement bpf_map_lookup_and_delete syscall
- Support peek operation
- Define replacement policy through flags in the update() method
- Add eBPF side tests
---
Mauricio Vasquez B (7):
bpf: rename stack trace map operations bpf/syscall: allow key to be null in map functions
bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall bpf/verifier: add ARG_PTR_TO_UNINIT_MAP_VALUE
bpf: add queue and stack maps
Sync uapi/bpf.h to tools/include selftests/bpf: add test cases for queue and stack maps
include/linux/bpf.h | 8 + include/linux/bpf_types.h | 4 include/uapi/linux/bpf.h | 30 ++ kernel/bpf/Makefile | 2 kernel/bpf/core.c | 3 kernel/bpf/helpers.c | 43 +++ kernel/bpf/queue_stack_maps.c | 288 ++++++++++++++++++++ kernel/bpf/stackmap.c | 2 kernel/bpf/syscall.c | 110 +++++++- kernel/bpf/verifier.c | 28 ++ net/core/filter.c | 6 tools/include/uapi/linux/bpf.h | 30 ++ tools/lib/bpf/bpf.c | 12 + tools/lib/bpf/bpf.h | 1 tools/testing/selftests/bpf/Makefile | 5 tools/testing/selftests/bpf/bpf_helpers.h | 7 tools/testing/selftests/bpf/test_maps.c | 122 ++++++++ tools/testing/selftests/bpf/test_progs.c | 99 +++++++ tools/testing/selftests/bpf/test_queue_map.c | 4 tools/testing/selftests/bpf/test_queue_stack_map.h | 59 ++++ tools/testing/selftests/bpf/test_stack_map.c | 4 21 files changed, 853 insertions(+), 14 deletions(-) create mode 100644 kernel/bpf/queue_stack_maps.c create mode 100644 tools/testing/selftests/bpf/test_queue_map.c create mode 100644 tools/testing/selftests/bpf/test_queue_stack_map.h create mode 100644 tools/testing/selftests/bpf/test_stack_map.c
PATCH v2 1/7 bpf: rename stack trace map operations
In the following patches queue and stack maps (FIFO and LIFO datastructures) will be implemented. In order to avoid confusion and a possible name clash rename stack_map_ops to stack_trace_map_ops
--- include/linux/bpf_types.h | 2 +- kernel/bpf/stackmap.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/bpf_types.h +1 -1
@@ -51,7 +51,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_HASH, htab_lru_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_PERCPU_HASH, htab_lru_percpu_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_LPM_TRIE, trie_map_ops)#ifdef CONFIG_PERF_EVENTS -BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_trace_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_HASH_OF_MAPS , htab_of_maps_map_ops)
kernel/bpf/stackmap.c +1 -1
@@ -600,7 +600,7 @@ static void stack_map_free(struct bpf_map *map)put_callchain_buffers (); } -const struct bpf_map_ops stack_map_ops = { +const struct bpf_map_ops stack_trace_map_ops = { .map_alloc = stack_map_alloc, .map_free = stack_map_free, .map_get_next_key = stack_map_get_next_key,
PATCH v2 2/7 bpf/syscall: allow key to be null in map functions
In reply to Mauricio Vasquez BThis commit adds the required logic to allow key being NULL in case the key_size of the map is 0.
A new __bpf_copy_key function helper only copies the key from userpsace when key_size != 0, otherwise it enforces that key must be null.
--- kernel/bpf/syscall.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
kernel/bpf/syscall.c +15 -4
@@ -651,6 +651,17 @@ int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)return -ENOTSUPP ; } +static void *__bpf_copy_key (void __user *ukey,u64 key_size) +{ +if (key_size) +return memdup_user (ukey, key_size); + +if (ukey) +return ERR_PTR (-EINVAL ); + +return NULL ; +} +/* last field in 'union bpf_attr' used by this command */ #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
@@ -678,7 +689,7 @@ static int map_lookup_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;
@@ -774,7 +785,7 @@ static int map_update_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;
@@ -876,7 +887,7 @@ static int map_delete_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;
@@ -928,7 +939,7 @@ static int map_get_next_key(union bpf_attr *attr) }if (ukey) { - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;
PATCH v2 3/7 bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
In reply to Mauricio Vasquez BThe following patch implements a bpf queue/stack maps that provides the peek/pop/push functions. There is not a direct relationship between those functions and the current maps syscalls, hence a new MAP_LOOKUP_AND_DELETE_ELEM syscall is added, this is mapped to the pop operation in the queue/stack maps and it is still to implement in other kind of maps.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+)
include/linux/bpf.h +1 -0
@@ -39,6 +39,7 @@ struct bpf_map_ops {void *(*map_lookup_elem)(struct bpf_map *map,void *key);int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); +void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
include/uapi/linux/bpf.h +1 -0
@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {
kernel/bpf/syscall.c +82 -0
94 lines not on this page · raw message
PATCH v2 4/7 bpf/verifier: add ARG_PTR_TO_UNINIT_MAP_VALUE
In reply to Mauricio Vasquez BARG_PTR_TO_UNINIT_MAP_VALUE argument is a pointer to a memory zone used to save the value of a map. Basically the same as ARG_PTR_TO_UNINIT_MEM, but the size has not be passed as an extra argument.
This will be used in the following patch that implements some new helpers that receive a pointer to be filled with a map value.
--- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
@@ -139,6 +139,7 @@ enum bpf_arg_type {ARG_CONST_MAP_PTR ,/* const argument used as pointer to bpf_map */ ARG_PTR_TO_MAP_KEY ,/* pointer to stack used as map key */ ARG_PTR_TO_MAP_VALUE ,/* pointer to stack used as map value */ +ARG_PTR_TO_UNINIT_MAP_VALUE ,/* pointer to valid memory used to store a map value */ /* the following constraints used to prototype bpf_memcmp() and other * functions that access data on eBPF program stack
kernel/bpf/verifier.c +6 -3
@@ -2117,7 +2117,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, }if (arg_type ==ARG_PTR_TO_MAP_KEY || - arg_type ==ARG_PTR_TO_MAP_VALUE ) { + arg_type ==ARG_PTR_TO_MAP_VALUE || + arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { expected_type =PTR_TO_STACK ;if (!type_is_pkt_pointer (type) && type !=PTR_TO_MAP_VALUE && type != expected_type)
@@ -2187,7 +2188,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, err =check_helper_mem_access (env, regno, meta->map_ptr->key_size, false,NULL ); - }else if (arg_type ==ARG_PTR_TO_MAP_VALUE ) { + }else if (arg_type ==ARG_PTR_TO_MAP_VALUE || + arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ) {/* bpf_map_xxx(..., map_ptr, ..., value) call: * check [value, value + map->value_size) validity */
@@ -2196,9 +2198,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,verbose (env,"invalid map_ptr to access map->value\n" );return -EACCES ; } + meta->raw_mode = (arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ); err =check_helper_mem_access (env, regno, meta->map_ptr->value_size, false, -NULL ); + meta); }else if (arg_type_is_mem_size (arg_type)) {bool zero_size_allowed = (arg_type ==ARG_CONST_SIZE_OR_ZERO );
PATCH v2 5/7 bpf: add queue and stack maps
In reply to Mauricio Vasquez BQueue/stack maps implement a FIFO/LIFO data storage for ebpf programs. These maps support peek, pop and push operations that are exposed to eBPF programs through the new bpf_map[peek/pop/push] helpers. Those operations are exposed to userspace applications through the already existing syscalls in the following way:
BPF_MAP_LOOKUP_ELEM -> peekBPF_MAP_LOOKUP_AND_DELETE_ELEM -> popBPF_MAP_UPDATE_ELEM -> push
Queue/stack maps are implemented using a buffer, tail and head indexes, hence BPF_F_NO_PREALLOC is not supported.
As opposite to other maps, queue and stack do not use RCU for protecting maps values, the bpf_map[peek/pop] have a ARG_PTR_TO_UNINIT_MAP_VALUE argument that is a pointer to a memory zone where to save the value of a map. Basically the same as ARG_PTR_TO_UNINIT_MEM, but the size has not be passed as an extra argument.
Our main motivation for implementing queue/stack maps was to keep track of a pool of elements, like network ports in a SNAT, however we forsee other use cases, like for exampling saving last N kernel events in a map and then analysing from userspace.
--- include/linux/bpf.h | 6 + include/linux/bpf_types.h | 2 include/uapi/linux/bpf.h | 29 ++++ kernel/bpf/Makefile | 2 kernel/bpf/core.c | 3 kernel/bpf/helpers.c | 43 ++++++ kernel/bpf/queue_stack_maps.c | 288 +++++++++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 11 +- kernel/bpf/verifier.c | 19 +++ net/core/filter.c | 6 + 10 files changed, 405 insertions(+), 4 deletions(-) create mode 100644 kernel/bpf/queue_stack_maps.c
include/linux/bpf.h +6 -0
@@ -40,6 +40,9 @@ struct bpf_map_ops {int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key);void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key); +int (*map_push_elem)(struct bpf_map *map,void *value,u64 flags); +int (*map_pop_elem)(struct bpf_map *map,void *value); +int (*map_peek_elem)(struct bpf_map *map,void *value);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
@@ -827,6 +830,9 @@ static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,extern const struct bpf_func_proto bpf_map_lookup_elem_proto;extern const struct bpf_func_proto bpf_map_update_elem_proto;extern const struct bpf_func_proto bpf_map_delete_elem_proto; +extern const struct bpf_func_proto bpf_map_push_elem_proto; +extern const struct bpf_func_proto bpf_map_pop_elem_proto; +extern const struct bpf_func_proto bpf_map_peek_elem_proto;extern const struct bpf_func_proto bpf_get_prandom_u32_proto;extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
include/linux/bpf_types.h +2 -0
@@ -69,3 +69,5 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, reuseport_array_ops)#endif #endif +BPF_MAP_TYPE(BPF_MAP_TYPE_QUEUE, queue_map_ops) +BPF_MAP_TYPE (BPF_MAP_TYPE_STACK , stack_map_ops)
include/uapi/linux/bpf.h +28 -1
@@ -129,6 +129,8 @@ enum bpf_map_type {BPF_MAP_TYPE_CGROUP_STORAGE ,BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ,BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , +BPF_MAP_TYPE_QUEUE , + BPF_MAP_TYPE_STACK, }; enum bpf_prog_type {
@@ -463,6 +465,28 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * +* int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) +* Description +* Push an element *value* in *map*. *flags* is one of: +* +* **BPF_EXIST** +* If the queue/stack is full, the oldest element is removed to +* make room for this. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_pop_elem(struct bpf_map *map, void *value) +* Description +* Pop an element from *map*. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_peek_elem(struct bpf_map *map, void *value) +* Description +* Get an element from *map* without removing it. +* Return +* 0 on success, or a negative error in case of failure. +* * int bpf_probe_read(void *dst, u32 size, const void *src) * Description * For tracing programs, safely attempt to read *size* bytes from
@@ -2304,7 +2328,10 @@ union bpf_attr {FN (skb_ancestor_cgroup_id), \FN (sk_lookup_tcp), \FN (sk_lookup_udp), \ -FN (sk_release), +FN (sk_release), \ +FN (map_push_elem), \ +FN (map_pop_elem), \ +FN (map_peek_elem),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/Makefile +1 -1
@@ -3,7 +3,7 @@ obj-y := core.o obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o -obj-$(CONFIG_BPF_SYSCALL) += local_storage.o +obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_SYSCALL) += btf.o ifeq ($(CONFIG_NET),y)
kernel/bpf/core.c +3 -0
@@ -1783,6 +1783,9 @@ BPF_CALL_0(bpf_user_rnd_u32)const struct bpf_func_proto bpf_map_lookup_elem_proto__weak ;const struct bpf_func_proto bpf_map_update_elem_proto__weak ;const struct bpf_func_proto bpf_map_delete_elem_proto__weak ; +const struct bpf_func_proto bpf_map_push_elem_proto__weak ; +const struct bpf_func_proto bpf_map_pop_elem_proto__weak ; +const struct bpf_func_proto bpf_map_peek_elem_proto__weak ;const struct bpf_func_proto bpf_get_prandom_u32_proto__weak ;const struct bpf_func_proto bpf_get_smp_processor_id_proto__weak ;
kernel/bpf/helpers.c +43 -0
49 lines not on this page · raw message
kernel/bpf/queue_stack_maps.c new file +288 -0
288 lines not on this page · raw message
kernel/bpf/syscall.c +10 -1
@@ -727,6 +727,9 @@ static int map_lookup_elem(union bpf_attr *attr) err =bpf_fd_htab_map_lookup_elem (map, key, value); }else if (map->map_type ==BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) { err =bpf_fd_reuseport_array_lookup_elem (map, key, value); + }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || + map->map_type ==BPF_MAP_TYPE_STACK ) { + err = map->ops->map_peek_elem (map, value); }else {rcu_read_lock (); ptr = map->ops->map_lookup_elem (map, key);
@@ -846,6 +849,9 @@ static int map_update_elem(union bpf_attr *attr)/* rcu_read_lock() is not needed */ err =bpf_fd_reuseport_array_update_elem (map, key, value, attr->flags); + }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || + map->map_type ==BPF_MAP_TYPE_STACK ) { + err = map->ops->map_push_elem (map, value, attr->flags); }else {rcu_read_lock (); err = map->ops->map_update_elem (map, key, value, attr->flags);
@@ -1028,7 +1034,10 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)*/ preempt_disable ();__this_cpu_inc (bpf_prog_active); -if (map->ops->map_lookup_and_delete_elem) { +if (map->map_type ==BPF_MAP_TYPE_QUEUE || + map->map_type ==BPF_MAP_TYPE_STACK ) { + err = map->ops->map_pop_elem (map, value); + }else if (map->ops->map_lookup_and_delete_elem) {rcu_read_lock (); ptr = map->ops->map_lookup_and_delete_elem (map, key);if (ptr)
kernel/bpf/verifier.c +18 -1
@@ -2324,6 +2324,13 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (func_id != BPF_FUNC_sk_select_reuseport) goto error;break ; +case BPF_MAP_TYPE_QUEUE : +case BPF_MAP_TYPE_STACK : +if (func_id != BPF_FUNC_map_peek_elem && + func_id != BPF_FUNC_map_pop_elem && + func_id != BPF_FUNC_map_push_elem) + goto error; +break ;default :break ; }
@@ -2380,6 +2387,13 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) goto error;break ; +case BPF_FUNC_map_peek_elem: +case BPF_FUNC_map_pop_elem: +case BPF_FUNC_map_push_elem: +if (map->map_type !=BPF_MAP_TYPE_QUEUE && + map->map_type !=BPF_MAP_TYPE_STACK ) + goto error; +break ;default :break ; }
@@ -2675,7 +2689,10 @@ record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,if (func_id != BPF_FUNC_tail_call && func_id != BPF_FUNC_map_lookup_elem && func_id != BPF_FUNC_map_update_elem && - func_id != BPF_FUNC_map_delete_elem) + func_id != BPF_FUNC_map_delete_elem && + func_id != BPF_FUNC_map_push_elem && + func_id != BPF_FUNC_map_pop_elem && + func_id != BPF_FUNC_map_peek_elem)return 0 ;if (meta->map_ptr ==NULL ) {
net/core/filter.c +6 -0
@@ -4995,6 +4995,12 @@ bpf_base_func_proto(enum bpf_func_id func_id)return &bpf_map_update_elem_proto;case BPF_FUNC_map_delete_elem:return &bpf_map_delete_elem_proto; +case BPF_FUNC_map_push_elem: +return &bpf_map_push_elem_proto; +case BPF_FUNC_map_pop_elem: +return &bpf_map_pop_elem_proto; +case BPF_FUNC_map_peek_elem: +return &bpf_map_peek_elem_proto;case BPF_FUNC_get_prandom_u32:return &bpf_get_prandom_u32_proto;case BPF_FUNC_get_smp_processor_id:
PATCH v2 6/7 Sync uapi/bpf.h to tools/include
In reply to Mauricio Vasquez BSync both files.
--- tools/include/uapi/linux/bpf.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +29 -1
@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {
@@ -128,6 +129,8 @@ enum bpf_map_type {BPF_MAP_TYPE_CGROUP_STORAGE ,BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ,BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , +BPF_MAP_TYPE_QUEUE , + BPF_MAP_TYPE_STACK, }; enum bpf_prog_type {
@@ -462,6 +465,28 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * +* int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) +* Description +* Push an element *value* in *map*. *flags* is one of: +* +* **BPF_EXIST** +* If the queue/stack is full, the oldest element is removed to +* make room for this. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_pop_elem(struct bpf_map *map, void *value) +* Description +* Pop an element from *map*. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_peek_elem(struct bpf_map *map, void *value) +* Description +* Get an element from *map* without removing it. +* Return +* 0 on success, or a negative error in case of failure. +* * int bpf_probe_read(void *dst, u32 size, const void *src) * Description * For tracing programs, safely attempt to read *size* bytes from
@@ -2303,7 +2328,10 @@ union bpf_attr {FN (skb_ancestor_cgroup_id), \FN (sk_lookup_tcp), \FN (sk_lookup_udp), \ -FN (sk_release), +FN (sk_release), \ +FN (map_push_elem), \ +FN (map_pop_elem), \ +FN (map_peek_elem),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
PATCH v2 7/7 selftests/bpf: add test cases for queue and stack maps
In reply to Mauricio Vasquez Btest_maps:
Tests that queue/stack maps are behaving correctly even in corner cases
test_progs:
Tests new ebpf helpers
--- tools/lib/bpf/bpf.c | 12 ++ tools/lib/bpf/bpf.h | 1 tools/testing/selftests/bpf/Makefile | 5 + tools/testing/selftests/bpf/bpf_helpers.h | 7 + tools/testing/selftests/bpf/test_maps.c | 122 ++++++++++++++++++++ tools/testing/selftests/bpf/test_progs.c | 99 ++++++++++++++++ tools/testing/selftests/bpf/test_queue_map.c | 4 + tools/testing/selftests/bpf/test_queue_stack_map.h | 59 ++++++++++ tools/testing/selftests/bpf/test_stack_map.c | 4 + 9 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_queue_map.c create mode 100644 tools/testing/selftests/bpf/test_queue_stack_map.h create mode 100644 tools/testing/selftests/bpf/test_stack_map.c
tools/lib/bpf/bpf.c +12 -0
@@ -278,6 +278,18 @@ int bpf_map_lookup_elem(int fd, const void *key, void *value)return sys_bpf (BPF_MAP_LOOKUP_ELEM , &attr,sizeof (attr)); } +int bpf_map_lookup_and_delete_elem (int fd,const void *key,const void *value) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.value =ptr_to_u64 (value); + +return sys_bpf (BPF_MAP_LOOKUP_AND_DELETE_ELEM , &attr,sizeof (attr)); +} +int bpf_map_delete_elem (int fd,const void *key) {union bpf_attr attr;
tools/lib/bpf/bpf.h +1 -0
@@ -86,6 +86,7 @@ int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags);int bpf_map_lookup_elem (int fd,const void *key,void *value); +int bpf_map_lookup_and_delete_elem (int fd,const void *key,const void *value);int bpf_map_delete_elem (int fd,const void *key);int bpf_map_get_next_key (int fd,const void *key,void *next_key);int bpf_obj_pin (int fd,const char *pathname);
tools/testing/selftests/bpf/Makefile +4 -1
@@ -37,7 +37,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \ - test_sk_lookup_kern.o test_xdp_vlan.o + test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
@@ -116,6 +116,9 @@ CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \ $(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline $(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline +$(OUTPUT)/test_queue_map.o: test_queue_stack_map.h +$(OUTPUT)/test_stack_map.o: test_queue_stack_map.h + BTF_LLC_PROBE := $(shell $(LLC) -march=bpf -mattr=help 2>&1 | grep dwarfris) BTF_PAHOLE_PROBE := $(shell $(BTF_PAHOLE) --help 2>&1 | grep BTF) BTF_OBJCOPY_PROBE := $(shell $(LLVM_OBJCOPY) --help 2>&1 | grep -i 'usage.*llvm')
tools/testing/selftests/bpf/bpf_helpers.h +7 -0
@@ -16,6 +16,13 @@ static int (*bpf_map_update_elem)(void *map, void *key, void *value, (void *) BPF_FUNC_map_update_elem;static int (*bpf_map_delete_elem)(void *map,void *key) = (void *) BPF_FUNC_map_delete_elem; +static int (*bpf_map_push_elem)(void *map,void *value, +unsigned long long flags) = + (void *) BPF_FUNC_map_push_elem; +static int (*bpf_map_pop_elem)(void *map,void *value) = + (void *) BPF_FUNC_map_pop_elem; +static int (*bpf_map_peek_elem)(void *map,void *value) = + (void *) BPF_FUNC_map_peek_elem;static int (*bpf_probe_read)(void *dst,int size,void *unsafe_ptr) = (void *) BPF_FUNC_probe_read; static unsigned long long (*bpf_ktime_get_ns)(void) =
tools/testing/selftests/bpf/test_maps.c +122 -0
144 lines not on this page · raw message
tools/testing/selftests/bpf/test_progs.c +99 -0
113 lines not on this page · raw message
tools/testing/selftests/bpf/test_queue_map.c new file +4 -0
@@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Politecnico di Torino +#define MAP_TYPE BPF_MAP_TYPE_QUEUE +#include "test_queue_stack_map.h"
tools/testing/selftests/bpf/test_queue_stack_map.h new file +59 -0
59 lines not on this page · raw message
tools/testing/selftests/bpf/test_stack_map.c new file +4 -0
@@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Politecnico di Torino +#define MAP_TYPE BPF_MAP_TYPE_STACK +#include "test_queue_stack_map.h"
Re: [PATCH bpf-next v2 2/7] bpf/syscall: allow key to be null in map functions
In reply to Mauricio Vasquez BQuoted 9 lines · Mauricio Vasquez B
This commit adds the required logic to allow key being NULL in case the
key_sizeof the map is 0.A new
__bpf_copy_keyfunction helper only copies the key from userpsace whenkey_size!= 0, otherwise it enforces that key must be null.Signed-off-by Mauricio Vasquez B <[email protected]>
Quoted 63 lines
--- kernel/bpf/syscall.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)kernel/bpf/syscall.c +15
-4@@ -651,6 +651,17 @@ int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)return -ENOTSUPP ; } +static void *__bpf_copy_key (void __user *ukey,u64 key_size) +{ +if (key_size) +return memdup_user (ukey, key_size); + +if (ukey) +return ERR_PTR (-EINVAL ); + +return NULL ; +} +/* last field in 'union bpf_attr' used by this command */ #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value@@ -678,7 +689,7 @@ static int map_lookup_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;@@ -774,7 +785,7 @@ static int map_update_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;@@ -876,7 +887,7 @@ static int map_delete_elem(union bpf_attr *attr) goto err_put; } - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;@@ -928,7 +939,7 @@ static int map_get_next_key(union bpf_attr *attr) }if (ukey) { - key =memdup_user (ukey, map->key_size); + key =__bpf_copy_key (ukey, map->key_size);if (IS_ERR (key)) { err =PTR_ERR (key); goto err_put;
Re: [PATCH bpf-next v2 4/7] bpf/verifier: add ARG_PTR_TO_UNINIT_MAP_VALUE
In reply to Mauricio Vasquez BQuoted 10 lines · Mauricio Vasquez B
ARG_PTR_TO_UNINIT_MAP_VALUEargument is a pointer to a memory zone used to save the value of a map. Basically the same asARG_PTR_TO_UNINIT_MEM, but the size has not be passed as an extra argument.This will be used in the following patch that implements some new helpers that receive a pointer to be filled with a map value.
Signed-off-by Mauricio Vasquez B <[email protected]>
Quoted 54 lines
--- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-)include/linux/bpf.h +1
-0@@ -139,6 +139,7 @@ enum bpf_arg_type {ARG_CONST_MAP_PTR ,/* const argument used as pointer to bpf_map */ ARG_PTR_TO_MAP_KEY ,/* pointer to stack used as map key */ ARG_PTR_TO_MAP_VALUE ,/* pointer to stack used as map value */ +ARG_PTR_TO_UNINIT_MAP_VALUE ,/* pointer to valid memory used to store a map value */ /* the following constraints used to prototype bpf_memcmp() and other * functions that access data on eBPF program stack kernel/bpf/verifier.c +6
-3@@ -2117,7 +2117,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, }if (arg_type ==ARG_PTR_TO_MAP_KEY || - arg_type ==ARG_PTR_TO_MAP_VALUE ) { + arg_type ==ARG_PTR_TO_MAP_VALUE || + arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ) { expected_type =PTR_TO_STACK ;if (!type_is_pkt_pointer (type) && type !=PTR_TO_MAP_VALUE && type != expected_type)@@ -2187,7 +2188,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, err =check_helper_mem_access (env, regno, meta->map_ptr->key_size, false,NULL ); - }else if (arg_type ==ARG_PTR_TO_MAP_VALUE ) { + }else if (arg_type ==ARG_PTR_TO_MAP_VALUE || + arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ) {/* bpf_map_xxx(..., map_ptr, ..., value) call: * check [value, value + map->value_size) validity */ @@ -2196,9 +2198,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,verbose (env,"invalid map_ptr to access map->value\n" );return -EACCES ; } + meta->raw_mode = (arg_type ==ARG_PTR_TO_UNINIT_MAP_VALUE ); err =check_helper_mem_access (env, regno, meta->map_ptr->value_size, false, -NULL ); + meta); }else if (arg_type_is_mem_size (arg_type)) {bool zero_size_allowed = (arg_type ==ARG_CONST_SIZE_OR_ZERO );
Re: [PATCH bpf-next v2 6/7] Sync uapi/bpf.h to tools/include
In reply to Mauricio Vasquez BQuoted 4 lines · Mauricio Vasquez B
Sync both files.
Signed-off-by Mauricio Vasquez B <[email protected]>
Quoted 67 lines
--- tools/include/uapi/linux/bpf.h | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-)tools/include/uapi/linux/bpf.h +29
-1@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {@@ -128,6 +129,8 @@ enum bpf_map_type {BPF_MAP_TYPE_CGROUP_STORAGE ,BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ,BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE , +BPF_MAP_TYPE_QUEUE , + BPF_MAP_TYPE_STACK, }; enum bpf_prog_type {@@ -462,6 +465,28 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * +* int bpf_map_push_elem(struct bpf_map *map, const void *value, u64 flags) +* Description +* Push an element *value* in *map*. *flags* is one of: +* +* **BPF_EXIST** +* If the queue/stack is full, the oldest element is removed to +* make room for this. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_pop_elem(struct bpf_map *map, void *value) +* Description +* Pop an element from *map*. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_map_peek_elem(struct bpf_map *map, void *value) +* Description +* Get an element from *map* without removing it. +* Return +* 0 on success, or a negative error in case of failure. +* * int bpf_probe_read(void *dst, u32 size, const void *src) * Description * For tracing programs, safely attempt to read *size* bytes from @@ -2303,7 +2328,10 @@ union bpf_attr {FN (skb_ancestor_cgroup_id), \FN (sk_lookup_tcp), \FN (sk_lookup_udp), \ -FN (sk_release), +FN (sk_release), \ +FN (map_push_elem), \ +FN (map_pop_elem), \ +FN (map_peek_elem),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Re: [PATCH bpf-next v2 1/7] bpf: rename stack trace map operations
In reply to Mauricio Vasquez BQuoted 6 lines · Mauricio Vasquez B
In the following patches queue and stack maps (FIFO and LIFO datastructures) will be implemented. In order to avoid confusion and a possible name clash rename
stack_map_opstostack_trace_map_opsSigned-off-by Mauricio Vasquez B <[email protected]>
Quoted 32 lines
--- include/linux/bpf_types.h | 2 +- kernel/bpf/stackmap.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)include/linux/bpf_types.h +1
-1@@ -51,7 +51,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_HASH, htab_lru_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_LRU_PERCPU_HASH, htab_lru_percpu_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_LPM_TRIE, trie_map_ops)#ifdef CONFIG_PERF_EVENTS -BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_STACK_TRACE, stack_trace_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_HASH_OF_MAPS , htab_of_maps_map_ops)kernel/bpf/stackmap.c +1
-1@@ -600,7 +600,7 @@ static void stack_map_free(struct bpf_map *map)put_callchain_buffers (); } -const struct bpf_map_ops stack_map_ops = { +const struct bpf_map_ops stack_trace_map_ops = { .map_alloc = stack_map_alloc, .map_free = stack_map_free, .map_get_next_key = stack_map_get_next_key,
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
In reply to Mauricio Vasquez BQuoted 90 lines · Mauricio Vasquez B
The following patch implements a bpf queue/stack maps that provides the peek/pop/push functions. There is not a direct relationship between those functions and the current maps syscalls, hence a new
MAP_LOOKUP_AND_DELETE_ELEMsyscall is added, this is mapped to the pop operation in the queue/stack maps and it is still to implement in other kind of maps.Signed-off-by Mauricio Vasquez B <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+)include/linux/bpf.h +1
-0@@ -39,6 +39,7 @@ struct bpf_map_ops {void *(*map_lookup_elem)(struct bpf_map *map,void *key);int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); +void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,include/uapi/linux/bpf.h +1
-0@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {kernel/bpf/syscall.c +43
-0@@ -980,6 +980,85 @@ static int map_get_next_key(union bpf_attr *attr)return err; } +#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value + +static int map_lookup_and_delete_elem (union bpf_attr *attr) +{ +void __user *ukey =u64_to_user_ptr (attr->key); +void __user *uvalue =u64_to_user_ptr (attr->value); +int ufd = attr->map_fd; +struct bpf_map *map; +void *key, *value, *ptr; +u32 value_size; +struct fd f; +int err; + +if (CHECK_ATTR (BPF_MAP_LOOKUP_AND_DELETE_ELEM )) +return -EINVAL ; + + f =fdget (ufd); + map =__bpf_map_get (f); +if (IS_ERR (map)) +return PTR_ERR (map); + +if (!(f.file->f_mode &FMODE_CAN_WRITE )) { + err = -EPERM ; + goto err_put; + } + + key =__bpf_copy_key (ukey, map->key_size); +if (IS_ERR (key)) { + err =PTR_ERR (key); + goto err_put; + } + + value_size = map->value_size; + + err = -ENOMEM ; + value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); +if (!value) + goto free_key; + + err = -EFAULT ; +if (copy_from_user (value, uvalue, value_size) !=0 ) + goto free_value; +
kernel/bpf/syscall.c static int map_get_next_key(union bpf_attr *attr) · line 1035
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+/* must increment bpf_prog_active to avoid kprobe+bpf triggering from +* inside bpf map update or delete otherwise deadlocks are possible +*/ +preempt_disable (); +__this_cpu_inc (bpf_prog_active); +if (map->ops->map_lookup_and_delete_elem) { +rcu_read_lock (); + ptr = map->ops->map_lookup_and_delete_elem (map, key); +if (ptr) +memcpy (value, ptr, value_size);
I think we are exposed to race condition with push and pop in parallel. map_lookup_and_delete_elem() only updates the head/tail, so it gives no protection for the buffer pointed by ptr.
Thanks,
Song
Quoted 40 lines
kernel/bpf/syscall.c +29
-0+rcu_read_unlock (); + err = ptr ?0 : -ENOENT ; + }else { + err = -ENOTSUPP ; + } + +__this_cpu_dec (bpf_prog_active); +preempt_enable (); + +if (err) + goto free_value; + +if (copy_to_user (uvalue, value, value_size) !=0 ) + goto free_value; + + err =0 ; + +free_value: +kfree (value); +free_key: +kfree (key); +err_put: +fdput (f); +return err; +} +static const struct bpf_prog_ops *const bpf_prog_types[] = { #define BPF_PROG_TYPE(_id, _name) \ [_id] = & _name ## _prog_ops,@@ -2453,6 +2532,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizcase BPF_TASK_FD_QUERY : err =bpf_task_fd_query (&attr, uattr);break ; +case BPF_MAP_LOOKUP_AND_DELETE_ELEM : + err =map_lookup_and_delete_elem (&attr); +break ;default : err = -EINVAL ;break ;
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 96 lines · Song Liu
Quoted 94 lines · Mauricio Vasquez B
The following patch implements a bpf queue/stack maps that provides the peek/pop/push functions. There is not a direct relationship between those functions and the current maps syscalls, hence a new
MAP_LOOKUP_AND_DELETE_ELEMsyscall is added, this is mapped to the pop operation in the queue/stack maps and it is still to implement in other kind of maps.Signed-off-by Mauricio Vasquez B <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+)include/linux/bpf.h +1
-0@@ -39,6 +39,7 @@ struct bpf_map_ops {void *(*map_lookup_elem)(struct bpf_map *map,void *key);int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); +void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,include/uapi/linux/bpf.h +1
-0@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {kernel/bpf/syscall.c +48
-0@@ -980,6 +980,85 @@ static int map_get_next_key(union bpf_attr *attr)return err; } +#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value + +static int map_lookup_and_delete_elem (union bpf_attr *attr) +{ +void __user *ukey =u64_to_user_ptr (attr->key); +void __user *uvalue =u64_to_user_ptr (attr->value); +int ufd = attr->map_fd; +struct bpf_map *map; +void *key, *value, *ptr; +u32 value_size; +struct fd f; +int err; + +if (CHECK_ATTR (BPF_MAP_LOOKUP_AND_DELETE_ELEM )) +return -EINVAL ; + + f =fdget (ufd); + map =__bpf_map_get (f); +if (IS_ERR (map)) +return PTR_ERR (map); + +if (!(f.file->f_mode &FMODE_CAN_WRITE )) { + err = -EPERM ; + goto err_put; + } + + key =__bpf_copy_key (ukey, map->key_size); +if (IS_ERR (key)) { + err =PTR_ERR (key); + goto err_put; + } + + value_size = map->value_size; + + err = -ENOMEM ; + value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); +if (!value) + goto free_key; + + err = -EFAULT ; +if (copy_from_user (value, uvalue, value_size) !=0 ) + goto free_value; + +/* must increment bpf_prog_active to avoid kprobe+bpf triggering from +* inside bpf map update or delete otherwise deadlocks are possible +*/ +preempt_disable (); +__this_cpu_inc (bpf_prog_active);
Quoted 8 lines
kernel/bpf/syscall.c static int map_get_next_key(union bpf_attr *attr) · line 1035
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+if (map->ops->map_lookup_and_delete_elem) { +rcu_read_lock (); + ptr = map->ops->map_lookup_and_delete_elem (map, key); +if (ptr) +memcpy (value, ptr, value_size);I think we are exposed to race condition with push and pop in parallel.
map_lookup_and_delete_elem()only updates the head/tail, so it gives no protection for the buffer pointed by ptr.
queue/stack maps does not use this 'ptr', the pop operation directly copies the value into the buffer allocated in map_lookup_and_delete_elem(). The copy from the queue/stack buffer into 'value' and the head/tail update are protected by a spinlock in the queue/stack maps implementation.
On the other hand, future implementation of map_lookup_and_delete operation in other kind of maps should guarantee that the return ptr is rcu protected.
Does it make sense to you?
Thanks,
Mauricio.
Quoted 43 lines
Thanks,
SongQuoted 40 lines
kernel/bpf/syscall.c +29
-0+rcu_read_unlock (); + err = ptr ?0 : -ENOENT ; + }else { + err = -ENOTSUPP ; + } + +__this_cpu_dec (bpf_prog_active); +preempt_enable (); + +if (err) + goto free_value; + +if (copy_to_user (uvalue, value, value_size) !=0 ) + goto free_value; + + err =0 ; + +free_value: +kfree (value); +free_key: +kfree (key); +err_put: +fdput (f); +return err; +} +static const struct bpf_prog_ops *const bpf_prog_types[] = { #define BPF_PROG_TYPE(_id, _name) \ [_id] = & _name ## _prog_ops,@@ -2453,6 +2532,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizcase BPF_TASK_FD_QUERY : err =bpf_task_fd_query (&attr, uattr);break ; +case BPF_MAP_LOOKUP_AND_DELETE_ELEM : + err =map_lookup_and_delete_elem (&attr); +break ;default : err = -EINVAL ;break ;
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 114 lines · Mauricio Vasquez
Quoted 96 lines · Song Liu
Quoted 94 lines · Mauricio Vasquez B
The following patch implements a bpf queue/stack maps that provides the peek/pop/push functions. There is not a direct relationship between those functions and the current maps syscalls, hence a new
MAP_LOOKUP_AND_DELETE_ELEMsyscall is added, this is mapped to the pop operation in the queue/stack maps and it is still to implement in other kind of maps.Signed-off-by Mauricio Vasquez B <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+)include/linux/bpf.h +1
-0@@ -39,6 +39,7 @@ struct bpf_map_ops {void *(*map_lookup_elem)(struct bpf_map *map,void *key);int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); +void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,include/uapi/linux/bpf.h +1
-0@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {kernel/bpf/syscall.c +48
-0@@ -980,6 +980,85 @@ static int map_get_next_key(union bpf_attr *attr)return err; } +#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value + +static int map_lookup_and_delete_elem (union bpf_attr *attr) +{ +void __user *ukey =u64_to_user_ptr (attr->key); +void __user *uvalue =u64_to_user_ptr (attr->value); +int ufd = attr->map_fd; +struct bpf_map *map; +void *key, *value, *ptr; +u32 value_size; +struct fd f; +int err; + +if (CHECK_ATTR (BPF_MAP_LOOKUP_AND_DELETE_ELEM )) +return -EINVAL ; + + f =fdget (ufd); + map =__bpf_map_get (f); +if (IS_ERR (map)) +return PTR_ERR (map); + +if (!(f.file->f_mode &FMODE_CAN_WRITE )) { + err = -EPERM ; + goto err_put; + } + + key =__bpf_copy_key (ukey, map->key_size); +if (IS_ERR (key)) { + err =PTR_ERR (key); + goto err_put; + } + + value_size = map->value_size; + + err = -ENOMEM ; + value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); +if (!value) + goto free_key; + + err = -EFAULT ; +if (copy_from_user (value, uvalue, value_size) !=0 ) + goto free_value; + +/* must increment bpf_prog_active to avoid kprobe+bpf triggering from +* inside bpf map update or delete otherwise deadlocks are possible +*/ +preempt_disable (); +__this_cpu_inc (bpf_prog_active);Quoted 8 lines
kernel/bpf/syscall.c static int map_get_next_key(union bpf_attr *attr) · line 1035
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+if (map->ops->map_lookup_and_delete_elem) { +rcu_read_lock (); + ptr = map->ops->map_lookup_and_delete_elem (map, key); +if (ptr) +memcpy (value, ptr, value_size);I think we are exposed to race condition with push and pop in parallel.
map_lookup_and_delete_elem()only updates the head/tail, so it gives no protection for the buffer pointed by ptr.queue/stack maps does not use this 'ptr', the pop operation directly copies the value into the buffer allocated in
map_lookup_and_delete_elem(). The copy from the queue/stack buffer into 'value' and the head/tail update are protected by a spinlock in the queue/stack maps implementation.
Quoted 5 lines
On the other hand, future implementation of
map_lookup_and_deleteoperation in other kind of maps should guarantee that the return ptr is rcu protected.Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock();
ptr = map->ops->map_lookup_and_delete_elem(map, key);
if (ptr)
memcpy(value, ptr, value_size);I guess this part is not used at all? Can we just remove it?
Thanks,
Song
Quoted 42 lines
Quoted 41 lines
Quoted 41 lines
kernel/bpf/syscall.c +29
-0+rcu_read_unlock (); + err = ptr ?0 : -ENOENT ; + }else { + err = -ENOTSUPP ; + } + +__this_cpu_dec (bpf_prog_active); +preempt_enable (); + +if (err) + goto free_value; + +if (copy_to_user (uvalue, value, value_size) !=0 ) + goto free_value; + + err =0 ; + +free_value: +kfree (value); +free_key: +kfree (key); +err_put: +fdput (f); +return err; +} +static const struct bpf_prog_ops *const bpf_prog_types[] = { #define BPF_PROG_TYPE(_id, _name) \ [_id] = & _name ## _prog_ops,@@ -2453,6 +2532,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizcase BPF_TASK_FD_QUERY : err =bpf_task_fd_query (&attr, uattr);break ; +case BPF_MAP_LOOKUP_AND_DELETE_ELEM : + err =map_lookup_and_delete_elem (&attr); +break ;default : err = -EINVAL ;break ;
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 127 lines · Song Liu
Quoted 112 lines · Mauricio Vasquez
Quoted 96 lines · Song Liu
Quoted 94 lines · Mauricio Vasquez B
The following patch implements a bpf queue/stack maps that provides the peek/pop/push functions. There is not a direct relationship between those functions and the current maps syscalls, hence a new
MAP_LOOKUP_AND_DELETE_ELEMsyscall is added, this is mapped to the pop operation in the queue/stack maps and it is still to implement in other kind of maps.Signed-off-by Mauricio Vasquez B <[email protected]>--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+)include/linux/bpf.h +1
-0@@ -39,6 +39,7 @@ struct bpf_map_ops {void *(*map_lookup_elem)(struct bpf_map *map,void *key);int (*map_update_elem)(struct bpf_map *map,void *key,void *value,u64 flags);int (*map_delete_elem)(struct bpf_map *map,void *key); +void *(*map_lookup_and_delete_elem)(struct bpf_map *map,void *key);/* funcs called by prog_array and perf_event_array map */ void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,include/uapi/linux/bpf.h +1
-0@@ -103,6 +103,7 @@ enum bpf_cmd {BPF_BTF_LOAD ,BPF_BTF_GET_FD_BY_ID ,BPF_TASK_FD_QUERY , + BPF_MAP_LOOKUP_AND_DELETE_ELEM, }; enum bpf_map_type {kernel/bpf/syscall.c +48
-0@@ -980,6 +980,85 @@ static int map_get_next_key(union bpf_attr *attr)return err; } +#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value + +static int map_lookup_and_delete_elem (union bpf_attr *attr) +{ +void __user *ukey =u64_to_user_ptr (attr->key); +void __user *uvalue =u64_to_user_ptr (attr->value); +int ufd = attr->map_fd; +struct bpf_map *map; +void *key, *value, *ptr; +u32 value_size; +struct fd f; +int err; + +if (CHECK_ATTR (BPF_MAP_LOOKUP_AND_DELETE_ELEM )) +return -EINVAL ; + + f =fdget (ufd); + map =__bpf_map_get (f); +if (IS_ERR (map)) +return PTR_ERR (map); + +if (!(f.file->f_mode &FMODE_CAN_WRITE )) { + err = -EPERM ; + goto err_put; + } + + key =__bpf_copy_key (ukey, map->key_size); +if (IS_ERR (key)) { + err =PTR_ERR (key); + goto err_put; + } + + value_size = map->value_size; + + err = -ENOMEM ; + value =kmalloc (value_size,GFP_USER | __GFP_NOWARN); +if (!value) + goto free_key; + + err = -EFAULT ; +if (copy_from_user (value, uvalue, value_size) !=0 ) + goto free_value; + +/* must increment bpf_prog_active to avoid kprobe+bpf triggering from +* inside bpf map update or delete otherwise deadlocks are possible +*/ +preempt_disable (); +__this_cpu_inc (bpf_prog_active);Quoted 8 lines
kernel/bpf/syscall.c static int map_get_next_key(union bpf_attr *attr) · line 1035
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+if (map->ops->map_lookup_and_delete_elem) { +rcu_read_lock (); + ptr = map->ops->map_lookup_and_delete_elem (map, key); +if (ptr) +memcpy (value, ptr, value_size);I think we are exposed to race condition with push and pop in parallel.
map_lookup_and_delete_elem()only updates the head/tail, so it gives no protection for the buffer pointed by ptr.queue/stack maps does not use this 'ptr', the pop operation directly copies the value into the buffer allocated in
map_lookup_and_delete_elem(). The copy from the queue/stack buffer into 'value' and the head/tail update are protected by a spinlock in the queue/stack maps implementation.Quoted 5 lines
On the other hand, future implementation of
map_lookup_and_deleteoperation in other kind of maps should guarantee that the return ptr is rcu protected.Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock(); ptr = map->ops->map_lookup_and_delete_elem(map, key); if (ptr) memcpy(value, ptr, value_size);
Quoted 4 lines
I guess this part is not used at all? Can we just remove it?
Thanks,
Song
This is the base code for map_lookup_and_delete support, it is not used in queue/stack maps.
I think we can leave it there, so when somebody implements lookup_and_delete for other maps doesn't have to care about implementing also this.
Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
kernel/bpf/syscall.c +29
-0+rcu_read_unlock (); + err = ptr ?0 : -ENOENT ; + }else { + err = -ENOTSUPP ; + } + +__this_cpu_dec (bpf_prog_active); +preempt_enable (); + +if (err) + goto free_value; + +if (copy_to_user (uvalue, value, value_size) !=0 ) + goto free_value; + + err =0 ; + +free_value: +kfree (value); +free_key: +kfree (key); +err_put: +fdput (f); +return err; +} +static const struct bpf_prog_ops *const bpf_prog_types[] = { #define BPF_PROG_TYPE(_id, _name) \ [_id] = & _name ## _prog_ops,@@ -2453,6 +2532,9 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, sizcase BPF_TASK_FD_QUERY : err =bpf_task_fd_query (&attr, uattr);break ; +case BPF_MAP_LOOKUP_AND_DELETE_ELEM : + err =map_lookup_and_delete_elem (&attr); +break ;default : err = -EINVAL ;break ;
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 11 lines · Mauricio Vasquez
Quoted 11 lines
Quoted 1 line
Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock(); ptr = map->ops->map_lookup_and_delete_elem(map, key); if (ptr) memcpy(value, ptr, value_size);I guess this part is not used at all? Can we just remove it?
Quoted 8 lines
Quoted 2 lines
Thanks,
SongThis is the base code for
map_lookup_and_deletesupport, it is not used in queue/stack maps.I think we can leave it there, so when somebody implements
lookup_and_deletefor other maps doesn't have to care about implementing also this.
The code looks useful to me, but I also agree with Song. And in the kernel we don't typically add 'almost dead code'.
May be provide an implementation of the lookup_and_delete for hash map so it's actually used ?
imo it would be a useful feature to have for hash map and will clarify the intent of it for all other maps and for stack/queue in particular.
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Mauricio Vasquez
Quoted 9 lines
Quoted 1 line
Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock(); ptr = map->ops->map_lookup_and_delete_elem(map, key); if (ptr) memcpy(value, ptr, value_size);Quoted 4 lines
I guess this part is not used at all? Can we just remove it?
Thanks,
SongThis is the base code for
map_lookup_and_deletesupport, it is not used in queue/stack maps.
Quoted 6 lines
Quoted 2 lines
I think we can leave it there, so when somebody implements
lookup_and_deletefor other maps doesn't have to care about implementing also this.The code looks useful to me, but I also agree with Song. And in the kernel we don't typically add 'almost dead code'.
May be provide an implementation of thelookup_and_deletefor hash map so it's actually used ?
I haven't written any code but I think there is a potential problem here.
Current lookup_and_delete returns a pointer to the element, hence deletion of the element should be done using call_rcu to guarantee this is valid after returning.
In the hashtab, the deletion only uses call_rcu when there is not prealloc, otherwise the element is pushed on the list of free elements immediately.
If we move the logic to push elements into the free list under a call_rcu invocation, it could happen that the free list is empty because the call_rcu is still pending (a similar issue we had with the queue/stack maps when they used a pass by reference API).
There is another way to implement it without this issue, in syscall.c:
l = ops->lookup(key);memcpy(l, some_buffer)ops->delete(key)
The point here is that the lookup_and_delete operation is not being used at all, so, is lookup + delete = lookup_and_delete?, can it be generalized? If this is true, then what is the sense of having lookup_and_delete syscall?,
Quoted 4 lines
imo it would be a useful feature to have for hash map and will clarify the intent of it for all other maps and for stack/queue in particular.
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 43 lines · Mauricio Vasquez
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Mauricio Vasquez
Quoted 9 lines
Quoted 1 line
Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock(); ptr = map->ops->map_lookup_and_delete_elem(map, key); if (ptr) memcpy(value, ptr, value_size);Quoted 4 lines
I guess this part is not used at all? Can we just remove it?
Thanks,
SongThis is the base code for
map_lookup_and_deletesupport, it is not used in queue/stack maps.Quoted 6 lines
Quoted 2 lines
I think we can leave it there, so when somebody implements
lookup_and_deletefor other maps doesn't have to care about implementing also this.The code looks useful to me, but I also agree with Song. And in the kernel we don't typically add 'almost dead code'.
May be provide an implementation of thelookup_and_deletefor hash map so it's actually used ?I haven't written any code but I think there is a potential problem here. Current
lookup_and_deletereturns a pointer to the element, hence deletion of the element should be done usingcall_rcuto guarantee this is valid after returning.
In the hashtab, the deletion only usescall_rcuwhen there is not prealloc, otherwise the element is pushed on the list of free elements immediately. If we move the logic to push elements into the free list under acall_rcuinvocation, it could happen that the free list is empty because thecall_rcuis still pending (a similar issue we had with the queue/stack maps when they used a pass by reference API).There is another way to implement it without this issue, in
syscall.c:
l =ops->lookup(key);memcpy(l, some_buffer)ops->delete(key)
Quoted 4 lines
The point here is that the
lookup_and_deleteoperation is not being used at all, so, is lookup + delete =lookup_and_delete?, can it be generalized? If this is true, then what is the sense of havinglookup_and_deletesyscall?,
I though of lookup_and_delete command as atomic operation. Only in such case it would make sense.
Otherwise there is no point in having additional cmd. In case of hash map the implementation would be similar to delete:
raw_spin_lock_irqsave(&b->lock, flags);
l = lookup_elem_raw(head, hash, key, key_size);
if (l) {
hlist_nulls_del_rcu(&l->hash_node);
bpf_long_memcpy(); // into temp kernel area
free_htab_elem(htab, l);
ret = 0; }
raw_spin_unlock_irqrestore(&b->lock, flags);
copy_to_user();
there is a chance that some other cpu is doing lookup in parallel and may be modifying value, so bpf_long_mempcy() isn't fully atomic. But bpf side is written together with user space side,
so above almost-atomic lookup_and_delete is usable instead of lookup and then delete which races too much.
Having said that I think it's fine to defer this new ndo for now and leave lookup_and_delete syscall cmd for stack/queue map only.
Re: [PATCH bpf-next v2 3/7] bpf: add MAP_LOOKUP_AND_DELETE_ELEM syscall
Quoted 50 lines · Alexei Starovoitov
Quoted 41 lines · Mauricio Vasquez
Quoted 17 lines · Alexei Starovoitov
Quoted 16 lines · Mauricio Vasquez
Quoted 9 lines
Quoted 1 line
Does it make sense to you?
I reread the other patch, and found it does NOT use the following logic for queue and stack:
rcu_read_lock(); ptr = map->ops->map_lookup_and_delete_elem(map, key); if (ptr) memcpy(value, ptr, value_size);Quoted 4 lines
I guess this part is not used at all? Can we just remove it?
Thanks,
SongThis is the base code for
map_lookup_and_deletesupport, it is not used in queue/stack maps.Quoted 6 lines
Quoted 2 lines
I think we can leave it there, so when somebody implements
lookup_and_deletefor other maps doesn't have to care about implementing also this.The code looks useful to me, but I also agree with Song. And in the kernel we don't typically add 'almost dead code'.
May be provide an implementation of thelookup_and_deletefor hash map so it's actually used ?I haven't written any code but I think there is a potential problem here. Current
lookup_and_deletereturns a pointer to the element, hence deletion of the element should be done usingcall_rcuto guarantee this is valid after returning.
In the hashtab, the deletion only usescall_rcuwhen there is not prealloc, otherwise the element is pushed on the list of free elements immediately. If we move the logic to push elements into the free list under acall_rcuinvocation, it could happen that the free list is empty because thecall_rcuis still pending (a similar issue we had with the queue/stack maps when they used a pass by reference API).There is another way to implement it without this issue, in
syscall.c:
l =ops->lookup(key);memcpy(l, some_buffer)ops->delete(key)Quoted 4 lines
The point here is that the
lookup_and_deleteoperation is not being used at all, so, is lookup + delete =lookup_and_delete?, can it be generalized? If this is true, then what is the sense of havinglookup_and_deletesyscall?,I though of
lookup_and_deletecommand as atomic operation. Only in such case it would make sense.
Otherwise there is no point in having additional cmd. In case of hash map the implementation would be similar to delete:
Quoted 10 lines
raw_spin_lock_irqsave(&b->lock, flags);
l = lookup_elem_raw(head, hash, key, key_size);
if (l) {
hlist_nulls_del_rcu(&l->hash_node);
bpf_long_memcpy(); // into temp kernel area
free_htab_elem(htab, l);
ret = 0;
}
raw_spin_unlock_irqrestore(&b->lock, flags);
copy_to_user();Well, this is new approach, currently operations have no enough info to perform the copy_to_user directly, btw, is there any technical reason why a double mem copy is done? (from the map value into a temp kernel buffer and then to userspace?)
Quoted 3 lines
there is a chance that some other cpu is doing lookup in parallel and may be modifying value, so
bpf_long_mempcy()isn't fully atomic.
I think we already have that case, if an eBPF program is updating the map, a lookup from userspace could see a partially updated value.
Quoted 7 lines
But bpf side is written together with user space side, so above almost-atomic
lookup_and_deleteis usable instead of lookup and then delete which races too much.Having said that I think it's fine to defer this new ndo for now and leave
lookup_and_deletesyscall cmd for stack/queue map only.
I agree, just a question, should we remove the "almost dead code" or leave it there?
Thanks,
Mauricio.