Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6252306imu; Mon, 21 Jan 2019 05:53:22 -0800 (PST) X-Google-Smtp-Source: ALg8bN4XL42BcCIvlS6AiEyvitdGukVZzeYUssIWzUZE1HghchF6X442qU3ecrjKkLAVhtG5zRrC X-Received: by 2002:a63:2bc4:: with SMTP id r187mr27959300pgr.306.1548078802144; Mon, 21 Jan 2019 05:53:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548078802; cv=none; d=google.com; s=arc-20160816; b=GS04l+OOgSdQ7pqVnYOfZ3HJ8qssf+ECr5bnII8hmZl+u8T7lvSGCRGjphgEHvQRe/ m7SV/7hoZ+sXxl2pAuC/Bxkk0hAFIw+8hKxcKedpVTGG63qapr3z6iB8DfPFzZjqgd9u xLaLzjBhzVpAM/r/9fCgAtuXwlduJfKDFTGtiYTCsJO884KkW4JsVPoX6OCOjM79JdHn ih30tBdf9s0E3ZHz3/lQFkvFdDuWkaUA5cNMBgQ/V3YbCAWYgn9cQmnQYYZfqfRmssdq nZmRMoNiaSAUosIJYlS3PK1XWsClAe5rDRXCDFD56cgXcWWtWdJtFxfwcswMOzhOkrZ0 uPNA== 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=HX3qeokR1lCQ2TNU7XmtvnEv7IDo1gt3daZ0ZCr6vTc=; b=tDs1IrO5rcgnyUvIJwdEUZB7NR6l9gFg3yyzmfgxpziSEYFm0nonE0afgUaURoSNOJ Y7FVekE+mSVrSo2hGwFnwcqhGswNdxCBIgWyoRV7oFVuv5D4jnRVWhL/n2xEufzhMFS6 tJkYYiGvlbMI6XUyzm/reaYZqqEbOxJcn8hNLe23z/SBBSYitHPGE40mEpdOHJZhP644 XsuNz+TYZaWp8uyMDg3AHQ/BotHHkb+s4BKg53WAfSiMOz9cyt5jdWKQoTdVfYypXD// afAo4nwLLzqWUHJu1m1WzOgxNMCtL2au9prBOV8y8xanX3x94wMP54E0gYpGNtTtfa45 27RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tPHQo8mi; 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 m38si12618934pgl.125.2019.01.21.05.53.06; Mon, 21 Jan 2019 05:53:22 -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=tPHQo8mi; 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 S1730556AbfAUNu3 (ORCPT + 99 others); Mon, 21 Jan 2019 08:50:29 -0500 Received: from mail.kernel.org ([198.145.29.99]:32954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729897AbfAUNuZ (ORCPT ); Mon, 21 Jan 2019 08:50:25 -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 4B07F2063F; Mon, 21 Jan 2019 13:50:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548078622; bh=g6is9RFcHxvF47aQaIxniW5bvOaCpvC91ndU+T811qo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tPHQo8miTomKBczG09L3icf0rCr1Wrk/rx9qR/xudVZerJC0AyqdTGotkcLGSyOAW zGDIBecfWG0ZS3NhS1B29ERwSpgfyAUZL/hId3PBzq6My16n8sS7H2zYQxzYXMIlbR 3Jmet4jv26aCUWNBah8FbI1aiQfiEwTyQ72hOqsc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Jens Axboe Subject: [PATCH 4.20 097/111] loop: Push lo_ctl_mutex down into individual ioctls Date: Mon, 21 Jan 2019 14:43:31 +0100 Message-Id: <20190121122506.544697790@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121122455.819406896@linuxfoundation.org> References: <20190121122455.819406896@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.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jan Kara commit a13165441d58b216adbd50252a9cc829d78a6bce upstream. Push acquisition of lo_ctl_mutex down into individual ioctl handling branches. This is a preparatory step for pushing the lock down into individual ioctl handling functions so that they can release the lock as they need it. We also factor out some simple ioctl handlers that will not need any special handling to reduce unnecessary code duplication. Signed-off-by: Jan Kara Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- drivers/block/loop.c | 88 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 25 deletions(-) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -1393,70 +1393,108 @@ static int loop_set_block_size(struct lo return 0; } -static int lo_ioctl(struct block_device *bdev, fmode_t mode, - unsigned int cmd, unsigned long arg) +static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd, + unsigned long arg) { - struct loop_device *lo = bdev->bd_disk->private_data; int err; err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); if (err) - goto out_unlocked; + return err; + switch (cmd) { + case LOOP_SET_CAPACITY: + err = loop_set_capacity(lo); + break; + case LOOP_SET_DIRECT_IO: + err = loop_set_dio(lo, arg); + break; + case LOOP_SET_BLOCK_SIZE: + err = loop_set_block_size(lo, arg); + break; + default: + err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; + } + mutex_unlock(&loop_ctl_mutex); + return err; +} + +static int lo_ioctl(struct block_device *bdev, fmode_t mode, + unsigned int cmd, unsigned long arg) +{ + struct loop_device *lo = bdev->bd_disk->private_data; + int err; switch (cmd) { case LOOP_SET_FD: + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_set_fd(lo, mode, bdev, arg); + mutex_unlock(&loop_ctl_mutex); break; case LOOP_CHANGE_FD: + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_change_fd(lo, bdev, arg); + mutex_unlock(&loop_ctl_mutex); break; case LOOP_CLR_FD: + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; /* loop_clr_fd would have unlocked loop_ctl_mutex on success */ err = loop_clr_fd(lo); - if (!err) - goto out_unlocked; + if (err) + mutex_unlock(&loop_ctl_mutex); break; case LOOP_SET_STATUS: err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) + if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_set_status_old(lo, (struct loop_info __user *)arg); + mutex_unlock(&loop_ctl_mutex); + } break; case LOOP_GET_STATUS: + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_get_status_old(lo, (struct loop_info __user *) arg); /* loop_get_status() unlocks loop_ctl_mutex */ - goto out_unlocked; + break; case LOOP_SET_STATUS64: err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) + if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) { + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_set_status64(lo, (struct loop_info64 __user *) arg); + mutex_unlock(&loop_ctl_mutex); + } break; case LOOP_GET_STATUS64: + err = mutex_lock_killable_nested(&loop_ctl_mutex, 1); + if (err) + return err; err = loop_get_status64(lo, (struct loop_info64 __user *) arg); /* loop_get_status() unlocks loop_ctl_mutex */ - goto out_unlocked; - case LOOP_SET_CAPACITY: - err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) - err = loop_set_capacity(lo); break; + case LOOP_SET_CAPACITY: case LOOP_SET_DIRECT_IO: - err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) - err = loop_set_dio(lo, arg); - break; case LOOP_SET_BLOCK_SIZE: - err = -EPERM; - if ((mode & FMODE_WRITE) || capable(CAP_SYS_ADMIN)) - err = loop_set_block_size(lo, arg); - break; + if (!(mode & FMODE_WRITE) && !capable(CAP_SYS_ADMIN)) + return -EPERM; + /* Fall through */ default: - err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; + err = lo_simple_ioctl(lo, cmd, arg); + break; } - mutex_unlock(&loop_ctl_mutex); -out_unlocked: return err; }