Skip to content

Conversation

@mtardy
Copy link
Contributor

@mtardy mtardy commented Jul 25, 2025

Fixes missing inner map struct type definitions 1. We should visit the type of nested array of maps like we do for global maps. This patch adds a boolean to convey the information to visitTypeEntry and visitDerivedType that the pointee is a map definition and should be treated as such.

It ressembles and works with commit 0d21c95 ("[BPF] Handle nested wrapper structs in BPF map definition traversal (#144097)") which focused on directly nested wrapper structs.

Before that patch, this ARRAY_OF_MAPS definition would lead to the BTF information include the 'missing_type' as "FWD 'missing_type' fwd_kind=struct":

struct missing_type { uint64_t foo; };
struct {
	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
	[...]
	__array(
		values, struct {
			[...]
			__type(value, struct missing_type);
		});
} map SEC(".maps");

Which lead to errors while trying to load the map:

libbpf: map 'outer_map.inner': can't determine value size for type [N]: -22.

To solve this issue, users had to use the struct in a dummy variable or in a dummy function for the BTF to be generated correctly 2.

Footnotes

  1. https://lore.kernel.org/netdev/[email protected]/T/#u

  2. https://github.com/cilium/ebpf/discussions/1658#discussioncomment-12491339

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@mtardy
Copy link
Contributor Author

mtardy commented Jul 25, 2025

cc @yonghong-song

@eddyz87
Copy link
Contributor

eddyz87 commented Jul 25, 2025

This whole business with boolean flags for visitDerivedType gets out of hand.
I need to think a bit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, now we have three bool parameters. Probably we should have a struct to replace these bool parameters now.

@mtardy
Copy link
Contributor Author

mtardy commented Jul 25, 2025

Probably we should have a struct to replace these bool parameters now

If you want I can do a follow up patch in this PR with this. Or in another.

@eddyz87
Copy link
Contributor

eddyz87 commented Jul 26, 2025

Please wait a bit.

@eddyz87
Copy link
Contributor

eddyz87 commented Jul 26, 2025

First, I think that there is no need to modify visitTypeEntry at all, the changes can be confined to visitMapDefType, as in this commit.

Second, I think that visitMapDefType should be refactored to make intent more clear, as in this commit.

In both cases llvm unit tests require adjustments, kernel BPF selftests are passing, no significant changes in BPF generated for kernel BPF selftests.

@yonghong-song
Copy link
Contributor

That is true. If in visitMapDefType() we can identify inner maps, we can just recurse visitMapDefType() itself. This should make code simpler.

@mtardy mtardy force-pushed the btf-nested-map-fix branch from feec108 to 75e6c57 Compare July 28, 2025 11:11
@mtardy
Copy link
Contributor Author

mtardy commented Jul 28, 2025

First, I think that there is no need to modify visitTypeEntry at all, the changes can be confined to visitMapDefType, as in this commit.

Ah nice, I thought we needed to visitDerivedType anyway to generated the BTF type for the pointers to the map type (so I wrote such patch, skipping just visitTypeEntry), but it seems the visitTypeEntry at the end of visitMapDefType does the job (in a different order, thus the unit test BTF output modification). Thank you!

Second, I think that visitMapDefType should be refactored to make intent more clear, as in this commit.

Agree, it's better this way, only logical difference is you don't return on CTy->isForwardDecl(). I imagine you noticed and it's okay but just saying.

if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
return;

@eddyz87
Copy link
Contributor

eddyz87 commented Jul 28, 2025

Agree, it's better this way, only logical difference is you don't return on CTy->isForwardDecl(). I imagine you noticed and it's okay but just saying.

if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
return;

I thought about it and concluded that it shouldn't matter with the following reasoning: for top-level or nested structures in map key/value definition the reference in debug info shouldn't be a forward declaration. Even if it happens to be somehow, old code didn't handle this situation gracefully (didn't search for corresponding definition).

@eddyz87
Copy link
Contributor

eddyz87 commented Jul 28, 2025

@mtardy ,

Could you please fix the following tests:

Failed Tests (3):
  LLVM :: CodeGen/BPF/BTF/map-def-2.ll
  LLVM :: CodeGen/BPF/BTF/map-def-3.ll
  LLVM :: CodeGen/BPF/BTF/map-def-nested-array.ll

Some reordering is because of my changes in patch #1?
Probably would be nice to switch map-def-{2,3}.ll to ./llvm/test/CodeGen/BPF/BTF/print_btf.py.

mtardy and others added 2 commits July 28, 2025 17:46
Fixes missing inner map struct type definitions [^1]. We should visit
the type of nested array of maps like we do for global maps. We visit
the derived type of the array directly in visitMapDefType.

Because of the small changes in visitMapDefType, ordering of the BTF is
changed, for example, the PTR for the nested maps might appear later.
This explain why this patch had to update the BTF checks of the tests
'map-def-nested-array.ll', 'map-def-2.ll' and 'map-def-3.ll'. We took
the opportunity to migrate the last two to use 'print_btf.py'.

It ressembles commit 0d21c95 ("[BPF] Handle nested wrapper structs
in BPF map definition traversal (llvm#144097)") which focused on directly
nested wrapper structs.

Before that patch, this ARRAY_OF_MAPS definition would lead to the BTF
information include the 'missing_type' as "FWD 'missing_type'
fwd_kind=struct":

	struct missing_type { uint64_t foo; };
	struct {
		__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
		[...]
		__array(
			values, struct {
				[...]
				__type(value, struct missing_type);
			});
	} map SEC(".maps");

Which lead to errors while trying to load the map:

	libbpf: map 'outer_map.inner': can't determine value size for type [N]: -22.

To solve this issue, users had to use the struct in a dummy variable or
in a dummy function for the BTF to be generated correctly [^2].

[^1]: https://lore.kernel.org/netdev/[email protected]/T/#u
[^2]: cilium/ebpf#1658 (reply in thread)

Co-authored-by: Eduard Zingerman <[email protected]>
Signed-off-by: Mahe Tardy <[email protected]>
Kernel BPF selftests passing. No change in BTF generated for kernel
selftests, modulo some reorderings.

Signed-off-by: Mahe Tardy <[email protected]>
@mtardy mtardy force-pushed the btf-nested-map-fix branch from 75e6c57 to f67f8ea Compare July 28, 2025 17:53
@mtardy
Copy link
Contributor Author

mtardy commented Jul 28, 2025

Could you please fix the following tests:

Failed Tests (3):
  LLVM :: CodeGen/BPF/BTF/map-def-2.ll
  LLVM :: CodeGen/BPF/BTF/map-def-3.ll
  LLVM :: CodeGen/BPF/BTF/map-def-nested-array.ll

Some reordering is because of my changes in patch #1? Probably would be nice to switch map-def-{2,3}.ll to ./llvm/test/CodeGen/BPF/BTF/print_btf.py.

Ah yep I missed this indeed! This should be fixed now and I migrated the tests to use print_btf.py.

@yonghong-song yonghong-song merged commit 379949d into llvm:main Jul 28, 2025
9 checks passed
@github-actions
Copy link

@mtardy Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@mtardy mtardy deleted the btf-nested-map-fix branch September 2, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants