From patchwork Fri Feb 24 11:13:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 94428 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp640073qgi; Fri, 24 Feb 2017 03:14:22 -0800 (PST) X-Received: by 10.99.189.9 with SMTP id a9mr2724566pgf.190.1487934862080; Fri, 24 Feb 2017 03:14:22 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si7093880pgo.251.2017.02.24.03.14.21; Fri, 24 Feb 2017 03:14:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751414AbdBXLOF (ORCPT + 25 others); Fri, 24 Feb 2017 06:14:05 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:13587 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751386AbdBXLNu (ORCPT ); Fri, 24 Feb 2017 06:13:50 -0500 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OLV00KDELUZY280@mailout2.w1.samsung.com> for linux-kernel@vger.kernel.org; Fri, 24 Feb 2017 11:13:47 +0000 (GMT) Received: from eusmges1.samsung.com (unknown [203.254.199.239]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20170224111346eucas1p1bf98662985680c4854ed58488faf172d~mNNdxmcXW2353423534eucas1p1W; Fri, 24 Feb 2017 11:13:46 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1.samsung.com (EUCPMTA) with SMTP id DF.DB.16908.D6510B85; Fri, 24 Feb 2017 11:13:49 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170224111346eucas1p2039e4d85e34f2a4cdd3adc298e2a1b6f~mNNdJHQBn0675806758eucas1p2Y; Fri, 24 Feb 2017 11:13:46 +0000 (GMT) X-AuditID: cbfec7ef-f79d26d00000420c-75-58b0156d67ce Received: from eusync2.samsung.com ( [203.254.199.212]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 1D.6C.06687.1C510B85; Fri, 24 Feb 2017 11:15:13 +0000 (GMT) Received: from AMDC2765.digital.local ([106.116.147.25]) by eusync2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OLV000MYLUUR200@eusync2.samsung.com>; Fri, 24 Feb 2017 11:13:46 +0000 (GMT) From: Marek Szyprowski To: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Joerg Roedel , Bartlomiej Zolnierkiewicz , Robin Murphy Subject: [PATCH v2] iommu: iova: Consolidate code for adding new node to iovad domain rbtree Date: Fri, 24 Feb 2017 12:13:37 +0100 Message-id: <1487934817-11725-1-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrKIsWRmVeSWpSXmKPExsWy7djPc7q5ohsiDFb/FLbYOGM9q8WC/dYW nbM3sFtc3jWHzWLtkbvsFgc/PGF1YPN4cnAek8eaeWsYPSbfWM7o0bdlFaPH501yAaxRXDYp qTmZZalF+nYJXBmr+g+xF5xWrjjU8I+1gfGITBcjJ4eEgInE6SubGSFsMYkL99azdTFycQgJ LGOUuNb0lxHC+cwocbtzNTtMx6srO1nhqpo+nYFyGpgkTk98xwpSxSZgKNH1tgtoFge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GCQ5mJRHecvYNEUK8KYmVValF+fFF pTmpxYcYTYGWT2SWEk3OB8ZKXkm8oYmhuaWhkbGFhbmRkZI4b8mHK+FCAumJJanZqakFqUUw fUwcnFINjDFPfX+uMeG81fKNtdL/aHX9VK36HVeX7rovv2BC/eGiica74/QZTj62zpjg0/Sw sVXX+DDTK9EjkVrWS4vFO7qY3r4zsPnkHfM1aPHZLfuO3/D+nbSyav6523N2d9lL3vkkHvG5 8rbJOskc9sYjjmdF7HkMfHk3tl2taRfSW6by/JR7flEQgxJLcUaioRZzUXEiAHSbVpJdAgAA X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170224111346eucas1p2039e4d85e34f2a4cdd3adc298e2a1b6f X-Msg-Generator: CA X-Sender-IP: 182.198.249.179 X-Local-Sender: =?utf-8?q?Marek_Szyprowski=1BSRPOL-Kernel_=28TP=29=1B?= =?utf-8?b?7IK87ISx7KCE7J6QG1NlbmlvciBTb2Z0d2FyZSBFbmdpbmVlcg==?= X-Global-Sender: =?utf-8?q?Marek_Szyprowski=1BSRPOL-Kernel_=28TP=29=1BSam?= =?utf-8?q?sung_Electronics=1BSenior_Software_Engineer?= X-Sender-Code: =?utf-8?q?C10=1BEHQ=1BC10CD02CD027392?= CMS-TYPE: 201P X-HopCount: 7 X-CMS-RootMailID: 20170224111346eucas1p2039e4d85e34f2a4cdd3adc298e2a1b6f X-RootMTR: 20170224111346eucas1p2039e4d85e34f2a4cdd3adc298e2a1b6f References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch consolidates almost the same code used in iova_insert_rbtree() and __alloc_and_insert_iova_range() functions. While touching this code, replace BUG() with WARN_ON(1) to avoid taking down the whole system in case of corrupted iova tree or incorrect calls. Signed-off-by: Marek Szyprowski --- v2: - replaced BUG() with WARN_ON(1) as suggested by Robin Murphy v1: - initial version --- drivers/iommu/iova.c | 87 ++++++++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 54 deletions(-) -- 1.9.1 Tested-by: Robin Murphy Reviewed-by: Robin Murphy diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index b7268a14184f..e80a4105ac2a 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -100,6 +100,34 @@ static unsigned long iova_rcache_get(struct iova_domain *iovad, } } +/* Insert the iova into domain rbtree by holding writer lock */ +static void +iova_insert_rbtree(struct rb_root *root, struct iova *iova, + struct rb_node *start) +{ + struct rb_node **new, *parent = NULL; + + new = (start) ? &start : &(root->rb_node); + /* Figure out where to put new node */ + while (*new) { + struct iova *this = rb_entry(*new, struct iova, node); + + parent = *new; + + if (iova->pfn_lo < this->pfn_lo) + new = &((*new)->rb_left); + else if (iova->pfn_lo > this->pfn_lo) + new = &((*new)->rb_right); + else { + WARN_ON(1); /* this should not happen */ + return; + } + } + /* Add new node and rebalance tree. */ + rb_link_node(&iova->node, parent, new); + rb_insert_color(&iova->node, root); +} + /* * Computes the padding size required, to make the start address * naturally aligned on the power-of-two order of its size @@ -157,35 +185,8 @@ static int __alloc_and_insert_iova_range(struct iova_domain *iovad, new->pfn_lo = limit_pfn - (size + pad_size) + 1; new->pfn_hi = new->pfn_lo + size - 1; - /* Insert the new_iova into domain rbtree by holding writer lock */ - /* Add new node and rebalance tree. */ - { - struct rb_node **entry, *parent = NULL; - - /* If we have 'prev', it's a valid place to start the - insertion. Otherwise, start from the root. */ - if (prev) - entry = &prev; - else - entry = &iovad->rbroot.rb_node; - - /* Figure out where to put new node */ - while (*entry) { - struct iova *this = rb_entry(*entry, struct iova, node); - parent = *entry; - - if (new->pfn_lo < this->pfn_lo) - entry = &((*entry)->rb_left); - else if (new->pfn_lo > this->pfn_lo) - entry = &((*entry)->rb_right); - else - BUG(); /* this should not happen */ - } - - /* Add new node and rebalance tree. */ - rb_link_node(&new->node, parent, entry); - rb_insert_color(&new->node, &iovad->rbroot); - } + /* If we have 'prev', it's a valid place to start the insertion. */ + iova_insert_rbtree(&iovad->rbroot, new, prev); __cached_rbnode_insert_update(iovad, saved_pfn, new); spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); @@ -194,28 +195,6 @@ static int __alloc_and_insert_iova_range(struct iova_domain *iovad, return 0; } -static void -iova_insert_rbtree(struct rb_root *root, struct iova *iova) -{ - struct rb_node **new = &(root->rb_node), *parent = NULL; - /* Figure out where to put new node */ - while (*new) { - struct iova *this = rb_entry(*new, struct iova, node); - - parent = *new; - - if (iova->pfn_lo < this->pfn_lo) - new = &((*new)->rb_left); - else if (iova->pfn_lo > this->pfn_lo) - new = &((*new)->rb_right); - else - BUG(); /* this should not happen */ - } - /* Add new node and rebalance tree. */ - rb_link_node(&iova->node, parent, new); - rb_insert_color(&iova->node, root); -} - static struct kmem_cache *iova_cache; static unsigned int iova_cache_users; static DEFINE_MUTEX(iova_cache_mutex); @@ -505,7 +484,7 @@ void put_iova_domain(struct iova_domain *iovad) iova = alloc_and_init_iova(pfn_lo, pfn_hi); if (iova) - iova_insert_rbtree(&iovad->rbroot, iova); + iova_insert_rbtree(&iovad->rbroot, iova, NULL); return iova; } @@ -612,11 +591,11 @@ struct iova * rb_erase(&iova->node, &iovad->rbroot); if (prev) { - iova_insert_rbtree(&iovad->rbroot, prev); + iova_insert_rbtree(&iovad->rbroot, prev, NULL); iova->pfn_lo = pfn_lo; } if (next) { - iova_insert_rbtree(&iovad->rbroot, next); + iova_insert_rbtree(&iovad->rbroot, next, NULL); iova->pfn_hi = pfn_hi; } spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);