From patchwork Tue Nov 15 00:59:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 9428711 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 D8ED460484 for ; Tue, 15 Nov 2016 01:00:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE04A28B30 for ; Tue, 15 Nov 2016 01:00:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BF7D628B33; Tue, 15 Nov 2016 01:00:03 +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.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI 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 8E8B028B30 for ; Tue, 15 Nov 2016 01:00:01 +0000 (UTC) Received: from localhost ([::1]:43433 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6S6R-0007uL-7X for patchwork-qemu-devel@patchwork.kernel.org; Mon, 14 Nov 2016 19:59:59 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46443) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6S69-0007uF-0T for qemu-devel@nongnu.org; Mon, 14 Nov 2016 19:59:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6S65-0001gg-SR for qemu-devel@nongnu.org; Mon, 14 Nov 2016 19:59:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51378) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1c6S65-0001gU-KV for qemu-devel@nongnu.org; Mon, 14 Nov 2016 19:59:37 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0F42EC04D292; Tue, 15 Nov 2016 00:59:36 +0000 (UTC) Received: from pxdev.xzpeter.org.com (vpn-63-110.rdu2.redhat.com [10.10.63.110]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uAF0xUvT009642; Mon, 14 Nov 2016 19:59:31 -0500 From: Peter Xu To: qemu-devel@nongnu.org Date: Mon, 14 Nov 2016 19:59:28 -0500 Message-Id: <1479171568-19175-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 15 Nov 2016 00:59:36 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] vfio: avoid adding same iommu mr for notify 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: alex.williamson@redhat.com, bd.aviv@gmail.com, peterx@redhat.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP When one IOMMU memory region is splitted into multiple memory sections, vfio will register multiple same notifiers to a vIOMMU for the same region. That's not sensible. What we need is to register one IOMMU notifier for each IOMMU region, not per section. Solution is simple - we traverse the container->giommu_list, and skip the registration if memory region is already registered. To make vfio's region_add() short, vfio_listener_region_add_iommu() is introduced. Signed-off-by: Peter Xu --- hw/vfio/common.c | 56 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 801578b..5279fd1 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -360,6 +360,40 @@ out: rcu_read_unlock(); } +static void vfio_listener_region_add_iommu(VFIOContainer *container, + MemoryRegionSection *section, + hwaddr iova, + hwaddr end) +{ + VFIOGuestIOMMU *giommu; + + QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { + if (giommu->iommu == section->mr) { + /* We have already registered with this MR, skip */ + return; + } + } + + trace_vfio_listener_region_add_iommu(iova, end); + + /* + * FIXME: For VFIO iommu types which have KVM acceleration to + * avoid bouncing all map/unmaps through qemu this way, this + * would be the right place to wire that up (tell the KVM + * device emulation the VFIO iommu handles to use). + */ + giommu = g_malloc0(sizeof(*giommu)); + giommu->iommu = section->mr; + giommu->iommu_offset = section->offset_within_address_space - + section->offset_within_region; + giommu->container = container; + giommu->n.notify = vfio_iommu_map_notify; + giommu->n.notifier_flags = IOMMU_NOTIFIER_ALL; + QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); + memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); + memory_region_iommu_replay(giommu->iommu, &giommu->n, false); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -439,27 +473,7 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { - VFIOGuestIOMMU *giommu; - - trace_vfio_listener_region_add_iommu(iova, end); - /* - * FIXME: For VFIO iommu types which have KVM acceleration to - * avoid bouncing all map/unmaps through qemu this way, this - * would be the right place to wire that up (tell the KVM - * device emulation the VFIO iommu handles to use). - */ - giommu = g_malloc0(sizeof(*giommu)); - giommu->iommu = section->mr; - giommu->iommu_offset = section->offset_within_address_space - - section->offset_within_region; - giommu->container = container; - giommu->n.notify = vfio_iommu_map_notify; - giommu->n.notifier_flags = IOMMU_NOTIFIER_ALL; - QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - - memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); - memory_region_iommu_replay(giommu->iommu, &giommu->n, false); - + vfio_listener_region_add_iommu(container, section, iova, end); return; }