Skip to contentSkip to conversation

kfence: move the size check to the beginning of __kfence_alloc()

PATCH Superseded Alexander Potapenko 2 participants 4 messages Jun 30, 2021

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Alexander Potapenko

author maintainer
#0 lore raw

PATCH v3 1/2 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.

Cc Andrew Morton <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 mm/kfence/core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/kfence/core.c +7 -3
index 4d21ac44d5d35..33bb20d91bf6a 100644
@@ -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
@@ -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);
 }
 
Signature · 1 line
2.32.0.93.g670b81a890-goog
author maintainer
#1 lore raw

PATCH v3 2/2 kfence: skip all GFP_ZONEMASK allocations

Allocation requests outside ZONE_NORMAL (MOVABLE, HIGHMEM or DNA) cannot be fulfilled by KFENCE, because KFENCE memory pool is located in a zone different from the requested one.

Because callers of kmem_cache_alloc() may actually rely on the allocation to reside in the requested zone (e.g. memory allocations done with __GFP_DMA must be DMAable), skip all allocations done with GFP_ZONEMASK and/or respective SLAB flags (SLAB_CACHE_DMA and SLAB_CACHE_DMA32).

Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Andrew Morton <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Souptick Joarder <[email protected]>

---

v2:
- added parentheses around the GFP clause, as requested by Marco
v3:
- ignore GFP_ZONEMASK, which also covers __GFP_HIGHMEM and __GFP_MOVABLE
- move the flag check at the beginning of the function, as requested by Souptick Joarder

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +8 -0
index 33bb20d91bf6a..d51f77329fd3c 100644
@@ -740,6 +740,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
 	if (size > PAGE_SIZE)
 		return NULL;
 
+	/*
+	 * Skip allocations from non-default zones, including DMA. We cannot guarantee that pages
+	 * in the KFENCE pool will have the requested properties (e.g. reside in DMAable memory).
+	 */
+	if ((flags & GFP_ZONEMASK) ||
+	    (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+		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
Signature · 1 line
2.32.0.93.g670b81a890-goog
maintainer Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] kfence: skip all GFP_ZONEMASK allocations

Quoted 1 line · Alexander Potapenko

Allocation requests outside ZONE_NORMAL (MOVABLE, HIGHMEM or DNA) cannot

s/DNA/DMA/
... but probably no need to do v4 just for this (everyone knows we're not yet in the business of allocating DNA ;-)).

Quoted 9 lines

be fulfilled by KFENCE, because KFENCE memory pool is located in a zone different from the requested one.

Because callers of kmem_cache_alloc() may actually rely on the allocation to reside in the requested zone (e.g. memory allocations done with __GFP_DMA must be DMAable), skip all allocations done with GFP_ZONEMASK and/or respective SLAB flags (SLAB_CACHE_DMA and SLAB_CACHE_DMA32).

Quoted 8 lines
Fixes 0ce20dd84089 ("mm: add Kernel Electric-Fence infrastructure")
Cc Andrew Morton <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Souptick Joarder <[email protected]>

With the change below, you can add:

Quoted 17 lines

---

v2:
- added parentheses around the GFP clause, as requested by Marco
v3:
- ignore GFP_ZONEMASK, which also covers __GFP_HIGHMEM and __GFP_MOVABLE
- move the flag check at the beginning of the function, as requested by Souptick Joarder

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +0 -0
index 33bb20d91bf6a..d51f77329fd3c 100644

mm/kfence/core.c void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) · line 745

Quoted 6 lines
mm/kfence/core.c +3 -0
@@ -740,6 +740,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
        if (size > PAGE_SIZE)
                return NULL;
 
+       /*
+        * Skip allocations from non-default zones, including DMA. We cannot guarantee that pages
+        * in the KFENCE pool will have the requested properties (e.g. reside in DMAable memory).

Comments should still be 80 cols, like the rest of the file. :-/

Quoted 11 lines
mm/kfence/core.c +5 -1
+        */
+       if ((flags & GFP_ZONEMASK) ||
+           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+               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
--

2.32.0.93.g670b81a890-goog

maintainer Reviewed-by on v3 1/2
#3 lore raw
In reply to Alexander Potapenko
Quoted 6 lines · Alexander Potapenko

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.

Quoted 7 lines
Cc Andrew Morton <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Marco Elver <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Quoted 35 lines
---
 mm/kfence/core.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/kfence/core.c +7 -4
index 4d21ac44d5d35..33bb20d91bf6a 100644
@@ -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
@@ -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);
 }
 
--

2.32.0.93.g670b81a890-goog