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

Chapter2 confusing footnote description of tgid/pid #35

Open
AliJavidiCS opened this issue Dec 12, 2023 · 1 comment
Open

Chapter2 confusing footnote description of tgid/pid #35

AliJavidiCS opened this issue Dec 12, 2023 · 1 comment

Comments

@AliJavidiCS
Copy link

AliJavidiCS commented Dec 12, 2023

Hi, Hope you're having a wonderful day!

This is a quote form bpf-helpers man page:

u64 bpf_get_current_pid_tgid(void)
Return A 64-bit integer containing the current tgid and pid, created as such: current_task->tgid << 32 | current_task->pid.

As you can see the top 32 bits contain tgid and the bottom 32 bits hold pid of the process.
In the book this is explained the other way around on page 26. The foot note of page 26 also refers to the lower 32 bits as tgid which is incorrect.

Edit: As I continued reading the book I noticed the same situation with hello-buffer-config.py, hello-ring-buffer-config.py and page 61.

AliJavidiCS added a commit to AliJavidiCS/learning-ebpf that referenced this issue Dec 12, 2023
According to `man bpf-helpers` pid is located in the lower 32 bits of the return value. Pid can be easily extracted by using an and operation with `0xFFFFFFFF` 

Signed-off-by: Ali Javidi Ghasr <71215076+AliJavidiCS@users.noreply.github.com>
@AliJavidiCS AliJavidiCS changed the title Chapter2/hello-buffer.py: incorrectly trimming the output of bpf_get_current_pid_tgid Chapter2/hello-buffer.py: incorrectly trimming the output of bpf_get_current_pid_tgid() Dec 12, 2023
AliJavidiCS added a commit to AliJavidiCS/learning-ebpf that referenced this issue Dec 16, 2023
Based on Issue lizrice#35,
pid must be extracted from the return value by using an and operation with 0xFFFFFFFF.

Signed-off-by: Ali Javidi Ghasr <71215076+AliJavidiCS@users.noreply.github.com>
AliJavidiCS added a commit to AliJavidiCS/learning-ebpf that referenced this issue Dec 16, 2023
Based on Issue lizrice#35,
pid must be extracted from the return value by using an and operation with 0xFFFFFFFF.

Signed-off-by: Ali Javidi Ghasr <71215076+AliJavidiCS@users.noreply.github.com>
@lizrice
Copy link
Owner

lizrice commented Apr 4, 2024

This is pretty confusing but my understanding is that what we typically call "process ID" in user space is actually the thread group ID in the kernel.

From the BCC reference for this helper function:

Returns the process ID in the lower 32 bits (kernel's view of the PID, which in user space is usually presented as the thread ID), and the thread group ID in the upper 32 bits (what user space often thinks of as the PID)

So, the footnote on p26 is wrong, and could explain this better, but (assuming the user is interested in the user space view of process ID) the code is correct. I'll need to do an errata to fix this in the book but for some reason can't log into the O'Reilly author portal right now, so I'll leave this issue to track it. But I think this means the PR in #36 is not needed

@lizrice lizrice changed the title Chapter2/hello-buffer.py: incorrectly trimming the output of bpf_get_current_pid_tgid() Chapter2 confusing footnote description of tgid/pid Apr 4, 2024
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

No branches or pull requests

2 participants