subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Ingo Molnar, 2002 |
| 3 | * Copyright (C) Ricardo Salveti de Araujo, 2007 |
| 4 | * Copyright (C) International Business Machines Corp., 2007 |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * Further, this software is distributed without any warranty that it is |
| 15 | * free of the rightful claim of any third person regarding infringement |
| 16 | * or the like. Any license provided herein, whether implied or |
| 17 | * otherwise, applies only to this software file. Patent licenses, if |
| 18 | * any, provided herein do not apply to combinations of this program with |
| 19 | * other software, or any other product whatsoever. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 22 | * with this program; if not, write the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * NAME |
| 28 | * remap_file_pages01 |
| 29 | * |
| 30 | * DESCRIPTION |
| 31 | * The remap_file_pages() system call is used to create a non-linear |
| 32 | * mapping, that is, a mapping in which the pages of the file are mapped |
| 33 | * into a non-sequential order in memory. The advantage of using |
| 34 | * remap_file_pages() over using repeated calls to mmap(2) is that |
| 35 | * the former approach does not require the kernel to create |
| 36 | * additional VMA (Virtual Memory Area) data structures. |
| 37 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 38 | * Runs remap_file_pages agains a mmaped area and check the results |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 39 | * |
| 40 | * Setup: |
| 41 | * Create a temp directory, open a file and get the file descriptor |
| 42 | * |
| 43 | * Test: |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 44 | * Test with a normal file and with /dev/shm/cache_<pid> |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 45 | * 1. Set up the cache |
| 46 | * 2. Write the cache to the file |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 47 | * 3. Runs mmap at the same file |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 48 | * 4. Runs remap_file_pages at the mapped memory |
| 49 | * 5. Check the results |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | * $ |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 51 | * Cleanup: |
| 52 | * Remove the file and erase the tmp directory |
| 53 | * |
| 54 | * Usage: <for command-line> |
| 55 | * remap_file_pages01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 56 | * where, -c n : Run n copies concurrently. |
| 57 | * -f : Turn off functionality Testing. |
| 58 | * -i n : Execute test n times. |
| 59 | * -I x : Execute test for x seconds. |
| 60 | * -P x : Pause for x seconds between iterations. |
| 61 | * -t : Turn on syscall timing. |
| 62 | * |
| 63 | * HISTORY |
| 64 | * - Ingo Molnar, <mingo@elte.hu> wrote this test case |
| 65 | * - Nick Piggin, <nickpiggin@yahoo.com.au> did the following cleanup |
| 66 | * |
| 67 | * 11/10/2007 - Port to LTP format by Subrata Modak, <subrata@linux.vnet.ibm.com> |
| 68 | * and Ricardo Salveti de Araujo, <rsalveti@linux.vnet.ibm.com> |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 69 | * 25/02/2008 - Renaud Lottiaux, <Renaud.Lottiaux@kerlabs.com> |
| 70 | * Fix NFS remove tmpdir issue due to non unmapped files. |
| 71 | * Fix concurrency issue on the file /dev/shm/cache. |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | #define _GNU_SOURCE |
| 75 | #include <stdio.h> |
| 76 | #include <unistd.h> |
| 77 | #include <sys/mman.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <sys/types.h> |
| 80 | #include <fcntl.h> |
| 81 | #include <errno.h> |
| 82 | #include <stdlib.h> |
| 83 | #include <sys/times.h> |
| 84 | #include <sys/wait.h> |
| 85 | #include <sys/ioctl.h> |
| 86 | #include <sys/syscall.h> |
| 87 | #include <linux/unistd.h> |
| 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | #include "test.h" /*LTP Specific Include File */ |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 90 | |
| 91 | /* Test case defines */ |
| 92 | #define WINDOW_START 0x48000000 |
| 93 | |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 94 | static int page_sz; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 95 | size_t page_words; |
| 96 | size_t cache_pages; |
| 97 | size_t cache_sz; |
| 98 | size_t window_pages; |
| 99 | size_t window_sz; |
| 100 | |
| 101 | static void setup(); |
| 102 | static void cleanup(); |
| 103 | static void test_nonlinear(int fd); |
| 104 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 105 | char *TCID = "remap_file_pages01"; |
| 106 | int TST_TOTAL = 2; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 107 | |
| 108 | static char *cache_contents; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | int fd1, fd2; /* File descriptors used at the test */ |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 110 | char fname[255]; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | int main(int ac, char **av) |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 113 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 114 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 115 | const char *msg; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 116 | |
subrata_modak | 631d20d | 2008-10-15 15:12:21 +0000 | [diff] [blame] | 117 | #if defined (__s390__) || (__s390x__) || (__ia64__) |
subrata_modak | 6aa34d0 | 2008-01-21 06:52:54 +0000 | [diff] [blame] | 118 | /* Disables the test in case the kernel version is lower than 2.6.12 and arch is s390 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | if ((tst_kvercmp(2, 6, 12)) < 0) { |
| 120 | tst_resm(TWARN, |
| 121 | "This test can only run on kernels that are 2.6.12 and higher"); |
subrata_modak | 6aa34d0 | 2008-01-21 06:52:54 +0000 | [diff] [blame] | 122 | exit(0); |
| 123 | } |
| 124 | #endif |
| 125 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 126 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 127 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 128 | } |
| 129 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 130 | setup(); |
| 131 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 132 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 133 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 134 | tst_count = 0; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 135 | |
| 136 | test_nonlinear(fd1); |
| 137 | tst_resm(TPASS, "Non-Linear shm file OK"); |
| 138 | |
| 139 | test_nonlinear(fd2); |
| 140 | tst_resm(TPASS, "Non-Linear /tmp/ file OK"); |
| 141 | } |
| 142 | |
| 143 | /* clean up and exit */ |
| 144 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 145 | tst_exit(); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 146 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* test case function, that runs remap_file_pages */ |
| 150 | static void test_nonlinear(int fd) |
| 151 | { |
| 152 | char *data = NULL; |
| 153 | int i, j, repeat = 2; |
| 154 | |
| 155 | for (i = 0; i < cache_pages; i++) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | char *page = cache_contents + i * page_sz; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 157 | |
| 158 | for (j = 0; j < page_words; j++) |
| 159 | page[j] = i; |
| 160 | } |
| 161 | |
| 162 | if (write(fd, cache_contents, cache_sz) != cache_sz) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | tst_resm(TFAIL, |
Markos Chandras | f4539c6 | 2012-01-03 09:41:10 +0000 | [diff] [blame] | 164 | "Write Error for \"cache_contents\" to \"cache_sz\" of %zu (errno=%d : %s)", |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 165 | cache_sz, errno, strerror(errno)); |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 166 | cleanup(NULL); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | data = mmap((void *)WINDOW_START, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | window_sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 171 | |
| 172 | if (data == MAP_FAILED) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | tst_resm(TFAIL, "mmap Error, errno=%d : %s", errno, |
| 174 | strerror(errno)); |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 175 | cleanup(NULL); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | again: |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 179 | for (i = 0; i < window_pages; i += 2) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | char *page = data + i * page_sz; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 181 | |
| 182 | if (remap_file_pages(page, page_sz * 2, 0, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | (window_pages - i - 2), 0) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | tst_resm(TFAIL | TERRNO, |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 185 | "remap_file_pages error for page=%p, " |
| 186 | "page_sz=%d, window_pages=%zu", |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 187 | page, (page_sz * 2), (window_pages - i - 2)); |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 188 | cleanup(data); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | for (i = 0; i < window_pages; i++) { |
| 193 | /* |
| 194 | * Double-check the correctness of the mapping: |
| 195 | */ |
| 196 | if (i & 1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | if (data[i * page_sz] != window_pages - i) { |
| 198 | tst_resm(TFAIL, |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 199 | "hm, mapped incorrect data, " |
| 200 | "data[%d]=%d, (window_pages-%d)=%zu", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 201 | (i * page_sz), data[i * page_sz], i, |
| 202 | (window_pages - i)); |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 203 | cleanup(data); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 204 | } |
| 205 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | if (data[i * page_sz] != window_pages - i - 2) { |
| 207 | tst_resm(TFAIL, |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 208 | "hm, mapped incorrect data, " |
| 209 | "data[%d]=%d, (window_pages-%d-2)=%zu", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | (i * page_sz), data[i * page_sz], i, |
| 211 | (window_pages - i - 2)); |
| 212 | cleanup(data); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (--repeat) |
| 218 | goto again; |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 219 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | munmap(data, window_sz); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* setup() - performs all ONE TIME setup for this test */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 224 | void setup(void) |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 225 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 226 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 227 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 228 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 229 | tst_tmpdir(); |
| 230 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 231 | TEST_PAUSE; |
| 232 | |
| 233 | /* Get page size */ |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 234 | page_sz = getpagesize(); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 235 | |
Zeng Linggang | 9d048f8 | 2015-01-29 18:20:45 +0800 | [diff] [blame] | 236 | page_words = page_sz; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 237 | |
| 238 | /* Set the cache size */ |
| 239 | cache_pages = 1024; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 240 | cache_sz = cache_pages * page_sz; |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 241 | cache_contents = malloc(cache_sz * sizeof(char)); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 242 | |
| 243 | /* Set the window size */ |
| 244 | window_pages = 16; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 245 | window_sz = window_pages * page_sz; |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 246 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 247 | sprintf(fname, "/dev/shm/cache_%d", getpid()); |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 248 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | if ((fd1 = open(fname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) { |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 250 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 251 | "open(%s, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU) Failed, errno=%d : %s", |
| 252 | fname, errno, strerror(errno)); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 255 | if ((fd2 = open("cache", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) { |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 256 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 257 | "open(%s, O_RDWR|O_CREAT|O_TRUNC,S_IRWXU) Failed, errno=%d : %s", |
| 258 | "cache", errno, strerror(errno)); |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 261 | } |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * cleanup() - Performs one time cleanup for this test at |
| 265 | * completion or premature exit |
| 266 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 267 | void cleanup(char *data) |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 268 | { |
| 269 | /* Close the file descriptors */ |
| 270 | close(fd1); |
| 271 | close(fd2); |
| 272 | |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 273 | if (data) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 274 | munmap(data, window_sz); |
subrata_modak | 8b2171f | 2008-02-26 07:52:26 +0000 | [diff] [blame] | 275 | |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 276 | /* Remove the /dev/shm/cache_<pid> file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 277 | unlink(fname); |
subrata_modak | cf17958 | 2008-02-26 08:01:53 +0000 | [diff] [blame] | 278 | |
subrata_modak | fcfd160 | 2007-10-17 14:33:24 +0000 | [diff] [blame] | 279 | tst_rmdir(); |
| 280 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 281 | } |