Re: [PATCH v3] kvmalloc: always use vmalloc if CONFIG_DEBUG_SG

From: Mikulas Patocka
Date: Tue Apr 24 2018 - 07:04:38 EST




On Mon, 23 Apr 2018, David Rientjes wrote:

> On Mon, 23 Apr 2018, Mikulas Patocka wrote:
>
> > The kvmalloc function tries to use kmalloc and falls back to vmalloc if
> > kmalloc fails.
> >
> > Unfortunatelly, some kernel code has bugs - it uses kvmalloc and then
> > uses DMA-API on the returned memory or frees it with kfree. Such bugs were
> > found in the virtio-net driver, dm-integrity or RHEL7 powerpc-specific
> > code.
> >
> > These bugs are hard to reproduce because kvmalloc falls back to vmalloc
> > only if memory is fragmented.
> >
> > In order to detect these bugs reliably I submit this patch that changes
> > kvmalloc to fall back to vmalloc with 1/2 probability if CONFIG_DEBUG_SG
> > is turned on. CONFIG_DEBUG_SG is used, because it makes the DMA API layer
> > verify the addresses passed to it, and so the user will get a reliable
> > stacktrace.
>
> Why not just do it unconditionally? Sounds better than "50% of the time
> this will catch bugs".

Because kmalloc (with slub_debug) detects buffer overflows better than
vmalloc. vmalloc detects buffer overflows only at a page boundary. This is
intended for debugging kernels and debugging kernels should detect as many
bugs as possible.

Mikulas