8000 Fix recursive anon structures in btf. by haesbaert · Pull Request #187 · elastic/quark · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix recursive anon structures in btf. 8000 #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2025
Merged

Fix recursive anon structures in btf. #187

merged 3 commits into from
Jun 9, 2025

Conversation

haesbaert
Copy link
Collaborator
@haesbaert haesbaert commented Jun 7, 2025

Fix recursive anon structures in btf
Consider the following structure:

struct iov_iter {
	u8 iter_type;
	bool copy_mc;
	bool nofault;
	bool data_source;
	bool user_backed;
	union {
		size_t iov_offset;
		int last_offset;
	};
	union {
		struct iovec __ubuf_iovec;
		struct {
			union {
				/* use iter_iov() to get the current vec */
---------->			const struct iovec *__iov;
				const struct kvec *kvec;
				const struct bio_vec *bvec;
				struct xarray *xarray;
				void __user *ubuf;
			};
			size_t count;
		};
	};
	union {
		unsigned long nr_segs;
		loff_t xarray_start;
	};
};

If we want to get the offsetof(iov_iter, __iov) we need to go through 3
anonymous structures/unions.

This change makes it so that when we ask for iov_iter.__iov and we encounter an
anonymous structure/union in its members, we recurse into it trying to find
___iov.

Fixes tty_probes that rely on iov_iter.__iov, as a bonus it cuts the (anon) hack
we had before, now we don't need to specify anything, which implies a btfhub.c
regen in the upcoming commits.

Spotted, debugged and verified that the fix works by @nicholasberlin, many
thanks!!!

+++

Adapt libbpf error handling to post 1.0
In libbf commit 62e8af46d2c269300f1e9e019ef717b283bd808a, the default error
scheme has changed to libbpf_set_strict_mode(), which means no more error codes
embeded in pointers. The commit is from 2022, and quark started in 2024, so it
never made sense to use IS_ERR_OF_NULL, as it would always be NULL.

Makes the code a bit more digestible and cuts an ugly include.

@nicholasberlin nicholasberlin requested a review from Copilot June 7, 2025 20:05
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses the recursive handling of anonymous structures in BTF definitions by updating offset calculation routines and cleaning up kprobe structure definitions.

  • kprobe_defs.h: Updated the string identifiers to remove the redundant "(anon)" reference.
  • btf.c: Introduced a recursive helper function (btf_offsetof_rec) for offset computation and updated related functions accordingly.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
kprobe_defs.h Edited probe parameter labels to remove "(anon)" from the path.
btf.c Refactored offset computation functions to support recursion and adjusted anonymous structure handling.
Comments suppressed due to low confidence (1)

btf.c:214

  • The btf_root_offset2 function no longer checks that the offset is a multiple of 8 and does not perform a division by 8. Please confirm that this change in behavior is intentional and consistent with the expected offset units.
return (off);

@haesbaert haesbaert force-pushed the btf-iov branch 3 times, most recently from 9b87f5c to 33f8b58 Compare June 8, 2025 21:49
@haesbaert haesbaert marked this pull request as ready for review June 8, 2025 21:51
@haesbaert haesbaert requested a review from a team as a code owner June 8, 2025 21:51
haesbaert and others added 3 commits June 9, 2025 20:59
Consider the following structure:

```
struct iov_iter {
	u8 iter_type;
	bool copy_mc;
	bool nofault;
	bool data_source;
	bool user_backed;
	union {
		size_t iov_offset;
		int last_offset;
	};
	union {
		struct iovec __ubuf_iovec;
		struct {
			union {
				/* use iter_iov() to get the current vec */
---------->			const struct iovec *__iov;
				const struct kvec *kvec;
				const struct bio_vec *bvec;
				struct xarray *xarray;
				void __user *ubuf;
			};
			size_t count;
		};
	};
	union {
		unsigned long nr_segs;
		loff_t xarray_start;
	};
};
```

If we want to get the offsetof(iov_iter, __iov) we need to go through 3
anonymous structures/unions.

This change makes it so that when we ask for iov_iter.__iov and we encounter an
anonymous structure/union in its members, we recurse into it trying to find
___iov.

Fixes tty_probes that rely on iov_iter.__iov, as a bonus it cuts the (anon) hack
we had before, now we don't need to specify anything, which implies a btfhub.c
regen in the upcoming commits.

Spotted, debugged and verified that the fix works by @nicholasberlin, many
thanks!!!

Co-authored-by: Nicholas Berlin <56366649+nicholasberlin@users.noreply.github.com>
In libbf commit 62e8af46d2c269300f1e9e019ef717b283bd808a, the default error
scheme has changed to libbpf_set_strict_mode(), which means no more error codes
embeded in pointers. The commit is from 2022, and quark started in 2024, so it
never made sense to use IS_ERR_OF_NULL, as it would always be NULL.

Makes the code a bit more digestible and cuts an ugly include.
Now that the btf parser is recursive for anonymous structures, we don't need
this anymore, see previous commit.
@haesbaert haesbaert merged commit 683bc5b into main Jun 9, 2025
2 checks passed
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
0