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) From patchwork Fri Sep 22 08:25:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13395346 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 D5887CD4F32 for ; Fri, 22 Sep 2023 08:25:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 756F56B029B; Fri, 22 Sep 2023 04:25:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 642CF6B029C; Fri, 22 Sep 2023 04:25:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E4236B029D; Fri, 22 Sep 2023 04:25:52 -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 371166B029B for ; Fri, 22 Sep 2023 04:25:52 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0B1E4C016C for ; Fri, 22 Sep 2023 08:25:52 +0000 (UTC) X-FDA: 81263550144.03.3B6DA7C Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf10.hostedemail.com (Postfix) with ESMTP id 4B83AC0016 for ; Fri, 22 Sep 2023 08:25:50 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uscsogjI; spf=pass (imf10.hostedemail.com: domain of 3jU8NZQoKCJAI8CBIu16yx08805y.w86527EH-664Fuw4.8B0@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3jU8NZQoKCJAI8CBIu16yx08805y.w86527EH-664Fuw4.8B0@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=1695371150; 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=rIAAYaTevangAQtHpP5uU5ftp8AXloDBwmYe1UIN8vQ=; b=7yIOpydIiUvRp0UmWV5g+jrLLB4ex2B/F83YMuZA7d71uFPm/kTUioQGaMhYwRInqXutMl 25Hb/Cotrk/8PLqpKtPqPewQjSxgRerTXW6TnHKhPl5xuTL8fY0hsRiEVsdQhnnZFjSjOS R1/xII03uLLrRh93eYIyzcE4qf2d2FA= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uscsogjI; spf=pass (imf10.hostedemail.com: domain of 3jU8NZQoKCJAI8CBIu16yx08805y.w86527EH-664Fuw4.8B0@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3jU8NZQoKCJAI8CBIu16yx08805y.w86527EH-664Fuw4.8B0@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695371150; a=rsa-sha256; cv=none; b=mhhANwFSxGRUN9zAHgs9bMcvjkv/tzU7sY12rhV0FBTcKg5eyyNUOLKBe0QRopuG5F6oRl YgDS31Y5UWFz8xiqdTtt+dPjga+KwmtaZK0G5kHqsXpoh32uR7l9h33M5jTVGz5yFFkY+/ vd6FhBxV+7Ql33BCKZmkAbddw83ckLE= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59bdac026f7so27768057b3.0 for ; Fri, 22 Sep 2023 01:25:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695371149; x=1695975949; 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=rIAAYaTevangAQtHpP5uU5ftp8AXloDBwmYe1UIN8vQ=; b=uscsogjI1sM8C75yNqh912g3QowJ0xUAwUJVUUEdjRbM0wg+d2J0igz6prc63/onkE VZ9LR77v1LEAujsDNBjahzjxHuhM7rXmN/XH8OPUY1t6nKAiVTDcItmkBURrUTYOruIl 0CJ6gVvQVe2tt+zsvJac5daYzo04sfwqfujV5rIDSg977G/3ibm9FtshvEzTHtinJc2S HdkaiYniwR+yBXFbF3G4Zd0LbproV8ShQIQf7YTb7YqN3ku1beGiyHLp40fCXlHfJJAv AkpHHpWIexPC/0hmIN9PC+U2MGSUKURK885xWBeESEZSKwSDSt4dv1YThMpFCsRbaJxL 5SeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695371149; x=1695975949; 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=rIAAYaTevangAQtHpP5uU5ftp8AXloDBwmYe1UIN8vQ=; b=PaL/wwXrYnP5N+et9QjReh3I/ZVS6B3+BYQtSNR37Pi9vM/ch4/vmzGFrI9amk/1Ds lZSIR0XyuFBAkos3Wh4S/deUX0Vc+6s4ejWKnZaONR7SMaZkU9j5J6uOfht3ja+ONIxe /n2im9VjMVTvtANMMUTUCbaTQnEEYUQdzfKjc+W5nsawaP9AwW9ZVDMv4q/JDqrgBrQ4 TqvE+C/3vbA0rn3WbpiKUn1asz8L3UsDFiZYUY+lANAhokggkWbOwiFJPfgaPMVfBFRZ ZFO+i6rrDdNgtwtTnb4YoOfzcxh5m7kEoQepZrKa3euGfDJVn7FfDD7bUt5bhUKvaS9U L6uw== X-Gm-Message-State: AOJu0Yz6Gi+qzlOZrlgA7Bcx9o/H2ErQd7RTNQl2J6r4hvT0m8Jgqtvy 8dfKCweeXs/09IjoU6cvDD8xVFeRFGZALnnc X-Google-Smtp-Source: AGHT+IH9FmhKpmE/aOSN1QtFpxWAOwHMroAGexQ2KmLhCy1SW/yq8LxLetuszbv2gMPyLfSx0b3BqtGGq80q2XS3 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a81:ac5a:0:b0:592:7a69:f61b with SMTP id z26-20020a81ac5a000000b005927a69f61bmr106372ywj.0.1695371149321; Fri, 22 Sep 2023 01:25:49 -0700 (PDT) Date: Fri, 22 Sep 2023 08:25:42 +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-3-yosryahmed@google.com> Subject: [PATCH 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: 4B83AC0016 X-Rspam-User: X-Stat-Signature: gr6rpihw8ooaedy8sjhpntzgtqs31ohm X-Rspamd-Server: rspam01 X-HE-Tag: 1695371150-967835 X-HE-Meta: U2FsdGVkX19aok5K9BxIgdLjxeYlnrLgv1/aDzx3Y46p5+ELuc82eh/SJUhjdCpkVZDrU4feey6hGA2drahj/N5Mj8+rfIPW1RAu2aGIG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gZZQNs8R4VfO6iXVncb9OnINuYtof4M6LsSrtIsLZahXrt6yKUB0kYsn5SIdjVzpzO9zc2JyABX/Vc+knUVshYIxvXeKCeMra5z66CWYrECM/hxAISgXCm2pgfvJelrM8PTRzLK1D7VizdxEY267W3WC+91j20DaByUkId5hiAb76Nppkygb6fRLM3tq+aE6GLqfHbWbP9o8n9vpklQdSkUetWQejN0qKdDZNvBkMon3EgAL0JIFXaRYGNHV3AP9I3ML6nJ0KLDZNXLCFeXW8cDABDJua7ZMQbkhh6p0JpGuFLTo+87M8egxf58Mz6BaKV6cfInA6abzTYPCtSJEiQ1GdwWpedG80JwgpEdHHzpzJgiFVYlMUQg4LFrXe7LoPXCrgJaOFhBbAmc2fLc42Kk5X8W08AFrIxJ9W+FfV4ejDjOFNndaR7IFyBhiUCHJ3TkjwmXysKQ1FuercjgVDKr9wlyvLjZ2+4YG+Ss8W5+r0A== 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 --- mm/memcontrol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 683aa8405c22..ea050908338a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -761,6 +761,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 @@ -773,7 +789,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. */ @@ -824,7 +840,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(); }