Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp807017rwr; Wed, 26 Apr 2023 06:41:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6AsiBHjm2/22mz2lQYjnjlNAvEXhkK1MZW6ZfZnK+L10xxgUjCVKy8xdbHGNWaWC3uptIU X-Received: by 2002:a05:6a00:1415:b0:635:7fb2:2ab4 with SMTP id l21-20020a056a00141500b006357fb22ab4mr3263644pfu.6.1682516465767; Wed, 26 Apr 2023 06:41:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682516465; cv=none; d=google.com; s=arc-20160816; b=sKvglrHimBaz+PkUFgEg5wRAXLRDGLHLu3iLY2i/rga9JQGHjkjGqHXojnC+ti+3NB hD/eHcGyYrcIrQGGzDB3fvyc4yS2JQ1OXOUxRT5vgjtdbOepQb5fUK/72srWvg+YNcXQ txQ28ae3Gi0la/L+FeT+X7C1CQbB+TPUc7zEWf6qlN6PSc2V4nSUVqVjqAF1Kv9c4Kz8 /Vdg8HDIUYnjTaTWIOd225Hdmj74Dn+lrIvHZSO8FA6w6vyVPAwYBR4hKQ22+2QmgX5q abQsAHBc/wAj2XBsMO92plu6etn8baotk9Enc5MW1ajZUccpqWpnmtlOtwT/VubAANDV A6jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=NvDSnUS+MAA09hIqW3zUQhLkSsb1P4vBALA1mlThJ28=; b=UsWlUoMcxAHw0J6siBT2YxQmS4iOaWbgoGBziHdyNtbYzmEeOJD/2HMq7MLAEeohNE IZsugaFqHOHGKHJp4M2/yDSlC8heD7hPXNohdeLLuvnzy8ubXJJNWigcJx6T1mt6Coxk nNaBbmKvC8U1BFMeHP6Sn4G4ZdlVQH/wpjWx0gH1rG9lKnxMugPURu2B5sJH5m9DMG4f 6GTt8iVd7K8H4Ck9um6fZwFLc7qpTUJLGjybdKpb5kyZqJbkUwCOqGWC5Yl+fs3ESerM RpWBiqJ3tnpcvSzFYWThvWtHtwW47T0S2pXXy973JuWIJ/TJWwWHMwuTVW8wMYq90lcZ RzSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=aqdVpamT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h184-20020a6383c1000000b0052851408ea3si6505700pge.423.2023.04.26.06.40.39; Wed, 26 Apr 2023 06:41:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=aqdVpamT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241043AbjDZNjb (ORCPT + 99 others); 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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 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 --- 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); } -static void memory_stat_format(struct mem_cgroup *memcg, char *buf, int bufsize) +static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) { - struct seq_buf s; int i; - 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 *memcg, char *buf, int bufsize) u64 size; size = 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); if (unlikely(memory_stats[i].idx == NR_SLAB_UNRECLAIMABLE_B)) { size += 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); } } /* 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 *memcg, char *buf, int bufsize) memcg_vm_event_stat[i] == PGPGOUT) continue; - 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])); } /* The above should easily fit into one page */ - WARN_ON_ONCE(seq_buf_has_overflowed(&s)); + WARN_ON_ONCE(seq_buf_has_overflowed(s)); } #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; lockdep_assert_held(&oom_lock); @@ -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); } /* @@ -6573,10 +6572,12 @@ static int memory_stat_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_seq(m); char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); + struct seq_buf s; 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; -- 2.40.1.495.gc816e09b53d-goog