Skip to content

Commit 483ac74

Browse files
keesgregkh
authored andcommitted
mm: vmalloc: only zero-init on vrealloc shrink
commit 70d1eb0 upstream. The common case is to grow reallocations, and since init_on_alloc will have already zeroed the whole allocation, we only need to zero when shrinking the allocation. Link: https://lkml.kernel.org/r/[email protected] Fixes: a0309fa ("mm: vmalloc: support more granular vrealloc() sizing") Signed-off-by: Kees Cook <[email protected]> Tested-by: Pawan Gupta <[email protected]> Cc: Danilo Krummrich <[email protected]> Cc: Eduard Zingerman <[email protected]> Cc: "Erhard F." <[email protected]> Cc: Shung-Hsi Yu <[email protected]> Cc: "Uladzislau Rezki (Sony)" <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 94efb0d commit 483ac74

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

mm/vmalloc.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,8 +4097,8 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
40974097
* would be a good heuristic for when to shrink the vm_area?
40984098
*/
40994099
if (size <= old_size) {
4100-
/* Zero out "freed" memory. */
4101-
if (want_init_on_free())
4100+
/* Zero out "freed" memory, potentially for future realloc. */
4101+
if (want_init_on_free() || want_init_on_alloc(flags))
41024102
memset((void *)p + size, 0, old_size - size);
41034103
vm->requested_size = size;
41044104
kasan_poison_vmalloc(p + size, old_size - size);
@@ -4111,9 +4111,11 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
41114111
if (size <= alloced_size) {
41124112
kasan_unpoison_vmalloc(p + old_size, size - old_size,
41134113
KASAN_VMALLOC_PROT_NORMAL);
4114-
/* Zero out "alloced" memory. */
4115-
if (want_init_on_alloc(flags))
4116-
memset((void *)p + old_size, 0, size - old_size);
4114+
/*
4115+
* No need to zero memory here, as unused memory will have
4116+
* already been zeroed at initial allocation time or during
4117+
* realloc shrink time.
4118+
*/
41174119
vm->requested_size = size;
41184120
return (void *)p;
41194121
}

0 commit comments

Comments
 (0)