Skip to content

Commit

Permalink
bpf: allow any program to use the bpf_map_sum_elem_count kfunc
Browse files Browse the repository at this point in the history
Register the bpf_map_sum_elem_count func for all programs, and update the
map_ptr subtest of the test_progs test to test the new functionality.

The usage is allowed as long as the pointer to the map is trusted (when
using tracing programs) or is a const pointer to map, as in the following
example:

    struct {
            __uint(type, BPF_MAP_TYPE_HASH);
            ...
    } hash SEC(".maps");

    ...

    static inline int some_bpf_prog(void)
    {
            struct bpf_map *map = (struct bpf_map *)&hash;
            __s64 count;

            count = bpf_map_sum_elem_count(map);

            ...
    }

Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
Link: https://lore.kernel.org/r/20230719092952.41202-5-aspsk@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Anton Protopopov authored and Alexei Starovoitov committed Jul 19, 2023
1 parent 9c29804 commit 72829b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kernel/bpf/map_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,6 @@ static const struct btf_kfunc_id_set bpf_map_iter_kfunc_set = {

static int init_subsystem(void)
{
return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_map_iter_kfunc_set);
return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &bpf_map_iter_kfunc_set);
}
late_initcall(init_subsystem);
5 changes: 5 additions & 0 deletions tools/testing/selftests/bpf/progs/map_ptr_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ struct {
__type(value, __u32);
} m_hash SEC(".maps");

__s64 bpf_map_sum_elem_count(struct bpf_map *map) __ksym;

static inline int check_hash(void)
{
struct bpf_htab *hash = (struct bpf_htab *)&m_hash;
Expand All @@ -115,6 +117,8 @@ static inline int check_hash(void)
VERIFY(hash->elem_size == 64);

VERIFY(hash->count.counter == 0);
VERIFY(bpf_map_sum_elem_count(map) == 0);

for (i = 0; i < HALF_ENTRIES; ++i) {
const __u32 key = i;
const __u32 val = 1;
Expand All @@ -123,6 +127,7 @@ static inline int check_hash(void)
return 0;
}
VERIFY(hash->count.counter == HALF_ENTRIES);
VERIFY(bpf_map_sum_elem_count(map) == HALF_ENTRIES);

return 1;
}
Expand Down

0 comments on commit 72829b1

Please sign in to comment.