From patchwork Wed Jan 8 01:47:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13929936 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 48213E77197 for ; Wed, 8 Jan 2025 01:47:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D395F6B0082; Tue, 7 Jan 2025 20:47:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC2146B0083; Tue, 7 Jan 2025 20:47:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3C3D6B0089; Tue, 7 Jan 2025 20:47:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 941086B0082 for ; Tue, 7 Jan 2025 20:47:29 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3B0D81C5EA8 for ; Wed, 8 Jan 2025 01:47:29 +0000 (UTC) X-FDA: 82982597418.04.DE30768 Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) by imf20.hostedemail.com (Postfix) with ESMTP id 61A471C0009 for ; Wed, 8 Jan 2025 01:47:27 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=devkernel.io header.s=fm3 header.b=OQwtYONq; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=otripk5h; dmarc=none; spf=pass (imf20.hostedemail.com: domain of shr@devkernel.io designates 103.168.172.152 as permitted sender) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736300847; a=rsa-sha256; cv=none; b=jEbTfKO5pFhKh+RPBV9Sqpm2pwbd/y2bNhr856pkqZfiuGqIsdB5bwRkVva4+JJNoXo6/s ZzGWR0gKMHsuJQb6z/wQHLlEfuQqUCp4XJ3Ul2xCn6GQsxUJgbBy6geWrN9jx9hzIbaJA8 VD+jiz5ouHCLvH+7cfk8JWd2abkIXS0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=devkernel.io header.s=fm3 header.b=OQwtYONq; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=otripk5h; dmarc=none; spf=pass (imf20.hostedemail.com: domain of shr@devkernel.io designates 103.168.172.152 as permitted sender) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736300847; 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=RN+WmroQAy5U//HcW1Bpqk2VS4LIfsmqhq83IHpBk4s=; b=E8uWKreL28nP+t/iBYVMXUp7Epp9w/9qoKw1AHYkY4V+ppON0Gqz7yD3bJYQpUbdyQz4q/ 9tJ9lZRAQ9vHiAfwuw8sePBox7mtO/6HRzhRd2ZCawgAQv2nDhA8EFl4GbmlExvh89OI2m r6rKy7CdbpU96Z6cbTLnBCX9INWlq3o= Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.phl.internal (Postfix) with ESMTP id D25DC114015B; Tue, 7 Jan 2025 20:47:26 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Tue, 07 Jan 2025 20:47:26 -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=1736300846; x=1736387246; bh=RN+WmroQAy5U//HcW1Bpq k2VS4LIfsmqhq83IHpBk4s=; b=OQwtYONqsNiaBxYn1go/sqjUGPAxtJhtu5v8z HIapm3DAAZu3XuYSP2LT905rfjAMXTEMeeajvQXHkUZkL/2UTuIxZWd7UmHsRuvI 40c4ZWpWeVn5tGhPLMheoTquoXFkAMsesDVYcnhkxjim873yEJJhGCRzCD8s0nmW N93AJHORHeGsl6W3I9bZbyXefMVt/9m2mHhfELr+pCbx2Kn5SZIXxCLVy7cQeiGq hZJdBvh4qZ7YIs5q3/uaScg0pAVyXF5oqfFuDa88K4ISOu/mA6AvzqNd5Rzh90Lk NpXmO4NnJ4vA2TvMYYNXdiCkLlPJg/cZ+GJaDjV1xSHgwtYTQ== 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= 1736300846; x=1736387246; bh=RN+WmroQAy5U//HcW1Bpqk2VS4LIfsmqhq8 3IHpBk4s=; b=otripk5h0y09oGxooWLmyVuiIb7eXAz9vk9nVAcCDdXopvV05oc NCcPkO8frDUkLpTYtT0x7E5XUnz/RHWdttfKjWiFUN/fsqBI+KuNy995X9Qweulp Z0rZHZIs8YIStvbDSLxrVZZ8EUqyaF1ku3BCabqlFU0i1rOCNvFmFNy9u0/jBR05 CLYSY0FyBOBWgAZp98H68GQ7FH7zcDdHHCJqO518IT4g9TkQ8kNbWLqMtRLox6qu 1aznYUK7vlySkORJxEt9BqcbNgpNYexztMmuVQX65VMHgEN8kaf0sxkXl+bk38k1 VpR4cIhFeRlY4sZgUa3nxERYuk14BckajJw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudegfedgfeeiucetufdoteggodetrfdotf 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; Tue, 7 Jan 2025 20:47:25 -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 v2] mm: fix div by zero in bdi_ratio_from_pages Date: Tue, 7 Jan 2025 17:47:23 -0800 Message-ID: <20250108014723.166637-1-shr@devkernel.io> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Rspamd-Server: rspam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Q7 ftSS1Tl9Ip8jBqfeMTHMC2lYCUTmH6k0juxcImCdlJ+j/yWwHr/fPFKVXtsNMHsv82hjh25tUqbuW2Zk9CG4QqrRJzd03rVrWzRtSxUL2AqDVKEv7trxtgF293VI89c5pBV42806t42OXyRmVFU9cPlgBbrLK7N0VTCD2fTY31tUFLmuAObfPIGKF8d9L6dQQWgSkfcqfIcp4HIk1+Vi0uRKl7IkWzucN6/bP6psx0iyre0V9IKLFRq+1LWBxqaErUZ0wKxMxmpyg8nUoV/b9jbm0kMFDezM5pzF7nFvKumuZAr8havv+aZ8gR5BEI0fm3NG26NHrUpSrQev339QkBJrAJCGckWqSAYe9py4LNNaf9OE9STj/9gUJNMZ2xTghHCRJ53ni49PIQ53zdAIiFE8KY1fgVdKJxf8Ivo+8U4Tw41k= 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 --- Changes in V2: - check for -EINVAL in bdi_set_min_bytes() - check for -EINVAL in bdi_set_max_bytes() --- mm/page-writeback.c | 6 ++++++ 1 file changed, 6 insertions(+) base-commit: fbfd64d25c7af3b8695201ebc85efe90be28c5a3 diff --git a/mm/page-writeback.c b/mm/page-writeback.c index d213ead95675..fcc486e0d5c2 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; @@ -797,6 +799,8 @@ int bdi_set_min_bytes(struct backing_dev_info *bdi, u64 min_bytes) return ret; min_ratio = bdi_ratio_from_pages(pages); + if (min_ratio == -EINVAL) + return -EINVAL; return __bdi_set_min_ratio(bdi, min_ratio); } @@ -816,6 +820,8 @@ int bdi_set_max_bytes(struct backing_dev_info *bdi, u64 max_bytes) return ret; max_ratio = bdi_ratio_from_pages(pages); + if (max_ratio == -EINVAL) + return -EINVAL; return __bdi_set_max_ratio(bdi, max_ratio); }