-
Notifications
You must be signed in to change notification settings - Fork 596
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
criu: Add support for pidfds #2449
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d22b9bd
images: Add protobuf definition for pidfd
bsach64 e6e9b3b
criu: Support C/R of pidfds
bsach64 d2099fe
zdtm: Check pidfd fdinfo entry is consistent
bsach64 e13ed2f
zdtm: Check pidfd can send signal after C/R
bsach64 f64e4c6
zdtm: Check pidfd can kill descendant processes
bsach64 dc9ce75
zdtm: Check dead pidfd is restored correctly
bsach64 f78a7f4
zdtm: Check fd from pidfd_getfd is C/Red correctly
bsach64 875be05
zdtm: Check pidfd for thread is valid after C/R
bsach64 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ TST_NOFILE := \ | |
shm-mp \ | ||
ptrace_sig \ | ||
pidfd_self \ | ||
pidfd_dead \ | ||
pidfd_child \ | ||
pidfd_kill \ | ||
pipe00 \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
#include <sys/statfs.h> | ||
#include <sys/syscall.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
|
||
#include "zdtmtst.h" | ||
|
||
const char *test_doc = "Check C/R of pidfds that point to dead processes\n"; | ||
const char *test_author = "Bhavik Sachdev <[email protected]>"; | ||
|
||
#ifndef PID_FS_MAGIC | ||
#define PID_FS_MAGIC 0x50494446 | ||
#endif | ||
|
||
/* | ||
* main | ||
* `- child | ||
* `- grandchild | ||
* | ||
* main opens a pidfd for both child and grandchild. | ||
* Before C/R we kill both child and grandchild. | ||
* We end up with two unique dead pidfds. | ||
*/ | ||
|
||
static long get_fs_type(int lfd) | ||
{ | ||
struct statfs fst; | ||
|
||
if (fstatfs(lfd, &fst)) { | ||
return -1; | ||
} | ||
return fst.f_type; | ||
} | ||
|
||
static int pidfd_open(pid_t pid, unsigned int flags) | ||
{ | ||
return syscall(__NR_pidfd_open, pid, flags); | ||
} | ||
|
||
static int pidfd_send_signal(int pidfd, int sig, siginfo_t* info, unsigned int flags) | ||
{ | ||
return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); | ||
} | ||
|
||
static int open_pidfd_pair(int pidfd[2], int pid) | ||
{ | ||
pidfd[0] = pidfd_open(pid, 0); | ||
if (pidfd[0] < 0) { | ||
pr_perror("pidfd_open() failed"); | ||
return 1; | ||
} | ||
|
||
pidfd[1] = pidfd_open(pid, 0); | ||
if (pidfd[1] < 0) { | ||
close(pidfd[0]); | ||
pr_perror("pidfd_open() failed"); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
static int compare_pidfds(int pidfd[2]) | ||
{ | ||
/* | ||
* After linux 6.9 we can compare inode numbers | ||
* to determine if two pidfds point to the same process. | ||
* While the inode number may change before and after C/R | ||
* pidfds pointing to the same pid should have the same inode number. | ||
*/ | ||
struct statx stats[2]; | ||
statx(pidfd[0], "", AT_EMPTY_PATH, STATX_ALL, &stats[0]); | ||
statx(pidfd[1], "", AT_EMPTY_PATH, STATX_ALL, &stats[1]); | ||
if (stats[0].stx_ino != stats[1].stx_ino) | ||
return 1; | ||
return 0; | ||
} | ||
|
||
static int check_for_pidfs(void) | ||
{ | ||
long type; | ||
int pidfd = pidfd_open(getpid(), 0); | ||
if (pidfd < 0) { | ||
pr_perror("pidfd open() failed"); | ||
return -1; | ||
} | ||
type = get_fs_type(pidfd); | ||
close(pidfd); | ||
return type == PID_FS_MAGIC; | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
#define READ 0 | ||
#define WRITE 1 | ||
|
||
int child, ret, gchild, p[2], status; | ||
int cpidfd[2], gpidfd[2]; | ||
struct statx stats[2]; | ||
|
||
test_init(argc, argv); | ||
|
||
ret = check_for_pidfs(); | ||
if (ret < 0) | ||
return 1; | ||
|
||
if (ret == 0) { | ||
test_daemon(); | ||
test_waitsig(); | ||
skip("Test requires pidfs. skipping..."); | ||
pass(); | ||
return 0; | ||
} | ||
|
||
if (pipe(p)) { | ||
pr_perror("pipe"); | ||
return 1; | ||
} | ||
|
||
child = test_fork(); | ||
if (child < 0) { | ||
pr_perror("fork"); | ||
return 1; | ||
} else if (child == 0) { | ||
avagin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int gchild = test_fork(); | ||
close(p[READ]); | ||
if (gchild < 0) { | ||
pr_perror("fork"); | ||
return 1; | ||
} else if (gchild == 0) { | ||
close(p[WRITE]); | ||
while(1) | ||
sleep(1000); | ||
} else { | ||
if (write(p[WRITE], &gchild, sizeof(int)) != sizeof(int)) { | ||
pr_perror("write"); | ||
return 1; | ||
} | ||
close(p[WRITE]); | ||
if (waitpid(gchild, &status, 0) != gchild) { | ||
pr_perror("waitpid"); | ||
return 1; | ||
} | ||
|
||
if (!WIFSIGNALED(status)) { | ||
fail("Expected grandchild to be terminated by a signal"); | ||
return 1; | ||
} | ||
|
||
if (WTERMSIG(status) != SIGKILL) { | ||
fail("Expected grandchild to be terminated by SIGKILL"); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
ret = open_pidfd_pair(cpidfd, child); | ||
if (ret) | ||
return 1; | ||
|
||
close(p[WRITE]); | ||
if (read(p[READ], &gchild, sizeof(int)) != sizeof(int)) { | ||
pr_perror("write"); | ||
return 1; | ||
} | ||
close(p[READ]); | ||
|
||
ret = open_pidfd_pair(gpidfd, gchild); | ||
if (ret) | ||
return 1; | ||
|
||
/* | ||
* We kill grandchild and child processes only after opening pidfds. | ||
*/ | ||
if (pidfd_send_signal(gpidfd[0], SIGKILL, NULL, 0)) { | ||
pr_perror("pidfd_send_signal"); | ||
goto fail_close; | ||
} | ||
|
||
if (waitpid(child, &status, 0) != child) { | ||
pr_perror("waitpid"); | ||
goto fail_close; | ||
} | ||
|
||
if (!WIFEXITED(status)) { | ||
fail("Expected child to exit normally"); | ||
goto fail_close; | ||
} | ||
|
||
if (WEXITSTATUS(status) != 0) { | ||
fail("Expected child to exit with 0"); | ||
goto fail_close; | ||
} | ||
usleep(1000); | ||
|
||
if (kill(gchild, 0) != -1 && errno != ESRCH) { | ||
fail("Expected grand child to not exist"); | ||
goto fail_close; | ||
} | ||
|
||
if (kill(child, 0) != -1 && errno != ESRCH) { | ||
fail("Expected child to not exist"); | ||
goto fail_close; | ||
} | ||
|
||
test_daemon(); | ||
test_waitsig(); | ||
|
||
avagin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret = compare_pidfds(cpidfd); | ||
if (ret) { | ||
fail("inodes not same for same pid"); | ||
goto fail_close; | ||
} | ||
|
||
ret = compare_pidfds(gpidfd); | ||
if (ret) { | ||
fail("inodes not same for same pid"); | ||
goto fail_close; | ||
} | ||
|
||
statx(cpidfd[0], "", AT_EMPTY_PATH, STATX_ALL, &stats[0]); | ||
statx(gpidfd[0], "", AT_EMPTY_PATH, STATX_ALL, &stats[1]); | ||
if (stats[0].stx_ino == stats[1].stx_ino) { | ||
fail("pidfds pointing to diff pids should have diff inodes"); | ||
goto fail_close; | ||
} | ||
|
||
pass(); | ||
close(cpidfd[0]); | ||
close(cpidfd[1]); | ||
close(gpidfd[0]); | ||
close(gpidfd[1]); | ||
return 0; | ||
|
||
fail_close: | ||
close(cpidfd[0]); | ||
close(cpidfd[1]); | ||
close(gpidfd[0]); | ||
close(gpidfd[1]); | ||
return 1; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 looks like this test fails in Fedora Rawhide:
https://github.com/checkpoint-restore/criu/runs/31040419148
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 will try to see why this happens!
Is there any way that I can replicate this test locally?
I did try running the tests on a VM with Fedora Rawhide
6.12.0-0.rc1.20241005git27cc6fdf7201.22.fc42.x86_64
where these tests end up passing!
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.
@bsach64 There is a race condition between
kill()
andwaitpid()
infree_dead_pidfd()
.You can replicate the error with the following change:
We might need to add the temporary process PID (i.e., the processes created with
create_tmp_process()
) in the list of task helpers (ta->helpers
). Seecollect_helper_pids()
incr-restore.c
.cc: @avagin
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.
Another solution might be to block
SIGCHLD
?See
sysctl.c
line 271 https://github.com/checkpoint-restore/criu/blob/criu-dev/criu/sysctl.c#L271cc: @mihalicyn