selftests/mm: add fork CoW guard page test

When we fork anonymous pages, apply a guard page then remove it, the
previous CoW mapping is cleared.

This might not be obvious to an outside observer without taking some time
to think about how the overall process functions, so document that this is
the case through a test, which also usefully asserts that the behaviour is
as we expect.

This is grouped with other, more important, fork tests that ensure that
guard pages are correctly propagated on fork.

Fix a typo in a nearby comment at the same time.

[ryan.roberts@arm.com: static process_madvise() wrapper for guard-pages]
  Link: https://lkml.kernel.org/r/20250107142937.1870478-1-ryan.roberts@arm.com
Link: https://lkml.kernel.org/r/20241205190748.115656-1-lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
Cc: Jann Horn <jannh@google.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
pull/1134/merge
Lorenzo Stoakes 2024-12-05 19:07:48 +00:00 committed by Andrew Morton
parent 67c8b11bd5
commit 19b65ffae9
2 changed files with 85 additions and 3 deletions

View File

@ -55,6 +55,12 @@ static int pidfd_open(pid_t pid, unsigned int flags)
return syscall(SYS_pidfd_open, pid, flags);
}
static ssize_t sys_process_madvise(int pidfd, const struct iovec *iovec,
size_t n, int advice, unsigned int flags)
{
return syscall(__NR_process_madvise, pidfd, iovec, n, advice, flags);
}
/*
* Enable our signal catcher and try to read/write the specified buffer. The
* return value indicates whether the read/write succeeds without a fatal
@ -419,7 +425,7 @@ TEST_F(guard_pages, process_madvise)
ASSERT_EQ(munmap(&ptr_region[99 * page_size], page_size), 0);
/* Now guard in one step. */
count = process_madvise(pidfd, vec, 6, MADV_GUARD_INSTALL, 0);
count = sys_process_madvise(pidfd, vec, 6, MADV_GUARD_INSTALL, 0);
/* OK we don't have permission to do this, skip. */
if (count == -1 && errno == EPERM)
@ -440,7 +446,7 @@ TEST_F(guard_pages, process_madvise)
ASSERT_FALSE(try_read_write_buf(&ptr3[19 * page_size]));
/* Now do the same with unguard... */
count = process_madvise(pidfd, vec, 6, MADV_GUARD_REMOVE, 0);
count = sys_process_madvise(pidfd, vec, 6, MADV_GUARD_REMOVE, 0);
/* ...and everything should now succeed. */
@ -990,7 +996,7 @@ TEST_F(guard_pages, fork)
MAP_ANON | MAP_PRIVATE, -1, 0);
ASSERT_NE(ptr, MAP_FAILED);
/* Establish guard apges in the first 5 pages. */
/* Establish guard pages in the first 5 pages. */
ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_INSTALL), 0);
pid = fork();
@ -1029,6 +1035,77 @@ TEST_F(guard_pages, fork)
ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
}
/*
* Assert expected behaviour after we fork populated ranges of anonymous memory
* and then guard and unguard the range.
*/
TEST_F(guard_pages, fork_cow)
{
const unsigned long page_size = self->page_size;
char *ptr;
pid_t pid;
int i;
/* Map 10 pages. */
ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
ASSERT_NE(ptr, MAP_FAILED);
/* Populate range. */
for (i = 0; i < 10 * page_size; i++) {
char chr = 'a' + (i % 26);
ptr[i] = chr;
}
pid = fork();
ASSERT_NE(pid, -1);
if (!pid) {
/* This is the child process now. */
/* Ensure the range is as expected. */
for (i = 0; i < 10 * page_size; i++) {
char expected = 'a' + (i % 26);
char actual = ptr[i];
ASSERT_EQ(actual, expected);
}
/* Establish guard pages across the whole range. */
ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0);
/* Remove it. */
ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_REMOVE), 0);
/*
* By removing the guard pages, the page tables will be
* cleared. Assert that we are looking at the zero page now.
*/
for (i = 0; i < 10 * page_size; i++) {
char actual = ptr[i];
ASSERT_EQ(actual, '\0');
}
exit(0);
}
/* Parent process. */
/* Parent simply waits on child. */
waitpid(pid, NULL, 0);
/* Ensure the range is unchanged in parent anon range. */
for (i = 0; i < 10 * page_size; i++) {
char expected = 'a' + (i % 26);
char actual = ptr[i];
ASSERT_EQ(actual, expected);
}
/* Cleanup. */
ASSERT_EQ(munmap(ptr, 10 * page_size), 0);
}
/*
* Assert that forking a process with VMAs that do have VM_WIPEONFORK set
* behave as expected.

View File

@ -45,6 +45,8 @@ separated by spaces:
vmalloc smoke tests
- hmm
hmm smoke tests
- madv_guard
test madvise(2) MADV_GUARD_INSTALL and MADV_GUARD_REMOVE options
- madv_populate
test memadvise(2) MADV_POPULATE_{READ,WRITE} options
- memfd_secret
@ -375,6 +377,9 @@ CATEGORY="mremap" run_test ./mremap_dontunmap
CATEGORY="hmm" run_test bash ./test_hmm.sh smoke
# MADV_GUARD_INSTALL and MADV_GUARD_REMOVE tests
CATEGORY="madv_guard" run_test ./guard-pages
# MADV_POPULATE_READ and MADV_POPULATE_WRITE tests
CATEGORY="madv_populate" run_test ./madv_populate