Return-Path: Received: from mail-co1nam03on0118.outbound.protection.outlook.com ([104.47.40.118]:6048 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032196AbeCAP27 (ORCPT ); Thu, 1 Mar 2018 10:28:59 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Andrey Ryabinin , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [added to the 4.1 stable tree] mm/page-writeback: fix dirty_ratelimit calculation Date: Thu, 1 Mar 2018 15:23:56 +0000 Message-ID: <20180301152116.1486-140-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Andrey Ryabinin This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit d59b1087a98e402ed9a7cc577f4da435f9a555f5 ] Calculation of dirty_ratelimit sometimes is not correct. E.g. initial values of dirty_ratelimit =3D=3D INIT_BW and step =3D=3D 0, lead to the following result: UBSAN: Undefined behaviour in ../mm/page-writeback.c:1286:7 shift exponent 25600 is too large for 64-bit type 'long unsigned int' The fix is straightforward - make step 0 if the shift exponent is too big. Signed-off-by: Andrey Ryabinin Cc: Wu Fengguang Cc: Tejun Heo Cc: Andy Shevchenko Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/page-writeback.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index eb59f7eea508..308757ae508d 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -967,6 +967,7 @@ static void bdi_update_dirty_ratelimit(struct backing_d= ev_info *bdi, unsigned long pos_ratio; unsigned long step; unsigned long x; + unsigned long shift; =20 /* * The dirty rate will match the writeout rate in long term, except @@ -1094,11 +1095,11 @@ static void bdi_update_dirty_ratelimit(struct backi= ng_dev_info *bdi, * rate itself is constantly fluctuating. So decrease the track speed * when it gets close to the target. Helps eliminate pointless tremors. */ - step >>=3D dirty_ratelimit / (2 * step + 1); - /* - * Limit the tracking speed to avoid overshooting. - */ - step =3D (step + 7) / 8; + shift =3D dirty_ratelimit / (2 * step + 1); + if (shift < BITS_PER_LONG) + step =3D DIV_ROUND_UP(step >> shift, 8); + else + step =3D 0; =20 if (dirty_ratelimit < balanced_dirty_ratelimit) dirty_ratelimit +=3D step; --=20 2.14.1