From patchwork Fri Dec 6 00:31:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896155 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 5E963E77171 for ; Fri, 6 Dec 2024 00:31:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D82EB6B0122; Thu, 5 Dec 2024 19:31:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D316A6B0126; Thu, 5 Dec 2024 19:31:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BABEE6B0128; Thu, 5 Dec 2024 19:31:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9816F6B0122 for ; Thu, 5 Dec 2024 19:31:37 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 24C911C81BA for ; Fri, 6 Dec 2024 00:31:37 +0000 (UTC) X-FDA: 82862655330.10.4B2D85F Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf22.hostedemail.com (Postfix) with ESMTP id C1706C0009 for ; Fri, 6 Dec 2024 00:31:17 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeCQUqYF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 35kVSZwYKCPs1x2kdrjrrjoh.frpolqx0-ppnydfn.ruj@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=35kVSZwYKCPs1x2kdrjrrjoh.frpolqx0-ppnydfn.ruj@flex--yuzhao.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445087; a=rsa-sha256; cv=none; b=fLhHkBqdxcMM+pM55KyYRYl02bSQnYHeCt/dHP9VtHGt651RCbDYOAKaf27tl8PnGts2sm pZgn0u3F3dJ2aa/TCr3ULCXsNHLS4I03u8yvx9rJHH4h9RwaHJuj7A19EqqeiS6r20wd2W jhBIZ+JWxnta3tW9itGrQ7ZaA++VXeM= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeCQUqYF; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 35kVSZwYKCPs1x2kdrjrrjoh.frpolqx0-ppnydfn.ruj@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=35kVSZwYKCPs1x2kdrjrrjoh.frpolqx0-ppnydfn.ruj@flex--yuzhao.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733445087; 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=gY9A2hZ07C4gph5EkXou5eK7gRFH6gXGFcx+0iC/Exo=; b=1SqX7QIgiKZesXHlSwCnSo0CRZIrCnW302Nl66sa9nmsEMBHRXso4iBS7ebMr9azvIvVQB mPQiJLRJxuDR1X+TghAaZnCW0H43bfzQIGPd2q2nLpRjSGuVdEyXt6ppCMP9W3cEEk4Qmx dhq1uouUjHILgmhBTvEW9Hal7AU3KcE= Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-725a30c9a35so903547b3a.2 for ; Thu, 05 Dec 2024 16:31:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445094; x=1734049894; 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=gY9A2hZ07C4gph5EkXou5eK7gRFH6gXGFcx+0iC/Exo=; b=FeCQUqYFwgw41nYw6JVdGyw/g0jTeWBE9zUBrAyxcGd9q1LNB5eCAA+APkdpss6Pcy ugCCV0nMbNaaiPgNeB8KSDx+p9X6BdJ4YVM5rUu4zOiCjQPwJ5sFyAg859DD1g0xIKb0 N7hi+vPSxMWPToo99s4r71CbYz7EcoZHQ5sKVKavYnNM1wYf3elpKc3E8+U50zaecCPi +wmXi5SWRyLFZ/M4OPwHWUHT2QA8myIbENRuUDYjOLb0tfNz6VDsBgLBUuQOfio5wK4k qgp9cpZbJRzdb1iUdORCebG7HcMVP0VCQ/+c3FWQsbqzjhONKk8z5DIWoIfE41pC9BYr OXfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445094; x=1734049894; 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=gY9A2hZ07C4gph5EkXou5eK7gRFH6gXGFcx+0iC/Exo=; b=DtxxLTyR0OQ3qZACfjxYUdn7hBbYVhlnZi9z9pd8nxMc0r4hPbgyOq1EjDqmgJYjHN i1nQ6fsBo8gwN7wz/iIivqHVsogVG7FxLDt7hRRxON18//nRDf2qKJIaaHH2uSN3aG5R YKiQuvsb0vPg5usWa4u+UJ3SeAZ624QouZUy+kAEqyqTp9nEFFOm6pZyqwRDzMNDl9/p 0tnj4V5yffSeYDkmpJWN4ReoxxKdveqVg1+pChsFb3eIMGnejHhzPhRUQi5TEZ2O+NDu sOTQGjAp2TvwjIcT08HfQKduiWbV3WhCwSwVVSwUj0BNzlWrY/0rnDmKMVNQ9qSLjl2t Qlaw== X-Gm-Message-State: AOJu0YyM0pey2idTgWZik9gyCQPWRL3lNL3xnAgXfqinaJDR/Guf7Xl9 eh/rpY3NNWJxKfkITovdZ5aGUIKiKvDUA+skR4bhNuHQ9kE22OXrGQUydsjZ0XmDuv8CEoh4ab+ wAw== X-Google-Smtp-Source: AGHT+IG6Au9pfRghDcM8iM0cy1enSAIDDkXjALs9OLLc2AlZ1mMtncq3AmviK9jGO8YbyJSyYoHMLUHAqO4= X-Received: from pfbc10.prod.google.com ([2002:a05:6a00:ad0a:b0:725:8ee5:e458]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8d6:b0:725:8f15:f897 with SMTP id d2e1a72fcca58-725b80e4a9dmr2046628b3a.4.1733445094214; Thu, 05 Dec 2024 16:31:34 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:21 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-2-yuzhao@google.com> Subject: [PATCH mm-unstable v2 1/6] mm/mglru: clean up workingset From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kalesh Singh X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: C1706C0009 X-Stat-Signature: 6onarkzdxamkuogaywbmagazj7w1nqy9 X-HE-Tag: 1733445077-123279 X-HE-Meta: U2FsdGVkX19QxwWvfHY3O3d5h2new7d8ycHeAlVRD+Y80CGPjoZA7y3sT5ftfgv98BpKyHFdM24XzgOj+zE7rVN7zGZjQSBHsoj08Uub10zCJ30+PxGk5kObj9snIRqFeR6J60oxdBprlNEj1SDS6bWM4J9HwWqdTk74RMJfNMD5ZqreGsdrxrkLyyt0atV9cMkFi8Ee8l/mkS5uF3vjn4cCBMn1bo/fID5+RK83d0jtsZfA7dj68uGExcXDVnhyqcXdJbcxaa6yLhjUCUQPH2FedCv76BILkR6OULYXf45VamCeFSydDEWGgPSJPYO+y+uBIBgMJfrzS4brHtemqYmN3BC6aoX6GsJQ9lNFyLnc5zKXIWmTSFny52ulnbGtieNBFwFG8Sjy7mGUKOENxFa41nyiWl0a6N7E4m+24pgTOg4g763338DQ/Hnfh8JaoOAmZkL4YPrsoV45gL7ACYnOP61ySQsiVD5jyQotnPqm+kgZg288WlJloTnrLyQJ3yrCORzB2QhWqnsjgluN4iZnAuX+hYByjD0U7bHSrohkpB3NnFGML+YTWXINQZISLlwjkbLMhPhpO3PP6VIjrd8x5tDSmU6mrHey07PbswLE6Wdo6hDWnLeQvKBX3mUq7OwpRfP2wqwcL7ZbmepLu3ZSa6SNOSPt6qm1h7pSkIXsHIjSwLKlaV7P0lBDPyEw1uBcOHEbK+0yeo82sQvg5fphkURkrIdfBx8QPOCGB6vBqL6SpgYgQbJzwuLdb+43Bz18cCGo5RHQJcIx1vHmmwH4a/SgmsPIQZhl8YUJ08z2b3GmfTsc5i1AC3eZ0T3oLEubYjdKTy519fERfgSN5K5lxBTFfisdnjqxLE5gd1LNn5GlWWMuUwngd9XCBZiG6Yvtc0IagV4cNE+MmIx0UJTX3NKiOVE0Nbf/2fcR8OrKp01xSgsG3KJjl+JTM/leZKvmUz+FYyn5+CJobIu STbBPFXZ PjlYdtbwNLxtyIy/oapScfsbML05HAuAHjBpPc/wi5TQEEC2uDRplf3C/B8q8AG4kjAs+dFVKZ43+Vpa2hwjD1oN1Wc5lnBagk0VJ9cnKfeI2mXsjpLeZGL2n0nMixRqbizHc26wcmtFTPDEpsCp9kqEC5+xce9ozInLV7b/FWOat+V7wD+xynL22jSgrgMV4uUUnRG9DacZVRHsedbCm2h3W9KszcntLNUtNRfs0KFCi2wSJivsubgCh2UnlMQZK3N0jY//Rm+3ORfcQaWjoMSYXdwps2mXOd4zJA7kbBBgD9N3WI72KoScT5VOhM18rVKAV2sT8BfXEZNYYu3NtrCexi+lvKNQraRDYT9G9wlJkLDz9x0iCQ3BTfYmW9Lw8/t0GXybnLRQXJobfCgI4kR7huV5+XDAGvWHdNTILT0LVYeKu5EfO7zPUItsYnWUcfPOhlhvFy19hXt2pnqi4YrX0bxgyQriZjlTTJHHCeu63uQdg9umnEalfOlM0JcV0TYsJ/2Y7uOpaUkA7a+ZfXs+HNRkd+jzHC2LK X-Bogosity: Ham, tests=bogofilter, spamicity=0.006686, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Move VM_BUG_ON_FOLIO() to cover both the default and MGLRU paths. Also use a pair of rcu_read_lock() and rcu_read_unlock() within each path, to improve readability. This change should not have any side effects. Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/workingset.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index a4705e196545..ad181d1b8cf1 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -428,17 +428,17 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset, struct pglist_data *pgdat; unsigned long eviction; - rcu_read_lock(); - if (lru_gen_enabled()) { - bool recent = lru_gen_test_recent(shadow, file, - &eviction_lruvec, &eviction, workingset); + bool recent; + rcu_read_lock(); + recent = lru_gen_test_recent(shadow, file, &eviction_lruvec, + &eviction, workingset); rcu_read_unlock(); return recent; } - + rcu_read_lock(); unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset); eviction <<= bucket_order; @@ -459,14 +459,12 @@ 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 || !mem_cgroup_tryget(eviction_memcg))) { - rcu_read_unlock(); + if (!mem_cgroup_tryget(eviction_memcg)) + eviction_memcg = NULL; + rcu_read_unlock(); + + if (!mem_cgroup_disabled() && !eviction_memcg) return false; - } - - rcu_read_unlock(); - /* * Flush stats (and potentially sleep) outside the RCU read section. * @@ -544,6 +542,8 @@ void workingset_refault(struct folio *folio, void *shadow) bool workingset; long nr; + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + if (lru_gen_enabled()) { lru_gen_refault(folio, shadow); return; @@ -558,7 +558,6 @@ void workingset_refault(struct folio *folio, void *shadow) * 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); From patchwork Fri Dec 6 00:31:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896156 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 D3CEAE7716E for ; Fri, 6 Dec 2024 00:31:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4814E6B0128; Thu, 5 Dec 2024 19:31:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4314F6B012A; Thu, 5 Dec 2024 19:31:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25C616B012B; Thu, 5 Dec 2024 19:31:40 -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 01B886B0128 for ; Thu, 5 Dec 2024 19:31:39 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A6AF9AF0E1 for ; Fri, 6 Dec 2024 00:31:39 +0000 (UTC) X-FDA: 82862655750.28.2F79DCE Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 96F9D4000A for ; Fri, 6 Dec 2024 00:31:25 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="PPrJdc/H"; spf=pass (imf01.hostedemail.com: domain of 36EVSZwYKCP03z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.160.74 as permitted sender) smtp.mailfrom=36EVSZwYKCP03z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445082; a=rsa-sha256; cv=none; b=lxP6lf+rZz6r0JZlkXl5Igm8Rr3yGJpj6UExdVNKb/U+X36gmXFamB9anlFKka7ElgIfJJ uPmNghn60LhsLUByvBPWRjaHqOGLcVv5e4+3URwuracrKAiZR/8iyDcyNnjZIju/1ySSss 99CG0R6nBCRKO1I4ea6IE69UQ+qq0QA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="PPrJdc/H"; spf=pass (imf01.hostedemail.com: domain of 36EVSZwYKCP03z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.160.74 as permitted sender) smtp.mailfrom=36EVSZwYKCP03z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.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=1733445082; 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=MW8P2vBtR0tiHVuNXF698UnUQTzNbjkVYUdDrWgvFuo=; b=FSfXNGoNB58U5x31ry6HSgAq6gJv/XOGGhZEXhICHBZsqbLI5weYTGhflEYMzKq6QeKnnK 25lTJd7gNft7ENFZUhPdKra8FSdinVXe/++GPy4eMDpndmBG488bpzL/67Ax8peXNa3VSX /G9Ab3pnyRLeml9kf9xfB8keW37bNns= Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-29e5f045c93so2186190fac.0 for ; Thu, 05 Dec 2024 16:31:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445097; x=1734049897; 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=MW8P2vBtR0tiHVuNXF698UnUQTzNbjkVYUdDrWgvFuo=; b=PPrJdc/HS6oA782MnsGZHTGSUvkVCEBAKjXPbXkwzQoqmNWpD0/q1o3ZmZvv5sJIKB 9Qu5zF200QV7ox1QAhkx4Ph/wJaSiK+HczXOvcjNozkykvWI2PeAyK5q765PUciDL/3c oiirn/JPNif1L2hpJXF37DI9+Uc5tdNokZ5QF5ACG1ELXcGXcaqTI7/Tu7Fc9noiUttn n98ge+TxqmlIvCmQS8bj7JJDMWcph884C8DkIUXV+ReauMMkxIVv5jQcnKw3a5aNcGmP FzXC3f/fMp+WDOWh0N2Pf2Gy9RzM+H+Lijoc0Z9DTnrHIfaIZVriE0u41whrPFl74HkX Hw9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445097; x=1734049897; 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=MW8P2vBtR0tiHVuNXF698UnUQTzNbjkVYUdDrWgvFuo=; b=MGuGFvZHluE3ssEmIgEYC4lCsRLiqLxx/oxH/ZDU/RZBrT+8Pn4P6rZ1M3Ba2AV3CC 1d0m9TmrABW+dYE2uTzwQ8L6qd+In3DLOH6UzRuh6kvVNadmMX93h811nBNGigtN8MFM lL2qwxTCpZE1Jul9cdITg9IWODYajBDv5t32cgSkzZva3lg7KD6+NYwHPLRMwwOo4Urs WKyAWzN6tP+2M0qIFoCNbJd6LvuAfq77e76BZ818m4pRN6X5vH4TxYF/83RkMtswYV7U vc02o0tVgrV2l3qbojU/JYsI+2DoL6bZe0Z0mXrAVvlYsOF6VgvhYFYyOshv/GX9QC4d zGBw== X-Gm-Message-State: AOJu0YxDwcdLCJEwOG1VD+SNkm3WJJPXp3NFYZy0wJn3ZbIkgUzXv8lO eBcPWLBIgGf2moSQxvyo3DT6/gy61jWUhRX8ge+8QGMNlDcPOrzZS6kdWVzx4KLmha4bXHHwKS/ uNQ== X-Google-Smtp-Source: AGHT+IHG56U6ff/2ISJCA5gXcXDrcTLQkj6CAfollUPpPgK2o7iXSrjbLgXvQK+fFy4kFbchimZeACpqNOE= X-Received: from oabra18.prod.google.com ([2002:a05:6871:6112:b0:29e:9a9e:5041]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:d90c:b0:29e:4b63:d7 with SMTP id 586e51a60fabf-29f731a2894mr1012088fac.1.1733445096951; Thu, 05 Dec 2024 16:31:36 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:22 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-3-yuzhao@google.com> Subject: [PATCH mm-unstable v2 2/6] mm/mglru: optimize deactivation From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Bharata B Rao , Kalesh Singh X-Rspamd-Queue-Id: 96F9D4000A X-Stat-Signature: kh4y9cjzeayqmsadigrfnbeznts31s3f X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1733445085-392889 X-HE-Meta: U2FsdGVkX19n2m+nOT9kmfMS6RnhWElydRAO8ux3dIgBhLv8QkwvmeD3ABHqO4PajyknRU7l/t7BGSqxCL3rN0tqxYLipiUQWpyP88vJBrwWmUUbWBNaaINOaLCYsx8/+n7Iu91DMxEYXaGTijrjIwTuQRqXmDucD0P1SWGQrRlr9YhjBPp+MqFUXERsVGoJRffuH6F1GjX12nevVDoTX+9oQOE7EOTPS5ZOA2Toub/0feAZEZpKhH3TYqGl74M+AGGNsCeBFyhq6Scxhuzo1EiWIEOBwsVaGwUX2MxmOOaiH9CF9YiKJ8Qvmst28AmVNi2YTkaWrnZpLWZJs+iU+f7nc6gxye5AosqUlVhXH0n9EhlzULBXrz5ON5J7Jj1NR2pZKQqNWb8mdBAZoVNG9RdgswBg+xkxdjyhRMA9PLBdoXZaddetDByekR1MgvJmA4UzMmPje1dvv0kMkCkC0whcAWpZZPji/hzA4/eFIBc8L6AVZn5ZLGDIwrL1EF5iCAcaJZQZLVvSSQBs6yOnfJcARSOBq8cyKZSyUiejHCsO2aJiWSDSpKa5WxK64fKi/418a+ITukRDtgIMU9ojNef6smYx69weSY8yzq7gBXJuFU/mhfFlwV4hYkEro8mC87/XNlguS/jnvr7m210o2eiCkjhs9LbqDFLwGbTM2qp5/vEbM1cFGX0x/Din6bvHw6s3US3hi0uELjO5XMONLqrhn6NsHgM6Anu30BgHlFfbcdr22/dLV7p1Uq2Xix6NAyIOwLlQP5daKuY+AauhK0z/tZXOk9+Vrdtl8q5dBjhEVSrHePqsQVZywfWuHxB170dUnHYLW7c1OB8WzrTjUhPhOKcwO0asvtwEIZlDaCnZh3Em59lPJCNmJsp6/qiLTKki8JBt+RyDjbgHN4L4tuYqUu5c8xC+nLU1bCiE5CHWbczpiEUCuAMC78jjj68HEfBV5xuayuhnY2Ie1EO 0PZTcod+ YjypHfXrv6xonGr/xy1SWZL+ovIkYKNIaIzzIGVh1wwAktsMLlicXkhUbymXuLPPDw0VRkjI6ddLdQ1PHTGDGi/Z0XGgBLJHNrqCgkUL62xfNfxf0Dlj71uYilwFtO3CEXZU7Qce2XN3quvffrOapJnC11Vai6ewrsYL8guch6sJcPk/jSL2CQIQgUd+Aid6kqagzfQpHCkLo1Bw2BPMqcAUWNcApXfoUVZknagUg2HxvyevIAa58CxWDO30/d3SCr+l3OGV3iOt0uOkGyNu1qGEPvrvmPf5hil8ZDAtrjXwyMSrhz3/Nj6Onp9krFdwh5La0XN+jiBDt0Lh9raTgv9i0B2OVqKKo3s9iVL0QYQuhOkHiweBAVCoTXMj6AX+GB8M9gyNUFN/6N1h/5XcUXRX8k4YcB3HrE+DpD8AVjCDiHcrTz+ErWxumnXIMizz20UYR2vn0/gWXQcQ1vUh14tHLrX61LVGABn7e10dw3vgvsjwgAEzt8WWVT8Qd74hif0orku1uCI6B/1/MiALNZAZQKUVDONbPtlFLfUqIFVsBG38xuavvcoHLT5k1+3V7XEPqMtZCJcYqmfY7JhqkDB758Swqh4WBzmw0HAdpWX2cFI4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.004072, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Do not shuffle a folio in the deactivation paths if it is already in the oldest generation. This reduces the LRU lock contention. Before this patch, the contention is reproducible by FIO, e.g., fio -filename=/dev/nvme1n1p2 -direct=0 -thread -size=1024G \ -rwmixwrite=30 --norandommap --randrepeat=0 -ioengine=sync \ -bs=4k -numjobs=400 -runtime=25000 --time_based \ -group_reporting -name=mglru 98.96%--_raw_spin_lock_irqsave folio_lruvec_lock_irqsave | --98.78%--folio_batch_move_lru | --98.63%--deactivate_file_folio mapping_try_invalidate invalidate_mapping_pages invalidate_bdev blkdev_common_ioctl blkdev_ioctl After this patch, deactivate_file_folio() bails out early without taking the LRU lock. A side effect is that a folio can be left at the head of the oldest generation, rather than the tail. If reclaim happens at the same time, it cannot reclaim this folio immediately. Since there is no known correlation between truncation and reclaim, this side effect is considered insignificant. Reported-by: Bharata B Rao Closes: https://lore.kernel.org/CAOUHufawNerxqLm7L9Yywp3HJFiYVrYO26ePUb1jH-qxNGWzyA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/swap.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/mm/swap.c b/mm/swap.c index 3a01acfd5a89..15a94be8b0af 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -379,7 +379,8 @@ static void __lru_cache_activate_folio(struct folio *folio) } #ifdef CONFIG_LRU_GEN -static void folio_inc_refs(struct folio *folio) + +static void lru_gen_inc_refs(struct folio *folio) { unsigned long new_flags, old_flags = READ_ONCE(folio->flags); @@ -406,10 +407,30 @@ static void folio_inc_refs(struct folio *folio) new_flags |= old_flags & ~LRU_REFS_MASK; } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); } -#else -static void folio_inc_refs(struct folio *folio) + +static bool lru_gen_clear_refs(struct folio *folio) { + int gen = folio_lru_gen(folio); + int type = folio_is_file_lru(folio); + struct lru_gen_folio *lrugen = &folio_lruvec(folio)->lrugen; + + set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + + /* whether can do without shuffling under the LRU lock */ + return gen == lru_gen_from_seq(READ_ONCE(lrugen->min_seq[type])); } + +#else /* !CONFIG_LRU_GEN */ + +static void lru_gen_inc_refs(struct folio *folio) +{ +} + +static bool lru_gen_clear_refs(struct folio *folio) +{ + return false; +} + #endif /* CONFIG_LRU_GEN */ /** @@ -428,7 +449,7 @@ static void folio_inc_refs(struct folio *folio) void folio_mark_accessed(struct folio *folio) { if (lru_gen_enabled()) { - folio_inc_refs(folio); + lru_gen_inc_refs(folio); return; } @@ -524,7 +545,7 @@ void folio_add_lru_vma(struct folio *folio, struct vm_area_struct *vma) */ static void lru_deactivate_file(struct lruvec *lruvec, struct folio *folio) { - bool active = folio_test_active(folio); + bool active = folio_test_active(folio) || lru_gen_enabled(); long nr_pages = folio_nr_pages(folio); if (folio_test_unevictable(folio)) @@ -589,7 +610,10 @@ static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio) lruvec_del_folio(lruvec, folio); folio_clear_active(folio); - folio_clear_referenced(folio); + if (lru_gen_enabled()) + lru_gen_clear_refs(folio); + else + folio_clear_referenced(folio); /* * Lazyfree folios are clean anonymous folios. They have * the swapbacked flag cleared, to distinguish them from normal @@ -657,6 +681,9 @@ void deactivate_file_folio(struct folio *folio) if (folio_test_unevictable(folio)) return; + if (lru_gen_enabled() && lru_gen_clear_refs(folio)) + return; + folio_batch_add_and_move(folio, lru_deactivate_file, true); } @@ -670,7 +697,10 @@ void deactivate_file_folio(struct folio *folio) */ void folio_deactivate(struct folio *folio) { - if (folio_test_unevictable(folio) || !(folio_test_active(folio) || lru_gen_enabled())) + if (folio_test_unevictable(folio)) + return; + + if (lru_gen_enabled() ? lru_gen_clear_refs(folio) : !folio_test_active(folio)) return; folio_batch_add_and_move(folio, lru_deactivate, true); From patchwork Fri Dec 6 00:31:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896157 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 01AE3E77170 for ; Fri, 6 Dec 2024 00:31:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 878AF6B012B; Thu, 5 Dec 2024 19:31:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 829386B012E; Thu, 5 Dec 2024 19:31:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62E606B0130; Thu, 5 Dec 2024 19:31:43 -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 3BBA16B012B for ; Thu, 5 Dec 2024 19:31:43 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EEE9E1A0E15 for ; Fri, 6 Dec 2024 00:31:42 +0000 (UTC) X-FDA: 82862655414.25.E9BB2FF Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf12.hostedemail.com (Postfix) with ESMTP id 0FB574000C for ; Fri, 6 Dec 2024 00:31:33 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=h8+FqnlS; spf=pass (imf12.hostedemail.com: domain of 360VSZwYKCAI0w1jcqiqqing.eqonkpwz-oomxcem.qti@flex--yuzhao.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=360VSZwYKCAI0w1jcqiqqing.eqonkpwz-oomxcem.qti@flex--yuzhao.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=1733445090; 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=x/WXMwNfw0sDUCuQfYx8lzIKxCIUKsshP78UrWZEISs=; b=KpWBi9IHwRuh307/QSs0OrWaEiF9PQI1iO8wvTZxxmoK36wL88++auBzKODvbyGuh5T/9l vztlOasoBExXDTC7aM53osQwJbIj+mjEq1VWOWo9YSavqkOUT42MwsRAhEAQ0BByLGuuWq xdHLOboIWkFFbh9Ne6JZsbjgVe6qXec= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=h8+FqnlS; spf=pass (imf12.hostedemail.com: domain of 360VSZwYKCAI0w1jcqiqqing.eqonkpwz-oomxcem.qti@flex--yuzhao.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=360VSZwYKCAI0w1jcqiqqing.eqonkpwz-oomxcem.qti@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445090; a=rsa-sha256; cv=none; b=gWIIRmd/ewMc7qhICa0YNzTjQ66nnRKRQQeniO1rk5lLe2TbzzaHEwbzHb58OYFbvYCJkr 8ip7Oc9pkU7xdByapyjFkDNbvoaR6YNrRseI7fKrZstcDobHKhV0mIjiFRkKF8L2Am16nR syILcmW90xQTwhuF4KFn0QUvDm75uJQ= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2153e070e62so16874605ad.0 for ; Thu, 05 Dec 2024 16:31:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445100; x=1734049900; 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=x/WXMwNfw0sDUCuQfYx8lzIKxCIUKsshP78UrWZEISs=; b=h8+FqnlSUKI8Z35EFBLdsSXPagHjYCqyYqlSkdaV8UEfLQk55PW5H/eVv0MiNMPnuk uy8KlbdRHDXB7WSXpSWNRJOOpdRJkM5lqj3/XzgZ91h4UzTQ9OAYjQkoOBbY+Z4MYuC6 q53ddlpD/tZldxVnDzog0faqODIAJW4Ct6lZULxqMW+V5xvoNlivvuqY/KQKL6bSYsLv 4/T/lRUKE1FC8PsZ3slVxVHWzchsOseqM8TNnh11mT42oPU7DPXTDyy7ms0DPZDevbvc uN6eURJzELZxsP8KpgwyczJvs6piVrcojq1kdQqgtpL5WsjuBZaOaBajZcZ0lsf/0Tp1 px5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445100; x=1734049900; 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=x/WXMwNfw0sDUCuQfYx8lzIKxCIUKsshP78UrWZEISs=; b=ohA7R8E3krr/VZ9Lg/FcOS5JhvFfgBqpIqw0xJ9Q79Vx4NA4hB1DX0T7uBxVLCwS/x YbeMXM6L+FYIictkjT+te0I01VhK2Q1T3t58EjhsEARnxeQuz54fZtYzHYQnuWC6xQYD 49Jo7BxJOwpdhrnH4M3Pxsw0Yhz2JNltTU4C1V4moWXGIXS0YfEQvEPV28PBKfelWzC3 FoSs4f7RejxNLCVSqiQA6CJx2ieOpItyultcnXMFdWkKz5j1lhTVDeITeV7mB7SezNO3 6nuh5vAm+Yt5bhnC5ihHYBxVfCJw/kdspUMqU+yLVnoodfv18gd96yhlwFBG+XFFYxQS xn7A== X-Gm-Message-State: AOJu0Yy1dDMPWs6ZaT17jq8LOt5nsK63yg+yJdVXfD4E04x8EfG4WJv1 v7Xuu/iHRfTLthS2qTx2+vZr93aqp/gV0Dn48oRxOFZkIHRpGk0mSZQe6Wuej1TtCyY7SJspjRs 6RQ== X-Google-Smtp-Source: AGHT+IGARiTmCyLkm3u7hD+Znks+begLp6HQetG4yLAdPKodzK2Xx8PDWX1HYfje4Mbo+dqEamZnBwYUV6c= X-Received: from pltf4.prod.google.com ([2002:a17:902:74c4:b0:215:ac55:d3e1]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:41d0:b0:215:8af7:eff8 with SMTP id d9443c01a7336-21614dc710amr11353915ad.52.1733445099701; Thu, 05 Dec 2024 16:31:39 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:23 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-4-yuzhao@google.com> Subject: [PATCH mm-unstable v2 3/6] mm/mglru: rework aging feedback From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , David Stevens , Kalesh Singh X-Rspamd-Server: rspam05 X-Stat-Signature: exdpbf81aaoqbo78fuoz4z4ssr6n9grp X-Rspamd-Queue-Id: 0FB574000C X-Rspam-User: X-HE-Tag: 1733445093-782853 X-HE-Meta: U2FsdGVkX1+wsWpfk3FQ7aPRlM4w5IN0Iy9tsHpaH+nR8L4JhmcJNvuTGeP1JrJiVdpnmuVQoRC+5rNgzGmdDoUtDzoka5LGxEh53c9NtPoVGGFQhcDGM3fe69UmtEmsYW/7pfAy7cVYnj1zc6IkXXU0uotuVCGnjcpr64ZRaLI7Rsu7mtWZkv/fO27FoSIT6TjATkLrq58knOzb2OlAtSfrpYeZOzpjXyMks6v961jaTrGayu+QkvTouHlSN34FoAyNa0oxlLDNfhRoRnFbTyt6Jl6d7ePnUjQdFuXTW7H+K56DZxW4qYzTPTYv9BQD6ppuPh53pVTP5vYcHRGfnXm1gauC3I8LesAU+y77umHvxkwUIKD/+dMyRIpqsqG6/R854nacMWA6fw5eWQ9o/ETNI9flrQo20+ZQsZRmIvTd6Aq9Xgu0AAaikmXxE91GPzjuHIKdNdN5eVjR+Y/ucwP6sT8mrLs1cxmpC+XznNOwTnMa8PKATaiOJobYOOCqAYLWJq9xehKj2qWxhbfFuxe5cwz1YS50a983L8ky78r49R9nnS5/kcya5iudwdBBWjv4b0cEDtoimPyEcNvwjx7ye2pFUq6hHnC4Nv9xWdlNTtU8U2g+Iy/a94XzzX8/z8xUXi8DryZsmzir3FpSzK5nJL6xNgIxoJWU3NG3pxPSD1ca+jYBKNp7PuppJvKQL07SiB6KWyomCefjognzG3YxTMHQGuyLzFZwappWmb3MAINXBYvY8ZyQBjI0FPvTwU/WbUunZc8BA6byrKVIFTPU/5LaK3u0nLj7PT2xzYsQi5fPa97QHPUNxcxvex7CRbir5/fQ8OGuoJ2twAzxcqAwIK/mCZnmKz+FIzSn1PKuVe3RMVotSdtCWlLrUtnAdLQg83w6Miml5fK/1E4RkubU2TAdX1gf/AnTWN8QZibTb0uhoMYO82leBWOOvgnbohwHnaCsoCz4OgrsVUr MNTOG+Uk jVc1B7GO5Y0JDQZVuYJgWD4+2c0U6qHnrJf8mpxbh2ZKoG2EupLY4nfsKe5GlwRT96aE5UvpSQS5XNYpvWbu5ykLTEBbRZyNTVJv4Qo3b2jpSJiLaINCQ4yV8UudJJ7pIDV8qoyzPNnENX4zyVJqJakaRNyG6U0GSwJU2vqWO35sBRY86QBoEpwyNC8FZrTIv4zlHiej4+zDVAsI5EXx1jCqxxf0+tBncJwMzsWV9+XdNwZEfz0cvlK3SO3LqijYJqXJDaOgszqY+faVzVkwOYM2NVOmOX4+7ZeZ+7eckGQtRp3YWizHIVpEWw6qBc7BsgYLoCYoWwc1VLbvaVQYdRBFSYqMHrEwvg2Ku2RNJ5Nx2xyf5mLk1/KackXP17NsbvSozj1CLc6H86C3h847pBCLP3M+Z2msJvjl0CFPbD9VvbIuuzS7q3lmLmCvpxN7aeTjbuT+5CjG3OvqpY70BlAl2P6+Tt2l0oj5E4L0L1U6zooSDw6OX4xQGJ+wlt3UVJRXhn716zH+DCNIO2/8rJ0tvieyHi7BfmsfkYRBoGTKVgO1a0OahXOW5ug== 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 aging feedback is based on both the number of generations and the distribution of folios in each generation. The number of generations is currently the distance between max_seq and anon min_seq. This is because anon min_seq is not allowed to move past file min_seq. The rationale for that is that file is always evictable whereas anon is not. However, for use cases where anon is a lot cheaper than file: 1. Anon in the second oldest generation can be a better choice than file in the oldest generation. 2. A large amount of file in the oldest generation can skew the distribution, making should_run_aging() return false negative. Allow anon and file min_seq to move independently, and use solely the number of generations as the feedback for aging. Specifically, when both anon and file are evictable, anon min_seq can now be greater than file min_seq, and therefore the number of generations becomes the distance between max_seq and min(min_seq[0],min_seq[1]). And should_run_aging() returns true if and only if the number of generations is less than MAX_NR_GENS. As the first step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. The next twos patch will take advantage of this change; the last patch in this series will better distribute folios across MAX_NR_GENS. Reported-by: David Stevens Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- include/linux/mmzone.h | 6 +- mm/vmscan.c | 217 +++++++++++++++++------------------------ 2 files changed, 91 insertions(+), 132 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index b36124145a16..b998ccc5c341 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -446,8 +446,8 @@ struct lru_gen_folio { unsigned long avg_refaulted[ANON_AND_FILE][MAX_NR_TIERS]; /* the exponential moving average of evicted+protected */ unsigned long avg_total[ANON_AND_FILE][MAX_NR_TIERS]; - /* the first tier doesn't need protection, hence the minus one */ - unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS - 1]; + /* can only be modified under the LRU lock */ + unsigned long protected[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; /* can be modified without holding the LRU lock */ atomic_long_t evicted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; atomic_long_t refaulted[NR_HIST_GENS][ANON_AND_FILE][MAX_NR_TIERS]; @@ -498,7 +498,7 @@ struct lru_gen_mm_walk { int mm_stats[NR_MM_STATS]; /* total batched items */ int batched; - bool can_swap; + int swappiness; bool force_scan; }; diff --git a/mm/vmscan.c b/mm/vmscan.c index 2a8db048d581..00a5aff3db42 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -2623,11 +2623,17 @@ static bool should_clear_pmd_young(void) READ_ONCE((lruvec)->lrugen.min_seq[LRU_GEN_FILE]), \ } +#define evictable_min_seq(min_seq, swappiness) \ + min((min_seq)[!(swappiness)], (min_seq)[(swappiness) != MAX_SWAPPINESS]) + #define for_each_gen_type_zone(gen, type, zone) \ for ((gen) = 0; (gen) < MAX_NR_GENS; (gen)++) \ for ((type) = 0; (type) < ANON_AND_FILE; (type)++) \ for ((zone) = 0; (zone) < MAX_NR_ZONES; (zone)++) +#define for_each_evictable_type(type, swappiness) \ + for ((type) = !(swappiness); (type) <= ((swappiness) != MAX_SWAPPINESS); (type)++) + #define get_memcg_gen(seq) ((seq) % MEMCG_NR_GENS) #define get_memcg_bin(bin) ((bin) % MEMCG_NR_BINS) @@ -2673,10 +2679,16 @@ static int get_nr_gens(struct lruvec *lruvec, int type) static bool __maybe_unused seq_is_valid(struct lruvec *lruvec) { - /* see the comment on lru_gen_folio */ - return get_nr_gens(lruvec, LRU_GEN_FILE) >= MIN_NR_GENS && - get_nr_gens(lruvec, LRU_GEN_FILE) <= get_nr_gens(lruvec, LRU_GEN_ANON) && - get_nr_gens(lruvec, LRU_GEN_ANON) <= MAX_NR_GENS; + int type; + + for (type = 0; type < ANON_AND_FILE; type++) { + int n = get_nr_gens(lruvec, type); + + if (n < MIN_NR_GENS || n > MAX_NR_GENS) + return false; + } + + return true; } /****************************************************************************** @@ -3083,9 +3095,8 @@ static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain, pos->refaulted = lrugen->avg_refaulted[type][tier] + atomic_long_read(&lrugen->refaulted[hist][type][tier]); pos->total = lrugen->avg_total[type][tier] + + lrugen->protected[hist][type][tier] + atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - pos->total += lrugen->protected[hist][type][tier - 1]; pos->gain = gain; } @@ -3112,17 +3123,15 @@ static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) WRITE_ONCE(lrugen->avg_refaulted[type][tier], sum / 2); sum = lrugen->avg_total[type][tier] + + lrugen->protected[hist][type][tier] + atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - sum += lrugen->protected[hist][type][tier - 1]; WRITE_ONCE(lrugen->avg_total[type][tier], sum / 2); } if (clear) { atomic_long_set(&lrugen->refaulted[hist][type][tier], 0); atomic_long_set(&lrugen->evicted[hist][type][tier], 0); - if (tier) - WRITE_ONCE(lrugen->protected[hist][type][tier - 1], 0); + WRITE_ONCE(lrugen->protected[hist][type][tier], 0); } } } @@ -3257,7 +3266,7 @@ static int should_skip_vma(unsigned long start, unsigned long end, struct mm_wal return true; if (vma_is_anonymous(vma)) - return !walk->can_swap; + return !walk->swappiness; if (WARN_ON_ONCE(!vma->vm_file || !vma->vm_file->f_mapping)) return true; @@ -3267,7 +3276,10 @@ static int should_skip_vma(unsigned long start, unsigned long end, struct mm_wal return true; if (shmem_mapping(mapping)) - return !walk->can_swap; + return !walk->swappiness; + + if (walk->swappiness == MAX_SWAPPINESS) + return true; /* to exclude special mappings like dax, etc. */ return !mapping->a_ops->read_folio; @@ -3355,7 +3367,7 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned } static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, - struct pglist_data *pgdat, bool can_swap) + struct pglist_data *pgdat) { struct folio *folio; @@ -3366,10 +3378,6 @@ static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, if (folio_memcg(folio) != memcg) return NULL; - /* file VMAs can contain anon pages from COW */ - if (!folio_is_file_lru(folio) && !can_swap) - return NULL; - return folio; } @@ -3425,7 +3433,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, if (pfn == -1) continue; - folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); + folio = get_pfn_folio(pfn, memcg, pgdat); if (!folio) continue; @@ -3510,7 +3518,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area if (pfn == -1) goto next; - folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); + folio = get_pfn_folio(pfn, memcg, pgdat); if (!folio) goto next; @@ -3722,22 +3730,26 @@ static void clear_mm_walk(void) kfree(walk); } -static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) +static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness) { int zone; int remaining = MAX_LRU_BATCH; struct lru_gen_folio *lrugen = &lruvec->lrugen; + int hist = lru_hist_from_seq(lrugen->min_seq[type]); int new_gen, old_gen = lru_gen_from_seq(lrugen->min_seq[type]); - if (type == LRU_GEN_ANON && !can_swap) + if (type ? swappiness == MAX_SWAPPINESS : !swappiness) goto done; - /* prevent cold/hot inversion if force_scan is true */ + /* prevent cold/hot inversion if the type is evictable */ for (zone = 0; zone < MAX_NR_ZONES; zone++) { struct list_head *head = &lrugen->folios[old_gen][type][zone]; while (!list_empty(head)) { struct folio *folio = lru_to_folio(head); + int refs = folio_lru_refs(folio); + int tier = lru_tier_from_refs(refs); + int delta = folio_nr_pages(folio); VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); @@ -3747,6 +3759,9 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) new_gen = folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + if (!--remaining) return false; } @@ -3758,51 +3773,37 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, bool can_swap) return true; } -static bool try_to_inc_min_seq(struct lruvec *lruvec, bool can_swap) +static bool try_to_inc_min_seq(struct lruvec *lruvec, int swappiness) { int gen, type, zone; bool success = false; struct lru_gen_folio *lrugen = &lruvec->lrugen; - DEFINE_MIN_SEQ(lruvec); VM_WARN_ON_ONCE(!seq_is_valid(lruvec)); - /* find the oldest populated generation */ - for (type = !can_swap; type < ANON_AND_FILE; type++) { - while (min_seq[type] + MIN_NR_GENS <= lrugen->max_seq) { - gen = lru_gen_from_seq(min_seq[type]); + for_each_evictable_type(type, swappiness) { + unsigned long seq; + + for (seq = lrugen->min_seq[type]; seq + MIN_NR_GENS <= lrugen->max_seq; seq++) { + gen = lru_gen_from_seq(seq); for (zone = 0; zone < MAX_NR_ZONES; zone++) { if (!list_empty(&lrugen->folios[gen][type][zone])) goto next; } - - min_seq[type]++; } next: - ; - } - - /* see the comment on lru_gen_folio */ - if (can_swap) { - min_seq[LRU_GEN_ANON] = min(min_seq[LRU_GEN_ANON], min_seq[LRU_GEN_FILE]); - min_seq[LRU_GEN_FILE] = max(min_seq[LRU_GEN_ANON], lrugen->min_seq[LRU_GEN_FILE]); - } - - for (type = !can_swap; type < ANON_AND_FILE; type++) { - if (min_seq[type] == lrugen->min_seq[type]) - continue; - - reset_ctrl_pos(lruvec, type, true); - WRITE_ONCE(lrugen->min_seq[type], min_seq[type]); - success = true; + if (seq != lrugen->min_seq[type]) { + reset_ctrl_pos(lruvec, type, true); + WRITE_ONCE(lrugen->min_seq[type], seq); + success = true; + } } return success; } -static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) +static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, int swappiness) { bool success; int prev, next; @@ -3820,13 +3821,11 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, if (!success) goto unlock; - for (type = ANON_AND_FILE - 1; type >= 0; type--) { + for (type = 0; type < ANON_AND_FILE; type++) { if (get_nr_gens(lruvec, type) != MAX_NR_GENS) continue; - VM_WARN_ON_ONCE(!force_scan && (type == LRU_GEN_FILE || can_swap)); - - if (inc_min_seq(lruvec, type, can_swap)) + if (inc_min_seq(lruvec, type, swappiness)) continue; spin_unlock_irq(&lruvec->lru_lock); @@ -3870,7 +3869,7 @@ static bool inc_max_seq(struct lruvec *lruvec, unsigned long seq, } static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) + int swappiness, bool force_scan) { bool success; struct lru_gen_mm_walk *walk; @@ -3881,7 +3880,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, VM_WARN_ON_ONCE(seq > READ_ONCE(lrugen->max_seq)); if (!mm_state) - return inc_max_seq(lruvec, seq, can_swap, force_scan); + return inc_max_seq(lruvec, seq, swappiness); /* see the comment in iterate_mm_list() */ if (seq <= READ_ONCE(mm_state->seq)) @@ -3906,7 +3905,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, walk->lruvec = lruvec; walk->seq = seq; - walk->can_swap = can_swap; + walk->swappiness = swappiness; walk->force_scan = force_scan; do { @@ -3916,7 +3915,7 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq, } while (mm); done: if (success) { - success = inc_max_seq(lruvec, seq, can_swap, force_scan); + success = inc_max_seq(lruvec, seq, swappiness); WARN_ON_ONCE(!success); } @@ -3957,13 +3956,13 @@ static bool lruvec_is_sizable(struct lruvec *lruvec, struct scan_control *sc) { int gen, type, zone; unsigned long total = 0; - bool can_swap = get_swappiness(lruvec, sc); + int swappiness = get_swappiness(lruvec, sc); struct lru_gen_folio *lrugen = &lruvec->lrugen; struct mem_cgroup *memcg = lruvec_memcg(lruvec); DEFINE_MAX_SEQ(lruvec); DEFINE_MIN_SEQ(lruvec); - for (type = !can_swap; type < ANON_AND_FILE; type++) { + for_each_evictable_type(type, swappiness) { unsigned long seq; for (seq = min_seq[type]; seq <= max_seq; seq++) { @@ -3983,6 +3982,7 @@ static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc { int gen; unsigned long birth; + int swappiness = get_swappiness(lruvec, sc); struct mem_cgroup *memcg = lruvec_memcg(lruvec); DEFINE_MIN_SEQ(lruvec); @@ -3992,8 +3992,7 @@ static bool lruvec_is_reclaimable(struct lruvec *lruvec, struct scan_control *sc if (!lruvec_is_sizable(lruvec, sc)) return false; - /* see the comment on lru_gen_folio */ - gen = lru_gen_from_seq(min_seq[LRU_GEN_FILE]); + gen = lru_gen_from_seq(evictable_min_seq(min_seq, swappiness)); birth = READ_ONCE(lruvec->lrugen.timestamps[gen]); return time_is_before_jiffies(birth + min_ttl); @@ -4060,7 +4059,6 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) unsigned long addr = pvmw->address; struct vm_area_struct *vma = pvmw->vma; struct folio *folio = pfn_folio(pvmw->pfn); - bool can_swap = !folio_is_file_lru(folio); struct mem_cgroup *memcg = folio_memcg(folio); struct pglist_data *pgdat = folio_pgdat(folio); struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat); @@ -4113,7 +4111,7 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) if (pfn == -1) continue; - folio = get_pfn_folio(pfn, memcg, pgdat, can_swap); + folio = get_pfn_folio(pfn, memcg, pgdat); if (!folio) continue; @@ -4329,8 +4327,8 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c gen = folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); - WRITE_ONCE(lrugen->protected[hist][type][tier - 1], - lrugen->protected[hist][type][tier - 1] + delta); + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); return true; } @@ -4529,7 +4527,6 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw { int i; int type; - int scanned; int tier = -1; DEFINE_MIN_SEQ(lruvec); @@ -4554,21 +4551,23 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw else type = get_type_to_scan(lruvec, swappiness, &tier); - for (i = !swappiness; i < ANON_AND_FILE; i++) { + for_each_evictable_type(i, swappiness) { + int scanned; + if (tier < 0) tier = get_tier_idx(lruvec, type); + *type_scanned = type; + scanned = scan_folios(lruvec, sc, type, tier, list); if (scanned) - break; + return scanned; type = !type; tier = -1; } - *type_scanned = type; - - return scanned; + return 0; } static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness) @@ -4584,6 +4583,7 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap struct reclaim_stat stat; struct lru_gen_mm_walk *walk; bool skip_retry = false; + struct lru_gen_folio *lrugen = &lruvec->lrugen; struct mem_cgroup *memcg = lruvec_memcg(lruvec); struct pglist_data *pgdat = lruvec_pgdat(lruvec); @@ -4593,7 +4593,7 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap scanned += try_to_inc_min_seq(lruvec, swappiness); - if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS) + if (evictable_min_seq(lrugen->min_seq, swappiness) + MIN_NR_GENS > lrugen->max_seq) scanned = 0; spin_unlock_irq(&lruvec->lru_lock); @@ -4665,63 +4665,32 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap } static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, - bool can_swap, unsigned long *nr_to_scan) + int swappiness, unsigned long *nr_to_scan) { int gen, type, zone; - unsigned long old = 0; - unsigned long young = 0; - unsigned long total = 0; + unsigned long size = 0; struct lru_gen_folio *lrugen = &lruvec->lrugen; DEFINE_MIN_SEQ(lruvec); - /* whether this lruvec is completely out of cold folios */ - if (min_seq[!can_swap] + MIN_NR_GENS > max_seq) { - *nr_to_scan = 0; + *nr_to_scan = 0; + /* have to run aging, since eviction is not possible anymore */ + if (evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS > max_seq) return true; - } - for (type = !can_swap; type < ANON_AND_FILE; type++) { + for_each_evictable_type(type, swappiness) { unsigned long seq; for (seq = min_seq[type]; seq <= max_seq; seq++) { - unsigned long size = 0; - gen = lru_gen_from_seq(seq); for (zone = 0; zone < MAX_NR_ZONES; zone++) size += max(READ_ONCE(lrugen->nr_pages[gen][type][zone]), 0L); - - total += size; - if (seq == max_seq) - young += size; - else if (seq + MIN_NR_GENS == max_seq) - old += size; } } - *nr_to_scan = total; - - /* - * The aging tries to be lazy to reduce the overhead, while the eviction - * stalls when the number of generations reaches MIN_NR_GENS. Hence, the - * ideal number of generations is MIN_NR_GENS+1. - */ - if (min_seq[!can_swap] + MIN_NR_GENS < max_seq) - return false; - - /* - * It's also ideal to spread pages out evenly, i.e., 1/(MIN_NR_GENS+1) - * of the total number of pages for each generation. A reasonable range - * for this average portion is [1/MIN_NR_GENS, 1/(MIN_NR_GENS+2)]. The - * aging cares about the upper bound of hot pages, while the eviction - * cares about the lower bound of cold pages. - */ - if (young * MIN_NR_GENS > total) - return true; - if (old * (MIN_NR_GENS + 2) < total) - return true; - - return false; + *nr_to_scan = size; + /* better to run aging even though eviction is still possible */ + return evictable_min_seq(min_seq, swappiness) + MIN_NR_GENS == max_seq; } /* @@ -4729,7 +4698,7 @@ static bool should_run_aging(struct lruvec *lruvec, unsigned long max_seq, * 1. Defer try_to_inc_max_seq() to workqueues to reduce latency for memcg * reclaim. */ -static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool can_swap) +static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, int swappiness) { bool success; unsigned long nr_to_scan; @@ -4739,7 +4708,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool if (mem_cgroup_below_min(sc->target_mem_cgroup, memcg)) return -1; - success = should_run_aging(lruvec, max_seq, can_swap, &nr_to_scan); + success = should_run_aging(lruvec, max_seq, swappiness, &nr_to_scan); /* try to scrape all its memory if this memcg was deleted */ if (nr_to_scan && !mem_cgroup_online(memcg)) @@ -4750,7 +4719,7 @@ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc, bool return nr_to_scan >> sc->priority; /* stop scanning this lruvec as it's low on cold folios */ - return try_to_inc_max_seq(lruvec, max_seq, can_swap, false) ? -1 : 0; + return try_to_inc_max_seq(lruvec, max_seq, swappiness, false) ? -1 : 0; } static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc) @@ -5294,8 +5263,7 @@ static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec, s = "rep"; n[0] = atomic_long_read(&lrugen->refaulted[hist][type][tier]); n[1] = atomic_long_read(&lrugen->evicted[hist][type][tier]); - if (tier) - n[2] = READ_ONCE(lrugen->protected[hist][type][tier - 1]); + n[2] = READ_ONCE(lrugen->protected[hist][type][tier]); } for (i = 0; i < 3; i++) @@ -5350,7 +5318,7 @@ static int lru_gen_seq_show(struct seq_file *m, void *v) seq_printf(m, " node %5d\n", nid); if (!full) - seq = min_seq[LRU_GEN_ANON]; + seq = evictable_min_seq(min_seq, MAX_SWAPPINESS / 2); else if (max_seq >= MAX_NR_GENS) seq = max_seq - MAX_NR_GENS + 1; else @@ -5390,23 +5358,14 @@ static const struct seq_operations lru_gen_seq_ops = { }; static int run_aging(struct lruvec *lruvec, unsigned long seq, - bool can_swap, bool force_scan) + int swappiness, bool force_scan) { DEFINE_MAX_SEQ(lruvec); - DEFINE_MIN_SEQ(lruvec); - - if (seq < max_seq) - return 0; if (seq > max_seq) return -EINVAL; - if (!force_scan && min_seq[!can_swap] + MAX_NR_GENS - 1 <= max_seq) - return -ERANGE; - - try_to_inc_max_seq(lruvec, max_seq, can_swap, force_scan); - - return 0; + return try_to_inc_max_seq(lruvec, max_seq, swappiness, force_scan) ? 0 : -EEXIST; } static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_control *sc, @@ -5422,7 +5381,7 @@ static int run_eviction(struct lruvec *lruvec, unsigned long seq, struct scan_co while (!signal_pending(current)) { DEFINE_MIN_SEQ(lruvec); - if (seq < min_seq[!swappiness]) + if (seq < evictable_min_seq(min_seq, swappiness)) return 0; if (sc->nr_reclaimed >= nr_to_reclaim) From patchwork Fri Dec 6 00:31:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896158 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 61843E77171 for ; Fri, 6 Dec 2024 00:31:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF8766B00E6; Thu, 5 Dec 2024 19:31:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D7FE06B00ED; Thu, 5 Dec 2024 19:31:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B84B56B0109; Thu, 5 Dec 2024 19:31:45 -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 96A0B6B00E6 for ; Thu, 5 Dec 2024 19:31:45 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 55671141811 for ; Fri, 6 Dec 2024 00:31:45 +0000 (UTC) X-FDA: 82862655414.01.26F8104 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf26.hostedemail.com (Postfix) with ESMTP id 39E8214000F for ; Fri, 6 Dec 2024 00:31:31 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gMeesYfD; spf=pass (imf26.hostedemail.com: domain of 37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.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=1733445096; 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=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=SffSIRKoaopFCv+g0cKWa5JzmY2iuMxTLdGCOHY1JBIe3apFjAaajS2rLVeRctiQObK0Oa Fv3qCHEs2OZHxKOuFmHEkdkw/ak6zmat4iILy1q5a206UcnRrSSOU+WnRm4fDyWB4jIQCs fvltowzO30bEMZM12g8epaYI5Ixh2lY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445096; a=rsa-sha256; cv=none; b=ZeAnA3pDiyUAs4d1uTLfwgIGKVRlcln/3O+IECRCX89RaxiedrrOsYUFDWPdCr64fmD6X5 Ir6L9+V1+S++3EXaUbWbxOWgtMRRy1lY8rx/4PmKxDCOu6sKk3qe7WNjfdXxif7nM0eBuJ 4WqcuoWKRY9tpzZr6bzarHdOm7e5SBE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=gMeesYfD; spf=pass (imf26.hostedemail.com: domain of 37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=37kVSZwYKCAU3z4mftlttlqj.htrqnsz2-rrp0fhp.twl@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-724fc3f60c1so2317528b3a.2 for ; Thu, 05 Dec 2024 16:31:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445102; x=1734049902; 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=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=gMeesYfDm07sqWFuQl6FpgsS8fYNno3t9XUvik/r4pnZGO+1OPQ6ijOyR4eLMdZkNw 5rpP6ogU8ch1AikpeQMsJYWz76QE0sCEaWkSip7NxsC1dXO9V0HFPR3xzH0mNVzSAKSs UUrkrTFfPnLoqCk4mIFGlP4vNnxuLShSz1+Yu7HU90VR5WsaAdqU3ewhyynjmDuwEyo7 GcWZ/dth+XYO4R4lwa5RvMteBqri86QZu+YZwPS7HgzjByRZEZvaeeyHzFcZ1IonYoyL XSj7IKQRzm67Fx+Dcdq4CEDZUwZThFhCZRphIRxZcPotV4X6lJg00prXqJs/4dYtTK6o 2/5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445102; x=1734049902; 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=h1mvjlX5nT1rLjjtVWZn8iR4hG3sSfAtSE80GuqS3ww=; b=a+rY6tJXruaUZGKGK6lQtv4v4Jj6tf5qwL/4KzYy/5FO+GGrl650SWPvhbfTBPkbbi PnCxx7jym6QPjps2PRkh2RHgn+fmnGtNNBwP5UBj3VMQ2/3UbbkbJMHE83z9qVRdbwcb bM1raqBEfFq2Ycv9nm2tYWcdl4ac8cxcmcd0B9nzdM1rCVk9ZXnRkLX2DwL3tT/gN0R1 DV/ePD7rdbzKWbsm2fxh+Yqa+uf1vVkwcgpimOKDCycSSd84JJrUp7rY+Rjk45qld+qz W3a2RxL7D/Lf0oRLrhMOf4v5cJOAJN2tGsp9441LkZLYytr0SjFfETTQm+WZ1daLBuAV y+Aw== X-Gm-Message-State: AOJu0YxBauImdh5eH3tSa5oov9fRtUjeQrmqGE11QNBpCiJ6R0YjP+6J 9r3A5QvknRgImy2kthlC+aamcKT9h/o3R/OFtuZRvYiN194HZFbu2v88uE/MWB3QEAuLcmPUInI qOg== X-Google-Smtp-Source: AGHT+IHDpfbWWJjycwRdIOATm4/TSA+FDknfDh4D0XImpwFKpP+OCJtRJF+zzzhrB+mJ306ByBPns1yu/E8= X-Received: from pfbim19.prod.google.com ([2002:a05:6a00:8d93:b0:725:3321:1f0c]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4fc7:b0:725:9d1d:3462 with SMTP id d2e1a72fcca58-725b81c1481mr1987282b3a.25.1733445102450; Thu, 05 Dec 2024 16:31:42 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:24 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-5-yuzhao@google.com> Subject: [PATCH mm-unstable v2 4/6] mm/mglru: rework type selection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , David Stevens , Kalesh Singh X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 39E8214000F X-Stat-Signature: 44yb1fzf1t17eiexox7ioo3xmbpbi8rj X-Rspam-User: X-HE-Tag: 1733445091-374490 X-HE-Meta: U2FsdGVkX19fdOb8DwvJmkZmHZiaqQZ5bwn23TkuIsAsWoIj83JSox8XUp6jBgeCVuSz1H2mB6qUAnwrhamG1WuRSyxOWikYppAFirBOZGs6r0zCgqW5SQsHqDdP6cxxalFe4rezGygtPM21gCrTY8GZ6S2b6sp6+YRwIYFB+6y1SrRg0b+hvx70WC+YVGuUbyHfxax7lzI+DCKuBSiUAdInwo0w9yXHoFBkHOFMMiwWtyFloZ1YyMeUAY6GZpM2R46M5mSH+YNIr8n9Mn83dkZE4QTSXLxLJNghtf83GfKMaoP/G2GRdH28rYTJ6giBjYXhEsNhHR9llUB1o0+iaMlw+w4OuUWRTeFb8hGw3+4P5btRH8vLU+NgeDJpiX0hc2+fySojoEvCcDRoQm/v9ai0m6B2u7Yf5P103xKcrezDocnh1K+pz8QXhJkQimQyCsUX9pVedrYI1IIjVi4Ym41ydLDbpIvQZEbguURJJMDY0HzSdOqdV7Afhdq7zgn2b2Hx5uVVoXD8TxUnAe8nPfJjuvlG35l3vtseoND+qUBMm5bJeFzwtUzXpS7o76Pba5lkhTV8Z0hU0n8HJbe9t26VAFfBgtQt3CjYJaoxMvUoCp2q6i3necGbb969KvdJRs1BuEXcehHGFiYCkvr3gBcZHlZ62xR9roZGQvwwrw8BMsMbUDrgwg9Lj7qdnOMAKsIWPVgH5b8/oB1+jh33ivhcHw+hEJn5qseSS2WR76kqW/+R8rRO72FD+VWok81B/fh6gsh5M7KcdDAdy8n5dr0TW3e9gyp6RjoDzqfIOvQLRL4NMKfVQtN/A1gTzV4D+OU4Oipd2DQiZYtcnAoYkJSolVwUPOEq6zDpco5RV/orbw/d6pQjqaGhm7dJPcyAn9HYOZNYlB/K/qnPnoIRurIJWuUMOEDNpLb74h8IgKvwoGLR49rfZ6vOyxD1B+2vZnEr1rICZu++KZ4TYmW Txk8ZzsZ jyeSirCwFXGvi2bXt0Tu+y6f72OCKGsS37T4701Av6uOedroBMXBwY6tKiNqLKzLDgyvl+7nVLokromdfP5XUdta6+bL5XCfE/uQzlBZTmWaEVaO6Fa5rOxxXejEQ3lMx+9vn0JJsZ84+921FkmOqf4rvdLsM/pZ4n1GomTGWFC5paBxnNIBXYJrJGkshTS1HOxJT3OP8MSO9Fac71Xfrxvu4XCEq9FE6lKUCIsnOXD3kzhxpzpV33d9usaChdkdtearpjkr9b3kYT1EQVS3+TWmjY+0yQORQGYyp30E6KAYzOnNLVi60zjH70jmm4IvOI713IdYnYUyGBL3e8T/QLE+urQ/qPePxYhxREHRsJsBE+onLm8ukfdLeLENbafhQGHCaVJ+QUOTul93KkOVYcrsV1sPlTzozTsdQaSICicOWuS2j+qNvk69YC/yPd0KBwvm2m/OuKlWbh2eHaTYgHcYe6QB7l81mCgk23W+0eEGuBO8wY/3JtAkAR+85zaVp5QxYNU5vD7aMDhOHVzXiHMNauOGJLLtuxWj3uaJlKmFfB+WIeoA926+sNQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.011265, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With anon and file min_seq being able to move independently, rework type selection so that it is based on the total refaults from all tiers of each type. Also allow a type to be selected until that type reaches MIN_NR_GENS, and therefore abs_diff(min_seq[0],min_seq[1]) now can be 2 (MAX_NR_GENS-MIN_NR_GENS) instead of 1. Since some tiers of a selected type can have higher refaults than the first tier of the other type, use a less larger gain factor 2:3 instead of 1:2, in order for those tiers in the selected type to be better protected. As an intermediate step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. Reported-by: David Stevens Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/vmscan.c | 82 +++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 56 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 00a5aff3db42..02b01ae2bdbb 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3089,15 +3089,20 @@ struct ctrl_pos { static void read_ctrl_pos(struct lruvec *lruvec, int type, int tier, int gain, struct ctrl_pos *pos) { + int i; struct lru_gen_folio *lrugen = &lruvec->lrugen; int hist = lru_hist_from_seq(lrugen->min_seq[type]); - pos->refaulted = lrugen->avg_refaulted[type][tier] + - atomic_long_read(&lrugen->refaulted[hist][type][tier]); - pos->total = lrugen->avg_total[type][tier] + - lrugen->protected[hist][type][tier] + - atomic_long_read(&lrugen->evicted[hist][type][tier]); pos->gain = gain; + pos->refaulted = pos->total = 0; + + for (i = tier % MAX_NR_TIERS; i <= min(tier, MAX_NR_TIERS - 1); i++) { + pos->refaulted += lrugen->avg_refaulted[type][i] + + atomic_long_read(&lrugen->refaulted[hist][type][i]); + pos->total += lrugen->avg_total[type][i] + + lrugen->protected[hist][type][i] + + atomic_long_read(&lrugen->evicted[hist][type][i]); + } } static void reset_ctrl_pos(struct lruvec *lruvec, int type, bool carryover) @@ -4480,13 +4485,13 @@ static int get_tier_idx(struct lruvec *lruvec, int type) struct ctrl_pos sp, pv; /* - * To leave a margin for fluctuations, use a larger gain factor (1:2). + * To leave a margin for fluctuations, use a larger gain factor (2:3). * This value is chosen because any other tier would have at least twice * as many refaults as the first tier. */ - read_ctrl_pos(lruvec, type, 0, 1, &sp); + read_ctrl_pos(lruvec, type, 0, 2, &sp); for (tier = 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, 2, &pv); + read_ctrl_pos(lruvec, type, tier, 3, &pv); if (!positive_ctrl_err(&sp, &pv)) break; } @@ -4494,68 +4499,34 @@ static int get_tier_idx(struct lruvec *lruvec, int type) return tier - 1; } -static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx) +static int get_type_to_scan(struct lruvec *lruvec, int swappiness) { - int type, tier; struct ctrl_pos sp, pv; - int gain[ANON_AND_FILE] = { swappiness, MAX_SWAPPINESS - swappiness }; + if (!swappiness) + return LRU_GEN_FILE; + + if (swappiness == MAX_SWAPPINESS) + return LRU_GEN_ANON; /* - * Compare the first tier of anon with that of file to determine which - * type to scan. Also need to compare other tiers of the selected type - * with the first tier of the other type to determine the last tier (of - * the selected type) to evict. + * Compare the sum of all tiers of anon with that of file to determine + * which type to scan. */ - read_ctrl_pos(lruvec, LRU_GEN_ANON, 0, gain[LRU_GEN_ANON], &sp); - read_ctrl_pos(lruvec, LRU_GEN_FILE, 0, gain[LRU_GEN_FILE], &pv); - type = positive_ctrl_err(&sp, &pv); + read_ctrl_pos(lruvec, LRU_GEN_ANON, MAX_NR_TIERS, swappiness, &sp); + read_ctrl_pos(lruvec, LRU_GEN_FILE, MAX_NR_TIERS, MAX_SWAPPINESS - swappiness, &pv); - read_ctrl_pos(lruvec, !type, 0, gain[!type], &sp); - for (tier = 1; tier < MAX_NR_TIERS; tier++) { - read_ctrl_pos(lruvec, type, tier, gain[type], &pv); - if (!positive_ctrl_err(&sp, &pv)) - break; - } - - *tier_idx = tier - 1; - - return type; + return positive_ctrl_err(&sp, &pv); } static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int swappiness, int *type_scanned, struct list_head *list) { int i; - int type; - int tier = -1; - DEFINE_MIN_SEQ(lruvec); - - /* - * Try to make the obvious choice first, and if anon and file are both - * available from the same generation, - * 1. Interpret swappiness 1 as file first and MAX_SWAPPINESS as anon - * first. - * 2. If !__GFP_IO, file first since clean pagecache is more likely to - * exist than clean swapcache. - */ - if (!swappiness) - type = LRU_GEN_FILE; - else if (min_seq[LRU_GEN_ANON] < min_seq[LRU_GEN_FILE]) - type = LRU_GEN_ANON; - else if (swappiness == 1) - type = LRU_GEN_FILE; - else if (swappiness == MAX_SWAPPINESS) - type = LRU_GEN_ANON; - else if (!(sc->gfp_mask & __GFP_IO)) - type = LRU_GEN_FILE; - else - type = get_type_to_scan(lruvec, swappiness, &tier); + int type = get_type_to_scan(lruvec, swappiness); for_each_evictable_type(i, swappiness) { int scanned; - - if (tier < 0) - tier = get_tier_idx(lruvec, type); + int tier = get_tier_idx(lruvec, type); *type_scanned = type; @@ -4564,7 +4535,6 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw return scanned; type = !type; - tier = -1; } return 0; From patchwork Fri Dec 6 00:31:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896159 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 29395E77170 for ; Fri, 6 Dec 2024 00:31:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC3726B0133; Thu, 5 Dec 2024 19:31:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A72246B0134; Thu, 5 Dec 2024 19:31:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C7426B0136; Thu, 5 Dec 2024 19:31:48 -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 6375B6B0133 for ; Thu, 5 Dec 2024 19:31:48 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 19970161903 for ; Fri, 6 Dec 2024 00:31:48 +0000 (UTC) X-FDA: 82862655120.17.60A316E Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf30.hostedemail.com (Postfix) with ESMTP id 645F280013 for ; Fri, 6 Dec 2024 00:31:15 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cabHbOqK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 38UVSZwYKCAg627piwowwotm.kwutqv25-uus3iks.wzo@flex--yuzhao.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=38UVSZwYKCAg627piwowwotm.kwutqv25-uus3iks.wzo@flex--yuzhao.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445098; a=rsa-sha256; cv=none; b=ZpogRO3W1fCyOh31iJ/fH6PJ1bXe6VTQdQ50Ra6bYyyUNj1K/OJNIsILa1YavFeHYgldia mFPQ03akA7BS7llzPxT579ucJqR1jR+ybwMHkxlJIPc3/ScohF4+PVYBk7H/Nav6MGCu3r yzL3lvubKXyCY/01RXqp/yFaDQXGPN4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cabHbOqK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 38UVSZwYKCAg627piwowwotm.kwutqv25-uus3iks.wzo@flex--yuzhao.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=38UVSZwYKCAg627piwowwotm.kwutqv25-uus3iks.wzo@flex--yuzhao.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733445098; 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=KW2cgcwHB51Y5IYZaisd+Bp0HxAyG3VEQ4AUms4DlWs=; b=BygYtUkHeSK9/GK60pwaZCsiZyA9ZZaxTp8hjgo/vg8v6+h//OjiQ1Qw2X5T3WS0HDI7i1 xAeKtVmHgqB28Uuiofu5UOgbZ5X/93mf6KPywnvNcKSDbx60YwsT7Tt8dQtEAtDvSNi8K2 +wxPdkXu2u0ihfrmRDXIlyve3vQCVWU= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-215b30585b8so16142355ad.0 for ; Thu, 05 Dec 2024 16:31:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445105; x=1734049905; 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=KW2cgcwHB51Y5IYZaisd+Bp0HxAyG3VEQ4AUms4DlWs=; b=cabHbOqKSKWi0qfx/2mbXmKIIYABpQtLno/4xMG1kuYdH9mfWcBZpEGxXv49I5AZ0v 6R9r/OHy+d5y7ms4YQkPad0mdQn86m1/9OjDqhf70F4FLpAhVHjBO9QOmHS3djrydAlp MEi47Fy5nin4yhnX8x7oOkLjO9oGOmC2HhWhyhSVabzPpAebcRZMg/5VQ/9tl9iUWunh MS1og+POvSr8nzDERuPPwxqC9dROar96KlDYE6MYOQMymPmKOpzqNP35B7BVWF+k0o69 +pvvwJovNWvQ/K5aXhqfqSyFgigo8ev17/+crGs8eoCO4biaWztAzJQ1NCzWzsDV8Wy3 F73A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445105; x=1734049905; 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=KW2cgcwHB51Y5IYZaisd+Bp0HxAyG3VEQ4AUms4DlWs=; b=R1YrwtOeIeSDQqtsdrEspqchL47Cudy1ORMGKBUALY5SYBxKxUferB6cQBcLyL78sE 7gOuiWCZ4clWAjohsnBXcCnIhWF1ntpyRIoOHJX7ZyhP7agwvULTA1boPAfS3tfVIESs gSs9a9YSAtzGfjrT+NDgA+gEWHgGJQydjaLluMkG1l1RNW8EhjvsL6D5R8nNc8QNQngh wZtjy3YVJHqAx1wxFd/wjQzYqgGTElT+mlzbAkbTrFVgnzQ5BoqH+EibChUkHOmYVoT1 SmisEjHJU3BmeHyzY1eE0mfYuWI/Ydbx52jMK8H7NZxiJPD6ZpYGIh3OF1WyIj/lvFnk SUJg== X-Gm-Message-State: AOJu0YxjERFEWA4f7LBUZVHlMoiWUHup/iDiGBqb+OvNpU5rlt+TGPcT vzHFVLaB4+HhMRfQh5YNXZAMmvUxRtWSzU2Ykxeree0U1zjB4ALOiDwhlSl9TRM2uWd9fTmxUeL WGQ== X-Google-Smtp-Source: AGHT+IGcBw64P6PC43lwi6CEzaODCf5NQzsvKmv+pkev0Vd7uHGWUIM2qXHUyE9c6qDAYcuhyiX00sGOta8= X-Received: from pllb6.prod.google.com ([2002:a17:902:e946:b0:215:83cc:b135]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:24d:b0:216:1367:7e3d with SMTP id d9443c01a7336-21614d67fc3mr14295335ad.31.1733445105156; Thu, 05 Dec 2024 16:31:45 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:25 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-6-yuzhao@google.com> Subject: [PATCH mm-unstable v2 5/6] mm/mglru: rework refault detection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kairui Song , Kalesh Singh X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 645F280013 X-Stat-Signature: jqcwuuyrxknqzaepxdbceyqmfbrj5fmo X-HE-Tag: 1733445075-663449 X-HE-Meta: U2FsdGVkX1/L46T9/6Z57DV2TmICR4i0Vs+e4KKWGC9dpEU7f1i5yU6fZT0GlF4Yic9lXaR/gGrvib/XVoXVASli0dAEutyu5j7YHf42644ip8+ygJjjesMk305qGfnMxInQv+wcGDuPBlBmDxLdVLU9aWX9u2GTtjdfRcLeVUUAriJ3mVs/7jISWFdcXU2D1hkHClmRmpz5ehvCm332cs6E8L455NWvSxIPSMPAshyIVc6M8rCzrt1/MQv9/Z8ZETsjfBkTBWXsbr2AtbUHHowdPdony1qBMG7G4zeLgRqYHFFNafJ0G399NN/kTm6yoyHU/vv1qSXwJVTwlt7wEksuP5DJK3AWxlAEAoEmz/Adw43Ao8jS3xkUQLDpZ83xOYnHv98tjkCS0oiQA7CNExZ41feo/jeE9KlRBH/lvW/wX+iKSP8kTb+1isNH2vEqJ8+MSTqereWMOJKSyFJUgDn8SRDC+tx7sXHDlw95fBVudBT8Y9ANNsHCpQ7HJBo02t2/aFtpJiJag35Q+KtemwiV6oemZ9yRRyhjDVtQ4JKuUhoUzvyTisRzIbfUQln7ZLDtnnpk/Tq9x8HRE4cLQmVUfQH9kmbWTSMMxhl3Rw6f45VfyCgvaI8C06v9OjscjM+4touc/U/kAD9yJfGIYrdxOzY4Hfcryw3Qoqg0dTLezMqudVdZu8oGOQb5qr9C9d0G2QeXhgEF0Z+tkKOYHlZVQFZdnTj+kLLncJT2QyB6AlJpBo6FLDlE4z/WpCHmYtuZOcCXhoIubKFoT91Dr5J8tzQbFc5l1Jq6Tenn4jZv3lwosyr++SrwhnZda2xkKNYnsyKS+w/AYFiWwWrlxSYzdXQKTsNnw5+BQSRddOBE5hwBWYW4vtamT2Z1CFraPfjdOKlOzGNCslIkwhqnr/PWLKfZOrlIkOJpAQPwcElbAXu8AadQKJcQKiy9vgUchfpor5qdS3ddpb2aRUr myfIgMc/ pOb4zZnXgJ39hcvz8c5fLV67qE1zGiL2f52U4YowR56yXC/Zny7UEszwf/LcrvN0iX75KpCIqwGdx3i72OvwcON6A6Su7qhkazhXIqoPqPUodqprTYyGTbrmK1ogpic4VDFO3iz3PKCYq6aVvr9N5+5DlKeWocM01F3vRN30QQLd3ht96zz0HRIHoCb8E2oVP1aWnwDQcJxmfe3RyFn8xpDLLWcVdVgFAwPcdWVVIlVhIL3N697F6SaKAVDWVblBmr85T8GqPxYhtyGlTp4JFBnUdnfuoobiyMQDyUF6nVUp5YDJY+6VgNRK9cAMe3i/Zc+HS9scodHFwaWfZ/kDMeEtbcBaQR78EYICeRvjAyTPwRUkpT059xiBNDiSQM7IKMl6OxaLiZ55YLqY4qGxzeHBC11loUPjvah6f+kLg1jK+a/LrHOgy0Xtkm/C8FtVeHIlWpE1UCzWSucIa0rqYcP7RvOt02gfsbjN6jiGfGCIeaxzx3H03SpvHQUz4IRoIS8prhU9UVE7Tb7S/P7c+GexA1lDWMGiKSnIfDIohg/RzIqcPNlTH4VEU7dkLfX5DDRmnCaY4CNu2VTWAgT5QpuPG4xvm3okv2dJmSlTizzwIPE0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000520, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With anon and file min_seq being able to move independently, rework workingset protection as well so that the comparison of refaults between anon and file is always on an equal footing. Specifically, make lru_gen_test_recent() return true for refaults happening within the distance of MAX_NR_GENS. For example, if min_seq of a type is max_seq-MIN_NR_GENS, refaults from min_seq-1, i.e., max_seq-MIN_NR_GENS-1, are also considered recent, since the distance max_seq-(max_seq-MIN_NR_GENS-1), i.e., MIN_NR_GENS+1 is less than MAX_NR_GENS. As an intermediate step to the final optimization, this change by itself should not have userspace-visiable effects beyond performance. Reported-by: Kairui Song Closes: https://lore.kernel.org/CAOUHufahuWcKf5f1Sg3emnqX+cODuR=2TQo7T4Gr-QYLujn4RA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- mm/workingset.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mm/workingset.c b/mm/workingset.c index ad181d1b8cf1..2c310c29f51e 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -260,11 +260,11 @@ static void *lru_gen_eviction(struct folio *folio) * Tests if the shadow entry is for a folio that was recently evicted. * Fills in @lruvec, @token, @workingset with the values unpacked from shadow. */ -static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **lruvec, +static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec, unsigned long *token, bool *workingset) { int memcg_id; - unsigned long min_seq; + unsigned long max_seq; struct mem_cgroup *memcg; struct pglist_data *pgdat; @@ -273,8 +273,10 @@ static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **lruvec, memcg = mem_cgroup_from_id(memcg_id); *lruvec = mem_cgroup_lruvec(memcg, pgdat); - min_seq = READ_ONCE((*lruvec)->lrugen.min_seq[file]); - return (*token >> LRU_REFS_WIDTH) == (min_seq & (EVICTION_MASK >> LRU_REFS_WIDTH)); + max_seq = READ_ONCE((*lruvec)->lrugen.max_seq); + max_seq &= EVICTION_MASK >> LRU_REFS_WIDTH; + + return abs_diff(max_seq, *token >> LRU_REFS_WIDTH) < MAX_NR_GENS; } static void lru_gen_refault(struct folio *folio, void *shadow) @@ -290,7 +292,7 @@ static void lru_gen_refault(struct folio *folio, void *shadow) rcu_read_lock(); - recent = lru_gen_test_recent(shadow, type, &lruvec, &token, &workingset); + recent = lru_gen_test_recent(shadow, &lruvec, &token, &workingset); if (lruvec != folio_lruvec(folio)) goto unlock; @@ -331,7 +333,7 @@ static void *lru_gen_eviction(struct folio *folio) return NULL; } -static bool lru_gen_test_recent(void *shadow, bool file, struct lruvec **lruvec, +static bool lru_gen_test_recent(void *shadow, struct lruvec **lruvec, unsigned long *token, bool *workingset) { return false; @@ -432,8 +434,7 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset, bool recent; rcu_read_lock(); - recent = lru_gen_test_recent(shadow, file, &eviction_lruvec, - &eviction, workingset); + recent = lru_gen_test_recent(shadow, &eviction_lruvec, &eviction, workingset); rcu_read_unlock(); return recent; } From patchwork Fri Dec 6 00:31:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13896160 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 10313E7716E for ; Fri, 6 Dec 2024 00:31:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 965826B0136; Thu, 5 Dec 2024 19:31:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 913586B0138; Thu, 5 Dec 2024 19:31:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 765FD6B0139; Thu, 5 Dec 2024 19:31:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4D3F06B0136 for ; Thu, 5 Dec 2024 19:31:51 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D51BE1A17D6 for ; Fri, 6 Dec 2024 00:31:50 +0000 (UTC) X-FDA: 82862656086.11.FA8E5FA Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf07.hostedemail.com (Postfix) with ESMTP id 3E40D40007 for ; Fri, 6 Dec 2024 00:31:31 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cLFDk5wT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 380VSZwYKCAo849rkyqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yuzhao.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=380VSZwYKCAo849rkyqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yuzhao.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733445101; 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=jxh/CyTiTo80t1btGtiyWK44N5wckY+h9Q8TEZ4a9hc=; b=tpxwrptbk1qhRHFbP9oTTYwAfb2sXeYncGNuUXvq5hdn2oTkPhTTreK4SabRAONn4Ha+z1 ImmaX6YE30oZiVQmnuRqVt7NMH2CJ4oZgZDObEbtnSKyeiYOGnFS2kVQK8T2vNEXW5UtIT vfNPLadm2OyDkTEzOJmRkhmZGQwxWGg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733445101; a=rsa-sha256; cv=none; b=EQ5fMmXxwMIHtvKhRbxNFzK/qvIdkxHU3cmmwf+qGAO0C+cFM/0BdPi5RF8/At1AGCsNFK AAG9Ah6UQyoRqA0nI/9j4YeJSR6RTsymDPmjCiUOy65Sc9q97H8NFWxrhNF+uRj8tPaY0w U2GOmikGLWjGu2Ed6jciuaD8cRmCYZs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cLFDk5wT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 380VSZwYKCAo849rkyqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yuzhao.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=380VSZwYKCAo849rkyqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yuzhao.bounces.google.com Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7e9fb5352dfso1338023a12.3 for ; Thu, 05 Dec 2024 16:31:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733445107; x=1734049907; 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=jxh/CyTiTo80t1btGtiyWK44N5wckY+h9Q8TEZ4a9hc=; b=cLFDk5wTNvlIAkVLxfDzlQqiCA+ryXMFKLxxBingscpw7aLULKIc8S+Ep9vPYepPqv ekvCyZlKjSah0wXn95pX8tgw1EhvAzaTiIrmTX3lEp9hgA24gcfNnV9H80Xn1uNueyrX PZNRCbMw5EbSlFOhSrYcaO6JhEsKUJZgek/IlgdLKV+nKnuQzcVyao2H2g2s+2Fody+8 eeLqU9LeZuzz/AH6HnZ3wKkZI9i1/6qKC77c+B61A6XotYkxmJuVG1uP1EhPmkCkvIOw Yw6hN3xQxx8ihZ1+usv7bBB354NzzYruH8nbBD67Kc+iP3rsJfk186ptqCcmUuEeK0PJ YsoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733445107; x=1734049907; 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=jxh/CyTiTo80t1btGtiyWK44N5wckY+h9Q8TEZ4a9hc=; b=BBdhmQuxpgnCFuMeHZsB8AJvQjdXKqjct8Bv5ezVyHdRcsfrN5W9o+tiTu6mx5loGg HSOkq0ydty9IFRvz8gbb+ENKbzF+nnOQyQmLUtsaKpyxmfnwW5Elp+yxdUj1a7LvU8h/ AZS3zVzbOG0qkihCn9WjglRJrDOgSvJIE8HCZuuVXQyCgxH3VQ31mbIO43ok3Fmy0ga+ ZwOxFVOUYdBNdcNQv1A8B9Ojq+p7A0fxaCOJH3sd4bNvoh6K+3k3cPVXzGJJrz7yw5cM aVyYKkpcLVPBUXsZfoQBD2qavajVj/xlow2d3kk+KWjYQX6SeGIYavuCMxQUklnqOsnp gvsQ== X-Gm-Message-State: AOJu0YxfPoi5fI4AaOPI9zwgEyvwM60Omthu0ENc1vbtkRTsae3fsOS8 a5kV9LgiCIDblWoE+8R07V0B7wSVdMMy2tBxJxzM8ay4tQmm2gdFE+85Scc5E+UWkiomfKtkKzh tFw== X-Google-Smtp-Source: AGHT+IHJZ27MMa+QM4bFIoZmXc7e0cPr2Wfn3+ytQioqkCZG+HaMt6KJnbAzL7i3cXCeUdXXjK50wIdQ7DI= X-Received: from plbma14.prod.google.com ([2002:a17:903:94e:b0:20d:1e2b:3284]) (user=yuzhao job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c4:b0:215:b307:5bb2 with SMTP id d9443c01a7336-21614d68a58mr11549455ad.14.1733445107659; Thu, 05 Dec 2024 16:31:47 -0800 (PST) Date: Thu, 5 Dec 2024 17:31:26 -0700 In-Reply-To: <20241206003126.1338283-1-yuzhao@google.com> Mime-Version: 1.0 References: <20241206003126.1338283-1-yuzhao@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Message-ID: <20241206003126.1338283-7-yuzhao@google.com> Subject: [PATCH mm-unstable v2 6/6] mm/mglru: rework workingset protection From: Yu Zhao To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yu Zhao , Kairui Song , Kalesh Singh X-Stat-Signature: 68p1fgkqhm7a1kub4fiq64o6emyonbui X-Rspamd-Queue-Id: 3E40D40007 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1733445091-849730 X-HE-Meta: U2FsdGVkX1/4JPOlQLe7F7L4XKGw4E23AiXAW2Kfh+40+LnIIplY/42UTsJbw7m9JTmY7r7N9ZFkkCJvELD+mY2TNAs8sejZRw1ef9YNhgQuRJisHsMwQHQNjs2nBnZ6iVRRJzukork3YbIZMr/9uqQTqccRoy69vhQ7tb6hnnD7y2LoJfb5YzAH6sguSSB9PcNDiTSt/U0UJFmxWjhPcUAnDdTqJuwh6255jFgqt45HkK/VKDVtgA61rxoNE/VMbq4jp6pcD4ypynytzMXenjnZHqBQkCEUnwZnE4//d7ATKZ2oqjtW6w9hYqkNgHLXI6zLPQKZn0W5D2LHs18I9QWZvX+9BsIBR2D6yqAaVr0GE7nB3y60rUupK0FBi+H3oFVMxEFEr3JXp05UetLR8LOZkuCUTEfcB6EcjopAZfmCF3UElTJYCcwaO/lCtW/nCC1FwiygZi4TmCCkGL+G6tNO8dhwt2WawCA5hnyTyUWFL+2IuYiH331retfQDBKLwtBnoVseEiCmtuZuq4YdxWvy3ais6jW86snYFisf9/vff5N7oPWZ/5eJiugnFoeY+iwR7tKEhxGZwSCqhj0Hm+9CNHgRjxM7zVx6ZH+eErkb6NNTv2/Us/AxWV0DDUGwEAyrWNgdd77GEXGqsvzcdlwUTYSMaar/k30IzJi8/OCZ5n+e1gFkZJQkA5Otk5OT7i2kOeJX4Cp38qKqI6F8dc2XrzHrjURUYnmvcqwdBZB6D3G0i3LLXAYLt1eMvU77K04YmLu5eESnRM9WIkho1mkb+GDLgRULMV7/gT1AGtuxpvyM1Oo4G1hAxsgtNPi6ypuQzc0VaWhnwUdd8HcmaJi0tPC+NNEVKa22QNkjuvUUPV1KVGpEBrJxVTWsGEqeOxbVmcJVAxpkdR8WV2CwyIQX6RstSB50meMgT7mhErdq+Z47TfwnqcCmCIxGmVj8P+OFS9hPKZlCZgyxJpc tstGEN2w 8mw2SYUpN6EntH4nIrVCBMuFKY0v/4cGFHDevQlMBDsiXp/3WOc0II0eJgWhdi9HCGMtW4LkYfij3ySknJZoiVg9v1XhIohikE7+iw1kr/exE2kmbXDBlRRkwitWZn+VnwF50r5/lfoC6A5eaEcDPu1UQ4EYu06CR3Bysy7RRuKSw54u1QZlQI31r0/W37ci3BJpCec4xEQg6xDWgfucuboo6M4W1lBYzN579kIy8n58D4PrFZRFSQcEMfewQ/wjX9yms0makNswBhMl4GGewWH6kdB37BVwVrj0S9nWR+xqkEPOuX67QAIz8bfRYj0HMLbRlWTHzS9CnaREepL/0MUC65GoC9+ar70zfvhgOHLVWH8nNLOXaholzHlBBjzAhWmAqP6BkOFQB5SL2fVzcuaVnTJ241KHzy1yswnLdklDh3ygg+kX87GlrbwAZFZlmlbo2XFIaB968K0fx05qVvOaGymV+knUTXMM8+JkRewpfHqM1DN4+/bEbou9RncFYDU3TBGQlTKTdNxm3XglE8Uq03rQWBUo5IiDtYyy0VaGEjAJP5D3ifvzEV+aDBF+19hL5ZvPOEipi0lTeIUxA2xnpNy40YJNOK6HdBbPCMh011js= 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: With the aging feedback no longer considering the distribution of folios in each generation, rework workingset protection to better distribute folios across MAX_NR_GENS. This is achieved by reusing PG_workingset and PG_referenced/LRU_REFS_FLAGS in a slightly different way. For folios accessed multiple times through file descriptors, make lru_gen_inc_refs() set additional bits of LRU_REFS_WIDTH in folio->flags after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily promoted into the second oldest generation in the eviction path. And when folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that lru_gen_inc_refs() can start over. For this case, LRU_REFS_MASK is only valid when PG_referenced is set. For folios accessed multiple times through page tables, folio_update_gen() from a page table walk or lru_gen_set_refs() from a rmap walk sets PG_referenced after the accessed bit is cleared for the first time. Thereafter, those two paths set PG_workingset and promote folios to the youngest generation. Like folio_inc_gen(), when folio_update_gen() does that, it also clears PG_referenced. For this case, LRU_REFS_MASK is not used. For both of the cases, after PG_workingset is set on a folio, it remains until this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It can be set again if lru_gen_test_recent() returns true upon a refault. When adding folios to the LRU lists, lru_gen_distance() distributes them as follows: +---------------------------------+---------------------------------+ | Accessed thru page tables | Accessed thru file descriptors | +---------------------------------+---------------------------------+ | PG_active (set while isolated) | | +----------------+----------------+----------------+----------------+ | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | +---------------------------------+---------------------------------+ |<--------- MIN_NR_GENS --------->| | |<-------------------------- MAX_NR_GENS -------------------------->| After this patch, some typical client and server workloads showed improvements under heavy memory pressure. For example, Python TPC-C, which was used to benchmark a different approach [1] to better detect refault distances, showed a significant decrease in total refaults: Before After Change Time (seconds) 10801 10801 0% Executed (transactions) 41472 43663 +5% workingset_nodes 109070 120244 +10% workingset_refault_anon 5019627 7281831 +45% workingset_refault_file 1294678786 554855564 -57% workingset_refault_total 1299698413 562137395 -57% [1] https://lore.kernel.org/20230920190244.16839-1-ryncsn@gmail.com/ Reported-by: Kairui Song Closes: https://lore.kernel.org/CAOUHufahuWcKf5f1Sg3emnqX+cODuR=2TQo7T4Gr-QYLujn4RA@mail.gmail.com/ Signed-off-by: Yu Zhao Tested-by: Kalesh Singh --- include/linux/mm_inline.h | 94 +++++++++++++------------ include/linux/mmzone.h | 82 +++++++++++++--------- mm/swap.c | 23 +++--- mm/vmscan.c | 142 +++++++++++++++++++++++--------------- mm/workingset.c | 29 ++++---- 5 files changed, 209 insertions(+), 161 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 34e5097182a0..3fcf5fa797fe 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -133,31 +133,25 @@ static inline int lru_hist_from_seq(unsigned long seq) return seq % NR_HIST_GENS; } -static inline int lru_tier_from_refs(int refs) +static inline int lru_tier_from_refs(int refs, bool workingset) { VM_WARN_ON_ONCE(refs > BIT(LRU_REFS_WIDTH)); - /* see the comment in folio_lru_refs() */ - return order_base_2(refs + 1); + /* see the comment on MAX_NR_TIERS */ + return workingset ? MAX_NR_TIERS - 1 : order_base_2(refs); } static inline int folio_lru_refs(struct folio *folio) { unsigned long flags = READ_ONCE(folio->flags); - bool workingset = flags & BIT(PG_workingset); + if (!(flags & BIT(PG_referenced))) + return 0; /* - * Return the number of accesses beyond PG_referenced, i.e., N-1 if the - * total number of accesses is N>1, since N=0,1 both map to the first - * tier. lru_tier_from_refs() will account for this off-by-one. Also see - * the comment on MAX_NR_TIERS. + * Return the total number of accesses including PG_referenced. Also see + * the comment on LRU_REFS_FLAGS. */ - return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + workingset; -} - -static inline void folio_clear_lru_refs(struct folio *folio) -{ - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + return ((flags & LRU_REFS_MASK) >> LRU_REFS_PGOFF) + 1; } static inline int folio_lru_gen(struct folio *folio) @@ -223,11 +217,46 @@ static inline void lru_gen_update_size(struct lruvec *lruvec, struct folio *foli VM_WARN_ON_ONCE(lru_gen_is_active(lruvec, old_gen) && !lru_gen_is_active(lruvec, new_gen)); } +static inline int lru_gen_distance(struct folio *folio, bool reclaiming) +{ + /* + * Distance until eviction (larger values provide stronger protection): + * +-------------------------------------+-------------------------------------+ + * | Accessed through page tables and | Accessed through file descriptors | + * | promoted by folio_update_gen() | and protected by folio_inc_gen() | + * +-------------------------------------+-------------------------------------+ + * | PG_active (only set while isolated) | | + * +------------------+------------------+------------------+------------------+ + * | PG_workingset | PG_referenced | PG_workingset | LRU_REFS_FLAGS | + * +-------------------------------------+-------------------------------------+ + * | 3 | 2 | 1 | 0 | + * +-------------------------------------+-------------------------------------+ + * |<----------- MIN_NR_GENS ----------->| | + * |<------------------------------ MAX_NR_GENS ------------------------------>| + */ + if (reclaiming) + return 0; + + if (folio_test_active(folio)) + return MIN_NR_GENS + folio_test_workingset(folio); + + if (folio_test_workingset(folio)) + return MIN_NR_GENS - 1; + + if (!folio_is_file_lru(folio) && !folio_test_swapcache(folio)) + return MIN_NR_GENS - 1; + + if (folio_test_reclaim(folio) && (folio_test_dirty(folio) || folio_test_writeback(folio))) + return MIN_NR_GENS - 1; + + return 0; +} + static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming) { + int dist; unsigned long seq; unsigned long flags; - unsigned long mask; int gen = folio_lru_gen(folio); int type = folio_is_file_lru(folio); int zone = folio_zonenum(folio); @@ -237,40 +266,17 @@ static inline bool lru_gen_add_folio(struct lruvec *lruvec, struct folio *folio, if (folio_test_unevictable(folio) || !lrugen->enabled) return false; - /* - * There are four common cases for this page: - * 1. If it's hot, i.e., freshly faulted in, add it to the youngest - * generation, and it's protected over the rest below. - * 2. If it can't be evicted immediately, i.e., a dirty page pending - * writeback, add it to the second youngest generation. - * 3. If it should be evicted first, e.g., cold and clean from - * folio_rotate_reclaimable(), add it to the oldest generation. - * 4. Everything else falls between 2 & 3 above and is added to the - * second oldest generation if it's considered inactive, or the - * oldest generation otherwise. See lru_gen_is_active(). - */ - if (folio_test_active(folio)) - seq = lrugen->max_seq; - else if ((type == LRU_GEN_ANON && !folio_test_swapcache(folio)) || - (folio_test_reclaim(folio) && - (folio_test_dirty(folio) || folio_test_writeback(folio)))) - seq = lrugen->max_seq - 1; - else if (reclaiming || lrugen->min_seq[type] + MIN_NR_GENS >= lrugen->max_seq) - seq = lrugen->min_seq[type]; + + dist = lru_gen_distance(folio, reclaiming); + if (dist < MIN_NR_GENS) + seq = lrugen->min_seq[type] + dist; else - seq = lrugen->min_seq[type] + 1; + seq = lrugen->max_seq + dist - MIN_NR_GENS - 1; gen = lru_gen_from_seq(seq); flags = (gen + 1UL) << LRU_GEN_PGOFF; /* see the comment on MIN_NR_GENS about PG_active */ - mask = LRU_GEN_MASK; - /* - * Don't clear PG_workingset here because it can affect PSI accounting - * if the activation is due to workingset refault. - */ - if (folio_test_active(folio)) - mask |= LRU_REFS_MASK | BIT(PG_referenced) | BIT(PG_active); - set_mask_bits(&folio->flags, mask, flags); + set_mask_bits(&folio->flags, LRU_GEN_MASK | BIT(PG_active), flags); lru_gen_update_size(lruvec, folio, -1, gen); /* for folio_rotate_reclaimable() */ diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index b998ccc5c341..c7ad4d6e1618 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -332,66 +332,88 @@ enum lruvec_flags { #endif /* !__GENERATING_BOUNDS_H */ /* - * Evictable pages are divided into multiple generations. The youngest and the + * Evictable folios are divided into multiple generations. The youngest and the * oldest generation numbers, max_seq and min_seq, are monotonically increasing. * They form a sliding window of a variable size [MIN_NR_GENS, MAX_NR_GENS]. An * offset within MAX_NR_GENS, i.e., gen, indexes the LRU list of the * corresponding generation. The gen counter in folio->flags stores gen+1 while - * a page is on one of lrugen->folios[]. Otherwise it stores 0. + * a folio is on one of lrugen->folios[]. Otherwise it stores 0. * - * A page is added to the youngest generation on faulting. The aging needs to - * check the accessed bit at least twice before handing this page over to the - * eviction. The first check takes care of the accessed bit set on the initial - * fault; the second check makes sure this page hasn't been used since then. - * This process, AKA second chance, requires a minimum of two generations, - * hence MIN_NR_GENS. And to maintain ABI compatibility with the active/inactive - * LRU, e.g., /proc/vmstat, these two generations are considered active; the - * rest of generations, if they exist, are considered inactive. See - * lru_gen_is_active(). + * After a folio is faulted in, the aging needs to check the accessed bit at + * least twice before handing this folio over to the eviction. The first check + * clears the accessed bit from the initial fault; the second check makes sure + * this folio hasn't been used since then. This process, AKA second chance, + * requires a minimum of two generations, hence MIN_NR_GENS. And to maintain ABI + * compatibility with the active/inactive LRU, e.g., /proc/vmstat, these two + * generations are considered active; the rest of generations, if they exist, + * are considered inactive. See lru_gen_is_active(). * - * PG_active is always cleared while a page is on one of lrugen->folios[] so - * that the aging needs not to worry about it. And it's set again when a page - * considered active is isolated for non-reclaiming purposes, e.g., migration. - * See lru_gen_add_folio() and lru_gen_del_folio(). + * PG_active is always cleared while a folio is on one of lrugen->folios[] so + * that the sliding window needs not to worry about it. And it's set again when + * a folio considered active is isolated for non-reclaiming purposes, e.g., + * migration. See lru_gen_add_folio() and lru_gen_del_folio(). * * MAX_NR_GENS is set to 4 so that the multi-gen LRU can support twice the * number of categories of the active/inactive LRU when keeping track of * accesses through page tables. This requires order_base_2(MAX_NR_GENS+1) bits - * in folio->flags. + * in folio->flags, masked by LRU_GEN_MASK. */ #define MIN_NR_GENS 2U #define MAX_NR_GENS 4U /* - * Each generation is divided into multiple tiers. A page accessed N times - * through file descriptors is in tier order_base_2(N). A page in the first tier - * (N=0,1) is marked by PG_referenced unless it was faulted in through page - * tables or read ahead. A page in any other tier (N>1) is marked by - * PG_referenced and PG_workingset. This implies a minimum of two tiers is - * supported without using additional bits in folio->flags. + * Each generation is divided into multiple tiers. A folio accessed N times + * through file descriptors is in tier order_base_2(N). A folio in the first + * tier (N=0,1) is marked by PG_referenced unless it was faulted in through page + * tables or read ahead. A folio in the last tier (MAX_NR_TIERS-1) is marked by + * PG_workingset. A folio in any other tier (1flags. * * In contrast to moving across generations which requires the LRU lock, moving * across tiers only involves atomic operations on folio->flags and therefore * has a negligible cost in the buffered access path. In the eviction path, - * comparisons of refaulted/(evicted+protected) from the first tier and the - * rest infer whether pages accessed multiple times through file descriptors - * are statistically hot and thus worth protecting. + * comparisons of refaulted/(evicted+protected) from the first tier and the rest + * infer whether folios accessed multiple times through file descriptors are + * statistically hot and thus worth protecting. * * MAX_NR_TIERS is set to 4 so that the multi-gen LRU can support twice the * number of categories of the active/inactive LRU when keeping track of * accesses through file descriptors. This uses MAX_NR_TIERS-2 spare bits in - * folio->flags. + * folio->flags, masked by LRU_REFS_MASK. */ #define MAX_NR_TIERS 4U #ifndef __GENERATING_BOUNDS_H -struct lruvec; -struct page_vma_mapped_walk; - #define LRU_GEN_MASK ((BIT(LRU_GEN_WIDTH) - 1) << LRU_GEN_PGOFF) #define LRU_REFS_MASK ((BIT(LRU_REFS_WIDTH) - 1) << LRU_REFS_PGOFF) +/* + * For folios accessed multiple times through file descriptors, + * lru_gen_inc_refs() sets additional bits of LRU_REFS_WIDTH in folio->flags + * after PG_referenced, then PG_workingset after LRU_REFS_WIDTH. After all its + * bits are set, i.e., LRU_REFS_FLAGS|BIT(PG_workingset), a folio is lazily + * promoted into the second oldest generation in the eviction path. And when + * folio_inc_gen() does that, it clears LRU_REFS_FLAGS so that + * lru_gen_inc_refs() can start over. Note that for this case, LRU_REFS_MASK is + * only valid when PG_referenced is set. + * + * For folios accessed multiple times through page tables, folio_update_gen() + * from a page table walk or lru_gen_set_refs() from a rmap walk sets + * PG_referenced after the accessed bit is cleared for the first time. + * Thereafter, those two paths set PG_workingset and promote folios to the + * youngest generation. Like folio_inc_gen(), folio_update_gen() also clears + * PG_referenced. Note that for this case, LRU_REFS_MASK is not used. + * + * For both cases above, after PG_workingset is set on a folio, it remains until + * this folio is either reclaimed, or "deactivated" by lru_gen_clear_refs(). It + * can be set again if lru_gen_test_recent() returns true upon a refault. + */ +#define LRU_REFS_FLAGS (LRU_REFS_MASK | BIT(PG_referenced)) + +struct lruvec; +struct page_vma_mapped_walk; + #ifdef CONFIG_LRU_GEN enum { @@ -406,8 +428,6 @@ enum { NR_LRU_GEN_CAPS }; -#define LRU_REFS_FLAGS (BIT(PG_referenced) | BIT(PG_workingset)) - #define MIN_LRU_BATCH BITS_PER_LONG #define MAX_LRU_BATCH (MIN_LRU_BATCH * 64) diff --git a/mm/swap.c b/mm/swap.c index 15a94be8b0af..320b959b74c6 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -387,24 +387,19 @@ static void lru_gen_inc_refs(struct folio *folio) if (folio_test_unevictable(folio)) return; + /* see the comment on LRU_REFS_FLAGS */ if (!folio_test_referenced(folio)) { - folio_set_referenced(folio); + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); return; } - if (!folio_test_workingset(folio)) { - folio_set_workingset(folio); - return; - } - - /* see the comment on MAX_NR_TIERS */ do { - new_flags = old_flags & LRU_REFS_MASK; - if (new_flags == LRU_REFS_MASK) - break; + if ((old_flags & LRU_REFS_MASK) == LRU_REFS_MASK) { + folio_set_workingset(folio); + return; + } - new_flags += BIT(LRU_REFS_PGOFF); - new_flags |= old_flags & ~LRU_REFS_MASK; + new_flags = old_flags + BIT(LRU_REFS_PGOFF); } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); } @@ -414,7 +409,7 @@ static bool lru_gen_clear_refs(struct folio *folio) int type = folio_is_file_lru(folio); struct lru_gen_folio *lrugen = &folio_lruvec(folio)->lrugen; - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, 0); + set_mask_bits(&folio->flags, LRU_REFS_FLAGS | BIT(PG_workingset), 0); /* whether can do without shuffling under the LRU lock */ return gen == lru_gen_from_seq(READ_ONCE(lrugen->min_seq[type])); @@ -495,7 +490,7 @@ void folio_add_lru(struct folio *folio) folio_test_unevictable(folio), folio); VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); - /* see the comment in lru_gen_add_folio() */ + /* see the comment in lru_gen_distance() */ if (lru_gen_enabled() && !folio_test_unevictable(folio) && lru_gen_in_fault() && !(current->flags & PF_MEMALLOC)) folio_set_active(folio); diff --git a/mm/vmscan.c b/mm/vmscan.c index 02b01ae2bdbb..90bbc2b3be8b 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -862,6 +862,31 @@ enum folio_references { FOLIOREF_ACTIVATE, }; +#ifdef CONFIG_LRU_GEN +/* + * Only used on a mapped folio in the eviction (rmap walk) path, where promotion + * needs to be done by taking the folio off the LRU list and then adding it back + * with PG_active set. In contrast, the aging (page table walk) path uses + * folio_update_gen(). + */ +static bool lru_gen_set_refs(struct folio *folio) +{ + /* see the comment on LRU_REFS_FLAGS */ + if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) { + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); + return false; + } + + set_mask_bits(&folio->flags, LRU_REFS_FLAGS, BIT(PG_workingset)); + return true; +} +#else +static bool lru_gen_set_refs(struct folio *folio) +{ + return false; +} +#endif /* CONFIG_LRU_GEN */ + static enum folio_references folio_check_references(struct folio *folio, struct scan_control *sc) { @@ -870,7 +895,6 @@ static enum folio_references folio_check_references(struct folio *folio, referenced_ptes = folio_referenced(folio, 1, sc->target_mem_cgroup, &vm_flags); - referenced_folio = folio_test_clear_referenced(folio); /* * The supposedly reclaimable folio was found to be in a VM_LOCKED vma. @@ -888,6 +912,16 @@ static enum folio_references folio_check_references(struct folio *folio, if (referenced_ptes == -1) return FOLIOREF_KEEP; + if (lru_gen_enabled()) { + if (!referenced_ptes) + return FOLIOREF_RECLAIM; + + lru_gen_set_refs(folio); + return FOLIOREF_ACTIVATE; + } + + referenced_folio = folio_test_clear_referenced(folio); + if (referenced_ptes) { /* * All mapped folios start out with page table @@ -1092,11 +1126,6 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (!sc->may_unmap && folio_mapped(folio)) goto keep_locked; - /* folio_update_gen() tried to promote this page? */ - if (lru_gen_enabled() && !ignore_references && - folio_mapped(folio) && folio_test_referenced(folio)) - goto keep_locked; - /* * The number of dirty pages determines if a node is marked * reclaim_congested. kswapd will stall and start writing @@ -3163,16 +3192,19 @@ static int folio_update_gen(struct folio *folio, int gen) VM_WARN_ON_ONCE(gen >= MAX_NR_GENS); + /* see the comment on LRU_REFS_FLAGS */ + if (!folio_test_referenced(folio) && !folio_test_workingset(folio)) { + set_mask_bits(&folio->flags, LRU_REFS_MASK, BIT(PG_referenced)); + return -1; + } + do { /* lru_gen_del_folio() has isolated this page? */ - if (!(old_flags & LRU_GEN_MASK)) { - /* for shrink_folio_list() */ - new_flags = old_flags | BIT(PG_referenced); - continue; - } + if (!(old_flags & LRU_GEN_MASK)) + return -1; - new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); - new_flags |= (gen + 1UL) << LRU_GEN_PGOFF; + new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS); + new_flags |= ((gen + 1UL) << LRU_GEN_PGOFF) | BIT(PG_workingset); } while (!try_cmpxchg(&folio->flags, &old_flags, new_flags)); return ((old_flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1; @@ -3196,7 +3228,7 @@ static int folio_inc_gen(struct lruvec *lruvec, struct folio *folio, bool reclai new_gen = (old_gen + 1) % MAX_NR_GENS; - new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_MASK | LRU_REFS_FLAGS); + new_flags = old_flags & ~(LRU_GEN_MASK | LRU_REFS_FLAGS); new_flags |= (new_gen + 1UL) << LRU_GEN_PGOFF; /* for folio_end_writeback() */ if (reclaiming) @@ -3374,9 +3406,11 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, struct pglist_data *pgdat) { - struct folio *folio; + struct folio *folio = pfn_folio(pfn); + + if (folio_lru_gen(folio) < 0) + return NULL; - folio = pfn_folio(pfn); if (folio_nid(folio) != pgdat->node_id) return NULL; @@ -3753,8 +3787,7 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness) while (!list_empty(head)) { struct folio *folio = lru_to_folio(head); int refs = folio_lru_refs(folio); - int tier = lru_tier_from_refs(refs); - int delta = folio_nr_pages(folio); + bool workingset = folio_test_workingset(folio); VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio); VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio); @@ -3764,8 +3797,14 @@ static bool inc_min_seq(struct lruvec *lruvec, int type, int swappiness) new_gen = folio_inc_gen(lruvec, folio, false); list_move_tail(&folio->lru, &lrugen->folios[new_gen][type][zone]); - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + delta); + /* don't count the workingset being lazily promoted */ + if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) { + int tier = lru_tier_from_refs(refs, workingset); + int delta = folio_nr_pages(folio); + + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + } if (!--remaining) return false; @@ -4138,12 +4177,10 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw) continue; } - old_gen = folio_lru_gen(folio); - if (old_gen < 0) - folio_set_referenced(folio); - else if (old_gen != new_gen) { - folio_clear_lru_refs(folio); - folio_activate(folio); + if (lru_gen_set_refs(folio)) { + old_gen = folio_lru_gen(folio); + if (old_gen >= 0 && old_gen != new_gen) + folio_activate(folio); } } @@ -4304,7 +4341,8 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c int zone = folio_zonenum(folio); int delta = folio_nr_pages(folio); int refs = folio_lru_refs(folio); - int tier = lru_tier_from_refs(refs); + bool workingset = folio_test_workingset(folio); + int tier = lru_tier_from_refs(refs, workingset); struct lru_gen_folio *lrugen = &lruvec->lrugen; VM_WARN_ON_ONCE_FOLIO(gen >= MAX_NR_GENS, folio); @@ -4326,14 +4364,17 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c } /* protected */ - if (tier > tier_idx || refs == BIT(LRU_REFS_WIDTH)) { - int hist = lru_hist_from_seq(lrugen->min_seq[type]); - + if (tier > tier_idx || refs + workingset == BIT(LRU_REFS_WIDTH) + 1) { gen = folio_inc_gen(lruvec, folio, false); - list_move_tail(&folio->lru, &lrugen->folios[gen][type][zone]); + list_move(&folio->lru, &lrugen->folios[gen][type][zone]); - WRITE_ONCE(lrugen->protected[hist][type][tier], - lrugen->protected[hist][type][tier] + delta); + /* don't count the workingset being lazily promoted */ + if (refs + workingset != BIT(LRU_REFS_WIDTH) + 1) { + int hist = lru_hist_from_seq(lrugen->min_seq[type]); + + WRITE_ONCE(lrugen->protected[hist][type][tier], + lrugen->protected[hist][type][tier] + delta); + } return true; } @@ -4353,8 +4394,7 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c } /* waiting for writeback */ - if (folio_test_locked(folio) || writeback || - (type == LRU_GEN_FILE && dirty)) { + if (writeback || (type == LRU_GEN_FILE && dirty)) { gen = folio_inc_gen(lruvec, folio, true); list_move(&folio->lru, &lrugen->folios[gen][type][zone]); return true; @@ -4383,13 +4423,12 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca return false; } - /* see the comment on MAX_NR_TIERS */ + /* see the comment on LRU_REFS_FLAGS */ if (!folio_test_referenced(folio)) - folio_clear_lru_refs(folio); + set_mask_bits(&folio->flags, LRU_REFS_MASK, 0); /* for shrink_folio_list() */ folio_clear_reclaim(folio); - folio_clear_referenced(folio); success = lru_gen_del_folio(lruvec, folio, true); VM_WARN_ON_ONCE_FOLIO(!success, folio); @@ -4585,25 +4624,16 @@ static int evict_folios(struct lruvec *lruvec, struct scan_control *sc, int swap continue; } - if (folio_test_reclaim(folio) && - (folio_test_dirty(folio) || folio_test_writeback(folio))) { - /* restore LRU_REFS_FLAGS cleared by isolate_folio() */ - if (folio_test_workingset(folio)) - folio_set_referenced(folio); - continue; - } - - if (skip_retry || folio_test_active(folio) || folio_test_referenced(folio) || - folio_mapped(folio) || folio_test_locked(folio) || - folio_test_dirty(folio) || folio_test_writeback(folio)) { - /* don't add rejected folios to the oldest generation */ - set_mask_bits(&folio->flags, LRU_REFS_MASK | LRU_REFS_FLAGS, - BIT(PG_active)); - continue; - } - /* retry folios that may have missed folio_rotate_reclaimable() */ - list_move(&folio->lru, &clean); + if (!skip_retry && !folio_test_active(folio) && !folio_mapped(folio) && + !folio_test_dirty(folio) && !folio_test_writeback(folio)) { + list_move(&folio->lru, &clean); + continue; + } + + /* don't add rejected folios to the oldest generation */ + if (!lru_gen_distance(folio, false)) + set_mask_bits(&folio->flags, LRU_REFS_FLAGS, BIT(PG_active)); } spin_lock_irq(&lruvec->lru_lock); diff --git a/mm/workingset.c b/mm/workingset.c index 2c310c29f51e..3662c0def77a 100644 --- a/mm/workingset.c +++ b/mm/workingset.c @@ -239,7 +239,8 @@ static void *lru_gen_eviction(struct folio *folio) int type = folio_is_file_lru(folio); int delta = folio_nr_pages(folio); int refs = folio_lru_refs(folio); - int tier = lru_tier_from_refs(refs); + bool workingset = folio_test_workingset(folio); + int tier = lru_tier_from_refs(refs, workingset); struct mem_cgroup *memcg = folio_memcg(folio); struct pglist_data *pgdat = folio_pgdat(folio); @@ -253,7 +254,7 @@ static void *lru_gen_eviction(struct folio *folio) hist = lru_hist_from_seq(min_seq); atomic_long_add(delta, &lrugen->evicted[hist][type][tier]); - return pack_shadow(mem_cgroup_id(memcg), pgdat, token, refs); + return pack_shadow(mem_cgroup_id(memcg), pgdat, token, workingset); } /* @@ -304,24 +305,20 @@ static void lru_gen_refault(struct folio *folio, void *shadow) lrugen = &lruvec->lrugen; hist = lru_hist_from_seq(READ_ONCE(lrugen->min_seq[type])); - /* see the comment in folio_lru_refs() */ - refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + workingset; - tier = lru_tier_from_refs(refs); + refs = (token & (BIT(LRU_REFS_WIDTH) - 1)) + 1; + tier = lru_tier_from_refs(refs, workingset); atomic_long_add(delta, &lrugen->refaulted[hist][type][tier]); - mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); - /* - * Count the following two cases as stalls: - * 1. For pages accessed through page tables, hotter pages pushed out - * hot pages which refaulted immediately. - * 2. For pages accessed multiple times through file descriptors, - * they would have been protected by sort_folio(). - */ - if (lru_gen_in_fault() || refs >= BIT(LRU_REFS_WIDTH) - 1) { - set_mask_bits(&folio->flags, 0, LRU_REFS_MASK | BIT(PG_workingset)); + /* see folio_add_lru() where folio_set_active() happens */ + if (lru_gen_in_fault()) + mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + type, delta); + + if (workingset) { + folio_set_workingset(folio); mod_lruvec_state(lruvec, WORKINGSET_RESTORE_BASE + type, delta); - } + } else + set_mask_bits(&folio->flags, LRU_REFS_MASK, (refs - 1UL) << LRU_REFS_PGOFF); unlock: rcu_read_unlock(); }