• 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 struct tcase {
43 	int *shmid;
44 	void *addr;
45 	int error;
46 } tcases[] = {
47 	/* EINVAL - the shared memory ID is not valid */
48 	{&shm_id_1, NULL, EINVAL},
49 	/* EINVAL - the address is not page aligned and SHM_RND is not given */
50 	{&shm_id_2, (void *)NADDR, EINVAL}
51 };
52 
verify_hugeshmat(unsigned int i)53 static void verify_hugeshmat(unsigned int i)
54 {
55 	struct tcase *tc = &tcases[i];
56 
57 	addr = shmat(*(tc->shmid), tc->addr, 0);
58 	if (addr != (void *)-1) {
59 		tst_res(TFAIL, "shmat suceeded unexpectedly");
60 		return;
61 	}
62 
63 	if (errno == tc->error) {
64 		tst_res(TPASS | TERRNO, "shmat failed as "
65 				"expected");
66 	} else {
67 		tst_res(TFAIL | TERRNO, "shmat failed "
68 				"unexpectedly - expect errno=%d, "
69 				"got", tc->error);
70 	}
71 }
72 
setup(void)73 void setup(void)
74 {
75 	long hpage_size;
76 
77 	if (tst_hugepages == 0)
78 		tst_brk(TCONF, "No enough hugepages for testing.");
79 
80 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
81 
82 	shm_size = hpage_size * tst_hugepages / 2;
83 	update_shm_size(&shm_size);
84 	shmkey = getipckey();
85 
86 	/* create a shared memory resource with read and write permissions */
87 	/* also post increment the shmkey for the next shmget call */
88 	shm_id_2 = shmget(shmkey++, shm_size,
89 			  SHM_HUGETLB | SHM_RW | IPC_CREAT | IPC_EXCL);
90 	if (shm_id_2 == -1)
91 		tst_brk(TBROK | TERRNO, "shmget");
92 }
93 
cleanup(void)94 void cleanup(void)
95 {
96 	rm_shm(shm_id_2);
97 }
98 
99 static struct tst_test test = {
100 	.needs_root = 1,
101 	.needs_tmpdir = 1,
102 	.options = (struct tst_option[]) {
103 		{"s:", &nr_opt, "Set the number of the been allocated hugepages"},
104 		{}
105 	},
106 	.tcnt = ARRAY_SIZE(tcases),
107 	.test = verify_hugeshmat,
108 	.setup = setup,
109 	.cleanup = cleanup,
110 	.hugepages = {128, TST_REQUEST},
111 };
112