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

tetragon: Use do_task_dead probe for exit sensor #1012

Merged
merged 2 commits into from Jun 1, 2023

Conversation

olsajiri
Copy link
Contributor

@olsajiri olsajiri commented May 19, 2023

Hooking on do_task_dead kernel function, which is the last one the
task would execute after exiting. It's stable since v4.19, so it's
safe to hook for us.

To find out if we are the last thread of execution in the task we
use current->signal->live counter (thanks Djalal! ;-) )

It's initialized for thread leader:

  clone {
    copy_process
      copy_signal
        atomic_set(&sig->live, 1); 
  }

Incremented for each new thread:

  clone {
    copy_process
      atomic_inc(&current->signal->live);
    ... 
    wake_up_new_task
  }

Decremented for each exiting thread:

  do_exit {
    atomic_dec_and_test(&tsk->signal->live);
    ... 
    do_task_dead
      __schedule
      BUG 
  }

If task->signal->live == 0 we are the last thread of execution and we
won't race with another clone, because there's no other thread to call
it (current thread is in do_exit).

@netlify
Copy link

netlify bot commented May 19, 2023

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 2673768
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/64772e1674145e0008312a59
😎 Deploy Preview https://deploy-preview-1012--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@olsajiri olsajiri force-pushed the exit_fix branch 5 times, most recently from de59db0 to bb3b292 Compare May 23, 2023 12:17
Copy link
Member

@tixxdz tixxdz left a comment

Choose a reason for hiding this comment

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

Lgtm , thank you ;-)

@tixxdz
Copy link
Member

tixxdz commented May 24, 2023

failure is not related to this PR as it is tracked here: #1025

Hooking on do_task_dead kernel function, which is the last one the
task would execute after exiting. It's stable since v4.19, so it's
safe to hook for us.

To find out if we are the last thread of execution in the task we
use current->signal->live counter (thanks Djalal! ;-) )

It's initialized for thread leader:

  clone {
    copy_process
      copy_signal
        atomic_set(&sig->live, 1);
  }

Incremented for each new thread:

  clone {
    copy_process
      atomic_inc(&current->signal->live);
    ...
    wake_up_new_task
  }

Decremented for each exiting thread:

  do_exit {
    atomic_dec_and_test(&tsk->signal->live);
    ...
    do_task_dead
      __schedule
      BUG
  }

If task->signal->live == 0 we are the last thread of execution and we
won't race with another clone, because there's no other thread to call
it (current thread is in do_exit).

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding extra threads to the exit-leader test prog.

Spawning 2 threads directly from leader and another 2
from the newly created threads.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
@olsajiri olsajiri marked this pull request as ready for review May 31, 2023 12:31
@olsajiri olsajiri requested a review from a team as a code owner May 31, 2023 12:31
@olsajiri olsajiri requested a review from kkourt May 31, 2023 12:31
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

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

Cool! Thanks!

*
* If task->signal->live == 0 we are the last thread of execution and we
* won't race with another clone, because there's no other thread to call
* it (current thread is in do_exit).
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the nice comment :)

@kkourt kkourt merged commit 0646c56 into cilium:main Jun 1, 2023
24 checks passed
@kkourt kkourt added needs-backport/0.8 This PR needs backporting to 0.8 needs-backport/0.9 and removed needs-backport/0.8 This PR needs backporting to 0.8 labels Jun 27, 2023
@kkourt kkourt mentioned this pull request Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants