Received: by 2002:a05:7412:bb8d:b0:d7:7d3a:4fe2 with SMTP id js13csp142043rdb; Mon, 14 Aug 2023 12:02:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2/UeUuxbgxzb1sqSvfSZdD3KrzWBh2ywihQx2FslhP/rziCuYq+Eyb7ghkezHlGKnwfP9 X-Received: by 2002:a05:6512:3a8b:b0:4f9:5396:ed1b with SMTP id q11-20020a0565123a8b00b004f95396ed1bmr7961461lfu.28.1692039765201; Mon, 14 Aug 2023 12:02:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692039765; cv=none; d=google.com; s=arc-20160816; b=ZceF4JhZbr5fKT9DkIQEPLr7MkfSshikVnh+okfhlE7Pww5QpdBTeNIi2n+r+hnrLK li2xx6YxglgQuRDOfBDvLgV6MWKfnsQ0fAyFNXPeOPhoemha/f6mJkpmbOdDEsU0VXRp ympI9QHa/p2OTU7AjGAcjkGFh0nLf1GTK4Bv9GOrAh4DUnB+e2pJWyVLMU/pjmrLhNwF 7Za6b65cQjzeSZc7DodXSrsfrO5l2PWHNfmvWg9OERXJbpD7Q25U+S6V7tjkWq8/+GGt 90HeDwCcybsZBuZIfUuKxlqNt9qPKTLNOuOD/U2AajsmdjqJW/wRii1QobrwTtVb7BMJ H++g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=5St4uT9xS3bFjTLZ3R6klpVUFhd4iE8oTz11B6wioy0=; fh=1Feb1JXt2Dt8yu+7AJs9zOjE+zylW3fU3D86G8q+Vcc=; b=bnbC6jpqqLGL3X4qZygz7k0gbICUhSHX715+U7n/81nJ9xvKL/M9wYVWE+QfYOsqFz vNKuRXHDTUAv6+2B5qyln99zzDYMngwRRpr+eyB2KY0RzlzjRDA3z8u+Cdih4WwQHuPM dWbXwI1iSQBUR+AEkZ9WtJFobqHSARNQZVXotcYOO4pd0LGzx8sE2esg0SJWpSBcj5b6 AbaiXLTcAr2T29aPshzH6pLyOeAkEX0+dips5c7LVffXOOylMTbd4biz6kuFOeryQAzn Hip/WHHEbnkhPJ6NF4VvcU5vnAmxVBWMIvqnxoT6J4o2Bq4rXc7VoCWXlazenI1c8BwL P2Dg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b16-20020aa7c910000000b005255337a1c8si3296585edt.625.2023.08.14.12.02.12; Mon, 14 Aug 2023 12:02:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231577AbjHNRyT (ORCPT + 99 others); Mon, 14 Aug 2023 13:54:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231582AbjHNRxx (ORCPT ); Mon, 14 Aug 2023 13:53:53 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 45F241711 for ; Mon, 14 Aug 2023 10:53:40 -0700 (PDT) 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 54E43143D; Mon, 14 Aug 2023 10:54:22 -0700 (PDT) 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 117363F762; Mon, 14 Aug 2023 10:53:38 -0700 (PDT) From: Robin Murphy To: joro@8bytes.org Cc: will@kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, john.g.garry@oracle.com, zhangzekun11@huawei.com Subject: [PATCH 1/2] iommu/iova: Make the rcache depot scale better Date: Mon, 14 Aug 2023 18:53:33 +0100 Message-Id: <2c3a0f6b381ae5f743c1b28bbc6eae486160f3b7.1692033783.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The algorithm in the original paper specifies the storage of full magazines in the depot as an unbounded list rather than a fixed-size array. It turns out to be pretty straightforward to do this in our implementation with no significant loss of efficiency. This allows the depot to scale up to the working set sizes of larger systems, while also potentially saving some memory on smaller ones too. Signed-off-by: Robin Murphy --- drivers/iommu/iova.c | 65 ++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/drivers/iommu/iova.c b/drivers/iommu/iova.c index 10b964600948..d2de6fb0e9f4 100644 --- a/drivers/iommu/iova.c +++ b/drivers/iommu/iova.c @@ -625,10 +625,16 @@ EXPORT_SYMBOL_GPL(reserve_iova); * will be wasted. */ #define IOVA_MAG_SIZE 127 -#define MAX_GLOBAL_MAGS 32 /* magazines per bin */ struct iova_magazine { - unsigned long size; + /* + * Only full magazines are inserted into the depot, so we can avoid + * a separate list head and preserve maximum space-efficiency. + */ + union { + unsigned long size; + struct iova_magazine *next; + }; unsigned long pfns[IOVA_MAG_SIZE]; }; @@ -640,8 +646,7 @@ struct iova_cpu_rcache { struct iova_rcache { spinlock_t lock; - unsigned long depot_size; - struct iova_magazine *depot[MAX_GLOBAL_MAGS]; + struct iova_magazine *depot; struct iova_cpu_rcache __percpu *cpu_rcaches; }; @@ -717,6 +722,21 @@ static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn) mag->pfns[mag->size++] = pfn; } +static struct iova_magazine *iova_depot_pop(struct iova_rcache *rcache) +{ + struct iova_magazine *mag = rcache->depot; + + rcache->depot = mag->next; + mag->size = IOVA_MAG_SIZE; + return mag; +} + +static void iova_depot_push(struct iova_rcache *rcache, struct iova_magazine *mag) +{ + mag->next = rcache->depot; + rcache->depot = mag; +} + int iova_domain_init_rcaches(struct iova_domain *iovad) { unsigned int cpu; @@ -734,7 +754,6 @@ int iova_domain_init_rcaches(struct iova_domain *iovad) rcache = &iovad->rcaches[i]; spin_lock_init(&rcache->lock); - rcache->depot_size = 0; rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), cache_line_size()); if (!rcache->cpu_rcaches) { @@ -776,7 +795,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, struct iova_rcache *rcache, unsigned long iova_pfn) { - struct iova_magazine *mag_to_free = NULL; struct iova_cpu_rcache *cpu_rcache; bool can_insert = false; unsigned long flags; @@ -794,12 +812,7 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, if (new_mag) { spin_lock(&rcache->lock); - if (rcache->depot_size < MAX_GLOBAL_MAGS) { - rcache->depot[rcache->depot_size++] = - cpu_rcache->loaded; - } else { - mag_to_free = cpu_rcache->loaded; - } + iova_depot_push(rcache, cpu_rcache->loaded); spin_unlock(&rcache->lock); cpu_rcache->loaded = new_mag; @@ -812,11 +825,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, spin_unlock_irqrestore(&cpu_rcache->lock, flags); - if (mag_to_free) { - iova_magazine_free_pfns(mag_to_free, iovad); - iova_magazine_free(mag_to_free); - } - return can_insert; } @@ -854,9 +862,9 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache, has_pfn = true; } else { spin_lock(&rcache->lock); - if (rcache->depot_size > 0) { + if (rcache->depot) { iova_magazine_free(cpu_rcache->loaded); - cpu_rcache->loaded = rcache->depot[--rcache->depot_size]; + cpu_rcache->loaded = iova_depot_pop(rcache); has_pfn = true; } spin_unlock(&rcache->lock); @@ -894,10 +902,10 @@ static void free_iova_rcaches(struct iova_domain *iovad) { struct iova_rcache *rcache; struct iova_cpu_rcache *cpu_rcache; + struct iova_magazine *mag; unsigned int cpu; - int i, j; - for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { + for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { rcache = &iovad->rcaches[i]; if (!rcache->cpu_rcaches) break; @@ -907,8 +915,8 @@ static void free_iova_rcaches(struct iova_domain *iovad) iova_magazine_free(cpu_rcache->prev); } free_percpu(rcache->cpu_rcaches); - for (j = 0; j < rcache->depot_size; ++j) - iova_magazine_free(rcache->depot[j]); + while ((mag = iova_depot_pop(rcache))) + iova_magazine_free(mag); } kfree(iovad->rcaches); @@ -941,17 +949,16 @@ static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad) static void free_global_cached_iovas(struct iova_domain *iovad) { struct iova_rcache *rcache; + struct iova_magazine *mag; unsigned long flags; - int i, j; - for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { + for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) { rcache = &iovad->rcaches[i]; spin_lock_irqsave(&rcache->lock, flags); - for (j = 0; j < rcache->depot_size; ++j) { - iova_magazine_free_pfns(rcache->depot[j], iovad); - iova_magazine_free(rcache->depot[j]); + while ((mag = iova_depot_pop(rcache))) { + iova_magazine_free_pfns(mag, iovad); + iova_magazine_free(mag); } - rcache->depot_size = 0; spin_unlock_irqrestore(&rcache->lock, flags); } } -- 2.39.2.101.g768bb238c484.dirty