From patchwork Wed May 24 06:50:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13253378 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 E95BAC77B7C for ; Wed, 24 May 2023 06:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59D296B007B; Wed, 24 May 2023 02:50:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 54D836B007D; Wed, 24 May 2023 02:50:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4151E900002; Wed, 24 May 2023 02:50:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 328C36B007B for ; Wed, 24 May 2023 02:50:59 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EEAB4AE2E7 for ; Wed, 24 May 2023 06:50:58 +0000 (UTC) X-FDA: 80824226196.19.001F783 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by imf02.hostedemail.com (Postfix) with ESMTP id 3887680003 for ; Wed, 24 May 2023 06:50:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Egvyynvn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684911057; 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=Q1BgDaEuGbsPtj89gXejmiemKbgHi9EHq9Za8nOtL1I=; b=7EdoQQQhm3SzxUezg++Hk33k2cA8sOjRq4ztVDfifZFJf+QItMoZFQexGuiwpq8N5FaKc0 3wZoWbqOVsw5VAt9O+tc242Vna//lxKE1Pg5IhsywhTek2PlrQbvut+JyUCcJvB8OmmBT5 DA4oE73XUMEdnJuWRnCotq89lp5bob4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Egvyynvn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.208.51 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684911057; a=rsa-sha256; cv=none; b=EPI8K13AVAmr9kfaL0bgTw8tsler+Sh8QnthqadWcI8IniO9BU477h9F7wfhwZ9E0oo1A9 PW0eqQht4YCRAzKCegyiBQZcqHTSTnzLU5znAu3UYmWKWEqJ2++j96R2gFJlVZqWe4YcZk vFbHDIwIbG1ZaEZB08mHTkGcEb/KXz8= Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-50db91640d3so1199980a12.0 for ; Tue, 23 May 2023 23:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684911055; x=1687503055; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Q1BgDaEuGbsPtj89gXejmiemKbgHi9EHq9Za8nOtL1I=; b=EgvyynvnYSsaA25G+7MLLPYzMpi3yN4o3G1RyAt/VSfgaKCM7SrDfnfm0KmHEHZGjP 6ErdO7t5dAR1q46BChrApVBhwG5Vb92CZ1oZK86qhqRHEmf3sujlTrbqiO4wxpL3JTNr VA+AT64mDoeWhg0YbN618Rv/lqhd2VhzOVvyMh6ax3QDSWquV9xGEAWzVD+eArOvTyWZ hNVQuYLf8YhjHY3rnOvazlHr0gr2hzJiSu1lXhavatw2r/iO8QFulUoKK5SiN6IZ0gp/ GnDhDXyLRFIrGtCB4426XmfP+W8hPr0LLQqxtvfYx6RSoyD44e/4l9EX4ibbCMLMA7j3 7Ilg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684911055; x=1687503055; 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=Q1BgDaEuGbsPtj89gXejmiemKbgHi9EHq9Za8nOtL1I=; b=RuHQ6HDE9zWDJ1CzE22r0OWw8nhxW8NgOon71hJtrzZuMGB7FuAXB1nHoXzeIFvJSz 37pgdfyvp1ccrA8agZfn/sNvTlj8TVSAkfpfsyfpuROKWdQOOW/OF5KAxcaxRZXxWeIr MjVUYBYh45hjyi8X0/HaRDQel/MmJ99TWuX7ANe52TKwy1JZDmVVVXzE+OPXo+Poegay ERBBCirC74GxZtUh8Rroek7QJSgaqxtaZUwImKI4S45J1Oc+lVMY0CU0RRXJdI8gP38K /HjCtR0M1pzmG1X9QtczKaE6Lk7nErv/xpM4F2s6XS9vNcGfcKw0iOHbZb5ga+vyGXma 5EjQ== X-Gm-Message-State: AC+VfDx89kMQsvajtVMywKiicRlhigu5RUzqj1jXnoJ2UmlrK2Ew6DBG EisB5Bo8iwIahVS7GJu8iW60GHCAtKvbuX36 X-Google-Smtp-Source: ACHHUZ7eSE7CehhQUk2qxVR8zYd9xtqKtzU+hEDjQn7ZMSMx65xOzZ1NG0htRR3myXtzygOkFztzWg== X-Received: by 2002:a17:907:7b8d:b0:92b:3c78:91fa with SMTP id ne13-20020a1709077b8d00b0092b3c7891famr18509329ejc.28.1684911054895; Tue, 23 May 2023 23:50:54 -0700 (PDT) Received: from lelloman-5950.homenet.telecomitalia.it (host-95-247-156-74.retail.telecomitalia.it. [95.247.156.74]) by smtp.gmail.com with ESMTPSA id f16-20020a170906739000b0096650f46004sm5444304ejl.56.2023.05.23.23.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 May 2023 23:50:54 -0700 (PDT) From: Domenico Cerasuolo To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, yosryahmed@google.com, hannes@cmpxchg.org, kernel-team@fb.com, Domenico Cerasuolo Subject: [PATCH] mm: zswap: shrink until can accept Date: Wed, 24 May 2023 08:50:51 +0200 Message-Id: <20230524065051.6328-1-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3887680003 X-Stat-Signature: 98xpw7mx49o3zox1nosp7w6fw9opm7or X-Rspam-User: X-HE-Tag: 1684911056-430023 X-HE-Meta: U2FsdGVkX1+SPtpEAPeQbpz4ykeHEHCXQyfi7bsmOwo88EUwqckkB+3SxisjMA9l2olCbJ4iu0EQRWfdZEL4SmlCtQ6uEPn4LxDGelhKT9vK/XlYwZKaamfMjNHEMEshCyRR1863fyPHZ7Qws3JiUdW7ISVt3CC5XA8FGhZfxJYW+jSIYYjHzzBL2ckGZ4J4PPc66JVh3+fWDHwxBDGiYW9co/XC7hEdHpvVKRYeN4CZyqDF9mfGKtOw1POZTRVHJ5iPFCw7iSKuN+iv9kvK/QQg+NfWCJwssAQrIe3l8aDbxETQ2fO12HNxTd3zQlKmXXZFn6coLzCGEtiEcaFAQvlNqC/JFniX+jHgdkLR2WE9w+HlCIDeBWSS/bMoK1Vq485FrsV7PI+33uvfK99yo3jr37gOmFV8NhP4JnWrNQ9d+fyo2tDZ6UgslAjSA75XeuT3jJ41Wk9V4Pek8VCLR8FMy4zKUaeYoUbcEZqZVu/z68Xq+nGPrz8Yo3pJ/j5tQd7Y9Flhhh/M9RARqa21dkNM0yIjlgbyRoW2lpnLxJYV0srC9vpOb413C9SICTMQziWPcOld/TqKniVl8rMX8bAB4IHhWGb/FAzOEZocpxaT8hV0v7tbRSOhJ9bLLVEET2/gOIkm7yxxR2g8WPMWvnVRdCj2whI1zSfyI81fEuHcWIWHeqs3MvUFdhMpD6n3A5Sz2dQTeNN9SfyAndL0aUwksxaZkPMva00zTH3fJkGoEsANBGfAZW6AsIN2AiZP1XGpm6XRirkUTQEO2x5bv+3i9aGiTnjswtGxRxd8iJVWvIq5LLY/hhxpzebaqyn7v4YNd6eD1g10TGPBPLquYk55GnFGqw/969pFJ3kOaDm7AvMZJECJGO6C2lXTtTy37UuLWqtAYMcePXqt2dq5ssb70XU60R2LsQWoXqOKl1rqwKAezmPRxTw6mE53IOJ7FV7gxgS61eOe23ugp6B qKjz7/GW At8G7wrL1L26Av+KSE7yYxjb1yYbHnec7DIQotV+uIDttQnSa7tCYiUc0G0nskrTiX6I9/9ndFBRRjr8icuMscBTu5LwdvEaGrPvYGXcP+r+WE+7roH+jHsAXbeNRzG4KvQJizQeb9NMpeZ7d5o8laG7E8xS+5gGSJpL/F3OlZARjunozKlj2DaUgd2KsQktYNK+G5VxwWuTwB9hLhq2j92NzSFkWyaM/9jn/MBXHUOi0BZHDMzXjK9N2KyxPtd2a0aqwB4tltmZPlvY9rcSLCWa8OGYgj3NWCzQK962imkywTiH+nB5FPO88j/pd8aw0N0SAoq9bpzo/zlH0+qgdpt2vqp6kAT77k9vi9NlKzpManwR463ZBck4u1ojQMXEjreTqEGtmKyOdX7e9V9BObPEbfZlAi7jNZwF6/mnRPZrkmv/8jOAvPcSHM8yWH1Cj9EVu X-Bogosity: Ham, tests=bogofilter, spamicity=0.004011, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This update addresses an issue with the zswap reclaim mechanism, which hinders the efficient offloading of cold pages to disk, thereby compromising the preservation of the LRU order and consequently diminishing, if not inverting, its performance benefits. The functioning of the zswap shrink worker was found to be inadequate, as shown by basic benchmark test. For the test, a kernel build was utilized as a reference, with its memory confined to 1G via a cgroup and a 5G swap file provided. The results are presented below, these are averages of three runs without the use of zswap: real 46m26s user 35m4s sys 7m37s With zswap (zbud) enabled and max_pool_percent set to 1 (in a 32G system), the results changed to: real 56m4s user 35m13s sys 8m43s written_back_pages: 18 reject_reclaim_fail: 0 pool_limit_hit:1478 Besides the evident regression, one thing to notice from this data is the extremely low number of written_back_pages and pool_limit_hit. The pool_limit_hit counter, which is increased in zswap_frontswap_store when zswap is completely full, doesn't account for a particular scenario: once zswap hits his limit, zswap_pool_reached_full is set to true; with this flag on, zswap_frontswap_store rejects pages if zswap is still above the acceptance threshold. Once we include the rejections due to zswap_pool_reached_full && !zswap_can_accept(), the number goes from 1478 to a significant 21578266. Zswap is stuck in an undesirable state where it rejects pages because it's above the acceptance threshold, yet fails to attempt memory reclaimation. This happens because the shrink work is only queued when zswap_frontswap_store detects that it's full and the work itself only reclaims one page per run. This state results in hot pages getting written directly to disk, while cold ones remain memory, waiting only to be invalidated. The LRU order is completely broken and zswap ends up being just an overhead without providing any benefits. This commit applies 2 changes: a) the shrink worker is set to reclaim pages until the acceptance threshold is met and b) the task is also enqueued when zswap is not full but still above the threshold. Testing this suggested update showed much better numbers: real 36m37s user 35m8s sys 9m32s written_back_pages: 10459423 reject_reclaim_fail: 12896 pool_limit_hit: 75653 Fixes: 45190f01dd40 ("mm/zswap.c: add allocation hysteresis if pool limit is hit") Signed-off-by: Domenico Cerasuolo --- mm/zswap.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 59da2a415fbb..2ee0775d8213 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -587,9 +587,13 @@ static void shrink_worker(struct work_struct *w) { struct zswap_pool *pool = container_of(w, typeof(*pool), shrink_work); + int ret; - if (zpool_shrink(pool->zpool, 1, NULL)) - zswap_reject_reclaim_fail++; + do { + ret = zpool_shrink(pool->zpool, 1, NULL); + if (ret) + zswap_reject_reclaim_fail++; + } while (!zswap_can_accept() && ret != -EINVAL); zswap_pool_put(pool); } @@ -1188,7 +1192,7 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset, if (zswap_pool_reached_full) { if (!zswap_can_accept()) { ret = -ENOMEM; - goto reject; + goto shrink; } else zswap_pool_reached_full = false; }