From patchwork Tue Dec 4 11:08:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Thierry X-Patchwork-Id: 10711513 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 18D711057 for ; Tue, 4 Dec 2018 11:09:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 078EB2AE36 for ; Tue, 4 Dec 2018 11:09:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EFCAC2AE77; Tue, 4 Dec 2018 11:09:13 +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 73C4A2AE5C for ; Tue, 4 Dec 2018 11:09:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728269AbeLDLJL (ORCPT ); Tue, 4 Dec 2018 06:09:11 -0500 Received: from foss.arm.com ([217.140.101.70]:57416 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728725AbeLDLJJ (ORCPT ); Tue, 4 Dec 2018 06:09:09 -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 AAEB5A78; Tue, 4 Dec 2018 03:09:08 -0800 (PST) Received: from e112298-lin.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 92D5B3F5AF; Tue, 4 Dec 2018 03:09:07 -0800 (PST) From: Julien Thierry To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu Cc: jean-philippe.brucker@arm.com, will.deacon@arm.com, Julien Thierry Subject: [PATCH kvmtool 13/13] virtio/console: Implement reset Date: Tue, 4 Dec 2018 11:08:46 +0000 Message-Id: <1543921726-54571-14-git-send-email-julien.thierry@arm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1543921726-54571-1-git-send-email-julien.thierry@arm.com> References: <1543921726-54571-1-git-send-email-julien.thierry@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jean-Philippe Brucker The virtio-console reset cancels all running jobs. Unfortunately we don't have a good way to prevent the term polling thread from getting in the way, read invalid data during reset and cause a segfault. To prevent this, move all handling of the Rx queue in the threadpool job. Signed-off-by: Jean-Philippe Brucker Signed-off-by: Julien Thierry --- virtio/console.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/virtio/console.c b/virtio/console.c index d2b312c..f1be025 100644 --- a/virtio/console.c +++ b/virtio/console.c @@ -35,8 +35,6 @@ struct con_dev { struct virt_queue vqs[VIRTIO_CONSOLE_NUM_QUEUES]; struct virtio_console_config config; u32 features; - - pthread_cond_t poll_cond; int vq_ready; struct thread_pool__job jobs[VIRTIO_CONSOLE_NUM_QUEUES]; @@ -44,7 +42,6 @@ struct con_dev { static struct con_dev cdev = { .mutex = MUTEX_INITIALIZER, - .poll_cond = PTHREAD_COND_INITIALIZER, .vq_ready = 0, @@ -68,16 +65,10 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par u16 head; int len; - if (kvm->cfg.active_console != CONSOLE_VIRTIO) - return; - mutex_lock(&cdev.mutex); vq = param; - if (!cdev.vq_ready) - pthread_cond_wait(&cdev.poll_cond, &cdev.mutex.mutex); - if (term_readable(0) && virt_queue__available(vq)) { head = virt_queue__get_iov(vq, iov, &out, &in, kvm); len = term_getc_iov(kvm, iov, in, 0); @@ -90,8 +81,13 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par void virtio_console__inject_interrupt(struct kvm *kvm) { - virtio_console__inject_interrupt_callback(kvm, - &cdev.vqs[VIRTIO_CONSOLE_RX_QUEUE]); + if (kvm->cfg.active_console != CONSOLE_VIRTIO) + return; + + mutex_lock(&cdev.mutex); + if (cdev.vq_ready) + thread_pool__do_job(&cdev.jobs[VIRTIO_CONSOLE_RX_QUEUE]); + mutex_unlock(&cdev.mutex); } static void virtio_console_handle_callback(struct kvm *kvm, void *param) @@ -168,13 +164,24 @@ static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 page_size, u32 align, /* Tell the waiting poll thread that we're ready to go */ mutex_lock(&cdev.mutex); cdev.vq_ready = 1; - pthread_cond_signal(&cdev.poll_cond); mutex_unlock(&cdev.mutex); } return 0; } +static void exit_vq(struct kvm *kvm, void *dev, u32 vq) +{ + if (vq == VIRTIO_CONSOLE_RX_QUEUE) { + mutex_lock(&cdev.mutex); + cdev.vq_ready = 0; + mutex_unlock(&cdev.mutex); + thread_pool__cancel_job(&cdev.jobs[vq]); + } else if (vq == VIRTIO_CONSOLE_TX_QUEUE) { + thread_pool__cancel_job(&cdev.jobs[vq]); + } +} + static int notify_vq(struct kvm *kvm, void *dev, u32 vq) { struct con_dev *cdev = dev; @@ -213,6 +220,7 @@ static struct virtio_ops con_dev_virtio_ops = { .set_guest_features = set_guest_features, .get_vq_count = get_vq_count, .init_vq = init_vq, + .exit_vq = exit_vq, .notify_status = notify_status, .notify_vq = notify_vq, .get_vq = get_vq,