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+rfIPW1RAu2aGIGrBZcXVQN0T0CwSzp+Z4wXxUvk/nC1TXzJn1UmlAMo4dH1c+jHjytoPna0P2I5AYySMvwmEWoZk6pqcRELukPUKDolXdwg1wurFXYdidRIRHsHndq8w1OM7h72IwT7TfXGdn8+82pBDVCVh69UzZbgkm3kHNX5ICIfWA74ROhjeAZngVvVIwAxeGv9FREw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fHbWbP9o8n9vpklQdSkUetWQejN0qKdDZNvBkMon3EgAL0JIFXaRYGNHV3AP9I3ML6nJ0KLDZNXLCFeXW8cDABDJua7ZMQbkhh6p0JpGuFLTo+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(); }