From patchwork Fri May 26 17:39:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 13257238 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 16F95C77B73 for ; Fri, 26 May 2023 17:40:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C890900004; Fri, 26 May 2023 13:40:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 577C5900002; Fri, 26 May 2023 13:40:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44033900004; Fri, 26 May 2023 13:40:05 -0400 (EDT) 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 31454900002 for ; Fri, 26 May 2023 13:40:05 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F3BFD140EDF for ; Fri, 26 May 2023 17:40:04 +0000 (UTC) X-FDA: 80833119528.16.8A26903 Received: from mail-ej1-f54.google.com (mail-ej1-f54.google.com [209.85.218.54]) by imf23.hostedemail.com (Postfix) with ESMTP id 011AE14000B for ; Fri, 26 May 2023 17:40:02 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=qpu9Bv+u; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.54 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=1685122803; 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=o16G9dPIrqAga4lo4DLIjG9Wv4N2BLNRqrSSEriNgT0=; b=RC5IjtGbHo/92XHjlFvrRbLqRieZypFohWVyJvd3f8Ar5rGwjd2w+oNLB4cjqD0PDCYkhd d0MFHtRPyDNwFf3e43hMz+zqqT4/2i7zgOqWqLYOvOuhMc/Fg9OS/RbExf7M8/phCWQSjB hs/qEQVdPbGoVMrLnqQpypcMYcXLivs= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=qpu9Bv+u; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf23.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.218.54 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685122803; a=rsa-sha256; cv=none; b=6lceDZtdoo84gTKDHz93X7kw1P/MORKtefgZamv63irDL4+9PKCAGuXZdNSpjZJVyzUeqt HI3Nkf+fSufpaHEbe1J0r2KdLK2Kdy620Ee5GDhAc8dIf0HyQTwmetEXNSdAN0+0QEOcij Uwwz6B37P0rzAF7XP+oJeFNaBoAAvcw= Received: by mail-ej1-f54.google.com with SMTP id a640c23a62f3a-97000a039b2so170019166b.2 for ; Fri, 26 May 2023 10:40:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685122801; x=1687714801; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=o16G9dPIrqAga4lo4DLIjG9Wv4N2BLNRqrSSEriNgT0=; b=qpu9Bv+u7sPo5eBwj+7k6ZtiCbuuZCpIEi9hh0plPkI6u5aNcCQTzZzsn0fS3h/C7Z NhT6vIH+D/GFR7jQKVruYL7dW6qexXdHpy5hjtaWtn5yQa3hxefZ/OAoYsi9MeSlDxo6 WMmk4Qn+26E8hXHeS0v+XapEpqLxnkXIupRY0Z/SPpZE5RUTxHUjMivfnwQu9mMtk4le LzBEblatDND/t3yh3h7APgqOQZvFMHQq/q4RGW/F2P2FEewb6b4expNsd91LlEZh8OFc kv900xSJiPdqrbHxQ5sIXsTbewL3netuAfbyKTys8STiDziFfVKrNXNvrIRxZ4+orlDU K6kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685122801; x=1687714801; 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=o16G9dPIrqAga4lo4DLIjG9Wv4N2BLNRqrSSEriNgT0=; b=H5jrJEeCaAZmWme5KsO8cZA3V+vJLfXCj46kWOuKDMQwmYH8MnItTH6eXJYHjVgR8p sdxHHrUvNwX+ln2Nhw6FkexCQ3Qn4dqecUoZs/XWpG+kKFJl9udDd1YCZaWe7Q/nX4Ua bA3vs/5UBJ965qEEFsa3DyWahdWaJE5DKeDOGM5idAitKjnlToLrIUA/T/nSmdzaculO bkHTYB9WkEeL5BCwansME0+3p7rRmdfSbZ3Zc3rsxPR80F+gEeTQkS7Th6P5hdEoOi3Q IM7F8Ov67k/hKQW3TPyE/lZuCxbDeoHE+DK9InK8Jrq4GeHehGclkXFnYmpDxRk/yct8 DGhw== X-Gm-Message-State: AC+VfDw/ankfxC1gLZpKjmUBTffF8VByaq1iodGrVIkRg7ca5jkf5lLW TuJjPKp7mgki9WZTVEJ1y+7QHbfe8qmgI13B X-Google-Smtp-Source: ACHHUZ7HiivSeQORwj2h+UiuD32hVlBu7zcVhSUj2RPNw/rPXS+3z0TyozmYmEVObz5aoV1AuyOf2w== X-Received: by 2002:a17:907:98e:b0:957:943e:7416 with SMTP id bf14-20020a170907098e00b00957943e7416mr2375842ejc.15.1685122800824; Fri, 26 May 2023 10:40:00 -0700 (PDT) Received: from lelloman-5950.. (host-79-43-28-95.retail.telecomitalia.it. [79.43.28.95]) by smtp.gmail.com with ESMTPSA id lf20-20020a170906ae5400b0096f84292cf5sm2381205ejb.132.2023.05.26.10.39.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 10:40:00 -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 v2] mm: zswap: shrink until can accept Date: Fri, 26 May 2023 19:39:55 +0200 Message-Id: <20230526173955.781115-1-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 011AE14000B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: fn7mwh51w3o7ynkb5tnzxc3it748u3an X-HE-Tag: 1685122802-46439 X-HE-Meta: U2FsdGVkX1/+u5mzfbb5YlMs7K7Zj61d3EK7h08ecewa5obH0GNZcVVH+6FXIW5zuQACNd8xdYm6/YxGZe8FXDLNYqCzG4xYM0rE+G24yy12V7EpAKZoVAaE4m/mf4kzA5t1+qlzUd2REokAazaDItKtNBl39V46Vt0/lzFjw3r+otteYlA+V7GKlzTvifht3ZakRx/Rs9RIvIr3M3yFKRxm6ibopjms5Je/dOWKAx1b7m+ztJ83bICvmAGoOogUoiQ3An7ufDnJ9Umx4UmaLBtt3RII86OQ+ZpB1LqbBGojMnxhn50cPXHi6/+4kKmnXQZCAt006u99FA5pxYParvRKI0iQRg0jpHFfYyncpac/nYKqUPYwb6VanfWWaHxQqC1MC4xtCMG0K9f5fD7rWlwlokyXpVrIghsJO5ljTNHCds+qS09peFvOKpE8gxfmRtx9dJ/6Z/EqJqtO28ng3aZjd+pYELaByu1lPlmXCdvNBYcZwETbPRxzN/d0dbPHyQ0Vzm0mtQPdcHVgOrFIUav2Kdh28L0dYw30rmLhNNzPNPXgUrcz8YFSTJgZMKvSb96vxyRHLyoEdv9pOr0rx/z2jZLKyJsc7Sqbqp9Kkg/7Zxtm/xDsJcxDFnBrhkQ2XmE/GSbtj+3k1LnOqs6dx4DEWw+un4nSge0HJc3vilU6PRYPsMF4R6UrvawzSxgFuYTGNcrYGRByyzmVqFlyJBXSTXO2BMbdn5z1r2b6TVqY1riw/HvZJ11xFn+GtqPvR381xNRqsQqnaksSRUXod3fjYxXXBxXbSzk6/U7z+PJUkT7efnJa7thHcYFuY6cNtMBkPz9/cUWNjwV2qQ2vNYYuVVdwzh8UkFI5a/XmUp91sp6UanhTbeCOvmQD4VArM8CJFJ+7P9fCzdHWy/huadrUmZmQG+FKkrkYSnkbMBJFXAPqYlN3VmHxU/bLaY1ij56y674iQs/5cLQwNfx 5IwePWLf tNsmMixByzCw9di8/cPbpDjhppyotR38f+tmoavhNeSbtY4kjItrsRRkAMhijIGnETdwbBXD+ECSobC3KwD2oV/9eSUxFvPRHZL8UqxWKoCGl6v3JNmxoMnT7rgK52OJucm+mR4Kl6D8kpm1As0a8faMFIvxs4aAvN7A8LuE1q3InE4Ijxv+CMYzpaVslMhrf+vCu8bNrn/chB7PbQi4+LkuirGKoz8fMCSUQeBEwpvwR/x7WaObw2mMZrp2+3XGOu91cTBC834QnrZk+68hEQ18079Nwc14zPy5yE3MQA3VjooG/H83DnvMeHrcElx3f1a9cy95TYDgZj3Ksfy/0hjZMmMK/96QpB9ioMhh9RvdyBrpF9EI6w6KkB77dHUY2fdk1p/mYyxmsEbRp6znPtFfzXyVQhb3/YrHI+riNaqUquY5g8TEUnlj6qO2KFMJieaOx 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: 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 V2: - loop against == -EAGAIN rather than != -EINVAL and also break the loop on MAX_RECLAIM_RETRIES (thanks Yosry) - cond_resched() to ensure that the loop doesn't burn the cpu (thanks Vitaly) Fixes: 45190f01dd40 ("mm/zswap.c: add allocation hysteresis if pool limit is hit") Signed-off-by: Domenico Cerasuolo --- mm/zswap.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 59da2a415fbb..f953dceaab34 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -37,6 +37,7 @@ #include #include "swap.h" +#include "internal.h" /********************************* * statistics @@ -587,9 +588,17 @@ static void shrink_worker(struct work_struct *w) { struct zswap_pool *pool = container_of(w, typeof(*pool), shrink_work); + int ret, failures = 0; - 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++; + failures++; + } + cond_resched(); + } while (!zswap_can_accept() && ret == -EAGAIN && + failures < MAX_RECLAIM_RETRIES); zswap_pool_put(pool); } @@ -1188,7 +1197,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; }