• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Red Hat, Inc.
4  * 01/02/2003	Port to LTP avenkat@us.ibm.com
5  * 06/30/2001	Port to Linux	nsharoff@us.ibm.com
6  */
7 /*
8  * Mmap parts of a file, and then write to the file causing single
9  * write requests to jump back and forth between mmaped io and regular io.
10  */
11 
12 #define _GNU_SOURCE
13 #include <stdlib.h>
14 #include "tst_test.h"
15 #include "tst_safe_macros.h"
16 
17 #define NUM_PAGES (192)
18 #define TEST_FILE "mmapstress04-testfile"
19 
20 static int page_size;
21 static unsigned char *mmap_area;
22 
setup(void)23 static void setup(void)
24 {
25 	page_size = getpagesize();
26 
27 	/*
28 	 * Pick large enough area, PROT_NONE doesn't matter,
29 	 * because we remap it later.
30 	 */
31 	mmap_area = SAFE_MMAP(NULL, page_size * NUM_PAGES, PROT_NONE,
32 		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
33 }
34 
mmapstress04(void)35 static void mmapstress04(void)
36 {
37 	int i, j, rofd, rwfd;
38 	char *buf;
39 	int mapped_pages = 0;
40 
41 	if (tst_fill_file(TEST_FILE, 'b', page_size, 1))
42 		tst_brk(TBROK | TERRNO, "fill_file");
43 
44 	rofd = SAFE_OPEN(TEST_FILE, O_RDONLY | O_CREAT, 0777);
45 	/*
46 	 * Assuming disk blocks are 8k, and logical pages are 4k, there are
47 	 * two maps per block. In order to test mapping at the beginning and
48 	 * ends of the block, mapping the whole block, or none of the block
49 	 * with different mappings on preceding and following blocks, each
50 	 * 3 blocks with 6 pages can be thought of as a binary number from 0 to
51 	 * 64 with a bit set for mapped or cleared for unmapped. This number
52 	 * is represented by i. The value j is used to look at the bits of i
53 	 * and decided to map the page or not.
54 	 * NOTE: None of the above assumptions are critical.
55 	 */
56 	for (i = 0; i < 64; i++) {
57 		for (j = 0; j < 6; j++) {
58 			off_t mapoff;
59 
60 			if (!(i & (1 << j)))
61 				continue;
62 
63 			mapoff = page_size * (off_t)(6 * i + j);
64 			SAFE_MMAP(mmap_area + page_size * mapped_pages++,
65 				 page_size, PROT_READ,
66 				 MAP_FILE | MAP_PRIVATE | MAP_FIXED,
67 				 rofd, mapoff);
68 		}
69 	}
70 	SAFE_CLOSE(rofd);
71 
72 	/* write out 6 pages of stuff into each of the 64 six page sections */
73 	rwfd = SAFE_OPEN(TEST_FILE, O_RDWR);
74 	buf = SAFE_MALLOC(page_size);
75 	memset(buf, 'a', page_size);
76 	for (i = 0; i < 6 * 64; i++)
77 		SAFE_WRITE(SAFE_WRITE_RETRY, rwfd, buf, page_size);
78 	free(buf);
79 	SAFE_CLOSE(rwfd);
80 
81 	/*
82 	 * Just finished scribbling all over interwoven mmapped and unmapped
83 	 * regions. Check the data.
84 	 */
85 	for (i = 0; i < mapped_pages * page_size; i++) {
86 		unsigned char val = *(mmap_area + i);
87 
88 		if (val != 'a') {
89 			tst_res(TFAIL, "unexpected value in map, "
90 				"i=%d,val=0x%x", i, val);
91 			goto done;
92 		}
93 	}
94 	tst_res(TPASS, "blocks have expected data");
95 done:
96 	SAFE_UNLINK(TEST_FILE);
97 }
98 
99 static struct tst_test test = {
100 	.needs_tmpdir = 1,
101 	.test_all = mmapstress04,
102 	.setup = setup,
103 };
104