|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
1 | 2 | /*
|
2 |
| - * This is a reproducer copied from one of LKML patch submission, |
| 3 | + * Copyright (C) 2010 Red Hat, Inc. |
| 4 | + */ |
| 5 | + |
| 6 | +/*\ |
| 7 | + * [Description] |
| 8 | + * |
| 9 | + * This is a reproducer copied from one of LKML patch submission |
3 | 10 | * which subject is
|
4 | 11 | *
|
5 | 12 | * [PATCH] mlock: revert the optimization for dirtying pages and triggering writeback.
|
| 13 | + * url see https://www.spinics.net/lists/kernel/msg1141090.html |
6 | 14 | *
|
7 | 15 | * "In 5ecfda0, we do some optimization in mlock, but it causes
|
8 | 16 | * a very basic test case(attached below) of mlock to fail. So
|
9 | 17 | * this patch revert it with some tiny modification so that it
|
10 | 18 | * apply successfully with the lastest 38-rc2 kernel."
|
11 | 19 | *
|
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. |
| 20 | + * This bug was fixed by kernel |
| 21 | + * commit fdf4c587a7 ("mlock: operate on any regions with protection != PROT_NONE") |
27 | 22 | *
|
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. |
| 23 | + * As this case does, mmaps a file with PROT_WRITE permissions but without |
| 24 | + * PROT_READ, so attempt to not unnecessarity break COW during mlock ended up |
| 25 | + * causing mlock to fail with a permission problem on unfixed kernel. |
32 | 26 | */
|
33 |
| -#include "test.h" |
34 |
| -#include "safe_macros.h" |
35 |
| -#include "config.h" |
36 |
| - |
37 |
| -char *TCID = "mlock04"; |
38 |
| -int TST_TOTAL = 1; |
39 | 27 |
|
40 | 28 | #include <sys/mman.h>
|
41 | 29 | #include <stdio.h>
|
42 | 30 | #include <sys/types.h>
|
43 |
| -#include <sys/stat.h> |
44 |
| -#include <errno.h> |
45 |
| -#include <fcntl.h> |
46 |
| -#include <unistd.h> |
47 |
| -#include <sys/types.h> |
48 |
| - |
49 |
| -int fd, file_len = 40960; |
50 |
| -char *testfile = "test_mlock"; |
| 31 | +#include "tst_test.h" |
| 32 | +#include "tst_safe_macros.h" |
51 | 33 |
|
52 |
| -static void setup(void); |
53 |
| -static void cleanup(void); |
| 34 | +static int fd = -1, file_len = 40960; |
| 35 | +static char *testfile = "test_mlock"; |
54 | 36 |
|
55 |
| -int main(void) |
| 37 | +static void verify_mlock(void) |
56 | 38 | {
|
57 | 39 | char *buf;
|
58 |
| - int lc; |
59 |
| - |
60 |
| - setup(); |
61 |
| - |
62 |
| - for (lc = 0; TEST_LOOPING(lc); lc++) { |
63 |
| - buf = mmap(NULL, file_len, PROT_WRITE, MAP_SHARED, fd, 0); |
64 |
| - |
65 |
| - if (buf == MAP_FAILED) |
66 |
| - tst_brkm(TBROK | TERRNO, cleanup, "mmap"); |
67 | 40 |
|
68 |
| - if (mlock(buf, file_len) == -1) |
69 |
| - tst_brkm(TBROK | TERRNO, cleanup, "mlock"); |
70 |
| - |
71 |
| - tst_resm(TINFO, "locked %d bytes from %p", file_len, buf); |
72 |
| - |
73 |
| - if (munlock(buf, file_len) == -1) |
74 |
| - tst_brkm(TBROK | TERRNO, cleanup, "munlock"); |
75 |
| - |
76 |
| - SAFE_MUNMAP(cleanup, buf, file_len); |
77 |
| - } |
78 |
| - |
79 |
| - tst_resm(TPASS, "test succeeded."); |
80 |
| - |
81 |
| - cleanup(); |
82 |
| - |
83 |
| - tst_exit(); |
| 41 | + buf = SAFE_MMAP(NULL, file_len, PROT_WRITE, MAP_SHARED, fd, 0); |
| 42 | + TST_EXP_PASS(mlock(buf, file_len), "mlock(%p, %d)", buf, file_len); |
| 43 | + SAFE_MUNLOCK(buf, file_len); |
| 44 | + SAFE_MUNMAP(buf, file_len); |
84 | 45 | }
|
85 | 46 |
|
86 | 47 | static void setup(void)
|
87 | 48 | {
|
88 |
| - tst_tmpdir(); |
89 |
| - |
90 |
| - fd = SAFE_OPEN(cleanup, testfile, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
91 |
| - |
92 |
| - SAFE_FTRUNCATE(cleanup, fd, file_len); |
93 |
| - |
94 |
| - TEST_PAUSE; |
| 49 | + fd = SAFE_OPEN(testfile, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| 50 | + SAFE_FTRUNCATE(fd, file_len); |
95 | 51 | }
|
96 | 52 |
|
97 | 53 | static void cleanup(void)
|
98 | 54 | {
|
99 |
| - close(fd); |
100 |
| - |
101 |
| - tst_rmdir(); |
| 55 | + if (fd > -1) |
| 56 | + SAFE_CLOSE(fd); |
102 | 57 | }
|
| 58 | + |
| 59 | +static struct tst_test test = { |
| 60 | + .needs_tmpdir = 1, |
| 61 | + .setup = setup, |
| 62 | + .cleanup = cleanup, |
| 63 | + .test_all = verify_mlock, |
| 64 | + .tags = (const struct tst_tag[]) { |
| 65 | + {"linux-git", "fdf4c587a793"}, |
| 66 | + {} |
| 67 | + } |
| 68 | +}; |
0 commit comments