1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2004
4f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2004-2017
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * DESCRIPTION
7f08c3bdfSopenharmony_ci *	hugeshmget03 - test for ENOSPC error
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * HISTORY
10f08c3bdfSopenharmony_ci *	03/2001 - Written by Wayne Boyer
11f08c3bdfSopenharmony_ci *	04/2004 - Updated by Robbie Williamson
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <limits.h>
15f08c3bdfSopenharmony_ci#include "hugetlb.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/*
18f08c3bdfSopenharmony_ci * The MAXIDS value is somewhat arbitrary and may need to be increased
19f08c3bdfSopenharmony_ci * depending on the system being tested.
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci#define MAXIDS	8192
22f08c3bdfSopenharmony_ci#define PATH_SHMMNI	"/proc/sys/kernel/shmmni"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic size_t shm_size;
25f08c3bdfSopenharmony_cistatic int shm_id_1 = -1;
26f08c3bdfSopenharmony_cistatic int num_shms;
27f08c3bdfSopenharmony_cistatic int shm_id_arr[MAXIDS];
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic long orig_shmmni = -1;
30f08c3bdfSopenharmony_cistatic void test_hugeshmget(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	TEST(shmget(IPC_PRIVATE, shm_size,
33f08c3bdfSopenharmony_ci				SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW));
34f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
35f08c3bdfSopenharmony_ci		tst_res(TFAIL, "shmget succeeded unexpectedly");
36f08c3bdfSopenharmony_ci		return;
37f08c3bdfSopenharmony_ci	}
38f08c3bdfSopenharmony_ci	if (TST_ERR == ENOSPC)
39f08c3bdfSopenharmony_ci		tst_res(TPASS | TTERRNO, "shmget failed as expected");
40f08c3bdfSopenharmony_ci	else
41f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
42f08c3bdfSopenharmony_ci				"- expect errno=ENOSPC, got");
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void setup(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	long hpage_size;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if (tst_hugepages == 0)
50f08c3bdfSopenharmony_ci		tst_brk(TCONF, "No enough hugepages for testing.");
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	SAFE_FILE_SCANF(PATH_SHMMNI, "%ld", &orig_shmmni);
53f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(PATH_SHMMNI, "%ld", tst_hugepages / 2);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
56f08c3bdfSopenharmony_ci	shm_size = hpage_size;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	/*
59f08c3bdfSopenharmony_ci	 * Use a while loop to create the maximum number of memory segments.
60f08c3bdfSopenharmony_ci	 * If the loop exceeds MAXIDS, then break the test and cleanup.
61f08c3bdfSopenharmony_ci	 */
62f08c3bdfSopenharmony_ci	num_shms = 0;
63f08c3bdfSopenharmony_ci	shm_id_1 = shmget(IPC_PRIVATE, shm_size,
64f08c3bdfSopenharmony_ci			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
65f08c3bdfSopenharmony_ci	while (shm_id_1 != -1) {
66f08c3bdfSopenharmony_ci		shm_id_arr[num_shms++] = shm_id_1;
67f08c3bdfSopenharmony_ci		if (num_shms == MAXIDS)
68f08c3bdfSopenharmony_ci			tst_brk(TBROK, "The maximum number of "
69f08c3bdfSopenharmony_ci				 "shared memory ID's has been reached. "
70f08c3bdfSopenharmony_ci				 "Please increase the MAXIDS value in "
71f08c3bdfSopenharmony_ci				 "the test.");
72f08c3bdfSopenharmony_ci		shm_id_1 = shmget(IPC_PRIVATE, shm_size,
73f08c3bdfSopenharmony_ci				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci	if (errno != ENOSPC)
76f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "shmget #setup");
77f08c3bdfSopenharmony_ci}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic void cleanup(void)
80f08c3bdfSopenharmony_ci{
81f08c3bdfSopenharmony_ci	int i;
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	for (i = 0; i < num_shms; i++)
84f08c3bdfSopenharmony_ci		rm_shm(shm_id_arr[i]);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	if (orig_shmmni != -1)
87f08c3bdfSopenharmony_ci		SAFE_FILE_PRINTF(PATH_SHMMNI, "%ld", orig_shmmni);
88f08c3bdfSopenharmony_ci}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_cistatic struct tst_test test = {
91f08c3bdfSopenharmony_ci	.needs_root = 1,
92f08c3bdfSopenharmony_ci	.options = (struct tst_option[]) {
93f08c3bdfSopenharmony_ci		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
94f08c3bdfSopenharmony_ci		{}
95f08c3bdfSopenharmony_ci	},
96f08c3bdfSopenharmony_ci	.setup = setup,
97f08c3bdfSopenharmony_ci	.cleanup = cleanup,
98f08c3bdfSopenharmony_ci	.test_all = test_hugeshmget,
99f08c3bdfSopenharmony_ci	.hugepages = {128, TST_REQUEST},
100f08c3bdfSopenharmony_ci};
101