From patchwork Thu Jan 9 06:34:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13932101 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 B2B9BE77197 for ; Thu, 9 Jan 2025 06:34:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 450F46B0092; Thu, 9 Jan 2025 01:34:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 401946B0093; Thu, 9 Jan 2025 01:34:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A02F6B0096; Thu, 9 Jan 2025 01:34:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 0C89F6B0092 for ; Thu, 9 Jan 2025 01:34:19 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B7BDC12175D for ; Thu, 9 Jan 2025 06:34:17 +0000 (UTC) X-FDA: 82986948954.11.3D0111D Received: from fout-b4-smtp.messagingengine.com (fout-b4-smtp.messagingengine.com [202.12.124.147]) by imf04.hostedemail.com (Postfix) with ESMTP id D57B340003 for ; Thu, 9 Jan 2025 06:34:15 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=devkernel.io header.s=fm3 header.b=JvkfvkAm; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=Cse9jzVa; spf=pass (imf04.hostedemail.com: domain of shr@devkernel.io designates 202.12.124.147 as permitted sender) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736404456; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=trX5EeQwImb16/yYuWr2dbMrpJFC9z272st4xwQ6DGU=; b=oNke74OvN2OULo6r4SmSVu6pEM46YZLYBTFNokwShUTVGiLhh97sxe0byxDN0re2/nQCSS 5m0dWN1AWTsrwYMSQJ0AukBiCg3mMrI4jskL/kRuypXZW03dYU8C8c9ZLqbdVA0FN15Ao2 Y3CpHIok3b+3KkOrTa6JvJHdsr9etAc= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=devkernel.io header.s=fm3 header.b=JvkfvkAm; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=Cse9jzVa; spf=pass (imf04.hostedemail.com: domain of shr@devkernel.io designates 202.12.124.147 as permitted sender) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736404456; a=rsa-sha256; cv=none; b=xI5drBldNGIs+MYmhx15Ux0lkwYmUs/61W188vLZSeChpB4fpCEaOGEG99R9dfJSp+j1rb xwo/Zexbb7w96HUNSVcjIyAV3EGnT00dr04xMyckpOob7jvSmHta8viZUJLqA3dlrBf0zM Sn22r2iU/hBTe84y9YgWCgsI1PmmL+A= Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfout.stl.internal (Postfix) with ESMTP id D65C01140169; Thu, 9 Jan 2025 01:34:14 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Thu, 09 Jan 2025 01:34:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devkernel.io; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to; s=fm3; t=1736404454; x=1736490854; bh=trX5EeQwImb16/yYuWr2d bMrpJFC9z272st4xwQ6DGU=; b=JvkfvkAmdj1pPxj25+v49pCLQ9aQr4DXdcAPF XyNl8UD3i/jyp10phsg0gLxQn0Nb9e9Njz19c5jVFByltj/6ZxudDkjLLuA1sf/o zD3YPp6jrAT+/sKQtpu7nlrwZ9fFUw6+FW3t7GwDF039JCtDjgYRwPml5146fnjb Fe3okDUwol21VW/BBEm7tYbDmpLTAGfuAtu0x3pFO/lt8soIA+s5OcEIlTqd4MEl sU+5ugQPxnH1S4z/R7h4UhuvudPjRd98O15rPaIk9yu7uP4JnqAWmlNjRl8kxvml U1YoAZtADONm8GT5tBsIG4L4KLo8H1Yxd5yl1Z7nB7jGA2F8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1736404454; x=1736490854; bh=trX5EeQwImb16/yYuWr2dbMrpJFC9z272st 4xwQ6DGU=; b=Cse9jzVaPEhsCYsbq2vmry7XAR+O/yqaXj14kXKUJrmyKPoJFIK 8esyk9O2FK1WO2kJhPtLxqOXPlSKKybZ92FbSQBw4/f5PQKo6FrzJtt770RiFdLC hu+WQmzxq/eF7Al8ZhPjr/p35vG5RrlCuuLJjiyLagpRGC3zxW3L9Q/r5LcM9aPc XkTgxVvMhDO1viuHvZs6RKwCeLdEZ4iHb7r8TdwSvm5PyRL3PUGe/fjuoWdADLku bKSxX/LgeOZ4vOxnJhtqkO85pHnWszDnVYiVIwudntBx4dpyKpMAQMbbTnotj+EJ kabOIcAkaBLgcVgiUDOVK/h61VESPCwr+fw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudeghedgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefhvfevuf ffkffoggfgsedtkeertdertddtnecuhfhrohhmpefuthgvfhgrnhcutfhovghstghhuceo shhhrhesuggvvhhkvghrnhgvlhdrihhoqeenucggtffrrghtthgvrhhnpeevieegfeffhf ekudeuieelgfeljeekgedthfeiveeltedukeegfeeuvdelvdejueenucffohhmrghinhep khgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepshhhrhesuggvvhhkvghrnhgvlhdrihhopdhnsggprhgtphhtthhopeej pdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegurghvihgusehrvgguhhgrthdrtg homhdprhgtphhtthhopeifihhllhihsehinhhfrhgruggvrggurdhorhhgpdhrtghpthht ohepiiiiqhhqtddutdefrdhhvgihsehgmhgrihhlrdgtohhmpdhrtghpthhtoheprghkph hmsehlihhnuhigqdhfohhunhgurghtihhonhdrohhrghdprhgtphhtthhopehlihhnuhig qdhmmheskhhvrggtkhdrohhrghdprhgtphhtthhopehlihhnuhigqdhfshguvghvvghlse hvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepshhhrhesuggvvhhkvghrnhgv lhdrihho X-ME-Proxy: Feedback-ID: i84614614:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 9 Jan 2025 01:34:13 -0500 (EST) From: Stefan Roesch To: david@redhat.com, willy@infradead.org, zzqq0103.hey@gmail.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Cc: shr@devkernel.io Subject: [PATCH v3] mm: fix div by zero in bdi_ratio_from_pages Date: Wed, 8 Jan 2025 22:34:11 -0800 Message-ID: <20250109063411.6591-1-shr@devkernel.io> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: D57B340003 X-Rspamd-Server: rspam12 X-Stat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hlrUNLwzaNi7z5puu5mtAbQD46Gf6y79z3veLwRJfYSuQd6wfCwF6el7qRk71qiNB001F90Y5TAQrJBBTsoVgj2W6na1ydn7lpAPrJZs73V3pnQv15DFyh8wVhg8a8R2NWQpQPLqfU2aFUrr7EvWbVl4olfZW271QetOke7ixdAmLRBEBIFDvipu1vt+fBSFNNDIIH53qYLRUk8YnX7yr6saVh9+A+DQ4myjkAv6w2o+z7UZwoH/JLAiO33j1cbOzH90YOcyh5Tejxe+TiEC2mcaBorhLm1Hx4iJN8sGxWi8NO/ayrEVSCnQMSpXHCHnlzr4l4YzKbKszRJ9LlbFKjAuquXFa+5idgWWp8yZ84WoZrzSA6tIc8cx3bnG4BzIS/eOB/Hqo05rEQNeXeQiZgyzMR70X6qXJlJEum8+j69g7F3i7vFsC/NaWE44tnx2+4UoA/nI9fKhomw== 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: List-Subscribe: List-Unsubscribe: During testing it has been detected, that it is possible to get div by zero error in bdi_set_min_bytes. The error is caused by the function bdi_ratio_from_pages(). bdi_ratio_from_pages() calls global_dirty_limits. If the dirty threshold is 0, the div by zero is raised. This can happen if the root user is setting: echo 0 > /proc/sys/vm/dirty_ratio The following is a test case: echo 0 > /proc/sys/vm/dirty_ratio cd /sys/class/bdi/ echo 1 > strict_limit echo 8192 > min_bytes ==> error is raised. The problem is addressed by returning -EINVAL if dirty_ratio or dirty_bytes is set to 0. Reported-by: cheung wall Closes: https://lore.kernel.org/linux-mm/87pll35yd0.fsf@devkernel.io/T/#t Signed-off-by: Stefan Roesch Acked-by: David Hildenbrand --- Changes in V3: - Used long instead of unsigned long for min_ratio / max_ratio Changes in V2: - check for -EINVAL in bdi_set_min_bytes() - check for -EINVAL in bdi_set_max_bytes() --- mm/page-writeback.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) base-commit: fbfd64d25c7af3b8695201ebc85efe90be28c5a3 diff --git a/mm/page-writeback.c b/mm/page-writeback.c index d213ead95675..d9861e42b2bd 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -692,6 +692,8 @@ static unsigned long bdi_ratio_from_pages(unsigned long pages) unsigned long ratio; global_dirty_limits(&background_thresh, &dirty_thresh); + if (!dirty_thresh) + return -EINVAL; ratio = div64_u64(pages * 100ULL * BDI_RATIO_SCALE, dirty_thresh); return ratio; @@ -790,13 +792,15 @@ int bdi_set_min_bytes(struct backing_dev_info *bdi, u64 min_bytes) { int ret; unsigned long pages = min_bytes >> PAGE_SHIFT; - unsigned long min_ratio; + long min_ratio; ret = bdi_check_pages_limit(pages); if (ret) return ret; min_ratio = bdi_ratio_from_pages(pages); + if (min_ratio < 0) + return min_ratio; return __bdi_set_min_ratio(bdi, min_ratio); } @@ -809,13 +813,15 @@ int bdi_set_max_bytes(struct backing_dev_info *bdi, u64 max_bytes) { int ret; unsigned long pages = max_bytes >> PAGE_SHIFT; - unsigned long max_ratio; + long max_ratio; ret = bdi_check_pages_limit(pages); if (ret) return ret; max_ratio = bdi_ratio_from_pages(pages); + if (max_ratio < 0) + return max_ratio; return __bdi_set_max_ratio(bdi, max_ratio); }