Skip to content

Commit 4daab71

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add bpf_{update, delete}_map_elem in hashmap iter program
Added bpf_{updata,delete}_map_elem to the very map element the iter program is visiting. Due to rcu protection, the visited map elements, although stale, should still contain correct values. $ ./test_progs -n 4/18 #4/18 bpf_hash_map:OK #4 bpf_iter:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent dc0988b commit 4daab71

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

tools/testing/selftests/bpf/progs/bpf_iter_bpf_hash_map.c

+15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx)
4747
__u32 seq_num = ctx->meta->seq_num;
4848
struct bpf_map *map = ctx->map;
4949
struct key_t *key = ctx->key;
50+
struct key_t tmp_key;
5051
__u64 *val = ctx->value;
52+
__u64 tmp_val = 0;
53+
int ret;
5154

5255
if (in_test_mode) {
5356
/* test mode is used by selftests to
@@ -61,6 +64,18 @@ int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx)
6164
if (key == (void *)0 || val == (void *)0)
6265
return 0;
6366

67+
/* update the value and then delete the <key, value> pair.
68+
* it should not impact the existing 'val' which is still
69+
* accessible under rcu.
70+
*/
71+
__builtin_memcpy(&tmp_key, key, sizeof(struct key_t));
72+
ret = bpf_map_update_elem(&hashmap1, &tmp_key, &tmp_val, 0);
73+
if (ret)
74+
return 0;
75+
ret = bpf_map_delete_elem(&hashmap1, &tmp_key);
76+
if (ret)
77+
return 0;
78+
6479
key_sum_a += key->a;
6580
key_sum_b += key->b;
6681
key_sum_c += key->c;

0 commit comments

Comments
 (0)