Skip to content

Commit 8f4bc15

Browse files
davemarchevskyAlexei Starovoitov
authored andcommitted
selftests/bpf: Add write to hashmap to array_map iter test
Modify iter prog in existing bpf_iter_bpf_array_map.c, which currently dumps arraymap key/val, to also do a write of (val, key) into a newly-added hashmap. Confirm that the write succeeds as expected by modifying the userspace runner program. Before a change added in an earlier commit - considering PTR_TO_BUF reg a valid input to helpers which expect MAP_{KEY,VAL} - the verifier would've rejected this prog change due to type mismatch. Since using current iter's key/val to access a separate map is a reasonable usecase, let's add support for it. Note that the test prog cannot directly write (val, key) into hashmap via bpf_map_update_elem when both come from iter context because key is marked MEM_RDONLY. This is due to bpf_map_update_elem - and other basic map helpers - taking ARG_PTR_TO_MAP_{KEY,VALUE} w/o MEM_RDONLY type flag. bpf_map_{lookup,update,delete}_elem don't modify their input key/val so it should be possible to tag their args READONLY, but due to the ubiquitous use of these helpers and verifier checks for type == MAP_VALUE, such a change is nontrivial and seems better to address in a followup series. Also fixup some 'goto's in test runner's map checking loop. Signed-off-by: Dave Marchevsky <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 51ee71d commit 8f4bc15

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,10 @@ static void test_bpf_array_map(void)
941941
{
942942
__u64 val, expected_val = 0, res_first_val, first_val = 0;
943943
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
944-
__u32 expected_key = 0, res_first_key;
944+
__u32 key, expected_key = 0, res_first_key;
945+
int err, i, map_fd, hash_fd, iter_fd;
945946
struct bpf_iter_bpf_array_map *skel;
946947
union bpf_iter_link_info linfo;
947-
int err, i, map_fd, iter_fd;
948948
struct bpf_link *link;
949949
char buf[64] = {};
950950
int len, start;
@@ -1001,12 +1001,20 @@ static void test_bpf_array_map(void)
10011001
if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
10021002
goto close_iter;
10031003

1004+
hash_fd = bpf_map__fd(skel->maps.hashmap1);
10041005
for (i = 0; i < bpf_map__max_entries(skel->maps.arraymap1); i++) {
10051006
err = bpf_map_lookup_elem(map_fd, &i, &val);
1006-
if (!ASSERT_OK(err, "map_lookup"))
1007-
goto out;
1008-
if (!ASSERT_EQ(i, val, "invalid_val"))
1009-
goto out;
1007+
if (!ASSERT_OK(err, "map_lookup arraymap1"))
1008+
goto close_iter;
1009+
if (!ASSERT_EQ(i, val, "invalid_val arraymap1"))
1010+
goto close_iter;
1011+
1012+
val = i + 4;
1013+
err = bpf_map_lookup_elem(hash_fd, &val, &key);
1014+
if (!ASSERT_OK(err, "map_lookup hashmap1"))
1015+
goto close_iter;
1016+
if (!ASSERT_EQ(key, val - 4, "invalid_val hashmap1"))
1017+
goto close_iter;
10101018
}
10111019

10121020
close_iter:

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ struct {
1919
__type(value, __u64);
2020
} arraymap1 SEC(".maps");
2121

22+
struct {
23+
__uint(type, BPF_MAP_TYPE_HASH);
24+
__uint(max_entries, 10);
25+
__type(key, __u64);
26+
__type(value, __u32);
27+
} hashmap1 SEC(".maps");
28+
2229
__u32 key_sum = 0;
2330
__u64 val_sum = 0;
2431

2532
SEC("iter/bpf_map_elem")
2633
int dump_bpf_array_map(struct bpf_iter__bpf_map_elem *ctx)
2734
{
28-
__u32 *key = ctx->key;
35+
__u32 *hmap_val, *key = ctx->key;
2936
__u64 *val = ctx->value;
3037

3138
if (key == (void *)0 || val == (void *)0)
@@ -35,6 +42,18 @@ int dump_bpf_array_map(struct bpf_iter__bpf_map_elem *ctx)
3542
bpf_seq_write(ctx->meta->seq, val, sizeof(__u64));
3643
key_sum += *key;
3744
val_sum += *val;
45+
46+
/* workaround - It's necessary to do this convoluted (val, key)
47+
* write into hashmap1, instead of simply doing
48+
* bpf_map_update_elem(&hashmap1, val, key, BPF_ANY);
49+
* because key has MEM_RDONLY flag and bpf_map_update elem expects
50+
* types without this flag
51+
*/
52+
bpf_map_update_elem(&hashmap1, val, val, BPF_ANY);
53+
hmap_val = bpf_map_lookup_elem(&hashmap1, val);
54+
if (hmap_val)
55+
*hmap_val = *key;
56+
3857
*val = *key;
3958
return 0;
4059
}

0 commit comments

Comments
 (0)