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
21 static struct tst_option options[] = {
22 {"s:", &nr_opt, "-s num Set the number of the been allocated hugepages"},
23 {NULL, NULL, NULL}
24 };
25
test_hugeshmget(void)26 static void test_hugeshmget(void)
27 {
28 struct shmid_ds buf;
29
30 if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
31 tst_res(TFAIL | TERRNO, "shmctl(IPC_STAT)");
32 return;
33 }
34
35 if (buf.shm_segsz != shm_size) {
36 tst_res(TFAIL, "seqment size is not correct");
37 return;
38 }
39
40 if (buf.shm_cpid != getpid()) {
41 tst_res(TFAIL, "creator pid is not correct");
42 return;
43 }
44
45 if ((buf.shm_perm.mode & MODE_MASK) != ((SHM_RW) & MODE_MASK)) {
46 tst_res(TFAIL, "segment mode is not correct");
47 return;
48 }
49
50 tst_res(TPASS, "size, pid & mode are correct");
51 }
52
setup(void)53 static void setup(void)
54 {
55 long hpage_size;
56
57 if (tst_hugepages == 0)
58 tst_brk(TCONF, "No enough hugepages for testing.");
59
60 hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
61
62 shm_size = hpage_size * tst_hugepages / 2;
63 update_shm_size(&shm_size);
64 shmkey = getipckey();
65
66 shm_id_1 = shmget(shmkey, shm_size,
67 SHM_HUGETLB | IPC_CREAT | IPC_EXCL | SHM_RW);
68 if (shm_id_1 == -1)
69 tst_brk(TBROK | TERRNO, "shmget");
70 }
71
cleanup(void)72 static void cleanup(void)
73 {
74 rm_shm(shm_id_1);
75 }
76
77 static struct tst_test test = {
78 .needs_root = 1,
79 .options = options,
80 .setup = setup,
81 .cleanup = cleanup,
82 .test_all = test_hugeshmget,
83 .request_hugepages = 128,
84 };
85