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: Auger Eric X-Patchwork-Id: 60453 Delivered-To: patches@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp1949013lbb; Tue, 26 Jan 2016 05:13:28 -0800 (PST) X-Received: by 10.194.80.65 with SMTP id p1mr26429525wjx.152.1453814008177; Tue, 26 Jan 2016 05:13:28 -0800 (PST) Return-Path: Received: from mail-wm0-x22b.google.com (mail-wm0-x22b.google.com. [2a00:1450:400c:c09::22b]) by mx.google.com with ESMTPS id h68si5431542wmi.58.2016.01.26.05.13.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Jan 2016 05:13:28 -0800 (PST) Received-SPF: pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22b as permitted sender) client-ip=2a00:1450:400c:c09::22b; Authentication-Results: mx.google.com; spf=pass (google.com: domain of eric.auger@linaro.org designates 2a00:1450:400c:c09::22b as permitted sender) smtp.mailfrom=eric.auger@linaro.org; dkim=pass header.i=@linaro.org Received: by mail-wm0-x22b.google.com with SMTP id u188so105676887wmu.1 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=MfrG2GEf8yUDUdIXmSFAw1WCmY/eI+g7enKwqP+COcOzbJVzubBvAU/kwS2FiJR0Q6 0+to2WW8lFy7AnA+MZtrvShJWRyOOtGrzIpsQ/xjgCMkoOk8bJcSzWhY/bUpnbONDdis vZYSGabYMqlcbB1tv3fcd/FezI/ie8ZcIW++W2+GqWxGaBIAl8c9l+mJnB89wvrPhmaL +619GtrmtBxn1043ixBoc8Lxetu65scrNPwk97x6bomM3tuh2QgkM3ibqOB3dc/E1xvE 3l8W7QFBKqetJSM8sAmmh/h6oGgYwVAW+ugl1+KjtWo9vIYqFdUxjKGrCJKnMe4q2oIY 2pTw== X-Gm-Message-State: AG10YOSwM52Osr3JQNDawrhN5c62xv4kEldUufi9gIIH/hnjZ/FN0oy4pO/PI0QoFMe++H2Q504= X-Received: by 10.28.4.145 with SMTP id 139mr25313098wme.56.1453814007972; Tue, 26 Jan 2016 05:13:27 -0800 (PST) Return-Path: 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> 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(+) -- 1.9.1 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;