1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2010-2017  Red Hat, Inc.
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * hugetlbfs allows to overcommit hugepages and there are tunables in
6f08c3bdfSopenharmony_ci * sysfs and procfs. The test here want to ensure it is possible to
7f08c3bdfSopenharmony_ci * overcommit by either mmap or shared memory. Also ensure those
8f08c3bdfSopenharmony_ci * reservation can be read/write, and several statistics work correctly.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * First, it resets nr_hugepages and nr_overcommit_hugepages. Then, set
11f08c3bdfSopenharmony_ci * both to a specify value - N, and allocate N + %50 x N hugepages.
12f08c3bdfSopenharmony_ci * Finally, it reads and writes every page. There are command options to
13f08c3bdfSopenharmony_ci * choose either to manage hugepages from sysfs or procfs, and reserve
14f08c3bdfSopenharmony_ci * them by mmap or shmget.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <string.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <stdio.h>
20f08c3bdfSopenharmony_ci#include "hugetlb.h"
21f08c3bdfSopenharmony_ci#include "tst_safe_sysv_ipc.h"
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define PROTECTION		(PROT_READ | PROT_WRITE)
25f08c3bdfSopenharmony_ci#define PATH_MEMINFO		"/proc/meminfo"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic char path_sys_sz[BUFSIZ];
28f08c3bdfSopenharmony_cistatic char path_sys_sz_over[BUFSIZ];
29f08c3bdfSopenharmony_cistatic char path_sys_sz_free[BUFSIZ];
30f08c3bdfSopenharmony_cistatic char path_sys_sz_resv[BUFSIZ];
31f08c3bdfSopenharmony_cistatic char path_sys_sz_surp[BUFSIZ];
32f08c3bdfSopenharmony_cistatic char path_sys_sz_huge[BUFSIZ];
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#define PATH_PROC_VM		"/proc/sys/vm/"
35f08c3bdfSopenharmony_ci#define PATH_PROC_OVER		PATH_PROC_VM "nr_overcommit_hugepages"
36f08c3bdfSopenharmony_ci#define PATH_PROC_HUGE		PATH_PROC_VM "nr_hugepages"
37f08c3bdfSopenharmony_ci#define PATH_SHMMAX		"/proc/sys/kernel/shmmax"
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci/* Only ia64 requires this */
40f08c3bdfSopenharmony_ci#ifdef __ia64__
41f08c3bdfSopenharmony_ci#define ADDR (void *)(0x8000000000000000UL)
42f08c3bdfSopenharmony_ci#define FLAGS (MAP_SHARED | MAP_FIXED)
43f08c3bdfSopenharmony_ci#define SHMAT_FLAGS (SHM_RND)
44f08c3bdfSopenharmony_ci#else
45f08c3bdfSopenharmony_ci#define ADDR (void *)(0x0UL)
46f08c3bdfSopenharmony_ci#define FLAGS (MAP_SHARED)
47f08c3bdfSopenharmony_ci#define SHMAT_FLAGS (0)
48f08c3bdfSopenharmony_ci#endif
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci#ifndef SHM_HUGETLB
51f08c3bdfSopenharmony_ci#define SHM_HUGETLB 04000
52f08c3bdfSopenharmony_ci#endif
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci#define NR_HPAGES 2
55f08c3bdfSopenharmony_ci#define MOUNT_DIR "hugemmap05"
56f08c3bdfSopenharmony_ci#define TEST_FILE MOUNT_DIR "/file"
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic unsigned long long shmmax;
59f08c3bdfSopenharmony_cistatic char *path, *pathover;
60f08c3bdfSopenharmony_cistatic int key = -1, shmid = -1, fd = -1;
61f08c3bdfSopenharmony_cistatic int mounted, restore_shmmax, restore_overcomm_hgpgs;
62f08c3bdfSopenharmony_cistatic long hugepagesize, nr_overcommit_hugepages;
63f08c3bdfSopenharmony_cistatic long size = NR_HPAGES, length = (NR_HPAGES + NR_HPAGES/2) * 2;
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cichar *opt_sysfs;
66f08c3bdfSopenharmony_cichar *opt_alloc;
67f08c3bdfSopenharmony_cichar *opt_shmid;
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic void check_wr_bytes(void *addr);
70f08c3bdfSopenharmony_cistatic int checkproc(long act_val, char *string, long exp_val);
71f08c3bdfSopenharmony_cistatic int checksys(char *path, char *pattern, long exp_val);
72f08c3bdfSopenharmony_cistatic void init_sys_sz_paths(void);
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic void test_overcommit(void)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	void *addr = NULL, *shmaddr = NULL;
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (opt_shmid) {
79f08c3bdfSopenharmony_ci		shmid = SAFE_SHMGET(key, (length / 2 * hugepagesize),
80f08c3bdfSopenharmony_ci				 SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
81f08c3bdfSopenharmony_ci	} else {
82f08c3bdfSopenharmony_ci		fd = SAFE_OPEN(TEST_FILE, O_CREAT | O_RDWR, 0755);
83f08c3bdfSopenharmony_ci		addr = SAFE_MMAP(ADDR, (length / 2 * hugepagesize),
84f08c3bdfSopenharmony_ci				 PROTECTION, FLAGS, fd, 0);
85f08c3bdfSopenharmony_ci	}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	if (opt_sysfs) {
88f08c3bdfSopenharmony_ci		tst_res(TINFO, "check sysfs before allocation.");
89f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
90f08c3bdfSopenharmony_ci			return;
91f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_free, "HugePages_Free", length / 2))
92f08c3bdfSopenharmony_ci			return;
93f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_surp, "HugePages_Surp",
94f08c3bdfSopenharmony_ci			     length / 2 - size))
95f08c3bdfSopenharmony_ci			return;
96f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_resv, "HugePages_Rsvd", length / 2))
97f08c3bdfSopenharmony_ci			return;
98f08c3bdfSopenharmony_ci	} else {
99f08c3bdfSopenharmony_ci		tst_res(TINFO, "check /proc/meminfo before allocation.");
100f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
101f08c3bdfSopenharmony_ci			      "HugePages_Total", length / 2))
102f08c3bdfSopenharmony_ci			return;
103f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
104f08c3bdfSopenharmony_ci			      "HugePages_Free", length / 2))
105f08c3bdfSopenharmony_ci			return;
106f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
107f08c3bdfSopenharmony_ci			      "HugePages_Surp", length / 2 - size))
108f08c3bdfSopenharmony_ci			return;
109f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
110f08c3bdfSopenharmony_ci			      "HugePages_Rsvd", length / 2))
111f08c3bdfSopenharmony_ci			return;
112f08c3bdfSopenharmony_ci	}
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	if (opt_shmid) {
115f08c3bdfSopenharmony_ci		tst_res(TINFO, "shmid: 0x%x", shmid);
116f08c3bdfSopenharmony_ci		shmaddr = SAFE_SHMAT(shmid, ADDR, SHMAT_FLAGS);
117f08c3bdfSopenharmony_ci		check_wr_bytes(shmaddr);
118f08c3bdfSopenharmony_ci	} else {
119f08c3bdfSopenharmony_ci		check_wr_bytes(addr);
120f08c3bdfSopenharmony_ci	}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	if (opt_sysfs) {
123f08c3bdfSopenharmony_ci		tst_res(TINFO, "check sysfs.");
124f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2))
125f08c3bdfSopenharmony_ci			return;
126f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_free, "HugePages_Free", 0))
127f08c3bdfSopenharmony_ci			return;
128f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_surp, "HugePages_Surp",
129f08c3bdfSopenharmony_ci			     length / 2 - size))
130f08c3bdfSopenharmony_ci			return;
131f08c3bdfSopenharmony_ci		if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0))
132f08c3bdfSopenharmony_ci			return;
133f08c3bdfSopenharmony_ci	} else {
134f08c3bdfSopenharmony_ci		tst_res(TINFO, "check /proc/meminfo.");
135f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Total:"),
136f08c3bdfSopenharmony_ci			      "HugePages_Total", length / 2))
137f08c3bdfSopenharmony_ci			return;
138f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Free:"),
139f08c3bdfSopenharmony_ci			      "HugePages_Free", 0))
140f08c3bdfSopenharmony_ci			return;
141f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Surp:"),
142f08c3bdfSopenharmony_ci			      "HugePages_Surp", length / 2 - size))
143f08c3bdfSopenharmony_ci			return;
144f08c3bdfSopenharmony_ci		if (checkproc(SAFE_READ_MEMINFO("HugePages_Rsvd:"),
145f08c3bdfSopenharmony_ci			      "HugePages_Rsvd", 0))
146f08c3bdfSopenharmony_ci			return;
147f08c3bdfSopenharmony_ci	}
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci	if (opt_shmid) {
150f08c3bdfSopenharmony_ci		SAFE_SHMDT(shmaddr);
151f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
152f08c3bdfSopenharmony_ci	} else {
153f08c3bdfSopenharmony_ci		SAFE_MUNMAP(addr, (length / 2 * hugepagesize));
154f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
155f08c3bdfSopenharmony_ci		SAFE_UNLINK(TEST_FILE);
156f08c3bdfSopenharmony_ci	}
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	tst_res(TPASS, "hugepages overcommit test pass");
159f08c3bdfSopenharmony_ci}
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_cistatic void cleanup(void)
162f08c3bdfSopenharmony_ci{
163f08c3bdfSopenharmony_ci	if (opt_shmid && shmid != -1)
164f08c3bdfSopenharmony_ci		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	if (!opt_shmid && fd != -1) {
167f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
168f08c3bdfSopenharmony_ci		SAFE_UNLINK(TEST_FILE);
169f08c3bdfSopenharmony_ci	}
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci	if (mounted)
172f08c3bdfSopenharmony_ci		tst_umount(MOUNT_DIR);
173f08c3bdfSopenharmony_ci
174f08c3bdfSopenharmony_ci	if (restore_shmmax)
175f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(PATH_SHMMAX, "%llu", shmmax);
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_ci	if (restore_overcomm_hgpgs) {
178f08c3bdfSopenharmony_ci		tst_res(TINFO, "restore nr_overcommit_hugepages to %ld.",
179f08c3bdfSopenharmony_ci			nr_overcommit_hugepages);
180f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(pathover, "%ld", nr_overcommit_hugepages);
181f08c3bdfSopenharmony_ci	}
182f08c3bdfSopenharmony_ci}
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_cistatic void setup(void)
185f08c3bdfSopenharmony_ci{
186f08c3bdfSopenharmony_ci	unsigned long hpages;
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci	hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
189f08c3bdfSopenharmony_ci	init_sys_sz_paths();
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_ci	if (opt_sysfs) {
192f08c3bdfSopenharmony_ci		path = path_sys_sz_huge;
193f08c3bdfSopenharmony_ci		pathover = path_sys_sz_over;
194f08c3bdfSopenharmony_ci	} else {
195f08c3bdfSopenharmony_ci		path = PATH_PROC_HUGE;
196f08c3bdfSopenharmony_ci		pathover = PATH_PROC_OVER;
197f08c3bdfSopenharmony_ci	}
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	if (opt_alloc) {
200f08c3bdfSopenharmony_ci		size = atoi(opt_alloc);
201f08c3bdfSopenharmony_ci		length = (size + size * 0.5) * 2;
202f08c3bdfSopenharmony_ci	}
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_ci	if (opt_shmid) {
205f08c3bdfSopenharmony_ci		SAFE_FILE_SCANF(PATH_SHMMAX, "%llu", &shmmax);
206f08c3bdfSopenharmony_ci		if (shmmax < (unsigned long long)(length / 2 * hugepagesize)) {
207f08c3bdfSopenharmony_ci			restore_shmmax = 1;
208f08c3bdfSopenharmony_ci			SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld",
209f08c3bdfSopenharmony_ci					(length / 2 * hugepagesize));
210f08c3bdfSopenharmony_ci		}
211f08c3bdfSopenharmony_ci	}
212f08c3bdfSopenharmony_ci
213f08c3bdfSopenharmony_ci	/* Reset. */
214f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(path, "%ld", size);
215f08c3bdfSopenharmony_ci	SAFE_FILE_SCANF(path, "%lu", &hpages);
216f08c3bdfSopenharmony_ci	if (hpages != size)
217f08c3bdfSopenharmony_ci		tst_brk(TCONF, "Not enough hugepages for testing!");
218f08c3bdfSopenharmony_ci
219f08c3bdfSopenharmony_ci	if (access(pathover, F_OK)) {
220f08c3bdfSopenharmony_ci		tst_brk(TCONF, "file %s does not exist in the system",
221f08c3bdfSopenharmony_ci			pathover);
222f08c3bdfSopenharmony_ci	}
223f08c3bdfSopenharmony_ci
224f08c3bdfSopenharmony_ci	SAFE_FILE_SCANF(pathover, "%ld", &nr_overcommit_hugepages);
225f08c3bdfSopenharmony_ci	tst_res(TINFO, "original nr_overcommit_hugepages is %ld",
226f08c3bdfSopenharmony_ci		nr_overcommit_hugepages);
227f08c3bdfSopenharmony_ci
228f08c3bdfSopenharmony_ci	/* Reset. */
229f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(pathover, "%ld", size);
230f08c3bdfSopenharmony_ci	restore_overcomm_hgpgs = 1;
231f08c3bdfSopenharmony_ci
232f08c3bdfSopenharmony_ci	SAFE_MKDIR(MOUNT_DIR, 0700);
233f08c3bdfSopenharmony_ci	SAFE_MOUNT(NULL, MOUNT_DIR, "hugetlbfs", 0, NULL);
234f08c3bdfSopenharmony_ci	mounted = 1;
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ci	if (opt_shmid) {
237f08c3bdfSopenharmony_ci		/* Use /proc/meminfo to generate an IPC key. */
238f08c3bdfSopenharmony_ci		key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO));
239f08c3bdfSopenharmony_ci		if (key == -1)
240f08c3bdfSopenharmony_ci			tst_brk(TBROK | TERRNO, "ftok");
241f08c3bdfSopenharmony_ci	}
242f08c3bdfSopenharmony_ci}
243f08c3bdfSopenharmony_ci
244f08c3bdfSopenharmony_cistatic void check_wr_bytes(void *addr)
245f08c3bdfSopenharmony_ci{
246f08c3bdfSopenharmony_ci	long i;
247f08c3bdfSopenharmony_ci
248f08c3bdfSopenharmony_ci	memset((char *)addr, '\a', (length / 2 * hugepagesize));
249f08c3bdfSopenharmony_ci
250f08c3bdfSopenharmony_ci	tst_res(TINFO, "First hex is %x", *((unsigned int *)addr));
251f08c3bdfSopenharmony_ci	for (i = 0; i < (length / 2 * hugepagesize); i++) {
252f08c3bdfSopenharmony_ci		if (((char *)addr)[i] != '\a') {
253f08c3bdfSopenharmony_ci			tst_res(TFAIL, "mismatch at %ld", i);
254f08c3bdfSopenharmony_ci			break;
255f08c3bdfSopenharmony_ci		}
256f08c3bdfSopenharmony_ci	}
257f08c3bdfSopenharmony_ci}
258f08c3bdfSopenharmony_ci
259f08c3bdfSopenharmony_cistatic int checksys(char *path, char *string, long exp_val)
260f08c3bdfSopenharmony_ci{
261f08c3bdfSopenharmony_ci	long act_val;
262f08c3bdfSopenharmony_ci
263f08c3bdfSopenharmony_ci	SAFE_FILE_SCANF(path, "%ld", &act_val);
264f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s is %ld.", string, act_val);
265f08c3bdfSopenharmony_ci	if (act_val != exp_val) {
266f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s is not %ld but %ld.", string, exp_val,
267f08c3bdfSopenharmony_ci			act_val);
268f08c3bdfSopenharmony_ci		return 1;
269f08c3bdfSopenharmony_ci	}
270f08c3bdfSopenharmony_ci	return 0;
271f08c3bdfSopenharmony_ci}
272f08c3bdfSopenharmony_ci
273f08c3bdfSopenharmony_cistatic int checkproc(long act_val, char *pattern, long exp_val)
274f08c3bdfSopenharmony_ci{
275f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s is %ld.", pattern, act_val);
276f08c3bdfSopenharmony_ci	if (act_val != exp_val) {
277f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s is not %ld but %ld.",
278f08c3bdfSopenharmony_ci			pattern, exp_val, act_val);
279f08c3bdfSopenharmony_ci		return 1;
280f08c3bdfSopenharmony_ci	}
281f08c3bdfSopenharmony_ci	return 0;
282f08c3bdfSopenharmony_ci}
283f08c3bdfSopenharmony_ci
284f08c3bdfSopenharmony_cistatic void init_sys_sz_paths(void)
285f08c3bdfSopenharmony_ci{
286f08c3bdfSopenharmony_ci	sprintf(path_sys_sz, "/sys/kernel/mm/hugepages/hugepages-%ldkB",
287f08c3bdfSopenharmony_ci		hugepagesize / 1024);
288f08c3bdfSopenharmony_ci	sprintf(path_sys_sz_over, "%s/nr_overcommit_hugepages", path_sys_sz);
289f08c3bdfSopenharmony_ci	sprintf(path_sys_sz_free, "%s/free_hugepages", path_sys_sz);
290f08c3bdfSopenharmony_ci	sprintf(path_sys_sz_resv, "%s/resv_hugepages", path_sys_sz);
291f08c3bdfSopenharmony_ci	sprintf(path_sys_sz_surp, "%s/surplus_hugepages", path_sys_sz);
292f08c3bdfSopenharmony_ci	sprintf(path_sys_sz_huge, "%s/nr_hugepages", path_sys_sz);
293f08c3bdfSopenharmony_ci}
294f08c3bdfSopenharmony_ci
295f08c3bdfSopenharmony_cistatic struct tst_test test = {
296f08c3bdfSopenharmony_ci	.needs_root = 1,
297f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
298f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
299f08c3bdfSopenharmony_ci		{"s",  &opt_sysfs, "Setup hugepages from sysfs"},
300f08c3bdfSopenharmony_ci		{"m",  &opt_shmid, "Reserve hugepages by shmget"},
301f08c3bdfSopenharmony_ci		{"a:", &opt_alloc, "Number of overcommint hugepages"},
302f08c3bdfSopenharmony_ci		{}
303f08c3bdfSopenharmony_ci},
304f08c3bdfSopenharmony_ci	.setup = setup,
305f08c3bdfSopenharmony_ci	.cleanup = cleanup,
306f08c3bdfSopenharmony_ci	.test_all = test_overcommit,
307f08c3bdfSopenharmony_ci	.hugepages = {NR_HPAGES, TST_NEEDS},
308f08c3bdfSopenharmony_ci};
309