Skip to content

Commit 2e53c4e

Browse files
joelagneltorvalds
authored andcommitted
memfd: add test for COW on MAP_PRIVATE and F_SEAL_FUTURE_WRITE mappings
In this test, the parent and child both have writable private mappings. The test shows that without the patch in this series, the parent and child shared the same memory which is incorrect. In other words, COW needs to be triggered so any writes to child's copy stays local to the child. Link: http://lkml.kernel.org/r/20191107195355.80608-2-joel@joelfernandes.org Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org> Cc: Hugh Dickins <hughd@google.com> Cc: Nicolas Geoffray <ngeoffray@google.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 05d3511 commit 2e53c4e

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

‎tools/testing/selftests/memfd/memfd_test.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,40 @@ static void mfd_assert_read_shared(int fd)
290290
munmap(p, mfd_def_size);
291291
}
292292

293+
static void mfd_assert_fork_private_write(int fd)
294+
{
295+
int *p;
296+
pid_t pid;
297+
298+
p = mmap(NULL,
299+
mfd_def_size,
300+
PROT_READ | PROT_WRITE,
301+
MAP_PRIVATE,
302+
fd,
303+
0);
304+
if (p == MAP_FAILED) {
305+
printf("mmap() failed: %m\n");
306+
abort();
307+
}
308+
309+
p[0] = 22;
310+
311+
pid = fork();
312+
if (pid == 0) {
313+
p[0] = 33;
314+
exit(0);
315+
} else {
316+
waitpid(pid, NULL, 0);
317+
318+
if (p[0] != 22) {
319+
printf("MAP_PRIVATE copy-on-write failed: %m\n");
320+
abort();
321+
}
322+
}
323+
324+
munmap(p, mfd_def_size);
325+
}
326+
293327
static void mfd_assert_write(int fd)
294328
{
295329
ssize_t l;
@@ -760,6 +794,8 @@ static void test_seal_future_write(void)
760794
mfd_assert_read_shared(fd2);
761795
mfd_fail_write(fd2);
762796

797+
mfd_assert_fork_private_write(fd);
798+
763799
munmap(p, mfd_def_size);
764800
close(fd2);
765801
close(fd);

0 commit comments

Comments
 (0)