• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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 	set_sys_tune("nr_hugepages", hugepages, 1);
74 	SAFE_FILE_PRINTF(PATH_SHMMNI, "%ld", hugepages / 2);
75 
76 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
77 	shm_size = hpage_size;
78 
79 	/*
80 	 * Use a while loop to create the maximum number of memory segments.
81 	 * If the loop exceeds MAXIDS, then break the test and cleanup.
82 	 */
83 	num_shms = 0;
84 	shm_id_1 = shmget(IPC_PRIVATE, shm_size,
85 			  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
86 	while (shm_id_1 != -1) {
87 		shm_id_arr[num_shms++] = shm_id_1;
88 		if (num_shms == MAXIDS)
89 			tst_brk(TBROK, "The maximum number of "
90 				 "shared memory ID's has been reached. "
91 				 "Please increase the MAXIDS value in "
92 				 "the test.");
93 		shm_id_1 = shmget(IPC_PRIVATE, shm_size,
94 				  SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
95 	}
96 	if (errno != ENOSPC)
97 		tst_brk(TBROK | TERRNO, "shmget #setup");
98 }
99 
cleanup(void)100 static void cleanup(void)
101 {
102 	int i;
103 
104 	for (i = 0; i < num_shms; i++)
105 		rm_shm(shm_id_arr[i]);
106 
107 	FILE_PRINTF(PATH_SHMMNI, "%ld", orig_shmmni);
108 	restore_nr_hugepages();
109 }
110 
111 static struct tst_test test = {
112 	.needs_root = 1,
113 	.options = options,
114 	.setup = setup,
115 	.cleanup = cleanup,
116 	.test_all = test_hugeshmget,
117 };
118