From patchwork Thu Mar 30 19:17:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13194753 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 86B48C761A6 for ; Thu, 30 Mar 2023 19:18:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D55C900005; Thu, 30 Mar 2023 15:18:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35FB1900004; Thu, 30 Mar 2023 15:18:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13F01900005; Thu, 30 Mar 2023 15:18:12 -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 E36B7900004 for ; Thu, 30 Mar 2023 15:18:11 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BCAB941103 for ; Thu, 30 Mar 2023 19:18:11 +0000 (UTC) X-FDA: 80626525182.16.9B348E3 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf13.hostedemail.com (Postfix) with ESMTP id 03DF420013 for ; Thu, 30 Mar 2023 19:18:09 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Rb2fsB8a; spf=pass (imf13.hostedemail.com: domain of 3ceAlZAoKCPAqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3ceAlZAoKCPAqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@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=1680203890; 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=aVus0GFA9nzxOZ5lNIH1Nx8kHcLEa/aLkX98gl9HKOI=; b=NSkahq5EeEW0kiLf6xABY4/lvGeqZTYxwmE5s1boWUYxuQ1YZ0zHRyA/7ohilErFwZvOw1 fmSu2oB0D5KmcvAwJlsTKZVwlA56Aw9sRbff8Gh/cilOOw7RiHd+7+ldcH+9wRnb+v6hcL R67EWdmMpYx3WKlGotWUPI/vlglNH0M= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Rb2fsB8a; spf=pass (imf13.hostedemail.com: domain of 3ceAlZAoKCPAqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3ceAlZAoKCPAqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680203890; a=rsa-sha256; cv=none; b=qeeHWosvM4Kw5XbDO2sogXT2C+sx4qavE89Ij3nRmJdTQmE5V+xRo1VDzQM5UdOlNCRIP4 Csqjc4S9PXE6OzyY8oP5pFP4z0fqDvvplMTR1UBbR9eJxXXghXa45JJ1yjTHik5aENrgFK kdtKswh9DK8etotP/57Ns0T7N3xlvc0= Received: by mail-yb1-f202.google.com with SMTP id y144-20020a253296000000b00b69ce0e6f2dso19770063yby.18 for ; Thu, 30 Mar 2023 12:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680203889; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aVus0GFA9nzxOZ5lNIH1Nx8kHcLEa/aLkX98gl9HKOI=; b=Rb2fsB8aWLuAh/6UfhLacuvSldBOfVahXww4db+vgAZH6d8hHK5iS/Pb3yBuHu8zXu 8OHs3K+JgNd5BWKL8HmUqSNmeBMWOUfM93BgScQIJGiFyUTuS/OhqqsgB8B/zESgECWW id0La031n/vRfUHiqMH5ptV4ZUHLtuC1awDopegwUjfP8gYPSzkD9GboR6Sv+BT5F0il 4cP0p/kpaTUv9ECKBi7X6nDyHboM9b8Z+lLZ6iyDmkLsMIt+cWstg1DQo4e/ZnrIN56I iRBi8QgWvfbGxJTMXxv2zZuY7CwzGwzqEOEYe4XvyV9MRzo4MibTgBPKLkzWKa272DY9 IgGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680203889; 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=aVus0GFA9nzxOZ5lNIH1Nx8kHcLEa/aLkX98gl9HKOI=; b=lOtma1IMzvJ534QWEu1QcZtBzPMu+4wMi249ol7zhXZUDa6JZ8zUgd4xVYhWMve32Z Kpcm1C9gLtzXK76EuiIjRyq9t1P35AZ+wPmMACATuGblB4lZVN/LDHKq9O0Wvq97XRQy q11dF2qwSTy14GZtTIOt+dSOf5FM7DUe0QUtKqU0fUF+iTUveJ2r1nJcUBbTuw+2vOfm YscMVZ2ALx/jwfVkWhpLIXDr+h72NbTmSGuSIa9OhdSYG0xXVIojbpI0hEsC5Ovf6/0w KvM/c2LFYgRv5j6n31ZkKHUrdl5XraliiZol4nsoTs1Q6+mjg6QzkQ9zFkUR5zegS5L9 Qmig== X-Gm-Message-State: AAQBX9c7mG/PDa7VIkj5c/c+OM+DGJ92k8Hqsr4/c8oSds9ejxv6Vqrh yJscRmd2Vvl3SV3EoNqqehRyoBtNJm6MF+FR X-Google-Smtp-Source: AKy350az4CClmAfjDaxG6/AcT2u/aalMEwRTuNYj9qh25Mudi/3feszrufxHB4k9is1ESXC81UQXzGZVI+EC5ExV X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:10c3:b0:b79:4826:e8e3 with SMTP id w3-20020a05690210c300b00b794826e8e3mr10930499ybu.1.1680203889210; Thu, 30 Mar 2023 12:18:09 -0700 (PDT) Date: Thu, 30 Mar 2023 19:17:56 +0000 In-Reply-To: <20230330191801.1967435-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230330191801.1967435-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230330191801.1967435-4-yosryahmed@google.com> Subject: [PATCH v3 3/8] memcg: do not flush stats in irq context From: Yosry Ahmed To: Tejun Heo , Josef Bacik , Jens Axboe , Zefan Li , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , " =?utf-8?q?Michal_Koutn=C3=BD?= " Cc: Vasily Averin , cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, Yosry Ahmed , Michal Hocko X-Rspamd-Queue-Id: 03DF420013 X-Stat-Signature: 1yrzanjzyhqcsupf5pj8nq9x94phqpeq X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680203889-9800 X-HE-Meta: U2FsdGVkX1/OjXkKg0R1orkShi+A19tencPmKNIx5w65m7UtVw6+1kO3LCigovNsV+ZNRBJtfj2tKkQp+4wxfaclh105zLf4i3wVsalwJOYZ194qMidHQt4zLmLctMKCXW66dtckUOJ9uZGxr8q8YlCupgoQ7/vLmW4SjUJp6ItBK9SftpGEDqK0uekA/N5hN9HDFtganfryrvUL99SA+VV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yJRjBCa2idhNk2zIvGLpBJhoG63aMnlpyGGhXmVVKwqRIcD/FruXQQwV9s6mVCWybdAtDNlDJJFySvID1Sspdftqxnw1ISmLAlyWOGPljiCpcPN7J3ekcPumRoznb+knGv6Xesm7hkycLC82ZgNwm+sY+urpHqOp/mYJ1WcFJ1G+2CGOZflFE26hWYRNC0HsSQBb4muzA5onHZ/8YJpJBciWnNYA8e5LaxcO0tdmYJgNWJDxkkJqp6w7yVC0ZcuMBQ8oHnpoxGe7eLQc68CDp6AIeb+D2dHNIYwHlEM6l9OPL3AkLfjLdLa+F7XgKSVsmFsQdkJHonJKC6TJvdTqYCPcAX2c6VSIOjiaWz/RTKyifmqYedcGB7T4RnWTxiLRfmBUPGG4UtMArNwPq+X+gaKB47DxwfjcMFxpImc3Xw2fEVz4HGTA6ilPJr8kjxFo6nUgkzrXY+6QWOeXVoHpXJwk07f7mvNcdIiC2OekXWwjdWFX77Jw== 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: Currently, the only context in which we can invoke an rstat flush from irq context is through mem_cgroup_usage() on the root memcg when called from memcg_check_events(). An rstat flush is an expensive operation that should not be done in irq context, so do not flush stats and use the stale stats in this case. Arguably, usage threshold events are not reliable on the root memcg anyway since its usage is ill-defined. Suggested-by: Johannes Weiner Suggested-by: Shakeel Butt Signed-off-by: Yosry Ahmed Acked-by: Shakeel Butt Acked-by: Johannes Weiner Acked-by: Michal Hocko --- mm/memcontrol.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index c3b6aae78901..ff39f78f962e 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3669,7 +3669,21 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) unsigned long val; if (mem_cgroup_is_root(memcg)) { - mem_cgroup_flush_stats(); + /* + * We can reach here from irq context through: + * uncharge_batch() + * |--memcg_check_events() + * |--mem_cgroup_threshold() + * |--__mem_cgroup_threshold() + * |--mem_cgroup_usage + * + * rstat flushing is an expensive operation that should not be + * done from irq context; use stale stats in this case. + * Arguably, usage threshold events are not reliable on the root + * memcg anyway since its usage is ill-defined. + */ + if (in_task()) + mem_cgroup_flush_stats(); val = memcg_page_state(memcg, NR_FILE_PAGES) + memcg_page_state(memcg, NR_ANON_MAPPED); if (swap)