From mboxrd@z Thu Jan 1 00:00:00 1970 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 94F20C4167B for ; Tue, 28 Nov 2023 23:04:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E8468D000C; Tue, 28 Nov 2023 18:04:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 299228D0001; Tue, 28 Nov 2023 18:04:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 187B58D000C; Tue, 28 Nov 2023 18:04:18 -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 084888D0001 for ; Tue, 28 Nov 2023 18:04:18 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A9255140381 for ; Tue, 28 Nov 2023 23:04:17 +0000 (UTC) X-FDA: 81508893354.15.F85525A Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) by imf12.hostedemail.com (Postfix) with ESMTP id CCA5840021 for ; Tue, 28 Nov 2023 23:04:15 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IXSNTlNV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.166.51 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=1701212655; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2DFhzUwmSAVWyj8UA9HM/5RmuhrapMWgT80OB1ol+pw=; b=k03trbH8nTj37KUMuq9j2/6l7a4rhLaMXbRbjm8OuPMwZRbVD7f+f8AmwGarhRyBoD862m 3qhkP+ogCrSQaHn1yAG8XwldGPBhjSBgSnLnM4dQjLUprj0L/jkzu0+vWpc4FQbmTpny7O BfTSt7dUzJijwdQg1pktPUUw6Qqk6Kc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=IXSNTlNV; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf12.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.166.51 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701212655; a=rsa-sha256; cv=none; b=8D8NDhbCmoAEomglPfiCi9owotC9XThus99jC0Z+K8nRkdsbL/MC6CBpKYXB3M8gPyVvQq /Cr0zyQSM3/uAz2vAkh6vG/oIUwmTJmwhSsH75dDwDTRFjmyVsPEszjxTeIibW5wNjApQU OBe757PSslp43I/wJS2lw6O9o9i2oM0= Received: by mail-io1-f51.google.com with SMTP id ca18e2360f4ac-7b395a9b893so9254339f.0 for ; Tue, 28 Nov 2023 15:04:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701212655; x=1701817455; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=2DFhzUwmSAVWyj8UA9HM/5RmuhrapMWgT80OB1ol+pw=; b=IXSNTlNVRhsy2PPIK+gCXFtXWLKsoRRygwlcj+HIE+IuIm9SLtmbb59vb2KzYuwG3S 1ap31k2K3a99Di08fzfmhGyejnuInmwcUWQ+nc9G2464/wIpdg3x3j88YDl35YCBxt8G LO76JDhdqMaqPit70RHN0oLxsGQlBY6DR02Xy58phq9Ng4nHzZfQje3EyVKEyh7SGL6d 9FZkmOaLZlsFWdJxENVwkMwIDjKMD8TV40xm85EV9kYhHZFYU2ot6KHqqcA4WWhvC7c5 ijez4/SeAtK1EaoArunWxXQoPZAV3nKhbCkGsOQWWLNxlIC9ajDvNMzFyxQ1W4gi61XF rvng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701212655; x=1701817455; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2DFhzUwmSAVWyj8UA9HM/5RmuhrapMWgT80OB1ol+pw=; b=kdbuzDzanJ99EJzUmiR8IfReAZT/NhuUqKq8tiUmGuMfbZKmRpbOo125n7OKZxNhBs TbUMD+yElj/KJq85/FLoeeSBProEvrsDf9HrOB4L5YqBWexIjICMQGCEzcWkN4qq4+ex +K43vKQl2jJcEZPOX5r0QJ29fHzw7jfviEKN+xgXnvzq9pbDhYZ3Y5u6tJGbatN1uuRh n7C80KrR0CY+6E1I09k06n5WT17yQ0NW34Jw/z96o5N3yFrMPzNW2jOV5/qMHbPQ4vP4 Yjcb6zrsw/Mlk15eNNa69mD/I9DdEvoMm0Av7QCHA02MZCw18ZeLcJoUDlxC7jEOVMJZ 5rjg== X-Gm-Message-State: AOJu0YzcdNp8Lu+BMUeHVCoMp5iZZPFqSbGpz1tJIp3Ft0ioUptqVq6/ MDpIgozACM3dJPA7NXU9O5NbcwrvElO0bARmB+8= X-Google-Smtp-Source: AGHT+IHEQbQKDe8LZI8hD3vy2YVk1aTnCS615hOJnwxGT9PKd2Vc2radk/Uw2Cm0lySrjxcR7yiXVY4Tbcgyou67tQ4= X-Received: by 2002:a6b:e40a:0:b0:7b3:7491:1dee with SMTP id u10-20020a6be40a000000b007b374911deemr13877445iog.10.1701212654841; Tue, 28 Nov 2023 15:04:14 -0800 (PST) MIME-Version: 1.0 References: <20231127193703.1980089-1-nphamcs@gmail.com> <20231127193703.1980089-7-nphamcs@gmail.com> <20231127130055.30c455906d912e09dcb7e79b@linux-foundation.org> In-Reply-To: <20231127130055.30c455906d912e09dcb7e79b@linux-foundation.org> From: Nhat Pham Date: Tue, 28 Nov 2023 15:04:03 -0800 Message-ID: Subject: Re: [PATCH v6 6/6] zswap: shrinks zswap pool based on memory pressure To: Andrew Morton 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CCA5840021 X-Stat-Signature: 374bxfws7zygoz6zq95am3r17jo99tmk X-Rspam-User: X-HE-Tag: 1701212655-542863 X-HE-Meta: U2FsdGVkX19AiiJckqU0UW7boY7Y6JZChlQdFLNVZnrxeLdv9WqwiP65YF68nBn3Ay/t2fD09dtiIj9bg7tzGAD1rOGSMReyr6OwcLzIzhNSdbuhohDnNaViXPFsTlHiu3mB/yVfWTyYQtB+q0PZPhtc+W+7sArSyPvOIid4kz4lg7VTRA5JN1HH7dxA4qZWCJHdl3h8kcjRYKFGEjX+59cbDVIpMvQpZRNQK6DP/G9Mkhku4+8Hw+zjQV6a+3ovJTPV/sjmk6rOQ6IAut/kGP94DFGngOuC3kczZKlos/DDQMD1lIg8vG7BaWwVwk1ZEtja5dprpuaGM6gzL/31f6ReQUl30wZ0i/9gA88TjJPmMD/9Xov/SqwGXcLYXV+g2IKEY9BGL8ucP56ILAkinXl71nPGpge1ZvszTTr5JZfCMndryiNDxLzTSgKwbm7TdRSFELmfXp+8c4GHu1Gts1tvnJLT3tlf5VbC9WgHTGtfYvoV7xRf3ty/boziz4VGYjdv1X8LJPHbtRvPdA93G/1bZmxdO2gwO7tYqsj/k8aXJfAaTJXTx8piarFEm798/R8EoLySKQhTGkvjFfC1l1S0/8hP8Vy/h0/bxtZnWC/HD14P8+oFTsvdA3AWDuVNAi91qg8Ozanc/WB6mJDmzJGX/21exu0Iqq8BrVzns1eZn4lpcYLWvkh1Zmwzlb2l7P//00RY3FENnxMAJAMUntKzSk8UGCTfdBLEOXEVIAYAI1W7i4xY9I6uSB22Jmg+ZyXWjhZthQ9WDEiiGXPw1JNYwPByGJSIzj+Xjy5UrVvikdf40fQsKK58Iv1Uua6jmqceoB6nAouWYKqB1P6hBotNUgOcubXnRjBAzpKrDl6rpL0Mw/cu164YG+ZITqC9XCMXpv4HZMvqLRYTblAO5Fz3JUOquSN/I5NMfbOt+bZE9Qcil9fN7AlT86VekJs3GNiqyt0Qto+Whqklt9D Gwh7NqTD RLQZV6GvdlUrsq8Oorx2vE2mTOpLwk8W5CShj12YMCRP30YpasDsD6vdrxP4ZxwiJN5poTXU4GY4WrgiJBRTNVUb++L47r83FUpMgCXPEqbq29x+wOdKS73uYakGkrJVsPp9g25GVajqTbbsdwIZtcxl5qtsq0x/r4j5Gxsyjml+uOfKv98HTqfglp17TeB8v7WRLxo2wPiw1SZkyo8/UIj3Hzb+qa91aFBB2HsveAH616RSSNmlYitMpxyoX1SsQ1WKskbMbrmE3PQu7ZjwBlFTNFXXhotRoTY4lkM9Ya0wJnmEbgIdFC2b2CWlwwF/TB9Ed3WTkOIauu63HXtH50ehx0au5kPYs8QhSEoaYLpDySexbAH4++13uC2CTpog9L94wxLNGe68K3DbScZe5evBnse2XtRbVtI/fAN4r80ry706PcxkJbn5osA== 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: On Mon, Nov 27, 2023 at 1:00=E2=80=AFPM Andrew Morton wrote: > > On Mon, 27 Nov 2023 11:37:03 -0800 Nhat Pham wrote: > > > Currently, 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 implements a memcg- and NUMA-aware shrinker for zswap, that > > is initiated when there is memory pressure. The 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. > > > > Furthermore, to make it more robust for many workloads and prevent > > overshrinking (i.e evicting warm pages that might be refaulted into > > memory), we build in the following heuristics: > > > > * Estimate the number of warm pages residing in zswap, and attempt to > > protect this region of the zswap LRU. > > * Scale the number of freeable objects by an estimate of the memory > > saving factor. The better zswap compresses the data, the fewer pages > > we will evict to swap (as we will otherwise incur IO for relatively > > small memory saving). > > * During reclaim, if the shrinker encounters a page that is also being > > brought into memory, the shrinker will cautiously terminate its > > shrinking action, as this is a sign that it is touching the warmer > > region of the zswap LRU. > > > > 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. > > > > ... > > > > --- a/include/linux/mmzone.h > > +++ b/include/linux/mmzone.h > > @@ -22,6 +22,7 @@ > > #include > > #include > > #include > > +#include > > #include > > > > /* Free memory management - zoned buddy allocator. */ > > @@ -641,6 +642,7 @@ struct lruvec { > > #ifdef CONFIG_MEMCG > > struct pglist_data *pgdat; > > #endif > > + struct zswap_lruvec_state zswap_lruvec_state; > > Normally we'd put this in #ifdef CONFIG_ZSWAP. > > > --- a/include/linux/zswap.h > > +++ b/include/linux/zswap.h > > @@ -5,20 +5,40 @@ > > #include > > #include > > > > +struct lruvec; > > + > > extern u64 zswap_pool_total_size; > > extern atomic_t zswap_stored_pages; > > > > #ifdef CONFIG_ZSWAP > > > > +struct zswap_lruvec_state { > > + /* > > + * Number of pages in zswap that should be protected from the shr= inker. > > + * This number is an estimate of the following counts: > > + * > > + * a) Recent page faults. > > + * b) Recent insertion to the zswap LRU. This includes new zswap = stores, > > + * as well as recent zswap LRU rotations. > > + * > > + * These pages are likely to be warm, and might incur IO if the a= re written > > + * to swap. > > + */ > > + atomic_long_t nr_zswap_protected; > > +}; > > + > > bool zswap_store(struct folio *folio); > > bool zswap_load(struct folio *folio); > > void zswap_invalidate(int type, pgoff_t offset); > > void zswap_swapon(int type); > > 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); > > #else > > > > +struct zswap_lruvec_state {}; > > But instead you made it an empty struct in this case. > > That's a bit funky, but I guess OK. It does send a careful reader of > struct lruvec over to look at the zswap_lruvec_state definition to > understand what's going on. I agree. Originally, I included the fields in struct lruvec itself, guarded by a #ifdef CONFIG_ZSWAP as you pointed out here. Yosry gave me this suggestion to hide the zswap-specific states and details from ordinary lruvec user, and direct people who care and/or need to know about details towards the zswap codebase, which is good IMHO. It is a bit weird I admit, but in this case I think it works out. > > > static inline bool zswap_store(struct folio *folio) > > { > > return false; > > @@ -33,7 +53,8 @@ static inline void zswap_invalidate(int type, pgoff_t= offset) {} > > static inline void zswap_swapon(int type) {} > > static inline void zswap_swapoff(int type) {} > > static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memc= g) {} > > - > > +static inline void zswap_lruvec_init(struct lruvec *lruvec) {} > > +static inline void zswap_lruvec_swapin(struct page *page) {} > > Needed this build fix: > > --- a/include/linux/zswap.h~zswap-shrinks-zswap-pool-based-on-memory-pres= sure-fix > +++ a/include/linux/zswap.h > @@ -54,6 +54,7 @@ static inline void zswap_swapon(int type > 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_state_init(struct lruvec *lruvec) {} > static inline void zswap_lruvec_swapin(struct page *page) {} > #endif > > _ > Yeah that looks like a typo on my part. My bad. v7 includes this fix.