Skip to content

Commit 235a85c

Browse files
ramosian-glidertorvalds
authored andcommitted
kfence: move the size check to the beginning of __kfence_alloc()
Check the allocation size before toggling kfence_allocation_gate. This way allocations that can't be served by KFENCE will not result in waiting for another CONFIG_KFENCE_SAMPLE_INTERVAL without allocating anything. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Alexander Potapenko <[email protected]> Suggested-by: Marco Elver <[email protected]> Reviewed-by: Marco Elver <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Marco Elver <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: <[email protected]> [5.12+] Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 32ae8a0 commit 235a85c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mm/kfence/core.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,13 @@ void kfence_shutdown_cache(struct kmem_cache *s)
733733

734734
void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
735735
{
736+
/*
737+
* Perform size check before switching kfence_allocation_gate, so that
738+
* we don't disable KFENCE without making an allocation.
739+
*/
740+
if (size > PAGE_SIZE)
741+
return NULL;
742+
736743
/*
737744
* allocation_gate only needs to become non-zero, so it doesn't make
738745
* sense to continue writing to it and pay the associated contention
@@ -757,9 +764,6 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
757764
if (!READ_ONCE(kfence_enabled))
758765
return NULL;
759766

760-
if (size > PAGE_SIZE)
761-
return NULL;
762-
763767
return kfence_guarded_alloc(s, size, flags);
764768
}
765769

0 commit comments

Comments
 (0)