Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This is a reproducer copied from one of LKML patch submission, |
| 3 | * which subject is |
| 4 | * |
| 5 | * [PATCH] mlock: revert the optimization for dirtying pages and triggering writeback. |
| 6 | * |
| 7 | * "In 5ecfda0, we do some optimization in mlock, but it causes |
| 8 | * a very basic test case(attached below) of mlock to fail. So |
| 9 | * this patch revert it with some tiny modification so that it |
| 10 | * apply successfully with the lastest 38-rc2 kernel." |
| 11 | * |
| 12 | * Copyright (C) 2010 Red Hat, Inc. |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of version 2 of the GNU General Public |
| 15 | * License as published by the Free Software Foundation. |
| 16 | * |
| 17 | * This program is distributed in the hope that it would be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 20 | * |
| 21 | * Further, this software is distributed without any warranty that it |
| 22 | * is free of the rightful claim of any third person regarding |
| 23 | * infringement or the like. Any license provided herein, whether |
| 24 | * implied or otherwise, applies only to this software file. Patent |
| 25 | * licenses, if any, provided herein do not apply to combinations of |
| 26 | * this program with other software, or any other product whatsoever. |
| 27 | * |
| 28 | * You should have received a copy of the GNU General Public License |
| 29 | * along with this program; if not, write the Free Software |
| 30 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 31 | * 02110-1301, USA. |
| 32 | */ |
| 33 | #include "test.h" |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 34 | #include "config.h" |
| 35 | |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 36 | char *TCID = "mlock04"; |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 37 | int TST_TOTAL = 1; |
| 38 | |
| 39 | #include <sys/mman.h> |
| 40 | #include <stdio.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <errno.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <unistd.h> |
| 46 | #include <sys/types.h> |
| 47 | |
| 48 | int fd, file_len = 40960; |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 49 | char *testfile = "test_mlock"; |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 50 | |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 51 | static void setup(void); |
| 52 | static void cleanup(void); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 53 | |
| 54 | int main(void) |
| 55 | { |
| 56 | char *buf; |
| 57 | int lc; |
| 58 | |
| 59 | setup(); |
| 60 | |
| 61 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 62 | buf = mmap(NULL, file_len, PROT_WRITE, MAP_SHARED, fd, 0); |
| 63 | |
| 64 | if (buf == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | tst_brkm(TBROK | TERRNO, cleanup, "mmap"); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 66 | |
| 67 | if (mlock(buf, file_len) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 68 | tst_brkm(TBROK | TERRNO, cleanup, "mlock"); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 69 | |
| 70 | tst_resm(TINFO, "locked %d bytes from %p", file_len, buf); |
| 71 | |
| 72 | if (munlock(buf, file_len) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | tst_brkm(TBROK | TERRNO, cleanup, "munlock"); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 74 | |
| 75 | if (munmap(buf, file_len) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | tst_brkm(TBROK | TERRNO, cleanup, "munmap"); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 77 | } |
| 78 | |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 79 | tst_resm(TPASS, "test succeeded."); |
Garrett Cooper | eabf30f | 2011-03-04 02:31:37 -0800 | [diff] [blame] | 80 | |
| 81 | cleanup(); |
| 82 | |
| 83 | tst_exit(); |
| 84 | } |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 85 | |
| 86 | static void setup(void) |
| 87 | { |
| 88 | tst_tmpdir(); |
| 89 | |
| 90 | fd = open(testfile, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| 91 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 92 | tst_brkm(TBROK | TERRNO, cleanup, "open"); |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 93 | |
| 94 | if (ftruncate(fd, file_len) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_brkm(TBROK | TERRNO, cleanup, "ftruncate"); |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 96 | |
| 97 | TEST_PAUSE; |
| 98 | } |
| 99 | |
| 100 | static void cleanup(void) |
| 101 | { |
czhang@redhat.com | b2735f7 | 2011-03-25 21:55:51 +0800 | [diff] [blame] | 102 | close(fd); |
| 103 | |
| 104 | tst_rmdir(); |
| 105 | } |