Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6266139imu; Mon, 21 Jan 2019 06:06:01 -0800 (PST) X-Google-Smtp-Source: ALg8bN6KZzeaEme0iIFE4grQtRzj/BmSNqk0/jMoZliGmQiqAIDX7483Y3Nt8wfjfi3juz37/uKg X-Received: by 2002:a65:6215:: with SMTP id d21mr28235273pgv.289.1548079561637; Mon, 21 Jan 2019 06:06:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079561; cv=none; d=google.com; s=arc-20160816; b=hdwoeNFvrLaGviqg3rW/1tgv9muXL0TVbm8t/fuYqd1AMrqU3y2/Kd/Op5T239ZThy pk2ibnpPzt27h/nnKxbpvxYDRpvUY8oqS1o0EBKHxemh97miEztTfO4YTac91pZVV6gD ZpJ6NumtNuh4vIhJV2tmzAZye1YfC5V+la7l0eaKu+VwsNi40ZF7UN+5EnObiZZ7jrMA oR3bWPHeOsYLB5UH+PrJx2mmMOjeHFgy06r2DfbW7syiVrel2Qv/63VrKJIYS2Fz4imh Uwjtu7b74WQJH6b1SlHxItySz9jDFgOaeloUjtYua7BJOV9HD7nWscFGn2Oa4eOHn08+ q2ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=adhAEaYR9sGDSL/AI1Z1L4aN9WMzx83cKyJsaXknE2Y=; b=ZZaVuM68mBykt1JIiW+LgLXBpr0zSPHIxiS3BF3JZQpkLeHb84wWG3ySdxM2kdhQyH ZDRaHx67PQyTWr2GdJbelF899KKDB+Y21X6MnqTws9PYwSltf2QwdVE9q4VLZNtRsAMk JK+1myLZyCa8eQIkE8CEXcxQ9QoKZ/xjZfwyu4rn1Z+hiAYug9vzSPKHjQSmbR6eTfWb eRJUumjagw9kOLjeWvy+J7wp3K96CttxY96/raEICfzI0DNkpQ9/RpBouhRju5qZAEc1 czWsx2Hx/eUUqLRF6cHiNQ1WUog07YO/TW06WN+mWsqQZmVlhXoLr3MXWoXKlqBNkr1V eFQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IeWElrjT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 62si12553624plc.87.2019.01.21.06.05.45; Mon, 21 Jan 2019 06:06:01 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IeWElrjT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732807AbfAUOC2 (ORCPT + 99 others); Mon, 21 Jan 2019 09:02:28 -0500 Received: from mail.kernel.org ([198.145.29.99]:49954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732794AbfAUOC1 (ORCPT ); Mon, 21 Jan 2019 09:02:27 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2C00A2089F; Mon, 21 Jan 2019 14:02:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079345; bh=H+SSyBwjKxYmN4LkbQhMamFdI5ICfg6iiTTJUcjxshU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IeWElrjTnvHuDzOwd5H4nnSOM5RmcOY9RJQv/f0XD75yPdHMQglEzyhOuHonr8vK6 mKMULIORCIHfFMYKeTtm5sj6Gf3OxMwRAoKeqpOZ98J1Du8JD0TyQzzvN5TAmyEWXP 0CI7wIDGoHjsp6OgTlgXaE3ehGhEsCKcR4ZEZmR4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Jan Kara , Jens Axboe Subject: [PATCH 4.19 93/99] loop: Fix deadlock when calling blkdev_reread_part() Date: Mon, 21 Jan 2019 14:49:25 +0100 Message-Id: <20190121134918.866879956@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121134913.924726465@linuxfoundation.org> References: <20190121134913.924726465@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jan Kara commit 0da03cab87e6323ff2e05b14bc7d5c6fcc531efd upstream. Calling blkdev_reread_part() under loop_ctl_mutex causes lockdep to complain about circular lock dependency between bdev->bd_mutex and lo->lo_ctl_mutex. The problem is that on loop device open or close lo_open() and lo_release() get called with bdev->bd_mutex held and they need to acquire loop_ctl_mutex. OTOH when loop_reread_partitions() is called with loop_ctl_mutex held, it will call blkdev_reread_part() which acquires bdev->bd_mutex. See syzbot report for details [1]. Move call to blkdev_reread_part() in __loop_clr_fd() from under loop_ctl_mutex to finish fixing of the lockdep warning and the possible deadlock. [1] https://syzkaller.appspot.com/bug?id=bf154052f0eea4bc7712499e4569505907d1588 Reported-by: syzbot Signed-off-by: Jan Kara Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/block/loop.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1031,12 +1031,14 @@ loop_init_xfer(struct loop_device *lo, s return err; } -static int __loop_clr_fd(struct loop_device *lo) +static int __loop_clr_fd(struct loop_device *lo, bool release) { struct file *filp = NULL; gfp_t gfp = lo->old_gfp_mask; struct block_device *bdev = lo->lo_device; int err = 0; + bool partscan = false; + int lo_number; mutex_lock(&loop_ctl_mutex); if (WARN_ON_ONCE(lo->lo_state != Lo_rundown)) { @@ -1089,7 +1091,15 @@ static int __loop_clr_fd(struct loop_dev module_put(THIS_MODULE); blk_mq_unfreeze_queue(lo->lo_queue); - if (lo->lo_flags & LO_FLAGS_PARTSCAN && bdev) { + partscan = lo->lo_flags & LO_FLAGS_PARTSCAN && bdev; + lo_number = lo->lo_number; + lo->lo_flags = 0; + if (!part_shift) + lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; + loop_unprepare_queue(lo); +out_unlock: + mutex_unlock(&loop_ctl_mutex); + if (partscan) { /* * bd_mutex has been held already in release path, so don't * acquire it if this function is called in such case. @@ -1098,21 +1108,15 @@ static int __loop_clr_fd(struct loop_dev * must be at least one and it can only become zero when the * current holder is released. */ - if (!atomic_read(&lo->lo_refcnt)) + if (release) err = __blkdev_reread_part(bdev); else err = blkdev_reread_part(bdev); pr_warn("%s: partition scan of loop%d failed (rc=%d)\n", - __func__, lo->lo_number, err); + __func__, lo_number, err); /* Device is gone, no point in returning error */ err = 0; } - lo->lo_flags = 0; - if (!part_shift) - lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; - loop_unprepare_queue(lo); -out_unlock: - mutex_unlock(&loop_ctl_mutex); /* * Need not hold loop_ctl_mutex to fput backing file. * Calling fput holding loop_ctl_mutex triggers a circular @@ -1153,7 +1157,7 @@ static int loop_clr_fd(struct loop_devic lo->lo_state = Lo_rundown; mutex_unlock(&loop_ctl_mutex); - return __loop_clr_fd(lo); + return __loop_clr_fd(lo, false); } static int @@ -1714,7 +1718,7 @@ static void lo_release(struct gendisk *d * In autoclear mode, stop the loop thread * and remove configuration after last close. */ - __loop_clr_fd(lo); + __loop_clr_fd(lo, true); return; } else if (lo->lo_state == Lo_bound) { /*