2010-10-26 12:39:19 +04:00
|
|
|
/*
|
|
|
|
* os-posix-lib.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
* Copyright (c) 2010 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* QEMU library functions on POSIX which are shared between QEMU and
|
|
|
|
* the QEMU tools.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-14 14:54:16 +04:00
|
|
|
#include <termios.h>
|
|
|
|
|
2013-05-18 08:31:48 +04:00
|
|
|
#include <glib/gprintf.h>
|
|
|
|
|
2019-05-23 17:35:08 +03:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
2010-10-26 12:39:19 +04:00
|
|
|
#include "trace.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/thread.h"
|
2014-02-10 10:48:51 +04:00
|
|
|
#include <libgen.h>
|
2014-05-14 13:43:21 +04:00
|
|
|
#include <sys/signal.h>
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2010-10-26 12:39:19 +04:00
|
|
|
|
2011-09-12 18:20:11 +04:00
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-13 17:27:59 +04:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/sysctl.h>
|
2016-11-22 04:32:45 +03:00
|
|
|
#include <sys/user.h>
|
2016-09-27 18:24:56 +03:00
|
|
|
#include <libutil.h>
|
2014-03-13 17:27:59 +04:00
|
|
|
#endif
|
|
|
|
|
2017-10-28 22:48:33 +03:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "qemu/mmap-alloc.h"
|
2015-09-24 14:41:17 +03:00
|
|
|
|
2016-09-27 12:58:45 +03:00
|
|
|
#ifdef CONFIG_DEBUG_STACK_USAGE
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#endif
|
|
|
|
|
2017-03-21 09:50:06 +03:00
|
|
|
#define MAX_MEM_PREALLOC_THREAD_COUNT 16
|
2017-02-24 06:31:43 +03:00
|
|
|
|
|
|
|
struct MemsetThread {
|
|
|
|
char *addr;
|
2017-10-16 23:29:12 +03:00
|
|
|
size_t numpages;
|
|
|
|
size_t hpagesize;
|
2017-02-24 06:31:43 +03:00
|
|
|
QemuThread pgthread;
|
|
|
|
sigjmp_buf env;
|
|
|
|
};
|
|
|
|
typedef struct MemsetThread MemsetThread;
|
|
|
|
|
|
|
|
static MemsetThread *memset_thread;
|
|
|
|
static int memset_num_threads;
|
|
|
|
static bool memset_thread_failed;
|
|
|
|
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
static QemuMutex page_mutex;
|
|
|
|
static QemuCond page_cond;
|
|
|
|
static bool threads_created_flag;
|
|
|
|
|
2011-09-12 18:20:11 +04:00
|
|
|
int qemu_get_thread_id(void)
|
|
|
|
{
|
|
|
|
#if defined(__linux__)
|
|
|
|
return syscall(SYS_gettid);
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
2011-06-07 07:34:10 +04:00
|
|
|
|
|
|
|
int qemu_daemon(int nochdir, int noclose)
|
|
|
|
{
|
|
|
|
return daemon(nochdir, noclose);
|
|
|
|
}
|
|
|
|
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 17:53:12 +03:00
|
|
|
bool qemu_write_pidfile(const char *path, Error **errp)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char pidstr[32];
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct stat a, b;
|
2018-08-31 17:53:13 +03:00
|
|
|
struct flock lock = {
|
|
|
|
.l_type = F_WRLCK,
|
|
|
|
.l_whence = SEEK_SET,
|
|
|
|
.l_len = 0,
|
|
|
|
};
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 17:53:12 +03:00
|
|
|
|
|
|
|
fd = qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd == -1) {
|
|
|
|
error_setg_errno(errp, errno, "Cannot open pid file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &b) < 0) {
|
|
|
|
error_setg_errno(errp, errno, "Cannot stat file");
|
|
|
|
goto fail_close;
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:53:13 +03:00
|
|
|
if (fcntl(fd, F_SETLK, &lock)) {
|
util: add qemu_write_pidfile()
There are variants of qemu_create_pidfile() in qemu-pr-helper and
qemu-ga. Let's have a common implementation in libqemuutil.
The code is initially based from pr-helper write_pidfile(), with
various improvements and suggestions from Daniel Berrangé:
QEMU will leave the pidfile existing on disk when it exits which
initially made me think it avoids the deletion race. The app
managing QEMU, however, may well delete the pidfile after it has
seen QEMU exit, and even if the app locks the pidfile before
deleting it, there is still a race.
eg consider the following sequence
QEMU 1 libvirtd QEMU 2
1. lock(pidfile)
2. exit()
3. open(pidfile)
4. lock(pidfile)
5. open(pidfile)
6. unlink(pidfile)
7. close(pidfile)
8. lock(pidfile)
IOW, at step 8 the new QEMU has successfully acquired the lock, but
the pidfile no longer exists on disk because it was deleted after
the original QEMU exited.
While we could just say no external app should ever delete the
pidfile, I don't think that is satisfactory as people don't read
docs, and admins don't like stale pidfiles being left around on
disk.
To make this robust, I think we might want to copy libvirt's
approach to pidfile acquisition which runs in a loop and checks that
the file on disk /after/ acquiring the lock matches the file that
was locked. Then we could in fact safely let QEMU delete its own
pidfiles on clean exit..
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20180831145314.14736-2-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-08-31 17:53:12 +03:00
|
|
|
error_setg_errno(errp, errno, "Cannot lock pid file");
|
|
|
|
goto fail_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now make sure the path we locked is the same one that now
|
|
|
|
* exists on the filesystem.
|
|
|
|
*/
|
|
|
|
if (stat(path, &a) < 0) {
|
|
|
|
/*
|
|
|
|
* PID file disappeared, someone else must be racing with
|
|
|
|
* us, so try again.
|
|
|
|
*/
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.st_ino == b.st_ino) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PID file was recreated, someone else must be racing with
|
|
|
|
* us, so try again.
|
|
|
|
*/
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ftruncate(fd, 0) < 0) {
|
|
|
|
error_setg_errno(errp, errno, "Failed to truncate pid file");
|
|
|
|
goto fail_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(pidstr, sizeof(pidstr), FMT_pid "\n", getpid());
|
|
|
|
if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
|
|
|
|
error_setg(errp, "Failed to write pid file");
|
|
|
|
goto fail_unlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
fail_unlink:
|
|
|
|
unlink(path);
|
|
|
|
fail_close:
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-26 12:39:26 +04:00
|
|
|
void *qemu_oom_check(void *ptr)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
|
|
|
if (ptr == NULL) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:24:05 +04:00
|
|
|
void *qemu_try_memalign(size_t alignment, size_t size)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
|
|
|
void *ptr;
|
2013-11-30 00:29:17 +04:00
|
|
|
|
|
|
|
if (alignment < sizeof(void*)) {
|
|
|
|
alignment = sizeof(void*);
|
|
|
|
}
|
|
|
|
|
2018-01-04 20:39:36 +03:00
|
|
|
#if defined(CONFIG_POSIX_MEMALIGN)
|
2010-10-26 12:39:19 +04:00
|
|
|
int ret;
|
|
|
|
ret = posix_memalign(&ptr, alignment, size);
|
|
|
|
if (ret != 0) {
|
2014-05-20 14:24:05 +04:00
|
|
|
errno = ret;
|
|
|
|
ptr = NULL;
|
2010-10-26 12:39:19 +04:00
|
|
|
}
|
|
|
|
#elif defined(CONFIG_BSD)
|
2014-05-20 14:24:05 +04:00
|
|
|
ptr = valloc(size);
|
2010-10-26 12:39:19 +04:00
|
|
|
#else
|
2014-05-20 14:24:05 +04:00
|
|
|
ptr = memalign(alignment, size);
|
2010-10-26 12:39:19 +04:00
|
|
|
#endif
|
|
|
|
trace_qemu_memalign(alignment, size, ptr);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:24:05 +04:00
|
|
|
void *qemu_memalign(size_t alignment, size_t size)
|
|
|
|
{
|
|
|
|
return qemu_oom_check(qemu_try_memalign(alignment, size));
|
|
|
|
}
|
|
|
|
|
2010-10-26 12:39:19 +04:00
|
|
|
/* alloc shared memory pages */
|
2017-12-13 17:37:37 +03:00
|
|
|
void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)
|
2010-10-26 12:39:19 +04:00
|
|
|
{
|
2011-09-05 12:07:05 +04:00
|
|
|
size_t align = QEMU_VMALLOC_ALIGN;
|
2019-02-08 13:10:37 +03:00
|
|
|
void *ptr = qemu_ram_mmap(-1, size, align, shared, false);
|
2011-09-05 12:07:05 +04:00
|
|
|
|
2013-04-09 19:43:43 +04:00
|
|
|
if (ptr == MAP_FAILED) {
|
2013-07-31 17:11:11 +04:00
|
|
|
return NULL;
|
2011-11-01 00:29:46 +04:00
|
|
|
}
|
|
|
|
|
2014-10-31 19:38:37 +03:00
|
|
|
if (alignment) {
|
|
|
|
*alignment = align;
|
|
|
|
}
|
2015-09-10 16:36:51 +03:00
|
|
|
|
2013-05-13 18:19:55 +04:00
|
|
|
trace_qemu_anon_ram_alloc(size, ptr);
|
2011-07-25 19:13:36 +04:00
|
|
|
return ptr;
|
2010-10-26 12:39:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_vfree(void *ptr)
|
|
|
|
{
|
|
|
|
trace_qemu_vfree(ptr);
|
|
|
|
free(ptr);
|
|
|
|
}
|
2010-10-26 12:39:20 +04:00
|
|
|
|
2013-05-13 18:19:56 +04:00
|
|
|
void qemu_anon_ram_free(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
trace_qemu_anon_ram_free(ptr, size);
|
2019-01-31 02:36:05 +03:00
|
|
|
qemu_ram_munmap(-1, ptr, size);
|
2013-05-13 18:19:56 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 13:10:43 +04:00
|
|
|
void qemu_set_block(int fd)
|
2011-10-05 11:17:32 +04:00
|
|
|
{
|
|
|
|
int f;
|
|
|
|
f = fcntl(fd, F_GETFL);
|
2018-12-15 15:03:53 +03:00
|
|
|
assert(f != -1);
|
|
|
|
f = fcntl(fd, F_SETFL, f & ~O_NONBLOCK);
|
|
|
|
assert(f != -1);
|
2011-10-05 11:17:32 +04:00
|
|
|
}
|
|
|
|
|
2013-03-27 13:10:43 +04:00
|
|
|
void qemu_set_nonblock(int fd)
|
2010-10-26 12:39:20 +04:00
|
|
|
{
|
|
|
|
int f;
|
|
|
|
f = fcntl(fd, F_GETFL);
|
2018-12-15 15:03:53 +03:00
|
|
|
assert(f != -1);
|
|
|
|
f = fcntl(fd, F_SETFL, f | O_NONBLOCK);
|
oslib-posix: Ignore fcntl("/dev/null", F_SETFL, O_NONBLOCK) failure
Previous to OpenBSD 6.3 [1], fcntl(F_SETFL) is not permitted on
memory devices.
Trying this call sets errno to ENODEV ("not a memory device"):
19 ENODEV Operation not supported by device.
An attempt was made to apply an inappropriate function to a device,
for example, trying to read a write-only device such as a printer.
Do not assert fcntl failures in this specific case (errno set to ENODEV)
on OpenBSD. This fixes:
$ lm32-softmmu/qemu-system-lm32
assertion "f != -1" failed: file "util/oslib-posix.c", line 247, function "qemu_set_nonblock"
Abort trap (core dumped)
[1] The fix seems https://github.com/openbsd/src/commit/c2a35b387f9d3c
"fcntl(F_SETFL) invokes the FIONBIO and FIOASYNC ioctls internally, so
the memory devices (/dev/null, /dev/zero, etc) need to permit them."
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20190307142822.8531-2-philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-03-07 17:28:21 +03:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
if (f == -1) {
|
|
|
|
/*
|
|
|
|
* Previous to OpenBSD 6.3, fcntl(F_SETFL) is not permitted on
|
|
|
|
* memory devices and sets errno to ENODEV.
|
|
|
|
* It's OK if we fail to set O_NONBLOCK on devices like /dev/null,
|
|
|
|
* because they will never block anyway.
|
|
|
|
*/
|
|
|
|
assert(errno == ENODEV);
|
|
|
|
}
|
|
|
|
#else
|
2018-12-15 15:03:53 +03:00
|
|
|
assert(f != -1);
|
oslib-posix: Ignore fcntl("/dev/null", F_SETFL, O_NONBLOCK) failure
Previous to OpenBSD 6.3 [1], fcntl(F_SETFL) is not permitted on
memory devices.
Trying this call sets errno to ENODEV ("not a memory device"):
19 ENODEV Operation not supported by device.
An attempt was made to apply an inappropriate function to a device,
for example, trying to read a write-only device such as a printer.
Do not assert fcntl failures in this specific case (errno set to ENODEV)
on OpenBSD. This fixes:
$ lm32-softmmu/qemu-system-lm32
assertion "f != -1" failed: file "util/oslib-posix.c", line 247, function "qemu_set_nonblock"
Abort trap (core dumped)
[1] The fix seems https://github.com/openbsd/src/commit/c2a35b387f9d3c
"fcntl(F_SETFL) invokes the FIONBIO and FIOASYNC ioctls internally, so
the memory devices (/dev/null, /dev/zero, etc) need to permit them."
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20190307142822.8531-2-philmd@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-03-07 17:28:21 +03:00
|
|
|
#endif
|
2010-10-26 12:39:20 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 14:23:12 +04:00
|
|
|
int socket_set_fast_reuse(int fd)
|
|
|
|
{
|
|
|
|
int val = 1, ret;
|
|
|
|
|
|
|
|
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(const char *)&val, sizeof(val));
|
|
|
|
|
|
|
|
assert(ret == 0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-26 12:39:20 +04:00
|
|
|
void qemu_set_cloexec(int fd)
|
|
|
|
{
|
|
|
|
int f;
|
|
|
|
f = fcntl(fd, F_GETFD);
|
2017-05-09 22:04:52 +03:00
|
|
|
assert(f != -1);
|
|
|
|
f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
|
|
|
|
assert(f != -1);
|
2010-10-26 12:39:20 +04:00
|
|
|
}
|
2010-10-26 12:39:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates a pipe with FD_CLOEXEC set on both file descriptors
|
|
|
|
*/
|
|
|
|
int qemu_pipe(int pipefd[2])
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PIPE2
|
|
|
|
ret = pipe2(pipefd, O_CLOEXEC);
|
|
|
|
if (ret != -1 || errno != ENOSYS) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ret = pipe(pipefd);
|
|
|
|
if (ret == 0) {
|
|
|
|
qemu_set_cloexec(pipefd[0]);
|
|
|
|
qemu_set_cloexec(pipefd[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-11-24 05:38:10 +03:00
|
|
|
|
2013-05-18 08:31:48 +04:00
|
|
|
char *
|
|
|
|
qemu_get_local_state_pathname(const char *relative_pathname)
|
|
|
|
{
|
|
|
|
return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR,
|
|
|
|
relative_pathname);
|
|
|
|
}
|
2013-11-14 14:54:16 +04:00
|
|
|
|
|
|
|
void qemu_set_tty_echo(int fd, bool echo)
|
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
|
|
|
|
tcgetattr(fd, &tty);
|
|
|
|
|
|
|
|
if (echo) {
|
|
|
|
tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN;
|
|
|
|
} else {
|
|
|
|
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
tcsetattr(fd, TCSANOW, &tty);
|
|
|
|
}
|
2014-02-10 10:48:51 +04:00
|
|
|
|
|
|
|
static char exec_dir[PATH_MAX];
|
|
|
|
|
|
|
|
void qemu_init_exec_dir(const char *argv0)
|
|
|
|
{
|
|
|
|
char *dir;
|
|
|
|
char *p = NULL;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
|
|
|
|
assert(!exec_dir[0]);
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
|
|
|
|
if (len > 0) {
|
|
|
|
buf[len] = 0;
|
|
|
|
p = buf;
|
|
|
|
}
|
|
|
|
}
|
2017-10-28 22:48:33 +03:00
|
|
|
#elif defined(__FreeBSD__) \
|
|
|
|
|| (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
|
2014-02-10 10:48:51 +04:00
|
|
|
{
|
2017-10-28 22:48:33 +03:00
|
|
|
#if defined(__FreeBSD__)
|
2014-02-10 10:48:51 +04:00
|
|
|
static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
|
2017-10-28 22:48:33 +03:00
|
|
|
#else
|
|
|
|
static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
|
|
|
|
#endif
|
2014-02-10 10:48:51 +04:00
|
|
|
size_t len = sizeof(buf) - 1;
|
|
|
|
|
|
|
|
*buf = '\0';
|
|
|
|
if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
|
|
|
|
*buf) {
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
p = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* If we don't have any way of figuring out the actual executable
|
|
|
|
location then try argv[0]. */
|
|
|
|
if (!p) {
|
|
|
|
if (!argv0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p = realpath(argv0, buf);
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 05:46:24 +03:00
|
|
|
dir = g_path_get_dirname(p);
|
2014-02-10 10:48:51 +04:00
|
|
|
|
|
|
|
pstrcpy(exec_dir, sizeof(exec_dir), dir);
|
2016-04-07 05:46:24 +03:00
|
|
|
|
|
|
|
g_free(dir);
|
2014-02-10 10:48:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *qemu_get_exec_dir(void)
|
|
|
|
{
|
|
|
|
return g_strdup(exec_dir);
|
|
|
|
}
|
2014-05-14 13:43:21 +04:00
|
|
|
|
|
|
|
static void sigbus_handler(int signal)
|
|
|
|
{
|
2017-02-24 06:31:43 +03:00
|
|
|
int i;
|
|
|
|
if (memset_thread) {
|
|
|
|
for (i = 0; i < memset_num_threads; i++) {
|
|
|
|
if (qemu_thread_is_self(&memset_thread[i].pgthread)) {
|
|
|
|
siglongjmp(memset_thread[i].env, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 13:43:21 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
static void *do_touch_pages(void *arg)
|
|
|
|
{
|
|
|
|
MemsetThread *memset_args = (MemsetThread *)arg;
|
|
|
|
sigset_t set, oldset;
|
|
|
|
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
/*
|
|
|
|
* On Linux, the page faults from the loop below can cause mmap_sem
|
|
|
|
* contention with allocation of the thread stacks. Do not start
|
|
|
|
* clearing until all threads have been created.
|
|
|
|
*/
|
|
|
|
qemu_mutex_lock(&page_mutex);
|
|
|
|
while(!threads_created_flag){
|
|
|
|
qemu_cond_wait(&page_cond, &page_mutex);
|
|
|
|
}
|
|
|
|
qemu_mutex_unlock(&page_mutex);
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
/* unblock SIGBUS */
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, SIGBUS);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
|
|
|
|
|
|
|
|
if (sigsetjmp(memset_args->env, 1)) {
|
|
|
|
memset_thread_failed = true;
|
|
|
|
} else {
|
2017-10-16 23:29:12 +03:00
|
|
|
char *addr = memset_args->addr;
|
|
|
|
size_t numpages = memset_args->numpages;
|
|
|
|
size_t hpagesize = memset_args->hpagesize;
|
|
|
|
size_t i;
|
2017-02-24 06:31:43 +03:00
|
|
|
for (i = 0; i < numpages; i++) {
|
2017-03-03 14:32:55 +03:00
|
|
|
/*
|
|
|
|
* Read & write back the same value, so we don't
|
|
|
|
* corrupt existing user/app data that might be
|
|
|
|
* stored.
|
|
|
|
*
|
|
|
|
* 'volatile' to stop compiler optimizing this away
|
|
|
|
* to a no-op
|
|
|
|
*
|
|
|
|
* TODO: get a better solution from kernel so we
|
|
|
|
* don't need to write at all so we don't cause
|
|
|
|
* wear on the storage backing the region...
|
|
|
|
*/
|
|
|
|
*(volatile char *)addr = *addr;
|
2017-02-24 06:31:43 +03:00
|
|
|
addr += hpagesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-21 09:50:06 +03:00
|
|
|
static inline int get_memset_num_threads(int smp_cpus)
|
|
|
|
{
|
|
|
|
long host_procs = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (host_procs > 0) {
|
|
|
|
ret = MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), smp_cpus);
|
|
|
|
}
|
|
|
|
/* In case sysconf() fails, we fall back to single threaded */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages,
|
|
|
|
int smp_cpus)
|
|
|
|
{
|
2020-03-10 20:58:30 +03:00
|
|
|
static gsize initialized = 0;
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
size_t numpages_per_thread, leftover;
|
2017-02-24 06:31:43 +03:00
|
|
|
char *addr = area;
|
|
|
|
int i = 0;
|
|
|
|
|
2020-03-10 20:58:30 +03:00
|
|
|
if (g_once_init_enter(&initialized)) {
|
|
|
|
qemu_mutex_init(&page_mutex);
|
|
|
|
qemu_cond_init(&page_cond);
|
|
|
|
g_once_init_leave(&initialized, 1);
|
|
|
|
}
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
memset_thread_failed = false;
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
threads_created_flag = false;
|
2017-03-21 09:50:06 +03:00
|
|
|
memset_num_threads = get_memset_num_threads(smp_cpus);
|
2017-02-24 06:31:43 +03:00
|
|
|
memset_thread = g_new0(MemsetThread, memset_num_threads);
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
numpages_per_thread = numpages / memset_num_threads;
|
|
|
|
leftover = numpages % memset_num_threads;
|
2017-02-24 06:31:43 +03:00
|
|
|
for (i = 0; i < memset_num_threads; i++) {
|
|
|
|
memset_thread[i].addr = addr;
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
memset_thread[i].numpages = numpages_per_thread + (i < leftover);
|
2017-02-24 06:31:43 +03:00
|
|
|
memset_thread[i].hpagesize = hpagesize;
|
|
|
|
qemu_thread_create(&memset_thread[i].pgthread, "touch_pages",
|
|
|
|
do_touch_pages, &memset_thread[i],
|
|
|
|
QEMU_THREAD_JOINABLE);
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
addr += memset_thread[i].numpages * hpagesize;
|
2017-02-24 06:31:43 +03:00
|
|
|
}
|
mem-prealloc: optimize large guest startup
[desc]:
Large memory VM starts slowly when using -mem-prealloc, and
there are some areas to optimize in current method;
1、mmap will be used to alloc threads stack during create page
clearing threads, and it will attempt mm->mmap_sem for write
lock, but clearing threads have hold read lock, this competition
will cause threads createion very slow;
2、methods of calcuating pages for per threads is not well;if we use
64 threads to split 160 hugepage,63 threads clear 2page,1 thread
clear 34 page,so the entire speed is very slow;
to solve the first problem,we add a mutex in thread function,and
start all threads when all threads finished createion;
and the second problem, we spread remainder to other threads,in
situation that 160 hugepage and 64 threads, there are 32 threads
clear 3 pages,and 32 threads clear 2 pages.
[test]:
320G 84c VM start time can be reduced to 10s
680G 84c VM start time can be reduced to 18s
Signed-off-by: bauerchen <bauerchen@tencent.com>
Reviewed-by: Pan Rui <ruippan@tencent.com>
Reviewed-by: Ivan Ren <ivanren@tencent.com>
[Simplify computation of the number of pages per thread. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-02-11 12:10:35 +03:00
|
|
|
threads_created_flag = true;
|
|
|
|
qemu_cond_broadcast(&page_cond);
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
for (i = 0; i < memset_num_threads; i++) {
|
|
|
|
qemu_thread_join(&memset_thread[i].pgthread);
|
|
|
|
}
|
|
|
|
g_free(memset_thread);
|
|
|
|
memset_thread = NULL;
|
|
|
|
|
|
|
|
return memset_thread_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
|
|
|
|
Error **errp)
|
2014-05-14 13:43:21 +04:00
|
|
|
{
|
2014-06-25 00:52:29 +04:00
|
|
|
int ret;
|
2014-05-14 13:43:21 +04:00
|
|
|
struct sigaction act, oldact;
|
2017-02-24 06:31:43 +03:00
|
|
|
size_t hpagesize = qemu_fd_getpagesize(fd);
|
|
|
|
size_t numpages = DIV_ROUND_UP(memory, hpagesize);
|
2014-05-14 13:43:21 +04:00
|
|
|
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_handler = &sigbus_handler;
|
|
|
|
act.sa_flags = 0;
|
|
|
|
|
|
|
|
ret = sigaction(SIGBUS, &act, &oldact);
|
|
|
|
if (ret) {
|
2016-07-20 12:54:03 +03:00
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"os_mem_prealloc: failed to install signal handler");
|
|
|
|
return;
|
2014-05-14 13:43:21 +04:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:31:43 +03:00
|
|
|
/* touch pages simultaneously */
|
|
|
|
if (touch_all_pages(area, hpagesize, numpages, smp_cpus)) {
|
2016-07-20 12:54:03 +03:00
|
|
|
error_setg(errp, "os_mem_prealloc: Insufficient free host memory "
|
2017-05-16 03:11:49 +03:00
|
|
|
"pages available to allocate guest RAM");
|
2016-07-20 12:54:03 +03:00
|
|
|
}
|
2014-05-14 13:43:21 +04:00
|
|
|
|
2016-07-20 12:54:03 +03:00
|
|
|
ret = sigaction(SIGBUS, &oldact, NULL);
|
|
|
|
if (ret) {
|
|
|
|
/* Terminate QEMU since it can't recover from error */
|
|
|
|
perror("os_mem_prealloc: failed to reinstall signal handler");
|
|
|
|
exit(1);
|
2014-06-25 00:52:29 +04:00
|
|
|
}
|
2014-05-14 13:43:21 +04:00
|
|
|
}
|
2015-05-12 19:09:19 +03:00
|
|
|
|
2016-09-27 18:24:56 +03:00
|
|
|
char *qemu_get_pid_name(pid_t pid)
|
|
|
|
{
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
/* BSDs don't have /proc, but they provide a nice substitute */
|
|
|
|
struct kinfo_proc *proc = kinfo_getproc(pid);
|
|
|
|
|
|
|
|
if (proc) {
|
|
|
|
name = g_strdup(proc->ki_comm);
|
|
|
|
free(proc);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Assume a system with reasonable procfs */
|
|
|
|
char *pid_path;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pid_path = g_strdup_printf("/proc/%d/cmdline", pid);
|
|
|
|
g_file_get_contents(pid_path, &name, &len, NULL);
|
|
|
|
g_free(pid_path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-28 16:40:01 +03:00
|
|
|
pid_t qemu_fork(Error **errp)
|
|
|
|
{
|
|
|
|
sigset_t oldmask, newmask;
|
|
|
|
struct sigaction sig_action;
|
|
|
|
int saved_errno;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to block signals now, so that child process can safely
|
|
|
|
* kill off caller's signal handlers without a race.
|
|
|
|
*/
|
|
|
|
sigfillset(&newmask);
|
|
|
|
if (pthread_sigmask(SIG_SETMASK, &newmask, &oldmask) != 0) {
|
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"cannot block signals");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
saved_errno = errno;
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
/* attempt to restore signal mask, but ignore failure, to
|
|
|
|
* avoid obscuring the fork failure */
|
|
|
|
(void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
error_setg_errno(errp, saved_errno,
|
|
|
|
"cannot fork child process");
|
|
|
|
errno = saved_errno;
|
|
|
|
return -1;
|
|
|
|
} else if (pid) {
|
|
|
|
/* parent process */
|
|
|
|
|
|
|
|
/* Restore our original signal mask now that the child is
|
|
|
|
* safely running. Only documented failures are EFAULT (not
|
|
|
|
* possible, since we are using just-grabbed mask) or EINVAL
|
|
|
|
* (not possible, since we are using correct arguments). */
|
|
|
|
(void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
} else {
|
|
|
|
/* child process */
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Clear out all signal handlers from parent so nothing
|
|
|
|
* unexpected can happen in our child once we unblock
|
|
|
|
* signals */
|
|
|
|
sig_action.sa_handler = SIG_DFL;
|
|
|
|
sig_action.sa_flags = 0;
|
|
|
|
sigemptyset(&sig_action.sa_mask);
|
|
|
|
|
|
|
|
for (i = 1; i < NSIG; i++) {
|
|
|
|
/* Only possible errors are EFAULT or EINVAL The former
|
|
|
|
* won't happen, the latter we expect, so no need to check
|
|
|
|
* return value */
|
|
|
|
(void)sigaction(i, &sig_action, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unmask all signals in child, since we've no idea what the
|
|
|
|
* caller's done with their signal mask and don't want to
|
|
|
|
* propagate that to children */
|
|
|
|
sigemptyset(&newmask);
|
|
|
|
if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) {
|
|
|
|
Error *local_err = NULL;
|
|
|
|
error_setg_errno(&local_err, errno,
|
|
|
|
"cannot unblock signals");
|
|
|
|
error_report_err(local_err);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
2016-09-27 12:58:40 +03:00
|
|
|
|
|
|
|
void *qemu_alloc_stack(size_t *sz)
|
|
|
|
{
|
|
|
|
void *ptr, *guardpage;
|
2018-10-19 15:52:39 +03:00
|
|
|
int flags;
|
2016-09-27 12:58:45 +03:00
|
|
|
#ifdef CONFIG_DEBUG_STACK_USAGE
|
|
|
|
void *ptr2;
|
|
|
|
#endif
|
2019-10-13 05:11:45 +03:00
|
|
|
size_t pagesz = qemu_real_host_page_size;
|
2016-09-27 12:58:40 +03:00
|
|
|
#ifdef _SC_THREAD_STACK_MIN
|
|
|
|
/* avoid stacks smaller than _SC_THREAD_STACK_MIN */
|
|
|
|
long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN);
|
|
|
|
*sz = MAX(MAX(min_stack_sz, 0), *sz);
|
|
|
|
#endif
|
|
|
|
/* adjust stack size to a multiple of the page size */
|
|
|
|
*sz = ROUND_UP(*sz, pagesz);
|
|
|
|
/* allocate one extra page for the guard page */
|
|
|
|
*sz += pagesz;
|
|
|
|
|
2018-10-19 15:52:39 +03:00
|
|
|
flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
|
|
|
#if defined(MAP_STACK) && defined(__OpenBSD__)
|
|
|
|
/* Only enable MAP_STACK on OpenBSD. Other OS's such as
|
|
|
|
* Linux/FreeBSD/NetBSD have a flag with the same name
|
|
|
|
* but have differing functionality. OpenBSD will SEGV
|
|
|
|
* if it spots execution with a stack pointer pointing
|
|
|
|
* at memory that was not allocated with MAP_STACK.
|
|
|
|
*/
|
|
|
|
flags |= MAP_STACK;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
|
2016-09-27 12:58:40 +03:00
|
|
|
if (ptr == MAP_FAILED) {
|
2017-08-30 00:20:53 +03:00
|
|
|
perror("failed to allocate memory for stack");
|
2016-09-27 12:58:40 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HOST_IA64)
|
|
|
|
/* separate register stack */
|
|
|
|
guardpage = ptr + (((*sz - pagesz) / 2) & ~pagesz);
|
|
|
|
#elif defined(HOST_HPPA)
|
|
|
|
/* stack grows up */
|
|
|
|
guardpage = ptr + *sz - pagesz;
|
|
|
|
#else
|
|
|
|
/* stack grows down */
|
|
|
|
guardpage = ptr;
|
|
|
|
#endif
|
|
|
|
if (mprotect(guardpage, pagesz, PROT_NONE) != 0) {
|
2017-08-30 00:20:53 +03:00
|
|
|
perror("failed to set up stack guard page");
|
2016-09-27 12:58:40 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2016-09-27 12:58:45 +03:00
|
|
|
#ifdef CONFIG_DEBUG_STACK_USAGE
|
|
|
|
for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) {
|
|
|
|
*(uint32_t *)ptr2 = 0xdeadbeaf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-27 12:58:40 +03:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2016-09-27 12:58:45 +03:00
|
|
|
#ifdef CONFIG_DEBUG_STACK_USAGE
|
|
|
|
static __thread unsigned int max_stack_usage;
|
|
|
|
#endif
|
|
|
|
|
2016-09-27 12:58:40 +03:00
|
|
|
void qemu_free_stack(void *stack, size_t sz)
|
|
|
|
{
|
2016-09-27 12:58:45 +03:00
|
|
|
#ifdef CONFIG_DEBUG_STACK_USAGE
|
|
|
|
unsigned int usage;
|
|
|
|
void *ptr;
|
|
|
|
|
2019-10-13 05:11:45 +03:00
|
|
|
for (ptr = stack + qemu_real_host_page_size; ptr < stack + sz;
|
2016-09-27 12:58:45 +03:00
|
|
|
ptr += sizeof(uint32_t)) {
|
|
|
|
if (*(uint32_t *)ptr != 0xdeadbeaf) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usage = sz - (uintptr_t) (ptr - stack);
|
|
|
|
if (usage > max_stack_usage) {
|
|
|
|
error_report("thread %d max stack usage increased from %u to %u",
|
|
|
|
qemu_get_thread_id(), max_stack_usage, usage);
|
|
|
|
max_stack_usage = usage;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-27 12:58:40 +03:00
|
|
|
munmap(stack, sz);
|
|
|
|
}
|
2017-02-08 15:22:12 +03:00
|
|
|
|
|
|
|
void sigaction_invoke(struct sigaction *action,
|
|
|
|
struct qemu_signalfd_siginfo *info)
|
|
|
|
{
|
2017-07-20 19:32:21 +03:00
|
|
|
siginfo_t si = {};
|
2017-02-08 15:22:12 +03:00
|
|
|
si.si_signo = info->ssi_signo;
|
|
|
|
si.si_errno = info->ssi_errno;
|
|
|
|
si.si_code = info->ssi_code;
|
|
|
|
|
|
|
|
/* Convert the minimal set of fields defined by POSIX.
|
|
|
|
* Positive si_code values are reserved for kernel-generated
|
|
|
|
* signals, where the valid siginfo fields are determined by
|
|
|
|
* the signal number. But according to POSIX, it is unspecified
|
|
|
|
* whether SI_USER and SI_QUEUE have values less than or equal to
|
|
|
|
* zero.
|
|
|
|
*/
|
|
|
|
if (info->ssi_code == SI_USER || info->ssi_code == SI_QUEUE ||
|
|
|
|
info->ssi_code <= 0) {
|
|
|
|
/* SIGTERM, etc. */
|
|
|
|
si.si_pid = info->ssi_pid;
|
|
|
|
si.si_uid = info->ssi_uid;
|
|
|
|
} else if (info->ssi_signo == SIGILL || info->ssi_signo == SIGFPE ||
|
|
|
|
info->ssi_signo == SIGSEGV || info->ssi_signo == SIGBUS) {
|
|
|
|
si.si_addr = (void *)(uintptr_t)info->ssi_addr;
|
|
|
|
} else if (info->ssi_signo == SIGCHLD) {
|
|
|
|
si.si_pid = info->ssi_pid;
|
|
|
|
si.si_status = info->ssi_status;
|
|
|
|
si.si_uid = info->ssi_uid;
|
|
|
|
}
|
|
|
|
action->sa_sigaction(info->ssi_signo, &si, NULL);
|
|
|
|
}
|