From patchwork Tue Jul 30 22:27:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13747948 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 AD647C3DA7F for ; Tue, 30 Jul 2024 22:27:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 699AD6B0082; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 648986B0083; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 50F8A6B0085; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) 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 309916B0082 for ; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D458F160126 for ; Tue, 30 Jul 2024 22:27:11 +0000 (UTC) X-FDA: 82397855862.07.495816D Received: from mail-yw1-f179.google.com (mail-yw1-f179.google.com [209.85.128.179]) by imf28.hostedemail.com (Postfix) with ESMTP id 07537C001A for ; Tue, 30 Jul 2024 22:27:09 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KYGZbXn+; spf=pass (imf28.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.179 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722378402; a=rsa-sha256; cv=none; b=Aj/1gzk9BnP3kNsXHsyJq8piMejHg7+mbu067RQURDxZuL628/ZXQVx4s9YuuOngb7q5qF A3VS+XwM8qPdbtbxcX1bkr75iJ36kqvcAu8HGSwcbyQbhWfcvpOfiS7tSe5t4rVAJ7KQT7 sELF0LvmFuI75v01FNfrsfF6GSr0hg8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KYGZbXn+; spf=pass (imf28.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.179 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=1722378402; 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:in-reply-to:references:references:dkim-signature; bh=DmEtv+cmst1vzae8Xa5+9ouwhHcSggBfq4aRXyVWgVA=; b=FYb05n62vdokPqdb56Ueh/xlBxdpwjuv22nye0bNpb590nkgMoTnKjEPY1GkJofU2dP/RC +wjV0XCO6NwbjpGbOPabQle+Z9gQMhVYfFN5FrJyBB4WIZpkpkcDF6XREJwszH4ipWzdH/ v0c3DKDYt69/BsqwdK5F/hKX51YJskM= Received: by mail-yw1-f179.google.com with SMTP id 00721157ae682-6694b50a937so45736907b3.0 for ; Tue, 30 Jul 2024 15:27:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722378429; x=1722983229; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DmEtv+cmst1vzae8Xa5+9ouwhHcSggBfq4aRXyVWgVA=; b=KYGZbXn+onB3GTbTCAE6qsLlFbnZNMgoEclMNIorJUZpcPsumzMDyxPcG/TOKq5n/f ifG49jKGmWaAvYPoaoDjV1O3LGT9szLRenlrYioNL07kthTXw9s2U6p1n0j6HL1/QE/E gQfTrTPnRvjfqgg1aHKw9kb7uSTQZeLfeK5H5O8KyuVpgr18VErtT5uTLIiLV596MsoL CqL7q0f2SQRvRAA5Q05xtpA1qVl5OWHedtmqNQK59Hc2mETLevtyBwNI+b4voSVEDA3P fWfG73DUZae/4GPbhnKPcN6+Ava/XxFOs4jJDKiP86jJlE5QSqTgZHMm88GajmVIlVTR 5IpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722378429; x=1722983229; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DmEtv+cmst1vzae8Xa5+9ouwhHcSggBfq4aRXyVWgVA=; b=Y+d9rthd8Y7Z3X7/2fCpPWvw60GomuL8vL3sHmOT/H9Dn9Lxw//bycskWiZLw8HqNh hLRrtbJA6l4Ye3KGl8egtTsiYuWzXgxaXANihuJi21gXlqJULDF6PozGIJKW0i5HSxHc sBMLeZ/wQOp6KExj+3njAhUXOriRV1JB8Oe2xprbRleiPZxA0jIE61XJbOywNtdLkxZO xsLlmEn2oLmI07H0vNSxi5gGyLOfKQi6XS+WjgiR4gGoMvmFZW0tADcQItjDzRSxtNEZ Nna3POzq7d7yWVNdMQtz8EinXiagDZQH2hgsML0+rgmLol3aEG9ufjkjj/rjlPh9V2nn B1cg== X-Forwarded-Encrypted: i=1; AJvYcCXqnTddV/EXj8mZz/kZYALeg6OsuEtyxrD3e/y3GTogE+KkZw5Otbxu6gHEsO/yYdJeKHdFs4PrwQnfzpEaHFfk4AE= X-Gm-Message-State: AOJu0YzGQJKvsYLopnBu2qofDpmXJaZ6ylLpKS6jSRf41aJBg+NOvdvn ndKdqju6sgAf3pmNmYmM71yQVESWqqy4OsnWGTJ5jsYQAsEPZRVv X-Google-Smtp-Source: AGHT+IFJpsFUe9mM5BsvjhMWkYvyxBRomAGh1+/0eSiMUyMc4x53nf4TjFLhJ6bAHDckIOx16kCBKg== X-Received: by 2002:a05:690c:6c8c:b0:65f:ca2a:9d77 with SMTP id 00721157ae682-67a09783e65mr135573007b3.36.1722378428943; Tue, 30 Jul 2024 15:27:08 -0700 (PDT) Received: from localhost (fwdproxy-nha-113.fbsv.net. [2a03:2880:25ff:71::face:b00c]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6756795e799sm27449417b3.36.2024.07.30.15.27.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 15:27:08 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, shakeel.butt@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, flintglass@gmail.com, chengming.zhou@linux.dev Subject: [PATCH v2 1/2] zswap: implement a second chance algorithm for dynamic zswap shrinker Date: Tue, 30 Jul 2024 15:27:06 -0700 Message-ID: <20240730222707.2324536-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730222707.2324536-1-nphamcs@gmail.com> References: <20240730222707.2324536-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Stat-Signature: gozwh1r8zia1tnbayneq83ikg6rna6fs X-Rspamd-Queue-Id: 07537C001A X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722378429-699101 X-HE-Meta: U2FsdGVkX18tu/EcgIUZKHSBLlo+mbpsL/xkDgzwDQ3vTLQR10oIECLwcyTHzCFbRj9Vi2Z4KAhwWY0l676sWTccu5UO2KhdfrRiXaWLaYUB+hQGaK21QfPgnLtv3PcF/DG/egMgBPnwXc7NNUv72vo6DiR8YPoApsQFd1tMabCqENiTcOkKNf3adgZh6g+RW1biak7sjyCQlN79sDrygLe8kqhP6EkUIYY3/1GD3BO7GGdGiM4X/FViXaLet5GzOu3VcAk+veJQuXKXE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b7ayE5gXJNw/rEpGF+96gMZ6HA/KXnJnQX/UQWvksbSMWewtI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.026471, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Current zswap shrinker's heuristics to prevent overshrinking is brittle and inaccurate, specifically in the way we decay the protection size (i.e making pages in the zswap LRU eligible for reclaim). We currently decay protection aggressively in zswap_lru_add() calls. This leads to the following unfortunate effect: when a new batch of pages enter zswap, the protection size rapidly decays to below 25% of the zswap LRU size, which is way too low. We have observed this effect in production, when experimenting with the zswap shrinker: the rate of shrinking shoots up massively right after a new batch of zswap stores. This is somewhat the opposite of what we want originally - when new pages enter zswap, we want to protect both these new pages AND the pages that are already protected in the zswap LRU. Replace existing heuristics with a second chance algorithm 1. When a new zswap entry is stored in the zswap pool, its reference bit is set. 2. When the zswap shrinker encounters a zswap entry with the reference bit set, give it a second chance - only flips the reference bit and rotate it in the LRU. 3. If the shrinker encounters the entry again, this time with its reference bit unset, then it can reclaim the entry. In this manner, the aging of the pages in the zswap LRUs are decoupled from zswap stores, and picks up the pace with increasing memory pressure (which is what we want). The second chance scheme allows us to modulate the writeback rate based on recent pool activities. Entries that recently entered the pool will be protected, so if the pool is dominated by such entries the writeback rate will reduce proportionally, protecting the workload's workingset.On the other hand, stale entries will be written back quickly, which increases the effective writeback rate. We will still maintain the count of swapins, which is consumed and subtracted from the lru size in zswap_shrinker_count(), to further penalize past overshrinking that led to disk swapins. The idea is that had we considered this many more pages in the LRU active/protected, they would not have been written back and we would not have had to swapped them in. To test this new heuristics, I built the kernel under a cgroup with memory.max set to 2G, on a host with 36 cores: With the old shrinker: real: 263.89s user: 4318.11s sys: 673.29s swapins: 227300.5 With the second chance algorithm: real: 244.85s user: 4327.22s sys: 664.39s swapins: 94663 (average over 5 runs) We observe an 1.3% reduction in kernel CPU usage, and around 7.2% reduction in real time. Note that the number of swapped in pages dropped by 58%. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham --- include/linux/zswap.h | 16 +++--- mm/zswap.c | 110 ++++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 56 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 6cecb4a4f68b..b94b6ae262d5 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -13,17 +13,15 @@ extern atomic_t zswap_stored_pages; struct zswap_lruvec_state { /* - * Number of pages in zswap that should be protected from the shrinker. - * This number is an estimate of the following counts: + * Number of swapped in pages, i.e not found in the zswap pool. * - * 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 are written - * to swap. + * This is consumed and subtracted from the lru size in + * zswap_shrinker_count() to penalize past overshrinking that led to disk + * swapins. The idea is that had we considered this many more pages in the + * LRU active/protected and not written them back, we would not have had to + * swapped them in. */ - atomic_long_t nr_zswap_protected; + atomic_long_t nr_swapins; }; unsigned long zswap_total_pages(void); diff --git a/mm/zswap.c b/mm/zswap.c index adeaf9c97fde..f4e001c9e7e0 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -184,6 +184,10 @@ static struct shrinker *zswap_shrinker; * page within zswap. * * swpentry - associated swap entry, the offset indexes into the red-black tree + * referenced - true if the entry recently entered the zswap pool. Unset by the + * dynamic shrinker. The entry is only reclaimed by the dynamic + * shrinker if referenced is unset. See comments in the shrinker + * section for context. * length - the length in bytes of the compressed page data. Needed during * decompression. For a same value filled page length is 0, and both * pool and lru are invalid and must be ignored. @@ -196,6 +200,7 @@ static struct shrinker *zswap_shrinker; struct zswap_entry { swp_entry_t swpentry; unsigned int length; + bool referenced; struct zswap_pool *pool; union { unsigned long handle; @@ -700,11 +705,10 @@ static inline int entry_to_nid(struct zswap_entry *entry) static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) { - atomic_long_t *nr_zswap_protected; - unsigned long lru_size, old, new; int nid = entry_to_nid(entry); struct mem_cgroup *memcg; - struct lruvec *lruvec; + + entry->referenced = true; /* * Note that it is safe to use rcu_read_lock() here, even in the face of @@ -722,19 +726,6 @@ static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *entry) memcg = mem_cgroup_from_entry(entry); /* will always succeed */ list_lru_add(list_lru, &entry->lru, nid, memcg); - - /* Update the protection area */ - lru_size = list_lru_count_one(list_lru, nid, memcg); - lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - nr_zswap_protected = &lruvec->zswap_lruvec_state.nr_zswap_protected; - old = atomic_long_inc_return(nr_zswap_protected); - /* - * Decay to avoid overflow and adapt to changing workloads. - * This is based on LRU reclaim cost decaying heuristics. - */ - do { - new = old > lru_size / 4 ? old / 2 : old; - } while (!atomic_long_try_cmpxchg(nr_zswap_protected, &old, new)); rcu_read_unlock(); } @@ -752,7 +743,7 @@ static void zswap_lru_del(struct list_lru *list_lru, struct zswap_entry *entry) void zswap_lruvec_state_init(struct lruvec *lruvec) { - atomic_long_set(&lruvec->zswap_lruvec_state.nr_zswap_protected, 0); + atomic_long_set(&lruvec->zswap_lruvec_state.nr_swapins, 0); } void zswap_folio_swapin(struct folio *folio) @@ -761,7 +752,7 @@ void zswap_folio_swapin(struct folio *folio) if (folio) { lruvec = folio_lruvec(folio); - atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_swapins); } } @@ -1082,6 +1073,28 @@ static int zswap_writeback_entry(struct zswap_entry *entry, /********************************* * shrinker functions **********************************/ +/* + * The dynamic shrinker is modulated by the following factors: + * + * 1. Each zswap entry has a referenced bit, which the shrinker unsets (giving + * the entry a second chance) before rotating it in the LRU list. If the + * entry is considered again by the shrinker, with its referenced bit unset, + * it is written back. The writeback rate as a result is dynamically + * adjusted by the pool activities - if the pool is dominated by new entries + * (i.e lots of recent zswapouts), these entries will be protected and + * the writeback rate will slow down. On the other hand, if the pool has a + * lot of stagnant entries, these entries will be reclaimed immediately, + * effectively increasing the writeback rate. + * + * 2. Swapins counter: If we observe swapins, it is a sign that we are + * overshrinking and should slow down. We maintain a swapins counter, which + * is consumed and subtract from the number of eligible objects on the LRU + * in zswap_shrinker_count(). + * + * 3. Compression ratio. The better the workload compresses, the less gains we + * can expect from writeback. We scale down the number of objects available + * for reclaim by this ratio. + */ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_one *l, spinlock_t *lock, void *arg) { @@ -1091,6 +1104,16 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o enum lru_status ret = LRU_REMOVED_RETRY; int writeback_result; + /* + * Second chance algorithm: if the entry has its referenced bit set, give it + * a second chance. Only clear the referenced bit and rotate it in the + * zswap's LRU list. + */ + if (entry->referenced) { + entry->referenced = false; + return LRU_ROTATE; + } + /* * As soon as we drop the LRU lock, the entry can be freed by * a concurrent invalidation. This means the following: @@ -1157,8 +1180,7 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) { - struct lruvec *lruvec = mem_cgroup_lruvec(sc->memcg, NODE_DATA(sc->nid)); - unsigned long shrink_ret, nr_protected, lru_size; + unsigned long shrink_ret; bool encountered_page_in_swapcache = false; if (!zswap_shrinker_enabled || @@ -1167,25 +1189,6 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, return SHRINK_STOP; } - nr_protected = - atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); - lru_size = list_lru_shrink_count(&zswap_list_lru, sc); - - /* - * Abort if we are shrinking into the protected region. - * - * This short-circuiting is necessary because if we have too many multiple - * concurrent reclaimers getting the freeable zswap object counts at the - * same time (before any of them made reasonable progress), the total - * number of reclaimed objects might be more than the number of unprotected - * objects (i.e the reclaimers will reclaim into the protected area of the - * zswap LRU). - */ - if (nr_protected >= lru_size - sc->nr_to_scan) { - sc->nr_scanned = 0; - return SHRINK_STOP; - } - shrink_ret = list_lru_shrink_walk(&zswap_list_lru, sc, &shrink_memcg_cb, &encountered_page_in_swapcache); @@ -1200,7 +1203,8 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, { struct mem_cgroup *memcg = sc->memcg; struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); - unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; + atomic_long_t *nr_swapins = &lruvec->zswap_lruvec_state.nr_swapins; + unsigned long nr_backing, nr_stored, lru_size, nr_swapins_cur, nr_remain; if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg)) return 0; @@ -1233,14 +1237,26 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, if (!nr_stored) return 0; - nr_protected = - atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); - nr_freeable = list_lru_shrink_count(&zswap_list_lru, sc); + lru_size = list_lru_shrink_count(&zswap_list_lru, sc); + if (!lru_size) + return 0; + /* - * Subtract the lru size by an estimate of the number of pages - * that should be protected. + * Subtract the lru size by the number of pages that are recently swapped + * in. The idea is that had we protect the zswap's LRU by this amount of + * pages, these swap in would not have happened. */ - nr_freeable = nr_freeable > nr_protected ? nr_freeable - nr_protected : 0; + nr_swapins_cur = atomic_long_read(nr_swapins); + do { + if (lru_size >= nr_swapins_cur) + nr_remain = 0; + else + nr_remain = nr_swapins_cur - lru_size; + } while (!atomic_long_try_cmpxchg(nr_swapins, &nr_swapins_cur, nr_remain)); + + lru_size -= nr_swapins_cur - nr_remain; + if (!lru_size) + return 0; /* * Scale the number of freeable pages by the memory saving factor. @@ -1253,7 +1269,7 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, * 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); + return mult_frac(lru_size, nr_backing, nr_stored); } static struct shrinker *zswap_alloc_shrinker(void) From patchwork Tue Jul 30 22:27:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13747949 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 ADB54C3DA49 for ; Tue, 30 Jul 2024 22:27:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11B396B0083; Tue, 30 Jul 2024 18:27:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CA446B0085; Tue, 30 Jul 2024 18:27:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E85E66B0089; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) 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 CB6A66B0083 for ; Tue, 30 Jul 2024 18:27:12 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 72C131A0124 for ; Tue, 30 Jul 2024 22:27:12 +0000 (UTC) X-FDA: 82397855904.10.C37D9E3 Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) by imf22.hostedemail.com (Postfix) with ESMTP id 9CE50C0003 for ; Tue, 30 Jul 2024 22:27:10 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="haeXqU/C"; spf=pass (imf22.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.219.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=1722378426; 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:in-reply-to:references:references:dkim-signature; bh=p4hnJFsfLt33aZBeoJY1bqyzrEBlyMzRqpVt6Wmxo/I=; b=Ofz9YLBNoXbGo8IDRAjgBC9nx4/57BFSbLl7D/5/fGUYBezxXtmBCzlTpmmb7rKt31FW7t 6YrSyQg3yKlP5rqG5BCCL+demGQSl8LAyIXJXi7xrT9MD+TBSslbqScoYv8wM2cmNXgLDn OCYZ5DTaFsJDjJ2GsR5+fXYb/CaTDXc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="haeXqU/C"; spf=pass (imf22.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.219.178 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722378426; a=rsa-sha256; cv=none; b=l0BUY1AUF/Le8YcFoKz8hS1uL6NNu62nvDxccDDECbbaj9MhUO89kqFMzUQ+BkRRxA6OJ0 +VYSMRLr8m3LLYTWqOb8jcyK1IUYDiQRwW+BWRrd/ovRJ/sq/tC7pwbcFrf3BoH01iiUcg Q+LYdwC2p3nG1cgd/2Rf6SmYCg/smZU= Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-e087641d2a2so3994043276.0 for ; Tue, 30 Jul 2024 15:27:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722378430; x=1722983230; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p4hnJFsfLt33aZBeoJY1bqyzrEBlyMzRqpVt6Wmxo/I=; b=haeXqU/CLjpYIEtdNt20WJ/oOf5V6Z7OVeirRorzxOxfxInonJkGbDDvgrovVNua7O F8r68+gdbFzW9uyfS7kvm5VPLlQmyRM0fGQNA/p1bIeteGVVIkVEYDx5tyRpjFGPXa/+ jEPM4ODwgzVBTW8EXYYlWKrAq/TILyZSIn8j+SPLPD8YN+GuCPYWnfKMCcYjZFEio2ox gU0gGTZsi6tEs+gKOsQRO3Yt+oU0QM0dcnbmC26Xzr7+eyWWUh7Lh19c9L1mrW4ShyyL 0KG/WPc4OdFx4WJGPRNtFKu6Qra4/UwEzdfaznREQVocObFamooZ83DqCnC1nVQK3ecr +6ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722378430; x=1722983230; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p4hnJFsfLt33aZBeoJY1bqyzrEBlyMzRqpVt6Wmxo/I=; b=hfrxXeYTShxtHE2PVrVcF4BIRN0E1tBVUbXJ342dJHl8rFqH9xV6lGv4HSHfkgUi8A kFjl9bj3PCqeYHtipXaIhNTefMO2DIVrz4IEHIjdcfWQAUA0ygcZlsd6Q8K/eXCc7o1l NoqU62pOZrchifiKk3b6kHl7habtBue4WGAw/fl5lKwXRieKZSGkePZlUBx2cgzIOnje AIAj110zVIzFNS8OKnJGXKTQsmDMbgnf9U0rebfZQQdY9d82NopB47ZfZs9I88yIzpJU s4NgzUTz0SlBoxwd81M3NV0lNJbBLzcKzoA9dZNnG5FJBWxpYp97kiQP5XBjPw8kdD6d quwg== X-Forwarded-Encrypted: i=1; AJvYcCW4xDl0kH/bI9QpePxmmteBfYsm4XIzkgiR4Uo7PTNvLfRObSH9K4EcF8shiF+Mb8U2A05ispU9+a3YyZmZTFzLNQk= X-Gm-Message-State: AOJu0YzeTnESnmSvNYjAKqXXHdi3Tw8zL5cAhTar0pgtp3KymF8xVLju rEfO/cE9zsqs56xUmUQdLz6uEfIszMSmG7iRLkN1tqaBXoIf64o/ X-Google-Smtp-Source: AGHT+IEDpXcJKoeIOERp2NH7899jLowbHdtHLsB6NDYOMXttj9SNXSNeVKXOov11l1XP/MwO3SJ8Jg== X-Received: by 2002:a05:6902:1209:b0:e03:a983:3d83 with SMTP id 3f1490d57ef6-e0b544c9f51mr12729774276.27.1722378429577; Tue, 30 Jul 2024 15:27:09 -0700 (PDT) Received: from localhost (fwdproxy-nha-112.fbsv.net. [2a03:2880:25ff:70::face:b00c]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e0b29f7e037sm2597472276.26.2024.07.30.15.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 15:27:09 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, shakeel.butt@linux.dev, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, flintglass@gmail.com, chengming.zhou@linux.dev Subject: [PATCH v2 2/2] zswap: increment swapin count for non-pivot swapped in pages Date: Tue, 30 Jul 2024 15:27:07 -0700 Message-ID: <20240730222707.2324536-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730222707.2324536-1-nphamcs@gmail.com> References: <20240730222707.2324536-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: jhnkidzamszgpi5hefaoqinkhgbm3e96 X-Rspamd-Queue-Id: 9CE50C0003 X-Rspamd-Server: rspam11 X-HE-Tag: 1722378430-883887 X-HE-Meta: U2FsdGVkX1+a/DY7T59GFGdnkVheEnaUGYWCgy6O8Rl/aDuuejvF3FWa6kWTDz7C6R03Izu7/a0sreJKMKQXPAHC6Km4Vi7kX8k49ZfWnU/NYHnEVvbaxImZYdtTg2hNMOOkoh3CGfZsWjr5A8O3Nx1cpQOjcmESYdC4mN0RKwYjgiE7wz3JmPgq4H5mNYuFWTcAHeVo0w2X5PhEBrPVVtE02bybue0CysQOQOVULZsK720lGCxG04XVHTk6CWgxOwm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KZG720ufrmUwsMDIPm3J+XmDln9mu127HQpI+FirkL1or48DLvGsOZjSZ6R7TYUNlik4= 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, we only increment the swapin counter on pivot pages. This means we are not taking into account pages that also need to be swapped in, but are already taken care of as part of the readahead window. We are also incrementing when the pages are read from the zswap pool, which is inaccurate. This patch rectifies this issue by incrementing whenever we need to perform a non-zswap read. To test this change, I built the kernel under a cgroup with its memory.max set to 2 GB: real: 236.66s user: 4286.06s sys: 652.86s swapins: 81552 For comparison, with just the new second chance algorithm, the build time is as follows: real: 244.85s user: 4327.22s sys: 664.39s swapins: 94663 Without neither: real: 263.89s user: 4318.11s sys: 673.29s swapins: 227300.5 (average over 5 runs) With this change, the kernel CPU time reduces by a further 1.7%, and the real time is reduced by another 3.3%, compared to just the second chance algorithm by itself. The swapins count also reduces by another 13.85%. Combinng the two changes, we reduce the real time by 10.32%, kernel CPU time by 3%, and number of swapins by 64.12%. To gauge the new scheme's ability to offload cold data, I ran another benchmark, in which the kernel was built under a cgroup with memory.max set to 3 GB, but with 0.5 GB worth of cold data allocated before each build (in a shmem file). Under the old scheme: real: 197.18s user: 4365.08s sys: 289.02s zswpwb: 72115.2 Under the new scheme: real: 195.8s user: 4362.25s sys: 290.14s zswpwb: 87277.8 (average over 5 runs) Notice that we actually observe a 21% increase in the number of written back pages - so the new scheme is just as good, if not better at offloading pages from the zswap pool when they are cold. Build time reduces by around 0.7% as a result. Suggested-by: Johannes Weiner Signed-off-by: Nhat Pham --- mm/page_io.c | 11 ++++++++++- mm/swap_state.c | 8 ++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/mm/page_io.c b/mm/page_io.c index ff8c99ee3af7..0004c9fbf7e8 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -521,7 +521,15 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) if (zswap_load(folio)) { folio_unlock(folio); - } else if (data_race(sis->flags & SWP_FS_OPS)) { + goto finish; + } + + /* + * We have to read the page from slower devices. Increase zswap protection. + */ + zswap_folio_swapin(folio); + + if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); } else if (synchronous) { swap_read_folio_bdev_sync(folio, sis); @@ -529,6 +537,7 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) swap_read_folio_bdev_async(folio, sis); } +finish: if (workingset) { delayacct_thrashing_end(&in_thrashing); psi_memstall_leave(&pflags); diff --git a/mm/swap_state.c b/mm/swap_state.c index a1726e49a5eb..3a0cf965f32b 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -698,10 +698,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, NULL); - } return folio; } @@ -850,10 +848,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, NULL); - } return folio; }