From patchwork Tue Jan 26 13:12:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 8121561 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 66383BEEE5 for ; Tue, 26 Jan 2016 13:13:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7027020114 for ; Tue, 26 Jan 2016 13:13:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 703752026D for ; Tue, 26 Jan 2016 13:13:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S964824AbcAZNNd (ORCPT ); Tue, 26 Jan 2016 08:13:33 -0500 Received: from mail-wm0-f53.google.com ([74.125.82.53]:33505 "EHLO mail-wm0-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965868AbcAZNN3 (ORCPT ); Tue, 26 Jan 2016 08:13:29 -0500 Received: by mail-wm0-f53.google.com with SMTP id 123so105331558wmz.0 for ; Tue, 26 Jan 2016 05:13:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aJHt8JZ4DtqzokDAYoZRiD477A/U1UznFlMPZrsNgg4=; b=G8hBPxZYuXuqRGSf9Ua0g3H/BAdMQtEKCMy3gTB8lh6fRJe38r0/aRDv/yQCTPmaP2 DZLnRPOdHhGSpJzRUihQwEHpnK1JwClyuophvxS96jq8slAu+snWhkEW7p5mnvYv/dMA hm4u8HPqik5ZnUxvFqupla70Y96MYo4lT3F2Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=aJHt8JZ4DtqzokDAYoZRiD477A/U1UznFlMPZrsNgg4=; b=I9sngG4H+p9Em1Ap4JyVx+CQ6GSCL6AIHZrZBEuse9852rYnNPVHUPhXaDPDos23Al mnz7vaWk+Ytf2ULBetoVVPpbS0EUEgIX9taFmxNcd+ZoVqLmod0CUyNsIYR6j75HGeGo vSBJg/itpj99S9oKiFnA9vK7CSN5ky6EAQ5nO4PrAlV6Q37p/qWLJPD8SABnBeLx01pc 14a3R9wbwODf3iv4mWX+cM2Y2um2x8OSldPqUGcocu4IYR1U9/9NwbmHN1SJiFDwIqML Kz4rw2Es4jYBUkALsssHL78W0v5f2KIHpoXaLePUTIbKBxfyobxN6juYFkSguL40wqQr HTDQ== X-Gm-Message-State: AG10YORJ3L5PfpcJGQAq69UaAAZTWgIXxZUxwjmrq1xX51FpD4Ar1KTTri5BfKoFN534Oxr0 X-Received: by 10.28.4.145 with SMTP id 139mr25313098wme.56.1453814007972; Tue, 26 Jan 2016 05:13:27 -0800 (PST) Received: from localhost.localdomain (LMontsouris-657-1-37-90.w80-11.abo.wanadoo.fr. [80.11.198.90]) by smtp.gmail.com with ESMTPSA id ct2sm1388885wjb.46.2016.01.26.05.13.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 05:13:26 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, alex.williamson@redhat.com, will.deacon@arm.com, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Cc: Bharat.Bhushan@freescale.com, pranav.sawargaonkar@gmail.com, p.fedin@samsung.com, suravee.suthikulpanit@amd.com, linux-kernel@vger.kernel.org, patches@linaro.org, iommu@lists.linux-foundation.org Subject: [PATCH 03/10] vfio_iommu_type1: add reserved binding RB tree management Date: Tue, 26 Jan 2016 13:12:41 +0000 Message-Id: <1453813968-2024-4-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453813968-2024-1-git-send-email-eric.auger@linaro.org> References: <1453813968-2024-1-git-send-email-eric.auger@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Legacy dma_list is just used to insert the reserved iova region and check mapping of reserved iova happens in this window. As opposed to other vfio_dma slots, the reserved one is not necessarily mapped. We will need to track which host physical addresses are mapped to reserved IOVA. In that prospect we introduce a new RB tree indexed by physical address. This reverse RB tree only is used for reserved IOVA bindings. It belongs to a given iommu domain. It is expected this RB tree will contain very few bindings. Those generally correspond to single page mapping one MSI frame (at least for ARM, containing the GICv2m frame or ITS GITS_TRANSLATER frame. Signed-off-by: Eric Auger --- drivers/vfio/vfio_iommu_type1.c | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index c5b57e1..32438d9 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -56,6 +56,7 @@ MODULE_PARM_DESC(disable_hugepages, struct vfio_iommu { struct list_head domain_list; struct mutex lock; + /* rb tree indexed by IOVA */ struct rb_root dma_list; bool v2; bool nesting; @@ -65,6 +66,8 @@ struct vfio_domain { struct iommu_domain *domain; struct list_head next; struct list_head group_list; + /* rb tree indexed by PA, for reserved bindings only */ + struct rb_root reserved_binding_list; int prot; /* IOMMU_CACHE */ bool fgsp; /* Fine-grained super pages */ }; @@ -77,11 +80,70 @@ struct vfio_dma { int prot; /* IOMMU_READ/WRITE */ }; +struct vfio_reserved_binding { + struct kref kref; + struct rb_node node; + struct vfio_domain *domain; + phys_addr_t addr; + dma_addr_t iova; + size_t size; +}; + struct vfio_group { struct iommu_group *iommu_group; struct list_head next; }; +/* Reserved binding RB-tree manipulation */ + +static struct vfio_reserved_binding *vfio_find_reserved_binding( + struct vfio_domain *d, + phys_addr_t start, size_t size) +{ + struct rb_node *node = d->reserved_binding_list.rb_node; + + while (node) { + struct vfio_reserved_binding *binding = + rb_entry(node, struct vfio_reserved_binding, node); + + if (start + size <= binding->addr) + node = node->rb_left; + else if (start >= binding->addr + binding->size) + node = node->rb_right; + else + return binding; + } + + return NULL; +} + +static void vfio_link_reserved_binding(struct vfio_domain *d, + struct vfio_reserved_binding *new) +{ + struct rb_node **link = &d->reserved_binding_list.rb_node; + struct rb_node *parent = NULL; + struct vfio_reserved_binding *binding; + + while (*link) { + parent = *link; + binding = rb_entry(parent, struct vfio_reserved_binding, node); + + if (new->addr + new->size <= binding->addr) + link = &(*link)->rb_left; + else + link = &(*link)->rb_right; + } + + rb_link_node(&new->node, parent, link); + rb_insert_color(&new->node, &d->reserved_binding_list); +} + +static void vfio_unlink_reserved_binding(struct vfio_domain *d, + struct vfio_reserved_binding *old) +{ + rb_erase(&old->node, &d->reserved_binding_list); +} + /* * This code handles mapping and unmapping of user data buffers * into DMA'ble space using the IOMMU @@ -784,6 +846,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, ret = -ENOMEM; goto out_free; } + domain->reserved_binding_list = RB_ROOT; group->iommu_group = iommu_group;