From patchwork Tue Oct 19 10:40:30 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 68315 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 01C92B70E4 for ; Tue, 19 Oct 2010 21:52:33 +1100 (EST) Received: from localhost ([127.0.0.1]:41202 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P89nx-0007bU-Te for incoming@patchwork.ozlabs.org; Tue, 19 Oct 2010 06:52:30 -0400 Received: from [140.186.70.92] (port=46277 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P89dE-0001uE-3a for qemu-devel@nongnu.org; Tue, 19 Oct 2010 06:41:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P89dB-00066r-CG for qemu-devel@nongnu.org; Tue, 19 Oct 2010 06:41:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8220) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P89dB-00066c-60 for qemu-devel@nongnu.org; Tue, 19 Oct 2010 06:41:21 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o9JAfKav032228 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 19 Oct 2010 06:41:20 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o9JAfKn7011919; Tue, 19 Oct 2010 06:41:20 -0400 Received: from amt.cnet (vpn-9-223.rdu.redhat.com [10.11.9.223]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o9JAfIdP001468; Tue, 19 Oct 2010 06:41:19 -0400 Received: from amt.cnet (localhost.localdomain [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 53A51652071; Tue, 19 Oct 2010 08:40:53 -0200 (BRST) Received: (from marcelo@localhost) by amt.cnet (8.14.4/8.14.4/Submit) id o9JAepPR027477; Tue, 19 Oct 2010 08:40:51 -0200 From: Marcelo Tosatti To: Anthony Liguori Date: Tue, 19 Oct 2010 08:40:30 -0200 Message-Id: <2da93e62a3d2ec54e157deedb182b5c1c5fedc45.1287484836.git.mtosatti@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Marcelo Tosatti , qemu-devel@nongnu.org, kvm@vger.kernel.org, Avi Kivity Subject: [Qemu-devel] [PATCH 04/10] iothread: use signalfd X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Block SIGALRM, SIGIO and consume them via signalfd. Signed-off-by: Marcelo Tosatti Signed-off-by: Avi Kivity --- cpus.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 69 insertions(+), 5 deletions(-) diff --git a/cpus.c b/cpus.c index b09f5e3..3875657 100644 --- a/cpus.c +++ b/cpus.c @@ -33,6 +33,7 @@ #include "exec-all.h" #include "cpus.h" +#include "compatfd.h" #ifdef SIGRTMIN #define SIG_IPI (SIGRTMIN+4) @@ -329,14 +330,75 @@ static QemuCond qemu_work_cond; static void tcg_init_ipi(void); static void kvm_init_ipi(CPUState *env); -static void unblock_io_signals(void); +static sigset_t block_io_signals(void); + +/* 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. + */ +static void sigfd_handler(void *opaque) +{ + int fd = (unsigned long) opaque; + struct qemu_signalfd_siginfo info; + struct sigaction action; + ssize_t len; + + while (1) { + do { + len = read(fd, &info, sizeof(info)); + } while (len == -1 && errno == EINTR); + + if (len == -1 && errno == EAGAIN) { + break; + } + + if (len != sizeof(info)) { + printf("read from sigfd returned %zd: %m\n", len); + return; + } + + 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); + } else if (action.sa_handler) { + action.sa_handler(info.ssi_signo); + } + } +} + +static int qemu_signalfd_init(sigset_t mask) +{ + int sigfd; + + sigfd = qemu_signalfd(&mask); + if (sigfd == -1) { + fprintf(stderr, "failed to create signalfd\n"); + return -errno; + } + + fcntl_setfl(sigfd, O_NONBLOCK); + + qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, + (void *)(unsigned long) sigfd); + + return 0; +} int qemu_init_main_loop(void) { int ret; + sigset_t blocked_signals; cpu_set_debug_excp_handler(cpu_debug_handler); + blocked_signals = block_io_signals(); + + ret = qemu_signalfd_init(blocked_signals); + if (ret) + return ret; + + /* Note eventfd must be drained before signalfd handlers run */ ret = qemu_event_init(); if (ret) return ret; @@ -347,7 +409,6 @@ int qemu_init_main_loop(void) qemu_mutex_init(&qemu_global_mutex); qemu_mutex_lock(&qemu_global_mutex); - unblock_io_signals(); qemu_thread_self(&io_thread); return 0; @@ -586,19 +647,22 @@ static void kvm_init_ipi(CPUState *env) } } -static void unblock_io_signals(void) +static sigset_t block_io_signals(void) { sigset_t set; + /* SIGUSR2 used by posix-aio-compat.c */ sigemptyset(&set); sigaddset(&set, SIGUSR2); - sigaddset(&set, SIGIO); - sigaddset(&set, SIGALRM); pthread_sigmask(SIG_UNBLOCK, &set, NULL); sigemptyset(&set); + sigaddset(&set, SIGIO); + sigaddset(&set, SIGALRM); sigaddset(&set, SIG_IPI); pthread_sigmask(SIG_BLOCK, &set, NULL); + + return set; } void qemu_mutex_lock_iothread(void)