Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6267131imu; Mon, 21 Jan 2019 06:06:48 -0800 (PST) X-Google-Smtp-Source: ALg8bN6OdFCT3P6g36RxHV0Aeb5XQUIkUP4tUBmAV0+jo2o2qP7hdIi12NLPA+4iMNqf0GR71tMg X-Received: by 2002:a62:34c6:: with SMTP id b189mr30510482pfa.229.1548079608549; Mon, 21 Jan 2019 06:06:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079608; cv=none; d=google.com; s=arc-20160816; b=h8nP5HtTuVWI3Rt9hHWSuoV4PON0jj4+ZCQ6c64bveMNSmlDFRv6vgymlqKHy/7wke BmRaBLzz7BgbwHOv665f5IsVwhsGbLTXFeUdbZQYoLMlGguZEMTObDhs9RJD8sevj0zM lsiZTyQsdlzCtwXv+FXMMEUWNpbQqsKFOdQsjOeUBCQ5z1sEwfimDgFNsEPchZB5Z70D qVyy4xA4NOpCa0jbVmyfBeTDgnXs6/bPdNYJNLehTOKNfI9drfHxGsK2l+Tui97meKSa iDRH6PgZnyWToJvpDVNKj6ii8VVtufGUCR8t91rplVvc1APeS0Z8JONmBGCwA/R4bjwO csgQ== 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=sf3rnl5zYF65MQEyro3t/ZHgaNkp5kfdLtRe2IpHk6U=; b=XrbbMWmMS/x4SsY1laDieoFuhXeafezyx3y0g52Tc/fSL4aL5Fvd6LivqiXAYaLMR8 vqomN1SrsZBJS3vN6CMVOC+bq2fpt8JlUUZ6Lafp1iQlDxXH1b+tuBqkQIR62TE3iC4t xZEdZt7EObS6NXBaR//JWbAWnFwf4Ijg/Y7A/yFiEw0y4zV8J3IMje6VImP77oBgiygq cVs7cSJ98qPTi7RLqnWXAWMYZ64+CXDGbL/AO+kerFCsYrLGGj6fL1oNra3vTMLK6vvr kpDlvXj3mF0i4oKOFGDw2FDNopnvE19H3ah9JKJ6OvUjcqDSXvjprESiMHcygF80jpJO t9+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=R+IF+A67; 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 k11si12761941pgh.132.2019.01.21.06.06.33; Mon, 21 Jan 2019 06:06:48 -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=R+IF+A67; 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 S1732823AbfAUOCc (ORCPT + 99 others); Mon, 21 Jan 2019 09:02:32 -0500 Received: from mail.kernel.org ([198.145.29.99]:50068 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732794AbfAUOCa (ORCPT ); Mon, 21 Jan 2019 09:02:30 -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 7017C2089F; Mon, 21 Jan 2019 14:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079348; bh=SQb1UvU5+nbrwnLg01DO80VHtQBoYxjFHOfeGcJwagQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R+IF+A67lLLIWlkY4qi9x8WeweVCFg2OTgPcaSBgwdcaxivPsdP9bdTgtC4Ebu705 83TEBCO5PRLSD5+9I/KaP1+PtiVPAjEE5sDt+nog1SKbiIpu0NjiggsGWPa23oBZYc RVgI45Av+BWS4b/8laQDlEyVOv78yeTbQfFiPCNk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , Jan Kara , Jens Axboe Subject: [PATCH 4.19 94/99] loop: Avoid circular locking dependency between loop_ctl_mutex and bd_mutex Date: Mon, 21 Jan 2019 14:49:26 +0100 Message-Id: <20190121134919.270842431@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 1dded9acf6dc9a34cd27fcf8815507e4e65b3c4f upstream. Code in loop_change_fd() drops reference to the old file (and also the new file in a failure case) under loop_ctl_mutex. Similarly to a situation in loop_set_fd() this can create a circular locking dependency if this was the last reference holding the file open. Delay dropping of the file reference until we have released loop_ctl_mutex. Reported-by: Tetsuo Handa Signed-off-by: Jan Kara Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/block/loop.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -678,7 +678,7 @@ static int loop_validate_file(struct fil static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, unsigned int arg) { - struct file *file, *old_file; + struct file *file = NULL, *old_file; int error; bool partscan; @@ -687,21 +687,21 @@ static int loop_change_fd(struct loop_de return error; error = -ENXIO; if (lo->lo_state != Lo_bound) - goto out_unlock; + goto out_err; /* the loop device has to be read-only */ error = -EINVAL; if (!(lo->lo_flags & LO_FLAGS_READ_ONLY)) - goto out_unlock; + goto out_err; error = -EBADF; file = fget(arg); if (!file) - goto out_unlock; + goto out_err; error = loop_validate_file(file, bdev); if (error) - goto out_putf; + goto out_err; old_file = lo->lo_backing_file; @@ -709,7 +709,7 @@ static int loop_change_fd(struct loop_de /* size of the new backing store needs to be the same */ if (get_loop_size(lo, file) != get_loop_size(lo, old_file)) - goto out_putf; + goto out_err; /* and ... switch */ blk_mq_freeze_queue(lo->lo_queue); @@ -720,18 +720,22 @@ static int loop_change_fd(struct loop_de lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); loop_update_dio(lo); blk_mq_unfreeze_queue(lo->lo_queue); - - fput(old_file); partscan = lo->lo_flags & LO_FLAGS_PARTSCAN; mutex_unlock(&loop_ctl_mutex); + /* + * We must drop file reference outside of loop_ctl_mutex as dropping + * the file ref can take bd_mutex which creates circular locking + * dependency. + */ + fput(old_file); if (partscan) loop_reread_partitions(lo, bdev); return 0; -out_putf: - fput(file); -out_unlock: +out_err: mutex_unlock(&loop_ctl_mutex); + if (file) + fput(file); return error; }