1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2015 Oracle Corporation
4f08c3bdfSopenharmony_ci * Author: Mike Kravetz
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * It tests basic fallocate functionality in hugetlbfs. Preallocate huge
11f08c3bdfSopenharmony_ci * pages to a file in hugetlbfs, and then remove the pages via hole punch.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#define _GNU_SOURCE
15f08c3bdfSopenharmony_ci#include <stdio.h>
16f08c3bdfSopenharmony_ci#include <sys/mount.h>
17f08c3bdfSopenharmony_ci#include <limits.h>
18f08c3bdfSopenharmony_ci#include <sys/param.h>
19f08c3bdfSopenharmony_ci#include <sys/types.h>
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include "hugetlb.h"
22f08c3bdfSopenharmony_ci#include "lapi/fallocate.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define MAX_PAGES_TO_USE 5
25f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int  fd = -1;
28f08c3bdfSopenharmony_cistatic long hpage_size;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void run_test(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	int err;
33f08c3bdfSopenharmony_ci	unsigned long max_iterations;
34f08c3bdfSopenharmony_ci	unsigned long free_initial, free_after, free_end;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	free_initial = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
37f08c3bdfSopenharmony_ci	max_iterations = MIN(free_initial, MAX_PAGES_TO_USE);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	fd = tst_creat_unlinked(MNTPOINT, 0);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	/* First preallocate file with max_iterations pages */
42f08c3bdfSopenharmony_ci	err = fallocate(fd, 0, 0, hpage_size * max_iterations);
43f08c3bdfSopenharmony_ci	if (err) {
44f08c3bdfSopenharmony_ci		if (errno == EOPNOTSUPP)
45f08c3bdfSopenharmony_ci			tst_brk(TCONF, "fallocate() Operation is not supported");
46f08c3bdfSopenharmony_ci		tst_res(TFAIL|TERRNO, "fallocate()");
47f08c3bdfSopenharmony_ci		goto cleanup;
48f08c3bdfSopenharmony_ci	}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
51f08c3bdfSopenharmony_ci	if (free_initial - free_after != max_iterations) {
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "fallocate did not preallocate %lu huge pages",
53f08c3bdfSopenharmony_ci							max_iterations);
54f08c3bdfSopenharmony_ci		goto cleanup;
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	/* Now punch a hole of the same size */
58f08c3bdfSopenharmony_ci	err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
59f08c3bdfSopenharmony_ci			0, hpage_size * max_iterations);
60f08c3bdfSopenharmony_ci	if (err) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
62f08c3bdfSopenharmony_ci		goto cleanup;
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	free_end = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
66f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(free_end, free_initial);
67f08c3bdfSopenharmony_cicleanup:
68f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic void setup(void)
72f08c3bdfSopenharmony_ci{
73f08c3bdfSopenharmony_ci	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic void cleanup(void)
77f08c3bdfSopenharmony_ci{
78f08c3bdfSopenharmony_ci	if (fd > 0)
79f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic struct tst_test test = {
83f08c3bdfSopenharmony_ci	.needs_root = 1,
84f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
85f08c3bdfSopenharmony_ci	.needs_hugetlbfs = 1,
86f08c3bdfSopenharmony_ci	.setup = setup,
87f08c3bdfSopenharmony_ci	.cleanup = cleanup,
88f08c3bdfSopenharmony_ci	.test_all = run_test,
89f08c3bdfSopenharmony_ci	.hugepages = {3, TST_NEEDS},
90f08c3bdfSopenharmony_ci};
91