From nobody Fri Dec 19 04:18:35 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6481AC77B7C for ; Mon, 1 May 2023 17:03:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233270AbjEARDC (ORCPT ); Mon, 1 May 2023 13:03:02 -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-Google-Smtp-Source: ACHHUZ4bAJGviPj6gi0jXXmQNcf1vAdyBJ0X5d7ddmrZp75YBKgThQIRi2xp+pDFZfbHaui7RkEBtZ2XytA= 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 { =20 #ifdef CONFIG_MEM_ALLOC_PROFILING =20 +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 @@ =20 #define STACK_BUF_SIZE 1024 =20 +static struct codetag_type *alloc_tag_cttype; + DEFINE_STATIC_KEY_TRUE(mem_alloc_profiling_key); =20 /* @@ -133,6 +135,43 @@ static ssize_t allocations_file_read(struct file *file= , char __user *ubuf, return err ? : buf.ret; } =20 +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 =3D 0; + + codetag_init_iter(&iter, alloc_tag_cttype); + + codetag_lock_module_list(alloc_tag_cttype, true); + while ((ct =3D codetag_next_ct(&iter))) { + n.tag =3D ct; + n.bytes =3D lazy_percpu_counter_read(&ct_to_alloc_tag(ct)->bytes_allocat= ed); + + for (i =3D 0; i < nr; i++) + if (n.bytes > tags[i].bytes) + break; + + if (i < ARRAY_SIZE(tags)) { + nr -=3D nr =3D=3D ARRAY_SIZE(tags); + memmove(&tags[i + 1], + &tags[i], + sizeof(tags[0]) * (nr - i)); + nr++; + tags[i] =3D n; + } + } + + for (i =3D 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 =3D { .owner =3D THIS_MODULE, .open =3D allocations_file_open, @@ -409,7 +448,6 @@ EXPORT_SYMBOL(page_alloc_tagging_ops); =20 static int __init alloc_tag_init(void) { - struct codetag_type *cttype; const struct codetag_type_desc desc =3D { .section =3D "alloc_tags", .tag_size =3D sizeof(struct alloc_tag), @@ -417,10 +455,10 @@ static int __init alloc_tag_init(void) .free_ctx =3D alloc_tag_ops_free_ctx, }; =20 - cttype =3D codetag_register_type(&desc); - if (IS_ERR_OR_NULL(cttype)) - return PTR_ERR(cttype); + alloc_tag_cttype =3D codetag_register_type(&desc); + if (IS_ERR_OR_NULL(alloc_tag_cttype)) + return PTR_ERR(alloc_tag_cttype); =20 - 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 @@ =20 #include #include +#include =20 void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_id= x) { @@ -34,4 +35,18 @@ void __show_mem(unsigned int filter, nodemask_t *nodemas= k, 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 =3D 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 } --=20 2.40.1.495.gc816e09b53d-goog