Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2939588rwb; Wed, 30 Nov 2022 13:03:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf6G/PDRwMLQFEsMWDmSqP8A0RoO+UJr4RE52E4hsGaHAFIL3UkYrl6YKRkILuEKlZzXq4qt X-Received: by 2002:a17:906:a0d9:b0:78d:b912:6a6c with SMTP id bh25-20020a170906a0d900b0078db9126a6cmr54294374ejb.124.1669842189356; Wed, 30 Nov 2022 13:03:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669842189; cv=none; d=google.com; s=arc-20160816; b=p0NU1exfwzlaCR+XyoknTZNpA5mAMj4j+FsG7c5YDCPlMIYJ0r5yn0u3wAdLTTbD57 9P8ZHOdGbDiHgQcsfVmdEFA6WC9rJvjj12yA12n1b22eO+AoMEGqKA1gCw+5uXtsaR+t +bi1LaVcOITLBGA0geTUe0TQHfpKfVIDDi/qylWVzp7sUsYhAEuWdujRzXKo/Pts26NV RBKVBRC2FHNytCkJfGFKbRnjXWcdcDy6kIGKkc5Wbd5SFHCFqVrU1T8rMZyaQlUMX/BS jL0deAFxCm26IUCxk+9v5pP3UvU496/Ts0Y6fdbJ52GfZyv0tV7qkwKzBwefFUppeiYM pZzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:sender:dkim-signature; bh=hFED/2oaWzSQkBiwKASIUwgMFRRJ9B92EdLcPu53GAI=; b=nXiaBbt5i4Vf14sBPXKTj8otpGWNI2yuc3dD+XYRh4OTZFIbvrJm1paq6B2iuVk0UC lWe7WeMF8lYVe93eFAQmza5JGoeFOxhqNcocKMPDQEEA+IV2DBSVzXWTEgatXya1wYbV TTVcFQ+Q5+x1D4F5jmMVGNMCwoxOL+JiwQvSOez25HTmqqSpySSFkPZOqV4u84wirHFZ ZXDdJoTIlMmzEv64I5UU8RZHYOsimbiORp8DamvZk2dXhMgrjxjUG40Zo9/9e0iJahHz jIp/A37a3GmCMLvygqETPEoaYFikLiZSB13pZ3LVL633FzEvsb8vZoLR53JIKQBmLBLI lwSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="A/uWTQh2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r8-20020aa7d148000000b0046775f92f19si2088101edo.50.2022.11.30.13.02.49; Wed, 30 Nov 2022 13:03:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="A/uWTQh2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230128AbiK3UWn (ORCPT + 83 others); Wed, 30 Nov 2022 15:22:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229821AbiK3UWU (ORCPT ); Wed, 30 Nov 2022 15:22:20 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7EFD91363; Wed, 30 Nov 2022 12:20:17 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id x13-20020a17090a46cd00b00218f611b6e9so3451087pjg.1; Wed, 30 Nov 2022 12:20:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=hFED/2oaWzSQkBiwKASIUwgMFRRJ9B92EdLcPu53GAI=; b=A/uWTQh20hD7NILSrZcskkGI/NGv6GGEFIw80L8MRSrUAHpXVNqlkWFiSWqB0+UDLP PY/XPC6I3tQCv3X8dJRmADc7BoIeZ/tfq2XEwaSrA9MeI2qqF50nQHCv8BbB9+7fLOoG rotPGrDQAyRCRDD/OnAhKbvGWYPUQyLbg+Q3RpD0gFJkaPBAi0kDEaDvi8OGy3yaaWds 0PwlOMFUINg4txG221xgYjRvaiRs4TrssssmMtXpj5ajS6RJfNt7XSYkLSSoyFXTARXG 3Uv7CkXTXHgDOdQP3eISdvq1BKZnSCKQm13+QpiJC4iBrZ1W8veVE4d/v57d3SSfShRg o00w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hFED/2oaWzSQkBiwKASIUwgMFRRJ9B92EdLcPu53GAI=; b=hZQwC0K+Ak+KETE4XKiQ4Jx30PEpbSJiTh8WeX284lWmBCuIcjp3yYP7TYzmvDcdL6 ecInXKMY7xmjjALTcs8m6qa5dvatrFlxjt19w9oxjWisLIk6EhZGLQFyTu0GhsOFU6/V qf96Xp2Xd7rB25R7kmQ80dv1raFyErWYCt8ARIWA4VpAHvo/aMUgWahI6bRJkgFDX158 6xvmoVa1zViSaDP7y8iP9Joozdc/JfE96MVDHzh2YZNYaLx9jBEh9x2WndUh3X8MT1QU F4kFcKtsk4DYw8eAZs4+DxxG8Vg2zxAXkWIfY9xJFW9gBqUEFgj+JDo49mhS/ZNavo+Z jkIQ== X-Gm-Message-State: ANoB5pmDllQ1vXsAaKmEoncLdNFT3XwnRM9JVxAzrciHVt9en18j4tVn zWZP2wwzyMEWJ9n7jxycsN8= X-Received: by 2002:a17:90a:5d08:b0:218:770c:9a40 with SMTP id s8-20020a17090a5d0800b00218770c9a40mr49845287pji.158.1669839616969; Wed, 30 Nov 2022 12:20:16 -0800 (PST) Received: from localhost (2603-800c-1a02-1bae-a7fa-157f-969a-4cde.res6.spectrum.com. [2603:800c:1a02:1bae:a7fa:157f:969a:4cde]) by smtp.gmail.com with ESMTPSA id z14-20020a1709027e8e00b00176a2d23d1asm1914493pla.56.2022.11.30.12.20.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Nov 2022 12:20:16 -0800 (PST) Sender: Tejun Heo Date: Wed, 30 Nov 2022 10:20:15 -1000 From: Tejun Heo To: Li Nan Cc: josef@toxicpanda.com, axboe@kernel.dk, cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yi.zhang@huawei.com Subject: Re: [PATCH -next v2 5/9] blk-iocost: fix divide by 0 error in calc_lcoefs() Message-ID: References: <20221130132156.2836184-1-linan122@huawei.com> <20221130132156.2836184-6-linan122@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221130132156.2836184-6-linan122@huawei.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 30, 2022 at 09:21:52PM +0800, Li Nan wrote: > 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.I_LCOEF_MAX is introduced to > prevent it. > > Signed-off-by: Li Nan > --- > block/blk-iocost.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/block/blk-iocost.c b/block/blk-iocost.c > index 3a96cd557c47..f4a754b9d10f 100644 > --- a/block/blk-iocost.c > +++ b/block/blk-iocost.c > @@ -306,6 +306,9 @@ enum { > IOC_PAGE_SIZE = 1 << IOC_PAGE_SHIFT, > IOC_SECT_TO_PAGE_SHIFT = IOC_PAGE_SHIFT - SECTOR_SHIFT, > > + /* avoid overflow */ > + I_LCOEF_MAX = ULLONG_MAX - IOC_PAGE_SIZE, > + > /* if apart further than 16M, consider randio for linear model */ > LCOEF_RANDIO_PAGES = 4096, > }; > @@ -3431,6 +3434,8 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, > goto out_unlock; > } > > + if (v > I_LCOEF_MAX) > + goto out_unlock; This is kinda round-about. Can you just make calc_lcoefs to not divide by zero (e.g. just make it 1 if zero)? Thanks. -- tejun