Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp613161lqp; Thu, 21 Mar 2024 10:12:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXswnizPRpwROEqYZ1wOpnIKAm6Ia4/UpPG4tdkAubYR6M6yZXDsxnoQgLUS/Cdqa57HUiWoLgmRL8hQ9AeS0Hm1FYL9IP9HcG2eo0iaQ== X-Google-Smtp-Source: AGHT+IEbnOyNKMK7SgyUfUBeVyl/+YZ4/aXbaZJVGzDqNsVzzAr/lO5jQyrb7U06ceMm8jQitq78 X-Received: by 2002:a17:902:ecd2:b0:1db:f952:eebf with SMTP id a18-20020a170902ecd200b001dbf952eebfmr231plh.44.1711041135887; Thu, 21 Mar 2024 10:12:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711041135; cv=pass; d=google.com; s=arc-20160816; b=TC2DOvpNpTJSY5YkbRY8piyydCpHNIOgr+yhZM55mAFDT4rT5K2tbeGGLztfSbMMsG Ep7KKfwio2/yjp1G3EK/5rfK4s/EgYo6a/GNTbQWIQCYfSBfibP45juV8X6rvDeoqab9 LcOGstIFpNsVy381O4pAL/FrtjbEsvB+O+EvbVxV3IsfqVOGeqG63glCfJW37SLtX+hz IXJ+DFuUkm61n2MgUMcptpgRxs8EMlJX3PHK4iXwBXq5Ty27ELUPB4piVbN7Q5EQKZGg KX1N8hM5iaMANEIqstpE4QL/CDJ96V6q81dM5GQeBKLdxTRvxt2EsYcpAeT+DwqIHSr7 m2Dw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=iYMC0aOkTaJuIlgenfbozu9BZf0q2gbSOAGaj6P53nM=; fh=cvV07OAIfCes2MLrD/Mp/RfZFKm+8Fw31cAyjNPBIiU=; b=xQS7+VShvUTBO9Gx6TdENwytvY1rtmn7Ey85jH6QpAijhkD43j4WBvM8d8XHmJdNW8 LphyUeDib+FHDzc3r49yeqfBmHsXe7vAfdHLO44kpahEez8Rz1VL9Wa8Eb+ScI7HDHjU tysFA/wKxYAbQ7JgX8xI2OmEuT/tSI9FZUsoDgLvlmOdxCBsthI5Yda4h6GbOv2i+bkI 21DeVOFgpTuCivIVzGEDWU6Ey83LruMmbQ8v+hkW1JKuiGXl4DTFpgKI3ulzbFufmvpg yeLbZbb6INiIzKYMnTIc6Xy7jNG3qjYa5RKH5wf6pg0tU5SSWUzKaIIMzUKSQPsyWoHC u3sw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=E0KvrSRM; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110392-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110392-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id c17-20020a170903235100b001dfffe3f79esi78575plh.568.2024.03.21.10.12.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 10:12:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110392-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=E0KvrSRM; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-110392-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110392-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0D0E7B246CA for ; Thu, 21 Mar 2024 16:51:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0E921145328; Thu, 21 Mar 2024 16:38:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="E0KvrSRM" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65A7C143C5A for ; Thu, 21 Mar 2024 16:38:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039103; cv=none; b=qA3JTkTI2n+IMlTb4qUhsl2hspeWB5e88Va/g/HyqYa330kueRFrUzpCaoR72brrFZv8GyEcmUL2XKcgvpfOO8ehRfeqPFXHzfTxJTOXETSTcnqAdWvSe/Yx+mgRagZ50omsgkga5Nd8oHPCnrmhENnLoU3Pvc3ALBQ7GoNQAiQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039103; c=relaxed/simple; bh=fjKlaW/3iFj7zPGvTnymctnlWUNRXZfhInu7vpc6prw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PTrCAPxu23vjhmRLVl3XeORrpUpE/f7vM9rWzll0Uf/Jjf0hPoHZaY/1Nh6WU2wT7p/zGoo6IsB899Rr+Au6SFY72dHCbWZWaGhde/LlLMgp2VFVcYmViCuzJaSOBCGeHZRvv12QKgSNkCDDD9pN73FX4ITWCRUrzaNtrHB1fas= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=E0KvrSRM; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc74ac7d015so1392328276.0 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=vger.kernel.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=E0KvrSRMpOVxB6+HRxKmyqpn6yLuCVPcbTSetv8BZo2Bkk3ZZ0JN+kb4fSidNRcyre 6VivHskWOl+XBLRVQsL4C+66Q2fGO6Sa0U7eyX9wq2RwvAScaK9vggovE12tgLa6aomK ORvcKmBraYoBAzmAbfrQEIdb0KLroarrKp9+KqYOdj9NES1LWjw/r+5msqvpZnPYvkAP MI9vl1+IuR12dohLhhLLICPe0EiD/CUjEBFL5dVoX8Qhsi49rS2RLCsC2TsdKwCeHxW2 ZtCii3YQmJuB4uFezQalvPk+0mKc91xueeVCZBzb67VmDzD58RHw/z8PY+hQsA6v642c EOjA== 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=GfgehVmjdcp8p25YNXZjd4NrqNGcVuWGv2YrluD5WRUTJmqTNzmFFiOC94lKuEapAg w+5dB7eJoQ4JklpzGNdMNjc4lAiVrm7TiEPwvBPiWAclD9XMyKnRFT8OWG87Om1lLuN0 GC4dV1c5j5V9C80KuAyrnD3E/2A/MwkXqynzvmxJWogcC098yBHoMrIevkEuqZYRfk0Y 0UAj+jgQjVKBGODeJscNvhiUkCCnn3J1tTgqfffLRinP9/6/wFjdyBKgOexcic1JX4YY 8KRgWhvsUWj82czFRT0ooyIAvstSxwfGAN/cwRxVnVVA3VRdwrAr+M99MfU3mZd0tJXd 4hlw== X-Forwarded-Encrypted: i=1; AJvYcCUstCt9lS4sv5L0vOBY926tCQv3SFYa9AkSP8AYgFy8tbbqznBWh4Ubv5zX806+2qt8Guci5RGhoBoYzDsX0Mh+vnfbaBor/P7ydNN1 X-Gm-Message-State: AOJu0YwnY63Z2AnFOMykkQBk2YQ+prlas/MgpOLiAtofisw2947aTuXa gIFQFugIkvEQZ+4uPEFevr5reebcLS2TmTeL/ghqZIvm8syoTLqDqs2rbvVpu1XxF8sCgFdUblE eMw== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Type: text/plain; charset="UTF-8" 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 } -- 2.44.0.291.gc1ea87d7ee-goog