From patchwork Wed Jan 4 08:53:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 13088275 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65305C4332F for ; Wed, 4 Jan 2023 08:29:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233926AbjADI32 (ORCPT ); Wed, 4 Jan 2023 03:29:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234036AbjADI3Y (ORCPT ); Wed, 4 Jan 2023 03:29:24 -0500 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0CEB1869B; Wed, 4 Jan 2023 00:29:23 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Nn2np03H3z4f3mT1; Wed, 4 Jan 2023 16:29:18 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP2 (Coremail) with SMTP id Syh0CgBnW+ndOLVjwys+BA--.58806S5; Wed, 04 Jan 2023 16:29:20 +0800 (CST) From: Yu Kuai To: tj@kernel.org, hch@infradead.org, josef@toxicpanda.com, axboe@kernel.dk Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH -next 1/4] block/rq_qos: move implementions of init/exit rq-qos apis to blk-rq-qos.c Date: Wed, 4 Jan 2023 16:53:51 +0800 Message-Id: <20230104085354.2343590-2-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230104085354.2343590-1-yukuai1@huaweicloud.com> References: <20230104085354.2343590-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgBnW+ndOLVjwys+BA--.58806S5 X-Coremail-Antispam: 1UD129KBjvJXoWxZryDKF48ArW3CrWxZw1fZwb_yoWrtFy5pa yfK3W3A3yvgrsrW3s8Gw4xX39IkwnYgr47JrWfJFWfAr1v9r1YvF1vyFyUWrWFqrZ7Ar45 Ar45KrZ5Cr1UAwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBE14x267AKxVW5JVWrJwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JU2_M3UUUUU = X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai These init/exit rq-qos apis are super cold path, there is no need to inline them to improve performance. This patch also prepare to use a global mutex to protect these apis, move these implementions to blk-rq-qos.c so that the global mutex won't be exposed. There are no functional changes. Signed-off-by: Yu Kuai Acked-by: Tejun Heo --- block/blk-rq-qos.c | 59 +++++++++++++++++++++++++++++++++++++++++ block/blk-rq-qos.h | 65 +++------------------------------------------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c index 88f0fe7dcf54..b6ea40775b2a 100644 --- a/block/blk-rq-qos.c +++ b/block/blk-rq-qos.c @@ -286,6 +286,65 @@ void rq_qos_wait(struct rq_wait *rqw, void *private_data, finish_wait(&rqw->wait, &data.wq); } +int rq_qos_add(struct request_queue *q, struct rq_qos *rqos) +{ + /* + * No IO can be in-flight when adding rqos, so freeze queue, which + * is fine since we only support rq_qos for blk-mq queue. + * + * Reuse ->queue_lock for protecting against other concurrent + * rq_qos adding/deleting + */ + blk_mq_freeze_queue(q); + + spin_lock_irq(&q->queue_lock); + if (rq_qos_id(q, rqos->id)) + goto ebusy; + rqos->next = q->rq_qos; + q->rq_qos = rqos; + spin_unlock_irq(&q->queue_lock); + + blk_mq_unfreeze_queue(q); + + if (rqos->ops->debugfs_attrs) { + mutex_lock(&q->debugfs_mutex); + blk_mq_debugfs_register_rqos(rqos); + mutex_unlock(&q->debugfs_mutex); + } + + return 0; +ebusy: + spin_unlock_irq(&q->queue_lock); + blk_mq_unfreeze_queue(q); + return -EBUSY; +} + +void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) +{ + struct rq_qos **cur; + + /* + * See comment in rq_qos_add() about freezing queue & using + * ->queue_lock. + */ + blk_mq_freeze_queue(q); + + spin_lock_irq(&q->queue_lock); + for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) { + if (*cur == rqos) { + *cur = rqos->next; + break; + } + } + spin_unlock_irq(&q->queue_lock); + + blk_mq_unfreeze_queue(q); + + mutex_lock(&q->debugfs_mutex); + blk_mq_debugfs_unregister_rqos(rqos); + mutex_unlock(&q->debugfs_mutex); +} + void rq_qos_exit(struct request_queue *q) { while (q->rq_qos) { diff --git a/block/blk-rq-qos.h b/block/blk-rq-qos.h index 1ef1f7d4bc3c..f2d95e19d7a8 100644 --- a/block/blk-rq-qos.h +++ b/block/blk-rq-qos.h @@ -85,69 +85,12 @@ static inline void rq_wait_init(struct rq_wait *rq_wait) init_waitqueue_head(&rq_wait->wait); } -static inline int rq_qos_add(struct request_queue *q, struct rq_qos *rqos) -{ - /* - * No IO can be in-flight when adding rqos, so freeze queue, which - * is fine since we only support rq_qos for blk-mq queue. - * - * Reuse ->queue_lock for protecting against other concurrent - * rq_qos adding/deleting - */ - blk_mq_freeze_queue(q); - - spin_lock_irq(&q->queue_lock); - if (rq_qos_id(q, rqos->id)) - goto ebusy; - rqos->next = q->rq_qos; - q->rq_qos = rqos; - spin_unlock_irq(&q->queue_lock); - - blk_mq_unfreeze_queue(q); - - if (rqos->ops->debugfs_attrs) { - mutex_lock(&q->debugfs_mutex); - blk_mq_debugfs_register_rqos(rqos); - mutex_unlock(&q->debugfs_mutex); - } - - return 0; -ebusy: - spin_unlock_irq(&q->queue_lock); - blk_mq_unfreeze_queue(q); - return -EBUSY; - -} - -static inline void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) -{ - struct rq_qos **cur; - - /* - * See comment in rq_qos_add() about freezing queue & using - * ->queue_lock. - */ - blk_mq_freeze_queue(q); - - spin_lock_irq(&q->queue_lock); - for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) { - if (*cur == rqos) { - *cur = rqos->next; - break; - } - } - spin_unlock_irq(&q->queue_lock); - - blk_mq_unfreeze_queue(q); - - mutex_lock(&q->debugfs_mutex); - blk_mq_debugfs_unregister_rqos(rqos); - mutex_unlock(&q->debugfs_mutex); -} - typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data); typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data); +int rq_qos_add(struct request_queue *q, struct rq_qos *rqos); +void rq_qos_del(struct request_queue *q, struct rq_qos *rqos); +void rq_qos_exit(struct request_queue *q); void rq_qos_wait(struct rq_wait *rqw, void *private_data, acquire_inflight_cb_t *acquire_inflight_cb, cleanup_cb_t *cleanup_cb); @@ -230,6 +173,4 @@ static inline void rq_qos_queue_depth_changed(struct request_queue *q) __rq_qos_queue_depth_changed(q->rq_qos); } -void rq_qos_exit(struct request_queue *); - #endif From patchwork Wed Jan 4 08:53:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 13088277 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14072C4332F for ; Wed, 4 Jan 2023 08:29:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233947AbjADI33 (ORCPT ); Wed, 4 Jan 2023 03:29:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234040AbjADI3Z (ORCPT ); Wed, 4 Jan 2023 03:29:25 -0500 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FB8A186D9; Wed, 4 Jan 2023 00:29:24 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Nn2np3WZwz4f3nFG; Wed, 4 Jan 2023 16:29:18 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP2 (Coremail) with SMTP id Syh0CgBnW+ndOLVjwys+BA--.58806S6; Wed, 04 Jan 2023 16:29:21 +0800 (CST) From: Yu Kuai To: tj@kernel.org, hch@infradead.org, josef@toxicpanda.com, axboe@kernel.dk Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH -next 2/4] block/rq_qos: factor out a helper to add rq_qos and activate policy Date: Wed, 4 Jan 2023 16:53:52 +0800 Message-Id: <20230104085354.2343590-3-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230104085354.2343590-1-yukuai1@huaweicloud.com> References: <20230104085354.2343590-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgBnW+ndOLVjwys+BA--.58806S6 X-Coremail-Antispam: 1UD129KBjvJXoWxWr4rCF1xtrykXr48XF1rCrg_yoW5Kw47pa yfKrnIyrWjgr4I9a1xGw4rJr98uw48Kry5Gay8AryfArW29w1Iy3W0yF1DKa4fZrsxArs5 ZF4Yqry8GFy5G3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUHbyAUUUUU = X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai For the policy that use both rq_qos and blkcg_policy, rq_qos_add() and blkcg_activate_policy() should be atomic, otherwise null-ptr-deference can be triggered. This patch prepare to use a global mutex to protect them, there are no functional changes. Signed-off-by: Yu Kuai --- block/blk-iocost.c | 14 +------------- block/blk-iolatency.c | 7 +------ block/blk-rq-qos.c | 23 +++++++++++++++++++++++ block/blk-rq-qos.h | 6 ++++++ 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/block/blk-iocost.c b/block/blk-iocost.c index 6955605629e4..9199124f0cc2 100644 --- a/block/blk-iocost.c +++ b/block/blk-iocost.c @@ -2883,23 +2883,11 @@ static int blk_iocost_init(struct gendisk *disk) ioc_refresh_params(ioc, true); spin_unlock_irq(&ioc->lock); - /* - * rqos must be added before activation to allow ioc_pd_init() to - * lookup the ioc from q. This means that the rqos methods may get - * called before policy activation completion, can't assume that the - * target bio has an iocg associated and need to test for NULL iocg. - */ - ret = rq_qos_add(q, rqos); + ret = rq_qos_add_and_activate_policy(q, rqos, &blkcg_policy_iocost); if (ret) goto err_free_ioc; - - ret = blkcg_activate_policy(q, &blkcg_policy_iocost); - if (ret) - goto err_del_qos; return 0; -err_del_qos: - rq_qos_del(q, rqos); err_free_ioc: free_percpu(ioc->pcpu_stat); kfree(ioc); diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c index ecdc10741836..a29b923e2a6a 100644 --- a/block/blk-iolatency.c +++ b/block/blk-iolatency.c @@ -771,20 +771,15 @@ int blk_iolatency_init(struct gendisk *disk) rqos->ops = &blkcg_iolatency_ops; rqos->q = q; - ret = rq_qos_add(q, rqos); + ret = rq_qos_add_and_activate_policy(q, rqos, &blkcg_policy_iolatency); if (ret) goto err_free; - ret = blkcg_activate_policy(q, &blkcg_policy_iolatency); - if (ret) - goto err_qos_del; timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0); INIT_WORK(&blkiolat->enable_work, blkiolatency_enable_work_fn); return 0; -err_qos_del: - rq_qos_del(q, rqos); err_free: kfree(blkiolat); return ret; diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c index b6ea40775b2a..50544bfb12f1 100644 --- a/block/blk-rq-qos.c +++ b/block/blk-rq-qos.c @@ -353,3 +353,26 @@ void rq_qos_exit(struct request_queue *q) rqos->ops->exit(rqos); } } + +#ifdef CONFIG_BLK_CGROUP +int rq_qos_add_and_activate_policy(struct request_queue *q, struct rq_qos *rqos, + const struct blkcg_policy *pol) +{ + /* + * rqos must be added before activation to allow pd_init_fn() to + * lookup the global structure from q. This means that the rqos methods + * may get called before policy activation completion, can't assume that + * the target bio has an pd associated and need to test for NULL. + */ + int ret = rq_qos_add(q, rqos); + + if (ret) + return ret; + + ret = blkcg_activate_policy(q, pol); + if (ret) + rq_qos_del(q, rqos); + + return ret; +} +#endif diff --git a/block/blk-rq-qos.h b/block/blk-rq-qos.h index f2d95e19d7a8..0778cff3777c 100644 --- a/block/blk-rq-qos.h +++ b/block/blk-rq-qos.h @@ -173,4 +173,10 @@ static inline void rq_qos_queue_depth_changed(struct request_queue *q) __rq_qos_queue_depth_changed(q->rq_qos); } +#ifdef CONFIG_BLK_CGROUP +#include "blk-cgroup.h" +int rq_qos_add_and_activate_policy(struct request_queue *q, struct rq_qos *rqos, + const struct blkcg_policy *pol); +#endif + #endif From patchwork Wed Jan 4 08:53:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 13088279 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6581FC4332F for ; Wed, 4 Jan 2023 08:29:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233329AbjADI3d (ORCPT ); Wed, 4 Jan 2023 03:29:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234041AbjADI3Z (ORCPT ); Wed, 4 Jan 2023 03:29:25 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE96E192BA; Wed, 4 Jan 2023 00:29:24 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Nn2nq0Qy6z4f42p3; Wed, 4 Jan 2023 16:29:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP2 (Coremail) with SMTP id Syh0CgBnW+ndOLVjwys+BA--.58806S7; Wed, 04 Jan 2023 16:29:21 +0800 (CST) From: Yu Kuai To: tj@kernel.org, hch@infradead.org, josef@toxicpanda.com, axboe@kernel.dk Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH -next 3/4] block/rq_qos: use a global mutex to protect rq_qos apis Date: Wed, 4 Jan 2023 16:53:53 +0800 Message-Id: <20230104085354.2343590-4-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230104085354.2343590-1-yukuai1@huaweicloud.com> References: <20230104085354.2343590-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgBnW+ndOLVjwys+BA--.58806S7 X-Coremail-Antispam: 1UD129KBjvJXoWxAryftw1DGrW7ZFy8Cw1Utrb_yoWrGr4Dpa 1fKrWay3yqgrs7Was8Gw48ZasxKw1rKryUJF4xJrWfArWvvF1jvF10yFyxWFWrZF9rJrs3 Ary5trs5Ar1UXwUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBE14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v2 6r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2 Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_ Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMI IF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUd8n5UUUUU = X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai This patch fix following problems: 1) rq_qos_add() and rq_qos_del() is protected, while rq_qos_exit() is not. 2) rq_qos_add() and blkcg_activate_policy() is not atomic, if rq_qos_exit() is done before blkcg_activate_policy(), null-ptr-deference can be triggered. rq_qos_add(), rq_qos_del() and rq_qos_exit() are super cold path, hence fix the problems by using a global mutex to protect them. Signed-off-by: Yu Kuai --- block/blk-rq-qos.c | 50 ++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c index 50544bfb12f1..5f7ccc249c11 100644 --- a/block/blk-rq-qos.c +++ b/block/blk-rq-qos.c @@ -2,6 +2,8 @@ #include "blk-rq-qos.h" +static DEFINE_MUTEX(rq_qos_lock); + /* * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded, * false if 'v' + 1 would be bigger than 'below'. @@ -286,23 +288,18 @@ void rq_qos_wait(struct rq_wait *rqw, void *private_data, finish_wait(&rqw->wait, &data.wq); } -int rq_qos_add(struct request_queue *q, struct rq_qos *rqos) +static int __rq_qos_add(struct request_queue *q, struct rq_qos *rqos) { /* * No IO can be in-flight when adding rqos, so freeze queue, which * is fine since we only support rq_qos for blk-mq queue. - * - * Reuse ->queue_lock for protecting against other concurrent - * rq_qos adding/deleting */ blk_mq_freeze_queue(q); - spin_lock_irq(&q->queue_lock); if (rq_qos_id(q, rqos->id)) goto ebusy; rqos->next = q->rq_qos; q->rq_qos = rqos; - spin_unlock_irq(&q->queue_lock); blk_mq_unfreeze_queue(q); @@ -314,29 +311,23 @@ int rq_qos_add(struct request_queue *q, struct rq_qos *rqos) return 0; ebusy: - spin_unlock_irq(&q->queue_lock); blk_mq_unfreeze_queue(q); return -EBUSY; } -void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) +static void __rq_qos_del(struct request_queue *q, struct rq_qos *rqos) { struct rq_qos **cur; - /* - * See comment in rq_qos_add() about freezing queue & using - * ->queue_lock. - */ + /* See comment in __rq_qos_add() about freezing queue */ blk_mq_freeze_queue(q); - spin_lock_irq(&q->queue_lock); for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) { if (*cur == rqos) { *cur = rqos->next; break; } } - spin_unlock_irq(&q->queue_lock); blk_mq_unfreeze_queue(q); @@ -345,13 +336,33 @@ void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) mutex_unlock(&q->debugfs_mutex); } +int rq_qos_add(struct request_queue *q, struct rq_qos *rqos) +{ + int ret; + + mutex_lock(&rq_qos_lock); + ret = __rq_qos_add(q, rqos); + mutex_unlock(&rq_qos_lock); + + return ret; +} + +void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) +{ + mutex_lock(&rq_qos_lock); + __rq_qos_del(q, rqos); + mutex_unlock(&rq_qos_lock); +} + void rq_qos_exit(struct request_queue *q) { + mutex_lock(&rq_qos_lock); while (q->rq_qos) { struct rq_qos *rqos = q->rq_qos; q->rq_qos = rqos->next; rqos->ops->exit(rqos); } + mutex_unlock(&rq_qos_lock); } #ifdef CONFIG_BLK_CGROUP @@ -364,15 +375,20 @@ int rq_qos_add_and_activate_policy(struct request_queue *q, struct rq_qos *rqos, * may get called before policy activation completion, can't assume that * the target bio has an pd associated and need to test for NULL. */ - int ret = rq_qos_add(q, rqos); + int ret; - if (ret) + mutex_lock(&rq_qos_lock); + ret = __rq_qos_add(q, rqos); + if (ret) { + mutex_unlock(&rq_qos_lock); return ret; + } ret = blkcg_activate_policy(q, pol); if (ret) - rq_qos_del(q, rqos); + __rq_qos_del(q, rqos); + mutex_unlock(&rq_qos_lock); return ret; } #endif From patchwork Wed Jan 4 08:53:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Kuai X-Patchwork-Id: 13088278 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9D85C53210 for ; Wed, 4 Jan 2023 08:29:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231201AbjADI3b (ORCPT ); Wed, 4 Jan 2023 03:29:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234042AbjADI3Z (ORCPT ); Wed, 4 Jan 2023 03:29:25 -0500 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEA44193CF; Wed, 4 Jan 2023 00:29:24 -0800 (PST) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Nn2nq4J7sz4f3wYs; Wed, 4 Jan 2023 16:29:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP2 (Coremail) with SMTP id Syh0CgBnW+ndOLVjwys+BA--.58806S8; Wed, 04 Jan 2023 16:29:22 +0800 (CST) From: Yu Kuai To: tj@kernel.org, hch@infradead.org, josef@toxicpanda.com, axboe@kernel.dk Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: [PATCH -next 4/4] block/rq_qos: fail rq_qos_add() after rq_qos_exit() Date: Wed, 4 Jan 2023 16:53:54 +0800 Message-Id: <20230104085354.2343590-5-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230104085354.2343590-1-yukuai1@huaweicloud.com> References: <20230104085354.2343590-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: Syh0CgBnW+ndOLVjwys+BA--.58806S8 X-Coremail-Antispam: 1UD129KBjvJXoW7CF15XrWkCrWxZr4xCF4kZwb_yoW8ArW8pa yfKr1ay3yqgrs7W3WfGw48X39xA3yrKr47AFn7X3yfArWUCr1j9F1vyFWUtayfAFnrJF4f trn5Kr15Cr15J3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPF14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjfUOBTY UUUUU X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Yu Kuai rq_qos_add() can still succeed after rq_qos_exit() is done, which will cause memory leak because such rq_qos will never be removed. t1 t2 // configure iocost blk_iocost_init //remove device del_gendisk rq_qos_exit // done nothing because rq_qos doesn't exist rq_qos_add // will succeed, and rq_qos won't be removed Fix the problem by setting q->rq_qos to a special value in rq_qos_exit(), and check the value in rq_qos_add(). Signed-off-by: Yu Kuai --- block/blk-rq-qos.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/block/blk-rq-qos.c b/block/blk-rq-qos.c index 5f7ccc249c11..cfd8024ff6e8 100644 --- a/block/blk-rq-qos.c +++ b/block/blk-rq-qos.c @@ -290,6 +290,10 @@ void rq_qos_wait(struct rq_wait *rqw, void *private_data, static int __rq_qos_add(struct request_queue *q, struct rq_qos *rqos) { + /* See details in rq_qos_exit() for this specail value. */ + if (IS_ERR(q->rq_qos)) + return PTR_ERR(q->rq_qos); + /* * No IO can be in-flight when adding rqos, so freeze queue, which * is fine since we only support rq_qos for blk-mq queue. @@ -356,12 +360,22 @@ void rq_qos_del(struct request_queue *q, struct rq_qos *rqos) void rq_qos_exit(struct request_queue *q) { + struct rq_qos *rqos; + mutex_lock(&rq_qos_lock); - while (q->rq_qos) { - struct rq_qos *rqos = q->rq_qos; - q->rq_qos = rqos->next; + rqos = q->rq_qos; + + /* + * Set q->rq_qos to a special value to make sure rq_qos_add() will fail + * after rq_qos_exit(). + */ + q->rq_qos = ERR_PTR(-ENODEV); + + while (rqos) { rqos->ops->exit(rqos); + rqos = rqos->next; } + mutex_unlock(&rq_qos_lock); }