1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2023 SUSE LLC
4 * Author: Vlastimil Babka <[email protected]>
5 * https://bugzilla.suse.com/attachment.cgi?id=867254
6 * LTP port: Petr Vorel <[email protected]>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Bug reproducer for 7e7757876f25 ("mm/mremap: fix vm_pgoff in vma_merge() case 3")
13 */
14
15 #define _GNU_SOURCE
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <sys/mman.h>
21 #include <lapi/mmap.h>
22
23 #include "tst_test.h"
24 #include "tst_safe_macros.h"
25
26 #define NUM_GRANULARITYS 3
27
28 static int fd;
29 static char *buf, *buf2;
30 static int mmap_size, mremap_size;
31
32 static struct tcase {
33 size_t incompatible;
34 const char *desc;
35 } tcases[] = {
36 {
37 .desc = "all pages with compatible mapping",
38 },
39 {
40 .incompatible = 3,
41 .desc = "third MMAP_GRANULARITY's mapping incompatible",
42 },
43 {
44 .incompatible = 1,
45 .desc = "first MMAP_GRANULARITY's mapping incompatible",
46 },
47 };
48
check_pages(void)49 static int check_pages(void)
50 {
51 int fail = 0, i;
52 char val;
53
54 for (i = 0; i < (int)ARRAY_SIZE(tcases); i++) {
55 val = buf[i * MMAP_GRANULARITY];
56 if (val != 0x30 + i) {
57 tst_res(TFAIL, "page %d wrong value %d (0x%x)", i, val - 0x30, val);
58 fail = 1;
59 }
60 }
61
62 return fail;
63 }
64
do_test(unsigned int n)65 static void do_test(unsigned int n)
66 {
67 struct tcase *tc = &tcases[n];
68 int ret;
69
70 tst_res(TINFO, "%s", tc->desc);
71
72 buf = SAFE_MMAP(0, mmap_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
73
74 buf2 = mremap(buf + MMAP_GRANULARITY, MMAP_GRANULARITY, MMAP_GRANULARITY,
75 MREMAP_MAYMOVE|MREMAP_FIXED, buf + mremap_size);
76 if (buf2 == MAP_FAILED)
77 tst_brk(TBROK, "mremap() failed");
78
79 if (tc->incompatible) {
80 ret = mprotect(buf + (tc->incompatible-1)*MMAP_GRANULARITY,
81 MMAP_GRANULARITY, PROT_READ);
82 if (ret == -1)
83 tst_brk(TBROK, "mprotect() failed");
84 }
85
86 buf2 = mremap(buf + mremap_size, MMAP_GRANULARITY, MMAP_GRANULARITY,
87 MREMAP_MAYMOVE|MREMAP_FIXED, buf + MMAP_GRANULARITY);
88 if (buf2 == MAP_FAILED)
89 tst_brk(TBROK, "mremap() failed");
90
91 if (!check_pages())
92 tst_res(TPASS, "mmap/mremap work properly");
93
94 SAFE_MUNMAP(buf, mremap_size);
95 }
96
setup(void)97 static void setup(void)
98 {
99 int ret, i;
100
101 mmap_size = (NUM_GRANULARITYS+1) * MMAP_GRANULARITY;
102 mremap_size = NUM_GRANULARITYS * MMAP_GRANULARITY;
103
104 fd = SAFE_OPEN("testfile", O_CREAT | O_RDWR | O_TRUNC, 0600);
105
106 ret = fallocate(fd, 0, 0, mmap_size);
107 if (ret != 0) {
108 if (tst_fs_type(".") == TST_NFS_MAGIC && (errno == EOPNOTSUPP ||
109 errno == ENOSYS)) {
110 tst_brk(TCONF, "fallocate system call is not implemented");
111 }
112 tst_brk(TBROK, "fallocate() failed");
113 }
114
115 buf = SAFE_MMAP(0, mmap_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
116
117 for (i = 0; i < (int)ARRAY_SIZE(tcases)+1; i++)
118 buf[i*MMAP_GRANULARITY] = 0x30 + i;
119
120 /* clear the page tables */
121 SAFE_MUNMAP(buf, mmap_size);
122 }
123
cleanup(void)124 static void cleanup(void)
125 {
126 if (fd > 0)
127 SAFE_CLOSE(fd);
128 }
129
130 static struct tst_test test = {
131 .setup = setup,
132 .cleanup = cleanup,
133 .test = do_test,
134 .needs_tmpdir = 1,
135 .tcnt = ARRAY_SIZE(tcases),
136 .tags = (struct tst_tag[]) {
137 {"linux-git", "7e7757876f25"},
138 {}
139 },
140 };
141