From patchwork Thu Nov 30 19:40:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13474954 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 09CF5C4167B for ; Thu, 30 Nov 2023 19:40:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8055B6B048A; Thu, 30 Nov 2023 14:40:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B48F6B048C; Thu, 30 Nov 2023 14:40:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67C206B048D; Thu, 30 Nov 2023 14:40:27 -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 581946B048A for ; Thu, 30 Nov 2023 14:40:27 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3088CA01A6 for ; Thu, 30 Nov 2023 19:40:27 +0000 (UTC) X-FDA: 81515637294.28.0D56CC8 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf05.hostedemail.com (Postfix) with ESMTP id 4F62B100007 for ; Thu, 30 Nov 2023 19:40:25 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IvxjyLVb; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.181 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=1701373225; 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=+x/LKk3FvB937MhuGOYKQ0IyJa8w9wJ6MIupisyP6ic=; b=Q9qUHcBCCwlbEHtWmOUtm6yGjm0xUz/C6OfBdl5F9E47196Y2WUjl3peF+nJ7oa+Q/GtXW deaxhW7Oo8/jXlgDceBKQuyu5Zn02jfXZtNz2ytN04jz+2oJYjKwOaoj+oBarHUC/kkaGH vR1Js5rAkW81TYZnAwjZqszz+/8mpNM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701373225; a=rsa-sha256; cv=none; b=NMY7BXQXrU0Tl1jZmWJLfLr2OdnAL2Y+QRQSj33RBbswFQqGE0aKZQzQVHITJlhjrPeiMY 7VqZ8LWW0sv8nPpdL6vqRZ24zOFd+YhoYU+FqJMZBQLIpTeSFZkQLyQFEcBMrxFEBeyd80 2yWDqmrpOdXwQxePaM/baT9BaHjgNXU= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IvxjyLVb; spf=pass (imf05.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6cbc8199a2aso1344589b3a.1 for ; Thu, 30 Nov 2023 11:40:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701373224; x=1701978024; 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=+x/LKk3FvB937MhuGOYKQ0IyJa8w9wJ6MIupisyP6ic=; b=IvxjyLVb745+HdqrGLWY26E9ij0adSRGsgSXymIL34PvxmkFuxivaek7nJHT78DkHQ MbqyaiMZDwddOsLMHX5iazR7/WtgjDQ8xG2VFe98pndxpVklsqgXa5o+3Kqxr1nL5yRY 98LFuJVACo2+STG5SeCaeGenZ1DSH+15k+QwXrOij1pKIc2krZC5VitftAdK19iz7SS7 E+ffqnPfrC+XD23wu3hXR64aIz43kxiNxUNAZb/rUN3DcTHUopar95qBgGUfLCr5KD8h FN8pu3O1lgKvb/6+PXDngglwEobakUsHLqsQ6/27IYmqDDwrvRe87CoQix2Jij5IxcbE ULQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701373224; x=1701978024; 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=+x/LKk3FvB937MhuGOYKQ0IyJa8w9wJ6MIupisyP6ic=; b=T8n9Ae9K7s4Mez+H5zg0cG5jmG3unEnLgnQJpkYaFMdiW0I2WaE3gfLlxFxl1lavYr pod/qMGpIzgIx3GrFIVQm2Z6oLdRWkACvkexwFePA4vOdtQNQR3GXq6W4fI4iztqnABu 0JdoHTbnZxBYQK2h1M/u8Zm12L8/P62lTp1ZYCB0RoN5k7dKK/i+bH+0kcGusCHVt+hf Pru0RN+L0HfffzjmVeh/hQ3x0gbnKdm66cBZB416/aUVArANxe3+sGWY4ZW4CAGfjXXn M0qH1WqpO7p4VzY9xGARyNab5cpc2JX9PHsd2LanRo1Kfh9rzy0NuiNgHhtW2YEVzEoj Nccw== X-Gm-Message-State: AOJu0Yxhg4Pu12lt8HaRc8TLyZeMwvFU6zuhNtfat6SAHjg+aSKQCaoU kX4YCx3f37okx6Kb3S5hzzY= X-Google-Smtp-Source: AGHT+IEmdSdnMH+kII1N7XUeBVLX4vq3m9jYQohZEcsNhiLq8IQwMxJyAHsNE5doFtymTJtoJnEBZg== X-Received: by 2002:a05:6a00:2e19:b0:6cb:a431:2d75 with SMTP id fc25-20020a056a002e1900b006cba4312d75mr27388691pfb.7.1701373223877; Thu, 30 Nov 2023 11:40:23 -0800 (PST) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id n5-20020a056a000d4500b006c341cf08f9sm1641980pfv.140.2023.11.30.11.40.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 11:40:23 -0800 (PST) From: Nhat Pham To: akpm@linux-foundation.org Cc: 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, chrisl@kernel.org, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, shuah@kernel.org Subject: [PATCH v8 0/6] workload-specific and memory pressure-driven zswap writeback Date: Thu, 30 Nov 2023 11:40:17 -0800 Message-Id: <20231130194023.4102148-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Stat-Signature: qg4c8dnhnfxwttuiaiey7c71ssjntgg8 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4F62B100007 X-Rspam-User: X-HE-Tag: 1701373225-111091 X-HE-Meta: U2FsdGVkX19VkoFFBHuPC9Uxp3P2kV4kmU9GcozX/0vWs9qD6dc5+YnSO/nzmAQM7240Qo4y0MyE76ydEwprSn11Fchr0KFy5KQDBcavKNqLKpwYzuun3Ladw/G8iirirJA7tBxpE9BTVcG/0kQzubVaMEH4ln2+kMhhKk00KVsgQcsZcF3vHASJs7iTZ0SBoZyH9cmYYx73p9uGHG6828FcaXYO/mlrhS3jOwbwtQBoyh8g72cnKImdseG8+WiX+v9KMIZeaS+Ub9M+lgpwhJIYMa3n3hKJWs1fKXes7oogcu5DjmajlGJVpLR7S0WbThvzfFilyalJOEAQUm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m2d8LUE5t2DvbqcSbaHmnIM4JseerOfVeBH57X/pv8eAbv5SC78kG7+fC78DQNfrSW2CaPsnClTl/9xA3JVxe8zQQgcOYfX5EPO7TxSZELzZtB 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: Changelog: v8: * Fixed a couple of build errors in the case of !CONFIG_MEMCG * Simplified the online memcg selection scheme for the zswap global limit reclaim (suggested by Michal Hocko and Johannes Weiner) (patch 2 and patch 3) * Added a new kconfig to allows users to enable zswap shrinker by default. (suggested by Johannes Weiner) (patch 6) v7: * Added the mem_cgroup_iter_online() function to the API for the new behavior (suggested by Andrew Morton) (patch 2) * Fixed a missing list_lru_del -> list_lru_del_obj (patch 1) v6: * Rebase on top of latest mm-unstable. * Fix/improve the in-code documentation of the new list_lru manipulation functions (patch 1) v5: * Replace reference getting with an rcu_read_lock() section for zswap lru modifications (suggested by Yosry) * Add a new prep patch that allows mem_cgroup_iter() to return online cgroup. * Add a callback that updates pool->next_shrink when the cgroup is offlined (suggested by Yosry Ahmed, Johannes Weiner) v4: * Rename list_lru_add to list_lru_add_obj and __list_lru_add to list_lru_add (patch 1) (suggested by Johannes Weiner and Yosry Ahmed) * Some cleanups on the memcg aware LRU patch (patch 2) (suggested by Yosry Ahmed) * Use event interface for the new per-cgroup writeback counters. (patch 3) (suggested by Yosry Ahmed) * Abstract zswap's lruvec states and handling into zswap_lruvec_state (patch 5) (suggested by Yosry Ahmed) v3: * Add a patch to export per-cgroup zswap writeback counters * Add a patch to update zswap's kselftest * Separate the new list_lru functions into its own prep patch * Do not start from the top of the hierarchy when encounter a memcg that is not online for the global limit zswap writeback (patch 2) (suggested by Yosry Ahmed) * Do not remove the swap entry from list_lru in __read_swapcache_async() (patch 2) (suggested by Yosry Ahmed) * Removed a redundant zswap pool getting (patch 2) (reported by Ryan Roberts) * Use atomic for the nr_zswap_protected (instead of lruvec's lock) (patch 5) (suggested by Yosry Ahmed) * Remove the per-cgroup zswap shrinker knob (patch 5) (suggested by Yosry Ahmed) v2: * Fix loongarch compiler errors * Use pool stats instead of memcg stats when !CONFIG_MEMCG_KEM There are currently several issues with zswap writeback: 1. There is only a single global LRU for zswap, making it impossible to perform worload-specific shrinking - an memcg under memory pressure cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking: https://lore.kernel.org/all/20230530232435.3097106-1-nphamcs@gmail.com/T/#u But this solution leaves a lot to be desired, as we still do not have an avenue for an memcg to free up its own memory locked up in the zswap pool. 2. We only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages). This patch series solves these issues by separating the global zswap LRU into per-memcg and per-NUMA LRUs, and performs workload-specific (i.e memcg- and NUMA-aware) zswap writeback under memory pressure. The new shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis. As a proof of concept, we ran the following synthetic benchmark: build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds. Domenico Cerasuolo (3): zswap: make shrinking memcg-aware mm: memcg: add per-memcg zswap writeback stat selftests: cgroup: update per-memcg zswap writeback selftest Nhat Pham (3): list_lru: allows explicit memcg and NUMA node selection memcontrol: implement mem_cgroup_tryget_online() zswap: shrinks zswap pool based on memory pressure Documentation/admin-guide/mm/zswap.rst | 10 + drivers/android/binder_alloc.c | 7 +- fs/dcache.c | 8 +- fs/gfs2/quota.c | 6 +- fs/inode.c | 4 +- fs/nfs/nfs42xattr.c | 8 +- fs/nfsd/filecache.c | 4 +- fs/xfs/xfs_buf.c | 6 +- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 54 ++- include/linux/memcontrol.h | 15 + include/linux/mmzone.h | 2 + include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 27 +- mm/Kconfig | 14 + mm/list_lru.c | 48 ++- mm/memcontrol.c | 3 + mm/mmzone.c | 1 + mm/swap.h | 3 +- mm/swap_state.c | 26 +- mm/vmstat.c | 1 + mm/workingset.c | 4 +- mm/zswap.c | 456 +++++++++++++++++--- tools/testing/selftests/cgroup/test_zswap.c | 74 ++-- 25 files changed, 661 insertions(+), 125 deletions(-) base-commit: 5cdba94229e58a39ca389ad99763af29e6b0c5a5 Tested-by: Bagas Sanjaya