From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720350773886.3533957736915; Fri, 10 Feb 2017 01:52:30 -0800 (PST) Received: from localhost ([::1]:42786 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7sT-0001Wm-5y for importer@patchew.org; Fri, 10 Feb 2017 04:52:29 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33623) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qN-0000KX-OH for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qL-0005N8-Jk for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:19 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:35541) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qL-0005MS-AL for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:17 -0500 Received: by mail-wr0-x242.google.com with SMTP id o16so14577459wra.2 for ; Fri, 10 Feb 2017 01:50:15 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=rjtzyiZRQKHr5EYsU/Cnb8jTjqVfOgetl8DKlAlN0yE=; b=YasEdVdtEtZZKs99oaPCK7+QPCbqocEvpi+l9JeX8Bvm56U6Yx5Ts/EUX9/7WsbmlX yisb+NQj5KZ51WizgRxEU+aJEzWQnxeqrBZJ00ktpS6raEAMjB80gH9TCOnHg/0EiB+e sODKJyHQ1inu/SlskZ5AzrzljXcD7w7qDqev71g3TEQiaFoqpW1oBviNkaeZloVmoLD8 Hd5r0OGGJU4vWatifkXcyRvkS5uOzbz7brM61wk+xzFsp7opqcYWntlEYi2s6RLhe00r rpBe2z5K6bmlbItuJImmIGjBio+G/uKBPX1RIMJJI9co8rcOwN3Yq41ogErTaPr5CVYf U+nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=rjtzyiZRQKHr5EYsU/Cnb8jTjqVfOgetl8DKlAlN0yE=; b=Ihd1GG799NPsPC/kB0iRQAvv7y4zQJ9OjLxfT94fptuVVazkg3dhZkXXo1vbdHLehw xJY3k7NueDn26OOswIqb1+BZQ1zdXYLUj40DMvp+A1Q3/a20bprdOUgHd5sSiOJJgZr+ roaaDzfahFejd7BOJvRztm3wR3JZ5Nf8IH9Mb6bZh+H8FO+mCBoof8Jbft5wLaixqokZ bXS+qgNK0ljfXcKJkY3gfBZpFSmMjoOB+OfvP9n4+47/4p3DN6vvxnwwYauLNtpgZzGf mLpfdRoFbE3ILf9ZNGwvlptdntW20Vp5mg9bmk9EnYDP2Ulrkt8GXDKXuMKSHnEFnvoL bPFQ== X-Gm-Message-State: AMke39kDRY6eODoxgn9m/TKWfd8KH84/nxcZVDdLSLJbbwO+EFcCceHcy7USapR/3ok1aw== X-Received: by 10.223.128.5 with SMTP id 5mr6811064wrk.163.1486720214677; Fri, 10 Feb 2017 01:50:14 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:06 +0100 Message-Id: <20170210095012.16039-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PATCH 1/7] cpus: remove ugly cast on sigbus_handler X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The cast is there because sigbus_handler is invoked via sigfd_handler. But it feels just wrong to use struct qemu_signalfd_siginfo in the prototype of a function that is passed to sigaction. Instead, do a simple-minded conversion of qemu_signalfd_siginfo to siginfo_t. Signed-off-by: Paolo Bonzini --- cpus.c | 12 +++--------- include/qemu/compatfd.h | 42 ------------------------------------------ include/qemu/osdep.h | 26 ++++++++++++++++++++++++++ main-loop.c | 5 +---- os-win32.c | 7 +++++++ util/compatfd.c | 1 - util/oslib-posix.c | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 56 deletions(-) delete mode 100644 include/qemu/compatfd.h diff --git a/cpus.c b/cpus.c index 71a82e5..b28e08e 100644 --- a/cpus.c +++ b/cpus.c @@ -49,10 +49,6 @@ #include "hw/nmi.h" #include "sysemu/replay.h" =20 -#ifndef _WIN32 -#include "qemu/compatfd.h" -#endif - #ifdef CONFIG_LINUX =20 #include @@ -794,11 +790,9 @@ static void sigbus_reraise(void) abort(); } =20 -static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, - void *ctx) +static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) { - if (kvm_on_sigbus(siginfo->ssi_code, - (void *)(intptr_t)siginfo->ssi_addr)) { + if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { sigbus_reraise(); } } @@ -809,7 +803,7 @@ static void qemu_init_sigbus(void) =20 memset(&action, 0, sizeof(action)); action.sa_flags =3D SA_SIGINFO; - action.sa_sigaction =3D (void (*)(int, siginfo_t*, void*))sigbus_handl= er; + action.sa_sigaction =3D sigbus_handler; sigaction(SIGBUS, &action, NULL); =20 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); diff --git a/include/qemu/compatfd.h b/include/qemu/compatfd.h deleted file mode 100644 index aa12ee9..0000000 --- a/include/qemu/compatfd.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * signalfd/eventfd compatibility - * - * Copyright IBM, Corp. 2008 - * - * Authors: - * Anthony Liguori - * - * This work is licensed under the terms of the GNU GPL, version 2. See - * the COPYING file in the top-level directory. - * - */ - -#ifndef QEMU_COMPATFD_H -#define QEMU_COMPATFD_H - - -struct qemu_signalfd_siginfo { - uint32_t ssi_signo; /* Signal number */ - int32_t ssi_errno; /* Error number (unused) */ - int32_t ssi_code; /* Signal code */ - uint32_t ssi_pid; /* PID of sender */ - uint32_t ssi_uid; /* Real UID of sender */ - int32_t ssi_fd; /* File descriptor (SIGIO) */ - uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */ - uint32_t ssi_band; /* Band event (SIGIO) */ - uint32_t ssi_overrun; /* POSIX timer overrun count */ - uint32_t ssi_trapno; /* Trap number that caused signal */ - int32_t ssi_status; /* Exit status or signal (SIGCHLD) */ - int32_t ssi_int; /* Integer sent by sigqueue(2) */ - uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */ - uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */ - uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */ - uint64_t ssi_addr; /* Address that generated signal - (for hardware-generated signals) */ - uint8_t pad[48]; /* Pad size to 128 bytes (allow for - additional fields in the future) */ -}; - -int qemu_signalfd(const sigset_t *mask); - -#endif diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 689f253..5201dbd 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -290,6 +290,32 @@ void qemu_anon_ram_free(void *ptr, size_t size); # define QEMU_VMALLOC_ALIGN getpagesize() #endif =20 +struct qemu_signalfd_siginfo { + uint32_t ssi_signo; /* Signal number */ + int32_t ssi_errno; /* Error number (unused) */ + int32_t ssi_code; /* Signal code */ + uint32_t ssi_pid; /* PID of sender */ + uint32_t ssi_uid; /* Real UID of sender */ + int32_t ssi_fd; /* File descriptor (SIGIO) */ + uint32_t ssi_tid; /* Kernel timer ID (POSIX timers) */ + uint32_t ssi_band; /* Band event (SIGIO) */ + uint32_t ssi_overrun; /* POSIX timer overrun count */ + uint32_t ssi_trapno; /* Trap number that caused signal */ + int32_t ssi_status; /* Exit status or signal (SIGCHLD) */ + int32_t ssi_int; /* Integer sent by sigqueue(2) */ + uint64_t ssi_ptr; /* Pointer sent by sigqueue(2) */ + uint64_t ssi_utime; /* User CPU time consumed (SIGCHLD) */ + uint64_t ssi_stime; /* System CPU time consumed (SIGCHLD) */ + uint64_t ssi_addr; /* Address that generated signal + (for hardware-generated signals) */ + uint8_t pad[48]; /* Pad size to 128 bytes (allow for + additional fields in the future) */ +}; + +int qemu_signalfd(const sigset_t *mask); +void sigaction_invoke(struct sigaction *action, + struct qemu_signalfd_siginfo *info); + int qemu_madvise(void *addr, size_t len, int advice); =20 int qemu_open(const char *name, int flags, ...); diff --git a/main-loop.c b/main-loop.c index ad10bca..ca7bb07 100644 --- a/main-loop.c +++ b/main-loop.c @@ -34,8 +34,6 @@ =20 #ifndef _WIN32 =20 -#include "qemu/compatfd.h" - /* If we have signalfd, we mask out the signals we want to handle and then * use signalfd to listen for them. We rely on whatever the current signal * handler is to dispatch the signals when we receive them. @@ -63,8 +61,7 @@ static void sigfd_handler(void *opaque) =20 sigaction(info.ssi_signo, NULL, &action); if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) { - action.sa_sigaction(info.ssi_signo, - (siginfo_t *)&info, NULL); + sigaction_invoke(&action, &info); } else if (action.sa_handler) { action.sa_handler(info.ssi_signo); } diff --git a/os-win32.c b/os-win32.c index ae98574..949829c 100644 --- a/os-win32.c +++ b/os-win32.c @@ -77,6 +77,13 @@ void os_setup_early_signal_handling(void) atexit(os_undo_timer_resolution); } =20 +void sigaction_invoke(struct sigaction *action, + struct qemu_signalfd_siginfo *info) +{ + /* SA_SIGINFO not used on Windows. */ + abort(); +} + /* Look for support files in the same directory as the executable. */ char *os_find_datadir(void) { diff --git a/util/compatfd.c b/util/compatfd.c index 9a43042..980bd33 100644 --- a/util/compatfd.c +++ b/util/compatfd.c @@ -15,7 +15,6 @@ =20 #include "qemu/osdep.h" #include "qemu-common.h" -#include "qemu/compatfd.h" #include "qemu/thread.h" =20 #include diff --git a/util/oslib-posix.c b/util/oslib-posix.c index f631464..bba6b26 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -603,3 +603,36 @@ void qemu_free_stack(void *stack, size_t sz) =20 munmap(stack, sz); } + +void sigaction_invoke(struct sigaction *action, + struct qemu_signalfd_siginfo *info) +{ + siginfo_t si =3D { 0 }; + si.si_signo =3D info->ssi_signo; + si.si_errno =3D info->ssi_errno; + si.si_code =3D 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 =3D=3D SI_USER || info->ssi_code =3D=3D SI_QUEUE || + info->ssi_code <=3D 0) { + /* SIGTERM, etc. */ + si.si_pid =3D info->ssi_pid; + si.si_uid =3D info->ssi_uid; + } else if (info->ssi_signo =3D=3D SIGILL || info->ssi_signo =3D=3D SIG= FPE || + info->ssi_signo =3D=3D SIGSEGV || info->ssi_signo =3D=3D SI= GBUS) { + si.si_addr =3D (void *)(uintptr_t)info->ssi_addr; + } else if (info->ssi_signo =3D=3D SIGCHLD) { + si.si_pid =3D info->ssi_pid; + si.si_status =3D info->ssi_status; + si.si_uid =3D info->ssi_uid; + } else if (info->ssi_signo =3D=3D SIGPOLL) { + si.si_band =3D info->ssi_band; + } + action->sa_sigaction(info->ssi_signo, &si, NULL); +} --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720344292123.78488862860786; Fri, 10 Feb 2017 01:52:24 -0800 (PST) Received: from localhost ([::1]:42784 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7sM-0001Nr-5Y for importer@patchew.org; Fri, 10 Feb 2017 04:52:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33620) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qN-0000KW-Nm for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qL-0005Mx-7f for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:19 -0500 Received: from mail-wr0-x243.google.com ([2a00:1450:400c:c0c::243]:35542) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qL-0005Mb-10 for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:17 -0500 Received: by mail-wr0-x243.google.com with SMTP id o16so14577508wra.2 for ; Fri, 10 Feb 2017 01:50:16 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=oX/buaOcVLZZekiBuJB43zKw9GGtxx9nLPtjnMhu9Ds=; b=ZyWNaTH/r17NbnQ/q6QHzZxpuRk/rQ5JlomrsSgPvLrl9rrBJfbudScZ7ts8af0b2i nxAb1SscKi068JlqBdQj7rgkzXkcLNONZtLfNULks8js+8nvUDSazHhX15HLCOXaNHFq hUH+5VJwuBAI8gPEdDvIK2vNbVLGYYHf3U6szS3ktXlgwkFaIIdbWXrlG9Jh4mhKTE1t Q74O5Z6ePkOFJR3Se+aDGzr+x53eWt7eHMwG/MuwRuhJjkScbSVVFRoOb+Cf3Yuuj1XU gJPd/jOFW9hF2smEfpn3U6X5bhHxZQ7LPsdyhuKoJ5zyHgSlvdM73E8+a16YaW8btHqV qkOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=oX/buaOcVLZZekiBuJB43zKw9GGtxx9nLPtjnMhu9Ds=; b=Dp0up2S4QBBM2mYLUWWPt+ptPh2knMgz3F279Jbj0FYmhEH1IJQECaNuCQ2Lb0HCW1 tAhAN0fkNyKneMcsDoFEBSp/NsPrkLduTiyNJLPr8uL9v7NnQqQUcTDA9uxfeTfVVfyd CkaWC7YMxCRTp8R0GDQsQlpvD3nob0suAYgqK4zMk7TnCnmgr/t9VpcusB+6YonzQeWC 5+8G6ObAt26o1ic1d+oC0s8I4NHpnXva9dQSOASGAI37AzgPzUk6GPh4D7Km+0SLrt1n ZT0045JF8gfTJKrPUqj+scRezXeLTTdRY/wh924lbUvxmjAxn5O6FYgUUsQzzxLFa0t1 qIzQ== X-Gm-Message-State: AMke39kKN2pcDGwuCQK2lMb+s9fhaACtxHAPOrabic7MFp4lWCl9pZY8/5sOv1zjRRWpzw== X-Received: by 10.223.178.87 with SMTP id y23mr6402919wra.91.1486720215697; Fri, 10 Feb 2017 01:50:15 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:07 +0100 Message-Id: <20170210095012.16039-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c0c::243 Subject: [Qemu-devel] [PATCH 2/7] KVM: x86: cleanup SIGBUS handlers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch should have no semantic change. Signed-off-by: Paolo Bonzini --- target/i386/kvm.c | 81 ++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 27fd050..0c48dfd 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -469,31 +469,34 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, vo= id *addr) ram_addr_t ram_addr; hwaddr paddr; =20 - if ((env->mcg_cap & MCG_SER_P) && addr - && (code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO)) { + if (code !=3D BUS_MCEERR_AR && code !=3D BUS_MCEERR_AO) { + return 1; + } + + /* Because the MCE happened while running the VCPU, KVM could have + * injected action required MCEs too. Action optional MCEs should + * be delivered to the main thread, which qemu_init_sigbus identifies + * as the "early kill" thread, but if we get one for whatever reason + * we just handle it just like the main thread would. + */ + if ((env->mcg_cap & MCG_SER_P) && addr) { ram_addr =3D qemu_ram_addr_from_host(addr); - if (ram_addr =3D=3D RAM_ADDR_INVALID || - !kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr= )) { - fprintf(stderr, "Hardware memory error for memory used by " - "QEMU itself instead of guest system!\n"); - /* Hope we are lucky for AO MCE */ - if (code =3D=3D BUS_MCEERR_AO) { - return 0; - } else { - hardware_memory_error(); - } - } - kvm_hwpoison_page_add(ram_addr); - kvm_mce_inject(cpu, paddr, code); - } else { - if (code =3D=3D BUS_MCEERR_AO) { + if (ram_addr !=3D RAM_ADDR_INVALID && + kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)= ) { + kvm_hwpoison_page_add(ram_addr); + kvm_mce_inject(cpu, paddr, code); return 0; - } else if (code =3D=3D BUS_MCEERR_AR) { - hardware_memory_error(); - } else { - return 1; } + + fprintf(stderr, "Hardware memory error for memory used by " + "QEMU itself instead of guest system!\n"); + } + + if (code =3D=3D BUS_MCEERR_AR) { + hardware_memory_error(); } + + /* Hope we are lucky for AO MCE */ return 0; } =20 @@ -501,29 +504,29 @@ int kvm_arch_on_sigbus(int code, void *addr) { X86CPU *cpu =3D X86_CPU(first_cpu); =20 - if ((cpu->env.mcg_cap & MCG_SER_P) && addr && code =3D=3D BUS_MCEERR_A= O) { + if (code !=3D BUS_MCEERR_AR && code !=3D BUS_MCEERR_AO) { + return 1; + } + + if (code =3D=3D BUS_MCEERR_AR) { + hardware_memory_error(); + } + + /* Hope we are lucky for AO MCE */ + if ((cpu->env.mcg_cap & MCG_SER_P) && addr) { ram_addr_t ram_addr; hwaddr paddr; =20 - /* Hope we are lucky for AO MCE */ ram_addr =3D qemu_ram_addr_from_host(addr); - if (ram_addr =3D=3D RAM_ADDR_INVALID || - !kvm_physical_memory_addr_from_host(first_cpu->kvm_state, - addr, &paddr)) { - fprintf(stderr, "Hardware memory error for memory used by " - "QEMU itself instead of guest system!: %p\n", addr); - return 0; - } - kvm_hwpoison_page_add(ram_addr); - kvm_mce_inject(X86_CPU(first_cpu), paddr, code); - } else { - if (code =3D=3D BUS_MCEERR_AO) { - return 0; - } else if (code =3D=3D BUS_MCEERR_AR) { - hardware_memory_error(); - } else { - return 1; + if (ram_addr !=3D RAM_ADDR_INVALID && + kvm_physical_memory_addr_from_host(first_cpu->kvm_state, + addr, &paddr)) { + kvm_hwpoison_page_add(ram_addr); + kvm_mce_inject(X86_CPU(first_cpu), paddr, code); } + + fprintf(stderr, "Hardware memory error for memory used by " + "QEMU itself instead of guest system!: %p\n", addr); } return 0; } --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 148672057935294.40577021832758; Fri, 10 Feb 2017 01:56:19 -0800 (PST) Received: from localhost ([::1]:42807 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7w9-0005AJ-V9 for importer@patchew.org; Fri, 10 Feb 2017 04:56:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33619) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qN-0000KV-NQ for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qM-0005NG-5U for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:19 -0500 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:36070) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qL-0005Mv-SW for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:18 -0500 Received: by mail-wm0-x241.google.com with SMTP id r18so6570939wmd.3 for ; Fri, 10 Feb 2017 01:50:17 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=7iuHp2TSDO6xpoxr2LIsR9b6KA1lp63PfeOzAGrKcFI=; b=tc51BLt188OKtSojTv4yWWqZ9Em2doW0PRq63sdbHK8oUt5lmN0d4OWUcbe5IKml4M ASZsh6RZSUyWx2Cf6lXcUnsk8MEO/sARzNUHjbB5cSQWlijfT4PY79o4zaJcjrgRnESa 0QuV4Vh2Olx8kG8jZu2GQHOgOBwBzEko8EL9o4nKy5X7rgIyMhaHZv3YzmvNvhSZkPbl g4cNn2wEmMrykq51eYvATI/b3cJ++J2Tivbny677Ag3kp7thpiKKFFRGANelwjvyEqh4 jZgUO7aq0dwkOCidZ0lPVxKXyicrT1KsS9LGiECqJ1KtKMH05zFZpMZp5zi1gGhzxUUK 9YAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=7iuHp2TSDO6xpoxr2LIsR9b6KA1lp63PfeOzAGrKcFI=; b=X0vr425gjdkhWi8FrMTk5StuMgC/kWrQOomojuQHw1PV6nJPfdhH3lOK8Bh+fsLHTv f4fJiZID4I1ShjdbhBdg4XHpSdzjm/P8lPxqTmoWSR/Jvk1SbH98vnOMWfCRohED3Us+ eS4Hjya0Zv1L/cBl9bt78sdsIRHbJaSxTL7XbGJ8+JW5lxX1jF1+6emGYTuBd2wDOk1k xj4gmfWdQRVoD3Z6gu+OV+ynAzSd1IFKw9yrkVGufIuZt2CAgnfvHaNOQNnatZLJUmy9 4chP/v83jHfj0stqr7RojP3qU0oBqHDLLxmmF7qljErXoCQA6d5bM9t+e6EbbPvIPGUH NoDg== X-Gm-Message-State: AMke39nzlMYA9iaXbI4jfYcIA3gMzmYZGT99MvID/KfMsP7ozFnB4uUvc+FaYUB1tF+uPg== X-Received: by 10.28.228.213 with SMTP id b204mr27687591wmh.59.1486720216740; Fri, 10 Feb 2017 01:50:16 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:08 +0100 Message-Id: <20170210095012.16039-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 3/7] cpus: reorganize signal handling code X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Move the KVM "eat signals" code under CONFIG_LINUX, in preparation for moving it to kvm-all.c; reraise non-MCE SIGBUS immediately, without passing it to KVM. Signed-off-by: Paolo Bonzini --- cpus.c | 63 ++++++++++++++++++++++++++----------------------= ---- include/qemu/osdep.h | 9 ++++++++ target/i386/kvm.c | 15 ++----------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/cpus.c b/cpus.c index b28e08e..ce80783 100644 --- a/cpus.c +++ b/cpus.c @@ -792,6 +792,10 @@ static void sigbus_reraise(void) =20 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) { + if (siginfo->si_code !=3D BUS_MCEERR_AO && siginfo->si_code !=3D BUS_M= CEERR_AR) { + sigbus_reraise(); + } + if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { sigbus_reraise(); } @@ -809,6 +813,30 @@ static void qemu_init_sigbus(void) prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); } =20 +static void dummy_signal(int sig) +{ +} + +static void qemu_kvm_init_cpu_signals(CPUState *cpu) +{ + int r; + sigset_t set; + struct sigaction sigact; + + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler =3D dummy_signal; + sigaction(SIG_IPI, &sigact, NULL); + + pthread_sigmask(SIG_BLOCK, NULL, &set); + sigdelset(&set, SIG_IPI); + sigdelset(&set, SIGBUS); + r =3D kvm_set_signal_mask(cpu, &set); + if (r) { + fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); + exit(1); + } +} + static void qemu_kvm_eat_signals(CPUState *cpu) { struct timespec ts =3D { 0, 0 }; @@ -830,6 +858,9 @@ static void qemu_kvm_eat_signals(CPUState *cpu) =20 switch (r) { case SIGBUS: + if (siginfo.si_code !=3D BUS_MCEERR_AO && siginfo.si_code !=3D= BUS_MCEERR_AR) { + sigbus_reraise(); + } if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr))= { sigbus_reraise(); } @@ -845,9 +876,7 @@ static void qemu_kvm_eat_signals(CPUState *cpu) } } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)= ); } - #else /* !CONFIG_LINUX */ - static void qemu_init_sigbus(void) { } @@ -855,39 +884,11 @@ static void qemu_init_sigbus(void) static void qemu_kvm_eat_signals(CPUState *cpu) { } -#endif /* !CONFIG_LINUX */ - -#ifndef _WIN32 -static void dummy_signal(int sig) -{ -} - -static void qemu_kvm_init_cpu_signals(CPUState *cpu) -{ - int r; - sigset_t set; - struct sigaction sigact; - - memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler =3D dummy_signal; - sigaction(SIG_IPI, &sigact, NULL); - - pthread_sigmask(SIG_BLOCK, NULL, &set); - sigdelset(&set, SIG_IPI); - sigdelset(&set, SIGBUS); - r =3D kvm_set_signal_mask(cpu, &set); - if (r) { - fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); - exit(1); - } -} =20 -#else /* _WIN32 */ static void qemu_kvm_init_cpu_signals(CPUState *cpu) { - abort(); } -#endif /* _WIN32 */ +#endif /* !CONFIG_LINUX */ =20 static QemuMutex qemu_global_mutex; static QemuCond qemu_io_proceeded_cond; diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 5201dbd..48bed95 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -277,6 +277,15 @@ void qemu_anon_ram_free(void *ptr, size_t size); =20 #endif =20 +#if defined(CONFIG_LINUX) +#ifndef BUS_MCEERR_AR +#define BUS_MCEERR_AR 4 +#endif +#ifndef BUS_MCEERR_AO +#define BUS_MCEERR_AO 5 +#endif +#endif + #if defined(__linux__) && \ (defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)) /* Use 2 MiB alignment so transparent hugepages can be used by KVM. diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 0c48dfd..f49a786 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -64,13 +64,6 @@ * 255 kvm_msr_entry structs */ #define MSR_BUF_SIZE 4096 =20 -#ifndef BUS_MCEERR_AR -#define BUS_MCEERR_AR 4 -#endif -#ifndef BUS_MCEERR_AO -#define BUS_MCEERR_AO 5 -#endif - const KVMCapabilityInfo kvm_arch_required_capabilities[] =3D { KVM_CAP_INFO(SET_TSS_ADDR), KVM_CAP_INFO(EXT_CPUID), @@ -469,9 +462,7 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void= *addr) ram_addr_t ram_addr; hwaddr paddr; =20 - if (code !=3D BUS_MCEERR_AR && code !=3D BUS_MCEERR_AO) { - return 1; - } + assert(code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO); =20 /* Because the MCE happened while running the VCPU, KVM could have * injected action required MCEs too. Action optional MCEs should @@ -504,9 +495,7 @@ int kvm_arch_on_sigbus(int code, void *addr) { X86CPU *cpu =3D X86_CPU(first_cpu); =20 - if (code !=3D BUS_MCEERR_AR && code !=3D BUS_MCEERR_AO) { - return 1; - } + assert(code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO); =20 if (code =3D=3D BUS_MCEERR_AR) { hardware_memory_error(); --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720581233548.6047244889459; Fri, 10 Feb 2017 01:56:21 -0800 (PST) Received: from localhost ([::1]:42808 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7wC-0005CF-3z for importer@patchew.org; Fri, 10 Feb 2017 04:56:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33638) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qO-0000KZ-LS for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qN-0005Nd-Fm for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:20 -0500 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:33663) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qN-0005NL-60 for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:19 -0500 Received: by mail-wm0-x244.google.com with SMTP id v77so6539127wmv.0 for ; Fri, 10 Feb 2017 01:50:19 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=Wn1NrodBsqLuBXxSv82i28LCpG2kEtRBtHC9JrelxuI=; b=X74P3YRefQiFL0Wb41UxBMeBSEPdqhpEUTMcEU31qtrlXm9gKNCToGGLHMhBgDT87X +QJc1gkW83dLAknEJfeqMYjFaXBHrI02AKyTgpdZhqBD1UobDEO1cRSPGKlSGrLTbG/Y l+DkMojZ1kXeMDk2muYg6e4oPlpMF/IMTxyJXrgcQ1mkdJkWdgFy+OA43VOuydOWQDLG rOhRGGo3H7Wi+y88qAZjGALaJCzGEA0VwZ85kcvYkZbjx0A9OqyvcHdKSan6bXRxV2j2 vDmexTfM1PYQ/HTxaxImLbyKwHgKjtZ/POdYgsGx3dbLw/bZkf9hWYVP9/oZ886DXRV2 RSKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Wn1NrodBsqLuBXxSv82i28LCpG2kEtRBtHC9JrelxuI=; b=H8ZZ+8/RHJIkEsQlsAnHP5RlOPto2z/gA1sBJ4aUmr+2rDxKSKqelVkVLBQQTaPL8B bWJkzxXpJELfl2EtQejZvjcn8WK0OzmJrayW+//183OEif9rXynOD9glPmamSpXKFDf3 JK/4vInQ2+7D7W7U5xFGlB0n+y8P35dGWUzWQwzDMvfymSVQv3bXnOSTZNYElajQEN4O DtyMv/+2MAL1l/PSWWB1fCzPh0XKjQT4tlcdrtRXbuz6JiqTJL2rYSAvs5iDfJKV717J uudRR/4rCp7JtKxd/GNRnVEdfi8jNdTB/zRyAD638nSgjOrZRcVDhisFDTC9kuOp89l1 10zA== X-Gm-Message-State: AMke39kkyOIWd5QuqfJXS7lqb2Bo7LMrdiNJY+SiTkabMNBPWOjQuwhHsHPaE4gHURapiw== X-Received: by 10.28.226.67 with SMTP id z64mr24514277wmg.137.1486720217783; Fri, 10 Feb 2017 01:50:17 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:09 +0100 Message-Id: <20170210095012.16039-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Subject: [Qemu-devel] [PATCH 4/7] KVM: remove kvm_arch_on_sigbus X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Build it on kvm_arch_on_sigbus_vcpu instead. They do the same for "action optional" SIGBUSes, and the main thread should never get "action required" SIGBUSes because it blocks the signal. Signed-off-by: Paolo Bonzini --- include/sysemu/kvm.h | 1 - kvm-all.c | 9 ++++++++- target/arm/kvm.c | 5 ----- target/i386/kvm.c | 40 +++++----------------------------------- target/mips/kvm.c | 6 ------ target/ppc/kvm.c | 5 ----- target/s390x/kvm.c | 5 ----- 7 files changed, 13 insertions(+), 58 deletions(-) diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index 3045ee7..6ecb61c 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -358,7 +358,6 @@ bool kvm_vcpu_id_is_valid(int vcpu_id); unsigned long kvm_arch_vcpu_id(CPUState *cpu); =20 int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); -int kvm_arch_on_sigbus(int code, void *addr); =20 void kvm_arch_init_irq_routing(KVMState *s); =20 diff --git a/kvm-all.c b/kvm-all.c index 330219e..33ebeb1 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -2390,6 +2390,7 @@ int kvm_set_signal_mask(CPUState *cpu, const sigset_t= *sigset) =20 return r; } + int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) { return kvm_arch_on_sigbus_vcpu(cpu, code, addr); @@ -2397,7 +2398,13 @@ int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void= *addr) =20 int kvm_on_sigbus(int code, void *addr) { - return kvm_arch_on_sigbus(code, addr); + /* Action required MCE kills the process if SIGBUS is blocked. Because + * that's what happens in the I/O thread, where we handle MCE via sign= alfd, + * we can only get action optional here. + */ + assert(code !=3D BUS_MCEERR_AR); + kvm_arch_on_sigbus_vcpu(first_cpu, code, addr); + return 0; } =20 int kvm_create_device(KVMState *s, uint64_t type, bool test) diff --git a/target/arm/kvm.c b/target/arm/kvm.c index c00b94e..6f2d590 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -564,11 +564,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, vo= id *addr) return 1; } =20 -int kvm_arch_on_sigbus(int code, void *addr) -{ - return 1; -} - /* The #ifdef protections are until 32bit headers are imported and can * be removed once both 32 and 64 bit reach feature parity. */ diff --git a/target/i386/kvm.c b/target/i386/kvm.c index f49a786..8eac26c 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -462,14 +462,13 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, vo= id *addr) ram_addr_t ram_addr; hwaddr paddr; =20 + /* If we get an action required MCE, it has been injected by KVM + * while the VM was running. An action optional MCE instead should + * be coming from the main thread, which qemu_init_sigbus identifies + * as the "early kill" thread. + */ assert(code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO); =20 - /* Because the MCE happened while running the VCPU, KVM could have - * injected action required MCEs too. Action optional MCEs should - * be delivered to the main thread, which qemu_init_sigbus identifies - * as the "early kill" thread, but if we get one for whatever reason - * we just handle it just like the main thread would. - */ if ((env->mcg_cap & MCG_SER_P) && addr) { ram_addr =3D qemu_ram_addr_from_host(addr); if (ram_addr !=3D RAM_ADDR_INVALID && @@ -491,35 +490,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, voi= d *addr) return 0; } =20 -int kvm_arch_on_sigbus(int code, void *addr) -{ - X86CPU *cpu =3D X86_CPU(first_cpu); - - assert(code =3D=3D BUS_MCEERR_AR || code =3D=3D BUS_MCEERR_AO); - - if (code =3D=3D BUS_MCEERR_AR) { - hardware_memory_error(); - } - - /* Hope we are lucky for AO MCE */ - if ((cpu->env.mcg_cap & MCG_SER_P) && addr) { - ram_addr_t ram_addr; - hwaddr paddr; - - ram_addr =3D qemu_ram_addr_from_host(addr); - if (ram_addr !=3D RAM_ADDR_INVALID && - kvm_physical_memory_addr_from_host(first_cpu->kvm_state, - addr, &paddr)) { - kvm_hwpoison_page_add(ram_addr); - kvm_mce_inject(X86_CPU(first_cpu), paddr, code); - } - - fprintf(stderr, "Hardware memory error for memory used by " - "QEMU itself instead of guest system!: %p\n", addr); - } - return 0; -} - static int kvm_inject_mce_oldstyle(X86CPU *cpu) { CPUX86State *env =3D &cpu->env; diff --git a/target/mips/kvm.c b/target/mips/kvm.c index 998c341..3e686e7 100644 --- a/target/mips/kvm.c +++ b/target/mips/kvm.c @@ -186,12 +186,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, vo= id *addr) return 1; } =20 -int kvm_arch_on_sigbus(int code, void *addr) -{ - DPRINTF("%s\n", __func__); - return 1; -} - void kvm_arch_init_irq_routing(KVMState *s) { } diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index ec92c64..6a76cfc 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -2553,11 +2553,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code,= void *addr) return 1; } =20 -int kvm_arch_on_sigbus(int code, void *addr) -{ - return 1; -} - void kvm_arch_init_irq_routing(KVMState *s) { } diff --git a/target/s390x/kvm.c b/target/s390x/kvm.c index 6ed3876..fc6d04c 100644 --- a/target/s390x/kvm.c +++ b/target/s390x/kvm.c @@ -2108,11 +2108,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code,= void *addr) return 1; } =20 -int kvm_arch_on_sigbus(int code, void *addr) -{ - return 1; -} - void kvm_s390_io_interrupt(uint16_t subchannel_id, uint16_t subchannel_nr, uint32_t io_int_parm, uint32_t io_int_word) --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720761779337.31844371707314; Fri, 10 Feb 2017 01:59:21 -0800 (PST) Received: from localhost ([::1]:42820 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7z6-0007uR-Aa for importer@patchew.org; Fri, 10 Feb 2017 04:59:20 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33669) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qQ-0000LE-5b for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qO-0005ON-K9 for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:22 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:34071) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qO-0005Nj-BC for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:20 -0500 Received: by mail-wr0-x242.google.com with SMTP id 89so14538021wrr.1 for ; Fri, 10 Feb 2017 01:50:20 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=agySRbl5v+YyxdbTUDrZ71u5norVL9GvphcXgQnt2rY=; b=XN83w+IcunY9vsSWM8tpV4HTaGO8MMY1924OMxTZ0X2vwxnjjoAhzqR+ZGujCSSzxo cttXWnhwPoL8JM0yVl9Q7BmlZjxj89wRCeT7lQTvApa5MYyL5rTw6NC/xhdjDt11cZRh CVvYKpgTun3ncDWCxYpSeMUxXHZYxPpIEiD6zKwFbdcABVAy/xFEQPT4ujEt713BNT89 Jx1Wm6u31gDLlB1Z7Jdei+j5PAfazr1Xi0zMR7LxB1Y+GSnnBWGlf2WueQWGqFjxw7+8 or1o5MxCX6L4UXZO7ecfXw1B8uDokAjG1Nv1hCrROCg2ZCFuTZuNDJu6C7pvIU9oW3VG 0Ufw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=agySRbl5v+YyxdbTUDrZ71u5norVL9GvphcXgQnt2rY=; b=qGkwtm22yWZAh6AFWNQs8TD3IDvsBeO2uQAMgr/7hr0ZJ6XGAAFjwOHIVvQSnefs8b kKvVldJbiTKOzTiIuFi0QvQsp4zXmMRrL5tycb5t96hmUnG1s62V5hOgd3cKSJ9WbZ5f HU/bluCDNBB7SRuXllD+RWkhQ2V4bCmuuzdnSNZV9cjQonqHC8O74+fz9ZFpi67LmsKZ 6T5kZn+EMMbKhaf3yOqS0jSoho5QIA2PWLTCCzOwrinPDOy3IscfzwKq+iz1SuZxYabF pFal62uspo3xUssY+UDHj4wSvKvMlF5flp7WDZA+AArS5mB/ceg6DC8ef22OeU+L28ec +86g== X-Gm-Message-State: AMke39mWnHKdOLHorNsf2EDHufKYPj9jo7u65JEC8LCWSIEs8hWwCLij53Hp9m8+Wqeo3g== X-Received: by 10.223.164.151 with SMTP id g23mr7048495wrb.86.1486720219059; Fri, 10 Feb 2017 01:50:19 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:10 +0100 Message-Id: <20170210095012.16039-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PATCH 5/7] KVM: do not use sigtimedwait to catch SIGBUS X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Call kvm_on_sigbus_vcpu asynchronously from the VCPU thread. Information for the SIGBUS can be stored in thread-local variables and processed later in kvm_cpu_exec. Signed-off-by: Paolo Bonzini --- cpus.c | 31 +++++++++++++------------------ include/sysemu/kvm.h | 5 ++++- kvm-all.c | 31 ++++++++++++++++++++++++++++++- target/arm/kvm.c | 5 ----- target/i386/kvm.c | 5 ++--- target/mips/kvm.c | 6 ------ target/ppc/kvm.c | 5 ----- target/s390x/kvm.c | 5 ----- 8 files changed, 49 insertions(+), 44 deletions(-) diff --git a/cpus.c b/cpus.c index ce80783..9e1cd76 100644 --- a/cpus.c +++ b/cpus.c @@ -796,8 +796,16 @@ static void sigbus_handler(int n, siginfo_t *siginfo, = void *ctx) sigbus_reraise(); } =20 - if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { - sigbus_reraise(); + if (current_cpu) { + /* Called asynchronously in VCPU thread. */ + if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_= addr)) { + sigbus_reraise(); + } + } else { + /* Called synchronously (via signalfd) in main thread. */ + if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { + sigbus_reraise(); + } } } =20 @@ -828,8 +836,9 @@ static void qemu_kvm_init_cpu_signals(CPUState *cpu) sigaction(SIG_IPI, &sigact, NULL); =20 pthread_sigmask(SIG_BLOCK, NULL, &set); - sigdelset(&set, SIG_IPI); sigdelset(&set, SIGBUS); + pthread_sigmask(SIG_SETMASK, &set, NULL); + sigdelset(&set, SIG_IPI); r =3D kvm_set_signal_mask(cpu, &set); if (r) { fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); @@ -847,7 +856,6 @@ static void qemu_kvm_eat_signals(CPUState *cpu) =20 sigemptyset(&waitset); sigaddset(&waitset, SIG_IPI); - sigaddset(&waitset, SIGBUS); =20 do { r =3D sigtimedwait(&waitset, &siginfo, &ts); @@ -856,25 +864,12 @@ static void qemu_kvm_eat_signals(CPUState *cpu) exit(1); } =20 - switch (r) { - case SIGBUS: - if (siginfo.si_code !=3D BUS_MCEERR_AO && siginfo.si_code !=3D= BUS_MCEERR_AR) { - sigbus_reraise(); - } - if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr))= { - sigbus_reraise(); - } - break; - default: - break; - } - r =3D sigpending(&chkset); if (r =3D=3D -1) { perror("sigpending"); exit(1); } - } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)= ); + } while (sigismember(&chkset, SIG_IPI)); } #else /* !CONFIG_LINUX */ static void qemu_init_sigbus(void) diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index 6ecb61c..a1b019d 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -357,7 +357,10 @@ bool kvm_vcpu_id_is_valid(int vcpu_id); /* Returns VCPU ID to be used on KVM_CREATE_VCPU ioctl() */ unsigned long kvm_arch_vcpu_id(CPUState *cpu); =20 -int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); +#ifdef TARGET_I386 +#define KVM_HAVE_MCE_INJECTION 1 +void kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); +#endif =20 void kvm_arch_init_irq_routing(KVMState *s); =20 diff --git a/kvm-all.c b/kvm-all.c index 33ebeb1..1c817ac 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -1893,6 +1893,10 @@ void kvm_cpu_synchronize_post_init(CPUState *cpu) run_on_cpu(cpu, do_kvm_cpu_synchronize_post_init, RUN_ON_CPU_NULL); } =20 +static __thread void *pending_sigbus_addr; +static __thread int pending_sigbus_code; +static __thread bool have_sigbus_pending; + int kvm_cpu_exec(CPUState *cpu) { struct kvm_run *run =3D cpu->kvm_run; @@ -1930,6 +1934,14 @@ int kvm_cpu_exec(CPUState *cpu) =20 attrs =3D kvm_arch_post_run(cpu, run); =20 + if (unlikely(have_sigbus_pending)) { + qemu_mutex_lock_iothread(); + kvm_arch_on_sigbus_vcpu(cpu, pending_sigbus_code, + pending_sigbus_addr); + have_sigbus_pending =3D false; + qemu_mutex_unlock_iothread(); + } + if (run_ret < 0) { if (run_ret =3D=3D -EINTR || run_ret =3D=3D -EAGAIN) { DPRINTF("io window exit\n"); @@ -2391,13 +2403,27 @@ int kvm_set_signal_mask(CPUState *cpu, const sigset= _t *sigset) return r; } =20 +/* Called asynchronously in VCPU thread. */ int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) { - return kvm_arch_on_sigbus_vcpu(cpu, code, addr); +#ifdef KVM_HAVE_MCE_INJECTION + if (have_sigbus_pending) { + return 1; + } + have_sigbus_pending =3D true; + pending_sigbus_addr =3D addr; + pending_sigbus_code =3D code; + atomic_set(&cpu->exit_request, 1); + return 0; +#else + return 1; +#endif } =20 +/* Called synchronously (via signalfd) in main thread. */ int kvm_on_sigbus(int code, void *addr) { +#ifdef KVM_HAVE_MCE_INJECTION /* Action required MCE kills the process if SIGBUS is blocked. Because * that's what happens in the I/O thread, where we handle MCE via sign= alfd, * we can only get action optional here. @@ -2405,6 +2431,9 @@ int kvm_on_sigbus(int code, void *addr) assert(code !=3D BUS_MCEERR_AR); kvm_arch_on_sigbus_vcpu(first_cpu, code, addr); return 0; +#else + return 1; +#endif } =20 int kvm_create_device(KVMState *s, uint64_t type, bool test) diff --git a/target/arm/kvm.c b/target/arm/kvm.c index 6f2d590..c6d0b3a 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -559,11 +559,6 @@ int kvm_arch_process_async_events(CPUState *cs) return 0; } =20 -int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, void *addr) -{ - return 1; -} - /* The #ifdef protections are until 32bit headers are imported and can * be removed once both 32 and 64 bit reach feature parity. */ diff --git a/target/i386/kvm.c b/target/i386/kvm.c index 8eac26c..e252ddc 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -455,7 +455,7 @@ static void hardware_memory_error(void) exit(1); } =20 -int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr) +void kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void *addr) { X86CPU *cpu =3D X86_CPU(c); CPUX86State *env =3D &cpu->env; @@ -475,7 +475,7 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void= *addr) kvm_physical_memory_addr_from_host(c->kvm_state, addr, &paddr)= ) { kvm_hwpoison_page_add(ram_addr); kvm_mce_inject(cpu, paddr, code); - return 0; + return; } =20 fprintf(stderr, "Hardware memory error for memory used by " @@ -487,7 +487,6 @@ int kvm_arch_on_sigbus_vcpu(CPUState *c, int code, void= *addr) } =20 /* Hope we are lucky for AO MCE */ - return 0; } =20 static int kvm_inject_mce_oldstyle(X86CPU *cpu) diff --git a/target/mips/kvm.c b/target/mips/kvm.c index 3e686e7..0982e87 100644 --- a/target/mips/kvm.c +++ b/target/mips/kvm.c @@ -180,12 +180,6 @@ bool kvm_arch_stop_on_emulation_error(CPUState *cs) return true; } =20 -int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, void *addr) -{ - DPRINTF("%s\n", __func__); - return 1; -} - void kvm_arch_init_irq_routing(KVMState *s) { } diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index 6a76cfc..c6a45ab 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -2548,11 +2548,6 @@ bool kvm_arch_stop_on_emulation_error(CPUState *cpu) return true; } =20 -int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) -{ - return 1; -} - void kvm_arch_init_irq_routing(KVMState *s) { } diff --git a/target/s390x/kvm.c b/target/s390x/kvm.c index fc6d04c..9b59ba3 100644 --- a/target/s390x/kvm.c +++ b/target/s390x/kvm.c @@ -2103,11 +2103,6 @@ bool kvm_arch_stop_on_emulation_error(CPUState *cpu) return true; } =20 -int kvm_arch_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) -{ - return 1; -} - void kvm_s390_io_interrupt(uint16_t subchannel_id, uint16_t subchannel_nr, uint32_t io_int_parm, uint32_t io_int_word) --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720762769653.8821144818605; Fri, 10 Feb 2017 01:59:22 -0800 (PST) Received: from localhost ([::1]:42821 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7z7-0007vn-BW for importer@patchew.org; Fri, 10 Feb 2017 04:59:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33682) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qR-0000MI-9z for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qP-0005On-Ug for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:23 -0500 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:33863) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qP-0005OZ-Kz for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:21 -0500 Received: by mail-wm0-x244.google.com with SMTP id c85so6574022wmi.1 for ; Fri, 10 Feb 2017 01:50:21 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=0mxmD0y8dHdt9+3/JramW/pFY1zuRY3Z9lOaJsNU/jU=; b=L3fOKqgLtZFqgmmfAF5n0qusKFNeY929lT5/vtxp8EB/StsOMjxUMd21j4ugQ4hmfT +m3HrDaftnDt8Y8IENu6no6kvFCnPVpPcw9c00TgKTBm5ZK9SndRmdqrVuAWh/JNy2E6 Jnt2KAhHu6+C9T/MV/KhheBvXOcrQIrSA540zhWVbDcDK3EfV0NCP6OorfdfkWHVWOie X/boUGLiy7n91nBCJhCOqW7Qsi51PeY3h7zxKPjgyjoXxWBORjz+Tp9aP0WMMkv4Fejy V2ZVZMRAnjhWpSlPBEkqVgBTNvU41PvR4EWpRs9maszUJR1xmQ9iGASe3wssmKjSzXsE x0xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=0mxmD0y8dHdt9+3/JramW/pFY1zuRY3Z9lOaJsNU/jU=; b=MaxNreUBM61eQNFuRsfZGylwG5dU+lqg5164ejueICJ/4WnZ1PSTCcpc+hsQ7z65y1 TYTosBBQ5CfxpbSf/OetrcjIeTljO2987brDZICDnFiqgYjxvZG5WIuTobof9q+3ejOi ehFXSgTwlJjYXoU8inqznnXCSAB8zxfGTYGnQq9r4w0vc1DYhGUpNpYohIJXqoU7TUpB /zA7vg52ivC2pQh2VAGPAN3MPUu7ZW48s/1C/mK5n6qKxGZs/WTf2xETjmab+NhPPBqZ de0TTi2DMu4F97tsOSg6ox2+mcakhPmAtEifm4OgPmlxu+j1sx6s6T1rXbb6rtlNzWri TXPg== X-Gm-Message-State: AMke39lerzgIyy/ckrFF3W/+MptelkeecgMbhaJ+zqgr2L9FbsNZ4fUsoxBQsdpTomwiXQ== X-Received: by 10.28.138.136 with SMTP id m130mr24977961wmd.72.1486720220305; Fri, 10 Feb 2017 01:50:20 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:11 +0100 Message-Id: <20170210095012.16039-7-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Subject: [Qemu-devel] [PATCH 6/7] KVM: move SIG_IPI handling to kvm-all.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This lets us remove a bunch of CONFIG_LINUX defines. Signed-off-by: Paolo Bonzini --- cpus.c | 62 +-----------------------------------------------= ---- include/sysemu/kvm.h | 5 ++--- kvm-all.c | 60 +++++++++++++++++++++++++++++++++++++++++++++---= -- kvm-stub.c | 12 +++++----- 4 files changed, 63 insertions(+), 76 deletions(-) diff --git a/cpus.c b/cpus.c index 9e1cd76..9c351c1 100644 --- a/cpus.c +++ b/cpus.c @@ -820,69 +820,10 @@ static void qemu_init_sigbus(void) =20 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); } - -static void dummy_signal(int sig) -{ -} - -static void qemu_kvm_init_cpu_signals(CPUState *cpu) -{ - int r; - sigset_t set; - struct sigaction sigact; - - memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler =3D dummy_signal; - sigaction(SIG_IPI, &sigact, NULL); - - pthread_sigmask(SIG_BLOCK, NULL, &set); - sigdelset(&set, SIGBUS); - pthread_sigmask(SIG_SETMASK, &set, NULL); - sigdelset(&set, SIG_IPI); - r =3D kvm_set_signal_mask(cpu, &set); - if (r) { - fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); - exit(1); - } -} - -static void qemu_kvm_eat_signals(CPUState *cpu) -{ - struct timespec ts =3D { 0, 0 }; - siginfo_t siginfo; - sigset_t waitset; - sigset_t chkset; - int r; - - sigemptyset(&waitset); - sigaddset(&waitset, SIG_IPI); - - do { - r =3D sigtimedwait(&waitset, &siginfo, &ts); - if (r =3D=3D -1 && !(errno =3D=3D EAGAIN || errno =3D=3D EINTR)) { - perror("sigtimedwait"); - exit(1); - } - - r =3D sigpending(&chkset); - if (r =3D=3D -1) { - perror("sigpending"); - exit(1); - } - } while (sigismember(&chkset, SIG_IPI)); -} #else /* !CONFIG_LINUX */ static void qemu_init_sigbus(void) { } - -static void qemu_kvm_eat_signals(CPUState *cpu) -{ -} - -static void qemu_kvm_init_cpu_signals(CPUState *cpu) -{ -} #endif /* !CONFIG_LINUX */ =20 static QemuMutex qemu_global_mutex; @@ -956,7 +897,6 @@ static void qemu_kvm_wait_io_event(CPUState *cpu) qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); } =20 - qemu_kvm_eat_signals(cpu); qemu_wait_io_event_common(cpu); } =20 @@ -979,7 +919,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) exit(1); } =20 - qemu_kvm_init_cpu_signals(cpu); + kvm_init_cpu_signals(cpu); =20 /* signal CPU creation */ cpu->created =3D true; diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index a1b019d..24281fc 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -238,9 +238,6 @@ int kvm_remove_breakpoint(CPUState *cpu, target_ulong a= ddr, target_ulong len, int type); void kvm_remove_all_breakpoints(CPUState *cpu); int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap); -#ifndef _WIN32 -int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset); -#endif =20 int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); int kvm_on_sigbus(int code, void *addr); @@ -463,6 +460,8 @@ void kvm_cpu_synchronize_state(CPUState *cpu); void kvm_cpu_synchronize_post_reset(CPUState *cpu); void kvm_cpu_synchronize_post_init(CPUState *cpu); =20 +void kvm_init_cpu_signals(CPUState *cpu); + /** * kvm_irqchip_add_msi_route - Add MSI route for specific vector * @s: KVM state diff --git a/kvm-all.c b/kvm-all.c index 1c817ac..eaff0dc 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -1897,6 +1897,32 @@ static __thread void *pending_sigbus_addr; static __thread int pending_sigbus_code; static __thread bool have_sigbus_pending; =20 +static void kvm_eat_signals(CPUState *cpu) +{ + struct timespec ts =3D { 0, 0 }; + siginfo_t siginfo; + sigset_t waitset; + sigset_t chkset; + int r; + + sigemptyset(&waitset); + sigaddset(&waitset, SIG_IPI); + + do { + r =3D sigtimedwait(&waitset, &siginfo, &ts); + if (r =3D=3D -1 && !(errno =3D=3D EAGAIN || errno =3D=3D EINTR)) { + perror("sigtimedwait"); + exit(1); + } + + r =3D sigpending(&chkset); + if (r =3D=3D -1) { + perror("sigpending"); + exit(1); + } + } while (sigismember(&chkset, SIG_IPI)); +} + int kvm_cpu_exec(CPUState *cpu) { struct kvm_run *run =3D cpu->kvm_run; @@ -1945,6 +1971,7 @@ int kvm_cpu_exec(CPUState *cpu) if (run_ret < 0) { if (run_ret =3D=3D -EINTR || run_ret =3D=3D -EAGAIN) { DPRINTF("io window exit\n"); + kvm_eat_signals(cpu); ret =3D EXCP_INTERRUPT; break; } @@ -2383,16 +2410,12 @@ void kvm_remove_all_breakpoints(CPUState *cpu) } #endif /* !KVM_CAP_SET_GUEST_DEBUG */ =20 -int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset) +static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset) { KVMState *s =3D kvm_state; struct kvm_signal_mask *sigmask; int r; =20 - if (!sigset) { - return kvm_vcpu_ioctl(cpu, KVM_SET_SIGNAL_MASK, NULL); - } - sigmask =3D g_malloc(sizeof(*sigmask) + sizeof(*sigset)); =20 sigmask->len =3D s->sigmask_len; @@ -2403,6 +2426,33 @@ int kvm_set_signal_mask(CPUState *cpu, const sigset_= t *sigset) return r; } =20 +static void dummy_signal(int sig) +{ +} + +void kvm_init_cpu_signals(CPUState *cpu) +{ + int r; + sigset_t set; + struct sigaction sigact; + + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler =3D dummy_signal; + sigaction(SIG_IPI, &sigact, NULL); + + pthread_sigmask(SIG_BLOCK, NULL, &set); +#if defined KVM_HAVE_MCE_INJECTION + sigdelset(&set, SIGBUS); + pthread_sigmask(SIG_SETMASK, &set, NULL); +#endif + sigdelset(&set, SIG_IPI); + r =3D kvm_set_signal_mask(cpu, &set); + if (r) { + fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); + exit(1); + } +} + /* Called asynchronously in VCPU thread. */ int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) { diff --git a/kvm-stub.c b/kvm-stub.c index b1b6b96..ef0c734 100644 --- a/kvm-stub.c +++ b/kvm-stub.c @@ -95,13 +95,6 @@ void kvm_remove_all_breakpoints(CPUState *cpu) { } =20 -#ifndef _WIN32 -int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset) -{ - abort(); -} -#endif - int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) { return 1; @@ -157,4 +150,9 @@ bool kvm_has_free_slot(MachineState *ms) { return false; } + +void kvm_init_cpu_signals(CPUState *cpu) +{ + abort(); +} #endif --=20 1.8.3.1 From nobody Wed Apr 24 18:25:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1486720352359288.472743028118; Fri, 10 Feb 2017 01:52:32 -0800 (PST) Received: from localhost ([::1]:42787 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7sV-0001XX-3I for importer@patchew.org; Fri, 10 Feb 2017 04:52:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33688) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cc7qR-0000Mh-Q7 for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cc7qQ-0005P2-N3 for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:23 -0500 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:36074) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cc7qQ-0005Oj-Dn for qemu-devel@nongnu.org; Fri, 10 Feb 2017 04:50:22 -0500 Received: by mail-wm0-x243.google.com with SMTP id r18so6571166wmd.3 for ; Fri, 10 Feb 2017 01:50:22 -0800 (PST) Received: from donizetti.lan (94-39-187-56.adsl-ull.clienti.tiscali.it. [94.39.187.56]) by smtp.gmail.com with ESMTPSA id i203sm723664wmf.12.2017.02.10.01.50.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 01:50:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=m2frUyk0h+MVcrccMh/vkERPu2gtgeYDpNubSPrfugA=; b=V0ZaA+vHTtzV+yN+wmZNNP+jn6ISoPxgGmskj4RbikbgFiFH6Fms9/C0NMBHyDqaCN PmiZypgIDWesJ9kPLvWbtxiaAaePqxvpPrMdX3D9zCvoB8VM1ERdcVlnEy4a/7as1vUg BTLJOHQwBk4UZqOrpXGTYFmkKR7lcPzmp1mqvN/raEVqhQvdv0pZc3cIhssweMhrLcL6 eYDodsuXCFFhbSw1E/VeMv3lGq9vnRf+VI7mjPaXDLGwQxD4tcOHTsTGp+P/bRGA8Xmx CclT+29xZQbjYCiCuDcNRNgoY5HNMV6ydn8K5C7QQmk8C7objxgc5xIBjcexTR9b72FD 0X4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=m2frUyk0h+MVcrccMh/vkERPu2gtgeYDpNubSPrfugA=; b=Ct9ryKv4UJGO04uy3Zl5MKAs1iZ878fY7Cn1ntxj+K+2QQTX2KbYgWfC6wKqc+2FXI nmVvlCaDmFV/8356wufVCgU8q5b38ESXXpcJV8mjVA9Jp1vNhcfDYqU4fWwb+lyAtb8G vjOQBkDSpvjvUQDiX+2HomNwK3hW+cVmXs0EjphQpx5CByaIw9yvAGSxluhSEoIzkSoD 55VtTbzmYk0vIBZSUA2uBxvT4nnR5a+Ie8tpCYHqtLsH2J8IHql5nU7JrAF2SqC7kgkC ticKGWqQya5T3aJMR/dckZfwSozUPaS8jjyc/NuMJMAmWTijO6HpQS+V0sqLLJtlddRb AdGw== X-Gm-Message-State: AMke39l2y1YWbJzBF+KxWwCq6AbpDUFxk2lUfMkJojN4mXC4bGz4upXKoQUB3ve5Q7pyfg== X-Received: by 10.28.195.70 with SMTP id t67mr26840741wmf.98.1486720221347; Fri, 10 Feb 2017 01:50:21 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 10 Feb 2017 10:50:12 +0100 Message-Id: <20170210095012.16039-8-pbonzini@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170210095012.16039-1-pbonzini@redhat.com> References: <20170210095012.16039-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH 7/7] KVM: use KVM_CAP_IMMEDIATE_EXIT X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The purpose of the KVM_SET_SIGNAL_MASK API is to let userspace "kick" a VCPU out of KVM_RUN through a POSIX signal. A signal is attached to a dummy signal handler; by blocking the signal outside KVM_RUN and unblocking it inside, this possible race is closed: VCPU thread service thread -------------------------------------------------------------- check flag set flag raise signal (signal handler does nothing) KVM_RUN However, one issue with KVM_SET_SIGNAL_MASK is that it has to take tsk->sighand->siglock on every KVM_RUN. This lock is often on a remote NUMA node, because it is on the node of a thread's creator. Taking this lock can be very expensive if there are many userspace exits (as is the case for SMP Windows VMs without Hyper-V reference time counter). KVM_CAP_IMMEDIATE_EXIT provides an alternative, where the flag is placed directly in kvm_run so that KVM can see it: VCPU thread service thread -------------------------------------------------------------- raise signal signal handler set run->immediate_exit KVM_RUN check run->immediate_exit The previous patches changed QEMU so that the only blocked signal is SIG_IPI, so we can now stop using KVM_SET_SIGNAL_MASK and sigtimedwait if KVM_CAP_IMMEDIATE_EXIT is available. On a 14-VCPU guest, an "inl" operation goes down from 30k to 6k on an unlocked (no BQL) MemoryRegion, or from 30k to 15k if the BQL is involved. Signed-off-by: Paolo Bonzini --- kvm-all.c | 36 ++++++++++++++++++++++++++++++++---- linux-headers/linux/kvm.h | 4 +++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/kvm-all.c b/kvm-all.c index eaff0dc..083143f 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -120,6 +120,7 @@ bool kvm_vm_attributes_allowed; bool kvm_direct_msi_allowed; bool kvm_ioeventfd_any_length_allowed; bool kvm_msi_use_devid; +static bool kvm_immediate_exit; =20 static const KVMCapabilityInfo kvm_required_capabilites[] =3D { KVM_CAP_INFO(USER_MEMORY), @@ -1619,6 +1620,7 @@ static int kvm_init(MachineState *ms) goto err; } =20 + kvm_immediate_exit =3D kvm_check_extension(s, KVM_CAP_IMMEDIATE_EXIT); s->nr_slots =3D kvm_check_extension(s, KVM_CAP_NR_MEMSLOTS); =20 /* If unspecified, use the default value */ @@ -1897,6 +1899,20 @@ static __thread void *pending_sigbus_addr; static __thread int pending_sigbus_code; static __thread bool have_sigbus_pending; =20 +static void kvm_cpu_kick(CPUState *cpu) +{ + atomic_set(&cpu->kvm_run->immediate_exit, 1); +} + +static void kvm_cpu_kick_self(void) +{ + if (kvm_immediate_exit) { + kvm_cpu_kick(current_cpu); + } else { + qemu_cpu_kick_self(); + } +} + static void kvm_eat_signals(CPUState *cpu) { struct timespec ts =3D { 0, 0 }; @@ -1905,6 +1921,10 @@ static void kvm_eat_signals(CPUState *cpu) sigset_t chkset; int r; =20 + if (kvm_immediate_exit) { + return; + } + sigemptyset(&waitset); sigaddset(&waitset, SIG_IPI); =20 @@ -1953,7 +1973,7 @@ int kvm_cpu_exec(CPUState *cpu) * instruction emulation. This self-signal will ensure that we * leave ASAP again. */ - qemu_cpu_kick_self(); + kvm_cpu_kick_self(); } =20 run_ret =3D kvm_vcpu_ioctl(cpu, KVM_RUN, 0); @@ -2426,8 +2446,12 @@ static int kvm_set_signal_mask(CPUState *cpu, const = sigset_t *sigset) return r; } =20 -static void dummy_signal(int sig) +static void kvm_ipi_signal(int sig) { + if (current_cpu) { + assert(kvm_immediate_exit); + kvm_cpu_kick(current_cpu); + } } =20 void kvm_init_cpu_signals(CPUState *cpu) @@ -2437,7 +2461,7 @@ void kvm_init_cpu_signals(CPUState *cpu) struct sigaction sigact; =20 memset(&sigact, 0, sizeof(sigact)); - sigact.sa_handler =3D dummy_signal; + sigact.sa_handler =3D kvm_ipi_signal; sigaction(SIG_IPI, &sigact, NULL); =20 pthread_sigmask(SIG_BLOCK, NULL, &set); @@ -2446,7 +2470,11 @@ void kvm_init_cpu_signals(CPUState *cpu) pthread_sigmask(SIG_SETMASK, &set, NULL); #endif sigdelset(&set, SIG_IPI); - r =3D kvm_set_signal_mask(cpu, &set); + if (kvm_immediate_exit) { + r =3D pthread_sigmask(SIG_SETMASK, &set, NULL); + } else { + r =3D kvm_set_signal_mask(cpu, &set); + } if (r) { fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); exit(1); diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index bb0ed71..b7d53ee 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -218,7 +218,8 @@ struct kvm_hyperv_exit { struct kvm_run { /* in */ __u8 request_interrupt_window; - __u8 padding1[7]; + __u8 immediate_exit; + __u8 padding1[6]; =20 /* out */ __u32 exit_reason; @@ -870,6 +871,7 @@ struct kvm_ppc_smmu_info { #define KVM_CAP_S390_USER_INSTR0 130 #define KVM_CAP_MSI_DEVID 131 #define KVM_CAP_PPC_HTM 132 +#define KVM_CAP_IMMEDIATE_EXIT 136 =20 #ifdef KVM_CAP_IRQ_ROUTING =20 --=20 1.8.3.1