Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5133630rwl; Mon, 3 Apr 2023 15:05:22 -0700 (PDT) X-Google-Smtp-Source: AKy350YgkKBHKHCpQ3SIUs/mGFi5qzXbpIcOXum/vaotIHFEb5ee5wRxcOEO7rTx7FLJe8fWBJlH X-Received: by 2002:a17:90a:5646:b0:237:c52f:a54d with SMTP id d6-20020a17090a564600b00237c52fa54dmr20439869pji.21.1680559522132; Mon, 03 Apr 2023 15:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680559522; cv=none; d=google.com; s=arc-20160816; b=tmYagkTrbHInZ1MSw2ojlfpFoo8ZEO7whuTJHsma8OUqzMDrXUmlgchzRdPhaknQtr ZBJfmW98Mg2akaBgZImFAhQZxN7MMt+nOIH2rrKr+dPBauktcpohc+/EBV+CETUbqlhx fl/e0de13T5sa7aCDzP+xYBBqgh8UJ39hCvpCvLXBIqN/dQQQt0XaIUc8Jc0g2k3ul2V jBXKeAwcs0E9ttDyH6atYLX05+T/cxMTHuF/wRci0aLgaKmqc2CQvY62ZrPf51FzjzKi N+4Gmyp5+yRQgr96lIw1Z7NewlQujD3g2+VQbuZHWXBusOkF3fdqELHoS/MG1hUZgKm8 Auag== 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=kF9j9xhVt+HAtgUv/sW6MmUPAVRA6NHiHn3mXAJRGAA=; b=J0g397FkfB+4h/cGGMkMMauPkdEJbvIlW3VPghPZQtDe8KPZ81TOIMbKwy/1cuProM aPMqPADrcTFqVXb7YqelwN6B1hxZnQVKfClh/WvXK3mnoXiQ5qogvnghLlMIvrPwShrS W/vmYpPHMPhDv2OyVAiPoELhBrqxKtPR+38XtRIjXGUAiHTs5b0KOUwdTQVBziJ4QtjS aUQuUCZXusJ5Ml3FcL/WldvyWJuEgkOkQG7DkwjzDJiqQxeYcxeqjSA8rfSvJTB2KIoZ ouUWI7eg1esFk1kA+TDn/XCbAiLTGFcHpaTfeGO+lwUt/XfaGq4cctFYN04ZcWmx5hdd 1IXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=QZZrc3A9; 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 t11-20020a170902dccb00b001a2979e96b4si8250786pll.90.2023.04.03.15.05.10; Mon, 03 Apr 2023 15:05:22 -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=QZZrc3A9; 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 S233731AbjDCWEH (ORCPT + 99 others); Mon, 3 Apr 2023 18:04:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233615AbjDCWDq (ORCPT ); Mon, 3 Apr 2023 18:03:46 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F98335A9 for ; Mon, 3 Apr 2023 15:03:45 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id q15-20020a63d60f000000b00502e1c551aaso9005648pgg.21 for ; Mon, 03 Apr 2023 15:03:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680559424; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kF9j9xhVt+HAtgUv/sW6MmUPAVRA6NHiHn3mXAJRGAA=; b=QZZrc3A9Ala3Y1fUrUeH9aHQEpEHIm56zSkCNE0A0d2CfnkVvyEn83miymwgAbVi5v 9d0zVCVjgbHefP1pXO+Nwo1Ul1PtmNl2tpG2LpPChio/My4WZElf1o1jchSNOgvJZUsW WbV5lrmHTcH3fAqoJ7gZenJSRA3/dWbn3kNYUTeryGOWXdDf83/N+i1IFVKcpHglHDCj nH4nWeMmhgItD/X0NlX5brqxxgrkgNj//KowEl9SOf+dOa58iks3E9glke7V87blHVFo T2x7kiC5rzRzeUUzMZ/tXlij1MqA5Wi+XXZnHplS+57XrL+yaVLwig14jPX/om3gIc+M IAag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680559424; 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=kF9j9xhVt+HAtgUv/sW6MmUPAVRA6NHiHn3mXAJRGAA=; b=JWtFcwwgI+rnKFHkD62RC2ZavEcq8ZhXWDt1rurNg+y/TRMCamP0Ul4ynSVoKNeYxH V9GXqDDPOByl2i8oX2jEbxYmCwspIysZ0gco+3/JPzbhpXYTF4WKrXGfkuSkPF2uViMD PR5rSDe2cbXTnbzzHrDF6LMYlseJ1BVODucr7ZRGew5pFmpNdvuG3mklHcT3DREn/PSj FWvVC3CF/5s214gvK8N0q3xDGvL5bOyUuCmr8p5QNhADzm0O2Z0+QOVF3oJcHNZHWv/Y ya+KrVxzB9PWl43FXJGHBd2+bHfHtZGurc+oEmxsJzC0ic/Zwi0NGiJvaBeqN4sPxNoY ahzA== X-Gm-Message-State: AAQBX9c2lZVG1AxG/1UwsoFp0b2MOXK8VaZxl7NoAiBnHrImdrOpxspx 5l4JsDnb3vJkqEz/SI1H8p9ukScwQlz6B/yy X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a63:2301:0:b0:503:91ff:8dd8 with SMTP id j1-20020a632301000000b0050391ff8dd8mr27700pgj.4.1680559424700; Mon, 03 Apr 2023 15:03:44 -0700 (PDT) Date: Mon, 3 Apr 2023 22:03:35 +0000 In-Reply-To: <20230403220337.443510-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230403220337.443510-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230403220337.443510-4-yosryahmed@google.com> Subject: [PATCH mm-unstable RFC 3/5] memcg: calculate root usage from global state From: Yosry Ahmed To: Alexander Viro , Christian Brauner , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.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, we approximate the root usage by adding the memcg stats for anon, file, and conditionally swap (for memsw). To read the memcg stats we need to invoke an rstat flush. rstat flushes can be expensive, they scale with the number of cpus and cgroups on the system. mem_cgroup_usage() is called by memcg_events()->mem_cgroup_threshold() with irqs disabled, so such an expensive operation with irqs disabled can cause problems. Instead, approximate the root usage from global state. This is not 100% accurate, but the root usage has always been ill-defined anyway. Signed-off-by: Yosry Ahmed --- mm/memcontrol.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index bdd52fe9e7e4b..e7fe18c0c0ef2 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3698,27 +3698,13 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap) if (mem_cgroup_is_root(memcg)) { /* - * 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. - * - * Additionally, other call paths through memcg_check_events() - * disable irqs, so make sure we are flushing stats atomically. + * Approximate root's usage from global state. This isn't + * perfect, but the root usage was always an approximation. */ - if (in_task()) - mem_cgroup_flush_stats_atomic(); - val = memcg_page_state(memcg, NR_FILE_PAGES) + - memcg_page_state(memcg, NR_ANON_MAPPED); + val = global_node_page_state(NR_FILE_PAGES) + + global_node_page_state(NR_ANON_MAPPED); if (swap) - val += memcg_page_state(memcg, MEMCG_SWAP); + val += total_swap_pages - get_nr_swap_pages(); } else { if (!swap) val = page_counter_read(&memcg->memory); -- 2.40.0.348.gf938b09366-goog