• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *	hugeshmat02 - check for EINVAL and EACCES errors with hugetlb
8  *
9  * ALGORITHM
10  *	loop if that option was specified
11  *	  call shmat() using three invalid test cases
12  *	  check the errno value
13  *	    issue a PASS message if we get EINVAL or EACCES
14  *	  otherwise, the tests fails
15  *	    issue a FAIL message
16  *	call cleanup
17  *
18  * HISTORY
19  *	03/2001 - Written by Wayne Boyer
20  *	04/2004 - Updated By Robbie Williamson
21  *
22  * RESTRICTIONS
23  *	Must be ran as root
24  */
25 
26 #include <pwd.h>
27 #include <limits.h>
28 #include "lapi/abisize.h"
29 #include "hugetlb.h"
30 
31 #ifdef TST_ABI64
32 #define NADDR	0x10000000eef	/* a 64bit non alligned address value */
33 #else
34 #define NADDR	0x60000eef	/* a non alligned address value */
35 #endif
36 
37 static size_t shm_size;
38 static int shm_id_1 = -1;
39 static int shm_id_2 = -1;
40 static void *addr;
41 
42 static struct tst_option options[] = {
43 	{"s:", &nr_opt, "-s num   Set the number of the been allocated hugepages"},
44 	{NULL, NULL, NULL}
45 };
46 
47 struct tcase {
48 	int *shmid;
49 	void *addr;
50 	int error;
51 } tcases[] = {
52 	/* EINVAL - the shared memory ID is not valid */
53 	{&shm_id_1, NULL, EINVAL},
54 	/* EINVAL - the address is not page aligned and SHM_RND is not given */
55 	{&shm_id_2, (void *)NADDR, EINVAL}
56 };
57 
verify_hugeshmat(unsigned int i)58 static void verify_hugeshmat(unsigned int i)
59 {
60 	struct tcase *tc = &tcases[i];
61 
62 	addr = shmat(*(tc->shmid), tc->addr, 0);
63 	if (addr != (void *)-1) {
64 		tst_res(TFAIL, "shmat suceeded unexpectedly");
65 		return;
66 	}
67 
68 	if (errno == tc->error) {
69 		tst_res(TPASS | TERRNO, "shmat failed as "
70 				"expected");
71 	} else {
72 		tst_res(TFAIL | TERRNO, "shmat failed "
73 				"unexpectedly - expect errno=%d, "
74 				"got", tc->error);
75 	}
76 }
77 
setup(void)78 void setup(void)
79 {
80 	long hpage_size;
81 
82 	if (tst_hugepages == 0)
83 		tst_brk(TCONF, "No enough hugepages for testing.");
84 
85 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
86 
87 	shm_size = hpage_size * tst_hugepages / 2;
88 	update_shm_size(&shm_size);
89 	shmkey = getipckey();
90 
91 	/* create a shared memory resource with read and write permissions */
92 	/* also post increment the shmkey for the next shmget call */
93 	shm_id_2 = shmget(shmkey++, shm_size,
94 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
95 	if (shm_id_2 == -1)
96 		tst_brk(TBROK | TERRNO, "shmget");
97 }
98 
cleanup(void)99 void cleanup(void)
100 {
101 	rm_shm(shm_id_2);
102 }
103 
104 static struct tst_test test = {
105 	.needs_root = 1,
106 	.needs_tmpdir = 1,
107 	.options = options,
108 	.tcnt = ARRAY_SIZE(tcases),
109 	.test = verify_hugeshmat,
110 	.setup = setup,
111 	.cleanup = cleanup,
112 	.request_hugepages = 128,
113 };
114