Received: by 10.192.165.156 with SMTP id m28csp69478imm; Tue, 10 Apr 2018 16:37:15 -0700 (PDT) X-Google-Smtp-Source: AIpwx48WFZUTFMpgqlZEph9xOAjtT/XEf92u9fs9Wm+2SHgYAT5MpwRR0IkB4hxA9halOKfx1Jmk X-Received: by 10.99.124.16 with SMTP id x16mr1688367pgc.201.1523403435033; Tue, 10 Apr 2018 16:37:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523403434; cv=none; d=google.com; s=arc-20160816; b=WH6XIT9b+99X9xIGJHg1+XGgV8sKzmfgeLMrEp3CBqS07uV9Kx1I40yl/W0YKHjLbi lnghe5J2sT3sfnFExsNNk9qouEPGAF7Yg5bVu9idWnTBMNvH1YikMgbpGLzFflEI1Dfo +HwZ6driSvOboQqvE6BD26oOod9FcBz4dXxcUYn0LZdC2yVY1ZCcjwTXpPDlmWJ9UrLq xRJxOWEmFHTiU+D7Uw+KVViZ+yEGo2a1SpomE6VgZwqtB2Ly+i8iHHbFVuE8ySbvX4c5 t9aSJ25hdR+TJFd8GMtnOKOcGBC4+WwyID2coD+EdvNrjRSYFNhjL64JucQMscvJDAGG JRuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=c6Ai8sqZcw1fkd4pZdFWy1GTjE+Vu/ZI9AJaTpDoEHY=; b=oJZb2ryiWWkPyFfQSwbA73ADlPwLafMmFzvZeMS+BlE5cdsaEUba5GeVzlGM6CzMtp Ap6Mf/L+dDDKFLAQCKbjVolsFrfDsDfO1uLdP+s2zyVWonQ1dC25iBy86Kxnq9tmnX40 WkF2Y0JD8Lc8y6HfJ5t9yozG7VRoGSzF2NBf+mSNLfUP4s+zVxoJ42TFdmGPUEGKDwDZ 6Cnu9evpvMo/VVR56zYF/5W+d84FApDKRJSzAnxnjDNv1wAPQqqcNPOsjvzHMpgzHWKg 3TCAJOsS3TPtj14TngazDLhDA87Mc8+x3yKQGqcFjEmXkenbEmDvP0jcx+YfjaXYfM7D RuLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p7si2475134pgq.8.2018.04.10.16.36.38; Tue, 10 Apr 2018 16:37:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754077AbeDJW2e (ORCPT + 99 others); Tue, 10 Apr 2018 18:28:34 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:39126 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752847AbeDJW2b (ORCPT ); Tue, 10 Apr 2018 18:28:31 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 3867FD16; Tue, 10 Apr 2018 22:28:30 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christoph Hellwig , Yi Zhang , Ming Lei , Jens Axboe , Sasha Levin Subject: [PATCH 4.15 053/168] blk-mq: fix race between updating nr_hw_queues and switching io sched Date: Wed, 11 Apr 2018 00:23:15 +0200 Message-Id: <20180410212802.461896555@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212800.144079021@linuxfoundation.org> References: <20180410212800.144079021@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.15-stable review patch. If anyone has any objections, please let me know. ------------------ 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 Signed-off-by: Greg Kroah-Hartman --- block/blk-mq.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2388,6 +2388,9 @@ static void blk_mq_realloc_hw_ctxs(struc struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx; blk_mq_sysfs_unregister(q); + + /* protect against switching io scheduler */ + mutex_lock(&q->sysfs_lock); for (i = 0; i < set->nr_hw_queues; i++) { int node; @@ -2432,6 +2435,7 @@ static void blk_mq_realloc_hw_ctxs(struc } } q->nr_hw_queues = i; + mutex_unlock(&q->sysfs_lock); blk_mq_sysfs_register(q); }