• Andrey Konovalov's avatar
    kasan: allow sampling page_alloc allocations for HW_TAGS · 44383cef
    Andrey Konovalov authored
    As Hardware Tag-Based KASAN is intended to be used in production, its
    performance impact is crucial.  As page_alloc allocations tend to be big,
    tagging and checking all such allocations can introduce a significant
    slowdown.
    
    Add two new boot parameters that allow to alleviate that slowdown:
    
    - kasan.page_alloc.sample, which makes Hardware Tag-Based KASAN tag only
      every Nth page_alloc allocation with the order configured by the second
      added parameter (default: tag every such allocation).
    
    - kasan.page_alloc.sample.order, which makes sampling enabled by the first
      parameter only affect page_alloc allocations with the order equal or
      greater than the specified value (default: 3, see below).
    
    The exact performance improvement caused by using the new parameters
    depends on their values and the applied workload.
    
    The chosen default value for kasan.page_alloc.sample.order is 3, which
    matches both PAGE_ALLOC_COSTLY_ORDER and SKB_FRAG_PAGE_ORDER.  This is
    done for two reasons:
    
    1. PAGE_ALLOC_COSTLY_ORDER is "the order at which allocations are deemed
       costly to service", which corresponds to the idea that only large and
       thus costly allocations are supposed to sampled.
    
    2. One of the workloads targeted by this patch is a benchmark that sends
       a large amount of data over a local loopback connection. Most multi-page
       data allocations in the networking subsystem have the order of
       SKB_FRAG_PAGE_ORDER (or PAGE_ALLOC_COSTLY_ORDER).
    
    When running a local loopback test on a testing MTE-enabled device in sync
    mode, enabling Hardware Tag-Based KASAN introduces a ~50% slowdown. 
    Applying this patch and setting kasan.page_alloc.sampling to a value
    higher than 1 allows to lower the slowdown.  The performance improvement
    saturates around the sampling interval value of 10 with the default
    sampling page order of 3.  This lowers the slowdown to ~20%.  The slowdown
    in real scenarios involving the network will likely be better.
    
    Enabling page_alloc sampling has a downside: KASAN misses bad accesses to
    a page_alloc allocation that has not been tagged.  This lowers the value
    of KASAN as a security mitigation.
    
    However, based on measuring the number of page_alloc allocations of
    different orders during boot in a test build, sampling with the default
    kasan.page_alloc.sample.order value affects only ~7% of allocations.  The
    rest ~93% of allocations are still checked deterministically.
    
    Link: https://lkml.kernel.org/r/129da0614123bb85ed4dd61ae30842b2dd7c903f.1671471846.git.andreyknvl@google.comSigned-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
    Reviewed-by: default avatarMarco Elver <elver@google.com>
    Cc: Alexander Potapenko <glider@google.com>
    Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
    Cc: Dmitry Vyukov <dvyukov@google.com>
    Cc: Evgenii Stepanov <eugenis@google.com>
    Cc: Jann Horn <jannh@google.com>
    Cc: Mark Brand <markbrand@google.com>
    Cc: Peter Collingbourne <pcc@google.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    44383cef
kasan.h 19.9 KB