cilium/ebpf

Support for CORE type matches relocation

alban opened this issue · 0 comments

Describe the bug

Type matches relocation are currently not supported.

bpf_core_type_matches() (using BPF_TYPE_MATCHES) is defined in libbpf:
https://github.com/libbpf/libbpf/blob/v1.3.0/src/bpf_core_read.h#L194

Type match relocations was added in llvm/clang 15:
llvm/llvm-project@d129ac2

Example of usage:
https://github.com/torvalds/linux/blob/v6.6/tools/perf/util/bpf_skel/lock_contention.bpf.c#L215

Relocations of type BPF_CORE_TYPE_MATCHES are explained in:
https://docs.kernel.org/bpf/llvm_reloc.html

cilium/ebpf supports the following relocations:

enum bpf_core_relo_kind {
...
       BPF_CORE_ENUMVAL_VALUE     = 11, /* enum value integer value */
       BPF_CORE_TYPE_MATCHES      = 12, /* type match in target kernel */
};

It stops at BPF_CORE_ENUMVAL_VALUE / reloEnumvalValue:
https://github.com/cilium/ebpf/blob/v0.13.0/btf/core.go#L121

cc @eiffel-fl @mauriciovasquezbernal

How to reproduce

Code inspired from:

// https://github.com/torvalds/linux/commit/a54895fa057c67700270777f7661d8d3c7fda88a
// -       TP_PROTO(struct request_queue *q, struct request *rq),
// +       TP_PROTO(struct request *rq),
typedef void (*btf_trace_block_rq_insert___new)(void *, struct request *);
typedef void (*btf_trace_block_rq_insert___old)(void *, struct request_queue *q, struct request *);
typedef void (*btf_trace_block_rq_issue___new)(void *, struct request *);
typedef void (*btf_trace_block_rq_issue___old)(void *, struct request_queue *q, struct request *);

static void (*bpf_unreachable)(void) = (void *) 0xbad2310;

SEC("raw_tp/block_rq_insert")
int ig_profio_ins(u64 *ctx)
{
        if (bpf_core_type_matches(btf_trace_block_rq_insert___new)) {
                // After commit a54895fa (v5.11-rc1)
                return trace_rq_start((void *)ctx[0], true);
        } else if (bpf_core_type_matches(btf_trace_block_rq_insert___old)) {
                // Before commit a54895fa (v5.11-rc1)
                return trace_rq_start((void *)ctx[1], false);
        } else {
                // Couldn't detect block/block_rq_insert tracepoint
                bpf_unreachable();
                return 0;
        }
}

cilium/ebpf gives the following error:

installing tracer: loading ebpf program: field IgProfioIns: program ig_profio_ins: apply CO-RE relocations: relocate Typedef:"btf_trace_block_rq_insert___new": target Typedef:"btf_trace_block_rq_insert": unknown: not supported

Version information

github.com/cilium/ebpf v0.13.2