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 63471C77B7E for ; Thu, 25 May 2023 16:53:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CB13D6B0074; Thu, 25 May 2023 12:53:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C62226B0075; Thu, 25 May 2023 12:53:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2A666B0078; Thu, 25 May 2023 12:53:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A245D6B0074 for ; Thu, 25 May 2023 12:53:10 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 75304140BC9 for ; Thu, 25 May 2023 16:53:10 +0000 (UTC) X-FDA: 80829372540.01.79C3B9A Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 5BC5FA0016 for ; Thu, 25 May 2023 16:53:08 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=lpMRkuqL; spf=pass (imf15.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685033588; 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=8yHwoFl0A/2RKkd9CDZCOO1UVgBmrXuW4M9cQO6ErvY=; b=a/zvUOlZpM3CHs0/VBoZBeZ4beYJLu7XFjWSrg2nO0ucwIibdN4NFabs5L0g4VVWX52Wzh wQKZysyWskak/poaVu3xvqxetpIsAQi3CB0s8om2BVHkJZpLLyeVbuD0qCTXXuQYgZk3dh dILWPiElp9ekdZ2Rf7oiLsrRwJrG/WM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685033588; a=rsa-sha256; cv=none; b=XN5A3KADndEGNAD0SiWJYwVKMHbB7QCKQks71asVWuzyPYZOs/Z4xW/U2WpbjDzCO6EZL3 sV8fRFfwKomP6gz1urn6xMLHpF7qVqzNKT1iyU4CAWmKh/zMGXt73uWR9/mtrMJgEadCCi nm8cr8nYD7uJkNDbNvfM4PuAbvjeM1A= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=lpMRkuqL; spf=pass (imf15.hostedemail.com: domain of cerasuolodomenico@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=cerasuolodomenico@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-52cbd7d0c37so1304897a12.3 for ; Thu, 25 May 2023 09:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685033587; x=1687625587; 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=8yHwoFl0A/2RKkd9CDZCOO1UVgBmrXuW4M9cQO6ErvY=; b=lpMRkuqLYneULplpBZz2RXh/cuyVkwoJ68sZeWdnaqCzMMgVPaSeFMDqzv4JPpMK06 C3nvI3Jhsw/YNvHxVgu8/B2nJ/pAtQ0/R5TqhOgncfX9bJiZ3M3RF/rLUsNKvtspfsPe SwH5ebNGkl41kLCi/DP/M0OS8JLet+x9EUUQcv1r9ii1AmB69Wq31VOpC+n+d2PIZ9s5 YBsaA3jHsJ+KQYNXczXFsZzbdQBskG01pkkVEgvEPX0Pc2ERoKcWOCNoz8T5/0bSIaA/ T3Bu9rAWAJObEVv1Inq2PDL1lllQA2rBKqyrtvnSLVD9BD2BNS1GpV0bJXAXzVgoe+Uc YmLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685033587; x=1687625587; 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=8yHwoFl0A/2RKkd9CDZCOO1UVgBmrXuW4M9cQO6ErvY=; b=P1zDLZqug0xmcLpB794Njaz2GgY/vognOHBts5vuGtLswzHYdFH0wAhjX/1uFYvlaw z0R/BMdeuZ0Wp91txCbomI+V/KgnFndGq1pH47WHiQWzilIRY0QpxvoUV5U5cySuxyBS VYbrXlhVhyhsOnoaeRtyMzRT8HEgkQGsn61YOn7E0g3H8YIVK9ceXp89SzCjhw04L6j6 ubD/GNZO+lw0dFMxv/084tz+Zjhx7tXjpI2dK6bQOfTpmxxKiC29mPfJcn00yVVBLFx4 4OWdjac7ofkIrSl/XHmpbV7OMRaZZBk5hsdup0GGzj1bFfZP5G4JmtSGA6uyWMjYhTKJ 7Jcg== X-Gm-Message-State: AC+VfDwhESHwKPofcYIXsN8I5mMSHmD22/ouRiIBjdihXOOXgf54WRnd KCaaUQc3s49ZcSbuQKijDa27ji/T+Xk5yQxe+NCD4K7IVGo= X-Google-Smtp-Source: ACHHUZ5756kDyQ3thlbZSXVFs49/TzQSSWzfrXvvbCU9rvev1DAHCpYtHAWFgLoZPIxbSFz20FZvFNqWssJrqqcd+Ik= X-Received: by 2002:a17:903:22d1:b0:1ad:fcdc:bcb9 with SMTP id y17-20020a17090322d100b001adfcdcbcb9mr3047115plg.23.1685033586859; Thu, 25 May 2023 09:53:06 -0700 (PDT) MIME-Version: 1.0 References: <20230524065051.6328-1-cerasuolodomenico@gmail.com> In-Reply-To: From: Domenico Cerasuolo Date: Thu, 25 May 2023 18:52:55 +0200 Message-ID: Subject: Re: [PATCH] mm: zswap: shrink until can accept To: Yosry Ahmed 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-Stat-Signature: xrwwuwqqhqynh31njno41mo3z95z9yz5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5BC5FA0016 X-Rspam-User: X-HE-Tag: 1685033588-941808 X-HE-Meta: U2FsdGVkX1/kcA147qKQeh0NiUotbIzB/dksA/7On9sUiCVsD83IySiAkYr8THNp3BrEnjKyZ6thUH9962dt3UE8KXgrB0PKmCHmsCIB78bDlL7fRAAkJWDLOcjaRabj2Icjbq6Ia7HKQ5TSASvjY0ictJHBQXYGk4Yi3yMdjA/qu9bQTN0kCnOjJO8tssbgVzCpa+WAmbvfiKWRTAGMpT5uNUrQYt6bFdtctAIUaEGV988GFLMFRpKxIIJxrVSLnZwfpUsmiDqNxgTqxfNTxnPgz9Iry9cr5hZEUn63RruoF+K7rZEUc2+Bj9uCy9If4IEsdjYiBNUNxtFvWlH50p3oFuaTMS5wi7qArwYaTLsWwxHHERUh/l01qr/ZbUQMYyhOB4NgBkUhIrKkjDDE1u75jvLqanrUklxrMuig0+zD5WvGJDF4ZbsKbBn3+xr+MGoIkmf3P8lVUxLWlD1KMXpN8+f2D4F0TkjssW0y20sQJ0MQAk+XEWILa2TzXtU+wCZyTWb5RnCoHTk8CqpeqNXB3PXizhwj2moDNu5OCXoPJNDdXPrOU5EygYqk597VC2OcTeUm4m/rJCoeP+S2Aw72uQRZxuKmILkRbMBqoX2zcXzLlPDbuMFGuSuouZvCnIxX062uBwMP/WHcXMzM0PJA5FsTtfwqGfs/x6NJvFPCIPMlR8g7F3U24P3CzZLMfGfttewzKQTPkl0V8Jj03HdWILWoofql6iAWmjPZuF45TDhKbsIOA1psHvwBaW/Jwo6b+qYsIPtfpUQTSfCdsWPcKeqrhVFJGHb9cNUbFbTn6CzV5PkJUUqyM1u+IuZaBCyBbU01Aj9aENgL+8bFe7/35zJ7eyE3zel+r0PkCRa1NTnFTg+rM/jVnuW06OtHxnoZS9s0tSzsC/4CHRh3uX0KSg0fxsURFLkg4/PW/AFzqEnaj3EJiQSHDT3wqY+G4y7rDhSk+T5XMQqcIaO gh8LVcYV IRS/Fa8sQmqm0FMtBhD+gZnD+3O15LzFFx9Im5a6Od7MeJ1DW1ZahRyiTr/VrbVTl1DPGaFEDlA/I4jIlE9SRM7VzSGE+AUsIjpq+h9VDu9OL+7P+j0fQErq1FXF5EM4MQ6trPgNXhkIDRpIENpPRo0E83jRuF8hFqTc+ZUmgE9c5gKAa8k2IKHO1EPHoYBudoQWfR8Wl94t6UqL++i/Kb7SaYFNSWGnsu66hD9n+afjOrB2G9B5wt+qMuPdqO8uOuPuLn0bdmciozyY7QLPBCGvWn3kfKxxsfIlFeHfo9eqSloSDIV1AhkdTmF4TDu8L5BvnxWg8Be2Ic6aSmbb9LWP1CkPKMT3yvqfgOwRap0AdPeUprKjHNwAd9bDBrYI22/xMdUOyJEank3o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000041, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, May 25, 2023 at 2:59=E2=80=AFAM Yosry Ahmed = wrote: > > Hi Domenico, > > On Tue, May 23, 2023 at 11:50=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 an= d > > 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 i= s > > still above the acceptance threshold. Once we include the rejections du= e > > 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 > > Impressive numbers, and great find in general! > > I wonder how other workloads benefit/regress from this change. > Anything else that you happened to run? :) > Hi Yosry, thanks for the quick feedback! Besides stressers, I haven't tested any other actual workload, we don't hav= e writeback in production yet so I can't provide any data from there. I was wondering what kind of actual workload I could use to test this on my deskt= op, but I couldn't think of anything relevant, I'm open to suggestions though := ) > > > > Fixes: 45190f01dd40 ("mm/zswap.c: add allocation hysteresis if pool lim= it 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 =3D container_of(w, typeof(*pool), > > shrink_work); > > + int ret; > > > > - 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++; > > + } while (!zswap_can_accept() && ret !=3D -EINVAL); > > One question/comment here about the retry logic. > > So I looked through the awfully convoluted writeback code, and there > are multiple layers, and some of them tend to overwrite the return > value of the layer underneath :( > > For zsmalloc (as an example): > zpool_shrink()->zs_zpool_shrink()->zs_reclaim_page()->zpool_ops.evict()->= zswap_writeback_entry(). > > First of all, that zpool_ops is an unnecessarily confusing > indirection, but anyway. > > - zswap_writeback_entry() will either return -ENOMEM or -EEXIST on error > - zs_reclaim_page()/zbud_reclaim_page() will return -EINVAL if the lru > is empty, and -EAGAIN on other errors. > - zs_zpool_shrink()/zbud_zpool_shrink() will mostly propagate the > return value of zs_reclaim_page()/zbud_reclaim_page(). > - zpool_shrink() will return -EINVAL if the driver does not support > shrinking, otherwise it will propagate the return value from the > driver. > > So it looks like we will get -EINVAL only if the driver lru is empty > or the driver does not support writeback, so rightfully we should not > retry. > > If zswap_writeback_entry() returns -EEXIST, it probably means that we > raced with another task decompressing the page, so rightfully we > should retry to writeback another page instead. > > If zswap_writeback_entry() returns -ENOMEM, it doesn't necessarily > mean that we couldn't allocate memory (unfortunately), it looks like > we will return -ENOMEM in other cases as well. Arguably, we can retry > in all cases, because even if we were actually out of memory, we are > trying to make an allocation that will eventually free more memory. > > In all cases, I think it would be nicer if we retry if ret =3D=3D -EAGAIN > instead. It is semantically more sane. In this specific case it is > functionally NOP as zs_reclaim_page()/zbud_reclaim_page() will mostly > return -EAGAIN anyway, but in case someone tries to use saner errnos > in the future, this will age better. Retrying if ret =3D=3D -EAGAIN seems much nicer indeed, will change it. > > Also, do we intentionally want to keep retrying forever on failure? Do > we want to add a max number of retries? If the drivers guaranteed that zpool_shrink will remove at least an entry from their LRU, the retry wouldn't be needed because the LRU will eventually be emptied. But this is an assumption on the implementation of the zpool, so yes, we could use a max retries. I think that being a sanity check, it should overshoot the required number of iterations in order to avoid a premature break, what about retrying a max of zswap_stored_pages times? > > > zswap_pool_put(pool); > > } > > > > @@ -1188,7 +1192,7 @@ static int zswap_frontswap_store(unsigned type, p= goff_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 > >