This is an archive of the discontinued LLVM Phabricator instance.

[BPF] fix a CO-RE bitfield relocation error with >8 record alignment
ClosedPublic

Authored by yonghong-song on Mar 16 2022, 9:37 AM.

Details

Summary

Jussi Maki reported a fatal error like below for a bitfield
CO-RE relocation:

fatal error: error in backend: Unsupported field expression for
llvm.bpf.preserve.field.info, requiring too big alignment

The failure is related to kernel struct thread_struct. The following
is a simplied example.

Suppose we have below structure:

struct t2 {
  int a[8];
} __attribute__((aligned(64))) __attribute__((preserve_access_index));
struct t1 {
  int f1:1;
  int f2:2;
  struct t2 f3;
} __attribute__((preserve_access_index));

Note that struct t2 has aligned 64, which is used sometimes in the
kernel to enforce cache line alignment.

The above struct will be encoded into BTF and the following is what
C code looks like and the struct will appear in the file like vmlinux.h.

struct t2 {
      int a[8];
      long: 64;
      long: 64;
      long: 64;
      long: 64;
} __attribute__((preserve_access_index));
struct t1 {
      int f1: 1;
      int f2: 2;
      long: 61;
      long: 64;
      long: 64;
      long: 64;
      long: 64;
      long: 64;
      long: 64;
      long: 64;
      struct t2 f3;
} __attribute__((preserve_access_index));

Note that after

origin_source -> BTF -> new_source

transition, the new source has the same memory layout as the old one
but the alignment interpretation inside the compiler could be different.
The bpf program will use the later explicitly padded structure as in
vmlinux.h.

In the above case, the compiler internal ABI alignment for new struct t1
is 16 while it is 4 for old struct t1. I didn't do a thorough investigation
why the ABI alignment is 16 and I suspect it is related to anonymous padding
in the above.

Current BPF bitfield CO-RE handling requires alignment <= 8 so proper
bitfield operatin can be performed. Therefore, alignment 16 will cause
a compiler fatal error.

To fix the ABI alignment >=16, let us check whether the bitfield
can be held within a 8-byte-aligned range. If this is the case,
we can use alignment 8. Otherwise, a fatal error will be reported.

Diff Detail

Event Timeline

yonghong-song created this revision.Mar 16 2022, 9:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2022, 9:37 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
yonghong-song requested review of this revision.Mar 16 2022, 9:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 16 2022, 9:37 AM
ast accepted this revision.Mar 16 2022, 10:59 AM
This revision is now accepted and ready to land.Mar 16 2022, 10:59 AM
This revision was landed with ongoing or failed builds.Mar 16 2022, 12:22 PM
This revision was automatically updated to reflect the committed changes.