1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2013 Joonsoo Kim, LG Electronics.
4 * Author: Joonsoo Kim
5 */
6
7 /*\
8 * [Description]
9 *
10 * Test sanity of cow optimization on page cache. If a page in page cache
11 * has only 1 ref count, it is mapped for a private mapping directly and
12 * is overwritten freely, so next time we access the page, we can see
13 * corrupt data.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <sys/mount.h>
19 #include <limits.h>
20 #include <sys/param.h>
21 #include <sys/types.h>
22
23 #include "hugetlb.h"
24
25 #define MNTPOINT "hugetlbfs/"
26 static long hpage_size;
27 static int huge_fd = -1;
28
run_test(void)29 static void run_test(void)
30 {
31 char *p;
32 char c;
33
34 p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
35 huge_fd, 0);
36 *p = 's';
37 tst_res(TINFO, "Write %c to %p via shared mapping", *p, p);
38 SAFE_MUNMAP(p, hpage_size);
39
40 p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
41 huge_fd, 0);
42 *p = 'p';
43 tst_res(TINFO, "Write %c to %p via private mapping", *p, p);
44 SAFE_MUNMAP(p, hpage_size);
45
46 p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
47 huge_fd, 0);
48 c = *p;
49 tst_res(TINFO, "Read %c from %p via shared mapping", *p, p);
50 SAFE_MUNMAP(p, hpage_size);
51
52 /* Direct write from huge page */
53 if (c != 's')
54 tst_res(TFAIL, "Data got corrupted.");
55 else
56 tst_res(TPASS, "Successful");
57 }
58
setup(void)59 static void setup(void)
60 {
61 hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
62 huge_fd = tst_creat_unlinked(MNTPOINT, 0);
63 }
64
cleanup(void)65 static void cleanup(void)
66 {
67 SAFE_CLOSE(huge_fd);
68 }
69
70 static struct tst_test test = {
71 .needs_root = 1,
72 .mntpoint = MNTPOINT,
73 .needs_hugetlbfs = 1,
74 .setup = setup,
75 .cleanup = cleanup,
76 .test_all = run_test,
77 .hugepages = {2, TST_NEEDS},
78 };
79