Skip to content

libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

Yonghong Song bpf 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest3 patches 2 repliesIn review
  2. v32 patches 0 repliesSuperseded
  3. v24 patches 2 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v12 patches1 replySuperseded

Changes in v4 from the cover letter

- v3: https://lore.kernel.org/bpf/[email protected]/
- Take a 32-bit target enum value as signed if the target enum has the kflag 0 but the local enum is signed, for the BTF of kernels without enum signedness (patch 1).
- Test it with a target BTF whose enums have the kflag cleared
(patch 3).

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3libbpf: Fix CO-RE relocations of unsigned 32-bit enum valuesinclude/linux/btf.h tools/lib/bpf/relo_core.c+60 -42 replies
2/3selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum valuestools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/core_reloc_types.h tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c+31 -00 replies
3/3selftests/bpf: Test CO-RE relocations of signed enum values without kflagtools/testing/selftests/bpf/prog_tests/core_reloc.c tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c tools/testing/selftests/bpf/progs/core_reloc_types.h +1 more+121 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [PATCH bpf-next v4 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values

For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value of a 32-bit enumerator even if the enum is unsigned. A value in [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags, then doesn't match the zero-extended value clang emits, and the whole object is refused.

Patch 1 extends the value as given by the kflag of the enum, which is the form clang emits. The kernel builds the same relo_core.c, so it gets the fix too.

GCC emits every 32-bit enum value sign-extended, since its __builtin_preserve_enum_value() returns int. So with the change above alone, GCC objects that load today would be refused. Patch 1 therefore also accepts the sign-extended form of an unsigned value, and patches the target value in the same form.

The BTF of a kernel before v6.0, or generated by pahole before v1.24, has the kflag 0 on every enum, signed or not, so there the kflag 0 doesn't mean unsigned. If the local enum is signed, patch 1 takes such a target value as signed too, so that, e.g., -1 doesn't become 0xffffffff.

Patch 2 tests the unsigned enum values, with clang and GCC. Patch 3 tests the signed ones against a target BTF without the kflag.

Changelog:
v3 -> v4:
- v3: https://lore.kernel.org/bpf/[email protected]/
- Take a 32-bit target enum value as signed if the target enum has the kflag 0 but the local enum is signed, for the BTF of kernels without enum signedness (patch 1).
- Test it with a target BTF whose enums have the kflag cleared
(patch 3).
v2 -> v3:
- v2: https://lore.kernel.org/bpf/[email protected]/
- Fold patch 3 into patch 1 and patch 4 into patch 2 to avoid test failures when bisecting.
v1 -> v2:
- v1: https://lore.kernel.org/bpf/[email protected]/
- Accept the sign-extended enum values emitted by GCC (patch 3), and make the new test work with GCC (patch 4).

Yonghong Song (3):
libbpf: Fix CO-RE relocations of unsigned 32-bit enum values selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values selftests/bpf: Test CO-RE relocations of signed enum values without
kflag

 include/linux/btf.h                           |  5 +
 tools/lib/bpf/relo_core.c                     | 59 +++++++++++-
 .../selftests/bpf/prog_tests/core_reloc.c     | 92 ++++++++++++++++++-
 .../btf__core_reloc_enumval___no_kflag.c      |  3 +
 .../selftests/bpf/progs/core_reloc_types.h    | 24 +++++
 .../bpf/progs/test_core_reloc_enumval.c       | 35 +++++++
 6 files changed, 212 insertions(+), 6 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf__core_reloc_enumval___no_kflag.c
Signature · 1 line
2.53.1-Meta