Skip to content

Commit 70d1eb0

Browse files
keesakpm00
authored andcommitted
mm: vmalloc: only zero-init on vrealloc shrink
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: a0309fa ("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>
1 parent f7a35a3 commit 70d1eb0

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
@@ -4093,8 +4093,8 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
40934093
* would be a good heuristic for when to shrink the vm_area?
40944094
*/
40954095
if (size <= old_size) {
4096-
/* Zero out "freed" memory. */
4097-
if (want_init_on_free())
4096+
/* Zero out "freed" memory, potentially for future realloc. */
4097+
if (want_init_on_free() || want_init_on_alloc(flags))
40984098
memset((void *)p + size, 0, old_size - size);
40994099
vm->requested_size = size;
41004100
kasan_poison_vmalloc(p + size, old_size - size);
@@ -4107,9 +4107,11 @@ void *vrealloc_noprof(const void *p, size_t size, gfp_t flags)
41074107
if (size <= alloced_size) {
41084108
kasan_unpoison_vmalloc(p + old_size, size - old_size,
41094109
KASAN_VMALLOC_PROT_NORMAL);
4110-
/* Zero out "alloced" memory. */
4111-
if (want_init_on_alloc(flags))
4112-
memset((void *)p + old_size, 0, size - old_size);
4110+
/*
4111+
* No need to zero memory here, as unused memory will have
4112+
* already been zeroed at initial allocation time or during
4113+
* realloc shrink time.
4114+
*/
41134115
vm->requested_size = size;
41144116
return (void *)p;
41154117
}

0 commit comments

Comments
 (0)