From: Sasha Levin Subject: [PATCH AUTOSEL 6.2 08/21] blk-iocost: fix divide by 0 error in calc_lcoefs() Date: Sat, 25 Feb 2023 22:41:37 -0500 Message-ID: <20230226034150.771411-8-sashal@kernel.org> References: <20230226034150.771411-1-sashal@kernel.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Return-path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1677382927; bh=WLuOMG/bVQNKvU7Hd47X8E8GsihxLPJ5GN0xQ8TUlW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A6HaYCgARqZOOLOvYisiJvsLqOOP/N+iKtMjMTJtUnl26hyou2hlHcVh55ciDHzfd q7AQ7KatSYCjTRyNLjayujZmlz8RHL4L7xGiKt7Z7dxBcveENalHIgfZWb5fTsFsaf vAY5UJoYrJi2CUxP0cQEvYbo9PRo0uouJHCSN7I2IrphCoochrMTp2wLFzE8a1JsZ+ LSZO96QZ355teyTpQ/Tv8w/FFH1W8C6nSIqbGcasayObo5hLnUiJLZPpCkFSNdMe5l cLuGxg1k2pkq3vDs0+08EfFx7O0r3//fFdNUPLut1YXT/mrrq7QDGgfp86zMQVi1iL mOUdAvyGMfUPQ== In-Reply-To: <20230226034150.771411-1-sashal-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org> List-ID: Content-Type: text/plain; charset="us-ascii" To: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org Cc: Li Nan , Yu Kuai , Tejun Heo , Jens Axboe , Sasha Levin , josef-DigfWCa+lFGyeJad7bwFQA@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-block-u79uwXL29TY76Z2rM5mHXA@public.gmane.org From: Li Nan [ Upstream commit 984af1e66b4126cf145153661cc24c213e2ec231 ] echo max of u64 to cost.model can cause divide by 0 error. # echo 8:0 rbps=18446744073709551615 > /sys/fs/cgroup/io.cost.model divide error: 0000 [#1] PREEMPT SMP RIP: 0010:calc_lcoefs+0x4c/0xc0 Call Trace: ioc_refresh_params+0x2b3/0x4f0 ioc_cost_model_write+0x3cb/0x4c0 ? _copy_from_iter+0x6d/0x6c0 ? kernfs_fop_write_iter+0xfc/0x270 cgroup_file_write+0xa0/0x200 kernfs_fop_write_iter+0x17d/0x270 vfs_write+0x414/0x620 ksys_write+0x73/0x160 __x64_sys_write+0x1e/0x30 do_syscall_64+0x35/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd calc_lcoefs() uses the input value of cost.model in DIV_ROUND_UP_ULL, overflow would happen if bps plus IOC_PAGE_SIZE is greater than ULLONG_MAX, it can cause divide by 0 error. Fix the problem by setting basecost Signed-off-by: Li Nan Signed-off-by: Yu Kuai Acked-by: Tejun Heo Link: https://lore.kernel.org/r/20230117070806.3857142-5-yukuai1-XF6JlduFytWkHkcT6e4Xnw@public.gmane.org Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-iocost.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 6955605629e4f..ec7219caea165 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -866,9 +866,14 @@ static void calc_lcoefs(u64 bps, u64 seqiops, u64 randiops, *page = *seqio = *randio = 0; - if (bps) - *page = DIV64_U64_ROUND_UP(VTIME_PER_SEC, - DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE)); + if (bps) { + u64 bps_pages = DIV_ROUND_UP_ULL(bps, IOC_PAGE_SIZE); + + if (bps_pages) + *page = DIV64_U64_ROUND_UP(VTIME_PER_SEC, bps_pages); + else + *page = 1; + } if (seqiops) { v = DIV64_U64_ROUND_UP(VTIME_PER_SEC, seqiops); -- 2.39.0