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