Blame tests/fadvise_reserve.c

Packit 2d622a
/*
Packit 2d622a
 * libhugetlbfs - Easy use of Linux hugepages
Packit 2d622a
 * Copyright (C) 2005-2006 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
#define _XOPEN_SOURCE 600
Packit 2d622a
#include <fcntl.h>
Packit 2d622a
#include <stdio.h>
Packit 2d622a
#include <stdlib.h>
Packit 2d622a
#include <string.h>
Packit 2d622a
#include <errno.h>
Packit 2d622a
#include <unistd.h>
Packit 2d622a
#include <sys/mman.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
 * fadvise() on some kernels can cause the reservation counter to get
Packit 2d622a
 * corrupted. The problem is that the patches are allocated for the
Packit 2d622a
 * reservation but not faulted in at the time of allocation. The
Packit 2d622a
 * counters do not get updated and effectively "leak". This test
Packit 2d622a
 * identifies whether the kernel is vunerable to the problem or not.
Packit 2d622a
 * It's fixed in kernel by commit f2deae9d4e70793568ef9e85d227abb7bef5b622.
Packit 2d622a
 */
Packit 2d622a
int main(int argc, char *argv[])
Packit 2d622a
{
Packit 2d622a
	long hpage_size;
Packit 2d622a
	int fd;
Packit 2d622a
	void *p;
Packit 2d622a
	unsigned long initial_rsvd, map_rsvd, fadvise_rsvd, end_rsvd;
Packit 2d622a
Packit 2d622a
	test_init(argc, argv);
Packit 2d622a
Packit 2d622a
	/* Setup */
Packit 2d622a
	hpage_size = check_hugepagesize();
Packit 2d622a
	fd = hugetlbfs_unlinked_fd();
Packit 2d622a
	if (fd < 0)
Packit 2d622a
		FAIL("hugetlbfs_unlinked_fd()");
Packit 2d622a
	initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
Packit 2d622a
	verbose_printf("Reserve count before map: %lu\n", initial_rsvd);
Packit 2d622a
Packit 2d622a
	/* mmap a region and record reservations */
Packit 2d622a
	p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
Packit 2d622a
		 fd, 0);
Packit 2d622a
	if (p == MAP_FAILED)
Packit 2d622a
		FAIL("mmap(): %s", strerror(errno));
Packit 2d622a
	map_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
Packit 2d622a
	verbose_printf("Reserve count after map: %lu\n", map_rsvd);
Packit 2d622a
Packit 2d622a
	/* fadvise the region and record reservations */
Packit 2d622a
	if (posix_fadvise(fd, 0, hpage_size, POSIX_FADV_WILLNEED) == -1)
Packit 2d622a
		FAIL("fadvise(): %s", strerror(errno));
Packit 2d622a
	fadvise_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
Packit 2d622a
	verbose_printf("Reserve count after fadvise: %lu\n", fadvise_rsvd);
Packit 2d622a
Packit 2d622a
	/* Write the region */
Packit 2d622a
	memset(p, 1, hpage_size);
Packit 2d622a
Packit 2d622a
	/* Free region */
Packit 2d622a
	munmap(p, hpage_size);
Packit 2d622a
	close(fd);
Packit 2d622a
	end_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
Packit 2d622a
	verbose_printf("Reserve count after close(): %lu\n", end_rsvd);
Packit 2d622a
Packit 2d622a
	/* Reserve count should match initial reserve count */
Packit 2d622a
	if (end_rsvd != initial_rsvd)
Packit 2d622a
		FAIL("Reserve leaked: %lu != %lu\n", end_rsvd, initial_rsvd);
Packit 2d622a
Packit 2d622a
	PASS();
Packit 2d622a
}