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