Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp2264579rdb; Mon, 9 Oct 2023 20:21:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFaeZlXrnfKf2JYT0mCjvnxjBauRsQyA7XJoXPbBRzWH54eHBZjdsNY4R5O3NV4E/Ty+oe2 X-Received: by 2002:a05:6830:1bc7:b0:6bd:c8c2:b70f with SMTP id v7-20020a0568301bc700b006bdc8c2b70fmr17026599ota.34.1696908112966; Mon, 09 Oct 2023 20:21:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696908112; cv=none; d=google.com; s=arc-20160816; b=S7uEeIGIZV4E43QoSR+fbv4UKpKZ/fcsfJhcelqp9IcDFoBWrXPmlu7XopASbcM+vG a2DhJZ+wLS717IQjQsYiYhSvLfquEyky5MMpThXnVnUm35gb5y9qH/svkpegDr2VtN4e Z9nu/qNPvFdfalW1+i8E25VSmyv3XLWNxMpAiCyc5UtPXpos4VVlXZAviOClGgfQ3qa3 Kd7U4dhZpvAL1YhnS7poIghBVsYLVXAyXyGyLjxGbXWYrH4vsAJs7XzSiXdV+w59OyW8 txOQuvTrJiiMZZSYrV5JGk4buLfHOzI+UG8oHOezN4rJ0bDJEugB0RAwi90stDLqdVHL 3DbA== 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=2uIMTmtGdqUKunPgdDdu073V0kaqxVUtvtbHday0wrc=; fh=i6r7D8Tbv5L0V2RUiJXfGU+sNI1eFV0UjCNL0oNyQ80=; b=JvUfXB/KOemAy1lSiD8ecXuR/KLVZwiGBXvXQsw8F7W51UtZkI41LUsrRXMj8czUI/ hC4XayABp9qqxS9vhCgCOwbc8kIR4ry9huWqvauIkJ4P0lGlsGlh76rhUm3agYphySZJ 51YzZXi43YE9pKMs9WleY105LrpLK5to9MCTy6KTXdJ38+FaZNK1yE8j81gAXxZnRN9X xhtonfdthKoEK/C2KnayqBrHk8yoEHfB+7UBLUNXNopk+9oxVZDjv0zTzX0sb+jz4LEA UTF56WqcnnQi+E4cXxiTquscM8LCjjN4yVasTi6LcF6LbZQMi163+R6SzZ9h1dxhFiJN tZ5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Wlih9CQg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id b9-20020a63d809000000b0058989aa1a1fsi8658240pgh.758.2023.10.09.20.21.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 20:21:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Wlih9CQg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A6E6D810C2F6; Mon, 9 Oct 2023 20:21:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441982AbjJJDVk (ORCPT + 99 others); Mon, 9 Oct 2023 23:21:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441979AbjJJDVb (ORCPT ); Mon, 9 Oct 2023 23:21:31 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E168B0 for ; Mon, 9 Oct 2023 20:21:28 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a21c283542so89924587b3.3 for ; Mon, 09 Oct 2023 20:21:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696908087; x=1697512887; darn=vger.kernel.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=2uIMTmtGdqUKunPgdDdu073V0kaqxVUtvtbHday0wrc=; b=Wlih9CQgTOSvCKOUzBNw+I6e5fdxI5H5mgw3wf4O4qOlCsNqIz8xShIwVneez9K8RU 74SQf01um+Dyv4V+JsZUgP72iakse/Girf5eZ3AyQAqUCgZYYWOyZ1pLcDzl5H9e3N7d qlDN3G1NI7pyW1HNBmelKPSYtNtXbUGTE9LNIhOS2TQ86BPdXUrc2UCTOuY4EzjFyQVg c+dFBYyoZ1/juzjX7gfMx3UHqGyVGLE7nPI2xNmZm0qkCJ70i0JTNjbWwLIGLNbRU940 QBg6TsdP29BH15jEjNz3jejJYb0BmZK/swihy6HmGBlS0XzZU4tPqhtXM7ZSMQUHhexa Vz7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696908087; x=1697512887; 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=2uIMTmtGdqUKunPgdDdu073V0kaqxVUtvtbHday0wrc=; b=CMUQmCsmNvvSFeaer6R6rVkJHs71eKuWtyfRqSJgLfXvq6LrE2jYP4000MACX4vcSN jzLjaRmUGLGOe6sWY1tAbkD32myLoToA32vqKq7W4ugZST9cslXH7ThFBVuyF1Vk7ku7 iHMEEcjRVoTNDEYn0VliVw3J+1meC5A4IrJ+TWDAhhAmcJsnjUlbMV9oyxJ7LKJx43b0 NvT5BrsspUtWGKBbCGeqiajVwx22WnsH9MdzgBlMnK9rA7tNtMl4f4pUj2tozEQlHI1H di1HZO0ZJuRfXvZOtMuwumpMArR2hvKn7zUBupoFpwYdkWQTSvjHLHnj04Pa3O85wtYY IdiA== X-Gm-Message-State: AOJu0YzWOg+yYKaFbTlNS4fRww62IReuGplLfsVRSlFga0+lrk5PESXD kjyiumRcLjJyG6jA3DLQEnWucwpx+mZDSNh4 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a81:c44c:0:b0:586:50cf:e13f with SMTP id s12-20020a81c44c000000b0058650cfe13fmr286393ywj.1.1696908087335; Mon, 09 Oct 2023 20:21:27 -0700 (PDT) Date: Tue, 10 Oct 2023 03:21:15 +0000 In-Reply-To: <20231010032117.1577496-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20231010032117.1577496-1-yosryahmed@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Message-ID: <20231010032117.1577496-5-yosryahmed@google.com> Subject: [PATCH v2 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=20Koutn=C3=BD?=" , Waiman Long , kernel-team@cloudflare.com, Wei Xu , Greg Thelen , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 09 Oct 2023 20:21:51 -0700 (PDT) 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 --- mm/workingset.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index b192e44a0e7c..a573be6c59fd 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(); } /** -- 2.42.0.609.gbb76f46606-goog