Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp113596pxv; Wed, 30 Jun 2021 01:05:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWHldUhRgZgJWu/JteLjWDOhQJwe+Kb0kUgOu8cObgaR+gapSpLsVX0ndRa0h8FSsf/lpX X-Received: by 2002:a05:6402:1a34:: with SMTP id be20mr44583387edb.106.1625040339264; Wed, 30 Jun 2021 01:05:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625040339; cv=none; d=google.com; s=arc-20160816; b=RwnYsEFj/w6wkQ3nSzqsvF00WDQ+NRuEl2XeOgQo+W+hLY8z9GU/I7ehgXNv087QrB EoNOMB642+xHbRn8MenqGHEJ37kyMxY1DlUY/0KGKKkJKOchqWmuhfH9T+suuUSWjBpd K7Jch1lwPuTxKkYLxD3A5MIaPjDaPPhAB05qhBXjz3C4khoTqF/X5r8MbcU7DUvxlPLN yiVYRmU5FU3aszW0Otq2sjz33ZY0EZpVWhEq3u6+i/aT805BtfNw1yF2JRZPjkmq3i+q XMO/dmnSEf/dY+YaagnXRw3/oRWnVERJaIof4DHPV175P8J+4QIdBUFWc18LoAOW4Cfp se9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=hbH/z7OhdU+iVEdzRBnCUZFHkiZm6uSMAwD77kW53EQ=; b=Gt+1FueW92sQcPbFTfeUB0G7NOkWX54HC/UaC4VTxHUeLzQ3ilyGy1PCtxJyhiKHen v+hCJDC0v+FPEg924jfMibQwMiZn8LrSnbkkyjWe3Dt1EA7EVHdNvHj6mgr6Eg6IVSIz D9nUyadqgtRD0OtKv9nXrUiaWTtQi6Gl6U4eniBxckIRfZrORQ9EWKKGhiWPiJ+UdTOw 1LePB/RJ2tpg81tK5EiBubFXgS13ik+Kqe335klgeoe8xW2Xv+rA3C8IzOLtbiZwifxa Ei8677CwQJTcsCyupbiQe2XNsKoA2KmjZrnsNXXdMwttMGnsC7UFxMW9hnRnxebqZKK+ xcog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BU3Qv47L; 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 r17si19019792edv.508.2021.06.30.01.05.15; Wed, 30 Jun 2021 01:05:39 -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=BU3Qv47L; 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 S233230AbhF3IGw (ORCPT + 99 others); Wed, 30 Jun 2021 04:06:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233035AbhF3IGw (ORCPT ); Wed, 30 Jun 2021 04:06:52 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB2B9C061768 for ; Wed, 30 Jun 2021 01:04:22 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id s14so1744608pfg.0 for ; Wed, 30 Jun 2021 01:04:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=hbH/z7OhdU+iVEdzRBnCUZFHkiZm6uSMAwD77kW53EQ=; b=BU3Qv47LJ81s3KJ3CZ5YBfYg/zGxqG3yu7jvAU1zg+c4dyh4CHtk9+15weOuX+ekvR s5IR6Lc7yD9ogLG+sT0ClErracxfeatoIlo7LixKA+shMhWJ37eweKAx6IeZqmbem4E5 y0/zaKpJ8qYYp08lmdIJ/wyT7RkHksR1W5PA2DPZul9i5EhhrAe0cJuhKfICraRe/FFl 5eZp2XvnEy3z9zDnTZYWAFYWPMuhIANMlm7joFeZnNVruXlnxzWrS8W7hI7OiuWl6nMQ 8+4ACl127VruEaP/flWaidFrolQ412tebQW6LDHQOJjqGmhS2FEPcqholPo/2jllLZR/ E7fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=hbH/z7OhdU+iVEdzRBnCUZFHkiZm6uSMAwD77kW53EQ=; b=cgr4yOJzE/edXQ8SXuq/IZkzYTAXg2Q/UtkbGCLaTWvgx5tD7bxwQoFY658PG6InKI 0N3u6aQygAZK79DpPxunM9ktn6wAiQp++98lA1Pboi8RFL7a16MDoZVu55BTnuc7ZuLv hyViMyDilARJvb97mSDMhkiZM4P/tFseYGF95GZQ6gQAi1Th0UafwkoF/0/X/+SSqzrQ M0Iu/itB0L2KdrN87S1WGGGZJHRBxPpbMomMHgPqwD21XVYZTCKQbkzKcaF8KX0gCdpC lfHnmHlqou4f+DPj/81e06JTgLLeue5u50oJaHguBF7yfz0lAtlenomdYW3LDp6zq3NO OXng== X-Gm-Message-State: AOAM5324Zpf2o9or5lBdNKEr7PRlpZ+3dv2zF+6asIyyPqCfAY4LhUY5 PQv4qb1dmbxvMBe3FEBVC9s= X-Received: by 2002:aa7:9e0d:0:b029:30f:fb8b:2584 with SMTP id y13-20020aa79e0d0000b029030ffb8b2584mr1816966pfq.24.1625040262354; Wed, 30 Jun 2021 01:04:22 -0700 (PDT) Received: from [192.168.1.237] ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id z5sm20296398pfb.114.2021.06.30.01.04.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 30 Jun 2021 01:04:21 -0700 (PDT) Subject: Re: [PATCH v2] mtd: break circular locks in register_mtd_blktrans To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com Cc: 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 References: <20210617160904.570111-1-desmondcheongzx@gmail.com> From: Desmond Cheong Zhi Xi Message-ID: <03e19ec8-7479-9be2-3563-a2fcf9d0ec0c@gmail.com> Date: Wed, 30 Jun 2021 16:04:17 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210617160904.570111-1-desmondcheongzx@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18/6/21 12:09 am, Desmond Cheong Zhi Xi wrote: > 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; > } > Hi maintainers, Any chance to review this patch? For additional reference, the mtd_table_mutex --> major_names_lock hierarchy that can be removed by this patch also appears in a different lock chain: https://syzkaller.appspot.com/bug?id=cbf5fe846f14a90f05e10df200b08c57941dc750 Best wishes, Desmond