From patchwork Thu Nov 1 14:55:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10664047 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 4885014BD for ; Thu, 1 Nov 2018 15:01:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 350512BF8A for ; Thu, 1 Nov 2018 15:01:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 27C802BFCA; Thu, 1 Nov 2018 15:01:54 +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.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A41E82BFD7 for ; Thu, 1 Nov 2018 15:01:53 +0000 (UTC) Received: from localhost ([::1]:42450 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIETo-0004XK-UH for patchwork-qemu-devel@patchwork.kernel.org; Thu, 01 Nov 2018 11:01:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49677) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIESH-0003Gv-SS for qemu-devel@nongnu.org; Thu, 01 Nov 2018 11:00:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gIENl-0002eC-EY for qemu-devel@nongnu.org; Thu, 01 Nov 2018 10:55:43 -0400 Received: from mail-lj1-x242.google.com ([2a00:1450:4864:20::242]:34931) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gIENh-0002YV-Gi for qemu-devel@nongnu.org; Thu, 01 Nov 2018 10:55:37 -0400 Received: by mail-lj1-x242.google.com with SMTP id x85-v6so14112682ljb.2 for ; Thu, 01 Nov 2018 07:55:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=KR+kXHbeEWNFntPULWvgRlkRd0oK4mtxSIFjgeUb8Es=; b=nkuUIWFrrNHnSnF24fntFga0HhkJArg9/51tOVzqRbZ7/MIP0Qymm8T3oBsRQF2G03 cLope4Yll5VPLvhcfEF0vzu2jqRmdkiiqxG4ibqNlVWIX56R6OHHQIbN1EZvh5bBeSyD xvnJQyAliKxteXyQGQTGbeCSlle1z41CQYyMP4Eo9E/2O6Ac9b0wbfsYiwd8YWdlfKav YHCQJODWP8g09y2i7d4XkUspTpOV7XibY5mngvJ+ClnOYM3Zm3ErxAceuIoVsDa4T+Nq UmVJo9gN9bgscs9Kb/uN1NP9bWMVEHv7a6JGvTKcORrDvJDetxLNYVdl6HX0gE70ZiIa 2juA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=KR+kXHbeEWNFntPULWvgRlkRd0oK4mtxSIFjgeUb8Es=; b=DaZR8l+dZA2rTwgpbezQhQCpyQJjWyufZeYMCPG/jlsdFGJBk/58FA1qXuGpkql66M js4PXpXasFZq0Lhal3Jg9amrpcNf7dDu74iNBZrImMhElO0m7XrC59ewrJPZMAr9rUQY mm6GvXA06xWIXyekpDzdU6iF94ojA+J5vJDS3CJzJ9CiYElCdqKw/tZs3uWd0CH925iX Db0R20fWpteGRS+UsHsLql2U9d/M503srl9/Zf1RttCWzjc2gM1IuJ9+OQl+v4Xas7YM aYnoOP5gp3iIOXY5SbLxuHg/eyPvLU5MFT4V5N4tgVxKHY5xLU5i/Ed6lNqgHPPQqsSf czog== X-Gm-Message-State: AGRZ1gLVxEOQP8r1zyDPAQ+DXEiBh8F4BCQa+P21UpaiGR3/SiVm+KFy Hg8kxYH1WcB9MWeeY7DN+s1BnI/h X-Google-Smtp-Source: AJdET5eKBlG32iuZP4HKYfJLNC7ZFLTpCIXkbPa1lDutYJ0CxbLhrQT4WCb6ocTEShRMkPQh2Vpt/w== X-Received: by 2002:a2e:8e89:: with SMTP id z9-v6mr761500ljk.126.1541084125052; Thu, 01 Nov 2018 07:55:25 -0700 (PDT) Received: from PisarenkoPC.Dlink ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id m4-v6sm271271ljb.58.2018.11.01.07.55.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Nov 2018 07:55:24 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 1 Nov 2018 20:55:01 +0600 Message-Id: <0084f7223c080cdbdfc2c5a2d132f8d6c0eff866.1541083966.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::242 Subject: [Qemu-devel] [PATCH] chardev: fix mess in OPENED/CLOSED events when muxed 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: Paolo Bonzini , Artem Pisarenko , =?utf-8?q?Marc-Andr=C3=A9_Lu?= =?utf-8?q?reau?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When chardev is multiplexed (mux=on) there are a lot of cases, when CHR_EVENT_OPENED/CHR_EVENT_CLOSED events pairing (expected from frontend side) is broken. There are either generation of multiple repeated or extra CHR_EVENT_OPENED events, or CHR_EVENT_CLOSED just isn't generated at all (when it does with mux=off). Fix that. Signed-off-by: Artem Pisarenko --- Notes: This issue actually more complex. Idea of generating events from inside function called '*_set_handlers' isn't good, at least its implicit nature, and especially a fact, that function decides about open state (see 'fe_open' variable), but generates event only in one direction. Combined with 'mux_chr_set_handlers()' hack this makes things even worse. Better solution is to change fe interface and rewrite all frontends code (a lot of stuff in hw/char/* and somewhere else). Although this patch doesn't fix any issue/bug (known to me), it prevents them in future. Also it optimizes emulation performance by avoiding extra activity. I did several trivial tests on x86_64 target and seems like nothing broken. chardev/char-fe.c | 9 ++++++--- chardev/char-mux.c | 13 ++++++++----- include/chardev/char-mux.h | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/chardev/char-fe.c b/chardev/char-fe.c index a8931f7..31cf7f0 100644 --- a/chardev/char-fe.c +++ b/chardev/char-fe.c @@ -257,6 +257,7 @@ void qemu_chr_fe_set_handlers(CharBackend *b, { Chardev *s; int fe_open; + static __thread bool mux_reentered; s = b->chr; if (!s) { @@ -284,14 +285,16 @@ void qemu_chr_fe_set_handlers(CharBackend *b, if (fe_open) { qemu_chr_fe_take_focus(b); /* We're connecting to an already opened device, so let's make sure we - also get the open event */ - if (s->be_open) { + also get the open event (hack: except when chardev is muxed) */ + if (s->be_open && !mux_reentered) { qemu_chr_be_event(s, CHR_EVENT_OPENED); } } if (CHARDEV_IS_MUX(s)) { - mux_chr_set_handlers(s, context); + mux_reentered = true; + mux_chr_set_handlers(s, fe_open, context); + mux_reentered = false; } } diff --git a/chardev/char-mux.c b/chardev/char-mux.c index 6055e76..9244802 100644 --- a/chardev/char-mux.c +++ b/chardev/char-mux.c @@ -272,21 +272,24 @@ static void char_mux_finalize(Object *obj) for (i = 0; i < d->mux_cnt; i++) { CharBackend *be = d->backends[i]; if (be) { + if (be->chr && be->chr->be_open) { + qemu_chr_be_event(be->chr, CHR_EVENT_CLOSED); + } be->chr = NULL; } } qemu_chr_fe_deinit(&d->chr, false); } -void mux_chr_set_handlers(Chardev *chr, GMainContext *context) +void mux_chr_set_handlers(Chardev *chr, bool is_open, GMainContext *context) { MuxChardev *d = MUX_CHARDEV(chr); /* Fix up the real driver with mux routines */ qemu_chr_fe_set_handlers(&d->chr, - mux_chr_can_read, - mux_chr_read, - mux_chr_event, + is_open ? mux_chr_can_read : NULL, + is_open ? mux_chr_read : NULL, + is_open ? mux_chr_event : NULL, NULL, chr, context, true); @@ -367,7 +370,7 @@ static int open_muxes(Chardev *chr) * mark mux as OPENED so any new FEs will immediately receive * OPENED event */ - qemu_chr_be_event(chr, CHR_EVENT_OPENED); + chr->be_open = 1; return 0; } diff --git a/include/chardev/char-mux.h b/include/chardev/char-mux.h index 1e13187..4b4df6e 100644 --- a/include/chardev/char-mux.h +++ b/include/chardev/char-mux.h @@ -55,7 +55,7 @@ typedef struct MuxChardev { #define CHARDEV_IS_MUX(chr) \ object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX) -void mux_chr_set_handlers(Chardev *chr, GMainContext *context); +void mux_chr_set_handlers(Chardev *chr, bool is_open, GMainContext *context); void mux_set_focus(Chardev *chr, int focus); void mux_chr_send_all_event(Chardev *chr, int event);