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 0BC0DC77B7A for ; Fri, 26 May 2023 19:14:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B62B900003; Fri, 26 May 2023 15:14:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 66430900002; Fri, 26 May 2023 15:14:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52B31900003; Fri, 26 May 2023 15:14:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 40E5C900002 for ; Fri, 26 May 2023 15:14:34 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F3E67A024B for ; Fri, 26 May 2023 19:14:33 +0000 (UTC) X-FDA: 80833357668.16.8966DAA Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf25.hostedemail.com (Postfix) with ESMTP id 0C2F2A000C for ; Fri, 26 May 2023 19:14:31 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=konsulko.com header.s=google header.b=TMqInZcA; dmarc=pass (policy=none) header.from=konsulko.com; spf=pass (imf25.hostedemail.com: domain of vitaly.wool@konsulko.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=vitaly.wool@konsulko.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685128472; 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=u5x3XJWx1/nmx62cve4XckRaGFykR4ZpnsebTaJ31FU=; b=8O72i2GiTMGW0oTAPGVmRGcFl0A8NNRe9aTdgiHtpPaMIdaWCySN+eTMZMvrF1esro0h3h Ad3Y7AZ0NiEC0LLnM30houTznZegWqwkzLJrGctjcgEG9wWXj+WaI58iuGz3eu2yMoGhji gtHqCQ2nK6VBag7PfriCgnjDa/DQDMQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=konsulko.com header.s=google header.b=TMqInZcA; dmarc=pass (policy=none) header.from=konsulko.com; spf=pass (imf25.hostedemail.com: domain of vitaly.wool@konsulko.com designates 209.85.215.181 as permitted sender) smtp.mailfrom=vitaly.wool@konsulko.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685128472; a=rsa-sha256; cv=none; b=ZLK4k2c1LJKeWi54W0cDm7/fmAxPi3MqXrGtlSCtZeDxgJ7lwxYYZnf1M/Thfd8huzE+sa 5Uy1fk9Bp0tkFOSCVS+xWajkdAJCnRueh8MCPIOeIxuPX1gnkUXsuHHP/r3+ioRQmPP1JG KYi1Ej/LhviS9diFotS9jkfI7HRNlrA= Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-53eee18a192so990420a12.3 for ; Fri, 26 May 2023 12:14:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=konsulko.com; s=google; t=1685128471; x=1687720471; 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=u5x3XJWx1/nmx62cve4XckRaGFykR4ZpnsebTaJ31FU=; b=TMqInZcA6suiFfe0+hyfa9q5CSrRxFnc803hnqBHTivD4iyiKD3DDDQwcBOGY36bNP Ff7owmywwGkLncogN43ovQHWk+DpFXSwOspnU2AT8yx0XMd/21FLtq2P2Qh3/qiBhCC3 xP9p8LTdAsn/0yxFjhdaKiSsXCljDY3tNVOq8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685128471; x=1687720471; 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=u5x3XJWx1/nmx62cve4XckRaGFykR4ZpnsebTaJ31FU=; b=QJk0EsLF8//oUjgXKEBtpxbzeH99NlTvXdAMFDWSKDAuQdTAj0WXZFWOEwqceSU+Q+ qMQzcc8K1fK0v/gD+JWnhKiYfqbmnbnpShkwVFJeTN6IdhQaV+eWUgYmhQ51X0BoehjT 3HxOkwa9r2Cvyq7vissx2I0DSbrA95YSVonLcoU45wkhjZWuuOe7z1Awl39+TzGe/iKk G0div+9SiGkz75rbtLW1pYIc3932bSkg2spdpBH/hJMifPaW4OXMvAaFOrRrHYKUzFt3 CCT59yOBdD8sntPlNmYIwAAaUUK3+F5j/Kxq0zZwLyxwKQ+ERWNwj73TP65cJSTZghjG 2B0g== X-Gm-Message-State: AC+VfDwfXDbOsrzl3JiEOsVzqZeZLQgavM9499ObiExS23ZfKLxEusI9 ITXTsm3QA3vAvOK9OdNHRyUGLTvlmyVUTMEzNo5kJg== X-Google-Smtp-Source: ACHHUZ55SmRVQfgWXX7XDjjkUEr0Y5hLZsamcIz8yyUNqpwpuRDwUQXDdOelxW4oWR+IE6SHQsX3IAqJ+V97kx+u8Jc= X-Received: by 2002:a17:903:280f:b0:1ac:a6b0:1c87 with SMTP id kp15-20020a170903280f00b001aca6b01c87mr3098912plb.48.1685128470662; Fri, 26 May 2023 12:14:30 -0700 (PDT) MIME-Version: 1.0 References: <20230526183227.793977-1-cerasuolodomenico@gmail.com> In-Reply-To: <20230526183227.793977-1-cerasuolodomenico@gmail.com> From: Vitaly Wool Date: Fri, 26 May 2023 21:14:19 +0200 Message-ID: Subject: Re: [PATCH v3] mm: zswap: shrink until can accept To: Domenico Cerasuolo Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, sjenning@redhat.com, ddstreet@ieee.org, yosryahmed@google.com, hannes@cmpxchg.org, kernel-team@fb.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 0C2F2A000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: es6ofidusbnf4rsiwnjpm589t1bddd8d X-HE-Tag: 1685128471-188701 X-HE-Meta: U2FsdGVkX18+SXQ/7tDJzCEXG1xXKMJaBmrtXrec0GUUAn2OEBm7nG5Ep6Y685pygh5Gd7ZaxPB4wjSm05KfELjzcONO/1hMVzRdEEHM3qWRA8e6CrQ4OZRe3T0HbOant7fNI+Ron/OHzDaERCKWB93/4Wp9d6xSMNIsFfhdyDup4t+ZzQqjfi23Ogy5x9xi/R6HAQ0pcl7nGCGjKZKRWDZ5wZaPLhALoXU9BEsxddSnLfOFkpAT7yEFqdRvmLMI4iTaIB6R7XAaAAZNChAZ0e6+3edK6q+wCsPDWSWNEcz2iB4AzEI2dYPRmitOsXs0ku5p50S0Zym7jUpQQGeracWvxDQ6HWCTtseUnNVLXFcn5LCh0kAaEuG5oq/AjA5f4tqQKEqhGzk95G0HA0aBBdW6UIZnHyLKRlTDq8Pw6PkB3tKnUxnoeAnVrKp25mw98RcQtYMQ0SWT6oEuIc2hPWwHvAC7TPHUOQOPqvQo45o6chLdKH898vXRXtuUO00vFJUGshjyV3l2b8fyxH1x5yWXm1R/R7jsVG9ANn/mrXF3c4OZKekatGda+3pEqu8CZxgF3Orppn2cvCFhrAp/6KBGszngunsT+CqOAwpfbFrmKm2NU1kELbYDyIv/TqXifXWPbaJTbgdd4TCVBQ7TyVgoiDpF5o2UQ2HvI/9CvKvyDRLKU3G/C98ouaZfBN8+dCI91wFb8EJsYKf8chFpOAlEeJKvYmvlBS6PlyB2F182dEWF7FNTtbhh3jgeeriWXhphSKIK6LY8mCB94Eob7faGcLc8DhiUvjDs78wCoN1bqqbGGIpUrm5T0TGRd8eZdIAf9Ghh+fxeUIKBdffuKJJUg3KhF6Vw0hbtgBfog+m/ylgKaV6EyyZP+YsECafJySVYE0v6WVTq00duUDvYxl7TxWpg+AaJ2a79Rwi2ajL2hrbmWM2qfYtrRWbVyoueDaXZPMF+vN5xl1M2i1c Rfrf3P8m QzpHaTSG15N3dBtIFV5lkf3s/nSAHP21S5ts9DlLjHRNsmHz/u2t6cY1w/+tK/Vf0zw90hzjpACrX1Z8dbTHQTRV34Ru9y/9OToFshYKlzoZJO80PCF533GjqOQYZbxQEgXoW4DEyzep1ImgCHihOlkmDkI+5SI/+b/+foTXj05aJu2XYEy9/XPIzziB+jN2O/RDPd3Md9w+OcIFXMGHqklkuBZ7d7KcOZ1zomAKGkxjqUCPEKlNQ3bVEKDInkua01s9UNr1bsCewZL/TXmcQdj5p2bL4m5QT8eLlGp0S6I4dwTOb1h8cr1hKLg== 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: On Fri, May 26, 2023 at 8:32=E2=80=AFPM Domenico Cerasuolo wrote: > > 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 =3D=3D -EAGAIN rather than !=3D -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) > > V3: > - fix wrong loop break, should continue on !ret (thanks Johannes) > > Fixes: 45190f01dd40 ("mm/zswap.c: add allocation hysteresis if pool limit= is hit") > Signed-off-by: Domenico Cerasuolo Reviewed-by: Vitaly Wool > --- > mm/zswap.c | 17 ++++++++++++++--- > 1 file changed, 14 insertions(+), 3 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index 59da2a415fbb..bcb82e09eb64 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -37,6 +37,7 @@ > #include > > #include "swap.h" > +#include "internal.h" > > /********************************* > * statistics > @@ -587,9 +588,19 @@ static void shrink_worker(struct work_struct *w) > { > struct zswap_pool *pool =3D container_of(w, typeof(*pool), > shrink_work); > + int ret, failures =3D 0; > > - if (zpool_shrink(pool->zpool, 1, NULL)) > - zswap_reject_reclaim_fail++; > + do { > + ret =3D zpool_shrink(pool->zpool, 1, NULL); > + if (ret) { > + zswap_reject_reclaim_fail++; > + if (ret !=3D -EAGAIN) > + break; > + if (++failures =3D=3D MAX_RECLAIM_RETRIES) > + break; > + } > + cond_resched(); > + } while (!zswap_can_accept()); > zswap_pool_put(pool); > } > > @@ -1188,7 +1199,7 @@ static int zswap_frontswap_store(unsigned type, pgo= ff_t offset, > if (zswap_pool_reached_full) { > if (!zswap_can_accept()) { > ret =3D -ENOMEM; > - goto reject; > + goto shrink; > } else > zswap_pool_reached_full =3D false; > } > -- > 2.34.1 >