BPF List
 help / color / mirror / Atom feed
* [PATCH] WIP: bpf: Support lookup_and_delete_elem for stackmap
@ 2024-03-08 16:49 Tao Chen
  0 siblings, 0 replies; only message in thread
From: Tao Chen @ 2024-03-08 16:49 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Yonghong Song, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa
  Cc: chen.dylane, bpf, linux-kernel

Extend lookup_and_delete_elem for stackmap, so we can clear the
elem in time in the userspace like the eBPF tool profile in bcc.

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
---
 include/linux/bpf.h   |  2 +-
 kernel/bpf/stackmap.c |  8 ++++++--
 kernel/bpf/syscall.c  | 12 +++++++++---
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 49f8b691496c..11d21e4e861b 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -2284,7 +2284,7 @@ int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
 			    u64 flags);
 
-int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
+int bpf_stackmap_copy_and_delete(struct bpf_map *map, void *key, void *value, bool delete);
 
 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
 				 void *key, void *value, u64 map_flags);
diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c
index 458bb80b14d5..935f537dced0 100644
--- a/kernel/bpf/stackmap.c
+++ b/kernel/bpf/stackmap.c
@@ -563,7 +563,7 @@ static void *stack_map_lookup_elem(struct bpf_map *map, void *key)
 }
 
 /* Called from syscall */
-int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
+int bpf_stackmap_copy_and_delete(struct bpf_map *map, void *key, void *value, bool delete)
 {
 	struct bpf_stack_map *smap = container_of(map, struct bpf_stack_map, map);
 	struct stack_map_bucket *bucket, *old_bucket;
@@ -580,7 +580,11 @@ int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
 	memcpy(value, bucket->data, trace_len);
 	memset(value + trace_len, 0, map->value_size - trace_len);
 
-	old_bucket = xchg(&smap->buckets[id], bucket);
+	if (!delete)
+		old_bucket = xchg(&smap->buckets[id], bucket);
+	else
+		old_bucket = bucket;
+
 	if (old_bucket)
 		pcpu_freelist_push(&smap->freelist, &old_bucket->fnode);
 	return 0;
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index d77b2f8b9364..77afce8710a4 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -225,7 +225,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
 		err = bpf_percpu_cgroup_storage_copy(map, key, value);
 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
-		err = bpf_stackmap_copy(map, key, value);
+		err = bpf_stackmap_copy_and_delete(map, key, value, false);
 	} else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
 		err = bpf_fd_array_map_lookup_elem(map, key, value);
 	} else if (IS_FD_HASH(map)) {
@@ -1372,7 +1372,8 @@ struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
 }
 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
 
-int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
+int __weak bpf_stackmap_copy_and_delete(struct bpf_map *map, void *key, void *value,
+										bool delete)
 {
 	return -ENOTSUPP;
 }
@@ -1897,7 +1898,8 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
 
 	if (attr->flags &&
 	    (map->map_type == BPF_MAP_TYPE_QUEUE ||
-	     map->map_type == BPF_MAP_TYPE_STACK)) {
+	     map->map_type == BPF_MAP_TYPE_STACK ||
+		 map->map_type == BPF_MAP_TYPE_STACK_TRACE)) {
 		err = -EINVAL;
 		goto err_put;
 	}
@@ -1936,6 +1938,10 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
 			rcu_read_unlock();
 			bpf_enable_instrumentation();
 		}
+	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
+		bpf_disable_instrumentation();
+		err = bpf_stackmap_copy_and_delete(map, key, value, true);
+		bpf_enable_instrumentation();
 	}
 
 	if (err)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-08 16:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-08 16:49 [PATCH] WIP: bpf: Support lookup_and_delete_elem for stackmap Tao Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox