Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2611280rdb; Fri, 22 Sep 2023 04:00:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEotlgz6RpjBuPFAhLwC3EYaCCTphUUhSrxUy5qvCFFat7VJ7iY45k366GWDGOx8AXHlycy X-Received: by 2002:a17:903:11cd:b0:1c3:bbad:9b7c with SMTP id q13-20020a17090311cd00b001c3bbad9b7cmr9203682plh.31.1695380425251; Fri, 22 Sep 2023 04:00:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695380425; cv=none; d=google.com; s=arc-20160816; b=BKfjy8m3/7wcx6WpktzVbFf3Q137Sy6Tf1jbIBYkwT6/xeZlb6KNwHoawpwqNwQ8OV J1yQix4ZalDAMY/FQAlxFuPmTmsyPEQ1IneIbHkApAik8dOuPDP2ih5ZDFXjDOP0Oj6n peaYZHXFUM/kI0A4gk2Wp5v6w2KXd3K+PNOWwEQwSh75hb/nxBJdmU5p73EElKwAEe67 Ab2gPktyB6o59w2SIcV44RLkMtkXpK/D9hq/5RUaXRPlpe5kvbOni+UW1h+pP8zLdohG M8Po9+ew+IU94apF9ptPEeWQR7/hHu3Nzru9zXm8Fl8Mt87xouuGiF7gjcKntmTmKtG0 4w5g== 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=rIAAYaTevangAQtHpP5uU5ftp8AXloDBwmYe1UIN8vQ=; fh=8R3NybVVYxF3mxyewwJ7cqkhC8pADgA9jxe98eswTvE=; b=qO/qfVM+Oibu38fYehDSXhVkWF0Jzoa7Dduu7uICE0tnJcGjetlyGme+EgcLHC4QrV FRXuTgs/9cxlY6iWVsP3LYiGeEIhtqaJIZgDSkNXZzagrACPQujYtgX2GMAljE4WtdVk MdXLuVdevBTghSx0/QS2GWGrrlE9MAOB9jJUddYsA5g5H9eFcu5yFYHSg0DFQHUbBBDm mi9HHV3907PN4yDd1xI9xByu9NDs4KwJ7lsG/nqtDChLtHHo9iRqAja9XBDshsSnZlcf hOP1Mbq1JmXWLWfZKTNNQQxklpcssFXSnN2UWfV/Yttw3t8mc0WcoVPS6lPG6Pa0aEqW B8FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XBjbXg+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id c18-20020a170902d49200b001b886d36bf6si3719043plg.226.2023.09.22.04.00.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 04:00:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=XBjbXg+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id D380B804913E; Fri, 22 Sep 2023 01:26:03 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232222AbjIVI0A (ORCPT + 99 others); Fri, 22 Sep 2023 04:26:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232328AbjIVIZ4 (ORCPT ); Fri, 22 Sep 2023 04:25:56 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DD2B8F for ; Fri, 22 Sep 2023 01:25:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-594e1154756so27404817b3.2 for ; Fri, 22 Sep 2023 01:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695371149; x=1695975949; 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=rIAAYaTevangAQtHpP5uU5ftp8AXloDBwmYe1UIN8vQ=; b=XBjbXg+OCwQRzWSTySzeE3xjrZNExAPDUkhbOvLO3oyemoMhg+n+/8OOQeFtp8zjtd NcZ3ngZPDetAfxubUT4T3c1GPJr8I0Nddh0dWkf80zfGvDegE2CnxRh+72pQ7AIvbs88 8btKp3IZGxFzOC8uOHfXSeAcH08Mv3ky2Ebz6VMCS9sMET/7+w6IjMNQBJAIXcR/76fx fGh/11DdtjCr/tS+5l4GURiVu0p2Eavakt4072+jZlw8LUSZsH3rqq0e7n8Kb5YKscWh NOe8Ds6BFFjw1MAIB2W5695bokZTXu1o4tggtxZRq54TzLRr/IVijIsmKIVKamb6q+fH DdqA== 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=Iz9mEiI8mD0P0MV56qh927wz8Vkho8pAuPUQl0mrNuTbgJ4014il5XNNtfiL21pMTW NKO1beZqaigdkA4Ej9BLiIt+jFpmRalR7whQVA0jBgtI/BdH1cuonXXDMErBVNnWyrbV jgBvFryTAz5Mcpj7K+XJeCN+Rb+23AlN+T696RGn4bguHqSLqvF2Ot3yPZ2EM64olmjL XTaUSXZpkAJX1v4RZoTgBCwbya5u5WyLn4d8A0bHLiQeS6Pz8wOoHjwxsMvYuySQRyo0 2Jk9ZEqGvfNda96e3n6nI1CbaSXRMtwagutCUTkrxOu00yMdydSflt0VzroO+O79xTI3 JoDg== X-Gm-Message-State: AOJu0YwZgBImAsDrpMmJwOrHYvMNKOFUKEq7OzYzfW4yHCCB2BTYHVVN RqBHt2tuOigZzCUIFIx85MkkcWd0pJfKMAc5 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=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=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (howler.vger.email [0.0.0.0]); Fri, 22 Sep 2023 01:26:04 -0700 (PDT) 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(); } -- 2.42.0.515.g380fc7ccd1-goog