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) From patchwork Fri Sep 22 17:57:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13396301 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 C8B4FCD4847 for ; Fri, 22 Sep 2023 17:57:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 638776B0301; Fri, 22 Sep 2023 13:57:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E7D96B0302; Fri, 22 Sep 2023 13:57:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 461EA6B0303; Fri, 22 Sep 2023 13:57:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2C4AA6B0301 for ; Fri, 22 Sep 2023 13:57:50 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 02FE1402B9 for ; Fri, 22 Sep 2023 17:57:49 +0000 (UTC) X-FDA: 81264991500.30.B03E9C5 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf04.hostedemail.com (Postfix) with ESMTP id 32A7B4000A for ; Fri, 22 Sep 2023 17:57:48 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gwLysyAM; spf=pass (imf04.hostedemail.com: domain of 3m9UNZQoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3m9UNZQoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1695405468; 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=3CSLPc6OXSIH75VPEK2Or6+ByoA8DGo3BCTW1zZyxTA=; b=hxG2ULyIkuoG6rPAK+8tHTUuB4n9PZbmOZas6bw3ifkSAj6/Vtt6Y6lBkSylQVsCMGH3q3 YH533GDjSB4cUke8RP5gzahJcWkNlYgGMdF/iR6zdIIPgdLB8QKEvxboDcRC1rsTYXbfkZ XsaMqDA9G7e2T22e2/hzMi6eK34Fhd4= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gwLysyAM; spf=pass (imf04.hostedemail.com: domain of 3m9UNZQoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3m9UNZQoKCKwkaedkMTYQPSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695405468; a=rsa-sha256; cv=none; b=T0i8R1pv4hJUWuzUZbnq6ike7OoWV2xJxpCv074edJBf1+xE0AFR0crXt1ig8tfgZWtBVp IaOUMV296PCVYaIpTMeL/ndoCNkIb3xkeVEXfK1A9CAlfYukvVQuq7Rqvi56n5PYZAdq99 /dydQy0ORDsoSlMXpJB3N9Kfq3i7Vl8= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d814a1f7378so458176276.1 for ; Fri, 22 Sep 2023 10:57:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695405467; x=1696010267; 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=3CSLPc6OXSIH75VPEK2Or6+ByoA8DGo3BCTW1zZyxTA=; b=gwLysyAMB56N2o/tvBU+67GQKsvG0NViYV/MjTCUgQcGLtD9jr1bwFWEbFfkjCOnXJ i4hW70I3dJXDnw0oRfRQNwbK6NHPJeSlReoesp1c+B9YmYGig5vYAVviQz6HdvIHkAZm PpGdh0j81UQZi0oX0m3ovzOwNo32i9ZLNaKekHL2e2qF78cmlaW6QbCJe09vLa8EKIg4 k103sM2Jz34q+4kW2TOBhQtu+7lJazQzXLcTj+4nnm6lhx3+5oBOit1TGcJbznsgIw3E Gw430P8r5QQ/NK1Jus6iKQZzRCCTNjoQInEq2HAN2tSj4QcjH6pWW+/1OcQnJjY9Pmvl iarA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695405467; x=1696010267; 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=3CSLPc6OXSIH75VPEK2Or6+ByoA8DGo3BCTW1zZyxTA=; b=SP/7FrNN6blVDQGy7rDMK7Mfgv4SLhUWqP0ljlCvGgxyvzItrhH84PKWurVB6GSzZC auXyAXO25R/1ofwb7V7hHvu/crwmrCzzDzqLdeTEpnrB8HsSpg5slPVDuLRsL77UFVUq tCmhdtaXD+8TGZHyIlQFJZuDu31RgElV0b3b3M9ldr4OKwwjfotawDJtgCsSU1qdbuTL nCzHXSdBZYY5wPyh8xYu96ihJyb6aUrV1E39n8/ewTTypfQOYFC0wXRKPaz5d8yMniED hHXC6AoCDiiMEYTX+G+AqOG/xKKoxAl9vTOOUJ1/5p+2E2jDmzOt18YscXKfmlJ7LCLS U4CQ== X-Gm-Message-State: AOJu0YxiWXK/bsLgBaOU20OO3Pf5aIVZenizlLGygk1vJ6qbc8NO/cmr PArUAcbHWrn8amf8lKBeBIWz7bivnGtjP96c X-Google-Smtp-Source: AGHT+IH/GVgcWoyTnI6CjuidQQj2eCHCb/7OcETmpcRHx+mMydVisjSv3mlfwbIqt/FcEH+WIvuFEmX/eSnvqJcR X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:8304:0:b0:d86:5913:55cb with SMTP id s4-20020a258304000000b00d86591355cbmr1694ybk.0.1695405467283; Fri, 22 Sep 2023 10:57:47 -0700 (PDT) Date: Fri, 22 Sep 2023 17:57:40 +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-3-yosryahmed@google.com> Subject: [PATCH v2 2/2] mm: memcg: normalize the value passed into memcg_rstat_updated() 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-Rspamd-Queue-Id: 32A7B4000A X-Rspam-User: X-Stat-Signature: 9yup7pk8c6rpmr4b9ycmywu4fcfcy4nb X-Rspamd-Server: rspam01 X-HE-Tag: 1695405467-479028 X-HE-Meta: U2FsdGVkX18e69vvSGZ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BQ1nfPq7LqWmTccmmTVvOf+AKJznErzBzGeTc0cjgfHb1IDwATr8F5vhcgQru4QSIJM3kp3Em8ch+HgOi1RwgTgC0omOvkTHet3+lWumBWOqQsmuaH6B1oeafwUaUmxn6ZjlhLukZpkewbjV48BkNHbnU9rVD4HjY8CI2j7lz8T1rf2Aq6nNj+nwJIrTR5cxeIW29smGo6bLwN13uHhG0vw4vPqgZ1EA0MWnd/Hxz25bBm2Y6Bzdr/f5DIHt6SmcJPytRfeVpyRXhJx3Hf7C2JSCQYYwM3V1POOB7O8CpMNW0avV7SA0Eik800i8XUQ5PiZh8n/FYRW6JEoolYJKDdZB1SbT5KR0k7x+tppEY+eYe1xR/DVTXNa8sZWnG2sdfmbpE9lNCsiwqnUSffBzSo6IyD/NS3Ok0rh1PCHVZUIL88Nz2tphwG3PfEQPLMc6VDECG9RIE14dESari2x0/77X6VgEy5r/fFmU6cNaQlFQVB5AA== 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: memcg_rstat_updated() uses the value of the state update to keep track of the magnitude of pending updates, so that we only do a stats flush when it's worth the work. Most values passed into memcg_rstat_updated() are in pages, however, a few of them are actually in bytes or KBs. To put this into perspective, a 512 byte slab allocation today would look the same as allocating 512 pages. This may result in premature flushes, which means unnecessary work and latency. Normalize all the state values passed into memcg_rstat_updated() to pages. Round up non-zero sub-page to 1 page, because memcg_rstat_updated() ignores 0 page updates. Fixes: 5b3be698a872 ("memcg: better bounds on the memcg stats updates") Signed-off-by: Yosry Ahmed Acked-by: Johannes Weiner --- mm/memcontrol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 308cc7353ef0..d1a322a75172 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -763,6 +763,22 @@ unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) return x; } +static int memcg_page_state_unit(int item); + +/* + * Normalize the value passed into memcg_rstat_updated() to be in pages. Round + * up non-zero sub-page updates to 1 page as zero page updates are ignored. + */ +static int memcg_state_val_in_pages(int idx, int val) +{ + int unit = memcg_page_state_unit(idx); + + if (!val || unit == PAGE_SIZE) + return val; + else + return max(val * unit / PAGE_SIZE, 1UL); +} + /** * __mod_memcg_state - update cgroup memory statistics * @memcg: the memory cgroup @@ -775,7 +791,7 @@ void __mod_memcg_state(struct mem_cgroup *memcg, int idx, int val) return; __this_cpu_add(memcg->vmstats_percpu->state[idx], val); - memcg_rstat_updated(memcg, val); + memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); } /* idx can be of type enum memcg_stat_item or node_stat_item. */ @@ -826,7 +842,7 @@ void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, /* Update lruvec */ __this_cpu_add(pn->lruvec_stats_percpu->state[idx], val); - memcg_rstat_updated(memcg, val); + memcg_rstat_updated(memcg, memcg_state_val_in_pages(idx, val)); memcg_stats_unlock(); }