1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2005-2006 IBM Corporation.
4 * Author: David Gibson & Adam Litke
5 */
6
7 /*\
8 * [Description]
9 *
10 * This baseline test validates that a mapping of a certain size can be
11 * created, correctly. Once created, all the pages are filled with a
12 * pattern and rechecked to test for corruption. The mapping is then
13 * released. This process is repeated for a specified number of
14 * iterations.
15 */
16
17 #include "hugetlb.h"
18
19 #define NR_HUGEPAGES 2
20 #define MNTPOINT "hugetlbfs/"
21
22 static unsigned long hpage_size;
23 static int fd = -1;
24
run_test(unsigned int iter)25 static void run_test(unsigned int iter)
26 {
27 char *m;
28 size_t i, j;
29 char pattern = 'A';
30 size_t size = NR_HUGEPAGES*hpage_size;
31
32 fd = tst_creat_unlinked(MNTPOINT, 0);
33 m = SAFE_MMAP(NULL, size, (PROT_READ|PROT_WRITE), MAP_SHARED, fd, 0);
34
35 for (i = 0; i < NR_HUGEPAGES; i++) {
36 for (j = 0; j < hpage_size; j++) {
37 if (*(m+(i*hpage_size)+j) != 0) {
38 tst_res(TFAIL, "Iter %u: Verifying the mmap area failed. "
39 "Got %c, expected 0", iter,
40 *(m+(i*hpage_size)+j));
41 goto cleanup;
42 }
43 }
44 }
45
46 for (i = 0; i < NR_HUGEPAGES; i++) {
47 pattern = 65+(i%26);
48 memset(m+(i*hpage_size), pattern, hpage_size);
49 }
50
51 for (i = 0; i < NR_HUGEPAGES; i++) {
52 pattern = 65+(i%26);
53 for (j = 0; j < hpage_size; j++) {
54 if (*(m+(i*hpage_size)+j) != pattern) {
55 tst_res(TFAIL, "Iter %u: Verifying the mmap area failed. "
56 "got: %c, expected: %c", iter,
57 *(m+(i*hpage_size)+j), pattern);
58 goto cleanup;
59 }
60 }
61 }
62
63 tst_res(TPASS, "Iter %u: Successfully verified the mmap area.", iter);
64
65 cleanup:
66 SAFE_MUNMAP(m, size);
67 SAFE_CLOSE(fd);
68 }
69
setup(void)70 static void setup(void)
71 {
72 hpage_size = tst_get_hugepage_size();
73 }
74
cleanup(void)75 static void cleanup(void)
76 {
77 if (fd >= 0)
78 SAFE_CLOSE(fd);
79 }
80
81 static struct tst_test test = {
82 .tcnt = 2,
83 .needs_root = 1,
84 .mntpoint = MNTPOINT,
85 .needs_hugetlbfs = 1,
86 .needs_tmpdir = 1,
87 .setup = setup,
88 .cleanup = cleanup,
89 .test = run_test,
90 .hugepages = {NR_HUGEPAGES, TST_NEEDS},
91 };
92