mm: vmalloc: only zero-init on vrealloc shrink

commit 70d1eb031a68cbde4eed8099674be21778441c94 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/20250515214217.619685-2-kees@kernel.org
Fixes: a0309faf1cb0 ("mm: vmalloc: support more granular vrealloc() sizing")
Signed-off-by: Kees Cook <kees@kernel.org>
Tested-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Eduard Zingerman <eddyz87@gmail.com>
Cc: "Erhard F." <erhard_f@mailbox.org>
Cc: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Cc: "Uladzislau Rezki (Sony)" <urezki@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Kees Cook 2025-05-15 14:42:16 -07:00 committed by Greg Kroah-Hartman
parent 94efb0d656
commit 483ac74183

View File

@ -4097,8 +4097,8 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
* would be a good heuristic for when to shrink the vm_area? * would be a good heuristic for when to shrink the vm_area?
*/ */
if (size <= old_size) { if (size <= old_size) {
/* Zero out "freed" memory. */ /* Zero out "freed" memory, potentially for future realloc. */
if (want_init_on_free()) if (want_init_on_free() || want_init_on_alloc(flags))
memset((void *)p + size, 0, old_size - size); memset((void *)p + size, 0, old_size - size);
vm->requested_size = size; vm->requested_size = size;
kasan_poison_vmalloc(p + size, old_size - size); kasan_poison_vmalloc(p + size, old_size - size);
@ -4111,9 +4111,11 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
if (size <= alloced_size) { if (size <= alloced_size) {
kasan_unpoison_vmalloc(p + old_size, size - old_size, kasan_unpoison_vmalloc(p + old_size, size - old_size,
KASAN_VMALLOC_PROT_NORMAL); KASAN_VMALLOC_PROT_NORMAL);
/* Zero out "alloced" memory. */ /*
if (want_init_on_alloc(flags)) * No need to zero memory here, as unused memory will have
memset((void *)p + old_size, 0, size - old_size); * already been zeroed at initial allocation time or during
* realloc shrink time.
*/
vm->requested_size = size; vm->requested_size = size;
return (void *)p; return (void *)p;
} }