Skip to contentSkip to conversation

remove unnecessary -EOPNOTSUPP callbacks

PATCH In review Xiaomeng Zhang 2 participants 12 messages Feb 17, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 095a9554e1de..0aaefa5d6b09 100644
@@ -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
index f27ce162427a..0f429171de6d 100644
@@ -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
index ff1f980bd59a..e6e859f71c5d 100644
@@ -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
#2 lore raw

Re: [PATCH -next 1/5] bpf: Remove map_peek_elem callbacks with -EOPNOTSUPP

Hi,

Quoted 39 lines · Xiaomeng Zhang

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
index 095a9554e1de..0aaefa5d6b09 100644
@@ -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
index f27ce162427a..0f429171de6d 100644

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 -0
 const struct bpf_func_proto bpf_map_peek_elem_proto = {
kernel/bpf/syscall.c +0 -0
index ff1f980bd59a..e6e859f71c5d 100644

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);
#3 lore raw

PATCH 2/5 bpf: Remove map_push_elem callbacks with -EOPNOTSUPP

In reply to Xiaomeng Zhang

Remove 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
index 0aaefa5d6b09..7ee98aeccf3c 100644
@@ -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
index 0f429171de6d..000409c8308a 100644
@@ -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
index e6e859f71c5d..79a118ea9270 100644
@@ -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
#4 lore raw

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_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when 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_proto bpf_map_push_elem_proto = {

kernel/bpf/syscall.c +4 -1
index e6e859f71c5d..79a118ea9270 100644
@@ -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) {
#5 lore raw

回复: [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_elem callbacks with return -EOPNOTSUPP. We can directly return -EOPNOTSUPP when 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_proto bpf_map_push_elem_proto = { diff --git
a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index e6e859f71c5d..79a118ea9270 100644

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) {
#6 lore raw

PATCH 3/5 bpf: Remove map_pop_elem callbacks with -EOPNOTSUPP

In reply to Xiaomeng Zhang

Remove 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
index 7ee98aeccf3c..2c95baa8ece2 100644
@@ -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
index 35e1ddca74d2..d8d4dd7b711d 100644
@@ -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
index 000409c8308a..cb61c06155c8 100644
@@ -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
index 79a118ea9270..c6f55283f4ff 100644
@@ -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
#7 lore raw

Re: [PATCH -next 3/5] bpf: Remove map_pop_elem callbacks with -EOPNOTSUPP

Quoted 15 lines · Xiaomeng Zhang

Remove 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(-)

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_proto bpf_map_pop_elem_proto = {

kernel/bpf/syscall.c +0 -0
index 79a118ea9270..c6f55283f4ff 100644

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 ||
#8 lore raw

PATCH 4/5 bpf: Remove map_delete_elem callbacks with -EOPNOTSUPP

In reply to Xiaomeng Zhang

Remove 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
index 2c95baa8ece2..50f07bbd33fa 100644
@@ -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
index d8d4dd7b711d..f3bba8ac2532 100644
@@ -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
index cb61c06155c8..132c2830c758 100644
@@ -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
index 1499d8caa9a3..e2d22f10a11f 100644
@@ -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
index c6f55283f4ff..36eed7016da4 100644
@@ -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
#9 lore raw

Re: [PATCH -next 4/5] bpf: Remove map_delete_elem callbacks with -EOPNOTSUPP

Quoted 65 lines · Xiaomeng Zhang

Remove 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
index 2c95baa8ece2..50f07bbd33fa 100644
@@ -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
index d8d4dd7b711d..f3bba8ac2532 100644
@@ -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
index cb61c06155c8..132c2830c758 100644

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 -0
 const struct bpf_func_proto bpf_map_delete_elem_proto = {
kernel/bpf/ringbuf.c +0 -7
index 1499d8caa9a3..e2d22f10a11f 100644
@@ -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
index c6f55283f4ff..36eed7016da4 100644

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
 	}
 
#10 lore raw

PATCH 5/5 bpf: Remove map_get_next_key callbacks with -EOPNOTSUPP

In reply to Xiaomeng Zhang

Remove 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
index 50f07bbd33fa..7f37ef1b72ce 100644
@@ -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
index f3bba8ac2532..3aed40c92c26 100644
@@ -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
index 54ff2a85d4c0..305f23c36afc 100644
@@ -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
index 15a3eb9b02d9..6db158c2a42b 100644
@@ -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
index 1109475953c0..3bdb49aab55f 100644
@@ -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
index e2d22f10a11f..932a6c06e3e0 100644
@@ -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
index 36eed7016da4..087abbacbd05 100644
@@ -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
#11 lore raw

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_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 -0
index 50f07bbd33fa..7f37ef1b72ce 100644
@@ -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
index e2d22f10a11f..932a6c06e3e0 100644
@@ -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
index 36eed7016da4..087abbacbd05 100644

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 -1
 	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();

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 -0
 		if (err)
 			break;