bpf: A few struct_ops fixes
Subsystems (4)
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
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.
--- kernel/bpf/bpf_struct_ops.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
kernel/bpf/bpf_struct_ops.c +11 -3
@@ -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
PATCH 2/2 bpf: Do not allow map_freeze in struct_ops map
In reply to Martin KaFai Laustruct_ops map cannot support map_freeze. Otherwise, a struct_ops cannot be unregistered from the subsystem.
--- kernel/bpf/syscall.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/bpf/syscall.c +5 -0
@@ -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
Alexei Starovoitov
AppliedQuoted 2 lines · Martin KaFai Lau
This set addresses a few
struct_opsissues.
Please see individual patch for details.
Applied to bpf tree without this cover letter, since it's too terse.