FIXTURE_TEARDOWN_PARENT(process_madvise)
{ /* This teardown is guaranteed to run, even if tests SKIP or ASSERT */ if (self->child_pid > 0) {
kill(self->child_pid, SIGKILL);
waitpid(self->child_pid, NULL, 0);
}
if (self->remote_pidfd >= 0)
close(self->remote_pidfd);
}
/* * This test uses PIDFD_SELF to target the current process. The main * goal is to verify the basic behavior of process_madvise() with * a vector of non-contiguous memory ranges, not its cross-process * capabilities.
*/
TEST_F(process_madvise, basic)
{ constunsignedlong pagesize = self->page_size; constint madvise_pages = 4; struct iovec vec[madvise_pages]; int pidfd = self->pidfd;
ssize_t ret; char *map;
/* * Create a single large mapping. We will pick pages from this * mapping to advise on. This ensures we test non-contiguous iovecs.
*/
map = mmap(NULL, pagesize * 10, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
/* Fill the entire region with a known pattern. */
memset(map, 'A', pagesize * 10);
/* * Setup the iovec to point to 4 non-contiguous pages * within the mapping.
*/
vec[0].iov_base = &map[0 * pagesize];
vec[0].iov_len = pagesize;
vec[1].iov_base = &map[3 * pagesize];
vec[1].iov_len = pagesize;
vec[2].iov_base = &map[5 * pagesize];
vec[2].iov_len = pagesize;
vec[3].iov_base = &map[8 * pagesize];
vec[3].iov_len = pagesize;
ret = sys_process_madvise(pidfd, vec, madvise_pages, MADV_DONTNEED, 0); if (ret == -1 && errno == EPERM)
SKIP(return, "process_madvise() unsupported or permission denied, try running as root.\n"); elseif (errno == EINVAL)
SKIP(return, "process_madvise() unsupported or parameter invalid, please check arguments.\n");
/* The call should succeed and report the total bytes processed. */
ASSERT_EQ(ret, madvise_pages * pagesize);
/* Check that advised pages are now zero. */ for (int i = 0; i < madvise_pages; i++) { char *advised_page = (char *)vec[i].iov_base;
/* Content must be 0, not 'A'. */
ASSERT_EQ(*advised_page, '\0');
}
/* Check that an un-advised page in between is still 'A'. */ char *unadvised_page = &map[1 * pagesize];
for (int i = 0; i < pagesize; i++)
ASSERT_EQ(unadvised_page[i], 'A');
/* * This test deterministically validates process_madvise() with MADV_COLLAPSE * on a remote process, other advices are difficult to verify reliably. * * The test verifies that a memory region in a child process, * focus on process_madv remote result, only check addresses and lengths. * The correctness of the MADV_COLLAPSE can be found in the relevant test examples in khugepaged.
*/
TEST_F(process_madvise, remote_collapse)
{ constunsignedlong pagesize = self->page_size; long huge_page_size; int pipe_info[2];
ssize_t ret; struct iovec vec;
ret = sys_process_madvise(self->remote_pidfd, &vec, 1, MADV_COLLAPSE,
0); if (ret == -1) { if (errno == EINVAL)
SKIP(return, "PROCESS_MADV_ADVISE is not supported.\n"); elseif (errno == EPERM)
SKIP(return, "No process_madvise() permissions, try running as root.\n"); return;
}
ASSERT_EQ(ret, huge_page_size);
}
/* * Test process_madvise() with a pidfd for a process that has already * exited to ensure correct error handling.
*/
TEST_F(process_madvise, exited_process_pidfd)
{ constunsignedlong pagesize = self->page_size; struct iovec vec; char *map;
ssize_t ret;
map = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0); if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
vec.iov_base = map;
vec.iov_len = pagesize;
/* * Using a pidfd for a process that has already exited should fail * with ESRCH.
*/
self->child_pid = fork();
ASSERT_NE(self->child_pid, -1);
/* * Test process_madvise() with bad pidfds to ensure correct error * handling.
*/
TEST_F(process_madvise, bad_pidfd)
{ constunsignedlong pagesize = self->page_size; struct iovec vec; char *map;
ssize_t ret;
map = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0); if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
vec.iov_base = map;
vec.iov_len = pagesize;
/* Using an invalid fd number (-1) should fail with EBADF. */
ret = sys_process_madvise(-1, &vec, 1, MADV_DONTNEED, 0);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EBADF);
/* * Using a valid fd that is not a pidfd (e.g. stdin) should fail * with EBADF.
*/
ret = sys_process_madvise(STDIN_FILENO, &vec, 1, MADV_DONTNEED, 0);
ASSERT_EQ(ret, -1);
ASSERT_EQ(errno, EBADF);
}
/* * Test that process_madvise() rejects vlen > UIO_MAXIOV. * The kernel should return -EINVAL when the number of iovecs exceeds 1024.
*/
TEST_F(process_madvise, invalid_vlen)
{ constunsignedlong pagesize = self->page_size; int pidfd = self->pidfd; struct iovec vec; char *map;
ssize_t ret;
map = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0); if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
/* * Test process_madvise() with an invalid flag value. Currently, only a flag * value of 0 is supported. This test is reserved for the future, e.g., if * synchronous flags are added.
*/
TEST_F(process_madvise, flag)
{ constunsignedlong pagesize = self->page_size; unsignedint invalid_flag; int pidfd = self->pidfd; struct iovec vec; char *map;
ssize_t ret;
map = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0); if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.