From patchwork Fri May 19 12:47:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Nefedov X-Patchwork-Id: 9737141 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1D4386034C for ; Fri, 19 May 2017 12:52:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 10C8128929 for ; Fri, 19 May 2017 12:52:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0525728930; Fri, 19 May 2017 12:52:11 +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=-6.8 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 0EBDF2892F for ; Fri, 19 May 2017 12:52:10 +0000 (UTC) Received: from localhost ([::1]:58637 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBhO5-0002sG-8h for patchwork-qemu-devel@patchwork.kernel.org; Fri, 19 May 2017 08:52:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46858) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBhK0-0000Zv-8X for qemu-devel@nongnu.org; Fri, 19 May 2017 08:47:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dBhJy-0004J5-Cc for qemu-devel@nongnu.org; Fri, 19 May 2017 08:47:56 -0400 Received: from mail-eopbgr00136.outbound.protection.outlook.com ([40.107.0.136]:31167 helo=EUR02-AM5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dBhJy-0004AG-4t for qemu-devel@nongnu.org; Fri, 19 May 2017 08:47:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=6Fe/ZYL0Whx+fDtDLEoVZqcUQx6yWdBosJT+0xxUHBA=; b=RzAkB4sJR7SlCasAmGbZxQORTY1fBWnXsKNIERN6Ok90AcnZC9Rhns362mxd46HZVi+jXs+61Fp6nYLnyyDUnv9oBAD3XQ3kO6Sz1s+0ulB1t2XOgGrUGFnG5Wxb3YZGfXeX0zy39y8YOu8LZEGndjGf/Il8+N/sa0VAr0KpoH8= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none; nongnu.org; dmarc=none action=none header.from=virtuozzo.com; Received: from xantnef-ws.sw.ru (195.214.232.6) by DB6PR0801MB1989.eurprd08.prod.outlook.com (10.168.85.150) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1101.14; Fri, 19 May 2017 12:47:48 +0000 From: Anton Nefedov To: Date: Fri, 19 May 2017 15:47:16 +0300 Message-ID: <1495198042-124203-4-git-send-email-anton.nefedov@virtuozzo.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1495198042-124203-1-git-send-email-anton.nefedov@virtuozzo.com> References: <1495198042-124203-1-git-send-email-anton.nefedov@virtuozzo.com> MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: DB6PR1001CA0002.EURPRD10.PROD.OUTLOOK.COM (10.171.79.12) To DB6PR0801MB1989.eurprd08.prod.outlook.com (10.168.85.150) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB6PR0801MB1989: X-MS-Office365-Filtering-Correlation-Id: 0c5dbdda-5782-4660-4f6b-08d49eb54143 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(201703131423075)(201703031133081); SRVR:DB6PR0801MB1989; X-Microsoft-Exchange-Diagnostics: 1; DB6PR0801MB1989; 3:VPpPKh7w/8L5JRslw5XdnRkLoDZ92ZXoX/58izQlJ9USC8HzVaR65GJ3N9j02y4itxFS8DKjZgUHbfBnvFKod8tzXHiPu4sAeHFkzOlecaoWvqvEFIHf36WHw7HM1zyR09LyC/KXZXfPB7likT5gn9nVk4Uh4xCqRaXJwJzZ/IaGhyI01oYrRRrXQm5uK2hQZTEYIDL3j3tKJlBHDtpxOa7u+CPSaX95O8LeM0KettZcUxcd0wAfkpgxhsCkx0zadtJRmOhfwfBMIu6GRoV9ED1ouW3/d+g29v+Tr5VdqLINp1sdwi6iXKuIs0GTPp3qUyOI91cwjnLL9gV0OujFIw==; 25:+E6HT0HJOpnx2Xe4OpHKG+xRnc9uf7pFaijQ8VPxenL1vDLAcuvHReWobYmD1tmOkswPkPgHMaqiUILzJNl+Hdd0q0PPk1vTgpBiqWZZ3KEpyfUxFiM2tcEcNpBV+heE9UCNC4TV7xfE8UUbwXQQcypbd+BSjrOgIrRBH+JpsTi+TACQMvEO10ViFVL1DvvNrkC0ApJNC+EWG5tGdIhO7/1ExQuJIT1ReFH8BjejT7vT9ltSY1xacDXc/d5+nw4Ngo+BvPMlQh3h56GKHaZgrLcn3SXYt+TqW7027A6SXn6GzRjfRVA8SDGWI05bUNM6T+vl17EPYirhRsL0sR0N/cqTx8IyA2IODeUI/WBnERVxmkKHGzRPU6utFM9O46lDCXRyJ0xGeNB2GKenpQQE5vaTIbklex04MQgW7TWZytt10JzJNM6ySALOssCqlBaJH0cR0fYGcePx8NPcx0qymMpi/DMBeQwsh0zWFqXGtjk= X-Microsoft-Exchange-Diagnostics: 1; DB6PR0801MB1989; 31:NZolQEz2jWQC0fwIKBgz4QH44LK6oj2ZMLvOp+UqKYuighCqDYC0p9wBwzRMaEZ1hf1aCZbRB0DvJDme51gwWcNTqQABmz6xEj3GungLVmHAccNkJMlWfFYWhXLZ01Aw1mtxFCf+iqgDbDD6lTYKlcNXOiFOWVqB8TFNrR4GnANacfod5ADrIWKRRWklzzeLk/rkE6RvkguKmLoICgDFFJG7ganyt+00FWSjJRNCl+8=; 20:Byw83l7FmTdMx62gpg3gYjzK4Scg8Kpl5OzVdOskRIxiLuDvmNfvyxS/PdzEqw/DxDAQE2uxjZfwBeIB3INHZAhUhh6TsaoHGApaLHQz/PnX6qxNrTgeSPb+wFLswnmtiacH4+H1oHrwpMYxCk1eoCJcvctW1fCwZMizhc0QCfAqJ1rvejKkktFa48t1pVeP7/Qq4vDCwM92iME1Z3Bju8ICFfU4TTg5ShwXiEhw0nmEEprjX5hUKAo7L0Km31nAf6YvBi42XgcymFqAJRV3EbJA6tzZUmq0gIO+YToTPOLu0t0g0B+cFMTXhfEst9+WhiyAtWnRWiU19vhm5jfT0/J+NUnjLoshIMH4s6PBEFM8PzB7iOxnPk7lJrD+R4+Tce/x1ShmiUxS5d/vLhUaiyIXuhGVqkk8F4BWogAF7fY= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(158342451672863); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(93006095)(93001095)(3002001)(6041248)(20161123560025)(20161123555025)(20161123558100)(20161123562025)(20161123564025)(201703131423075)(201703061421075)(6072148); SRVR:DB6PR0801MB1989; BCL:0; PCL:0; RULEID:; SRVR:DB6PR0801MB1989; X-Microsoft-Exchange-Diagnostics: 1; DB6PR0801MB1989; 4:omL4vgfuYoD3LX0O6W1GcrJusOLMmhE/4nKY0o0lX9OkZLSy0D3VurDF+r8N2gw2oBIlUkH5dBHqOaVm+svI/dEWaHlEA+cOT3kKOAwEE+kntHmZP5ZOVuQSjkHfzfeMvw+LzPCWhR5He9xA4POSa8W6rswisYj2LtEHDhISgIS4zc6bYvJRBpxUWvcDPjnAPfva2htcwq5Q9EPZySZQEsbQQY0U425WVlWUa7D6zhE5bbH1x7TdWVY4w/UvqMuXqHHwAKRHHcizqHnm+BOdqji6yCcnKO83MQrZRAA3tfgiZ8OqEbbeA8A5n60l9rdXvMhpQ4fX3pCk5hRiknQAXCpqkOhepueJAeicfqto3gkObjB6Ji8JMaHGao65Jb/fdA7llGXhnTIBGeolMVcsx8a7PakNfkGpCcFdTotip2yVjiIGl/f9PHO4qZIf7zfEpaHosXlMZSnj+SfNX8o96KWayQ5A3527aOJhDF+U5D6FnIyi8dztUtE1kJ+OfnD7LXaFFh1wnUveJGvUgVhPaQoWnegAX29tPjz8/J+kuC+ZSlUvR/IDktdP1Hvx057+ndN+bJf1uwPx4T6wW3tPd/Wi51jMJhHC+soDUO8i7z7cPtc6cxFmetTwf8PPwHXPWAGYuTtuW0eoF3bJ5xBSTo6ErlL5vn8LNX8aQvV/dZ7N8Bm9Z8PpLiSmTFErsYNOEXZWO8kcQtAtyaudncLDTFt6jZ7lu3lhN1pNYkLGLYcAe4qmdd8ddR7aabB7/LlgKKl3gSD9vUTunNMb9vFSkA== X-Forefront-PRVS: 031257FE13 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6009001)(39400400002)(39450400003)(33646002)(48376002)(5003940100001)(53936002)(38730400002)(25786009)(2351001)(6506006)(6486002)(107886003)(4326008)(110136004)(5660300001)(36756003)(7736002)(6916009)(2950100002)(305945005)(6666003)(66066001)(2906002)(189998001)(50226002)(50466002)(54906002)(508600001)(86362001)(6512007)(81166006)(8676002)(50986999)(53416004)(3846002)(76176999)(42186005); DIR:OUT; SFP:1102; SCL:1; SRVR:DB6PR0801MB1989; H:xantnef-ws.sw.ru; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; DB6PR0801MB1989; 23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; DB6PR0801MB1989; 7:SFemqQFmuXK/yg0huY+pGioj1gJFEwTfAzMHPOcxDPI2vmsQaj7CfqDmj23XeKsEjVO5mG77/rsNxMXTcmUIr/ErvMDUgvmlt3B1iaSOB2q8aIC4ktbOz7tNHMaGIdBj60PaJnJF2j10F9ma+MtjLAgU4VfKCB/fVg9b3AqzYnkGrYJ6SSeYFcZZ1oqxj4jLBKE5ZT3WtCSYcBTXkTrcTtCleCHr8ytGPPP8WTNuIz9mz9QC7+2PMEW7weqilTcFGZR8ZCfDEPH1ccd/kwJkvOc9ibUyFdQK74xMOm6PYcIfefqc/Bt+RHhuh4FevMFv5K8O8zhXMPv5ddPb0L/tzw==; 20:npcmjTRmfmUORfj3/HvBn08R6B2wFjESS9aAhjmRVvjKizbbcoi+FKm2XFnmXHpZkTriyz5cCdHDqIv+cIGeeLvE4AEOuLCca2OPm7OoZLLM7Jrqr8tnX6btlESj2vaqZ+r8NyfePrH8+FK/Av/tlPJo9EdMMoeAhpnhgsDat2M= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2017 12:47:48.2808 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6PR0801MB1989 X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-Received-From: 40.107.0.136 Subject: [Qemu-devel] [PATCH v2 3/9] char: chardevice hotswap 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: pbonzini@redhat.com, Anton Nefedov , den@virtuozzo.com, marcandre.lureau@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch adds a possibility to change a char device without a frontend removal. 1. Ideally, it would have to happen transparently to a frontend, i.e. frontend would continue its regular operation. However, backends are not stateless and are set up by the frontends via qemu_chr_fe_<> functions, and it's not (generally) possible to replay that setup entirely in a backend code, as different chardevs respond to the setup calls differently, so do frontends work differently basing on those setup responses. Moreover, some frontend can generally get and save the backend pointer (qemu_chr_fe_get_driver()), and it will become invalid after backend change. So, a frontend which would like to support chardev hotswap has to register a "backend change" handler, and redo its backend setup there. 2. Write path can be used by multiple threads and thus protected with chr_write_lock. So hotswap also has to be protected so write functions won't access a backend being replaced. 3. Hotswap function can be called from e.g. a read handler of a monitor socket. This can cause troubles so it's safer to defer execution to a bottom-half (however, it means we cannot return some of the errors synchronously - but most of them we can) Signed-off-by: Anton Nefedov Reviewed-by: Vladimir Sementsov-Ogievskiy --- chardev/char.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++---- include/sysemu/char.h | 10 ++++ qapi-schema.json | 40 ++++++++++++++ 3 files changed, 187 insertions(+), 10 deletions(-) diff --git a/chardev/char.c b/chardev/char.c index ae60950..bac5e1c 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -132,12 +132,16 @@ static bool qemu_chr_replay(Chardev *chr) int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) { - Chardev *s = be->chr; + Chardev *s; ChardevClass *cc; int ret; + qemu_mutex_lock(&be->chr_lock); + s = be->chr; + if (!s) { - return 0; + ret = 0; + goto end; } if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) { @@ -145,7 +149,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) replay_char_write_event_load(&ret, &offset); assert(offset <= len); qemu_chr_fe_write_buffer(s, buf, offset, &offset); - return ret; + goto end; } cc = CHARDEV_GET_CLASS(s); @@ -161,7 +165,9 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len) if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) { replay_char_write_event_save(ret, ret < 0 ? 0 : ret); } - + +end: + qemu_mutex_unlock(&be->chr_lock); return ret; } @@ -191,13 +197,16 @@ int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len) int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len) { - Chardev *s = be->chr; + Chardev *s; + int ret; - if (!s) { - return 0; - } + qemu_mutex_lock(&be->chr_lock); - return qemu_chr_write_all(s, buf, len); + s = be->chr; + ret = s ? qemu_chr_write_all(s, buf, len) : 0; + + qemu_mutex_unlock(&be->chr_lock); + return ret; } int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) @@ -478,7 +487,7 @@ Chardev *qemu_chr_fe_get_driver(CharBackend *be) return be->chr; } -bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) +static bool fe_connect(CharBackend *b, Chardev *s, Error **errp) { int tag = 0; @@ -507,6 +516,17 @@ unavailable: return false; } +bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp) +{ + if (!fe_connect(b, s, errp)) { + return false; + } + + qemu_mutex_init(&b->chr_lock); + b->hotswap_bh = NULL; + return true; +} + static bool qemu_chr_is_busy(Chardev *s) { if (CHARDEV_IS_MUX(s)) { @@ -531,6 +551,10 @@ void qemu_chr_fe_deinit(CharBackend *b) d->backends[b->tag] = NULL; } b->chr = NULL; + qemu_mutex_destroy(&b->chr_lock); + if (b->hotswap_bh) { + qemu_bh_delete(b->hotswap_bh); + } } } @@ -1308,6 +1332,109 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, return ret; } +static void chardev_change_bh(void *opaque) +{ + Chardev *chr_new = opaque; + const char *id = chr_new->label; + Chardev *chr = qemu_chr_find(id); + CharBackend *be = chr->be; + bool closed_sent = false; + + if (!be) { + /* disconnected since we checked: ok, less work for us */ + goto end; + } + + if (chr->be_open && !chr_new->be_open) { + qemu_chr_be_event(chr, CHR_EVENT_CLOSED); + closed_sent = true; + } + + qemu_mutex_lock(&be->chr_lock); + chr->be = NULL; + fe_connect(be, chr_new, &error_abort); + + if (be->chr_be_change(be->opaque) < 0) { + error_report("Chardev '%s' change failed", id); + fe_connect(be, chr, &error_abort); + qemu_mutex_unlock(&be->chr_lock); + if (closed_sent) { + qemu_chr_be_event(chr, CHR_EVENT_OPENED); + } + object_unref(OBJECT(chr_new)); + return; + } + qemu_mutex_unlock(&be->chr_lock); + +end: + object_unparent(OBJECT(chr)); + object_property_add_child(get_chardevs_root(), id, OBJECT(chr_new), + &error_abort); + object_unref(OBJECT(chr_new)); +} + +ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, + Error **errp) +{ + const ChardevClass *cc; + Chardev *chr, *chr_new; + ChardevReturn *ret; + + chr = qemu_chr_find(id); + if (!chr) { + error_setg(errp, "Chardev '%s' does not exist", id); + return NULL; + } + + if (CHARDEV_IS_MUX(chr)) { + error_setg(errp, "Mux device hotswap not supported yet"); + return NULL; + } + + if (qemu_chr_replay(chr)) { + error_setg(errp, + "Chardev '%s' cannot be changed in record/replay mode", id); + return NULL; + } + + if (!chr->be) { + /* easy case */ + object_unparent(OBJECT(chr)); + return qmp_chardev_add(id, backend, errp); + } + + if (!chr->be->chr_be_change) { + error_setg(errp, "Chardev user does not support chardev hotswap"); + return NULL; + } + + cc = char_get_class(ChardevBackendKind_lookup[backend->type], errp); + if (!cc) { + return NULL; + } + + chr_new = qemu_chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)), + backend, errp); + chr_new->label = g_strdup(id); + if (!chr_new) { + return NULL; + } + + if (chr->be->hotswap_bh) { + qemu_bh_delete(chr->be->hotswap_bh); + } + chr->be->hotswap_bh = qemu_bh_new(chardev_change_bh, chr_new); + qemu_bh_schedule(chr->be->hotswap_bh); + + ret = g_new0(ChardevReturn, 1); + if (CHARDEV_IS_PTY(chr_new)) { + ret->pty = g_strdup(chr_new->filename + 4); + ret->has_pty = true; + } + + return ret; +} + void qmp_chardev_remove(const char *id, Error **errp) { Chardev *chr; diff --git a/include/sysemu/char.h b/include/sysemu/char.h index 9f8df07..68c7876 100644 --- a/include/sysemu/char.h +++ b/include/sysemu/char.h @@ -92,6 +92,8 @@ typedef struct CharBackend { void *opaque; int tag; int fe_open; + QemuMutex chr_lock; + QEMUBH *hotswap_bh; } CharBackend; struct Chardev { @@ -141,6 +143,14 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend); */ Chardev *qemu_chr_new(const char *label, const char *filename); +/** + * @qemu_chr_change: + * + * Change an existing character backend + * + * @opts the new backend options + */ +void qemu_chr_change(QemuOpts *opts, Error **errp); /** * @qemu_chr_fe_disconnect: diff --git a/qapi-schema.json b/qapi-schema.json index 80603cf..bf72166 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -5075,6 +5075,46 @@ 'returns': 'ChardevReturn' } ## +# @chardev-change: +# +# Change a character device backend +# +# @id: the chardev's ID, must exist +# @backend: new backend type and parameters +# +# Returns: ChardevReturn. +# +# Since: 2.10 +# +# Example: +# +# -> { "execute" : "chardev-change", +# "arguments" : { "id" : "baz", +# "backend" : { "type" : "pty", "data" : {} } } } +# <- { "return": { "pty" : "/dev/pty/42" } } +# +# -> {"execute" : "chardev-change", +# "arguments" : { +# "id" : "charchannel2", +# "backend" : { +# "type" : "socket", +# "data" : { +# "addr" : { +# "type" : "unix" , +# "data" : { +# "path" : "/tmp/charchannel2.socket" +# } +# }, +# "server" : true, +# "wait" : false }}}} +# <- {"return": {}} +# +## +{ 'command': 'chardev-change', 'data': {'id' : 'str', + 'backend' : 'ChardevBackend' }, + 'returns': 'ChardevReturn' } + +## # @chardev-remove: # # Remove a character device backend