Commit Graph

52 Commits

Author SHA1 Message Date
Jerome Marchand 9187621f65 selftests/bpf: fix some typos in selftests
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 5db0ba6766f8a6606e655ddad745c87bc01349c7
Author: Lin Yikai <yikai.lin@vivo.com>
Date:   Thu Sep 5 19:03:05 2024 +0800

    selftests/bpf: fix some typos in selftests

    Hi, fix some spelling errors in selftest, the details are as follows:

    -in the codes:
    	test_bpf_sk_stoarge_map_iter_fd(void)
    		->test_bpf_sk_storage_map_iter_fd(void)
    	load BTF from btf_data.o->load BTF from btf_data.bpf.o

    -in the code comments:
    	preample->preamble
    	multi-contollers->multi-controllers
    	errono->errno
    	unsighed/unsinged->unsigned
    	egree->egress
    	shoud->should
    	regsiter->register
    	assummed->assumed
    	conditiona->conditional
    	rougly->roughly
    	timetamp->timestamp
    	ingores->ignores
    	null-termainted->null-terminated
    	slepable->sleepable
    	implemenation->implementation
    	veriables->variables
    	timetamps->timestamps
    	substitue a costant->substitute a constant
    	secton->section
    	unreferened->unreferenced
    	verifer->verifier
    	libppf->libbpf
    ...

    Signed-off-by: Lin Yikai <yikai.lin@vivo.com>
    Link: https://lore.kernel.org/r/20240905110354.3274546-1-yikai.lin@vivo.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:03 +01:00
Viktor Malik aca6eeeb50
selftests/bpf: Add a selftest to check for incorrect names
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 743070894724bf5ee0b2c77a28f838f6244d19bd
Author: Jeongjun Park <aha310510@gmail.com>
Date:   Sat Aug 31 14:47:42 2024 +0900

    selftests/bpf: Add a selftest to check for incorrect names
    
    Add selftest for cases where btf_name_valid_section() does not properly
    check for certain types of names.
    
    Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Jeongjun Park <aha310510@gmail.com>
    Link: https://lore.kernel.org/r/20240831054742.364585-1-aha310510@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:20 +01:00
Jerome Marchand 114c4f7fe5 selftests/bpf: Test cases for '?' in BTF names
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 5208930a909ad618363471e2872d79abef103626
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Mar 6 12:45:29 2024 +0200

    selftests/bpf: Test cases for '?' in BTF names

    Two test cases to verify that '?' and other printable characters are
    allowed in BTF DATASEC names:
    - DATASEC with name "?.foo bar:buz" should be accepted;
    - type with name "?foo" should be rejected.

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240306104529.6453-16-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:14 +02:00
Viktor Malik 525975f0f3
selftests/bpf: Remove flaky test_btf_id test
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 56925f389e152dcb8d093435d43b78a310539c23
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Thu Dec 14 12:38:20 2023 -0800

    selftests/bpf: Remove flaky test_btf_id test
    
    With previous patch, one of subtests in test_btf_id becomes
    flaky and may fail. The following is a failing example:
    
      Error: #26 btf
      Error: #26/174 btf/BTF ID
        Error: #26/174 btf/BTF ID
        btf_raw_create:PASS:check 0 nsec
        btf_raw_create:PASS:check 0 nsec
        test_btf_id:PASS:check 0 nsec
        ...
        test_btf_id:PASS:check 0 nsec
        test_btf_id:FAIL:check BTF lingersdo_test_get_info:FAIL:check failed: -1
    
    The test tries to prove a btf_id not available after the map is closed.
    But btf_id is freed only after workqueue and a rcu grace period, compared
    to previous case just after a rcu grade period.
    Depending on system workload, workqueue could take quite some time
    to execute function bpf_map_free_deferred() which may cause the test failure.
    Instead of adding arbitrary delays, let us remove the logic to
    check btf_id availability after map is closed.
    
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20231214203820.1469402-1-yonghong.song@linux.dev
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:28 +02:00
Viktor Malik 78b98f163b
selftests/bpf: satisfy compiler by having explicit return in btf test
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit f4c7e887324f5776eef6e6e47a90e0ac8058a7a8
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 1 20:37:44 2023 -0700

    selftests/bpf: satisfy compiler by having explicit return in btf test
    
    Some compilers complain about get_pprint_mapv_size() not returning value
    in some code paths. Fix with explicit return.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231102033759.2541186-3-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:35 +02:00
Artem Savkov 7c7e7d4dd4 libbpf: Add basic BTF sanity validation
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit 3903802bb99a263a3c26422c3d30a121b1f6f939
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Aug 25 13:21:52 2023 -0700

    libbpf: Add basic BTF sanity validation
    
    Implement a simple and straightforward BTF sanity check when parsing BTF
    data. Right now it's very basic and just validates that all the string
    offsets and type IDs are within valid range. For FUNC we also check that
    it points to FUNC_PROTO kinds.
    
    Even with such simple checks it fixes a bunch of crashes found by OSS
    fuzzer ([0]-[5]) and will allow fuzzer to make further progress.
    
    Some other invariants will be checked in follow up patches (like
    ensuring there is no infinite type loops), but this seems like a good
    start already.
    
    Adding FUNC -> FUNC_PROTO check revealed that one of selftests has
    a problem with FUNC pointing to VAR instead, so fix it up in the same
    commit.
    
      [0] https://github.com/libbpf/libbpf/issues/482
      [1] https://github.com/libbpf/libbpf/issues/483
      [2] https://github.com/libbpf/libbpf/issues/485
      [3] https://github.com/libbpf/libbpf/issues/613
      [4] https://github.com/libbpf/libbpf/issues/618
      [5] https://github.com/libbpf/libbpf/issues/619
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Reviewed-by: Song Liu <song@kernel.org>
    Closes: https://github.com/libbpf/libbpf/issues/617
    Link: https://lore.kernel.org/bpf/20230825202152.1813394-1-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:45 +01:00
Viktor Malik a0db075e83
selftests/bpf: Add a test where map key_type_id with decl_tag type
JIRA: https://issues.redhat.com/browse/RHEL-9957

commit e38096d95f4d7e8cc15280b4a3515eee31925561
Author: Yonghong Song <yhs@fb.com>
Date:   Tue May 30 13:50:34 2023 -0700

    selftests/bpf: Add a test where map key_type_id with decl_tag type
    
    Add two selftests where map creation key/value type_id's are
    decl_tags. Without previous patch, kernel warnings will
    appear similar to the one in the previous patch. With the previous
    patch, both kernel warnings are silenced.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20230530205034.266643-1-yhs@fb.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-10-26 17:06:16 +02:00
Viktor Malik fac6e8a619 selftests/bpf: check that modifier resolves after pointer
Bugzilla: https://bugzilla.redhat.com/2178930

commit dfdd608c3b365f0fd49d7e13911ebcde06b9865b
Author: Lorenz Bauer <lorenz.bauer@isovalent.com>
Date:   Mon Mar 6 11:21:38 2023 +0000

    selftests/bpf: check that modifier resolves after pointer
    
    Add a regression test that ensures that a VAR pointing at a
    modifier which follows a PTR (or STRUCT or ARRAY) is resolved
    correctly by the datasec validator.
    
    Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
    Link: https://lore.kernel.org/r/20230306112138.155352-3-lmb@isovalent.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:45 +02:00
Viktor Malik 7a4a8ac4be selftests/bpf: Use bpf_{btf,link,map,prog}_get_info_by_fd()
Bugzilla: https://bugzilla.redhat.com/2178930

commit c5a237a4db21ca7a28518c994def39d7bd62a0d1
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 15 00:12:18 2023 +0100

    selftests/bpf: Use bpf_{btf,link,map,prog}_get_info_by_fd()
    
    Use the new type-safe wrappers around bpf_obj_get_info_by_fd().
    Fix a prog/map mixup in prog_holds_map().
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20230214231221.249277-6-iii@linux.ibm.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:39 +02:00
Jerome Marchand d6db7dbbbc selftests/bpf: Add reproducer for decl_tag in func_proto argument
Bugzilla: https://bugzilla.redhat.com/2177177

commit 8e898aaa733eca61393fc036c8a4b5834fee5dd3
Author: Stanislav Fomichev <sdf@google.com>
Date:   Tue Nov 22 19:54:21 2022 -0800

    selftests/bpf: Add reproducer for decl_tag in func_proto argument

    It should trigger a WARN_ON_ONCE in btf_type_id_size:

      RIP: 0010:btf_type_id_size+0x8bd/0x940 kernel/bpf/btf.c:1952
      btf_func_proto_check kernel/bpf/btf.c:4506 [inline]
      btf_check_all_types kernel/bpf/btf.c:4734 [inline]
      btf_parse_type_sec+0x1175/0x1980 kernel/bpf/btf.c:4763
      btf_parse kernel/bpf/btf.c:5042 [inline]
      btf_new_fd+0x65a/0xb00 kernel/bpf/btf.c:6709
      bpf_btf_load+0x6f/0x90 kernel/bpf/syscall.c:4342
      __sys_bpf+0x50a/0x6c0 kernel/bpf/syscall.c:5034
      __do_sys_bpf kernel/bpf/syscall.c:5093 [inline]
      __se_sys_bpf kernel/bpf/syscall.c:5091 [inline]
      __x64_sys_bpf+0x7c/0x90 kernel/bpf/syscall.c:5091
      do_syscall_64+0x54/0x70 arch/x86/entry/common.c:48

    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20221123035422.872531-1-sdf@google.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:10 +02:00
Jerome Marchand 92c7dd3bae selftests/bpf: Tests for btf_dedup_resolve_fwds
Bugzilla: https://bugzilla.redhat.com/2177177

commit 99e18fad5ff79eea2ea9ec7e6d800b286552e005
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Nov 9 16:26:11 2022 +0200

    selftests/bpf: Tests for btf_dedup_resolve_fwds

    Tests to verify the following behavior of `btf_dedup_resolve_fwds`:
    - remapping for struct forward declarations;
    - remapping for union forward declarations;
    - no remapping if forward declaration kind does not match similarly
      named struct or union declaration;
    - no remapping if forward declaration name is ambiguous;
    - base ids are considered for fwd resolution in split btf scenario.

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20221109142611.879983-4-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:02 +02:00
Jerome Marchand 3ace20b67d selftests/bpf: Tests for enum fwd resolved as full enum64
Bugzilla: https://bugzilla.redhat.com/2177177

commit 2e20f50ff84903964bcfca10ecdab0fa08cd6a4c
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Nov 2 01:54:13 2022 +0200

    selftests/bpf: Tests for enum fwd resolved as full enum64

    A set of test cases to verify enum fwd resolution logic:
    - verify that enum fwd can be resolved as full enum64;
    - verify that enum64 fwd can be resolved as full enum;
    - verify that enum size is considered when enums are compared for
      equivalence.

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20221101235413.1824260-2-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:02 +02:00
Artem Savkov 53efc3dfab selftests/bpf: Add reproducer for decl_tag in func_proto return type
Bugzilla: https://bugzilla.redhat.com/2166911

commit 35cc9d622e8cd45029a1656ab2c6817538bc4180
Author: Stanislav Fomichev <sdf@google.com>
Date:   Fri Oct 14 17:24:43 2022 -0700

    selftests/bpf: Add reproducer for decl_tag in func_proto return type
    
    It should trigger a WARN_ON_ONCE in btf_type_id_size.
    
         btf_func_proto_check kernel/bpf/btf.c:4447 [inline]
         btf_check_all_types kernel/bpf/btf.c:4723 [inline]
         btf_parse_type_sec kernel/bpf/btf.c:4752 [inline]
         btf_parse kernel/bpf/btf.c:5026 [inline]
         btf_new_fd+0x1926/0x1e70 kernel/bpf/btf.c:6892
         bpf_btf_load kernel/bpf/syscall.c:4324 [inline]
         __sys_bpf+0xb7d/0x4cf0 kernel/bpf/syscall.c:5010
         __do_sys_bpf kernel/bpf/syscall.c:5069 [inline]
         __se_sys_bpf kernel/bpf/syscall.c:5067 [inline]
         __x64_sys_bpf+0x75/0xb0 kernel/bpf/syscall.c:5067
         do_syscall_x64 arch/x86/entry/common.c:50 [inline]
         do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80
         entry_SYSCALL_64_after_hwframe+0x63/0xcd
    
    Cc: Yonghong Song <yhs@fb.com>
    Cc: Martin KaFai Lau <martin.lau@kernel.org>
    Signed-off-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20221015002444.2680969-1-sdf@google.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:21 +01:00
Artem Savkov fbd9cfcd5b selftests/bpf: Store BPF object files with .bpf.o extension
Bugzilla: https://bugzilla.redhat.com/2166911

commit afef88e65554c3e8691513b8350d6445e292560e
Author: Daniel Müller <deso@posteo.net>
Date:   Thu Sep 1 22:22:53 2022 +0000

    selftests/bpf: Store BPF object files with .bpf.o extension
    
    BPF object files are, in a way, the final artifact produced as part of
    the ahead-of-time compilation process. That makes them somewhat special
    compared to "regular" object files, which are a intermediate build
    artifacts that can typically be removed safely. As such, it can make
    sense to name them differently to make it easier to spot this difference
    at a glance.
    
    Among others, libbpf-bootstrap [0] has established the extension .bpf.o
    for BPF object files. It seems reasonable to follow this example and
    establish the same denomination for selftest build artifacts. To that
    end, this change adjusts the corresponding part of the build system and
    the test programs loading BPF object files to work with .bpf.o files.
    
      [0] https://github.com/libbpf/libbpf-bootstrap
    
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20220901222253.1199242-1-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:06 +01:00
Artem Savkov 8a540b8845 selftests/bpf: fix a test for snprintf() overflow
Bugzilla: https://bugzilla.redhat.com/2137876

commit c5d22f4cfe8dfb93f1db0a1e7e2e7ebc41395d98
Author: Dan Carpenter <dan.carpenter@oracle.com>
Date:   Tue Jul 19 12:50:32 2022 +0300

    selftests/bpf: fix a test for snprintf() overflow
    
    The snprintf() function returns the number of bytes which *would*
    have been copied if there were space.  In other words, it can be
    > sizeof(pin_path).
    
    Fixes: c0fa1b6c3e ("bpf: btf: Add BTF tests")
    Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/r/YtZ+aD/tZMkgOUw+@kili
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:41 +01:00
Artem Savkov 67d129f42b selftests/bpf: remove last tests with legacy BPF map definitions
Bugzilla: https://bugzilla.redhat.com/2137876

commit 31e42721976b9c445477038f8a4006150cd27a60
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jun 27 14:15:25 2022 -0700

    selftests/bpf: remove last tests with legacy BPF map definitions
    
    Libbpf 1.0 stops support legacy-style BPF map definitions. Selftests has
    been migrated away from using legacy BPF map definitions except for two
    selftests, to make sure that legacy functionality still worked in
    pre-1.0 libbpf. Now it's time to let those tests go as libbpf 1.0 is
    imminent.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220627211527.2245459-14-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:33 +01:00
Artem Savkov ef155df614 selftests/bpf: allow BTF specs and func infos in test_verifier tests
Bugzilla: https://bugzilla.redhat.com/2137876

commit 7a42008ca5c700819e4b3003025e5e1695fd1f86
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Tue Jun 21 02:53:41 2022 +0300

    selftests/bpf: allow BTF specs and func infos in test_verifier tests
    
    The BTF and func_info specification for test_verifier tests follows
    the same notation as in prog_tests/btf.c tests. E.g.:
    
      ...
      .func_info = { { 0, 6 }, { 8, 7 } },
      .func_info_cnt = 2,
      .btf_strings = "\0int\0",
      .btf_types = {
        BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
        BTF_PTR_ENC(1),
      },
      ...
    
    The BTF specification is loaded only when specified.
    
    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/r/20220620235344.569325-3-eddyz87@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:30 +01:00
Yauheni Kaliuta c2c59d2646 selftests/bpf: Test BTF_KIND_ENUM64 for deduplication
Bugzilla: http://bugzilla.redhat.com/2120968

commit adc26d134ef3454c3d8ffb75ee6ca20c169b23d0
Author: Yonghong Song <yhs@fb.com>
Date:   Mon Jun 6 23:27:13 2022 -0700

    selftests/bpf: Test BTF_KIND_ENUM64 for deduplication
    
    Add a few unit tests for BTF_KIND_ENUM64 deduplication.
    
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220607062713.3725409-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:12 +02:00
Yauheni Kaliuta 3b065de15f selftests/bpf: Add BTF_KIND_ENUM64 unit tests
Bugzilla: http://bugzilla.redhat.com/2120968

commit 3b5325186dfad5ad2b2d8f7e8a79662de1b2749d
Author: Yonghong Song <yhs@fb.com>
Date:   Mon Jun 6 23:27:08 2022 -0700

    selftests/bpf: Add BTF_KIND_ENUM64 unit tests
    
    Add unit tests for basic BTF_KIND_ENUM64 encoding.
    
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220607062708.3724845-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:12 +02:00
Yauheni Kaliuta dddb8e2914 selftests/bpf: Fix selftests failure
Bugzilla: http://bugzilla.redhat.com/2120968

commit d932815a4394b6e8e861f75600666db40c706b8b
Author: Yonghong Song <yhs@fb.com>
Date:   Mon Jun 6 23:26:57 2022 -0700

    selftests/bpf: Fix selftests failure
    
    The kflag is supported now for BTF_KIND_ENUM.
    So remove the test which tests verifier failure
    due to existence of kflag.
    
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220607062657.3723737-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:12 +02:00
Yauheni Kaliuta 579b55d712 selftests/bpf: Add tests for type tag order validation
Bugzilla: https://bugzilla.redhat.com/2120968

commit 24fe983abe01c53e1a9354fe21fab92579fcda6d
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Tue Apr 19 22:16:08 2022 +0530

    selftests/bpf: Add tests for type tag order validation
    
    Add a few test cases that ensure we catch cases of badly ordered type
    tags in modifier chains.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20220419164608.1990559-3-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:52:10 +02:00
Yauheni Kaliuta c36a904150 selftests/bpf: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK
Bugzilla: https://bugzilla.redhat.com/2120968

commit b858ba8c52b64c038de156c455a39a89bfd214e8
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Sat Apr 9 12:59:56 2022 +0000

    selftests/bpf: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK
    
    We have switched to memcg-based memory accouting and thus the rlimit is
    not needed any more. LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK was introduced in
    libbpf for backward compatibility, so we can use it instead now. After
    this change, the header tools/testing/selftests/bpf/bpf_rlimit.h can be
    removed.
    
    This patch also removes the useless header sys/resource.h from many files
    in tools/testing/selftests/bpf/.
    
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220409125958.92629-3-laoar.shao@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:58 +02:00
Jerome Marchand 2e1d3ad44e selftests/bpf: Fix GCC11 compiler warnings in -O2 mode
Bugzilla: https://bugzilla.redhat.com/2120966

commit d3b0b80064e0416850f818184b8f7bba9fdf8c40
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Feb 11 11:09:27 2022 -0800

    selftests/bpf: Fix GCC11 compiler warnings in -O2 mode

    When compiling selftests in -O2 mode with GCC1, we get three new
    compilations warnings about potentially uninitialized variables.

    Compiler is wrong 2 out of 3 times, but this patch makes GCC11 happy
    anyways, as it doesn't cost us anything and makes optimized selftests
    build less annoying.

    The amazing one is tc_redirect case of token that is malloc()'ed before
    ASSERT_OK_PTR() check is done on it. Seems like GCC pessimistically
    assumes that libbpf_get_error() will dereference the contents of the
    pointer (no it won't), so the only way I found to shut GCC up was to do
    zero-initializaing calloc(). This one was new to me.

    For linfo case, GCC didn't realize that linfo_size will be initialized
    by the function that is returning linfo_size as out parameter.

    core_reloc.c case was a real bug, we can goto cleanup before initializing
    obj. But we don't need to do any clean up, so just continue iteration
    intstead.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220211190927.1434329-1-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 5c4d6cd0c4 selftests/bpf: Fix strict mode calculation
Bugzilla: https://bugzilla.redhat.com/2120966

commit 2b9e2eadc9c899af3b508503677afecc85e44766
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Mon Feb 7 09:50:52 2022 -0500

    selftests/bpf: Fix strict mode calculation

    "(__LIBBPF_STRICT_LAST - 1) & ~LIBBPF_STRICT_MAP_DEFINITIONS" is wrong
    as it is equal to 0 (LIBBPF_STRICT_NONE). Let's use
    "LIBBPF_STRICT_ALL & ~LIBBPF_STRICT_MAP_DEFINITIONS" now that the
    previous commit makes it possible in libbpf.

    Fixes: 93b8952d223a ("libbpf: deprecate legacy BPF map definitions")
    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220207145052.124421-4-mauricio@kinvolk.io

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:47 +02:00
Jerome Marchand 2c46d13dc3 selftests/bpf: Add a selftest for invalid func btf with btf decl_tag
Bugzilla: https://bugzilla.redhat.com/2120966

commit cf1a4cbce63b766d3b7aa5eb57a56d9a2c45ca6c
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Feb 3 11:17:32 2022 -0800

    selftests/bpf: Add a selftest for invalid func btf with btf decl_tag

    Added a selftest similar to [1] which exposed a kernel bug.
    Without the fix in the previous patch, the similar kasan error will appear.

      [1] https://lore.kernel.org/bpf/0000000000009b6eaa05d71a8c06@google.com/

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20220203191732.742285-1-yhs@fb.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:47 +02:00
Jerome Marchand f55ca6e379 libbpf: deprecate legacy BPF map definitions
Bugzilla: https://bugzilla.redhat.com/2120966

commit 93b8952d223af03c51fba0c6258173d2ffbd2cb7
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Jan 19 22:05:28 2022 -0800

    libbpf: deprecate legacy BPF map definitions

    Enact deprecation of legacy BPF map definition in SEC("maps") ([0]). For
    the definitions themselves introduce LIBBPF_STRICT_MAP_DEFINITIONS flag
    for libbpf strict mode. If it is set, error out on any struct
    bpf_map_def-based map definition. If not set, libbpf will print out
    a warning for each legacy BPF map to raise awareness that it goes away.

    For any use of BPF_ANNOTATE_KV_PAIR() macro providing a legacy way to
    associate BTF key/value type information with legacy BPF map definition,
    warn through libbpf's pr_warn() error message (but don't fail BPF object
    open).

    BPF-side struct bpf_map_def is marked as deprecated. User-space struct
    bpf_map_def has to be used internally in libbpf, so it is left
    untouched. It should be enough for bpf_map__def() to be marked
    deprecated to raise awareness that it goes away.

    bpftool is an interesting case that utilizes libbpf to open BPF ELF
    object to generate skeleton. As such, even though bpftool itself uses
    full on strict libbpf mode (LIBBPF_STRICT_ALL), it has to relax it a bit
    for BPF map definition handling to minimize unnecessary disruptions. So
    opt-out of LIBBPF_STRICT_MAP_DEFINITIONS for bpftool. User's code that
    will later use generated skeleton will make its own decision whether to
    enforce LIBBPF_STRICT_MAP_DEFINITIONS or not.

    There are few tests in selftests/bpf that are consciously using legacy
    BPF map definitions to test libbpf functionality. For those, temporary
    opt out of LIBBPF_STRICT_MAP_DEFINITIONS mode for the duration of those
    tests.

      [0] Closes: https://github.com/libbpf/libbpf/issues/272

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20220120060529.1890907-4-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:41 +02:00
Artem Savkov 69ec6d9f19 selftests/bpf: Remove explicit setrlimit(RLIMIT_MEMLOCK) in main selftests
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit c164b8b40422ef5c643d08bbc63280e1e1610573
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Dec 14 11:59:04 2021 -0800

    selftests/bpf: Remove explicit setrlimit(RLIMIT_MEMLOCK) in main selftests

    As libbpf now is able to automatically take care of RLIMIT_MEMLOCK
    increase (or skip it altogether on recent enough kernels), remove
    explicit setrlimit() invocations in bench, test_maps, test_verifier, and
    test_progs.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211214195904.1785155-3-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:48 +02:00
Artem Savkov 6632be4a3a selftests/bpf: Replace all uses of bpf_load_btf() with bpf_btf_load()
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit dc94121b5ca17adaaabb7959c10d9c6ea504f7b1
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Dec 9 11:38:37 2021 -0800

    selftests/bpf: Replace all uses of bpf_load_btf() with bpf_btf_load()

    Switch all selftests uses of to-be-deprecated bpf_load_btf() with
    equivalent bpf_btf_load() calls.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211209193840.1248570-10-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:46 +02:00
Artem Savkov 91c8c1ff60 selftests/bpf: Migrate selftests to bpf_map_create()
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 2fe256a429cb6c0b0064563af4158470143a363c
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 24 11:32:33 2021 -0800

    selftests/bpf: Migrate selftests to bpf_map_create()

    Conversion is straightforward for most cases. In few cases tests are
    using mutable map_flags and attribute structs, but bpf_map_create_opts
    can be used in the similar fashion, so there were no problems. Just lots
    of repetitive conversions.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211124193233.3115996-5-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:40 +02:00
Artem Savkov 67b2d846cb selftests/bpf: Add a dedup selftest with equivalent structure types
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 4746158305e98c91c479539d53ef9bf8c520dd66
Author: Yonghong Song <yhs@fb.com>
Date:   Mon Nov 15 08:39:43 2021 -0800

    selftests/bpf: Add a dedup selftest with equivalent structure types

    Without previous libbpf patch, the following error will occur:

      $ ./test_progs -t btf
      ...
      do_test_dedup:FAIL:check btf_dedup failed errno:-22#13/205 btf/dedup: btf_type_tag #5, struct:FAIL

    And the previous libbpf patch fixed the issue.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211115163943.3922547-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:37 +02:00
Artem Savkov 10e5ad04f5 selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 846f4826d18e660ab668eb26e83c6adf0ceb24d2
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Nov 11 17:26:35 2021 -0800

    selftests/bpf: Test BTF_KIND_DECL_TAG for deduplication

    Add BTF_KIND_TYPE_TAG duplication unit tests.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112012635.1506853-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:36 +02:00
Artem Savkov 53b994df00 selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 6aa5dabc9d0ef722905e4ca4f9751d70cf3ec8a4
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Nov 11 17:26:30 2021 -0800

    selftests/bpf: Add BTF_KIND_TYPE_TAG unit tests

    Add BTF_KIND_TYPE_TAG unit tests.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211112012630.1506095-1-yhs@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:36 +02:00
Artem Savkov 0e4a705962 libbpf: Turn btf_dedup_opts into OPTS-based struct
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 957d350a8b94133d114a9b1ac3e79f1f77100681
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 10 21:36:18 2021 -0800

    libbpf: Turn btf_dedup_opts into OPTS-based struct

    btf__dedup() and struct btf_dedup_opts were added before we figured out
    OPTS mechanism. As such, btf_dedup_opts is non-extensible without
    breaking an ABI and potentially crashing user application.

    Unfortunately, btf__dedup() and btf_dedup_opts are short and succinct
    names that would be great to preserve and use going forward. So we use
    ___libbpf_override() macro approach, used previously for bpf_prog_load()
    API, to define a new btf__dedup() variant that accepts only struct btf *
    and struct btf_dedup_opts * arguments, and rename the old btf__dedup()
    implementation into btf__dedup_deprecated(). This keeps both source and
    binary compatibility with old and new applications.

    The biggest problem was struct btf_dedup_opts, which wasn't OPTS-based,
    and as such doesn't have `size_t sz;` as a first field. But btf__dedup()
    is a pretty rarely used API and I believe that the only currently known
    users (besides selftests) are libbpf's own bpf_linker and pahole.
    Neither use case actually uses options and just passes NULL. So instead
    of doing extra hacks, just rewrite struct btf_dedup_opts into OPTS-based
    one, move btf_ext argument into those opts (only bpf_linker needs to
    dedup btf_ext, so it's not a typical thing to specify), and drop never
    used `dont_resolve_fwds` option (it was never used anywhere, AFAIK, it
    makes BTF dedup much less useful and efficient).

    Just in case, for old implementation, btf__dedup_deprecated(), detect
    non-NULL options and error out with helpful message, to help users
    migrate, if there are any user playing with btf__dedup().

    The last remaining piece is dedup_table_size, which is another
    anachronism from very early days of BTF dedup. Since then it has been
    reduced to the only valid value, 1, to request forced hash collisions.
    This is only used during testing. So instead introduce a bool flag to
    force collisions explicitly.

    This patch also adapts selftests to new btf__dedup() and btf_dedup_opts
    use to avoid selftests breakage.

      [0] Closes: https://github.com/libbpf/libbpf/issues/281

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211111053624.190580-4-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:35 +02:00
Artem Savkov 01856f8ff7 selftests/bpf: Free inner strings index in btf selftest
Bugzilla: https://bugzilla.redhat.com/2069046

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 5309b516bcc6f76dda0e44a7a1824324277093d6
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Sun Nov 7 08:55:17 2021 -0800

    selftests/bpf: Free inner strings index in btf selftest

    Inner array of allocated strings wasn't freed on success. Now it's
    always freed.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Reviewed-by: Hengqi Chen <hengqi.chen@gmail.com>
    Link: https://lore.kernel.org/bpf/20211107165521.9240-6-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:34 +02:00
Yauheni Kaliuta 582edb4cd0 selftests/bpf: Test deduplication for BTF_KIND_DECL_TAG typedef
Bugzilla: http://bugzilla.redhat.com/2069045

commit 557c8c480401457d885bf7a82221dcc877692aa7
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Oct 21 12:56:38 2021 -0700

    selftests/bpf: Test deduplication for BTF_KIND_DECL_TAG typedef
    
    Add unit tests for deduplication of BTF_KIND_DECL_TAG to typedef types.
    Also changed a few comments from "tag" to "decl_tag" to match
    BTF_KIND_DECL_TAG enum value name.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211021195638.4019770-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:45 +03:00
Yauheni Kaliuta cc1ec0f99c selftests/bpf: Add BTF_KIND_DECL_TAG typedef unit tests
Bugzilla: http://bugzilla.redhat.com/2069045

commit 9d19a12b02bf009fefc3620234b4297e4bd7c5d5
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Oct 21 12:56:33 2021 -0700

    selftests/bpf: Add BTF_KIND_DECL_TAG typedef unit tests
    
    Test good and bad variants of typedef BTF_KIND_DECL_TAG encoding.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211021195633.4019472-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:45 +03:00
Yauheni Kaliuta 0946d363ca selftests/bpf: Switch to new btf__type_cnt/btf__raw_data APIs
Bugzilla: http://bugzilla.redhat.com/2069045

commit 487ef148cf17730442444c07a4c56f16578ec73e
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Fri Oct 22 21:06:23 2021 +0800

    selftests/bpf: Switch to new btf__type_cnt/btf__raw_data APIs
    
    Replace the calls to btf__get_nr_types/btf__get_raw_data in
    selftests with new APIs btf__type_cnt/btf__raw_data. The old
    APIs will be deprecated in libbpf v0.7+.
    
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211022130623.1548429-6-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:45 +03:00
Yauheni Kaliuta fdbdd94ffe bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
Bugzilla: http://bugzilla.redhat.com/2069045

commit 223f903e9c832699f4e5f422281a60756c1c6cfe
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Oct 12 09:48:38 2021 -0700

    bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG
    
    Patch set [1] introduced BTF_KIND_TAG to allow tagging
    declarations for struct/union, struct/union field, var, func
    and func arguments and these tags will be encoded into
    dwarf. They are also encoded to btf by llvm for the bpf target.
    
    After BTF_KIND_TAG is introduced, we intended to use it
    for kernel __user attributes. But kernel __user is actually
    a type attribute. Upstream and internal discussion showed
    it is not a good idea to mix declaration attribute and
    type attribute. So we proposed to introduce btf_type_tag
    as a type attribute and existing btf_tag renamed to
    btf_decl_tag ([2]).
    
    This patch renamed BTF_KIND_TAG to BTF_KIND_DECL_TAG and some
    other declarations with *_tag to *_decl_tag to make it clear
    the tag is for declaration. In the future, BTF_KIND_TYPE_TAG
    might be introduced per [3].
    
     [1] https://lore.kernel.org/bpf/20210914223004.244411-1-yhs@fb.com/
     [2] https://reviews.llvm.org/D111588
     [3] https://reviews.llvm.org/D111199
    
    Fixes: b5ea834dde6b ("bpf: Support for new btf kind BTF_KIND_TAG")
    Fixes: 5b84bd10363e ("libbpf: Add support for BTF_KIND_TAG")
    Fixes: 5c07f2fec003 ("bpftool: Add support for BTF_KIND_TAG")
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20211012164838.3345699-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:42 +03:00
Yauheni Kaliuta aa580f2f50 selftests/bpf: Switch to new bpf_object__next_{map,program} APIs
Bugzilla: http://bugzilla.redhat.com/2069045

commit 6f2b219b62a4376ca2da15c503de79d0650c8155
Author: Hengqi Chen <hengqi.chen@gmail.com>
Date:   Mon Oct 4 00:58:44 2021 +0800

    selftests/bpf: Switch to new bpf_object__next_{map,program} APIs
    
    Replace deprecated bpf_{map,program}__next APIs with newly added
    bpf_object__next_{map,program} APIs, so that no compilation warnings
    emit.
    
    Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20211003165844.4054931-3-hengqi.chen@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:39 +03:00
Yauheni Kaliuta dbf59d472e selftests/bpf: Test BTF_KIND_TAG for deduplication
Bugzilla: http://bugzilla.redhat.com/2069045

commit ad526474aec1d9abb9838e3fb1330bb991715c22
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:52 2021 -0700

    selftests/bpf: Test BTF_KIND_TAG for deduplication
    
    Add unit tests for BTF_KIND_TAG deduplication for
      - struct and struct member
      - variable
      - func and func argument
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223052.248535-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:13 +03:00
Yauheni Kaliuta 6a2a78a0d3 selftests/bpf: Add BTF_KIND_TAG unit tests
Bugzilla: http://bugzilla.redhat.com/2069045

commit 35baba7a832fa466b4aa2e0d00473d795cec8f20
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:47 2021 -0700

    selftests/bpf: Add BTF_KIND_TAG unit tests
    
    Test good and bad variants of BTF_KIND_TAG encoding.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223047.248223-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:13 +03:00
Yauheni Kaliuta 8c08414d6c selftests/bpf: Change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format
Bugzilla: http://bugzilla.redhat.com/2069045

commit 3df3bd68d4811bccc74adc04d4d84512957a1a07
Author: Yonghong Song <yhs@fb.com>
Date:   Tue Sep 14 15:30:41 2021 -0700

    selftests/bpf: Change NAME_NTH/IS_NAME_NTH for BTF_KIND_TAG format
    
    BTF_KIND_TAG ELF format has a component_idx which might have value -1.
    test_btf may confuse it with common_type.name as NAME_NTH checkes
    high 16bit to be 0xffff. Change NAME_NTH high 16bit check to be
    0xfffe so it won't confuse with component_idx.
    
    Signed-off-by: Yonghong Song <yhs@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210914223041.248009-1-yhs@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:16:12 +03:00
Jerome Marchand d12c67c42c tools: Replace btf__get_from_id() with btf__load_from_kernel_by_id()
Bugzilla: http://bugzilla.redhat.com/2041365

commit 86f4b7f2578f69284fa782be54e700c42c757897
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Jul 29 17:20:25 2021 +0100

    tools: Replace btf__get_from_id() with btf__load_from_kernel_by_id()

    Replace the calls to function btf__get_from_id(), which we plan to
    deprecate before the library reaches v1.0, with calls to
    btf__load_from_kernel_by_id() in tools/ (bpftool, perf, selftests).
    Update the surrounding code accordingly (instead of passing a pointer to
    the btf struct, get it as a return value from the function).

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20210729162028.29512-6-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:36 +02:00
Jerome Marchand 8dd396adf7 tools: Free BTF objects at various locations
Bugzilla: http://bugzilla.redhat.com/2041365

commit 369e955b3d1c12f6ec2e51a95911bb80ada55d79
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Thu Jul 29 17:20:24 2021 +0100

    tools: Free BTF objects at various locations

    Make sure to call btf__free() (and not simply free(), which does not
    free all pointers stored in the struct) on pointers to struct btf
    objects retrieved at various locations.

    These were found while updating the calls to btf__get_from_id().

    Fixes: 999d82cbc0 ("tools/bpf: enhance test_btf file testing to test func info")
    Fixes: 254471e57a ("tools/bpf: bpftool: add support for func types")
    Fixes: 7b612e291a ("perf tools: Synthesize PERF_RECORD_* for loaded BPF programs")
    Fixes: d56354dc49 ("perf tools: Save bpf_prog_info and BTF of new BPF programs")
    Fixes: 47c09d6a9f ("bpftool: Introduce "prog profile" command")
    Fixes: fa853c4b83 ("perf stat: Enable counting events for BPF programs")
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210729162028.29512-5-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:36 +02:00
Andrii Nakryiko bad2e478af selftests/bpf: Turn on libbpf 1.0 mode and fix all IS_ERR checks
Turn ony libbpf 1.0 mode. Fix all the explicit IS_ERR checks that now will be
broken because libbpf returns NULL on error (and sets errno). Fix
ASSERT_OK_PTR and ASSERT_ERR_PTR to work for both old mode and new modes and
use them throughout selftests. This is trivial to do by using
libbpf_get_error() API that all libbpf users are supposed to use, instead of
IS_ERR checks.

A bunch of checks also did explicit -1 comparison for various fd-returning
APIs. Such checks are replaced with >= 0 or < 0 cases.

There were also few misuses of bpf_object__find_map_by_name() in test_maps.
Those are fixed in this patch as well.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210525035935.1461796-3-andrii@kernel.org
2021-05-25 17:32:35 -07:00
Ilya Leoshkevich 7999cf7df8 selftests/bpf: Add BTF_KIND_FLOAT to the existing deduplication tests
Check that floats don't interfere with struct deduplication, that they
are not merged with another kinds and that floats of different sizes are
not merged with each other.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210226202256.116518-9-iii@linux.ibm.com
2021-03-04 17:58:16 -08:00
Ilya Leoshkevich 7e72aad3a1 selftest/bpf: Add BTF_KIND_FLOAT tests
Test the good variants as well as the potential malformed ones.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210226202256.116518-8-iii@linux.ibm.com
2021-03-04 17:58:16 -08:00
Ilya Leoshkevich eea154a852 selftests/bpf: Use the 25th bit in the "invalid BTF_INFO" test
The bit being checked by this test is no longer reserved after
introducing BTF_KIND_FLOAT, so use the next one instead.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210226202256.116518-6-iii@linux.ibm.com
2021-03-04 17:58:15 -08:00
Junlin Yang 443edcefb8 selftest/bpf: Fix typo
Change 'exeeds' to 'exceeds'.

Signed-off-by: Junlin Yang <yangjunlin@yulong.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210121122309.1501-1-angkery@163.com
2021-01-21 15:58:06 -08:00
Yonghong Song 13ca51d5eb bpf: Permit size-0 datasec
llvm patch https://reviews.llvm.org/D84002 permitted
to emit empty rodata datasec if the elf .rodata section
contains read-only data from local variables. These
local variables will be not emitted as BTF_KIND_VARs
since llvm converted these local variables as
static variables with private linkage without debuginfo
types. Such an empty rodata datasec will make
skeleton code generation easy since for skeleton
a rodata struct will be generated if there is a
.rodata elf section. The existence of a rodata
btf datasec is also consistent with the existence
of a rodata map created by libbpf.

The btf with such an empty rodata datasec will fail
in the kernel though as kernel will reject a datasec
with zero vlen and zero size. For example, for the below code,
    int sys_enter(void *ctx)
    {
       int fmt[6] = {1, 2, 3, 4, 5, 6};
       int dst[6];

       bpf_probe_read(dst, sizeof(dst), fmt);
       return 0;
    }
We got the below btf (bpftool btf dump ./test.o):
    [1] PTR '(anon)' type_id=0
    [2] FUNC_PROTO '(anon)' ret_type_id=3 vlen=1
            'ctx' type_id=1
    [3] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
    [4] FUNC 'sys_enter' type_id=2 linkage=global
    [5] INT 'char' size=1 bits_offset=0 nr_bits=8 encoding=SIGNED
    [6] ARRAY '(anon)' type_id=5 index_type_id=7 nr_elems=4
    [7] INT '__ARRAY_SIZE_TYPE__' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [8] VAR '_license' type_id=6, linkage=global-alloc
    [9] DATASEC '.rodata' size=0 vlen=0
    [10] DATASEC 'license' size=0 vlen=1
            type_id=8 offset=0 size=4
When loading the ./test.o to the kernel with bpftool,
we see the following error:
    libbpf: Error loading BTF: Invalid argument(22)
    libbpf: magic: 0xeb9f
    ...
    [6] ARRAY (anon) type_id=5 index_type_id=7 nr_elems=4
    [7] INT __ARRAY_SIZE_TYPE__ size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [8] VAR _license type_id=6 linkage=1
    [9] DATASEC .rodata size=24 vlen=0 vlen == 0
    libbpf: Error loading .BTF into kernel: -22. BTF is optional, ignoring.

Basically, libbpf changed .rodata datasec size to 24 since elf .rodata
section size is 24. The kernel then rejected the BTF since vlen = 0.
Note that the above kernel verifier failure can be worked around with
changing local variable "fmt" to a static or global, optionally const, variable.

This patch permits a datasec with vlen = 0 in kernel.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210119153519.3901963-1-yhs@fb.com
2021-01-20 14:14:09 -08:00