2018-04-13 09:18:40

by Chintan Pandya

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Chintan Pandya <[email protected]>
---
mm/vmalloc.c | 23 +----------------------
1 file changed, 1 insertion(+), 22 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index ebff729..920378a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -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);

--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation
Center, Inc., is a member of Code Aurora Forum, a Linux Foundation
Collaborative Project



2018-04-13 10:02:19

by Anshuman Khandual

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range

On 04/13/2018 02:46 PM, Chintan Pandya wrote:
> 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.


2018-04-13 10:58:01

by Anshuman Khandual

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range

On 04/13/2018 03:47 PM, Chintan Pandya wrote:
>
>
> On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
>> On 04/13/2018 02:46 PM, Chintan Pandya wrote:
>>> 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.
>>
> 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.

> 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.


2018-04-13 10:58:14

by Chintan Pandya

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range



On 4/13/2018 4:10 PM, Anshuman Khandual wrote:
> On 04/13/2018 03:47 PM, Chintan Pandya wrote:
>>
>>
>> On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
>>> On 04/13/2018 02:46 PM, Chintan Pandya wrote:
>>>> 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.
>>>
>> 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.
>
>> 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.
>
Okay. I will raise v2 for that.

Chintan
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project

2018-04-13 11:51:34

by Michal Hocko

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range

On Fri 13-04-18 16:15:26, Chintan Pandya wrote:
>
>
> On 4/13/2018 4:10 PM, Anshuman Khandual wrote:
> > On 04/13/2018 03:47 PM, Chintan Pandya wrote:
> > >
> > >
> > > On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
> > > > On 04/13/2018 02:46 PM, Chintan Pandya wrote:
> > > > > 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.
> > > >
> > > 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.
> >
> > > 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.
> >
> 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.
--
Michal Hocko
SUSE Labs

2018-04-13 12:01:12

by Chintan Pandya

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range



On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
> On 04/13/2018 02:46 PM, Chintan Pandya wrote:
>> 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.
>
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
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project

2018-04-13 12:46:52

by Chintan Pandya

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range



On 4/13/2018 5:11 PM, Michal Hocko wrote:
> On Fri 13-04-18 16:57:06, Chintan Pandya wrote:
>>
>>
>> On 4/13/2018 4:39 PM, Michal Hocko wrote:
>>> On Fri 13-04-18 16:15:26, Chintan Pandya wrote:
>>>>
>>>>
>>>> On 4/13/2018 4:10 PM, Anshuman Khandual wrote:
>>>>> On 04/13/2018 03:47 PM, Chintan Pandya wrote:
>>>>>>
>>>>>>
>>>>>> On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
>>>>>>> On 04/13/2018 02:46 PM, Chintan Pandya wrote:
>>>>>>>> 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.
>>>>>>>
>>>>>> 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.
>>>>>
>>>>>> 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.
>>>>>
>>>> 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.
>
> Do not forget to add some numbers presenting the benefits when
> resubmitting.
Okay.

>
>> 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
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project

2018-04-13 13:34:35

by Michal Hocko

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range

On Fri 13-04-18 16:57:06, Chintan Pandya wrote:
>
>
> On 4/13/2018 4:39 PM, Michal Hocko wrote:
> > On Fri 13-04-18 16:15:26, Chintan Pandya wrote:
> > >
> > >
> > > On 4/13/2018 4:10 PM, Anshuman Khandual wrote:
> > > > On 04/13/2018 03:47 PM, Chintan Pandya wrote:
> > > > >
> > > > >
> > > > > On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
> > > > > > On 04/13/2018 02:46 PM, Chintan Pandya wrote:
> > > > > > > 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.
> > > > > >
> > > > > 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.
> > > >
> > > > > 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.
> > > >
> > > 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.

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

> 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.
--
Michal Hocko
SUSE Labs

2018-04-13 13:41:39

by Chintan Pandya

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: Remove double execution of vunmap_page_range



On 4/13/2018 4:39 PM, Michal Hocko wrote:
> On Fri 13-04-18 16:15:26, Chintan Pandya wrote:
>>
>>
>> On 4/13/2018 4:10 PM, Anshuman Khandual wrote:
>>> On 04/13/2018 03:47 PM, Chintan Pandya wrote:
>>>>
>>>>
>>>> On 4/13/2018 3:29 PM, Anshuman Khandual wrote:
>>>>> On 04/13/2018 02:46 PM, Chintan Pandya wrote:
>>>>>> 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.
>>>>>
>>>> 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.
>>>
>>>> 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.
>>>
>> 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
--
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation
Collaborative Project