Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2797510rdb; Fri, 22 Sep 2023 08:37:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFcomnhUIX0+F01UcJy3lTZMZAPsEb/+Ko341n4QntPNiC8ORnXugGX90kwYNf7igWNHPBU X-Received: by 2002:a05:6a20:9384:b0:138:60e:9c4 with SMTP id x4-20020a056a20938400b00138060e09c4mr10359310pzh.23.1695397039157; Fri, 22 Sep 2023 08:37:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695397039; cv=none; d=google.com; s=arc-20160816; b=ANMKI+Ar6pkIcQLBTJ+F67Lkx2dVPmd/IeFYXiryY58yhDp+RJbP6bEio79okAjzhH Ptr1AAd1+GfBTtXaYe9Hk/vCKmMaypRBUCfIhFtRCn6z4W5hS/Et4Brfo4m1f8JaU57e ZGafT5+kSLPqa+mV2WuIgBGLmgx5Ts9Mtq9FcX2MTIFxhGuFCzdCInD4CCK+QF+kALbQ uAGeYvdVhXmGwoV9zugcst1X2+QSm5fW2SaPzKRGtZv8BjIIAPD30ummBDYL42UN/aj0 jUhHtWf0hli/Y+Raal+Zo8TY10Rt0HMypgYIk1J7h3raPBZtUmDeQx6WPJ2cOvN6fbVQ OkQA== 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 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id z5-20020a17090ad78500b00267f3363479si4102795pju.57.2023.09.22.08.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 08:37:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; 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 23.128.96.31 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 D754E80473FD; Fri, 22 Sep 2023 01:26:04 -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 S232330AbjIVIZ4 (ORCPT + 99 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=20Koutn=C3=BD?=" , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Content-Type: text/plain; charset="UTF-8" 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:05 -0700 (PDT) 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) -- 2.42.0.515.g380fc7ccd1-goog