From patchwork Fri Oct 28 10:10:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 12198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp728093wru; Fri, 28 Oct 2022 02:53:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM71vepvT5Zy7tTIL3izbJycFLxDXLDGbslrURR7KUiUlovbJodb62/yvqwg4GgZ+3NXbAQR X-Received: by 2002:a17:906:8a63:b0:7ad:95cf:726a with SMTP id hy3-20020a1709068a6300b007ad95cf726amr4893400ejc.82.1666950796674; Fri, 28 Oct 2022 02:53:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666950796; cv=none; d=google.com; s=arc-20160816; b=E6w5ASdR9p+5132jW5t7jzP4B9O84lJ4Kmvw028C3gpN/sWOfPEpbB/WOOhOm4Olfp S5vtCBaUQsG2Hu6oA2IkTM3AbypmT5U3u9TkWHAy/qAcMCEpvLVUEndMZLyxI7TYmYVJ yamRq6lf91C5Q7EQW0R8vMkpMcd5/Fjalx6ttVSjs+rA1epQ7j9tE7U9vs1pOeVgZ1+8 u0oVkhSXiJ9Lfm1dhHw0vBwiPxeEJwOHLoX+yMoUe+QwRR5wysy1Bo+rGzcMM6cXtnbX 1VfGTxME5UBJR2eSkiNDQorZl8oCFvpgkTjqT4p1D6/YNxreDhGSyZw5r/jbOYkJWIKz nieQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=TbrGEiu+ggC75n/IDa8HnLWTtk9G2pjXV8Li9UjaESU=; b=nOkcARjCPBWExzfsX1khoKVLCa55Oq+179jrAkQlrVFDwU2nBJsBja8q+W7skbYa3w hY+sw3SLdKKadVMEGbxr2G1MY2E1r+woQIFQr42ZXGOvFlUb1HipBeC6KyBucMkEibOb xm3XagryL7Kg6FWtp0L+XPwKe4Ik05pfmRleqbY0KGOtOuoOYeAnoddEFcY0DGOtjS6n RqD8A+U0BykxHjIo6YckCDSiPxxwp7rwxlsSbp7eBth9bK/Q+HwWfka91qaqwRVf9+VV kUTEp7rvkZTAgt3TEvBPUfs3jyWdkWcOQmvPFw2KuOGeBKy/8PueVfrFGxkoO4ElNYw4 i7+g== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm4-20020a05640222c400b00459b0141076si170968edb.465.2022.10.28.02.52.53; Fri, 28 Oct 2022 02:53:16 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbiJ1Jtr (ORCPT + 99 others); Fri, 28 Oct 2022 05:49:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229868AbiJ1JtM (ORCPT ); Fri, 28 Oct 2022 05:49:12 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7D2B1C6BD7; Fri, 28 Oct 2022 02:49:09 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.153]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4MzHkR5J8Yz6T8sr; Fri, 28 Oct 2022 17:46:39 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgCntOaQpVtjHAtNAQ--.59885S9; Fri, 28 Oct 2022 17:49:08 +0800 (CST) From: Yu Kuai To: tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com Subject: [PATCH -next 5/5] blk-iocost: fix sleeping in atomic context warnning in ioc_cost_model_write() Date: Fri, 28 Oct 2022 18:10:56 +0800 Message-Id: <20221028101056.1971715-6-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221028101056.1971715-1-yukuai1@huaweicloud.com> References: <20221028101056.1971715-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgCntOaQpVtjHAtNAQ--.59885S9 X-Coremail-Antispam: 1UD129KBjvJXoWxCF4fWw18Zr4fCFWDZF1kZrb_yoWrZrW7pF Wfu3sxt3y0qrs3CFWIq3yIqF1fuwn7Ww47Gay3Grn3tr17Kr109F18uryjkFW8J3yrJFWY qF98tr4UWrW7CFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9K14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwCF04k20xvY0x0EwIxGrw CFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE 14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2 IY67AKxVWUCVW8JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAv wI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14 v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfUOBTYUUUUU X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747924598598454238?= X-GMAIL-MSGID: =?utf-8?q?1747924598598454238?= From: Yu Kuai match_u64() is called inside ioc->lock, which causes smatch static checker warnings: block/blk-iocost.c:3407 ioc_cost_model_write() warn: sleeping in atomic context Fix the problem by prase params before holding the lock. Fixes: 2c0647988433 ("blk-iocost: don't release 'ioc->lock' while updating params") Reported-by: Dan Carpenter Signed-off-by: Yu Kuai --- block/blk-iocost.c | 128 +++++++++++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 44 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 27b41f3f1b07..62e18c2719cb 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -3403,43 +3403,23 @@ static const match_table_t i_lcoef_tokens = { { NR_I_LCOEFS, NULL }, }; -static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, - size_t nbytes, loff_t off) -{ - struct block_device *bdev; - struct request_queue *q; - struct ioc *ioc; +struct ioc_model_params { u64 u[NR_I_LCOEFS]; bool user; - char *p; - int ret; - - bdev = blkcg_conf_open_bdev(&input); - if (IS_ERR(bdev)) - return PTR_ERR(bdev); - - q = bdev_get_queue(bdev); - if (!queue_is_mq(q)) { - ret = -EPERM; - goto out; - } - - ioc = q_to_ioc(q); - if (!ioc) { - ret = blk_iocost_init(bdev->bd_disk); - if (ret) - goto out; - ioc = q_to_ioc(q); - } + bool set_u[NR_I_LCOEFS]; + bool set_user; +}; - blk_mq_freeze_queue(q); - blk_mq_quiesce_queue(q); +static struct ioc_model_params *ioc_model_parse_params(char *input) +{ + struct ioc_model_params *params; + char *p; + int ret = -EINVAL; - spin_lock_irq(&ioc->lock); - memcpy(u, ioc->params.i_lcoefs, sizeof(u)); - user = ioc->user_cost_model; + params = kzalloc(sizeof(*params), GFP_KERNEL); + if (!params) + return ERR_PTR(-ENOMEM); - ret = -EINVAL; while ((p = strsep(&input, " \t\n"))) { substring_t args[MAX_OPT_ARGS]; char buf[32]; @@ -3454,48 +3434,108 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, case COST_CTRL: match_strlcpy(buf, &args[0], sizeof(buf)); if (!strcmp(buf, "auto")) - user = false; + params->user = false; else if (!strcmp(buf, "user")) - user = true; + params->user = true; else - goto out_unlock; + goto err; + params->set_user = true; continue; case COST_MODEL: match_strlcpy(buf, &args[0], sizeof(buf)); if (strcmp(buf, "linear")) - goto out_unlock; + goto err; continue; } tok = match_token(p, i_lcoef_tokens, args); if (tok == NR_I_LCOEFS) - goto out_unlock; + goto err; err = match_u64(&args[0], &v); if (err) { ret = err; - goto out_unlock; + goto err; } - u[tok] = v; - user = true; + params->u[tok] = v; + params->set_u[tok] = true; + params->user = true; + params->set_user = true; } - if (user) { - memcpy(ioc->params.i_lcoefs, u, sizeof(u)); + return params; + +err: + kfree(params); + return ERR_PTR(ret); +} + +static void ioc_model_update_params(struct ioc *ioc, + struct ioc_model_params *params) +{ + int i; + + if (!params->set_user) + params->user = ioc->user_cost_model; + if (params->user) { + for (i = 0; i < NR_I_LCOEFS; ++i) + if (params->set_u[i]) + ioc->params.i_lcoefs[i] = params->u[i]; ioc->user_cost_model = true; } else { ioc->user_cost_model = false; } +} + +static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, + size_t nbytes, loff_t off) +{ + struct block_device *bdev; + struct request_queue *q; + struct ioc *ioc; + struct ioc_model_params *params; + int ret; + + bdev = blkcg_conf_open_bdev(&input); + if (IS_ERR(bdev)) + return PTR_ERR(bdev); + + q = bdev_get_queue(bdev); + if (!queue_is_mq(q)) { + ret = -EPERM; + goto out; + } + ioc = q_to_ioc(q); + if (!ioc) { + ret = blk_iocost_init(bdev->bd_disk); + if (ret) + goto out; + ioc = q_to_ioc(q); + } + + params = ioc_model_parse_params(input); + if (IS_ERR(params)) { + ret = PTR_ERR(params); + goto out; + } + + blk_mq_freeze_queue(q); + blk_mq_quiesce_queue(q); + + spin_lock_irq(&ioc->lock); + + ioc_model_update_params(ioc, params); ioc_refresh_params(ioc, true); - ret = nbytes; -out_unlock: spin_unlock_irq(&ioc->lock); + blk_mq_unquiesce_queue(q); blk_mq_unfreeze_queue(q); + kfree(params); + ret = nbytes; out: blkdev_put_no_open(bdev); return ret;