1 /*
2  * Copyright (c) International Business Machines  Corp., 2004
3  * Copyright (c) Linux Test Project, 2004-2017
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  */
15 
16 /*
17  * DESCRIPTION
18  *	hugeshmget03 - test for ENOSPC error
19  *
20  * HISTORY
21  *	03/2001 - Written by Wayne Boyer
22  *	04/2004 - Updated by Robbie Williamson
23  */
24 
25 #include <limits.h>
26 #include "hugetlb.h"
27 #include "hugetlb.h"
28 
29 /*
30  * The MAXIDS value is somewhat arbitrary and may need to be increased
31  * depending on the system being tested.
32  */
33 #define MAXIDS	8192
34 #define PATH_SHMMNI	"/proc/sys/kernel/shmmni"
35 
36 static size_t shm_size;
37 static int shm_id_1 = -1;
38 static int num_shms;
39 static int shm_id_arr[MAXIDS];
40 
41 static long hugepages = 128;
42 static long orig_shmmni = -1;
43 static struct tst_option options[] = {
44 	{"s:", &nr_opt, "-s   num  Set the number of the been allocated hugepages"},
45 	{NULL, NULL, NULL}
46 };
47 
test_hugeshmget(void)48 static void test_hugeshmget(void)
49 {
50 	TEST(shmget(IPC_PRIVATE, shm_size,
51 				SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW));
52 	if (TST_RET != -1) {
53 		tst_res(TFAIL, "shmget succeeded unexpectedly");
54 		return;
55 	}
56 	if (TST_ERR == ENOSPC)
57 		tst_res(TPASS | TTERRNO, "shmget failed as expected");
58 	else
59 		tst_res(TFAIL | TTERRNO, "shmget failed unexpectedly "
60 				"- expect errno=ENOSPC, got");
61 }
62 
setup(void)63 static void setup(void)
64 {
65 	long hpage_size;
66 
67 	save_nr_hugepages();
68 	if (nr_opt)
69 		hugepages = SAFE_STRTOL(nr_opt, 0, LONG_MAX);
70 
71 	SAFE_FILE_SCANF(PATH_SHMMNI, "%ld", &orig_shmmni);
72 
73 	limit_hugepages(&hugepages);
74 	set_sys_tune("nr_hugepages", hugepages, 1);
75 	SAFE_FILE_PRINTF(PATH_SHMMNI, "%ld", hugepages / 2);
76 
77 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
78 	shm_size = hpage_size;
79 
80 	/*
81 	 * Use a while loop to create the maximum number of memory segments.
82 	 * If the loop exceeds MAXIDS, then break the test and cleanup.
83 	 */
84 	num_shms = 0;
85 	shm_id_1 = shmget(IPC_PRIVATE, shm_size,
86 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
87 	while (shm_id_1 != -1) {
88 		shm_id_arr[num_shms++] = shm_id_1;
89 		if (num_shms == MAXIDS)
90 			tst_brk(TBROK, "The maximum number of "
91 				 "shared memory ID's has been reached. "
92 				 "Please increase the MAXIDS value in "
93 				 "the test.");
94 		shm_id_1 = shmget(IPC_PRIVATE, shm_size,
95 				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
96 	}
97 	if (errno != ENOSPC)
98 		tst_brk(TBROK | TERRNO, "shmget #setup");
99 }
100 
cleanup(void)101 static void cleanup(void)
102 {
103 	int i;
104 
105 	for (i = 0; i < num_shms; i++)
106 		rm_shm(shm_id_arr[i]);
107 
108 	if (orig_shmmni != -1)
109 		FILE_PRINTF(PATH_SHMMNI, "%ld", orig_shmmni);
110 	restore_nr_hugepages();
111 }
112 
113 static struct tst_test test = {
114 	.needs_root = 1,
115 	.options = options,
116 	.setup = setup,
117 	.cleanup = cleanup,
118 	.test_all = test_hugeshmget,
119 };
120