Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2855894rwd; Fri, 26 May 2023 12:17:42 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7iR7aarg0S6brOPLheHeC8T2k+7PxGbbZ355w16WOO3Q2IpDAmOCJo3CJ6Wq49LhQMjHtv X-Received: by 2002:a17:902:e548:b0:1ac:815e:320b with SMTP id n8-20020a170902e54800b001ac815e320bmr4125356plf.17.1685128661682; Fri, 26 May 2023 12:17:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685128661; cv=none; d=google.com; s=arc-20160816; b=GpJoHP0BoBuUypnPzJIaVdMFxVMtPWzX/cFzrnIPClwhG2eMjKAqoWRl2X/7YTECvZ eOfsvHa9wncyKWJHtiqYYxtOdvV7wcyXJV4jkrlGwRcHyfIax7KJTmCkc3B7tL0F86Ga iak1q+5PfRq7t+yEpviWumLoTOQoYP7xVZu/QmLwzO3NW7h0j626jFQq17xXH/FSqWRZ mcSCs7ValiQVV3Heq8iZnRsfy1R9SeY5A0a4ZKpfM6RiEVGBEv9JT6RwfGREqH8pqEMM 5Lg28w0OwS/vTuDJNnqTuBpymJNh+defMgdYvC0N0gE896i6AC6t751XIpX93PxQWP8P xMmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=/dhx1N1TGbdvEAeyn+I2yRhyKZQXdgWDNWhZQJTXxzk=; b=GzbE5fAptfDYGfvk3OPTSYQW+F8qSUBVy4gQatNsvz+l1Bp1pmPhBbDr9UPBUMm64U VIUeUvNv9yf+yDYA2oGXojf8QkEjNGNwuU8CYr0+V91QWs4Qx+bEEF05RbdtJVVWkScM LJ/lKyhKRc97RcSI8ZpctF7M36Cw9bLsBOoarTftgcOd6J5an6fyqUx8JYroFyXup1cM Z4EH70ygXnrZkB+9Ddh2HTYAWDsM1AImrBlc+WUUac7rAi5WMr2EhIVqj4J46QZTUuDq yu5NSKYftOvnhRriAmHCkZPXKwqWWDHJQpt9au7a0HfWMWHoVIp3SDzvTdESXAZ5Phxz nStw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JlR2KKEc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k18-20020a170902761200b001ac6e1fd203si4125956pll.180.2023.05.26.12.17.28; Fri, 26 May 2023 12:17:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JlR2KKEc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242710AbjEZSxr (ORCPT + 99 others); Fri, 26 May 2023 14:53:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237506AbjEZSxp (ORCPT ); Fri, 26 May 2023 14:53:45 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54BF513A for ; Fri, 26 May 2023 11:53:44 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-96aae59bbd6so186649766b.3 for ; Fri, 26 May 2023 11:53:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685127223; x=1687719223; 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=/dhx1N1TGbdvEAeyn+I2yRhyKZQXdgWDNWhZQJTXxzk=; b=JlR2KKEcvO4TYDGY/qCKJnnEN1I6+TVVKBVzDxAwUXL5Szv6TJ2BVB1WoekEuG3xwd 92bYotG3atN7k+AOlirMP/bjRx41mEGDm6Hs29/0Ni5KVTd3uObR9WwN58p+4YlsFNPF iSFsghph/aeCwCTO0MPEqjeK03a0YWsV0fCQnUUr/IxQ9q+rk9UdsFCt64+IQwXg3wCN DtKNyXOlFQ9vcgiVhkyRz2Gzlm6yacd40kCKPe14XLLqdZw5V+hGqPw6v4wyVfl+jmVH e9i2L4aHCUwiK5pNxBNRpY1AuI6W5YhV/mHV/ZrrHCBrA6Epu3tohkQI/A7PqcIMJ/C/ ZM+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685127223; x=1687719223; 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=/dhx1N1TGbdvEAeyn+I2yRhyKZQXdgWDNWhZQJTXxzk=; b=PUiq93qqVc0IayottC5a2IMC3+DuvTB0U99ErDc13FysVshAEMl4lL6FngMGpClr0C 4vozCzWRA/wJWeSGe5Sar3JFOPnb6U2vwTgx9fk3eBUibeniD49FQaHsffRpU3Gnl1cf JqFFK0n+ypQK4H2MP5IBQmXP+Ikres4X/ISmiyYLsBcu9YbEhOYNrvGu8MtPNUzOKvdS lPJuuGokiucOGAO11xPOxlbpRD1dk+zmiicAV012D/vUKfQszHcnpbs3SComWjVDMsJI C4LLdtL8GQmucbQPOdrokyhPKMSaJnRtIIH/8RG63NHyEHpCCOmI54cKYYIgkZrjjbzQ wblw== X-Gm-Message-State: AC+VfDw87zJ6JIUZg7a1posiTLk7mMAaMANg1b+5X/kmrp1P0cl0m+y0 jcsDOg5B+kxydBW5J+pTP5O5HKg/mZchdzgDEB8LGmNbNNSllUftSEA= X-Received: by 2002:a17:906:5d0c:b0:965:7fba:6bcf with SMTP id g12-20020a1709065d0c00b009657fba6bcfmr3288218ejt.67.1685127222651; Fri, 26 May 2023 11:53:42 -0700 (PDT) MIME-Version: 1.0 References: <20230526183227.793977-1-cerasuolodomenico@gmail.com> In-Reply-To: <20230526183227.793977-1-cerasuolodomenico@gmail.com> From: Yosry Ahmed Date: Fri, 26 May 2023 11:53:05 -0700 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, vitaly.wool@konsulko.com, hannes@cmpxchg.org, kernel-team@fb.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 26, 2023 at 11:32=E2=80=AFAM 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: Yosry Ahmed > --- > 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 >