From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B77D414A82 for ; Tue, 25 Jul 2023 10:52:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 397E8C433C8; Tue, 25 Jul 2023 10:52:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1690282332; bh=BKTqJouS8yPK9eFZoLcOwHKr19TEjAMyG0eagwvqU4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UrhrSXny9tRD8And1a8SS7VxbdrJImlClS8IQBQ2Z7TWSCWvfZm1lSBCjm8CuQZjn Cvah9nCjwxJgyTCmWHXHOS7lp3lE5gT7lFmdo2HDdfGlsWjiWUvWLjTd5aKjY/HHlV qWJdOjFHiIQhydK2GaDQQ0dWVPHktEu2gc3fpu5E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Guangwu Zhang , Ming Lei , Jens Axboe , Sasha Levin Subject: [PATCH 6.4 091/227] blk-mq: fix NULL dereference on q->elevator in blk_mq_elv_switch_none Date: Tue, 25 Jul 2023 12:44:18 +0200 Message-ID: <20230725104518.490601273@linuxfoundation.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725104514.821564989@linuxfoundation.org> References: <20230725104514.821564989@linuxfoundation.org> User-Agent: quilt/0.67 Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Ming Lei [ Upstream commit 245165658e1c9f95c0fecfe02b9b1ebd30a1198a ] After grabbing q->sysfs_lock, q->elevator may become NULL because of elevator switch. Fix the NULL dereference on q->elevator by checking it with lock. Reported-by: Guangwu Zhang Signed-off-by: Ming Lei Link: https://lore.kernel.org/r/20230616132354.415109-1-ming.lei@redhat.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/blk-mq.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index b9f4546139894..73ed8ccb09ce8 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -4617,9 +4617,6 @@ static bool blk_mq_elv_switch_none(struct list_head *head, { struct blk_mq_qe_pair *qe; - if (!q->elevator) - return true; - qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY); if (!qe) return false; @@ -4627,6 +4624,12 @@ static bool blk_mq_elv_switch_none(struct list_head *head, /* q->elevator needs protection from ->sysfs_lock */ mutex_lock(&q->sysfs_lock); + /* the check has to be done with holding sysfs_lock */ + if (!q->elevator) { + kfree(qe); + goto unlock; + } + INIT_LIST_HEAD(&qe->node); qe->q = q; qe->type = q->elevator->type; @@ -4634,6 +4637,7 @@ static bool blk_mq_elv_switch_none(struct list_head *head, __elevator_get(qe->type); list_add(&qe->node, head); elevator_disable(q); +unlock: mutex_unlock(&q->sysfs_lock); return true; -- 2.39.2