From patchwork Tue Mar 28 06:16:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13190556 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 2D2D5C77B61 for ; Tue, 28 Mar 2023 06:16:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8872E900005; Tue, 28 Mar 2023 02:16:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8375A900003; Tue, 28 Mar 2023 02:16:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 661DF900005; Tue, 28 Mar 2023 02:16:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 576E1900003 for ; Tue, 28 Mar 2023 02:16:50 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2E5161606F5 for ; Tue, 28 Mar 2023 06:16:50 +0000 (UTC) X-FDA: 80617298580.12.7FEE79D Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 6954418000F for ; Tue, 28 Mar 2023 06:16:48 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sq4WxgrX; spf=pass (imf24.hostedemail.com: domain of 3T4YiZAoKCA4C265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3T4YiZAoKCA4C265Cov0sru22uzs.q20zw18B-00y9oqy.25u@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=1679984208; 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=wILXj9LqWwom+6PQC1+rRSpOfUrkR+GzdkCDGSSBhGY=; b=iWkDdRVioO5F0tLLLd7qXrFCwSRcZlrO7rxDodr9Zos4SVig0XQuxRzl2A0Thnsd9kEglA boo+vr2BE9CCLBcUNfOcxko9nKxWycD8eqp9K/ZxuzZm13wa6FWALsgHQAiabLw2rfMO5l 7qrC2umO6sipg/kUimfK6coA3Q2Y7/g= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sq4WxgrX; spf=pass (imf24.hostedemail.com: domain of 3T4YiZAoKCA4C265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3T4YiZAoKCA4C265Cov0sru22uzs.q20zw18B-00y9oqy.25u@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679984208; a=rsa-sha256; cv=none; b=r76yvcE0q62N5diIb30B8mJJXMsGYUDHvuxMS9oIgR8hYRGQBp+q9aiFHoKaIQEFQk/lDi CoFZR2n2/rua4SQXytbyFzilH8YwkVPh9aCOOuyAXT01fsOyT8kHXtiR2ZwJXZ3mxKqPzA TLgsSfTzC1nb2npSEPLMnd9Eez4QmrM= Received: by mail-pj1-f73.google.com with SMTP id d13-20020a17090ad98d00b00240922fdb7cso1384477pjv.6 for ; Mon, 27 Mar 2023 23:16:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679984207; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wILXj9LqWwom+6PQC1+rRSpOfUrkR+GzdkCDGSSBhGY=; b=sq4WxgrXs35T1kNaqttQxLQHBBfHPzw9iKcGHtZRmWn7KoDZlSxKdNufMOgLyKUFr+ TMiE/wqtjNlMTixUEQeY/Vrw6MeR14OOP90t7VeDCccykNrGNGWLRshNs7Tr5dzPpQxM EqNAZipGy1I27uXZW4pV5ZH3WWGxC9s0suXWrK0/Qi+Fx4TfEA0shsUjup5r4I0T4jcO otnYwmVT7RvIIuwqMJTULG/AsEi9P5/3lltCZIuxMi9242jZnrgGHko3vAN7pXgBxOSv 6bmFPpVjK2j81kXxaYSwcWty18LJNFnzIpVeFZG9+e6GZQd6MIhYJNlu+ckzRRvaCz8L Rlqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679984207; 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=wILXj9LqWwom+6PQC1+rRSpOfUrkR+GzdkCDGSSBhGY=; b=U9CwoM1UJadpMsc2pRCbLSPakfSQZKQ2b32shGd0lvIs3YPCU0EIRiwM8dtdhCEN1G PbC9Im3bh+97ma7zkD0Qvvc3pYrmRIl4HY7XWYO8exn6uA07U2oJw5F/W8xR61L4IVSf HqLaDjqCT+XnnsIU4oGD4H8GlGVF4l5jNIavuSNYhoabB+0kxX4EsVXKJV2IvmY9VvuC ozcwtk4d02+nC31BXJIFRbHdYWZnBU1kKU7/IK/ayWQ+fVesPDzrpxKz1yqrA4xib3N0 pDV+aUM1cMWiDVEp88RZDXmlxmmzbHmTmfKBpEU0VjnZutz2p8w6Gft3L2uXrV8inm04 vTdw== X-Gm-Message-State: AO0yUKVby/DoKqXLC+RENZ4HPjAZUFLMbWbdKBfoqmUvp61N3edd58lq 1pn0iBNnkJQjahm1EsnBxGxOH1Bkgxt1h2Fc X-Google-Smtp-Source: AK7set/NXJ9x9Sdrim9yKl61I9Pd5pvCD8XFm/ZA+2N5HaWD82pwRjDqKXOdg8RZFiQsg1c0bhIGvLI1ByyxVbFu X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6a00:4510:b0:626:1710:9b7d with SMTP id cw16-20020a056a00451000b0062617109b7dmr8924786pfb.0.1679984207461; Mon, 27 Mar 2023 23:16:47 -0700 (PDT) Date: Tue, 28 Mar 2023 06:16:32 +0000 In-Reply-To: <20230328061638.203420-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230328061638.203420-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230328061638.203420-4-yosryahmed@google.com> Subject: [PATCH v1 3/9] 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 X-Stat-Signature: 4b88gkoiezhq86ihxha8koazwby57gdh X-Rspam-User: X-Rspamd-Queue-Id: 6954418000F X-Rspamd-Server: rspam06 X-HE-Tag: 1679984208-850795 X-HE-Meta: U2FsdGVkX18/Nr6qaCT5ndIFv81bGjIkm43qLXWbM8CJ9G/cyWioq1Vkg8D/fuACl89QjQwN0VIfdf7+FZ0hUxDKJ+SSHiu6CVJeqetfp5Pdaia6LNywBSZGhp5IuhID5rm7FY4Z63KYpzZazD5otOIKadeaASKh88Ry4nb+XtBo2wqkE0VVj/JhcfeyDkIaoZ9+Q7Lxk7A5OZ/6zXlG2zrUzBL8P2g9ygKy+LiLEIL81Yrj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LmwGT3cRxry739AeDNjuUNZ79tB8yixf+8fStGOABm1exyspKwvaSwjiv5+wPq39u0Oh8pTk9/zSMkXxUFq5mO6BLKBayZ9eV8wm6ygViGrtmUM97IA0KRLewA+DB7dtZ3nT3n7aQVSOuEulQE5zOanyRsH5v7gFcoM4WhPrBbKWMxN6T+jurlv4t3WZ0pj1EXxIcRyM/EJDSFOoguOG2H2zpnQ6q1xnkBAKGJ9TjFXdTnayjD+kYaPR39IzzPe0YNhuH0dRsQ+qQVhDX65Wz1zT70EQhoA7GfljJKYmB3G9IeJPIj44CPBEfHpH15PNR5LVngirWhB5naxPKKeLGkWrW27Pq79DYLFxjxhqts9t9DHtsmAQSQYAfr/DWuqkfsdpHtznw== 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 --- 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)