Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5741671rwr; Mon, 1 May 2023 10:04:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ70to7xi2a8Fmhak2olQHvthez4hlFKKOVnHuJ00Xz/LQVdT4cZqwpeeRjikRy09dHAC8i8 X-Received: by 2002:a17:902:f68e:b0:1a9:8ddd:8213 with SMTP id l14-20020a170902f68e00b001a98ddd8213mr18682694plg.57.1682960643282; Mon, 01 May 2023 10:04:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682960643; cv=none; d=google.com; s=arc-20160816; b=qRjqHoKldGmWEi0Zx5hwbnKA0akzbvjusdJw2uasYhn+zq0fGRnPXHSJAHHn1AQomN cKaXsGPHvzk13WCe9P7foQHUJ3ANLLUyFNUe3AASqmm3biuAhtE/KsfcuUxYFWAkMii7 7WozG9euFPz12ZlhsIlQ/AhbeVecErF8r87S11uzdPNlPBzb0PWsQ8dcire46d4FwEFi pXoayQ0XfuQqoH4oG0o7EYUVaPhZO0V4y6lwnqGHstZOJSMPdlgTqi5hajOgqdaQ8L06 UXnPStI/gkh4ETnyiGgMJweq6Z3WZXImmzP5VUA8n69yHe7T3zrmhTW5nYeb5rc1hmiP 7gqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=5F2YZ+zSEbu/k1s4F5etygangPIkg0EznnOkCCfoew4=; b=0GHYbXYMH4RuUqaDK1/Z9f2EY+lRiqkfdWEqMcyUKbrH95B3WtEoBDtqNxmfeREadY zCX/gXRjuPq8R0RPUBOdyAW8FC2LX7nYU4K4hOQgkEZ5g5NzGRLIq6rv/4625i4BeiHd SV1aPnM0G1LvmHsF/5XWBeFZS6WzNXVP8SFDbNYqkzDSVwYLPgbhapmQZcJn7OyFRi5Z 0MludqjUJ6llU8/KeZgo5pA4JgLAIGgk7mhZzaPQC5yOxdujBJ8ijmzkYcd8Wy54zBo+ uv6xe3L+sCehd+fSDwlZU2i0ADrVdp8lbQ1rcPAyaHiqy3BYYosNtBU7YmLDIyQuek5J 4OQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=y9PADbKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a1709029b8c00b0019a59c52fd3si26552543plp.508.2023.05.01.10.03.50; Mon, 01 May 2023 10:04:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=y9PADbKN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233149AbjEARDB (ORCPT + 99 others); Mon, 1 May 2023 13:03:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233153AbjEARBe (ORCPT ); Mon, 1 May 2023 13:01:34 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EAFF540C1 for ; Mon, 1 May 2023 09:56:59 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-55a1427cbacso32880887b3.3 for ; Mon, 01 May 2023 09:56:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682960190; x=1685552190; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5F2YZ+zSEbu/k1s4F5etygangPIkg0EznnOkCCfoew4=; b=y9PADbKNRkycgFljwwW4rqucG3eA68YCKeeZo3WcsU1FmoEQqcIFYkjuX0vuBDZ+Ql NTwaESSbp1RZc7LHc0KY8IZ9ABdUTaz7iD+fqU6hTkv+OjleRGkRYXEn3mmMGDZeb37K RR5c6bta6TNyvXnvanAVd7N5usS73yNUZBprdVddhAocVImGlGKImrhAPkg7FdlsDO1g 0/gV7SzEHyQ+UDygmg90PVZ1tDJNusamWgID4ov2qqJT8Mlk7B+3jkcThGeG+RtvCTCf JdDB5SJtODP6opHYk8cvNwCb/U4pdSF9HDKWf6Vbz1ntZy0fwly+Flt/OMhilMEsPt5F Cb4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682960190; x=1685552190; 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=5F2YZ+zSEbu/k1s4F5etygangPIkg0EznnOkCCfoew4=; b=FtC6LlwYceAZybQw4keZfchn8ZrAo7YWDHT1KYAx6N6csO12bO7EgOJ1gUW8e7gGwn ZS7K1ki3EJSPDwqSLzDJtAO6xQL7tV7ECDrFTvex8vno3W/+s1hRWj7518Fekh0rzPZt 35O9HcyQvym4W7smZFSGFGz4G4YIVQ9qD8Jy3W7V5vZ/Xcu6D6mecV8xjkblf3IYeXIB qhhJ4abI3eRdUZceZSy+jNLKftQxlnHC3Q+AioX0UEs8Av8YMN6/M3YTBdlCNowj9tRj ygPUspRGXOxuQ7iiQyRYySfkpT2zeK/Dh86pZuz9ovhzJ0lDMOhv8pu3WdCGLynk7a12 VKBg== X-Gm-Message-State: AC+VfDwZXR1KPJdSZNa0VyKRhZ61vqniuo1L6BnmO89gV3hZd/qdyTyn SROIt1S+j6eqJIEMFZQQOyQNAizOwwk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a81:a8c4:0:b0:54d:3afc:d503 with SMTP id f187-20020a81a8c4000000b0054d3afcd503mr8631819ywh.8.1682960190091; Mon, 01 May 2023 09:56:30 -0700 (PDT) Date: Mon, 1 May 2023 09:54:46 -0700 In-Reply-To: <20230501165450.15352-1-surenb@google.com> Mime-Version: 1.0 References: <20230501165450.15352-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501165450.15352-37-surenb@google.com> Subject: [PATCH 36/40] lib: add memory allocations report in show_mem() From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, ldufour@linux.ibm.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Include allocations in show_mem reports. Signed-off-by: Kent Overstreet Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 2 ++ lib/alloc_tag.c | 48 +++++++++++++++++++++++++++++++++++---- lib/show_mem.c | 15 ++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 2a3d248aae10..190ab793f7e5 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -23,6 +23,8 @@ struct alloc_tag { #ifdef CONFIG_MEM_ALLOC_PROFILING +void alloc_tags_show_mem_report(struct seq_buf *s); + static inline struct alloc_tag *ctc_to_alloc_tag(struct codetag_with_ctx *ctc) { return container_of(ctc, struct alloc_tag, ctc); diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 675c7a08e38b..e2ebab8999a9 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -13,6 +13,8 @@ #define STACK_BUF_SIZE 1024 +static struct codetag_type *alloc_tag_cttype; + DEFINE_STATIC_KEY_TRUE(mem_alloc_profiling_key); /* @@ -133,6 +135,43 @@ static ssize_t allocations_file_read(struct file *file, char __user *ubuf, return err ? : buf.ret; } +void alloc_tags_show_mem_report(struct seq_buf *s) +{ + struct codetag_iterator iter; + struct codetag *ct; + struct { + struct codetag *tag; + size_t bytes; + } tags[10], n; + unsigned int i, nr = 0; + + codetag_init_iter(&iter, alloc_tag_cttype); + + codetag_lock_module_list(alloc_tag_cttype, true); + while ((ct = codetag_next_ct(&iter))) { + n.tag = ct; + n.bytes = lazy_percpu_counter_read(&ct_to_alloc_tag(ct)->bytes_allocated); + + for (i = 0; i < nr; i++) + if (n.bytes > tags[i].bytes) + break; + + if (i < ARRAY_SIZE(tags)) { + nr -= nr == ARRAY_SIZE(tags); + memmove(&tags[i + 1], + &tags[i], + sizeof(tags[0]) * (nr - i)); + nr++; + tags[i] = n; + } + } + + for (i = 0; i < nr; i++) + alloc_tag_to_text(s, tags[i].tag); + + codetag_lock_module_list(alloc_tag_cttype, false); +} + static const struct file_operations allocations_file_ops = { .owner = THIS_MODULE, .open = allocations_file_open, @@ -409,7 +448,6 @@ EXPORT_SYMBOL(page_alloc_tagging_ops); static int __init alloc_tag_init(void) { - struct codetag_type *cttype; const struct codetag_type_desc desc = { .section = "alloc_tags", .tag_size = sizeof(struct alloc_tag), @@ -417,10 +455,10 @@ static int __init alloc_tag_init(void) .free_ctx = alloc_tag_ops_free_ctx, }; - cttype = codetag_register_type(&desc); - if (IS_ERR_OR_NULL(cttype)) - return PTR_ERR(cttype); + alloc_tag_cttype = codetag_register_type(&desc); + if (IS_ERR_OR_NULL(alloc_tag_cttype)) + return PTR_ERR(alloc_tag_cttype); - return dbgfs_init(cttype); + return dbgfs_init(alloc_tag_cttype); } module_init(alloc_tag_init); diff --git a/lib/show_mem.c b/lib/show_mem.c index 1485c87be935..5c82f29168e3 100644 --- a/lib/show_mem.c +++ b/lib/show_mem.c @@ -7,6 +7,7 @@ #include #include +#include void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) { @@ -34,4 +35,18 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) #ifdef CONFIG_MEMORY_FAILURE printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); #endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + { + struct seq_buf s; + char *buf = kmalloc(4096, GFP_ATOMIC); + + if (buf) { + printk("Memory allocations:\n"); + seq_buf_init(&s, buf, 4096); + alloc_tags_show_mem_report(&s); + printk("%s", buf); + kfree(buf); + } + } +#endif } -- 2.40.1.495.gc816e09b53d-goog