Blame shm.c

Packit Service b439df
/*
Packit Service b439df
 * libhugetlbfs - Easy use of Linux hugepages
Packit Service b439df
 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
Packit Service b439df
 *
Packit Service b439df
 * This library is free software; you can redistribute it and/or
Packit Service b439df
 * modify it under the terms of the GNU Lesser General Public License
Packit Service b439df
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service b439df
 * the License, or (at your option) any later version.
Packit Service b439df
 *
Packit Service b439df
 * This library is distributed in the hope that it will be useful, but
Packit Service b439df
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b439df
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b439df
 * Lesser General Public License for more details.
Packit Service b439df
 *
Packit Service b439df
 * You should have received a copy of the GNU Lesser General Public
Packit Service b439df
 * License along with this library; if not, write to the Free Software
Packit Service b439df
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service b439df
 */
Packit Service b439df
#define _GNU_SOURCE
Packit Service b439df
#include <dlfcn.h>
Packit Service b439df
#include <errno.h>
Packit Service b439df
#include <stdio.h>
Packit Service b439df
#include <stdlib.h>
Packit Service b439df
#include <string.h>
Packit Service b439df
#include <unistd.h>
Packit Service b439df
#include <sys/ipc.h>
Packit Service b439df
#include <sys/shm.h>
Packit Service b439df
#include <sys/types.h>
Packit Service b439df
#include "libhugetlbfs_internal.h"
Packit Service b439df
#include "hugetlbfs.h"
Packit Service b439df
#include <sys/syscall.h>
Packit Service b439df
Packit Service b439df
#if defined(SYS_shmget) || defined(SYS_ipc)
Packit Service b439df
#define HAVE_SHMGET_SYSCALL
Packit Service b439df
#endif
Packit Service b439df
Packit Service b439df
#ifdef HAVE_SHMGET_SYSCALL
Packit Service b439df
/*
Packit Service b439df
 * The calls to dlsym() and dlerror() in the shmget() wrapper below force
Packit Service b439df
 * a dependency on libdl.so.  This does not work for static executables
Packit Service b439df
 * as the glibc dynamic library implementation does not automatically
Packit Service b439df
 * have static dl* function stubs linked into static executables.
Packit Service b439df
 *
Packit Service b439df
 * Work around this problem by adding a weak attribute to the declarations
Packit Service b439df
 * of dlsym() and dlerror().  (The declaration is otherwise the same as in
Packit Service b439df
 * <dlfcn.h>).  This allows a static executable to be linked without -ldl.
Packit Service b439df
 * If &dlsym is NULL then this is a static executable and a call to the
Packit Service b439df
 * system shmget() may be performed without worry as there is no dynamic
Packit Service b439df
 * call chain.
Packit Service b439df
 */
Packit Service b439df
extern void *dlsym (void *__restrict __handle, __const char *__restrict __name)
Packit Service b439df
		__attribute__((weak)) __THROW __nonnull ((2));
Packit Service b439df
extern char *dlerror (void) __attribute__((weak)) __THROW;
Packit Service b439df
Packit Service b439df
Packit Service b439df
/* call syscall shmget through the generic syscall mechanism */
Packit Service b439df
static int syscall_shmget(key_t key, size_t size, int shmflg)
Packit Service b439df
{
Packit Service b439df
#ifdef SYS_shmget
Packit Service b439df
	return syscall(SYS_shmget, key, size, shmflg);
Packit Service b439df
#else
Packit Service b439df
	/*
Packit Service b439df
	 * Some platforms do not have have a direct shmget syscall.  Instead,
Packit Service b439df
	 * all SysV IPC calls are funneled through the ipc() system call.
Packit Service b439df
	 *
Packit Service b439df
	 * ipc() is expected to only be used by libc implementors, so using
Packit Service b439df
	 * it has not been smoothed out.  There is no function declaration.
Packit Service b439df
	 * The needed define for SHMGET is in linux/ipc.h, but that file
Packit Service b439df
	 * also includes a conflicting definition of ipc_perm.  So,
Packit Service b439df
	 * just define the needed items here.
Packit Service b439df
	 *
Packit Service b439df
	 * When compiling -m32 on x86_64, the ipc glibc wrapper does not
Packit Service b439df
	 * exist.  Instead, just use SYS_ipc.
Packit Service b439df
	 *
Packit Service b439df
	 * The ipc system call below does not set the IPC_64 version flag
Packit Service b439df
	 * with SHMGET because that would have required more private defines
Packit Service b439df
	 * and the version number is not used for the SHMGET call.
Packit Service b439df
	 */
Packit Service b439df
	#define SHMGET 23
Packit Service b439df
Packit Service b439df
	return syscall(SYS_ipc, SHMGET, key, size, shmflg, (void *)NULL, 0L);
Packit Service b439df
#endif
Packit Service b439df
}
Packit Service b439df
Packit Service b439df
#endif /* HAVE_SHMGET_SYSCALL */
Packit Service b439df
Packit Service b439df
int shmget(key_t key, size_t size, int shmflg)
Packit Service b439df
{
Packit Service b439df
	static int (*real_shmget)(key_t key, size_t size, int shmflg) = NULL;
Packit Service b439df
	char *error;
Packit Service b439df
	int retval;
Packit Service b439df
	size_t aligned_size = size;
Packit Service b439df
Packit Service b439df
	DEBUG("hugetlb_shmem: entering overridden shmget() call\n");
Packit Service b439df
Packit Service b439df
	/* Get a handle to the "real" shmget system call */
Packit Service b439df
	if (!real_shmget) {
Packit Service b439df
#ifdef HAVE_SHMGET_SYSCALL
Packit Service b439df
		if (&dlsym == NULL) {
Packit Service b439df
			/* in a static executable, call shmget directly */
Packit Service b439df
			real_shmget = syscall_shmget;
Packit Service b439df
		} else
Packit Service b439df
#endif /* HAVE_SHMGET_SYSCALL */
Packit Service b439df
		{
Packit Service b439df
			real_shmget = dlsym(RTLD_NEXT, "shmget");
Packit Service b439df
			if ((error = dlerror()) != NULL) {
Packit Service b439df
				ERROR("%s", error);
Packit Service b439df
				return -1;
Packit Service b439df
			}
Packit Service b439df
		}
Packit Service b439df
	}
Packit Service b439df
Packit Service b439df
	/* Align the size and set SHM_HUGETLB on request */
Packit Service b439df
	if (__hugetlb_opts.shm_enabled) {
Packit Service b439df
		/*
Packit Service b439df
		 * Use /proc/meminfo because shm always uses the system
Packit Service b439df
		 * default huge page size.
Packit Service b439df
		 */
Packit Service b439df
		long hpage_size = kernel_default_hugepage_size();
Packit Service b439df
		aligned_size = ALIGN(size, hpage_size);
Packit Service b439df
		if (size != aligned_size) {
Packit Service b439df
			DEBUG("hugetlb_shmem: size growth align %zd -> %zd\n",
Packit Service b439df
				size, aligned_size);
Packit Service b439df
		}
Packit Service b439df
Packit Service b439df
		INFO("hugetlb_shmem: Adding SHM_HUGETLB flag\n");
Packit Service b439df
		shmflg |= SHM_HUGETLB;
Packit Service b439df
	} else {
Packit Service b439df
		DEBUG("hugetlb_shmem: shmget override not requested\n");
Packit Service b439df
	}
Packit Service b439df
Packit Service b439df
	/* Call the "real" shmget. If hugepages fail, use small pages */
Packit Service b439df
	retval = real_shmget(key, aligned_size, shmflg);
Packit Service b439df
	if (retval == -1 && __hugetlb_opts.shm_enabled) {
Packit Service b439df
		WARNING("While overriding shmget(%zd) to add SHM_HUGETLB: %s\n",
Packit Service b439df
			aligned_size, strerror(errno));
Packit Service b439df
		shmflg &= ~SHM_HUGETLB;
Packit Service b439df
		retval = real_shmget(key, size, shmflg);
Packit Service b439df
		WARNING("Using small pages for shmget despite HUGETLB_SHM\n");
Packit Service b439df
	}
Packit Service b439df
Packit Service b439df
	return retval;
Packit Service b439df
}