linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kfence: skip DMA allocations
@ 2021-06-29 16:17 Alexander Potapenko
  2021-06-29 16:40 ` Marco Elver
  2021-06-30  7:02 ` Souptick Joarder
  0 siblings, 2 replies; 4+ messages in thread
From: Alexander Potapenko @ 2021-06-29 16:17 UTC (permalink / raw)
  To: akpm
  Cc: dvyukov, elver, linux-kernel, linux-mm, stable, gregkh,
	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 <akpm@linux-foundation.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: stable@vger.kernel.org # 5.12+
Signed-off-by: Alexander Potapenko <glider@google.com>

---

v2:
 - added parentheses around the GFP clause, as requested by Marco
---
 mm/kfence/core.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/kfence/core.c b/mm/kfence/core.c
index 4d21ac44d5d35..f7ce3d876bc9e 100644
--- a/mm/kfence/core.c
+++ b/mm/kfence/core.c
@@ -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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kfence: skip DMA allocations
  2021-06-29 16:17 [PATCH v2] kfence: skip DMA allocations Alexander Potapenko
@ 2021-06-29 16:40 ` Marco Elver
  2021-06-30  7:02 ` Souptick Joarder
  1 sibling, 0 replies; 4+ messages in thread
From: Marco Elver @ 2021-06-29 16:40 UTC (permalink / raw)
  To: Alexander Potapenko; +Cc: akpm, dvyukov, linux-kernel, linux-mm, stable, gregkh

On Tue, 29 Jun 2021 at 18:17, Alexander Potapenko <glider@google.com> wrote:
> 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 <akpm@linux-foundation.org>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: stable@vger.kernel.org # 5.12+
> Signed-off-by: Alexander Potapenko <glider@google.com>

Reviewed-by: Marco Elver <elver@google.com>

Thanks!


> ---
>
> v2:
>  - added parentheses around the GFP clause, as requested by Marco
> ---
>  mm/kfence/core.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index 4d21ac44d5d35..f7ce3d876bc9e 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kfence: skip DMA allocations
  2021-06-29 16:17 [PATCH v2] kfence: skip DMA allocations Alexander Potapenko
  2021-06-29 16:40 ` Marco Elver
@ 2021-06-30  7:02 ` Souptick Joarder
  2021-06-30  9:35   ` Alexander Potapenko
  1 sibling, 1 reply; 4+ messages in thread
From: Souptick Joarder @ 2021-06-30  7:02 UTC (permalink / raw)
  To: Alexander Potapenko
  Cc: Andrew Morton, Dmitry Vyukov, elver, linux-kernel, Linux-MM,
	stable, Greg KH

On Tue, Jun 29, 2021 at 9:47 PM Alexander Potapenko <glider@google.com> wrote:
>
> 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 <akpm@linux-foundation.org>
> Cc: Dmitry Vyukov <dvyukov@google.com>
> Cc: Marco Elver <elver@google.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: stable@vger.kernel.org # 5.12+
> Signed-off-by: Alexander Potapenko <glider@google.com>
>
> ---
>
> v2:
>  - added parentheses around the GFP clause, as requested by Marco
> ---
>  mm/kfence/core.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> index 4d21ac44d5d35..f7ce3d876bc9e 100644
> --- a/mm/kfence/core.c
> +++ b/mm/kfence/core.c
> @@ -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;
> +

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.

>         return kfence_guarded_alloc(s, size, flags);
>  }
>
> --
> 2.32.0.93.g670b81a890-goog
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] kfence: skip DMA allocations
  2021-06-30  7:02 ` Souptick Joarder
@ 2021-06-30  9:35   ` Alexander Potapenko
  0 siblings, 0 replies; 4+ messages in thread
From: Alexander Potapenko @ 2021-06-30  9:35 UTC (permalink / raw)
  To: Souptick Joarder
  Cc: Andrew Morton, Dmitry Vyukov, Marco Elver, LKML, Linux-MM,
	stable, Greg KH

On Wed, Jun 30, 2021 at 9:02 AM Souptick Joarder <jrdr.linux@gmail.com> wrote:
>
> On Tue, Jun 29, 2021 at 9:47 PM Alexander Potapenko <glider@google.com> wrote:
> >
> > 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 <akpm@linux-foundation.org>
> > Cc: Dmitry Vyukov <dvyukov@google.com>
> > Cc: Marco Elver <elver@google.com>
> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Cc: stable@vger.kernel.org # 5.12+
> > Signed-off-by: Alexander Potapenko <glider@google.com>
> >
> > ---
> >
> > v2:
> >  - added parentheses around the GFP clause, as requested by Marco
> > ---
> >  mm/kfence/core.c | 8 ++++++++
> >  1 file changed, 8 insertions(+)
> >
> > diff --git a/mm/kfence/core.c b/mm/kfence/core.c
> > index 4d21ac44d5d35..f7ce3d876bc9e 100644
> > --- a/mm/kfence/core.c
> > +++ b/mm/kfence/core.c
> > @@ -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;
> > +
>
> 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.

> >         return kfence_guarded_alloc(s, size, flags);
> >  }
> >
> > --
> > 2.32.0.93.g670b81a890-goog
> >
> >



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-30  9:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-29 16:17 [PATCH v2] kfence: skip DMA allocations Alexander Potapenko
2021-06-29 16:40 ` Marco Elver
2021-06-30  7:02 ` Souptick Joarder
2021-06-30  9:35   ` Alexander Potapenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).