Commit Graph

11 Commits

Author SHA1 Message Date
Jerome Marchand e76c5acdde libbpf: add non-mmapable data section selftest
Bugzilla: https://bugzilla.redhat.com/2177177

commit 2f968e9f4a953037f798802006ecd298c014b5b4
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Oct 18 17:28:16 2022 -0700

    libbpf: add non-mmapable data section selftest

    Add non-mmapable data section to test_skeleton selftest and make sure it
    really isn't mmapable by trying to mmap() it anyways.

    Also make sure that libbpf doesn't report BPF_F_MMAPABLE flag to users.

    Additional, some more manual testing was performed that this feature
    works as intended.

    Looking at created map through bpftool shows that flags passed to kernel are
    indeed zero:

      $ bpftool map show
      ...
      1782: array  name .data.non_mmapa  flags 0x0
              key 4B  value 16B  max_entries 1  memlock 4096B
              btf_id 1169
              pids test_progs(8311)
      ...

    Checking BTF uploaded to kernel for this map shows that zero_key and
    zero_value are indeed marked as static, even though zero_key is actually
    original global (but STV_HIDDEN) variable:

      $ bpftool btf dump id 1169
      ...
      [51] VAR 'zero_key' type_id=2, linkage=static
      [52] VAR 'zero_value' type_id=7, linkage=static
      ...
      [62] DATASEC '.data.non_mmapable' size=16 vlen=2
              type_id=51 offset=0 size=4 (VAR 'zero_key')
              type_id=52 offset=4 size=12 (VAR 'zero_value')
      ...

    And original BTF does have zero_key marked as linkage=global:

      $ bpftool btf dump file test_skeleton.bpf.linked3.o
      ...
      [51] VAR 'zero_key' type_id=2, linkage=global
      [52] VAR 'zero_value' type_id=7, linkage=static
      ...
      [62] DATASEC '.data.non_mmapable' size=16 vlen=2
              type_id=51 offset=0 size=4 (VAR 'zero_key')
              type_id=52 offset=4 size=12 (VAR 'zero_value')

    Bpftool didn't require any changes at all because it checks whether internal
    map is mmapable already, but just to double-check generated skeleton, we
    see that .data.non_mmapable neither sets mmaped pointer nor has
    a corresponding field in the skeleton:

      $ grep non_mmapable test_skeleton.skel.h
                      struct bpf_map *data_non_mmapable;
              s->maps[7].name = ".data.non_mmapable";
              s->maps[7].map = &obj->maps.data_non_mmapable;

    But .data.read_mostly has all of those things:

      $ grep read_mostly test_skeleton.skel.h
                      struct bpf_map *data_read_mostly;
              struct test_skeleton__data_read_mostly {
                      int read_mostly_var;
              } *data_read_mostly;
              s->maps[6].name = ".data.read_mostly";
              s->maps[6].map = &obj->maps.data_read_mostly;
              s->maps[6].mmaped = (void **)&obj->data_read_mostly;
              _Static_assert(sizeof(s->data_read_mostly->read_mostly_var) == 4, "unexpected size of 'read_mostly_var'");

    Acked-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: Dave Marchevsky <davemarchevsky@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20221019002816.359650-4-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:42:53 +02:00
Artem Savkov 9616c2812d selftests/bpf: validate .bss section bigger than 8MB is possible now
Bugzilla: https://bugzilla.redhat.com/2137876

commit 24316461200502aa5feddaa72dcbb8059503a528
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Jul 14 22:31:46 2022 -0700

    selftests/bpf: validate .bss section bigger than 8MB is possible now
    
    Add a simple big 16MB array and validate access to the very last byte of
    it to make sure that kernel supports > KMALLOC_MAX_SIZE value_size for
    BPF array maps (which are backing .bss in this case).
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220715053146.1291891-5-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:40 +01:00
Yauheni Kaliuta c4f7b56c82 selftests/bpf: Demonstrate use of custom .rodata/.data sections
Bugzilla: http://bugzilla.redhat.com/2069045

commit 30c5bd96476ced0d3e08372be5186ff7f421d10c
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Oct 20 18:44:02 2021 -0700

    selftests/bpf: Demonstrate use of custom .rodata/.data sections
    
    Enhance existing selftests to demonstrate the use of custom
    .data/.rodata sections.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211021014404.2635234-9-andrii@kernel.org

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:44 +03:00
Yauheni Kaliuta 80f110c743 selftests/bpf: Add checks for X__elf_bytes() skeleton helper
Bugzilla: http://bugzilla.redhat.com/2069045

commit 980a1a4c342f353a62d64174d0a6a9466a741273
Author: Matt Smith <alastorze@fb.com>
Date:   Wed Sep 1 12:44:39 2021 -0700

    selftests/bpf: Add checks for X__elf_bytes() skeleton helper
    
    This patch adds two checks for the X__elf_bytes BPF skeleton helper
    method. The first asserts that the pointer returned from the helper
    method is valid, the second asserts that the provided size pointer is
    set.
    
    Signed-off-by: Matt Smith <alastorze@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210901194439.3853238-4-alastorze@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:15:38 +03:00
Andrii Nakryiko 256eab48e7 selftests/bpf: Stop using static variables for passing data to/from user-space
In preparation of skipping emitting static variables in BPF skeletons, switch
all current selftests uses of static variables to pass data between BPF and
user-space to use global variables.

All non-read-only `static volatile` variables become just plain global
variables by dropping `static volatile` part.

Read-only `static volatile const` variables, though, still require `volatile`
modifier, otherwise compiler will ignore whatever values are set from
user-space.

Few static linker tests are using name-conflicting static variables to
validate that static linker still properly handles static variables and
doesn't trip up on name conflicts.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210507054119.270888-4-andrii@kernel.org
2021-05-11 15:07:17 -07:00
Andrii Nakryiko 0b20933d8c tools/bpftool: Strip away modifiers from global variables
Reliably remove all the type modifiers from read-only (.rodata) global
variable definitions, including cases of inner field const modifiers and
arrays of const values.

Also modify one of selftests to ensure that const volatile struct doesn't
prevent user-space from modifying .rodata variable.

Fixes: 985ead416d ("bpftool: Add skeleton codegen command")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200713232409.3062144-3-andriin@fb.com
2020-07-13 17:07:43 -07:00
Andrii Nakryiko caf62492f4 libbpf: Support pre-initializing .bss global variables
Remove invalid assumption in libbpf that .bss map doesn't have to be updated
in kernel. With addition of skeleton and memory-mapped initialization image,
.bss doesn't have to be all zeroes when BPF map is created, because user-code
might have initialized those variables from user-space.

Fixes: eba9c5f498 ("libbpf: Refactor global data map initialization")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200612194504.557844-1-andriin@fb.com
2020-06-12 15:27:47 -07:00
Andrii Nakryiko 81bfdd087b libbpf: Put Kconfig externs into .kconfig section
Move Kconfig-provided externs into custom .kconfig section. Add __kconfig into
bpf_helpers.h for user convenience. Update selftests accordingly.

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191219002837.3074619-2-andriin@fb.com
2019-12-18 17:33:36 -08:00
Andrii Nakryiko 5dc7a8b211 bpftool, selftests/bpf: Embed object file inside skeleton
Embed contents of BPF object file used for BPF skeleton generation inside
skeleton itself. This allows to keep BPF object file and its skeleton in sync
at all times, and simpifies skeleton instantiation.

Also switch existing selftests to not require BPF_EMBED_OBJ anymore.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20191218052552.2915188-2-andriin@fb.com
2019-12-17 22:16:35 -08:00
Andrii Nakryiko 330a73a7b6 selftests/bpf: Add tests for libbpf-provided externs
Add a set of tests validating libbpf-provided extern variables. One crucial
feature that's tested is dead code elimination together with using invalid BPF
helper. CONFIG_MISSING is not supposed to exist and should always be specified
by libbpf as zero, which allows BPF verifier to correctly do branch pruning
and not fail validation, when invalid BPF helper is called from dead if branch.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191214014710.3449601-5-andriin@fb.com
2019-12-15 16:41:12 -08:00
Andrii Nakryiko 197448eaac selftests/bpf: Add test validating data section to struct convertion layout
Add a simple selftests validating datasection-to-struct layour dumping. Global
variables are constructed in such a way as to cause both natural and
artificial padding (through custom alignment requirement).

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-17-andriin@fb.com
2019-12-15 15:58:05 -08:00