From patchwork Fri Sep 22 08:25:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 143526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5698747vqi; Fri, 22 Sep 2023 09:13:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEM+MedipPmxjGv28UN+mqViPCueuCnVJzjq+foWrwuPtV+HpoLYl4QmNwPd5Cg69aRk4/m X-Received: by 2002:a17:90a:3d0f:b0:26d:355a:47e3 with SMTP id h15-20020a17090a3d0f00b0026d355a47e3mr159814pjc.38.1695399181260; Fri, 22 Sep 2023 09:13:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695399181; cv=none; d=google.com; s=arc-20160816; b=k5FQCfrHOKEoDcotVGyvFJvloUC9mElTSn0MF5o7+2X5NWSJEKknxl3gCHX15ZCVWF rIsWznaxrRYodIGI27AQFil7WSYcSQ5ydmEK9BznquIPyAC1IbzPEaI7nseCIrv3WHoD I0MMpfaGj+mm2kiW9b/IbZ2KfB1yogC8zpapt0qXXQ6RC8aoErftis7yxtGFQ1zizLDL dMlkF+HYD74tYI7uhlLhxnlWSSOaez4o4cEQziPUI7ji5v5TJ5LdyxxiaOdTk6rKJaJ7 0xPClKt6KbPyYKkrbONoTGH+ERUEmQmF5R/mwnFRcrRLhaMSBByFRIBMCG8vat1PiwGO JSEA== 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=hA6UCA+pZ5efAZZsDxOX7GRWmAIwRazdHR7qd0NBsLQ=; fh=8R3NybVVYxF3mxyewwJ7cqkhC8pADgA9jxe98eswTvE=; b=D3SCL5NKTeHT2m5bgdPNDpw2++s6/3jxq7/cUYXatNY6FLL7Q6eFB0H7PTVBHNu2+W M0Ln7/HnQjoEtWXvY81NcgRS7IcGkHKEF+7H122KKWbQLLv7ldtANw9PStHue3RHMPxG qRz//NqJV6631gei4Omt3lR2flf8jNa0CqV4w0GkVe1UkdAc7HBiUA7iVVkaiq5/9qYc me5QnIuiXi2B/ReiEQp+AwmOeehUNdhSM1DQmXWlJ3yO8mMtdG0eVsM4DHzKyb//jHxJ XvxJthTPL6VduXAEOEqATwrSef99qzIozLHShaqGDXsN2A31vNLJv815D3QUbBIQID5c 1/xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SQERdks0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id nk6-20020a17090b194600b00256d7cc5b67si4193500pjb.133.2023.09.22.09.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 09:13:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SQERdks0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 661BA80BEF12; Fri, 22 Sep 2023 01:26:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232365AbjIVIZ4 (ORCPT + 30 others); Fri, 22 Sep 2023 04:25:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232222AbjIVIZy (ORCPT ); Fri, 22 Sep 2023 04:25:54 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 445088F for ; Fri, 22 Sep 2023 01:25:48 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d84acda47aeso2736610276.3 for ; Fri, 22 Sep 2023 01:25:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695371147; x=1695975947; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hA6UCA+pZ5efAZZsDxOX7GRWmAIwRazdHR7qd0NBsLQ=; b=SQERdks0zi0PSIP41M1JAfs4srPJ3OG5AhWG8eQIg4LwH0qTgWWyzkYSrJw6FcYMZm dEiwXXeXIE6bkqzT0wNp8LakRHRz5+LAOMF4uthWy660XAFlvaPseJjcVcv7to9siRbt D/eFBvzFvz5976ts/5/GUazqJkQ2rFQXkLX9aKtFEPkOVfiVhQjI5how+beoc/QursBr XkswxWtZ/arxPCKw7y8aB3/FwBPzuo53tRbvA9fLK4rWQDEWkGkAZ7Pv0qF9qaaBQVZp Tq6fhLsD+B6kRtD3xj+kCw5OWXEyurDZK4MWs8v3UcB0C9gKPlU66qXFCY6nSGKI4GkI Zhfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695371147; x=1695975947; 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=hA6UCA+pZ5efAZZsDxOX7GRWmAIwRazdHR7qd0NBsLQ=; b=hIubR8XyE3SJXeR6D5CRZPo/PSlWX7pHhVqxcT4j4OYMPl+F2pfzGSbV7/WIBLLtpR E7t+blYk98+i0L1i+92ub1X07fqmgM3HoH9uELekyeXhAAAsqnLtgP3nxNM6CP4uR4CH vRMQE66v1vB2F83Lkiqq2BpsTF80X3xfs8PS6+NkkEwuFwHNlIox81ZscYTEQ9xEAMCe DgcXnMLsIrlRNp+PwglnpimWf/JhLnCrb9e55UJfo7B26BhGrIZIGX00gjZlt61x7UDT MOD8P6670HHpHsfxRGAwKHHhy9nd5ipBFvdjwgG89wixYCGqoPB+9zranSoryQLVUUmJ dkPQ== X-Gm-Message-State: AOJu0YyDJsi7nS5lTP6EHNSEszFZN+ZavLGNkaXiFkFVlZp1EGR/xOKD dHH8MDhGDpLE+YdFCnPGsS+cNzpN4WO8+uxG X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:ae66:0:b0:d7e:79c3:cd0b with SMTP id g38-20020a25ae66000000b00d7e79c3cd0bmr98381ybe.3.1695371147538; Fri, 22 Sep 2023 01:25:47 -0700 (PDT) Date: Fri, 22 Sep 2023 08:25:41 +0000 In-Reply-To: <20230922082542.466579-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230922082542.466579-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230922082542.466579-2-yosryahmed@google.com> Subject: [PATCH 1/2] mm: memcg: refactor page state unit helpers From: Yosry Ahmed To: Andrew Morton , Shakeel Butt Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , " =?utf-8?q?Michal_Koutn=C3=BD?= " , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Fri, 22 Sep 2023 01:26:01 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777754891517905587 X-GMAIL-MSGID: 1777754891517905587 memcg_page_state_unit() is currently used to identify the unit of a memcg state item so that all stats in memory.stat are in bytes. However, it lies about the units of WORKINGSET_* stats. These stats actually represent pages, but we present them to userspace as a scalar number of events. In retrospect, maybe those stats should have been memcg "events" rather than memcg "state". In preparation for using memcg_page_state_unit() for other purposes that need to know the truthful units of different stat items, break it down into two helpers: - memcg_page_state_unit() retuns the actual unit of the item. - memcg_page_state_output_unit() returns the unit used for output. Use the latter instead of the former in memcg_page_state_output() and lruvec_page_state_output(). While we are at it, let's show cgroup v1 some love and add memcg_page_state_local_output() for consistency. No functional change intended. Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index a4d3282493b6..683aa8405c22 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1533,7 +1533,7 @@ static const struct memory_stat memory_stats[] = { { "workingset_nodereclaim", WORKINGSET_NODERECLAIM }, }; -/* Translate stat items to the correct unit for memory.stat output */ +/* The actual unit of the state item, not the same as the output unit */ static int memcg_page_state_unit(int item) { switch (item) { @@ -1541,6 +1541,22 @@ static int memcg_page_state_unit(int item) case MEMCG_ZSWAP_B: case NR_SLAB_RECLAIMABLE_B: case NR_SLAB_UNRECLAIMABLE_B: + return 1; + case NR_KERNEL_STACK_KB: + return SZ_1K; + default: + return PAGE_SIZE; + } +} + +/* Translate stat items to the correct unit for memory.stat output */ +static int memcg_page_state_output_unit(int item) +{ + /* + * Workingset state is actually in pages, but we export it to userspace + * as a scalar count of events, so special case it here. + */ + switch (item) { case WORKINGSET_REFAULT_ANON: case WORKINGSET_REFAULT_FILE: case WORKINGSET_ACTIVATE_ANON: @@ -1549,17 +1565,23 @@ static int memcg_page_state_unit(int item) case WORKINGSET_RESTORE_FILE: case WORKINGSET_NODERECLAIM: return 1; - case NR_KERNEL_STACK_KB: - return SZ_1K; default: - return PAGE_SIZE; + return memcg_page_state_unit(item); } } static inline unsigned long memcg_page_state_output(struct mem_cgroup *memcg, int item) { - return memcg_page_state(memcg, item) * memcg_page_state_unit(item); + return memcg_page_state(memcg, item) * + memcg_page_state_output_unit(item); +} + +static inline unsigned long memcg_page_state_local_output( + struct mem_cgroup *memcg, int item) +{ + return memcg_page_state_local(memcg, item) * + memcg_page_state_output_unit(item); } static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) @@ -4100,9 +4122,8 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) continue; - nr = memcg_page_state_local(memcg, memcg1_stats[i]); - seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], - nr * memcg_page_state_unit(memcg1_stats[i])); + nr = memcg_page_state_local_output(memcg, memcg1_stats[i]); + seq_buf_printf(s, "%s %lu\n", memcg1_stat_names[i], nr); } for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) @@ -4131,9 +4152,9 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) if (memcg1_stats[i] == MEMCG_SWAP && !do_memsw_account()) continue; - nr = memcg_page_state(memcg, memcg1_stats[i]); + nr = memcg_page_state_output(memcg, memcg1_stats[i]); seq_buf_printf(s, "total_%s %llu\n", memcg1_stat_names[i], - (u64)nr * memcg_page_state_unit(memcg1_stats[i])); + (u64)nr); } for (i = 0; i < ARRAY_SIZE(memcg1_events); i++) @@ -6609,7 +6630,8 @@ static int memory_stat_show(struct seq_file *m, void *v) static inline unsigned long lruvec_page_state_output(struct lruvec *lruvec, int item) { - return lruvec_page_state(lruvec, item) * memcg_page_state_unit(item); + return lruvec_page_state(lruvec, item) * + memcg_page_state_output_unit(item); } static int memory_numa_stat_show(struct seq_file *m, void *v)