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: 13395345 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 0B65DCD4F2B for ; Fri, 22 Sep 2023 08:25:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92BB16B0298; Fri, 22 Sep 2023 04:25:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DAD36B029A; Fri, 22 Sep 2023 04:25:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 754D26B029B; Fri, 22 Sep 2023 04:25:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 671776B0298 for ; Fri, 22 Sep 2023 04:25:50 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3D41D1408D8 for ; Fri, 22 Sep 2023 08:25:50 +0000 (UTC) X-FDA: 81263550060.13.59A02C8 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf07.hostedemail.com (Postfix) with ESMTP id 6B29840027 for ; Fri, 22 Sep 2023 08:25:48 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yyj8RI8M; spf=pass (imf07.hostedemail.com: domain of 3i08NZQoKCI4G6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3i08NZQoKCI4G6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@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=1695371148; 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=hA6UCA+pZ5efAZZsDxOX7GRWmAIwRazdHR7qd0NBsLQ=; b=X8BoGpKwXrlSWPrbBaRsPCvwGrNUp79U/NzlOOam+xN2eg6YdBEfr1ZNBbueV+yrWzhoOk IfXIxYv0zkEkmHzTKKM20V+nbZcc+wqw5cXoPWOZ1Q2ByAyjeFTCxKiCLo5cCtoBL1nz5Q HCjIZBUOhe0NLNvWW4teebvLn3ZHfdE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695371148; a=rsa-sha256; cv=none; b=0/pPp5KbEu1OZD4i46Mo2fmkbiJDF7F8E13mR6KJvxQ9N5MvBqpTtqLlj99EHJZImkidKc qp/5FzbQXnt9wT3CtFufwlN+MIztrkzgAawcKOZhfsuPtovs7GC3GMpgGQtVqhfXnxHR1j flsLm1bbQ3fZqlH5hpoGhbYdE9+R7pI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yyj8RI8M; spf=pass (imf07.hostedemail.com: domain of 3i08NZQoKCI4G6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3i08NZQoKCI4G6A9Gsz4wvy66y3w.u64305CF-442Dsu2.69y@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d81841ef79bso2744049276.1 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=kvack.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=yyj8RI8MeO/abSBjGjaRjZlR5I8EbKbAyY3k2svn6YdMZ95pP0RSwGCS+yZCfnNLJX I/pLIDmhUJz1qZgE4TYYA7ndODwG9f/beqJvtoaFyUOjsn+WDeH9Da+GTzKfFhtzOCnM EZcWJ2wuATh4DvlDe4UbHeGKfPQVORWgmUBj8gjmbtd8thT5cBvGWgV8QEUxxd/2/rY7 vDEQRiJ2VifNxCzhljqlJRxi8JuO184at9dEW67wE7pfW3Xc3PAIVOIKfrmWbSR+CSPF n81Xc/ZkYP8Y6uRwSaqpkochtYonJZ5ADzdtdqKlPEcKmcrCwA1wAtCTPCDB7annMy4T ++Dw== 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=NFqnZPWOkaoiBSgPId8WZY77e6XV92fDz4zym5xfWgz5wNQdfgudwj9chb7npNuptr gDMBr+vJk09Wv1xoKftuD0oZk9OnniDqa+NpBaeYLBBi+mftvSUFc0kxKWXMsw1Km2fF a+VOXDYHgfheVSU7VKNBepmSL/FIo5uI+sIfcuLH7NqtOqj/RPHmD4TCvdW//yTJr9WV vLpF4sCPlEUeV/ufAvIJ+Qq68JIgq5ZHKYBmHLb6tS4bducwqQ71Hgh7akTx9P6ugiX/ WRZpRxfR/bKqRW6ciW2nJuTo4RH3jKf12ugKZez/CwLKRpNaBBzDTZrimsqGFCuLrQp7 JkLw== X-Gm-Message-State: AOJu0Yxa9S3aMya2POFG1Dxn1ASl1rvKcsem0BMGF8Ch+EIco1g61Ph9 A9Uw+E6iv471yUEHx4blKNxOMrNizrzy0wo8 X-Google-Smtp-Source: AGHT+IF9J46+ga8t43y1HWJEuqBoX35B757JKcac8X/f1AVgRTwmBgQww6zAvjp7qBOb/efgYTVzWDV31G7vl9VT 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-Stat-Signature: fynmwnnwkxckm9ope3w4qeqeecg5b3cy X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6B29840027 X-Rspam-User: X-HE-Tag: 1695371148-913664 X-HE-Meta: U2FsdGVkX182pA4IBTYJygIOk/7ioUE8uaW7prUYVXdVGL03PkMdu2Q7qtIQich69gzcSEchh8HKUi3hk7SCUD21d85CpHNcO8k1Azs94IDAlGLZXzy2bWubSE4wjL+EvTYNVRlWUSuTuFZf6iGGD3ojrnjHID7WzUmM+QEJIlmOBL70FZCpUmO+ZRXq87LR9ncGdRnIggtVrs4ol9Gu+913Pf7nq6VqFd5fT/nqSfPk1EZ/r2oMUlD3FJWwRHWKH9f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yyXbHm+4ka/kd3++9XWRlegDX2mrSvCtpvTt3jQEqe4bxM86EuztA+NU8stY5p3qwbsdFru1W9IdMyfILpgbnJDtY6iSCCbKbZlJD3TQkWJjqj+mowcPwHX6e9BupdKvZDM4/AECzy8u0iK2EJ/t2JJda7MdyfIWQdsghlFXuNjGaG9yL/Xd6RCbK7NuiStMxRwUz7Zlj1puBMA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000524, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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)