mm: vmalloc fix lazy unmapping cache aliasing
Subsystems (3)
PATCH 1/1 mm: vmalloc fix lazy unmapping cache aliasing
Jim Radford has reported that the vmap subsystem rewrite was sometimes causing his VIVT ARM system to behave strangely (seemed like going into infinite loops trying to fault in pages to userspace).
We determined that the problem was most likely due to a cache aliasing issue. flush_cache_vunmap was only being called at the moment the page tables were to be taken down, however with lazy unmapping, this can happen after the page has subsequently been freed and allocated for something else. The dangling alias may still have dirty data attached to it.
The fix for this problem is to do the cache flushing when the caller has called vunmap -- it would be a bug for them to write anything else to the mapping at that point.
That appeared to solve Jim's problems.
---
mm/vmalloc.c +15 -4
@@ -77,7 +77,6 @@ static void vunmap_page_range(unsigned lBUG_ON (addr >= end); pgd =pgd_offset_k (addr); -flush_cache_vunmap (addr, end); do { next = pgd_addr_end(addr, end); if (pgd_none_or_clear_bad(pgd))
@@ -543,9 +542,10 @@ static void purge_vmap_area_lazy(void) }/* -* Free and unmap a vmap area +* Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been +* called for the correct range previously. */ -static void free_unmap_vmap_area (struct vmap_area *va) +static void free_unmap_vmap_area_noflush (struct vmap_area *va) { va->flags |=VM_LAZY_FREE ;atomic_add ((va->va_end - va->va_start) >>PAGE_SHIFT , &vmap_lazy_nr);
@@ -553,6 +553,15 @@ static void free_unmap_vmap_area(structtry_purge_vmap_area_lazy (); } +/* +* Free and unmap a vmap area +*/ +static void free_unmap_vmap_area (struct vmap_area *va) +{ +flush_cache_vunmap (va->va_start, va->va_end); +free_unmap_vmap_area_noflush (va); +} +static struct vmap_area *find_vmap_area (unsigned long addr) {struct vmap_area *va;
@@ -734,7 +743,7 @@ static void free_vmap_block(struct vmap_spin_unlock (&vmap_block_tree_lock);BUG_ON (tmp != vb); -free_unmap_vmap_area (vb->va); +free_unmap_vmap_area_noflush (vb->va);call_rcu (&vb->rcu_head, rcu_free_vb); }
@@ -820,6 +829,8 @@ static void vb_free(const void *addr, unfree_vmap_block (vb); }else spin_unlock (&vb->lock); + +flush_cache_vunmap ((unsigned long )addr, (unsigned long )addr + size); }/**
Added Jim to CC...
Quoted 79 lines · Nick Piggin
Jim Radford has reported that the vmap subsystem rewrite was sometimes causing his VIVT ARM system to behave strangely (seemed like going into infinite loops trying to fault in pages to userspace).
We determined that the problem was most likely due to a cache aliasing issue.
flush_cache_vunmapwas only being called at the moment the page tables were to be taken down, however with lazy unmapping, this can happen after the page has subsequently been freed and allocated for something else. The dangling alias may still have dirty data attached to it.The fix for this problem is to do the cache flushing when the caller has called vunmap -- it would be a bug for them to write anything else to the mapping at that point.
That appeared to solve Jim's problems.
Reported-by Jim Radford <[email protected]>Signed-off-by Nick Piggin <[email protected]>---
Index:linux-2.6/mm/vmalloc.c
===================================================================mm/vmalloc.c +15
-4@@ -77,7 +77,6 @@ static void vunmap_page_range(unsigned lBUG_ON (addr >= end); pgd =pgd_offset_k (addr); -flush_cache_vunmap (addr, end); do { next = pgd_addr_end(addr, end); if (pgd_none_or_clear_bad(pgd))@@ -543,9 +542,10 @@ static void purge_vmap_area_lazy(void) }/* -* Free and unmap a vmap area +* Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been +* called for the correct range previously. */ -static void free_unmap_vmap_area (struct vmap_area *va) +static void free_unmap_vmap_area_noflush (struct vmap_area *va) { va->flags |=VM_LAZY_FREE ;atomic_add ((va->va_end - va->va_start) >>PAGE_SHIFT , &vmap_lazy_nr);@@ -553,6 +553,15 @@ static void free_unmap_vmap_area(structtry_purge_vmap_area_lazy (); } +/* +* Free and unmap a vmap area +*/ +static void free_unmap_vmap_area (struct vmap_area *va) +{ +flush_cache_vunmap (va->va_start, va->va_end); +free_unmap_vmap_area_noflush (va); +} +static struct vmap_area *find_vmap_area (unsigned long addr) {struct vmap_area *va;@@ -734,7 +743,7 @@ static void free_vmap_block(struct vmap_spin_unlock (&vmap_block_tree_lock);BUG_ON (tmp != vb); -free_unmap_vmap_area (vb->va); +free_unmap_vmap_area_noflush (vb->va);call_rcu (&vb->rcu_head, rcu_free_vb); }@@ -820,6 +829,8 @@ static void vb_free(const void *addr, unfree_vmap_block (vb); }else spin_unlock (&vb->lock); + +flush_cache_vunmap ((unsigned long )addr, (unsigned long )addr + size); }/**
Quoted 11 lines · Nick Piggin
Quoted 9 lines · Nick Piggin
Jim Radford has reported that the vmap subsystem rewrite was sometimes causing his VIVT ARM system to behave strangely (seemed like going into infinite loops trying to fault in pages to userspace).
We determined that the problem was most likely due to a cache aliasing issue.
flush_cache_vunmapwas only being called at the moment the page tables were to be taken down, however with lazy unmapping, this can happen after the page has subsequently been freed and allocated for something else. The dangling alias may still have dirty data attached to it.
Quoted 5 lines
Quoted 3 lines
The fix for this problem is to do the cache flushing when the caller has called vunmap -- it would be a bug for them to write anything else to the mapping at that point.
Added Jim to CC...
Thanks Nick. I just wanted to confirm that this latest version of the patch works for me as well.
-Jim
Quoted 6 lines · Nick Piggin
We determined that the problem was most likely due to a cache aliasing issue.
flush_cache_vunmapwas only being called at the moment the page tables were to be taken down, however with lazy unmapping, this can happen after the page has subsequently been freed and allocated for something else. The dangling alias may still have dirty data attached to it.
This would certainly cause problems - writes which hit the cache for the vmapped pages will remain in the cache, and would be evicted some time later.
With VIVT ARMs, that time is bounded by the context switch, which does a full cache flush, but between that a page may well have been reallocated for some other purpose.
So, ensuring that we have no dirty lines corresponding to stale mappings on pages before they're freed is an absolute must for VIVT caches.
Quoted 10 lines
mm/vmalloc.c +1
-1@@ -734,7 +743,7 @@ static void free_vmap_block(struct vmap_spin_unlock (&vmap_block_tree_lock);BUG_ON (tmp != vb); -free_unmap_vmap_area (vb->va); +free_unmap_vmap_area_noflush (vb->va);call_rcu (&vb->rcu_head, rcu_free_vb); }
mm/vmalloc.c static void vb_free(const void *addr, un · line 833
Quoted 5 lines
mm/vmalloc.c +2
-0@@ -820,6 +829,8 @@ static void vb_free(const void *addr, unfree_vmap_block (vb); }else spin_unlock (&vb->lock); + +flush_cache_vunmap ((unsigned long )addr, (unsigned long )addr + size);
One minor nit - this looks like we're flushing the cache after the range has been marked as needing the mapping torn down. Can the mapping be torn down before the cache is flushed?
The reason I ask is that with VIPT caches, the mapping has to be present so the hardware knows the physical tag to be flushed, so if the mapping has been torn down, we'll get an exception. _If_ and only if we convert flush_cache_vunmap() to be less heavy weight than its current flush_cache_all().
IOW, shouldn't flush_cache_vunmap() be towards the start of vb_free() ?
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of:
Quoted 39 lines · Russell King
Quoted 6 lines · Nick Piggin
We determined that the problem was most likely due to a cache aliasing issue.
flush_cache_vunmapwas only being called at the moment the page tables were to be taken down, however with lazy unmapping, this can happen after the page has subsequently been freed and allocated for something else. The dangling alias may still have dirty data attached to it.This would certainly cause problems - writes which hit the cache for the vmapped pages will remain in the cache, and would be evicted some time later.
With VIVT ARMs, that time is bounded by the context switch, which does a full cache flush, but between that a page may well have been reallocated for some other purpose.
So, ensuring that we have no dirty lines corresponding to stale mappings on pages before they're freed is an absolute must for VIVT caches.
Quoted 10 lines
(quoted diff) +1
-1@@ -734,7 +743,7 @@ static void free_vmap_block(struct vmap_ spin_unlock(&vmap_block_tree_lock); BUG_ON(tmp != vb); - free_unmap_vmap_area(vb->va); + free_unmap_vmap_area_noflush(vb->va); call_rcu(&vb->rcu_head, rcu_free_vb); }Review static void vb_free(const void *addr, un · line 833
Quoted 5 lines
(quoted diff) +2
-0@@ -820,6 +829,8 @@ static void vb_free(const void *addr, un free_vmap_block(vb); } else spin_unlock(&vb->lock); + + flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);One minor nit - this looks like we're flushing the cache after the range has been marked as needing the mapping torn down. Can the mapping be torn down before the cache is flushed?
Quoted 7 lines
The reason I ask is that with VIPT caches, the mapping has to be present so the hardware knows the physical tag to be flushed, so if the mapping has been torn down, we'll get an exception. _If_ and only if we convert
flush_cache_vunmap()to be less heavy weight than its currentflush_cache_all().IOW, shouldn't
flush_cache_vunmap()be towards the start ofvb_free()?
You are correct. Thanks.
Andrew, can you queue this incremental fix please?
--
Quoted 11 lines · Russell King
One minor nit - this looks like we're flushing the cache after the range has been marked as needing the mapping torn down. Can the mapping be torn down before the cache is flushed?
The reason I ask is that with VIPT caches, the mapping has to be present so the hardware knows the physical tag to be flushed, so if the mapping has been torn down, we'll get an exception. _If_ and only if we convert
flush_cache_vunmap()to be less heavy weight than its currentflush_cache_all().IOW, shouldn't
flush_cache_vunmap()be towards the start ofvb_free()?
---
mm/vmalloc.c +3 -2
@@ -805,6 +805,9 @@ static void vb_free(const void *addr, unBUG_ON (size & ~PAGE_MASK );BUG_ON (size >PAGE_SIZE *VMAP_MAX_ALLOC ); + +flush_cache_vunmap ((unsigned long )addr, (unsigned long )addr + size); + order =get_order (size); offset = (unsigned long )addr & (VMAP_BLOCK_SIZE -1 );
@@ -829,8 +832,6 @@ static void vb_free(const void *addr, unfree_vmap_block (vb); }else spin_unlock (&vb->lock); - -flush_cache_vunmap ((unsigned long )addr, (unsigned long )addr + size); }/**
Quoted 5 lines · Russell King
The reason I ask is that with VIPT caches, the mapping has to be present so the hardware knows the physical tag to be flushed, so if the mapping has been torn down, we'll get an exception. _If_ and only if we convert
flush_cache_vunmap()to be less heavy weight than its currentflush_cache_all().
I posted a patch for simplifying flush_cache_v(un)map some time ago but only for the VIPT non-aliasing case. I think we still need it to do cache flushing for the VIPT aliasing systems.
Signature · 7 lines
Catalin ------------------------------------------------------------------- List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm-kernel FAQ: http://www.arm.linux.org.uk/mailinglists/faq.php Etiquette: http://www.arm.linux.org.uk/mailinglists/etiquette.php