• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2015-2017 Red Hat, Inc.
4  *
5  *
6  * DESCRIPTION
7  *	shmget()/shmat() fails to allocate huge pages shared memory segment
8  *	with EINVAL if its size is not in the range [ N*HUGE_PAGE_SIZE - 4095,
9  *	N*HUGE_PAGE_SIZE ]. This is a problem in the memory segment size round
10  *	up algorithm. The requested size is rounded up to PAGE_SIZE (4096), but
11  *	if this roundup does not match HUGE_PAGE_SIZE (2Mb) boundary - the
12  *	allocation fails.
13  *
14  *	This bug is present in all RHEL6 versions, but not in RHEL7. It looks
15  *	like this was fixed in mainline kernel > v3.3 by the following patches:
16  *
17  *	091d0d55b286 (shm: fix null pointer deref when userspace specifies
18  *		 invalid hugepage size)
19  *	af73e4d9506d (hugetlbfs: fix mmap failure in unaligned size request)
20  *	42d7395feb56 (mm: support more pagesizes for MAP_HUGETLB/SHM_HUGETLB)
21  *	40716e29243d (hugetlbfs: fix alignment of huge page requests)
22  *
23  * AUTHORS
24  *	Vladislav Dronov <vdronov@redhat.com>
25  *	Li Wang <liwang@redhat.com>
26  *
27  */
28 
29 #include "hugetlb.h"
30 
31 static long page_size;
32 static long hpage_size;
33 
34 #define N 4
35 
setup(void)36 void setup(void)
37 {
38 	page_size = getpagesize();
39 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
40 }
41 
shm_test(int size)42 void shm_test(int size)
43 {
44 	int shmid;
45 	char *shmaddr;
46 
47 	shmid = shmget(IPC_PRIVATE, size, 0600 | IPC_CREAT | SHM_HUGETLB);
48 	if (shmid < 0)
49 		tst_brk(TBROK | TERRNO, "shmget failed");
50 
51 	shmaddr = shmat(shmid, 0, 0);
52 	if (shmaddr == (char *)-1) {
53 		shmctl(shmid, IPC_RMID, NULL);
54 		tst_brk(TFAIL | TERRNO,
55 			 "Bug: shared memory attach failure.");
56 	}
57 
58 	shmaddr[0] = 1;
59 	tst_res(TINFO, "allocated %d huge bytes", size);
60 
61 	if (shmdt((const void *)shmaddr) != 0) {
62 		shmctl(shmid, IPC_RMID, NULL);
63 		tst_brk(TFAIL | TERRNO, "Detach failure.");
64 	}
65 
66 	shmctl(shmid, IPC_RMID, NULL);
67 }
68 
test_hugeshmat(void)69 static void test_hugeshmat(void)
70 {
71 	unsigned int i;
72 
73 	const int tst_sizes[] = {
74 		N * hpage_size - page_size,
75 		N * hpage_size - page_size - 1,
76 		hpage_size,
77 		hpage_size + 1
78 	};
79 
80 	for (i = 0; i < ARRAY_SIZE(tst_sizes); ++i)
81 		shm_test(tst_sizes[i]);
82 
83 	tst_res(TPASS, "No regression found.");
84 }
85 
86 static struct tst_test test = {
87 	.needs_root = 1,
88 	.needs_tmpdir = 1,
89 	.test_all = test_hugeshmat,
90 	.setup = setup,
91 	.hugepages = {N+1, TST_NEEDS},
92 	.tags = (const struct tst_tag[]) {
93 		{"linux-git", "091d0d55b286"},
94 		{"linux-git", "af73e4d9506d"},
95 		{"linux-git", "42d7395feb56"},
96 		{"linux-git", "40716e29243d"},
97 		{}
98 	}
99 };
100