From patchwork Sat Aug 24 19:10:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 13776493 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 D2D8FC5320E for ; Sat, 24 Aug 2024 19:10:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A93F6B0487; Sat, 24 Aug 2024 15:10:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39EA3800EE; Sat, 24 Aug 2024 15:10:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C9DB6B048A; Sat, 24 Aug 2024 15:10:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E72E4800E9 for ; Sat, 24 Aug 2024 15:10:32 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8D95140888 for ; Sat, 24 Aug 2024 19:10:32 +0000 (UTC) X-FDA: 82488080304.17.998D962 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) by imf25.hostedemail.com (Postfix) with ESMTP id BBA14A0012 for ; Sat, 24 Aug 2024 19:10:30 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=hd8pUDRY; spf=pass (imf25.hostedemail.com: domain of kent.overstreet@linux.dev designates 95.215.58.189 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724526547; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5EwinnhQJWLN2dpAsOdfTffV0dyCCM/ESmRUthjs8fA=; b=VNwpExF5Gm843UuvU3aNhaT6qIGIvnlAdNDeyVsCX0wsue6A9UafVd6LdR9FFXzYR5ie/+ ZV10FN5CF+RxQZs52KMvxrCPaeEkQAgNoHLrYrfEyG5+CWObUidXUEz7k59LlN7WOAVo/S IJ/+pOwzbOl00JWMmg8tWFnXiN8tuTY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724526547; a=rsa-sha256; cv=none; b=kxm0OzddizalevOaQUO4fvb7fMINg+HTkO8gH4E779I+qnZTzimHxIBwACSjeAkiBmT2z/ HD7Fu0brnBjsNXf75HJvsQQ9QGw6kNLsQht9OBtg4mr7yiyLgJhS8X49MXqpQfCbpmjkCs sS1msobyHEdHXKRpezv6oO1BvahGl6Y= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=hd8pUDRY; spf=pass (imf25.hostedemail.com: domain of kent.overstreet@linux.dev designates 95.215.58.189 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1724526629; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5EwinnhQJWLN2dpAsOdfTffV0dyCCM/ESmRUthjs8fA=; b=hd8pUDRYBSAWcHds+IcAqN78oUmonPqT+4y7lH2sUVv5krRH0pvFcgoJ4Djj25e25KUd1G mxD5LSlRWMkLPaxOyPd8+cN6zRNf1PXH4B+L+55uJpKMskDO1PgVM+FG9mF/fgdf8Oeigt hXDSXbFiS2guQ9CO5iKc2r52cY7syxo= From: Kent Overstreet To: david@fromorbit.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Cc: Kent Overstreet , Andrew Morton , Qi Zheng , Roman Gushchin Subject: [PATCH 02/10] mm: shrinker: Add a .to_text() method for shrinkers Date: Sat, 24 Aug 2024 15:10:09 -0400 Message-ID: <20240824191020.3170516-3-kent.overstreet@linux.dev> In-Reply-To: <20240824191020.3170516-1-kent.overstreet@linux.dev> References: <20240824191020.3170516-1-kent.overstreet@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: j6g95tpr8a8mibatybcm6c91kjn67kti X-Rspamd-Queue-Id: BBA14A0012 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724526630-376755 X-HE-Meta: U2FsdGVkX18IF+Yl1kH3U9P+Z5g5iMW9ZbbKCgNJV2gVARlaZwXeOpm7wZ2J8u8zIW4/sR/4/VzdveWytAIDaAQshp3I7Kck+FEdztwIabi4b2XPp5tozBQGoKjUviZWis4nTtHN9rlEYKKZb/Gbc19QwiHXgB+T1tO92yRi0FTzNCdSRzw2di5cPhqIHyPVT6fLaqFGugOCNxHtz9FdrhaGRMxOuw8+V4Y61UqQ9okv819D4YSETzWuXpLmdHJp+dVeNskJJeNwat3HXFbPp8e9L2+zFQzoWIT55mQBekym9TOX3Hs+FBNhTHL2zoemywywxn13cNT7hwwaEat0YedALoV74OGrk/SQnl1PtZNz9qEobXaWJp3KhhMnm02gH0kBZyKoLL8C5jafUTRI4DBq0Wf6zHfqmDzOM6vcO5LUsnrt9dJl9Dfx43+mwpr8b370euhdtwl0qXKTbRNj3pzhkqKaP5qL59QfehDH4p5q2miWDxRr8FYOREM9r8m/G4amEHHT68TWGN90uCkJAsO8jW2yQ21EwL6SnvkMl68FbGHFKT+/NTROy/b1URatKUTZEL77uSz3Mfr73Cg/Z+CUXpUy/GMJH51AT5DI4usf0QHLyiuPZ5Kgx9pKJ0SgCHO0qYFHwMsDgxwAUNq+I2DFescSmkhxdkTbJhY/VQXooObDkBs6YYde6I7kkS7PGSXYyCKfrRy6+gR4hJfv9aArA7LGvPdg32hsXpdQ5x7k5m3z7BL7USUDjXUUEi1TtRRjtl7jkBLcvywh+3npvYq6RQ+a7UZ3KovucYsXmCZYbAdX+nugDXVnAqEOfu6dHmxc4WkXnWmpp5YrCcCpMcoxUYa+QJ0/wd6tXoqg6I/D7dkL+zxKKc4qLfZAerFxylnIAG6B6sL9T+dOkb3Vej+6nZiSs5ld+224239KlaNXmkzcS3oLBP5Nu8Tk8M7oo0psycHY91BDcG9lxPU xgGdBlQW coV6iZ+qWWsV8Cqnw4yHaExEsJUdhyAU+yAdszPoq0lM7ddmohCqulMpU1EHsynP+fY8RzFWugsPRvesnk/BY4DtfLFqhnhvFvH32RlKhmgopjcq8x7FKMbCp7j5TbeLuLl5j5kUP6GBqYCp9QjAglRQ9RPDpLYxVNhQQGGYXLAI2CpXxSWKOuWbQ4MBXcuUTeBiRbBMg8z9BZszh2wi2A209cn9xklpc9mbOSq4APyPTKCMBoz+t9c9CNHZeWyZV9EKHnnoZCp4+4x8V6ni0x1/hiBdt+cKnzhpW2dtOm7o7t5YEBh5ggOaLYyAgBrIiud1t1nnopEBIs3YkAIyFoM4+dg== 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: 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 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. Cc: Andrew Morton Cc: Qi Zheng Cc: Roman Gushchin Cc: linux-mm@kvack.org Signed-off-by: Kent Overstreet --- include/linux/shrinker.h | 7 +++- mm/shrinker.c | 73 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 1a00be90d93a..6193612617a1 100644 --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -24,6 +24,8 @@ struct shrinker_info { struct shrinker_info_unit *unit[]; }; +struct seq_buf; + /* * This struct is used to pass information from page reclaim to the shrinkers. * We consolidate the values for easier extension later. @@ -80,10 +82,12 @@ struct shrink_control { * @flags determine the shrinker abilities, like numa awareness */ struct shrinker { + const char *name; unsigned long (*count_objects)(struct shrinker *, struct shrink_control *sc); unsigned long (*scan_objects)(struct shrinker *, struct shrink_control *sc); + void (*to_text)(struct seq_buf *, struct shrinker *); long batch; /* reclaim batch size, 0 = default */ int seeks; /* seeks to recreate an obj */ @@ -110,7 +114,6 @@ struct shrinker { #endif #ifdef CONFIG_SHRINKER_DEBUG int debugfs_id; - const char *name; struct dentry *debugfs_entry; #endif /* objs pending delete, per node */ @@ -135,6 +138,8 @@ __printf(2, 3) struct shrinker *shrinker_alloc(unsigned int flags, const char *fmt, ...); void shrinker_register(struct shrinker *shrinker); void shrinker_free(struct shrinker *shrinker); +void shrinker_to_text(struct seq_buf *, struct shrinker *); +void shrinkers_to_text(struct seq_buf *); static inline bool shrinker_try_get(struct shrinker *shrinker) { diff --git a/mm/shrinker.c b/mm/shrinker.c index dc5d2a6fcfc4..ad52c269bb48 100644 --- a/mm/shrinker.c +++ b/mm/shrinker.c @@ -1,8 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include +#include #include -#include #include #include "internal.h" @@ -807,3 +808,73 @@ void shrinker_free(struct shrinker *shrinker) call_rcu(&shrinker->rcu, shrinker_free_rcu_cb); } EXPORT_SYMBOL_GPL(shrinker_free); + +void shrinker_to_text(struct seq_buf *out, struct shrinker *shrinker) +{ + struct shrink_control sc = { .gfp_mask = GFP_KERNEL, }; + + seq_buf_puts(out, shrinker->name); + seq_buf_printf(out, " objects: %lu\n", shrinker->count_objects(shrinker, &sc)); + + if (shrinker->to_text) { + shrinker->to_text(out, shrinker); + seq_buf_puts(out, "\n"); + } +} + +/** + * 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 seq_buf *out) +{ + struct shrinker *shrinker; + struct shrinker_by_mem { + struct shrinker *shrinker; + unsigned long mem; + } shrinkers_by_mem[10]; + int i, nr = 0; + + if (!mutex_trylock(&shrinker_mutex)) { + seq_buf_puts(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); + + mutex_unlock(&shrinker_mutex); +}