From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 229B7C49361 for ; Thu, 17 Jun 2021 16:13:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0A4C9613A9 for ; Thu, 17 Jun 2021 16:13:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232155AbhFQQP0 (ORCPT ); Thu, 17 Jun 2021 12:15:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233655AbhFQQNi (ORCPT ); Thu, 17 Jun 2021 12:13:38 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5953AC061574 for ; Thu, 17 Jun 2021 09:11:27 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id v13so3184197ple.9 for ; Thu, 17 Jun 2021 09:11:27 -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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=i2QXD0vhjVadbjwCmRdyNJqmCJfF+D+qzE+5On//JOZFpoHUgMErQ2ApsO0fvPgBrh WB0J16l5X7WlSWdjxiYJPOKw5I3EWB990vCX8td+8KS8DcuNK64Fufah0VfpTZB974ut F1zqdkpTJXa/XRMpt2nkaZ6DrDW5y2qblxKoNcQWrPpVx2TaFULd4vJd09aBovLSir80 KqgIBeu9fB9/8aGrWyTYmiOjQn6oUAudcN17lDi9OXG2KyeqX9MkERC3sLhTEHeZRt+J p/JpjX4ALsoqTASC5bZvSmcETVpAm4voc7uT7gegQ6EJ+yvf6+F/vYtqbL7OfIl8UxtX DTrA== 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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=j2c7mht6bWJ3dK8PZ7Ue2Ro6Ml2SWZDiyeZ+3UI9qU+o+Kce9/R6+6h7gO9Suf7Ghv PN4IkOC4QFjinF0bnQNwNoWs3xQS3fq45XNUNWD3DVtYVFxLHotXisZi1GF9GLemEdGH oGhkPjGUXX25pvvRYrdWJoE7GeCJZIs3SZHq4V0QwUC4CxD1+G8V8+wD18DHBOxTRKZv hddOlu/P5t0SPmapG9BUxEI2YPFAXHtEnWrCfxek579ZAQ69cyUlekrOg7kKnoxPq8xL epbgZJHWYh1/fYoCfJ3GnmqQzKDhrvLmIx8dnKknZ3m1q7NoDTuSJsknHxV8tBBc7KwM 9F7A== X-Gm-Message-State: AOAM532BxkG/t38d8ey2DAjr+xg4tjJ/FbL7UjLyKpaE8Kn7J1S0H84H 551UvTfsM1TzWIBRLJff24Q= X-Google-Smtp-Source: ABdhPJxA7oUNbFsGjhbNIo746b9a5E8ZEjnY31bzeD1lZjEFk7hoeAfcyTpUfGXFvil2uefPLd8A/A== X-Received: by 2002:a17:902:be10:b029:109:7460:cc41 with SMTP id r16-20020a170902be10b02901097460cc41mr612469pls.4.1623946286013; Thu, 17 Jun 2021 09:11:26 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id 4sm8090293pjg.30.2021.06.17.09.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 09:11:25 -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, syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com, Christoph Hellwig Subject: [PATCH v2] mtd: break circular locks in register_mtd_blktrans Date: Fri, 18 Jun 2021 00:09:04 +0800 Message-Id: <20210617160904.570111-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 Syzbot reported a circular locking dependency: https://syzkaller.appspot.com/bug?id=7bd106c28e846d1023d4ca915718b1a0905444cb This happens because of the following lock dependencies: 1. loop_ctl_mutex -> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk; this also happens in loop_exit which eventually calls loop_remove) 2. bdev->bd_mutex -> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open) 3. mtd_table_mutex -> major_names_lock (when register_mtd_blktrans calls __register_blkdev) 4. major_names_lock -> loop_ctl_mutex (when blk_request_module calls loop_probe) Hence there's an overall dependency of: loop_ctl_mutex ----------> bdev->bd_mutex ^ | | | | v major_names_lock <--------- mtd_table_mutex We can break this circular dependency by holding mtd_table_mutex only for the required critical section in register_mtd_blktrans. This avoids the mtd_table_mutex -> major_names_lock dependency. Reported-and-tested-by: syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com Co-developed-by: Christoph Hellwig Signed-off-by: Christoph Hellwig Signed-off-by: Desmond Cheong Zhi Xi --- Changes in v1 -> v2: Break the circular dependency in register_mtd_blktrans instead of blk_request_module, as suggested by Christoph Hellwig. drivers/mtd/mtd_blkdevs.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c index fb8e12d590a1..7d26cfe24d05 100644 --- a/drivers/mtd/mtd_blkdevs.c +++ b/drivers/mtd/mtd_blkdevs.c @@ -528,14 +528,10 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) if (!blktrans_notifier.list.next) register_mtd_user(&blktrans_notifier); - - mutex_lock(&mtd_table_mutex); - ret = register_blkdev(tr->major, tr->name); if (ret < 0) { printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", tr->name, tr->major, ret); - mutex_unlock(&mtd_table_mutex); return ret; } @@ -545,12 +541,12 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) tr->blkshift = ffs(tr->blksize) - 1; INIT_LIST_HEAD(&tr->devs); - list_add(&tr->list, &blktrans_majors); + mutex_lock(&mtd_table_mutex); + list_add(&tr->list, &blktrans_majors); mtd_for_each_device(mtd) if (mtd->type != MTD_ABSENT) tr->add_mtd(tr, mtd); - mutex_unlock(&mtd_table_mutex); return 0; } -- 2.25.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E328FC2B9F4 for ; Thu, 17 Jun 2021 16:12:18 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B188A613A9 for ; Thu, 17 Jun 2021 16:12:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B188A613A9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=ERD6wNoLprpekXIN5QpIZUeaTbo18CR1pEr+8rXfh8I=; b=eXz17aKWTXOGiA jnm3U4twKdPukMVPGYKwWzbcMadLfLUHPMu5y8/qV6HDXTqA7Gne/Zidy7YHeNFWudvTyvRX9SdGL PWmledXbi8pKqHdJ5ACdOkmC6BWf9i1wgk4Yz+2ghfTOE3F83x2cUrLJBFcQqFekpMGUnwM2DoJjt TusQT0yZWSlGs+fsZ1Tg1ULwW9PyrmbOssf+kXIM2JDuEtQO7DxwuqbYnQnExjlW2nqLSXGGFlx9Q GOA11V5/KJpvkIhaUU5WxalNPuy8lANyrRqcuWo/sx+VYDQ8YwsZO1bjShl8lyR8mNmO3WD1K/7Nj zvEHnd9fwmSVj6Nd01cA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ltuc5-00B3nN-QR; Thu, 17 Jun 2021 16:11:29 +0000 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ltuc2-00B3mZ-RA for linux-mtd@lists.infradead.org; Thu, 17 Jun 2021 16:11:28 +0000 Received: by mail-pl1-x62c.google.com with SMTP id c15so3109912pls.13 for ; Thu, 17 Jun 2021 09:11:26 -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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=i2QXD0vhjVadbjwCmRdyNJqmCJfF+D+qzE+5On//JOZFpoHUgMErQ2ApsO0fvPgBrh WB0J16l5X7WlSWdjxiYJPOKw5I3EWB990vCX8td+8KS8DcuNK64Fufah0VfpTZB974ut F1zqdkpTJXa/XRMpt2nkaZ6DrDW5y2qblxKoNcQWrPpVx2TaFULd4vJd09aBovLSir80 KqgIBeu9fB9/8aGrWyTYmiOjQn6oUAudcN17lDi9OXG2KyeqX9MkERC3sLhTEHeZRt+J p/JpjX4ALsoqTASC5bZvSmcETVpAm4voc7uT7gegQ6EJ+yvf6+F/vYtqbL7OfIl8UxtX DTrA== 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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=Z9u2sdoZ8MrQg/zlVJ7TOCNjSotuu5qUBjpMvDNLgqJOJ2+cuI1me7NUidoybFfWjD Nk58ZzY05e61YtE7OgibMIwUrEm1e+FCdD24hnqDfb0QNfFj/QVHIZC3l+dvRTLXF8GW ASCTgfyL/sEevH1ob9dMaAf3KiA/RcyYDqwKCvQGmtgcfdTSwoeIJKb+guDGcyG8Rb4h J84eJP2wYWquAy5vrbQDody2oDr3tzKNYpsr5/CEZqtEuJNXvmgpg7ZM/B53Y1c/yOIp 6i4d+vyqmXElTizIjATc3cpnjSKfKG3adc/fdGpRoVktOLHVanBkoR4ZzKJ01lAI6aas SA3w== X-Gm-Message-State: AOAM532WPkai3ppmS/943+SZuDpuAaebfh40jG3tAMyOClJXJhHfgQjy g/g13Cd7YpUEvSHwblLoNRc= X-Google-Smtp-Source: ABdhPJxA7oUNbFsGjhbNIo746b9a5E8ZEjnY31bzeD1lZjEFk7hoeAfcyTpUfGXFvil2uefPLd8A/A== X-Received: by 2002:a17:902:be10:b029:109:7460:cc41 with SMTP id r16-20020a170902be10b02901097460cc41mr612469pls.4.1623946286013; Thu, 17 Jun 2021 09:11:26 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id 4sm8090293pjg.30.2021.06.17.09.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 09:11:25 -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, syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com, Christoph Hellwig Subject: [PATCH v2] mtd: break circular locks in register_mtd_blktrans Date: Fri, 18 Jun 2021 00:09:04 +0800 Message-Id: <20210617160904.570111-1-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210617_091126_933792_F27EE3FB X-CRM114-Status: GOOD ( 11.27 ) X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-mtd" Errors-To: linux-mtd-bounces+linux-mtd=archiver.kernel.org@lists.infradead.org Syzbot reported a circular locking dependency: https://syzkaller.appspot.com/bug?id=7bd106c28e846d1023d4ca915718b1a0905444cb This happens because of the following lock dependencies: 1. loop_ctl_mutex -> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk; this also happens in loop_exit which eventually calls loop_remove) 2. bdev->bd_mutex -> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open) 3. mtd_table_mutex -> major_names_lock (when register_mtd_blktrans calls __register_blkdev) 4. major_names_lock -> loop_ctl_mutex (when blk_request_module calls loop_probe) Hence there's an overall dependency of: loop_ctl_mutex ----------> bdev->bd_mutex ^ | | | | v major_names_lock <--------- mtd_table_mutex We can break this circular dependency by holding mtd_table_mutex only for the required critical section in register_mtd_blktrans. This avoids the mtd_table_mutex -> major_names_lock dependency. Reported-and-tested-by: syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com Co-developed-by: Christoph Hellwig Signed-off-by: Christoph Hellwig Signed-off-by: Desmond Cheong Zhi Xi --- Changes in v1 -> v2: Break the circular dependency in register_mtd_blktrans instead of blk_request_module, as suggested by Christoph Hellwig. drivers/mtd/mtd_blkdevs.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c index fb8e12d590a1..7d26cfe24d05 100644 --- a/drivers/mtd/mtd_blkdevs.c +++ b/drivers/mtd/mtd_blkdevs.c @@ -528,14 +528,10 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) if (!blktrans_notifier.list.next) register_mtd_user(&blktrans_notifier); - - mutex_lock(&mtd_table_mutex); - ret = register_blkdev(tr->major, tr->name); if (ret < 0) { printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", tr->name, tr->major, ret); - mutex_unlock(&mtd_table_mutex); return ret; } @@ -545,12 +541,12 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) tr->blkshift = ffs(tr->blksize) - 1; INIT_LIST_HEAD(&tr->devs); - list_add(&tr->list, &blktrans_majors); + mutex_lock(&mtd_table_mutex); + list_add(&tr->list, &blktrans_majors); mtd_for_each_device(mtd) if (mtd->type != MTD_ABSENT) tr->add_mtd(tr, mtd); - mutex_unlock(&mtd_table_mutex); return 0; } -- 2.25.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/ From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_RED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44A0AC2B9F4 for ; Thu, 17 Jun 2021 16:11:30 +0000 (UTC) Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A3CAF61428 for ; Thu, 17 Jun 2021 16:11:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A3CAF61428 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=linux-kernel-mentees-bounces@lists.linuxfoundation.org Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 65C4440149; Thu, 17 Jun 2021 16:11:29 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bc8rJLbilvAT; Thu, 17 Jun 2021 16:11:28 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 7195840447; Thu, 17 Jun 2021 16:11:28 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 395A2C000D; Thu, 17 Jun 2021 16:11:28 +0000 (UTC) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 92661C000B for ; Thu, 17 Jun 2021 16:11:27 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 6CA8E83E26 for ; Thu, 17 Jun 2021 16:11:27 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp1.osuosl.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IGJruxC2rBsL for ; Thu, 17 Jun 2021 16:11:26 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by smtp1.osuosl.org (Postfix) with ESMTPS id 9E11283E0B for ; Thu, 17 Jun 2021 16:11:26 +0000 (UTC) Received: by mail-pl1-x633.google.com with SMTP id e1so3181815plh.8 for ; Thu, 17 Jun 2021 09:11:26 -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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=i2QXD0vhjVadbjwCmRdyNJqmCJfF+D+qzE+5On//JOZFpoHUgMErQ2ApsO0fvPgBrh WB0J16l5X7WlSWdjxiYJPOKw5I3EWB990vCX8td+8KS8DcuNK64Fufah0VfpTZB974ut F1zqdkpTJXa/XRMpt2nkaZ6DrDW5y2qblxKoNcQWrPpVx2TaFULd4vJd09aBovLSir80 KqgIBeu9fB9/8aGrWyTYmiOjQn6oUAudcN17lDi9OXG2KyeqX9MkERC3sLhTEHeZRt+J p/JpjX4ALsoqTASC5bZvSmcETVpAm4voc7uT7gegQ6EJ+yvf6+F/vYtqbL7OfIl8UxtX DTrA== 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=Ax6cETdinXWixOBfa2JXuuJreyz/2EakPhTszPVNovE=; b=pdz57NonXK+k4c7VINRJ311jctEQ1Vupdzmn1SbRZWxWG6im0MPe0Dtt+GZ29ABAMC AyZjY7g8ROGLMG7IeCltPkHQDqKPKWkpVlw8sRZBGwdlOedmuw53d2vakiDw7ObwsdqO xU+jYAuhOZ7GcL1sjZxzOw79Y/cEed3NdkSigF1iTFpUCz9Qu7nTAiNpuwC4nG2NRfnR 4pvxJtGtaej1P4c0GZrfj3mOfQn2lhl2hkCfhUbFi2UukHPO1powdIQhY9FOaz1fI2P7 RXbrGoV0XEpMFRoNG4PYneoJw5IEleC1WTnOqEyz2dZpisylP+VSSZNLawKQ43CIuk0Z lDSA== X-Gm-Message-State: AOAM531bhKL8d1OlgjoGp5cwxpMM6cPZyxB0H++jqS4ZwPEUJbUkjeMi wrWPanqiNhbWEZQs3GpCHMk= X-Google-Smtp-Source: ABdhPJxA7oUNbFsGjhbNIo746b9a5E8ZEjnY31bzeD1lZjEFk7hoeAfcyTpUfGXFvil2uefPLd8A/A== X-Received: by 2002:a17:902:be10:b029:109:7460:cc41 with SMTP id r16-20020a170902be10b02901097460cc41mr612469pls.4.1623946286013; Thu, 17 Jun 2021 09:11:26 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id 4sm8090293pjg.30.2021.06.17.09.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 09:11:25 -0700 (PDT) From: Desmond Cheong Zhi Xi To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com Subject: [PATCH v2] mtd: break circular locks in register_mtd_blktrans Date: Fri, 18 Jun 2021 00:09:04 +0800 Message-Id: <20210617160904.570111-1-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Cc: linux-kernel@vger.kernel.org, syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com, linux-mtd@lists.infradead.org, Desmond Cheong Zhi Xi , linux-kernel-mentees@lists.linuxfoundation.org, Christoph Hellwig X-BeenThere: linux-kernel-mentees@lists.linuxfoundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-kernel-mentees-bounces@lists.linuxfoundation.org Sender: "Linux-kernel-mentees" Syzbot reported a circular locking dependency: https://syzkaller.appspot.com/bug?id=7bd106c28e846d1023d4ca915718b1a0905444cb This happens because of the following lock dependencies: 1. loop_ctl_mutex -> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk; this also happens in loop_exit which eventually calls loop_remove) 2. bdev->bd_mutex -> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open) 3. mtd_table_mutex -> major_names_lock (when register_mtd_blktrans calls __register_blkdev) 4. major_names_lock -> loop_ctl_mutex (when blk_request_module calls loop_probe) Hence there's an overall dependency of: loop_ctl_mutex ----------> bdev->bd_mutex ^ | | | | v major_names_lock <--------- mtd_table_mutex We can break this circular dependency by holding mtd_table_mutex only for the required critical section in register_mtd_blktrans. This avoids the mtd_table_mutex -> major_names_lock dependency. Reported-and-tested-by: syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com Co-developed-by: Christoph Hellwig Signed-off-by: Christoph Hellwig Signed-off-by: Desmond Cheong Zhi Xi --- Changes in v1 -> v2: Break the circular dependency in register_mtd_blktrans instead of blk_request_module, as suggested by Christoph Hellwig. drivers/mtd/mtd_blkdevs.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c index fb8e12d590a1..7d26cfe24d05 100644 --- a/drivers/mtd/mtd_blkdevs.c +++ b/drivers/mtd/mtd_blkdevs.c @@ -528,14 +528,10 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) if (!blktrans_notifier.list.next) register_mtd_user(&blktrans_notifier); - - mutex_lock(&mtd_table_mutex); - ret = register_blkdev(tr->major, tr->name); if (ret < 0) { printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", tr->name, tr->major, ret); - mutex_unlock(&mtd_table_mutex); return ret; } @@ -545,12 +541,12 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr) tr->blkshift = ffs(tr->blksize) - 1; INIT_LIST_HEAD(&tr->devs); - list_add(&tr->list, &blktrans_majors); + mutex_lock(&mtd_table_mutex); + list_add(&tr->list, &blktrans_majors); mtd_for_each_device(mtd) if (mtd->type != MTD_ABSENT) tr->add_mtd(tr, mtd); - mutex_unlock(&mtd_table_mutex); return 0; } -- 2.25.1 _______________________________________________ Linux-kernel-mentees mailing list Linux-kernel-mentees@lists.linuxfoundation.org https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees