aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
blob: 04325ce1047fee95f47186a4fd73b29e05d740fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) International Business Machines  Corp., 2004
 * Copyright (c) Linux Test Project, 2004-2023
 * Original author: Wayne Boyer, modified by Robbie Williamson
 */

/*\
 * [Description]
 *
 * Test the IPC_STAT, IPC_SET and IPC_RMID commands used by shmctl().
 */

#include <limits.h>
#include "hugetlb.h"

#define N_ATTACH	4U
#define NEWMODE		0066

static size_t shm_size;
static int shm_id_1 = -1;
static struct shmid_ds buf;
static time_t save_time;
static void *attach_to_parent;

static void stat_setup_1(void);
static void stat_cleanup(void);
static void stat_setup_2(void);
static void set_setup(void);
static void func_stat(void);
static void func_set(void);
static void func_rmid(void);
static void *set_shmat(void);

static struct tcase {
	int cmd;
	void (*func_test)(void);
	void (*func_setup)(void);
} tcases[] = {
	{IPC_STAT, func_stat, stat_setup_1},
	{IPC_STAT, func_stat, stat_setup_2},
	{IPC_SET,  func_set,  set_setup},
	{IPC_RMID, func_rmid, NULL}
};

static void test_hugeshmctl(unsigned int i)
{
	/*
	 * Create a shared memory segment with read and write
	 * permissions.  Do this here instead of in setup()
	 * so that looping (-i) will work correctly.
	 */
	if (i == 0) {
		shm_id_1 = shmget(shmkey, shm_size,
			SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
	}

	if (shm_id_1 == -1)
		tst_brk(TBROK | TERRNO, "shmget #main");

	if (tcases[i].func_setup != NULL)
		(*tcases[i].func_setup) ();

	if (shmctl(shm_id_1, tcases[i].cmd, &buf) == -1) {
		tst_res(TFAIL | TERRNO, "shmctl #main");
		return;
	}
	(*tcases[i].func_test)();
}

/*
 * set_shmat() - Attach the shared memory and return the pointer.
 */
static void *set_shmat(void)
{
	void *rval;

	rval = shmat(shm_id_1, 0, 0);
	if (rval == (void *)-1)
		tst_brk(TBROK | TERRNO, "set shmat");

	return rval;
}

/*
 * stat_setup_2() - Set up for the IPC_STAT command with shmctl().
 *                Attach the shared memory to parent process and
 *                some children will inherit the shared memory.
 */
static void stat_setup_2(void)
{
	if (!attach_to_parent)
		attach_to_parent = set_shmat();
	stat_setup_1();
}

/*
 * stat_setup_1() - Set up for the IPC_STAT command with shmctl().
 *                some children will inherit or attatch the shared memory.
 *                It deponds on whther we attach the shared memory
 *                to parent process.
 */
static void stat_setup_1(void)
{
	unsigned int i;
	void *test;
	pid_t pid;

	for (i = 0; i < N_ATTACH; i++) {
		switch (pid = SAFE_FORK()) {
		case 0:
			test = (attach_to_parent == NULL) ? set_shmat() : attach_to_parent;
			/* do an assignement for fun */
			*(int *)test = i;

			TST_CHECKPOINT_WAKE(0);

			TST_CHECKPOINT_WAIT(1);

			/* now we're back - detach the memory and exit */
			if (shmdt(test) == -1)
				tst_brk(TBROK | TERRNO,
					 "shmdt in this function broke");

			exit(0);
		default:
			TST_CHECKPOINT_WAIT(0);
		}
	}
}


/*
 * func_stat() - check the functionality of the IPC_STAT command with shmctl()
 *		 by looking at the pid of the creator, the segement size,
 *		 the number of attaches and the mode.
 */
static void func_stat(void)
{
	pid_t pid;
	unsigned int num;

	/* check perm, pid, nattach and size */
	pid = getpid();

	if (buf.shm_cpid != pid) {
		tst_res(TFAIL, "creator pid is incorrect");
		goto fail;
	}

	if (buf.shm_segsz != shm_size) {
		tst_res(TFAIL, "segment size is incorrect");
		goto fail;
	}

	/*
	 * The first case, only the children attach the memory, so
	 * the attaches equal N_ATTACH. The second case, the parent
	 * attaches the memory and the children inherit that memory
	 * so the attaches equal N_ATTACH + 1.
	 */
	num = (attach_to_parent == NULL) ? 0 : 1;
	if (buf.shm_nattch != N_ATTACH + num) {
		tst_res(TFAIL, "# of attaches is incorrect - %lu",
			 (unsigned long)buf.shm_nattch);
		goto fail;
	}

	/* use MODE_MASK to make sure we are comparing the last 9 bits */
	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
		tst_res(TFAIL, "segment mode is incorrect");
		goto fail;
	}

	tst_res(TPASS, "pid, size, # of attaches and mode are correct "
		 "- pass #%d", num);

fail:
	stat_cleanup();

	/* save the change time for use in the next test */
	save_time = buf.shm_ctime;
}

/*
 * stat_cleanup() - signal the children to clean up after themselves and
 *		    have the parent make dessert, er, um, make that remove
 *		    the shared memory that is no longer needed.
 */
static void stat_cleanup(void)
{
	unsigned int i;
	int status;

	/* wake up the childern so they can detach the memory and exit */
	TST_CHECKPOINT_WAKE2(1, N_ATTACH);

	for (i = 0; i < N_ATTACH; i++)
		SAFE_WAIT(&status);

	/* remove the parent's shared memory if we set*/
	if (attach_to_parent) {
		if (shmdt(attach_to_parent) == -1)
			tst_res(TFAIL | TERRNO,
				"shmdt in this function failed");
		attach_to_parent = NULL;
	}
}

/*
 * set_setup() - set up for the IPC_SET command with shmctl()
 */
static void set_setup(void)
{
	/* set up a new mode for the shared memory segment */
	buf.shm_perm.mode = SHM_RW | NEWMODE;

	/* sleep for one second to get a different shm_ctime value */
	sleep(1);
}

/*
 * func_set() - check the functionality of the IPC_SET command with shmctl()
 */
static void func_set(void)
{
	/* first stat the shared memory to get the new data */
	if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
		tst_res(TFAIL | TERRNO, "shmctl in this function failed");
		return;
	}

	if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW | NEWMODE) & MODE_MASK)) {
		tst_res(TFAIL, "new mode is incorrect");
		return;
	}

	if (save_time >= buf.shm_ctime) {
		tst_res(TFAIL, "change time is incorrect");
		return;
	}

	tst_res(TPASS, "new mode and change time are correct");
}

/*
 * func_rmid() - check the functionality of the IPC_RMID command with shmctl()
 */
static void func_rmid(void)
{
	/* Do another shmctl() - we should get EINVAL */
	if (shmctl(shm_id_1, IPC_STAT, &buf) != -1)
		tst_brk(TBROK, "shmctl in this function "
			 "succeeded unexpectedly");
	if (errno != EINVAL)
		tst_res(TFAIL | TERRNO, "shmctl in this function failed "
			 "unexpectedly - expect errno=EINVAL, got");
	else
		tst_res(TPASS, "shmctl in this function failed as expected, "
			 "shared memory appears to be removed");
	shm_id_1 = -1;
}

static void setup(void)
{
	long hpage_size;

	if (tst_hugepages == 0)
		tst_brk(TCONF, "No enough hugepages for testing.");

	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;

	shm_size = hpage_size * tst_hugepages / 2;
	update_shm_size(&shm_size);
	shmkey = getipckey();
}

static void cleanup(void)
{
	rm_shm(shm_id_1);
}

static struct tst_test test = {
	.tcnt = ARRAY_SIZE(tcases),
	.needs_root = 1,
	.forks_child = 1,
	.options = (struct tst_option[]) {
		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
		{}
	},
	.setup = setup,
	.cleanup = cleanup,
	.test = test_hugeshmctl,
	.needs_checkpoints = 1,
	.hugepages = {128, TST_REQUEST},
};