Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4821414ybl; Wed, 22 Jan 2020 05:23:46 -0800 (PST) X-Google-Smtp-Source: APXvYqzNTaZRUrO53Q8NUIfNdcWERIAYWemkeSnwIq9Bma8pUi4h58uKNTf+1pksWqjn6+v7KZck X-Received: by 2002:a05:6808:9b4:: with SMTP id e20mr6387839oig.37.1579699426324; Wed, 22 Jan 2020 05:23:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579699426; cv=none; d=google.com; s=arc-20160816; b=UIEUCD3/I2s6DilQUT4hPXEaR65q+6k9wzeRaqMkFOlyybAHiGzFuqkhnYJfjv7019 Ep0FZTmPj0LNLC/cLk+dhRUS1isrp75eokd6jq6qngUOb5qlHb+E9RIfPHBOEYMmzvM7 vESDK7xmbrZEsPEXuHrcHt8lOUDAsJOTm6UPSj3h16K0ApjHxnBiVAfb3Syucx+s/LQU tu5Vvd3cuapSDcTdTyt6jGLnbjKRAJGxJZhcg/r7lwJZembdO2bUv8XuiJtUHt3VVC/B tU7J5Q3PTGfJ+lQJSrn/09StDpo2ysyMmcN1yESjDxSkYYJRcOKtyc16VTHClT9feFpE f84w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kP5drnnPj84Pq39v1jsenrEtFBFE93mG/RWckPNal5s=; b=0EpuIWJz9IYfefkfThjT/MBa4mnU/YAIsV66yLvupYYa5gTvB629KHat0i7M4kvPui 5zfDBWYxCkIpy5p6AC40j8ZCj0tJZGBVNonkzHCvkJAzdt3bUyROvWjzhty45Bvnlhsg f7KSueJ2WZx2dPy9JvZjvC/DDnqr+Sndep+pzxsJUY33iUSTWtX7XExhU51jQVoFc+ni tPFhPf8m25LWt+PhUHGUHU/hcvDCHTPwVzeswfanZpUcwet+Jy6IMW69P9WTu/KCCrLb 3KB4wXM043LEmS++IKZrBmr8lerxDDZWhQ/YtFYE3b8rMnM2UPVviztQBzG8DYzLnZ1v bJog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="syUNxM/7"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u84si21555389oib.34.2020.01.22.05.23.34; Wed, 22 Jan 2020 05:23:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="syUNxM/7"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730185AbgAVNVH (ORCPT + 99 others); Wed, 22 Jan 2020 08:21:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:37746 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730155AbgAVNVG (ORCPT ); Wed, 22 Jan 2020 08:21:06 -0500 Received: from localhost (unknown [84.241.205.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 39BCD2468C; Wed, 22 Jan 2020 13:21:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579699265; bh=KBgjthE4MXi4jjoOKexi2wKqKnsrS/GTG2qljJ1vEok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=syUNxM/7i1jhydHF9iW/5AHgjZsOS1EuTqknxlRNbFy8Jql7pk//y5zqTnW+v/ggp 2OCzWaXSrqeQEL/Hf5ZTwVEti5OGUvEmvWuJ6ofBjBQgcYYmVIodesYic3Ii/lBkco /yPfyCFXlNysZuY3Btwhq9mrHvV6MBidm5Z1330k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wen Yang , Andrew Morton , Qian Cai , Tejun Heo , Jens Axboe , Linus Torvalds Subject: [PATCH 5.4 091/222] mm/page-writeback.c: avoid potential division by zero in wb_min_max_ratio() Date: Wed, 22 Jan 2020 10:27:57 +0100 Message-Id: <20200122092840.249902197@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200122092833.339495161@linuxfoundation.org> References: <20200122092833.339495161@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wen Yang commit 6d9e8c651dd979aa666bee15f086745f3ea9c4b3 upstream. Patch series "use div64_ul() instead of div_u64() if the divisor is unsigned long". We were first inspired by commit b0ab99e7736a ("sched: Fix possible divide by zero in avg_atom () calculation"), then refer to the recently analyzed mm code, we found this suspicious place. 201 if (min) { 202 min *= this_bw; 203 do_div(min, tot_bw); 204 } And we also disassembled and confirmed it: /usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 201 0xffffffff811c37da <__wb_calc_thresh+234>: xor %r10d,%r10d 0xffffffff811c37dd <__wb_calc_thresh+237>: test %rax,%rax 0xffffffff811c37e0 <__wb_calc_thresh+240>: je 0xffffffff811c3800 <__wb_calc_thresh+272> /usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 202 0xffffffff811c37e2 <__wb_calc_thresh+242>: imul %r8,%rax /usr/src/debug/kernel-4.9.168-016.ali3000/linux-4.9.168-016.ali3000.alios7.x86_64/mm/page-writeback.c: 203 0xffffffff811c37e6 <__wb_calc_thresh+246>: mov %r9d,%r10d ---> truncates it to 32 bits here 0xffffffff811c37e9 <__wb_calc_thresh+249>: xor %edx,%edx 0xffffffff811c37eb <__wb_calc_thresh+251>: div %r10 0xffffffff811c37ee <__wb_calc_thresh+254>: imul %rbx,%rax 0xffffffff811c37f2 <__wb_calc_thresh+258>: shr $0x2,%rax 0xffffffff811c37f6 <__wb_calc_thresh+262>: mul %rcx 0xffffffff811c37f9 <__wb_calc_thresh+265>: shr $0x2,%rdx 0xffffffff811c37fd <__wb_calc_thresh+269>: mov %rdx,%r10 This series uses div64_ul() instead of div_u64() if the divisor is unsigned long, to avoid truncation to 32-bit on 64-bit platforms. This patch (of 3): The variables 'min' and 'max' are unsigned long and do_div truncates them to 32 bits, which means it can test non-zero and be truncated to zero for division. Fix this issue by using div64_ul() instead. Link: http://lkml.kernel.org/r/20200102081442.8273-2-wenyang@linux.alibaba.com Fixes: 693108a8a667 ("writeback: make bdi->min/max_ratio handling cgroup writeback aware") Signed-off-by: Wen Yang Reviewed-by: Andrew Morton Cc: Qian Cai Cc: Tejun Heo Cc: Jens Axboe Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/page-writeback.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -201,11 +201,11 @@ static void wb_min_max_ratio(struct bdi_ if (this_bw < tot_bw) { if (min) { min *= this_bw; - do_div(min, tot_bw); + min = div64_ul(min, tot_bw); } if (max < 100) { max *= this_bw; - do_div(max, tot_bw); + max = div64_ul(max, tot_bw); } }