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