From patchwork Thu Jan 18 11:01:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 13522698 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 0AD37C4707B for ; Thu, 18 Jan 2024 11:02:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 82A7C6B0078; Thu, 18 Jan 2024 06:02:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DA7B6B007D; Thu, 18 Jan 2024 06:02:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A1006B007E; Thu, 18 Jan 2024 06:02:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5DC386B0078 for ; Thu, 18 Jan 2024 06:02:25 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 373F71A05AD for ; Thu, 18 Jan 2024 11:02:25 +0000 (UTC) X-FDA: 81692143050.23.01D2559 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf23.hostedemail.com (Postfix) with ESMTP id 7445114001F for ; Thu, 18 Jan 2024 11:02:23 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PoQTKtvT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3PQWpZQUKCCAAHRANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--elver.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PQWpZQUKCCAAHRANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--elver.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705575743; 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=nt/piPitvuHRj/PfEKvId5IXLv15chj5uEnJGFs8RjM=; b=tQGXRRW2c5eLIxporN+3m+N3nB6ag2PyBCLWU9tuGZiyE/pscXgAFfVeVqqNYZPHP8B4Bn JvkTgw2R6enGnQV9tadSD3z5Pi3wq30r4vPgPbAmEesVSiQ5oShgyygZxm+or8uHkWpAyS 1bVtmL14xbBmSAYO5OZn24om8ApZF3Q= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PoQTKtvT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3PQWpZQUKCCAAHRANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--elver.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3PQWpZQUKCCAAHRANCKKCHA.8KIHEJQT-IIGR68G.KNC@flex--elver.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705575743; a=rsa-sha256; cv=none; b=8CVNpz2zljM52bkZhIyFKpkuamOgFxSQiN/M+HaqkuY3d/nOl64wFkuCOwFO4ZEzccLQxl FnisaWkrQosH5OuPfjTxJZmMP2JJzBV5p+lfKWI7AS+jmJNXeW2Q9xi/KVQiYWJ4FW2Z2b igwJIK5eJpSYe8RMYjjWZZJlQCIQUos= Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-55a077d0f36so325280a12.2 for ; Thu, 18 Jan 2024 03:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705575742; x=1706180542; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=nt/piPitvuHRj/PfEKvId5IXLv15chj5uEnJGFs8RjM=; b=PoQTKtvTaAZsWxCFASskvKBBdm7c3Cpd0eVCfo7+x4Q2LBSyEYtbdDzp6jsU4ocp9A bKnQdZM8Ob7/WgmFdCH8RZJVhqan+qW9/d1WuzKdk6tEI5OiIACnjCPI1O7gvLskWrkE IykVfZ0TibXLn6dG/8vtEK4HteOflVzdalPv0gHQIDMFwpu34+JxCRDUp/fIjxZ0l3JA UybipBS5cJrhcKpTIY2gWVXbTIv/vzYDscrtacN58HS4ujXxlkCD7tHo6uwy4WP/AK4V UVdPT416fVUEx/WMqsK3HPOcoDbPtXhQqnbejTIsL3o5bS+DrZd3jMtdGhjNBjZq+u5D CEEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705575742; x=1706180542; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nt/piPitvuHRj/PfEKvId5IXLv15chj5uEnJGFs8RjM=; b=Zf4+4NqRbkX8pkoGJKLdrxAM/a6xeJu5n3pKI84iqVZgYBI9/vmkC714dsvqOXuSpi cZrgdCgSngnji5jZf+gGYb9fRhiIbdTvLVTaKIN2VBas9UnnYnFj1HmNuEXQHsaIPPoT wD6KnHnLyhh6BOJ11nsvEQ/gfz0jA0juazkOjO56yP4rWpjFwUVZJDX1s1hSbbxrGiVS uXN1W6W4D+8R8MSQ1R5eJnMan+/GO16NWgii6afzt8qwDx5smY3n8e/cMYe3/YuFTFUz XauKiZ+LRQS9qKDR4tbLHrKedTtSFLNxByAt38iFeY82fBKrjBmcF/Qz5/tcflNRgLTm EYpw== X-Gm-Message-State: AOJu0YzGbaQQIQbNB1RCFyRUrNe3PWJGeiHLYVPDhnqgWbBSuFEZsW3d 9EDDnH50QPHdj/UUVV877dwN5l+7U+020KjmrHCkD//aJE0NVDACnNPoMfuEGN5mxHlWCF1j1A= = X-Google-Smtp-Source: AGHT+IFIPHUVEGpmNTcM9skVlrx/JI8ArShyuL+aXC255FwnIPaPGTUml+eefEgi92CmARI5g8w1oU12HA== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:9d7e:25fb:9605:2bef]) (user=elver job=sendgmr) by 2002:a05:6402:3712:b0:559:f7b9:319c with SMTP id ek18-20020a056402371200b00559f7b9319cmr5073edb.5.1705575741753; Thu, 18 Jan 2024 03:02:21 -0800 (PST) Date: Thu, 18 Jan 2024 12:01:29 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.381.gb435a96ce8-goog Message-ID: <20240118110216.2539519-1-elver@google.com> Subject: [PATCH 1/2] stackdepot: add stats counters exported via debugfs From: Marco Elver To: elver@google.com, Andrew Morton Cc: Andrey Konovalov , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7445114001F X-Stat-Signature: 6s3kzrcb4fwdpqa9hqitoxx97n44di8d X-HE-Tag: 1705575743-22761 X-HE-Meta: U2FsdGVkX1+y8phXiGuUknQX4MHOoAJ5AT+dwebyWupdJgjaaPGfTYSwsrsJDHmosJojn3FuV2cn3ZFBwgJbaaG69hI/laXcsVvFNZVevhzbRV7QWF2+PcgqcOzV8BD/M4+i3a/ZDXy33sZwhRT/SscVuBcgt4VQKovNaUbGLi8gYDHR7W4rwBWFaQ/NyQ67Kf2B81yXfxLUUIlm9NdkuzMxYh4wjD66CyFrxa6GWKH+ul/E1Vy+yLwML8cuPpAz380/Mqfg3PIzArGCuGexzB6y6ApPXec9KLbRgSJIEI5qJQmsUAc5YLuk6utym/ya/yE7w9FvrwQhaC23N6qJiEWcyQKQ6b84+QJm/XrU6tXybxLcDSuZWnoHbhI4d4M99XWABQpafMglVeATh4q8KCJYwhQTrvOqZlB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s9no7EO/pKiB5/3D4BB1Lv7oYBPM5WUzzgmD8RwchmRKOwINWAMYVwVXeG4FR7CgdS64lq5+3nnnUiMUS+SI8AuqEz2lg2QBl3XiBXP8BkZT0EoJX4PtX5wO7K2h5g== 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: Add a few basic stats counters for stack depot that can be used to derive if stack depot is working as intended. This is a snapshot of the new stats after booting a system with a KASAN-enabled kernel: $ cat /sys/kernel/debug/stackdepot/stats pools: 838 allocations: 29861 frees: 6561 in_use: 23300 freelist_size: 1840 Generally, "pools" should be well below the max; once the system is booted, "in_use" should remain relatively steady. Signed-off-by: Marco Elver Reviewed-by: Andrey Konovalov --- lib/stackdepot.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index a0be5d05c7f0..80a8ca24ccc8 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -14,6 +14,7 @@ #define pr_fmt(fmt) "stackdepot: " fmt +#include #include #include #include @@ -115,6 +116,23 @@ static bool new_pool_required = true; /* Lock that protects the variables above. */ static DEFINE_RWLOCK(pool_rwlock); +/* Statistics counters for debugfs. */ +enum depot_counter_id { + DEPOT_COUNTER_ALLOCS, + DEPOT_COUNTER_FREES, + DEPOT_COUNTER_INUSE, + DEPOT_COUNTER_FREELIST_SIZE, + DEPOT_COUNTER_COUNT, +}; +static long counters[DEPOT_COUNTER_COUNT]; +static const char *const counter_names[] = { + [DEPOT_COUNTER_ALLOCS] = "allocations", + [DEPOT_COUNTER_FREES] = "frees", + [DEPOT_COUNTER_INUSE] = "in_use", + [DEPOT_COUNTER_FREELIST_SIZE] = "freelist_size", +}; +static_assert(ARRAY_SIZE(counter_names) == DEPOT_COUNTER_COUNT); + static int __init disable_stack_depot(char *str) { return kstrtobool(str, &stack_depot_disabled); @@ -277,6 +295,7 @@ static void depot_init_pool(void *pool) stack->handle.extra = 0; list_add(&stack->list, &free_stacks); + counters[DEPOT_COUNTER_FREELIST_SIZE]++; } /* Save reference to the pool to be used by depot_fetch_stack(). */ @@ -376,6 +395,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) /* Get and unlink the first entry from the freelist. */ stack = list_first_entry(&free_stacks, struct stack_record, list); list_del(&stack->list); + counters[DEPOT_COUNTER_FREELIST_SIZE]--; /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ if (size > CONFIG_STACKDEPOT_MAX_FRAMES) @@ -394,6 +414,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) */ kmsan_unpoison_memory(stack, DEPOT_STACK_RECORD_SIZE); + counters[DEPOT_COUNTER_ALLOCS]++; + counters[DEPOT_COUNTER_INUSE]++; return stack; } @@ -426,6 +448,10 @@ static void depot_free_stack(struct stack_record *stack) lockdep_assert_held_write(&pool_rwlock); list_add(&stack->list, &free_stacks); + + counters[DEPOT_COUNTER_FREELIST_SIZE]++; + counters[DEPOT_COUNTER_FREES]++; + counters[DEPOT_COUNTER_INUSE]--; } /* Calculates the hash for a stack. */ @@ -690,3 +716,30 @@ unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) return parts.extra; } EXPORT_SYMBOL(stack_depot_get_extra_bits); + +static int stats_show(struct seq_file *seq, void *v) +{ + /* + * data race ok: These are just statistics counters, and approximate + * statistics are ok for debugging. + */ + seq_printf(seq, "pools: %d\n", data_race(pools_num)); + for (int i = 0; i < DEPOT_COUNTER_COUNT; i++) + seq_printf(seq, "%s: %ld\n", counter_names[i], data_race(counters[i])); + + return 0; +} +DEFINE_SHOW_ATTRIBUTE(stats); + +static int depot_debugfs_init(void) +{ + struct dentry *dir; + + if (stack_depot_disabled) + return 0; + + dir = debugfs_create_dir("stackdepot", NULL); + debugfs_create_file("stats", 0444, dir, NULL, &stats_fops); + return 0; +} +late_initcall(depot_debugfs_init);