From patchwork Thu Jan 25 09:47:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13530329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD583C48260 for ; Thu, 25 Jan 2024 09:48:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42FDA8D0018; Thu, 25 Jan 2024 04:48:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DE4D8D0015; Thu, 25 Jan 2024 04:48:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 259F18D0018; Thu, 25 Jan 2024 04:48:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0A4738D0015 for ; Thu, 25 Jan 2024 04:48:24 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CBAF8160D55 for ; Thu, 25 Jan 2024 09:48:23 +0000 (UTC) X-FDA: 81717358086.23.29D032E Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf11.hostedemail.com (Postfix) with ESMTP id 28A444000E for ; Thu, 25 Jan 2024 09:48:21 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=f3I9UONt; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ZC6yZQUKCL0hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ZC6yZQUKCL0hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706176101; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=LcyZJBErh32WdhxOYCRWNuYkcavusvnIBLghtfalcmI=; b=UzsakG/PVbF5mt2gXDnkN2yz6FLfJmO05+b7ubsH1gfdL4Z8z8uiETLkg8F7hxIW8ZcLWO SeD+ZwILgNIHPBjhaKTnjg6EZ0Vvxx4DHoSwuBORWd7EBTdFLpQoqtnwv/31c0r02SedpG jm2e9zy5V1c/wr+0wFNQh3po52lfOrA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=f3I9UONt; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ZC6yZQUKCL0hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ZC6yZQUKCL0hoyhujrrjoh.frpolqx0-ppnydfn.ruj@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706176101; a=rsa-sha256; cv=none; b=KHiXJ77bfqb8mqBeNmf6UTkVNKNjf2KuwX2TgsmEqNVa1mGq41OPjn/0ucahrHSOFP27nf NSGjCvjMIPQzYN0F2vYnk1YFJ1U47jRYgLhVWyCKTnHOB8qr118GM/kgxz5wlxnpZSxKdP sUfqNBNrxRJMbSpVt2NxjQndpNdshLo= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5eba564eb3fso106598837b3.1 for ; Thu, 25 Jan 2024 01:48:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706176100; x=1706780900; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=LcyZJBErh32WdhxOYCRWNuYkcavusvnIBLghtfalcmI=; b=f3I9UONtUqztZGAG5NmIuMkrBGRGgJ/2dujjPESxFMLP495XY5I0fsERHmpTuN3lGY eXEl8oBlaCJeMn4KXTJoG72IDAaw4M8oo6Hw5PgnHB7+UDD96Z053pPEJrNz15HtZf7D uX2zql2K3hcydxA5L84B0fHqMFLARV4fT7/V9+ow3JyfnGeM5j8sL8xIyMJ2C962jH/6 PReDN+2Kr9myD6LkKhAM+r4S/GWTI5GuoJdqtYjSL3PK0Sn6dGU0wdufprnGC9G4hs7W 4vl4od0vS2hl/BfHRjeekubsxbfJocyDU+/n5IIHvO678iIYFYznYSaETDsgcYxPb9V5 tdVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706176100; x=1706780900; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=LcyZJBErh32WdhxOYCRWNuYkcavusvnIBLghtfalcmI=; b=u2J3vHQU6VJhOC0G+745/32dkqgcVmbLPt234ppTvLQIMdY5mIq0hXGhMIl8EjQx5l YDDNpsbbpTk+1kL5dwj3n1t3YCD2+SbzqCGvxI/m0vjVUQMm2wAKam86hh3T0TvRtszZ z8/ezmOalsAGAD1Lgus77RjjxaYpe7CKJdl8pToxTprZ97ZLblTyKS8zD61k7xIxKU9p wQv6J2mWR13htNF73DbAD/xp3n9Lh3UUGOxw9j7BBIuJbdeP3bFPyYXnasrkMO/V/HL5 hONImpL5BW+ipUhFeOzD++LxjuZe98rSVMXp4k4ZU0Zxj3r2VQxLzOYP8BFwzwg+qQdI 2FHA== X-Gm-Message-State: AOJu0Yzmy5EE96ni3zix2+zD8YpspsWIVRkwbufBzki2jkxPVkV7Vs/D gy33TxeJ/GDFEmQdKz8XXAlgHkdHvyz2g+00WedHbu8J43p6rlZd93uSmDbeLId+O7XwVvO/1w= = X-Google-Smtp-Source: AGHT+IFT+4UFybdq2x7L0QXiPXA7/4ebE71p62XTQH0DmYiSxVdNk/SKMNHqPjSVBoJMiJSdfo7TL+S5ng== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:57d7:c308:70aa:3dee]) (user=elver job=sendgmr) by 2002:a81:7905:0:b0:5ff:a62d:e2a with SMTP id u5-20020a817905000000b005ffa62d0e2amr229769ywc.4.1706176100355; Thu, 25 Jan 2024 01:48:20 -0800 (PST) Date: Thu, 25 Jan 2024 10:47:42 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240125094815.2041933-1-elver@google.com> Subject: [PATCH 1/2] stackdepot: use variable size records for non-evictable entries From: Marco Elver To: elver@google.com, Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , Andrey Ryabinin , Vincenzo Frascino , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org X-Rspamd-Queue-Id: 28A444000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: te5y9aznrxfjsdtk5oj99qnuptrpf9ut X-HE-Tag: 1706176101-254004 X-HE-Meta: U2FsdGVkX1+CqswKHXrnBcKhbV7RR7E+jCby8oPgBOjMom+tuxOdl72lQ4MUOwgyaxMJCwvDd/zBSrRlR7tDxNuso7Pf046nIS/hlhqFcWDlE+b+bBGkJfjMTUXELjmNiRVF5w9o90VvKa3fRbNBlvbQB3N3KVg5qc9Sui37MmX08w6C/53fjadVTz5Iv1v5sRPJ0TCstgh7kMjIc6fh/yCARl/5iomnGsTqf9JdsqcfdMtV5pLLcUyyszuywJm0GlBNaiidwAvfnqFrx6Qp7LkwjMDVxY3wGp57q9SyvrLe6Zrfh/eaoiA/h/0uD9Dl2ToYamgaUxmxnUQfXxpeMqa2SZn5J+Lu+isuhq25xZBWxCepI3WKcTMQpMETKrGdAJDcn2bWAQXgptA9cCss+C/2zA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uqhBJeZ69Yvd1a2yq1oy2QbNv2sLWJYVqvHawwgvfFAdM3JRoOMvcSbapgVDK1nNiKrv1+cLWClISkhch4hreQIjRaSD/x2NEjpRM58xMJq/3YAqN5w930dcqDCcP9++QRHZY9zoFeJQMIVwbbOBWHjbjx/kl2oCWLklV39RYUiUO9qrg6osgOaW3kaoQ7ESCi5QgsB/G4HNYLp8 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With the introduction of stack depot evictions, each stack record is now fixed size, so that future reuse after an eviction can safely store differently sized stack traces. In all cases that do not make use of evictions, this wastes lots of space. Fix it by re-introducing variable size stack records (up to the max allowed size) for entries that will never be evicted. We know if an entry will never be evicted if the flag STACK_DEPOT_FLAG_GET is not provided, since a later stack_depot_put() attempt is undefined behavior. With my current kernel config that enables KASAN and also SLUB owner tracking, I observe (after a kernel boot) a whopping reduction of 296 stack depot pools, which translates into 4736 KiB saved. The savings here are from SLUB owner tracking only, because KASAN generic mode still uses refcounting. Before: pools: 893 allocations: 29841 frees: 6524 in_use: 23317 freelist_size: 3454 After: pools: 597 allocations: 29657 frees: 6425 in_use: 23232 freelist_size: 3493 Fixes: 108be8def46e ("lib/stackdepot: allow users to evict stack traces") Signed-off-by: Marco Elver Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Dmitry Vyukov Reviewed-by: Andrey Konovalov --- v1 (since RFC): * Get rid of new_pool_required to simplify the code. * Warn on attempts to switch a non-refcounted entry to refcounting. * Typos. --- include/linux/poison.h | 3 + lib/stackdepot.c | 212 +++++++++++++++++++++-------------------- 2 files changed, 113 insertions(+), 102 deletions(-) diff --git a/include/linux/poison.h b/include/linux/poison.h index 27a7dad17eef..1f0ee2459f2a 100644 --- a/include/linux/poison.h +++ b/include/linux/poison.h @@ -92,4 +92,7 @@ /********** VFS **********/ #define VFS_PTR_POISON ((void *)(0xF5 + POISON_POINTER_DELTA)) +/********** lib/stackdepot.c **********/ +#define STACK_DEPOT_POISON ((void *)(0xD390 + POISON_POINTER_DELTA)) + #endif diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 5caa1f566553..1b0d948a053c 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -93,9 +94,6 @@ struct stack_record { }; }; -#define DEPOT_STACK_RECORD_SIZE \ - ALIGN(sizeof(struct stack_record), 1 << DEPOT_STACK_ALIGN) - static bool stack_depot_disabled; static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT); static bool __stack_depot_early_init_passed __initdata; @@ -121,15 +119,10 @@ static void *stack_pools[DEPOT_MAX_POOLS]; static void *new_pool; /* Number of pools in stack_pools. */ static int pools_num; +/* Offset to the unused space in the currently used pool. */ +static size_t pool_offset = DEPOT_POOL_SIZE; /* Freelist of stack records within stack_pools. */ static LIST_HEAD(free_stacks); -/* - * Stack depot tries to keep an extra pool allocated even before it runs out - * of space in the currently used pool. This flag marks whether this extra pool - * needs to be allocated. It has the value 0 when either an extra pool is not - * yet allocated or if the limit on the number of pools is reached. - */ -static bool new_pool_required = true; /* The lock must be held when performing pool or freelist modifications. */ static DEFINE_RAW_SPINLOCK(pool_lock); @@ -294,48 +287,52 @@ int stack_depot_init(void) EXPORT_SYMBOL_GPL(stack_depot_init); /* - * Initializes new stack depot @pool, release all its entries to the freelist, - * and update the list of pools. + * Initializes new stack pool, and updates the list of pools. */ -static void depot_init_pool(void *pool) +static bool depot_init_pool(void **prealloc) { - int offset; - lockdep_assert_held(&pool_lock); - /* Initialize handles and link stack records into the freelist. */ - for (offset = 0; offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; - offset += DEPOT_STACK_RECORD_SIZE) { - struct stack_record *stack = pool + offset; - - stack->handle.pool_index = pools_num; - stack->handle.offset = offset >> DEPOT_STACK_ALIGN; - stack->handle.extra = 0; - - /* - * Stack traces of size 0 are never saved, and we can simply use - * the size field as an indicator if this is a new unused stack - * record in the freelist. - */ - stack->size = 0; + if (unlikely(pools_num >= DEPOT_MAX_POOLS)) { + /* Bail out if we reached the pool limit. */ + WARN_ON_ONCE(pools_num > DEPOT_MAX_POOLS); /* should never happen */ + WARN_ON_ONCE(!new_pool); /* to avoid unnecessary pre-allocation */ + WARN_ONCE(1, "Stack depot reached limit capacity"); + return false; + } - INIT_LIST_HEAD(&stack->hash_list); - /* - * Add to the freelist front to prioritize never-used entries: - * required in case there are entries in the freelist, but their - * RCU cookie still belongs to the current RCU grace period - * (there can still be concurrent readers). - */ - list_add(&stack->free_list, &free_stacks); - counters[DEPOT_COUNTER_FREELIST_SIZE]++; + if (!new_pool && *prealloc) { + /* We have preallocated memory, use it. */ + WRITE_ONCE(new_pool, *prealloc); + *prealloc = NULL; } + if (!new_pool) + return false; /* new_pool and *prealloc are NULL */ + /* Save reference to the pool to be used by depot_fetch_stack(). */ - stack_pools[pools_num] = pool; + stack_pools[pools_num] = new_pool; + + /* + * Stack depot tries to keep an extra pool allocated even before it runs + * out of space in the currently used pool. + * + * To indicate that a new preallocation is needed new_pool is reset to + * NULL; do not reset to NULL if we have reached the maximum number of + * pools. + */ + if (pools_num < DEPOT_MAX_POOLS) + WRITE_ONCE(new_pool, NULL); + else + WRITE_ONCE(new_pool, STACK_DEPOT_POISON); /* Pairs with concurrent READ_ONCE() in depot_fetch_stack(). */ WRITE_ONCE(pools_num, pools_num + 1); ASSERT_EXCLUSIVE_WRITER(pools_num); + + pool_offset = 0; + + return true; } /* Keeps the preallocated memory to be used for a new stack depot pool. */ @@ -347,60 +344,48 @@ static void depot_keep_new_pool(void **prealloc) * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. */ - if (!new_pool_required) + if (new_pool) return; - /* - * Use the preallocated memory for the new pool - * as long as we do not exceed the maximum number of pools. - */ - if (pools_num < DEPOT_MAX_POOLS) { - new_pool = *prealloc; - *prealloc = NULL; - } - - /* - * At this point, either a new pool is kept or the maximum - * number of pools is reached. In either case, take note that - * keeping another pool is not required. - */ - WRITE_ONCE(new_pool_required, false); + WRITE_ONCE(new_pool, *prealloc); + *prealloc = NULL; } /* - * Try to initialize a new stack depot pool from either a previous or the - * current pre-allocation, and release all its entries to the freelist. + * Try to initialize a new stack record from the current pool, a cached pool, or + * the current pre-allocation. */ -static bool depot_try_init_pool(void **prealloc) +static struct stack_record *depot_pop_free_pool(void **prealloc, size_t size) { + struct stack_record *stack; + void *current_pool; + u32 pool_index; + lockdep_assert_held(&pool_lock); - /* Check if we have a new pool saved and use it. */ - if (new_pool) { - depot_init_pool(new_pool); - new_pool = NULL; + if (pool_offset + size > DEPOT_POOL_SIZE) { + if (!depot_init_pool(prealloc)) + return NULL; + } - /* Take note that we might need a new new_pool. */ - if (pools_num < DEPOT_MAX_POOLS) - WRITE_ONCE(new_pool_required, true); + if (WARN_ON_ONCE(pools_num < 1)) + return NULL; + pool_index = pools_num - 1; + current_pool = stack_pools[pool_index]; + if (WARN_ON_ONCE(!current_pool)) + return NULL; - return true; - } + stack = current_pool + pool_offset; - /* Bail out if we reached the pool limit. */ - if (unlikely(pools_num >= DEPOT_MAX_POOLS)) { - WARN_ONCE(1, "Stack depot reached limit capacity"); - return false; - } + /* Pre-initialize handle once. */ + stack->handle.pool_index = pool_index; + stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; + stack->handle.extra = 0; + INIT_LIST_HEAD(&stack->hash_list); - /* Check if we have preallocated memory and use it. */ - if (*prealloc) { - depot_init_pool(*prealloc); - *prealloc = NULL; - return true; - } + pool_offset += size; - return false; + return stack; } /* Try to find next free usable entry. */ @@ -420,7 +405,7 @@ static struct stack_record *depot_pop_free(void) * check the first entry. */ stack = list_first_entry(&free_stacks, struct stack_record, free_list); - if (stack->size && !poll_state_synchronize_rcu(stack->rcu_state)) + if (!poll_state_synchronize_rcu(stack->rcu_state)) return NULL; list_del(&stack->free_list); @@ -429,45 +414,68 @@ static struct stack_record *depot_pop_free(void) return stack; } +static inline size_t depot_stack_record_size(struct stack_record *s, unsigned int nr_entries) +{ + const size_t used = flex_array_size(s, entries, nr_entries); + const size_t unused = sizeof(s->entries) - used; + + WARN_ON_ONCE(sizeof(s->entries) < used); + + return ALIGN(sizeof(struct stack_record) - unused, 1 << DEPOT_STACK_ALIGN); +} + /* Allocates a new stack in a stack depot pool. */ static struct stack_record * -depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +depot_alloc_stack(unsigned long *entries, int nr_entries, u32 hash, depot_flags_t flags, void **prealloc) { - struct stack_record *stack; + struct stack_record *stack = NULL; + size_t record_size; lockdep_assert_held(&pool_lock); /* This should already be checked by public API entry points. */ - if (WARN_ON_ONCE(!size)) + if (WARN_ON_ONCE(!nr_entries)) return NULL; - /* Check if we have a stack record to save the stack trace. */ - stack = depot_pop_free(); - if (!stack) { - /* No usable entries on the freelist - try to refill the freelist. */ - if (!depot_try_init_pool(prealloc)) - return NULL; + /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ + if (nr_entries > CONFIG_STACKDEPOT_MAX_FRAMES) + nr_entries = CONFIG_STACKDEPOT_MAX_FRAMES; + + if (flags & STACK_DEPOT_FLAG_GET) { + /* + * Evictable entries have to allocate the max. size so they may + * safely be re-used by differently sized allocations. + */ + record_size = depot_stack_record_size(stack, CONFIG_STACKDEPOT_MAX_FRAMES); stack = depot_pop_free(); - if (WARN_ON(!stack)) - return NULL; + } else { + record_size = depot_stack_record_size(stack, nr_entries); } - /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ - if (size > CONFIG_STACKDEPOT_MAX_FRAMES) - size = CONFIG_STACKDEPOT_MAX_FRAMES; + if (!stack) { + stack = depot_pop_free_pool(prealloc, record_size); + if (!stack) + return NULL; + } /* Save the stack trace. */ stack->hash = hash; - stack->size = size; - /* stack->handle is already filled in by depot_init_pool(). */ - refcount_set(&stack->count, 1); - memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); + stack->size = nr_entries; + /* stack->handle is already filled in by depot_pop_free_pool(). */ + memcpy(stack->entries, entries, flex_array_size(stack, entries, nr_entries)); + + if (flags & STACK_DEPOT_FLAG_GET) { + refcount_set(&stack->count, 1); + } else { + /* Warn on attempts to switch to refcounting this entry. */ + refcount_set(&stack->count, REFCOUNT_SATURATED); + } /* * Let KMSAN know the stored stack record is initialized. This shall * prevent false positive reports if instrumented code accesses it. */ - kmsan_unpoison_memory(stack, DEPOT_STACK_RECORD_SIZE); + kmsan_unpoison_memory(stack, record_size); counters[DEPOT_COUNTER_ALLOCS]++; counters[DEPOT_COUNTER_INUSE]++; @@ -660,7 +668,7 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, * Allocate memory for a new pool if required now: * we won't be able to do that under the lock. */ - if (unlikely(can_alloc && READ_ONCE(new_pool_required))) { + if (unlikely(can_alloc && !READ_ONCE(new_pool))) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -681,7 +689,7 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, found = find_stack(bucket, entries, nr_entries, hash, depot_flags); if (!found) { struct stack_record *new = - depot_alloc_stack(entries, nr_entries, hash, &prealloc); + depot_alloc_stack(entries, nr_entries, hash, depot_flags, &prealloc); if (new) { /* From patchwork Thu Jan 25 09:47:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13530330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FDA4C47258 for ; Thu, 25 Jan 2024 09:48:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CE1F38D0019; Thu, 25 Jan 2024 04:48:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C6B2B8D0015; Thu, 25 Jan 2024 04:48:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6EB98D0019; Thu, 25 Jan 2024 04:48:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 906AE8D0015 for ; Thu, 25 Jan 2024 04:48:26 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 60832A03F3 for ; Thu, 25 Jan 2024 09:48:26 +0000 (UTC) X-FDA: 81717358212.07.5E8EADF Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) by imf11.hostedemail.com (Postfix) with ESMTP id 97A8A40019 for ; Thu, 25 Jan 2024 09:48:24 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ITJi3+HB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Zy6yZQUKCMAkr1kxmuumrk.iusrot03-ssq1giq.uxm@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Zy6yZQUKCMAkr1kxmuumrk.iusrot03-ssq1giq.uxm@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706176104; a=rsa-sha256; cv=none; b=BhPQh7dIzFsISH7jM7GNHv8LdV9DjtbKK9AHH43Z1q9DD6pBuH3A0veOS5hI5t31N0Qiey OLbh6d4chTqEbWYj+DsTrISE+cbq3aLlU2Dw5XoM4PSHIqaXNPKusIPDFBJVW4BpbbCveb ZP/D9TTh3eCxu/GFy9BVUdV0wNr809k= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ITJi3+HB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3Zy6yZQUKCMAkr1kxmuumrk.iusrot03-ssq1giq.uxm@flex--elver.bounces.google.com designates 209.85.208.74 as permitted sender) smtp.mailfrom=3Zy6yZQUKCMAkr1kxmuumrk.iusrot03-ssq1giq.uxm@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706176104; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=R76x7/0RnR583W2gtQWaNlBYwLgRcgwa4b5YYEtfgyM=; b=zCv3Hd+q/nBsYYRSZidIPMHLXwoVT5YWQ+HQdBzetwuySnx8+KfOSH0QsVTu+WNupAjuAp EpLeEhaUHx+G59rzmq5itHC35toUd2lOLDUf432UeXeFd0vNsOASPOPMfMaM+XUl02JuVg 577S4jTXxCNJW7jN0LTU5GnwzoBnj+g= Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-55cd3c00d62so749552a12.1 for ; Thu, 25 Jan 2024 01:48:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706176103; x=1706780903; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R76x7/0RnR583W2gtQWaNlBYwLgRcgwa4b5YYEtfgyM=; b=ITJi3+HBR64IPrGxxttaCFKRR2xZagezMQ+zcGNUU8WPlNxv3kbXaWL+KVIym/SNyS AhqmMQhhidVUttXcy7x+h7jadzwD9fa0sGPToYvyzNNOCUf0wu/1jtNKxVR6UFDfrQg+ QQX+nw6TrRxtQDXXX67iw7wIVHSULSL1X8wk968B0WqCPybbJxxCM3IrBd0qPxHJyKX9 NK3buD0WawXpgPQSmGIYM33XJwbDIFEN1kn6LhgXunLhkXX06i/uTisOyRw8ZH/0wKal 5/soOeMrcWfCDEjmwXSJAc5Rd3Fxm/O6LbOJlltjEnyqN51Lk5c+JgkurNbP/dFn40c0 canw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706176103; x=1706780903; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R76x7/0RnR583W2gtQWaNlBYwLgRcgwa4b5YYEtfgyM=; b=N2V7nul3UXMRfzfltiUrS5OX2nh8uglFDU5zzrjv7RTNA0DZE8JiRALihkY1rAjSCi jbt/moncZ4UfVfSzEqxBOkXXvDijFtLjtcTdlDUNNNTbEIu6ebK9ChbrmYEgBaru154I 1Yl4cVy8JXWFzPeoeDSeXroyYrHz1uoO5UQ+z5k9KKioPNWn8DIyppVa9OJhgFZK43y0 1kQLeW9TFMRHsgnZ9bBXS+LMOJ/hMfLNUWowNzcZe6kL6Pm9PSUN2AUGJDK4AieBSErH nf0Wz2cOU9MtaSF0gJTZNc87QaAwk7ou9fLulq4O650+g/iF26LuShasXFQVM1PLaR3j s0SQ== X-Gm-Message-State: AOJu0Yzz8wRgwBqjSR7cYMZErdTZek1zpnTGbxJQWUFVDe1VTZ/59OaG 7edEOt4d7jMJw9v7poGEtsnqm/lSLld7tMu6WcTApTtgVExIW22VLYnGYGU7LfLdiSlr+v9pgQ= = X-Google-Smtp-Source: AGHT+IGzx91BDAendPMlOcrCKsVLFblSVOc8i+K+8mOTLS7MddYUFP0Mru42nA7iQPYL+If9KK1Y0Fm0YQ== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:57d7:c308:70aa:3dee]) (user=elver job=sendgmr) by 2002:a17:907:c24d:b0:a31:747c:b3c7 with SMTP id tj13-20020a170907c24d00b00a31747cb3c7mr4623ejc.3.1706176103117; Thu, 25 Jan 2024 01:48:23 -0800 (PST) Date: Thu, 25 Jan 2024 10:47:43 +0100 In-Reply-To: <20240125094815.2041933-1-elver@google.com> Mime-Version: 1.0 References: <20240125094815.2041933-1-elver@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240125094815.2041933-2-elver@google.com> Subject: [PATCH 2/2] kasan: revert eviction of stack traces in generic mode From: Marco Elver To: elver@google.com, Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , Andrey Ryabinin , Vincenzo Frascino , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 97A8A40019 X-Stat-Signature: dimbsckxh13t4w5xzzw9cht3s37ioz1a X-HE-Tag: 1706176104-203413 X-HE-Meta: U2FsdGVkX19TBlq65YCE1AH/1YvYM4EI+cpYEaGk5OVxDXTDZX2qBQw3vVdKnHhVbDIBYGfi0NP89w2O7O5Nwg6EOzhFrOlXu6ITOIQgOZGP4EsuEYY3SyqEaYU8wcr+87PgpU3zFYdHKKTNoOwFJA3fn+nMKoLkO6fi1Kp4v5LjeC39/owyNgi61WwfDAg5cAvTAAzh7LVMAKIKphkIvsupT22qpNmjxI8V+nQJyB70lnWyPX50AH77UFUup5QF49EeaYelVpbKgdRjungoc4c/jeD71pJCy2uSf4dWKdrqvcyjQS/3EqH9MjltPlAWc4NaRY5es3cIrbt4Zm5+ly4CWoNsNjzCH88cyZB7K9KUARkAiR5TOZl/lO65iSrlbB2m9EuNDd8jUSIViGE18Q4z12GPM0puiaUUoqjmu6WAkNPqYiUPJiBg3DE2QLnN8X2Y5yWt+/NJEEKmiWgv/DZkm0WaLzuRCzIsotyBFQIXdJS3/qCFYS+UTYDKfvqJ5kIUrFs+vE6xQcnu7e4nWRlvbCMUFbr3zz8bTSu9SNuGn2vvg2nWRZsTqYUzczoTBBcqUlWXxJ/cCttzojUx7iEhvFpj0bFquIXPsv0/HjdM5HnwsuH/g2spZQA4dgQz22aFqG6uGO8/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1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This partially reverts commits cc478e0b6bdf, 63b85ac56a64, 08d7c94d9635, a414d4286f34, and 773688a6cb24 to make use of variable-sized stack depot records, since eviction of stack entries from stack depot forces fixed- sized stack records. Care was taken to retain the code cleanups by the above commits. Eviction was added to generic KASAN as a response to alleviating the additional memory usage from fixed-sized stack records, but this still uses more memory than previously. With the re-introduction of variable-sized records for stack depot, we can just switch back to non-evictable stack records again, and return back to the previous performance and memory usage baseline. Before (observed after a KASAN kernel boot): pools: 597 allocations: 29657 frees: 6425 in_use: 23232 freelist_size: 3493 After: pools: 315 allocations: 28964 frees: 0 in_use: 28964 freelist_size: 0 As can be seen from the number of "frees", with a generic KASAN config, evictions are no longer used but due to using variable-sized records, I observe a reduction of 282 stack depot pools (saving 4512 KiB) with my test setup. Fixes: cc478e0b6bdf ("kasan: avoid resetting aux_lock") Fixes: 63b85ac56a64 ("kasan: stop leaking stack trace handles") Fixes: 08d7c94d9635 ("kasan: memset free track in qlink_free") Fixes: a414d4286f34 ("kasan: handle concurrent kasan_record_aux_stack calls") Fixes: 773688a6cb24 ("kasan: use stack_depot_put for Generic mode") Signed-off-by: Marco Elver Cc: Alexander Potapenko Cc: Andrey Konovalov Cc: Dmitry Vyukov Reviewed-by: Andrey Konovalov --- mm/kasan/common.c | 3 +-- mm/kasan/generic.c | 54 ++++++---------------------------------------- mm/kasan/kasan.h | 8 ------- 3 files changed, 8 insertions(+), 57 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 610efae91220..ad32803e34e9 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -65,8 +65,7 @@ void kasan_save_track(struct kasan_track *track, gfp_t flags) { depot_stack_handle_t stack; - stack = kasan_save_stack(flags, - STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); + stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); kasan_set_track(track, stack); } diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index df6627f62402..8bfb52b28c22 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -485,16 +485,6 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object) if (alloc_meta) { /* Zero out alloc meta to mark it as invalid. */ __memset(alloc_meta, 0, sizeof(*alloc_meta)); - - /* - * Prepare the lock for saving auxiliary stack traces. - * Temporarily disable KASAN bug reporting to allow instrumented - * raw_spin_lock_init to access aux_lock, which resides inside - * of a redzone. - */ - kasan_disable_current(); - raw_spin_lock_init(&alloc_meta->aux_lock); - kasan_enable_current(); } /* @@ -506,18 +496,8 @@ void kasan_init_object_meta(struct kmem_cache *cache, const void *object) static void release_alloc_meta(struct kasan_alloc_meta *meta) { - /* Evict the stack traces from stack depot. */ - stack_depot_put(meta->alloc_track.stack); - stack_depot_put(meta->aux_stack[0]); - stack_depot_put(meta->aux_stack[1]); - - /* - * Zero out alloc meta to mark it as invalid but keep aux_lock - * initialized to avoid having to reinitialize it when another object - * is allocated in the same slot. - */ - __memset(&meta->alloc_track, 0, sizeof(meta->alloc_track)); - __memset(meta->aux_stack, 0, sizeof(meta->aux_stack)); + /* Zero out alloc meta to mark it as invalid. */ + __memset(meta, 0, sizeof(*meta)); } static void release_free_meta(const void *object, struct kasan_free_meta *meta) @@ -526,9 +506,6 @@ static void release_free_meta(const void *object, struct kasan_free_meta *meta) if (*(u8 *)kasan_mem_to_shadow(object) != KASAN_SLAB_FREE_META) return; - /* Evict the stack trace from the stack depot. */ - stack_depot_put(meta->free_track.stack); - /* Mark free meta as invalid. */ *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREE; } @@ -571,8 +548,6 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) struct kmem_cache *cache; struct kasan_alloc_meta *alloc_meta; void *object; - depot_stack_handle_t new_handle, old_handle; - unsigned long flags; if (is_kfence_address(addr) || !slab) return; @@ -583,33 +558,18 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) if (!alloc_meta) return; - new_handle = kasan_save_stack(0, depot_flags); - - /* - * Temporarily disable KASAN bug reporting to allow instrumented - * spinlock functions to access aux_lock, which resides inside of a - * redzone. - */ - kasan_disable_current(); - raw_spin_lock_irqsave(&alloc_meta->aux_lock, flags); - old_handle = alloc_meta->aux_stack[1]; alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; - alloc_meta->aux_stack[0] = new_handle; - raw_spin_unlock_irqrestore(&alloc_meta->aux_lock, flags); - kasan_enable_current(); - - stack_depot_put(old_handle); + alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); } void kasan_record_aux_stack(void *addr) { - return __kasan_record_aux_stack(addr, - STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); + return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); } void kasan_record_aux_stack_noalloc(void *addr) { - return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_GET); + return __kasan_record_aux_stack(addr, 0); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) @@ -620,7 +580,7 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) if (!alloc_meta) return; - /* Evict previous stack traces (might exist for krealloc or mempool). */ + /* Invalidate previous stack traces (might exist for krealloc or mempool). */ release_alloc_meta(alloc_meta); kasan_save_track(&alloc_meta->alloc_track, flags); @@ -634,7 +594,7 @@ void kasan_save_free_info(struct kmem_cache *cache, void *object) if (!free_meta) return; - /* Evict previous stack trace (might exist for mempool). */ + /* Invalidate previous stack trace (might exist for mempool). */ release_free_meta(object, free_meta); kasan_save_track(&free_meta->free_track, 0); diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index d0f172f2b978..216ae0ef1e4b 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -6,7 +6,6 @@ #include #include #include -#include #include #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) @@ -265,13 +264,6 @@ struct kasan_global { struct kasan_alloc_meta { struct kasan_track alloc_track; /* Free track is stored in kasan_free_meta. */ - /* - * aux_lock protects aux_stack from accesses from concurrent - * kasan_record_aux_stack calls. It is a raw spinlock to avoid sleeping - * on RT kernels, as kasan_record_aux_stack_noalloc can be called from - * non-sleepable contexts. - */ - raw_spinlock_t aux_lock; depot_stack_handle_t aux_stack[2]; };