linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>
Cc: linux-block@vger.kernel.org, Ming Lei <ming.lei@redhat.com>,
	Colin Ian King <colin.king@canonical.com>,
	Pavel Tatashin <pasha.tatashin@soleen.com>
Subject: [PATCH] block: loop: fix deadlock between open and remove
Date: Fri,  4 Jun 2021 08:04:24 +0800	[thread overview]
Message-ID: <20210604000424.189928-1-ming.lei@redhat.com> (raw)

Commit c76f48eb5c08 ("block: take bd_mutex around delete_partitions in
del_gendisk") adds disk->part0->bd_mutex in del_gendisk(), this way
causes the following AB/BA deadlock between removing loop and opening
loop:

1) loop_control_ioctl(LOOP_CTL_REMOVE)
- mutex_lock(&loop_ctl_mutex)
- mutex_lock(&disk->part0->bd_mutex)	//del_gendisk

2) open look device
- mutex_lock(&disk->part0->bd_mutex)	//blkdev_get_by_dev
- mutex_lock(&loop_ctl_mutex)		//lo_open() <- __blkdev_get

Fixes the issue by not holding loop_ctl_mutex in lo_open(), and cover
the protection on bdev->bd_disk->private_data via disk->part0->bd_mutex.

Reported-by: Colin Ian King <colin.king@canonical.com>
Fixes: c76f48eb5c08 ("block: take bd_mutex around delete_partitions in del_gendisk")
Cc: Pavel Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/loop.c | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index d58d68f3c7cd..b03d8f4c1cdf 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -1883,20 +1883,14 @@ static int lo_open(struct block_device *bdev, fmode_t mode)
 	int err;
 
 	/*
-	 * take loop_ctl_mutex to protect lo pointer from race with
-	 * loop_control_ioctl(LOOP_CTL_REMOVE), however, to reduce contention
-	 * release it prior to updating lo->lo_refcnt.
+	 * both ->private_data and ->lo_refcnt are covered by disk's
+	 * open_mutex, so race between open and remove can be avoided
 	 */
-	err = mutex_lock_killable(&loop_ctl_mutex);
-	if (err)
-		return err;
 	lo = bdev->bd_disk->private_data;
-	if (!lo) {
-		mutex_unlock(&loop_ctl_mutex);
+	if (!lo)
 		return -ENXIO;
-	}
+
 	err = mutex_lock_killable(&lo->lo_mutex);
-	mutex_unlock(&loop_ctl_mutex);
 	if (err)
 		return err;
 	atomic_inc(&lo->lo_refcnt);
@@ -2272,21 +2266,26 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 		ret = loop_lookup(&lo, parm);
 		if (ret < 0)
 			break;
-		ret = mutex_lock_killable(&lo->lo_mutex);
+		/* cover removing vs. opening loop device */
+		ret = mutex_lock_killable(&lo->lo_disk->part0->bd_mutex);
 		if (ret)
 			break;
-		if (lo->lo_state != Lo_unbound) {
-			ret = -EBUSY;
-			mutex_unlock(&lo->lo_mutex);
+		ret = mutex_lock_killable(&lo->lo_mutex);
+		if (ret) {
+			mutex_unlock(&lo->lo_disk->part0->bd_mutex);
 			break;
 		}
-		if (atomic_read(&lo->lo_refcnt) > 0) {
+		if (lo->lo_state != Lo_unbound ||
+				atomic_read(&lo->lo_refcnt) > 0) {
 			ret = -EBUSY;
 			mutex_unlock(&lo->lo_mutex);
+			mutex_unlock(&lo->lo_disk->part0->bd_mutex);
 			break;
 		}
-		lo->lo_disk->private_data = NULL;
 		mutex_unlock(&lo->lo_mutex);
+		lo->lo_disk->private_data = NULL;
+		mutex_unlock(&lo->lo_disk->part0->bd_mutex);
+
 		idr_remove(&loop_index_idr, lo->lo_number);
 		loop_remove(lo);
 		break;
-- 
2.29.2


             reply	other threads:[~2021-06-04  0:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-04  0:04 Ming Lei [this message]
2021-06-04  6:57 ` [PATCH] block: loop: fix deadlock between open and remove Christoph Hellwig
2021-06-04  8:56   ` Ming Lei
2021-06-05 14:09 Christoph Hellwig
2021-06-05 15:00 ` Colin Ian King
2021-06-07  0:11 ` Ming Lei
2021-06-11 17:51 ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210604000424.189928-1-ming.lei@redhat.com \
    --to=ming.lei@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=colin.king@canonical.com \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=pasha.tatashin@soleen.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).