1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2005-2006 IBM Corporation.
4f08c3bdfSopenharmony_ci * Author: Mel Gorman
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * readahead() on some kernels can cause the reservation counter to get
11f08c3bdfSopenharmony_ci * corrupted. The problem is that the pages are allocated for the
12f08c3bdfSopenharmony_ci * reservation but not faulted in at the time of allocation. The
13f08c3bdfSopenharmony_ci * counters do not get updated and effectively "leak". This test
14f08c3bdfSopenharmony_ci * identifies whether the kernel is vulnerable to the problem or not.
15f08c3bdfSopenharmony_ci * It's fixed in kernel by commit f2deae9d4e70.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define _GNU_SOURCE
19f08c3bdfSopenharmony_ci#include "hugetlb.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/"
22f08c3bdfSopenharmony_cistatic long hpage_size;
23f08c3bdfSopenharmony_cistatic int fd = -1;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void run_test(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	void *p;
28f08c3bdfSopenharmony_ci	unsigned long initial_rsvd, map_rsvd, readahead_rsvd, end_rsvd;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	fd = tst_creat_unlinked(MNTPOINT, 0);
31f08c3bdfSopenharmony_ci	initial_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
34f08c3bdfSopenharmony_ci		 fd, 0);
35f08c3bdfSopenharmony_ci	map_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
36f08c3bdfSopenharmony_ci	tst_res(TINFO, "map_rsvd: %lu", map_rsvd);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	readahead(fd, 0, hpage_size);
39f08c3bdfSopenharmony_ci	readahead_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
40f08c3bdfSopenharmony_ci	tst_res(TINFO, "readahead_rsvd: %lu", readahead_rsvd);
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	memset(p, 1, hpage_size);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p, hpage_size);
45f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
46f08c3bdfSopenharmony_ci	end_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(end_rsvd, initial_rsvd);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	hpage_size = tst_get_hugepage_size();
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic void cleanup(void)
57f08c3bdfSopenharmony_ci{
58f08c3bdfSopenharmony_ci	if (fd >= 0)
59f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic struct tst_test test = {
63f08c3bdfSopenharmony_ci	.tags = (struct tst_tag[]) {
64f08c3bdfSopenharmony_ci		{"linux-git", "f2deae9d4e70"},
65f08c3bdfSopenharmony_ci		{}
66f08c3bdfSopenharmony_ci	},
67f08c3bdfSopenharmony_ci	.needs_root = 1,
68f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
69f08c3bdfSopenharmony_ci	.needs_hugetlbfs = 1,
70f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
71f08c3bdfSopenharmony_ci	.setup = setup,
72f08c3bdfSopenharmony_ci	.cleanup = cleanup,
73f08c3bdfSopenharmony_ci	.test_all = run_test,
74f08c3bdfSopenharmony_ci	.hugepages = {1, TST_NEEDS},
75f08c3bdfSopenharmony_ci};
76