From patchwork Mon Feb 18 13:07:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10817979 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0C5BF17E9 for ; Mon, 18 Feb 2019 13:10:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EAA8E2AA5B for ; Mon, 18 Feb 2019 13:10:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DC0CD2AAD7; Mon, 18 Feb 2019 13:10:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D1B0F2AA6B for ; Mon, 18 Feb 2019 13:10:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730945AbfBRNKC (ORCPT ); Mon, 18 Feb 2019 08:10:02 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:57998 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730928AbfBRNKB (ORCPT ); Mon, 18 Feb 2019 08:10:01 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6829D16A3; Mon, 18 Feb 2019 05:10:01 -0800 (PST) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.197.2]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9F0CB3F73F; Mon, 18 Feb 2019 05:10:00 -0800 (PST) From: Jean-Philippe Brucker To: kvm@vger.kernel.org Cc: will.deacon@arm.com, andre.przywara@arm.com Subject: [PATCH kvmtool 7/9] disk/aio: Cancel AIO thread on cleanup Date: Mon, 18 Feb 2019 13:07:00 +0000 Message-Id: <20190218130702.32575-8-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190218130702.32575-1-jean-philippe.brucker@arm.com> References: <20190218130702.32575-1-jean-philippe.brucker@arm.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the AIO thread is still calling io_getevents() while the exit path calls io_destroy(), it will segfault. Wait for the thread to finish before destroying the context. Signed-off-by: Jean-Philippe Brucker Reviewed-by: Andre Przywara --- disk/aio.c | 5 +++-- include/kvm/disk-image.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/disk/aio.c b/disk/aio.c index 1fcf36857..277ddf7c9 100644 --- a/disk/aio.c +++ b/disk/aio.c @@ -99,7 +99,6 @@ static void *disk_aio_thread(void *param) int disk_aio_setup(struct disk_image *disk) { int r; - pthread_t thread; /* No need to setup AIO if the disk ops won't make use of it */ if (!disk->ops->async) @@ -110,7 +109,7 @@ int disk_aio_setup(struct disk_image *disk) return -errno; io_setup(AIO_MAX, &disk->ctx); - r = pthread_create(&thread, NULL, disk_aio_thread, disk); + r = pthread_create(&disk->thread, NULL, disk_aio_thread, disk); if (r) { r = -errno; close(disk->evt); @@ -126,6 +125,8 @@ void disk_aio_destroy(struct disk_image *disk) if (!disk->async) return; + pthread_cancel(disk->thread); + pthread_join(disk->thread, NULL); close(disk->evt); io_destroy(disk->ctx); } diff --git a/include/kvm/disk-image.h b/include/kvm/disk-image.h index adc9fe465..2275e2343 100644 --- a/include/kvm/disk-image.h +++ b/include/kvm/disk-image.h @@ -69,6 +69,7 @@ struct disk_image { #ifdef CONFIG_HAS_AIO io_context_t ctx; int evt; + pthread_t thread; #endif /* CONFIG_HAS_AIO */ const char *wwpn; const char *tpgt;