From nobody Thu Feb 12 06:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC8ACC77B78 for ; Wed, 26 Apr 2023 13:39:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241056AbjDZNjb (ORCPT ); Wed, 26 Apr 2023 09:39:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240971AbjDZNj0 (ORCPT ); Wed, 26 Apr 2023 09:39:26 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 784AC199 for ; Wed, 26 Apr 2023 06:39:25 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f6dd3b329so118123827b3.0 for ; Wed, 26 Apr 2023 06:39:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682516364; x=1685108364; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NvDSnUS+MAA09hIqW3zUQhLkSsb1P4vBALA1mlThJ28=; b=aqdVpamTrjskGChIniYGsRwA8ufOxvP8FBQ/+/4SveoiRC/z/63BLZMtn021IyYVPH uF+kXYDs12M8b1BiTt6vyIpxzNIKQZwmmxGpOAGtuC+q1jVvmNH2SESK7inczKUamn3G th86Vzgr9WoCq31/yzzNvEiaP7RX6w9o/lcQDGmQZaR/qRHgyMWFZS8Z5Q6b8Y9x6QWD EXhkCqc5cbXomQgd2caoK3DuWBuZH5A+ghpQNxakhvKtZa/TVIUUxnv6WL5pDy5ZB0Ez xhplPlyQ6i/IKh0qKiWGI4Jvd/Yk/5R44c77s7+KAN34iQC7UO+g/zDrsosVqBapM/m/ DXHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682516364; x=1685108364; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NvDSnUS+MAA09hIqW3zUQhLkSsb1P4vBALA1mlThJ28=; b=LTGuhAoYrMO8dx91mo4uTwjKUO2uhSLFRzODwxZ/q589yCf09gqDN+H8D7S5WxINlH Z/iK1ukrrBbUGaAXc3b6goijqqfuz31k2rEkBgX05fpJ6d6QG9rkOhMSSqFZumOu0GGi 1JTS4KlFf3oBE8/dyhkSf0rEGpEy/Xw3OPydwxeRJ8HIWNRuwnUMEpeHr1PShzHuGSCz fgy2aP+9EQr9HMx4y/Ms1zLzDOzkRiWadurqOcXDVkbSnU5wIJN15stXaXhJMTvAKkoR zpZXu7sVBiWLflvll298Hhtg/L3gqzTyfh5r9XlV1N47qohq+KgTMDRtxW/0N2UIdTol TS2w== X-Gm-Message-State: AC+VfDxl7cX0j2yXBAvWZyFUVJ7ljazELVkksidmRkHcN5I8Bl1PM51O 2ZbBxPQ3rWUZM0bT3C2efQD7pJ5w0z5VF+GK X-Google-Smtp-Source: ACHHUZ7BrencnsXSFnxZRqIJpm8puEkhg0w+PJX56s0RsOTeawPQAUSgeFXgLPLsqpHa4JBuyBH8e8Z6laJUJDz5 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a81:441f:0:b0:556:b875:500e with SMTP id r31-20020a81441f000000b00556b875500emr4361688ywa.5.1682516364732; Wed, 26 Apr 2023 06:39:24 -0700 (PDT) Date: Wed, 26 Apr 2023 13:39:18 +0000 In-Reply-To: <20230426133919.1342942-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230426133919.1342942-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426133919.1342942-2-yosryahmed@google.com> Subject: [PATCH 1/2] memcg: use seq_buf_do_printk() with mem_cgroup_print_oom_meminfo() From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Andrew Morton Cc: Muchun Song , Sergey Senozhatsky , Steven Rostedt , Petr Mladek , Chris Li , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, we format all the memcg stats into a buffer in mem_cgroup_print_oom_meminfo() and use pr_info() to dump it to the logs. However, this buffer is large in size. Although it is currently working as intended, ther is a dependency between the memcg stats buffer and the printk record size limit. If we add more stats in the future and the buffer becomes larger than the printk record size limit, or if the prink record size limit is reduced, the logs may be truncated. It is safer to use seq_buf_do_printk(), which will automatically break up the buffer at line breaks and issue small printk() calls. Refactor the code to move the seq_buf from memory_stat_format() to its callers, and use seq_buf_do_printk() to print the seq_buf in mem_cgroup_print_oom_meminfo(). Signed-off-by: Yosry Ahmed Acked-by: Michal Hocko Reviewed-by: Sergey Senozhatsky --- mm/memcontrol.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5abffe6f8389..5922940f92c9 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1551,13 +1551,10 @@ static inline unsigned long memcg_page_state_output= (struct mem_cgroup *memcg, return memcg_page_state(memcg, item) * memcg_page_state_unit(item); } =20 -static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bu= fsize) +static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) { - struct seq_buf s; int i; =20 - seq_buf_init(&s, buf, bufsize); - /* * Provide statistics on the state of the memory subsystem as * well as cumulative event counters that show past behavior. @@ -1574,21 +1571,21 @@ static void memory_stat_format(struct mem_cgroup *m= emcg, char *buf, int bufsize) u64 size; =20 size =3D memcg_page_state_output(memcg, memory_stats[i].idx); - seq_buf_printf(&s, "%s %llu\n", memory_stats[i].name, size); + seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size); =20 if (unlikely(memory_stats[i].idx =3D=3D NR_SLAB_UNRECLAIMABLE_B)) { size +=3D memcg_page_state_output(memcg, NR_SLAB_RECLAIMABLE_B); - seq_buf_printf(&s, "slab %llu\n", size); + seq_buf_printf(s, "slab %llu\n", size); } } =20 /* Accumulated memory events */ - seq_buf_printf(&s, "pgscan %lu\n", + seq_buf_printf(s, "pgscan %lu\n", memcg_events(memcg, PGSCAN_KSWAPD) + memcg_events(memcg, PGSCAN_DIRECT) + memcg_events(memcg, PGSCAN_KHUGEPAGED)); - seq_buf_printf(&s, "pgsteal %lu\n", + seq_buf_printf(s, "pgsteal %lu\n", memcg_events(memcg, PGSTEAL_KSWAPD) + memcg_events(memcg, PGSTEAL_DIRECT) + memcg_events(memcg, PGSTEAL_KHUGEPAGED)); @@ -1598,13 +1595,13 @@ static void memory_stat_format(struct mem_cgroup *m= emcg, char *buf, int bufsize) memcg_vm_event_stat[i] =3D=3D PGPGOUT) continue; =20 - seq_buf_printf(&s, "%s %lu\n", + seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg_vm_event_stat[i]), memcg_events(memcg, memcg_vm_event_stat[i])); } =20 /* The above should easily fit into one page */ - WARN_ON_ONCE(seq_buf_has_overflowed(&s)); + WARN_ON_ONCE(seq_buf_has_overflowed(s)); } =20 #define K(x) ((x) << (PAGE_SHIFT-10)) @@ -1642,6 +1639,7 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *= memcg) { /* Use static buffer, for the caller is holding oom_lock. */ static char buf[PAGE_SIZE]; + struct seq_buf s; =20 lockdep_assert_held(&oom_lock); =20 @@ -1664,8 +1662,9 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *= memcg) pr_info("Memory cgroup stats for "); pr_cont_cgroup_path(memcg->css.cgroup); pr_cont(":"); - memory_stat_format(memcg, buf, sizeof(buf)); - pr_info("%s", buf); + seq_buf_init(&s, buf, sizeof(buf)); + memory_stat_format(memcg, &s); + seq_buf_do_printk(&s, KERN_INFO); } =20 /* @@ -6573,10 +6572,12 @@ static int memory_stat_show(struct seq_file *m, voi= d *v) { struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); char *buf =3D kmalloc(PAGE_SIZE, GFP_KERNEL); + struct seq_buf s; =20 if (!buf) return -ENOMEM; - memory_stat_format(memcg, buf, PAGE_SIZE); + seq_buf_init(&s, buf, PAGE_SIZE); + memory_stat_format(memcg, &s); seq_puts(m, buf); kfree(buf); return 0; --=20 2.40.1.495.gc816e09b53d-goog From nobody Thu Feb 12 06:26:20 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4332EC7618E for ; Wed, 26 Apr 2023 13:39:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241139AbjDZNjf (ORCPT ); Wed, 26 Apr 2023 09:39:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241010AbjDZNj2 (ORCPT ); Wed, 26 Apr 2023 09:39:28 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2160159FE for ; Wed, 26 Apr 2023 06:39:27 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id d2e1a72fcca58-63b64ada305so8348120b3a.0 for ; Wed, 26 Apr 2023 06:39:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682516366; x=1685108366; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=B3i7jLy77aXSdzt8MLnlwJrjLWa5VGev4cAQ6nx3Pzk=; b=VzLG+6h5CSkfvQZli1OGIiAHvWLeZ1UxaxLHwKBOnH2MCjUETX1QJE/sogMe69eqk7 vBbIbWZIN20q7FnUgXd+PGfMXTGUTczgWU8KKwjcZ8lBbyiyhGbKQYcc5cO7TX8Z3V4f 8DEtCJMSayke/qyFUeJHi41Q8w5O3Vw3jYELxMD9G2Yd43hlJ+5i9W56SzV8e4Y7qupz hvIeqP/bkO4QBcA+CZnvstw3SEisBauI073SKtvHsPG/3CBfKdbOME1DsBqBxapdAL07 yrZBPGmoTvf0k16+hlujpoOSCW6OIVEIvr6tqUDY4KOlfvJdizlM78YwN2B4KBUKjdh4 yo1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682516366; x=1685108366; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=B3i7jLy77aXSdzt8MLnlwJrjLWa5VGev4cAQ6nx3Pzk=; b=igda4ZSUBjqEQhI1tZaiUIvrAgkO4F5Un8ychGxIn98vsJpZ3LNNSYZPCKIKViVK4Y OG2vrbWzubad6AYCe84T1gM0ux00QpkKawbYeaoA08zYDwj0qijRCjRbkzul4xZlGJ7s H81JX0tnf3Cpw5ouMXC5O8RJLSoxBHPUWpzU2yH380XiBjA3qkIXRpZSPmrYj8xsKbxG U7278Louso3xCMHErT6Uz7wfy+lOa/2OaRr9lp3fjSEWiSQWhc3m3ANck222bfivKd5C j7BgXtWx03OyBqxdgqCwANHnX/C6QnwTv3amAmE97NgHmnOgC/qtTGqPzhlrVd8c07pT cM9w== X-Gm-Message-State: AAQBX9fMD2WIdhl/F4PAfuLp7+N8SkKiC6PPtI2+eFyUPan3PAeldtHI 7wRCmhZBThwEmXDxyqS9hPl2kIQ7p1c99Mok X-Google-Smtp-Source: AKy350bvBBlvhcymPuweaHrlA8kXbAA4KJRh9R4AzrPuP59p9sKX2jGZrLIZnT3j/yneuiDoaDCMGDaIkZEjKqJV X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6a00:17a2:b0:63d:4598:ff1e with SMTP id s34-20020a056a0017a200b0063d4598ff1emr9147931pfg.3.1682516366660; Wed, 26 Apr 2023 06:39:26 -0700 (PDT) Date: Wed, 26 Apr 2023 13:39:19 +0000 In-Reply-To: <20230426133919.1342942-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230426133919.1342942-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230426133919.1342942-3-yosryahmed@google.com> Subject: [PATCH 2/2] memcg: dump memory.stat during cgroup OOM for v1 From: Yosry Ahmed To: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Andrew Morton Cc: Muchun Song , Sergey Senozhatsky , Steven Rostedt , Petr Mladek , Chris Li , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit c8713d0b2312 ("mm: memcontrol: dump memory.stat during cgroup OOM") made sure we dump all the stats in memory.stat during a cgroup OOM, but it also introduced a slight behavioral change. The code used to print the non-hierarchical v1 cgroup stats for the entire cgroup subtree, not it only prints the v2 cgroup stats for the cgroup under OOM. Although v2 stats are a superset of v1 stats, some of them have different naming. We also lost the non-hierarchical stats for the cgroup under OOM in v1. Move the v2 specific formatting from memory_stat_format() to memcg_stat_format(), add memcg1_stat_format() for v1, and make memory_stat_format() select between them based on cgroup version. Since memory_stat_show() now works for both v1 & v2, drop memcg_stat_show(). Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 60 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5922940f92c9..2b492f8d540c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1551,7 +1551,7 @@ static inline unsigned long memcg_page_state_output(s= truct mem_cgroup *memcg, return memcg_page_state(memcg, item) * memcg_page_state_unit(item); } =20 -static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) +static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) { int i; =20 @@ -1604,6 +1604,17 @@ static void memory_stat_format(struct mem_cgroup *me= mcg, struct seq_buf *s) WARN_ON_ONCE(seq_buf_has_overflowed(s)); } =20 +static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s= ); + +static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) +{ + if (cgroup_subsys_on_dfl(memory_cgrp_subsys)) + memcg_stat_format(memcg, s); + else + memcg1_stat_format(memcg, s); + WARN_ON_ONCE(seq_buf_has_overflowed(s)); +} + #define K(x) ((x) << (PAGE_SHIFT-10)) /** * mem_cgroup_print_oom_context: Print OOM information relevant to @@ -4078,9 +4089,8 @@ static const unsigned int memcg1_events[] =3D { PGMAJFAULT, }; =20 -static int memcg_stat_show(struct seq_file *m, void *v) +static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) { - struct mem_cgroup *memcg =3D mem_cgroup_from_seq(m); unsigned long memory, memsw; struct mem_cgroup *mi; unsigned int i; @@ -4095,18 +4105,18 @@ static int memcg_stat_show(struct seq_file *m, void= *v) if (memcg1_stats[i] =3D=3D MEMCG_SWAP && !do_memsw_account()) continue; nr =3D memcg_page_state_local(memcg, memcg1_stats[i]); - seq_printf(m, "%s %lu\n", memcg1_stat_names[i], + seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr * memcg_page_state_unit(memcg1_stats[i])); } =20 for (i =3D 0; i < ARRAY_SIZE(memcg1_events); i++) - seq_printf(m, "%s %lu\n", vm_event_name(memcg1_events[i]), - memcg_events_local(memcg, memcg1_events[i])); + seq_buf_printf(s, "%s %lu\n", vm_event_name(memcg1_events[i]), + memcg_events_local(memcg, memcg1_events[i])); =20 for (i =3D 0; i < NR_LRU_LISTS; i++) - seq_printf(m, "%s %lu\n", lru_list_name(i), - memcg_page_state_local(memcg, NR_LRU_BASE + i) * - PAGE_SIZE); + seq_buf_printf(s, "%s %lu\n", lru_list_name(i), + memcg_page_state_local(memcg, NR_LRU_BASE + i) * + PAGE_SIZE); =20 /* Hierarchical information */ memory =3D memsw =3D PAGE_COUNTER_MAX; @@ -4114,11 +4124,11 @@ static int memcg_stat_show(struct seq_file *m, void= *v) memory =3D min(memory, READ_ONCE(mi->memory.max)); memsw =3D min(memsw, READ_ONCE(mi->memsw.max)); } - seq_printf(m, "hierarchical_memory_limit %llu\n", - (u64)memory * PAGE_SIZE); + seq_buf_printf(s, "hierarchical_memory_limit %llu\n", + (u64)memory * PAGE_SIZE); if (do_memsw_account()) - seq_printf(m, "hierarchical_memsw_limit %llu\n", - (u64)memsw * PAGE_SIZE); + seq_buf_printf(s, "hierarchical_memsw_limit %llu\n", + (u64)memsw * PAGE_SIZE); =20 for (i =3D 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; @@ -4126,19 +4136,19 @@ static int memcg_stat_show(struct seq_file *m, void= *v) if (memcg1_stats[i] =3D=3D MEMCG_SWAP && !do_memsw_account()) continue; nr =3D memcg_page_state(memcg, memcg1_stats[i]); - seq_printf(m, "total_%s %llu\n", memcg1_stat_names[i], + seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i], (u64)nr * memcg_page_state_unit(memcg1_stats[i])); } =20 for (i =3D 0; i < ARRAY_SIZE(memcg1_events); i++) - seq_printf(m, "total_%s %llu\n", - vm_event_name(memcg1_events[i]), - (u64)memcg_events(memcg, memcg1_events[i])); + seq_buf_printf(s, "total_%s %llu\n", + vm_event_name(memcg1_events[i]), + (u64)memcg_events(memcg, memcg1_events[i])); =20 for (i =3D 0; i < NR_LRU_LISTS; i++) - seq_printf(m, "total_%s %llu\n", lru_list_name(i), - (u64)memcg_page_state(memcg, NR_LRU_BASE + i) * - PAGE_SIZE); + seq_buf_printf(s, "total_%s %llu\n", lru_list_name(i), + (u64)memcg_page_state(memcg, NR_LRU_BASE + i) * + PAGE_SIZE); =20 #ifdef CONFIG_DEBUG_VM { @@ -4153,12 +4163,10 @@ static int memcg_stat_show(struct seq_file *m, void= *v) anon_cost +=3D mz->lruvec.anon_cost; file_cost +=3D mz->lruvec.file_cost; } - seq_printf(m, "anon_cost %lu\n", anon_cost); - seq_printf(m, "file_cost %lu\n", file_cost); + seq_buf_printf(s, "anon_cost %lu\n", anon_cost); + seq_buf_printf(s, "file_cost %lu\n", file_cost); } #endif - - return 0; } =20 static u64 mem_cgroup_swappiness_read(struct cgroup_subsys_state *css, @@ -4998,6 +5006,8 @@ static int mem_cgroup_slab_show(struct seq_file *m, v= oid *p) } #endif =20 +static int memory_stat_show(struct seq_file *m, void *v); + static struct cftype mem_cgroup_legacy_files[] =3D { { .name =3D "usage_in_bytes", @@ -5030,7 +5040,7 @@ static struct cftype mem_cgroup_legacy_files[] =3D { }, { .name =3D "stat", - .seq_show =3D memcg_stat_show, + .seq_show =3D memory_stat_show, }, { .name =3D "force_empty", --=20 2.40.1.495.gc816e09b53d-goog