Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754928AbdIRK5w (ORCPT ); Mon, 18 Sep 2017 06:57:52 -0400 Received: from mail-dm3nam03on0086.outbound.protection.outlook.com ([104.47.41.86]:21097 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754853AbdIRK5s (ORCPT ); Mon, 18 Sep 2017 06:57:48 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Tomasz.Nowicki@cavium.com; From: Tomasz Nowicki To: joro@8bytes.org, robin.murphy@arm.com, will.deacon@arm.com Cc: lorenzo.pieralisi@arm.com, Jayachandran.Nair@cavium.com, Ganapatrao.Kulkarni@cavium.com, ard.biesheuvel@linaro.org, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, Tomasz Nowicki , Tomasz Nowicki Subject: [PATCH 1/1] iommu/iova: Make rcache flush optional on IOVA allocation failure Date: Mon, 18 Sep 2017 12:56:54 +0200 Message-Id: <1505732214-9052-2-git-send-email-tomasz.nowicki@caviumnetworks.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1505732214-9052-1-git-send-email-tomasz.nowicki@caviumnetworks.com> References: <1505732214-9052-1-git-send-email-tomasz.nowicki@caviumnetworks.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [12.108.191.226] X-ClientProxiedBy: MWHPR21CA0044.namprd21.prod.outlook.com (2603:10b6:300:129::30) To SN4PR0701MB3662.namprd07.prod.outlook.com (2603:10b6:803:4d::16) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e04fc04e-d22a-4829-4faf-08d4fe841848 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(300000502095)(300135100095)(22001)(2017030254152)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:SN4PR0701MB3662; X-Microsoft-Exchange-Diagnostics: 1;SN4PR0701MB3662;3:Cie6snirYoz514DSTrwTyg8MDYub9F+TYYDnaPtxBjWgrwn2kxwVHA5kaHHeA2CObd5zWG4EpTon9wgeXFF+xFdi4kgjyNwPSseI+/lUrYx6GU6iR146TW+d0F7v/F7am75KHQKxY0usV5U6Btk5RyzvVB98sGBI1dDxM6OP5pTAntpQoN347umeKZx5+hH/SiiY2kMc4YQ3n4Lh7VhMqDvMr5ZbYdnrJN87mLlNdJv7jEcmyLjVWOcHgW1U8UKl;25:25ZDXNUGAGNtS5khJlnnAbDjXxkVT0zWqsMT7dU/82OobtHzpfICyJ7ZKRYsIiW03Am3HlRwU3KFrlXs+EVbPR081v+Tm3O6OfvZOi8SSFPL9TMx9P0AZsOzaHm9Ms5q9eaN+zVPhtIFGdsZwl8zviCbYzpBwDBmB/L8GWFAmcUnpCSHT97zk253sc+GE6tE2kOTLUppWVA94WeTZMgIkBE1pSYZEjBEoMYKhKLmZZD+nvpkhfkUkDxQVKfM35+ua0S7BbobDQFnJKpaO12cExzdUBLeDxWJKIOaIzSu3P0RfXLs6v6bN569gY/q19m+xN14t+Q19PRBvasX7H74og==;31:zB2zfOCLAk6vrk8WTzjlUzGj1WoCeo/LpYj/781FXiDd6PMtnmqR8T+GADVIbpqWPuv3dAObdMVztBmCpD3aurX2H+3xQPxJ8wd4PS6TLxQTiuYGbZjzYlwoA/thafTvRmBKc7pmhvq4kPg7SZKPk7TEa5/OU0B7Iz5RxwX/srxQYcBTmoW7i13lfRUQAbGt+BKvynCpsOwVmQ2y3nhgCUKo6FwFkCaFh9kv4dHyjlc= X-MS-TrafficTypeDiagnostic: SN4PR0701MB3662: X-Microsoft-Exchange-Diagnostics: 1;SN4PR0701MB3662;20:ijedBVI4fcgDUGXyN3qf2WotlwUgn/8xkKZ7Kpsc2XF1djxt5fz7/kfe0prHJTkI3KnpwMzFwFRabOiUn8MZrjia0OkO8HSLs7C3+Ofbyoi6U9ZkquALO1i/8hlH1Y9+6TDVBlNlK7n85Fh91Ju7ddAwEzon/qFRmGtZYLS29t+fFPxwjYfE8DUnNIlyhBbjQrVQKNXRBA7qEPNtsrZUOHBvt/5K1Me58zpm4mK5/whVnSc8NyQ7/U6zeZvL9qDJBeWz7Zr6slVfgp2ATmMcGH102HgEWd86P2dKRD2wrqSnl925eWVzI2WVKFUZfBcZ2VVWtyewY7i+UzNarxFNIc7JFK3ktm/35jlSJSbUWD9m8cFWatkxypFCozT/dIz6rnpxNm7dmtkcpX7l/xmLGdEDSirBgVE/pTpKJfi5VC5guJVIpiUpos5Lloo3R7K3jEnv47JUIWUEG5WxB1xo363LZAslmXqz+luiX2NbT/AggXEz44pZAOHNEtnsWsj6ObZFRWvm8FhzJwu3dYHOBVeCJFdTw7h8Y/HfVp2D5vOKGMqzO8tW+mvswerbM7dz/jwfn/y9jO1LVVoqFerUUqtg/Gmh6F9nhS51AfKAQTk=;4:NDgmjWG2rx/HY3HR/WxdmkqdONbNUK1gP3jMYr1vTYo40fo8MjlnchccAPU7/bf93E62OXDCRDEEImSkAJAPjZ7KziRZu5SlEwBN0F+K3RHXInU8pJoCZKYWUwNgs39SHWE134pUeQGvp+My3kDPjLUiYqBuN1qHWKMtpFulqBL+IFxT/hDzB7P+TqnhWN7nzSSBRVC7/LYlYic0kYVq3O83WlXYHCEHZz5iIqLZ2VK6ZARvMMahsfu9cYCYe06J X-Exchange-Antispam-Report-Test: UriScan:; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(3002001)(100000703101)(100105400095)(10201501046)(93006095)(6041248)(20161123560025)(20161123558100)(20161123562025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:SN4PR0701MB3662;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:SN4PR0701MB3662; X-Forefront-PRVS: 04347F8039 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6009001)(376002)(346002)(199003)(189002)(5660300001)(8936002)(8676002)(36756003)(6666003)(81156014)(81166006)(48376002)(189998001)(50466002)(575784001)(6486002)(6506006)(50226002)(4326008)(33646002)(106356001)(2950100002)(42882006)(105586002)(68736007)(6512007)(54906002)(107886003)(110136004)(53936002)(66066001)(5003940100001)(2906002)(47776003)(50986999)(76176999)(25786009)(72206003)(478600001)(97736004)(316002)(16586007)(16526017)(101416001)(3846002)(305945005)(6116002)(7736002);DIR:OUT;SFP:1101;SCL:1;SRVR:SN4PR0701MB3662;H:desktop.semihalf.local;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;SN4PR0701MB3662;23:r5mhOg5A8dRKyq4d/8qY92D2flTVFcqrMJ0KpO4?= =?us-ascii?Q?rmE0mVUt65l5LAql1yq0RNcq95sDHDiUyrj29IN+jvQU7vYU0eJvMw1i3QYl?= =?us-ascii?Q?ZXmKc+HSpLYtC4v4dcY+WoCnYKII2oCE+KpS5/G1T74qFRXkiblaMUGAbG+G?= =?us-ascii?Q?Xqd1N2wTT21qXyrH1PBow7aK2OK7n9ebJOBIVYst1667Q7rHYTrnRxZoX7Gy?= =?us-ascii?Q?J0N5umn3MAM/vAOnQEwRsQEwtS+9QC9AACgR+4myE3iaSRH9lhPDG8qvAq+2?= =?us-ascii?Q?x6LhSKvB+LMRsjoeXowdrxMXi///Sf4jti3hVDZFLW7Kqe2DXqkdqYPwQEbU?= =?us-ascii?Q?eILxANKEw/lcSxHraWGXjScUbpQAxrj8B9jFUSuEy2dCQ8Vu1WCNutdAaayZ?= =?us-ascii?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gLuOtI+oKfNKLtDAPL+zw6C0sOaRlbmC9GrlwmsUhksmUIVMtcshlxWFncRtjkDnNK021qCf31L0giV8xkT6vQS1LKJQT+0RZurJiFfXpcKAUe6c1msHH8QHxpvcnKcjzy5bscmm6fq8pFweYGalglzZk9RVkwWGPtLJdqjtl9J0vVATZtTbXQ7Tyca6E0AL4guEd7hR8EcGlgXOsnh7Q08/woRTBA8yR7wzERoDa2EI+4YkYxjq1aZn8WPJboEEaNPyNMIYA==;5:SlPZXxTVWbJTv26aUkFmKE8JkivDMizzUt2TBu+hsBgXNW5h2/sa5vbsmzd0FZmztlirB455NVlAkEHuNldQC4Eu9YPbOeOq414BIeIxyTULozNjOhJSCpSVXaMqSdl/yJGiSUMJwngo0KTKtruX/Q==;24:tQACp/6Qwb3JXxBS0sfEacKH+Neq5KK9P1ENCaoQsZtGZrEnul1Qk1LRIGcZnzgTayknD4z7GP72faU1ZjgATTiWBcYRVdfF9wgYN1Ax5I8=;7:5vu3qtG+XrO9k0r5PBv6MeHxvEL2O40UaEeYxL3xRd3V/0RpiHxG2ogITuJzdbm0aGG5Q6StzXnngoVPv2t9wxH7W9HGGtKTSlMrYcsZLc/zFMrg10BW9VBBtSheWNiiGSsGoBm4+HlBlA7v4XrL/37C8JtZei7feOTMWRwxbFQQ+0kHDAMGpiileUvxqlFsIompJVyb6bPu/cZSafSU3FCir6w2oQmMSqzwVerpt04= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2017 10:57:37.7455 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN4PR0701MB3662 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5594 Lines: 149 Since IOVA allocation failure is not unusual case we need to flush CPUs' rcache in hope we will succeed in next round. However, it is useful to decide whether we need rcache flush step because of two reasons: - Not scalability. On large system with ~100 CPUs iterating and flushing rcache for each CPU becomes serious bottleneck so we may want to deffer it. - free_cpu_cached_iovas() does not care about max PFN we are interested in. Thus we may flush our rcaches and still get no new IOVA like in the commonly used scenario: if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev)) iova = alloc_iova_fast(iovad, iova_len, DMA_BIT_MASK(32) >> shift); if (!iova) iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift); 1. First alloc_iova_fast() call is limited to DMA_BIT_MASK(32) to get PCI devices a SAC address 2. alloc_iova() fails due to full 32-bit space 3. rcaches contain PFNs out of 32-bit space so free_cpu_cached_iovas() throws entries away for nothing and alloc_iova() fails again 4. Next alloc_iova_fast() call cannot take advantage of rcache since we have just defeated caches. In this case we pick the slowest option to proceed. This patch reworks flushed_rcache local flag to be additional function argument instead and control rcache flush step. Also, it updates all users to do the flush as the last chance. Signed-off-by: Tomasz Nowicki --- drivers/iommu/amd_iommu.c | 5 +++-- drivers/iommu/dma-iommu.c | 6 ++++-- drivers/iommu/intel-iommu.c | 5 +++-- drivers/iommu/iova.c | 7 +++---- include/linux/iova.h | 5 +++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index 8d2ec60..ce68986 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -1604,10 +1604,11 @@ static unsigned long dma_ops_alloc_iova(struct device *dev, if (dma_mask > DMA_BIT_MASK(32)) pfn = alloc_iova_fast(&dma_dom->iovad, pages, - IOVA_PFN(DMA_BIT_MASK(32))); + IOVA_PFN(DMA_BIT_MASK(32)), false); if (!pfn) - pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask)); + pfn = alloc_iova_fast(&dma_dom->iovad, pages, + IOVA_PFN(dma_mask), true); return (pfn << PAGE_SHIFT); } diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 191be9c..25914d3 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -370,10 +370,12 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain, /* Try to get PCI devices a SAC address */ if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev)) - iova = alloc_iova_fast(iovad, iova_len, DMA_BIT_MASK(32) >> shift); + iova = alloc_iova_fast(iovad, iova_len, + DMA_BIT_MASK(32) >> shift, false); if (!iova) - iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift); + iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, + true); return (dma_addr_t)iova << shift; } diff --git a/drivers/iommu/intel-iommu.c b/drivers/iommu/intel-iommu.c index 05c0c3a..75c8320 100644 --- a/drivers/iommu/intel-iommu.c +++ b/drivers/iommu/intel-iommu.c @@ -3460,11 +3460,12 @@ static unsigned long intel_alloc_iova(struct device *dev, * from higher range */ iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, - IOVA_PFN(DMA_BIT_MASK(32))); + IOVA_PFN(DMA_BIT_MASK(32)), false); if (iova_pfn) return iova_pfn; } - iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, IOVA_PFN(dma_mask)); + iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, + IOVA_PFN(dma_mask), true); if (unlikely(!iova_pfn)) { pr_err("Allocating %ld-page iova for %s failed", nrpages, dev_name(dev)); diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index f88acad..1a18b14 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -358,9 +358,8 @@ EXPORT_SYMBOL_GPL(free_iova); */ unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size, - unsigned long limit_pfn) + unsigned long limit_pfn, bool flush_rcache) { - bool flushed_rcache = false; unsigned long iova_pfn; struct iova *new_iova; @@ -373,11 +372,11 @@ alloc_iova_fast(struct iova_domain *iovad, unsigned long size, if (!new_iova) { unsigned int cpu; - if (flushed_rcache) + if (!flush_rcache) return 0; /* Try replenishing IOVAs by flushing rcache. */ - flushed_rcache = true; + flush_rcache = false; for_each_online_cpu(cpu) free_cpu_cached_iovas(cpu, iovad); goto retry; diff --git a/include/linux/iova.h b/include/linux/iova.h index 58c2a36..8fdcb66 100644 --- a/include/linux/iova.h +++ b/include/linux/iova.h @@ -97,7 +97,7 @@ struct iova *alloc_iova(struct iova_domain *iovad, unsigned long size, void free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size); unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size, - unsigned long limit_pfn); + unsigned long limit_pfn, bool flush_rcache); struct iova *reserve_iova(struct iova_domain *iovad, unsigned long pfn_lo, unsigned long pfn_hi); void copy_reserved_iova(struct iova_domain *from, struct iova_domain *to); @@ -151,7 +151,8 @@ static inline void free_iova_fast(struct iova_domain *iovad, static inline unsigned long alloc_iova_fast(struct iova_domain *iovad, unsigned long size, - unsigned long limit_pfn) + unsigned long limit_pfn, + bool flush_rcache) { return 0; } -- 2.7.4