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+ZgXKR+xvFro3uCq0t0JzaSzF5cwzMHOrWOvoNQRRrjyk3A75RB03XNy6mdrz4z8YjNFd4x1mPoZamlPkFhOz8xwnpmAOnxZqxCR9+fxu9RlM5FlPZ3SLzfNSufM6pL9wnMWC9oyoZDr2YWvJ+AGr04DyX/8uzraTAQDFlPBf0Qwca/Giho9ucuiOFljCIUIdPbsp18qdaVwTfyuqIS4XqYuiuT7q9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nd/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(); }