Received: by 10.213.65.68 with SMTP id h4csp44015imn; Mon, 19 Mar 2018 19:00:20 -0700 (PDT) X-Google-Smtp-Source: AG47ELtnvDen4CaguqtCHDhxlucPjXpijkwoqw0BTScH/xIP1AC8e4ovPSA+Zuoo83SrReIjEVZU X-Received: by 10.98.35.68 with SMTP id j65mr11907415pfj.5.1521511219921; Mon, 19 Mar 2018 19:00:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521511219; cv=none; d=google.com; s=arc-20160816; b=syGtZCXHO/ADkgidbciLKDartQd/mPVhv/q/9BedIt23x1Xdjc7vAKWtOdry1HsGHg 71Lo5Z637a82iNchvDgFhz1U4CPX0Bs+mE8j1GcSbAQW/BzTLKSVsZG4jM6GddvZOamP 9lC1tP412vLs6HftmOH4dzfTB0uf7VNZX1ZPx2O5rBnyLA5J57fvgX5nLhhbLsNg/1pi ksCiAHMLCNmNjVK75CxcYmne0P1c3OIZn3LicHMZpwfe1CWL+PdRJE1tOm/Bw8KAewUn v3fI6O+vjH7JBdqDyWTwBJEXgnFSiYcAa78DS49FUh05+dWBPmgRpuLO+0Dktvg25Q89 rkdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=Xp+kSyHDx5b2K1knGBpW1WEyGWNU1SAwNaP2DWQVmyw=; b=ddCoQ7sRA9LC1jtflInaqlOeNB6AcZHW1Fkm20ln+EtID/uqHcxdv1evlUh4k7ZYR4 CEU2flQd5Ag30NMo1qBNtGFaJNL0emJNwjXFa/moXbzUrj0RBRfxZSETqI7i9i7Z34t6 rwiUH0X4piNiVrmG5SJe+HI0raDVx0Rgz329OdNDDMJow09ok5aezLD1kAUqhL8+w2lM PgPGn6UPbK0M05XCcsdNeLQzyhZCsVQyMb+Yk/07FWj85rBcUK/lhti/LcgNwVXNUDoZ MzcmAdr7Rg/8p936AaccuhShf8OVFGy2pDoPnXkbGYgqdJxSBbpMTfUTksd6mVdbtDUy PCsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=MjkAcogm; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o19si407302pgn.222.2018.03.19.19.00.06; Mon, 19 Mar 2018 19:00:19 -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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=MjkAcogm; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936583AbeCSW2z (ORCPT + 99 others); Mon, 19 Mar 2018 18:28:55 -0400 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=Xp+kSyHDx5b2K1knGBpW1WEyGWNU1SAwNaP2DWQVmyw=; b=MjkAcogmtpFdAb7YDsJdVHGkueCnSPMHT7WmIgj5WR56hQmF8ZW2yPMkIwX12nGd5PdaQBjM8um+ZTICCs6VDspLKEHiFk3v79Yksw39Op8ofWORxIehr8eJ8P4A+JAd6qOsIV+cEFLUULnUyJ0WtQBwuX8Y0oEl7izulvNW9KY= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1031.namprd21.prod.outlook.com (52.132.128.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.631.0; Mon, 19 Mar 2018 15:48:16 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62%5]) with mapi id 15.20.0631.004; Mon, 19 Mar 2018 15:48:16 +0000 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 Thread-Topic: [PATCH AUTOSEL for 4.15 057/124] blk-mq: fix race between updating nr_hw_queues and switching io sched Thread-Index: AQHTv5moz9vQWbV92UWRgZLzdJPfXQ== 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> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB1031;7:w/AjRAjXT+ZL/vQKgivHKAXRd5XMxP4f54Z75zV5HgCcx7QzE2ELXmbg99gscMbK6NqpGESeIsTNSo2FX6qIcdFbN5Y9agS82jkbx3zkDsPA6bln6r+7TuhtTB/Obi0Bgbap7ENcQ9h7w014iMoAwyZqgOHVOCATvcePvFSbcuLAFOcuzCeiL/4Lt8JIOgpIAindb+jXXAVWKVazC6EsnEThK3wV1rjEyWaYFlkMIFLqiEC5V1N9t+ALQ/BP8A19;20:tDogjdhl0U87k7TGm36akeeNGHX6u/V8oJXCKaUMgvlB/py+uI8H4y9/X89yKYIHWST8cQquh2MvQilT2tGyNkvnCKZKZUE2eGVhIlLBTjR9bawbRfT/4u1Bi05G9vgOqTjdr47faYiCQt/XnfVMzE/9HXT7nxayFQOYprralzw= x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 5f9fa77d-65b6-4bd0-94a5-08d58db0d4dd x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB1031; x-ms-traffictypediagnostic: DM5PR2101MB1031: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3231221)(944501300)(52105095)(3002001)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011);SRVR:DM5PR2101MB1031;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB1031; x-forefront-prvs: 06167FAD59 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(346002)(396003)(39860400002)(39380400002)(376002)(189003)(199004)(6436002)(6512007)(6666003)(3280700002)(2950100002)(7736002)(6116002)(3846002)(10090500001)(105586002)(4326008)(5660300001)(305945005)(1076002)(81156014)(2906002)(6486002)(81166006)(8676002)(8936002)(2900100001)(106356001)(36756003)(68736007)(54906003)(3660700001)(22452003)(97736004)(6506007)(25786009)(59450400001)(99286004)(10290500003)(76176011)(66066001)(14454004)(72206003)(478600001)(102836004)(26005)(186003)(316002)(2501003)(110136005)(5250100002)(575784001)(86362001)(53936002)(107886003)(86612001)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB1031;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: djHTSxyvo8XDb/37eyPjmCpC48gW5puzMyDUZEDWGdS7gcSk/We6mVnTOIDMlwZdBZiLbu7kljeXO82kuyn446t6Kljjx0qc1UbtvEhkV/4AUvsFcIBvopw+uYbxybXFm49+GRwWQIsRni6APhchlOACI8bi4spRtmYW64QevenNynUaFhoqfhDRJUFKYc1nGJ09yo78p8pXTrRaxTVD6l9bGM6VQxP1cg8hxXu7wVwzrOZDBjNonRiO5bV3PqCmhX6md+YBWh8Vopwk0GF5kFOwUcyRCDZYD7uRiVL8J6avgtvmKdwY2z0NBDcR1R+Xwb5JUirNB23UDm3Kk1k1QQ== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5f9fa77d-65b6-4bd0-94a5-08d58db0d4dd X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Mar 2018 15:47:59.9677 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1031 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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