From patchwork Mon Sep 5 09:56:12 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 9313327 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 36F6A6075E for ; Mon, 5 Sep 2016 09:56:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2021428A1E for ; Mon, 5 Sep 2016 09:56:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 13BB328A28; Mon, 5 Sep 2016 09:56:59 +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 7D44F28A1E for ; Mon, 5 Sep 2016 09:56:58 +0000 (UTC) Received: from localhost ([::1]:53490 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bgqe9-0007cf-Lv for patchwork-qemu-devel@patchwork.kernel.org; Mon, 05 Sep 2016 05:56:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39912) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bgqdb-0007am-Dm for qemu-devel@nongnu.org; Mon, 05 Sep 2016 05:56:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bgqdW-0004VO-Aw for qemu-devel@nongnu.org; Mon, 05 Sep 2016 05:56:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53704) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bgqdW-0004VG-29 for qemu-devel@nongnu.org; Mon, 05 Sep 2016 05:56:18 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (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 9604E7F368; Mon, 5 Sep 2016 09:56:17 +0000 (UTC) Received: from [10.36.112.58] (ovpn-112-58.ams2.redhat.com [10.36.112.58]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u859uCZB007837 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 5 Sep 2016 05:56:14 -0400 To: Peter Xu References: <1473060081-17835-1-git-send-email-peterx@redhat.com> <1473060081-17835-3-git-send-email-peterx@redhat.com> <2112298c-fe2a-c74f-7a68-a92625cd3533@redhat.com> <20160905083804.GB7761@pxdev.xzpeter.org> From: Paolo Bonzini Message-ID: Date: Mon, 5 Sep 2016 11:56:12 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: <20160905083804.GB7761@pxdev.xzpeter.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 05 Sep 2016 09:56:17 +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: Re: [Qemu-devel] [PATCH 2/3] memory: add iommu_notify_flag 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: mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org, cornelia.huck@de.ibm.com, alex.williamson@redhat.com, wexu@redhat.com, vkaplans@redhat.com, dgibson@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP On 05/09/2016 10:38, Peter Xu wrote: > However in this patch I was not meant to do that. I made it an > exclusive flag to identify two different use cases. I don't know > whether this is good, but at least for Intel IOMMU's current use case, > these two types should be totally isolated from each other: > > - IOMMU_NONE notification is used by future DMAR-enabled vhost, it > should only be notified with device-IOTLB invalidations, this will > only require "Device IOTLB" capability for Intel IOMMUs, and be > notified in Device IOTLB invalidation handlers. > > - IOMMU_RW notifications should only be used for vfio-pci, notified > with IOTLB invalidations. This will only require Cache Mode (CM=1) > capability, and will be notified in common IOTLB invalidations (no > matter whether it's an cache invalidation or not, we will all use > IOMMU_RW flag for this kind of notifies). > > Maybe here naming the flags as IOMMU_{RW_NONE} is a little bit > confusing (just to leverage existing access flags), Yeah, if you really want to have these semantics, you need to define an enum like this: IOMMU_NOTIFIER_NONE = -1, IOMMU_NOTIFIER_FLUSH = 0, IOMMU_NOTIFIER_CHANGED_ENTRY = 1, But I'm still not convinced of the exclusivity between "flush" and "entry changed" notifiers. If I saw the above, my first reaction would be that you need a bit mask: IOMMU_NOTIFIER_NONE = -1, IOMMU_NOTIFIER_FLUSH = 1, IOMMU_NOTIFIER_CHANGED_ENTRY = 2, But perhaps what you're looking for is to change the "notifier" to a "listener" like struct IOMMUListener { void (*flush)(IOMMUListener *); void (*entry_changed)(IOMMUListener *, IOMMUTLBEntry *); QLIST_ENTRY(IOMMUListener) node; }; The patches can start with an IOMMUListener that only has the entry_changed callback and that replaces the current use of Notifier. Then notify_started and notify_stopped can be called on every notifier that is added/removed (see attached prototype), and the Intel IOMMU can simply reject registration of a listener that has a non-NULL iotlb_changed member. Thanks, Paolo diff --git a/hw/ppc/spapr_iommu.c b/hw/ppc/spapr_iommu.c index 6bc4d4d..d8cbd90 100644 --- a/hw/ppc/spapr_iommu.c +++ b/hw/ppc/spapr_iommu.c @@ -156,14 +156,20 @@ static uint64_t spapr_tce_get_min_page_size(MemoryRegion *iommu) return 1ULL << tcet->page_shift; } -static void spapr_tce_notify_started(MemoryRegion *iommu) +static void spapr_tce_listener_add(MemoryRegion *iommu, IOMMUListener *l) { - spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), true); + sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu); + if (tcet->users++ == 0) { + spapr_tce_set_need_vfio(tcet, true); + } } -static void spapr_tce_notify_stopped(MemoryRegion *iommu) +static void spapr_tce_listener_del(MemoryRegion *iommu, IOMMUListener *l) { - spapr_tce_set_need_vfio(container_of(iommu, sPAPRTCETable, iommu), false); + sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu); + if (--tcet->users == 0) { + spapr_tce_set_need_vfio(tcet, false); + } } static int spapr_tce_table_post_load(void *opaque, int version_id) @@ -246,8 +252,8 @@ static const VMStateDescription vmstate_spapr_tce_table = { static MemoryRegionIOMMUOps spapr_iommu_ops = { .translate = spapr_tce_translate_iommu, .get_min_page_size = spapr_tce_get_min_page_size, - .notify_started = spapr_tce_notify_started, - .notify_stopped = spapr_tce_notify_stopped, + .listener_add = spapr_tce_listener_add, + .listener_del = spapr_tce_listener_del, }; static int spapr_tce_table_realize(DeviceState *dev) diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h index caf7be9..4761afd 100644 --- a/include/hw/ppc/spapr.h +++ b/include/hw/ppc/spapr.h @@ -558,6 +558,7 @@ struct sPAPRTCETable { uint64_t *table; uint32_t mig_nb_table; uint64_t *mig_table; + int users; bool bypass; bool need_vfio; int fd; diff --git a/memory.c b/memory.c index 0eb6895..e9f50af 100644 --- a/memory.c +++ b/memory.c @@ -1515,9 +1515,8 @@ bool memory_region_is_logging(MemoryRegion *mr, uint8_t client) void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n) { - if (mr->iommu_ops->notify_started && - QLIST_EMPTY(&mr->iommu_notify.notifiers)) { - mr->iommu_ops->notify_started(mr); + if (mr->iommu_ops->listener_add) { + mr->iommu_ops->listener_add(mr, ...); } notifier_list_add(&mr->iommu_notify, n); } @@ -1555,9 +1554,8 @@ void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, bool is_write) void memory_region_unregister_iommu_notifier(MemoryRegion *mr, Notifier *n) { notifier_remove(n); - if (mr->iommu_ops->notify_stopped && - QLIST_EMPTY(&mr->iommu_notify.notifiers)) { - mr->iommu_ops->notify_stopped(mr); + if (mr->iommu_ops->listener_del) { + mr->iommu_ops->listener_del(mr, ...); } }