From nobody Fri Apr 26 20:42:03 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1675198506; cv=none; d=zohomail.com; s=zohoarc; b=ZgJO571QVuEaeJwpiYYY3nT3h7qQa4DuyDuPag0TogusUElT3JBdZP6eXHC1BgKwK3vD6lvOpM9ZVCRvcJZRwKubRyV901t+OuQihGFTQPUujoNpf7ZQzrsK3+mEHtNyBVSfjtB16KQK7yyCk2IRLvr/tgRTQUcoWLSRrLYiQT4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675198506; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tT440oPsmvWe/E/zglkAuS0Ga4poZPOBe1uTUysR0cY=; b=nMg4eTed7fl7ZUtBHs27b0XtbRv3Vl1DaYo0+aofG7DPbfI/WtCoiiwPsTb6ZgxblGctUkgkXVCMH0YClIZ6l9OorbgBxrSupFMl8lrPZkVOv5jnZ7AOd/riNqbRouOADhzxrpcOVORqNTB6BpXPrlsZwMEBDyOusPRF9Az6hbs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675198506693987.138014351595; Tue, 31 Jan 2023 12:55:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pMxeU-0005AM-TQ; Tue, 31 Jan 2023 15:54:55 -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 1pMxeS-0004pk-9k for qemu-devel@nongnu.org; Tue, 31 Jan 2023 15:54:48 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMxeQ-0004jq-CY for qemu-devel@nongnu.org; Tue, 31 Jan 2023 15:54:47 -0500 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-626-cTHNZsW8PLWB80HhygJ1MQ-1; Tue, 31 Jan 2023 15:54:42 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 937AA2804823; Tue, 31 Jan 2023 20:54:41 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.193.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E49840C2064; Tue, 31 Jan 2023 20:54:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675198485; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tT440oPsmvWe/E/zglkAuS0Ga4poZPOBe1uTUysR0cY=; b=HoqJXTvsS9cXWeZVz1ktq+scUwsARztALH8C9n3gBJTjMZvFqSV3l2mdO22HffBG6H6boH cTfv4Ffg3JcE7HUNU+NtPo3GtNAiPCk13AjLu/nraNJeei/yIOvjOTVazshGol8EU60Gw0 SSRNoosukhuok1x4gFvJGelYrA2/SWM= X-MC-Unique: cTHNZsW8PLWB80HhygJ1MQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, yi.l.liu@intel.com, yi.y.sun@intel.com, alex.williamson@redhat.com, clg@redhat.com, qemu-devel@nongnu.org Cc: david@gibson.dropbear.id.au, thuth@redhat.com, farman@linux.ibm.com, mjrosato@linux.ibm.com, akrowiak@linux.ibm.com, pasic@linux.ibm.com, jjherne@linux.ibm.com, jasowang@redhat.com, kvm@vger.kernel.org, jgg@nvidia.com, nicolinc@nvidia.com, kevin.tian@intel.com, chao.p.peng@intel.com, peterx@redhat.com, shameerali.kolothum.thodi@huawei.com, zhangfei.gao@linaro.org, berrange@redhat.com, apopple@nvidia.com, suravee.suthikulpanit@amd.com Subject: [RFC v3 13/18] vfio/container-base: Introduce VFIOContainer reset callback Date: Tue, 31 Jan 2023 21:53:00 +0100 Message-Id: <20230131205305.2726330-14-eric.auger@redhat.com> In-Reply-To: <20230131205305.2726330-1-eric.auger@redhat.com> References: <20230131205305.2726330-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, 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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1675198508397100002 Content-Type: text/plain; charset="utf-8" Reset implementation depends on the container backend. Let's introduce a VFIOContainer class function and register a generic reset handler that will be able to call the right reset function depending on the container type. Also, let's move the registration/unregistration to a place that is not backend-specific (first vfio address space created instead of the first group). Signed-off-by: Eric Auger Signed-off-by: Yi Liu --- include/hw/vfio/vfio-container-base.h | 2 ++ hw/vfio/as.c | 18 ++++++++++++++++++ hw/vfio/container-base.c | 9 +++++++++ hw/vfio/container.c | 27 +++++++++++++++------------ 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/include/hw/vfio/vfio-container-base.h b/include/hw/vfio/vfio-c= ontainer-base.h index 5e47faf293..8ffd94f8ae 100644 --- a/include/hw/vfio/vfio-container-base.h +++ b/include/hw/vfio/vfio-container-base.h @@ -97,6 +97,7 @@ int vfio_container_dma_map(VFIOContainer *container, int vfio_container_dma_unmap(VFIOContainer *container, hwaddr iova, ram_addr_t size, IOMMUTLBEntry *iotlb); +int vfio_container_reset(VFIOContainer *container); bool vfio_container_devices_all_dirty_tracking(VFIOContainer *container); void vfio_container_set_dirty_page_tracking(VFIOContainer *container, bool start); @@ -135,6 +136,7 @@ struct VFIOIOMMUBackendOpsClass { IOMMUTLBEntry *iotlb); int (*attach_device)(VFIODevice *vbasedev, AddressSpace *as, Error **e= rrp); void (*detach_device)(VFIODevice *vbasedev); + int (*reset)(VFIOContainer *container); /* migration feature */ bool (*devices_all_dirty_tracking)(VFIOContainer *container); void (*set_dirty_page_tracking)(VFIOContainer *container, bool start); diff --git a/hw/vfio/as.c b/hw/vfio/as.c index d212974b9b..f186d0b789 100644 --- a/hw/vfio/as.c +++ b/hw/vfio/as.c @@ -823,6 +823,18 @@ const MemoryListener vfio_memory_listener =3D { .log_sync =3D vfio_listener_log_sync, }; =20 +void vfio_reset_handler(void *opaque) +{ + VFIOAddressSpace *space; + VFIOContainer *bcontainer; + + QLIST_FOREACH(space, &vfio_address_spaces, list) { + QLIST_FOREACH(bcontainer, &space->containers, next) { + vfio_container_reset(bcontainer); + } + } +} + VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) { VFIOAddressSpace *space; @@ -838,6 +850,9 @@ VFIOAddressSpace *vfio_get_address_space(AddressSpace *= as) space->as =3D as; QLIST_INIT(&space->containers); =20 + if (QLIST_EMPTY(&vfio_address_spaces)) { + qemu_register_reset(vfio_reset_handler, NULL); + } QLIST_INSERT_HEAD(&vfio_address_spaces, space, list); =20 return space; @@ -849,6 +864,9 @@ void vfio_put_address_space(VFIOAddressSpace *space) QLIST_REMOVE(space, list); g_free(space); } + if (QLIST_EMPTY(&vfio_address_spaces)) { + qemu_unregister_reset(vfio_reset_handler, NULL); + } } =20 int vfio_attach_device(VFIODevice *vbasedev, AddressSpace *as, Error **err= p) diff --git a/hw/vfio/container-base.c b/hw/vfio/container-base.c index b4ce0cd89a..3ae939c6c9 100644 --- a/hw/vfio/container-base.c +++ b/hw/vfio/container-base.c @@ -58,6 +58,15 @@ int vfio_container_dma_unmap(VFIOContainer *container, return container->ops->dma_unmap(container, iova, size, iotlb); } =20 +int vfio_container_reset(VFIOContainer *container) +{ + if (!container->ops->reset) { + return -ENOENT; + } + + return container->ops->reset(container); +} + void vfio_container_set_dirty_page_tracking(VFIOContainer *container, bool start) { diff --git a/hw/vfio/container.c b/hw/vfio/container.c index e99d12f4d8..61caf388c2 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -520,12 +520,16 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_= info *info, return true; } =20 -void vfio_reset_handler(void *opaque) +static int vfio_legacy_container_reset(VFIOContainer *bcontainer) { + VFIOLegacyContainer *container =3D container_of(bcontainer, + VFIOLegacyContainer, + bcontainer); VFIOGroup *group; VFIODevice *vbasedev; + int ret, final_ret =3D 0; =20 - QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(group, &container->group_list, container_next) { QLIST_FOREACH(vbasedev, &group->device_list, next) { if (vbasedev->dev->realized) { vbasedev->ops->vfio_compute_needs_reset(vbasedev); @@ -533,13 +537,19 @@ void vfio_reset_handler(void *opaque) } } =20 - QLIST_FOREACH(group, &vfio_group_list, next) { + QLIST_FOREACH(group, &container->group_list, next) { QLIST_FOREACH(vbasedev, &group->device_list, next) { if (vbasedev->dev->realized && vbasedev->needs_reset) { - vbasedev->ops->vfio_hot_reset_multi(vbasedev); + ret =3D vbasedev->ops->vfio_hot_reset_multi(vbasedev); + if (ret) { + error_report("failed to reset %s (%d)", + vbasedev->name, ret); + final_ret =3D ret; + } } } } + return final_ret; } =20 static void vfio_kvm_device_add_group(VFIOGroup *group) @@ -1045,10 +1055,6 @@ static VFIOGroup *vfio_get_group(int groupid, Addres= sSpace *as, Error **errp) goto close_fd_exit; } =20 - if (QLIST_EMPTY(&vfio_group_list)) { - qemu_register_reset(vfio_reset_handler, NULL); - } - QLIST_INSERT_HEAD(&vfio_group_list, group, next); =20 return group; @@ -1077,10 +1083,6 @@ static void vfio_put_group(VFIOGroup *group) trace_vfio_put_group(group->fd); close(group->fd); g_free(group); - - if (QLIST_EMPTY(&vfio_group_list)) { - qemu_unregister_reset(vfio_reset_handler, NULL); - } } =20 static int vfio_get_device(VFIOGroup *group, const char *name, @@ -1334,6 +1336,7 @@ static void vfio_iommu_backend_legacy_ops_class_init(= ObjectClass *oc, ops->check_extension =3D vfio_legacy_container_check_extension; ops->attach_device =3D vfio_legacy_attach_device; ops->detach_device =3D vfio_legacy_detach_device; + ops->reset =3D vfio_legacy_container_reset; } =20 static const TypeInfo vfio_iommu_backend_legacy_ops_type =3D { --=20 2.37.3