From patchwork Fri Apr 21 17:40:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13220542 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 63B00C77B76 for ; Fri, 21 Apr 2023 17:40:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59D286B0078; Fri, 21 Apr 2023 13:40:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5272A6B007B; Fri, 21 Apr 2023 13:40:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32AC36B007D; Fri, 21 Apr 2023 13:40:31 -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 1EA716B0078 for ; Fri, 21 Apr 2023 13:40:31 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AB07B1A0592 for ; Fri, 21 Apr 2023 17:40:30 +0000 (UTC) X-FDA: 80706112620.07.478146C Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf01.hostedemail.com (Postfix) with ESMTP id DD7F240010 for ; Fri, 21 Apr 2023 17:40:28 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Vz7q7Ozm; spf=pass (imf01.hostedemail.com: domain of 3jMpCZAoKCFULBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3jMpCZAoKCFULBFELx49103BB381.zB985AHK-997Ixz7.BE3@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=1682098829; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZwYNf0IQKL13Uvalja3XtUe1fz98Sj9XyCNuQ2xKGcs=; b=K2R7olR83mnEvlidqar2pmrYRelafpVnBrV7VSQ/tUQOVbV2KowaTefFd+Mknp1QCVXdi7 QBw2qv3edM6icpOVnwNNRLjrgJ8AZIeew9oZHyxqtz2Ch2gkQvxH9pb0PiY3D305Uw6QG3 bppyvr9oAf06BXfdVxqOgg+Pe8lcDRo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=Vz7q7Ozm; spf=pass (imf01.hostedemail.com: domain of 3jMpCZAoKCFULBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3jMpCZAoKCFULBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682098829; a=rsa-sha256; cv=none; b=tvq93POghesvPHALJELPGWdY4K74JrPDK9qV95bBfT6xzeqX1QmHcLBqtfFO1zNHY+nral v1IGGgQjeEUvDlS07oLHcazw5cFfUAJ+HdYRInXGDylm4R8zV3xF2TUbJ7iudwu4tkBCwZ LrMJmpdsUv6I+YaddxplIxka4EYDcH0= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1a66bd44f6dso16018335ad.0 for ; Fri, 21 Apr 2023 10:40:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682098828; x=1684690828; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=ZwYNf0IQKL13Uvalja3XtUe1fz98Sj9XyCNuQ2xKGcs=; b=Vz7q7OzmTKVhjKuL1Z10tx4sjzaZzQc1RfkV5JIKDa4QF+qc7PE1nRpKj4JbTW26CL 6HWg0Bk/Qn7IeGOg2v+aAU068B55qLODSnp8tJGuESSICbp+6Gjpfa3HASvT4P/INIIX FNJAyhgiBW8P7szSQRrC+9yXtrHR4LwEf5c+uGkIF6nWGq5t81yHqYblmUSNV6/Pj4a5 x4hPCDxoK7EryyUSzUifkrvbv6JClx5/LSyTe0bPdsXuYCvvh5cuu7sfplfgaUZyWnve cPw/375qmd31nmxG3Xi1PRi7CTxei8qHjgER9ienP6JVZlSSk1rMC1yFsifBD6+eDFuk /A6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682098828; x=1684690828; h=content-transfer-encoding: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=ZwYNf0IQKL13Uvalja3XtUe1fz98Sj9XyCNuQ2xKGcs=; b=PnQGMpwvje6SldBhzIiO9NvJ1svgwgLq65HNrOsbEM1C1inlQ2k1yvz+E601YeIxtn uyjdOVDfOBLvLCyPUAbrkeIe6AthjiQCrtdKF17NAOuiViACwrk+ZRF+p5A5ON8W+/0D 1zbIc48oW9lkuKT53ANtVgLD8AwBGYD1TY0GHrG6M8zIaGS42MVh0ISUYDuMPGU4qn+K yE2jiDVgVrD+mF2eGWM2XF3UlFs/DzpH5Tr8Nu54KcbK4AMZMXHWs0/CPrdxF4C9oJAd khfNEUNsi/wEgIAOLoL1/qmtSbSmlmqVAMYvSPH21xFYcu1NQsTrX2ankAv1fQPHH53I J9vA== X-Gm-Message-State: AAQBX9cTVxNqeXs7Xm47Oc894PK40NtZwoEZyPPOeDTyedmAOvNynprK /kmkVF/T41ofVnR2zUB0tVwluq2yQYvGqiSe X-Google-Smtp-Source: AKy350aINzx6WzxMD77WpFdXBe8LNCyI2424id24uJ5dWpjeDUwupScbzRciRo/Uc9LOO+muamaNQYT7/vZDgFTY X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a17:903:2447:b0:1a9:2c3e:b087 with SMTP id l7-20020a170903244700b001a92c3eb087mr1970101pls.0.1682098828387; Fri, 21 Apr 2023 10:40:28 -0700 (PDT) Date: Fri, 21 Apr 2023 17:40:18 +0000 In-Reply-To: <20230421174020.2994750-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230421174020.2994750-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230421174020.2994750-4-yosryahmed@google.com> Subject: [PATCH v5 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 , Tejun Heo Cc: Jan Kara , Jens Axboe , " =?utf-8?q?Mich?= =?utf-8?q?al_Koutn=C3=BD?= " , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: DD7F240010 X-Stat-Signature: j1b74aaamiqm74wf8yrrrnxqi1jp4poe X-Rspam-User: X-HE-Tag: 1682098828-577801 X-HE-Meta: U2FsdGVkX19WfKJKaGLCd0IiCVyuanjHFMQ/yTYbz5pUUZWeijsRKObJZGJIEWDSWUz6sK28QC9aXx+EgZ1AldZ2gZgX7EHUkKUyusilBWFxV7ZFVJnW5oNMTkLh74ySVzYAMrntkCvoDyrxEmPjYfxJhTopVlFaMMozHhiKrQtl1hPapwPDQsSblpTc1rgxKfuuEj1/bLk5Vmw8A2UL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fN/n3iYWyKXXRAQ7uGBse/IkToY1vITCPXKPVueNUj7VfKVVVHtPetoAwt0cBbW8CLI+sIKDrPQhVQ0SIKjWjA8Niw26GO6Bw1AM1ij9rm6KfGnVY+Nnnp/MlY62op9oJvv0cWhD1rq+UM2t9T87VMch0Xq6HG4cK0VpeCYMi2C/bRLv1dV1BTfsBir6cBkTCiIGN9Wyf8qoZr4nlkSqHq+7Zb3QCKKFRwha9vcYe+FrZTpOpbPRACE/FYhUWvJsk9/cLP5UaQ+itB7C1Md35qgMtUszbSQrFs2pOaFpxMSUKM0FrDWqLbuzPFCv9e64NM0+2YHzsc+juCS2y2e3w0QmY+bQ9D5ZpF6fvzX2N5zeSPmENPH30I/A0Krs01VTUBrFFfN6NX3WUkvsjxdS 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, 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 Reviewed-by: Michal Koutný Acked-by: Shakeel Butt --- mm/memcontrol.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5e79fdf8442b..cb78bba5b4a4 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3699,27 +3699,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);