iommu/iova: Solve longterm IOVA issue
Subsystems (2)
v3 cover iommu/iova: Solve longterm IOVA issue
This series contains a patch to solve the longterm IOVA issue which leizhen originally tried to address at [0].
A sieved kernel log is at the following, showing periodic dumps of IOVA sizes, per CPU and per depot bin, per IOVA size granule:
https://raw.githubusercontent.com/hisilicon/kernel-dev/topic-iommu-5.10-iova-debug-v3/aging_test
Notice, for example, the following logs:
[13175.355584] print_iova1 cpu_total=40135 depot_total=3866 total=44001 [83483.457858] print_iova1 cpu_total=62532 depot_total=24476 total=87008
Where total IOVA rcache size has grown from 44K->87K over a long time.
Along with this patch, I included the following:
- A smaller helper to clear all IOVAs for a domain
- Change polarity of the IOVA magazine helpers
- Small optimisation from Cong Wang included, which was never applied [1]. There was some debate of the other patches in that series, but this one is quite straightforward.
Differnces to v2:
- Update commit message for patch 3/4
Differences to v1:
- Add IOVA clearing helper
- Add patch to change polarity of mag helpers
- Avoid logically-redundant extra variable in __iova_rcache_insert()
[0] https://lore.kernel.org/linux-iommu/[email protected]/
[1] https://lore.kernel.org/linux-iommu/[email protected]/
Cong Wang (1):
iommu: avoid taking iova_rbtree_lock twice
John Garry (3):
iommu/iova: Add free_all_cpu_cached_iovas() iommu/iova: Avoid double-negatives in magazine helpers iommu/iova: Flush CPU rcache for when a depot fills
drivers/iommu/iova.c | 66 +++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 28 deletions(-)
Signature · 1 line
2.26.2
PATCH v3 1/4 iommu/iova: Add free_all_cpu_cached_iovas()
Add a helper function to free the CPU rcache for all online CPUs.
There also exists a function of the same name in drivers/iommu/intel/iommu.c, but the parameters are different, and there should be no conflict.
--- drivers/iommu/iova.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/iommu/iova.c +9 -4
@@ -227,6 +227,14 @@ static int __alloc_and_insert_iova_range(struct iova_domain *iovad,return -ENOMEM ; } +static void free_all_cpu_cached_iovas (struct iova_domain *iovad) +{ +unsigned int cpu; + +for_each_online_cpu (cpu) +free_cpu_cached_iovas (cpu, iovad); +} +static struct kmem_cache *iova_cache;static unsigned int iova_cache_users;static DEFINE_MUTEX (iova_cache_mutex);
@@ -422,15 +430,12 @@ alloc_iova_fast(struct iova_domain *iovad, unsigned long size, retry: new_iova =alloc_iova (iovad, size, limit_pfn, true);if (!new_iova) { -unsigned int cpu; -if (!flush_rcache)return 0 ;/* Try replenishing IOVAs by flushing rcache. */ flush_rcache = false; -for_each_online_cpu (cpu) -free_cpu_cached_iovas (cpu, iovad); +free_all_cpu_cached_iovas (iovad); goto retry; }
Signature · 1 line
2.26.2
PATCH v3 2/4 iommu/iova: Avoid double-negatives in magazine helpers
In reply to John GarryA similar crash to the following could be observed if initial CPU rcache magazine allocations fail in init_iova_rcaches():
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
Mem abort info:
ESR = 0x96000004
EC = 0x25: DABT (current EL), IL = 32 bits SET = 0, FnV = 0
EA = 0, S1PTW = 0
Data abort info:
ISV = 0, ISS = 0x00000004
CM = 0, WnR = 0
[0000000000000000] user address but active_mm is swapper
Internal error: Oops: 96000004 [#1] PREEMPT SMP Modules linked in: CPU: 11 PID: 696 Comm: irq/40-hisi_sas Not tainted 5.9.0-rc7-dirty #109 Hardware name: Huawei D06 /D06, BIOS Hisilicon D06 UEFI RC0 - V1.16.01 03/15/2019 Call trace: free_iova_fast+0xfc/0x280 iommu_dma_free_iova+0x64/0x70 __iommu_dma_unmap+0x9c/0xf8 iommu_dma_unmap_sg+0xa8/0xc8 dma_unmap_sg_attrs+0x28/0x50 cq_thread_v3_hw+0x2dc/0x528 irq_thread_fn+0x2c/0xa0 irq_thread+0x130/0x1e0 kthread+0x154/0x158 ret_from_fork+0x10/0x34 Code: f9400060 f102001f 54000981 d4210000 (f9400043) ---[ end trace 4afcbdfc61b60467 ]---
The issue is that expression !iova_magazine_full(NULL) evaluates true; this falls over in in __iova_rcache_insert() when we attempt to cache a mag and cpu_rcache->loaded == NULL:
if (!iova_magazine_full(cpu_rcache->loaded)) {
can_insert = true;...
if (can_insert)
iova_magazine_push(cpu_rcache->loaded, iova_pfn);
As above, can_insert is evaluated true, which it shouldn't be, and we try to insert pfns in a NULL mag, which is not safe.
To avoid this, stop using double-negatives, like !iova_magazine_full() and !iova_magazine_empty(), and use positive tests, like iova_magazine_has_space() and iova_magazine_has_pfns(), respectively; these can safely deal with cpu_rcache->{loaded, prev} = NULL.
--- drivers/iommu/iova.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)
drivers/iommu/iova.c +17 -12
@@ -827,14 +827,18 @@ iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad) mag->size =0 ; } -static bool iova_magazine_full (struct iova_magazine *mag) +static bool iova_magazine_has_space (struct iova_magazine *mag) { -return (mag && mag->size ==IOVA_MAG_SIZE ); +if (!mag) +return false; +return mag->size <IOVA_MAG_SIZE ; } -static bool iova_magazine_empty (struct iova_magazine *mag) +static bool iova_magazine_has_pfns (struct iova_magazine *mag) { -return (!mag || mag->size ==0 ); +if (!mag) +return false; +return mag->size; } static unsigned long iova_magazine_pop(struct iova_magazine *mag,
@@ -843,7 +847,7 @@ static unsigned long iova_magazine_pop(struct iova_magazine *mag,int i;unsigned long pfn; -BUG_ON (iova_magazine_empty (mag)); +BUG_ON (!iova_magazine_has_pfns (mag));/* Only fall back to the rbtree if we have no suitable pfns at all */ for (i = mag->size -1 ; mag->pfns[i] > limit_pfn; i--)
@@ -859,7 +863,7 @@ static unsigned long iova_magazine_pop(struct iova_magazine *mag,static void iova_magazine_push (struct iova_magazine *mag,unsigned long pfn) { -BUG_ON (iova_magazine_full (mag)); +BUG_ON (!iova_magazine_has_space (mag)); mag->pfns[mag->size++] = pfn; }
@@ -905,9 +909,9 @@ static bool __iova_rcache_insert(struct iova_domain *iovad, cpu_rcache =raw_cpu_ptr (rcache->cpu_rcaches);spin_lock_irqsave (&cpu_rcache->lock, flags); -if (!iova_magazine_full (cpu_rcache->loaded)) { +if (iova_magazine_has_space (cpu_rcache->loaded)) { can_insert = true; - }else if (!iova_magazine_full (cpu_rcache->prev)) { + }else if (iova_magazine_has_space (cpu_rcache->prev)) {swap (cpu_rcache->prev, cpu_rcache->loaded); can_insert = true; }else {
@@ -916,8 +920,9 @@ 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; +if (cpu_rcache->loaded) + rcache->depot[rcache->depot_size++] = + cpu_rcache->loaded; }else { mag_to_free = cpu_rcache->loaded; }
@@ -968,9 +973,9 @@ static unsigned long __iova_rcache_get(struct iova_rcache *rcache, cpu_rcache =raw_cpu_ptr (rcache->cpu_rcaches);spin_lock_irqsave (&cpu_rcache->lock, flags); -if (!iova_magazine_empty (cpu_rcache->loaded)) { +if (iova_magazine_has_pfns (cpu_rcache->loaded)) { has_pfn = true; - }else if (!iova_magazine_empty (cpu_rcache->prev)) { + }else if (iova_magazine_has_pfns (cpu_rcache->prev)) {swap (cpu_rcache->prev, cpu_rcache->loaded); has_pfn = true; }else {
Signature · 6 lines
2.26.2 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v3 3/4 iommu/iova: Flush CPU rcache for when a depot fills
In reply to John GarryLeizhen reported some time ago that IOVA performance may degrade over time [0], but unfortunately his solution to fix this problem was not given attention.
To summarize, the issue is that as time goes by, the CPU rcache and depot rcache continue to grow. As such, IOVA RB tree access time also continues to grow.
At a certain point, a depot may become full, and also some CPU rcaches may also be full when inserting another IOVA is attempted. For this scenario, currently the "loaded" CPU rcache is freed and a new one is created. This freeing means that many IOVAs in the RB tree need to be freed, which makes IO throughput performance fall off a cliff in some storage scenarios:
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6314MB/0KB/0KB /s] [1616K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [5669MB/0KB/0KB /s] [1451K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6031MB/0KB/0KB /s] [1544K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6673MB/0KB/0KB /s] [1708K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6705MB/0KB/0KB /s] [1717K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6031MB/0KB/0KB /s] [1544K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6761MB/0KB/0KB /s] [1731K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6705MB/0KB/0KB /s] [1717K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6685MB/0KB/0KB /s] [1711K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6178MB/0KB/0KB /s] [1582K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [6731MB/0KB/0KB /s] [1723K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [2387MB/0KB/0KB /s] [611K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [2689MB/0KB/0KB /s] [688K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [2278MB/0KB/0KB /s] [583K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [1288MB/0KB/0KB /s] [330K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [1632MB/0KB/0KB /s] [418K/0/0 iops]
Jobs: 12 (f=12): [RRRRRRRRRRRR] [0.0% done] [1765MB/0KB/0KB /s] [452K/0/0 iops]
And continue in this fashion, without recovering. Note that in this example it was required to wait 16 hours for this to occur. Also note that IO throughput also becomes gradually becomes more unstable leading up to this point.
This problem is only seen for non-strict mode. For strict mode, the rcaches stay quite compact.
As a solution to this issue, judge that the IOVA caches have grown too big when cached magazines need to be free, and just flush all the CPUs rcaches instead.
The depot rcaches, however, are not flushed, as they can be used to immediately replenish active CPUs.
In future, some IOVA compaction could be implemented to solve the instabilty issue, which I figure could be quite complex to implement.
[0] https://lore.kernel.org/linux-iommu/[email protected]/
--- drivers/iommu/iova.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/iommu/iova.c +6 -10
@@ -901,7 +901,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;
@@ -923,13 +922,12 @@ static bool __iova_rcache_insert(struct iova_domain *iovad,if (cpu_rcache->loaded) rcache->depot[rcache->depot_size++] = cpu_rcache->loaded; - }else { - mag_to_free = cpu_rcache->loaded; + can_insert = true; + cpu_rcache->loaded = new_mag; }spin_unlock (&rcache->lock); - - cpu_rcache->loaded = new_mag; - can_insert = true; +if (!can_insert) +iova_magazine_free (new_mag); } }
@@ -938,10 +936,8 @@ 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); - } +if (!can_insert) +free_all_cpu_cached_iovas (iovad);return can_insert; }
Signature · 1 line
2.26.2
PATCH v3 4/4 iommu: avoid taking iova_rbtree_lock twice
In reply to John GarryFrom: Cong Wang <[email protected]>
Both find_iova() and __free_iova() take iova_rbtree_lock, there is no reason to take and release it twice inside free_iova().
Fold them into one critical section by calling the unlock versions instead.
--- drivers/iommu/iova.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/iommu/iova.c +6 -2
@@ -398,10 +398,14 @@ EXPORT_SYMBOL_GPL(__free_iova);void free_iova (struct iova_domain *iovad,unsigned long pfn) { -struct iova *iova =find_iova (iovad, pfn); +unsigned long flags; +struct iova *iova; +spin_lock_irqsave (&iovad->iova_rbtree_lock, flags); + iova =private_find_iova (iovad, pfn);if (iova) -__free_iova (iovad, iova); +private_free_iova (iovad, iova); +spin_unlock_irqrestore (&iovad->iova_rbtree_lock, flags); }EXPORT_SYMBOL_GPL (free_iova);
Signature · 6 lines
2.26.2 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu