From nobody Fri Sep 5 22:27:10 2025 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1658852816; cv=none; d=zohomail.com; s=zohoarc; b=QVoocyD995zTpnXZ5gRNnfj80UbEXwc4GNB/VA75BrZvXVkKq4vBi3MhLFHqKBqSmvha9keaRFkkb7XeThQVT1teDq0aADhCsnX4hquMW+YRrx2llaQkTDuOd+eiswOKcZDOtBXwlO7qp61qUjuGOlSOuM+/uSORId16Au62GQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1658852816; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=PSJWekmjFYoSMJvI8bg2b39oWZpHq6LD9ykDjH4iW9k=; b=kqfumTW7RspGpCinISfqNTiZBJSEXTg1PF82cKPWc7WgZyNICKxqoQFtpl365GcY/BRuKT2m5YpTClevh6/PoSP/WUrKno7Lb78lfGgcnfee11RQKiY056JWt02/MGBMOy2AklDjYtiKBJd6wB16fwwkrVycrWk9ZpmlWeI1yrk= 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+importer=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 1658852816433582.7738248421417; Tue, 26 Jul 2022 09:26:56 -0700 (PDT) Received: from localhost ([::1]:46464 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oGNOZ-0007Js-BI for importer@patchew.org; Tue, 26 Jul 2022 12:26:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34956) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGN9p-0002ya-QT for qemu-devel@nongnu.org; Tue, 26 Jul 2022 12:11:42 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:35254) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGN9n-0006C9-7w for qemu-devel@nongnu.org; Tue, 26 Jul 2022 12:11:41 -0400 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 26QFnE4N024578; Tue, 26 Jul 2022 16:11:30 GMT Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3hg9hsq2na-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 26 Jul 2022 16:11:30 +0000 Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 26QFH3W1006242; Tue, 26 Jul 2022 16:11:29 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3hh65bq2bj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 26 Jul 2022 16:11:29 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 26QG5uT9023334; Tue, 26 Jul 2022 16:11:28 GMT Received: from ca-dev63.us.oracle.com (ca-dev63.us.oracle.com [10.211.8.221]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 3hh65bq0y5-31; Tue, 26 Jul 2022 16:11:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2022-7-12; bh=PSJWekmjFYoSMJvI8bg2b39oWZpHq6LD9ykDjH4iW9k=; b=Ta96dOcvyzrWoV+9ISnKa6pksMtzqQUeLov+Kxd1KZ1uwfdXq5sgNtfshfraRbXx10At tRZUaH/EFML07tfUUMDlIzddXaFR0z+3MjOrIUtoKVRfo0Hak71j75xi9sruhhjZwm2v E636CmNkZxtyqlSuOdnbZ0BQBudTvM8WJ0gqlZie6M+EelW0PcQIs1rFF51ppvtTpwHc Y00fTnzwl0oqvogEpmKrWG2aZteX/hoMfoJABeV/DIye2Dj22YE0jBKMZIJVxOZmaFgf Vusg5o2XruUqmjBg3N/RlOyL29qo2yWTgdv0+d9275Hs8/8fyx8d+Y2PalOLSlVY37WD mQ== From: Steve Sistare To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Alex=20Benn=C3=A9e?= , "Dr. David Alan Gilbert" , "Michael S. Tsirkin" , Marcel Apfelbaum , Alex Williamson , "Daniel P. Berrange" , Juan Quintela , Markus Armbruster , Eric Blake , Jason Zeng , Zheng Chuan , Steve Sistare , Mark Kanda , Guoyi Tu , Peter Maydell , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Igor Mammedov , David Hildenbrand , John Snow , Peng Liang Subject: [PATCH V9 30/46] vfio-pci: refactor for cpr Date: Tue, 26 Jul 2022 09:10:27 -0700 Message-Id: <1658851843-236870-31-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1658851843-236870-1-git-send-email-steven.sistare@oracle.com> References: <1658851843-236870-1-git-send-email-steven.sistare@oracle.com> X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-07-26_04,2022-07-26_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 phishscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2206140000 definitions=main-2207260063 X-Proofpoint-ORIG-GUID: xd0fFihzX6nRKpkWnUlkATcKT-M4XKSe X-Proofpoint-GUID: xd0fFihzX6nRKpkWnUlkATcKT-M4XKSe 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.220.165.32; envelope-from=steven.sistare@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @oracle.com) X-ZM-MESSAGEID: 1658852817396100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Refactor vector use into a helper vfio_vector_init. Add vfio_notifier_init and vfio_notifier_cleanup for named notifiers, and pass additional arguments to vfio_remove_kvm_msi_virq. All for use by cpr in a subsequent patch. No functional change. Signed-off-by: Steve Sistare --- hw/vfio/pci.c | 99 +++++++++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 66 insertions(+), 33 deletions(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 939dcc3..0143c9a 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -52,6 +52,27 @@ static void vfio_disable_interrupts(VFIOPCIDevice *vdev); static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); static void vfio_msi_disable_common(VFIOPCIDevice *vdev); =20 +/* Create new or reuse existing eventfd */ +static int vfio_notifier_init(VFIOPCIDevice *vdev, EventNotifier *e, + const char *name, int nr) +{ + int fd =3D -1; /* placeholder until a subsequent patch */ + int ret =3D 0; + + if (fd >=3D 0) { + event_notifier_init_fd(e, fd); + } else { + ret =3D event_notifier_init(e, 0); + } + return ret; +} + +static void vfio_notifier_cleanup(VFIOPCIDevice *vdev, EventNotifier *e, + const char *name, int nr) +{ + event_notifier_cleanup(e); +} + /* * Disabling BAR mmaping can be slow, but toggling it around INTx can * also be a huge overhead. We try to get the best of both worlds by @@ -132,8 +153,8 @@ static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, E= rror **errp) pci_irq_deassert(&vdev->pdev); =20 /* Get an eventfd for resample/unmask */ - if (event_notifier_init(&vdev->intx.unmask, 0)) { - error_setg(errp, "event_notifier_init failed eoi"); + if (vfio_notifier_init(vdev, &vdev->intx.unmask, "intx-unmask", 0)) { + error_setg(errp, "vfio_notifier_init intx-unmask failed"); goto fail; } =20 @@ -165,7 +186,7 @@ fail_vfio: kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vdev->intx.interrupt, vdev->intx.route.irq); fail_irqfd: - event_notifier_cleanup(&vdev->intx.unmask); + vfio_notifier_cleanup(vdev, &vdev->intx.unmask, "intx-unmask", 0); fail: qemu_set_fd_handler(irq_fd, vfio_intx_interrupt, NULL, vdev); vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX); @@ -194,7 +215,7 @@ static void vfio_intx_disable_kvm(VFIOPCIDevice *vdev) } =20 /* We only need to close the eventfd for VFIO to cleanup the kernel si= de */ - event_notifier_cleanup(&vdev->intx.unmask); + vfio_notifier_cleanup(vdev, &vdev->intx.unmask, "intx-unmask", 0); =20 /* QEMU starts listening for interrupt events. */ qemu_set_fd_handler(event_notifier_get_fd(&vdev->intx.interrupt), @@ -285,9 +306,10 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error= **errp) } #endif =20 - ret =3D event_notifier_init(&vdev->intx.interrupt, 0); + ret =3D vfio_notifier_init(vdev, &vdev->intx.interrupt, "intx-interrup= t", 0); if (ret) { - error_setg_errno(errp, -ret, "event_notifier_init failed"); + error_setg_errno(errp, -ret, + "vfio_notifier_init intx-interrupt failed"); return ret; } fd =3D event_notifier_get_fd(&vdev->intx.interrupt); @@ -296,7 +318,7 @@ static int vfio_intx_enable(VFIOPCIDevice *vdev, Error = **errp) if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0, VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) { qemu_set_fd_handler(fd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->intx.interrupt); + vfio_notifier_cleanup(vdev, &vdev->intx.interrupt, "intx-interrupt= ", 0); return -errno; } =20 @@ -324,7 +346,7 @@ static void vfio_intx_disable(VFIOPCIDevice *vdev) =20 fd =3D event_notifier_get_fd(&vdev->intx.interrupt); qemu_set_fd_handler(fd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->intx.interrupt); + vfio_notifier_cleanup(vdev, &vdev->intx.interrupt, "intx-interrupt", 0= ); =20 vdev->interrupt =3D VFIO_INT_NONE; =20 @@ -424,13 +446,15 @@ static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev= , VFIOMSIVector *vector, vector_n, &vdev->pdev); } =20 -static void vfio_connect_kvm_msi_virq(VFIOMSIVector *vector) +static void vfio_connect_kvm_msi_virq(VFIOMSIVector *vector, int nr) { + const char *name =3D "kvm_interrupt"; + if (vector->virq < 0) { return; } =20 - if (event_notifier_init(&vector->kvm_interrupt, 0)) { + if (vfio_notifier_init(vector->vdev, &vector->kvm_interrupt, name, nr)= ) { goto fail_notifier; } =20 @@ -442,19 +466,20 @@ static void vfio_connect_kvm_msi_virq(VFIOMSIVector *= vector) return; =20 fail_kvm: - event_notifier_cleanup(&vector->kvm_interrupt); + vfio_notifier_cleanup(vector->vdev, &vector->kvm_interrupt, name, nr); fail_notifier: kvm_irqchip_release_virq(kvm_state, vector->virq); vector->virq =3D -1; } =20 -static void vfio_remove_kvm_msi_virq(VFIOMSIVector *vector) +static void vfio_remove_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *v= ector, + int nr) { kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vector->kvm_interrup= t, vector->virq); kvm_irqchip_release_virq(kvm_state, vector->virq); vector->virq =3D -1; - event_notifier_cleanup(&vector->kvm_interrupt); + vfio_notifier_cleanup(vdev, &vector->kvm_interrupt, "kvm_interrupt", n= r); } =20 static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg, @@ -464,6 +489,20 @@ static void vfio_update_kvm_msi_virq(VFIOMSIVector *ve= ctor, MSIMessage msg, kvm_irqchip_commit_routes(kvm_state); } =20 +static void vfio_vector_init(VFIOPCIDevice *vdev, int nr) +{ + VFIOMSIVector *vector =3D &vdev->msi_vectors[nr]; + PCIDevice *pdev =3D &vdev->pdev; + + vector->vdev =3D vdev; + vector->virq =3D -1; + if (vfio_notifier_init(vdev, &vector->interrupt, "interrupt", nr)) { + error_report("vfio: vfio_notifier_init interrupt failed"); + } + vector->use =3D true; + msix_vector_use(pdev, nr); +} + static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, MSIMessage *msg, IOHandler *handler) { @@ -476,13 +515,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, un= signed int nr, vector =3D &vdev->msi_vectors[nr]; =20 if (!vector->use) { - vector->vdev =3D vdev; - vector->virq =3D -1; - if (event_notifier_init(&vector->interrupt, 0)) { - error_report("vfio: Error: event_notifier_init failed"); - } - vector->use =3D true; - msix_vector_use(pdev, nr); + vfio_vector_init(vdev, nr); } =20 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt), @@ -494,7 +527,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, uns= igned int nr, */ if (vector->virq >=3D 0) { if (!msg) { - vfio_remove_kvm_msi_virq(vector); + vfio_remove_kvm_msi_virq(vdev, vector, nr); } else { vfio_update_kvm_msi_virq(vector, *msg, pdev); } @@ -506,7 +539,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, uns= igned int nr, vfio_route_change =3D kvm_irqchip_begin_route_changes(kvm_= state); vfio_add_kvm_msi_virq(vdev, vector, nr, true); kvm_irqchip_commit_route_changes(&vfio_route_change); - vfio_connect_kvm_msi_virq(vector); + vfio_connect_kvm_msi_virq(vector, nr); } } } @@ -602,7 +635,7 @@ static void vfio_commit_kvm_msi_virq_batch(VFIOPCIDevic= e *vdev) kvm_irqchip_commit_route_changes(&vfio_route_change); =20 for (i =3D 0; i < vdev->nr_vectors; i++) { - vfio_connect_kvm_msi_virq(&vdev->msi_vectors[i]); + vfio_connect_kvm_msi_virq(&vdev->msi_vectors[i], i); } } =20 @@ -681,8 +714,8 @@ retry: vector->virq =3D -1; vector->use =3D true; =20 - if (event_notifier_init(&vector->interrupt, 0)) { - error_report("vfio: Error: event_notifier_init failed"); + if (vfio_notifier_init(vdev, &vector->interrupt, "interrupt", i)) { + error_report("vfio: Error: vfio_notifier_init failed"); } =20 qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt), @@ -737,11 +770,11 @@ static void vfio_msi_disable_common(VFIOPCIDevice *vd= ev) VFIOMSIVector *vector =3D &vdev->msi_vectors[i]; if (vdev->msi_vectors[i].use) { if (vector->virq >=3D 0) { - vfio_remove_kvm_msi_virq(vector); + vfio_remove_kvm_msi_virq(vdev, vector, i); } qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt), NULL, NULL, NULL); - event_notifier_cleanup(&vector->interrupt); + vfio_notifier_cleanup(vdev, &vector->interrupt, "interrupt", i= ); } } =20 @@ -2740,7 +2773,7 @@ static void vfio_register_err_notifier(VFIOPCIDevice = *vdev) return; } =20 - if (event_notifier_init(&vdev->err_notifier, 0)) { + if (vfio_notifier_init(vdev, &vdev->err_notifier, "err_notifier", 0)) { error_report("vfio: Unable to init event notifier for error detect= ion"); vdev->pci_aer =3D false; return; @@ -2753,7 +2786,7 @@ static void vfio_register_err_notifier(VFIOPCIDevice = *vdev) VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); qemu_set_fd_handler(fd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->err_notifier); + vfio_notifier_cleanup(vdev, &vdev->err_notifier, "err_notifier", 0= ); vdev->pci_aer =3D false; } } @@ -2772,7 +2805,7 @@ static void vfio_unregister_err_notifier(VFIOPCIDevic= e *vdev) } qemu_set_fd_handler(event_notifier_get_fd(&vdev->err_notifier), NULL, NULL, vdev); - event_notifier_cleanup(&vdev->err_notifier); + vfio_notifier_cleanup(vdev, &vdev->err_notifier, "err_notifier", 0); } =20 static void vfio_req_notifier_handler(void *opaque) @@ -2806,7 +2839,7 @@ static void vfio_register_req_notifier(VFIOPCIDevice = *vdev) return; } =20 - if (event_notifier_init(&vdev->req_notifier, 0)) { + if (vfio_notifier_init(vdev, &vdev->req_notifier, "req_notifier", 0)) { error_report("vfio: Unable to init event notifier for device reque= st"); return; } @@ -2818,7 +2851,7 @@ static void vfio_register_req_notifier(VFIOPCIDevice = *vdev) VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) { error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); qemu_set_fd_handler(fd, NULL, NULL, vdev); - event_notifier_cleanup(&vdev->req_notifier); + vfio_notifier_cleanup(vdev, &vdev->req_notifier, "req_notifier", 0= ); } else { vdev->req_enabled =3D true; } @@ -2838,7 +2871,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevic= e *vdev) } qemu_set_fd_handler(event_notifier_get_fd(&vdev->req_notifier), NULL, NULL, vdev); - event_notifier_cleanup(&vdev->req_notifier); + vfio_notifier_cleanup(vdev, &vdev->req_notifier, "req_notifier", 0); =20 vdev->req_enabled =3D false; } --=20 1.8.3.1