]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
kfence: count unexpectedly skipped allocations
authorMarco Elver <elver@google.com>
Fri, 5 Nov 2021 20:45:28 +0000 (13:45 -0700)
committerStefan Bader <stefan.bader@canonical.com>
Wed, 27 Apr 2022 09:59:04 +0000 (11:59 +0200)
BugLink: https://bugs.launchpad.net/bugs/1969107
[ Upstream commit 9a19aeb5665068c3e2727230588684aae2cab7ef ]

Maintain a counter to count allocations that are skipped due to being
incompatible (oversized, incompatible gfp flags) or no capacity.

This is to compute the fraction of allocations that could not be
serviced by KFENCE, which we expect to be rare.

Link: https://lkml.kernel.org/r/20210923104803.2620285-2-elver@google.com
Signed-off-by: Marco Elver <elver@google.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Acked-by: Alexander Potapenko <glider@google.com>
Cc: Aleksandr Nogikh <nogikh@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Taras Madan <tarasmadan@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
(cherry picked from commit 72d2d94a981d79703ee9e9073360464174db2ad7)
Signed-off-by: Paolo Pisati <paolo.pisati@canonical.com>
mm/kfence/core.c

index 84555b8233efccf96dacde68371d5d8a4e39e116..f26f55850ad7e1c0a3ffbdaf7ee660639fd8aa21 100644 (file)
@@ -113,6 +113,8 @@ enum kfence_counter_id {
        KFENCE_COUNTER_FREES,
        KFENCE_COUNTER_ZOMBIES,
        KFENCE_COUNTER_BUGS,
+       KFENCE_COUNTER_SKIP_INCOMPAT,
+       KFENCE_COUNTER_SKIP_CAPACITY,
        KFENCE_COUNTER_COUNT,
 };
 static atomic_long_t counters[KFENCE_COUNTER_COUNT];
@@ -122,6 +124,8 @@ static const char *const counter_names[] = {
        [KFENCE_COUNTER_FREES]          = "total frees",
        [KFENCE_COUNTER_ZOMBIES]        = "zombie allocations",
        [KFENCE_COUNTER_BUGS]           = "total bugs",
+       [KFENCE_COUNTER_SKIP_INCOMPAT]  = "skipped allocations (incompatible)",
+       [KFENCE_COUNTER_SKIP_CAPACITY]  = "skipped allocations (capacity)",
 };
 static_assert(ARRAY_SIZE(counter_names) == KFENCE_COUNTER_COUNT);
 
@@ -272,8 +276,10 @@ static void *kfence_guarded_alloc(struct kmem_cache *cache, size_t size, gfp_t g
                list_del_init(&meta->list);
        }
        raw_spin_unlock_irqrestore(&kfence_freelist_lock, flags);
-       if (!meta)
+       if (!meta) {
+               atomic_long_inc(&counters[KFENCE_COUNTER_SKIP_CAPACITY]);
                return NULL;
+       }
 
        if (unlikely(!raw_spin_trylock_irqsave(&meta->lock, flags))) {
                /*
@@ -744,8 +750,10 @@ 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)
+       if (size > PAGE_SIZE) {
+               atomic_long_inc(&counters[KFENCE_COUNTER_SKIP_INCOMPAT]);
                return NULL;
+       }
 
        /*
         * Skip allocations from non-default zones, including DMA. We cannot
@@ -753,8 +761,10 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
         * properties (e.g. reside in DMAable memory).
         */
        if ((flags & GFP_ZONEMASK) ||
-           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32)))
+           (s->flags & (SLAB_CACHE_DMA | SLAB_CACHE_DMA32))) {
+               atomic_long_inc(&counters[KFENCE_COUNTER_SKIP_INCOMPAT]);
                return NULL;
+       }
 
        if (atomic_inc_return(&kfence_allocation_gate) > 1)
                return NULL;