From patchwork Mon Nov 6 18:31:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13447284 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 E604BC4167D for ; Mon, 6 Nov 2023 18:32:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 665BE6B0271; Mon, 6 Nov 2023 13:32:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6165A6B0272; Mon, 6 Nov 2023 13:32:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B6C56B0273; Mon, 6 Nov 2023 13:32:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 388C46B0271 for ; Mon, 6 Nov 2023 13:32:04 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E8ED6C085A for ; Mon, 6 Nov 2023 18:32:03 +0000 (UTC) X-FDA: 81428373726.12.8770D35 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf12.hostedemail.com (Postfix) with ESMTP id F2EBB4000F for ; Mon, 6 Nov 2023 18:32:01 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=N2+9Ovwi; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699295522; 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=RolW8kmfT6ZCz66bTNAjEmvRmOGj5ZqsnmWESYphJzc=; b=8qHmpEQEnevfANpw4v7HB8m727eDF39mj/UpbG7ljre4x1021KKOtbM0FbKecJaiKIP8Ek 3QrfSpJD18wZvayL3A2dAbPGRy8mhdqaxnhivt8PJxMNPiau40x0C+vxQOlyVYgfzWR6wM C4VnCPfxBl+ZMCU4pxiOcjbtFegw8bo= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=N2+9Ovwi; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699295522; a=rsa-sha256; cv=none; b=obzoCwgYDs3t37nFVfu/M34LWugweUG87ll0Bq7+D00R7M2niZZdpVu0EngMVnFyhy7hKS ajSR4ZGgxGUwJRxANK26ch/bGRYA23zcpAeO8sT60QqVXNzyKE+qm7PKm1l6qhr/3fZTsT +lOe2WKRkUUR+iElj4yNOruLRN7AZn4= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1cc3bc5df96so33638145ad.2 for ; Mon, 06 Nov 2023 10:32:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699295521; x=1699900321; 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=RolW8kmfT6ZCz66bTNAjEmvRmOGj5ZqsnmWESYphJzc=; b=N2+9OvwiuWkRO/ord/LDGA86T6FOa1KvCG2uCfoJ2RUtHd9V9KSNRsUvxF5ppMXWw3 /mbCi+Z7jrAVDr2Rt+WHPuxT9Q4BdsKs7LPQia6cGTYc6FOFeaGv6ppzrXbZUYd6Qez9 HptHROH72qYdnAmBNVyRqGlm+PcmOHb7V1TEOV52LbJOKa5Ery8BQ7qYKgaYz0Ww4zv5 85fEcywla0qemDol70DiNOthMuuTamWVZqMNdTA6CbZ4U/2FFd+bSOMsNxOGGsa85np6 Cs8b3RquzErmehTpKIgYeLSaqkBUVKwXIxgSriQOxhSzA+ITlIrs3+VuCzX3Nt49jpZk +7GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699295521; x=1699900321; 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=RolW8kmfT6ZCz66bTNAjEmvRmOGj5ZqsnmWESYphJzc=; b=srx8fPJj1Y9nAcH+aiQsLPzP3ZNFTyP020IbNwXAsdpOKjSNzRe7w08Dmd7sRhxNvU mHig0L2qKa0jo2B/P8GGrbpfB2j8Tv7Ww+cjuC8RqbrWpA7f3yQLYRtWFC2M2f0LFQPf nklQ9E/cIYPdyUoalH5/Bz7K0BuVXNNyYjae/uzzVJh57pOKCRGQ7O+doZOGO1c/y5x/ fzWA3DxlSrYtm9HWezmtIXQlgAW85zjD08Ck4FBO4qpoWDdIGJX1CNyBEybrU2LlQFKD uPSQts1h5llEyka4YHZTTcWKnBlfX0+JWvESx/re5rH7ijYaDmFxTCwHSwk1ggxE88SZ 0Uig== X-Gm-Message-State: AOJu0YzrtR69Jz5B2iaiDpMXb3aK2QkXcYZpnuJ1mxfdeJP2FjWvbJaU 6DgA0vtXL5Wbqq9gEGDPf6Q= X-Google-Smtp-Source: AGHT+IGdvV5WuqZ+RWDIMrRsDenhCX5seXleE023kFPddMhxXei7cSUD1PW2U+fLmxlxyMLTKt2JeQ== X-Received: by 2002:a17:903:2013:b0:1cc:251c:c381 with SMTP id s19-20020a170903201300b001cc251cc381mr19680064pla.29.1699295520329; Mon, 06 Nov 2023 10:32:00 -0800 (PST) Received: from localhost (fwdproxy-prn-020.fbsv.net. [2a03:2880:ff:14::face:b00c]) by smtp.gmail.com with ESMTPSA id u14-20020a170902e5ce00b001c9b35287aesm6213048plf.88.2023.11.06.10.31.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 10:31:59 -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 v5 0/6] workload-specific and memory pressure-driven zswap writeback Date: Mon, 6 Nov 2023 10:31:53 -0800 Message-Id: <20231106183159.3562879-1-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: F2EBB4000F X-Stat-Signature: e8ennjcc31maaiskcjy3ttazq7mo8izd X-HE-Tag: 1699295521-967019 X-HE-Meta: U2FsdGVkX1+tvJH79/Qzpk/Ms25wo5WvJgrwBu18Zx6dtbuNUaG3QzYWWZeG6CyabOCpi5GWzZ1o4Gd8TaTms5g8aoabkhfLp/hBM/4M8JdmNytR06/+P51eywdCnDpf+0sbHqLjXRehDX0dsyGciJ13Q/oXabJkanwqetV/2CjhGeCESuCO6ulnjXN23JYLLLw2O0/FMipRSL1RYmKuHvK1XqZrqrMSXUrEMeMNXGXaDf4QtCAeY08m+hTv7ViF1hVOD46nrvgWdjCjWwCpL9zxIpn30dTj/dZ+h4XSU9TOPNSjNmfc7ndAnoabSdH69zLvH31UINU5URGQwuo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fAQSEqAFqPYPORdGiUO3TxfATo1d0JcCCnrpNICwM0Xdr8lo9L2sSXAmKGIffR+X4hyReRilHgTn7Pk+S2OEBb/mHAIW56axj 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: 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: allows mem_cgroup_iter() to check for onlineness zswap: shrinks zswap pool based on memory pressure Documentation/admin-guide/mm/zswap.rst | 7 + drivers/android/binder_alloc.c | 5 +- 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 | 46 ++- include/linux/memcontrol.h | 9 +- include/linux/mmzone.h | 2 + include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 27 +- mm/list_lru.c | 48 ++- mm/memcontrol.c | 20 +- mm/mmzone.c | 1 + mm/shrinker.c | 4 +- mm/swap.h | 3 +- mm/swap_state.c | 26 +- mm/vmscan.c | 26 +- mm/vmstat.c | 1 + mm/workingset.c | 4 +- mm/zswap.c | 430 +++++++++++++++++--- tools/testing/selftests/cgroup/test_zswap.c | 74 ++-- 26 files changed, 625 insertions(+), 149 deletions(-)