1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2014-2017
4 *
5 * DESCRIPTION
6 * hugeshmat04 - test for hugepage leak inspection.
7 *
8 * It is a regression test for shared hugepage leak, when over 1GB
9 * shared memory was alocated in hugepage, the hugepage is not released
10 * though process finished.
11 *
12 * You need more than 2GB memory in test job
13 *
14 * HISTORY
15 * 05/2014 - Written by Fujistu Corp.
16 * 12/2014 - Port to LTP by Li Wang.
17 *
18 * RESTRICTIONS
19 * test must be run at root
20 */
21
22 #include "hugetlb.h"
23
24 #define SIZE (1024 * 1024 * 1024)
25 #define BOUNDARY (1024 * 1024 * 1024)
26 #define BOUNDARY_MAX (3U * 1024 * 1024 * 1024)
27
28 static long huge_free;
29 static long huge_free2;
30 static long orig_shmmax = -1, new_shmmax;
31
32 static void shared_hugepage(void);
33
test_hugeshmat(unsigned int i LTP_ATTRIBUTE_UNUSED)34 static void test_hugeshmat(unsigned int i LTP_ATTRIBUTE_UNUSED)
35 {
36 huge_free = SAFE_READ_MEMINFO("HugePages_Free:");
37 shared_hugepage();
38 huge_free2 = SAFE_READ_MEMINFO("HugePages_Free:");
39
40 if (huge_free2 != huge_free)
41 tst_brk(TFAIL, "Test failed. Hugepage leak inspection.");
42 else
43 tst_res(TPASS, "No regression found.");
44 }
45
shared_hugepage(void)46 static void shared_hugepage(void)
47 {
48 pid_t pid;
49 int status, shmid;
50 size_t size = (size_t)SIZE;
51 void *buf;
52 unsigned long boundary = BOUNDARY;
53
54 shmid = shmget(IPC_PRIVATE, size, SHM_HUGETLB | IPC_CREAT | 0777);
55 if (shmid < 0)
56 tst_brk(TBROK | TERRNO, "shmget");
57
58 while (boundary <= BOUNDARY_MAX
59 && range_is_mapped(boundary, boundary+SIZE))
60 boundary += 128*1024*1024;
61 if (boundary > BOUNDARY_MAX)
62 tst_brk(TCONF, "failed to find free unmapped range");
63
64 tst_res(TINFO, "attaching at 0x%lx", boundary);
65 buf = shmat(shmid, (void *)boundary, SHM_RND | 0777);
66 if (buf == (void *)-1) {
67 shmctl(shmid, IPC_RMID, NULL);
68 tst_brk(TBROK | TERRNO, "shmat");
69 }
70
71 memset(buf, 2, size);
72 pid = SAFE_FORK();
73 if (pid == 0)
74 exit(1);
75
76 wait(&status);
77 shmdt(buf);
78 shmctl(shmid, IPC_RMID, NULL);
79 }
80
setup(void)81 static void setup(void)
82 {
83 long hpage_size, orig_hugepages;
84
85 orig_hugepages = get_sys_tune("nr_hugepages");
86 SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
87 SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
88 SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &new_shmmax);
89
90 if (new_shmmax < SIZE)
91 tst_brk(TCONF, "shmmax too low, have: %ld", new_shmmax);
92
93 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
94
95 struct tst_hugepage hp = { orig_hugepages + SIZE / hpage_size, TST_NEEDS };
96 tst_reserve_hugepages(&hp);
97 }
98
cleanup(void)99 static void cleanup(void)
100 {
101 if (orig_shmmax != -1)
102 SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", orig_shmmax);
103 }
104
105 static struct tst_test test = {
106 .tags = (const struct tst_tag[]) {
107 {"linux-git", "c5c99429fa57"},
108 {}
109 },
110 .needs_root = 1,
111 .forks_child = 1,
112 .needs_tmpdir = 1,
113 .tcnt = 3,
114 .test = test_hugeshmat,
115 .min_mem_avail = 2048,
116 .setup = setup,
117 .cleanup = cleanup,
118 .hugepages = {1, TST_NEEDS},
119 };
120