From patchwork Thu Nov 16 02:24:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13457528 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 8FBA1C072A2 for ; Thu, 16 Nov 2023 02:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 243436B03DA; Wed, 15 Nov 2023 21:24:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 17E326B03DC; Wed, 15 Nov 2023 21:24:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0456C6B03DD; Wed, 15 Nov 2023 21:24:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E861F6B03DA for ; Wed, 15 Nov 2023 21:24:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C2668B5BCA for ; Thu, 16 Nov 2023 02:24:18 +0000 (UTC) X-FDA: 81462222996.23.5AEB911 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 0C44E40009 for ; Thu, 16 Nov 2023 02:24:16 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Lz4er087; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3UH1VZQoKCNELBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UH1VZQoKCNELBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700101457; 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=BcE9vJD0hOv2TBpe3ivca8OUmRQ1FOEoUuzJXTFrwQU=; b=V/cQ+HPub3LRc5wRmHRqgmdZDgagO0gT78aM6gqGhcr+q/Iicz8xNvyxhvsIHqqBvekbB8 tLblR3zLDA/OZ89AJTA7eQudhtX5IlhrwnRbKe952q548rpzfOeHG1sTUUsQHNhwrHw+ju 9izIAf/Y7H3AFVH4c1cQPXlJOcwCxNc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Lz4er087; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3UH1VZQoKCNELBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UH1VZQoKCNELBFELx49103BB381.zB985AHK-997Ixz7.BE3@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700101457; a=rsa-sha256; cv=none; b=rxcPehLDZhoNEReFDQVz/rFUQULYj0pWbrtLfRYh1ZE8DV8dleUIvoTYmMpU2hDb8B8h1p 4WJ3veT5o7Xv86qlj3s0tkaclQBUdywoex0MCSUfUe98XXs8BANyUjOdw7nXgmq4Cuhn1I nNtN3/kkQwXHMM5qoiWkuOMfSx1ZZOs= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-daf35a935fcso401421276.2 for ; Wed, 15 Nov 2023 18:24:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700101456; x=1700706256; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BcE9vJD0hOv2TBpe3ivca8OUmRQ1FOEoUuzJXTFrwQU=; b=Lz4er087QG+xQnsOtBopWjKXijaXJhajN/WNLlKJ9XxqxYHbY1XGYV9O5Lmc6eOgLK c3IZLlgNpN7Vugq99WcLerWEU9sABfVjQd7RI7eOhmxWEtdf6aSuFlQGSpLnTqoYJ8v7 rPcdxqxGY+garxk43MiiApl8kO2b4m52DC47PwlfzblXK8U6a+O6nr6Bg18cmkSFv577 5ZOzk2RbqdDSAIEfSWjHRh8MKsB9njhoBEnjNpWkA2SCsx1wg5hArNluVDQOW8I6y9rN tGq0qfC9KjbdUNc7ZgwN1PxeC6rTWPB9054UEHxAL/tYv9AUKlKcVkhNMS/T8gqDYYI5 Tfcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700101456; x=1700706256; 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=BcE9vJD0hOv2TBpe3ivca8OUmRQ1FOEoUuzJXTFrwQU=; b=xK0IMHQcSWFZLHSWQpfpHYwS0yBJ4VYi7GjKFsZ8URJVBdNIIh/r78BvKTIq9Ybzch rbF6cSyVRh6fAfjL+AWfaNKD6uh1SSndBhqYji/md4/EACPf/9NhyAAmHKIeg5HuOSfh PMqdYzIAABwifF9+pdRnov2IJOhjl64yJsA5PtL9zwNVHppAvkn4PI90ysLSAH1uPc+X vhOoctiHI3xZ5yciVkDQIWPWzYiICbp4JpAHbSFl8R10OenAV3GNKOajJN3aivGS+OlG DSQcHDxOGGwqya98vx3VVme2fg6E6c6XabLLCSd3aYtoF1SVhgbMn23jXYFDV1CjKE8C fj1A== X-Gm-Message-State: AOJu0YyujsDANLm639fEGKwExHTJr85uDpASyjD9+jWVdsStvO4otTX8 GySgee6QxcVBSVDlB77SEU3JCpX7oBDDMRyM X-Google-Smtp-Source: AGHT+IFy97x2nQDJgIiWGH9dEF3svZisBvzaosL7zloYt6xMQ4EjhERYd/7GmdSfHzW6U7mYn8R6aavP5vLCYx6D X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:285:b0:d9a:d272:ee58 with SMTP id v5-20020a056902028500b00d9ad272ee58mr402081ybh.9.1700101456166; Wed, 15 Nov 2023 18:24:16 -0800 (PST) Date: Thu, 16 Nov 2023 02:24:06 +0000 In-Reply-To: <20231116022411.2250072-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231116022411.2250072-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231116022411.2250072-2-yosryahmed@google.com> Subject: [PATCH v3 1/5] mm: memcg: change flush_next_time to flush_last_time From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 0C44E40009 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: cs3x7ky9eypdoqywfxk95xotgzr7tszh X-HE-Tag: 1700101456-83682 X-HE-Meta: U2FsdGVkX1+gC8AobI580H4v9OnNKLDllmfOs4iZoxMEOsHWpwZ1hihqrjxlfFM9XkmzEU8QunXX6PVBQ0CItwEX2VEfjZ9H4QQvYNnlmT5jFGzxs9448yMYC08gZtIpBxm2H5LxPwZOjjY7+WugatEiJPEd54bqBu72b+FWHn6HA57t48nvfMMAFI4lfm9FtPAKdV+/eBI3TfGgKPxiaiv88vKO71eRnMbdLf5yXY0ADtmTXi2NOFcVNg/5vbn0rDjmeWlldqKcU+UPMwx73jfOI3q0qUe4LE4UkBj57JeZpoxOm6K8DnshhWMqR/RgdNHBNumSBzYWTl/QNt4kFGGBZG27bhyIX3ySaeaZYono3xqA7m00jT+a2i/8clX3O9WU3S5kXMRT8efwbKhXevScVfsiG4/OsnPjhKvUdU/Cny/Anb3baTghHDf795hRzZgs58YvZqErO/81Phy4KRJjzNvCCA0CbBrokdiE5Wxeeip1/QWZNudRNlRcS5sCGKt47WIt4Xl9a2b4t7ehApBlSpfeAJpAynCjs+8yBsMbETd5dJroTRZR4pGeX39zfdAq8dVD9W+aZsGFQ6czhKh8/04SKfIWPMecYqGToEdi1I/2wWM7lG0F+XxTDBIGtdYlwdXC5xJGJKBdVor56/0kObaZWVFeCiHnEQOUqutCnt8EcvSAizQhpQ301HpZpqJOx6ixOwPgM/1HrenQABf3RoaFizI+bMVdqWR8mlhP1V2gg2nDwrLJVqz7iSw7F/y+J1oLoSRZA/XlTG43aWNQ7YqRcV4Wc+batw7eoGokaLyjoGGUhfaAqfKaa51MdCmC/BOeyPLF2bO8hw2gKd67VlHqZRMSLRztuMP8r713TaDdK7xa7QvqvToHehXD/3zcgxGgDvImZGzt/vZMe+Cyzf8Pm85Vq38D2qS080DaA4ACQX3iHwuuueogkQYKMw7lb9e6zCR5qy3OkN4 StZRGz3c SXkUpR4sH3K688/xSN0O5jmAZi0S4n2F9/jSbkzvMmFntpfBMu0Sm/ybeK2hnEbZmXyIwujD8DQONiQsxdMw2z3cZvTGyiDyz4qT1dpjvTC8FrsWQeaSuabPK33HqCVAlN5NqkCuVI3bdoLXzFgIPvNYY6+PBsRBwXYe0blRNEGqtTn2p3iT6bEntYqrm6yR7Vrk0/ckX/0/tOeNDvxDapnD/wfR9vkWSX78rB5Bx1eKPTfHv+apOMb+R17HNoCYqwLgvjgTVA7hcgMbjR6SmDJtzvbfMRL7F0KqOQEmwbL4gi7fvCnoehCW4H/XF7JHX2HzEwOwbWPk0ecIv5HAv2dlrNBEuujp4Mp/vuCxDVk/bzQIYa1TdNDYmMz6As7Quwv5MzVLtSrC3ghJpPufTK6sbLoU5ZVJkmd9C1BZOuh941fBtt9D68QmcCFhsfR8mVH9TOe6Y8BMcpwPv9ttPkpI9uL3Zd67h0PQbOp928Jtuxy6+s+X5EyCs9fgfnx5CvKGfcSszrAS2eOwPMVT5amBH6cxQdqIzaMCRM/iDVzgHxbF0nKiZeSLdn2WX2a4UdftiwMsigsi0NmDEFkXBz1c3bcb7o4r2ZGA54muULVXaFHWKMiUEPxfeHkTfE5M5Y4MI+KV/yoiCOdiAlKsS+w1eICx3HTKrv9ILHM9MNod+7NPgQ5TXASbVyjeIg6YdxKaZ7LR/q+K7Yy8= 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: List-Subscribe: List-Unsubscribe: flush_next_time is an inaccurate name. It's not the next time that periodic flushing will happen, it's rather the next time that ratelimited flushing can happen if the periodic flusher is late. Simplify its semantics by just storing the timestamp of the last flush instead, flush_last_time. Move the 2*FLUSH_TIME addition to mem_cgroup_flush_stats_ratelimited(), and add a comment explaining it. This way, all the ratelimiting semantics live in one place. No functional change intended. Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo Acked-by: Shakeel Butt Acked-by: Chris Li (Google) --- mm/memcontrol.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 774bd6e21e278..18931d82f108f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -593,7 +593,7 @@ static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); static DEFINE_PER_CPU(unsigned int, stats_updates); static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); static atomic_t stats_flush_threshold = ATOMIC_INIT(0); -static u64 flush_next_time; +static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -653,7 +653,7 @@ static void do_flush_stats(void) atomic_xchg(&stats_flush_ongoing, 1)) return; - WRITE_ONCE(flush_next_time, jiffies_64 + 2*FLUSH_TIME); + WRITE_ONCE(flush_last_time, jiffies_64); cgroup_rstat_flush(root_mem_cgroup->css.cgroup); @@ -669,7 +669,8 @@ void mem_cgroup_flush_stats(void) void mem_cgroup_flush_stats_ratelimited(void) { - if (time_after64(jiffies_64, READ_ONCE(flush_next_time))) + /* Only flush if the periodic flusher is one full cycle late */ + if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME)) mem_cgroup_flush_stats(); } From patchwork Thu Nov 16 02:24:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13457529 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 9E6A7C5ACB3 for ; Thu, 16 Nov 2023 02:24:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8E596B03DC; Wed, 15 Nov 2023 21:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D16C26B03DE; Wed, 15 Nov 2023 21:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B6A1E6B03E0; Wed, 15 Nov 2023 21:24:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A3EC66B03DC for ; Wed, 15 Nov 2023 21:24:20 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 70A8A12035C for ; Thu, 16 Nov 2023 02:24:20 +0000 (UTC) X-FDA: 81462223080.15.8F6E953 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf22.hostedemail.com (Postfix) with ESMTP id A51E0C0008 for ; Thu, 16 Nov 2023 02:24:18 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yfAMCvNz; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3UX1VZQoKCNIMCGFMy5A214CC492.0CA96BIL-AA8Jy08.CF4@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3UX1VZQoKCNIMCGFMy5A214CC492.0CA96BIL-AA8Jy08.CF4@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700101458; a=rsa-sha256; cv=none; b=KsDfDpo5mJltMB7x8od2D3E45CQxH1elDZ2NA+b9/uz0X6SliaKWalTh+evBEnNAnfIRlD JHapsQP0G6JINOSXjhpgatj/ac83PLqyZDlNpmEBTqHhP2sNupOeWuZTYKY9OJNViKrCIA WkW+AqNMzbz5KjuCI4kD/asa/tCYCJc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yfAMCvNz; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3UX1VZQoKCNIMCGFMy5A214CC492.0CA96BIL-AA8Jy08.CF4@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3UX1VZQoKCNIMCGFMy5A214CC492.0CA96BIL-AA8Jy08.CF4@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700101458; 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=CsQgPePmMbwkmYg1tT46kxvFflY+DocB+A/S3VGDzrg=; b=09M03Xk53uuTiaJXHIEJ82QQXs2k/Zxft86s77t2ahTFzo/RjNZzWI2XZ+T9Vl6WrZTH8w rwoKcUAVX+6gY5IOVRgFWn5OX8UWn4uFOJFRzG76+m5iRNcb0ukmM4d7dPDgq6ampb02jX OrQuH3MBmQdBUlDFzJa5MvZkdaIpbpE= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5af16e00fadso4835207b3.0 for ; Wed, 15 Nov 2023 18:24:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700101458; x=1700706258; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CsQgPePmMbwkmYg1tT46kxvFflY+DocB+A/S3VGDzrg=; b=yfAMCvNzpQAuhKmBkPeO3mXmAdmVJE3qavs3TwK0Mgw1yoDbWpqe6w1Ob0q3PI2OZO aR/dn97YTHfs9083Pm0Wm0VzYSMebAL5J+M5XHiscQzpVGtp0OTJaY0lBLTxTURptDpE T5Xkw3vMrRS6jX8A+8xFCeNjJjs6C7ZQ3zZrcMrhL6gpEwLzGER+yh3BjY4rvfhrb9xr pn5hDU8UN8q2Osv56dsEAfHD5kRAipIoyAh4EgegJJnFl2OaOmoM4h++Y38JQg38iH1u I/q0KnW2QEiXMJSWPxwCD7jBY9QeXTwcfgLDxlBlriIPOein6LOLsogGWh9/kx8bBbzC pXCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700101458; x=1700706258; 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=CsQgPePmMbwkmYg1tT46kxvFflY+DocB+A/S3VGDzrg=; b=JnFQ6/Xupoh45y8SpSKlUlhunwigAdKmTuEdo9w3D8i/n/MGLbAgfw7XkjR/F/XnOF /eIuxjmC7sTvGfppG9ZCVD456SdKIMa6zqkWngigQMf4asgmeFeYnibzxOIKjAO9wxuk eG7aNfKp5yqCUfVEFCgxbcGVDdFoPORTVojxNDEse1dk90UpO55JOxWmBVpSDBxQDmaw oFzFnMyySpKTM3nWOFvBK1kFlfAsMZzTztqh0GXnZ7IphQEvoCsK1lxDBP5U804hSPZn k+YrW+Oi5XtCZBgE3O6SJEgE/NEQDUutzHQWCPcdYxkgB2bJRC1PjLnJIlQDBpHJCsO7 MzRQ== X-Gm-Message-State: AOJu0YzInYChwvLcGL63gPjqnIIKh66juDYjxvrnlGyOR5P2tCJ4HQiE KMcG7bRN6eu1A3PLfaoG3WVEzY3272JhWh0k X-Google-Smtp-Source: AGHT+IFLy40NOoa1rb6dZOa2HGVCBZYSVTkHt6esqpaY9lO76idIWli3bPfPKtfXVxgT/7MwnEla17Q3y28TlYUG X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:d747:0:b0:5a7:a929:5b1d with SMTP id z68-20020a0dd747000000b005a7a9295b1dmr375983ywd.4.1700101457790; Wed, 15 Nov 2023 18:24:17 -0800 (PST) Date: Thu, 16 Nov 2023 02:24:07 +0000 In-Reply-To: <20231116022411.2250072-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231116022411.2250072-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231116022411.2250072-3-yosryahmed@google.com> Subject: [PATCH v3 2/5] mm: memcg: move vmstats structs definition above flushing code From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A51E0C0008 X-Stat-Signature: 9az8hioa8f8maox3ugxjskn6raic6mdn X-HE-Tag: 1700101458-253701 X-HE-Meta: U2FsdGVkX19VcwjcIx6fvusYZ1/JlsfR6uyxRQ0eFjkKnBnuxrGb9mJfnQ80F+Ahzx9F4gODLmpABDmX96KFadNrndKtpdjStH3V1SU4NS/HBly6J2PVnhMmBhoeSdELGaXi4BC4DmVo9IY/3XipHxz8mP53W5xG6HOqVQMNzxTpp7UCsqrlOf8pj2DIF8UqYxOa7/2vhXQSxrvvtCEVgpY7WveRDF6I6fqks9tuosdezsvmSOhuZnuiOjX5W618gdrLRyOnxfhazI1qDPJJM3pN2ng0luNt1ronUwnnNTYiaOtwDPS008PRDHk0bizG9+cndqugtLbd97BB+kWJNCz6KPfJ+mCTZVflDLIBwc0QrI1gEsmCpf0AkeOBDh2I6q4dNh4bq2WNZTo516u1FxWWUR7u3nUYb+8ZYmzK0etFl9uWLkNVQgrUuORhm+Fc9jYoymulG7L2j8sokzRn+njASY9fDAU2HhxoWQu5/pffW4EEq5zEiIjA02tDItPUKQHZWG660LV/7+/s/LMij7tFtQkQKaW48G1M6N6RJS3eqTGgghIQyh0WdCR2yyWeVjP16JV9VC7Zf/02ifPXWP2PqSSb5qs3xuun/rdhRw+uC++5W/xYbqqv4o3YfnGi3aigFzIiq1bpkUXzHsbiOzAG8zCA9e7rBQCPvIaRyUw5h5HU0tfRvAd9X+37KFbNn/Uj3OyJnUO9WqqKysJbecOFDG+Sr26ndGl6kZ7vh/yj78pGb7RdP6JKdk1jdpGodsjJ8BAaLxXJ3LFL4tRrpp3fWsbFSFd8QstXOLYrtLSuwy9+Qaib8s9OGT5nbl1ofpj5SS5S4+odMc3wveYVYEc+M5NSwO0L7nn+Gyt8CyDYd5kjnHU9CS3EpVZjBWfdSVdGgHnVPs8SERyJaadZ5zp44WWrHZKcyVy0nFAx7vM9NgAOs+a0ILTWgW/yNNX3AmKFFKaeLnlRy2njCOk vurPmErM 5VIuk0gb/FGpPM5Uf0CM8m2WZtC9SNdIHWNtzoSee2MqUZ2as6gXO43SGeNKzKXdf6DjsUXBWyGW7/ZmLJ52tIp/lGEM8iZQJ/Re3QiI7Yv/fpnx4Kfrq/qkc//xuqvPjCfz1UOLBtmaqTo27rbpl86SidhrOOrxt53DcrLQXB/UWcJIaxrk2WsjBMPxi/xzMFQbAZIxnuOxX2BHL9pbVXTvcr6qXRwH3WJ8T3BTZnzix1nvatsYry26nIzJrGTeKLGHJ9fp8vOgCZkmpSUPxoz+/676zUdAbDCx39PM6hvKvB7VagYptao+OAC+fQ6IMUQYpMeeiuMN4Xl1RbSkAmq/fI2A150EE40/+luKpi/Y4KIDbMWaZm3ODVahi4CCxSTqdpIwSzth3iMrYT89VFrb4YzQdpzzVhKgPsWE6qe2pVfTxrRwpuGca4eNZAnIYXQwdsc9q+1fbfvrV10C9Q+OsQoMpRLFGakHykS75YWeUkMOo+mzAy1Gx/6QWT9UtTMTOJxmPeuBwk9tRCq3S+sZGB8J52TAGpfUeBhaUMqw8GhgpzsxoI3afcKXm/bSQ2+8BZJuk2H2uNVueayslGt0aHoqJNvEm46uMYX4IXc8tZPGJCj97Z2SEvv6Ek7iV644/0vA/7snxXFX7Zeo48I3k3TeAXnl/hwCU 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: List-Subscribe: List-Unsubscribe: The following patch will make use of those structs in the flushing code, so move their definitions (and a few other dependencies) a little bit up to reduce the diff noise in the following patch. No functional change intended. Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo Acked-by: Shakeel Butt --- mm/memcontrol.c | 146 ++++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 73 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 18931d82f108f..5ae2a8f04be45 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -573,6 +573,79 @@ mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_node *mctz) return mz; } +/* Subset of vm_event_item to report for memcg event stats */ +static const unsigned int memcg_vm_event_stat[] = { + PGPGIN, + PGPGOUT, + PGSCAN_KSWAPD, + PGSCAN_DIRECT, + PGSCAN_KHUGEPAGED, + PGSTEAL_KSWAPD, + PGSTEAL_DIRECT, + PGSTEAL_KHUGEPAGED, + PGFAULT, + PGMAJFAULT, + PGREFILL, + PGACTIVATE, + PGDEACTIVATE, + PGLAZYFREE, + PGLAZYFREED, +#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) + ZSWPIN, + ZSWPOUT, +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + THP_FAULT_ALLOC, + THP_COLLAPSE_ALLOC, + THP_SWPOUT, + THP_SWPOUT_FALLBACK, +#endif +}; + +#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) +static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; + +static void init_memcg_events(void) +{ + int i; + + for (i = 0; i < NR_MEMCG_EVENTS; ++i) + mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; +} + +static inline int memcg_events_index(enum vm_event_item idx) +{ + return mem_cgroup_events_index[idx] - 1; +} + +struct memcg_vmstats_percpu { + /* Local (CPU and cgroup) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_MEMCG_EVENTS]; + + /* Delta calculation for lockless upward propagation */ + long state_prev[MEMCG_NR_STAT]; + unsigned long events_prev[NR_MEMCG_EVENTS]; + + /* Cgroup1: threshold notifications & softlimit tree updates */ + unsigned long nr_page_events; + unsigned long targets[MEM_CGROUP_NTARGETS]; +}; + +struct memcg_vmstats { + /* Aggregated (CPU and subtree) page state & events */ + long state[MEMCG_NR_STAT]; + unsigned long events[NR_MEMCG_EVENTS]; + + /* Non-hierarchical (CPU aggregated) page state & events */ + long state_local[MEMCG_NR_STAT]; + unsigned long events_local[NR_MEMCG_EVENTS]; + + /* Pending child counts during tree propagation */ + long state_pending[MEMCG_NR_STAT]; + unsigned long events_pending[NR_MEMCG_EVENTS]; +}; + /* * memcg and lruvec stats flushing * @@ -684,79 +757,6 @@ static void flush_memcg_stats_dwork(struct work_struct *w) queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); } -/* Subset of vm_event_item to report for memcg event stats */ -static const unsigned int memcg_vm_event_stat[] = { - PGPGIN, - PGPGOUT, - PGSCAN_KSWAPD, - PGSCAN_DIRECT, - PGSCAN_KHUGEPAGED, - PGSTEAL_KSWAPD, - PGSTEAL_DIRECT, - PGSTEAL_KHUGEPAGED, - PGFAULT, - PGMAJFAULT, - PGREFILL, - PGACTIVATE, - PGDEACTIVATE, - PGLAZYFREE, - PGLAZYFREED, -#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) - ZSWPIN, - ZSWPOUT, -#endif -#ifdef CONFIG_TRANSPARENT_HUGEPAGE - THP_FAULT_ALLOC, - THP_COLLAPSE_ALLOC, - THP_SWPOUT, - THP_SWPOUT_FALLBACK, -#endif -}; - -#define NR_MEMCG_EVENTS ARRAY_SIZE(memcg_vm_event_stat) -static int mem_cgroup_events_index[NR_VM_EVENT_ITEMS] __read_mostly; - -static void init_memcg_events(void) -{ - int i; - - for (i = 0; i < NR_MEMCG_EVENTS; ++i) - mem_cgroup_events_index[memcg_vm_event_stat[i]] = i + 1; -} - -static inline int memcg_events_index(enum vm_event_item idx) -{ - return mem_cgroup_events_index[idx] - 1; -} - -struct memcg_vmstats_percpu { - /* Local (CPU and cgroup) page state & events */ - long state[MEMCG_NR_STAT]; - unsigned long events[NR_MEMCG_EVENTS]; - - /* Delta calculation for lockless upward propagation */ - long state_prev[MEMCG_NR_STAT]; - unsigned long events_prev[NR_MEMCG_EVENTS]; - - /* Cgroup1: threshold notifications & softlimit tree updates */ - unsigned long nr_page_events; - unsigned long targets[MEM_CGROUP_NTARGETS]; -}; - -struct memcg_vmstats { - /* Aggregated (CPU and subtree) page state & events */ - long state[MEMCG_NR_STAT]; - unsigned long events[NR_MEMCG_EVENTS]; - - /* Non-hierarchical (CPU aggregated) page state & events */ - long state_local[MEMCG_NR_STAT]; - unsigned long events_local[NR_MEMCG_EVENTS]; - - /* Pending child counts during tree propagation */ - long state_pending[MEMCG_NR_STAT]; - unsigned long events_pending[NR_MEMCG_EVENTS]; -}; - unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { long x = READ_ONCE(memcg->vmstats->state[idx]); From patchwork Thu Nov 16 02:24:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13457530 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 4D9BBC2BB3F for ; Thu, 16 Nov 2023 02:24:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D3CF6B03DE; Wed, 15 Nov 2023 21:24:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85BC56B03E2; Wed, 15 Nov 2023 21:24:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AF576B03E4; Wed, 15 Nov 2023 21:24:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4E0E76B03DE for ; Wed, 15 Nov 2023 21:24:22 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2D978B571C for ; Thu, 16 Nov 2023 02:24:22 +0000 (UTC) X-FDA: 81462223164.09.E4A9051 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 525ED20007 for ; Thu, 16 Nov 2023 02:24:20 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LOt6xWGY; spf=pass (imf03.hostedemail.com: domain of 3U31VZQoKCNQOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3U31VZQoKCNQOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@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=1700101460; 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=LSZ+R0SqSzDzKoZBVcJ1mA+eTEuDRTwjKPgQcB0rBEk=; b=WMdnnO1vkZFnz+vJD2ac3f0uo2W/Iq+pP9+k2EqKXLqNLKfdbzqo9hYhmKjTwM4aHmKMCv sBiOKzZyuxpBtJvbUtkuJ04Xft0ERT+/57C05f3TTd+vCmdEbgacDOVhfYd8XljZWzBL1+ i22TfupEqa8f724O7EJ5IWXn1Q3NmVQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700101460; a=rsa-sha256; cv=none; b=yjNzrDyCdK+Prk1u+6HukLiUKXzv5d2DCPr6MFGaixzYFlLmeRiF8mqwveuSL3TJym28Uq PuQ5VZt1KJiU7ZdhMl/Sz76BFnDjCjl0BgrxbOjlXiffaSNt6BQ8XZzvv7+tKgb9ubcX+8 u8AtDwVplgmWTBQQxPFUqpDb0ze67GU= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LOt6xWGY; spf=pass (imf03.hostedemail.com: domain of 3U31VZQoKCNQOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3U31VZQoKCNQOEIHO07C436EE6B4.2ECB8DKN-CCAL02A.EH6@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5b1ff96d5b9so4089357b3.1 for ; Wed, 15 Nov 2023 18:24:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700101459; x=1700706259; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LSZ+R0SqSzDzKoZBVcJ1mA+eTEuDRTwjKPgQcB0rBEk=; b=LOt6xWGYNDMUJa75L98M6OJlnPs7jSVyxTc/eHuCpX/b6eLFXfRd53q6HL8AZJpsSD gQbBrKMuqU+xijW00n/tNvrs26XXHyI3tML8Yd+Dc+F4u/uy1H9/xgUg8gdaTJs+T4iF SZaOQydq5QNhJsWgs2H16gsC/IzaE/yTOn3MlFCP9Y20d8K2BmmpJEt26AGn2aW4eo9/ ahqgSMNzFvr+RCVdk4mZ/9Rka5zkaUfq2aL9SvrgbxlhdOX9MtKG03vQJOhIbr7kvKGv KXV20Ux8RHOcSYfIURHE3iGB6B4SlSW8IROy1IYm2iLwFWqJo27QgM7DNA/+WlHZ/p0Z Qgog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700101459; x=1700706259; 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=LSZ+R0SqSzDzKoZBVcJ1mA+eTEuDRTwjKPgQcB0rBEk=; b=NP3VlNfGGlpbW1K+rgJwrHClfQBNyaY1H/yvJtzfZrkemP53pnxfT/YD5D8LO6/pLg sPqBcx12NlRsMJPWF9BQwjHmMAzyJLZnLY7G2HUEUzI3nJ1szLQ+QlP6RiTKtHuUbfve VDv6t7xtiL5uthkRyQMMMpx6nXYmL9anO7oIC3MkiYzTIL3P37w8Qq+zFqFcDkQalKZW DGxGGB9cX8kpIHj0PWc5o3D2vP3BquJGlrDuJkGHvFGdgid+38aBU2A2LQsyfcwtAJnv 7vA7vE3Tv02xrPtG7D5NdOojTxOtMB+3jQVQ3HoAinf4ydyQlRKmOJdTqNSsYnqwNtOs +Aww== X-Gm-Message-State: AOJu0YwcYrrhUK3ZMosCYmoOIbUpfgc8WPkaR2YwM2wN4bJzbnFFH70f 1x8JJNLzFLtXLDX3/eoZUwWvgSq361jFSpeE X-Google-Smtp-Source: AGHT+IEFkRjPRA+t8bnXs4r4alzEC6UzKUxHSRyn9fc3Cf7ttKBPYuT2Hd+c68LlEN9cCFYG3gXViVMxsm4UDLTD X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a81:4f90:0:b0:5a7:b543:7f0c with SMTP id d138-20020a814f90000000b005a7b5437f0cmr404902ywb.10.1700101459465; Wed, 15 Nov 2023 18:24:19 -0800 (PST) Date: Thu, 16 Nov 2023 02:24:08 +0000 In-Reply-To: <20231116022411.2250072-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231116022411.2250072-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231116022411.2250072-4-yosryahmed@google.com> Subject: [PATCH v3 3/5] mm: memcg: make stats flushing threshold per-memcg From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 525ED20007 X-Rspam-User: X-Stat-Signature: fkq4mj618kzrd396o8oxnhgjhey3zso5 X-Rspamd-Server: rspam03 X-HE-Tag: 1700101460-336297 X-HE-Meta: U2FsdGVkX1+n/OV/V9cN3IEUeOOD/d/ugb9eI3sRTuFatIU33ClD/AZb2J3qKUiYo3Q70vE9cHZQ3PfZDC+B2FRkz1wGbMwVpg637WWFklt9wZHUIYp39+9mSqAWDwQa+44DcThp+Prm/e4kaMkD4ql5HCvvzbFXclT1m7VhF70W8TxyqSVKgRyrjlLiT9P3g4SSU4KmZu15NYwRKpFHsjzKKv09GXzxih9xpmK7Fy8mxDXwTZZhoTuRkpzGI8YWKAK/m8kBKWfD+/kaULo22kYlRwt8ixR6tK3nimaBnjXGNnmWy97VFejVvCdbcSe/0qFURccRH3pEovuDf5hZ91EVdxaxbVcm4VI9KRzyCMHwk3Sq4PxprkroKRrF1oCK0XdW8R/6tgPrJs0/68pswW8xzPnkDAfNef6U3b6teXmXIMWYJcwmP0oaXbwrDWONzAAlLcmoGIrABU9i+W7CvpyP3L6uKyR7A+el5R0pE5Om/ladXF8jmfNa9ZRs2w+dnDyAhPt4p1c8WgSJdTrDwAtuJ9SCMpJfyyjpc7ap5haCX6TwcYEkfJI/T2sVEtJ8acQVaGiCyDNjMBfCArzto1ymW5yHCiqiPJNx+U7SMQlaubZa1H1QtEthJ7c2OUzFn5GcmXr/2mPOoPBMzHW5DkuqNUbdR7y5oNNsQoKcxyqbWQmVNNjbONmSDWHA+Ge/Vm+1ZpIMRX7aAiDN0xVGl08c8685xMfx90CcRJhtQeDuQaaaYsr+U38WB90id0BTV1TCIBO1wQyNNF5r3gSbE6Ph0lttACpdajJCWFvkRlbAAp1q6grzAIN+PBl9tmF8lS+28iR6OSZRhnOg1twhD1rJdBf1NI4N+FNwM5F2oU7mKnuGwj5bEg9oL9+rkX+F0+15z8nkD6H+Ls9IaPmYcC79/kIg4Z5nRwcL4OKG0DZvmDia9Gtq4w/RtV1YGFL+6Irv/oXsRPBzYL3aS7i Smv89ht2 rhx0Z/w2LyLYsTABmygwCZNVJnPjxvR+UE9WwgcDwHOtGO34QtKIlTcot8LaZPpaw/BZvQtQ4hGHDXry66wO2Q93inaK2fQ1dVcQzyHcgPBQoz0+fSKJnZHTODBSS2X3uLLYXJ3WuWYl/Serje2rPBon+fHN+91afkqf6nCclhCeJcOX9GDqqFYlM3T8jfwZ7XgcwMkoVvh8EIWgD+jYtH/r3NYiaMaOtdpN4nLnOtb/RZJIQST0AUpj/117l9wT/Ny6kZkMbm4RSiiy7YULUQfb2Na52e+d6rlD/BwZTYBRaml/TjpY0MoSRsCwy5jPiv3PeRuccqeqma41Kz88yYkynoi4d9VjrqLJcEhuSbYJNUh6iNLiFFLs8RNm1O9IDuQ1L+EG+Wu2olxcK+8f3nPAInndV/y0f5k7beYut4RXMODOINOHSbMMOMICfI88QkDispzchQtrLZ48K+KsslPt4mG0BpE3ARQtCZ5/xjH3Wv1AsmUcgoBZC8hg9wqXFRO5OKKmeFZ2CS6Nr9qMdzX0JpylIUeJyrkzrc+/jFrFNi+FQ/hmDaSzTDlY/HTBH9dLqlNjkh9iYNFciq3FquWQswK/IFNv6+bIG9il4C9/Wdc96mC1xvWx573m50IYAhz1xPitn4kXzUZ7SZDYhf3AylIap8/LVKw/7Vy/nHR+pFxGCM8eQLGty5HUPLJ1SGOC/ew0MRp05dUXiuAVoNPBLbfddbRukGvG0yAgD0/KH+tfNc3JTDcu0ric7KfpoRO/cA/1GEIJPoorM9xWjPQVdUQ== 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: List-Subscribe: List-Unsubscribe: A global counter for the magnitude of memcg stats update is maintained on the memcg side to avoid invoking rstat flushes when the pending updates are not significant. This avoids unnecessary flushes, which are not very cheap even if there isn't a lot of stats to flush. It also avoids unnecessary lock contention on the underlying global rstat lock. Make this threshold per-memcg. The scheme is followed where percpu (now also per-memcg) counters are incremented in the update path, and only propagated to per-memcg atomics when they exceed a certain threshold. This provides two benefits: (a) On large machines with a lot of memcgs, the global threshold can be reached relatively fast, so guarding the underlying lock becomes less effective. Making the threshold per-memcg avoids this. (b) Having a global threshold makes it hard to do subtree flushes, as we cannot reset the global counter except for a full flush. Per-memcg counters removes this as a blocker from doing subtree flushes, which helps avoid unnecessary work when the stats of a small subtree are needed. Nothing is free, of course. This comes at a cost: (a) A new per-cpu counter per memcg, consuming NR_CPUS * NR_MEMCGS * 4 bytes. The extra memory usage is insigificant. (b) More work on the update side, although in the common case it will only be percpu counter updates. The amount of work scales with the number of ancestors (i.e. tree depth). This is not a new concept, adding a cgroup to the rstat tree involves a parent loop, so is charging. Testing results below show no significant regressions. (c) The error margin in the stats for the system as a whole increases from NR_CPUS * MEMCG_CHARGE_BATCH to NR_CPUS * MEMCG_CHARGE_BATCH * NR_MEMCGS. This is probably fine because we have a similar per-memcg error in charges coming from percpu stocks, and we have a periodic flusher that makes sure we always flush all the stats every 2s anyway. This patch was tested to make sure no significant regressions are introduced on the update path as follows. The following benchmarks were ran in a cgroup that is 2 levels deep (/sys/fs/cgroup/a/b/): (1) Running 22 instances of netperf on a 44 cpu machine with hyperthreading disabled. All instances are run in a level 2 cgroup, as well as netserver: # netserver -6 # netperf -6 -H ::1 -l 60 -t TCP_SENDFILE -- -m 10K Averaging 20 runs, the numbers are as follows: Base: 40198.0 mbps Patched: 38629.7 mbps (-3.9%) The regression is minimal, especially for 22 instances in the same cgroup sharing all ancestors (so updating the same atomics). (2) will-it-scale page_fault tests. These tests (specifically per_process_ops in page_fault3 test) detected a 25.9% regression before for a change in the stats update path [1]. These are the numbers from 10 runs (+ is good) on a machine with 256 cpus: LABEL | MEAN | MEDIAN | STDDEV | ------------------------------+-------------+-------------+------------- page_fault1_per_process_ops | | | | (A) base | 270249.164 | 265437.000 | 13451.836 | (B) patched | 261368.709 | 255725.000 | 13394.767 | | -3.29% | -3.66% | | page_fault1_per_thread_ops | | | | (A) base | 242111.345 | 239737.000 | 10026.031 | (B) patched | 237057.109 | 235305.000 | 9769.687 | | -2.09% | -1.85% | | page_fault1_scalability | | | (A) base | 0.034387 | 0.035168 | 0.0018283 | (B) patched | 0.033988 | 0.034573 | 0.0018056 | | -1.16% | -1.69% | | page_fault2_per_process_ops | | | (A) base | 203561.836 | 203301.000 | 2550.764 | (B) patched | 197195.945 | 197746.000 | 2264.263 | | -3.13% | -2.73% | | page_fault2_per_thread_ops | | | (A) base | 171046.473 | 170776.000 | 1509.679 | (B) patched | 166626.327 | 166406.000 | 768.753 | | -2.58% | -2.56% | | page_fault2_scalability | | | (A) base | 0.054026 | 0.053821 | 0.00062121 | (B) patched | 0.053329 | 0.05306 | 0.00048394 | | -1.29% | -1.41% | | page_fault3_per_process_ops | | | (A) base | 1295807.782 | 1297550.000 | 5907.585 | (B) patched | 1275579.873 | 1273359.000 | 8759.160 | | -1.56% | -1.86% | | page_fault3_per_thread_ops | | | (A) base | 391234.164 | 390860.000 | 1760.720 | (B) patched | 377231.273 | 376369.000 | 1874.971 | | -3.58% | -3.71% | | page_fault3_scalability | | | (A) base | 0.60369 | 0.60072 | 0.0083029 | (B) patched | 0.61733 | 0.61544 | 0.009855 | | +2.26% | +2.45% | | All regressions seem to be minimal, and within the normal variance for the benchmark. The fix for [1] assumes that 3% is noise -- and there were no further practical complaints), so hopefully this means that such variations in these microbenchmarks do not reflect on practical workloads. (3) I also ran stress-ng in a nested cgroup and did not observe any obvious regressions. [1]https://lore.kernel.org/all/20190520063534.GB19312@shao2-debian/ Suggested-by: Johannes Weiner Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo --- mm/memcontrol.c | 50 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 5ae2a8f04be45..74db05237775d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -630,6 +630,9 @@ struct memcg_vmstats_percpu { /* Cgroup1: threshold notifications & softlimit tree updates */ unsigned long nr_page_events; unsigned long targets[MEM_CGROUP_NTARGETS]; + + /* Stats updates since the last flush */ + unsigned int stats_updates; }; struct memcg_vmstats { @@ -644,6 +647,9 @@ struct memcg_vmstats { /* Pending child counts during tree propagation */ long state_pending[MEMCG_NR_STAT]; unsigned long events_pending[NR_MEMCG_EVENTS]; + + /* Stats updates since the last flush */ + atomic64_t stats_updates; }; /* @@ -663,9 +669,7 @@ struct memcg_vmstats { */ static void flush_memcg_stats_dwork(struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); -static DEFINE_PER_CPU(unsigned int, stats_updates); static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); -static atomic_t stats_flush_threshold = ATOMIC_INIT(0); static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -692,26 +696,37 @@ static void memcg_stats_unlock(void) preempt_enable_nested(); } + +static bool memcg_should_flush_stats(struct mem_cgroup *memcg) +{ + return atomic64_read(&memcg->vmstats->stats_updates) > + MEMCG_CHARGE_BATCH * num_online_cpus(); +} + static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) { + int cpu = smp_processor_id(); unsigned int x; if (!val) return; - cgroup_rstat_updated(memcg->css.cgroup, smp_processor_id()); + cgroup_rstat_updated(memcg->css.cgroup, cpu); + + for (; memcg; memcg = parent_mem_cgroup(memcg)) { + x = __this_cpu_add_return(memcg->vmstats_percpu->stats_updates, + abs(val)); + + if (x < MEMCG_CHARGE_BATCH) + continue; - x = __this_cpu_add_return(stats_updates, abs(val)); - if (x > MEMCG_CHARGE_BATCH) { /* - * If stats_flush_threshold exceeds the threshold - * (>num_online_cpus()), cgroup stats update will be triggered - * in __mem_cgroup_flush_stats(). Increasing this var further - * is redundant and simply adds overhead in atomic update. + * If @memcg is already flush-able, increasing stats_updates is + * redundant. Avoid the overhead of the atomic update. */ - if (atomic_read(&stats_flush_threshold) <= num_online_cpus()) - atomic_add(x / MEMCG_CHARGE_BATCH, &stats_flush_threshold); - __this_cpu_write(stats_updates, 0); + if (!memcg_should_flush_stats(memcg)) + atomic64_add(x, &memcg->vmstats->stats_updates); + __this_cpu_write(memcg->vmstats_percpu->stats_updates, 0); } } @@ -730,13 +745,12 @@ static void do_flush_stats(void) cgroup_rstat_flush(root_mem_cgroup->css.cgroup); - atomic_set(&stats_flush_threshold, 0); atomic_set(&stats_flush_ongoing, 0); } void mem_cgroup_flush_stats(void) { - if (atomic_read(&stats_flush_threshold) > num_online_cpus()) + if (memcg_should_flush_stats(root_mem_cgroup)) do_flush_stats(); } @@ -750,8 +764,8 @@ void mem_cgroup_flush_stats_ratelimited(void) static void flush_memcg_stats_dwork(struct work_struct *w) { /* - * Always flush here so that flushing in latency-sensitive paths is - * as cheap as possible. + * Deliberately ignore memcg_should_flush_stats() here so that flushing + * in latency-sensitive paths is as cheap as possible. */ do_flush_stats(); queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); @@ -5784,6 +5798,10 @@ static void mem_cgroup_css_rstat_flush(struct cgroup_subsys_state *css, int cpu) } } } + statc->stats_updates = 0; + /* We are in a per-cpu loop here, only do the atomic write once */ + if (atomic64_read(&memcg->vmstats->stats_updates)) + atomic64_set(&memcg->vmstats->stats_updates, 0); } #ifdef CONFIG_MMU From patchwork Thu Nov 16 02:24:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13457531 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 CF4ACC072A2 for ; Thu, 16 Nov 2023 02:24:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10B786B03E4; Wed, 15 Nov 2023 21:24:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 01EF86B03E6; Wed, 15 Nov 2023 21:24:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB5606B03E8; Wed, 15 Nov 2023 21:24:23 -0500 (EST) 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 BE42A6B03E4 for ; Wed, 15 Nov 2023 21:24:23 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9D0B912035C for ; Thu, 16 Nov 2023 02:24:23 +0000 (UTC) X-FDA: 81462223206.01.EC43A1D Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf15.hostedemail.com (Postfix) with ESMTP id E2E85A001B for ; Thu, 16 Nov 2023 02:24:21 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PqiSAEOI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3VX1VZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3VX1VZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700101461; 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=hOK1UyBshtkV5dW8DFB6ZrNswsHIuDTjDTN/Td8b6tw=; b=UHhqFjpaB/9argHwrQ1fcIHS5Y6guI31FKz7ja681bSVB1cxKViYB/LQeTG2FKrEkpgU4j lqXP5bEGLiwsB+6YWtax37KZH/j1MGRcml7gB6bfgX4EMKLwiIIrqYXVq7s4LFM8tXKtRV HOB//tvMqjPKwMAcftaEQV9G/If6kqs= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PqiSAEOI; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3VX1VZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3VX1VZQoKCNYQGKJQ29E658GG8D6.4GEDAFMP-EECN24C.GJ8@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700101461; a=rsa-sha256; cv=none; b=5ZM628Ef9jv9CMQxPui0VHQ04FZztIfTn4gUy9Y4YbIDY1p5VvW2tvXLY+zkiYZpzcqbF+ dDzeVNL5ZwUzU72p6eqOw9TyUV2JVDyJh73Ix/wtZ1Obd9RiT5eZ8fvun74DG7eOLkkjzV 7FoQ5txigLcE5LOsyJWBoUtSdnaOi14= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5b59662ff67so12690827b3.0 for ; Wed, 15 Nov 2023 18:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700101461; x=1700706261; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hOK1UyBshtkV5dW8DFB6ZrNswsHIuDTjDTN/Td8b6tw=; b=PqiSAEOIVgg45ICYwB1sjTBd/BJhJllQ0+irGCVRQgCTE17qd1GNcDfKZr3POUmbqP U6J7W0sPBl+u0rs4tDJJwq8oVyqVr/lhL8q6CLo5JlOmBFpffmoa5bsd8laGWnjtPU3E 7CVzew83aCpADZaGsb6Zu01wew2MQH0hqNu/Je21o/mWtgd9h8/+sEt9K8u5D9XPQg/W PDDwDEBsNGn7KZOZB7MgXC9uQQKOO37NZVSziURQasqny6288m9WXuttKJp4Y2NgzNpL EVQN4Uvv2iCNMlJW9B/i7+X8T0BiSFFvWg2Hnhdv9LEPjhiBj6Jrv5FBwq+simcpYB0F 1IpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700101461; x=1700706261; 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=hOK1UyBshtkV5dW8DFB6ZrNswsHIuDTjDTN/Td8b6tw=; b=op+WHveIoEzefFdG9Mpq9I+W9BWKmX3aSGbJDcXnX6J2UgBPcwDC1gw+7s7rK/h9KR Bnoc2MAo0NryIA3qT/+VFk7gvWC5lJ4VM0xHaqYnnhABEt8GXoyi3x1oEFEBevXMfRh7 alBPua8NmWrVGyqeAEL06eUjhXFCT6lco1veexI7V392CFZgcga27nl7xXkNwW8oKuTz NVk9IEbbp2/zaWhYq9AUUFfeY23jVWNNPx139HF3As3aIR15Tfbc776xDPZEoKaVrOg9 Utzk0v8e44fk36PtqNkx2WtEXbKWIFU6em0/T/nWoyTcfNZCVEeo/fF5bzZk6MYeIwF5 1TYw== X-Gm-Message-State: AOJu0Ywjho6wBq4c/N4v3ge4RYcOuRG9UDDViTUoekumRxXFcBup+P3W JwrVxpU1w7f2vi1KdAYEWfcAOHIFTCNRC/DW X-Google-Smtp-Source: AGHT+IEQtgXmf9iSCqMERLu3YFDRxYLIfFN9r8bkSUo7k+VLrtBcNBEUIUl5/rQbpV2uMfGCYzqdDt8J6V5tLAR0 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:d716:0:b0:5ae:75db:92c5 with SMTP id z22-20020a0dd716000000b005ae75db92c5mr12091ywd.2.1700101461069; Wed, 15 Nov 2023 18:24:21 -0800 (PST) Date: Thu, 16 Nov 2023 02:24:09 +0000 In-Reply-To: <20231116022411.2250072-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231116022411.2250072-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231116022411.2250072-5-yosryahmed@google.com> Subject: [PATCH v3 4/5] mm: workingset: move the stats flush into workingset_test_recent() From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E2E85A001B X-Stat-Signature: zjeq9ie1fxrn39j5k7mrzyubousg1suo X-HE-Tag: 1700101461-46699 X-HE-Meta: U2FsdGVkX1+SgxouWWVCC1yxEeyvKpyWE7hK3qzprDzHlDuTUAtxMo5wcqH3br9UgnnA9uwTR/+9LPOLf6CLA8wX8Kcj8NJCbR6ysqSAjQkHE87tja8/AHFuSaPzZpC80Dm8BwxsMXKbtJXfvaFyjcJD5+U+SZOKr8yaDX7i00+LwER37Antr68rI9nZ5edYGTU/n6am73QGARnox/nto07ZNguQp1/Soc55KN9mV/gT5FJ1ZFvOQKtfSVFoiIvZ2vOla8W2p8jOjzJLE4ZmRVBboBpGW8QljidL+T1BJK9FxeCFvcgEjU8dKk+HrbEe1qNwzimaGjGlGwxWi0IywpHtYWS9TLPxvPgzxKhOKGOFmc9824sq2BzQ4gQ9pfM3qkh6AjwZwMMfBh86OEXsFPuj54ZUCz2skvimIF9yM+Wond5DHCcQzGf9RRBQ8UNyHMCnJvOg6bWXhpW+UBjF7FKTTapyvoYs4aHljT18jGDVLKWcczxMENAXa86b0xJG9vCk4T2PJ7UIi5Glkxg4KgN0Ah7P+oyuwboeFMjcYAA8dIkiYwm/eVLLhiOA8Ck06vZc+mLaL9KSQEfiZ83rM1oyQMxoj6XDd4a5h4jmIZzulZJzwpVf2rCgiCzfQyZG11QCtKdJ5nGhHpqRGA353n/aYVy004oguU5yqB/GHv6TzuTgMmYOPfEKfwrx4vJSxe1oVRzhmwyUlUz5vtMNSdHMGPz9w541keMNpfalUQWbA198EBoFxPT8dutFhoKWRjofMy91h0ZC/o7o2ep55cNX/G8ts40yMKqSpMf3yRvr8eOaI0tbNADj/8XN10I8sVJ5d4b3vxhZ8x2epN4SfFy2itk6ogVDVzjThh2MchhFqRQvxFhvEqq+4P4/+OwaSqs8G0Naw9MYCAyMvzdgwdQrunqy3s63ZUhvf4dgLkoxRwpxtqLn9Wx74eFUN+Ug93jNTVX/b8xJFNMvPdH yOW/wL3I fG/XpOWlUeyywMfUR+3l/PcFGlW195ShaD4zNUX/0zZAO0ez5vFCQ1PwDVdcn6TLlUjqBWRVGEvAOlmxL5BybkDeR0ie6+fV5YqN40T272vN34ahTseuXyl2BP8y9WeUjoFs/NS2SUq7IyBwYQUC5dp6xsxjPdtdnD9CtLgxGeON5MfoH0kVXwbaVUVJWSPa/5xHje3ZxhFSwPORQkc3l243sx5SXNMTRctjIE8id5JRjdpwpFAq9udfh+HDCjffQN7/1AumRLEC3+wRqlpX4dhJsbHJoW9m5kkZMkrlvfXY1tP4VZ6dUcB/kXFxYW7Gj5xfyNp3B4b/R5L4pwStN6jrpFAYEERIV+AXyQaw76EJzEaWq011Dqmo8I8jzn8ClEN+km7sNN5OLy5K2MxvUt6M1Tc0TWPJ9V+TPMU9uSn9DHIJQf/3o8PqBueeKL7NswGYi2wTrvFgE8SUvTFdm0QjC3tIEtm4fXg4cwvUZNAZUdGmNXBKyk8rsRhJwiWtpEhYFSlo+xRMals2e+ohUaoqYcRb4Xi7l66vk88ZsIXlmLAZFpb7RQMITIi9/q3hw0A9LyEf3FhtUFurN3TfB+YK3ZEWSoVfKjHMaKxcb7mL+b9PXsSTe2e9Xuh2xVfiWmZQHJSsXtO/vfATvbLprRpQ3jV8GmZLHjglsGZ4Idbm++GKUJHmAmOI73vlWnc0/8gbJmG+2Y3Upr1Qtgt1dmot3gQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.031912, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The workingset code flushes the stats in workingset_refault() to get accurate stats of the eviction memcg. In preparation for more scoped flushed and passing the eviction memcg to the flush call, move the call to workingset_test_recent() where we have a pointer to the eviction memcg. The flush call is sleepable, and cannot be made in an rcu read section. Hence, minimize the rcu read section by also moving it into workingset_test_recent(). Furthermore, instead of holding the rcu read lock throughout workingset_test_recent(), only hold it briefly to get a ref on the eviction memcg. This allows us to make the flush call after we get the eviction memcg. As for workingset_refault(), nothing else there appears to be protected by rcu. The memcg of the faulted folio (which is not necessarily the same as the eviction memcg) is protected by the folio lock, which is held from all callsites. Add a VM_BUG_ON() to make sure this doesn't change from under us. No functional change intended. Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo --- mm/workingset.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index b192e44a0e7cc..a573be6c59fd9 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -425,8 +425,16 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) struct pglist_data *pgdat; unsigned long eviction; - if (lru_gen_enabled()) - return lru_gen_test_recent(shadow, file, &eviction_lruvec, &eviction, workingset); + rcu_read_lock(); + + if (lru_gen_enabled()) { + bool recent = lru_gen_test_recent(shadow, file, + &eviction_lruvec, &eviction, workingset); + + rcu_read_unlock(); + return recent; + } + unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<= bucket_order; @@ -448,8 +456,16 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) * configurations instead. */ eviction_memcg = mem_cgroup_from_id(memcgid); - if (!mem_cgroup_disabled() && !eviction_memcg) + if (!mem_cgroup_disabled() && + (!eviction_memcg || !mem_cgroup_tryget(eviction_memcg))) { + rcu_read_unlock(); return false; + } + + rcu_read_unlock(); + + /* Flush stats (and potentially sleep) outside the RCU read section */ + mem_cgroup_flush_stats_ratelimited(); eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -493,6 +509,7 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) } } + mem_cgroup_put(eviction_memcg); return refault_distance <= workingset_size; } @@ -519,19 +536,16 @@ void workingset_refault(struct folio *folio, void *shadow) return; } - /* Flush stats (and potentially sleep) before holding RCU read lock */ - mem_cgroup_flush_stats_ratelimited(); - - rcu_read_lock(); - /* * The activation decision for this folio is made at the level * where the eviction occurred, as that is where the LRU order * during folio reclaim is being determined. * * However, the cgroup that will own the folio is the one that - * is actually experiencing the refault event. + * is actually experiencing the refault event. Make sure the folio is + * locked to guarantee folio_memcg() stability throughout. */ + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); nr = folio_nr_pages(folio); memcg = folio_memcg(folio); pgdat = folio_pgdat(folio); @@ -540,7 +554,7 @@ void workingset_refault(struct folio *folio, void *shadow) mod_lruvec_state(lruvec, WORKINGSET_REFAULT_BASE + file, nr); if (!workingset_test_recent(shadow, file, &workingset)) - goto out; + return; folio_set_active(folio); workingset_age_nonresident(lruvec, nr); @@ -556,8 +570,6 @@ void workingset_refault(struct folio *folio, void *shadow) lru_note_cost_refault(folio); mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + file, nr); } -out: - rcu_read_unlock(); } /** From patchwork Thu Nov 16 02:24:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13457532 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 36C61C5ACB3 for ; Thu, 16 Nov 2023 02:24:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1FDC6B03E6; Wed, 15 Nov 2023 21:24:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AD1476B03EA; Wed, 15 Nov 2023 21:24:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 947686B03EC; Wed, 15 Nov 2023 21:24:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7CF4C6B03E6 for ; Wed, 15 Nov 2023 21:24:25 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5525E14038C for ; Thu, 16 Nov 2023 02:24:25 +0000 (UTC) X-FDA: 81462223290.23.2FB5CDB Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf23.hostedemail.com (Postfix) with ESMTP id 8AF25140018 for ; Thu, 16 Nov 2023 02:24:23 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=lz88PiZw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3Vn1VZQoKCNcRHLKR3AF769HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Vn1VZQoKCNcRHLKR3AF769HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700101463; 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=sYfIbMCcjOcaMk8XkEVZkKdIKTJOO/34lFccln3zlF4=; b=CUTcfT15iOk1GlRxWhCB9ShiypV+2brRefU7CyB5eVDGwBI76uUdclSnl1ChJfmum4XI6q P+6ZUAu3ffSWjqfAkw7F2DwW+9uQ35kpz9xUOm2ZTbIV142UJiQImB81VQqjPZ/ciQyClM QkBz9L9+BoQQXKU0G5p403DnTVqFp+Q= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=lz88PiZw; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of 3Vn1VZQoKCNcRHLKR3AF769HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--yosryahmed.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Vn1VZQoKCNcRHLKR3AF769HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700101463; a=rsa-sha256; cv=none; b=PYh+UK0P69cGOwmQUrDxwN73eLCRh2AVqCdC20DtAjpuV5KMM9q5rugdCwjDbpGbGOcLQT mCJ6t9y50zEJwg3nOGfXKUwTck+ryZq5y5BNkAP3d4+Ajm0n1S1YNtuw4VdlUZdmpdex2v 1+mlrMo1LcGcrfAMmOQDR4++pWaLjLI= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5b59662ff67so12690977b3.0 for ; Wed, 15 Nov 2023 18:24:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700101462; x=1700706262; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sYfIbMCcjOcaMk8XkEVZkKdIKTJOO/34lFccln3zlF4=; b=lz88PiZw8AguqGicxWpt+9anilatNRt0YLm4VT1+YIagUPh8wo+0O8Ip5XZHh6KVcY pgKBkuw0gzcXTzxM/x//YBbh0RPX/hS4TMzCITDrpGzom47ZYmWCFB9K67SVyMqXfpcr sPqGZVYelX6AUqcCRwzipkKHoIWUjnsgQW0IsLQXVOzJglRmOilzvgw2VelODcN5i1rP B8Rf8W/FXmjNN+v0jc/nopfVWZrZ/9Z1vCjDTibEB4T6QoiArn+ry62T2ZHWywlUhKk7 jbAOXoT7Zi2yAT8NlUaWYnHae/apYFHaMSdQVAbT3borVc9/U/mZ7RzupGIjX/cycCfA YcCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700101462; x=1700706262; 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=sYfIbMCcjOcaMk8XkEVZkKdIKTJOO/34lFccln3zlF4=; b=ZFtHjzLiMltIKF+Gqks8kjvRo+NEa/zx+bwNYGEvEL5+aPVIHOP6S39ChREvL5CIPV gi9EVRxtHggzPHqCK09cbHPFCE4seYQGWTCAaKuKu2YJQIHD3qrXtYD3krjryRSw5oOJ Yppclx3hGm98XDVffGKrvzokQXgbPs39GNna4yMKg50ZybtPNMe4Sr9bxHhN+UUUpiS9 pIsqaa7ENzFNt7CJMwRRm3fRbjAsgSY8U/nSNrw6pbPiptr2KhGRzxllnUJPCQatDILM heU7n/zrQ7oRa+1NjsGcxyDbFeHnnbz3WEbNaMm0mH9mmPENFed6sPjT+OMx6a8uh6pR q61A== X-Gm-Message-State: AOJu0YxtH6wxpmsxWEJef15K1benF/5kCCZ6ptM3Nic7U2TAjexoSvfu Cj483HTsZu971zP6o80Enn4P+OvW7yolDTqO X-Google-Smtp-Source: AGHT+IHOmB/fbH5s+WhrMo6La77w650Dx8voIzjIjTB3e9zO2veYErgpI8gmC3H0bM7gCxpZ7clqVyysog00UpE/ X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a25:ac9b:0:b0:d9a:4cc1:b59a with SMTP id x27-20020a25ac9b000000b00d9a4cc1b59amr11173ybi.1.1700101462675; Wed, 15 Nov 2023 18:24:22 -0800 (PST) Date: Thu, 16 Nov 2023 02:24:10 +0000 In-Reply-To: <20231116022411.2250072-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231116022411.2250072-1-yosryahmed@google.com> X-Mailer: git-send-email 2.43.0.rc0.421.g78406f8d94-goog Message-ID: <20231116022411.2250072-6-yosryahmed@google.com> Subject: [PATCH v3 5/5] mm: memcg: restore subtree stats flushing From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Ivan Babrou , Tejun Heo , " =?utf-8?q?Michal_Koutn=C3=BD?= " , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , Domenico Cerasuolo , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Queue-Id: 8AF25140018 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: hfwfbswuwex4ottqmkwcka5bz31oauzs X-HE-Tag: 1700101463-297524 X-HE-Meta: U2FsdGVkX1+Py0RzgfvaGWuw7zQbVjO3LQ/jCLVj+06xq6DZ6h14hdkbf1tNS9tDiKuDqfKjD0CIDGm6zInuT1z9Ts4Hj1y3JcNZEHhWNBlETaiCZGbvv5tluedqwYrpmOpkGecAYV9/10o5VL05PTPTm3ivAUSKIk3kiLmBoksLYxdfySZtotvOAVd3qT5NhRYq4XLUFbeIITz4+2s4vQC6bKbYFP0KHKMhyqL8iQa2GOtf4GGQp26HaK0Enbm59IbU+NhutkETtlkfLIzbCxcufCFT9sbTrPPd2LO+XTk8twtbxqjK/dSVtGe2nLycrq++8TRb6PL5dEWUPbxV1MeXSX7YG41cxRrypp/HwqJ6G1wWW3ABObd4VT7M1GxwzQLzeLtjehgJXXlFtosVdiRNVq44nNmqMn0ukufjE14FyMDby5ymhcTCqyqakVqToiIm8MHEtauVDEURw2O2edSxzXPGTWdAqa+fLV8rxV+VsSScgB2fCmlg2X+K8PwxCuYC1+0bI96wvkUEOfRt9FOqIKIgfXM5WlstVmK+DsUoPceKFsXKdUq9P9Wb70Se4ERpa340jruZq/EzxXFP7LoYCfb+N9/Br5nmBpWLqv0QuWDdl9aax5gqf9kAUNFU9CaouHeb04Ep0qkAsz00KOq4EmH4YaALvqmKUsDoDGuHHczP9WMWma3rGCyrxK1DKZ02Ylj94QVi2MiYxW7wt1ZIZ01TIrn+13ipa9VGNfC3L2JtgRiDpFazZqb+iZN+qxQK7EFtmgWRUUGDu+BwG2vY51q+XNqBRuOhR7MZK4+36G895Ved31tUPCWlBWqzNqFnNiT6bW8RN3gVnS+prr/hR8cYqyKUjiqRjvIrnnXjBICyDjEUgJyBZ6/0Lyrq/RvyouNdutBobRxOxr7nvNx/NTKbbPDZLfcm0kEk/UHOp1patLi0KXwKG3WlJ/PH5pLgGdb57ZJCE/P9zHG Zw//Rx6N 5CuhIHCTC056xHGUQQrS/DKfpiAhW+guahxfYpklcExX/iU7LVhpfj4t9XHCeT13oTVnK6UK4toM/cJ/5HZGSS4jusoLZnIcRN6g8cC0UMWHMGvsfvxoqyrXV83HFW2Pk4CEwoUvdexyFx9CSl9nlNGACQm5bV/XwY9JARdfR72pq5se++TjDIbGtwgRlr24A27NzFNxFXK5/az/xdh/DiyxNJ6jXFdGnRxuJJx1dqmEJP4NoHeOglIr6vKn/HuzNvVav+NNhmVVkhJCzs6edaVKLLV1O4krJkjLWS/v62OwjdAt7vnSTSOl3NOMjFwRbLi/pxNgtTSceTQPR67RFLTe3WETvA+3yQRV7AOx7xe4HIYjwF9Bs4IOdyz87EHykRHdgYzMTatvNiXuOwLSH0W3RDV6Yqco3vMFV01eU6Az/AdJ4Dc+1uxdPXOdaMZA73k5Y4jib/eXpo/iWoIRsi2aohrShmEhLFKaM44rmN+PT6zXp6KSel3WcO1Y03aKuS9GIHjfNSo9yPSdmz/u/BvvO3gW15O2hkajh5UVBm1aXOHZs7D+KU3T09lUv7pbm5xHFcx/kSZ4meOTal6P03SM2/UxdCD+thcaR6rKdGXrhNypPRK6hVN+pzdL0df8Gutf0Mv4wcmjHTY0M3mJQaMnZCB2O2jxivHq9O4fTUm+9+KGxTOYZNCGrFX2QMlu6UnbjjSXK+YieKluXeLcOJaBCFHUPbO5CXt63 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: List-Subscribe: List-Unsubscribe: Stats flushing for memcg currently follows the following rules: - Always flush the entire memcg hierarchy (i.e. flush the root). - Only one flusher is allowed at a time. If someone else tries to flush concurrently, they skip and return immediately. - A periodic flusher flushes all the stats every 2 seconds. The reason this approach is followed is because all flushes are serialized by a global rstat spinlock. On the memcg side, flushing is invoked from userspace reads as well as in-kernel flushers (e.g. reclaim, refault, etc). This approach aims to avoid serializing all flushers on the global lock, which can cause a significant performance hit under high concurrency. This approach has the following problems: - Occasionally a userspace read of the stats of a non-root cgroup will be too expensive as it has to flush the entire hierarchy [1]. - Sometimes the stats accuracy are compromised if there is an ongoing flush, and we skip and return before the subtree of interest is actually flushed, yielding stale stats (by up to 2s due to periodic flushing). This is more visible when reading stats from userspace, but can also affect in-kernel flushers. The latter problem is particulary a concern when userspace reads stats after an event occurs, but gets stats from before the event. Examples: - When memory usage / pressure spikes, a userspace OOM handler may look at the stats of different memcgs to select a victim based on various heuristics (e.g. how much private memory will be freed by killing this). Reading stale stats from before the usage spike in this case may cause a wrongful OOM kill. - A proactive reclaimer may read the stats after writing to memory.reclaim to measure the success of the reclaim operation. Stale stats from before reclaim may give a false negative. - Reading the stats of a parent and a child memcg may be inconsistent (child larger than parent), if the flush doesn't happen when the parent is read, but happens when the child is read. As for in-kernel flushers, they will occasionally get stale stats. No regressions are currently known from this, but if there are regressions, they would be very difficult to debug and link to the source of the problem. This patch aims to fix these problems by restoring subtree flushing, and removing the unified/coalesced flushing logic that skips flushing if there is an ongoing flush. This change would introduce a significant regression with global stats flushing thresholds. With per-memcg stats flushing thresholds, this seems to perform really well. The thresholds protect the underlying lock from unnecessary contention. Add a mutex to protect the underlying rstat lock from excessive memcg flushing. The thresholds are re-checked after the mutex is grabbed to make sure that a concurrent flush did not already get the subtree we are trying to flush. A call to cgroup_rstat_flush() is not cheap, even if there are no pending updates. This patch was tested in two ways to ensure the latency of flushing is up to bar, on a machine with 384 cpus: - A synthetic test with 5000 concurrent workers in 500 cgroups doing allocations and reclaim, as well as 1000 readers for memory.stat (variation of [2]). No regressions were noticed in the total runtime. Note that significant regressions in this test are observed with global stats thresholds, but not with per-memcg thresholds. - A synthetic stress test for concurrently reading memcg stats while memory allocation/freeing workers are running in the background, provided by Wei Xu [3]. With 250k threads reading the stats every 100ms in 50k cgroups, 99.9% of reads take <= 50us. Less than 0.01% of reads take more than 1ms, and no reads take more than 100ms. [1] https://lore.kernel.org/lkml/CABWYdi0c6__rh-K7dcM_pkf9BJdTRtAU08M43KO9ME4-dsgfoQ@mail.gmail.com/ [2] https://lore.kernel.org/lkml/CAJD7tka13M-zVZTyQJYL1iUAYvuQ1fcHbCjcOBZcz6POYTV-4g@mail.gmail.com/ [3] https://lore.kernel.org/lkml/CAAPL-u9D2b=iF5Lf_cRnKxUfkiEe0AMDTu6yhrUAzX0b6a6rDg@mail.gmail.com/ Signed-off-by: Yosry Ahmed Tested-by: Domenico Cerasuolo --- include/linux/memcontrol.h | 8 ++-- mm/memcontrol.c | 75 +++++++++++++++++++++++--------------- mm/vmscan.c | 2 +- mm/workingset.c | 10 +++-- 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 7bdcf3020d7a3..6edd3ec4d8d54 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1046,8 +1046,8 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, return x; } -void mem_cgroup_flush_stats(void); -void mem_cgroup_flush_stats_ratelimited(void); +void mem_cgroup_flush_stats(struct mem_cgroup *memcg); +void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg); void __mod_memcg_lruvec_state(struct lruvec *lruvec, enum node_stat_item idx, int val); @@ -1548,11 +1548,11 @@ static inline unsigned long lruvec_page_state_local(struct lruvec *lruvec, return node_page_state(lruvec_pgdat(lruvec), idx); } -static inline void mem_cgroup_flush_stats(void) +static inline void mem_cgroup_flush_stats(struct mem_cgroup *memcg) { } -static inline void mem_cgroup_flush_stats_ratelimited(void) +static inline void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) { } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 74db05237775d..2baa9349d1590 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -669,7 +669,6 @@ struct memcg_vmstats { */ static void flush_memcg_stats_dwork(struct work_struct *w); static DECLARE_DEFERRABLE_WORK(stats_flush_dwork, flush_memcg_stats_dwork); -static atomic_t stats_flush_ongoing = ATOMIC_INIT(0); static u64 flush_last_time; #define FLUSH_TIME (2UL*HZ) @@ -730,35 +729,47 @@ static inline void memcg_rstat_updated(struct mem_cgroup *memcg, int val) } } -static void do_flush_stats(void) +static void do_flush_stats(struct mem_cgroup *memcg) { - /* - * We always flush the entire tree, so concurrent flushers can just - * skip. This avoids a thundering herd problem on the rstat global lock - * from memcg flushers (e.g. reclaim, refault, etc). - */ - if (atomic_read(&stats_flush_ongoing) || - atomic_xchg(&stats_flush_ongoing, 1)) - return; - - WRITE_ONCE(flush_last_time, jiffies_64); - - cgroup_rstat_flush(root_mem_cgroup->css.cgroup); + if (mem_cgroup_is_root(memcg)) + WRITE_ONCE(flush_last_time, jiffies_64); - atomic_set(&stats_flush_ongoing, 0); + cgroup_rstat_flush(memcg->css.cgroup); } -void mem_cgroup_flush_stats(void) +/* + * mem_cgroup_flush_stats - flush the stats of a memory cgroup subtree + * @memcg: root of the subtree to flush + * + * Flushing is serialized by the underlying global rstat lock. There is also a + * minimum amount of work to be done even if there are no stat updates to flush. + * Hence, we only flush the stats if the updates delta exceeds a threshold. This + * avoids unnecessary work and contention on the underlying lock. + */ +void mem_cgroup_flush_stats(struct mem_cgroup *memcg) { - if (memcg_should_flush_stats(root_mem_cgroup)) - do_flush_stats(); + static DEFINE_MUTEX(memcg_stats_flush_mutex); + + if (mem_cgroup_disabled()) + return; + + if (!memcg) + memcg = root_mem_cgroup; + + if (memcg_should_flush_stats(memcg)) { + mutex_lock(&memcg_stats_flush_mutex); + /* Check again after locking, another flush may have occurred */ + if (memcg_should_flush_stats(memcg)) + do_flush_stats(memcg); + mutex_unlock(&memcg_stats_flush_mutex); + } } -void mem_cgroup_flush_stats_ratelimited(void) +void mem_cgroup_flush_stats_ratelimited(struct mem_cgroup *memcg) { /* Only flush if the periodic flusher is one full cycle late */ if (time_after64(jiffies_64, READ_ONCE(flush_last_time) + 2*FLUSH_TIME)) - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); } static void flush_memcg_stats_dwork(struct work_struct *w) @@ -767,7 +778,7 @@ static void flush_memcg_stats_dwork(struct work_struct *w) * Deliberately ignore memcg_should_flush_stats() here so that flushing * in latency-sensitive paths is as cheap as possible. */ - do_flush_stats(); + do_flush_stats(root_mem_cgroup); queue_delayed_work(system_unbound_wq, &stats_flush_dwork, FLUSH_TIME); } @@ -1642,7 +1653,7 @@ static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) * * Current memory state: */ - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { u64 size; @@ -4191,7 +4202,7 @@ static int memcg_numa_stat_show(struct seq_file *m, void *v) int nid; struct mem_cgroup *memcg = mem_cgroup_from_seq(m); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (stat = stats; stat < stats + ARRAY_SIZE(stats); stat++) { seq_printf(m, "%s=%lu", stat->name, @@ -4272,7 +4283,7 @@ static void memcg1_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) BUILD_BUG_ON(ARRAY_SIZE(memcg1_stat_names) != ARRAY_SIZE(memcg1_stats)); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memcg1_stats); i++) { unsigned long nr; @@ -4768,7 +4779,7 @@ void mem_cgroup_wb_stats(struct bdi_writeback *wb, unsigned long *pfilepages, struct mem_cgroup *memcg = mem_cgroup_from_css(wb->memcg_css); struct mem_cgroup *parent; - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); *pdirty = memcg_page_state(memcg, NR_FILE_DIRTY); *pwriteback = memcg_page_state(memcg, NR_WRITEBACK); @@ -6857,7 +6868,7 @@ static int memory_numa_stat_show(struct seq_file *m, void *v) int i; struct mem_cgroup *memcg = mem_cgroup_from_seq(m); - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(memcg); for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { int nid; @@ -8088,7 +8099,11 @@ bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) break; } - cgroup_rstat_flush(memcg->css.cgroup); + /* + * mem_cgroup_flush_stats() ignores small changes. Use + * do_flush_stats() directly to get accurate stats for charging. + */ + do_flush_stats(memcg); pages = memcg_page_state(memcg, MEMCG_ZSWAP_B) / PAGE_SIZE; if (pages < max) continue; @@ -8153,8 +8168,10 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) static u64 zswap_current_read(struct cgroup_subsys_state *css, struct cftype *cft) { - cgroup_rstat_flush(css->cgroup); - return memcg_page_state(mem_cgroup_from_css(css), MEMCG_ZSWAP_B); + struct mem_cgroup *memcg = mem_cgroup_from_css(css); + + mem_cgroup_flush_stats(memcg); + return memcg_page_state(memcg, MEMCG_ZSWAP_B); } static int zswap_max_show(struct seq_file *m, void *v) diff --git a/mm/vmscan.c b/mm/vmscan.c index 506f8220c5fe5..f93c989d7b387 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2222,7 +2222,7 @@ static void prepare_scan_control(pg_data_t *pgdat, struct scan_control *sc) * Flush the memory cgroup stats, so that we read accurate per-memcg * lruvec stats for heuristics. */ - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(sc->target_mem_cgroup); /* * Determine the scan balance between anon and file LRUs. diff --git a/mm/workingset.c b/mm/workingset.c index a573be6c59fd9..11045febc3838 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -464,8 +464,12 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset) rcu_read_unlock(); - /* Flush stats (and potentially sleep) outside the RCU read section */ - mem_cgroup_flush_stats_ratelimited(); + /* + * Flush stats (and potentially sleep) outside the RCU read section. + * XXX: With per-memcg flushing and thresholding, is ratelimiting + * still needed here? + */ + mem_cgroup_flush_stats_ratelimited(eviction_memcg); eviction_lruvec = mem_cgroup_lruvec(eviction_memcg, pgdat); refault = atomic_long_read(&eviction_lruvec->nonresident_age); @@ -676,7 +680,7 @@ static unsigned long count_shadow_nodes(struct shrinker *shrinker, struct lruvec *lruvec; int i; - mem_cgroup_flush_stats(); + mem_cgroup_flush_stats(sc->memcg); lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); for (pages = 0, i = 0; i < NR_LRU_LISTS; i++) pages += lruvec_page_state_local(lruvec,