Skip to content
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

fix(bpf): Access __state from task_struct #1550

Merged

Conversation

dave-tucker
Copy link
Collaborator

@dave-tucker dave-tucker commented Jun 17, 2024

In older kernels prev_state isn't passed as part of the context of the
function. Therefore it's better to access prev_task-->_state directly.

The field was renamed from state to __state in Kernel 5.14.
Given that we need to support Kernel 5.12+ we include the
bpf_core_read.h file such that we can use the bpf_core_field_exists
function that allows us to see which field is present in task struct
and return the correct value.

Copy link
Contributor

github-actions bot commented Jun 17, 2024

🤖 SeineSailor

Here is a concise summary of the pull request changes:

Summary: The fix(bpf): Access __state from task_struct pull request introduces changes to access prev_task->__state directly in older kernel versions where prev_state isn't passed as part of the function context.

Key Modifications:

  1. New helper function: A new get_task_state helper function is introduced to facilitate accessing task structures.
  2. New header file: A new bpf_core_read.h header file is added to provide a standardized way of accessing task structures.

Impact on Codebase:

  • The changes are internal and do not affect the external interface or behavior of the code.
  • The modifications ensure compatibility with older kernel versions where prev_state is not passed as part of the function context.

Observations and Suggestions:

  • The changes seem to be well-contained and do not introduce any breaking changes to the codebase.
  • It would be beneficial to include additional tests to ensure the new get_task_state function works correctly in different kernel versions.
  • Consider adding documentation to the bpf_core_read.h header file to explain its purpose and usage.

Overall, the pull request appears to be a well-structured and targeted fix to ensure compatibility with older kernel versions.

Comment on lines 198 to 199
int pid;
unsigned int __state;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it fixed / defined what the size of int is ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, these fields are BTF relocated - which will perform the necessary checks around int size and signedness at program load time.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks Dave.
Related question ... shouldn't we use unsigned int or u32 for pid as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we use BPF_CORE_READ() to read the pointers in a safe manner?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks a lot for clarifications, @dave-tucker 🙏

@dave-tucker dave-tucker marked this pull request as ready for review June 18, 2024 10:12
In older kernels prev_state isn't passed as part of the context of the
function. Therefore it's better to access prev_task-->_state directly.

The field was renamed from state to __state in Kernel 5.14.
Given that we need to support Kernel 5.12+ we include the
bpf_core_read.h file such that we can use the bpf_core_field_exists
function that allows us to see which field is present in task struct
and return the correct value.

Signed-off-by: Dave Tucker <[email protected]>
Copy link
Collaborator

@marceloamaral marceloamaral left a comment

Choose a reason for hiding this comment

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

/lgtm

@marceloamaral marceloamaral merged commit fbe9b3c into sustainable-computing-io:main Jun 18, 2024
26 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