From patchwork Thu Mar 6 06:16:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14003866 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 35DD3C282DE for ; Thu, 6 Mar 2025 06:17:55 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tq4XJ-00029L-16; Thu, 06 Mar 2025 01:16:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tq4XH-000292-IA for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:47 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tq4XF-0003VD-8R for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:47 -0500 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22382657540so3880355ad.2 for ; Wed, 05 Mar 2025 22:16:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741241803; x=1741846603; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=PNVlTce1uhHhS6pAL94t07YGCV3qzSeEltruk5DehEo=; b=YStnCZx//8ZtmSQwHr4H0ypAy01Pk4rdEuQaGb4gZzi15UgIv8aPCllTdvuQPOJ2or ZPwkj+GyMgRSlNTaK68QPKQm1rbSU/j9FULyE41nSrKXsV17H3/nWHYmdLKStyW86wAA F1GOhIm123kf+6Uf9KZ/+aBPloulc0eJtdUn0Ot89PIkvCJqkjRR+7k2Py6xsS5SxxPS zTW1F2G7nykllQMfOPzc1HLw/TkbvtBjWAWwzFV+j6a7j00md/MaTcafLOwHR/d6J8um VplAn5n9dugcpeU0JaGha8Xx/LG+6xe/ThcwMuUL+mdyPYJxRsPkZaVCFzIZHLSQjc2J xSrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741241803; x=1741846603; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PNVlTce1uhHhS6pAL94t07YGCV3qzSeEltruk5DehEo=; b=PshAZIX/EJlnQfxir1bXN4zm/qKhIaKIScnmfEeAG3Uh5xr1+D3OcTHNtEqMAk5NpU 7HFiRV9n4tRor1CpOS+MEXqBBlRjfIG4Pcwy4VmEJ2zsYFcgKd8d466Ox/xzwhxFF4Lc KQ4sZwyFrTvUKAgOZ6vIS/9FqssDiJs8vaP4AFoO7xUv5FzPz/7d7V2UuDZr0n0jrGvS TK7YLam1G8I+toHTlX9hmFDY8+5veMMAdeJR0ZaqsaZNH/io+Y9sXGOGTElawbPNoH9n eHIcB9atF8feuLfqbrh1kosNs5KYwtV5+jzmiyz4OuZ7onOhzpZwkMlYjEByFfc2NG2R UWSw== X-Gm-Message-State: AOJu0Yxd8dLypNMX5lvRT9CfjSQNsu+NC292eLRxHxXVVeU+M9VkCpds USVm9DqgtlkzAixnafocXjtFEkNunKsGv1jJDMb5xbI3ZZkbvTJGaP3Zzr5fNZw= X-Gm-Gg: ASbGncvHJR8Og6/O5a5Ry/l7YEdFvA2Kta1KEjXN8LWVQPmbndhqP1yB72wrTFxvPIx seVpZKFUJIpYugnpCGM3ku+Vbw5bOFLZSHQKIXtPLZJ5FlRLYyDhxyvDTfOFAvQs9GEFSz0DYIh B+KwzV3Ur5JV3tzn6K+NWh6ELpFPH6J1RU/vyomOepMmgigulSsfoLt7E4gQIk0Tr0QhlRbtfyh cu3wwWZJfRXHFxdlIeF7HLxLzmq6Da5eN0IKrcI6SoDKZ3CNQdYMCofnDLUUkDNw9p8a32cb2Bl FQnG70AeH/Cb/5RXrVP6ET7g/Rr5g9Sts3wVOUKimlN6L+IA X-Google-Smtp-Source: AGHT+IFjEjwNBXZq3sUdUFNGRgDCchxDULTae+EBzGiSea57VQUknE5+DPPRSgrKtIzRs6MxjfrbYw== X-Received: by 2002:a17:902:c94b:b0:21a:8300:b9ce with SMTP id d9443c01a7336-223f1d40770mr97259135ad.49.1741241803595; Wed, 05 Mar 2025 22:16:43 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-22410a918e5sm4244205ad.167.2025.03.05.22.16.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Mar 2025 22:16:43 -0800 (PST) From: Akihiko Odaki Date: Thu, 06 Mar 2025 15:16:27 +0900 Subject: [PATCH v6 1/4] qapi: Do not consume a value if failed MIME-Version: 1.0 Message-Id: <20250306-virtio-v6-1-1235eab776d9@daynix.com> References: <20250306-virtio-v6-0-1235eab776d9@daynix.com> In-Reply-To: <20250306-virtio-v6-0-1235eab776d9@daynix.com> To: Jason Wang , Dmitry Fleytman , Sriram Yagnaraman , "Michael S. Tsirkin" , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Andrew Melnychenko , Yuri Benditovich , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Markus Armbruster , Michael Roth , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-?= =?utf-8?q?Daud=C3=A9?= , Yanan Wang , Zhao Liu , Lei Yang , BALATON Zoltan Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.14.2 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x630.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Do not consume a value if interpreting one failed so that we can reinterpret the value with a different type. Signed-off-by: Akihiko Odaki --- qapi/qobject-input-visitor.c | 103 +++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 34 deletions(-) diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index c52d36997df8d99bc575c553a413df8ebcb60c32..0cc75b3c74e40a24cb1ce764330ae2703e80d7cf 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -116,9 +116,8 @@ static const char *full_name(QObjectInputVisitor *qiv, const char *name) return full_name_nth(qiv, name, 0); } -static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv, - const char *name, - bool consume) +static QObject *qobject_input_try_get_object(const QObjectInputVisitor *qiv, + const char *name) { StackObject *tos; QObject *qobj; @@ -138,34 +137,19 @@ static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv, if (qobject_type(qobj) == QTYPE_QDICT) { assert(name); ret = qdict_get(qobject_to(QDict, qobj), name); - if (tos->h && consume && ret) { - bool removed = g_hash_table_remove(tos->h, name); - assert(removed); - } } else { assert(qobject_type(qobj) == QTYPE_QLIST); assert(!name); - if (tos->entry) { - ret = qlist_entry_obj(tos->entry); - if (consume) { - tos->entry = qlist_next(tos->entry); - } - } else { - ret = NULL; - } - if (consume) { - tos->index++; - } + ret = tos->entry ? qlist_entry_obj(tos->entry) : NULL; } return ret; } static QObject *qobject_input_get_object(QObjectInputVisitor *qiv, - const char *name, - bool consume, Error **errp) + const char *name, Error **errp) { - QObject *obj = qobject_input_try_get_object(qiv, name, consume); + QObject *obj = qobject_input_try_get_object(qiv, name); if (!obj) { error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name)); @@ -173,6 +157,38 @@ static QObject *qobject_input_get_object(QObjectInputVisitor *qiv, return obj; } +static void qobject_input_consume_object(QObjectInputVisitor *qiv, + const char *name) +{ + StackObject *tos; + QObject *qobj; + + if (QSLIST_EMPTY(&qiv->stack)) { + /* Starting at root, name is ignored. */ + return; + } + + /* We are in a container; find the next element. */ + tos = QSLIST_FIRST(&qiv->stack); + qobj = tos->obj; + assert(qobj); + + if (qobject_type(qobj) == QTYPE_QDICT) { + assert(name); + if (tos->h) { + bool removed = g_hash_table_remove(tos->h, name); + assert(removed); + } + } else { + assert(qobject_type(qobj) == QTYPE_QLIST); + assert(!name); + if (tos->entry) { + tos->entry = qlist_next(tos->entry); + } + tos->index++; + } +} + static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv, const char *name, Error **errp) @@ -180,7 +196,7 @@ static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv, QObject *qobj; QString *qstr; - qobj = qobject_input_get_object(qiv, name, true, errp); + qobj = qobject_input_get_object(qiv, name, errp); if (!qobj) { return NULL; } @@ -233,6 +249,7 @@ static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv, tos->index = -1; } + qobject_input_consume_object(qiv, name); QSLIST_INSERT_HEAD(&qiv->stack, tos, node); return tos->entry; } @@ -279,7 +296,7 @@ static bool qobject_input_start_struct(Visitor *v, const char *name, void **obj, size_t size, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); if (obj) { *obj = NULL; @@ -316,7 +333,7 @@ static bool qobject_input_start_list(Visitor *v, const char *name, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); const QListEntry *entry; if (list) { @@ -382,7 +399,7 @@ static bool qobject_input_start_alternate(Visitor *v, const char *name, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, false, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); if (!qobj) { *obj = NULL; @@ -397,7 +414,7 @@ static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); QNum *qnum; if (!qobj) { @@ -409,6 +426,7 @@ static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj, full_name(qiv, name)); return false; } + qobject_input_consume_object(qiv, name); return true; } @@ -428,6 +446,7 @@ static bool qobject_input_type_int64_keyval(Visitor *v, const char *name, full_name(qiv, name), "integer"); return false; } + qobject_input_consume_object(qiv, name); return true; } @@ -435,7 +454,7 @@ static bool qobject_input_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); QNum *qnum; int64_t val; @@ -448,12 +467,14 @@ static bool qobject_input_type_uint64(Visitor *v, const char *name, } if (qnum_get_try_uint(qnum, obj)) { + qobject_input_consume_object(qiv, name); return true; } /* Need to accept negative values for backward compatibility */ if (qnum_get_try_int(qnum, &val)) { *obj = val; + qobject_input_consume_object(qiv, name); return true; } @@ -479,6 +500,7 @@ static bool qobject_input_type_uint64_keyval(Visitor *v, const char *name, full_name(qiv, name), "integer"); return false; } + qobject_input_consume_object(qiv, name); return true; } @@ -486,7 +508,7 @@ static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); QBool *qbool; if (!qobj) { @@ -500,6 +522,7 @@ static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj, } *obj = qbool_get_bool(qbool); + qobject_input_consume_object(qiv, name); return true; } @@ -518,6 +541,7 @@ static bool qobject_input_type_bool_keyval(Visitor *v, const char *name, full_name(qiv, name), "'on' or 'off'"); return false; } + qobject_input_consume_object(qiv, name); return true; } @@ -525,7 +549,7 @@ static bool qobject_input_type_str(Visitor *v, const char *name, char **obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); QString *qstr; *obj = NULL; @@ -540,6 +564,7 @@ static bool qobject_input_type_str(Visitor *v, const char *name, char **obj, } *obj = g_strdup(qstring_get_str(qstr)); + qobject_input_consume_object(qiv, name); return true; } @@ -549,15 +574,20 @@ static bool qobject_input_type_str_keyval(Visitor *v, const char *name, QObjectInputVisitor *qiv = to_qiv(v); const char *str = qobject_input_get_keyval(qiv, name, errp); + if (!str) { + return false; + } + *obj = g_strdup(str); - return !!str; + qobject_input_consume_object(qiv, name); + return true; } static bool qobject_input_type_number(Visitor *v, const char *name, double *obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); QNum *qnum; if (!qobj) { @@ -571,6 +601,7 @@ static bool qobject_input_type_number(Visitor *v, const char *name, double *obj, } *obj = qnum_get_double(qnum); + qobject_input_consume_object(qiv, name); return true; } @@ -593,6 +624,7 @@ static bool qobject_input_type_number_keyval(Visitor *v, const char *name, } *obj = val; + qobject_input_consume_object(qiv, name); return true; } @@ -600,7 +632,7 @@ static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); *obj = NULL; if (!qobj) { @@ -608,6 +640,7 @@ static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj, } *obj = qobject_ref(qobj); + qobject_input_consume_object(qiv, name); return true; } @@ -615,7 +648,7 @@ static bool qobject_input_type_null(Visitor *v, const char *name, QNull **obj, Error **errp) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_get_object(qiv, name, true, errp); + QObject *qobj = qobject_input_get_object(qiv, name, errp); *obj = NULL; if (!qobj) { @@ -628,6 +661,7 @@ static bool qobject_input_type_null(Visitor *v, const char *name, return false; } *obj = qnull(); + qobject_input_consume_object(qiv, name); return true; } @@ -647,13 +681,14 @@ static bool qobject_input_type_size_keyval(Visitor *v, const char *name, full_name(qiv, name), "size"); return false; } + qobject_input_consume_object(qiv, name); return true; } static void qobject_input_optional(Visitor *v, const char *name, bool *present) { QObjectInputVisitor *qiv = to_qiv(v); - QObject *qobj = qobject_input_try_get_object(qiv, name, false); + QObject *qobj = qobject_input_try_get_object(qiv, name); if (!qobj) { *present = false; From patchwork Thu Mar 6 06:16:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14003867 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9C93AC282D1 for ; Thu, 6 Mar 2025 06:18:48 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tq4XT-0002D4-5S; Thu, 06 Mar 2025 01:16:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tq4XN-0002Am-5b for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:56 -0500 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tq4XL-0003Vh-BZ for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:52 -0500 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2234e5347e2so3907675ad.1 for ; Wed, 05 Mar 2025 22:16:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741241809; x=1741846609; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=OLOTqxsnw4F4JJLwq/6kQuieMJFPp4Wc6Bq0bf8Xlig=; b=rx5e1EkNqtE3/F2iO3Hvc7CrvINnfsgHtHwWl9oDwH0XoGA/oG7hhFeAgo+p9Pv6Ji hY8wyoAAcr7PlNeh5YIJYEJpmzmf1DC2yDNQ0QcajVa2J7izQhUCNmnmOYQFM9dOgg2l XThXt4Ed8XCp/AeHtZkK2BUs4GBYkl4HfrqsGo142L7zGr4rHurjDWd/dn2yVdpvYCIO lLxuTtGknfBAhayOMgOqdPfQ1jYK7pUtM34mCuXxqAfG8UVD6fkr/bTxOd1ARzHTch9M uqdw8dL9Aqx25EyMA/ScqS7Els9oz/fHx3b60+Up3RWB1ivaz6q0gkuYBg6hrnZTkLOc oSmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741241809; x=1741846609; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OLOTqxsnw4F4JJLwq/6kQuieMJFPp4Wc6Bq0bf8Xlig=; b=XUvgCAnDxmVu8NPaN2JtC3GR1reJz4YMjtpWwOiGqf5/YHIKWID79NeF+08G4POJyf D4bydqaa/to8k1bpBnsYVmHNvk7g2rIo4HUbFVkpq9I5lovx1sVw1uWoznY5l/5dezAW pIGVqz2sqeFcMY/CgeGlXSkM7X2BvnAKjqXUliAjIzGrVsNaOwFX+vTBzvKGuAfiwvJI nhHxv49KNnc2RL+8URnKyU63nKIu601R5UU28pNHZAff+WP3tRetueWgB76FqEL3qdoh PQt4eTXFwiyRuaz5CaAFEl7gftDk97rGTHFXEjT62HSPsl1WbGuuXUiqkEXXfl+OZBZ9 wA8w== X-Gm-Message-State: AOJu0YynJwdOVCsEAbAxyTun7pCjw/XQwpNEpm+V7jNmDMKSJ/bZ59+A tGgFPYiZhjkWAZGSPeAjfB+hfuNIZgcQTCMBcX8qBCJDkb+YyDAo5YUrGKqWta4= X-Gm-Gg: ASbGncuuuuM+xWEzSdQ+kypDO80hbLHjC2Q0p9ZhoGveiad/Sx8SsBvEId4XHHaQWJ/ hO6gDH313R08obEFSN6gYFQvZHU1HL0bJYKz9nYA0wXlr+h0Tezg9RQTdfJd/F80sG5bfWT3zbC uvPKOzJFk99mwOkEr4HipdV0qdUfQDjdIT7fS8IunSsyZ7TEKlZIZrXaQg7VJ4jXOKHAJDirYQl KL9M5+262MN1wosJuqsG9ssTuT7IJ9efONdI8mAM23rKxJPiServ9osuBHdJJbw1xSDlS/oteva 6U7Z+5N6kPnnOcTkiIMcr81TLvaxNQIPgVkZBv5JXJnCZOfX X-Google-Smtp-Source: AGHT+IGhDAQUhdj+WPs0WXC+p2k+n2erbuGkxErWenxDhcbHUomGbl61oyuOjT4kP1sLR2PUbLMLVg== X-Received: by 2002:a17:902:da8c:b0:224:13c0:58e3 with SMTP id d9443c01a7336-22413c05b5fmr6975605ad.51.1741241809476; Wed, 05 Mar 2025 22:16:49 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-224109e944csm4362025ad.74.2025.03.05.22.16.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Mar 2025 22:16:49 -0800 (PST) From: Akihiko Odaki Date: Thu, 06 Mar 2025 15:16:28 +0900 Subject: [PATCH v6 2/4] qapi: Accept bool for OnOffAuto and OnOffSplit MIME-Version: 1.0 Message-Id: <20250306-virtio-v6-2-1235eab776d9@daynix.com> References: <20250306-virtio-v6-0-1235eab776d9@daynix.com> In-Reply-To: <20250306-virtio-v6-0-1235eab776d9@daynix.com> To: Jason Wang , Dmitry Fleytman , Sriram Yagnaraman , "Michael S. Tsirkin" , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Andrew Melnychenko , Yuri Benditovich , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Markus Armbruster , Michael Roth , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-?= =?utf-8?q?Daud=C3=A9?= , Yanan Wang , Zhao Liu , Lei Yang , BALATON Zoltan Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.14.2 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org bool has representations of "on" and "off" different from OnOffAuto/OnOffSplit: - The command line syntax accepts on/yes/true/y and off/no/false/n for bool but only on and off for OnOffAuto. - JSON uses true/false for bool but "on" and "off" for OnOffAuto/OnOffSplit. This inconsistency causes some problems: - Users need to take the underlying type into consideration to determine what literal to specify, increasing cognitive loads for human users and complexity for programs invoking QEMU. - Converting an existing bool property to OnOffAuto/OnOffSplit will break compatibility. Fix these problems by accepting bool literals for OnOffAuto/OnOffSplit. This change is specific to OnOffAuto/OnOffSplit; types added in the future may be defined as an alternate of bool and enum to avoid the mentioned problems in the first place. Signed-off-by: Akihiko Odaki --- hw/core/qdev-properties.c | 17 ++++++++++++++++- scripts/qapi/visit.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 0b52aad55516099917a4e9b71a920bc3da8a92ec..3462921841db74456284b91374955b6101f03bbc 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -2,6 +2,7 @@ #include "hw/qdev-properties.h" #include "qapi/error.h" #include "qapi/qapi-types-misc.h" +#include "qapi/qapi-visit-common.h" #include "qobject/qlist.h" #include "qemu/ctype.h" #include "qemu/error-report.h" @@ -493,12 +494,26 @@ const PropertyInfo qdev_prop_string = { /* --- on/off/auto --- */ +static void set_on_off_auto(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + Property *prop = opaque; + int *ptr = object_field_prop_ptr(obj, prop); + OnOffAuto value; + + if (!visit_type_OnOffAuto(v, name, &value, errp)) { + return; + } + + *ptr = value; +} + const PropertyInfo qdev_prop_on_off_auto = { .name = "OnOffAuto", .description = "on/off/auto", .enum_table = &OnOffAuto_lookup, .get = qdev_propinfo_get_enum, - .set = qdev_propinfo_set_enum, + .set = set_on_off_auto, .set_default_value = qdev_propinfo_set_default_value_enum, }; diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 36e240967b687db10115ba375da3371898b7d95c..5844b64ad6b49a7b88e286dd35ab28b2755282d0 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -205,6 +205,29 @@ def gen_visit_list(name: str, element_type: QAPISchemaType) -> str: def gen_visit_enum(name: str) -> str: + if name in ('OnOffAuto', 'OnOffSplit'): + return mcgen(''' + +bool visit_type_%(c_name)s(Visitor *v, const char *name, + %(c_name)s *obj, Error **errp) +{ + bool b; + int i; + + if (v->type == VISITOR_INPUT && visit_type_bool(v, name, &b, NULL)) { + *obj = b ? %(on)s : %(off)s; + return true; + } + + b = visit_type_enum(v, name, &i, &%(c_name)s_lookup, errp); + *obj = i; + + return b; +} +''', + c_name=c_name(name), + on=c_enum_const(name, 'on'), off=c_enum_const(name, 'off')) + return mcgen(''' bool visit_type_%(c_name)s(Visitor *v, const char *name, @@ -355,6 +378,7 @@ def _begin_user_module(self, name: str) -> None: self._genc.preamble_add(mcgen(''' #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/visitor-impl.h" #include "%(visit)s.h" #include "%(prefix)sqapi-features.h" ''', From patchwork Thu Mar 6 06:16:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14003863 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BB319C282D1 for ; Thu, 6 Mar 2025 06:17:18 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tq4Xb-0002KM-Fx; Thu, 06 Mar 2025 01:17:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tq4XT-0002D6-AQ for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:17:00 -0500 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tq4XR-0003W1-Eo for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:59 -0500 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-2234bec7192so5444305ad.2 for ; Wed, 05 Mar 2025 22:16:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741241816; x=1741846616; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ZxBk/fGR/dV8W/QywF6HTbYAQcdPlz1qBOC13iNfE1M=; b=q7TljaeCS7PE55s4gSA2cCBCN8DuPLpr1Q+g7GW9aR2hYp9XAj2k7kddXjq4aeM2Bb F20FZ8z+7VZZksdTqdPXlcPJf2gMJtn/znw1UVJdvVmgakKZAoSbKkCOjoZxqdAdvsrC ic8rmCg9ZFZWBZn38Hmgl3cM44bheb3Fzibfz5iAUDAqAJhVMyV+76MMiKVsG/OfSdOS fkMAH0ggmVNbv0eOZ4xBKYR7IAF/CFX+uPVZV0jz/Z6ECv6EtHFiM2lhgz8kDg7aOjJw rL+7w8FtZZ02Hn/gaEbIeiMSI6ztGFU37x3Wp3V8g+InLIqr7QlWnCfbFmJa45dQP0Ns HLqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741241816; x=1741846616; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZxBk/fGR/dV8W/QywF6HTbYAQcdPlz1qBOC13iNfE1M=; b=re46HZOEX5JRLb2gqdLjcQodZGEfaXnSFx43u4rka/ZJxe7NczAnhRsRkm+ur+DDeW xptsp3CiXqy0vPlsofgkZHoUC42xzwSSsN3FSA3+PUSVMJ7Pk8W4gsMeCtHrTKVe7ZED Lid0dAR2fOjJ4f+R91GwH7LRsEKhqaS/T7gNKeHf6K4sDl+QpwGNax0Kf3+X61gGaaCH e4YJoH+h8mSoPlb7nMkOVoMFUGwFYtVZBHTJKRj40iKei9mi6xYJYQahXPfY3pLXRuSY LsxM+fP4T3eWTXhOtw37/Ops8OoT0Ea7nnkEgAjJ0kwxK4UKRUFRHFnDJxO2i3usFZsU nWSA== X-Gm-Message-State: AOJu0YzTBooyReng7y4fxX87EdphvboimkqV1b6uNxEJrqdCSYwpcLDz /sSa1jLbKw82uAFX4gMUz8gcqUobiMBQ+R9kJ+4xyICdgfpnMYjDpHByoCnq544= X-Gm-Gg: ASbGnctVaEdyh0BR86l7zhiVC+LYiSwBQASPEIoCYk+RAJGgU+9dwAXXlrqbJkYFLNF QSHdIcgOjEtOiU5BnQDMV4QHxrJKBRZBukhVpzCEnpahCoALzBziXyiPleFCocuyWlxR7uwVER7 d539RnAUo57/J0TSnHC0htFCrr8ke0pexZI/1L+Uq9m4nXlT8ulLTF5EXey+AjtgDpcb3vKOaec WvcbNt9gnvb02ksSxDGB7lkKWeHtek2T9Ao3fnpTndVvrYEuricVayQ5cvnXVNDFmCydJZMriIX nEWVLboyUJZYu6HXrl2XeP0ye1PlLQpCK7BkKuvjfufaJalm X-Google-Smtp-Source: AGHT+IG1mxOhPXte33U8wwl5jxtymCcF5Xfqqr86JH2GDL90PfX2YFLJejn85IfRxNcTZNq0KAVW8w== X-Received: by 2002:a05:6a21:a43:b0:1f3:323e:373e with SMTP id adf61e73a8af0-1f3494784c8mr10660598637.16.1741241815959; Wed, 05 Mar 2025 22:16:55 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-7369853818esm489888b3a.168.2025.03.05.22.16.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Mar 2025 22:16:55 -0800 (PST) From: Akihiko Odaki Date: Thu, 06 Mar 2025 15:16:29 +0900 Subject: [PATCH v6 3/4] qdev-properties: Add DEFINE_PROP_ON_OFF_AUTO_BIT64() MIME-Version: 1.0 Message-Id: <20250306-virtio-v6-3-1235eab776d9@daynix.com> References: <20250306-virtio-v6-0-1235eab776d9@daynix.com> In-Reply-To: <20250306-virtio-v6-0-1235eab776d9@daynix.com> To: Jason Wang , Dmitry Fleytman , Sriram Yagnaraman , "Michael S. Tsirkin" , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Andrew Melnychenko , Yuri Benditovich , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Markus Armbruster , Michael Roth , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-?= =?utf-8?q?Daud=C3=A9?= , Yanan Wang , Zhao Liu , Lei Yang , BALATON Zoltan Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.14.2 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org DEFINE_PROP_ON_OFF_AUTO_BIT64() corresponds to DEFINE_PROP_ON_OFF_AUTO() as DEFINE_PROP_BIT64() corresponds to DEFINE_PROP_BOOL(). The difference is that DEFINE_PROP_ON_OFF_AUTO_BIT64() exposes OnOffAuto instead of bool. Signed-off-by: Akihiko Odaki --- include/hw/qdev-properties.h | 18 ++++++++++++ hw/core/qdev-properties.c | 66 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h index bf27375a3ccdb238ef3327dd85d3d0a1431cbfbf..0d161325e8dc92d0e0e5aa9a1e2dd734f7a55cae 100644 --- a/include/hw/qdev-properties.h +++ b/include/hw/qdev-properties.h @@ -43,11 +43,22 @@ struct PropertyInfo { ObjectPropertyRelease *release; }; +/** + * struct OnOffAutoBit64 - OnOffAuto storage with 64 elements. + * @on_bits: Bitmap of elements with "on". + * @auto_bits: Bitmap of elements with "auto". + */ +typedef struct OnOffAutoBit64 { + uint64_t on_bits; + uint64_t auto_bits; +} OnOffAutoBit64; + /*** qdev-properties.c ***/ extern const PropertyInfo qdev_prop_bit; extern const PropertyInfo qdev_prop_bit64; +extern const PropertyInfo qdev_prop_on_off_auto_bit64; extern const PropertyInfo qdev_prop_bool; extern const PropertyInfo qdev_prop_enum; extern const PropertyInfo qdev_prop_uint8; @@ -100,6 +111,13 @@ extern const PropertyInfo qdev_prop_link; .set_default = true, \ .defval.u = (bool)_defval) +#define DEFINE_PROP_ON_OFF_AUTO_BIT64(_name, _state, _field, _bit, _defval) \ + DEFINE_PROP(_name, _state, _field, qdev_prop_on_off_auto_bit64, \ + OnOffAutoBit64, \ + .bitnr = (_bit), \ + .set_default = true, \ + .defval.i = (OnOffAuto)_defval) + #define DEFINE_PROP_BOOL(_name, _state, _field, _defval) \ DEFINE_PROP(_name, _state, _field, qdev_prop_bool, bool, \ .set_default = true, \ diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 3462921841db74456284b91374955b6101f03bbc..54fbcc9fa8d4b66003d9a66b1e6af61684c59ae4 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -188,7 +188,8 @@ const PropertyInfo qdev_prop_bit = { static uint64_t qdev_get_prop_mask64(const Property *prop) { - assert(prop->info == &qdev_prop_bit64); + assert(prop->info == &qdev_prop_bit64 || + prop->info == &qdev_prop_on_off_auto_bit64); return 0x1ull << prop->bitnr; } @@ -233,6 +234,69 @@ const PropertyInfo qdev_prop_bit64 = { .set_default_value = set_default_value_bool, }; +static void prop_get_on_off_auto_bit64(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + Property *prop = opaque; + OnOffAutoBit64 *p = object_field_prop_ptr(obj, prop); + OnOffAuto value; + uint64_t mask = qdev_get_prop_mask64(prop); + + if (p->auto_bits & mask) { + value = ON_OFF_AUTO_AUTO; + } else if (p->on_bits & mask) { + value = ON_OFF_AUTO_ON; + } else { + value = ON_OFF_AUTO_OFF; + } + + visit_type_OnOffAuto(v, name, &value, errp); +} + +static void prop_set_on_off_auto_bit64(Object *obj, Visitor *v, + const char *name, void *opaque, + Error **errp) +{ + Property *prop = opaque; + OnOffAutoBit64 *p = object_field_prop_ptr(obj, prop); + OnOffAuto value; + uint64_t mask = qdev_get_prop_mask64(prop); + + if (!visit_type_OnOffAuto(v, name, &value, errp)) { + return; + } + + switch (value) { + case ON_OFF_AUTO_AUTO: + p->on_bits &= ~mask; + p->auto_bits |= mask; + break; + + case ON_OFF_AUTO_ON: + p->on_bits |= mask; + p->auto_bits &= ~mask; + break; + + case ON_OFF_AUTO_OFF: + p->on_bits &= ~mask; + p->auto_bits &= ~mask; + break; + + case ON_OFF_AUTO__MAX: + g_assert_not_reached(); + } +} + +const PropertyInfo qdev_prop_on_off_auto_bit64 = { + .name = "OnOffAuto", + .description = "on/off/auto", + .enum_table = &OnOffAuto_lookup, + .get = prop_get_on_off_auto_bit64, + .set = prop_set_on_off_auto_bit64, + .set_default_value = qdev_propinfo_set_default_value_enum, +}; + /* --- bool --- */ static void get_bool(Object *obj, Visitor *v, const char *name, void *opaque, From patchwork Thu Mar 6 06:16:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 14003864 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 10892C282D1 for ; Thu, 6 Mar 2025 06:17:42 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tq4Xf-0002Mw-FS; Thu, 06 Mar 2025 01:17:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tq4Xa-0002Jm-PJ for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:17:06 -0500 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tq4XX-0003Wc-R0 for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:17:06 -0500 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-22334203781so30547795ad.0 for ; Wed, 05 Mar 2025 22:17:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741241822; x=1741846622; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AGRsR0xCOtQhyX6ujNZbtw42ERJg2g6jZAFsjNyb/5c=; b=nJQArtk+KwS58B/lRS6Q3n24QYEwQpPtJhYt37nJTQfSr//dCdCji1rbaNx8b9vgCP bQ01XEhBTPKPCaIudhThT/7vt7C8KEQ+OVkpfRN6kP71hUKRdTe4LTopBC3qeLAyJkqI iQQ2GvhVRysFtITKemzN+1vQuqrNiGqoYeE7OEsA2DSrNklrjXYfKIIyHFs63l0BQqMU 33uY8lBuhnYuEq6iblD2jXr4o9cO8uyamWMYnpTH4bHBKIR5+J+cn3RHkrYIU2bwbZRl XgDUTX8C/qW8WxK9Wbbqx6UVtDB022iXeiSkkIhVufmPxGj6czqmqq2eypK+HEMl7+D8 1UEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741241822; x=1741846622; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AGRsR0xCOtQhyX6ujNZbtw42ERJg2g6jZAFsjNyb/5c=; b=igHwgxBVhRZUFoldD+qoh83JKUZp+o+LSX8eoNSEjgBkdqJ8CRAF55LLPQFY5/H6T4 C8csPHAMJ8YRB8i4SXUR6jqYuiU2J7A/XDaqBLqsrYkFZF2x+dVeZWb5BR0mpKvTJ3zN i38uiNERebV6d00ZUrvdXGWPrv16LMKYVfhFx158zW4xEv2Zf6/yeX0bOMUOE3vNK5EQ NvfnWJdljCTAI1PUVCKOQVB/RfjcKRpYLOF0Z/hrXfEjje+T9B3fDRs/2WmeM0W+dg6U QVaDZ6sIVXTvnlGEOF89iiIbVAPqa76Y4z7ati8VdRCWVxQ+hEfIsoO1R8ckysDHc6/e aGFQ== X-Gm-Message-State: AOJu0YwrGIypzgs8YMSVnOq98dFct/8cX6zS/y6DyKexn1d2ZnuNr+e/ r7cgNXoXfCCAeMEFq7sqN3WT4E75VsZZuiX6ZPuhYaa0z6W1M67UFotvISY+rlU= X-Gm-Gg: ASbGncuCb30X30UjiZk7X8Ph7x7WNeDqGBw6K20N43VwNb5xJ4w30RGSQhLpkLK+Krt h/Rnxu1nyvezdDJqRo/r4RBL4SI6EYqO7StHramP6e1u+uA6RyPNEiDT7UYZRyaj1Af7BrIEX+Q w+dDe5lRwm5uj83oNh5jo0xPRacbrdZ2Pvaa19wutOzTZUPe33XQmxt7HD8nAzMQKWIQrvKNVoh 4MnhXoyrCk7dgsuNoJlS5MCZTm64Sa2fLKkhCt9XNl74iAkQyYzYHlX3+G7FPY7lJGNKrlcty2p E0MbnVCrJAJJEnaBUxrUbavPNmgLVI0VPeLCdutdzLdzwSwo X-Google-Smtp-Source: AGHT+IG8eajS4GyqD5p++5kFvzmjivQbzFN0Vvv0tKWIiVXpqP6Qg/d5gbQxmCtCaVriHzLoKQnKtg== X-Received: by 2002:a05:6a00:1816:b0:732:706c:c4ff with SMTP id d2e1a72fcca58-73693f731b2mr3741981b3a.7.1741241822254; Wed, 05 Mar 2025 22:17:02 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-7369844ce73sm499880b3a.74.2025.03.05.22.16.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Mar 2025 22:17:01 -0800 (PST) From: Akihiko Odaki Date: Thu, 06 Mar 2025 15:16:30 +0900 Subject: [PATCH v6 4/4] virtio: Convert feature properties to OnOffAuto MIME-Version: 1.0 Message-Id: <20250306-virtio-v6-4-1235eab776d9@daynix.com> References: <20250306-virtio-v6-0-1235eab776d9@daynix.com> In-Reply-To: <20250306-virtio-v6-0-1235eab776d9@daynix.com> To: Jason Wang , Dmitry Fleytman , Sriram Yagnaraman , "Michael S. Tsirkin" , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Andrew Melnychenko , Yuri Benditovich , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Markus Armbruster , Michael Roth , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-?= =?utf-8?q?Daud=C3=A9?= , Yanan Wang , Zhao Liu , Lei Yang , BALATON Zoltan Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.14.2 Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x631.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Some features are not always available with vhost. Legacy features are not available with vp_vdpa in particular. virtio devices used to disable them when not available even if the corresponding properties were explicitly set to "on". QEMU already has OnOffAuto type, which includes the "auto" value to let it automatically decide the effective value. Convert feature properties to OnOffAuto and set them "auto" by default to utilize it. This allows QEMU to report an error if they are set "on" and the corresponding features are not available. Signed-off-by: Akihiko Odaki --- include/hw/virtio/virtio.h | 38 +++++++++++++++++++++----------------- hw/core/machine.c | 1 + hw/virtio/virtio-bus.c | 14 ++++++++++++-- hw/virtio/virtio.c | 4 +++- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 638691028050d2599592d8c7e95c75ac3913fbdd..b854c2cb1d04da0a35165289c28f87e8cb869df6 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -113,7 +113,8 @@ struct VirtIODevice uint16_t queue_sel; /** * These fields represent a set of VirtIO features at various - * levels of the stack. @host_features indicates the complete + * levels of the stack. @requested_features indicates the feature + * set the user requested. @host_features indicates the complete * feature set the VirtIO device can offer to the driver. * @guest_features indicates which features the VirtIO driver has * selected by writing to the feature register. Finally @@ -121,6 +122,7 @@ struct VirtIODevice * backend (e.g. vhost) and could potentially be a subset of the * total feature set offered by QEMU. */ + OnOffAutoBit64 requested_features; uint64_t host_features; uint64_t guest_features; uint64_t backend_features; @@ -149,6 +151,7 @@ struct VirtIODevice bool started; bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */ bool disable_legacy_check; + bool force_features_auto; bool vhost_started; VMChangeStateEntry *vmstate; char *bus_name; @@ -376,22 +379,23 @@ typedef struct VirtIOSCSIConf VirtIOSCSIConf; typedef struct VirtIORNGConf VirtIORNGConf; #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \ - DEFINE_PROP_BIT64("indirect_desc", _state, _field, \ - VIRTIO_RING_F_INDIRECT_DESC, true), \ - DEFINE_PROP_BIT64("event_idx", _state, _field, \ - VIRTIO_RING_F_EVENT_IDX, true), \ - DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \ - VIRTIO_F_NOTIFY_ON_EMPTY, true), \ - DEFINE_PROP_BIT64("any_layout", _state, _field, \ - VIRTIO_F_ANY_LAYOUT, true), \ - DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ - VIRTIO_F_IOMMU_PLATFORM, false), \ - DEFINE_PROP_BIT64("packed", _state, _field, \ - VIRTIO_F_RING_PACKED, false), \ - DEFINE_PROP_BIT64("queue_reset", _state, _field, \ - VIRTIO_F_RING_RESET, true), \ - DEFINE_PROP_BIT64("in_order", _state, _field, \ - VIRTIO_F_IN_ORDER, false) + DEFINE_PROP_ON_OFF_AUTO_BIT64("indirect_desc", _state, _field, \ + VIRTIO_RING_F_INDIRECT_DESC, \ + ON_OFF_AUTO_AUTO), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("event_idx", _state, _field, \ + VIRTIO_RING_F_EVENT_IDX, ON_OFF_AUTO_AUTO), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("notify_on_empty", _state, _field, \ + VIRTIO_F_NOTIFY_ON_EMPTY, ON_OFF_AUTO_AUTO), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("any_layout", _state, _field, \ + VIRTIO_F_ANY_LAYOUT, ON_OFF_AUTO_AUTO), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("iommu_platform", _state, _field, \ + VIRTIO_F_IOMMU_PLATFORM, ON_OFF_AUTO_OFF), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("packed", _state, _field, \ + VIRTIO_F_RING_PACKED, ON_OFF_AUTO_OFF), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("queue_reset", _state, _field, \ + VIRTIO_F_RING_RESET, ON_OFF_AUTO_AUTO), \ + DEFINE_PROP_ON_OFF_AUTO_BIT64("in_order", _state, _field, \ + VIRTIO_F_IN_ORDER, ON_OFF_AUTO_OFF) hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n); diff --git a/hw/core/machine.c b/hw/core/machine.c index 02cff735b3fb8cf0ce8a85ec7f1fb36a98005a24..b25b366bdc715acd5c90af7ac38cfb3ac88f4c5b 100644 --- a/hw/core/machine.c +++ b/hw/core/machine.c @@ -43,6 +43,7 @@ GlobalProperty hw_compat_9_2[] = { { "virtio-balloon-pci-non-transitional", "vectors", "0" }, { "virtio-mem-pci", "vectors", "0" }, { "migration", "multifd-clean-tls-termination", "false" }, + { TYPE_VIRTIO_DEVICE, "x-force-features-auto", "on" }, }; const size_t hw_compat_9_2_len = G_N_ELEMENTS(hw_compat_9_2); diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c index 896feb37a1caa805543e971c150d3673675b9a6b..75d433b252d5337d91616a2847b3dc12e811c2da 100644 --- a/hw/virtio/virtio-bus.c +++ b/hw/virtio/virtio-bus.c @@ -50,6 +50,7 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp) bool has_iommu = virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); bool vdev_has_iommu; Error *local_err = NULL; + uint64_t features; DPRINTF("%s: plug device.\n", qbus->name); @@ -63,13 +64,22 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp) /* Get the features of the plugged device. */ assert(vdc->get_features != NULL); - vdev->host_features = vdc->get_features(vdev, vdev->host_features, - &local_err); + features = vdev->host_features | vdev->requested_features.auto_bits | + vdev->requested_features.on_bits; + features = vdc->get_features(vdev, features, &local_err); if (local_err) { error_propagate(errp, local_err); return; } + if (!vdev->force_features_auto && + (features & vdev->requested_features.on_bits) != vdev->requested_features.on_bits) { + error_setg(errp, "A requested feature is not supported by the device"); + return; + } + + vdev->host_features = features; + if (klass->device_plugged != NULL) { klass->device_plugged(qbus->parent, &local_err); } diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 85110bce37443bb46c4159761af112d0dba466b4..83f803fc703da6257608e21476305c8e9c6a8b07 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -4013,11 +4013,13 @@ static void virtio_device_instance_finalize(Object *obj) } static const Property virtio_properties[] = { - DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features), + DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, requested_features), DEFINE_PROP_BOOL("use-started", VirtIODevice, use_started, true), DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice, use_disabled_flag, true), DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice, disable_legacy_check, false), + DEFINE_PROP_BOOL("x-force-features-auto", VirtIODevice, + force_features_auto, false), }; static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)