1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2018 Jan Stancek. All rights reserved. 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci/* 6f08c3bdfSopenharmony_ci * Test: Spawn 2 threads. First thread maps, writes and unmaps 7f08c3bdfSopenharmony_ci * an area. Second thread tries to read from it. Second thread 8f08c3bdfSopenharmony_ci * races against first thread. There is no synchronization 9f08c3bdfSopenharmony_ci * between threads, but each mmap/munmap increases a counter 10f08c3bdfSopenharmony_ci * that is checked to determine when has read occurred. If a read 11f08c3bdfSopenharmony_ci * hit SIGSEGV in between mmap/munmap it is a failure. If a read 12f08c3bdfSopenharmony_ci * between mmap/munmap worked, then its value must match expected 13f08c3bdfSopenharmony_ci * value. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Can trigger panics/stalls since at least 4.14 on some arches: 16f08c3bdfSopenharmony_ci * fc8efd2ddfed ("mm/memory.c: do_fault: avoid usage of stale vm_area_struct") 17f08c3bdfSopenharmony_ci * Can trigger user-space stalls on aarch64: 18f08c3bdfSopenharmony_ci * 7a30df49f63a ("mm: mmu_gather: remove __tlb_reset_range() for force flush") 19f08c3bdfSopenharmony_ci * https://lore.kernel.org/linux-mm/1817839533.20996552.1557065445233.JavaMail.zimbra@redhat.com 20f08c3bdfSopenharmony_ci * Can trigger "still mapped when deleted" BUG at mm/filemap.c:171, on aarch64 since 4.20 21f08c3bdfSopenharmony_ci * e1b98fa31664 ("locking/rwsem: Add missing ACQUIRE to read_slowpath exit when queue is empty") 22f08c3bdfSopenharmony_ci * 99143f82a255 ("lcoking/rwsem: Add missing ACQUIRE to read_slowpath sleep loop") 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci#include <errno.h> 25f08c3bdfSopenharmony_ci#include <float.h> 26f08c3bdfSopenharmony_ci#include <pthread.h> 27f08c3bdfSopenharmony_ci#include <sched.h> 28f08c3bdfSopenharmony_ci#include <setjmp.h> 29f08c3bdfSopenharmony_ci#include <stdio.h> 30f08c3bdfSopenharmony_ci#include <stdlib.h> 31f08c3bdfSopenharmony_ci#include "lapi/abisize.h" 32f08c3bdfSopenharmony_ci#include "tst_test.h" 33f08c3bdfSopenharmony_ci#include "tst_safe_pthread.h" 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#define GIGABYTE (1L*1024*1024*1024) 36f08c3bdfSopenharmony_ci#define TEST_FILENAME "ashfile" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci/* seconds remaining before reaching timeout */ 39f08c3bdfSopenharmony_ci#define STOP_THRESHOLD 10 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci#define PROGRESS_SEC 3 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_cistatic int file_size = 1024; 44f08c3bdfSopenharmony_cistatic int num_iter = 5000; 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_cistatic void *distant_area; 47f08c3bdfSopenharmony_cistatic jmp_buf jmpbuf; 48f08c3bdfSopenharmony_cistatic volatile unsigned char *map_address; 49f08c3bdfSopenharmony_cistatic unsigned long page_sz; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic unsigned long mapped_sigsegv_count; 52f08c3bdfSopenharmony_cistatic unsigned long map_count; 53f08c3bdfSopenharmony_cistatic unsigned long threads_spawned; 54f08c3bdfSopenharmony_cistatic unsigned long data_matched; 55f08c3bdfSopenharmony_cistatic unsigned long repeated_reads; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci/* sequence id for each map/unmap performed */ 58f08c3bdfSopenharmony_cistatic int mapcnt, unmapcnt; 59f08c3bdfSopenharmony_ci/* stored sequence id before making read attempt */ 60f08c3bdfSopenharmony_cistatic int br_map, br_unmap; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci/* compare "before read" counters with "after read" counters */ 63f08c3bdfSopenharmony_cistatic inline int was_area_mapped(int br_m, int br_u, int ar_m, int ar_u) 64f08c3bdfSopenharmony_ci{ 65f08c3bdfSopenharmony_ci return (br_m == ar_m && br_u == ar_u && br_m > br_u); 66f08c3bdfSopenharmony_ci} 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cistatic void sig_handler(int signal, siginfo_t *info, 69f08c3bdfSopenharmony_ci LTP_ATTRIBUTE_UNUSED void *ut) 70f08c3bdfSopenharmony_ci{ 71f08c3bdfSopenharmony_ci int ar_m, ar_u; 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci switch (signal) { 74f08c3bdfSopenharmony_ci case SIGSEGV: 75f08c3bdfSopenharmony_ci /* if we hit SIGSEGV between map/unmap, something is wrong */ 76f08c3bdfSopenharmony_ci ar_u = tst_atomic_load(&unmapcnt); 77f08c3bdfSopenharmony_ci ar_m = tst_atomic_load(&mapcnt); 78f08c3bdfSopenharmony_ci if (was_area_mapped(br_map, br_unmap, ar_m, ar_u)) { 79f08c3bdfSopenharmony_ci tst_res(TFAIL, "got sigsegv while mapped"); 80f08c3bdfSopenharmony_ci _exit(TFAIL); 81f08c3bdfSopenharmony_ci } 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci mapped_sigsegv_count++; 84f08c3bdfSopenharmony_ci longjmp(jmpbuf, 1); 85f08c3bdfSopenharmony_ci break; 86f08c3bdfSopenharmony_ci default: 87f08c3bdfSopenharmony_ci tst_res(TFAIL, "Unexpected signal - %d, addr: %p, exiting", 88f08c3bdfSopenharmony_ci signal, info->si_addr); 89f08c3bdfSopenharmony_ci _exit(TBROK); 90f08c3bdfSopenharmony_ci } 91f08c3bdfSopenharmony_ci} 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_civoid *map_write_unmap(void *ptr) 94f08c3bdfSopenharmony_ci{ 95f08c3bdfSopenharmony_ci int *fd = ptr; 96f08c3bdfSopenharmony_ci void *tmp; 97f08c3bdfSopenharmony_ci int i, j; 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci for (i = 0; i < num_iter; i++) { 100f08c3bdfSopenharmony_ci map_address = SAFE_MMAP(distant_area, 101f08c3bdfSopenharmony_ci (size_t) file_size, PROT_WRITE | PROT_READ, 102f08c3bdfSopenharmony_ci MAP_SHARED, *fd, 0); 103f08c3bdfSopenharmony_ci tst_atomic_inc(&mapcnt); 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci for (j = 0; j < file_size; j++) 106f08c3bdfSopenharmony_ci map_address[j] = 'b'; 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci tmp = (void *)map_address; 109f08c3bdfSopenharmony_ci tst_atomic_inc(&unmapcnt); 110f08c3bdfSopenharmony_ci SAFE_MUNMAP(tmp, file_size); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci map_count++; 113f08c3bdfSopenharmony_ci } 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci return NULL; 116f08c3bdfSopenharmony_ci} 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_civoid *read_mem(LTP_ATTRIBUTE_UNUSED void *ptr) 119f08c3bdfSopenharmony_ci{ 120f08c3bdfSopenharmony_ci volatile int i; /* longjmp could clobber i */ 121f08c3bdfSopenharmony_ci int j, ar_map, ar_unmap; 122f08c3bdfSopenharmony_ci unsigned char c; 123f08c3bdfSopenharmony_ci 124f08c3bdfSopenharmony_ci for (i = 0; i < num_iter; i++) { 125f08c3bdfSopenharmony_ci if (setjmp(jmpbuf) == 1) 126f08c3bdfSopenharmony_ci continue; 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_ci for (j = 0; j < file_size; j++) { 129f08c3bdfSopenharmony_ciread_again: 130f08c3bdfSopenharmony_ci br_map = tst_atomic_load(&mapcnt); 131f08c3bdfSopenharmony_ci br_unmap = tst_atomic_load(&unmapcnt); 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci c = map_address[j]; 134f08c3bdfSopenharmony_ci 135f08c3bdfSopenharmony_ci ar_unmap = tst_atomic_load(&unmapcnt); 136f08c3bdfSopenharmony_ci ar_map = tst_atomic_load(&mapcnt); 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ci /* 139f08c3bdfSopenharmony_ci * Read above is racing against munmap and mmap 140f08c3bdfSopenharmony_ci * in other thread. While the address might be valid 141f08c3bdfSopenharmony_ci * the mapping could be in various stages of being 142f08c3bdfSopenharmony_ci * 'ready'. We only check the value, if we can be sure 143f08c3bdfSopenharmony_ci * read hapenned in between single mmap and munmap as 144f08c3bdfSopenharmony_ci * observed by first thread. 145f08c3bdfSopenharmony_ci */ 146f08c3bdfSopenharmony_ci if (was_area_mapped(br_map, br_unmap, ar_map, 147f08c3bdfSopenharmony_ci ar_unmap)) { 148f08c3bdfSopenharmony_ci switch (c) { 149f08c3bdfSopenharmony_ci case 'a': 150f08c3bdfSopenharmony_ci repeated_reads++; 151f08c3bdfSopenharmony_ci goto read_again; 152f08c3bdfSopenharmony_ci case 'b': 153f08c3bdfSopenharmony_ci data_matched++; 154f08c3bdfSopenharmony_ci break; 155f08c3bdfSopenharmony_ci default: 156f08c3bdfSopenharmony_ci tst_res(TFAIL, "value[%d] is %c", j, c); 157f08c3bdfSopenharmony_ci break; 158f08c3bdfSopenharmony_ci } 159f08c3bdfSopenharmony_ci } 160f08c3bdfSopenharmony_ci } 161f08c3bdfSopenharmony_ci } 162f08c3bdfSopenharmony_ci 163f08c3bdfSopenharmony_ci return NULL; 164f08c3bdfSopenharmony_ci} 165f08c3bdfSopenharmony_ci 166f08c3bdfSopenharmony_ciint mkfile(int size) 167f08c3bdfSopenharmony_ci{ 168f08c3bdfSopenharmony_ci int fd, i; 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TEST_FILENAME, O_RDWR | O_CREAT, 0600); 171f08c3bdfSopenharmony_ci SAFE_UNLINK(TEST_FILENAME); 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci for (i = 0; i < size; i++) 174f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, "a", 1); 175f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1); 176f08c3bdfSopenharmony_ci 177f08c3bdfSopenharmony_ci if (fsync(fd) == -1) 178f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "fsync()"); 179f08c3bdfSopenharmony_ci 180f08c3bdfSopenharmony_ci return fd; 181f08c3bdfSopenharmony_ci} 182f08c3bdfSopenharmony_ci 183f08c3bdfSopenharmony_cistatic void setup(void) 184f08c3bdfSopenharmony_ci{ 185f08c3bdfSopenharmony_ci struct sigaction sigptr; 186f08c3bdfSopenharmony_ci size_t distant_mmap_size; 187f08c3bdfSopenharmony_ci size_t mem_total; 188f08c3bdfSopenharmony_ci 189f08c3bdfSopenharmony_ci page_sz = getpagesize(); 190f08c3bdfSopenharmony_ci mem_total = SAFE_READ_MEMINFO("MemTotal:"); 191f08c3bdfSopenharmony_ci mem_total *= 1024; 192f08c3bdfSopenharmony_ci 193f08c3bdfSopenharmony_ci#ifdef TST_ABI32 194f08c3bdfSopenharmony_ci distant_mmap_size = 256*1024*1024; 195f08c3bdfSopenharmony_ci#else 196f08c3bdfSopenharmony_ci distant_mmap_size = (mem_total > 4 * GIGABYTE) ? 2 * GIGABYTE : mem_total / 2; 197f08c3bdfSopenharmony_ci#endif 198f08c3bdfSopenharmony_ci /* 199f08c3bdfSopenharmony_ci * Used as hint for mmap thread, so it doesn't interfere 200f08c3bdfSopenharmony_ci * with other potential (temporary) mappings from libc 201f08c3bdfSopenharmony_ci */ 202f08c3bdfSopenharmony_ci distant_area = SAFE_MMAP(0, distant_mmap_size, PROT_WRITE | PROT_READ, 203f08c3bdfSopenharmony_ci MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 204f08c3bdfSopenharmony_ci SAFE_MUNMAP(distant_area, distant_mmap_size); 205f08c3bdfSopenharmony_ci distant_area += distant_mmap_size / 2; 206f08c3bdfSopenharmony_ci 207f08c3bdfSopenharmony_ci sigptr.sa_sigaction = sig_handler; 208f08c3bdfSopenharmony_ci sigemptyset(&sigptr.sa_mask); 209f08c3bdfSopenharmony_ci sigptr.sa_flags = SA_SIGINFO | SA_NODEFER; 210f08c3bdfSopenharmony_ci SAFE_SIGACTION(SIGSEGV, &sigptr, NULL); 211f08c3bdfSopenharmony_ci} 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_cistatic void run(void) 214f08c3bdfSopenharmony_ci{ 215f08c3bdfSopenharmony_ci pthread_t thid[2]; 216f08c3bdfSopenharmony_ci int start, last_update; 217f08c3bdfSopenharmony_ci 218f08c3bdfSopenharmony_ci start = last_update = tst_remaining_runtime(); 219f08c3bdfSopenharmony_ci while (tst_remaining_runtime()) { 220f08c3bdfSopenharmony_ci int fd = mkfile(file_size); 221f08c3bdfSopenharmony_ci 222f08c3bdfSopenharmony_ci tst_atomic_store(0, &mapcnt); 223f08c3bdfSopenharmony_ci tst_atomic_store(0, &unmapcnt); 224f08c3bdfSopenharmony_ci 225f08c3bdfSopenharmony_ci SAFE_PTHREAD_CREATE(&thid[0], NULL, map_write_unmap, &fd); 226f08c3bdfSopenharmony_ci SAFE_PTHREAD_CREATE(&thid[1], NULL, read_mem, &fd); 227f08c3bdfSopenharmony_ci threads_spawned += 2; 228f08c3bdfSopenharmony_ci 229f08c3bdfSopenharmony_ci SAFE_PTHREAD_JOIN(thid[0], NULL); 230f08c3bdfSopenharmony_ci SAFE_PTHREAD_JOIN(thid[1], NULL); 231f08c3bdfSopenharmony_ci 232f08c3bdfSopenharmony_ci close(fd); 233f08c3bdfSopenharmony_ci 234f08c3bdfSopenharmony_ci if (last_update - tst_remaining_runtime() >= PROGRESS_SEC) { 235f08c3bdfSopenharmony_ci last_update = tst_remaining_runtime(); 236f08c3bdfSopenharmony_ci tst_res(TINFO, "[%03d] mapped: %lu, sigsegv hit: %lu, " 237f08c3bdfSopenharmony_ci "threads spawned: %lu", 238f08c3bdfSopenharmony_ci start - last_update, 239f08c3bdfSopenharmony_ci map_count, mapped_sigsegv_count, 240f08c3bdfSopenharmony_ci threads_spawned); 241f08c3bdfSopenharmony_ci tst_res(TINFO, " repeated_reads: %ld, " 242f08c3bdfSopenharmony_ci "data_matched: %lu", repeated_reads, 243f08c3bdfSopenharmony_ci data_matched); 244f08c3bdfSopenharmony_ci } 245f08c3bdfSopenharmony_ci } 246f08c3bdfSopenharmony_ci tst_res(TPASS, "System survived."); 247f08c3bdfSopenharmony_ci} 248f08c3bdfSopenharmony_ci 249f08c3bdfSopenharmony_cistatic struct tst_test test = { 250f08c3bdfSopenharmony_ci .test_all = run, 251f08c3bdfSopenharmony_ci .setup = setup, 252f08c3bdfSopenharmony_ci .max_runtime = 180, 253f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 254f08c3bdfSopenharmony_ci}; 255