From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-by2nam01on0135.outbound.protection.outlook.com ([104.47.34.135]:31072 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933768AbeCSPsq (ORCPT ); Mon, 19 Mar 2018 11:48:46 -0400 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Ming Lei , Jens Axboe , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 057/124] blk-mq: fix race between updating nr_hw_queues and switching io sched Date: Mon, 19 Mar 2018 15:47:59 +0000 Message-ID: <20180319154645.11350-57-alexander.levin@microsoft.com> References: <20180319154645.11350-1-alexander.levin@microsoft.com> In-Reply-To: <20180319154645.11350-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Ming Lei [ Upstream commit fb350e0ad99359768e1e80b4784692031ec340e4 ] In both elevator_switch_mq() and blk_mq_update_nr_hw_queues(), sched tags can be allocated, and q->nr_hw_queue is used, and race is inevitable, for example: blk_mq_init_sched() may trigger use-after-free on hctx, which is freed in blk_mq_realloc_hw_ctxs() when nr_hw_queues is decreased. This patch fixes the race be holding q->sysfs_lock. Reviewed-by: Christoph Hellwig Reported-by: Yi Zhang Tested-by: Yi Zhang Signed-off-by: Ming Lei Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-mq.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index 4385c5cbf57b..1372ad3a21d3 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2388,6 +2388,9 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_= set *set, struct blk_mq_hw_ctx **hctxs =3D q->queue_hw_ctx; =20 blk_mq_sysfs_unregister(q); + + /* protect against switching io scheduler */ + mutex_lock(&q->sysfs_lock); for (i =3D 0; i < set->nr_hw_queues; i++) { int node; =20 @@ -2432,6 +2435,7 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_= set *set, } } q->nr_hw_queues =3D i; + mutex_unlock(&q->sysfs_lock); blk_mq_sysfs_register(q); } =20 --=20 2.14.1