From patchwork Wed Feb 21 19:40:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13566211 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 7D173C5478A for ; Wed, 21 Feb 2024 19:42:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7647B6B00C1; Wed, 21 Feb 2024 14:42:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6ED116B00C2; Wed, 21 Feb 2024 14:42:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A4326B00C3; Wed, 21 Feb 2024 14:42:08 -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 2FA466B00C1 for ; Wed, 21 Feb 2024 14:42:08 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 10806C0B6C for ; Wed, 21 Feb 2024 19:42:08 +0000 (UTC) X-FDA: 81816831936.02.5DE8DED Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 71F9812000F for ; Wed, 21 Feb 2024 19:42:06 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=10ImSj37; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3DVLWZQYKCEAuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3DVLWZQYKCEAuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708544526; a=rsa-sha256; cv=none; b=a/WG41+FTNf0R6U0H+YJtshGlswnnyWa9mmZetfK7b4QXd89cw6eRrwx65PjAcpbSb2ScE 8SrxGRbvdngTNPzk/aInksGGXk0yFzpiZDMM3OZQgtZqjX3e278/JDw6FvBNJrd6PHi+02 At9PYZtsDmCmPOGAyOHBI+x8epe44oc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=10ImSj37; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3DVLWZQYKCEAuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3DVLWZQYKCEAuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708544526; 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=LC7kagHHnwmHwKr3BMy6MtJI28X2XJIX7s9nejqSpcw=; b=J7ke8+h/6jHpyM0qsyAEd429YgpubSYLdnHlGi8IHb/fRuqKDXE0AbLrefPSP3+TpaOP5w rPkTSHWkeB43fGTPgXaxrQU5YAd35kDHfyq4H9FwVx5oMVkBQ6ifKf1MauA5vw9GCi9nH8 iMexG12K2SdshV8VkbPMDYVx0nxabUc= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-608835a1febso18674287b3.2 for ; Wed, 21 Feb 2024 11:42:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708544525; x=1709149325; 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=LC7kagHHnwmHwKr3BMy6MtJI28X2XJIX7s9nejqSpcw=; b=10ImSj37I2MK6te/OE1EpB/mKek/jq3YFdU3s2NBxdv3mlKe70f+lbGW7caeSIOX+p jD9yoZEP8TIQsIhihMExGekuzc3lynjPaBmcKG8gD3Z+g7QOp9uQrNBaD3cLyPA8iSYF eib2XPbH7W83puyxm3dhUB3gDw9HUOp+FPZL3k4eK3ecwqUcJPnBP6wJij+Povi7l0ol Kq+0YyQXJpoUceYk0PeAku5FTdQN/XgrpmtFdyabpdAyoFuLLhAxOwaqxhKSvcRpEDwY gzYrV8LVdYYrx4qPPDpNy1B0U2o293u9tfUKg/SvRg54S35EQnxdStgdBNugVqNLErKM QD1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708544525; x=1709149325; 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=LC7kagHHnwmHwKr3BMy6MtJI28X2XJIX7s9nejqSpcw=; b=vASzNkGzQDnGv4wYYPMZ+pbYJ2HU9hnKoayB+CLgjwaF9oNIvbkajaxgFTH6iiD6Sl JGqFaiNDlld68D18mKg1NLmJIMdb86fs6G+JCtfitxmlml/FukG2RXyiRNWGfZgxwce/ FqVTQz/bPeoQF3dJW7CGetyadCxHPVj2OBB0RsgAeDSkiDQPQWrzuYiYBZZPezK2HNMi KRQrU2jG6LNRgzO/l2MyxvHEWa36+S8irvKcrIz8Nlf4Z9Bm9Nriv64tZcd1uunZ8V1+ IuDCGyUzFOzRFZ4tI9+5oXqTt/X3n+tqXp6FWkjPPKTich5Tbi42IZpnInQBVrroP53F SsCg== X-Forwarded-Encrypted: i=1; AJvYcCWr6xvAa8A8nXDTUZ2AL7ymiXFIOinaFKLfnpBPwFUhtJo42nhK7ELbK2H9N5vXQdjHqSyE0Asj4xv2rFAamZeGsf0= X-Gm-Message-State: AOJu0YzbO76n65TlJ2BUL0NYYSPDDBom4Vs1ZI6dll2BhIeV/2WqEOJI slF24RI5we1xqXmkD2YmOHZU3e2Scv944FdvzCF2Mbsj7r2ym+y51vYnnjDVA2aOgGTSsTGEJCf Rig== X-Google-Smtp-Source: AGHT+IF/fw4+k0W9YQaAhd8X5D2pflUjenW5K2EydUoGfLHK5w3QTNH4iYX84WllsVXa/U8r+txZWwizBxc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:953b:9a4e:1e10:3f07]) (user=surenb job=sendgmr) by 2002:a0d:e611:0:b0:607:9268:6665 with SMTP id p17-20020a0de611000000b0060792686665mr4677189ywe.10.1708544525062; Wed, 21 Feb 2024 11:42:05 -0800 (PST) Date: Wed, 21 Feb 2024 11:40:44 -0800 In-Reply-To: <20240221194052.927623-1-surenb@google.com> Mime-Version: 1.0 References: <20240221194052.927623-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221194052.927623-32-surenb@google.com> Subject: [PATCH v4 31/36] 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, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.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, vvvvvv@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 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 71F9812000F X-Stat-Signature: 3iey99sqg4fi3drud5sr4gw8pscnzy8s X-HE-Tag: 1708544526-347484 X-HE-Meta: U2FsdGVkX1/628OknskFKTU9IK5U4jNTihRYz4RgM2/vKyyjKUZ+JC8rrq8B/5i7bY2a/BZYWFFQ0QPw9ohXN3Abz5H+x0cGEMSlTfDM+fHSMaoOgqO5MZNWDJRKFBTJVK3QNDNuLu3tZDEOhs3zeJZbFfJ/QpNLWbXv5EPvcwvMgAPKGGcmbLvbiHmo/vA11NM65rPaE3s2xEIXAp9DhegIUgC0fhV8nQrYWYu2+c0TopWXEau8kS+lgM1U7+CfBKzScfdkS6/jNDNGddfh9wmGOMCYSBwXIRf1wgplW79/z7hVw7WkugKutavAW4mVDm8fvh4jud3+hgAiPoEVj5kJ3uU5Pgiu1gSzkd1BfZ9Os0OcvfgOMHdpfcOhYFVfyx1iZwCOMxdOWLTGMz2ExwLjE7MTJJEJusSTNDkJ1iSRFhkTMwle07dOxd8DSHETqIjNuu8Q1NJiJQr+98VlMRk+UFx9GzTsf62E0rywBt9Acd9D/Y3rnmxRo+WlsMaLlUyoxMH3WprccD3N7ZFdGjfnPxuR7d8AUPODnLu7dwNib6e4RurnqLycydzkYafmzppM3knv7wjkZ5fFsj1mwe4kafwcCgd3iosOvSfpTH12/EM6Uq+z3fqYcGTnkEL7jQ29dVi23tJMYpsAy2zPI++owSJLyBti96XvH7c2qCy9nzLNO730OEaxiBBYaQ4bseGxIgDwVDCZIjuJNnn42nytStwjh1ptkSPpOONcr+lhAhd8d1wNgaOtjL2LWQecq9Tg32yQgGWfu0gj1HLtINsI4x/n5EmZWdshp4Sh+e2ivzoOEcsFEEMFjzw6myM0EaA6umYLfrJ9pSGD7HJGT8IdlapCyf7o/oWoXwWjJrQZebbtXfPqwKD/chQfS8BAYxzksMQres4w8VINeXfjincXVMVHkN+fT5RUwNPbrxaDHCojUUxhsmQMluU43le+nJ68XoPb2PT1QodmM/u gAPPdeF4 jigjXpt0qKgS6aKXntvL8Xq5wjioEZUnCZOvQTg2bY9DQcCa3Ct6ci9bTZ3CavTnoQLa38kNA2CaR7d7LlM7WotRV07nBFsDEYIKTmroMRQ0GtyjUdVv7kt1E799xW06QSB9s7xSub/0u8pZ31+yNVejq4vdmpxntglSvjmg/O80JI4khyXobKN4VZKDVZANPiVU+8XDbQ+sPr4CMR4wrRKe9uJmc4LzXI9/OP7vSodAXUStdqSZ/BVkyuqViw9mySxdtzIrJhT6jDOvIZWfpgwLdTCUvnUqjmJ/yKF0nime3Rn+CZvF6w787+4GuGTV4XrUphNRXQskLv1Gh8sLQI6L0U8LiFcAeA8f550sq5I1g1iWqvX6SAqtcGnqSlVzFqnCHGtctr8ioaTSU59Q1j0HkDXZN+PxokSKMI3kcs8qzPm5nwTLLmgxSEdT5JxXzK8/9WFcb8Ic4fxq1fpAlATBXvA== 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: Include allocations in show_mem reports. Signed-off-by: Kent Overstreet Signed-off-by: Suren Baghdasaryan Reviewed-by: Vlastimil Babka --- include/linux/alloc_tag.h | 7 +++++++ include/linux/codetag.h | 1 + lib/alloc_tag.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/codetag.c | 5 +++++ mm/show_mem.c | 26 ++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 29636719b276..85a24a027403 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,6 +30,13 @@ struct alloc_tag { #ifdef CONFIG_MEM_ALLOC_PROFILING +struct codetag_bytes { + struct codetag *ct; + s64 bytes; +}; + +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep); + static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct) { return container_of(ct, struct alloc_tag, ct); diff --git a/include/linux/codetag.h b/include/linux/codetag.h index bfd0ba5c4185..c2a579ccd455 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -61,6 +61,7 @@ struct codetag_iterator { } void codetag_lock_module_list(struct codetag_type *cttype, bool lock); +bool codetag_trylock_module_list(struct codetag_type *cttype); struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype); struct codetag *codetag_next_ct(struct codetag_iterator *iter); diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index cb5adec4b2e2..ec54f29482dc 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -86,6 +86,44 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep) +{ + struct codetag_iterator iter; + struct codetag *ct; + struct codetag_bytes n; + unsigned int i, nr = 0; + + if (can_sleep) + codetag_lock_module_list(alloc_tag_cttype, true); + else if (!codetag_trylock_module_list(alloc_tag_cttype)) + return 0; + + iter = codetag_get_ct_iter(alloc_tag_cttype); + while ((ct = codetag_next_ct(&iter))) { + struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct)); + + n.ct = ct; + n.bytes = counter.bytes; + + for (i = 0; i < nr; i++) + if (n.bytes > tags[i].bytes) + break; + + if (i < count) { + nr -= nr == count; + memmove(&tags[i + 1], + &tags[i], + sizeof(tags[0]) * (nr - i)); + nr++; + tags[i] = n; + } + } + + codetag_lock_module_list(alloc_tag_cttype, false); + + return nr; +} + static void __init procfs_init(void) { proc_create_seq("allocinfo", 0444, NULL, &allocinfo_seq_op); diff --git a/lib/codetag.c b/lib/codetag.c index b13412ca57cc..7b39cec9648a 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -36,6 +36,11 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock) up_read(&cttype->mod_lock); } +bool codetag_trylock_module_list(struct codetag_type *cttype) +{ + return down_read_trylock(&cttype->mod_lock) != 0; +} + struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype) { struct codetag_iterator iter = { diff --git a/mm/show_mem.c b/mm/show_mem.c index 8dcfafbd283c..1e41f8d6e297 100644 --- a/mm/show_mem.c +++ b/mm/show_mem.c @@ -423,4 +423,30 @@ 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 codetag_bytes tags[10]; + size_t i, nr; + + nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false); + if (nr) { + printk(KERN_NOTICE "Memory allocations:\n"); + for (i = 0; i < nr; i++) { + struct codetag *ct = tags[i].ct; + struct alloc_tag *tag = ct_to_alloc_tag(ct); + struct alloc_tag_counters counter = alloc_tag_read(tag); + + /* Same as alloc_tag_to_text() but w/o intermediate buffer */ + if (ct->modname) + printk(KERN_NOTICE "%12lli %8llu %s:%u [%s] func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->modname, ct->function); + else + printk(KERN_NOTICE "%12lli %8llu %s:%u func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->function); + } + } + } +#endif }