2015-11-12 15:17:09

by Jerome Marchand

[permalink] [raw]
Subject: [PATCH] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
allocation") missed a spot. Currently remove_vm_area() decreases
vm->size to remove the guard hole page, even when it isn't present.
This patch only decreases vm->size when VM_NO_GUARD isn't set.

Signed-off-by: Jerome Marchand <[email protected]>
---
mm/vmalloc.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d045634..1388c3d 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1443,7 +1443,8 @@ struct vm_struct *remove_vm_area(const void *addr)
vmap_debug_free_range(va->va_start, va->va_end);
kasan_free_shadow(vm);
free_unmap_vmap_area(va);
- vm->size -= PAGE_SIZE;
+ if (!(vm->flags & VM_NO_GUARD))
+ vm->size -= PAGE_SIZE;

return vm;
}
--
2.4.3


2015-11-12 15:55:53

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

2015-11-12 18:17 GMT+03:00 Jerome Marchand <[email protected]>:
> Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
> allocation") missed a spot. Currently remove_vm_area() decreases
> vm->size to remove the guard hole page, even when it isn't present.
> This patch only decreases vm->size when VM_NO_GUARD isn't set.
>
> Signed-off-by: Jerome Marchand <[email protected]>
> ---
> mm/vmalloc.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index d045634..1388c3d 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -1443,7 +1443,8 @@ struct vm_struct *remove_vm_area(const void *addr)
> vmap_debug_free_range(va->va_start, va->va_end);
> kasan_free_shadow(vm);
> free_unmap_vmap_area(va);
> - vm->size -= PAGE_SIZE;
> + if (!(vm->flags & VM_NO_GUARD))
> + vm->size -= PAGE_SIZE;
>

I'd fix this in another way. I think that remove_vm_area() shouldn't
change vm's size, IMO it doesn't make sense.
The only caller who cares about vm's size after removing is __vunmap():
area = remove_vm_area(addr);
....
debug_check_no_locks_freed(addr, area->size);
debug_check_no_obj_freed(addr, area->size);

We already have proper get_vm_area_size() helper which takes
VM_NO_GUARD into account.
So I think we should use that helper for debug_check_no_*() and just
remove 'vm->size -= PAGE_SIZE;' line
from remove_vm_area()



> return vm;
> }
> --
> 2.4.3
>

2015-11-12 16:28:57

by Jerome Marchand

[permalink] [raw]
Subject: Re: [PATCH] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

On 11/12/2015 04:55 PM, Andrey Ryabinin wrote:
> 2015-11-12 18:17 GMT+03:00 Jerome Marchand <[email protected]>:
>> Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
>> allocation") missed a spot. Currently remove_vm_area() decreases
>> vm->size to remove the guard hole page, even when it isn't present.
>> This patch only decreases vm->size when VM_NO_GUARD isn't set.
>>
>> Signed-off-by: Jerome Marchand <[email protected]>
>> ---
>> mm/vmalloc.c | 3 ++-
>> 1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
>> index d045634..1388c3d 100644
>> --- a/mm/vmalloc.c
>> +++ b/mm/vmalloc.c
>> @@ -1443,7 +1443,8 @@ struct vm_struct *remove_vm_area(const void *addr)
>> vmap_debug_free_range(va->va_start, va->va_end);
>> kasan_free_shadow(vm);
>> free_unmap_vmap_area(va);
>> - vm->size -= PAGE_SIZE;
>> + if (!(vm->flags & VM_NO_GUARD))
>> + vm->size -= PAGE_SIZE;
>>
>
> I'd fix this in another way. I think that remove_vm_area() shouldn't
> change vm's size, IMO it doesn't make sense.
> The only caller who cares about vm's size after removing is __vunmap():
> area = remove_vm_area(addr);
> ....
> debug_check_no_locks_freed(addr, area->size);
> debug_check_no_obj_freed(addr, area->size);
>
> We already have proper get_vm_area_size() helper which takes
> VM_NO_GUARD into account.
> So I think we should use that helper for debug_check_no_*() and just
> remove 'vm->size -= PAGE_SIZE;' line
> from remove_vm_area()

Sure, that would be cleaner.

Btw, there might be a leak in sq_unmap() (arch/sh/kernel/cpu/sh4/sq.c)
as the vm_struct doesn't seem to be freed. CCed the SuperH folks.

Thanks,
Jerome

>
>
>
>> return vm;
>> }
>> --
>> 2.4.3
>>



Attachments:
signature.asc (473.00 B)
OpenPGP digital signature

2015-11-12 16:37:22

by Jerome Marchand

[permalink] [raw]
Subject: [PATCH V2] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
allocation") missed a spot. Currently remove_vm_area() decreases
vm->size to "remove" the guard hole page, even when it isn't present.
All but one users just free the vm_struct rigth away and never access
vm->size anyway.
Don't touch the size in remove_vm_area() and have __vunmap() use the
proper get_vm_area_size() helper.

Signed-off-by: Jerome Marchand <[email protected]>
---
mm/vmalloc.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index d045634..8e3c9c5 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -1443,7 +1443,6 @@ struct vm_struct *remove_vm_area(const void *addr)
vmap_debug_free_range(va->va_start, va->va_end);
kasan_free_shadow(vm);
free_unmap_vmap_area(va);
- vm->size -= PAGE_SIZE;

return vm;
}
@@ -1468,8 +1467,8 @@ static void __vunmap(const void *addr, int deallocate_pages)
return;
}

- debug_check_no_locks_freed(addr, area->size);
- debug_check_no_obj_freed(addr, area->size);
+ debug_check_no_locks_freed(addr, get_vm_area_size(area));
+ debug_check_no_obj_freed(addr, get_vm_area_size(area));

if (deallocate_pages) {
int i;
--
2.4.3

2015-11-12 18:48:40

by Andrey Ryabinin

[permalink] [raw]
Subject: Re: [PATCH V2] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

2015-11-12 19:37 GMT+03:00 Jerome Marchand <[email protected]>:
> Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
> allocation") missed a spot. Currently remove_vm_area() decreases
> vm->size to "remove" the guard hole page, even when it isn't present.
> All but one users just free the vm_struct rigth away and never access
> vm->size anyway.
> Don't touch the size in remove_vm_area() and have __vunmap() use the
> proper get_vm_area_size() helper.
>
> Signed-off-by: Jerome Marchand <[email protected]>

Acked-by: Andrey Ryabinin <[email protected]>

2015-11-12 20:56:00

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH V2] mm: vmalloc: don't remove inexistent guard hole in remove_vm_area()

On Thu, 12 Nov 2015, Jerome Marchand wrote:

> Commit 71394fe50146 ("mm: vmalloc: add flag preventing guard hole
> allocation") missed a spot. Currently remove_vm_area() decreases
> vm->size to "remove" the guard hole page, even when it isn't present.
> All but one users just free the vm_struct rigth away and never access
> vm->size anyway.
> Don't touch the size in remove_vm_area() and have __vunmap() use the
> proper get_vm_area_size() helper.
>
> Signed-off-by: Jerome Marchand <[email protected]>

Acked-by: David Rientjes <[email protected]>