• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Copyright (c) Linux Test Project, 2001-2017
5  *
6  * DESCRIPTION
7  *	hugeshmat01 - test that shmat() works correctly
8  *
9  * ALGORITHM
10  *	create a large shared memory resouce with read/write permissions
11  *	loop if that option was specified
12  *	call shmat() with the TEST() macro using three valid conditions
13  *	check the return code
14  *	  if failure, issue a FAIL message.
15  *	otherwise,
16  *	  if doing functionality testing
17  *		check for the correct conditions after the call
18  *		if correct,
19  *			issue a PASS message
20  *		otherwise
21  *			issue a FAIL message
22  *	call cleanup
23  *
24  * HISTORY
25  *	03/2001 - Written by Wayne Boyer
26  *	04/2004 - Updated by Robbie Williamson
27  */
28 
29 #include <limits.h>
30 #include "hugetlb.h"
31 
32 #define CASE0 10 /* values to write into the shared */
33 #define CASE1 20 /* memory location.                */
34 
35 static size_t shm_size;
36 static int shm_id_1 = -1;
37 static void *addr;
38 
39 static struct tst_option options[] = {
40 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
41 	{NULL, NULL, NULL}
42 };
43 
44 static struct tcase {
45 	int *shmid;
46 	void *addr;
47 	int flags;
48 } tcases[] = {
49 	/* a straight forward read/write attach */
50 	{&shm_id_1, 0, 0},
51 	/*
52 	 * an attach using non aligned memory
53 	 * -1 will be replaced with an unaligned addr
54 	 */
55 	{&shm_id_1, (void *)-1, SHM_RND},
56 	/* a read only attach */
57 	{&shm_id_1, 0, SHM_RDONLY}
58 };
59 
60 static void check_functionality(unsigned int i);
61 
verify_hugeshmat(unsigned int i)62 static void verify_hugeshmat(unsigned int i)
63 {
64 	struct tcase *tc = &tcases[i];
65 
66 	addr = shmat(*(tc->shmid), tc->addr, tc->flags);
67 	if (addr == (void *)-1) {
68 		tst_brk(TFAIL | TERRNO, "shmat");
69 	} else {
70 		check_functionality(i);
71 	}
72 
73 	/*
74 	 * addr in tcases[0] will be used to generate an unaligned
75 	 * address for tcases[1]
76 	 */
77 	if (i == 0 && addr != (void *)-1)
78 		tc[1].addr = (void *)(((unsigned long)addr &
79 					~(SHMLBA - 1)) + SHMLBA - 1);
80 	if (shmdt(addr) == -1)
81 		tst_brk(TBROK | TERRNO, "shmdt");
82 }
83 
84 /*
85  * check_functionality - check various conditions to make sure they
86  *			 are correct.
87  */
check_functionality(unsigned int i)88 static void check_functionality(unsigned int i)
89 {
90 	void *orig_add;
91 	int *shared;
92 	struct shmid_ds buf;
93 
94 	shared = (int *)addr;
95 
96 	/* stat the shared memory ID */
97 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
98 		tst_brk(TBROK | TERRNO, "shmctl");
99 
100 	/* check the number of attaches */
101 	if (buf.shm_nattch != 1) {
102 		tst_res(TFAIL, "# of attaches is incorrect");
103 		return;
104 	}
105 
106 	/* check the size of the segment */
107 	if (buf.shm_segsz != shm_size) {
108 		tst_res(TFAIL, "segment size is incorrect");
109 		return;
110 	}
111 
112 	/* check for specific conditions depending on the type of attach */
113 	switch (i) {
114 	case 0:
115 		/*
116 		 * Check the functionality of the first call by simply
117 		 * "writing" a value to the shared memory space.
118 		 * If this fails the program will get a SIGSEGV, dump
119 		 * core and exit.
120 		 */
121 		*shared = CASE0;
122 		break;
123 	case 1:
124 		/*
125 		 * Check the functionality of the second call by writing
126 		 * a value to the shared memory space and then checking
127 		 * that the original address given was rounded down as
128 		 * specified in the man page.
129 		 */
130 		*shared = CASE1;
131 		orig_add = addr + ((unsigned long)tcases[i].addr % SHMLBA);
132 		if (orig_add != tcases[i].addr) {
133 			tst_res(TFAIL, "shared memory address is not "
134 				 "correct");
135 			return;
136 		}
137 		break;
138 	case 2:
139 		/*
140 		 * This time the shared memory is read only.  Read the value
141 		 * and check that it is equal to the value set in case #2,
142 		 * because shared memory is persistent.
143 		 */
144 		if (*shared != CASE1) {
145 			tst_res(TFAIL, "shared memory value isn't correct");
146 			return;
147 		}
148 		break;
149 	}
150 	tst_res(TPASS, "conditions and functionality are correct");
151 }
152 
setup(void)153 static void setup(void)
154 {
155 	long hpage_size;
156 
157 	if (tst_hugepages == 0)
158 		tst_brk(TCONF, "No enough hugepages for testing.");
159 
160 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
161 
162 	shm_size = hpage_size * tst_hugepages / 2;
163 	update_shm_size(&shm_size);
164 	shmkey = getipckey();
165 	shm_id_1 = shmget(shmkey++, shm_size,
166 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
167 	if (shm_id_1 == -1)
168 		tst_brk(TBROK | TERRNO, "shmget");
169 
170 }
171 
cleanup(void)172 static void cleanup(void)
173 {
174 	rm_shm(shm_id_1);
175 }
176 
177 static struct tst_test test = {
178 	.needs_root = 1,
179 	.needs_tmpdir = 1,
180 	.options = options,
181 	.tcnt = ARRAY_SIZE(tcases),
182 	.test = verify_hugeshmat,
183 	.setup = setup,
184 	.cleanup = cleanup,
185 	.request_hugepages = 128,
186 };
187