From patchwork Wed Apr 26 13:39:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13224618 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 610BBC77B78 for ; Wed, 26 Apr 2023 13:39:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0A006B00D4; Wed, 26 Apr 2023 09:39:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D69766B00D6; Wed, 26 Apr 2023 09:39:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0A746B00D7; Wed, 26 Apr 2023 09:39:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id ABCFF6B00D4 for ; Wed, 26 Apr 2023 09:39:28 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 65F90A0248 for ; Wed, 26 Apr 2023 13:39:28 +0000 (UTC) X-FDA: 80723649216.16.5963479 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf26.hostedemail.com (Postfix) with ESMTP id A7970140014 for ; Wed, 26 Apr 2023 13:39:25 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=aqdVpamT; spf=pass (imf26.hostedemail.com: domain of 3jClJZAoKCC0hXbahJQVNMPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3jClJZAoKCC0hXbahJQVNMPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682516365; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NvDSnUS+MAA09hIqW3zUQhLkSsb1P4vBALA1mlThJ28=; b=YTUSEQt1KvtoCmmP/6vJh0CbVfsDqBPNK1fzhMHmrLrIZRD5jMOe5Gs1U8idC0lA68uwUt 5fBdyyeNqcmNuHIvZJ6kw6GU7zorjZszukhneK7N1nJk4MYTUsXZAsLocNXPPEw6QcCQ7u C03PdpF5Hd7MpfYtlZn0fUei2hn6Dyw= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=aqdVpamT; spf=pass (imf26.hostedemail.com: domain of 3jClJZAoKCC0hXbahJQVNMPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3jClJZAoKCC0hXbahJQVNMPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682516365; a=rsa-sha256; cv=none; b=KAQBCDpq7H8ZRY0u9Jm84meACjWbQiPPEBVRRa9Jv4uJrRkMvLAx00oZ2ThrXmnDOQbd2k vJByBPBzVbsNpTuRmfOQKmMRMGGMYmdOJK/iQl42gCF+Xc0DBkTNUKfPJ5v1C8ptwMLNb6 bWx4b76dI7DEGhh6TfWjsG1TIeB+pek= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-54f8fcad732so117948267b3.3 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=e6+K+oMzqUD6DY4DjKi5u08iH6sUvNEATkSDN5QSAYLIv/gyJeoyQFTlFmR3qSndLG pbGs3g1UYbV8m+I1dLARoQ8s76/jxkIYSdgu2OfkCe0PttYAvgJoiUvIF9LAqRnqZRLp mp2SapsV/V9zITJC0RLk4Yi+HbbrGeVDpNXuqrHp4aXyisxvPVDCZC8K6wF6q44jO2JG ieHGwaZjTDQxG8hoIBIGOnmMtcfGMbBbWVbCV2CuPCEgdNuMlW5YPSJsWtAufc21DYoD QMepW+ruwyr1jdyiTawz2t4blLM1x40fxkS/7jDIxLT3wTzvIa0ZlM+YIJWrIoWfNLaI WqXg== X-Gm-Message-State: AC+VfDxh+WLyyqIxwA0LyNu4kuQFhaG1/cVafpl8z7H2mG/MKWAzUdqp hC4LjsUyN3xMYvUo3sVOyghbDa6E9nrAS62M 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 X-Rspamd-Queue-Id: A7970140014 X-Stat-Signature: g6758a1qn4usi4gneadxo4y33gfpt4ej X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1682516365-741307 X-HE-Meta: U2FsdGVkX18ctMKkFk3boCRPOnsPvQ9l0oj4RGoJJc4OZGOKOkBwnF9HhO9YRh01CYDByCD70rlyp9rt18VCdi0z/Q2Yyr5sQ8QDsktQqa2YG9OwR4oUuj8WUNnoGYRpz2n6iPiDenH0urzIHoUnqzwVl9hPJxLsGE+hObcFDuot0IGjrIfkWr02NPePzAXncAwXL6G69LCPK+YWVOsttupxKARQuuwQOsou05ic/Hn8W7ZlASKek6y82f1PbST4mWp7MC2xqMaVZjN+q/aUADE/fCHKck0pAp7a4anC8f4NS2tkXrMgtOiY1Bv+yuxGOQniU9ZyOKNJ47kqA8jFrvCoHABC/2vdIB1R9iEIcLTjUmWsGIOh27WcqYK8T51GWq4f8C6JhvaKVeZji6vei5gRLkTCLIZcHL9nrOCQnFaXcW54ZjWbAtHXv4uLeH/W4IQJHgCPkagXNbGE+yBncu+J3jZ4dF9Ht6DmwSXpyMILKdZswkdqvSLPBFPz9XohHEheZ9HviGpkRQ11lSffy2Gp6M/Npnyu+6TXK1XXKY3HRV0erq03HuixVa1ELNZfFFvHwmsNjHpqVx23mPV1ciS7IbFMNL8WxflhfGcDc3kid8ShnP6EKheAbGQB5to9fQEKu+ExwZTCda2xEjpaWRF6/dCH/9z5aa51TP7oyVFFvZMi+gz0CzWzdcYbI1aDdT1PwxjjUeWsCjt3yj8rtQWIYOwQpKG45gGw+3cMnX3+oSMyf0L6qZhDmXJAgcHhnuNCZoN5OiRvia7pA2BEcIuI4CZdCLvSL/IKjLoQe2YXpu/iYbE5Io0tyMQ9RirgbUduAFHeOhTNyBlzihWSp+wHJqtDehySFCcl1kSGw14OEE0rIvLlX+qnke6lntGYk0HG3LrH6koFI36qaT6jqB6GXUZFCahKK7oGQCMMaRMHRI8WSsN+8yi3xnrPnHaa9trO3v/LDiDRhvhT0rn SfwePa2R CUuiUQMAC5kZ4fItWzzmDm7G6/HTQrnAcD5aURWcTBZlXO1oeb6j8zSfJ4cOUaslu7OlPd/wOUXtcObH5jLdhYsJb0djTkUyBUU6XoGHhT/yu/aa2ZfzTaSD0DDkejdIFwyndr2pvB6l0aqYz/sHg8Txo8ifJotkb0tvQ3rgUdQwiKL8ZioWXW0osULMKoiOn8iACVwu1zzzLF8K+FYUt2X/rOZAfZpjds1XrDcI9Rkq4jDg0OWuJ5FSLFnhtF0oTWDhIe7zlJYe91OVg2eqt0I0E3sJkpSqaxUUztYs153VTxJtTv27MVdNoKoCVI0GlZs5DW1PaUDLyXiB4yAMmTH620dQcbFKtv16PC0+QJV8WQ0JNbkiwdaTARH+/89uzifL4ulhU4Zllzla7cqcziWZKdGxTpXeuZqwVRxn3P7laJWP4FQt33Rgvso72TUA+zFX0oyDJ9Ye05ZvffpI9DkS9bVXQLhoAjTC60SM5Q4NFEa2QQkAWDyTKmMdxY+KvnFELeWaD6dJBNP7kM424+gWk+8tJuRFItT3/JwApEFfAb8hD+WpRd1DJ0NMlhR4JuQs8TsDE2atUh3FCEca1a+Xrc4iBkBZ48pJmo/DoVA1Hz5XL4tGorR6t594i3zoZjsI36w7kA8UUOM2hs8uuBev8pK1OVYJxVVoLKm9SnFKnbWk8JaGlhQARPw== 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: 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); } -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; From patchwork Wed Apr 26 13:39:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13224619 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 C53CDC77B60 for ; Wed, 26 Apr 2023 13:39:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1385D6B00D6; Wed, 26 Apr 2023 09:39:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C0D36B00D8; Wed, 26 Apr 2023 09:39:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2DB16B00D9; Wed, 26 Apr 2023 09:39:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CDC226B00D6 for ; Wed, 26 Apr 2023 09:39:29 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A76D41C6768 for ; Wed, 26 Apr 2023 13:39:29 +0000 (UTC) X-FDA: 80723649258.03.F1DA96F Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf19.hostedemail.com (Postfix) with ESMTP id BEEA91A001D for ; Wed, 26 Apr 2023 13:39:27 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VzLG+6h5; spf=pass (imf19.hostedemail.com: domain of 3jilJZAoKCC8jZdcjLSXPORZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yosryahmed.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3jilJZAoKCC8jZdcjLSXPORZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682516367; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=B3i7jLy77aXSdzt8MLnlwJrjLWa5VGev4cAQ6nx3Pzk=; b=pFY2nK6j5coFw5KAtBEwULhZIb21f+0hwzVUHIkSOo0YQ91dsTb2CyKhZWAjvZ6vyO7/Gd MDqrPqaR1krux3k7qGeku4MfkGpJ/aNTAtjSj/aTLFY+suZaT+4k7W6fWDvhV11OgzTNC7 s0S5151GCkWVbhU9G2I7Y9FaVnl0ttw= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=VzLG+6h5; spf=pass (imf19.hostedemail.com: domain of 3jilJZAoKCC8jZdcjLSXPORZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yosryahmed.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3jilJZAoKCC8jZdcjLSXPORZZRWP.NZXWTYfi-XXVgLNV.ZcR@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682516367; a=rsa-sha256; cv=none; b=wUA+M8q4x4Ye8ygCyJSejEAs/mV8zAJ8mXs1DrZDbrSqOE7jDPdzkmkUuTTfkZKw4o7OSM C8gqtzJPc0f/4fYPZGvl7w7jeQMHtTvz9QXQsv3zfvVER9kNl5chW/AYQlaEKavOVGLwKC C8ZfUKhk35clUFI76h2jiQ3vys+SHKA= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-63b62529864so8325168b3a.2 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=UZlR8pQAeAb5VMqWuBD8T/inod0K95w6popDah/HrVZsdld06nZzBMuLoRXAAYnTYa ZFG7JNO/q2PH8a2nLnVLyQP4DZrC+1xHIszOpdFP7OPfTfAqyu+ruHA19eAutR81ccOd kR408CP/8XaYHusxsEm45mO7LAIV4dx2HYcHL/g9jE+R+epEkI/tAlyP6mS3X4a36Xwf UylVf56PwYjhlzT+gKxdhvHfRSHU2cPVEJj1+3zzFhJDWR0S5U7DwkDBKpfomQ3wmvc/ a06AGzDdFeMj2Je2/XBPILajX9WFcR58Ih4F2JniDs0SLM2lAxAjM/zbvtDGbbOpt04m 50/w== X-Gm-Message-State: AAQBX9eMhFw8LJjLWwn29uFM6WW9bSDfDfO301FUW85GkGV6jvYe3nnu osp7+t7NgzGA0AanjT1bHZrh8mma8kH+CnFY 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 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: BEEA91A001D X-Stat-Signature: rcxcaz1mugp8wj58zbugfymrea4mw41s X-Rspam-User: X-HE-Tag: 1682516367-468318 X-HE-Meta: U2FsdGVkX19hRZLIelx0YziYn8psskoLwB5ixluMk9k4xS1UQbBc/Px/KMPY3SzbhGsfdEnThsMDsiZBcGjs3OHnyR/srHKkpZNoKO5mCGMfSwuLEJdbnNF6JNX2cQ8u37AIfoujAoS6AsFAJ7fBTI4v8plAm6dK4+/ldZG7Fu+te1aCLyVFDasl76kqOhb/kL91X0DyDia36qS8ffZN0rI+Z7h/myUNeeouqOe3JCfajUZ/l0itPAPRrjrut8WXRRcPZyR+NKGrmiMVj0PDRoOQtxbnuHm4fVohn5gaST8SvHYWt5N7yoeooOjsCANuR0X/kms8uG/5C1Gqz95bzB9HVROzHCM7Pt+FSIlanQvU8OBnkF5qucQjDRxnzIqPlasFPu9dQtJIMbGSVOeB1yWlz4M6BR3q6QzQDq8hbhnwCVF+skXPtoMsmHU98FMY+ePtibX63hZ1zxxLXwxP79vCn4Ire4E6u+tP575xTzBnhmDt4gzEt9zUwfjBhZJxlb0n11eFevMx59200C6oQYoGfM74c+ZBKMgDUeewu6O6rePuiHmhh/BQngVR0CwJKOliJ9zhsadXuEbYaZFf68wSQ+keBEyktMKnUh6vIh7Kd76YtdlOk0b6uz7Kqij6WeWyRg/xXpBZ3ybo//H+71SfHpEFGZ1vLcm9y0m2qs3nFQuzy323Uij+JCJdWWS0pkbwtpD0qDCVnCX8arK1wOAtd1+AW8/MVVGIErE3FhTBjtDdQ5sehpZq8kxZkhP28jjQbCfyPQNdjKEs00BnQ9FlPs96kiqCzXiX6SGf7b7b1AtFgRpOxICKyM2PxUDbdL7a15hYjOEzxULQwMKVgiQEB4BIQZEF24GmvteX1ipn95B1cOBtZFomQa1th2rHsYqFz1fq9REOXnEcCkGt3JywNbWYi0Yz/TLIsabARAOllUfgcHYWCRBtH8IYH/e4jwRvwitsJIfqaTUNAia YGsHlZZ7 szl9P+zR4FJe/XmgHQjYkxfATYZPhqHztrAliE44gJrzB/LWb6vM05vHUvdCuHFzrSo/SgUcWEiPm+/R2ODyytINCyZSK6+dTqVB6Rn/m/FTv122usRiritrE419OcMmHcnNw+GGU6ylBeecJz5xaSrZzsSFynWjHmji0xqqXDG3SEeY+CAM3gaiKu9tnLDPd7HV7NsfwdVu3WM5S+fEbG5on6bUoxzUGN7ochsg+QdBvnDoOAJG0tO0BkfCMv792JpO4mHvF8W5elXBdaMYSnL0fpuR7aYQNTHYJJl7sWeazQkgMRgpjfGYiqwGaAImU5tXFk3/09wW+EPjKex0EpcY/9c3hqc/WyZDiDO8ingDwu8wzeC57jx/D3mZ2QvQu0PEU+Er2xx8i2QbbmzaDdy2ptlG1sdFsa9lt0thSat1HMVl3s4AN27VdzCksoB7x9X2VTgyh6F+Sa39q8r7wu9MPY6Mtk3/y+0pjJdMEnWQzd6cZxxf2i96AZLmPXSrJ5fTMx2NmXsIYxusLGhYHi5Npco1ra/AjxXf8Xtsj/l+KGIAH2grtZquJRa3ndhwrmduqS0sRM/kFQlnSmEjk9yxPYGyyPg9MWgudVCWPjNA07mxlsPLByD/uFatuhWTFWupUUsIpRCFRYSQ= 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: 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(struct mem_cgroup *memcg, return memcg_page_state(memcg, item) * memcg_page_state_unit(item); } -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; @@ -1604,6 +1604,17 @@ static void memory_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) WARN_ON_ONCE(seq_buf_has_overflowed(s)); } +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[] = { PGMAJFAULT, }; -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 = 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] == MEMCG_SWAP && !do_memsw_account()) continue; nr = 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])); } for (i = 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])); for (i = 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); /* Hierarchical information */ memory = memsw = PAGE_COUNTER_MAX; @@ -4114,11 +4124,11 @@ static int memcg_stat_show(struct seq_file *m, void *v) memory = min(memory, READ_ONCE(mi->memory.max)); memsw = 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); for (i = 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] == MEMCG_SWAP && !do_memsw_account()) continue; nr = 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])); } for (i = 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])); for (i = 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); #ifdef CONFIG_DEBUG_VM { @@ -4153,12 +4163,10 @@ static int memcg_stat_show(struct seq_file *m, void *v) anon_cost += mz->lruvec.anon_cost; file_cost += 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; } 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, void *p) } #endif +static int memory_stat_show(struct seq_file *m, void *v); + static struct cftype mem_cgroup_legacy_files[] = { { .name = "usage_in_bytes", @@ -5030,7 +5040,7 @@ static struct cftype mem_cgroup_legacy_files[] = { }, { .name = "stat", - .seq_show = memcg_stat_show, + .seq_show = memory_stat_show, }, { .name = "force_empty",