| 1 | /* |
| 2 | * libhugetlbfs - Easy use of Linux hugepages |
| 3 | * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation; either version 2.1 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #define _GNU_SOURCE |
| 20 | #include <dlfcn.h> |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/ipc.h> |
| 27 | #include <sys/shm.h> |
| 28 | #include <sys/types.h> |
| 29 | #include "libhugetlbfs_internal.h" |
| 30 | #include "hugetlbfs.h" |
| 31 | #include <sys/syscall.h> |
| 32 | |
| 33 | #if defined(SYS_shmget) || defined(SYS_ipc) |
| 34 | #define HAVE_SHMGET_SYSCALL |
| 35 | #endif |
| 36 | |
| 37 | #ifdef HAVE_SHMGET_SYSCALL |
| 38 | /* |
| 39 | * The calls to dlsym() and dlerror() in the shmget() wrapper below force |
| 40 | * a dependency on libdl.so. This does not work for static executables |
| 41 | * as the glibc dynamic library implementation does not automatically |
| 42 | * have static dl* function stubs linked into static executables. |
| 43 | * |
| 44 | * Work around this problem by adding a weak attribute to the declarations |
| 45 | * of dlsym() and dlerror(). (The declaration is otherwise the same as in |
| 46 | * <dlfcn.h>). This allows a static executable to be linked without -ldl. |
| 47 | * If &dlsym is NULL then this is a static executable and a call to the |
| 48 | * system shmget() may be performed without worry as there is no dynamic |
| 49 | * call chain. |
| 50 | */ |
| 51 | extern void *dlsym (void *__restrict __handle, __const char *__restrict __name) |
| 52 | __attribute__((weak)) __THROW __nonnull ((2)); |
| 53 | extern char *dlerror (void) __attribute__((weak)) __THROW; |
| 54 | |
| 55 | |
| 56 | /* call syscall shmget through the generic syscall mechanism */ |
| 57 | static int syscall_shmget(key_t key, size_t size, int shmflg) |
| 58 | { |
| 59 | #ifdef SYS_shmget |
| 60 | return syscall(SYS_shmget, key, size, shmflg); |
| 61 | #else |
| 62 | /* |
| 63 | * Some platforms do not have have a direct shmget syscall. Instead, |
| 64 | * all SysV IPC calls are funneled through the ipc() system call. |
| 65 | * |
| 66 | * ipc() is expected to only be used by libc implementors, so using |
| 67 | * it has not been smoothed out. There is no function declaration. |
| 68 | * The needed define for SHMGET is in linux/ipc.h, but that file |
| 69 | * also includes a conflicting definition of ipc_perm. So, |
| 70 | * just define the needed items here. |
| 71 | * |
| 72 | * When compiling -m32 on x86_64, the ipc glibc wrapper does not |
| 73 | * exist. Instead, just use SYS_ipc. |
| 74 | * |
| 75 | * The ipc system call below does not set the IPC_64 version flag |
| 76 | * with SHMGET because that would have required more private defines |
| 77 | * and the version number is not used for the SHMGET call. |
| 78 | */ |
| 79 | #define SHMGET 23 |
| 80 | |
| 81 | return syscall(SYS_ipc, SHMGET, key, size, shmflg, (void *)NULL, 0L); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | #endif /* HAVE_SHMGET_SYSCALL */ |
| 86 | |
| 87 | int shmget(key_t key, size_t size, int shmflg) |
| 88 | { |
| 89 | static int (*real_shmget)(key_t key, size_t size, int shmflg) = NULL; |
| 90 | char *error; |
| 91 | int retval; |
| 92 | size_t aligned_size = size; |
| 93 | |
| 94 | DEBUG("hugetlb_shmem: entering overridden shmget() call\n"); |
| 95 | |
| 96 | /* Get a handle to the "real" shmget system call */ |
| 97 | if (!real_shmget) { |
| 98 | #ifdef HAVE_SHMGET_SYSCALL |
| 99 | if (&dlsym == NULL) { |
| 100 | /* in a static executable, call shmget directly */ |
| 101 | real_shmget = syscall_shmget; |
| 102 | } else |
| 103 | #endif /* HAVE_SHMGET_SYSCALL */ |
| 104 | { |
| 105 | real_shmget = dlsym(RTLD_NEXT, "shmget"); |
| 106 | if ((error = dlerror()) != NULL) { |
| 107 | ERROR("%s", error); |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Align the size and set SHM_HUGETLB on request */ |
| 114 | if (__hugetlb_opts.shm_enabled) { |
| 115 | /* |
| 116 | * Use /proc/meminfo because shm always uses the system |
| 117 | * default huge page size. |
| 118 | */ |
| 119 | long hpage_size = kernel_default_hugepage_size(); |
| 120 | aligned_size = ALIGN(size, hpage_size); |
| 121 | if (size != aligned_size) { |
| 122 | DEBUG("hugetlb_shmem: size growth align %zd -> %zd\n", |
| 123 | size, aligned_size); |
| 124 | } |
| 125 | |
| 126 | INFO("hugetlb_shmem: Adding SHM_HUGETLB flag\n"); |
| 127 | shmflg |= SHM_HUGETLB; |
| 128 | } else { |
| 129 | DEBUG("hugetlb_shmem: shmget override not requested\n"); |
| 130 | } |
| 131 | |
| 132 | /* Call the "real" shmget. If hugepages fail, use small pages */ |
| 133 | retval = real_shmget(key, aligned_size, shmflg); |
| 134 | if (retval == -1 && __hugetlb_opts.shm_enabled) { |
| 135 | WARNING("While overriding shmget(%zd) to add SHM_HUGETLB: %s\n", |
| 136 | aligned_size, strerror(errno)); |
| 137 | shmflg &= ~SHM_HUGETLB; |
| 138 | retval = real_shmget(key, size, shmflg); |
| 139 | WARNING("Using small pages for shmget despite HUGETLB_SHM\n"); |
| 140 | } |
| 141 | |
| 142 | return retval; |
| 143 | } |