[llvm] [BPF] Handle nested wrapper structs in BPF map definition traversal (PR #144097)
Michal Rostecki via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 17 23:25:54 PDT 2025
vadorovsky wrote:
@eddyz87
> I mean, you can use my C example, it tests the same thing:
>
> ```
> struct key { int i; };
> struct val { int j; };
>
> #define __uint(name, val) int (*name)[val]
> #define __type(name, val) typeof(val) *name
>
> struct {
> __uint(type, 1);
> __uint(max_entries, 1);
> __type(key, struct key);
> __type(value, struct val);
> } map __attribute__((section(".maps")));
> ```
OK, I can write an example in C which is capable of reproducing the issue.
To be precise, the map struct itself needs to be nested, so I will try something like:
```c
struct key { int i; };
struct val { int j; };
#define __uint(name, val) int (*name)[val]
#define __type(name, val) typeof(val) *name
struct {
struct {
__uint(type, 1);
__uint(max_entries, 1);
__type(key, struct key);
__type(value, struct val);
} map_def;
} map __attribute__((section(".maps")));
```
On a side note, I know that such nested definition doesn't work on libbpf. I made it work in Aya by modyfing the logic for parsing BTF maps. It works fine and the kernel is able to work with such program.
https://github.com/vadorovsky/aya/blob/8d6e12107ad0a0c4d72459c56fcc9c8d78fb6c90/aya-obj/src/obj.rs#L1275-L1367
I'm happy to port that to libbpf eventually, if there is interest.
> Disregard my previous comment, here is a point in the kernel source code that checks for pointers to be w/o names:
>
> * https://elixir.bootlin.com/linux/v6.15.2/source/kernel/bpf/btf.c#L2817
>
> * https://elixir.bootlin.com/linux/v6.15.2/source/kernel/bpf/btf.c#L2568
>
>
> Function `btf.c:btf_ref_type_check_meta()`.
I see. Would you be still open to relaxing `print_btf.py` (which I already pushed as a prerequisite commit) and me sending a patch relaxing that requirement in the kernel as well? Even though we currently do a fixup in bpf-linker, I think the right thing to do is to teach kernel to accept BTF produced by Rust and gradually remove the necessity of fxups on Aya/bpf-linker's side. I believe that approach would be helpful also for Rust-for-Linux folks, as they would eventually like to generate BTF for kernel modules written in Rust. It would be great if they don't have to do such fixups on their side.
https://github.com/llvm/llvm-project/pull/144097
More information about the llvm-commits
mailing list