mmap-alloc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Support for RAM backed by mmaped host memory.
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * later. See the COPYING file in the top-level directory.
  11. */
  12. #ifdef CONFIG_LINUX
  13. #include <linux/mman.h>
  14. #else /* !CONFIG_LINUX */
  15. #define MAP_SYNC 0x0
  16. #define MAP_SHARED_VALIDATE 0x0
  17. #endif /* CONFIG_LINUX */
  18. #include "qemu/osdep.h"
  19. #include "qemu/mmap-alloc.h"
  20. #include "qemu/host-utils.h"
  21. #include "qemu/cutils.h"
  22. #include "qemu/error-report.h"
  23. #define HUGETLBFS_MAGIC 0x958458f6
  24. #ifdef CONFIG_LINUX
  25. #include <sys/vfs.h>
  26. #endif
  27. size_t qemu_fd_getpagesize(int fd)
  28. {
  29. #ifdef CONFIG_LINUX
  30. struct statfs fs;
  31. int ret;
  32. if (fd != -1) {
  33. do {
  34. ret = fstatfs(fd, &fs);
  35. } while (ret != 0 && errno == EINTR);
  36. if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
  37. return fs.f_bsize;
  38. }
  39. }
  40. #ifdef __sparc__
  41. /* SPARC Linux needs greater alignment than the pagesize */
  42. return QEMU_VMALLOC_ALIGN;
  43. #endif
  44. #endif
  45. return qemu_real_host_page_size();
  46. }
  47. #define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory"
  48. static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
  49. {
  50. #if defined(__linux__)
  51. const bool readonly = qemu_map_flags & QEMU_MAP_READONLY;
  52. const bool shared = qemu_map_flags & QEMU_MAP_SHARED;
  53. gchar *content = NULL;
  54. const char *endptr;
  55. unsigned int tmp;
  56. /*
  57. * hugeltb accounting is different than ordinary swap reservation:
  58. * a) Hugetlb pages from the pool are reserved for both private and
  59. * shared mappings. For shared mappings, all mappers have to specify
  60. * MAP_NORESERVE.
  61. * b) MAP_NORESERVE is not affected by /proc/sys/vm/overcommit_memory.
  62. */
  63. if (qemu_fd_getpagesize(fd) != qemu_real_host_page_size()) {
  64. return true;
  65. }
  66. /*
  67. * Accountable mappings in the kernel that can be affected by MAP_NORESEVE
  68. * are private writable mappings (see mm/mmap.c:accountable_mapping() in
  69. * Linux). For all shared or readonly mappings, MAP_NORESERVE is always
  70. * implicitly active -- no reservation; this includes shmem. The only
  71. * exception is shared anonymous memory, it is accounted like private
  72. * anonymous memory.
  73. */
  74. if (readonly || (shared && fd >= 0)) {
  75. return true;
  76. }
  77. /*
  78. * MAP_NORESERVE is globally ignored for applicable !hugetlb mappings when
  79. * memory overcommit is set to "never". Sparse memory regions aren't really
  80. * possible in this system configuration.
  81. *
  82. * Bail out now instead of silently committing way more memory than
  83. * currently desired by the user.
  84. */
  85. if (g_file_get_contents(OVERCOMMIT_MEMORY_PATH, &content, NULL, NULL) &&
  86. !qemu_strtoui(content, &endptr, 0, &tmp) &&
  87. (!endptr || *endptr == '\n')) {
  88. if (tmp == 2) {
  89. error_report("Skipping reservation of swap space is not supported:"
  90. " \"" OVERCOMMIT_MEMORY_PATH "\" is \"2\"");
  91. return false;
  92. }
  93. return true;
  94. }
  95. /* this interface has been around since Linux 2.6 */
  96. error_report("Skipping reservation of swap space is not supported:"
  97. " Could not read: \"" OVERCOMMIT_MEMORY_PATH "\"");
  98. return false;
  99. #endif
  100. /*
  101. * E.g., FreeBSD used to define MAP_NORESERVE, never implemented it,
  102. * and removed it a while ago.
  103. */
  104. error_report("Skipping reservation of swap space is not supported");
  105. return false;
  106. }
  107. /*
  108. * Reserve a new memory region of the requested size to be used for mapping
  109. * from the given fd (if any).
  110. */
  111. static void *mmap_reserve(size_t size, int fd)
  112. {
  113. int flags = MAP_PRIVATE;
  114. #if defined(__powerpc64__) && defined(__linux__)
  115. /*
  116. * On ppc64 mappings in the same segment (aka slice) must share the same
  117. * page size. Since we will be re-allocating part of this segment
  118. * from the supplied fd, we should make sure to use the same page size, to
  119. * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to
  120. * avoid allocating backing store memory.
  121. * We do this unless we are using the system page size, in which case
  122. * anonymous memory is OK.
  123. */
  124. if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size()) {
  125. fd = -1;
  126. flags |= MAP_ANONYMOUS;
  127. } else {
  128. flags |= MAP_NORESERVE;
  129. }
  130. #else
  131. fd = -1;
  132. flags |= MAP_ANONYMOUS;
  133. #endif
  134. return mmap(0, size, PROT_NONE, flags, fd, 0);
  135. }
  136. /*
  137. * Activate memory in a reserved region from the given fd (if any), to make
  138. * it accessible.
  139. */
  140. static void *mmap_activate(void *ptr, size_t size, int fd,
  141. uint32_t qemu_map_flags, off_t map_offset)
  142. {
  143. const bool noreserve = qemu_map_flags & QEMU_MAP_NORESERVE;
  144. const bool readonly = qemu_map_flags & QEMU_MAP_READONLY;
  145. const bool shared = qemu_map_flags & QEMU_MAP_SHARED;
  146. const bool sync = qemu_map_flags & QEMU_MAP_SYNC;
  147. const int prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
  148. int map_sync_flags = 0;
  149. int flags = MAP_FIXED;
  150. void *activated_ptr;
  151. if (noreserve && !map_noreserve_effective(fd, qemu_map_flags)) {
  152. return MAP_FAILED;
  153. }
  154. flags |= fd == -1 ? MAP_ANONYMOUS : 0;
  155. flags |= shared ? MAP_SHARED : MAP_PRIVATE;
  156. flags |= noreserve ? MAP_NORESERVE : 0;
  157. if (shared && sync) {
  158. map_sync_flags = MAP_SYNC | MAP_SHARED_VALIDATE;
  159. }
  160. activated_ptr = mmap(ptr, size, prot, flags | map_sync_flags, fd,
  161. map_offset);
  162. if (activated_ptr == MAP_FAILED && map_sync_flags) {
  163. if (errno == ENOTSUP) {
  164. char *proc_link = g_strdup_printf("/proc/self/fd/%d", fd);
  165. char *file_name = g_malloc0(PATH_MAX);
  166. int len = readlink(proc_link, file_name, PATH_MAX - 1);
  167. if (len < 0) {
  168. len = 0;
  169. }
  170. file_name[len] = '\0';
  171. fprintf(stderr, "Warning: requesting persistence across crashes "
  172. "for backend file %s failed. Proceeding without "
  173. "persistence, data might become corrupted in case of host "
  174. "crash.\n", file_name);
  175. g_free(proc_link);
  176. g_free(file_name);
  177. warn_report("Using non DAX backing file with 'pmem=on' option"
  178. " is deprecated");
  179. }
  180. /*
  181. * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try
  182. * again without these flags to handle backwards compatibility.
  183. */
  184. activated_ptr = mmap(ptr, size, prot, flags, fd, map_offset);
  185. }
  186. return activated_ptr;
  187. }
  188. static inline size_t mmap_guard_pagesize(int fd)
  189. {
  190. #if defined(__powerpc64__) && defined(__linux__)
  191. /* Mappings in the same segment must share the same page size */
  192. return qemu_fd_getpagesize(fd);
  193. #else
  194. return qemu_real_host_page_size();
  195. #endif
  196. }
  197. void *qemu_ram_mmap(int fd,
  198. size_t size,
  199. size_t align,
  200. uint32_t qemu_map_flags,
  201. off_t map_offset)
  202. {
  203. const size_t guard_pagesize = mmap_guard_pagesize(fd);
  204. size_t offset, total;
  205. void *ptr, *guardptr;
  206. /*
  207. * Note: this always allocates at least one extra page of virtual address
  208. * space, even if size is already aligned.
  209. */
  210. total = size + align;
  211. guardptr = mmap_reserve(total, fd);
  212. if (guardptr == MAP_FAILED) {
  213. return MAP_FAILED;
  214. }
  215. assert(is_power_of_2(align));
  216. /* Always align to host page size */
  217. assert(align >= guard_pagesize);
  218. offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
  219. ptr = mmap_activate(guardptr + offset, size, fd, qemu_map_flags,
  220. map_offset);
  221. if (ptr == MAP_FAILED) {
  222. munmap(guardptr, total);
  223. return MAP_FAILED;
  224. }
  225. if (offset > 0) {
  226. munmap(guardptr, offset);
  227. }
  228. /*
  229. * Leave a single PROT_NONE page allocated after the RAM block, to serve as
  230. * a guard page guarding against potential buffer overflows.
  231. */
  232. total -= offset;
  233. if (total > size + guard_pagesize) {
  234. munmap(ptr + size + guard_pagesize, total - size - guard_pagesize);
  235. }
  236. return ptr;
  237. }
  238. void qemu_ram_munmap(int fd, void *ptr, size_t size)
  239. {
  240. if (ptr) {
  241. /* Unmap both the RAM block and the guard page */
  242. munmap(ptr, size + mmap_guard_pagesize(fd));
  243. }
  244. }