From nobody Thu Sep 19 16:40:49 2024 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 517721547FB for ; Mon, 5 Aug 2024 23:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722900168; cv=none; b=eaUUlpgow/gGx2g0NcuKw+tF3MEOKsdKjGlXrOmoh6tOjmJizm/UiDvDwLSVTscZUNDSwfrt0HYltxm9Hs0yVS9oyx/9OCIDTnLltIjD32cnNBDrKW/zT7V6VAB2giJzWRmpHYkOqjZdhpfbodw1gCeDwPK1lFP2AhQHGuXBoT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722900168; c=relaxed/simple; bh=7CJTiXsbt2nkd6pkJ+rZEdv76PIjhj01ZNBYdag1bg8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZyV+XRBQ/4l563A1ogPCeIWqFd1SfH+1pJwWcDlACW+6YdaoXkeEm8ineHBm8a+yqeit0jzsfjgsk2rasqWiH9yqerxmOBca7A6xoCGGXHAOz+BMSr4J+NyGhzSPUq7jIRfBkqzYCMDI3JSDxGV1z66iCzNu0xhnQprIKjJHn5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Cj06oS7X; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Cj06oS7X" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-68aa9c894c7so205307b3.1 for ; Mon, 05 Aug 2024 16:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722900165; x=1723504965; darn=vger.kernel.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=B9jBszB5LLWkkfrks405QdswXL8fPSHh3jU0pMbZKL8=; b=Cj06oS7XUKwk5H6SkBJ/kKKXII4Gc/yDks9+RSqWEifOk2dc96lTlGJ+7s0+1nA8Kk 50dxyy023GJkMs5tvv5RWbplq2jqUSe60t7993nmWnZZ3Rh/wGY1cXawdZ970pQ1oFFX Ckf2IDFWhcpJhRfpyvk+sO1PbK/FlOpgFeHkx85hsCxnd1YLsIqVMh5twyczd1ahZpoz sA4lrm+618R5D8J/RPwbtEECDR4myMV3B9C2LE1Bizl1sRWmrGoz13ScVqBXqaYs7RYB ZcObK77L/c4Q8vkZTqYdvSC+bLl2E6G4271dv5rerfEjSdrXRVMhxSwBKKVPdUihj7ZH B7yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722900165; x=1723504965; 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=B9jBszB5LLWkkfrks405QdswXL8fPSHh3jU0pMbZKL8=; b=D8Za1IGyjguw7pQnW5v/4rcaPJJ1OWGxfwVG6ricdU1MgNWFJAZHNorB1Ybi1h3HoJ ZsyIH6Llt5BJgXqOxvGvjvYCtuSMweJILtn5a5o+Ao5XFioF0pM5oHXUiWEZoNQ/vMli QYd6jM4Z01KIxW1mwCsWA9JWKTRzfNg0IiyeYaaZCvr8272K9c3AOch7st1FgAMC59Nd E9GXynjWgVXbsgDCfRneQorjaMI1oTwKev3JoTT9ZzRRir/JvzQf5WiMxKg/fgPLH36B nch7ESdiaAytipzMjZAnCQ0bSxwcQ4qN66nIz/ttNLi187U0mp6OwYfLnByXH7Na9+Af srfQ== X-Forwarded-Encrypted: i=1; AJvYcCW4U+qnS3yPzSpIU5339uey3l90+ZgES3Ae8IbFUM7Fu98Sgf40vqe950q/AkvMDgyghSNFleHaFRXij9wdPD2WNhZaKJLD+kdiSojf X-Gm-Message-State: AOJu0YyCEH8nxiuZi91veLbprBR5bIYhHW4zAoC8yULqcDy1rAAFVoCu YaYKU+XU1+1OiWOZ0VGgUMStC0lKXkglasXZrCk6LI0Ojdi0fcne X-Google-Smtp-Source: AGHT+IEET/qcu+3/GcjWYDk0MU/fhGCcMkhZvJ8FSa3Q+ZhrpzZxf542Yj8C0aMe8IhCvVZbrmDb+g== X-Received: by 2002:a0d:dc07:0:b0:65f:8f77:720f with SMTP id 00721157ae682-6895f60d50emr140723007b3.5.1722900165075; Mon, 05 Aug 2024 16:22:45 -0700 (PDT) Received: from localhost (fwdproxy-nha-001.fbsv.net. [2a03:2880:25ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id 00721157ae682-68a140613bcsm13712107b3.135.2024.08.05.16.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Aug 2024 16:22:44 -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 v3 1/2] zswap: implement a second chance algorithm for dynamic zswap shrinker Date: Mon, 5 Aug 2024 16:22:42 -0700 Message-ID: <20240805232243.2896283-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805232243.2896283-1-nphamcs@gmail.com> References: <20240805232243.2896283-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 referenced bit is set. 2. When the zswap shrinker encounters a zswap entry with the referenced bit set, give it a second chance - only flips the referenced bit and rotate it in the LRU. 3. If the shrinker encounters the entry again, this time with its referenced 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. The referenced bit is added at the hole after the `length` field of struct zswap_entry, so there is no extra space overhead for this algorithm. 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 Acked-by: Johannes Weiner Acked-by: Yosry Ahmed --- include/linux/zswap.h | 16 +++---- mm/zswap.c | 108 ++++++++++++++++++++++++------------------ 2 files changed, 70 insertions(+), 54 deletions(-) diff --git a/include/linux/zswap.h b/include/linux/zswap.h index 6cecb4a4f68b..9cd1beef0654 100644 --- a/include/linux/zswap.h +++ b/include/linux/zswap.h @@ -13,17 +13,15 @@ extern atomic_t zswap_stored_pages; =20 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 from disk, 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 writt= en - * 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_disk_swapins; }; =20 unsigned long zswap_total_pages(void); diff --git a/mm/zswap.c b/mm/zswap.c index adeaf9c97fde..fb3d9cb88785 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -187,6 +187,10 @@ static struct shrinker *zswap_shrinker; * length - the length in bytes of the compressed page data. Needed during * decompression. For a same value filled page length is 0, and b= oth * pool and lru are invalid and must be ignored. + * referenced - true if the entry recently entered the zswap pool. Unset b= y the + * dynamic shrinker. The entry is only reclaimed by the dynam= ic + * shrinker if referenced is unset. See comments in the shrin= ker + * section for context. * pool - the zswap_pool the entry's data is in * handle - zpool allocation handle that stores the compressed page data * value - value of the same-value filled pages which have same content @@ -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,8 @@ static inline int entry_to_nid(struct zswap_entry *ent= ry) =20 static void zswap_lru_add(struct list_lru *list_lru, struct zswap_entry *e= ntry) { - atomic_long_t *nr_zswap_protected; - unsigned long lru_size, old, new; int nid =3D entry_to_nid(entry); struct mem_cgroup *memcg; - struct lruvec *lruvec; =20 /* * Note that it is safe to use rcu_read_lock() here, even in the face of @@ -722,19 +724,6 @@ static void zswap_lru_add(struct list_lru *list_lru, s= truct zswap_entry *entry) memcg =3D mem_cgroup_from_entry(entry); /* will always succeed */ list_lru_add(list_lru, &entry->lru, nid, memcg); - - /* Update the protection area */ - lru_size =3D list_lru_count_one(list_lru, nid, memcg); - lruvec =3D mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - nr_zswap_protected =3D &lruvec->zswap_lruvec_state.nr_zswap_protected; - old =3D 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 =3D old > lru_size / 4 ? old / 2 : old; - } while (!atomic_long_try_cmpxchg(nr_zswap_protected, &old, new)); rcu_read_unlock(); } =20 @@ -752,7 +741,7 @@ static void zswap_lru_del(struct list_lru *list_lru, st= ruct zswap_entry *entry) =20 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_disk_swapins, 0); } =20 void zswap_folio_swapin(struct folio *folio) @@ -761,7 +750,7 @@ void zswap_folio_swapin(struct folio *folio) =20 if (folio) { lruvec =3D folio_lruvec(folio); - atomic_long_inc(&lruvec->zswap_lruvec_state.nr_zswap_protected); + atomic_long_inc(&lruvec->zswap_lruvec_state.nr_disk_swapins); } } =20 @@ -1082,6 +1071,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 (gi= ving + * 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 u= nset, + * it is written back. The writeback rate as a result is dynamically + * adjusted by the pool activities - if the pool is dominated by new en= tries + * (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 ha= s 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, w= hich + * 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 gain= s we + * can expect from writeback. We scale down the number of objects avail= able + * 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 +1102,16 @@ static enum lru_status shrink_memcg_cb(struct list_h= ead *item, struct list_lru_o enum lru_status ret =3D LRU_REMOVED_RETRY; int writeback_result; =20 + /* + * 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 =3D 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 +1178,7 @@ static enum lru_status shrink_memcg_cb(struct list_he= ad *item, struct list_lru_o static unsigned long zswap_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc) { - struct lruvec *lruvec =3D 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 =3D false; =20 if (!zswap_shrinker_enabled || @@ -1167,25 +1187,6 @@ static unsigned long zswap_shrinker_scan(struct shri= nker *shrinker, return SHRINK_STOP; } =20 - nr_protected =3D - atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); - lru_size =3D 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 unprotect= ed - * objects (i.e the reclaimers will reclaim into the protected area of the - * zswap LRU). - */ - if (nr_protected >=3D lru_size - sc->nr_to_scan) { - sc->nr_scanned =3D 0; - return SHRINK_STOP; - } - shrink_ret =3D list_lru_shrink_walk(&zswap_list_lru, sc, &shrink_memcg_cb, &encountered_page_in_swapcache); =20 @@ -1200,7 +1201,10 @@ static unsigned long zswap_shrinker_count(struct shr= inker *shrinker, { struct mem_cgroup *memcg =3D sc->memcg; struct lruvec *lruvec =3D mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); - unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; + atomic_long_t *nr_disk_swapins =3D + &lruvec->zswap_lruvec_state.nr_disk_swapins; + unsigned long nr_backing, nr_stored, nr_freeable, nr_disk_swapins_cur, + nr_remain; =20 if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg)) return 0; @@ -1233,14 +1237,27 @@ static unsigned long zswap_shrinker_count(struct sh= rinker *shrinker, if (!nr_stored) return 0; =20 - nr_protected =3D - atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected); nr_freeable =3D list_lru_shrink_count(&zswap_list_lru, sc); + if (!nr_freeable) + return 0; + /* - * Subtract the lru size by an estimate of the number of pages - * that should be protected. + * Subtract from the lru size the number of pages that are recently swapp= ed + * in from disk. The idea is that had we protect the zswap's LRU by this + * amount of pages, these disk swapins would not have happened. */ - nr_freeable =3D nr_freeable > nr_protected ? nr_freeable - nr_protected := 0; + nr_disk_swapins_cur =3D atomic_long_read(nr_disk_swapins); + do { + if (nr_freeable >=3D nr_disk_swapins_cur) + nr_remain =3D 0; + else + nr_remain =3D nr_disk_swapins_cur - nr_freeable; + } while (!atomic_long_try_cmpxchg( + nr_disk_swapins, &nr_disk_swapins_cur, nr_remain)); + + nr_freeable -=3D nr_disk_swapins_cur - nr_remain; + if (!nr_freeable) + return 0; =20 /* * Scale the number of freeable pages by the memory saving factor. @@ -1462,6 +1479,7 @@ bool zswap_store(struct folio *folio) store_entry: entry->swpentry =3D swp; entry->objcg =3D objcg; + entry->referenced =3D true; =20 old =3D xa_store(tree, offset, entry, GFP_KERNEL); if (xa_is_err(old)) { --=20 2.43.0