Skip to contentSkip to conversation

mm: vmalloc: Remove double execution of vunmap_page_range

PATCH In review Chintan Pandya 3 participants 9 messages Apr 13, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: vmalloc: Remove double execution of vunmap_page_range

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

---
 mm/vmalloc.c | 23 +----------------------
 1 file changed, 1 insertion(+), 22 deletions(-)
mm/vmalloc.c +1 -22
index ebff729..920378a 100644
@@ -603,26 +603,6 @@ static void unmap_vmap_area(struct vmap_area *va)
 	vunmap_page_range(va->va_start, va->va_end);
 }
 
-static void vmap_debug_free_range(unsigned long start, unsigned long end)
-{
-	/*
-	 * Unmap page tables and force a TLB flush immediately if pagealloc
-	 * debugging is enabled.  This catches use after free bugs similarly to
-	 * those in linear kernel virtual address space after a page has been
-	 * freed.
-	 *
-	 * All the lazy freeing logic is still retained, in order to minimise
-	 * intrusiveness of this debugging feature.
-	 *
-	 * This is going to be *slow* (linear kernel virtual address debugging
-	 * doesn't do a broadcast TLB flush so it is a lot faster).
-	 */
-	if (debug_pagealloc_enabled()) {
-		vunmap_page_range(start, end);
-		flush_tlb_kernel_range(start, end);
-	}
-}
-
 /*
  * lazy_max_pages is the maximum amount of virtual address space we gather up
  * before attempting to purge with a TLB flush.
@@ -756,6 +736,7 @@ static void free_unmap_vmap_area(struct vmap_area *va)
 {
 	flush_cache_vunmap(va->va_start, va->va_end);
 	unmap_vmap_area(va);
+	flush_tlb_kernel_range(va->va_start, va->va_end);
 	free_vmap_area_noflush(va);
 }
 
@@ -1142,7 +1123,6 @@ void vm_unmap_ram(const void *mem, unsigned int count)
 	BUG_ON(!PAGE_ALIGNED(addr));
 
 	debug_check_no_locks_freed(mem, size);
-	vmap_debug_free_range(addr, addr+size);
 
 	if (likely(count <= VMAP_MAX_ALLOC)) {
 		vb_free(mem, size);
@@ -1499,7 +1479,6 @@ struct vm_struct *remove_vm_area(const void *addr)
 		va->flags |= VM_LAZY_FREE;
 		spin_unlock(&vmap_area_lock);
 
-		vmap_debug_free_range(va->va_start, va->va_end);
 		kasan_free_shadow(vm);
 		free_unmap_vmap_area(va);
 
Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation
Center, Inc., is a member of Code Aurora Forum, a Linux Foundation
Collaborative Project
#1 lore raw
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

#2 lore raw
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Chintan

Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project
#3 lore raw
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 5 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

#4 lore raw
Quoted 28 lines · Anshuman Khandual
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 3 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Quoted 7 lines
Quoted 2 lines

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

Okay. I will raise v2 for that.

Chintan

Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project
#5 lore raw
Quoted 31 lines · Chintan Pandya
Quoted 28 lines · Anshuman Khandual
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 3 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

Okay. I will raise v2 for that.

More importantly. Your changelog absolutely lacks the _why_ part. It just states what the code does which is not all that hard to read from the diff. It is usually much more important to present _why_ the patch is an improvement and worth merging.

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw
Quoted 39 lines · Michal Hocko
Quoted 38 lines · Chintan Pandya
Quoted 35 lines · Anshuman Khandual
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 5 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

Quoted 7 lines
Quoted 1 line

Okay. I will raise v2 for that.

More importantly. Your changelog absolutely lacks the _why_ part. It just states what the code does which is not all that hard to read from the diff. It is usually much more important to present _why_ the patch is an improvement and worth merging.

It is improving performance in debug scenario. More than that, I see it as a clean up. Sure, I will try to address *why* in next change log.

Chintan

Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project
#7 lore raw
Quoted 44 lines · Chintan Pandya
Quoted 41 lines · Michal Hocko
Quoted 39 lines · Chintan Pandya
Quoted 28 lines · Anshuman Khandual
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 3 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Quoted 7 lines
Quoted 2 lines

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

Okay. I will raise v2 for that.

Quoted 7 lines
Quoted 5 lines

More importantly. Your changelog absolutely lacks the _why_ part. It just states what the code does which is not all that hard to read from the diff. It is usually much more important to present _why_ the patch is an improvement and worth merging.

It is improving performance in debug scenario.

Do not forget to add some numbers presenting the benefits when resubmitting.

Quoted 2 lines

More than that, I see it
as a clean up. Sure, I will try to address *why* in next change log.

As Anshuman pointed out the current code layout is deliberate. If you believe that reasons mentioned previously are not valid then dispute them and provide your arguments in the changelog.

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw
Quoted 51 lines · Michal Hocko
Quoted 50 lines · Chintan Pandya
Quoted 46 lines · Michal Hocko
Quoted 31 lines · Chintan Pandya
Quoted 28 lines · Anshuman Khandual
Quoted 15 lines · Chintan Pandya
Quoted 5 lines · Anshuman Khandual
Quoted 3 lines · Chintan Pandya

Unmap legs do call vunmap_page_range() irrespective of debug_pagealloc_enabled() is enabled or not. So, remove redundant check and optional vunmap_page_range() routines.

Quoted 7 lines

vunmap_page_range() tears down the page table entries and does not really flush related TLB entries normally unless page alloc debug is enabled where it wants to make sure no stale mapping is still around for debug purpose. Deferring TLB flush improves performance. This patch will force TLB flush during each page table tear down and hence not desirable.

Quoted 2 lines

Deferred TLB invalidation will surely improve performance. But force flush can help in detecting invalid access right then and there. I

Deferred TLB invalidation was a choice made some time ago with the commit db64fe02258f1507e ("mm: rewrite vmap layer") as these vmalloc mappings wont be used other than inside the kernel and TLB gets flushed when they are reused. This way it can still avail the benefit of deferred TLB flushing without exposing itself to invalid accesses.

Quoted 3 lines

chose later. May be I should have clean up the vmap tear down code as well where it actually does the TLB invalidation.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Or make TLB invalidation in free_unmap_vmap_area() be dependent upon debug_pagealloc_enabled().

Immediate TLB invalidation needs to be dependent on debug_pagealloc_ enabled() and should be done only for debug purpose. Contrary to that is not desirable.

Okay. I will raise v2 for that.

More importantly. Your changelog absolutely lacks the _why_ part. It just states what the code does which is not all that hard to read from the diff. It is usually much more important to present _why_ the patch is an improvement and worth merging.

Quoted 4 lines
Quoted 1 line

It is improving performance in debug scenario.

Do not forget to add some numbers presenting the benefits when resubmitting.

Okay.

Quoted 7 lines
Quoted 3 lines

More than that, I see it
as a clean up. Sure, I will try to address *why* in next change log. >

As Anshuman pointed out the current code layout is deliberate. If you believe that reasons mentioned previously are not valid then dispute them and provide your arguments in the changelog.

Here, the trade off is, performance vs catching use-after-free. Original code is preferring performance gains. At first, it seemed to me that stability is more important than performance. But giving more thoughts on this (and reading commit db64fe02258f1507e ("mm: rewrite vmap layer")), I feel that use-after-free is client side wrong-doing. vmap layer need not loose its best case settings for potential client side mistakes. For that, vmap layer can provide debug settings. So, I plan to do TLB flush conditional on debug settings.

Chintan

Signature · 3 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project