Received: by 10.192.165.156 with SMTP id m28csp48391imm; Tue, 10 Apr 2018 16:07:41 -0700 (PDT) X-Google-Smtp-Source: AIpwx49QTJUZcaIrNUqRGR6KEmsGBasMOYjQCgn63/Xoy8v2HqrcKh7t6GG2jz9B1cC3LkqQ9eLx X-Received: by 10.99.119.195 with SMTP id s186mr1640786pgc.453.1523401661652; Tue, 10 Apr 2018 16:07:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523401661; cv=none; d=google.com; s=arc-20160816; b=uptDzB1G3mKFK7IRDkj81l2C1JCYIHZ1aVdlOza3DE7ccrm61PeXGuyZkdN7e4bOxH 2n89dWF/JVGoqZrUopte19xhmhzevQk7z+vrBOICMfTxvn0QjhE1baXMf3+xCu3tSqVa SY6J7cWv+W6y6saXQuHyf5RNgA+zkGwaXTpZhiCTsUMizkJvvBgvh07RIN+5OGBHHMfp +s63XAsERhTLva1dPsizpjeL0Owy3N+0VqGP7c+lqFb0/un666zxzJit4bPHfPvIcy4v +C7XGn5rma44Dt9nSX4uJEBull8qVaOLfcrHsJPtsCpmK5sOG3TJioFR5Ur+dZntjGbo 9Mrg== 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=ox8L8boABGwHDdN6tZij1wfkJ4FIIEdt0vZuYedmsYk=; b=INg2AozDYBtND9pr+7Scr23yhoIVhBQqEWSyWoTTqjYjXaVVrkZkL54V6BdB0GrkBi TeR1QBAGbNUal6KrwsxpVe5E1q1VSVIuerwKbI4VmzM5lXv64FQseVEjEMmjPg0dhsY4 Xwvok5Hr66ZnznmaRuTbzfguIEzE5TTVMeV9aP97AcST1AN6ucroRfLTkdhdy3GRI5vm uCp0zZ6xGWezSFmuKR9gBHSU99jzOtB/wtZz7cd0oDk2u/Lcm9Z0SBdxk2jvkYsdvP1n q/v0/kAXPwBk+EGRrdvGGTSz1vTWlvWGGdTLeRhaUP7YcOh7y46AC5gWCBMhRbz4bBnq 7/RQ== 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 z16si2504202pge.353.2018.04.10.16.07.03; Tue, 10 Apr 2018 16:07:41 -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 S1755811AbeDJXCE (ORCPT + 99 others); Tue, 10 Apr 2018 19:02:04 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:42088 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753443AbeDJWfV (ORCPT ); Tue, 10 Apr 2018 18:35:21 -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 5B8A7DDF; Tue, 10 Apr 2018 22:35:20 +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.14 040/138] blk-mq: fix race between updating nr_hw_queues and switching io sched Date: Wed, 11 Apr 2018 00:23:50 +0200 Message-Id: <20180410212906.727621952@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212902.121524696@linuxfoundation.org> References: <20180410212902.121524696@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.14-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 @@ -2314,6 +2314,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; @@ -2358,6 +2361,7 @@ static void blk_mq_realloc_hw_ctxs(struc } } q->nr_hw_queues = i; + mutex_unlock(&q->sysfs_lock); blk_mq_sysfs_register(q); }