|
Packit |
2d622a |
/*
|
|
Packit |
2d622a |
* libhugetlbfs - Easy use of Linux hugepages
|
|
Packit |
2d622a |
* Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* This library is free software; you can redistribute it and/or
|
|
Packit |
2d622a |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
2d622a |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
2d622a |
* the License, or (at your option) any later version.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* This library is distributed in the hope that it will be useful, but
|
|
Packit |
2d622a |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
2d622a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
2d622a |
* Lesser General Public License for more details.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
2d622a |
* License along with this library; if not, write to the Free Software
|
|
Packit |
2d622a |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
2d622a |
*/
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#define _GNU_SOURCE
|
|
Packit |
2d622a |
#include <stdlib.h>
|
|
Packit |
2d622a |
#include <stdio.h>
|
|
Packit |
2d622a |
#include <string.h>
|
|
Packit |
2d622a |
#include <unistd.h>
|
|
Packit |
2d622a |
#include <sys/mman.h>
|
|
Packit |
2d622a |
#include <sys/resource.h>
|
|
Packit |
2d622a |
#include <sys/wait.h>
|
|
Packit |
a158b3 |
#include <sched.h>
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#include <hugetlbfs.h>
|
|
Packit |
2d622a |
#include "hugetests.h"
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
/*
|
|
Packit |
2d622a |
* Test rationale:
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* On PowerPC, the address space is divided into segments. These segments can
|
|
Packit |
2d622a |
* contain either huge pages or normal pages, but not both. All segments are
|
|
Packit |
2d622a |
* initially set up to map normal pages. When a huge page mapping is created
|
|
Packit |
2d622a |
* within a set of empty segments, they are "enabled" for huge pages at that
|
|
Packit |
2d622a |
* time. Once enabled for huge pages, they can not be used again for normal
|
|
Packit |
2d622a |
* pages for the remaining lifetime of the process.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* If the segment immediately preceeding the segment containing the stack is
|
|
Packit |
2d622a |
* converted to huge pages and the stack is made to grow into the this
|
|
Packit |
2d622a |
* preceeding segment, some kernels may attempt to map normal pages into the
|
|
Packit |
2d622a |
* huge page-only segment -- resulting in bugs.
|
|
Packit |
2d622a |
*
|
|
Packit |
2d622a |
* The kernel bug in question was fixed by commit
|
|
Packit |
2d622a |
* 0d59a01bc461bbab4017ff449b8401151ef44cf6.
|
|
Packit |
2d622a |
*/
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
#ifdef __LP64__
|
|
Packit |
2d622a |
#define STACK_ALLOCATION_SIZE (256*1024*1024)
|
|
Packit |
2d622a |
#else
|
|
Packit |
2d622a |
#define STACK_ALLOCATION_SIZE (16*1024*1024)
|
|
Packit |
2d622a |
#endif
|
|
Packit |
2d622a |
|
|
Packit |
a158b3 |
#define MIN_CHILD_STACK (2*1024*1024)
|
|
Packit |
a158b3 |
#define STEP (STACK_ALLOCATION_SIZE)
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
int do_child(void *stop_address)
|
|
Packit |
2d622a |
{
|
|
Packit |
2d622a |
struct rlimit r;
|
|
Packit |
2d622a |
volatile int *x;
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
/* corefile from this process is not interesting and limiting
|
|
Packit |
2d622a |
* its size can save a lot of time. '1' is a special value,
|
|
Packit |
2d622a |
* that will also abort dumping via pipe, which by default
|
|
Packit |
2d622a |
* sets limit to RLIM_INFINITY. */
|
|
Packit |
2d622a |
r.rlim_cur = 1;
|
|
Packit |
2d622a |
r.rlim_max = 1;
|
|
Packit |
2d622a |
setrlimit(RLIMIT_CORE, &r);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
do {
|
|
Packit |
2d622a |
x = alloca(STACK_ALLOCATION_SIZE);
|
|
Packit |
2d622a |
*x = 1;
|
|
Packit |
2d622a |
} while ((void *)x >= stop_address);
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
return 0;
|
|
Packit |
a158b3 |
}
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
void *try_setup_stack_and_huge(int fd, void *hint)
|
|
Packit |
a158b3 |
{
|
|
Packit |
a158b3 |
void *mmap_address, *stack_start, *tmp;
|
|
Packit |
a158b3 |
long hpage_size = gethugepagesize();
|
|
Packit |
a158b3 |
void *stop = alloca(1);
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
/*
|
|
Packit |
a158b3 |
* Find a spot for huge page. We start at "hint" and
|
|
Packit |
a158b3 |
* keep going down in "STEP" increments until we find
|
|
Packit |
a158b3 |
* a place where we can mmap huge page.
|
|
Packit |
a158b3 |
*/
|
|
Packit |
a158b3 |
mmap_address = PALIGN(hint, hpage_size);
|
|
Packit |
a158b3 |
do {
|
|
Packit |
a158b3 |
mmap_address += STEP;
|
|
Packit |
a158b3 |
if (mmap_address >= stop)
|
|
Packit |
a158b3 |
return NULL;
|
|
Packit |
a158b3 |
if (range_is_mapped((unsigned long)mmap_address,
|
|
Packit |
a158b3 |
(unsigned long)mmap_address + hpage_size))
|
|
Packit |
a158b3 |
continue;
|
|
Packit |
a158b3 |
tmp = mmap(mmap_address, hpage_size,
|
|
Packit |
a158b3 |
PROT_READ|PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
|
|
Packit |
a158b3 |
} while (tmp == MAP_FAILED);
|
|
Packit |
a158b3 |
verbose_printf("huge page is at: %p-%p\n",
|
|
Packit |
a158b3 |
mmap_address, mmap_address + hpage_size);
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
/*
|
|
Packit |
a158b3 |
* Find a spot for stack below huge page. We start at end of
|
|
Packit |
a158b3 |
* huge page we found above and keep trying to mmap stack
|
|
Packit |
a158b3 |
* below. Because stack needs to grow into hugepage, we
|
|
Packit |
a158b3 |
* also have to make sure nothing is mapped in gap between
|
|
Packit |
a158b3 |
* stack and huge page.
|
|
Packit |
a158b3 |
*/
|
|
Packit |
a158b3 |
stack_start = mmap_address + hpage_size;
|
|
Packit |
a158b3 |
do {
|
|
Packit |
a158b3 |
if (range_is_mapped((unsigned long)stack_start,
|
|
Packit |
a158b3 |
(unsigned long)stack_start + STEP + MIN_CHILD_STACK)) {
|
|
Packit |
a158b3 |
verbose_printf("range is mapped: %p-%p\n", stack_start,
|
|
Packit |
a158b3 |
stack_start + STEP + MIN_CHILD_STACK);
|
|
Packit |
a158b3 |
munmap(mmap_address, hpage_size);
|
|
Packit |
a158b3 |
return NULL;
|
|
Packit |
a158b3 |
}
|
|
Packit |
a158b3 |
stack_start += STEP;
|
|
Packit |
a158b3 |
if (stack_start >= stop)
|
|
Packit |
a158b3 |
return NULL;
|
|
Packit |
a158b3 |
tmp = mmap(stack_start, MIN_CHILD_STACK, PROT_READ|PROT_WRITE,
|
|
Packit |
a158b3 |
MAP_GROWSDOWN|MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
|
|
Packit |
a158b3 |
} while (tmp == MAP_FAILED);
|
|
Packit |
a158b3 |
|
|
Packit |
a158b3 |
verbose_printf("Child stack is at %p-%p\n",
|
|
Packit |
a158b3 |
stack_start, stack_start + MIN_CHILD_STACK);
|
|
Packit |
a158b3 |
return stack_start + MIN_CHILD_STACK;
|
|
Packit |
2d622a |
}
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
int main(int argc, char *argv[])
|
|
Packit |
2d622a |
{
|
|
Packit |
2d622a |
int fd, pid, s, ret;
|
|
Packit |
2d622a |
struct rlimit r;
|
|
Packit |
a158b3 |
void *stack_end;
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
test_init(argc, argv);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
ret = getrlimit(RLIMIT_STACK, &r);
|
|
Packit |
2d622a |
if (ret)
|
|
Packit |
2d622a |
CONFIG("getrlimit failed: %s", strerror(errno));
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
if (r.rlim_cur != RLIM_INFINITY)
|
|
Packit |
2d622a |
CONFIG("Stack rlimit must be 'unlimited'");
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
fd = hugetlbfs_unlinked_fd();
|
|
Packit |
2d622a |
if (fd < 0)
|
|
Packit |
2d622a |
CONFIG("Couldn't get hugepage fd");
|
|
Packit |
2d622a |
|
|
Packit |
a158b3 |
stack_end = try_setup_stack_and_huge(fd, sbrk(0));
|
|
Packit |
a158b3 |
if (!stack_end)
|
|
Packit |
a158b3 |
PASS_INCONCLUSIVE();
|
|
Packit |
2d622a |
|
|
Packit |
a158b3 |
pid = clone(do_child, stack_end, SIGCHLD, 0);
|
|
Packit |
a158b3 |
if (pid < 0)
|
|
Packit |
a158b3 |
FAIL("clone: %s", strerror(errno));
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
ret = waitpid(pid, &s, 0);
|
|
Packit |
2d622a |
if (ret == -1)
|
|
Packit |
2d622a |
FAIL("waitpid: %s", strerror(errno));
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
/*
|
|
Packit |
2d622a |
* The child grows its stack until a failure occurs. We expect
|
|
Packit |
2d622a |
* this to result in a SIGSEGV. If any other signal is
|
|
Packit |
2d622a |
* delivered (ie. SIGTRAP) or no signal is sent at all, we
|
|
Packit |
2d622a |
* determine the kernel has not behaved correctly and trigger a
|
|
Packit |
2d622a |
* test failure.
|
|
Packit |
2d622a |
*/
|
|
Packit |
2d622a |
if (WIFSIGNALED(s)) {
|
|
Packit |
2d622a |
int sig = WTERMSIG(s);
|
|
Packit |
2d622a |
|
|
Packit |
2d622a |
if (sig == SIGSEGV) {
|
|
Packit |
2d622a |
PASS();
|
|
Packit |
2d622a |
} else {
|
|
Packit |
2d622a |
FAIL("Got unexpected signal: %s", strsignal(sig));
|
|
Packit |
2d622a |
}
|
|
Packit |
2d622a |
}
|
|
Packit |
2d622a |
FAIL("Child not signalled");
|
|
Packit |
2d622a |
}
|