Skip to contentSkip to conversation

bpf: A few struct_ops fixes

PATCH Applied Martin KaFai Lau 2 participants 4 messages Mar 5, 2020

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

cover bpf: A few struct_ops fixes

This set addresses a few struct_ops issues.
Please see individual patch for details.

Martin KaFai Lau (2):
bpf: Return better error value in delete_elem for struct_ops map
bpf: Do not allow map_freeze in struct_ops map

 kernel/bpf/bpf_struct_ops.c | 14 +++++++++++---
 kernel/bpf/syscall.c        |  5 +++++
 2 files changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 bpf: Return better error value in delete_elem for struct_ops map

The current always succeed behavior in bpf_struct_ops_map_delete_elem() is not ideal for userspace tool. It can be improved to return proper error value.

If it is in TOBEFREE, it means unregistration has been already done before but it is in progress and waiting for the subsystem to clear the refcnt to zero, so -EINPROGRESS.

If it is INIT, it means the struct_ops has not been registered yet, so -ENOENT.

Fixes 85d33df357b6 ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS")
---
 kernel/bpf/bpf_struct_ops.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
kernel/bpf/bpf_struct_ops.c +11 -3
index 042f95534f86..68a89a9f7ccd 100644
@@ -482,13 +482,21 @@ static int bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
 	prev_state = cmpxchg(&st_map->kvalue.state,
 			     BPF_STRUCT_OPS_STATE_INUSE,
 			     BPF_STRUCT_OPS_STATE_TOBEFREE);
-	if (prev_state == BPF_STRUCT_OPS_STATE_INUSE) {
+	switch (prev_state) {
+	case BPF_STRUCT_OPS_STATE_INUSE:
 		st_map->st_ops->unreg(&st_map->kvalue.data);
 		if (refcount_dec_and_test(&st_map->kvalue.refcnt))
 			bpf_map_put(map);
+		return 0;
+	case BPF_STRUCT_OPS_STATE_TOBEFREE:
+		return -EINPROGRESS;
+	case BPF_STRUCT_OPS_STATE_INIT:
+		return -ENOENT;
+	default:
+		WARN_ON_ONCE(1);
+		/* Should never happen.  Treat it as not found. */
+		return -ENOENT;
 	}
-
-	return 0;
 }
 
 static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/2 bpf: Do not allow map_freeze in struct_ops map

In reply to Martin KaFai Lau

struct_ops map cannot support map_freeze. Otherwise, a struct_ops cannot be unregistered from the subsystem.

Fixes 85d33df357b6 ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS")
---
 kernel/bpf/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +5 -0
index a91ad518c050..0c7fb0d4836d 100644
@@ -1510,6 +1510,11 @@ static int map_freeze(const union bpf_attr *attr)
 	if (IS_ERR(map))
 		return PTR_ERR(map);
 
+	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
+		fdput(f);
+		return -ENOTSUPP;
+	}
+
 	mutex_lock(&map->freeze_mutex);
 
 	if (map->writecnt) {
Signature · 1 line
2.17.1
#3 lore raw
In reply to Martin KaFai Lau
Quoted 2 lines · Martin KaFai Lau

This set addresses a few struct_ops issues.
Please see individual patch for details.

Applied to bpf tree without this cover letter, since it's too terse.