From patchwork Wed Mar 20 02:08:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13597211 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 E7591C54E71 for ; Wed, 20 Mar 2024 02:08:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6768B6B009A; Tue, 19 Mar 2024 22:08:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 627656B009B; Tue, 19 Mar 2024 22:08:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4EF8B6B009C; Tue, 19 Mar 2024 22:08:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3F4186B009A for ; Tue, 19 Mar 2024 22:08:28 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1AD261C0520 for ; Wed, 20 Mar 2024 02:08:28 +0000 (UTC) X-FDA: 81915783096.03.CD164B5 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf11.hostedemail.com (Postfix) with ESMTP id 752B840002 for ; Wed, 20 Mar 2024 02:08:26 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="F/yF2zb3"; spf=pass (imf11.hostedemail.com: domain of 3GUX6ZQoKCMI6w0z6ipumlowwotm.kwutqv25-uus3iks.wzo@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3GUX6ZQoKCMI6w0z6ipumlowwotm.kwutqv25-uus3iks.wzo@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710900506; a=rsa-sha256; cv=none; b=K3Uj0ZlQ2IzzVuejPtE9rEjWpn8kBeTvHKdCb/4pQTlCE0/4iQwfyj58eubyT49sPbBJ/N N24vwX2CeZozpxROlANb9bS06HzmLExDwQsD/Dqf+lWfyv4goHrUCZH4JMZQ2bOerOYGY0 kp4Ycg+Y/3/uCt7VteLAarHf4PC/K/g= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="F/yF2zb3"; spf=pass (imf11.hostedemail.com: domain of 3GUX6ZQoKCMI6w0z6ipumlowwotm.kwutqv25-uus3iks.wzo@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3GUX6ZQoKCMI6w0z6ipumlowwotm.kwutqv25-uus3iks.wzo@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710900506; 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: references:dkim-signature; bh=yYVHze7eFk58oRqMFdcZXvqqb2SqrJB7jmdU82oAHB0=; b=7RfyF7Vex9sDotqr6fpvfcbBw4yJ8uvuMIPjDHJasZivLDH1YOt1V2LaVqChdlKxYerE3V ckzKC85Pjg3kZm/7tHLEcpnq29+CMuOG0gaz3Rf7aGjyPmeISrNiK9P8AAZLIquJJmiFmn g0RwsAwwE8PPWkUdCMiCKMVMKz6mlhw= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-610f160f43fso10511437b3.2 for ; Tue, 19 Mar 2024 19:08:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710900505; x=1711505305; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yYVHze7eFk58oRqMFdcZXvqqb2SqrJB7jmdU82oAHB0=; b=F/yF2zb3h4NB/o6giWnZAhaSL0WL2EfE72mRb0syUOvR7FYBt4ebJNIwBDNSFck98o l9IVSKNvzhLxNijv5Li3vE2duxVMn/2mv+OxpCufuciy113zO22Drkb+WDv1I9flW8Nh 15mYxc00zexu0nWOU5GcaeEctv9tcLuKTkNsqRn6OKRuK4gM3I+PiOWvbVE6IuesCNVf 6Pk0PIewlD9kv4DqR/HH3DMuFZ0lI2aLmNU9kyxnFYkrN8MgfAb/BratRNbf8KnkuMVy WnOPpA/07EeohrZCsiCI9OjE2lKKUnhWyrNzwA061V9Fp6Ze/pY5BG0Jm94Lela12ICE gZRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710900505; x=1711505305; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yYVHze7eFk58oRqMFdcZXvqqb2SqrJB7jmdU82oAHB0=; b=bIHTRMI7BIXVC7peTSEa9q8qYXJn0UHU8HP51VNvG4pvFaLXMOhY8dYidGTBDGZbsy 9/06yfYOvfGjOQWfZNuQSiyeVupUm9BwqeKOTv44thEDiwOq7NknTd+GHJLYyBc/xP44 Vw/PPADYIWMF1TaBdyWEf1NnUgolSkOWYlKT8XY9aLjX79fk0CwneG6vHPYmM6+getLt pPouNLsFE3h14q30qT+PwZnWYraTRzGO8c09xuzy5JOjI0ngkEOC8qHN/j4V6amw2b3g uiLqbVV/dlKwnTzki+9L2Z+vF0W7op0n+CumMObOYS92FvlD7RWHwhJzaFY6J925lRxZ fOQA== X-Forwarded-Encrypted: i=1; AJvYcCVGEu8MPTZz1pn1IK1HHMZgoz89aBnzjuoxEXphP1ecOqyMx7EnC+DEC7/hPmeR7z/rRk+UQfZ3vOwYLapjWFNYYuA= X-Gm-Message-State: AOJu0YwM4wAGOzdv3QL2Epc36Vkecryr9zk7BDI+lNHPQL1UPtzPWHR2 35CWPFoTftV2IvB73pJavdn+h10NwDf1jTJ1vkKkt34uNSI+WxVm9xqVm2OB1XozoVzpNmqvOml X/usaq1RjxLX68CjNow== X-Google-Smtp-Source: AGHT+IHmnbH7OIFT3vXCgDBMNN/LSpOqXUQlqADVGRo/DP6oT7pIALKkMG8DtSLjyTbflgHEateyR8upPPsuWMd6 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:70e:b0:dcc:6bf0:2eb6 with SMTP id k14-20020a056902070e00b00dcc6bf02eb6mr966645ybt.6.1710900505416; Tue, 19 Mar 2024 19:08:25 -0700 (PDT) Date: Wed, 20 Mar 2024 02:08:22 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240320020823.337644-1-yosryahmed@google.com> Subject: [PATCH 1/2] mm: zswap: increase shrinking protection for zswap swapins only From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 752B840002 X-Stat-Signature: iscrdyd5qy8bww4ektgrhibbs6aua3w5 X-Rspam-User: X-HE-Tag: 1710900506-381817 X-HE-Meta: U2FsdGVkX1/DTcWUmiOUVWNi59wZn0uRM/uUhi+L8RGOlqmEX9wEki82PpFvuBCBwD9eMvdq/p3OKQ0l2pliSYbMLDn0iyjQI+3y5W3dj6GjA1GFBZsDJSPsJDeAhC+VcEfApcF3CnT0ot+7WByKE+FbYalVqHmUWz3e00Oj1UfP9YecD+ZAqxJSW/ktiE70ff+UvHLHo3+J5ddPl/jtXkgCeFNqu5GS+eoryWWd8WzBM6uXg+QbyHRKcn3QiGvD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DGaKuyu0S25pdz7GptQAoGqhxYR9MQEmMX/wksCSBRBhVAFAS+zE5diC6ealgmFMXxXAn4grr5Wmh607h40HqfLyNXLuXotz0trpV59z4fTU+LlfhqeKtgpgW1E4OEKArNzT873qwnxpq1VbwNK0HDEWPvB8UoVCIFq//zKT18C9MFTOb/s3e29or7dKfmUZaNFDzgvfAunisrCuV5sT5HoVycJ3F1rGr5PZJibTEpmsENNIFcLJpcN7r5dfSPaeYppS47r2Dbqp+UdrU7F25aw2UO658L++D5rzav6vHreK0H1y07Bm+u11TvaBaGw= 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: Currently, the number of protected zswap entries corresponding to an lruvec are incremented every time we swapin a page. This happens regardless of whether or not the page originated in zswap. Hence, swapins from disk will lead to increasing protection on potentially stale zswap entries. Furthermore, the increased shrinking protection can lead to more pages skipping zswap and going to disk, eventually leading to even more swapins from disk and starting a vicious circle. Instead, only increase the protection when pages are loaded from zswap. This also has a nice side effect of removing zswap_folio_swapin() and replacing it with a static helper that is only called from zswap_load(). No problems were observed in practice, this was found through code inspection. Signed-off-by: Yosry Ahmed --- include/linux/zswap.h | 2 -- mm/swap_state.c | 8 ++------ mm/zswap.c | 10 +++------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 2a85b941db975..1f020b5427e3d 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -34,7 +34,6 @@ int zswap_swapon(int type, unsigned long nr_pages); void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); void zswap_lruvec_state_init(struct lruvec *lruvec); -void zswap_folio_swapin(struct folio *folio); bool is_zswap_enabled(void); #else @@ -58,7 +57,6 @@ static inline int zswap_swapon(int type, unsigned long nr_pages) static inline void zswap_swapoff(int type) {} static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} static inline void zswap_lruvec_state_init(struct lruvec *lruvec) {} -static inline void zswap_folio_swapin(struct folio *folio) {} static inline bool is_zswap_enabled(void) { diff --git a/mm/swap_state.c b/mm/swap_state.c index bfc7e8c58a6d3..32e151054ec47 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -696,10 +696,8 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, /* The page was likely read above, so no need for plugging here */ folio = __read_swap_cache_async(entry, gfp_mask, mpol, ilx, &page_allocated, false); - if (unlikely(page_allocated)) { - zswap_folio_swapin(folio); + if (unlikely(page_allocated)) swap_read_folio(folio, false, NULL); - } return folio; } @@ -872,10 +870,8 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, /* The folio was likely read above, so no need for plugging here */ folio = __read_swap_cache_async(targ_entry, gfp_mask, mpol, targ_ilx, &page_allocated, false); - if (unlikely(page_allocated)) { - zswap_folio_swapin(folio); + if (unlikely(page_allocated)) swap_read_folio(folio, false, NULL); - } return folio; } diff --git a/mm/zswap.c b/mm/zswap.c index b31c977f53e9c..323f1dea43d22 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -773,14 +773,9 @@ void zswap_lruvec_state_init(struct lruvec *lruvec) atomic_long_set(&lruvec->zswap_lruvec_state.nr_zswap_protected, 0); } -void zswap_folio_swapin(struct folio *folio) +static void zswap_lruvec_inc_protected(struct lruvec *lruvec) { - struct lruvec *lruvec; - - if (folio) { - lruvec = folio_lruvec(folio); - atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); - } + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); } void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) @@ -1644,6 +1639,7 @@ bool zswap_load(struct folio *folio) zswap_entry_free(entry); folio_mark_dirty(folio); + zswap_lruvec_inc_protected(folio_lruvec(folio)); return true; } From patchwork Wed Mar 20 02:08:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13597212 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 CBE2DCD11BF for ; Wed, 20 Mar 2024 02:08:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58D6B6B009C; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 53E5D6B009D; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 342B56B009E; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) 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 181606B009C for ; Tue, 19 Mar 2024 22:08:30 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C40991606C1 for ; Wed, 20 Mar 2024 02:08:29 +0000 (UTC) X-FDA: 81915783138.27.3A11EF3 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 0B5D920005 for ; Wed, 20 Mar 2024 02:08:27 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nr92ik4T; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710900508; 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=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=wMyY06EujWz7U3Sb30J7klYivcU3fHvVviKnrDIjAua99QfzE2gdh1QiY6eJSm+cQ661Ic M56SXxY+p8fqBFa4kISxoHikvm3/vDA1ShRIfmECMXJuwNMudMmyEgSwDA126hQpOpsdFV xPBPj4upzurIn9lcZgl2WAnx2TmxSsw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nr92ik4T; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3G0X6ZQoKCMQ8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710900508; a=rsa-sha256; cv=none; b=B+Fs52NJ1g3ZJDcs0CJknNv2B6v4zcq40DDGhhF+Okym5eS82cTNGGDzG+ng8m4cG7aS4N ltuAUuyzeFfFGgoxyf8vnwlBMAMERO1FQox9XbLyZNtQfE3mI/xkBQCmrYufVcwvYQLxvF AqfyUERR2CBZXW+vaHw/4XXDJNw74Vg= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60f9d800a29so76023587b3.0 for ; Tue, 19 Mar 2024 19:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710900507; x=1711505307; 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=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=nr92ik4TjIqF8P/OnSCFULuZoYrf4ggvgy5y2oNw2P7pcBBIDGdK3TdCnnif33TYnT wHe7wm+LJyMbu1FPUXAIOrst4DFWL0Mi5do9jECK9s0Sr70YTgZ60qi8jwVy+a7pErvn E+JQqPeu6Edge3hAqu7EJOB4f0Znsh/v9RRlr+TAc0ruHEUEsc3Z6omTjEpsV+WseisX aDPM6RagNg3FkCslu0xA3vd5wWODW6q8VLGNUMV+ZgeIZoD9shs+VE9exJXIcMF3rmCz +IC8KOnNOqtA0aob0vTudwZtPamMpt0Xfh3injNGU359OGGzgS9gI/H1qXaQsmBLzQht QGkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710900507; x=1711505307; 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=4f2cmQGIIvYk0KlMI8Lk64bezK2vGQGAMrw9pw7eCqU=; b=T3+r+ckbuAjAPGoDKrKFLfgKF/LxXmneElpchhGYWO+VKrfiBzpyH2nxYSqIF3+/TZ j9efjAPuruPYImxgmrhEvigEzneTbZZaugttHMz/8hXhIplOz60ogwR2owZrrpGRhIrp vt6x3P5y0GOg41KL8Y1Z7Du03hA7R9qlaMmibQwjzFqrM692hglSMpBsTNeYkSNtA8ub 4yBpmvwJyoFYR6u7RId9RUhi4cVGFYxWi9MHuUo//tPiXsUoUlkDmqBUp/j2Z7DuyiRL ObTDjnhVfA35VLRt5HMHd8eeH7qTWuKR7qxh+6iqT+vkS7YhSut1ta5tD+H6sZAsz5av 962Q== X-Forwarded-Encrypted: i=1; AJvYcCW6Zy+4qzn3zjqI9z7Xq3NkOtm0Z2SQVKUqpvK13ef538GHf49qaEuHftYcHZ4XLsq7o729SXvSDr89iI+RguBb2MM= X-Gm-Message-State: AOJu0Yz0p/n6H7YS5tRfx2v5P9AXOyoivzzNg1S8hCVsqSieM09wt5MW s8ORepoXTMRDMrpzODmPLWb0gnw5k0txj5EsxE9TiYjgTyZyLw7xpj9lAIEVSmXkHpa3A1Ds42N PkXG9zKlI8jLtCMY2XQ== X-Google-Smtp-Source: AGHT+IFsvhJOuJBMAwJK0L9AcJGw/zZI7HjSUBr+0ZmCaaIAiZxx6HFfHdp9ILj8xjHSBNtVwPjPcmu8Za+Wats5 X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a0d:d54e:0:b0:60a:2307:e3b with SMTP id x75-20020a0dd54e000000b0060a23070e3bmr3040512ywd.3.1710900507103; Tue, 19 Mar 2024 19:08:27 -0700 (PDT) Date: Wed, 20 Mar 2024 02:08:23 +0000 In-Reply-To: <20240320020823.337644-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20240320020823.337644-1-yosryahmed@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240320020823.337644-2-yosryahmed@google.com> Subject: [PATCH 2/2] mm: zswap: remove nr_zswap_stored atomic From: Yosry Ahmed To: Andrew Morton Cc: Johannes Weiner , Nhat Pham , Chengming Zhou , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yosry Ahmed X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 0B5D920005 X-Stat-Signature: 8bb65gwsete8m58gkjyxqszki6g1xu77 X-HE-Tag: 1710900507-256696 X-HE-Meta: U2FsdGVkX1/UhaQyW+bigzuJ/22wsMUAgL/A+OTRpbbyvhVHwyBFCs22h0LXeHLqyUqBLXaK7dlDBC16vNvAm7e/jix86NEnhaNx9gZ+AEl4GzY6ddN2vmlF5fGmJjMPQdAgixpdI2EJptERFOKk7ZElmVtHKyI6JbtE0cECAffODmwzZBisThk+3VxykL7Qa8E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EXRCBwcFMvsDyoQmt5bDzstFp96qq06Uv+kSSW8kH9I89VOWcFDfs/PynVA4Zd+KPZUFbtsdKMM82CdKSMzjvRt3l3w3LwX+pPm/YM2rnQzrZlVw66yEXaT1q5edjh4wVYGKr17ZjPDSkrmEheNz4LnxO8gRA+wyWzCEJu1XBW5IqD6HSAIIMjBayKSFjcPIApD3+iNo2oksK+dxpDz1pIY0KpczIyYt+34C3aX9cQBgkZvQzYvq2Q5KrdL6eHwOmUjGVI7CJPX4Se17qQkPDAOUdCzDTUMD1qWu8pOqVIzbb/9+/pDFwj9OIJO7OacpmUIUFI5y1QhnYyoaP9jgaDHflsPdqkj5QndyU8QMP8z9ZlRcVsP4CEjKT6x8vxVztDb6gAw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000120, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zswap_nr_stored is used to maintain the number of stored pages in zswap that are not same-filled pages. It is used in zswap_shrinker_count() to scale the number of freeable compressed pages by the compression ratio. That is, to reduce the amount of writeback from zswap with higher compression ratios as the ROI from IO diminishes. However, the need for this counter is questionable due to two reasons: - It is redundant. The value can be inferred from (zswap_stored_pages - zswap_same_filled_pages). - When memcgs are enabled, we use memcg_page_state(memcg, MEMCG_ZSWAPPED), which includes same-filled pages anyway (i.e. equivalent to zswap_stored_pages). Use zswap_stored_pages instead in zswap_shrinker_count() to keep things consistent whether memcgs are enabled or not, and add a comment about the number of freeable pages possibly being scaled down more than it should if we have lots of same-filled pages (i.e. inflated compression ratio). Remove nr_zswap_stored and one atomic operation in the store and free paths. Signed-off-by: Yosry Ahmed Reviewed-by: Nhat Pham --- mm/zswap.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 323f1dea43d22..ffcfce05a4408 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -181,8 +181,6 @@ struct zswap_pool { /* Global LRU lists shared by all zswap pools. */ static struct list_lru zswap_list_lru; -/* counter of pages stored in all zswap pools. */ -static atomic_t zswap_nr_stored = ATOMIC_INIT(0); /* The lock protects zswap_next_shrink updates. */ static DEFINE_SPINLOCK(zswap_shrink_lock); @@ -880,7 +878,6 @@ static void zswap_entry_free(struct zswap_entry *entry) else { zswap_lru_del(&zswap_list_lru, entry); zpool_free(zswap_find_zpool(entry), entry->handle); - atomic_dec(&zswap_nr_stored); zswap_pool_put(entry->pool); } if (entry->objcg) { @@ -1305,7 +1302,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, #else /* use pool stats instead of memcg stats */ nr_backing = zswap_total_pages(); - nr_stored = atomic_read(&zswap_nr_stored); + nr_stored = atomic_read(&zswap_stored_pages); #endif if (!nr_stored) @@ -1325,6 +1322,11 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, * This ensures that the better zswap compresses memory, the fewer * pages we will evict to swap (as it will otherwise incur IO for * relatively small memory saving). + * + * The memory saving factor calculated here takes same-filled pages into + * account, but those are not freeable since they almost occupy no + * space. Hence, we may scale nr_freeable down a little bit more than we + * should if we have a lot of same-filled pages. */ return mult_frac(nr_freeable, nr_backing, nr_stored); } @@ -1570,7 +1572,6 @@ bool zswap_store(struct folio *folio) if (entry->length) { INIT_LIST_HEAD(&entry->lru); zswap_lru_add(&zswap_list_lru, entry); - atomic_inc(&zswap_nr_stored); } spin_unlock(&tree->lock);