lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20240308164911.118640-1-chen.dylane@gmail.com>
Date: Sat,  9 Mar 2024 00:49:11 +0800
From: Tao Chen <chen.dylane@...il.com>
To: Alexei Starovoitov <ast@...nel.org>,
	Daniel Borkmann <daniel@...earbox.net>,
	John Fastabend <john.fastabend@...il.com>,
	Andrii Nakryiko <andrii@...nel.org>,
	Martin KaFai Lau <martin.lau@...ux.dev>,
	Yonghong Song <yonghong.song@...ux.dev>,
	KP Singh <kpsingh@...nel.org>,
	Stanislav Fomichev <sdf@...gle.com>,
	Hao Luo <haoluo@...gle.com>,
	Jiri Olsa <jolsa@...nel.org>
Cc: chen.dylane@...il.com,
	bpf@...r.kernel.org,
	linux-kernel@...r.kernel.org
Subject: [PATCH] WIP: bpf: Support lookup_and_delete_elem for stackmap

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@...il.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


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ