Skip to content

Commit

Permalink
FROMLIST: kfence: move the size check to the beginning of __kfence_al…
Browse files Browse the repository at this point in the history
…loc()

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.

Suggested-by: Marco Elver <[email protected]>
Cc: Andrew Morton <[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: Alexander Potapenko <[email protected]>
Reviewed-by: Marco Elver <[email protected]>

Bug: 192294212
Test: ran KFENCE test suite with __GFP_DMA on QEMU
Link: https://lore.kernel.org/linux-mm/[email protected]/
Change-Id: Ie69c6134fc1c3e68238457b146e26355c17f5295
Signed-off-by: Alexander Potapenko <[email protected]>
  • Loading branch information
ramosian-glider committed Jul 16, 2021
1 parent ddabf14 commit 8478d8d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mm/kfence/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,13 @@ void kfence_shutdown_cache(struct kmem_cache *s)

void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
{
/*
* Perform size check before switching kfence_allocation_gate, so that
* we don't disable KFENCE without making an allocation.
*/
if (size > PAGE_SIZE)
return NULL;

/*
* allocation_gate only needs to become non-zero, so it doesn't make
* sense to continue writing to it and pay the associated contention
Expand All @@ -757,9 +764,6 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
if (!READ_ONCE(kfence_enabled))
return NULL;

if (size > PAGE_SIZE)
return NULL;

return kfence_guarded_alloc(s, size, flags);
}

Expand Down

0 comments on commit 8478d8d

Please sign in to comment.