-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Try to use pidfd and epoll to wait init process exit #4517
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
base: main
Are you sure you want to change the base?
Conversation
bcddc62
to
8126a8d
Compare
@abel-von PTAL |
It seems that the first commit can be merged now and is definitely an improvement. For the rest of it, give me a few days to review. |
Reviewing this reminded me the next step needed for pidfd support in Go, so I wrote this proposal: golang/go#70352 |
Wonderful proposal, I used to think that golang wouldn't support similar interfaces, but I think it's very useful, looking forward its coming. |
8126a8d
to
7833912
Compare
LGTM |
@lifubang are you going to keep working on it? This looks very good overall |
Thanks, I'll work on it later. |
cc64599
to
16ae7fc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frankly, I'm having a hard time reviewing this because it's hard to wrap around all the kill/destroy logic that we already have in place:
container.Signal
;terminate
method ofparentProcess
;destroy(c *Container)
;- etc.
All this seem like a bunch of code full of special cases and kludges, and (in my eyes) it cries to be refactored to be more straightforward and clean. Maybe I'm wrong but this stands in the way of me reviewing this.
As I can't finish this, here's my WIP review bits and pieces:
-
The logic added might also be useful from
func destroy(c *Container) error
. -
Using epoll on pidfd can also be used when there are multiple pids (i.e. from signalAllProcesses, in the current code).
-
This adds a new public method (
container.Kill
) when we already havecontainer.Signal
. I understand why, but maybe we should call itcontainer.KillSync
(orcontainer.EnsureKilled
) instead (so it's clear it not just sends the SIGKILL but also waits for the container to be killed). A note tocontainer.Signal
should be added referencing the new method. It should also be described in libcontainer's README.
This would be very nice to have in 1.3 but we might not have enough time to have it ready by 1.3-rc1. |
I think it's not worth to do:
If we don't want to introduce a wait mechanism in |
edc621d
to
5f102d8
Compare
I agree, let's drop the 'signalAllProcesses` (frankly I don't remember why I thought it would be useful in there). |
Will try to review the rest of it tomorrow. |
5f102d8
to
74e9927
Compare
libcontainer/container_linux.go
Outdated
// We don't need unix.PidfdSendSignal because go runtime will use it if possible. | ||
_ = c.Signal(unix.SIGKILL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use c.signal
here as we made sure that we have private pidns.
Maybe don't ignore error, because otherwise you might be waiting for 10 seconds for something that will never happen.
Or, you can be explicit and use PidfdSendSignal
here to be 100% sure that it succeeded.
6afd540
to
df920a9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
@rata PTAL
I'm on PTO, will take a look next week if it's not already merged ;) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left several comment. I feel like I'm missing something regarding the relation of pidfd and pidns. Sorry if that is the case :-/
libcontainer/container_linux.go
Outdated
n, err := unix.EpollWait(epollfd, events, 10000) | ||
if err != nil { | ||
if err == unix.EINTR { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a wrapper in libcontainer/internal/linux for unix.EpollWait? Using the retryOnEINTR helper, so we don't have to do it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rebase to add this based on #4697.
libcontainer/container_linux.go
Outdated
err := c.killViaPidfd() | ||
if err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need a linter to suggest automatically to use the single-line if here: if err := ...; err == nil {...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't use a single line to check the err, because we have to log the err and go to next.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you can check if no errors, IOW err==nil
. It's exactly the same
What am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to print this error in the next line, but anyway, I have declared the err as a block scope var now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is okay. I think using an else is even simpler. Or something like:
if err :=c.Kill...(); err != nil {
logrus.Debugf("pidfd....")
} else {
return nil
}
defer unix.Close(epollfd) | ||
|
||
event := unix.EpollEvent{ | ||
Events: unix.EPOLLIN, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing epoll with only one fd is kind of overkill, I wonder if there are simpler solutions. But if, as I mentioned in another comment, we can kill all the processes in the cgroup, then the epoll might be worth it?
libcontainer/container_linux.go
Outdated
return errors.New("container init still running") | ||
} | ||
|
||
if n > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a case where n < 0? I mean, do we need this if here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll refactor to cover this condition.
@@ -431,6 +435,81 @@ func (c *Container) signal(s os.Signal) error { | |||
return nil | |||
} | |||
|
|||
func (c *Container) killViaPidfd() error { | |||
pidfd, err := unix.PidfdOpen(c.initProcess.pid(), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the old code was killing only the init process. But would it make sense to kill all the processes in the cgroup instead?
We can do it as another PR, if that makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you mentioned in the above, we can consider this order:
- Use cgroup.kill if the kernel supports it
- Use pidfd if the kernel supports it
- Just send a signal otherwise
But I think we still need to consider whether the container has a private pid ns or not.
What I think it's that, it's reasonable to kill only the init process for a private pid ns container.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking into account this: #4517 (comment).
It seems simpler to kill pid1 if it has it's own pidns. Otherwise, cgroup.kill or, if not supported, send a signal.
Does it make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's no need to use cgroup.kill
to kill all process in the cgroup to kill the pid1 if it has a private own pidns. We just only need to kill the exact pid1 process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly what I said in the last comment :)
|
||
// EnsureKilled kills the container and waits for the kernel to finish killing it. | ||
func (c *Container) EnsureKilled() error { | ||
// When a container doesn't have a private pidns, we have to kill all processes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/doesn't/does/?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening again, sorry, but the if checks that it does have a private PID namespace. The comment says it doesn't. Something seems odd. Am I missing something?
@@ -431,6 +435,81 @@ func (c *Container) signal(s os.Signal) error { | |||
return nil | |||
} | |||
|
|||
func (c *Container) killViaPidfd() error { | |||
pidfd, err := unix.PidfdOpen(c.initProcess.pid(), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking into account this: #4517 (comment).
It seems simpler to kill pid1 if it has it's own pidns. Otherwise, cgroup.kill or, if not supported, send a signal.
Does it make sense?
delete.go
Outdated
// When --force is given, we kill all container processes and | ||
// then destroy the container. This is done even for a stopped | ||
// container, because (in case it does not have its own PID | ||
// namespace) there may be some leftover processes in the | ||
// container's cgroup. | ||
if force { | ||
return killAndDestroy(container) | ||
} | ||
s, err := container.Status() | ||
if err != nil { | ||
return err | ||
} | ||
switch s { | ||
case libcontainer.Stopped: | ||
return container.Destroy() | ||
// For a stopped container, because (in case it does not have | ||
// its own PID namespace) there may be some leftover processes | ||
// in the container's cgroup. | ||
if !container.Config().Namespaces.IsPrivate(configs.NEWPID) { | ||
if err := container.EnsureKilled(); err != nil { | ||
return err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't wrap my head around this. Before this PR on force, we were using container.Signal()
. Now, if a container is stopped and doesn't have a private pidns, we don't do nothing here, and at the end we do container.Destroy()
.
The commit msg doesn't mention anything about this. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's a mistake, removed now. Thanks.
Signed-off-by: lifubang <[email protected]>
When using unix.Kill to kill the container, we need a for loop to detect the init process exited or not manually, we sleep 100ms each time in the current, but for stopped containers or containers running in a low load machine, we don't need to wait so long time. This change will reduce the delete delay in some situations, especially for those pods with many containers in. Co-authored-by: Abel Feng <[email protected]> Signed-off-by: lifubang <[email protected]>
94caf04
to
e22ebcd
Compare
return unix.EpollWait(epfd, events, msec) | ||
}) | ||
if err != nil { | ||
return 0, os.NewSyscallError("epollwait", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
epoll_wait returns -1 on error.Let's return -1 here too.
|
||
// EnsureKilled kills the container and waits for the kernel to finish killing it. | ||
func (c *Container) EnsureKilled() error { | ||
// When a container doesn't have a private pidns, we have to kill all processes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening again, sorry, but the if checks that it does have a private PID namespace. The comment says it doesn't. Something seems odd. Am I missing something?
if err = c.killViaPidfd(); err == nil { | ||
return nil | ||
} | ||
|
||
logrus.Debugf("pidfd & epoll failed, falling back to unix.Signal: %v", err) | ||
} | ||
return c.kill() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't wrap my head around this either. Why don't we use a pidfd in c.Signal()
(if that is available, if it's not we fallback to sending a signal to the pid number) and create a new function to wait on the process to die? Again, if that if pidfd is possible, we wait on that, if it's not, we fallback to wait as we do now.
Having a (public/exported) Kill()
and KillViaPidfd()
seems like something that should be abstracted.
What we are doing now seems kind of complex:
- If it has a pidns, then try to kill with a pidfd the pid1
- If it doesn't have a pidns, then try to kill the process sending a signal to the pid number (even if pidfd is supported, why?). The function we call here also handles the case of having a private pidns, which makes this more tricky.
If what I propose doesn't seem okay, I'm open to other ways to simplify this :)
@lifubang if you address the comments and want another review, if you can please comment or request a review via github, that greatly helps me to know when it is ready for another round. Thanks! |
This PR does some optimizations for
runc delete -f
.unix.PidFDSendSignal
to send signal to the process,this is helpful to reduce the risk of pid reuse attack. So we should replace
unix.Kill
withos.Process.Signal
in runc when possible.os.Process.Wait
is used to wait the child process, to wait a unrelated process, weshould introduce pidfd & epoll to reduce the sleep time when we want to detect the init
process exited or not.
unix.Kill
solution, but for stopped containers or containers running in a low load machine,we don't need to wait 100ms to do the next detection.
Close: #4512