From patchwork Tue May 23 13:12:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Whitchurch X-Patchwork-Id: 1785139 X-Patchwork-Delegate: richard@nod.at Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=U10mJDCE; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=axis.com header.i=@axis.com header.a=rsa-sha256 header.s=axis-central1 header.b=Drw+sgoW; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QQZWL0r1Tz2020 for ; Tue, 23 May 2023 23:13:18 +1000 (AEST) 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:CC:To:In-Reply-To:References:Message-ID :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4RNN3e07M6LMA5XFUb+vNQuvdJr+enHG86V9YdsHAuU=; b=U10mJDCECO4jbw rlE/jh6I/Ra+JWz4xWTFSpq98I9YAFwz4lztQZZtFAaXDlRTQuNxHy68fPXyBRvQ8pajm1WcYDC8I e6Y6eJIpZyyfPLF3CjOY5VoGm/tMSjmyJ5CSDR1/jWjfl1Tlw34l5nCVFdhju8oZ3DlDNiRlvvp0S ipcxY69bqFWDR6C5rhz71gi9eoSFCYg/oX4YpWsmZB298jRAaO2GJVAyBoQG6rW0NOcveIUmIfMj+ 8DrTTOBfiLIbXKLLHcfjNYbaJfn/BLP77opZNmOHh9zs17YAlyo4ISAmVdKtU2Ry6y+pYv3WUhhlb MgaQrStiBlW5tXK93HtA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1RoY-00ALKQ-2m; Tue, 23 May 2023 13:12:34 +0000 Received: from smtp1.axis.com ([195.60.68.17]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q1RoW-00ALGG-11 for linux-mtd@lists.infradead.org; Tue, 23 May 2023 13:12:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1684847552; x=1716383552; h=from:date:subject:mime-version:content-transfer-encoding: message-id:references:in-reply-to:to:cc; bh=OfLmz9ckLWnjOPs/nZyBCIbifQUUmP+cRC2xReLIuEM=; b=Drw+sgoW3W/d1Yc/SHwqdzrScgMPrVUPUGWJtRZDjieGopdjYAM6yz9+ jgl+LJ4TiP230wecDiwwPofM1fdAn7fPasgS958JLHfaKgJKclEvJ1Z6N JmDiqkhPDg4VVUa0BfJueaFXNPNIuF+wwx/kyRy0YNwU4QOO90DW4wPle LAHFTU2ds/mZv1vqkFB7M7yKtk1E/dVonGNWWi/14GbQ9MzsVtrg58A7J BQVJJy0StQ6jPGTJ1b1q3zlgmwvTKnZrLEcwd+6WHI2k4Die0bzXWDgXK PyNzfxbhNCeJ+W59xKYklsjjCjQuQukNCpUreOwBWKkJhMimOKobfSIpQ g==; From: Vincent Whitchurch Date: Tue, 23 May 2023 15:12:17 +0200 Subject: [PATCH 2/2] ubi: block: Fix deadlock on remove MIME-Version: 1.0 Message-ID: <20230523-ubiblock-remove-v1-2-240bed75849b@axis.com> References: <20230523-ubiblock-remove-v1-0-240bed75849b@axis.com> In-Reply-To: <20230523-ubiblock-remove-v1-0-240bed75849b@axis.com> To: Richard Weinberger , Miquel Raynal , Vignesh Raghavendra CC: , , , Vincent Whitchurch X-Mailer: b4 0.12.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230523_061232_666515_31121732 X-CRM114-Status: GOOD ( 13.27 ) X-Spam-Score: -2.5 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Lockdep warns about possible circular locking when the following commands are run: ubiblock --create /dev/ubi0_0 head -c1 /dev/ubiblock0_0 > /dev/null ubiblock --remove /dev/ubi0_0 ====================================================== WARNING: possible circular locking dependency detected Content analysis details: (-2.5 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [195.60.68.17 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 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: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Lockdep warns about possible circular locking when the following commands are run: ubiblock --create /dev/ubi0_0 head -c1 /dev/ubiblock0_0 > /dev/null ubiblock --remove /dev/ubi0_0 ====================================================== WARNING: possible circular locking dependency detected ubiblock/364 is trying to acquire lock: (&disk->open_mutex){+.+.}-{3:3}, at: del_gendisk (block/genhd.c:616) but task is already holding lock: (&dev->dev_mutex){+.+.}-{3:3}, at: ubiblock_remove (drivers/mtd/ubi/block.c:476) which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (&dev->dev_mutex){+.+.}-{3:3}: ubiblock_open (drivers/mtd/ubi/block.c:236) blkdev_get_whole (block/bdev.c:607) blkdev_get_by_dev (block/bdev.c:756) blkdev_open (block/fops.c:493) ... do_sys_openat2 (fs/open.c:1356) -> #0 (&disk->open_mutex){+.+.}-{3:3}: del_gendisk (block/genhd.c:616) ubiblock_remove (drivers/mtd/ubi/block.c:456 drivers/mtd/ubi/block.c:483) vol_cdev_ioctl ... other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&dev->dev_mutex); lock(&disk->open_mutex); lock(&dev->dev_mutex); lock(&disk->open_mutex); *** DEADLOCK *** Call Trace: del_gendisk (block/genhd.c:616) ubiblock_remove (drivers/mtd/ubi/block.c:456 drivers/mtd/ubi/block.c:483) vol_cdev_ioctl ... The actual deadlock is also easily reproducible by running the above commands in parallel in a loop. Fix this by marking the device as going away and releasing the dev mutex before del_gendisk(). This is similar to other drivers such as drivers/block/zram/zram_drv.c. Signed-off-by: Vincent Whitchurch --- drivers/mtd/ubi/block.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 70caec4606cd..fcfea7cfdb6b 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -83,6 +83,8 @@ struct ubiblock { struct mutex dev_mutex; struct list_head list; struct blk_mq_tag_set tag_set; + + bool removing; }; /* Linked list of all ubiblock instances */ @@ -233,6 +235,11 @@ static int ubiblock_open(struct block_device *bdev, fmode_t mode) int ret; mutex_lock(&dev->dev_mutex); + if (dev->removing) { + ret = -ENODEV; + goto out_unlock; + } + if (dev->refcnt > 0) { /* * The volume is already open, just increase the reference @@ -480,8 +487,15 @@ int ubiblock_remove(struct ubi_volume_info *vi) /* Remove from device list */ list_del(&dev->list); - ubiblock_cleanup(dev); + + /* + * Prevent further opens. del_gendisk() will ensure that there are no + * parallel openers. + */ + dev->removing = true; mutex_unlock(&dev->dev_mutex); + + ubiblock_cleanup(dev); mutex_unlock(&devices_mutex); kfree(dev);