netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf 0/2] bpf: A few struct_ops fixes
@ 2020-03-05  1:34 Martin KaFai Lau
  2020-03-05  1:34 ` [PATCH bpf 1/2] bpf: Return better error value in delete_elem for struct_ops map Martin KaFai Lau
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Martin KaFai Lau @ 2020-03-05  1:34 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, David Miller, kernel-team, netdev

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

-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH bpf 1/2] bpf: Return better error value in delete_elem for struct_ops map
  2020-03-05  1:34 [PATCH bpf 0/2] bpf: A few struct_ops fixes Martin KaFai Lau
@ 2020-03-05  1:34 ` Martin KaFai Lau
  2020-03-05  1:34 ` [PATCH bpf 2/2] bpf: Do not allow map_freeze in " Martin KaFai Lau
  2020-03-05 22:25 ` [PATCH bpf 0/2] bpf: A few struct_ops fixes Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Martin KaFai Lau @ 2020-03-05  1:34 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, David Miller, kernel-team, netdev

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")
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
---
 kernel/bpf/bpf_struct_ops.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index 042f95534f86..68a89a9f7ccd 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -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,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH bpf 2/2] bpf: Do not allow map_freeze in struct_ops map
  2020-03-05  1:34 [PATCH bpf 0/2] bpf: A few struct_ops fixes Martin KaFai Lau
  2020-03-05  1:34 ` [PATCH bpf 1/2] bpf: Return better error value in delete_elem for struct_ops map Martin KaFai Lau
@ 2020-03-05  1:34 ` Martin KaFai Lau
  2020-03-05 22:25 ` [PATCH bpf 0/2] bpf: A few struct_ops fixes Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Martin KaFai Lau @ 2020-03-05  1:34 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, David Miller, kernel-team, netdev

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")
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
---
 kernel/bpf/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index a91ad518c050..0c7fb0d4836d 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -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) {
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH bpf 0/2] bpf: A few struct_ops fixes
  2020-03-05  1:34 [PATCH bpf 0/2] bpf: A few struct_ops fixes Martin KaFai Lau
  2020-03-05  1:34 ` [PATCH bpf 1/2] bpf: Return better error value in delete_elem for struct_ops map Martin KaFai Lau
  2020-03-05  1:34 ` [PATCH bpf 2/2] bpf: Do not allow map_freeze in " Martin KaFai Lau
@ 2020-03-05 22:25 ` Alexei Starovoitov
  2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2020-03-05 22:25 UTC (permalink / raw)
  To: Martin KaFai Lau
  Cc: bpf, Alexei Starovoitov, Daniel Borkmann, David Miller,
	kernel-team, netdev

On Wed, Mar 04, 2020 at 05:34:37PM -0800, Martin KaFai Lau wrote:
> 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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-03-05 22:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-05  1:34 [PATCH bpf 0/2] bpf: A few struct_ops fixes Martin KaFai Lau
2020-03-05  1:34 ` [PATCH bpf 1/2] bpf: Return better error value in delete_elem for struct_ops map Martin KaFai Lau
2020-03-05  1:34 ` [PATCH bpf 2/2] bpf: Do not allow map_freeze in " Martin KaFai Lau
2020-03-05 22:25 ` [PATCH bpf 0/2] bpf: A few struct_ops fixes Alexei Starovoitov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).