From patchwork Mon Nov 6 23:11:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447583 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 E9CB9C4332F for ; Mon, 6 Nov 2023 23:12:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA8E06B0151; Mon, 6 Nov 2023 18:12:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E579E6B015A; Mon, 6 Nov 2023 18:12:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD1F96B015C; Mon, 6 Nov 2023 18:12:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BC5176B0151 for ; Mon, 6 Nov 2023 18:12:02 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8959D80537 for ; Mon, 6 Nov 2023 23:12:02 +0000 (UTC) X-FDA: 81429079284.04.AF2B46C Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) by imf07.hostedemail.com (Postfix) with ESMTP id CFBA740014 for ; Mon, 6 Nov 2023 23:12:00 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FUll10Pu; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.167.178 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699312320; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=0AgAxCnqHSCbhu2l82ch3Qbwz+0S8FVvz1I/p/e9eTQ=; b=NEXrCHbRBv6dNmP9FNjXwbrq4oDjel8H1ykRUsJ0W+Gx1ueI3bTS8hh5LPzNNxv9g+4Uam gLaMKlqmOpaYIiPcaP05rEqpTQrOIhdnu33SZ1UgyZ0qVKl+pd1JAmS8fBUxsL9k4Rzbe1 +CnyOFsXHnrck+FyVHPWTfE9qAooB3M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699312320; a=rsa-sha256; cv=none; b=7wWv2lDSqCCri2RkGRtMf2Mhpul2HM30+hZOmI8cldPCvmzZBDE7eHvvjiMZwYFmf1wTx7 DPz4HeJkf7O7FQw+XiTrCIoGKXwabYXj8xEGNBr7VpAGn21KK1PiynIGV+9B6HoKrC2nvs dOk/1neOmUTxg05jTKrPj5UJOtz8Ie0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FUll10Pu; spf=pass (imf07.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.167.178 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3b3f6f330d4so2990840b6e.2 for ; Mon, 06 Nov 2023 15:12:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699312320; x=1699917120; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0AgAxCnqHSCbhu2l82ch3Qbwz+0S8FVvz1I/p/e9eTQ=; b=FUll10PuF0Vnyz7bSbjDIQpl9lRff5DQ1b10rLC5smrmnMRLgwNfkRP3VH58COIpkE sM/IMFFnCQKMG8IXc+g/iyq6LzJwTs78Qtk50E6obIz1+RBXxdAZSvmpDUZgGN5YqqXF CcJ5a7j0H1W3BiwxgmWW7uBMV2AzB0aP9UTLd8T9Dwp9IprWUj0l09I2dsEfDJe564uT bNA1fTuOyPaMQgGXf2Qp1zMB7xbFhalshCEVXDAmc+0K58QrY4upk7ft4u69nu7R1adp w05jlHmKthHvAH1se/I2z00GM/9wR0ymo3YD7bc+3a+f+XP3A/uryoY1zY21sHr+hWds LRwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699312320; x=1699917120; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0AgAxCnqHSCbhu2l82ch3Qbwz+0S8FVvz1I/p/e9eTQ=; b=J1a4Z5cCXFs0zSAOa8IK0MF3Y3e3RQ/eKva4HsuWfkVcJNGI1Oy2xFEYl4VQHdyXI8 pqWZElAf+6lSEKcIBrxQEeq1bR15DNAeFn+GT08NG5JDJ3yxoVf6XoUTTT5pM2ee23JL 5ft4dJ0hahCTmIqy9AK8UHqUtGNDiZgLJVBhJXFu3ZNOVVNoh39BBSlbC335f4OBXCcS TXK2wfvbG5PGom8ofq8kgGIDt7g7zCZZqoyRyVKZse0qCMP4gUfbeQfGPMblE22dtmiQ TQvxC/VBIYo22CtpqGrFxbUgDbrzbsIlZiVuJq2NH+Wy926DomY46nvOSw/NM/fWOek7 DnPg== X-Gm-Message-State: AOJu0YxxLesKikFy8VYPDbjKBnfisLrZDFjy/VgWFZQoRE+wygPZv/4P xY3d+XkPk/QI3sE7F15PTXY= X-Google-Smtp-Source: AGHT+IEkH4gSYwuaYo8Qq0rj8LuQ2tFYsy0RhnE4WabPD6656bH19h5X4INeyPMOILQpQ0UP30uq3g== X-Received: by 2002:a05:6808:6cf:b0:3a7:55f2:552d with SMTP id m15-20020a05680806cf00b003a755f2552dmr30041560oih.58.1699312319617; Mon, 06 Nov 2023 15:11:59 -0800 (PST) Received: from localhost (fwdproxy-prn-118.fbsv.net. [2a03:2880:ff:76::face:b00c]) by smtp.gmail.com with ESMTPSA id d19-20020aa78693000000b006be2f4105d0sm6268079pfo.175.2023.11.06.15.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 15:11:59 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, cerasuolodomenico@gmail.com, yosryahmed@google.com, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, hughd@google.com, corbet@lwn.net, konrad.wilk@oracle.com, senozhatsky@chromium.org, rppt@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, david@ixit.cz Subject: [PATCH v4] zswap: memcontrol: implement zswap writeback disabling Date: Mon, 6 Nov 2023 15:11:58 -0800 Message-Id: <20231106231158.380730-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: CFBA740014 X-Rspam-User: X-Stat-Signature: sbqt1ehxyrfqwykix1b3yixz6siczf9k X-Rspamd-Server: rspam03 X-HE-Tag: 1699312320-60713 X-HE-Meta: U2FsdGVkX198DGhWwHoqe0zO9tmV6yYqzMrUFTB9TwA2DgMCBmNQ+4sgvLSrC/EMLqiQlnoi3jiwW9PXz1lzP8tYg9uvAb+47r8bsYTVrs1pIfGXGdumMSlJyvA3glaQVzRQAdoLa53eZlWV7+P+wJf21PI0VQZdA6TfSB6VyfzW1pmhpIAJGZtQoWPuzdFExRicG4L5GnUB2PWc8gSKU7Gp5S+jCNmnOk+9LRW97flk/0OQiuKM3h8O1BQvBgGAq9CErAXbJ67qSmBTf1EL6v1LiuL76B3+m/fL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ToinHuZ7qM3EwY2csIoiHnGkcJnX4VIZCzBky28EjQetnXgRFYWREDjenK5DvVFhrQvpiRx6ctVkkHbvEIgPWbBvyboyzvV+SoXR6Ghro= 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: During our experiment with zswap, we sometimes observe swap IOs due to occasional zswap store failures and writebacks-to-swap. These swapping IOs prevent many users who cannot tolerate swapping from adopting zswap to save memory and improve performance where possible. This patch adds the option to disable this behavior entirely: do not writeback to backing swapping device when a zswap store attempt fail, and do not write pages in the zswap pool back to the backing swap device (both when the pool is full, and when the new zswap shrinker is called). This new behavior can be opted-in/out on a per-cgroup basis via a new cgroup file. By default, writebacks to swap device is enabled, which is the previous behavior. Initially, writeback is enabled for the root cgroup, and a newly created cgroup will inherit the current setting of its parent. Note that this is subtly different from setting memory.swap.max to 0, as it still allows for pages to be stored in the zswap pool (which itself consumes swap space in its current form). Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham Acked-by: Chris Li Reviewed-by: Yosry Ahmed --- Documentation/admin-guide/cgroup-v2.rst | 12 ++++++++ Documentation/admin-guide/mm/zswap.rst | 6 ++++ include/linux/memcontrol.h | 12 ++++++++ include/linux/zswap.h | 6 ++++ mm/memcontrol.c | 41 +++++++++++++++++++++++++ mm/page_io.c | 6 ++++ mm/shmem.c | 3 +- mm/zswap.c | 14 +++++++++ 8 files changed, 98 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 606b2e0eac4b..eefccfffdff7 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1672,6 +1672,18 @@ PAGE_SIZE multiple when read back. limit, it will refuse to take any more stores before existing entries fault back in or are written out to disk. + memory.zswap.writeback + A read-write single value file. The default value is "1". The + initial value of the root cgroup is 1, and when a new cgroup is + created, it inherits the current value of its parent. + + When this is set to 0, all swapping attempts to swapping devices + are disabled. This included both zswap writebacks, and swapping due + to zswap store failure. + + Note that this is subtly different from setting memory.swap.max to + 0, as it still allows for pages to be written to the zswap pool. + memory.pressure A read-only nested-keyed file. diff --git a/Documentation/admin-guide/mm/zswap.rst b/Documentation/admin-guide/mm/zswap.rst index 522ae22ccb84..b987e58edb70 100644 --- a/Documentation/admin-guide/mm/zswap.rst +++ b/Documentation/admin-guide/mm/zswap.rst @@ -153,6 +153,12 @@ attribute, e. g.:: Setting this parameter to 100 will disable the hysteresis. +Some users cannot tolerate the swapping that comes with zswap store failures +and zswap writebacks. Swapping can be disabled entirely (without disabling +zswap itself) on a cgroup-basis as follows: + + echo 0 > /sys/fs/cgroup//memory.zswap.writeback + When there is a sizable amount of cold memory residing in the zswap pool, it can be advantageous to proactively write these cold pages to swap and reclaim the memory for other use cases. By default, the zswap shrinker is disabled. diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 95f6c9e60ed1..e51eafdf2a15 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -219,6 +219,12 @@ struct mem_cgroup { #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) unsigned long zswap_max; + + /* + * Prevent pages from this memcg from being written back from zswap to + * swap, and from being swapped out on zswap store failures. + */ + bool zswap_writeback; #endif unsigned long soft_limit; @@ -1931,6 +1937,7 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size); +bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg); #else static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) { @@ -1944,6 +1951,11 @@ static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) { } +static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) +{ + /* if zswap is disabled, do not block pages going to the swapping device */ + return true; +} #endif #endif /* _LINUX_MEMCONTROL_H */ diff --git a/include/linux/zswap.h b/include/linux/zswap.h index cbd373ba88d2..b4997e27a74b 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -35,6 +35,7 @@ void zswap_swapoff(int type); void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); void zswap_lruvec_state_init(struct lruvec *lruvec); void zswap_lruvec_swapin(struct page *page); +bool is_zswap_enabled(void); #else struct zswap_lruvec_state {}; @@ -55,6 +56,11 @@ static inline void zswap_swapoff(int type) {} static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} static inline void zswap_lruvec_init(struct lruvec *lruvec) {} static inline void zswap_lruvec_swapin(struct page *page) {} + +static inline bool is_zswap_enabled(void) +{ + return false; +} #endif #endif /* _LINUX_ZSWAP_H */ diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e43b5aba8efc..9cb3ea912cbe 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5545,6 +5545,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) memcg->zswap_max = PAGE_COUNTER_MAX; + + if (parent) + WRITE_ONCE(memcg->zswap_writeback, READ_ONCE(parent->zswap_writeback)); + else + WRITE_ONCE(memcg->zswap_writeback, true); #endif page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); if (parent) { @@ -8177,6 +8182,12 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) rcu_read_unlock(); } +bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) +{ + /* if zswap is disabled, do not block pages going to the swapping device */ + return !is_zswap_enabled() || !memcg || READ_ONCE(memcg->zswap_writeback); +} + static u64 zswap_current_read(struct cgroup_subsys_state *css, struct cftype *cft) { @@ -8209,6 +8220,31 @@ static ssize_t zswap_max_write(struct kernfs_open_file *of, return nbytes; } +static int zswap_writeback_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + + seq_printf(m, "%d\n", READ_ONCE(memcg->zswap_writeback)); + return 0; +} + +static ssize_t zswap_writeback_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); + int zswap_writeback; + ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &zswap_writeback); + + if (parse_ret) + return parse_ret; + + if (zswap_writeback != 0 && zswap_writeback != 1) + return -EINVAL; + + WRITE_ONCE(memcg->zswap_writeback, zswap_writeback); + return nbytes; +} + static struct cftype zswap_files[] = { { .name = "zswap.current", @@ -8221,6 +8257,11 @@ static struct cftype zswap_files[] = { .seq_show = zswap_max_show, .write = zswap_max_write, }, + { + .name = "zswap.writeback", + .seq_show = zswap_writeback_show, + .write = zswap_writeback_write, + }, { } /* terminate */ }; #endif /* CONFIG_MEMCG_KMEM && CONFIG_ZSWAP */ diff --git a/mm/page_io.c b/mm/page_io.c index cb559ae324c6..5e606f1aa2f6 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -201,6 +201,12 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_end_writeback(folio); return 0; } + + if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) { + folio_mark_dirty(folio); + return AOP_WRITEPAGE_ACTIVATE; + } + __swap_writepage(&folio->page, wbc); return 0; } diff --git a/mm/shmem.c b/mm/shmem.c index cab053831fea..e5044678de8b 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1514,8 +1514,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc) mutex_unlock(&shmem_swaplist_mutex); BUG_ON(folio_mapped(folio)); - swap_writepage(&folio->page, wbc); - return 0; + return swap_writepage(&folio->page, wbc); } mutex_unlock(&shmem_swaplist_mutex); diff --git a/mm/zswap.c b/mm/zswap.c index 260e01180ee0..051f4487c9ab 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -150,6 +150,11 @@ module_param_named(exclusive_loads, zswap_exclusive_loads_enabled, bool, 0644); static bool zswap_shrinker_enabled; module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644); +bool is_zswap_enabled(void) +{ + return zswap_enabled; +} + /********************************* * data structures **********************************/ @@ -590,6 +595,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, struct zswap_pool *pool = shrinker->private_data; bool encountered_page_in_swapcache = false; + if (!mem_cgroup_zswap_writeback_enabled(sc->memcg)) + return SHRINK_STOP; + nr_protected = atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); lru_size = list_lru_shrink_count(&pool->list_lru, sc); @@ -620,6 +628,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; + if (!mem_cgroup_zswap_writeback_enabled(memcg)) + return 0; + #ifdef CONFIG_MEMCG_KMEM cgroup_rstat_flush(memcg->css.cgroup); nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT; @@ -935,6 +946,9 @@ static int shrink_memcg(struct mem_cgroup *memcg) struct zswap_pool *pool; int nid, shrunk = 0; + if (!mem_cgroup_zswap_writeback_enabled(memcg)) + return -EINVAL; + /* * Skip zombies because their LRUs are reparented and we would be * reclaiming from the parent instead of the dead memcg.