linux/tools/testing/selftests
Peter Xu 8913970c19 mm/userfaultfd: selftests: fix memory corruption with thp enabled
In RHEL's gating selftests we've encountered memory corruption in the
uffd event test even with upstream kernel:

        # ./userfaultfd anon 128 4
        nr_pages: 32768, nr_pages_per_cpu: 32768
        bounces: 3, mode: rnd racing read, userfaults: 6240 missing (6240) 14729 wp (14729)
        bounces: 2, mode: racing read, userfaults: 1444 missing (1444) 28877 wp (28877)
        bounces: 1, mode: rnd read, userfaults: 6055 missing (6055) 14699 wp (14699)
        bounces: 0, mode: read, userfaults: 82 missing (82) 25196 wp (25196)
        testing uffd-wp with pagemap (pgsize=4096): done
        testing uffd-wp with pagemap (pgsize=2097152): done
        testing events (fork, remap, remove): ERROR: nr 32427 memory corruption 0 1 (errno=0, line=963)
        ERROR: faulting process failed (errno=0, line=1117)

It can be easily reproduced when global thp enabled, which is the
default for RHEL.

It's also known as a side effect of commit 0db282ba2c ("selftest: use
mmap instead of posix_memalign to allocate memory", 2021-07-23), which
is imho right itself on using mmap() to make sure the addresses will be
untagged even on arm.

The problem is, for each test we allocate buffers using two
allocate_area() calls.  We assumed these two buffers won't affect each
other, however they could, because mmap() could have found that the two
buffers are near each other and having the same VMA flags, so they got
merged into one VMA.

It won't be a big problem if thp is not enabled, but when thp is
agressively enabled it means when initializing the src buffer it could
accidentally setup part of the dest buffer too when there's a shared THP
that overlaps the two regions.  Then some of the dest buffer won't be
able to be trapped by userfaultfd missing mode, then it'll cause memory
corruption as described.

To fix it, do release_pages() after initializing the src buffer.

Since the previous two release_pages() calls are after
uffd_test_ctx_clear() which will unmap all the buffers anyway (which is
stronger than release pages; as unmap() also tear town pgtables), drop
them as they shouldn't really be anything useful.

We can mark the Fixes tag upon 0db282ba2c as it's reported to only
happen there, however the real "Fixes" IMHO should be 8ba6e86408, as
before that commit we'll always do explicit release_pages() before
registration of uffd, and 8ba6e86408 changed that logic by adding
extra unmap/map and we didn't release the pages at the right place.
Meanwhile I don't have a solid glue anyway on whether posix_memalign()
could always avoid triggering this bug, hence it's safer to attach this
fix to commit 8ba6e86408.

Link: https://lkml.kernel.org/r/20210923232512.210092-1-peterx@redhat.com
Fixes: 8ba6e86408 ("userfaultfd/selftests: reinitialize test context in each test")
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1994931
Signed-off-by: Peter Xu <peterx@redhat.com>
Reported-by: Li Wang <liwan@redhat.com>
Tested-by: Li Wang <liwang@redhat.com>
Reviewed-by: Axel Rasmussen <axelrasmussen@google.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Nadav Amit <nadav.amit@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-10-18 20:22:02 -10:00
..
arm64 kselftest/arm64: signal: Skip tests if required features are missing 2021-09-21 18:12:03 +01:00
bpf selftests, bpf: test_lwt_ip_encap: Really disable rp_filter 2021-09-28 09:30:38 +02:00
breakpoints selftests: breakpoints: Use correct error messages in breakpoint_test_arm64.c 2021-02-08 17:04:41 -07:00
capabilities
cgroup tests/cgroup: test cgroup.kill 2021-05-10 10:41:11 -04:00
clone3 selftests/clone3: Fix build error 2020-12-07 14:34:55 -07:00
core selftests/core: add regression test for CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC 2020-12-19 16:23:19 +01:00
cpu-hotplug
cpufreq selftests/cpufreq: Rename DEBUG_PI_LIST to DEBUG_PLIST 2021-08-31 11:00:02 -06:00
damon mm/damon: add user space selftests 2021-09-08 11:50:25 -07:00
dma dma-mapping: benchmark: Add support for multi-pages map/unmap 2021-04-02 16:41:08 +02:00
dmabuf-heaps kselftests: dmabuf-heaps: Add extra checking that allocated buffers are zeroed 2021-02-08 16:25:53 -07:00
drivers linux-kselftest-fixes-5.15-rc5 2021-10-04 14:33:30 -07:00
efivarfs
exec tools/testing/selftests/exec: fix link error 2021-05-22 15:09:07 -10:00
filesystems selftests/binderfs: add test for feature files 2021-07-21 13:46:36 +02:00
firmware selftests: firmware: Fix ignored return val of asprintf() warn 2021-07-21 16:11:42 +02:00
fpu
ftrace selftests/ftrace: Update test for more eprobe removal process 2021-10-13 19:27:53 -04:00
futex selftests: futex: Add futex compare requeue test 2021-06-22 11:20:16 +02:00
gpio selftests: gpio: update .gitignore 2021-03-08 11:59:16 +01:00
ia64
intel_pstate
ipc selftests/ipc: remove unneeded semicolon 2021-02-08 16:32:43 -07:00
ir
kcmp
kexec
kmod
kselftest tweewide: Fix most Shebang lines 2020-12-08 23:30:04 +09:00
kvm Small x86 fixes. 2021-10-01 11:08:07 -07:00
landlock landlock: Enable user space to infer supported features 2021-04-22 12:22:11 -07:00
lib selftests: lib: Add wrapper script for test_scanf 2021-05-19 15:05:11 +02:00
livepatch
lkdtm lkdtm/fortify: Consolidate FORTIFY_SOURCE tests 2021-08-18 22:28:51 +02:00
locking
media_tests
membarrier
memfd selftests/memfd: remove unused variable 2021-09-08 11:50:28 -07:00
memory-hotplug selftests: memory-hotplug: avoid spamming logs with dump_page(), ratio limit hot-remove error test 2021-07-12 14:20:01 -06:00
mincore selftests: remove duplicate include 2021-05-07 00:26:33 -07:00
mount
mount_setattr tests: test MOUNT_ATTR_NOSYMFOLLOW with mount_setattr() 2021-06-01 15:06:51 +02:00
move_mount_set_group tests: add move_mount(MOVE_MOUNT_SET_GROUP) selftest 2021-07-26 14:45:19 +02:00
mqueue
nci selftests: nci: replace unsigned int with int 2021-09-16 13:55:51 +01:00
net selftests: net: modify IOAM tests for undef bits 2021-10-12 11:49:49 +01:00
netfilter selftests: netfilter: add zone stress test with colliding tuples 2021-09-21 03:46:55 +02:00
nsfs
ntb
openat2 selftests: openat2: Fix testing failure for O_LARGEFILE flag 2021-08-25 13:46:13 -06:00
perf_events signal: Deliver all of the siginfo perf data in _perf 2021-05-18 16:20:54 -05:00
pid_namespace
pidfd
powerpc selftests/powerpc: Add scv versions of the basic TM syscall tests 2021-09-13 22:34:11 +10:00
prctl
proc proc: add .gitignore for proc-subset-pid selftest 2021-06-05 08:58:11 -07:00
pstore
ptp
ptrace
rcutorture torture: Make kvm-test-1-run-qemu.sh check for reboot loops 2021-07-27 11:41:33 -07:00
resctrl selftests/resctrl: Fix incorrect parsing of option "-t" 2021-06-07 18:38:58 -06:00
rlimits kselftests: Add test to check for rlimit changes in different user namespaces 2021-04-30 14:14:03 -05:00
rseq rseq/selftests: Fix MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ build error under other arch. 2020-12-07 14:09:45 -07:00
rtc
safesetid selftests: safesetid: Fix spelling mistake "cant" -> "can't" 2021-08-26 15:15:24 -06:00
sched kselftest:sched: remove duplicate include in cs_prctl_test.c 2021-08-25 13:55:30 -06:00
seccomp seccomp updates for v5.14-rc1 2021-06-28 19:49:37 -07:00
sgx selftests/sgx: Fix Q1 and Q2 calculation in sigstruct.c 2021-07-30 17:20:01 -06:00
sigaltstack selftest/sigaltstack: Use the AT_MINSIGSTKSZ aux vector if available 2021-05-19 12:38:17 +02:00
size
sparc64
splice selftests: splice: Adjust for handler fallback removal 2021-06-07 18:39:43 -06:00
static_keys
sync selftests/sync: Remove the deprecated config SYNC 2021-08-31 10:58:00 -06:00
syscall_user_dispatch entry: Use different define for selector variable in SUD 2021-02-06 00:21:42 +01:00
sysctl
tc-testing tc-testing: Add control-plane selftests for sch_mq 2021-08-04 12:42:27 +01:00
timens selftests/timens: Fix gettime_perf to work on powerpc 2021-04-21 22:52:32 +10:00
timers selftests: timers: rtcpie: skip test if default RTC device does not exist 2021-06-07 19:18:52 -06:00
tmpfs
tpm2
uevent
user
vDSO selftests/vDSO: fix ABI selftest on riscv 2021-02-08 16:38:34 -07:00
vm mm/userfaultfd: selftests: fix memory corruption with thp enabled 2021-10-18 20:22:02 -10:00
watchdog
wireguard wireguard: selftests: make sure rp_filter is disabled on vethc 2021-06-04 14:25:14 -07:00
x86 selftests/x86: Fix error: variably modified 'altstack_data' at file scope 2021-08-25 16:54:39 -06:00
zram
.gitignore
gen_kselftest_tar.sh
kselftest.h
kselftest_deps.sh selftests: remove obsolete gpio references from kselftest_deps.sh 2021-02-15 11:43:28 +01:00
kselftest_harness.h selftests: kselftest_harness.h: partially fix kernel-doc markups 2021-01-21 14:06:00 -07:00
kselftest_install.sh
kselftest_module.h kselftest: add support for skipped tests 2021-02-15 11:07:42 +01:00
lib.mk selftests: be sure to make khdr before other targets 2021-09-15 10:34:21 -06:00
Makefile Core: 2021-08-31 16:43:06 -07:00
run_kselftest.sh