From patchwork Fri Nov 4 02:39:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 15288 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp120287wru; Thu, 3 Nov 2022 19:25:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM42eXiCzYBiQzliAP9eFRx09210I/QBmKqr+VOGSMRpC0H0PQKYnSd+ql/Gbl788I3L6xdP X-Received: by 2002:a17:906:9746:b0:798:baec:3a80 with SMTP id o6-20020a170906974600b00798baec3a80mr31612202ejy.610.1667528703149; Thu, 03 Nov 2022 19:25:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667528703; cv=none; d=google.com; s=arc-20160816; b=oxBPO2c9iTwDIGG/NE6HAW7qmot4i/7aTSlg0EogolSmKxoDtKoXgMun4ic7I/mTXy ycSOuhox/gZtoZgj5Ozp4bE/Ve5dPPThGTQPcZ0GzM5Xbl670vD25HsJsLhh8kCM6o1i OAgW2+NDuUpnY1QUyQGlDEAbLZ8Hy7ezTsojnKpB8ugSv0NfWwz4HIEca3jBFuICN+Uv m4msvly3Karc5+N4WfL/NKbjqjliMl2mmcnI+IdJytyqE8idRPS7dQNS+hQYV9VTQbNV vuNk+ytyld1qkz+ElK4lj08izILV+OWOqDlEh+erTPqWokr2mRQR8xc7Vt08c0oZq0Wd +O6g== 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=VUwxWgMIn9Y/ODdH8Wpo16K/fMETsGpP8YPMPmK+wrw=; b=Uz1j/dEsST2v9Tj8EF2pps8WfLEJZZihFtchLTAkAlnmcRK6QPCzm06BFu9JSRI5CA yiIa487tMts0rfp4VU5BGHvQxSjG8k7+s+HpwtS6yj7PMaLwP/ocEyOc8KmTcpiHdI1d qkv/u149GbhCZDYwNWbL434hqXT8+Ly0JfT1JfWKorPFPgCmaHol4r/LgEOfRvh2EFDJ D837bOF6pve9sLfkrFNgjhxy4q+QPCLKr83VZ3OJpLcNuR01kl0az/fWssHSDg30nPpf 00B8GsRuCQ6rIbYZta8S2BZY+NDMMClZOT5xqmYfPPiJlRWPzdo87dmGU4S30B78gb+w 4IDA== 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 dr14-20020a170907720e00b007a2d966eeccsi3658473ejc.686.2022.11.03.19.24.40; Thu, 03 Nov 2022 19:25:03 -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 S230456AbiKDCSJ (ORCPT + 99 others); Thu, 3 Nov 2022 22:18:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229611AbiKDCSB (ORCPT ); Thu, 3 Nov 2022 22:18:01 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEEC912B; Thu, 3 Nov 2022 19:18:00 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4N3PNy4WVRzlBwJ; Fri, 4 Nov 2022 10:15:46 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP2 (Coremail) with SMTP id Syh0CgB3D9RTdmRjDShNBQ--.16192S8; Fri, 04 Nov 2022 10:17:59 +0800 (CST) From: Yu Kuai To: hch@lst.de, 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 v2 4/5] blk-iocost: fix sleeping in atomic context warnning Date: Fri, 4 Nov 2022 10:39:37 +0800 Message-Id: <20221104023938.2346986-5-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20221104023938.2346986-1-yukuai1@huaweicloud.com> References: <20221104023938.2346986-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgB3D9RTdmRjDShNBQ--.16192S8 X-Coremail-Antispam: 1UD129KBjvJXoWxCF4fWw15XFy5Gw45uF17KFg_yoW7Jr4kpF WYg39xt3y8trsrXw1DWrsFvr4rC34kKrWxJ3s3JFn3AF9rKrn2qF1kCryj9F48tFZ3ZrZx tr4Fq395AF4UArJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPY14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r 4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUQSdkU UUUU= 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?1748530577297749562?= X-GMAIL-MSGID: =?utf-8?q?1748530577297749562?= From: Yu Kuai match_u64() is called inside ioc->lock, which causes smatch static checker warnings: block/blk-iocost.c:3211 ioc_qos_write() warn: sleeping in atomic context block/blk-iocost.c:3240 ioc_qos_write() warn: sleeping in atomic context block/blk-iocost.c:3407 ioc_cost_model_write() warn: sleeping in atomic context Fix the problem by introducing a mutex and using it while prasing input params. Fixes: 2c0647988433 ("blk-iocost: don't release 'ioc->lock' while updating params") Reported-by: Dan Carpenter Signed-off-by: Yu Kuai Reviewed-by: Christoph Hellwig Reviewed-by: Christoph Hellwig --- block/blk-iocost.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 2bfecc511dd9..192ad4e0cfc6 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -404,6 +404,7 @@ struct ioc { bool enabled; + struct mutex params_mutex; struct ioc_params params; struct ioc_margins margins; u32 period_us; @@ -2212,6 +2213,8 @@ static void ioc_timer_fn(struct timer_list *timer) /* how were the latencies during the period? */ ioc_lat_stat(ioc, missed_ppm, &rq_wait_pct); + mutex_lock(&ioc->params_mutex); + /* take care of active iocgs */ spin_lock_irq(&ioc->lock); @@ -2222,6 +2225,7 @@ static void ioc_timer_fn(struct timer_list *timer) period_vtime = now.vnow - ioc->period_at_vtime; if (WARN_ON_ONCE(!period_vtime)) { spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); return; } @@ -2419,6 +2423,7 @@ static void ioc_timer_fn(struct timer_list *timer) } spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); } static u64 adjust_inuse_and_calc_cost(struct ioc_gq *iocg, u64 vtime, @@ -2801,9 +2806,11 @@ static void ioc_rqos_queue_depth_changed(struct rq_qos *rqos) { struct ioc *ioc = rqos_to_ioc(rqos); + mutex_lock(&ioc->params_mutex); spin_lock_irq(&ioc->lock); ioc_refresh_params(ioc, false); spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); } static void ioc_rqos_exit(struct rq_qos *rqos) @@ -2862,6 +2869,7 @@ static int blk_iocost_init(struct gendisk *disk) rqos->ops = &ioc_rqos_ops; rqos->q = q; + mutex_init(&ioc->params_mutex); spin_lock_init(&ioc->lock); timer_setup(&ioc->timer, ioc_timer_fn, 0); INIT_LIST_HEAD(&ioc->active_iocgs); @@ -2874,10 +2882,12 @@ static int blk_iocost_init(struct gendisk *disk) atomic64_set(&ioc->cur_period, 0); atomic_set(&ioc->hweight_gen, 0); + mutex_lock(&ioc->params_mutex); spin_lock_irq(&ioc->lock); ioc->autop_idx = AUTOP_INVALID; ioc_refresh_params(ioc, true); spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); /* * rqos must be added before activation to allow iocg_pd_init() to @@ -3197,7 +3207,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input, blk_mq_freeze_queue(disk->queue); blk_mq_quiesce_queue(disk->queue); - spin_lock_irq(&ioc->lock); + mutex_lock(&ioc->params_mutex); memcpy(qos, ioc->params.qos, sizeof(qos)); enable = ioc->enabled; user = ioc->user_qos_params; @@ -3278,6 +3288,7 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input, if (qos[QOS_MIN] > qos[QOS_MAX]) goto out_unlock; + spin_lock_irq(&ioc->lock); if (enable) { blk_stat_enable_accounting(disk->queue); blk_queue_flag_set(QUEUE_FLAG_RQ_ALLOC_TIME, disk->queue); @@ -3298,9 +3309,10 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input, ioc_refresh_params(ioc, true); ret = nbytes; + spin_unlock_irq(&ioc->lock); out_unlock: - spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); blk_mq_unquiesce_queue(disk->queue); blk_mq_unfreeze_queue(disk->queue); @@ -3385,7 +3397,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, blk_mq_freeze_queue(q); blk_mq_quiesce_queue(q); - spin_lock_irq(&ioc->lock); + mutex_lock(&ioc->params_mutex); memcpy(u, ioc->params.i_lcoefs, sizeof(u)); user = ioc->user_cost_model; @@ -3431,6 +3443,7 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, user = true; } + spin_lock_irq(&ioc->lock); if (user) { memcpy(ioc->params.i_lcoefs, u, sizeof(u)); ioc->user_cost_model = true; @@ -3440,9 +3453,10 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input, ioc_refresh_params(ioc, true); ret = nbytes; + spin_unlock_irq(&ioc->lock); out_unlock: - spin_unlock_irq(&ioc->lock); + mutex_unlock(&ioc->params_mutex); blk_mq_unquiesce_queue(q); blk_mq_unfreeze_queue(q);