From nobody Fri Nov 29 03:39:44 2024 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4886149013 for ; Wed, 25 Sep 2024 03:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727234616; cv=none; b=jbeKB9Iob+FSYT2WmIVJM1yU7eCYujG5JkxqwQpZc64a1wdHwgIgYh1C21W2L0c9LqmtdYeDNyPjhxUBpdxpis3NB7nVPzTtfc/I/v0L1KnT1W3+lD70vK4oJuTbDt9dZ19Jzngc3JBL5KTwG0N7fwoZl0zKBAO+DpvXoPwUTSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727234616; c=relaxed/simple; bh=iBC4PsIF39LsPCctoHAWgZuRwBa7EwtIWQ0wmdIAhAU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JCWk2NSgtMiHMZyhg90UTniNfMhLZ7EK49ehDoNipZ+FV4wprEkPki8mKplZqUHVzE/8p+ga8v8oR8AugiWGsl8Wi1jqNlOujuBPY6MBk1XqsRwWgTg4nESwlOvGlj0SROk57DfKnr0e1GsQqWbEpnh/8dzsg9vLNzoylpp7Qo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 48P3N88G053972; Wed, 25 Sep 2024 11:23:08 +0800 (+08) (envelope-from fangzheng.zhang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4XD20r40LZz2Mx63v; Wed, 25 Sep 2024 11:15:24 +0800 (CST) Received: from bj10906pcu1.spreadtrum.com (10.0.73.72) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Wed, 25 Sep 2024 11:23:05 +0800 From: Fangzheng Zhang To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Greg KH CC: , , , Fangzheng Zhang , Fangzheng Zhang , Yuming Han Subject: [PATCH 1/2] mm/slub: Add panic function when slub leaks Date: Wed, 25 Sep 2024 11:22:55 +0800 Message-ID: <20240925032256.1782-2-fangzheng.zhang@unisoc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240925032256.1782-1-fangzheng.zhang@unisoc.com> References: <20240925032256.1782-1-fangzheng.zhang@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 48P3N88G053972 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Perform real-time memory usage monitoring on the slub page allocation paths, ie, kmalloc_large_alloced and alloc_slab_page. When the usage exceeds the set threshole value, the panic function will be triggered. Signed-off-by: Fangzheng Zhang --- mm/Kconfig | 11 ++++++++ mm/slub.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/mm/Kconfig b/mm/Kconfig index 09aebca1cae3..60cf72d4f0da 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -255,6 +255,17 @@ config SLUB_TINY =20 If unsure, say N. =20 +config SLUB_LEAK_PANIC + bool "Trigger panic when slub leaks" + default y + help + Detect slub leaks by monitoring its usage in real time on the page + allocation path of the slub. When the slub occupancy exceeds the + user-set value, it is considered that the slub is leaking at this + time, and a panic operation will be triggered immediately. Uers + can enable and set leak threshold by using the kernel command line + parameters "slub.leak_panic" and "slub.leak_panic_threshold". + config SLAB_MERGE_DEFAULT bool "Allow slab caches to be merged" default y diff --git a/mm/slub.c b/mm/slub.c index 21f71cb6cc06..91049f87ab98 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -42,6 +42,9 @@ #include #include #include +#ifdef CONFIG_SLUB_LEAK_PANIC +#include +#endif =20 #include #include @@ -218,6 +221,15 @@ DEFINE_STATIC_KEY_FALSE(slub_debug_enabled); #endif #endif /* CONFIG_SLUB_DEBUG */ =20 +/* Internal slub_leak_panic definitions */ +#ifdef CONFIG_SLUB_LEAK_PANIC +#define K(x) ((x) << (PAGE_SHIFT-10)) +static bool __read_mostly slub_leak_panic_enabled; +static unsigned int __read_mostly slub_leak_panic_threshold; +static long max_slab_count, temp_slab_count; +#endif + + /* Structure holding parameters for get_partial() call chain */ struct partial_context { gfp_t flags; @@ -2424,6 +2436,21 @@ static inline struct slab *alloc_slab_page(gfp_t fla= gs, int node, if (folio_is_pfmemalloc(folio)) slab_set_pfmemalloc(slab); =20 +#ifdef CONFIG_SLUB_LEAK_PANIC + if (likely(slub_leak_panic_enabled) && slub_leak_panic_threshold > 0) { + max_slab_count =3D K(totalram_pages()) * slub_leak_panic_threshold / 100; + temp_slab_count =3D K(global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B= )) + + K(global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B)) + + K(1 << order); + if (temp_slab_count > max_slab_count) + panic("SLAB LEAK: %s(temp_count %6luKB > max_count %6luKB):\n" + "%s gfp_mask=3D%#x(%pGg), order=3D%d kB, oom_score_adj=3D%d\n", + __func__, temp_slab_count, max_slab_count, + current->comm, flags, &flags, order, + current->signal->oom_score_adj); + } +#endif + return slab; } =20 @@ -4212,6 +4239,19 @@ static void *___kmalloc_large_node(size_t size, gfp_= t flags, int node) ptr =3D folio_address(folio); lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, PAGE_SIZE << order); +#ifdef CONFIG_SLUB_LEAK_PANIC + if (likely(slub_leak_panic_enabled) && slub_leak_panic_threshold > 0) { + max_slab_count =3D K(totalram_pages()) * slub_leak_panic_threshold / 10= 0; + temp_slab_count =3D K(global_node_page_state_pages(NR_SLAB_RECLAIMABLE_= B)) + + K(global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B)); + if (temp_slab_count > max_slab_count) + panic("SLAB LEAK: %s(temp_count %6luKB > max_count %6luKB):\n" + "%s gfp_mask=3D%#x(%pGg), order=3D%d kB, oom_score_adj=3D%d\n", + __func__, temp_slab_count, max_slab_count, + current->comm, flags, &flags, order, + current->signal->oom_score_adj); + } +#endif } =20 ptr =3D kasan_kmalloc_large(ptr, size, flags); @@ -7443,3 +7483,39 @@ void get_slabinfo(struct kmem_cache *s, struct slabi= nfo *sinfo) sinfo->cache_order =3D oo_order(s->oo); } #endif /* CONFIG_SLUB_DEBUG */ + +/* + * The /sys/module/slub ABI + */ +#ifdef CONFIG_SLUB_LEAK_PANIC +/* + * What: /sys/module/slub/parameters/leak_panic + * /sys/module/slub/parameters/leak_panic_threshold + * Date: Sep 2024 + * KernelVersion: v6.6+ + * Description: Used for slub memory leak check. When the user + * successfully allocates the slub page, it also performs + * statistics on the total slub usage in the system. + * When the usage exceeds the set value + * (threshold * memtotal / 100), it is considered that + * there is a risk of slub leakage in the system at this ti= me. + * A panic operation will be triggered. + * Users: userspace + */ +MODULE_PARM_DESC(leak_panic, "Disable/Enable slub_leak_panic"); +module_param_named(leak_panic, slub_leak_panic_enabled, bool, 0644); + +static int slub_leak_panic_threshold_set(const char *val, const struct ker= nel_param *kp) +{ + return param_set_uint_minmax(val, kp, 0, 100); +} + +static const struct kernel_param_ops slub_leak_panic_threshold_ops =3D { + .set =3D slub_leak_panic_threshold_set, + .get =3D param_get_uint, +}; + +MODULE_PARM_DESC(leak_panic_threshold, + "Upper limit value of slub, expressed as a percentage of memtotal (0 ~ 1= 00)"); +module_param_cb(leak_panic_threshold, + &slub_leak_panic_threshold_ops, &slub_leak_panic_threshold, 0644); +#endif /* CONFIG_SLUB_LEAK_PANIC */ --=20 2.17.1 From nobody Fri Nov 29 03:39:44 2024 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AC5DD14A4E2 for ; Wed, 25 Sep 2024 03:23:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727234624; cv=none; b=ipHn+QS7chw7ANByrf04glnJnD/oedb5xHXOxrZ8T7Ui/gqg1vVNbrlC7ScNvWykpGtmUQ9pUHNreIOGB5VKvQR705IGBCUSC6XA9oTKI8egwpRHoX7qTI7uzNPq/6TSnEd9SWJzk83r8xnEwlej40Dzr95C5kmP0dw/rMktnIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727234624; c=relaxed/simple; bh=ujYzwysx8BVaV/txjzevN6TgeIeQEwkBY9v+gQRY6tk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kR3nEATkqSyz9Y07zmzF+WEezgjM/kWbDaYUpgdevj6jdV1Q8EONBFLVznqmm5p3RQ33o3GJOKG43z+7401Y5ZbMjN0oyB3U++Wc8c/np1HakpnDACez4MyKjZKCqBVRRvgDJOjeIW2z/bEVZpx0AT9Dk/MRqp95qSFUROe6Vh4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 48P3NE3x054301; Wed, 25 Sep 2024 11:23:14 +0800 (+08) (envelope-from fangzheng.zhang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4XD20y3PGQz2Mx63v; Wed, 25 Sep 2024 11:15:30 +0800 (CST) Received: from bj10906pcu1.spreadtrum.com (10.0.73.72) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Wed, 25 Sep 2024 11:23:11 +0800 From: Fangzheng Zhang To: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Greg KH CC: , , , Fangzheng Zhang , Fangzheng Zhang , Yuming Han Subject: [PATCH 2/2] Documentation: admin-guide: kernel-parameters: Add parameter description for slub_leak_panic function Date: Wed, 25 Sep 2024 11:22:56 +0800 Message-ID: <20240925032256.1782-3-fangzheng.zhang@unisoc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240925032256.1782-1-fangzheng.zhang@unisoc.com> References: <20240925032256.1782-1-fangzheng.zhang@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 48P3NE3x054301 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce the details of the slub_leak_panic parameters in kernel-parameters.txt, so that users can understand how to use the parameters more quickly. Signed-off-by: Fangzheng Zhang --- Documentation/admin-guide/kernel-parameters.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index bb48ae24ae69..8a672766f05f 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6130,6 +6130,21 @@ For more information see Documentation/mm/slub.rst. (slub_nomerge legacy name also accepted for now) =20 + slub.leak_panic=3D [MM, SLUB] + Enabling slub.leak_panic can monitor the slub usage + in real time on the slub allocation path. When the + slub occupancy is abnormal, the alarm or panic will + be triggered. + Default: N + + slub.leak_panic_threshold=3D [MM, SLUB] + Upper limit value of the slub usage, expressed as + a percentage of memtotal. When the usage exceeds + the set value (threshold * memtotal / 100), the + slub_leak_panic function will be trigger. The + value setting range is [0, 100]. + Default: 0 + slram=3D [HW,MTD] =20 smart2=3D [HW] --=20 2.17.1