Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3915449rwl; Mon, 27 Mar 2023 23:18:25 -0700 (PDT) X-Google-Smtp-Source: AKy350ZlqDas2yB6exPYES+FSOwklettfWH+4S/BXCRABQojmbhR88D6d1U59+C8Vrq1W3ghWItn X-Received: by 2002:a17:906:6146:b0:944:70f7:6faf with SMTP id p6-20020a170906614600b0094470f76fafmr6722363ejl.33.1679984305694; Mon, 27 Mar 2023 23:18:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679984305; cv=none; d=google.com; s=arc-20160816; b=nukLe0n0ZCalaprUERxmuBSzlRnVsRfcbL44Y6TYY/mZTYnRFV3R42N/GjkbwZn2jx amIZUJR06v5VcNz92EcVHYQ4+NqQdHfgo9mGrbDgn7/q1HXoFJIDHM/BIHIB+sjuXTcm 3/AE0xCV2hf4vioAzQ9LvTKaXs4lJQh+nm1Dskq1VwDFvVwP40iy2A9Yim8Qa0iBHA/M Z0yPFm7uCPZSwc8cQNsnTCI0Iu4acsQ25Um7tTV61Bd4k9u28VDCe4D0xoFTX0DPe7fB 9IlpdkoHFZDQSqbOleTHTkF0gpUNTGdR3she0Ih1YiKuD9mFpyqg7LbJ+91qq+V5X5IO XkuQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qa17-20020a170907869100b009333cf9aaf0si31186095ejc.537.2023.03.27.23.18.00; Mon, 27 Mar 2023 23:18:25 -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 S232609AbjC1GR2 (ORCPT + 99 others); Tue, 28 Mar 2023 02:17:28 -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=20Koutn=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 Content-Type: text/plain; charset="UTF-8" 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 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 --- 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) -- 2.40.0.348.gf938b09366-goog