From patchwork Fri Sep 22 17:57:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13396300 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 D8BD7CD484C for ; Fri, 22 Sep 2023 17:57:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B2DB6B02FF; Fri, 22 Sep 2023 13:57:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 461D26B0300; Fri, 22 Sep 2023 13:57:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3298F6B0301; Fri, 22 Sep 2023 13:57:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 262536B02FF for ; Fri, 22 Sep 2023 13:57:48 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 005FD1A1167 for ; Fri, 22 Sep 2023 17:57:47 +0000 (UTC) X-FDA: 81264991374.14.EB895AE Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 3A5C0C001F for ; Fri, 22 Sep 2023 17:57:46 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=z1m2w2Gf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3mdUNZQoKCKoiYcbiKRWONQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3mdUNZQoKCKoiYcbiKRWONQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695405466; a=rsa-sha256; cv=none; b=oP3k7dUWWb/xJyMeU6vE+By0//oY5TB2yCBzWd6allQdFArnj8iUZ1C6CgvxXIT2dCuyMi N9VSKb0pD00tOi7BCjk2GEKEvdmFIme0xEfRk+UHhnoI14DWDfiBx0XKkHYLrfo4BZ+qFQ OBkRSZDSu//6TveBTg/gLyC8K0qK6gc= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=z1m2w2Gf; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3mdUNZQoKCKoiYcbiKRWONQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3mdUNZQoKCKoiYcbiKRWONQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695405466; 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=CIUVdWYJzS23SUkOVUGYlHJuolUCd+SBS6YJzjY/W/M=; b=7oNe0Yc0ZIBzBUjtVPng19f3j2ILJDDx+/n8tnZZxAi2ASNCf0qEsDzsAUW6eTribQu8Pv MdMEzrEoSIWnC8Hyg5AbDhaEnXHu604EI7MUjHfCET4hVfjCc+lgD0bE+9LmgXvNCan/zZ dzcaKIjex8EpuHCV4H+FU2YZIxlQAGE= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d7fd4c23315so3075396276.2 for ; Fri, 22 Sep 2023 10:57:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695405465; x=1696010265; 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=CIUVdWYJzS23SUkOVUGYlHJuolUCd+SBS6YJzjY/W/M=; b=z1m2w2GfzVom+l1+6oJsxb9wewF6ezV9F7oyT+GNwvAjBKXluQAMD3W4QdeUY35qtg BxTVt6q+bPSAEafjCD7Rq6BTeT+PBORsYnPsW2I2c7J9E+yjB2+KNLL2qlU+sxt0GC93 cacH9DI//Uc97KMcuZj9p6tuqcYiB9hDqAaKNwHoCm/arGGEw3ZdWEV/j8al7sT0OP+K 5G5u6Ux1sxcRVc/K9NIZjKYeW1XiCbY+6iDB/nLxsaKSBVaJEb4lNRdAzLB0lYB1yVd6 8j+Jt4D02fZnInD1m+WT0GA7pTzujGaPwwDd82f7oY7HE2xJqOLo3HMrb5e3Ezq+8vAT 4bPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695405465; x=1696010265; 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=CIUVdWYJzS23SUkOVUGYlHJuolUCd+SBS6YJzjY/W/M=; b=twWl9d/V90ImaASJ15W579K9qBKPcJLPmhfAXMLAMLWwLLQ417VuW/j5nQpQyne1jt Op84HiwtIo0CMgnN1YGWsZWaLm3rEJhpDXDQoPcTUOrH0E2lB2imApQ8uYyBcvvxdfiY SM3H32wDPgulBQ7PmJ9sba2QoOaMNO8SbXMDJx9QUHJJxhwszZMqpKmSnOAiPo9+llSt bAGH0JEWsm+4Ll6m98RD/2CtUmuvzAOp+agd6qhwLaUyt8SUpuwnG65vo01HlIiccrhX zxNzWXew8hZM8I3KWfM813GNdyrMEBB52TtpnTV0NW6a65MfWIiDNGUD3D03q3pDFvkp L+DA== X-Gm-Message-State: AOJu0YyPXBFflunvE6amvrOYP17ofRLqWYkNFNx98JuC32+nwWE7Bw3y 1hiaD5eOOaw3fSEn6XSpVhZZzLoiDc+m3gWC X-Google-Smtp-Source: AGHT+IGRY7CHNarSt8hxzLUxdPvhzPSOwb+6g2+4WOANIpjP9cYGG1L1f1r5GlTbEtZccPZQJPzwzGGJFWlsxbc8 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:6942:0:b0:d80:cf4:7e80 with SMTP id e63-20020a256942000000b00d800cf47e80mr453ybc.7.1695405465321; Fri, 22 Sep 2023 10:57:45 -0700 (PDT) Date: Fri, 22 Sep 2023 17:57:39 +0000 In-Reply-To: <20230922175741.635002-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230922175741.635002-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230922175741.635002-2-yosryahmed@google.com> Subject: [PATCH v2 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3A5C0C001F X-Stat-Signature: ptr5dr4peffsbptcnc4hddy8zma4zqni X-HE-Tag: 1695405466-892182 X-HE-Meta: U2FsdGVkX1/nDPe7eiluIEa7yJajLV6fPue8GAL2rQI2HcraNCNU0WZp5MXi7j76NeqdOnaOmPDMtcfjn8/gYzLWmMPtLqii3TbUhvrZ8Dez7OuZyQIzPGGT7tIhAhkwFMJRVA7ht//d4SmzORKLg5JbZ6j/Ih8O3SBXKrvWjHW3kXYKcVAJ3J7GX/OF8ndAZpI63jmLo5tWZXGxdIeyUvxUaHOr4y51loT2gt/kjIgFUS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AqLrfCPLEzA5zqxp80eTc4YDxUwfB+bLYm1+FG8mmAlAGTRQ4IDEPdXE3g5duxBxKHyo2A3UkyVVaJX8XOjCXqZUGRT+DxiNXjm+tW7n+vIukvofvXFYVGtRzgtxvd9TR/I7VxgBp6q3Yv4LvtAyZ+clvzP9z8sxZhaGsd45LvdRJqMJRxGxGypWtnpY87Vlugwk80S9T0PwJH7Jkqz+8vuR5XAT X-Bogosity: Ham, tests=bogofilter, spamicity=0.000028, 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 Acked-by: Johannes Weiner --- mm/memcontrol.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 927c64d3cbcb..308cc7353ef0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1535,7 +1535,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) { @@ -1543,6 +1543,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: @@ -1551,17 +1567,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) @@ -4106,9 +4128,8 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; - 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++) @@ -4134,9 +4155,9 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; - 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++) @@ -6614,7 +6635,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)