From patchwork Thu Feb 23 08:17:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 94368 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp111443qgi; Thu, 23 Feb 2017 00:17:57 -0800 (PST) X-Received: by 10.98.6.133 with SMTP id 127mr9157283pfg.110.1487837877102; Thu, 23 Feb 2017 00:17:57 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si3653822pfc.225.2017.02.23.00.17.56; Thu, 23 Feb 2017 00:17:57 -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 S1751420AbdBWIRz (ORCPT + 25 others); Thu, 23 Feb 2017 03:17:55 -0500 Received: from mailout4.w1.samsung.com ([210.118.77.14]:23477 "EHLO mailout4.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751218AbdBWIRx (ORCPT ); Thu, 23 Feb 2017 03:17:53 -0500 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTP id <0OLT00MSZJ1LY340@mailout4.w1.samsung.com> for linux-kernel@vger.kernel.org; Thu, 23 Feb 2017 08:17:45 +0000 (GMT) Received: from eusmges1.samsung.com (unknown [203.254.199.239]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170223081744eucas1p224238590968897b45c09f902b66f085a~l3KekUWiy1104911049eucas1p2g; Thu, 23 Feb 2017 08:17:44 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1.samsung.com (EUCPMTA) with SMTP id C7.96.16908.AAA9EA85; Thu, 23 Feb 2017 08:17:46 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20170223081743eucas1p2c4cec04758bd5fd950d5990eea6b84ae~l3Kd6BnCK3052630526eucas1p2p; Thu, 23 Feb 2017 08:17:43 +0000 (GMT) X-AuditID: cbfec7ef-f79d26d00000420c-b8-58ae9aaa2900 Received: from eusync3.samsung.com ( [203.254.199.213]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 67.36.10233.EAA9EA85; Thu, 23 Feb 2017 08:17:50 +0000 (GMT) Received: from AMDC2765.digital.local ([106.116.147.25]) by eusync3.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OLT00C4AJ1FFX20@eusync3.samsung.com>; Thu, 23 Feb 2017 08:17:43 +0000 (GMT) From: Marek Szyprowski To: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Cc: Marek Szyprowski , Joerg Roedel , Bartlomiej Zolnierkiewicz Subject: [PATCH] iommu: iova: Consolidate code for adding new node to iovad domain rbtree Date: Thu, 23 Feb 2017 09:17:33 +0100 Message-id: <1487837853-3195-1-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFlrJIsWRmVeSWpSXmKPExsWy7djPc7qrZq2LMFh53MZi44z1rBYL9ltb dM7ewG5xedccNou1R+6yO7B6PDk4j8lj8o3ljB59W1YxenzeJBfAEsVlk5Kak1mWWqRvl8CV 8ebCT9aCW4oVcybMZm5g7JTuYuTkkBAwkbhzaCELhC0mceHeerYuRi4OIYFljBKr1q5lgXA+ M0osuPuWDaZj6faJTHBVi8+uhWppYJK4dfkLK0gVm4ChRNfbLqAEB4eIgJPE5ZkKIDXMAu2M Ev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Tc4HRkZeSbyhiaG5paGRsYWFuZGRkjjv1A9XwoUE0hNLUrNTUwtSi2D6mDg4pRoYG6u3 bXFbzbBHWmzyrP5grb3/7iz/X8Cr9nDhb9Pjl7u7+FNjav+0JX2cKTKptupHzqu2i4yPSxbn pB02XBne9N6q+LBjE2fsf41VhsH/TWcHVM1YOCFbQyNsyfa6NFu9ffwLNCOmP/l4RuHT3Inz ov9tyIg2vGRpwjaVd936DvM7QbcvmKh4KbEUZyQaajEXFScCAPUBHLxUAgAA X-MTR: 20000000000000000@CPGS X-CMS-MailID: 20170223081743eucas1p2c4cec04758bd5fd950d5990eea6b84ae X-Msg-Generator: CA X-Sender-IP: 182.198.249.180 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: 20170223081743eucas1p2c4cec04758bd5fd950d5990eea6b84ae X-RootMTR: 20170223081743eucas1p2c4cec04758bd5fd950d5990eea6b84ae 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. There is no functional change. Signed-off-by: Marek Szyprowski --- drivers/iommu/iova.c | 85 +++++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 54 deletions(-) -- 1.9.1 diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index b7268a14184f..32b9c2fb37b6 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -100,6 +100,32 @@ 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 + BUG(); /* this should not happen */ + } + /* 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 +183,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 +193,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 +482,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 +589,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);