Commit 911aa124 authored by Robin Murphy's avatar Robin Murphy Committed by Joerg Roedel

iommu/iova: Make the rcache depot scale better

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.

Since this involves touching struct iova_magazine with the requisite
care, we may as well reinforce the comment with a proper assertion too.
Reviewed-by: default avatarJohn Garry <john.g.garry@oracle.com>
Reviewed-by: default avatarJerry Snitselaar <jsnitsel@redhat.com>
Signed-off-by: default avatarRobin Murphy <robin.murphy@arm.com>
Link: https://lore.kernel.org/r/f597aa72fc3e1d315bc4574af0ce0ebe5c31cd22.1694535580.git.robin.murphy@arm.comSigned-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent afad94a9
...@@ -622,15 +622,19 @@ EXPORT_SYMBOL_GPL(reserve_iova); ...@@ -622,15 +622,19 @@ EXPORT_SYMBOL_GPL(reserve_iova);
/* /*
* As kmalloc's buffer size is fixed to power of 2, 127 is chosen to * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to
* assure size of 'iova_magazine' to be 1024 bytes, so that no memory * assure size of 'iova_magazine' to be 1024 bytes, so that no memory
* will be wasted. * will be wasted. Since only full magazines are inserted into the depot,
* we don't need to waste PFN capacity on a separate list head either.
*/ */
#define IOVA_MAG_SIZE 127 #define IOVA_MAG_SIZE 127
#define MAX_GLOBAL_MAGS 32 /* magazines per bin */
struct iova_magazine { struct iova_magazine {
unsigned long size; union {
unsigned long size;
struct iova_magazine *next;
};
unsigned long pfns[IOVA_MAG_SIZE]; unsigned long pfns[IOVA_MAG_SIZE];
}; };
static_assert(!(sizeof(struct iova_magazine) & (sizeof(struct iova_magazine) - 1)));
struct iova_cpu_rcache { struct iova_cpu_rcache {
spinlock_t lock; spinlock_t lock;
...@@ -640,8 +644,7 @@ struct iova_cpu_rcache { ...@@ -640,8 +644,7 @@ struct iova_cpu_rcache {
struct iova_rcache { struct iova_rcache {
spinlock_t lock; spinlock_t lock;
unsigned long depot_size; struct iova_magazine *depot;
struct iova_magazine *depot[MAX_GLOBAL_MAGS];
struct iova_cpu_rcache __percpu *cpu_rcaches; struct iova_cpu_rcache __percpu *cpu_rcaches;
}; };
...@@ -717,6 +720,21 @@ static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn) ...@@ -717,6 +720,21 @@ static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
mag->pfns[mag->size++] = 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) int iova_domain_init_rcaches(struct iova_domain *iovad)
{ {
unsigned int cpu; unsigned int cpu;
...@@ -734,7 +752,6 @@ int iova_domain_init_rcaches(struct iova_domain *iovad) ...@@ -734,7 +752,6 @@ int iova_domain_init_rcaches(struct iova_domain *iovad)
rcache = &iovad->rcaches[i]; rcache = &iovad->rcaches[i];
spin_lock_init(&rcache->lock); spin_lock_init(&rcache->lock);
rcache->depot_size = 0;
rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache), rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),
cache_line_size()); cache_line_size());
if (!rcache->cpu_rcaches) { if (!rcache->cpu_rcaches) {
...@@ -776,7 +793,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, ...@@ -776,7 +793,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
struct iova_rcache *rcache, struct iova_rcache *rcache,
unsigned long iova_pfn) unsigned long iova_pfn)
{ {
struct iova_magazine *mag_to_free = NULL;
struct iova_cpu_rcache *cpu_rcache; struct iova_cpu_rcache *cpu_rcache;
bool can_insert = false; bool can_insert = false;
unsigned long flags; unsigned long flags;
...@@ -794,12 +810,7 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, ...@@ -794,12 +810,7 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
if (new_mag) { if (new_mag) {
spin_lock(&rcache->lock); spin_lock(&rcache->lock);
if (rcache->depot_size < MAX_GLOBAL_MAGS) { iova_depot_push(rcache, cpu_rcache->loaded);
rcache->depot[rcache->depot_size++] =
cpu_rcache->loaded;
} else {
mag_to_free = cpu_rcache->loaded;
}
spin_unlock(&rcache->lock); spin_unlock(&rcache->lock);
cpu_rcache->loaded = new_mag; cpu_rcache->loaded = new_mag;
...@@ -812,11 +823,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, ...@@ -812,11 +823,6 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
spin_unlock_irqrestore(&cpu_rcache->lock, flags); 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; return can_insert;
} }
...@@ -854,9 +860,9 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache, ...@@ -854,9 +860,9 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
has_pfn = true; has_pfn = true;
} else { } else {
spin_lock(&rcache->lock); spin_lock(&rcache->lock);
if (rcache->depot_size > 0) { if (rcache->depot) {
iova_magazine_free(cpu_rcache->loaded); iova_magazine_free(cpu_rcache->loaded);
cpu_rcache->loaded = rcache->depot[--rcache->depot_size]; cpu_rcache->loaded = iova_depot_pop(rcache);
has_pfn = true; has_pfn = true;
} }
spin_unlock(&rcache->lock); spin_unlock(&rcache->lock);
...@@ -895,9 +901,8 @@ static void free_iova_rcaches(struct iova_domain *iovad) ...@@ -895,9 +901,8 @@ static void free_iova_rcaches(struct iova_domain *iovad)
struct iova_rcache *rcache; struct iova_rcache *rcache;
struct iova_cpu_rcache *cpu_rcache; struct iova_cpu_rcache *cpu_rcache;
unsigned int cpu; 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]; rcache = &iovad->rcaches[i];
if (!rcache->cpu_rcaches) if (!rcache->cpu_rcaches)
break; break;
...@@ -907,8 +912,8 @@ static void free_iova_rcaches(struct iova_domain *iovad) ...@@ -907,8 +912,8 @@ static void free_iova_rcaches(struct iova_domain *iovad)
iova_magazine_free(cpu_rcache->prev); iova_magazine_free(cpu_rcache->prev);
} }
free_percpu(rcache->cpu_rcaches); free_percpu(rcache->cpu_rcaches);
for (j = 0; j < rcache->depot_size; ++j) while (rcache->depot)
iova_magazine_free(rcache->depot[j]); iova_magazine_free(iova_depot_pop(rcache));
} }
kfree(iovad->rcaches); kfree(iovad->rcaches);
...@@ -942,16 +947,16 @@ static void free_global_cached_iovas(struct iova_domain *iovad) ...@@ -942,16 +947,16 @@ static void free_global_cached_iovas(struct iova_domain *iovad)
{ {
struct iova_rcache *rcache; struct iova_rcache *rcache;
unsigned long flags; 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]; rcache = &iovad->rcaches[i];
spin_lock_irqsave(&rcache->lock, flags); spin_lock_irqsave(&rcache->lock, flags);
for (j = 0; j < rcache->depot_size; ++j) { while (rcache->depot) {
iova_magazine_free_pfns(rcache->depot[j], iovad); struct iova_magazine *mag = iova_depot_pop(rcache);
iova_magazine_free(rcache->depot[j]);
iova_magazine_free_pfns(mag, iovad);
iova_magazine_free(mag);
} }
rcache->depot_size = 0;
spin_unlock_irqrestore(&rcache->lock, flags); spin_unlock_irqrestore(&rcache->lock, flags);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment