From patchwork Thu Mar 21 16:36:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13599189 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 D7400C6FD1F for ; Thu, 21 Mar 2024 16:38:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E91E6B00C1; Thu, 21 Mar 2024 12:38:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 598BE6B00C2; Thu, 21 Mar 2024 12:38:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 327426B00C3; Thu, 21 Mar 2024 12:38:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1DEC96B00C1 for ; Thu, 21 Mar 2024 12:38:22 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E8E591601AD for ; Thu, 21 Mar 2024 16:38:21 +0000 (UTC) X-FDA: 81921604002.23.77AFF37 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf22.hostedemail.com (Postfix) with ESMTP id 51A5CC0006 for ; Thu, 21 Mar 2024 16:38:20 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GHAaHRBI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3e2L8ZQYKCGgYaXKTHMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3e2L8ZQYKCGgYaXKTHMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711039100; 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=iYMC0aOkTaJuIlgenfbozu9BZf0q2gbSOAGaj6P53nM=; b=1WybYPrzexn3gohsvKSUoliKn4uj3FPAIXkyd5jFfxQx69SSNwroGN2orJKBijVMejnibr 2Y6nleNvB9hwLQ+1LnfcHGjAnDWkCOegmORlSE0AlpbEXH+L1lpfSzkDRN8aBdVbL8w2Fq EniSV3g2RHoZok3UgzRy98RVFbimB6Y= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GHAaHRBI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3e2L8ZQYKCGgYaXKTHMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3e2L8ZQYKCGgYaXKTHMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711039100; a=rsa-sha256; cv=none; b=4gudAPFpN05jIcrLWc3CWThxmQbDuOiZR9Z1AmAxxvd77rYWsxTqjRiWtkSldnxV4+1cr5 zKKpt4Kb4/PtPfDJfH7tbr1h5Un280VSk5jFtqTSdpWA6laZ7I0WWrvlNdawfGhPNitKtN RzNUJpnfJzeY2tNzVuIj2wEeBa0K0KU= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc15b03287so1686715276.3 for ; Thu, 21 Mar 2024 09:38:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039099; x=1711643899; 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=iYMC0aOkTaJuIlgenfbozu9BZf0q2gbSOAGaj6P53nM=; b=GHAaHRBIWh4cHaCkWevDiMuHXFHTMBF9VXE8fPbjZ2qE4jIUeO75ywTuMODfeHItSs Bxt5R26K3HlGCJItLoT21Rzan0tBYhUh+rlCc0D9Ss9AX3ANMAqroH2BVvw14FSY66Ps L/z78Ph4RyIpOC8GJU8jIm0xKNMKrlRitsl13AtMJF8t7kYHy+8pvrLDTAwlKsRLZhm6 3juzwq1a7O9kbmoAK8kgcjANgRvvoxx5p2a/rlqYakpzmL1kTKVkr/W6//YzfrN9I1s7 z7Fg86C+CeRKe63gTxDGRi8eRCJOHyeJa/Q1261ZQBoO9Y78cOXmqol7BNqGAX3uO43g Npfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039099; x=1711643899; 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=iYMC0aOkTaJuIlgenfbozu9BZf0q2gbSOAGaj6P53nM=; b=To3cNQ0zl5GVJ/T4h8RVy+xsN9WOk1PR1bbXu9YbFYyCMkFRMrxq+aVeHjyXjKz6RR 8LNdfcp/JUvUtNGtGAd0i6JErYb9LVYal1kZsAH8kbd3atxcv9MgOIrT27ECenEItFqI El/joDboIkbba1ObyLjhBDE00akhxrFDaZ7GggvFCKZ9lOKTRGLIiNORfsI3GSjPvDSW cDKWVm/2iY4U+QrQTjuMSECWOSl002uFG69K9qscyRqY74FDL6D525ScVxmk2Ygx/75m P/u6Fwi4XzJ0Puh/TLyzCb3sLqvVwOh4BAIHCgzRKD2jeadB1IHt9bCI2rPVEBfie9xd sOdw== X-Forwarded-Encrypted: i=1; AJvYcCXcXQtLs65O4yVdJ0XK/rMv3XwcdrrW9DhCUYMrimdtVbbWvoVbjYglJQDabzBrF38npuxbS+Sph558nCDC+W4FN80= X-Gm-Message-State: AOJu0Yw42xbfwIe2RVjh38PnKnyrS89spH12SnpG8XlWGWt11gP/5+p8 kAa7KVr1QbsUJYBu7JjwRDM3F0FAwcSpW9DLD5GBCRuvHk9JLlvyUs4yTnvb+uqethrBnRBNSrs kOw== X-Google-Smtp-Source: AGHT+IEl39fT88zAybCKFbgD7Ybs+bIKptMpR9e50EtGQF6WZVLSjiN4xDj0a9gSVDcCEVc03olmfnidgNM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:a489:6433:be5d:e639]) (user=surenb job=sendgmr) by 2002:a05:6902:2009:b0:dcd:c091:e86 with SMTP id dh9-20020a056902200900b00dcdc0910e86mr979486ybb.13.1711039099264; Thu, 21 Mar 2024 09:38:19 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:54 -0700 In-Reply-To: <20240321163705.3067592-1-surenb@google.com> Mime-Version: 1.0 References: <20240321163705.3067592-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-33-surenb@google.com> Subject: [PATCH v6 32/37] 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, jhubbard@nvidia.com, 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, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.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-Rspamd-Queue-Id: 51A5CC0006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8of6q8cxdqdmt8hgwg8okourdbmmn538 X-HE-Tag: 1711039100-957490 X-HE-Meta: U2FsdGVkX1+bE8PlWzOTayt0R8MJp4Wu+RlcbwDdbqTTuaVzkAp5mcWJWGad7ADJT/J6voxAwGsxlrIoL3LIxLf+899Zvv3MR2O69hzqdazxTFfqWS/tw6NLGTlVmnoEI1xEyAWT5ckacc3Rd2odJX5FC3/MTRv/455+irwtubmv8fCffHZTOqjHwCZfJVtNo0rhLnbC2R7Xesr7mVPe8MsszuiU8E/BG9AohdUWiikM5oSHuq0g1lxZ0JuZnZbENEyVWVaacl70h8YPb9VzBKq+eEBB2fIfHgcwIjQTj6l50HCu+CwjxX+eb/SAAOtuApYFfDhcF07t5CgGsa8CehGtr3MnBVg8ADNvJn6/mOW7RYkiNdAIvlmDwenhJIIx3ZHPCDrmDWfwNHCySAw6dfHwitsI3prSa4qIQPRYAEte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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 cf69e037f645..aefe3c81a1e3 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 617c2fbb6673..e24830c44783 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 408062f722ce..5ace625f2328 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..bdb439551eef 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) { + pr_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) + pr_notice("%12lli %8llu %s:%u [%s] func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->modname, ct->function); + else + pr_notice("%12lli %8llu %s:%u func:%s\n", + counter.bytes, counter.calls, ct->filename, + ct->lineno, ct->function); + } + } + } +#endif }