From: Kent Overstreet <kent.overstreet@gmail.com>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org
Cc: Kent Overstreet <kent.overstreet@gmail.com>,
hch@lst.de, hannes@cmpxchg.org, akpm@linux-foundation.org,
linux-clk@vger.kernel.org, linux-tegra@vger.kernel.org,
linux-input@vger.kernel.org, roman.gushchin@linux.dev
Subject: [PATCH v2 5/8] mm: Add a .to_text() method for shrinkers
Date: Thu, 21 Apr 2022 19:48:34 -0400 [thread overview]
Message-ID: <20220421234837.3629927-11-kent.overstreet@gmail.com> (raw)
In-Reply-To: <20220421234837.3629927-1-kent.overstreet@gmail.com>
This adds a new callback method to shrinkers which they can use to
describe anything relevant to memory reclaim about their internal state,
for example object dirtyness.
This uses the new printbufs to output to heap allocated strings, so that
the .to_text() methods can be used both for messages logged to the
console, and also sysfs/debugfs.
This patch also adds shrinkers_to_text(), which reports on the top 10
shrinkers - by object count - in sorted order, to be used in OOM
reporting.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
---
include/linux/shrinker.h | 5 +++
mm/vmscan.c | 78 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+)
diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h
index 76fbf92b04..b5f411768b 100644
--- a/include/linux/shrinker.h
+++ b/include/linux/shrinker.h
@@ -2,6 +2,8 @@
#ifndef _LINUX_SHRINKER_H
#define _LINUX_SHRINKER_H
+struct printbuf;
+
/*
* This struct is used to pass information from page reclaim to the shrinkers.
* We consolidate the values for easier extension later.
@@ -58,10 +60,12 @@ struct shrink_control {
* @flags determine the shrinker abilities, like numa awareness
*/
struct shrinker {
+ char name[32];
unsigned long (*count_objects)(struct shrinker *,
struct shrink_control *sc);
unsigned long (*scan_objects)(struct shrinker *,
struct shrink_control *sc);
+ void (*to_text)(struct printbuf *, struct shrinker *);
long batch; /* reclaim batch size, 0 = default */
int seeks; /* seeks to recreate an obj */
@@ -94,4 +98,5 @@ extern int register_shrinker(struct shrinker *shrinker);
extern void unregister_shrinker(struct shrinker *shrinker);
extern void free_prealloced_shrinker(struct shrinker *shrinker);
extern void synchronize_shrinkers(void);
+void shrinkers_to_text(struct printbuf *);
#endif
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 59b14e0d69..905bc23800 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -50,6 +50,7 @@
#include <linux/printk.h>
#include <linux/dax.h>
#include <linux/psi.h>
+#include <linux/printbuf.h>
#include <asm/tlbflush.h>
#include <asm/div64.h>
@@ -702,6 +703,83 @@ void synchronize_shrinkers(void)
}
EXPORT_SYMBOL(synchronize_shrinkers);
+void shrinker_to_text(struct printbuf *out, struct shrinker *shrinker)
+{
+ struct shrink_control sc = { .gfp_mask = GFP_KERNEL, };
+
+ if (shrinker->name[0])
+ pr_buf(out, "%s", shrinker->name);
+ else
+ pr_buf(out, "%ps:", shrinker->scan_objects);
+
+ pr_buf(out, " objects: %lu", shrinker->count_objects(shrinker, &sc));
+ pr_newline(out);
+
+ if (shrinker->to_text) {
+ pr_indent_push(out, 2);
+ shrinker->to_text(out, shrinker);
+ pr_indent_pop(out, 2);
+ pr_newline(out);
+ }
+}
+
+/**
+ * shrinkers_to_text - Report on shrinkers with highest usage
+ *
+ * This reports on the top 10 shrinkers, by object counts, in sorted order:
+ * intended to be used for OOM reporting.
+ */
+void shrinkers_to_text(struct printbuf *out)
+{
+ struct shrinker *shrinker;
+ struct shrinker_by_mem {
+ struct shrinker *shrinker;
+ unsigned long mem;
+ } shrinkers_by_mem[10];
+ int i, nr = 0;
+
+ if (!down_read_trylock(&shrinker_rwsem)) {
+ pr_buf(out, "(couldn't take shrinker lock)");
+ return;
+ }
+
+ list_for_each_entry(shrinker, &shrinker_list, list) {
+ struct shrink_control sc = { .gfp_mask = GFP_KERNEL, };
+ unsigned long mem = shrinker->count_objects(shrinker, &sc);
+
+ if (!mem || mem == SHRINK_STOP || mem == SHRINK_EMPTY)
+ continue;
+
+ for (i = 0; i < nr; i++)
+ if (mem < shrinkers_by_mem[i].mem)
+ break;
+
+ if (nr < ARRAY_SIZE(shrinkers_by_mem)) {
+ memmove(&shrinkers_by_mem[i + 1],
+ &shrinkers_by_mem[i],
+ sizeof(shrinkers_by_mem[0]) * (nr - i));
+ nr++;
+ } else if (i) {
+ i--;
+ memmove(&shrinkers_by_mem[0],
+ &shrinkers_by_mem[1],
+ sizeof(shrinkers_by_mem[0]) * i);
+ } else {
+ continue;
+ }
+
+ shrinkers_by_mem[i] = (struct shrinker_by_mem) {
+ .shrinker = shrinker,
+ .mem = mem,
+ };
+ }
+
+ for (i = nr - 1; i >= 0; --i)
+ shrinker_to_text(out, shrinkers_by_mem[i].shrinker);
+
+ up_read(&shrinker_rwsem);
+}
+
#define SHRINK_BATCH 128
static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
--
2.35.2
next prev parent reply other threads:[~2022-04-21 23:50 UTC|newest]
Thread overview: 58+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-21 23:48 [PATCH 0/4] Printbufs & shrinker OOM reporting Kent Overstreet
2022-04-21 23:48 ` [PATCH 1/4] lib/printbuf: New data structure for heap-allocated strings Kent Overstreet
2022-04-21 23:48 ` [PATCH 2/4] mm: Add a .to_text() method for shrinkers Kent Overstreet
2022-04-22 12:21 ` Michal Hocko
2022-04-21 23:48 ` [PATCH 3/4] mm: Centralize & improve oom reporting in show_mem.c Kent Overstreet
2022-04-21 23:48 ` [PATCH 4/4] bcachefs: shrinker.to_text() methods Kent Overstreet
2022-04-21 23:48 ` [PATCH v2 0/8] Printbufs & improved shrinker debugging Kent Overstreet
2022-04-21 23:48 ` [PATCH v2 1/8] lib/printbuf: New data structure for heap-allocated strings Kent Overstreet
2022-04-22 4:20 ` Christoph Hellwig
2022-04-22 5:14 ` Kent Overstreet
2022-04-22 5:22 ` Christoph Hellwig
2022-04-22 5:40 ` Kent Overstreet
2022-04-22 5:52 ` Christoph Hellwig
2022-04-22 6:06 ` Kent Overstreet
2022-04-22 6:11 ` Christoph Hellwig
2022-04-22 6:18 ` Kent Overstreet
2022-04-22 15:37 ` Steven Rostedt
2022-04-22 19:30 ` Kent Overstreet
2022-04-22 19:39 ` Steven Rostedt
2022-04-22 20:30 ` Kent Overstreet
2022-04-22 20:47 ` Steven Rostedt
2022-04-22 21:51 ` Kent Overstreet
2022-04-22 22:20 ` Steven Rostedt
2022-04-22 20:03 ` James Bottomley
2022-04-22 21:13 ` Kent Overstreet
2022-04-23 14:16 ` Rust and Kernel Vendoring [Was Re: [PATCH v2 1/8] lib/printbuf: New data structure for heap-allocated strings] James Bottomley
2022-04-24 20:36 ` Kent Overstreet
2022-04-26 2:22 ` James Bottomley
2022-04-24 23:46 ` [PATCH v2 1/8] lib/printbuf: New data structure for heap-allocated strings Joe Perches
2022-04-25 0:45 ` Kent Overstreet
2022-04-25 2:44 ` Matthew Wilcox
2022-04-25 4:19 ` Kent Overstreet
2022-04-25 4:48 ` Joe Perches
2022-04-25 4:59 ` Kent Overstreet
2022-04-25 5:00 ` Joe Perches
2022-04-25 5:56 ` Kent Overstreet
2022-04-21 23:48 ` [PATCH v2 2/8] Input/joystick/analog: Convert from seq_buf -> printbuf Kent Overstreet
2022-04-21 23:48 ` [PATCH v2 3/8] mm/memcontrol.c: Convert to printbuf Kent Overstreet
2022-04-22 12:28 ` Michal Hocko
2022-04-21 23:48 ` [PATCH v2 4/8] clk: tegra: bpmp: " Kent Overstreet
2022-04-21 23:48 ` Kent Overstreet [this message]
2022-04-21 23:48 ` [PATCH v2 6/8] mm: Count requests to free & nr freed per shrinker Kent Overstreet
2022-04-21 23:48 ` [PATCH v2 7/8] mm: Move lib/show_mem.c to mm/ Kent Overstreet
2022-04-22 12:32 ` Michal Hocko
2022-04-21 23:48 ` [PATCH v2 8/8] mm: Centralize & improve oom reporting in show_mem.c Kent Overstreet
2022-04-22 12:58 ` Michal Hocko
2022-04-22 15:09 ` Roman Gushchin
2022-04-22 23:48 ` Kent Overstreet
2022-04-23 0:27 ` Roman Gushchin
2022-04-23 0:46 ` Kent Overstreet
2022-04-23 1:25 ` Roman Gushchin
2022-04-23 11:48 ` Tetsuo Handa
2022-04-25 9:28 ` Michal Hocko
2022-04-25 15:28 ` Kent Overstreet
2022-04-26 7:17 ` Michal Hocko
2022-04-26 7:26 ` Kent Overstreet
2022-04-26 7:40 ` Michal Hocko
2022-04-30 4:00 ` [PATCH 0/4] Printbufs & shrinker OOM reporting Dave Young
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220421234837.3629927-11-kent.overstreet@gmail.com \
--to=kent.overstreet@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=hannes@cmpxchg.org \
--cc=hch@lst.de \
--cc=linux-clk@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-tegra@vger.kernel.org \
--cc=roman.gushchin@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).