From patchwork Mon Nov 20 17:46:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13461814 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 9B88EC197A0 for ; Mon, 20 Nov 2023 17:47:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DBB56B0324; Mon, 20 Nov 2023 12:47:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 18C886B0329; Mon, 20 Nov 2023 12:47:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07B9A6B032A; Mon, 20 Nov 2023 12:47:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EE3CE6B0324 for ; Mon, 20 Nov 2023 12:47:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C6ACD80A0B for ; Mon, 20 Nov 2023 17:47:30 +0000 (UTC) X-FDA: 81479064660.14.C0A35A5 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) by imf20.hostedemail.com (Postfix) with ESMTP id DE7581C000C for ; Mon, 20 Nov 2023 17:47:28 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LrmT6z7B; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.179 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502449; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=dirg16X8IjWPdQeQGOiKGtH7xHuc5gOowsqXecWSLlM=; b=qUPL6AQLOWC7lAVWm3+21rbsDn5XbqTJQCL3SnUkZMR12nXfhhzMKIA15YucMA4zzZUF8A c+ZWT938yWn+0VTHLXvnwbStqDJN1Zo0au1ebFXoA0fcIMoRbMckwMJzO9aJHeZpPc80Ct nWUygkX7E9HPvStYBNj5wNFx4ZBQZzY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502449; a=rsa-sha256; cv=none; b=5nPlQapGRasIEzJl975pDDu1UprajBcCsfS6azyKYw2uHjo5C0JEh03G5ZNXyA+Iv4Hxha yJKKL3XuZ1B9nQljnwxGJ6MnmF6dy7v/r/69ip9aU4ubWdwc+2SkRdeJE3+iZS38HvzPPq 43eMlCh11Y1tjXiO5H7XB+McTxIsYXE= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LrmT6z7B; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.179 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1700502446; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=dirg16X8IjWPdQeQGOiKGtH7xHuc5gOowsqXecWSLlM=; b=LrmT6z7BevrEOVmLW0g9//fbwyY1UogPCDw8xj/Xlc92kuYfZXlzWvlU+nKASw0KSd7GY+ kD5OERtPjKafidVOi7TNebi8D7eNrTExLNbbWD6Ila0/S8nb06sDJQX7+FJZVc9EmXLfNE KI7cobYyoicYG6JJ416fOHwwFvor0uw= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 00/22] stackdepot: allow evicting stack traces Date: Mon, 20 Nov 2023 18:46:58 +0100 Message-Id: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: DE7581C000C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: yiymxjfbdgsyn3zgfsytqwoyoxtiq3jy X-HE-Tag: 1700502448-21934 X-HE-Meta: U2FsdGVkX1+09mlV9ekvtyeQVp/+su+L3MotRcf3sM8J1bzpRZX18GR+qpyWWtLFPvETc+WYe1y+i2IRqdoHdLq5xMlCa/P3uIC2xD3rubMlDAPCmLBL3BVIg+qFUE3hYsjuZZYWsFOZ9BYb7i4B5m/0gskMzY4J1mEU7D3EOx3QIdgsTC94mfBg85kqpWG94zzJwXIqMHhkMyetEFUMgk5CrvMAqzakLsahsdvCUXZrHkz4uxqScH4ZtEEQIbMvG49Z9eUzBDCT4zTvwK8lnk6sIinqvuNDPQbgsCWZ5HKDeAlO6l3YrBJnVjqx4+DbQSaGt8FiltPAhbaHupB1ZY33TmwuaqfA613rBu4iUaOBDMLDpdTnfv/+OfUihS238GqfUPMkxnA6VKQssR8gjXVwGAw608UKL8pm9Oj3tPv6/R/Eeau8j5nfTN3rsqx3BtDII50kFdK773GJquR4OL6YxduBlbo50vUJq8A6UFPoaXkxfJENikgaUILibuK7amA/FO8Fnsof8wpvJMtu/8XEFmmRM+jZ3SZ29WlNmC49zDne9QqcGzlIxBPMbOgvOM3ymcxCUw7y4tM+ZpM7bwePBwHjFbEwcatOUkDZB5vcCuoPNASm2ANNI9DoOhhL1RE9TpBD2XjoRJ1OQTXzswmwi1QOEq04Vjwc8JF9i8tMjLcNJI/0OXdB10RRJPkwY9Muyc/49uJseWy8tw4Dx79N3DF0jQlb6mm3zdIWtHY9gr7bg7EkMZEA64b2Q8cB7zGNSgRm4I5WLg6QYdzVMSQ/upZAjcu/MC4nxNDjnqjZz1mbEKVfVVSCje+7ypEjt/SX/8K0RbkhlPwXt+yYmtE4IEKve/v8OtdaLheAQqP0YfezwdH9VHf4mF0FFpjW8rso/DpokmJ0Gmi80FF7FLZMiBldxhGaJ3dFmy++qxwXydkAgUMNOTmj91pimXz/3lda59PJ+oxrkTSuWMg AYdukO31 nhK/F1bdYQ4PlvGcswWJ7eONDTk/jHKK2zqNyystXIUSvVtOE1EJ+ff70KsarxXBeu0CS 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: From: Andrey Konovalov Currently, the stack depot grows indefinitely until it reaches its capacity. Once that happens, the stack depot stops saving new stack traces. This creates a problem for using the stack depot for in-field testing and in production. For such uses, an ideal stack trace storage should: 1. Allow saving fresh stack traces on systems with a large uptime while limiting the amount of memory used to store the traces; 2. Have a low performance impact. Implementing #1 in the stack depot is impossible with the current keep-forever approach. This series targets to address that. Issue #2 is left to be addressed in a future series. This series changes the stack depot implementation to allow evicting unneeded stack traces from the stack depot. The users of the stack depot can do that via new stack_depot_save_flags(STACK_DEPOT_FLAG_GET) and stack_depot_put APIs. Internal changes to the stack depot code include: 1. Storing stack traces in fixed-frame-sized slots (vs precisely-sized slots in the current implementation); the slot size is controlled via CONFIG_STACKDEPOT_MAX_FRAMES (default: 64 frames); 2. Keeping available slots in a freelist (vs keeping an offset to the next free slot); 3. Using a read/write lock for synchronization (vs a lock-free approach combined with a spinlock). This series also integrates the eviction functionality into KASAN: the tag-based modes evict stack traces when the corresponding entry leaves the stack ring, and Generic KASAN evicts stack traces for objects once those leave the quarantine. With KASAN, despite wasting some space on rounding up the size of each stack record, the total memory consumed by stack depot gets saturated due to the eviction of irrelevant stack traces from the stack depot. With the tag-based KASAN modes, the average total amount of memory used for stack traces becomes ~0.5 MB (with the current default stack ring size of 32k entries and the default CONFIG_STACKDEPOT_MAX_FRAMES of 64). With Generic KASAN, the stack traces take up ~1 MB per 1 GB of RAM (as the quarantine's size depends on the amount of RAM). However, with KMSAN, the stack depot ends up using ~4x more memory per a stack trace than before. Thus, for KMSAN, the stack depot capacity is increased accordingly. KMSAN uses a lot of RAM for shadow memory anyway, so the increased stack depot memory usage will not make a significant difference. Other users of the stack depot do not save stack traces as often as KASAN and KMSAN. Thus, the increased memory usage is taken as an acceptable trade-off. In the future, these other users can take advantage of the eviction API to limit the memory waste. There is no measurable boot time performance impact of these changes for KASAN on x86-64. I haven't done any tests for arm64 modes (the stack depot without performance optimizations is not suitable for intended use of those anyway), but I expect a similar result. Obtaining and copying stack trace frames when saving them into stack depot is what takes the most time. This series does not yet provide a way to configure the maximum size of the stack depot externally (e.g. via a command-line parameter). This will be added in a separate series, possibly together with the performance improvement changes. --- Changes v3->v4: - Rebase onto 6.7-rc2. - Fix lockdep annotation in depot_fetch_stack. - New patch: "kasan: use stack_depot_put for Generic mode" (was sent for review separately but now merged into this series). - New patch: "lib/stackdepot: print disabled message only if truly disabled" (was sent for review separately but now merged into this series). - New patch: "lib/stackdepot: adjust DEPOT_POOLS_CAP for KMSAN". Changes v2->v3: - Fix null-ptr-deref by using the proper number of entries for initializing the stack table when alloc_large_system_hash() auto-calculates the number (see patch #12). - Keep STACKDEPOT/STACKDEPOT_ALWAYS_INIT Kconfig options not configurable by users. - Use lockdep_assert_held_read annotation in depot_fetch_stack. - WARN_ON invalid flags in stack_depot_save_flags. - Moved "../slab.h" include in mm/kasan/report_tags.c in the right patch. - Various comment fixes. Changes v1->v2: - Rework API to stack_depot_save_flags(STACK_DEPOT_FLAG_GET) + stack_depot_put. - Add CONFIG_STACKDEPOT_MAX_FRAMES Kconfig option. - Switch stack depot to using list_head's. - Assorted minor changes, see the commit message for each path. Andrey Konovalov (22): lib/stackdepot: print disabled message only if truly disabled lib/stackdepot: check disabled flag when fetching lib/stackdepot: simplify __stack_depot_save lib/stackdepot: drop valid bit from handles lib/stackdepot: add depot_fetch_stack helper lib/stackdepot: use fixed-sized slots for stack records lib/stackdepot: fix and clean-up atomic annotations lib/stackdepot: rework helpers for depot_alloc_stack lib/stackdepot: rename next_pool_required to new_pool_required lib/stackdepot: store next pool pointer in new_pool lib/stackdepot: store free stack records in a freelist lib/stackdepot: use read/write lock lib/stackdepot: use list_head for stack record links kmsan: use stack_depot_save instead of __stack_depot_save lib/stackdepot, kasan: add flags to __stack_depot_save and rename lib/stackdepot: add refcount for records lib/stackdepot: allow users to evict stack traces kasan: remove atomic accesses to stack ring entries kasan: check object_size in kasan_complete_mode_report_info kasan: use stack_depot_put for tag-based modes kasan: use stack_depot_put for Generic mode lib/stackdepot: adjust DEPOT_POOLS_CAP for KMSAN include/linux/stackdepot.h | 59 ++++- lib/Kconfig | 10 + lib/stackdepot.c | 452 ++++++++++++++++++++++++------------- mm/kasan/common.c | 8 +- mm/kasan/generic.c | 27 ++- mm/kasan/kasan.h | 2 +- mm/kasan/quarantine.c | 26 ++- mm/kasan/report_tags.c | 27 +-- mm/kasan/tags.c | 24 +- mm/kmsan/core.c | 7 +- 10 files changed, 427 insertions(+), 215 deletions(-)