From patchwork Mon Feb 5 15:32:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 13545692 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C3F7C48295 for ; Mon, 5 Feb 2024 15:32:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE8CA6B0082; Mon, 5 Feb 2024 10:32:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E84736B0083; Mon, 5 Feb 2024 10:32:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAE696B0085; Mon, 5 Feb 2024 10:32:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B9A476B0082 for ; Mon, 5 Feb 2024 10:32:51 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 748281201DB for ; Mon, 5 Feb 2024 15:32:51 +0000 (UTC) X-FDA: 81758142942.06.523B61A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf09.hostedemail.com (Postfix) with ESMTP id B06AC14002E for ; Mon, 5 Feb 2024 15:32:49 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf09.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707147169; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iYOjIPp5YF4pHRIdEAilCHpp38B3kyUgFoGeW/y2Mfw=; b=xeZAVpFyNeeNKxr/lyaRkukNKygUTI39AysxkXzo2sI6Jqv2podL3y/wqCW6YiPqdmOmt+ fWi+mhioI4BjQM3D/i6bEtGcW5gyRkgUFn6F25elG3HlzMbLKMGYqMn9/1IwFskEKjbXBI rYY7iy21iLk6wIcvlYAX6/zH2xwn6XU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf09.hostedemail.com: domain of robin.murphy@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=robin.murphy@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707147169; a=rsa-sha256; cv=none; b=LEEFr2Aivf7gWae6bNNmUqkiwjSr38AEkSRmdlUct5HIlPe06uU3YSogcnjKbzo8q9obqz +aswXK/vsVIuOX6aPDNI1xUD79UufAVpYkyxsOMfT5Hjiq+DABXgUaYpG7FP2o5kdjSB5S BDlIwlcdaCXuDea8ME0+attJ79loDVs= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9287C12FC; Mon, 5 Feb 2024 07:33:31 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C60753F5A1; Mon, 5 Feb 2024 07:32:47 -0800 (PST) From: Robin Murphy To: joro@8bytes.org Cc: will@kernel.org, pasha.tatashin@soleen.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, rientjes@google.com, yosryahmed@google.com, john.g.garry@oracle.com Subject: [PATCH v3 2/3] iommu/iova: Reorganise some code Date: Mon, 5 Feb 2024 15:32:40 +0000 Message-Id: X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: grxw5ffewu33dscqa6nw3f683aky4o1y X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B06AC14002E X-HE-Tag: 1707147169-861162 X-HE-Meta: U2FsdGVkX1/c9w5p6VCzlfgacjds4t7lQd+qubot0LAovE1qFUT0d9BcLZxcISENyJ9Xb167LE5EXiXU5YgwBNOlQyHjAsBW71EOkxmH2oYrMSqHJQ5L8LmqQkKEKX607Mdgcto64OSVf2pf8zS9rfOnk3bmN+HdCTkyNLZi88WZYXyIfiiY1zkpggKYzAAlouZZenuDKTfILNkm5/YlJOqE+UEc6o4qtqhovZJj62onC/XEi35zRfC9GO4AXRvBfDnaVd5b9bzXFvZamaSsmN9lMSu+V3Fw2gTKDJCj+45xmWuRw3ofY0M54hCVIoi8UlPRydaQVO8ONgZCO29EDp6TykRUboDT0IXQ/mHKwPAL/Upfut3A4SA4HgC5e5NtMu2wqrCI3E031NmNJrmEH1XP/VMZfrMrxiH4AquVMyph3S6zYUidzKcsJr9CjosmMmCe32fuqzb/moD+J2Ukp595VBTcNjMNDC2qpwaZ+358vxTNVRflECZuWedZbikrTpSgjWmIwH1CXSjHQZbGEBpH2G2d49tJDw3tOg6Tut0aXwtnDb0K8gEg7HNVKXNBeY46LKT/564lyJy5bxsOQmNH8yQzZi0MQNyBsDutDdIDY/ZxLWyAZH4toO7dVdpFi91V6MP0MeA9LOHvxnB2X9NoeUJn3VwV46lHtnsCr4mu2RdKOtbW5PEWGCt8s8ceuYdvEYl6e3hV4wcRUQhAhmyUJQUOukv0uSROUtSsbDlKH1uAi/UyaJq684RWDJM0vBcB+OEp+J9f27gsJTF7/XQnFwv2M8d/xsx72byWR7OedsOKog4WQoNkaJBXdTPG1M2NzOznZN+e88ghGilfbRAwDzgUv5FD8l/VoDDCEPSPIjnvJg2z20m4mG6YFmhGY20OCyNLL0bahngVIjyH9+BTiQSfg81qqFNFwFnJwOH5LhI4rfRXlINtam+trAzbcRxvwUECRTBzvzPEDGa Zlnuiqoj GIAp03ra4kUAk80TZw8OaJmQzm9b6+FRcGdMXM8bjfUZQbljwg4ZVvww35Wx0JeXtGuiZrjoDXBD28S0vJCtApb5cwMZQiPEb1iilgOjfMFtLe2qX+BGvnBS6DKxSUc1XDGxUO+lv0Po/wWumCOlqNxiRdq9sc2UsIuUD8MrJibEMc78UF3RxyPIaYg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file. Signed-off-by: Robin Murphy Acked-by: David Rientjes Reviewed-by: Pasha Tatashin Reviewed-by: John Garry Reviewed-by: Jerry Snitselaar --- drivers/iommu/iova.c | 128 +++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index cf95001d85c0..b5de865ee50b 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -24,24 +24,8 @@ static bool iova_rcache_insert(struct iova_domain *iovad, static unsigned long iova_rcache_get(struct iova_domain *iovad, unsigned long size, unsigned long limit_pfn); -static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad); static void free_iova_rcaches(struct iova_domain *iovad); - -unsigned long iova_rcache_range(void) -{ - return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1); -} - -static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node) -{ - struct iova_domain *iovad; - - iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead); - - free_cpu_cached_iovas(cpu, iovad); - return 0; -} - +static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad); static void free_global_cached_iovas(struct iova_domain *iovad); static struct iova *to_iova(struct rb_node *node) @@ -252,53 +236,6 @@ static void free_iova_mem(struct iova *iova) kmem_cache_free(iova_cache, iova); } -int iova_cache_get(void) -{ - int err = -ENOMEM; - - mutex_lock(&iova_cache_mutex); - if (!iova_cache_users) { - iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0, - SLAB_HWCACHE_ALIGN, NULL); - if (!iova_cache) - goto out_err; - - err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", - NULL, iova_cpuhp_dead); - if (err) { - pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err)); - goto out_err; - } - } - - iova_cache_users++; - mutex_unlock(&iova_cache_mutex); - - return 0; - -out_err: - kmem_cache_destroy(iova_cache); - mutex_unlock(&iova_cache_mutex); - return err; -} -EXPORT_SYMBOL_GPL(iova_cache_get); - -void iova_cache_put(void) -{ - mutex_lock(&iova_cache_mutex); - if (WARN_ON(!iova_cache_users)) { - mutex_unlock(&iova_cache_mutex); - return; - } - iova_cache_users--; - if (!iova_cache_users) { - cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD); - kmem_cache_destroy(iova_cache); - } - mutex_unlock(&iova_cache_mutex); -} -EXPORT_SYMBOL_GPL(iova_cache_put); - /** * alloc_iova - allocates an iova * @iovad: - iova domain in question @@ -653,6 +590,11 @@ struct iova_rcache { struct delayed_work work; }; +unsigned long iova_rcache_range(void) +{ + return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1); +} + static struct iova_magazine *iova_magazine_alloc(gfp_t flags) { struct iova_magazine *mag; @@ -989,5 +931,63 @@ static void free_global_cached_iovas(struct iova_domain *iovad) spin_unlock_irqrestore(&rcache->lock, flags); } } + +static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node) +{ + struct iova_domain *iovad; + + iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead); + + free_cpu_cached_iovas(cpu, iovad); + return 0; +} + +int iova_cache_get(void) +{ + int err = -ENOMEM; + + mutex_lock(&iova_cache_mutex); + if (!iova_cache_users) { + iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0, + SLAB_HWCACHE_ALIGN, NULL); + if (!iova_cache) + goto out_err; + + err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", + NULL, iova_cpuhp_dead); + if (err) { + pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err)); + goto out_err; + } + } + + iova_cache_users++; + mutex_unlock(&iova_cache_mutex); + + return 0; + +out_err: + kmem_cache_destroy(iova_cache); + mutex_unlock(&iova_cache_mutex); + return err; +} +EXPORT_SYMBOL_GPL(iova_cache_get); + +void iova_cache_put(void) +{ + mutex_lock(&iova_cache_mutex); + if (WARN_ON(!iova_cache_users)) { + mutex_unlock(&iova_cache_mutex); + return; + } + iova_cache_users--; + if (!iova_cache_users) { + cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD); + kmem_cache_destroy(iova_cache); + } + mutex_unlock(&iova_cache_mutex); +} +EXPORT_SYMBOL_GPL(iova_cache_put); + MODULE_AUTHOR("Anil S Keshavamurthy "); MODULE_LICENSE("GPL");