From patchwork Thu Sep 21 15:52:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 113289 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp2185337qgf; Thu, 21 Sep 2017 08:53:07 -0700 (PDT) X-Google-Smtp-Source: AOwi7QB6fng8Fhl2JF5BShbIbSe1/7i5P1w95nrStP6/2Igj24vai9zeWChEbusw8NILU1TJcAjs X-Received: by 10.99.112.24 with SMTP id l24mr6014027pgc.302.1506009187342; Thu, 21 Sep 2017 08:53:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506009187; cv=none; d=google.com; s=arc-20160816; b=Yf0ndsim4wKrf3SbIBG4ECRrFL1Dsk8nvBcuaoZWpal+npiu+idz7WMv2dzzIIRE4Y C0V1BBOqC5XJqu4+QukIMvGII89QHP52tmERudH+JbOQ6nQspdtURMRHSKRF+3KwrHJ/ 6QS/UVr+aZrsQm9zoLhRVdpDwc9nSg4cM2RCLHPrdumiqTcXeBdnSZTB6vwWE+MKHJiU bUM8gqEMq1JHgA8kL/t1q50tPlQ1To2KH0A+jZ3t8Z+CwuEI1y1IpoCOvqWQxAI7owBo vuMq5TI9x7fyTsIHgEuWV34nLJ2FwZQ2+1L0FWDt5k3rt8Oj/jdPQ/GyAMQ6qrtDLxQU sFGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=n+wtCAE/S2Bb6EoyCLOK5/XzdDmPQ9L7zOyKCWwOmOk=; b=a4j7LzbzdGLJcCr+gyjw0TLVC/bT/qga4iNfbID9umLnDzI3yuPtfPuXaVF9kbixoE NKsmkBWSPinPBKB/wrUA5DH0D4OtgJz9YVw2V1OyWtD9tv0D2eHllMTnEdJlS6DYYQQJ /k0GzTZfWkflmg3/qChER5kK+eik++KoMU6ZKB2Gts3J1VsD5eY3g/3T3w8hvwdm+37I 4/4jg9tfgsW+sVBLgGtLZ5JnYizlSfWh6u5NTWmbQvFDwFaJffDKBcAMWEv6V2sQcvVI 1c8NCowpKSnNd8eR6GJnFuaLWRceNkbkz8OmjN6Ax1UyeRCiwvduUjdKsSj9AZwR6cgh hvKw== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i17si1199212pgn.373.2017.09.21.08.53.06; Thu, 21 Sep 2017 08:53:07 -0700 (PDT) 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 S1751792AbdIUPxE (ORCPT + 26 others); Thu, 21 Sep 2017 11:53:04 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:48710 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751722AbdIUPxC (ORCPT ); Thu, 21 Sep 2017 11:53:02 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B744A1435; Thu, 21 Sep 2017 08:53:01 -0700 (PDT) Received: from e110467-lin.cambridge.arm.com (e110467-lin.cambridge.arm.com [10.1.210.88]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 614263F578; Thu, 21 Sep 2017 08:53:00 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org Cc: iommu@lists.linux-foundation.org, thunder.leizhen@huawei.com, nwatters@codeaurora.org, tomasz.nowicki@caviumnetworks.com, dwoods@mellanox.com, linux-kernel@vger.kernel.org Subject: [PATCH v5 2/6] iommu/iova: Optimise the padding calculation Date: Thu, 21 Sep 2017 16:52:43 +0100 Message-Id: <69401ae633a0a869423eaf6b4ea61bc38a3e0e80.1506007392.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.13.4.dirty In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhen Lei The mask for calculating the padding size doesn't change, so there's no need to recalculate it every loop iteration. Furthermore, Once we've done that, it becomes clear that we don't actually need to calculate a padding size at all - by flipping the arithmetic around, we can just combine the upper limit, size, and mask directly to check against the lower limit. For an arm64 build, this alone knocks 20% off the object code size of the entire alloc_iova() function! Signed-off-by: Zhen Lei Tested-by: Ard Biesheuvel Tested-by: Zhen Lei Tested-by: Nate Watterson [rm: simplified more of the arithmetic, rewrote commit message] Signed-off-by: Robin Murphy --- v5: No change drivers/iommu/iova.c | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) -- 2.13.4.dirty diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index f129ff4f5c89..20be9a8b3188 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -182,24 +182,17 @@ iova_insert_rbtree(struct rb_root *root, struct iova *iova, 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 - */ -static unsigned int -iova_get_pad_size(unsigned int size, unsigned int limit_pfn) -{ - return (limit_pfn - size) & (__roundup_pow_of_two(size) - 1); -} - static int __alloc_and_insert_iova_range(struct iova_domain *iovad, unsigned long size, unsigned long limit_pfn, struct iova *new, bool size_aligned) { struct rb_node *prev, *curr = NULL; unsigned long flags; - unsigned long saved_pfn; - unsigned int pad_size = 0; + unsigned long saved_pfn, new_pfn; + unsigned long align_mask = ~0UL; + + if (size_aligned) + align_mask <<= fls_long(size - 1); /* Walk the tree backwards */ spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); @@ -209,31 +202,26 @@ static int __alloc_and_insert_iova_range(struct iova_domain *iovad, while (curr) { struct iova *curr_iova = rb_entry(curr, struct iova, node); - if (limit_pfn <= curr_iova->pfn_lo) { + if (limit_pfn <= curr_iova->pfn_lo) goto move_left; - } else if (limit_pfn > curr_iova->pfn_hi) { - if (size_aligned) - pad_size = iova_get_pad_size(size, limit_pfn); - if ((curr_iova->pfn_hi + size + pad_size) < limit_pfn) - break; /* found a free slot */ - } + + if (((limit_pfn - size) & align_mask) > curr_iova->pfn_hi) + break; /* found a free slot */ + limit_pfn = curr_iova->pfn_lo; move_left: prev = curr; curr = rb_prev(curr); } - if (!curr) { - if (size_aligned) - pad_size = iova_get_pad_size(size, limit_pfn); - if ((iovad->start_pfn + size + pad_size) > limit_pfn) { - spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); - return -ENOMEM; - } + new_pfn = (limit_pfn - size) & align_mask; + if (limit_pfn < size || new_pfn < iovad->start_pfn) { + spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); + return -ENOMEM; } /* pfn_lo will point to size aligned address if size_aligned is set */ - new->pfn_lo = limit_pfn - (size + pad_size); + new->pfn_lo = new_pfn; new->pfn_hi = new->pfn_lo + size - 1; /* If we have 'prev', it's a valid place to start the insertion. */