From nobody Sat Feb 7 15:29:50 2026 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.zohomail.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; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1531929219900125.61342038055716; Wed, 18 Jul 2018 08:53:39 -0700 (PDT) Received: from localhost ([::1]:37313 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffolc-0004WB-Qv for importer@patchew.org; Wed, 18 Jul 2018 11:53:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52958) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffoal-0003He-Db for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ffoag-0000IK-NB for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:15 -0400 Received: from relay.sw.ru ([185.231.240.75]:36380) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ffoag-0000F9-AA for qemu-devel@nongnu.org; Wed, 18 Jul 2018 11:42:10 -0400 Received: from vz-out.virtuozzo.com ([185.231.240.5] helo=dptest2.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1ffoad-0006bu-9h; Wed, 18 Jul 2018 18:42:07 +0300 From: Denis Plotnikov To: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com Date: Wed, 18 Jul 2018 18:41:46 +0300 Message-Id: <20180718154200.26777-4-dplotnikov@virtuozzo.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180718154200.26777-1-dplotnikov@virtuozzo.com> References: <20180718154200.26777-1-dplotnikov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v1 03/17] threads: add infrastructure to process sigsegv 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: qemu-devel@nongnu.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" Allows to define sigsegv handler temporary for all threads. This is useful to implement copy-on-write logic while linux usefaultfd doesn't support write-protected faults. In the future, switch to using WP userfaultfd when it's available. Signed-off-by: Denis Plotnikov --- include/qemu/thread.h | 5 ++++ util/qemu-thread-posix.c | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 9910f49b3a..d6fed833fa 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -210,4 +210,9 @@ void qemu_lockcnt_inc_and_unlock(QemuLockCnt *lockcnt); */ unsigned qemu_lockcnt_count(QemuLockCnt *lockcnt); =20 + +typedef void (*sigsegv_handler)(int signum, siginfo_t *siginfo, void *sigc= tx); +void sigsegv_user_handler_set(sigsegv_handler handler); +void sigsegv_user_handler_reset(void); + #endif diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 7306475899..5424b7106d 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -489,6 +489,47 @@ static void qemu_thread_set_name(QemuThread *thread, c= onst char *name) #endif } =20 +static sigsegv_handler sigsegv_user_handler; + +void sigsegv_user_handler_set(sigsegv_handler handler) +{ + assert(handler); + atomic_set(&sigsegv_user_handler, handler); +} + +static sigsegv_handler sigsegv_user_handler_get(void) +{ + return atomic_read(&sigsegv_user_handler); +} + +void sigsegv_user_handler_reset(void) +{ + atomic_set(&sigsegv_user_handler, NULL); +} + +static void sigsegv_default_handler(int signum, siginfo_t *siginfo, void *= sigctx) +{ + sigsegv_handler handler =3D sigsegv_user_handler_get(); + + if (!handler) { + /* + * remove the sigsegv handler if it's not set by user + * this will lead to re-raising the error without a handler + * and exiting from the program with "Segmentation fault" + */ + int err; + struct sigaction act; + memset(&act, 0, sizeof(act)); + act.sa_flags =3D SA_RESETHAND; + err =3D sigaction(SIGSEGV, &act, NULL); + if (err) { + error_exit(err, __func__); + } + } else { + handler(signum, siginfo, sigctx); + } +} + void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void*), void *arg, int mode) @@ -496,14 +537,25 @@ void qemu_thread_create(QemuThread *thread, const cha= r *name, sigset_t set, oldset; int err; pthread_attr_t attr; + struct sigaction act; =20 err =3D pthread_attr_init(&attr); if (err) { error_exit(err, __func__); } =20 + memset(&act, 0, sizeof(act)); + act.sa_flags =3D SA_SIGINFO; + act.sa_sigaction =3D sigsegv_default_handler; + err =3D sigaction(SIGSEGV, &act, NULL); + if (err) { + error_exit(err, __func__); + } + /* Leave signal handling to the iothread. */ sigfillset(&set); + /* ...all but SIGSEGV */ + sigdelset(&set, SIGSEGV); pthread_sigmask(SIG_SETMASK, &set, &oldset); err =3D pthread_create(&thread->thread, &attr, start_routine, arg); if (err) --=20 2.17.0