remove unnecessary -EOPNOTSUPP callbacks
Subsystems (3)
cover remove unnecessary -EOPNOTSUPP callbacks
Remove unnecessary callbacks that simply return -EOPNOTSUPP for following map operations:
- map_peek_elem
- map_push_elem
- map_pop_elem
- map_delete_elem
- map_get_next_key
These operations are not supported for certain map types and can be handled by generic map code instead of having redundant single-line callbacks.
Xiaomeng Zhang (5):
bpf: Remove map_peek_elem callbacks with -EOPNOTSUPP
bpf: Remove map_push_elem callbacks with -EOPNOTSUPP
bpf: Remove map_pop_elem callbacks with -EOPNOTSUPP
bpf: Remove map_delete_elem callbacks with -EOPNOTSUPP
bpf: Remove map_get_next_key callbacks with -EOPNOTSUPP
kernel/bpf/arena.c | 30 ------------------------------ kernel/bpf/bloom_filter.c | 18 ------------------ kernel/bpf/bpf_cgrp_storage.c | 6 ------ kernel/bpf/bpf_inode_storage.c | 7 ------- kernel/bpf/bpf_task_storage.c | 6 ------ kernel/bpf/helpers.c | 20 ++++++++++++++++---- kernel/bpf/ringbuf.c | 15 --------------- kernel/bpf/syscall.c | 30 ++++++++++++++++++++++++------ 8 files changed, 40 insertions(+), 92 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/5 bpf: Remove map_peek_elem callbacks with -EOPNOTSUPP
Remove redundant map_peek_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when calling the unimplemented callbacks.
--- kernel/bpf/arena.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/syscall.c | 5 ++++- 3 files changed, 8 insertions(+), 8 deletions(-)
kernel/bpf/arena.c +0 -6
@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_peek_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static long arena_map_push_elem (struct bpf_map *map,void *value,u64 flags) {return -EOPNOTSUPP ;
@@ -404,7 +399,6 @@ const struct bpf_map_ops arena_map_ops = { .map_get_unmapped_area = arena_get_unmapped_area, .map_get_next_key = arena_map_get_next_key, .map_push_elem = arena_map_push_elem, - .map_peek_elem = arena_map_peek_elem, .map_pop_elem = arena_map_pop_elem, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem,
kernel/bpf/helpers.c +4 -1
@@ -116,7 +116,10 @@ const struct bpf_func_proto bpf_map_pop_elem_proto = { BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value) { -return map->ops->map_peek_elem (map, value); +if (map->ops->map_peek_elem) +return map->ops->map_peek_elem (map, value); +else +return -EOPNOTSUPP ; }const struct bpf_func_proto bpf_map_peek_elem_proto = {
kernel/bpf/syscall.c +4 -1
@@ -325,7 +325,10 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK || map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { - err = map->ops->map_peek_elem (map, value); +if (map->ops->map_peek_elem) + err = map->ops->map_peek_elem (map, value); +else + err = -EOPNOTSUPP ; }else if (map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {/* struct_ops map requires directly updating "value" */ err =bpf_struct_ops_map_sys_lookup_elem (map, key, value);
Signature · 1 line
2.34.1
Re: [PATCH -next 1/5] bpf: Remove map_peek_elem callbacks with -EOPNOTSUPP
Hi,
Quoted 39 lines · Xiaomeng Zhang
Remove redundant
map_peek_elemcallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.Signed-off-by Xiaomeng Zhang <[email protected]>--- kernel/bpf/arena.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/syscall.c | 5 ++++- 3 files changed, 8 insertions(+), 8 deletions(-)kernel/bpf/arena.c +0
-6@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_peek_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static long arena_map_push_elem (struct bpf_map *map,void *value,u64 flags) {return -EOPNOTSUPP ;@@ -404,7 +399,6 @@ const struct bpf_map_ops arena_map_ops = { .map_get_unmapped_area = arena_get_unmapped_area, .map_get_next_key = arena_map_get_next_key, .map_push_elem = arena_map_push_elem, - .map_peek_elem = arena_map_peek_elem, .map_pop_elem = arena_map_pop_elem, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem,kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c const struct bpf_func_proto bpf_map_pop_elem_proto = { · line 123
Quoted 9 lines
kernel/bpf/helpers.c +4
-1@@ -116,7 +116,10 @@ const struct bpf_func_proto bpf_map_pop_elem_proto = { BPF_CALL_2(bpf_map_peek_elem, struct bpf_map *, map, void *, value) { -return map->ops->map_peek_elem (map, value); +if (map->ops->map_peek_elem) +return map->ops->map_peek_elem (map, value); +else +return -EOPNOTSUPP ; }
It is unnecessary. Because the verifier has ensured the bpf_map_peek_elem helper is only available for specific maps in check_map_func_compatibility().
Quoted 7 lines
kernel/bpf/helpers.c +0
-0const struct bpf_func_proto bpf_map_peek_elem_proto = {kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, · line 331
Quoted 8 lines
kernel/bpf/syscall.c +4
-1@@ -325,7 +325,10 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK || map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { - err = map->ops->map_peek_elem (map, value); +if (map->ops->map_peek_elem) + err = map->ops->map_peek_elem (map, value); +else + err = -EOPNOTSUPP ;
It is also unnecessary due to the similar reason.
Quoted 3 lines
kernel/bpf/syscall.c +0
-0}else if (map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {/* struct_ops map requires directly updating "value" */ err =bpf_struct_ops_map_sys_lookup_elem (map, key, value);
PATCH 2/5 bpf: Remove map_push_elem callbacks with -EOPNOTSUPP
In reply to Xiaomeng ZhangRemove redundant map_push_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when calling the unimplemented callbacks.
--- kernel/bpf/arena.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/syscall.c | 5 ++++- 3 files changed, 8 insertions(+), 8 deletions(-)
kernel/bpf/arena.c +0 -6
@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_push_elem (struct bpf_map *map,void *value,u64 flags) -{ -return -EOPNOTSUPP ; -} -static long arena_map_pop_elem (struct bpf_map *map,void *value) {return -EOPNOTSUPP ;
@@ -398,7 +393,6 @@ const struct bpf_map_ops arena_map_ops = { .map_mmap = arena_map_mmap, .map_get_unmapped_area = arena_get_unmapped_area, .map_get_next_key = arena_map_get_next_key, - .map_push_elem = arena_map_push_elem, .map_pop_elem = arena_map_pop_elem, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem,
kernel/bpf/helpers.c +4 -1
@@ -88,7 +88,10 @@ const struct bpf_func_proto bpf_map_delete_elem_proto = { BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags) { -return map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) +return map->ops->map_push_elem (map, value, flags); +else +return -EOPNOTSUPP ; }const struct bpf_func_proto bpf_map_push_elem_proto = {
kernel/bpf/syscall.c +4 -1
@@ -281,7 +281,10 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK || map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { - err = map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) + err = map->ops->map_push_elem (map, value, flags); +else + err = -EOPNOTSUPP ; }else { err =bpf_obj_pin_uptrs (map->record, value);if (!err) {
Signature · 1 line
2.34.1
Re: [PATCH -next 2/5] bpf: Remove map_push_elem callbacks with -EOPNOTSUPP
kernel/bpf/helpers.c const struct bpf_func_proto bpf_map_delete_elem_proto = { · line 95
Quoted 12 lines · Xiaomeng Zhang
Remove redundant
map_push_elemcallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.
BPF_CALL_3(bpf_map_push_elem, struct bpf_map *, map, void *, value, u64, flags){kernel/bpf/helpers.c +4
-1-return map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) +return map->ops->map_push_elem (map, value, flags); +else +return -EOPNOTSUPP ; }
Similar with the previous patch, the modifications in both bpf_map_push_elem() and bpf_map_update_valu() are unnecessary.
Quoted 18 lines
const struct
bpf_func_protobpf_map_push_elem_proto= {kernel/bpf/syscall.c +4
-1@@ -281,7 +281,10 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK || map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { - err = map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) + err = map->ops->map_push_elem (map, value, flags); +else + err = -EOPNOTSUPP ; }else { err =bpf_obj_pin_uptrs (map->record, value);if (!err) {
回复: [PATCH -next 2/5] bpf: Remove map_push_elem callbacks with -EOPNOTSUPP
Hi,
Although when the BPF program invokes the bpf_map_peek_elem helper, the verifier intercepts it and ensures that this helper is only available for specific map types through check_map_func_compatibility(), there is no such verifier interception when this function is called from the syscall side.
Therefore, it is necessary to include appropriate checks in the syscall implementation to handle cases where map->ops->map_peek_elem may not be supported by certain map types. Without these checks, calling bpf_map_peek_elem through syscalls could lead to undefined behavior or errors if the map does not support this operation.
-----邮件原件-----
发件人: Hou Tao <[email protected]>
发送时间: 2025年2月17日 10:28
收件人: zhangxiaomeng (A) <[email protected]>; [email protected]
抄送: Alexei Starovoitov <[email protected]>; Daniel Borkmann <[email protected]>; Andrii Nakryiko <[email protected]>; Martin KaFai Lau <[email protected]>; Eduard Zingerman <[email protected]>; Song Liu <[email protected]>; Yonghong Song <[email protected]>; John Fastabend <[email protected]>; KP Singh <[email protected]>; Stanislav Fomichev <[email protected]>; Hao Luo <[email protected]>; Jiri Olsa <[email protected]> 主题: Re: [PATCH -next 2/5] bpf: Remove map_push_elem callbacks with -EOPNOTSUPP
kernel/bpf/helpers.c const struct bpf_func_proto bpf_map_delete_elem_proto = { · line 95
Quoted 12 lines · Xiaomeng Zhang
Remove redundant
map_push_elemcallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.BPF_CALL_3(bpf_map_push_elem, struct
bpf_map*, map, void *, value,
u64, flags) {kernel/bpf/helpers.c +4
-1-return map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) +return map->ops->map_push_elem (map, value, flags); +else +return -EOPNOTSUPP ; }
Similar with the previous patch, the modifications in both bpf_map_push_elem() and bpf_map_update_valu() are unnecessary.
Quoted 18 lines
const struct
bpf_func_protobpf_map_push_elem_proto= { diff --gita/kernel/bpf/syscall.cb/kernel/bpf/syscall.cindexe6e859f71c5d..79a118ea9270100644kernel/bpf/syscall.c +4
-1@@ -281,7 +281,10 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file, }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK || map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { - err = map->ops->map_push_elem (map, value, flags); +if (map->ops->map_push_elem) + err = map->ops->map_push_elem (map, value, flags); +else + err = -EOPNOTSUPP ; }else { err =bpf_obj_pin_uptrs (map->record, value);if (!err) {
PATCH 3/5 bpf: Remove map_pop_elem callbacks with -EOPNOTSUPP
In reply to Xiaomeng ZhangRemove redundant map_pop_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when calling the unimplemented callbacks.
--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/syscall.c | 5 ++++- 4 files changed, 8 insertions(+), 14 deletions(-)
kernel/bpf/arena.c +0 -6
@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_pop_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static long arena_map_delete_elem (struct bpf_map *map,void *value) {return -EOPNOTSUPP ;
@@ -393,7 +388,6 @@ const struct bpf_map_ops arena_map_ops = { .map_mmap = arena_map_mmap, .map_get_unmapped_area = arena_get_unmapped_area, .map_get_next_key = arena_map_get_next_key, - .map_pop_elem = arena_map_pop_elem, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem, .map_delete_elem = arena_map_delete_elem,
kernel/bpf/bloom_filter.c +0 -6
@@ -65,11 +65,6 @@ static long bloom_map_push_elem(struct bpf_map *map, void *value, u64 flags)return 0 ; } -static long bloom_map_pop_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static long bloom_map_delete_elem (struct bpf_map *map,void *value) {return -EOPNOTSUPP ;
@@ -209,7 +204,6 @@ const struct bpf_map_ops bloom_filter_map_ops = { .map_get_next_key = bloom_map_get_next_key, .map_push_elem = bloom_map_push_elem, .map_peek_elem = bloom_map_peek_elem, - .map_pop_elem = bloom_map_pop_elem, .map_lookup_elem = bloom_map_lookup_elem, .map_update_elem = bloom_map_update_elem, .map_delete_elem = bloom_map_delete_elem,
kernel/bpf/helpers.c +4 -1
@@ -106,7 +106,10 @@ const struct bpf_func_proto bpf_map_push_elem_proto = { BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value) { -return map->ops->map_pop_elem (map, value); +if (map->ops->map_pop_elem) +return map->ops->map_pop_elem (map, value); +else +return -EOPNOTSUPP ; }const struct bpf_func_proto bpf_map_pop_elem_proto = {
kernel/bpf/syscall.c +4 -1
@@ -2142,7 +2142,10 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr) err = -ENOTSUPP ;if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK ) { - err = map->ops->map_pop_elem (map, value); +if (map->ops->map_pop_elem) + err = map->ops->map_pop_elem (map, value); +else + err = -EOPNOTSUPP ; } else if (map->map_type == BPF_MAP_TYPE_HASH || map->map_type == BPF_MAP_TYPE_PERCPU_HASH || map->map_type == BPF_MAP_TYPE_LRU_HASH ||
Signature · 1 line
2.34.1
Re: [PATCH -next 3/5] bpf: Remove map_pop_elem callbacks with -EOPNOTSUPP
Quoted 15 lines · Xiaomeng Zhang
Remove redundant
map_pop_elemcallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.Signed-off-by Xiaomeng Zhang <[email protected]>--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/syscall.c | 5 ++++- 4 files changed, 8 insertions(+), 14 deletions(-)
BPF_CALL_2(bpf_map_pop_elem, struct bpf_map *, map, void *, value){
kernel/bpf/helpers.c const struct bpf_func_proto bpf_map_push_elem_proto = { · line 113
Quoted 6 lines
kernel/bpf/helpers.c +4
-1-return map->ops->map_pop_elem (map, value); +if (map->ops->map_pop_elem) +return map->ops->map_pop_elem (map, value); +else +return -EOPNOTSUPP ; }
Similar with the previous patch, it is unnecessary.
Quoted 6 lines
const struct
bpf_func_protobpf_map_pop_elem_proto= {kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int map_lookup_and_delete_elem(union bpf_attr *attr) · line 2148
Quoted 8 lines
kernel/bpf/syscall.c +4
-1@@ -2142,7 +2142,10 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr) err = -ENOTSUPP ;if (map->map_type ==BPF_MAP_TYPE_QUEUE || map->map_type ==BPF_MAP_TYPE_STACK ) { - err = map->ops->map_pop_elem (map, value); +if (map->ops->map_pop_elem) + err = map->ops->map_pop_elem (map, value); +else + err = -EOPNOTSUPP ;
It is also unnecessary.
Quoted 3 lines
kernel/bpf/syscall.c +0
-0} else if (map->map_type == BPF_MAP_TYPE_HASH || map->map_type == BPF_MAP_TYPE_PERCPU_HASH || map->map_type == BPF_MAP_TYPE_LRU_HASH ||
PATCH 4/5 bpf: Remove map_delete_elem callbacks with -EOPNOTSUPP
In reply to Xiaomeng ZhangRemove redundant map_delete_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when calling the unimplemented callbacks.
--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/ringbuf.c | 7 ------- kernel/bpf/syscall.c | 5 ++++- 5 files changed, 8 insertions(+), 21 deletions(-)
kernel/bpf/arena.c +0 -6
@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_delete_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static int arena_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {return -EOPNOTSUPP ;
@@ -390,7 +385,6 @@ const struct bpf_map_ops arena_map_ops = { .map_get_next_key = arena_map_get_next_key, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem, - .map_delete_elem = arena_map_delete_elem, .map_check_btf = arena_map_check_btf, .map_mem_usage = arena_map_mem_usage, .map_btf_id = &bpf_arena_map_btf_ids[0],
kernel/bpf/bloom_filter.c +0 -6
@@ -65,11 +65,6 @@ static long bloom_map_push_elem(struct bpf_map *map, void *value, u64 flags)return 0 ; } -static long bloom_map_delete_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static int bloom_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {return -EOPNOTSUPP ;
@@ -206,7 +201,6 @@ const struct bpf_map_ops bloom_filter_map_ops = { .map_peek_elem = bloom_map_peek_elem, .map_lookup_elem = bloom_map_lookup_elem, .map_update_elem = bloom_map_update_elem, - .map_delete_elem = bloom_map_delete_elem, .map_check_btf = bloom_map_check_btf, .map_mem_usage = bloom_map_mem_usage, .map_btf_id = &bpf_bloom_map_btf_ids[0],
kernel/bpf/helpers.c +4 -1
@@ -74,7 +74,10 @@ BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) {WARN_ON_ONCE (!rcu_read_lock_held () && !rcu_read_lock_trace_held () && !rcu_read_lock_bh_held ()); -return map->ops->map_delete_elem (map, key); +if (map->ops->map_delete_elem) +return map->ops->map_delete_elem (map, key); +else +return -EOPNOTSUPP ; }const struct bpf_func_proto bpf_map_delete_elem_proto = {
kernel/bpf/ringbuf.c +0 -7
@@ -247,11 +247,6 @@ static long ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,return -ENOTSUPP ; } -static long ringbuf_map_delete_elem (struct bpf_map *map,void *key) -{ -return -ENOTSUPP ; -} -static int ringbuf_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {
@@ -356,7 +351,6 @@ const struct bpf_map_ops ringbuf_map_ops = { .map_poll = ringbuf_map_poll_kern, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &ringbuf_map_btf_ids[0],
@@ -371,7 +365,6 @@ const struct bpf_map_ops user_ringbuf_map_ops = { .map_poll = ringbuf_map_poll_user, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &user_ringbuf_map_btf_ids[0],
kernel/bpf/syscall.c +4 -1
@@ -1789,7 +1789,10 @@ static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) }else if (IS_FD_PROG_ARRAY (map) || map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {/* These maps require sleepable context */ - err = map->ops->map_delete_elem (map, key); +if (map->ops->map_delete_elem) + err = map->ops->map_delete_elem (map, key); +else + err = -EOPNOTSUPP ; goto out; }
Signature · 1 line
2.34.1
Re: [PATCH -next 4/5] bpf: Remove map_delete_elem callbacks with -EOPNOTSUPP
Quoted 65 lines · Xiaomeng Zhang
Remove redundant
map_delete_elemcallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.Signed-off-by Xiaomeng Zhang <[email protected]>--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/helpers.c | 5 ++++- kernel/bpf/ringbuf.c | 7 ------- kernel/bpf/syscall.c | 5 ++++- 5 files changed, 8 insertions(+), 21 deletions(-)kernel/bpf/arena.c +0
-6@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static long arena_map_delete_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static int arena_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {return -EOPNOTSUPP ;@@ -390,7 +385,6 @@ const struct bpf_map_ops arena_map_ops = { .map_get_next_key = arena_map_get_next_key, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem, - .map_delete_elem = arena_map_delete_elem, .map_check_btf = arena_map_check_btf, .map_mem_usage = arena_map_mem_usage, .map_btf_id = &bpf_arena_map_btf_ids[0],kernel/bpf/bloom_filter.c +0
-6@@ -65,11 +65,6 @@ static long bloom_map_push_elem(struct bpf_map *map, void *value, u64 flags)return 0 ; } -static long bloom_map_delete_elem (struct bpf_map *map,void *value) -{ -return -EOPNOTSUPP ; -} -static int bloom_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {return -EOPNOTSUPP ;@@ -206,7 +201,6 @@ const struct bpf_map_ops bloom_filter_map_ops = { .map_peek_elem = bloom_map_peek_elem, .map_lookup_elem = bloom_map_lookup_elem, .map_update_elem = bloom_map_update_elem, - .map_delete_elem = bloom_map_delete_elem, .map_check_btf = bloom_map_check_btf, .map_mem_usage = bloom_map_mem_usage, .map_btf_id = &bpf_bloom_map_btf_ids[0],kernel/bpf/helpers.c +0
-0
kernel/bpf/helpers.c BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) · line 82
Quoted 10 lines
kernel/bpf/helpers.c +4
-1@@ -74,7 +74,10 @@ BPF_CALL_2(bpf_map_delete_elem, struct bpf_map *, map, void *, key) {WARN_ON_ONCE (!rcu_read_lock_held () && !rcu_read_lock_trace_held () && !rcu_read_lock_bh_held ()); -return map->ops->map_delete_elem (map, key); +if (map->ops->map_delete_elem) +return map->ops->map_delete_elem (map, key); +else +return -EOPNOTSUPP ; }
It will be better to add the check for arena in check_map_func_compatibility() statically, instead of adding an extra branch for each invocation of bpf_map_delete_elem.
Quoted 38 lines
kernel/bpf/helpers.c +0
-0const struct bpf_func_proto bpf_map_delete_elem_proto = {kernel/bpf/ringbuf.c +0
-7@@ -247,11 +247,6 @@ static long ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,return -ENOTSUPP ; } -static long ringbuf_map_delete_elem (struct bpf_map *map,void *key) -{ -return -ENOTSUPP ; -} -static int ringbuf_map_get_next_key (struct bpf_map *map,void *key,void *next_key) {@@ -356,7 +351,6 @@ const struct bpf_map_ops ringbuf_map_ops = { .map_poll = ringbuf_map_poll_kern, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &ringbuf_map_btf_ids[0],@@ -371,7 +365,6 @@ const struct bpf_map_ops user_ringbuf_map_ops = { .map_poll = ringbuf_map_poll_user, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_delete_elem = ringbuf_map_delete_elem, .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &user_ringbuf_map_btf_ids[0],kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) · line 1796
Quoted 9 lines
kernel/bpf/syscall.c +4
-1@@ -1789,7 +1789,10 @@ static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr) }else if (IS_FD_PROG_ARRAY (map) || map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {/* These maps require sleepable context */ - err = map->ops->map_delete_elem (map, key); +if (map->ops->map_delete_elem) + err = map->ops->map_delete_elem (map, key); +else + err = -EOPNOTSUPP ; goto out;
The invocation has already checked the map type, the extra checking of ->map_delete_elem is unnecessary.
Quoted 2 lines
kernel/bpf/syscall.c +0
-0}
PATCH 5/5 bpf: Remove map_get_next_key callbacks with -EOPNOTSUPP
In reply to Xiaomeng ZhangRemove redundant map_get_next_key callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when calling the unimplemented callbacks.
--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/bpf_cgrp_storage.c | 6 ------ kernel/bpf/bpf_inode_storage.c | 7 ------- kernel/bpf/bpf_task_storage.c | 6 ------ kernel/bpf/ringbuf.c | 8 -------- kernel/bpf/syscall.c | 10 ++++++++-- 7 files changed, 8 insertions(+), 41 deletions(-)
kernel/bpf/arena.c +0 -6
@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; } -static int arena_map_get_next_key (struct bpf_map *map,void *key,void *next_key) -{ -return -EOPNOTSUPP ; -} -static long compute_pgoff (struct bpf_arena *arena,long uaddr) {return (u32)(uaddr - (u32 )arena->user_vm_start) >>PAGE_SHIFT ;
@@ -382,7 +377,6 @@ const struct bpf_map_ops arena_map_ops = { .map_direct_value_addr = arena_map_direct_value_addr, .map_mmap = arena_map_mmap, .map_get_unmapped_area = arena_get_unmapped_area, - .map_get_next_key = arena_map_get_next_key, .map_lookup_elem = arena_map_lookup_elem, .map_update_elem = arena_map_update_elem, .map_check_btf = arena_map_check_btf,
kernel/bpf/bloom_filter.c +0 -6
@@ -65,11 +65,6 @@ static long bloom_map_push_elem(struct bpf_map *map, void *value, u64 flags)return 0 ; } -static int bloom_map_get_next_key (struct bpf_map *map,void *key,void *next_key) -{ -return -EOPNOTSUPP ; -} -/* Called from syscall */ static int bloom_map_alloc_check (union bpf_attr *attr) {
@@ -196,7 +191,6 @@ const struct bpf_map_ops bloom_filter_map_ops = { .map_alloc_check = bloom_map_alloc_check, .map_alloc = bloom_map_alloc, .map_free = bloom_map_free, - .map_get_next_key = bloom_map_get_next_key, .map_push_elem = bloom_map_push_elem, .map_peek_elem = bloom_map_peek_elem, .map_lookup_elem = bloom_map_lookup_elem,
kernel/bpf/bpf_cgrp_storage.c +0 -6
@@ -141,11 +141,6 @@ static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key)return err; } -static int notsupp_get_next_key (struct bpf_map *map,void *key,void *next_key) -{ -return -ENOTSUPP ; -} -static struct bpf_map *cgroup_storage_map_alloc (union bpf_attr *attr) {return bpf_local_storage_map_alloc (attr, &cgroup_cache, true);
@@ -208,7 +203,6 @@ const struct bpf_map_ops cgrp_storage_map_ops = { .map_alloc_check = bpf_local_storage_map_alloc_check, .map_alloc = cgroup_storage_map_alloc, .map_free = cgroup_storage_map_free, - .map_get_next_key = notsupp_get_next_key, .map_lookup_elem = bpf_cgrp_storage_lookup_elem, .map_update_elem = bpf_cgrp_storage_update_elem, .map_delete_elem = bpf_cgrp_storage_delete_elem,
kernel/bpf/bpf_inode_storage.c +0 -7
@@ -175,12 +175,6 @@ BPF_CALL_2(bpf_inode_storage_delete,return inode_storage_delete (inode, map); } -static int notsupp_get_next_key (struct bpf_map *map,void *key, -void *next_key) -{ -return -ENOTSUPP ; -} -static struct bpf_map *inode_storage_map_alloc (union bpf_attr *attr) {return bpf_local_storage_map_alloc (attr, &inode_cache, false);
@@ -196,7 +190,6 @@ const struct bpf_map_ops inode_storage_map_ops = { .map_alloc_check = bpf_local_storage_map_alloc_check, .map_alloc = inode_storage_map_alloc, .map_free = inode_storage_map_free, - .map_get_next_key = notsupp_get_next_key, .map_lookup_elem = bpf_fd_inode_storage_lookup_elem, .map_update_elem = bpf_fd_inode_storage_update_elem, .map_delete_elem = bpf_fd_inode_storage_delete_elem,
kernel/bpf/bpf_task_storage.c +0 -6
@@ -303,11 +303,6 @@ BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *,return ret; } -static int notsupp_get_next_key (struct bpf_map *map,void *key,void *next_key) -{ -return -ENOTSUPP ; -} -static struct bpf_map *task_storage_map_alloc (union bpf_attr *attr) {return bpf_local_storage_map_alloc (attr, &task_cache, true);
@@ -324,7 +319,6 @@ const struct bpf_map_ops task_storage_map_ops = { .map_alloc_check = bpf_local_storage_map_alloc_check, .map_alloc = task_storage_map_alloc, .map_free = task_storage_map_free, - .map_get_next_key = notsupp_get_next_key, .map_lookup_elem = bpf_pid_task_storage_lookup_elem, .map_update_elem = bpf_pid_task_storage_update_elem, .map_delete_elem = bpf_pid_task_storage_delete_elem,
kernel/bpf/ringbuf.c +0 -8
@@ -247,12 +247,6 @@ static long ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,return -ENOTSUPP ; } -static int ringbuf_map_get_next_key (struct bpf_map *map,void *key, -void *next_key) -{ -return -ENOTSUPP ; -} -static int ringbuf_map_mmap_kern (struct bpf_map *map,struct vm_area_struct *vma) {struct bpf_ringbuf_map *rb_map;
@@ -351,7 +345,6 @@ const struct bpf_map_ops ringbuf_map_ops = { .map_poll = ringbuf_map_poll_kern, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &ringbuf_map_btf_ids[0], };
@@ -365,7 +358,6 @@ const struct bpf_map_ops user_ringbuf_map_ops = { .map_poll = ringbuf_map_poll_user, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &user_ringbuf_map_btf_ids[0], };
kernel/bpf/syscall.c +8 -2
@@ -1850,7 +1850,10 @@ static int map_get_next_key(union bpf_attr *attr) }rcu_read_lock (); - err = map->ops->map_get_next_key (map, key, next_key); +if (map->ops->map_get_next_key) + err = map->ops->map_get_next_key (map, key, next_key); +else + err = -EOPNOTSUPP ;rcu_read_unlock (); out:if (err)
@@ -2037,7 +2040,10 @@ int generic_map_lookup_batch(struct bpf_map *map,for (cp =0 ; cp < max_count;) {rcu_read_lock (); - err = map->ops->map_get_next_key (map, prev_key, key); +if (map->ops->map_get_next_key) + err = map->ops->map_get_next_key (map, prev_key, key); +else + err = -EOPNOTSUPP ;rcu_read_unlock ();if (err)break ;
Signature · 1 line
2.34.1
Re: [PATCH -next 5/5] bpf: Remove map_get_next_key callbacks with -EOPNOTSUPP
Hi,
Quoted 22 lines · Xiaomeng Zhang
Remove redundant
map_get_next_keycallbacks with return-EOPNOTSUPP. We can directly return-EOPNOTSUPPwhen calling the unimplemented callbacks.Signed-off-by Xiaomeng Zhang <[email protected]>--- kernel/bpf/arena.c | 6 ------ kernel/bpf/bloom_filter.c | 6 ------ kernel/bpf/bpf_cgrp_storage.c | 6 ------ kernel/bpf/bpf_inode_storage.c | 7 ------- kernel/bpf/bpf_task_storage.c | 6 ------ kernel/bpf/ringbuf.c | 8 -------- kernel/bpf/syscall.c | 10 ++++++++-- 7 files changed, 8 insertions(+), 41 deletions(-)kernel/bpf/arena.c +0
-0@@ -62,11 +62,6 @@ u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena)return arena ? arena->user_vm_start :0 ; }
SNIP
Quoted 38 lines
kernel/bpf/ringbuf.c +0
-8@@ -247,12 +247,6 @@ static long ringbuf_map_update_elem(struct bpf_map *map, void *key, void *value,return -ENOTSUPP ; } -static int ringbuf_map_get_next_key (struct bpf_map *map,void *key, -void *next_key) -{ -return -ENOTSUPP ; -} -static int ringbuf_map_mmap_kern (struct bpf_map *map,struct vm_area_struct *vma) {struct bpf_ringbuf_map *rb_map;@@ -351,7 +345,6 @@ const struct bpf_map_ops ringbuf_map_ops = { .map_poll = ringbuf_map_poll_kern, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &ringbuf_map_btf_ids[0], };@@ -365,7 +358,6 @@ const struct bpf_map_ops user_ringbuf_map_ops = { .map_poll = ringbuf_map_poll_user, .map_lookup_elem = ringbuf_map_lookup_elem, .map_update_elem = ringbuf_map_update_elem, - .map_get_next_key = ringbuf_map_get_next_key, .map_mem_usage = ringbuf_map_mem_usage, .map_btf_id = &user_ringbuf_map_btf_ids[0], };kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int map_get_next_key(union bpf_attr *attr) · line 1856
Quoted 8 lines
kernel/bpf/syscall.c +4
-1@@ -1850,7 +1850,10 @@ static int map_get_next_key(union bpf_attr *attr) }rcu_read_lock (); - err = map->ops->map_get_next_key (map, key, next_key); +if (map->ops->map_get_next_key) + err = map->ops->map_get_next_key (map, key, next_key); +else + err = -EOPNOTSUPP ;
The old return value is ENOTSUPP. For consistency, I think it is better to still return ENOTSUPP.
kernel/bpf/syscall.c int generic_map_lookup_batch(struct bpf_map *map, · line 2047
Quoted 13 lines
kernel/bpf/syscall.c +4
-1rcu_read_unlock (); out:if (err)@@ -2037,7 +2040,10 @@ int generic_map_lookup_batch(struct bpf_map *map,for (cp =0 ; cp < max_count;) {rcu_read_lock (); - err = map->ops->map_get_next_key (map, prev_key, key); +if (map->ops->map_get_next_key) + err = map->ops->map_get_next_key (map, prev_key, key); +else + err = -EOPNOTSUPP ;rcu_read_unlock ();
How about only check ->map_get_next_key() once instead of checking it every time ? And there are another two invocations of ->map_get_next_key(), please update these invocations as well.
Quoted 2 lines
kernel/bpf/syscall.c +0
-0if (err)break ;