From nobody Tue Feb 10 08:03:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1697469626; cv=none; d=zohomail.com; s=zohoarc; b=F6SM26DNL3l7d3MAg+AzltnPIDjJ9cvqyarfz51K+vLEQe14skSgvg/3fwHk5jcoX1rnbBh0S+FZXqFLtnhsHUh/IK4RXq7LLG6nQsYbKVgtxKTncEw/jR5WooOlY5U126JldgPcVPnyY0WvwlxWiw/FIB/BCselQxgwbFyBtWM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697469626; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=TdW7pWLHlLv278eB+Yc1VQQLRAc6P36ZNDdKDPh28m0=; b=YYttbz4GsYXj1RnnSSV5D29BPW+2RKN+6bnDRBkRM7vJTB8WzUYoOuoOjEW1w5f6pSg4Jkd1zgrkdsHwvjImoqZ36zbdKVXp0oiUHW+aVLNqEnl9VhpCmQ2tCWP7JKu8LZ5ahhTPLFdWdGWAPHS5BUdU0dmUbS01HBn9JtqEnm4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1697469626197526.0909941046713; Mon, 16 Oct 2023 08:20:26 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.617757.960762 (Exim 4.92) (envelope-from ) id 1qsPNf-0003sy-6D; Mon, 16 Oct 2023 15:19:43 +0000 Received: by outflank-mailman (output) from mailman id 617757.960762; Mon, 16 Oct 2023 15:19:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsPNf-0003qr-0b; Mon, 16 Oct 2023 15:19:43 +0000 Received: by outflank-mailman (input) for mailman id 617757; Mon, 16 Oct 2023 15:19:40 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qsPNc-0001wj-GK for xen-devel@lists.xenproject.org; Mon, 16 Oct 2023 15:19:40 +0000 Received: from desiato.infradead.org (desiato.infradead.org [2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 6b0e86a4-6c37-11ee-9b0e-b553b5be7939; Mon, 16 Oct 2023 17:19:37 +0200 (CEST) Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qsPNB-0067AJ-2F; Mon, 16 Oct 2023 15:19:15 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qsPNC-0005ne-0D; Mon, 16 Oct 2023 16:19:14 +0100 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list X-Inumbo-ID: 6b0e86a4-6c37-11ee-9b0e-b553b5be7939 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=TdW7pWLHlLv278eB+Yc1VQQLRAc6P36ZNDdKDPh28m0=; b=RGDymOaeG8K1D/v44ruA5TZh8q yIisakCcpuamBOij8UlBgpYUEpR1D1wEBLaWOSU+ss27q//FiVgfKJeg7VS8pbyC9PsQmXmwnrOmW o5iVb9ww1iBkPJhyOi7Sc43Xfr7YBbFVdfusAPOOwRATZNOM2ObJhyC7K66YjP+S5gUOdMo0pr9mK Jo+USZWvusMx/TNmvhcGTcsrSwDS5+1Z1bYJmgYpEilt7n2HkBGrvg+D7T8zHfi73sb2dEblB7LX2 iNt0YZiJQwK3W1lkbDeL4VfcIg9XkLaUoqSE48WawQUxp9Q87L69aiRbTfJZJYSkjK1T/tBHXRNPD lvQikPAQ==; From: David Woodhouse To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , Stefano Stabellini , Anthony Perard , Paul Durrant , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , "Michael S. Tsirkin" , Marcel Apfelbaum , Richard Henderson , Eduardo Habkost , David Woodhouse , Marcelo Tosatti , qemu-block@nongnu.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org Subject: [PATCH 09/12] hw/xen: prevent duplicate device registrations Date: Mon, 16 Oct 2023 16:19:06 +0100 Message-Id: <20231016151909.22133-10-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231016151909.22133-1-dwmw2@infradead.org> References: <20231016151909.22133-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1697469627322100002 Content-Type: text/plain; charset="utf-8" From: David Woodhouse Ensure that we have a XenBackendInstance for every device regardless of whether it was "discovered" in XenStore or created directly in QEMU. This allows the backend_list to be a source of truth about whether a given backend exists, and allows us to reject duplicates. This also cleans up the fact that backend drivers were calling xen_backend_set_device() with a XenDevice immediately after calling qdev_realize_and_unref() on it, when it wasn't theirs to play with any more. Signed-off-by: David Woodhouse --- hw/block/xen-block.c | 1 - hw/char/xen_console.c | 2 +- hw/xen/xen-backend.c | 78 ++++++++++++++++++++++++++---------- hw/xen/xen-bus.c | 8 ++++ include/hw/xen/xen-backend.h | 3 ++ 5 files changed, 69 insertions(+), 23 deletions(-) diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c index a07cd7eb5d..9262338535 100644 --- a/hw/block/xen-block.c +++ b/hw/block/xen-block.c @@ -975,7 +975,6 @@ static void xen_block_device_create(XenBackendInstance = *backend, goto fail; } =20 - xen_backend_set_device(backend, xendev); return; =20 fail: diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c index bd20be116c..2825b8c511 100644 --- a/hw/char/xen_console.c +++ b/hw/char/xen_console.c @@ -468,7 +468,7 @@ static void xen_console_device_create(XenBackendInstanc= e *backend, Chardev *cd =3D NULL; struct qemu_xs_handle *xsh =3D xenbus->xsh; =20 - if (qemu_strtoul(name, NULL, 10, &number)) { + if (qemu_strtoul(name, NULL, 10, &number) || number >=3D INT_MAX) { error_setg(errp, "failed to parse name '%s'", name); goto fail; } diff --git a/hw/xen/xen-backend.c b/hw/xen/xen-backend.c index b9bf70a9f5..dcb4329258 100644 --- a/hw/xen/xen-backend.c +++ b/hw/xen/xen-backend.c @@ -101,22 +101,28 @@ static XenBackendInstance *xen_backend_list_find(XenD= evice *xendev) return NULL; } =20 -bool xen_backend_exists(const char *type, const char *name) +static XenBackendInstance *xen_backend_lookup(const XenBackendImpl *impl, = const char *name) { - const XenBackendImpl *impl =3D xen_backend_table_lookup(type); XenBackendInstance *backend; =20 - if (!impl) { - return false; - } - QLIST_FOREACH(backend, &backend_list, entry) { if (backend->impl =3D=3D impl && !strcmp(backend->name, name)) { - return true; + return backend; } } =20 - return false; + return NULL; +} + +bool xen_backend_exists(const char *type, const char *name) +{ + const XenBackendImpl *impl =3D xen_backend_table_lookup(type); + + if (!impl) { + return false; + } + + return !!xen_backend_lookup(impl, name); } =20 static void xen_backend_list_remove(XenBackendInstance *backend) @@ -138,11 +144,10 @@ void xen_backend_device_create(XenBus *xenbus, const = char *type, backend =3D g_new0(XenBackendInstance, 1); backend->xenbus =3D xenbus; backend->name =3D g_strdup(name); - - impl->create(backend, opts, errp); - backend->impl =3D impl; xen_backend_list_add(backend); + + impl->create(backend, opts, errp); } =20 XenBus *xen_backend_get_bus(XenBackendInstance *backend) @@ -155,13 +160,6 @@ const char *xen_backend_get_name(XenBackendInstance *b= ackend) return backend->name; } =20 -void xen_backend_set_device(XenBackendInstance *backend, - XenDevice *xendev) -{ - g_assert(!backend->xendev); - backend->xendev =3D xendev; -} - XenDevice *xen_backend_get_device(XenBackendInstance *backend) { return backend->xendev; @@ -178,9 +176,7 @@ bool xen_backend_try_device_destroy(XenDevice *xendev, = Error **errp) } =20 impl =3D backend->impl; - if (backend->xendev) { - impl->destroy(backend, errp); - } + impl->destroy(backend, errp); =20 xen_backend_list_remove(backend); g_free(backend->name); @@ -188,3 +184,43 @@ bool xen_backend_try_device_destroy(XenDevice *xendev,= Error **errp) =20 return true; } + +bool xen_backend_device_realized(XenDevice *xendev, Error **errp) +{ + XenDeviceClass *xendev_class =3D XEN_DEVICE_GET_CLASS(xendev); + const char *type =3D xendev_class->backend ? : object_get_typename(OBJ= ECT(xendev)); + const XenBackendImpl *impl =3D xen_backend_table_lookup(type); + XenBackendInstance *backend; + + if (!impl) { + return false; + } + + backend =3D xen_backend_lookup(impl, xendev->name); + if (backend) { + if (backend->xendev && backend->xendev !=3D xendev) { + error_setg(errp, "device %s/%s already exists", type, xendev->= name); + return false; + } + backend->xendev =3D xendev; + return true; + } + + backend =3D g_new0(XenBackendInstance, 1); + backend->xenbus =3D XEN_BUS(qdev_get_parent_bus(DEVICE(xendev))); + backend->xendev =3D xendev; + backend->name =3D g_strdup(xendev->name); + backend->impl =3D impl; + + xen_backend_list_add(backend); + return true; +} + +void xen_backend_device_unrealized(XenDevice *xendev) +{ + XenBackendInstance *backend =3D xen_backend_list_find(xendev); + + if (backend) { + backend->xendev =3D NULL; + } +} diff --git a/hw/xen/xen-bus.c b/hw/xen/xen-bus.c index 0da2aa219a..0b232d1f94 100644 --- a/hw/xen/xen-bus.c +++ b/hw/xen/xen-bus.c @@ -359,6 +359,8 @@ static void xen_bus_realize(BusState *bus, Error **errp) =20 g_free(type); g_free(key); + + xen_bus_enumerate(xenbus); return; =20 fail: @@ -958,6 +960,8 @@ static void xen_device_unrealize(DeviceState *dev) =20 trace_xen_device_unrealize(type, xendev->name); =20 + xen_backend_device_unrealized(xendev); + if (xendev->exit.notify) { qemu_remove_exit_notifier(&xendev->exit); xendev->exit.notify =3D NULL; @@ -1024,6 +1028,10 @@ static void xen_device_realize(DeviceState *dev, Err= or **errp) goto unrealize; } =20 + if (!xen_backend_device_realized(xendev, errp)) { + goto unrealize; + } + trace_xen_device_realize(type, xendev->name); =20 xendev->xsh =3D qemu_xen_xs_open(); diff --git a/include/hw/xen/xen-backend.h b/include/hw/xen/xen-backend.h index 0f01631ae7..3f1e764c51 100644 --- a/include/hw/xen/xen-backend.h +++ b/include/hw/xen/xen-backend.h @@ -38,4 +38,7 @@ void xen_backend_device_create(XenBus *xenbus, const char= *type, const char *name, QDict *opts, Error **errp= ); bool xen_backend_try_device_destroy(XenDevice *xendev, Error **errp); =20 +bool xen_backend_device_realized(XenDevice *xendev, Error **errp); +void xen_backend_device_unrealized(XenDevice *xendev); + #endif /* HW_XEN_BACKEND_H */ --=20 2.40.1