mmap-alloc.c 8.9 KB

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