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 X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF9E3C433ED for ; Sat, 8 May 2021 02:18:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5DEF56141B for ; Sat, 8 May 2021 02:18:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5DEF56141B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A56D28D0021; Fri, 7 May 2021 22:18:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E0028D0014; Fri, 7 May 2021 22:18:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 832AB8D0021; Fri, 7 May 2021 22:18:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0105.hostedemail.com [216.40.44.105]) by kanga.kvack.org (Postfix) with ESMTP id 5EE378D0014 for ; Fri, 7 May 2021 22:18:08 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 132728249980 for ; Sat, 8 May 2021 02:18:08 +0000 (UTC) X-FDA: 78116453856.03.AB0BEA3 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf17.hostedemail.com (Postfix) with ESMTP id EB32E40002C0 for ; Sat, 8 May 2021 02:18:02 +0000 (UTC) Received: by mail-pj1-f47.google.com with SMTP id lj11-20020a17090b344bb029015bc3073608so6577963pjb.3 for ; Fri, 07 May 2021 19:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=/5pdTXR1a72n8c5TEKM/+0a5Uc0rPeP5iyI2mZNg+5A=; b=BJkuhr2ea8Q2XrkvFq20nmCm7sUSDY+E+5+LWZyL9H7De8DwF3jUb4P/uaHpiD8PIn urTaDqjIMfEB0Kh+9bwsoJAw4YeQYtgZNvWpTaU77ggBqAoPCl/Q+IS8UKPi96VNi9Zd 87PVNflkS9yLc6QWEWWQVZ3nzOWwRGsH0qmTJ3xqpYaBFIWc0Dqx1lf/ET4NutPRSz24 51kfZeYGTED80kR2DPxz3ffdIgtYUCyrj7HdMiIj+bmBo1DNkIQVCKuYKQe/vNZE2FHG xw0FsgNy5NRTb6VpD+d6BfPd40Wq46cehkKlSFRUo5Lr5s7MhtjY/pCo43lkp6+RUaiU UK0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=/5pdTXR1a72n8c5TEKM/+0a5Uc0rPeP5iyI2mZNg+5A=; b=NU7PtfegK2aEnJKCBbMuvofbMuy6U6MEhkjxc/lW+1S8js0B6FV/LMYZz1PibKO8NY gHHxg+yuPsNb70YaWkziB+2/0xeTbD1F49kA858zscEP7rRschGX0iu7CNVrlTuwAXj6 M6w51CyToqKxIgqF6JXTTABlMr0PyuxHPW4MZnP3befUwSaghF9fZGWhO+G3aDGuUO9w Ybit1+w1WB5W9/jKk8RuwFrOHTi9i+u/wtSNJ74If3c3LfMkv7O/POG3AMzyALXL2Lh7 8zEEX8eJAbu+o2b9vMwniacrIa7hmQBMir77HTX6JGiHL8nRoKIHrhYYX1BHhUygMKyl kvew== X-Gm-Message-State: AOAM531Oz56/JBCx0uaDiGblUSGhrtlRK1vxueuGXMImlJi5JT/ULIjW AfV3hsL6CgIYXUgmZ5lvODP5p427DWx0PWIu7C8= X-Google-Smtp-Source: ABdhPJzMRIzPrgsVkm2d0+YNSx6BLTdxiYy4PnDpuNbCeF2vj47W+GdpFUj+kN5S8Z3lXIsJ6+uu0Qoeudt/w+CB0Ms= X-Received: by 2002:a17:90a:c297:: with SMTP id f23mr13642776pjt.197.1620440286868; Fri, 07 May 2021 19:18:06 -0700 (PDT) MIME-Version: 1.0 References: <20210428225046.16301-1-wuchi.zero@gmail.com> In-Reply-To: <20210428225046.16301-1-wuchi.zero@gmail.com> From: chi wu Date: Sat, 8 May 2021 10:17:56 +0800 Message-ID: Subject: Re: [PATCH] mm/page-writeback: Fix performance when BDI's share of ratio is 0. To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, tj@kernel.org Content-Type: multipart/alternative; boundary="000000000000adb7d705c1c82706" X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: EB32E40002C0 Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=BJkuhr2e; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf17.hostedemail.com: domain of wuchizero@gmail.com designates 209.85.216.47 as permitted sender) smtp.mailfrom=wuchizero@gmail.com X-Stat-Signature: 6xtxdxhmpf84ez9c4arsb9ejg3j1bck8 Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=mail-pj1-f47.google.com; client-ip=209.85.216.47 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1620440282-311521 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: --000000000000adb7d705c1c82706 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Ping... Chi Wu =E4=BA=8E2021=E5=B9=B44=E6=9C=8829=E6=97=A5= =E5=91=A8=E5=9B=9B =E4=B8=8A=E5=8D=886:51=E5=86=99=E9=81=93=EF=BC=9A > Fix performance when BDI's share of ratio is 0. > > The issue is similar to commit 74d369443325 ("writeback: Fix > performance regression in wb_over_bg_thresh()"). > > Balance_dirty_pages and the writeback worker will also disagree on > whether writeback when a BDI uses BDI_CAP_STRICTLIMIT and BDI's share > of the thresh ratio is zero. > > For example, A thread on cpu0 writes 32 pages and then > balance_dirty_pages, it will wake up background writeback and pauses > because wb_dirty > wb->wb_thresh =3D 0 (share of thresh ratio is zero). > A thread may runs on cpu0 again because scheduler prefers pre_cpu. > Then writeback worker may runs on other cpus(1,2..) which causes the > value of wb_stat(wb, WB_RECLAIMABLE) in wb_over_bg_thresh is 0 and does > not writeback and returns. > > Thus, balance_dirty_pages keeps looping, sleeping and then waking up the > worker who will do nothing. It remains stuck in this state until the > writeback worker hit the right dirty cpu or the dirty pages expire. > > The fix that we should get the wb_stat_sum radically when thresh is low. > > Signed-off-by: Chi Wu > --- > mm/page-writeback.c | 20 ++++++++++++++++---- > 1 file changed, 16 insertions(+), 4 deletions(-) > > diff --git a/mm/page-writeback.c b/mm/page-writeback.c > index 0062d5c57d41..bd7052295246 100644 > --- a/mm/page-writeback.c > +++ b/mm/page-writeback.c > @@ -1945,6 +1945,8 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb) > struct dirty_throttle_control * const gdtc =3D &gdtc_stor; > struct dirty_throttle_control * const mdtc =3D > mdtc_valid(&mdtc_stor) ? > &mdtc_stor : NULL; > + unsigned long reclaimable; > + unsigned long thresh; > > /* > * Similar to balance_dirty_pages() but ignores pages being writt= en > @@ -1957,8 +1959,13 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb) > if (gdtc->dirty > gdtc->bg_thresh) > return true; > > - if (wb_stat(wb, WB_RECLAIMABLE) > > - wb_calc_thresh(gdtc->wb, gdtc->bg_thresh)) > + thresh =3D wb_calc_thresh(gdtc->wb, gdtc->bg_thresh); > + if (thresh < 2 * wb_stat_error()) > + reclaimable =3D wb_stat_sum(wb, WB_RECLAIMABLE); > + else > + reclaimable =3D wb_stat(wb, WB_RECLAIMABLE); > + > + if (reclaimable > thresh) > return true; > > if (mdtc) { > @@ -1972,8 +1979,13 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb) > if (mdtc->dirty > mdtc->bg_thresh) > return true; > > - if (wb_stat(wb, WB_RECLAIMABLE) > > - wb_calc_thresh(mdtc->wb, mdtc->bg_thresh)) > + thresh =3D wb_calc_thresh(mdtc->wb, mdtc->bg_thresh); > + if (thresh < 2 * wb_stat_error()) > + reclaimable =3D wb_stat_sum(wb, WB_RECLAIMABLE); > + else > + reclaimable =3D wb_stat(wb, WB_RECLAIMABLE); > + > + if (reclaimable > thresh) > return true; > } > > -- > 2.17.1 > > --000000000000adb7d705c1c82706 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Ping...

Chi Wu <wuch= i.zero@gmail.com> =E4=BA=8E2021=E5=B9=B44=E6=9C=8829=E6=97=A5=E5=91= =A8=E5=9B=9B =E4=B8=8A=E5=8D=886:51=E5=86=99=E9=81=93=EF=BC=9A
Fix performance when BDI'= ;s share of ratio is 0.

The issue is similar to commit 74d369443325 ("writeback: Fix
performance regression in wb_over_bg_thresh()").

Balance_dirty_pages and the writeback worker will also disagree on
whether writeback when a BDI uses BDI_CAP_STRICTLIMIT and BDI's share of the thresh ratio is zero.

For example, A thread on cpu0 writes 32 pages and then
balance_dirty_pages, it will wake up background writeback and pauses
because wb_dirty > wb->wb_thresh =3D 0 (share of thresh ratio is zero= ).
A thread may runs on cpu0 again because scheduler prefers pre_cpu.
Then writeback worker may runs on other cpus(1,2..) which causes the
value of wb_stat(wb, WB_RECLAIMABLE) in wb_over_bg_thresh is 0 and does
not writeback and returns.

Thus, balance_dirty_pages keeps looping, sleeping and then waking up the worker who will do nothing. It remains stuck in this state until the
writeback worker hit the right dirty cpu or the dirty pages expire.

The fix that we should get the wb_stat_sum radically when thresh is low.
Signed-off-by: Chi Wu <wuchi.zero@gmail.com>
---
=C2=A0mm/page-writeback.c | 20 ++++++++++++++++----
=C2=A01 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 0062d5c57d41..bd7052295246 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -1945,6 +1945,8 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 struct dirty_throttle_control * const gdtc =3D = &gdtc_stor;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 struct dirty_throttle_control * const mdtc =3D = mdtc_valid(&mdtc_stor) ?
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&mdtc_stor : NULL;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned long reclaimable;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0unsigned long thresh;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 /*
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0* Similar to balance_dirty_pages() but ig= nores pages being written
@@ -1957,8 +1959,13 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (gdtc->dirty > gdtc->bg_thresh)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return true;

-=C2=A0 =C2=A0 =C2=A0 =C2=A0if (wb_stat(wb, WB_RECLAIMABLE) >
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0wb_calc_thresh(gdtc->wb, gdtc-= >bg_thresh))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0thresh =3D wb_calc_thresh(gdtc->wb, gdtc->= ;bg_thresh);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (thresh < 2 * wb_stat_error())
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0reclaimable =3D wb_= stat_sum(wb, WB_RECLAIMABLE);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0else
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0reclaimable =3D wb_= stat(wb, WB_RECLAIMABLE);
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0if (reclaimable > thresh)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return true;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (mdtc) {
@@ -1972,8 +1979,13 @@ bool wb_over_bg_thresh(struct bdi_writeback *wb)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (mdtc->dirty = > mdtc->bg_thresh)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 return true;

-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (wb_stat(wb, WB_= RECLAIMABLE) >
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0wb_ca= lc_thresh(mdtc->wb, mdtc->bg_thresh))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0thresh =3D wb_calc_= thresh(mdtc->wb, mdtc->bg_thresh);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (thresh < 2 *= wb_stat_error())
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0reclaimable =3D wb_stat_sum(wb, WB_RECLAIMABLE);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0reclaimable =3D wb_stat(wb, WB_RECLAIMABLE);
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (reclaimable >= ; thresh)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 return true;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

--
2.17.1

--000000000000adb7d705c1c82706--