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: 75851 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2005833vqo; Mon, 27 Mar 2023 23:25:36 -0700 (PDT) X-Google-Smtp-Source: AKy350bMMRM/QyVBC8kXye77S+U7QsHh1fqFVbOIArZyVKfkZrfyufITpE4MO1XeHEd1pRFXY6NZ X-Received: by 2002:a17:907:1819:b0:931:dcd4:4ff0 with SMTP id lg25-20020a170907181900b00931dcd44ff0mr17651119ejc.75.1679984736673; Mon, 27 Mar 2023 23:25:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679984736; cv=none; d=google.com; s=arc-20160816; b=DejYeI2WLbYUaYa0yVevUHL/4Bd6WwmW8q7c3++zZqoARo1VI1dxfoQnJ4RJtSxDD3 PvCGeTBNX2SwU17lb5JygGjkR5m+9uIspgidSodw6wqB2pvfg9L17SdUWFGcx1dj3A4V E5rR2mY9l6LkshxgQ4awaDbYmC/z16ysw8R/XrlkV0FhmnrrEvkYi8cn/86A+Uzh7o7p EL7pwMrXcN8i6i1e2YG7HvdbF9w2Bb3TBMEVj62rpVc30LEi7sU+vdzXmfYru5ZoWpEE EhOB1n39Nqioo3miYjxKPqiuNcvwmeWBHa3Ow7hxvdQqgVHANw25LUvliL8Ap/EKf9Hq qHmA== 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=wILXj9LqWwom+6PQC1+rRSpOfUrkR+GzdkCDGSSBhGY=; b=QhLaoeoTq/39aLy2sghBNf/WZWzGW8Kw1WGx6Xa6y0O4Y0RpfbQi8ITF1+7UOP9o10 JW5uVqI+P3Y2itKN7LF2vyh3ARfvhuq+IA575O2c/hAnAR4ZmXrQI8C/e2jngRApfe9r l2YMfggalHtv+hEA90jYxjPVdUYaQGy9xx0Z13SrcNdVZzNtNxf6G8F6WGzooGyMX+1G KhYfmN2GS7Ekkf7CNoOoUZ+mH+EOY4K83byKYBNyztQN/paiBcTilWqCfWhtuz1pMjB/ 7PCNDtpfkQhTbfAAWcOdppoQ3y9iRc0cbBv537qfrPvcUxh0ir9ApigTpy7L5/Da7bij ELgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=sq4WxgrX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r8-20020aa7cfc8000000b005024cdeac4dsi1963627edy.70.2023.03.27.23.25.13; Mon, 27 Mar 2023 23:25:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=sq4WxgrX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232620AbjC1GR3 (ORCPT + 99 others); Tue, 28 Mar 2023 02:17:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232475AbjC1GRE (ORCPT ); Tue, 28 Mar 2023 02:17:04 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9CDF3A87 for ; Mon, 27 Mar 2023 23:16:47 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id m12-20020a6562cc000000b0050bdfabc8e2so2931954pgv.9 for ; Mon, 27 Mar 2023 23:16:47 -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=I6vG682qT13m3sU9ngFFJrgMtnr0BOK/QOSPrxMY737/6dupNDH9MpOdxHyVGoUu3n 7hFTfumFGFHLw33WtT2FZ3FN3ivFzN0XZ/zxdAH6WS82LN2jUV6zoCYQAmbPrxtbgwO6 dwqkKcKSJnNMVIXrUqWCS49YrCqSSJb3+YhqrJvLrhGceDM4e9VMtl32vFt6np7kfaOP BL0tA+qp2rLrP6HdMriqJB+z87Y8/YF5whpYAy301M45bc99CaMjNn3AfJk1CURtv5Le Z31LvTcCvbSLlII7gdl4r0z+xMujhVzB8iL1lTqBazZwVEvUpKNxfEIZ2LwGJNQy+hYZ VV9Q== X-Gm-Message-State: AO0yUKUJXsbsIvULgpRXDd3o7Jdb/Gec+hnuJt0TyaPkhYa5pvi4GAxf h7Vey/cYtYY8BoPYG3bDD6w9L5qmNfhP0455 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-Spam-Status: No, score=-7.7 required=5.0 tests=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=unavailable 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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761591674940798879?= X-GMAIL-MSGID: =?utf-8?q?1761591674940798879?= 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)