Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp16270201ybl; Wed, 1 Jan 2020 01:40:25 -0800 (PST) X-Google-Smtp-Source: APXvYqzqQSA5/6LkGz9nbKb87H8TyXStcr3heIIojeYepWPOilbqoygzwHFGrOzk4SXSoQnLJSoU X-Received: by 2002:a17:906:a44d:: with SMTP id cb13mr82333342ejb.258.1577871625641; Wed, 01 Jan 2020 01:40:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577871625; cv=none; d=google.com; s=arc-20160816; b=EzOmGZtfLBONaUi7lDXVIpxmgC1tsLWiIbW8wdaHcW9E1qxxY3/S99Ny6sKJ8CwQ55 4iSvlvymXtOsOfDZC/7UK8I22wEMKpKr8k7qDfqS8+xY8j9UtGygGSdJ0zmYOlDsCRKN yU3UXTqfggzZRrFyx7ggOkIx9iO6AM0wd+ZWwFaOzvKzewsEKP4bSnue/WASuLieT85t ck3VkvamZDI9GjXyZzDGV6BnJCGTE++1n7a2zObvemuZRHdH3O+LhYICOXwK3QCHLUcs dEHyI+Waig8Pv/L9KfEw2ZtIk5zecZuCEyEKsA261t6ChStlwx39KtdMcHTg5mdAEl7G uujA== 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 :message-id:date:subject:cc:to:from; bh=xSx6FBKvgk7SsscMExKuFSJ5XZF8bBTAZk2bSMZ6t1Y=; b=uUz8h2NQKPFzCVeh1o8iJaqAfpyfY3wIH1WRzGnvLZzzBcqtoKdGzoLpqecbX4QryH aeDx3vg/4hFSE0R/UcKKbjvX5Nx0XK0SyyR9UGI+qr4YVoAsdZc3tEkdJe7GCBDFW+oR rKLB4h+CjEJHhfT0f/CC/MItZWz/lR4jrX6XsYHUOspFOnRLBekU7JCPE3PjRtHJKa6G ljVR672nAJwwJtiLGCWeXVlUY2IZ4MvSms/99jyGIvAzKQqSSvdoVaEqdJfVPDSjMZdC vSMNITaT79S+Tw91Q/TWQxa0k4VufLfMveq24Jg5gGK021ntjEnC5Ij0GB7hDVGsPPrB RjtQ== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e8si33595937ejb.200.2020.01.01.01.40.01; Wed, 01 Jan 2020 01:40:25 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725916AbgAAJci (ORCPT + 99 others); Wed, 1 Jan 2020 04:32:38 -0500 Received: from out30-54.freemail.mail.aliyun.com ([115.124.30.54]:42992 "EHLO out30-54.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725783AbgAAJci (ORCPT ); Wed, 1 Jan 2020 04:32:38 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R721e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01f04446;MF=wenyang@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0TmU9qf5_1577871126; Received: from localhost(mailfrom:wenyang@linux.alibaba.com fp:SMTPD_---0TmU9qf5_1577871126) by smtp.aliyun-inc.com(127.0.0.1); Wed, 01 Jan 2020 17:32:14 +0800 From: Wen Yang To: Andrew Morton Cc: xlpang@linux.alibaba.com, Wen Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/page-writeback.c: avoid potential division by zero Date: Wed, 1 Jan 2020 17:32:04 +0800 Message-Id: <20200101093204.3592-1-wenyang@linux.alibaba.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The variables 'min', 'max' and 'bw' 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. For the two variables 'numerator' and 'denominator', though they are declared as long, they should actually be unsigned long (according to the implementation of the fprop_fraction_percpu() function). Signed-off-by: Wen Yang Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/page-writeback.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 50055d2..2caf780 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -201,11 +201,11 @@ static void wb_min_max_ratio(struct bdi_writeback *wb, 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); } } @@ -766,7 +766,7 @@ static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc) struct wb_domain *dom = dtc_dom(dtc); unsigned long thresh = dtc->thresh; u64 wb_thresh; - long numerator, denominator; + unsigned long numerator, denominator; unsigned long wb_min_ratio, wb_max_ratio; /* @@ -777,7 +777,7 @@ static unsigned long __wb_calc_thresh(struct dirty_throttle_control *dtc) wb_thresh = (thresh * (100 - bdi_min_ratio)) / 100; wb_thresh *= numerator; - do_div(wb_thresh, denominator); + wb_thresh = div64_ul(wb_thresh, denominator); wb_min_max_ratio(dtc->wb, &wb_min_ratio, &wb_max_ratio); @@ -1102,7 +1102,7 @@ static void wb_update_write_bandwidth(struct bdi_writeback *wb, bw = written - min(written, wb->written_stamp); bw *= HZ; if (unlikely(elapsed > period)) { - do_div(bw, elapsed); + bw = div64_ul(bw, elapsed); avg = bw; goto out; } -- 1.8.3.1