Skip to content

Commit 2330453

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu/iova: Manage the depot list size
Automatically scaling the depot up to suit the peak capacity of a workload is all well and good, but it would be nice to have a way to scale it back down again if the workload changes. To that end, add backround reclaim that will gradually free surplus magazines if the depot size remains above a reasonable threshold for long enough. Reviewed-by: Jerry Snitselaar <[email protected]> Signed-off-by: Robin Murphy <[email protected]> Link: https://lore.kernel.org/r/03170665c56d89c6ce6081246b47f68d4e483308.1694535580.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <[email protected]>
1 parent 911aa12 commit 2330453

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

drivers/iommu/iova.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/smp.h>
1212
#include <linux/bitops.h>
1313
#include <linux/cpu.h>
14+
#include <linux/workqueue.h>
1415

1516
/* The anchor node sits above the top of the usable address space */
1617
#define IOVA_ANCHOR ~0UL
@@ -627,6 +628,8 @@ EXPORT_SYMBOL_GPL(reserve_iova);
627628
*/
628629
#define IOVA_MAG_SIZE 127
629630

631+
#define IOVA_DEPOT_DELAY msecs_to_jiffies(100)
632+
630633
struct iova_magazine {
631634
union {
632635
unsigned long size;
@@ -644,8 +647,11 @@ struct iova_cpu_rcache {
644647

645648
struct iova_rcache {
646649
spinlock_t lock;
650+
unsigned int depot_size;
647651
struct iova_magazine *depot;
648652
struct iova_cpu_rcache __percpu *cpu_rcaches;
653+
struct iova_domain *iovad;
654+
struct delayed_work work;
649655
};
650656

651657
static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
@@ -726,13 +732,33 @@ static struct iova_magazine *iova_depot_pop(struct iova_rcache *rcache)
726732

727733
rcache->depot = mag->next;
728734
mag->size = IOVA_MAG_SIZE;
735+
rcache->depot_size--;
729736
return mag;
730737
}
731738

732739
static void iova_depot_push(struct iova_rcache *rcache, struct iova_magazine *mag)
733740
{
734741
mag->next = rcache->depot;
735742
rcache->depot = mag;
743+
rcache->depot_size++;
744+
}
745+
746+
static void iova_depot_work_func(struct work_struct *work)
747+
{
748+
struct iova_rcache *rcache = container_of(work, typeof(*rcache), work.work);
749+
struct iova_magazine *mag = NULL;
750+
unsigned long flags;
751+
752+
spin_lock_irqsave(&rcache->lock, flags);
753+
if (rcache->depot_size > num_online_cpus())
754+
mag = iova_depot_pop(rcache);
755+
spin_unlock_irqrestore(&rcache->lock, flags);
756+
757+
if (mag) {
758+
iova_magazine_free_pfns(mag, rcache->iovad);
759+
iova_magazine_free(mag);
760+
schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);
761+
}
736762
}
737763

738764
int iova_domain_init_rcaches(struct iova_domain *iovad)
@@ -752,6 +778,8 @@ int iova_domain_init_rcaches(struct iova_domain *iovad)
752778

753779
rcache = &iovad->rcaches[i];
754780
spin_lock_init(&rcache->lock);
781+
rcache->iovad = iovad;
782+
INIT_DELAYED_WORK(&rcache->work, iova_depot_work_func);
755783
rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),
756784
cache_line_size());
757785
if (!rcache->cpu_rcaches) {
@@ -812,6 +840,7 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,
812840
spin_lock(&rcache->lock);
813841
iova_depot_push(rcache, cpu_rcache->loaded);
814842
spin_unlock(&rcache->lock);
843+
schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);
815844

816845
cpu_rcache->loaded = new_mag;
817846
can_insert = true;
@@ -912,6 +941,7 @@ static void free_iova_rcaches(struct iova_domain *iovad)
912941
iova_magazine_free(cpu_rcache->prev);
913942
}
914943
free_percpu(rcache->cpu_rcaches);
944+
cancel_delayed_work_sync(&rcache->work);
915945
while (rcache->depot)
916946
iova_magazine_free(iova_depot_pop(rcache));
917947
}

0 commit comments

Comments
 (0)