aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMel Gorman <mel@csn.ul.ie>2009-05-06 14:52:09 +0100
committerEric B Munson <ebmunson@us.ibm.com>2009-05-07 11:30:52 +0100
commita01fe7eced60f09f2d951a2905771e51e7e55a40 (patch)
tree8560c267798a0c1056699f64c37fc4a1bc185d43 /tests
parentc7f09a2de60395e069eada3fa1cdbae00381dfc0 (diff)
downloadlibhugetlbfs-a01fe7eced60f09f2d951a2905771e51e7e55a40.tar.gz
Add a regression test for leakingreserve count due to madvise() V2
Use of madvise() on a hugetlbfs-backed memory region can result in reservations being leaked. This affects recent kernels (e.g. 2.6.29) but is fixed by commit a425a638c858fd10370b573bde81df3ba500e271. This patch adds a regression test for the bug. Changelog since V1 o Add a note on what kernel commit fixes the bug Signed-off-by: Mel Gorman <mel@csn.ul.ie> Signed-off-by: Eric B Munson <ebmunson@us.ibm.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile2
-rw-r--r--tests/madvise_reserve.c81
-rwxr-xr-xtests/run_tests.py1
3 files changed, 83 insertions, 1 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 31b1b3b..d3efe79 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -8,7 +8,7 @@ LIB_TESTS = gethugepagesize test_root find_path unlinked_fd misalign \
map_high_truncate_2 truncate_above_4GB direct \
misaligned_offset brk_near_huge task-size-overrun stack_grow_into_huge \
counters quota heap-overflow get_huge_pages get_hugepage_region \
- shmoverride_linked gethugepagesizes
+ shmoverride_linked gethugepagesizes madvise_reserve
LIB_TESTS_64 = straddle_4GB huge_at_4GB_normal_below \
huge_below_4GB_normal_above
NOLIB_TESTS = malloc malloc_manysmall dummy heapshrink shmoverride_unlinked
diff --git a/tests/madvise_reserve.c b/tests/madvise_reserve.c
new file mode 100644
index 0000000..3509d40
--- /dev/null
+++ b/tests/madvise_reserve.c
@@ -0,0 +1,81 @@
+/*
+ * libhugetlbfs - Easy use of Linux hugepages
+ * Copyright (C) 2005-2006 IBM Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include <hugetlbfs.h>
+#include "hugetests.h"
+
+/*
+ * Test rationale:
+ *
+ * madvise() on some kernels can cause the reservation counter to get
+ * corrupted. The problem is that the patches are allocated for the
+ * reservation but not faulted in at the time of allocation. The
+ * counters do not get updated and effectively "leak". This test
+ * identifies whether the kernel is vunerable to the problem or not.
+ * It is fixed in kernel by commit a425a638c858fd10370b573bde81df3ba500e271
+ */
+int main(int argc, char *argv[])
+{
+ long hpage_size;
+ int fd;
+ void *p;
+ unsigned long initial_rsvd, map_rsvd, madvise_rsvd, end_rsvd;
+
+ test_init(argc, argv);
+
+ /* Setup */
+ hpage_size = check_hugepagesize();
+ fd = hugetlbfs_unlinked_fd();
+ if (fd < 0)
+ FAIL("hugetlbfs_unlinked_fd()");
+ initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
+ verbose_printf("Reserve count before map: %lu\n", initial_rsvd);
+
+ /* mmap a region and record reservations */
+ p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+ fd, 0);
+ if (p == MAP_FAILED)
+ FAIL("mmap(): %s", strerror(errno));
+ map_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
+ verbose_printf("Reserve count after map: %lu\n", map_rsvd);
+
+ /* madvise the region and record reservations */
+ if (madvise(p, hpage_size, MADV_WILLNEED) == -1)
+ FAIL("madvise(): %s", strerror(errno));
+ madvise_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
+ verbose_printf("Reserve count after madvise: %lu\n", madvise_rsvd);
+
+ /* Free region */
+ munmap(p, hpage_size);
+ close(fd);
+ end_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
+ verbose_printf("Reserve count after close(): %lu\n", end_rsvd);
+
+ /* Reserve count should match initial reserve count */
+ if (end_rsvd != initial_rsvd)
+ FAIL("Reserve leaked: %lu != %lu\n", end_rsvd, initial_rsvd);
+
+ PASS();
+}
diff --git a/tests/run_tests.py b/tests/run_tests.py
index 042d427..6510787 100755
--- a/tests/run_tests.py
+++ b/tests/run_tests.py
@@ -479,6 +479,7 @@ def functional_tests():
do_test("brk_near_huge")
do_test("task-size-overrun")
do_test("stack_grow_into_huge")
+ do_test("madvise_reserve")
# Tests requiring an active mount and hugepage COW
do_test("private")