diff options
Diffstat (limited to 'util')
| -rw-r--r-- | util/async.c | 20 | ||||
| -rw-r--r-- | util/main-loop.c | 1 | ||||
| -rw-r--r-- | util/mmap-alloc.c | 212 | ||||
| -rw-r--r-- | util/oslib-posix.c | 7 | ||||
| -rw-r--r-- | util/oslib-win32.c | 19 | ||||
| -rw-r--r-- | util/qemu-config.c | 17 | ||||
| -rw-r--r-- | util/qemu-sockets.c | 19 | ||||
| -rw-r--r-- | util/qemu-thread-posix.c | 24 | ||||
| -rw-r--r-- | util/qemu-thread-win32.c | 2 |
9 files changed, 236 insertions, 85 deletions
diff --git a/util/async.c b/util/async.c index 674dbefb7c..5d9b7cc1eb 100644 --- a/util/async.c +++ b/util/async.c @@ -649,3 +649,23 @@ void aio_context_release(AioContext *ctx) { qemu_rec_mutex_unlock(&ctx->lock); } + +static __thread AioContext *my_aiocontext; + +AioContext *qemu_get_current_aio_context(void) +{ + if (my_aiocontext) { + return my_aiocontext; + } + if (qemu_mutex_iothread_locked()) { + /* Possibly in a vCPU thread. */ + return qemu_get_aio_context(); + } + return NULL; +} + +void qemu_set_current_aio_context(AioContext *ctx) +{ + assert(!my_aiocontext); + my_aiocontext = ctx; +} diff --git a/util/main-loop.c b/util/main-loop.c index d9c55df6f5..4ae5b23e99 100644 --- a/util/main-loop.c +++ b/util/main-loop.c @@ -170,6 +170,7 @@ int qemu_init_main_loop(Error **errp) if (!qemu_aio_context) { return -EMFILE; } + qemu_set_current_aio_context(qemu_aio_context); qemu_notify_bh = qemu_bh_new(notify_event_cb, NULL); gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD)); src = aio_get_g_source(qemu_aio_context); diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index e6fa8b598b..838e286ce5 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -20,6 +20,8 @@ #include "qemu/osdep.h" #include "qemu/mmap-alloc.h" #include "qemu/host-utils.h" +#include "qemu/cutils.h" +#include "qemu/error-report.h" #define HUGETLBFS_MAGIC 0x958458f6 @@ -82,32 +84,81 @@ size_t qemu_mempath_getpagesize(const char *mem_path) return qemu_real_host_page_size; } -void *qemu_ram_mmap(int fd, - size_t size, - size_t align, - bool readonly, - bool shared, - bool is_pmem, - off_t map_offset) +#define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory" +static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags) { - int prot; - int flags; - int map_sync_flags = 0; - int guardfd; - size_t offset; - size_t pagesize; - size_t total; - void *guardptr; - void *ptr; +#if defined(__linux__) + const bool readonly = qemu_map_flags & QEMU_MAP_READONLY; + const bool shared = qemu_map_flags & QEMU_MAP_SHARED; + gchar *content = NULL; + const char *endptr; + unsigned int tmp; /* - * Note: this always allocates at least one extra page of virtual address - * space, even if size is already aligned. + * hugeltb accounting is different than ordinary swap reservation: + * a) Hugetlb pages from the pool are reserved for both private and + * shared mappings. For shared mappings, all mappers have to specify + * MAP_NORESERVE. + * b) MAP_NORESERVE is not affected by /proc/sys/vm/overcommit_memory. */ - total = size + align; + if (qemu_fd_getpagesize(fd) != qemu_real_host_page_size) { + return true; + } + + /* + * Accountable mappings in the kernel that can be affected by MAP_NORESEVE + * are private writable mappings (see mm/mmap.c:accountable_mapping() in + * Linux). For all shared or readonly mappings, MAP_NORESERVE is always + * implicitly active -- no reservation; this includes shmem. The only + * exception is shared anonymous memory, it is accounted like private + * anonymous memory. + */ + if (readonly || (shared && fd >= 0)) { + return true; + } + + /* + * MAP_NORESERVE is globally ignored for applicable !hugetlb mappings when + * memory overcommit is set to "never". Sparse memory regions aren't really + * possible in this system configuration. + * + * Bail out now instead of silently committing way more memory than + * currently desired by the user. + */ + if (g_file_get_contents(OVERCOMMIT_MEMORY_PATH, &content, NULL, NULL) && + !qemu_strtoui(content, &endptr, 0, &tmp) && + (!endptr || *endptr == '\n')) { + if (tmp == 2) { + error_report("Skipping reservation of swap space is not supported:" + " \"" OVERCOMMIT_MEMORY_PATH "\" is \"2\""); + return false; + } + return true; + } + /* this interface has been around since Linux 2.6 */ + error_report("Skipping reservation of swap space is not supported:" + " Could not read: \"" OVERCOMMIT_MEMORY_PATH "\""); + return false; +#endif + /* + * E.g., FreeBSD used to define MAP_NORESERVE, never implemented it, + * and removed it a while ago. + */ + error_report("Skipping reservation of swap space is not supported"); + return false; +} + +/* + * Reserve a new memory region of the requested size to be used for mapping + * from the given fd (if any). + */ +static void *mmap_reserve(size_t size, int fd) +{ + int flags = MAP_PRIVATE; #if defined(__powerpc64__) && defined(__linux__) - /* On ppc64 mappings in the same segment (aka slice) must share the same + /* + * On ppc64 mappings in the same segment (aka slice) must share the same * page size. Since we will be re-allocating part of this segment * from the supplied fd, we should make sure to use the same page size, to * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to @@ -115,52 +166,55 @@ void *qemu_ram_mmap(int fd, * We do this unless we are using the system page size, in which case * anonymous memory is OK. */ - flags = MAP_PRIVATE; - pagesize = qemu_fd_getpagesize(fd); - if (fd == -1 || pagesize == qemu_real_host_page_size) { - guardfd = -1; + if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size) { + fd = -1; flags |= MAP_ANONYMOUS; } else { - guardfd = fd; flags |= MAP_NORESERVE; } #else - guardfd = -1; - pagesize = qemu_real_host_page_size; - flags = MAP_PRIVATE | MAP_ANONYMOUS; + fd = -1; + flags |= MAP_ANONYMOUS; #endif - guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0); + return mmap(0, size, PROT_NONE, flags, fd, 0); +} - if (guardptr == MAP_FAILED) { +/* + * Activate memory in a reserved region from the given fd (if any), to make + * it accessible. + */ +static void *mmap_activate(void *ptr, size_t size, int fd, + uint32_t qemu_map_flags, off_t map_offset) +{ + const bool noreserve = qemu_map_flags & QEMU_MAP_NORESERVE; + const bool readonly = qemu_map_flags & QEMU_MAP_READONLY; + const bool shared = qemu_map_flags & QEMU_MAP_SHARED; + const bool sync = qemu_map_flags & QEMU_MAP_SYNC; + const int prot = PROT_READ | (readonly ? 0 : PROT_WRITE); + int map_sync_flags = 0; + int flags = MAP_FIXED; + void *activated_ptr; + + if (noreserve && !map_noreserve_effective(fd, qemu_map_flags)) { return MAP_FAILED; } - assert(is_power_of_2(align)); - /* Always align to host page size */ - assert(align >= pagesize); - - flags = MAP_FIXED; flags |= fd == -1 ? MAP_ANONYMOUS : 0; flags |= shared ? MAP_SHARED : MAP_PRIVATE; - if (shared && is_pmem) { + flags |= noreserve ? MAP_NORESERVE : 0; + if (shared && sync) { map_sync_flags = MAP_SYNC | MAP_SHARED_VALIDATE; } - offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr; - - prot = PROT_READ | (readonly ? 0 : PROT_WRITE); - - ptr = mmap(guardptr + offset, size, prot, - flags | map_sync_flags, fd, map_offset); - - if (ptr == MAP_FAILED && map_sync_flags) { + activated_ptr = mmap(ptr, size, prot, flags | map_sync_flags, fd, + map_offset); + if (activated_ptr == MAP_FAILED && map_sync_flags) { if (errno == ENOTSUP) { - char *proc_link, *file_name; - int len; - proc_link = g_strdup_printf("/proc/self/fd/%d", fd); - file_name = g_malloc0(PATH_MAX); - len = readlink(proc_link, file_name, PATH_MAX - 1); + char *proc_link = g_strdup_printf("/proc/self/fd/%d", fd); + char *file_name = g_malloc0(PATH_MAX); + int len = readlink(proc_link, file_name, PATH_MAX - 1); + if (len < 0) { len = 0; } @@ -173,12 +227,53 @@ void *qemu_ram_mmap(int fd, g_free(file_name); } /* - * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC, - * we will remove these flags to handle compatibility. + * If mmap failed with MAP_SHARED_VALIDATE | MAP_SYNC, we will try + * again without these flags to handle backwards compatibility. */ - ptr = mmap(guardptr + offset, size, prot, flags, fd, map_offset); + activated_ptr = mmap(ptr, size, prot, flags, fd, map_offset); } + return activated_ptr; +} +static inline size_t mmap_guard_pagesize(int fd) +{ +#if defined(__powerpc64__) && defined(__linux__) + /* Mappings in the same segment must share the same page size */ + return qemu_fd_getpagesize(fd); +#else + return qemu_real_host_page_size; +#endif +} + +void *qemu_ram_mmap(int fd, + size_t size, + size_t align, + uint32_t qemu_map_flags, + off_t map_offset) +{ + const size_t guard_pagesize = mmap_guard_pagesize(fd); + size_t offset, total; + void *ptr, *guardptr; + + /* + * Note: this always allocates at least one extra page of virtual address + * space, even if size is already aligned. + */ + total = size + align; + + guardptr = mmap_reserve(total, fd); + if (guardptr == MAP_FAILED) { + return MAP_FAILED; + } + + assert(is_power_of_2(align)); + /* Always align to host page size */ + assert(align >= guard_pagesize); + + offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr; + + ptr = mmap_activate(guardptr + offset, size, fd, qemu_map_flags, + map_offset); if (ptr == MAP_FAILED) { munmap(guardptr, total); return MAP_FAILED; @@ -193,8 +288,8 @@ void *qemu_ram_mmap(int fd, * a guard page guarding against potential buffer overflows. */ total -= offset; - if (total > size + pagesize) { - munmap(ptr + size + pagesize, total - size - pagesize); + if (total > size + guard_pagesize) { + munmap(ptr + size + guard_pagesize, total - size - guard_pagesize); } return ptr; @@ -202,15 +297,8 @@ void *qemu_ram_mmap(int fd, void qemu_ram_munmap(int fd, void *ptr, size_t size) { - size_t pagesize; - if (ptr) { /* Unmap both the RAM block and the guard page */ -#if defined(__powerpc64__) && defined(__linux__) - pagesize = qemu_fd_getpagesize(fd); -#else - pagesize = qemu_real_host_page_size; -#endif - munmap(ptr, size + pagesize); + munmap(ptr, size + mmap_guard_pagesize(fd)); } } diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 7b4bec1402..e8bdb02e1d 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -227,10 +227,13 @@ void *qemu_memalign(size_t alignment, size_t size) } /* alloc shared memory pages */ -void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared) +void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared, + bool noreserve) { + const uint32_t qemu_map_flags = (shared ? QEMU_MAP_SHARED : 0) | + (noreserve ? QEMU_MAP_NORESERVE : 0); size_t align = QEMU_VMALLOC_ALIGN; - void *ptr = qemu_ram_mmap(-1, size, align, false, shared, false, 0); + void *ptr = qemu_ram_mmap(-1, size, align, qemu_map_flags, 0); if (ptr == MAP_FAILED) { return NULL; diff --git a/util/oslib-win32.c b/util/oslib-win32.c index ca99356fdf..af559ef339 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -38,6 +38,7 @@ #include "trace.h" #include "qemu/sockets.h" #include "qemu/cutils.h" +#include "qemu/error-report.h" #include <malloc.h> /* this must come after including "trace.h" */ @@ -57,7 +58,11 @@ void *qemu_try_memalign(size_t alignment, size_t size) void *ptr; g_assert(size != 0); - g_assert(is_power_of_2(alignment)); + if (alignment < sizeof(void *)) { + alignment = sizeof(void *); + } else { + g_assert(is_power_of_2(alignment)); + } ptr = _aligned_malloc(size, alignment); trace_qemu_memalign(alignment, size, ptr); return ptr; @@ -76,10 +81,20 @@ static int get_allocation_granularity(void) return system_info.dwAllocationGranularity; } -void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared) +void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared, + bool noreserve) { void *ptr; + if (noreserve) { + /* + * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE + * area; we cannot easily mimic POSIX MAP_NORESERVE semantics. + */ + error_report("Skipping reservation of swap space is not supported."); + return NULL; + } + ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); trace_qemu_anon_ram_alloc(size, ptr); diff --git a/util/qemu-config.c b/util/qemu-config.c index 374f3bc460..84ee6dc4ea 100644 --- a/util/qemu-config.c +++ b/util/qemu-config.c @@ -429,29 +429,14 @@ out: void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp) { QemuOptsList **lists = opaque; - const char *id = qdict_get_try_str(qdict, "id"); QemuOptsList *list; - QemuOpts *opts; - const QDictEntry *unrecognized; list = find_list(lists, group, errp); if (!list) { return; } - opts = qemu_opts_create(list, id, 1, errp); - if (!opts) { - return; - } - if (!qemu_opts_absorb_qdict(opts, qdict, errp)) { - qemu_opts_del(opts); - return; - } - unrecognized = qdict_first(qdict); - if (unrecognized) { - error_setg(errp, QERR_INVALID_PARAMETER, unrecognized->key); - qemu_opts_del(opts); - } + qemu_opts_from_qdict(list, qdict, errp); } int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index c415c342c1..080a240b74 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1164,6 +1164,25 @@ static int socket_get_fd(const char *fdstr, Error **errp) return fd; } +int socket_address_parse_named_fd(SocketAddress *addr, Error **errp) +{ + int fd; + + if (addr->type != SOCKET_ADDRESS_TYPE_FD) { + return 0; + } + + fd = socket_get_fd(addr->u.fd.str, errp); + if (fd < 0) { + return fd; + } + + g_free(addr->u.fd.str); + addr->u.fd.str = g_strdup_printf("%d", fd); + + return 0; +} + int socket_connect(SocketAddress *addr, Error **errp) { int fd; diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index dcff5e7c5d..fd9d714038 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -116,12 +116,32 @@ void qemu_rec_mutex_init(QemuRecMutex *mutex) pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - err = pthread_mutex_init(&mutex->lock, &attr); + err = pthread_mutex_init(&mutex->m.lock, &attr); pthread_mutexattr_destroy(&attr); if (err) { error_exit(err, __func__); } - mutex->initialized = true; + mutex->m.initialized = true; +} + +void qemu_rec_mutex_destroy(QemuRecMutex *mutex) +{ + qemu_mutex_destroy(&mutex->m); +} + +void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line) +{ + qemu_mutex_lock_impl(&mutex->m, file, line); +} + +int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line) +{ + return qemu_mutex_trylock_impl(&mutex->m, file, line); +} + +void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line) +{ + qemu_mutex_unlock_impl(&mutex->m, file, line); } void qemu_cond_init(QemuCond *cond) diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index cb5aa2018c..52eb19f351 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -105,7 +105,7 @@ int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line) return !TryEnterCriticalSection(&mutex->lock); } -void qemu_rec_mutex_unlock(QemuRecMutex *mutex) +void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line) { assert(mutex->initialized); LeaveCriticalSection(&mutex->lock); |