• 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 tcase {
40 	int *shmid;
41 	void *addr;
42 	int flags;
43 } tcases[] = {
44 	/* a straight forward read/write attach */
45 	{&shm_id_1, 0, 0},
46 	/*
47 	 * an attach using non aligned memory
48 	 * -1 will be replaced with an unaligned addr
49 	 */
50 	{&shm_id_1, (void *)-1, SHM_RND},
51 	/* a read only attach */
52 	{&shm_id_1, 0, SHM_RDONLY}
53 };
54 
55 static void check_functionality(unsigned int i);
56 
verify_hugeshmat(unsigned int i)57 static void verify_hugeshmat(unsigned int i)
58 {
59 	struct tcase *tc = &tcases[i];
60 
61 	addr = shmat(*(tc->shmid), tc->addr, tc->flags);
62 	if (addr == (void *)-1) {
63 		tst_brk(TFAIL | TERRNO, "shmat");
64 	} else {
65 		check_functionality(i);
66 	}
67 
68 	/*
69 	 * addr in tcases[0] will be used to generate an unaligned
70 	 * address for tcases[1]
71 	 */
72 	if (i == 0 && addr != (void *)-1)
73 		tc[1].addr = (void *)(((unsigned long)addr &
74 					~(SHMLBA - 1)) + SHMLBA - 1);
75 	if (shmdt(addr) == -1)
76 		tst_brk(TBROK | TERRNO, "shmdt");
77 }
78 
79 /*
80  * check_functionality - check various conditions to make sure they
81  *			 are correct.
82  */
check_functionality(unsigned int i)83 static void check_functionality(unsigned int i)
84 {
85 	void *orig_add;
86 	int *shared;
87 	struct shmid_ds buf;
88 
89 	shared = (int *)addr;
90 
91 	/* stat the shared memory ID */
92 	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1)
93 		tst_brk(TBROK | TERRNO, "shmctl");
94 
95 	/* check the number of attaches */
96 	if (buf.shm_nattch != 1) {
97 		tst_res(TFAIL, "# of attaches is incorrect");
98 		return;
99 	}
100 
101 	/* check the size of the segment */
102 	if (buf.shm_segsz != shm_size) {
103 		tst_res(TFAIL, "segment size is incorrect");
104 		return;
105 	}
106 
107 	/* check for specific conditions depending on the type of attach */
108 	switch (i) {
109 	case 0:
110 		/*
111 		 * Check the functionality of the first call by simply
112 		 * "writing" a value to the shared memory space.
113 		 * If this fails the program will get a SIGSEGV, dump
114 		 * core and exit.
115 		 */
116 		*shared = CASE0;
117 		break;
118 	case 1:
119 		/*
120 		 * Check the functionality of the second call by writing
121 		 * a value to the shared memory space and then checking
122 		 * that the original address given was rounded down as
123 		 * specified in the man page.
124 		 */
125 		*shared = CASE1;
126 		orig_add = addr + ((unsigned long)tcases[i].addr % SHMLBA);
127 		if (orig_add != tcases[i].addr) {
128 			tst_res(TFAIL, "shared memory address is not "
129 				 "correct");
130 			return;
131 		}
132 		break;
133 	case 2:
134 		/*
135 		 * This time the shared memory is read only.  Read the value
136 		 * and check that it is equal to the value set in case #2,
137 		 * because shared memory is persistent.
138 		 */
139 		if (*shared != CASE1) {
140 			tst_res(TFAIL, "shared memory value isn't correct");
141 			return;
142 		}
143 		break;
144 	}
145 	tst_res(TPASS, "conditions and functionality are correct");
146 }
147 
setup(void)148 static void setup(void)
149 {
150 	long hpage_size;
151 
152 	if (tst_hugepages == 0)
153 		tst_brk(TCONF, "No enough hugepages for testing.");
154 
155 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
156 
157 	shm_size = hpage_size * tst_hugepages / 2;
158 	update_shm_size(&shm_size);
159 	shmkey = getipckey();
160 	shm_id_1 = shmget(shmkey++, shm_size,
161 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
162 	if (shm_id_1 == -1)
163 		tst_brk(TBROK | TERRNO, "shmget");
164 
165 }
166 
cleanup(void)167 static void cleanup(void)
168 {
169 	rm_shm(shm_id_1);
170 }
171 
172 static struct tst_test test = {
173 	.needs_root = 1,
174 	.needs_tmpdir = 1,
175 	.options = (struct tst_option[]) {
176 		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
177 		{}
178 	},
179 	.tcnt = ARRAY_SIZE(tcases),
180 	.test = verify_hugeshmat,
181 	.setup = setup,
182 	.cleanup = cleanup,
183 	.request_hugepages = 128,
184 };
185