From patchwork Sat Aug 24 18:04:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 13776467 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 E5B8BC5472D for ; Sat, 24 Aug 2024 18:05:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 62FA1800EA; Sat, 24 Aug 2024 14:05:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5B624800D4; Sat, 24 Aug 2024 14:05:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43059800EA; Sat, 24 Aug 2024 14:05:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1CB8D800D4 for ; Sat, 24 Aug 2024 14:05:09 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8BB69A0835 for ; Sat, 24 Aug 2024 18:05:08 +0000 (UTC) X-FDA: 82487915496.09.5D6D8A4 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) by imf30.hostedemail.com (Postfix) with ESMTP id 7D3D280022 for ; Sat, 24 Aug 2024 18:05:06 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PA8TAr8Z; spf=pass (imf30.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.171 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=1724522665; 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=jV0AUIOvryMwumgPkQ+KkEvDI+7tTzSE9znPvSA/y64l6DD5Hg4hCRCg/ITpQ0VJ9J53a4 CE8mHFPXrNbWGD5msTey8fltiZhXGWsjAi4L32YZ7U/EP4GYsJntx9Gl8MPAKkWyYb/a6s miz7XpYy0aD0zjgR0sDE0sP9NNleWSs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=PA8TAr8Z; spf=pass (imf30.hostedemail.com: domain of kent.overstreet@linux.dev designates 91.218.175.171 as permitted sender) smtp.mailfrom=kent.overstreet@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724522665; a=rsa-sha256; cv=none; b=KFoWlx4gG9PFVrRPPNsuPlea1M2jPVnqDnnv2hhWU0c9Q3rx7HIr670b3HInK5vuv4WCVB DpRvqWLqrJEvOswpTvXya6quHbOr6JpDi8oyY4g9t7f9kxepysCs3w8cmpAam+0nc+38Qt YP48ZNTTGOgEPOeaPIGYHy+UHQc31TM= 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=1724522704; 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=PA8TAr8ZBwgEDgS3dUduPbEF9QAcFTyYQrLdS+F6lb/qJjT5BGllUtY7YTKcmWlNrK0BCN BtjA9L1dgvKCxHgm2n8T3qLkBM6Hs85YLVJ0tbuZaS3AVU1RMNAH8NASKH8/c/EEhzVlJc /rtRv8/XbP6iq2PnOXKIUfq5AQ7p7YY= From: Kent Overstreet To: david@fromorbit.com, linux-fsdevel@vger.kernel.org, linux-mm@vger.kernel.org Cc: Kent Overstreet , Andrew Morton , Qi Zheng , Roman Gushchin , linux-mm@kvack.org Subject: [PATCH 02/10] mm: shrinker: Add a .to_text() method for shrinkers Date: Sat, 24 Aug 2024 14:04:44 -0400 Message-ID: <20240824180454.3160385-3-kent.overstreet@linux.dev> In-Reply-To: <20240824180454.3160385-1-kent.overstreet@linux.dev> References: <20240824180454.3160385-1-kent.overstreet@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 7D3D280022 X-Stat-Signature: q4wmoth8ffb55anwo3emro45kkeamai3 X-HE-Tag: 1724522706-148906 X-HE-Meta: U2FsdGVkX19TPzOZasunv4VhDEn0aVUrrma+kUJZ+hLV0T0ChR7xog4S0/LPM6FHmAMsMMw4s8BR2EGxOwDBoWwhDFFWF/N9K71b0VE9Ck4fFZ3Fg94yngp8gPPVNDnmMwFFaEbXx60eiqDkMcPLAUagojtz+bfVF+mfJaVEix+RN+swVv0gSG7jn7+Pex2a3O+jXEbYjxJWfSymKYzMh938ACceHd3p6Xfdte/5AMOqNRbJl6mw4aAPlALcMaGxoGBzHQzkofhnTkil1JLAHFhcni6an8P+ejRk4hajJjAoLba5Z72fTkScgcq0UszRfD/hHqnyvDfDJxmzZTZZa5jm+bLFw9hKsKNsXOxHoDDmwOabqCBJ7u8yYKI0aEiGPQxKkfv0iDJT7vfd1FmzccEApEkl2kNclFmlFUJbXgateeJ2AlbIrwOD0Y5VTUqmzfTd4VQ5KGu8MVRTU0pWatqLn6XlO1XgS8cGIUXY6yg1i1XjgMLu+OAlF75HcH2vNVMLB83euNyWoBEYUA3vQO7nTAw1qNEjF9QfypIxsVSAi/6/2fEtaf7FbLJul703LzFb/312il19armTpuTChFfQK9rGArvALxidtINN9FgeDKxMlIVybI8hkx7Ig5DR4OC+p25v3KNjrPIFzHmqkvDxd1HB2GED0DvMMuLpt93o2kb9OdyF0J0KNUdFb5dU8p6zQi8SFNlkpnfhfx/Gd42yjfvyRnVMxRDfXhiaduk2+B+habnK+bs2lE0jBZUdkSUqpaynhfMwCgGEYIaKpmDC/8DE+cYwGMAuWQxUu1cC19JFNMINonAbxXkSo5zW9AVAzJ0AjueStthMhHL0Bx0NBkINBZGeaauAihBh4s0Z0WZ1EFQBmrt1qv6lLOnAZkueIbcoqf3CFe+zWafHmL7+86ewOog0VPg3JiBrbL1I/HgyzKlW+6130eNvVL2dygpsT/TKfAqSQXb8DoW l2JDDdqw vYOvcE399JldxU8zbuqub4LeidsSPRB959v13svnMxhwslXb8cEVyezeOE579v9ODUpF+OxdL7ZVvCDnWfYkc5HaT9pitM2hry3rngfOpK/Ax/pzkSkzCU8LuQxQnkE08Z2yT0yIlTbryTL8z8rq5xYzixnfd5/+sT++piv5UTkehsNyP4zNE4Cx289mZbMYeQSmxCQ4YeKVU3xOBzK2VtEgBdf4wx9siLWrQdIBx7ozyptCS8Uac2O+KBX5K23rEaPGCE8cleAEBSBi6YFwv4hCm0TLkqLEwgs4pBluRUZCSXx4tZ0posn3tsRZhh1loSGTewhh1XaJHtyQ= 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); +}