1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2004
4 * Copyright (c) Linux Test Project, 2004-2017
5 *
6 * DESCRIPTION
7 * hugeshmget01 - test that shmget() correctly creates a large
8 * shared memory segment
9 *
10 * HISTORY
11 * 03/2001 - Written by Wayne Boyer
12 * 04/2004 - Updated by Robbie Williamson
13 */
14
15 #include <limits.h>
16 #include "hugetlb.h"
17
18 static size_t shm_size;
19 static int shm_id_1 = -1;
20
test_hugeshmget(void)21 static void test_hugeshmget(void)
22 {
23 struct shmid_ds buf;
24
25 if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
26 tst_res(TFAIL | TERRNO, "shmctl(IPC_STAT)");
27 return;
28 }
29
30 if (buf.shm_segsz != shm_size) {
31 tst_res(TFAIL, "seqment size is not correct");
32 return;
33 }
34
35 if (buf.shm_cpid != getpid()) {
36 tst_res(TFAIL, "creator pid is not correct");
37 return;
38 }
39
40 if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
41 tst_res(TFAIL, "segment mode is not correct");
42 return;
43 }
44
45 tst_res(TPASS, "size, pid & mode are correct");
46 }
47
setup(void)48 static void setup(void)
49 {
50 long hpage_size;
51
52 if (tst_hugepages == 0)
53 tst_brk(TCONF, "No enough hugepages for testing.");
54
55 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
56
57 shm_size = hpage_size * tst_hugepages / 2;
58 update_shm_size(&shm_size);
59 shmkey = getipckey();
60
61 shm_id_1 = shmget(shmkey, shm_size,
62 SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
63 if (shm_id_1 == -1)
64 tst_brk(TBROK | TERRNO, "shmget");
65 }
66
cleanup(void)67 static void cleanup(void)
68 {
69 rm_shm(shm_id_1);
70 }
71
72 static struct tst_test test = {
73 .needs_root = 1,
74 .options = (struct tst_option[]) {
75 {"s:", &nr_opt, "Set the number of the been allocated hugepages"},
76 {}
77 },
78 .setup = setup,
79 .cleanup = cleanup,
80 .test_all = test_hugeshmget,
81 .hugepages = {128, TST_REQUEST},
82 };
83