Received: by 2002:a05:7412:e79e:b0:f3:1519:9f41 with SMTP id o30csp259178rdd; Wed, 22 Nov 2023 15:25:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkTURh3BGpFa4Qq0VA7EFSldyJeM51Fg5tUStW3JOA+fhqvIxGv4n21yqTNB4+EtY+nyXQ X-Received: by 2002:a05:6a20:d90b:b0:18a:a210:cbbd with SMTP id jd11-20020a056a20d90b00b0018aa210cbbdmr4702903pzb.34.1700695541921; Wed, 22 Nov 2023 15:25:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700695541; cv=none; d=google.com; s=arc-20160816; b=itsAjnewYhhuIFO+cvA+9LxzXysbwj5i7U+WMqeodtzRTnYuqI+nFCY/oIw8PQi1e6 36ShmpL7lr9JiU/8KfU91AecwobEJieT/aYd5paKTIdYQORtI6DrJgsRlpyOGCmbTsrW gyKx1QBQEeHrZdh1UW2HP/iskPvdi2bFM+KZN3ZkgNAvRNDP2TYkYO5TtY5nX1c9C9ai CBACsxm30KJe46tIovspNyGOJ4KdcoZbjrtwwPuxbsuahyA8xFRcF989GtbyUt6YwH4x iBnRRzxutD0rgsWXCyvtlydYLv2y1lIaeubBFoXjPzgn/b8E5/WTSWSNuSafv/IQa1Rh MpeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ce6mQdeWYA7JZiZSelXeyRte1bT790UpclDUySrWbbI=; fh=mDiKJtb6HwOojzT/Tj0KHsHXTEWMd8hEup+kQMFjAPQ=; b=kuukQaPFVYmZEv0TbzXyRbuZEsNckTSKYBuFcLf4/BQ2b5yUtBW9xPV9oagOWKqg6r BWIE+sXufLzZt9N1Gnisrh7c4tRqYcd1tcvnx7m66lQLDCRA0rIObVv8zao4osg6UaBp HCtWxpl57YJKBUM1olvBxRPWo+d1UEvghbatsIOJ8Vi7tFEVFZFEwU8CI3ClVCtAlswh f2E6M9KPs0I+msL0tuFCj6DXl/8gREmtPzOVQdUHceqVF2K+QYWf8vacE/n7Ml5g12XE B8t3A2q5tRMhUwRTfgBlUqw++rtYa9GxqMxMuHerLa9SY/a8PjGo4HMpvibVbVDpoS2U AxLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=RwDGCsBx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id y15-20020a056a00190f00b006cbe3c123a6si68085pfi.9.2023.11.22.15.25.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 15:25:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=RwDGCsBx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 955D8827FFA6; Wed, 22 Nov 2023 15:25:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344548AbjKVXZf (ORCPT + 99 others); Wed, 22 Nov 2023 18:25:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbjKVXZc (ORCPT ); Wed, 22 Nov 2023 18:25:32 -0500 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [IPv6:2001:41d0:1004:224b::b2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6A67199 for ; Wed, 22 Nov 2023 15:25:28 -0800 (PST) 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=1700695527; 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=ce6mQdeWYA7JZiZSelXeyRte1bT790UpclDUySrWbbI=; b=RwDGCsBxUOAeC14xj7ENLjAvR8IPMtphog88OnO0JhzfsIcs+mraOc5OHgWapTIGcLSRsp Eu6N2oJ5sNyByGKgzowShHq/oDCtx/SHNWZszz3Jq5vB9+XfTDyzs5kpuj8NlrxAXhVCaG bk8karUNujFdsVJa4kR7ZHnGQ3NZheM= From: Kent Overstreet To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Kent Overstreet , Andrew Morton , Qi Zheng , Roman Gushchin Subject: [PATCH 2/7] mm: shrinker: Add a .to_text() method for shrinkers Date: Wed, 22 Nov 2023 18:25:07 -0500 Message-ID: <20231122232515.177833-3-kent.overstreet@linux.dev> In-Reply-To: <20231122232515.177833-1-kent.overstreet@linux.dev> References: <20231122232515.177833-1-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 22 Nov 2023 15:25:40 -0800 (PST) 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 | 6 +++- mm/shrinker.c | 73 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/include/linux/shrinker.h b/include/linux/shrinker.h index 1a00be90d93a..968c55474e78 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,7 @@ __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 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 dd91eab43ed3..4976dbac4c83 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); +} -- 2.42.0