Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Joel Fernandes (Google) and committed by
Linus Torvalds
2e53c4e1 05d35110

+36
+36
tools/testing/selftests/memfd/memfd_test.c
··· 290 290 munmap(p, mfd_def_size); 291 291 } 292 292 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 + 293 327 static void mfd_assert_write(int fd) 294 328 { 295 329 ssize_t l; ··· 793 759 mfd_assert_read(fd2); 794 760 mfd_assert_read_shared(fd2); 795 761 mfd_fail_write(fd2); 762 + 763 + mfd_assert_fork_private_write(fd); 796 764 797 765 munmap(p, mfd_def_size); 798 766 close(fd2);