• 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 	if (tst_hugepages != test.request_hugepages)
39 		tst_brk(TCONF, "Not enough hugepages for testing.");
40 
41 	page_size = getpagesize();
42 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
43 }
44 
shm_test(int size)45 void shm_test(int size)
46 {
47 	int shmid;
48 	char *shmaddr;
49 
50 	shmid = shmget(IPC_PRIVATE, size, 0600 | IPC_CREAT | SHM_HUGETLB);
51 	if (shmid < 0)
52 		tst_brk(TBROK | TERRNO, "shmget failed");
53 
54 	shmaddr = shmat(shmid, 0, 0);
55 	if (shmaddr == (char *)-1) {
56 		shmctl(shmid, IPC_RMID, NULL);
57 		tst_brk(TFAIL | TERRNO,
58 			 "Bug: shared memory attach failure.");
59 	}
60 
61 	shmaddr[0] = 1;
62 	tst_res(TINFO, "allocated %d huge bytes", size);
63 
64 	if (shmdt((const void *)shmaddr) != 0) {
65 		shmctl(shmid, IPC_RMID, NULL);
66 		tst_brk(TFAIL | TERRNO, "Detach failure.");
67 	}
68 
69 	shmctl(shmid, IPC_RMID, NULL);
70 }
71 
test_hugeshmat(void)72 static void test_hugeshmat(void)
73 {
74 	unsigned int i;
75 
76 	const int tst_sizes[] = {
77 		N * hpage_size - page_size,
78 		N * hpage_size - page_size - 1,
79 		hpage_size,
80 		hpage_size + 1
81 	};
82 
83 	for (i = 0; i < ARRAY_SIZE(tst_sizes); ++i)
84 		shm_test(tst_sizes[i]);
85 
86 	tst_res(TPASS, "No regression found.");
87 }
88 
89 static struct tst_test test = {
90 	.needs_root = 1,
91 	.needs_tmpdir = 1,
92 	.test_all = test_hugeshmat,
93 	.setup = setup,
94 	.request_hugepages = N + 1,
95 	.tags = (const struct tst_tag[]) {
96 		{"linux-git", "091d0d55b286"},
97 		{"linux-git", "af73e4d9506d"},
98 		{"linux-git", "42d7395feb56"},
99 		{"linux-git", "40716e29243d"},
100 		{}
101 	}
102 };
103