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
write_fully(int fd,void * buf,int len)35 static void write_fully(int fd, void *buf, int len)
36 {
37 int ret;
38
39 do {
40 ret = SAFE_WRITE(0, fd, buf, len);
41 buf += ret;
42 len -= ret;
43 } while (len > 0);
44 }
45
mmapstress04(void)46 static void mmapstress04(void)
47 {
48 int i, j, rofd, rwfd;
49 char *buf;
50 int mapped_pages = 0;
51
52 if (tst_fill_file(TEST_FILE, 'b', page_size, 1))
53 tst_brk(TBROK | TERRNO, "fill_file");
54
55 rofd = SAFE_OPEN(TEST_FILE, O_RDONLY | O_CREAT, 0777);
56 /*
57 * Assuming disk blocks are 8k, and logical pages are 4k, there are
58 * two maps per block. In order to test mapping at the beginning and
59 * ends of the block, mapping the whole block, or none of the block
60 * with different mappings on preceding and following blocks, each
61 * 3 blocks with 6 pages can be thought of as a binary number from 0 to
62 * 64 with a bit set for mapped or cleared for unmapped. This number
63 * is represented by i. The value j is used to look at the bits of i
64 * and decided to map the page or not.
65 * NOTE: None of the above assumptions are critical.
66 */
67 for (i = 0; i < 64; i++) {
68 for (j = 0; j < 6; j++) {
69 off_t mapoff;
70
71 if (!(i & (1 << j)))
72 continue;
73
74 mapoff = page_size * (off_t)(6 * i + j);
75 SAFE_MMAP(mmap_area + page_size * mapped_pages++,
76 page_size, PROT_READ,
77 MAP_FILE | MAP_PRIVATE | MAP_FIXED,
78 rofd, mapoff);
79 }
80 }
81 SAFE_CLOSE(rofd);
82
83 /* write out 6 pages of stuff into each of the 64 six page sections */
84 rwfd = SAFE_OPEN(TEST_FILE, O_RDWR);
85 buf = SAFE_MALLOC(page_size);
86 memset(buf, 'a', page_size);
87 for (i = 0; i < 6 * 64; i++)
88 write_fully(rwfd, buf, page_size);
89 free(buf);
90 SAFE_CLOSE(rwfd);
91
92 /*
93 * Just finished scribbling all over interwoven mmapped and unmapped
94 * regions. Check the data.
95 */
96 for (i = 0; i < mapped_pages * page_size; i++) {
97 unsigned char val = *(mmap_area + i);
98
99 if (val != 'a') {
100 tst_res(TFAIL, "unexpected value in map, "
101 "i=%d,val=0x%x", i, val);
102 goto done;
103 }
104 }
105 tst_res(TPASS, "blocks have expected data");
106 done:
107 SAFE_UNLINK(TEST_FILE);
108 }
109
110 static struct tst_test test = {
111 .needs_tmpdir = 1,
112 .test_all = mmapstress04,
113 .setup = setup,
114 };
115