From patchwork Sat Aug 24 19:10:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Overstreet X-Patchwork-Id: 13776495 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 0D275C52D6F for ; Sat, 24 Aug 2024 19:10:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48CE06B04B3; Sat, 24 Aug 2024 15:10:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C67B800EF; Sat, 24 Aug 2024 15:10:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CDBA800EE; Sat, 24 Aug 2024 15:10:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D4C526B04B3 for ; Sat, 24 Aug 2024 15:10:35 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 81C83160878 for ; Sat, 24 Aug 2024 19:10:35 +0000 (UTC) X-FDA: 82488080430.05.B910603 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) by imf12.hostedemail.com (Postfix) with ESMTP id C7C5540008 for ; Sat, 24 Aug 2024 19:10:33 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oj3Zemxp; spf=pass (imf12.hostedemail.com: domain of kent.overstreet@linux.dev designates 95.215.58.179 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=1724526550; 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=mkkf9bPKkUQD5+0NEMylYPBF2rZLY+sZgAz6x5jg4XE=; b=IyPzdiJodtf2TR9VCeguZTAxLQW9MQ2ZTk1BCCYuKQ/xlWWS1OboGnqL0lW1wDBJQ6lnLP xT4bWBjtR/Af/d9Ucl/w8zu+Mk9WnOPtMMeb7Tevxxvt617EdmXBeNtG/IpObhxwdI9FWm 1uHO8DJm5O/tjFRUnxqNiOi81ghmRko= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724526550; a=rsa-sha256; cv=none; b=CZPn6zX+9rGO8cwV+rWJXwQl61T3CBQggYUq/sobDMRt105KpuwY7WzHkKCdJtwvxvuIK3 /nfiywLunv7OLJ4wsUG8u3jSd0y/8lSJQZqiFJUuJn6WbVGq0dY6vQig41jkJc4fge06Yk z6vEjAW/OC/rQVWiB6MYa0Qci6PHvYc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=oj3Zemxp; spf=pass (imf12.hostedemail.com: domain of kent.overstreet@linux.dev designates 95.215.58.179 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=1724526632; 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=mkkf9bPKkUQD5+0NEMylYPBF2rZLY+sZgAz6x5jg4XE=; b=oj3ZemxpRFF/l0tgdqkjXGsIZmxnLsax/vpXKoEBc9jfBGNz3yZZ3Nxh/PlMV4LATg8u7+ 2W3qx6ciQR2V72/gSnSAeWcIwxRBjyilwjFtV3JLdGRjCTwFn/6pXDQY1RV2/jVR/fZyAV au6I+9Sgweb5qJhtlNdRg/6IvbHBus4= 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 04/10] mm: Centralize & improve oom reporting in show_mem.c Date: Sat, 24 Aug 2024 15:10:11 -0400 Message-ID: <20240824191020.3170516-5-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C7C5540008 X-Stat-Signature: f6m3fh87q7au6tkcbkfuraggeqzpkaaj X-HE-Tag: 1724526633-689386 X-HE-Meta: U2FsdGVkX19NnBlMyPjZgXa1uVfNg1rSGuAfghi0YB1SyPPkKT42AiVaHtSRflaFwvDro6KgfNzj+r8RDYGPe9mubBfmPKKGYxllyotBekv0yb+i623B9dUa/C7t3798e46BYAkD1TUvhdia69uXnasoeOI9HqMJLVDA14fbYbBrTWTfe8FrbYl7J6AnSRkRQJwXgCL+D0jVB4zQCJeD8F7Um5Bt+lhq5NrIlnqpWW0FHYd+mIxb1qeIOhwJ7c2/4mN5Avia+sCDM6t6tIZ2/l7rUqZrYwHb2+pfTN53XhQhyiJi617wsWw+oKpSxx/PGegTXZeCe94e1/PCOSHMv0oqpDMN1IKA08xoPflJWEAkgSzxXtcLUUVH3b3Sblv02NEQpsk6KGkeFLrILflr45eM1gmvmdeWNylqp1e51HPiV5gKBs/Vm9E/T1rRp2zrwocpqwwQnPQOiMhY7r0nDNgRFaDq558sM9nqduGE8GvTrwZZFsOMQXkKfqp5x5tIHFWTj2hNoiONNuTiF/fL4b2KvJw/QtktWEV5aYoZfeOlFwp6xy9i6Kohvqh7zMIEJ1btrpE1Z6hpronuFYHEXW71oESS7wb//oXw4GKDtpdlOy6nv/rfLnzmWleiVTrG6hEmXR7WQsMzB6fLTPWLGns441ukuAio8kt8R5oznsFdfhI8XL4e0XqDvL3+YOYQFiuRQWNmY0sfbx0sWrtlVxKTWHgb52nCp5ZmxlTnf5GjlaoH9f6FymIHxH93+pi8Y4q6FFjgwxiksYuwaVzzqKm/S0rp2VeHUjZg9L3RhVDIlaotjxpGDjA00QxAIyfgncNQrZYiIf7nFWraZbIEJanwI2IpDV0csTBBH5TwA7857e81IsLzy4po5S7t+oXVNSJMH8kuki6W6G/HBB0h3aQVZKNKfjJGIJdHD8IuL/UdjDiKDQPhdusfaBFD4lp6sbmF947RtKlzDJwF7c5 K3hzAylN yxrAWVlRxKhnsIBXqNiDPje5I94BG4xX1WC+d4G9yPghdK7rCqPjXnnPLJK+PeXveOJXTnwWyUswcQYY7buCo1xK9G/XbQtNir8nyB+X9sUp40toj9k95h/aF6cyZBFzwztdlXeP4Xf0Wi0EwwBHKLfxL83PoChQiGO26z/cqMiZcMJAZJ6qcHEe4zijjFYnV5u0yF4Uw1TatTLNOZRGtvHsuPV+mC0tpHlKA7lXuN3jFQ2Ln8dPuDLGNyWg0db/nlRJgRpJ0fUhO7UJbVwvewkWI+uSS6KjiiH8oK6iIgggBW7QuZlE4nEjDiuMTHg6UD6AHlCOcicEZkRY= 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 patch: - Changes show_mem() to always report on slab usage - Instead of reporting on all slabs, we only report on top 10 slabs, and in sorted order - Also reports on shrinkers, with the new shrinkers_to_text(). Shrinkers need to be included in OOM/allocation failure reporting because they're responsible for memory reclaim - if a shrinker isn't giving up its memory, we need to know which one and why. More OOM reporting can be moved to show_mem.c and improved, this patch is only a start. New example output on OOM/memory allocation failure: 00177 Mem-Info: 00177 active_anon:13706 inactive_anon:32266 isolated_anon:16 00177 active_file:1653 inactive_file:1822 isolated_file:0 00177 unevictable:0 dirty:0 writeback:0 00177 slab_reclaimable:6242 slab_unreclaimable:11168 00177 mapped:3824 shmem:3 pagetables:1266 bounce:0 00177 kernel_misc_reclaimable:0 00177 free:4362 free_pcp:35 free_cma:0 00177 Node 0 active_anon:54824kB inactive_anon:129064kB active_file:6612kB inactive_file:7288kB unevictable:0kB isolated(anon):64kB isolated(file):0kB mapped:15296kB dirty:0kB writeback:0kB shmem:12kB writeback_tmp:0kB kernel_stack:3392kB pagetables:5064kB all_unreclaimable? no 00177 DMA free:2232kB boost:0kB min:88kB low:108kB high:128kB reserved_highatomic:0KB active_anon:2924kB inactive_anon:6596kB active_file:428kB inactive_file:384kB unevictable:0kB writepending:0kB present:15992kB managed:15360kB mlocked:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB 00177 lowmem_reserve[]: 0 426 426 426 00177 DMA32 free:15092kB boost:5836kB min:8432kB low:9080kB high:9728kB reserved_highatomic:0KB active_anon:52196kB inactive_anon:122392kB active_file:6176kB inactive_file:7068kB unevictable:0kB writepending:0kB present:507760kB managed:441816kB mlocked:0kB bounce:0kB free_pcp:72kB local_pcp:0kB free_cma:0kB 00177 lowmem_reserve[]: 0 0 0 0 00177 DMA: 284*4kB (UM) 53*8kB (UM) 21*16kB (U) 11*32kB (U) 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 2248kB 00177 DMA32: 2765*4kB (UME) 375*8kB (UME) 57*16kB (UM) 5*32kB (U) 0*64kB 0*128kB 0*256kB 0*512kB 0*1024kB 0*2048kB 0*4096kB = 15132kB 00177 4656 total pagecache pages 00177 1031 pages in swap cache 00177 Swap cache stats: add 6572399, delete 6572173, find 488603/3286476 00177 Free swap = 509112kB 00177 Total swap = 2097148kB 00177 130938 pages RAM 00177 0 pages HighMem/MovableOnly 00177 16644 pages reserved 00177 Unreclaimable slab info: 00177 9p-fcall-cache total: 8.25 MiB active: 8.25 MiB 00177 kernfs_node_cache total: 2.15 MiB active: 2.15 MiB 00177 kmalloc-64 total: 2.08 MiB active: 2.07 MiB 00177 task_struct total: 1.95 MiB active: 1.95 MiB 00177 kmalloc-4k total: 1.50 MiB active: 1.50 MiB 00177 signal_cache total: 1.34 MiB active: 1.34 MiB 00177 kmalloc-2k total: 1.16 MiB active: 1.16 MiB 00177 bch_inode_info total: 1.02 MiB active: 922 KiB 00177 perf_event total: 1.02 MiB active: 1.02 MiB 00177 biovec-max total: 992 KiB active: 960 KiB 00177 Shrinkers: 00177 super_cache_scan: objects: 127 00177 super_cache_scan: objects: 106 00177 jbd2_journal_shrink_scan: objects: 32 00177 ext4_es_scan: objects: 32 00177 bch2_btree_cache_scan: objects: 8 00177 nr nodes: 24 00177 nr dirty: 0 00177 cannibalize lock: 0000000000000000 00177 00177 super_cache_scan: objects: 8 00177 super_cache_scan: objects: 1 Cc: Andrew Morton Cc: Qi Zheng Cc: Roman Gushchin Cc: linux-mm@kvack.org Signed-off-by: Kent Overstreet --- mm/oom_kill.c | 23 --------------------- mm/show_mem.c | 43 +++++++++++++++++++++++++++++++++++++++ mm/slab.h | 6 ++++-- mm/slab_common.c | 52 +++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 90 insertions(+), 34 deletions(-) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 4d7a0004df2c..dc56239ff057 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -169,27 +169,6 @@ static bool oom_unkillable_task(struct task_struct *p) return false; } -/* - * Check whether unreclaimable slab amount is greater than - * all user memory(LRU pages). - * dump_unreclaimable_slab() could help in the case that - * oom due to too much unreclaimable slab used by kernel. -*/ -static bool should_dump_unreclaim_slab(void) -{ - unsigned long nr_lru; - - nr_lru = global_node_page_state(NR_ACTIVE_ANON) + - global_node_page_state(NR_INACTIVE_ANON) + - global_node_page_state(NR_ACTIVE_FILE) + - global_node_page_state(NR_INACTIVE_FILE) + - global_node_page_state(NR_ISOLATED_ANON) + - global_node_page_state(NR_ISOLATED_FILE) + - global_node_page_state(NR_UNEVICTABLE); - - return (global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B) > nr_lru); -} - /** * oom_badness - heuristic function to determine which candidate task to kill * @p: task struct of which task we should calculate @@ -464,8 +443,6 @@ static void dump_header(struct oom_control *oc) mem_cgroup_print_oom_meminfo(oc->memcg); else { __show_mem(SHOW_MEM_FILTER_NODES, oc->nodemask, gfp_zone(oc->gfp_mask)); - if (should_dump_unreclaim_slab()) - dump_unreclaimable_slab(); } if (sysctl_oom_dump_tasks) dump_tasks(oc); diff --git a/mm/show_mem.c b/mm/show_mem.c index bdb439551eef..a8ea4c41ced5 100644 --- a/mm/show_mem.c +++ b/mm/show_mem.c @@ -7,15 +7,18 @@ #include #include +#include #include #include #include #include #include +#include #include #include #include "internal.h" +#include "slab.h" #include "swap.h" atomic_long_t _totalram_pages __read_mostly; @@ -397,10 +400,31 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z show_swap_cache_info(); } +static void print_string_as_lines(const char *prefix, const char *lines) +{ + if (!lines) { + printk("%s (null)\n", prefix); + return; + } + + bool locked = console_trylock(); + + while (1) { + const char *p = strchrnul(lines, '\n'); + printk("%s%.*s\n", prefix, (int) (p - lines), lines); + if (!*p) + break; + lines = p + 1; + } + if (locked) + console_unlock(); +} + void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) { unsigned long total = 0, reserved = 0, highmem = 0; struct zone *zone; + char *buf; printk("Mem-Info:\n"); show_free_areas(filter, nodemask, max_zone_idx); @@ -449,4 +473,23 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) } } #endif + + const unsigned buf_size = 8192; + buf = kmalloc(buf_size, GFP_ATOMIC); + if (buf) { + struct seq_buf s; + + printk("Unreclaimable slab info:\n"); + seq_buf_init(&s, buf, buf_size); + dump_unreclaimable_slab(&s); + print_string_as_lines(KERN_NOTICE, seq_buf_str(&s)); + + printk("Shrinkers:\n"); + seq_buf_init(&s, buf, buf_size); + shrinkers_to_text(&s); + print_string_as_lines(KERN_NOTICE, seq_buf_str(&s)); + /* previous output doesn't get flushed without this - why? */ + + kfree(buf); + } } diff --git a/mm/slab.h b/mm/slab.h index dcdb56b8e7f5..b523b3e3d9d3 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -611,10 +611,12 @@ static inline size_t slab_ksize(const struct kmem_cache *s) return s->size; } +struct seq_buf; + #ifdef CONFIG_SLUB_DEBUG -void dump_unreclaimable_slab(void); +void dump_unreclaimable_slab(struct seq_buf *); #else -static inline void dump_unreclaimable_slab(void) +static inline void dump_unreclaimable_slab(struct seq_buf *out) { } #endif diff --git a/mm/slab_common.c b/mm/slab_common.c index 40b582a014b8..bd50a57161cf 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "internal.h" @@ -1181,10 +1182,15 @@ static int slab_show(struct seq_file *m, void *p) return 0; } -void dump_unreclaimable_slab(void) +void dump_unreclaimable_slab(struct seq_buf *out) { struct kmem_cache *s; struct slabinfo sinfo; + struct slab_by_mem { + struct kmem_cache *s; + size_t total, active; + } slabs_by_mem[10], n; + int i, nr = 0; /* * Here acquiring slab_mutex is risky since we don't prefer to get @@ -1194,24 +1200,52 @@ void dump_unreclaimable_slab(void) * without acquiring the mutex. */ if (!mutex_trylock(&slab_mutex)) { - pr_warn("excessive unreclaimable slab but cannot dump stats\n"); + seq_buf_puts(out, "excessive unreclaimable slab but cannot dump stats\n"); return; } - pr_info("Unreclaimable slab info:\n"); - pr_info("Name Used Total\n"); - list_for_each_entry(s, &slab_caches, list) { if (s->flags & SLAB_RECLAIM_ACCOUNT) continue; get_slabinfo(s, &sinfo); - if (sinfo.num_objs > 0) - pr_info("%-17s %10luKB %10luKB\n", s->name, - (sinfo.active_objs * s->size) / 1024, - (sinfo.num_objs * s->size) / 1024); + if (!sinfo.num_objs) + continue; + + n.s = s; + n.total = sinfo.num_objs * s->size; + n.active = sinfo.active_objs * s->size; + + for (i = 0; i < nr; i++) + if (n.total < slabs_by_mem[i].total) + break; + + if (nr < ARRAY_SIZE(slabs_by_mem)) { + memmove(&slabs_by_mem[i + 1], + &slabs_by_mem[i], + sizeof(slabs_by_mem[0]) * (nr - i)); + nr++; + } else if (i) { + i--; + memmove(&slabs_by_mem[0], + &slabs_by_mem[1], + sizeof(slabs_by_mem[0]) * i); + } else { + continue; + } + + slabs_by_mem[i] = n; } + + for (i = nr - 1; i >= 0; --i) { + seq_buf_printf(out, "%-17s total: ", slabs_by_mem[i].s->name); + seq_buf_human_readable_u64(out, slabs_by_mem[i].total, STRING_UNITS_2); + seq_buf_printf(out, " active: "); + seq_buf_human_readable_u64(out, slabs_by_mem[i].active, STRING_UNITS_2); + seq_buf_putc(out, '\n'); + } + mutex_unlock(&slab_mutex); }