xref: /openbmc/qemu/util/mmap-alloc.c (revision d01cbf82)
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 
13 #ifdef CONFIG_LINUX
14 #include <linux/mman.h>
15 #else  /* !CONFIG_LINUX */
16 #define MAP_SYNC              0x0
17 #define MAP_SHARED_VALIDATE   0x0
18 #endif /* CONFIG_LINUX */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/mmap-alloc.h"
22 #include "qemu/host-utils.h"
23 
24 #define HUGETLBFS_MAGIC       0x958458f6
25 
26 #ifdef CONFIG_LINUX
27 #include <sys/vfs.h>
28 #endif
29 
30 size_t qemu_fd_getpagesize(int fd)
31 {
32 #ifdef CONFIG_LINUX
33     struct statfs fs;
34     int ret;
35 
36     if (fd != -1) {
37         do {
38             ret = fstatfs(fd, &fs);
39         } while (ret != 0 && errno == EINTR);
40 
41         if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
42             return fs.f_bsize;
43         }
44     }
45 #ifdef __sparc__
46     /* SPARC Linux needs greater alignment than the pagesize */
47     return QEMU_VMALLOC_ALIGN;
48 #endif
49 #endif
50 
51     return qemu_real_host_page_size;
52 }
53 
54 size_t qemu_mempath_getpagesize(const char *mem_path)
55 {
56 #ifdef CONFIG_LINUX
57     struct statfs fs;
58     int ret;
59 
60     if (mem_path) {
61         do {
62             ret = statfs(mem_path, &fs);
63         } while (ret != 0 && errno == EINTR);
64 
65         if (ret != 0) {
66             fprintf(stderr, "Couldn't statfs() memory path: %s\n",
67                     strerror(errno));
68             exit(1);
69         }
70 
71         if (fs.f_type == HUGETLBFS_MAGIC) {
72             /* It's hugepage, return the huge page size */
73             return fs.f_bsize;
74         }
75     }
76 #ifdef __sparc__
77     /* SPARC Linux needs greater alignment than the pagesize */
78     return QEMU_VMALLOC_ALIGN;
79 #endif
80 #endif
81 
82     return qemu_real_host_page_size;
83 }
84 
85 /*
86  * Reserve a new memory region of the requested size to be used for mapping
87  * from the given fd (if any).
88  */
89 static void *mmap_reserve(size_t size, int fd)
90 {
91     int flags = MAP_PRIVATE;
92 
93 #if defined(__powerpc64__) && defined(__linux__)
94     /*
95      * On ppc64 mappings in the same segment (aka slice) must share the same
96      * page size. Since we will be re-allocating part of this segment
97      * from the supplied fd, we should make sure to use the same page size, to
98      * this end we mmap the supplied fd.  In this case, set MAP_NORESERVE to
99      * avoid allocating backing store memory.
100      * We do this unless we are using the system page size, in which case
101      * anonymous memory is OK.
102      */
103     if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size) {
104         fd = -1;
105         flags |= MAP_ANONYMOUS;
106     } else {
107         flags |= MAP_NORESERVE;
108     }
109 #else
110     fd = -1;
111     flags |= MAP_ANONYMOUS;
112 #endif
113 
114     return mmap(0, size, PROT_NONE, flags, fd, 0);
115 }
116 
117 /*
118  * Activate memory in a reserved region from the given fd (if any), to make
119  * it accessible.
120  */
121 static void *mmap_activate(void *ptr, size_t size, int fd, bool readonly,
122                            bool shared, bool is_pmem, off_t map_offset)
123 {
124     const int prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
125     int map_sync_flags = 0;
126     int flags = MAP_FIXED;
127     void *activated_ptr;
128 
129     flags |= fd == -1 ? MAP_ANONYMOUS : 0;
130     flags |= shared ? MAP_SHARED : MAP_PRIVATE;
131     if (shared && is_pmem) {
132         map_sync_flags = MAP_SYNC | MAP_SHARED_VALIDATE;
133     }
134 
135     activated_ptr = mmap(ptr, size, prot, flags | map_sync_flags, fd,
136                          map_offset);
137     if (activated_ptr == MAP_FAILED && map_sync_flags) {
138         if (errno == ENOTSUP) {
139             char *proc_link = g_strdup_printf("/proc/self/fd/%d", fd);
140             char *file_name = g_malloc0(PATH_MAX);
141             int len = readlink(proc_link, file_name, PATH_MAX - 1);
142 
143             if (len < 0) {
144                 len = 0;
145             }
146             file_name[len] = '\0';
147             fprintf(stderr, "Warning: requesting persistence across crashes "
148                     "for backend file %s failed. Proceeding without "
149                     "persistence, data might become corrupted in case of host "
150                     "crash.\n", file_name);
151             g_free(proc_link);
152             g_free(file_name);
153         }
154         /*
155          * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try
156          * again without these flags to handle backwards compatibility.
157          */
158         activated_ptr = mmap(ptr, size, prot, flags, fd, map_offset);
159     }
160     return activated_ptr;
161 }
162 
163 static inline size_t mmap_guard_pagesize(int fd)
164 {
165 #if defined(__powerpc64__) && defined(__linux__)
166     /* Mappings in the same segment must share the same page size */
167     return qemu_fd_getpagesize(fd);
168 #else
169     return qemu_real_host_page_size;
170 #endif
171 }
172 
173 void *qemu_ram_mmap(int fd,
174                     size_t size,
175                     size_t align,
176                     bool readonly,
177                     bool shared,
178                     bool is_pmem,
179                     off_t map_offset)
180 {
181     const size_t guard_pagesize = mmap_guard_pagesize(fd);
182     size_t offset, total;
183     void *ptr, *guardptr;
184 
185     /*
186      * Note: this always allocates at least one extra page of virtual address
187      * space, even if size is already aligned.
188      */
189     total = size + align;
190 
191     guardptr = mmap_reserve(total, fd);
192     if (guardptr == MAP_FAILED) {
193         return MAP_FAILED;
194     }
195 
196     assert(is_power_of_2(align));
197     /* Always align to host page size */
198     assert(align >= guard_pagesize);
199 
200     offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
201 
202     ptr = mmap_activate(guardptr + offset, size, fd, readonly, shared, is_pmem,
203                         map_offset);
204     if (ptr == MAP_FAILED) {
205         munmap(guardptr, total);
206         return MAP_FAILED;
207     }
208 
209     if (offset > 0) {
210         munmap(guardptr, offset);
211     }
212 
213     /*
214      * Leave a single PROT_NONE page allocated after the RAM block, to serve as
215      * a guard page guarding against potential buffer overflows.
216      */
217     total -= offset;
218     if (total > size + guard_pagesize) {
219         munmap(ptr + size + guard_pagesize, total - size - guard_pagesize);
220     }
221 
222     return ptr;
223 }
224 
225 void qemu_ram_munmap(int fd, void *ptr, size_t size)
226 {
227     if (ptr) {
228         /* Unmap both the RAM block and the guard page */
229         munmap(ptr, size + mmap_guard_pagesize(fd));
230     }
231 }
232