Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp2068396pxv; Sat, 17 Jul 2021 03:11:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyd8p+ld/KQfstnEpKrEO90MYgACIlSQgz52bk22cfTbj+T6iN0cL1fV5CzFSStUA37gTJ0 X-Received: by 2002:a05:6638:3a1:: with SMTP id z1mr12519655jap.77.1626516717010; Sat, 17 Jul 2021 03:11:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626516717; cv=none; d=google.com; s=arc-20160816; b=o1Mx8iVaVdBwZFnw5YPUSNLXjgHyof9W+kE2ZlNs9yHELgYHRq+eYKi71JqozNvhYL r0LU/QucYfhV2UGMaPYV4qg2l9hFwewgAXXFtgXHOoR7zwkUQjr/c+13MnLeAEb/AHMM 39MRTaLZDdYYzPfN9MIewRUkXwl05dLeBFSMellsDTKfYAcYSa+9KOYRwB1VgQ7gkwSy w6RJkYwfbJ+k9X7TP2gaRyML3b+eVCCWPoRLURz1/kIRMwKK/YENjBOYMhMOk6UekAjT 2nf4JwmGgTfuH1urbP/mkn3QVVBDYPYE7lpTtT7qiAp35Bi0OyrqP5ZsSxCX5X2MJWue LEkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=83GbSGpZBfJHGXq15Yg2ngQ7/7/5XwVRHY0b6SDhljI=; b=k7EsyMZGOICqRF4d+a6TOmqkVQ+IMPgG0BLsZVGJU3JxjIPBathTXZXgQidpjgLJ/b YoDs31vVHPPpq7xPIWVEBbuJIYjZKIKLEWy3GhT9GmMl84p30denIhZoNwX0X4PhIHVy i3Q361bcTk8j3JR48paRFvWRKBFFsKFnCE90BWPDdzFMl/cm5WyGmJE30Q7wN39bMDY+ 99as5MOIZ+V0L3OfGzRYmLLR33EK3S4FXfJJN8bNzLfUDPfa9aVBKkIz+x8DjBSmY6Bg +9JfHO4dIN5ZTTJ5lm4v+1mHO0VtHOtpK/HdQbzDcC3YFjLP6Atu8FJUFYLz2o4TP0Q1 44zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="uyXB+/16"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t18si675602ilf.96.2021.07.17.03.11.44; Sat, 17 Jul 2021 03:11:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="uyXB+/16"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233065AbhGQKM1 (ORCPT + 99 others); Sat, 17 Jul 2021 06:12:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232993AbhGQKM0 (ORCPT ); Sat, 17 Jul 2021 06:12:26 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25FDCC06175F for ; Sat, 17 Jul 2021 03:09:29 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id g4-20020a17090ace84b029017554809f35so10288460pju.5 for ; Sat, 17 Jul 2021 03:09:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=83GbSGpZBfJHGXq15Yg2ngQ7/7/5XwVRHY0b6SDhljI=; b=uyXB+/16nvqrz8OL63K/aowqr/Mq1BwOMmDr72vNEAEScOUTTnm6SSGvF+7rT2Dgcc KAaxegtG0c2E5MqZqFgrQi7mXbG5gTKdIXV1ewE+kqwHm3m/31T6RjUeyzLSEfrgQGe5 oU2E1Ke4aSeZ7P9uNVPxWhRRkcmgVn5AOjrOhRcbVyzcHmjyuipoRoWPK2E2yeYBVvJ/ etVdo8RG4HSWZRau145JybWep6I7xLI1pw+NdAK3m+Wtt6J7Rp4CkKv9NC05XatnC9Xx h9x+XMC3KoTFBdcQQJmkO8uX/FzxE6AUsCeCST/DmMtSIFch2SlP5QpbALz0/ExX+yo7 feOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=83GbSGpZBfJHGXq15Yg2ngQ7/7/5XwVRHY0b6SDhljI=; b=P8wp2PComu6OS25xiKdFpTzYH92QVs+STO47Q4n8+mZuVYtFzEJc7mTvqrFH0xqdSH ibn0knTHxoEPMbO6UmjQDWqUOrkjkqvcwxKCk2xMpVd/8M1EsdCVSy5O/rRRZBKWXV0e lMUm9KmIBdsI2ACw6+nmAylQKUeYLoXVUVQApFK92kge+Lpb2P88rpdR6cJYZPrbhUK1 BZCT+gdhXnANv7NEgLNbfSxV6xu6rh5p/AHHnHMKesF7zGYyUWKTjxB5L3hz9AT7XCVw 94wVo0CiOU6Hqqp9goFVKxL45i003Nn70jnZZ22g48FJCACaGtcXkJTbQOLSBoH6UUnk 2RIw== X-Gm-Message-State: AOAM531tfIdfGVnTMN+gck/PXO9PGCPduQ6U/n166v3htS46glAqS9YE L9J2ziYlkbU9E7RxK0rBU1VF7lejrgCZqFKdT3A= X-Received: by 2002:a17:902:9a8c:b029:113:d891:2eaf with SMTP id w12-20020a1709029a8cb0290113d8912eafmr11286452plp.61.1626516568517; Sat, 17 Jul 2021 03:09:28 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id p24sm13487926pfh.17.2021.07.17.03.09.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jul 2021 03:09:27 -0700 (PDT) From: Desmond Cheong Zhi Xi To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com Cc: Desmond Cheong Zhi Xi , linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, Hillf Danton Subject: [PATCH] mtd: fix lock hierarchy in deregister_mtd_blktrans Date: Sat, 17 Jul 2021 18:07:19 +0800 Message-Id: <20210717100719.728829-1-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There is a lock hierarchy of major_names_lock --> mtd_table_mutex. One existing chain is as follows: 1. major_names_lock --> loop_ctl_mutex (when blk_request_module calls loop_probe) 2. loop_ctl_mutex --> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk) 3. bdev->bd_mutex --> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open) Since unregister_blkdev grabs the major_names_lock, we need to call it outside the critical section for mtd_table_mutex, otherwise we invert the lock hierarchy. Reported-by: Hillf Danton Signed-off-by: Desmond Cheong Zhi Xi --- drivers/mtd/mtd_blkdevs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c index 7d26cfe24d05..87e26788ef60 100644 --- a/drivers/mtd/mtd_blkdevs.c +++ b/drivers/mtd/mtd_blkdevs.c @@ -563,8 +563,8 @@ int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr) list_for_each_entry_safe(dev, next, &tr->devs, list) tr->remove_dev(dev); - unregister_blkdev(tr->major, tr->name); mutex_unlock(&mtd_table_mutex); + unregister_blkdev(tr->major, tr->name); BUG_ON(!list_empty(&tr->devs)); return 0; -- 2.25.1