blob: 928a902443e619ffce4f1a979f86e5708721dcfc [file] [log] [blame]
subrata_modakc79a92d2009-04-02 06:50:45 +00001/******************************************************************************/
2/* */
3/* Copyright (s) Ying Han <yinghan@google.com>, 2009 */
4/* */
5/* This program is free software; you can redistribute it and/or modify */
6/* it under the terms of the GNU General Public License as published by */
7/* the Free Software Foundation; either version 2 of the License, or */
8/* (at your option) any later version. */
9/* */
10/* This program is distributed in the hope that it will be useful, */
11/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13/* the GNU General Public License for more details. */
14/* */
15/* You should have received a copy of the GNU General Public License */
16/* along with this program; if not, write to the Free Software */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modakc79a92d2009-04-02 06:50:45 +000018/* */
19/******************************************************************************/
20/*
21 ftruncate-mmap: pages are lost after writing to mmaped file,
22
23 We triggered the failure during some internal experiment with
24 ftruncate/mmap/write/read sequence. And we found that some pages are
25 "lost" after writing to the mmaped file. which in the following test
26 cases (count >= 0).
Garrett Cooper2c282152010-12-16 00:55:50 -080027
subrata_modakc79a92d2009-04-02 06:50:45 +000028 First we deployed the test cases into group of machines and see about
29 >20% failure rate on average. Then, I did couple of experiment to try
30 to reproduce it on a single machine. what i found is that:
31 1. add a fsync after write the file, i can not reproduce this issue.
32 2. add memory pressure(mmap/mlock) while run the test in infinite
33 loop, the failure is reproduced quickly. ( background flushing ? )
Garrett Cooper2c282152010-12-16 00:55:50 -080034
subrata_modakc79a92d2009-04-02 06:50:45 +000035 The "bad pages" count differs each time from one digit to 4,5 digit
36 for 128M ftruncated file. and what i also found that the bad page
37 number are contiguous for each segment which total bad pages container
38 several segments. ext "1-4, 9-20, 48-50" ( batch flushing ? )
Garrett Cooper2c282152010-12-16 00:55:50 -080039
subrata_modakc79a92d2009-04-02 06:50:45 +000040 (The failure is reproduced based on 2.6.29-rc8, also happened on
41 2.6.18 kernel. . Here is the simple test case to reproduce it with
42 memory pressure. )
43*/
44
45#include <sys/mman.h>
46#include <sys/types.h>
47#include <fcntl.h>
48#include <unistd.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <signal.h>
53
subrata_modakc79a92d2009-04-02 06:50:45 +000054#include "test.h"
55#include "usctest.h"
56
57/* Extern Global Variables */
Caspar Zhangd59a6592013-03-07 14:59:12 +080058extern int tst_count;
subrata_modakc79a92d2009-04-02 06:50:45 +000059
60/* Global Variables */
Wanlong Gao354ebb42012-12-07 10:10:04 +080061char *TCID = "mmap-corruption01"; /* test program identifier. */
62int TST_TOTAL = 1; /* total number of tests in this file. */
subrata_modakc79a92d2009-04-02 06:50:45 +000063
Wanlong Gao354ebb42012-12-07 10:10:04 +080064long kMemSize = 128 << 20;
subrata_modakc79a92d2009-04-02 06:50:45 +000065int kPageSize = 4096;
66
Wanlong Gao354ebb42012-12-07 10:10:04 +080067char *usage = "-h hours -m minutes -s secs\n";
subrata_modakc79a92d2009-04-02 06:50:45 +000068
69int anyfail()
70{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +010071 tst_brkm(TFAIL, tst_rmdir, "Test failed\n");
subrata_modakc79a92d2009-04-02 06:50:45 +000072}
73
Wanlong Gao354ebb42012-12-07 10:10:04 +080074int main(int argc, char **argv)
75{
76 char *progname;
77 int status;
78 int count = 0;
79 int i, c;
80 char *fname = "test.mmap-corruption";
81 char *mem;
82 unsigned long alarmtime = 0;
83 struct sigaction sa;
84 void finish(int sig);
subrata_modakc79a92d2009-04-02 06:50:45 +000085
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 progname = *argv;
87 while ((c = getopt(argc, argv, ":h:m:s:")) != -1) {
88 switch (c) {
89 case 'h':
90 alarmtime += atoi(optarg) * 60 * 60;
91 break;
92 case 'm':
93 alarmtime += atoi(optarg) * 60;
94 break;
95 case 's':
96 alarmtime += atoi(optarg);
97 break;
98 default:
99 (void)fprintf(stderr, "usage: %s %s\n", progname,
100 usage);
101 anyfail();
102 }
103 }
subrata_modakc79a92d2009-04-02 06:50:45 +0000104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 /*
106 * Plan for death by signal. User may have specified
107 * a time limit, in which case set an alarm and catch SIGALRM.
108 * Also catch and cleanup with SIGINT, SIGQUIT, and SIGTERM.
109 */
110 sa.sa_handler = finish;
111 sa.sa_flags = 0;
112 if (sigemptyset(&sa.sa_mask)) {
113 perror("sigempty error");
114 exit(1);
115 }
subrata_modakc79a92d2009-04-02 06:50:45 +0000116
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 if (sigaction(SIGINT, &sa, 0) == -1) {
118 perror("sigaction error SIGINT");
119 exit(1);
120 }
121 if (alarmtime) {
122 if (sigaction(SIGALRM, &sa, 0) == -1) {
123 perror("sigaction error");
124 exit(1);
125 }
126 (void)alarm(alarmtime);
127 printf("mmap-corruption will run for=> %ld, seconds\n",
128 alarmtime);
129 } else { //Run for 5 secs only
130 if (sigaction(SIGALRM, &sa, 0) == -1) {
131 perror("sigaction error");
132 exit(1);
133 }
134 (void)alarm(5);
135 printf("mmap-corruption will run for=> 5, seconds\n");
136 }
137 /* If we get a SIGQUIT or SIGTERM, clean up and exit immediately. */
138 sa.sa_handler = finish;
139 if (sigaction(SIGQUIT, &sa, 0) == -1) {
140 perror("sigaction error SIGQUIT");
141 exit(1);
142 }
143 if (sigaction(SIGTERM, &sa, 0) == -1) {
144 perror("sigaction error SIGTERM");
145 exit(1);
146 }
subrata_modakc79a92d2009-04-02 06:50:45 +0000147
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 tst_tmpdir();
149 while (1) {
150 unlink(fname);
151 int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
152 status = ftruncate(fd, kMemSize);
subrata_modakc79a92d2009-04-02 06:50:45 +0000153
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 mem =
155 mmap(0, kMemSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
156 0);
157 // Fill the memory with 1s.
158 memset(mem, 1, kMemSize);
subrata_modakc79a92d2009-04-02 06:50:45 +0000159
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 for (i = 0; i < kMemSize; i++) {
161 int byte_good = mem[i] != 0;
162 if (!byte_good && ((i % kPageSize) == 0)) {
163 //printf("%d ", i / kPageSize);
164 count++;
165 }
166 }
167 munmap(mem, kMemSize);
168 close(fd);
169 unlink(fname);
170 if (count > 0) {
171 printf("Running %d bad page\n", count);
172 return 1;
173 }
174 count = 0;
175 }
176 return 0;
subrata_modakc79a92d2009-04-02 06:50:45 +0000177}
178
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179void finish(int sig)
180{
181 printf("mmap-corruption PASSED\n");
182 exit(0);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700183}