Skip to contentSkip to conversation

kfence: skip DMA allocations

PATCH In review Alexander Potapenko 3 participants 4 messages Jun 29, 2021 – Jun 30, 2021

Subsystems (3)
lore
Share & tools
lore

Alexander Potapenko

author maintainer
#0 lore raw

PATCH v2 1/1 kfence: skip DMA allocations

Allocation requests with __GFP_DMA/__GFP_DMA32 or SLAB_CACHE_DMA/SLAB_CACHE_DMA32 cannot be fulfilled by KFENCE, because they must reside in low memory, whereas KFENCE memory pool is located in high memory.

Skip such allocations to avoid crashes where DMAable memory is expected.

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]>

---

v2:
- added parentheses around the GFP clause, as requested by Marco

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +8 -0
index 4d21ac44d5d35..f7ce3d876bc9e 100644
@@ -760,6 +760,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
 	if (size > PAGE_SIZE)
 		return NULL;
 
+	/*
+	 * Skip DMA allocations. These must reside in the low memory, which we
+	 * cannot guarantee.
+	 */
+	if ((flags & (__GFP_DMA | __GFP_DMA32)) ||
+	    (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+		return NULL;
+
 	return kfence_guarded_alloc(s, size, flags);
 }
 
Signature · 1 line
2.32.0.93.g670b81a890-goog
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Alexander Potapenko

Allocation requests with __GFP_DMA/__GFP_DMA32 or SLAB_CACHE_DMA/SLAB_CACHE_DMA32 cannot be fulfilled by KFENCE, because they must reside in low memory, whereas KFENCE memory pool is located in high memory.

Skip such allocations to avoid crashes where DMAable memory is expected.

Quoted 7 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]>

Thanks!

Quoted 30 lines

---

v2:
- added parentheses around the GFP clause, as requested by Marco

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +8 -1
index 4d21ac44d5d35..f7ce3d876bc9e 100644
@@ -760,6 +760,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
        if (size > PAGE_SIZE)
                return NULL;
 
+       /*
+        * Skip DMA allocations. These must reside in the low memory, which we
+        * cannot guarantee.
+        */
+       if ((flags & (__GFP_DMA | __GFP_DMA32)) ||
+           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+               return NULL;
+
        return kfence_guarded_alloc(s, size, flags);
 }
 
--

2.32.0.93.g670b81a890-goog

#2 lore raw
In reply to Alexander Potapenko
Quoted 30 lines · Alexander Potapenko

Allocation requests with __GFP_DMA/__GFP_DMA32 or SLAB_CACHE_DMA/SLAB_CACHE_DMA32 cannot be fulfilled by KFENCE, because they must reside in low memory, whereas KFENCE memory pool is located in high memory.

Skip such allocations to avoid crashes where DMAable memory is expected.

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]>

---

v2:
- added parentheses around the GFP clause, as requested by Marco

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +0 -0
index 4d21ac44d5d35..f7ce3d876bc9e 100644
@@ -760,6 +760,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
        if (size > PAGE_SIZE)

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

Quoted 10 lines
mm/kfence/core.c +8 -0
                return NULL;
 
+       /*
+        * Skip DMA allocations. These must reside in the low memory, which we
+        * cannot guarantee.
+        */
+       if ((flags & (__GFP_DMA | __GFP_DMA32)) ||
+           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+               return NULL;
+

I prefer to move this check at the top of the function. Although it won't make much difference except avoiding atomic operations in case this condition is true.

Quoted 7 lines
mm/kfence/core.c +0 -1
        return kfence_guarded_alloc(s, size, flags);
 }
 
--

2.32.0.93.g670b81a890-goog

author maintainer
#3 lore raw
Quoted 34 lines · Souptick Joarder
Quoted 32 lines · Alexander Potapenko

Allocation requests with __GFP_DMA/__GFP_DMA32 or SLAB_CACHE_DMA/SLAB_CACHE_DMA32 cannot be fulfilled by KFENCE, because they must reside in low memory, whereas KFENCE memory pool is located in high memory.

Skip such allocations to avoid crashes where DMAable memory is expected.

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]>

---

v2:
- added parentheses around the GFP clause, as requested by Marco

---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/kfence/core.c +0 -0
index 4d21ac44d5d35..f7ce3d876bc9e 100644
@@ -760,6 +760,14 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
        if (size > PAGE_SIZE)
                return NULL;
 
Quoted 12 lines

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

Quoted 8 lines
mm/kfence/core.c +8 -0
+       /*
+        * Skip DMA allocations. These must reside in the low memory, which we
+        * cannot guarantee.
+        */
+       if ((flags & (__GFP_DMA | __GFP_DMA32)) ||
+           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+               return NULL;
+

I prefer to move this check at the top of the function. Although it won't make much difference except avoiding atomic operations in case this condition is true.

Agreed, we probably shouldn't be expecting a constant flow of allocations from these zones that will be slowed down by this check. On a related note, Marco suggested moving the PAGE_SIZE check to the top of the function as well.

It will also make sense to check for GFP_ZONEMASK instead of just GFP DMA flags. I couldn't see anyone passing e.g. __GFP_HIGHMEM or __GFP_MOVABLE to kmem_cache_alloc(), but according to mm/slab.c it is possible, so just to be on the safe side we'd better ignore them as well.

Quoted 7 lines
Quoted 7 lines
mm/kfence/core.c +0 -1
        return kfence_guarded_alloc(s, size, flags);
 }
 
--

2.32.0.93.g670b81a890-goog

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg