Message ID | 20240221194052.927623-32-surenb@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-75411-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp1262547dyc; Wed, 21 Feb 2024 11:54:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX7e6mQabB77m8XXe+m3XRwX2fwSUl2UCQEle+SOOFXpZxDB+V/735HlV9Lvy1pw4/4vHnhUkqZOEetd+XMONtZfAoG0g== X-Google-Smtp-Source: AGHT+IEY2fhLFGsdJiH1Iu7QaMQVcP9A+s7d5JNFuOn4LRMwRxCKhh2Ox/D9eRTGXlS4WlDS7caP X-Received: by 2002:a50:fb8d:0:b0:564:e994:7f0d with SMTP id e13-20020a50fb8d000000b00564e9947f0dmr2718394edq.23.1708545293647; Wed, 21 Feb 2024 11:54:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708545293; cv=pass; d=google.com; s=arc-20160816; b=DmV9MVXaRYprpuVjh6YX1bt/xDag4VjiR5sPZVA65Rf4z+UhGfhoSjnM5rXq5FCKYf bIIAHO0x26WzgGQ65BJ8cNySY4yAzCTX5ffDCCCuz/Attgr/9ePMDwJVzkzj0A9QBaFi DQpjf98lePhTLbwb3MRE0t9gbDGbLyBf8Rs6SecwDhXbrTAUfrEF4V5EzelUUZV2DTmi eh1s7KQ6bi9cgGV55tF8abRmZbsyXmIi82JPrn9+Cra1DHcIUCIPkgtEJRqc1m0CG8Ik dH/AOdS2a5B9nT2HDfO5Eb5c8ureQWs7f5J+yO3uYkghnMmMQFJ5TxfgFhewNd3/OGKl cCKQ== 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=LC7kagHHnwmHwKr3BMy6MtJI28X2XJIX7s9nejqSpcw=; fh=ZwPGoPASMTdquTOaGPpNhb9mFg3rmyYyKxU5+JKrGf0=; b=hfQ51Nlpeg+oPXsjatBxtlMXdbAU7xEAFcHde7bUgYQ6gb3cgfjYkNmoA2YtvjEW+6 JpsgqCa6/dGQL00FCGmfp9RMP0QM+Z2f3cVSDQmaO/sI5lEGZiahEfoxZYSnvlsEBDrj 10yGf9DybZhH0SWOXestBPc0Xr0DShtadgzzQNoe8vH96HS5CYktSQTgGLwo5TKVP8Be 8Rsa++rG9/rHArvXDFJ34sjFKr/FqQ7ehMRI2Z1WSr4JNVsEPC8GqvnygMjliWPcyBPk d/CQ6/79zDe64ghncdTUtXbNSXsEdIyOwV3c9xUIuMHwYQ1H80ciSfZ3B7mkNsQCX24n Y+sg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fnDbdDYt; 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-75411-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75411-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id v15-20020aa7d9cf000000b0056415926d13si4462997eds.223.2024.02.21.11.54.53 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 11:54:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75411-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fnDbdDYt; 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-75411-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75411-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 147D81F22FF9 for <ouuuleilei@gmail.com>; Wed, 21 Feb 2024 19:54:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8936015956F; Wed, 21 Feb 2024 19:42:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fnDbdDYt" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 591B21586D9 for <linux-kernel@vger.kernel.org>; Wed, 21 Feb 2024 19:42:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708544528; cv=none; b=FRTYNiNDmshP4gdFdgG4ewSJhbWw7g68l5oBj91D97UaUg0bWzsS0PzUvgKThQLe83kLH8mMQcZmrfgD0+8Qmkh9Y/LTK/1Vr4qVhUcI8+6tq/1oDc20EmlV5EckVefWJyn3SBB0/VHiYwDGx+k2/xfqJcrUvKu4bI/ES8mWfB4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708544528; c=relaxed/simple; bh=P/l83L7DEb0X0XM29YB6WUnPfeb/2u056JWdsmbZtHY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KobrfFOJkxEMATtKIYCYZp9Ua2d+v88cfZYhlKVCRmGTyW7qARACj1nRDtESnUSTDanbanWEzLkLRGpexZOeJIZ9tS3ZbscVgo8aUnmnBcshfHNp+FsRb0WKl2+wlljVoQICYt90gtSYPSGjebEBdmPdbKMEsqCE/7t2pEvTNtc= 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=fnDbdDYt; arc=none smtp.client-ip=209.85.128.201 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-yw1-f201.google.com with SMTP id 00721157ae682-608835a1febso18674267b3.2 for <linux-kernel@vger.kernel.org>; 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=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=LC7kagHHnwmHwKr3BMy6MtJI28X2XJIX7s9nejqSpcw=; b=fnDbdDYt3U/cQbDK96UCXFNcftUz52mXoXDf/4d7FabPCSE3tmRKB5rPVhjr8Qw1oX DTFOo30x7OUfsnq8H+yXaAhTRhelA1ysT39XnwD4g8YnmBuIAn1r+dGsRag+Mh/2kyie Z7WqBrzSTFmA2D8DeZqq2ftBM4vqFC9BsekT1DrMGkRNoAS98BMtetcr+yTXS7GBAKCj iQb8YjVUNpSb965eMSs4tZgdFuKxF7BJDh40zCVU3i7fFUZQNr0puajKsaLH2peslb3m vvrgc0gyhG1uibEgA8kn5WbwubQ06ZskYO+OQckKzqmHGIXLFFZjaburERKNY6PIp79q 9QFA== 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=ddNnv4M1h0t7/b/w8qh14zVpdLON//CJWN6D33H65Tc9E2QcxniRKVDWgd+HelkKiA 8zmq4QJ/ze8f/+cm9yytJVbEs5uXYwgyB5O5x7YxF0mbwjHJo5lYRZIYQX9CPuDzpp5G 6pVchEv2JK48fVa15RkQKmw6oHju5J0UERPsSVwTn5lRurXJ24kICDndwgnGUEBQoMx/ XRhiUVHx7WW+wgn9vrpbdNOCqfiZRIlT4pXfl8TJy2kcyCj0Osz3eMTifK/ZC58pEpIv YQj+H4fi1NIwqfh4rjfyHwYmzYJx6TuK1qA5owfs1Lftyf+YBlaHOdYU8zYWvQzA7uE7 qJFw== X-Forwarded-Encrypted: i=1; AJvYcCXI2CXw3/wde2yiZ4C1EB+8goFGNhmHfb+H3yiLrbtEqZYAc+gqcYsJODur7P/r+0iqppp34idFWGryKaF7TepohGscQbotAwbmQYPk X-Gm-Message-State: AOJu0YykPM0NmjrW9xHAeb/p4U32738Cd5KzN7ZrPuoL63IDqXzgX8zg 15jFoqqCjpV/YVVC6QeGB6fN71kMYSRDtWiPMW6Ki7sY5ftmKpN6N/BJdfB48OwF9EJaIHXxCKm Rew== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> 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 <surenb@google.com> 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 Content-Type: text/plain; charset="UTF-8" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791539589663078546 X-GMAIL-MSGID: 1791539589663078546 |
Series |
Memory allocation profiling
|
|
Commit Message
Suren Baghdasaryan
Feb. 21, 2024, 7:40 p.m. UTC
Include allocations in show_mem reports. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> Signed-off-by: Suren Baghdasaryan <surenb@google.com> --- 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(+)
Comments
On 2/21/24 20:40, Suren Baghdasaryan wrote: > Include allocations in show_mem reports. > > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> > Signed-off-by: Suren Baghdasaryan <surenb@google.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Nit: there's pr_notice() that's shorter than printk(KERN_NOTICE > --- > 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 > }
On Tue, Feb 27, 2024 at 5:18 AM Vlastimil Babka <vbabka@suse.cz> wrote: > > On 2/21/24 20:40, Suren Baghdasaryan wrote: > > Include allocations in show_mem reports. > > > > Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> > > Signed-off-by: Suren Baghdasaryan <surenb@google.com> > > Reviewed-by: Vlastimil Babka <vbabka@suse.cz> > > Nit: there's pr_notice() that's shorter than printk(KERN_NOTICE I used printk() since other parts of show_mem() used it but I can change if that's preferable. > > > --- > > 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 > > } > > -- > To unsubscribe from this group and stop receiving emails from it, send an email to kernel-team+unsubscribe@android.com. >
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 }