Skip to contentSkip to conversation

sort out the lock order in the loop driver

PATCH New Christoph Hellwig 3 participants 10 messages Aug 18, 2021 – Aug 19, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series sorts out lock order reversals involving the block layer open_mutex by drastically reducing the scope of loop_ctl_mutex. To do so it first merges the cryptoloop module into the main loop driver as the unregistrtion of transfers on live loop devices was causing some nasty locking interactions.

Diffstat:

 b/drivers/block/Kconfig    |    6 
 b/drivers/block/Makefile   |    1 
 b/drivers/block/loop.c     |  327 ++++++++++++++++++++++++---------------------
 b/drivers/block/loop.h     |   30 ----
 drivers/block/cryptoloop.c |  204 ----------------------------
 5 files changed, 188 insertions(+), 380 deletions(-)
#1 lore raw

PATCH 1/7 loop: remove the ->ioctl method in loop_func_table

Never set to anything useful.

---
 drivers/block/cryptoloop.c | 7 -------
 drivers/block/loop.c       | 4 +---
 drivers/block/loop.h       | 3 ---
 3 files changed, 1 insertion(+), 13 deletions(-)
drivers/block/cryptoloop.c +0 -7
index 3cabc335ae74..8be35d37e379 100644
@@ -154,12 +154,6 @@ cryptoloop_transfer(struct loop_device *lo, int cmd,
 	return err;
 }
 
-static int
-cryptoloop_ioctl(struct loop_device *lo, int cmd, unsigned long arg)
-{
-	return -EINVAL;
-}
-
 static int
 cryptoloop_release(struct loop_device *lo)
 {
@@ -176,7 +170,6 @@ cryptoloop_release(struct loop_device *lo)
 static struct loop_func_table cryptoloop_funcs = {
 	.number = LO_CRYPT_CRYPTOAPI,
 	.init = cryptoloop_init,
-	.ioctl = cryptoloop_ioctl,
 	.transfer = cryptoloop_transfer,
 	.release = cryptoloop_release,
 	.owner = THIS_MODULE
drivers/block/loop.c +1 -3
index fa1c298a8cfb..b0dfea6d4371 100644
@@ -1169,7 +1169,6 @@ loop_set_status_from_info(struct loop_device *lo,
 	if (!xfer)
 		xfer = &none_funcs;
 	lo->transfer = xfer->transfer;
-	lo->ioctl = xfer->ioctl;
 
 	lo->lo_flags = info->lo_flags;
 
@@ -1383,7 +1382,6 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
 
 	loop_release_xfer(lo);
 	lo->transfer = NULL;
-	lo->ioctl = NULL;
 	lo->lo_device = NULL;
 	lo->lo_encryption = NULL;
 	lo->lo_offset = 0;
@@ -1809,7 +1807,7 @@ static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
 		err = loop_set_block_size(lo, arg);
 		break;
 	default:
-		err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
+		err = -EINVAL;
 	}
 	mutex_unlock(&lo->lo_mutex);
 	return err;
drivers/block/loop.h +0 -3
index 1988899db63a..dcde46afc675 100644
@@ -43,8 +43,6 @@ struct loop_device {
 	struct loop_func_table *lo_encryption;
 	__u32           lo_init[2];
 	kuid_t		lo_key_owner;	/* Who set the key */
-	int		(*ioctl)(struct loop_device *, int cmd, 
-				 unsigned long arg); 
 
 	struct file *	lo_backing_file;
 	struct block_device *lo_device;
@@ -91,7 +89,6 @@ struct loop_func_table {
 	int (*init)(struct loop_device *, const struct loop_info64 *); 
 	/* release is called from loop_unregister_transfer or clr_fd */
 	int (*release)(struct loop_device *); 
-	int (*ioctl)(struct loop_device *, int cmd, unsigned long arg);
 	struct module *owner;
 }; 
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/7 loop: return void from the ->release method in loop_func_table

In reply to Christoph Hellwig

Returning an error here is no useful. So remove the cargo culted check in cryptoloop and remove the return value.

---
 drivers/block/cryptoloop.c | 12 +++---------
 drivers/block/loop.c       | 10 +++-------
 drivers/block/loop.h       |  2 +-
 3 files changed, 7 insertions(+), 17 deletions(-)
drivers/block/cryptoloop.c +3 -9
index 8be35d37e379..1d9e56860e56 100644
@@ -154,17 +154,11 @@ cryptoloop_transfer(struct loop_device *lo, int cmd,
 	return err;
 }
 
-static int
+static void
 cryptoloop_release(struct loop_device *lo)
 {
-	struct crypto_sync_skcipher *tfm = lo->key_data;
-	if (tfm != NULL) {
-		crypto_free_sync_skcipher(tfm);
-		lo->key_data = NULL;
-		return 0;
-	}
-	printk(KERN_ERR "cryptoloop_release(): tfm == NULL?\n");
-	return -EINVAL;
+	crypto_free_sync_skcipher(lo->key_data);
+	lo->key_data = NULL;
 }
 
 static struct loop_func_table cryptoloop_funcs = {
drivers/block/loop.c +3 -7
index b0dfea6d4371..d63e9f2c9e9b 100644
@@ -1084,20 +1084,18 @@ static void loop_update_rotational(struct loop_device *lo)
 		blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
 }
 
-static int
+static void
 loop_release_xfer(struct loop_device *lo)
 {
-	int err = 0;
 	struct loop_func_table *xfer = lo->lo_encryption;
 
 	if (xfer) {
 		if (xfer->release)
-			err = xfer->release(lo);
+			xfer->release(lo);
 		lo->transfer = NULL;
 		lo->lo_encryption = NULL;
 		module_put(xfer->owner);
 	}
-	return err;
 }
 
 static int
@@ -1140,9 +1138,7 @@ loop_set_status_from_info(struct loop_device *lo,
 	if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
 		return -EINVAL;
 
-	err = loop_release_xfer(lo);
-	if (err)
-		return err;
+	loop_release_xfer(lo);
 
 	if (info->lo_encrypt_type) {
 		unsigned int type = info->lo_encrypt_type;
drivers/block/loop.h +1 -1
index dcde46afc675..7b84ef724de1 100644
@@ -88,7 +88,7 @@ struct loop_func_table {
 			int size, sector_t real_block);
 	int (*init)(struct loop_device *, const struct loop_info64 *); 
 	/* release is called from loop_unregister_transfer or clr_fd */
-	int (*release)(struct loop_device *); 
+	void (*release)(struct loop_device *); 
 	struct module *owner;
 }; 
 
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/7 loop: merge the cryptoloop module into the main loop module

In reply to Christoph Hellwig

No need to keep a separate loadable module infrastructure for a tiny amount of cryptoapi glue, especially as unloading of the cryptoloop module leads to nasty interactions with the loop device state machine through loop_unregister_transfer.

---
 drivers/block/Kconfig      |   6 +-
 drivers/block/Makefile     |   1 -
 drivers/block/cryptoloop.c | 191 -----------------------------------
 drivers/block/loop.c       | 202 +++++++++++++++++++++++++++----------
 drivers/block/loop.h       |   5 -
 5 files changed, 153 insertions(+), 252 deletions(-)
 delete mode 100644 drivers/block/cryptoloop.c
drivers/block/Kconfig +3 -3
index 63056cfd4b62..c63386576ef6 100644
@@ -156,6 +156,8 @@ config BLK_DEV_COW_COMMON
 
 config BLK_DEV_LOOP
 	tristate "Loopback device support"
+	select CRYPTO if CRYPTOLOOP
+	select CRYPTO_CBC if CRYPTOLOOP
 	help
 	  Saying Y here will allow you to use a regular file as a block
 	  device; you can then create a file system on that block device and
@@ -213,9 +215,7 @@ config BLK_DEV_LOOP_MIN_COUNT
 	  dynamically allocated with the /dev/loop-control interface.
 
 config BLK_DEV_CRYPTOLOOP
-	tristate "Cryptoloop Support"
-	select CRYPTO
-	select CRYPTO_CBC
+	bool "Cryptoloop Support"
 	depends on BLK_DEV_LOOP
 	help
 	  Say Y here if you want to be able to use the ciphers that are 
drivers/block/Makefile +0 -1
index bc68817ef496..11a74f17c9ad 100644
@@ -24,7 +24,6 @@ obj-$(CONFIG_CDROM_PKTCDVD)	+= pktcdvd.o
 obj-$(CONFIG_SUNVDC)		+= sunvdc.o
 
 obj-$(CONFIG_BLK_DEV_NBD)	+= nbd.o
-obj-$(CONFIG_BLK_DEV_CRYPTOLOOP) += cryptoloop.o
 obj-$(CONFIG_VIRTIO_BLK)	+= virtio_blk.o
 
 obj-$(CONFIG_BLK_DEV_SX8)	+= sx8.o
drivers/block/cryptoloop.c deleted +0 -191

191 lines not on this page · raw message

drivers/block/loop.c +150 -52

246 lines not on this page · raw message

drivers/block/loop.h +0 -5
index 7b84ef724de1..dd12d7f1ce30 100644
@@ -87,12 +87,7 @@ struct loop_func_table {
 			struct page *loop_page, unsigned loop_off,
 			int size, sector_t real_block);
 	int (*init)(struct loop_device *, const struct loop_info64 *); 
-	/* release is called from loop_unregister_transfer or clr_fd */
 	void (*release)(struct loop_device *); 
-	struct module *owner;
 }; 
 
-int loop_register_transfer(struct loop_func_table *funcs);
-int loop_unregister_transfer(int number); 
-
 #endif
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/7 loop: devirtualize transfer transformations

In reply to Christoph Hellwig

Besides the already special cased non-transform fastpath there are only two different transfers. Hardcode them instead of the maze of indirect calls and switch the whole cryptoloop code to use IS_ENABLED for dead code elimination.

---
 drivers/block/loop.c | 146 +++++++++++--------------------------------
 drivers/block/loop.h |  21 ++-----
 2 files changed, 41 insertions(+), 126 deletions(-)
drivers/block/loop.c +37 -109
index 8b187987904e..9176784d4fca 100644
@@ -140,7 +140,7 @@ static void loop_global_unlock(struct loop_device *lo, bool global)
 static int max_part;
 static int part_shift;
 
-static int transfer_xor(struct loop_device *lo, int cmd,
+static void xor_transfer(struct loop_device *lo, int cmd,
 			struct page *raw_page, unsigned raw_off,
 			struct page *loop_page, unsigned loop_off,
 			int size, sector_t real_block)
@@ -166,27 +166,14 @@ static int transfer_xor(struct loop_device *lo, int cmd,
 	kunmap_atomic(loop_buf);
 	kunmap_atomic(raw_buf);
 	cond_resched();
-	return 0;
 }
 
-static int xor_init(struct loop_device *lo, const struct loop_info64 *info)
+static inline bool is_cryptoloop(int encrypt_type)
 {
-	if (unlikely(info->lo_encrypt_key_size <= 0))
-		return -EINVAL;
-	return 0;
+	return IS_ENABLED(CONFIG_BLK_DEV_CRYPTOLOOP) &&
+		encrypt_type == LO_CRYPT_CRYPTOAPI;
 }
 
-static struct loop_func_table none_funcs = {
-	.number = LO_CRYPT_NONE,
-}; 
-
-static struct loop_func_table xor_funcs = {
-	.number = LO_CRYPT_XOR,
-	.transfer = transfer_xor,
-	.init = xor_init
-}; 
-
-#ifdef CONFIG_BLK_DEV_CRYPTOLOOP
 static int cryptoloop_init(struct loop_device *lo,
 		const struct loop_info64 *info)
 {
@@ -235,7 +222,7 @@ static int cryptoloop_init(struct loop_device *lo,
 					  info->lo_encrypt_key_size);
 	if (err != 0)
 		goto out_free_tfm;
-	lo->key_data = tfm;
+	lo->lo_encrypt_tfm = tfm;
 	return 0;
 
  out_free_tfm:
@@ -249,7 +236,7 @@ static int cryptoloop_transfer(struct loop_device *lo, int cmd,
 		struct page *raw_page, unsigned raw_off, struct page *loop_page,
 		unsigned loop_off, int size, sector_t IV)
 {
-	struct crypto_sync_skcipher *tfm = lo->key_data;
+	struct crypto_sync_skcipher *tfm = lo->lo_encrypt_tfm;
 	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
 	struct scatterlist sg_out;
 	struct scatterlist sg_in;
@@ -302,32 +289,11 @@ static int cryptoloop_transfer(struct loop_device *lo, int cmd,
 	err = 0;
 out:
 	skcipher_request_zero(req);
+	pr_err_ratelimited("loop: Transfer error at byte offset %llu, length %i.\n",
+			IV << 9, size);
 	return err;
 }
 
-static void cryptoloop_release(struct loop_device *lo)
-{
-	crypto_free_sync_skcipher(lo->key_data);
-	lo->key_data = NULL;
-}
-
-static struct loop_func_table cryptoloop_funcs = {
-	.number		= LO_CRYPT_CRYPTOAPI,
-	.init		= cryptoloop_init,
-	.transfer	= cryptoloop_transfer,
-	.release	= cryptoloop_release,
-};
-#endif /* CONFIG_BLK_DEV_CRYPTOLOOP */
-
-/* xfer_funcs[0] is special - its release function is never called */
-static struct loop_func_table *xfer_funcs[MAX_LO_CRYPT] = {
-	[LO_CRYPT_NONE]		= &none_funcs,
-	[LO_CRYPT_XOR]		= &xor_funcs,
-#ifdef CONFIG_BLK_DEV_CRYPTOLOOP
-	[LO_CRYPT_CRYPTOAPI]	= &cryptoloop_funcs,
-#endif
-};
-
 static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
 {
 	loff_t loopsize;
@@ -382,7 +348,7 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 		if (queue_logical_block_size(lo->lo_queue) >= sb_bsize &&
 				!(lo->lo_offset & dio_align) &&
 				mapping->a_ops->direct_IO &&
-				!lo->transfer)
+				!lo->lo_encrypt_type)
 			use_dio = true;
 		else
 			use_dio = false;
@@ -448,16 +414,11 @@ lo_do_transfer(struct loop_device *lo, int cmd,
 	       struct page *lpage, unsigned loffs,
 	       int size, sector_t rblock)
 {
-	int ret;
-
-	ret = lo->transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
-	if (likely(!ret))
-		return 0;
-
-	printk_ratelimited(KERN_ERR
-		"loop: Transfer error at byte offset %llu, length %i.\n",
-		(unsigned long long)rblock << 9, size);
-	return ret;
+	if (is_cryptoloop(lo->lo_encrypt_type))
+		return cryptoloop_transfer(lo, cmd, rpage, roffs, lpage, loffs,
+					   size, rblock);
+	xor_transfer(lo, cmd, rpage, roffs, lpage, loffs, size, rblock);
+	return 0;
 }
 
 static int lo_write_bvec(struct file *file, struct bio_vec *bvec, loff_t *ppos)
@@ -803,14 +764,14 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
 	case REQ_OP_DISCARD:
 		return lo_fallocate(lo, rq, pos, FALLOC_FL_PUNCH_HOLE);
 	case REQ_OP_WRITE:
-		if (lo->transfer)
+		if (lo->lo_encrypt_type)
 			return lo_write_transfer(lo, rq, pos);
 		else if (cmd->use_aio)
 			return lo_rw_aio(lo, cmd, pos, WRITE);
 		else
 			return lo_write_simple(lo, rq, pos);
 	case REQ_OP_READ:
-		if (lo->transfer)
+		if (lo->lo_encrypt_type)
 			return lo_read_transfer(lo, rq, pos);
 		else if (cmd->use_aio)
 			return lo_rw_aio(lo, cmd, pos, READ);
@@ -1227,33 +1188,6 @@ static void loop_update_rotational(struct loop_device *lo)
 		blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
 }
 
-static void
-loop_release_xfer(struct loop_device *lo)
-{
-	struct loop_func_table *xfer = lo->lo_encryption;
-
-	if (xfer) {
-		if (xfer->release)
-			xfer->release(lo);
-		lo->transfer = NULL;
-		lo->lo_encryption = NULL;
-	}
-}
-
-static int
-loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
-	       const struct loop_info64 *i)
-{
-	int err = 0;
-
-	if (xfer && xfer->init) {
-		err = xfer->init(lo, i);
-		if (!err)
-			lo->lo_encryption = xfer;
-	}
-	return err;
-}
-
 /**
  * loop_set_status_from_info - configure device from loop_info
  * @lo: struct loop_device to configure
@@ -1267,29 +1201,28 @@ loop_set_status_from_info(struct loop_device *lo,
 			  const struct loop_info64 *info)
 {
 	int err;
-	struct loop_func_table *xfer;
 	kuid_t uid = current_uid();
 
 	if ((unsigned int) info->lo_encrypt_key_size > LO_KEY_SIZE)
 		return -EINVAL;
 
-	loop_release_xfer(lo);
-
-	if (info->lo_encrypt_type) {
-		unsigned int type = info->lo_encrypt_type;
+	if (is_cryptoloop(lo->lo_encrypt_type))
+		crypto_free_sync_skcipher(lo->lo_encrypt_tfm);
+	lo->lo_encrypt_tfm = NULL;
 
-		if (type >= MAX_LO_CRYPT)
+	if (info->lo_encrypt_type == LO_CRYPT_XOR) {
+		if (info->lo_encrypt_key_size <= 0)
 			return -EINVAL;
-		xfer = xfer_funcs[type];
-		if (xfer == NULL)
+	} else if (is_cryptoloop(info->lo_encrypt_type)) {
+		err = cryptoloop_init(lo, info);
+		if (err)
+			return err;
+	} else {
+		if (info->lo_encrypt_type != LO_CRYPT_NONE)
 			return -EINVAL;
-	} else
-		xfer = NULL;
-
-	err = loop_init_xfer(lo, xfer, info);
-	if (err)
-		return err;
+	}
 
+	lo->lo_encrypt_type = info->lo_encrypt_type;
 	lo->lo_offset = info->lo_offset;
 	lo->lo_sizelimit = info->lo_sizelimit;
 	memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
@@ -1297,10 +1230,6 @@ loop_set_status_from_info(struct loop_device *lo,
 	lo->lo_file_name[LO_NAME_SIZE-1] = 0;
 	lo->lo_crypt_name[LO_NAME_SIZE-1] = 0;
 
-	if (!xfer)
-		xfer = &none_funcs;
-	lo->transfer = xfer->transfer;
-
 	lo->lo_flags = info->lo_flags;
 
 	lo->lo_encrypt_key_size = info->lo_encrypt_key_size;
@@ -1511,10 +1440,10 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
 	lo->lo_backing_file = NULL;
 	spin_unlock_irq(&lo->lo_lock);
 
-	loop_release_xfer(lo);
-	lo->transfer = NULL;
+	if (is_cryptoloop(lo->lo_encrypt_type))
+		crypto_free_sync_skcipher(lo->lo_encrypt_tfm);
+	lo->lo_encrypt_tfm = NULL;
 	lo->lo_device = NULL;
-	lo->lo_encryption = NULL;
 	lo->lo_offset = 0;
 	lo->lo_sizelimit = 0;
 	lo->lo_encrypt_key_size = 0;
@@ -1727,8 +1656,7 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info)
 	info->lo_flags = lo->lo_flags;
 	memcpy(info->lo_file_name, lo->lo_file_name, LO_NAME_SIZE);
 	memcpy(info->lo_crypt_name, lo->lo_crypt_name, LO_NAME_SIZE);
-	info->lo_encrypt_type =
-		lo->lo_encryption ? lo->lo_encryption->number : 0;
+	info->lo_encrypt_type = lo->lo_encrypt_type;
 	if (lo->lo_encrypt_key_size && capable(CAP_SYS_ADMIN)) {
 		info->lo_encrypt_key_size = lo->lo_encrypt_key_size;
 		memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
@@ -1764,7 +1692,7 @@ loop_info64_from_old(const struct loop_info *info, struct loop_info64 *info64)
 	info64->lo_flags = info->lo_flags;
 	info64->lo_init[0] = info->lo_init[0];
 	info64->lo_init[1] = info->lo_init[1];
-	if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
+	if (is_cryptoloop(info->lo_encrypt_type))
 		memcpy(info64->lo_crypt_name, info->lo_name, LO_NAME_SIZE);
 	else
 		memcpy(info64->lo_file_name, info->lo_name, LO_NAME_SIZE);
@@ -1785,7 +1713,7 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
 	info->lo_flags = info64->lo_flags;
 	info->lo_init[0] = info64->lo_init[0];
 	info->lo_init[1] = info64->lo_init[1];
-	if (info->lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
+	if (is_cryptoloop(info->lo_encrypt_type))
 		memcpy(info->lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
 	else
 		memcpy(info->lo_name, info64->lo_file_name, LO_NAME_SIZE);
@@ -2048,7 +1976,7 @@ loop_info64_from_compat(const struct compat_loop_info __user *arg,
 	info64->lo_flags = info.lo_flags;
 	info64->lo_init[0] = info.lo_init[0];
 	info64->lo_init[1] = info.lo_init[1];
-	if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
+	if (is_cryptoloop(info.lo_encrypt_type))
 		memcpy(info64->lo_crypt_name, info.lo_name, LO_NAME_SIZE);
 	else
 		memcpy(info64->lo_file_name, info.lo_name, LO_NAME_SIZE);
@@ -2077,7 +2005,7 @@ loop_info64_to_compat(const struct loop_info64 *info64,
 	info.lo_flags = info64->lo_flags;
 	info.lo_init[0] = info64->lo_init[0];
 	info.lo_init[1] = info64->lo_init[1];
-	if (info.lo_encrypt_type == LO_CRYPT_CRYPTOAPI)
+	if (is_cryptoloop(info.lo_encrypt_type))
 		memcpy(info.lo_name, info64->lo_crypt_name, LO_NAME_SIZE);
 	else
 		memcpy(info.lo_name, info64->lo_file_name, LO_NAME_SIZE);
drivers/block/loop.h +4 -17
index dd12d7f1ce30..d14ce6bdc014 100644
@@ -16,6 +16,8 @@
 #include <linux/mutex.h>
 #include <uapi/linux/loop.h>
 
+struct crypto_sync_skcipher;
+
 /* Possible states of device */
 enum {
 	Lo_unbound,
@@ -32,21 +34,17 @@ struct loop_device {
 	loff_t		lo_offset;
 	loff_t		lo_sizelimit;
 	int		lo_flags;
-	int		(*transfer)(struct loop_device *, int cmd,
-				    struct page *raw_page, unsigned raw_off,
-				    struct page *loop_page, unsigned loop_off,
-				    int size, sector_t real_block);
 	char		lo_file_name[LO_NAME_SIZE];
 	char		lo_crypt_name[LO_NAME_SIZE];
 	char		lo_encrypt_key[LO_KEY_SIZE];
 	int		lo_encrypt_key_size;
-	struct loop_func_table *lo_encryption;
+	int		lo_encrypt_type;
+	struct crypto_sync_skcipher *lo_encrypt_tfm; 
 	__u32           lo_init[2];
 	kuid_t		lo_key_owner;	/* Who set the key */
 
 	struct file *	lo_backing_file;
 	struct block_device *lo_device;
-	void		*key_data; 
 
 	gfp_t		old_gfp_mask;
 
@@ -79,15 +77,4 @@ struct loop_cmd {
 	struct cgroup_subsys_state *memcg_css;
 };
 
-/* Support for loadable transfer modules */
-struct loop_func_table {
-	int number;	/* filter type */ 
-	int (*transfer)(struct loop_device *lo, int cmd,
-			struct page *raw_page, unsigned raw_off,
-			struct page *loop_page, unsigned loop_off,
-			int size, sector_t real_block);
-	int (*init)(struct loop_device *, const struct loop_info64 *); 
-	void (*release)(struct loop_device *); 
-}; 
-
 #endif
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 4/7] loop: devirtualize transfer transformations

I love your patch! Perhaps something to improve:

Hi Christoph,

I love your patch! Perhaps something to improve:

[auto build test WARNING on block/for-next]
[also build test WARNING on v5.14-rc6 next-20210819]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Christoph-Hellwig/loop-remove-the-ioctl-method-in-loop_func_table/20210818-152618
base:   https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux-block.git for-next
config: x86_64-rhel-8.3-kselftests (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-348-gf0e6938b-dirty
        # https://github.com/0day-ci/linux/commit/4bc5016d77d7be3372940a65169beaec5ed6aaf4
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Christoph-Hellwig/loop-remove-the-ioctl-method-in-loop_func_table/20210818-152618
        git checkout 4bc5016d77d7be3372940a65169beaec5ed6aaf4
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> drivers/block/loop.c:273:23: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned int @@     got restricted __le32 [usertype] @@

drivers/block/loop.c:273:23: sparse: expected unsigned int
drivers/block/loop.c:273:23: sparse: got restricted __le32 [usertype]

vim +273 drivers/block/loop.c

a4d25ed5a46e4a Christoph Hellwig 2021-08-18  234  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  235  static int cryptoloop_transfer(struct loop_device *lo, int cmd,
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  236  		struct page *raw_page, unsigned raw_off, struct page *loop_page,
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  237  		unsigned loop_off, int size, sector_t IV)
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  238  {
4bc5016d77d7be Christoph Hellwig 2021-08-18  239  	struct crypto_sync_skcipher *tfm = lo->lo_encrypt_tfm;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  240  	SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  241  	struct scatterlist sg_out;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  242  	struct scatterlist sg_in;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  243  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  244  	encdec_cbc_t encdecfunc;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  245  	struct page *in_page, *out_page;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  246  	unsigned in_offs, out_offs;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  247  	int err;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  248  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  249  	skcipher_request_set_sync_tfm(req, tfm);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  250  	skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  251  				      NULL, NULL);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  252  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  253  	sg_init_table(&sg_out, 1);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  254  	sg_init_table(&sg_in, 1);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  255  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  256  	if (cmd == READ) {
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  257  		in_page = raw_page;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  258  		in_offs = raw_off;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  259  		out_page = loop_page;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  260  		out_offs = loop_off;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  261  		encdecfunc = crypto_skcipher_decrypt;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  262  	} else {
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  263  		in_page = loop_page;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  264  		in_offs = loop_off;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  265  		out_page = raw_page;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  266  		out_offs = raw_off;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  267  		encdecfunc = crypto_skcipher_encrypt;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  268  	}
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  269  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  270  	while (size > 0) {
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  271  		const int sz = min(size, LOOP_IV_SECTOR_SIZE);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  272  		u32 iv[4] = { 0, };
a4d25ed5a46e4a Christoph Hellwig 2021-08-18 @273  		iv[0] = cpu_to_le32(IV & 0xffffffff);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  274  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  275  		sg_set_page(&sg_in, in_page, sz, in_offs);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  276  		sg_set_page(&sg_out, out_page, sz, out_offs);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  277  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  278  		skcipher_request_set_crypt(req, &sg_in, &sg_out, sz, iv);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  279  		err = encdecfunc(req);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  280  		if (err)
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  281  			goto out;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  282  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  283  		IV++;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  284  		size -= sz;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  285  		in_offs += sz;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  286  		out_offs += sz;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  287  	}
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  288  
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  289  	err = 0;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  290  out:
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  291  	skcipher_request_zero(req);
4bc5016d77d7be Christoph Hellwig 2021-08-18  292  	pr_err_ratelimited("loop: Transfer error at byte offset %llu, length %i.\n",
4bc5016d77d7be Christoph Hellwig 2021-08-18  293  			IV << 9, size);
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  294  	return err;
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  295  }
a4d25ed5a46e4a Christoph Hellwig 2021-08-18  296  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

#6 lore raw

PATCH 5/7 loop: remove the unused idx argument to loop_control_get_free

In reply to Christoph Hellwig
---
 drivers/block/loop.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/block/loop.c +2 -2
index 9176784d4fca..e93baff664c9 100644
@@ -2486,7 +2486,7 @@ static int loop_control_remove(int idx)
 	return ret;
 }
 
-static int loop_control_get_free(int idx)
+static int loop_control_get_free(void)
 {
 	struct loop_device *lo;
 	int id, ret;
@@ -2514,7 +2514,7 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
 	case LOOP_CTL_REMOVE:
 		return loop_control_remove(parm);
 	case LOOP_CTL_GET_FREE:
-		return loop_control_get_free(parm);
+		return loop_control_get_free();
 	default:
 		return -ENOSYS;
 	}
Signature · 1 line
2.30.2
#7 lore raw

PATCH 6/7 loop: move loop device deletion out of loop_ctl_mutex

In reply to Christoph Hellwig

To avoid complex lock ordering issues always delete loop devices outside of loop_ctl_mutex. In loop_control_remove the Lo_deleting state can be used to prevent further lookups, and given that module unload is synchronized vs new opens of the control device and thus ioctls there is no need for locks there at all.

Based on patches from Hillf Danton <[email protected]> and Tetsuo Handa <[email protected]>.

---
 drivers/block/loop.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/block/loop.c +8 -3
index e93baff664c9..043673bda8b3 100644
@@ -2480,7 +2480,11 @@ static int loop_control_remove(int idx)
 	mutex_unlock(&lo->lo_mutex);
 
 	idr_remove(&loop_index_idr, lo->lo_number);
+	mutex_unlock(&loop_ctl_mutex);
+
 	loop_remove(lo);
+	return 0;
+
 out_unlock_ctrl:
 	mutex_unlock(&loop_ctl_mutex);
 	return ret;
@@ -2611,11 +2615,12 @@ static void __exit loop_exit(void)
 	unregister_blkdev(LOOP_MAJOR, "loop");
 	misc_deregister(&loop_misc);
 
-	mutex_lock(&loop_ctl_mutex);
+	/*
+	 * No need for loop_ctl_mutex given that no new ioctls and thus
+	 * additions and removals can happen in parallel to module unloading.
+	 */
 	idr_for_each_entry(&loop_index_idr, lo, id)
 		loop_remove(lo);
-	mutex_unlock(&loop_ctl_mutex);
-
 	idr_destroy(&loop_index_idr);
 }
 
Signature · 1 line
2.30.2
#8 lore raw

PATCH 7/7 loop: avoid holding loop_ctl_mutex over add_disk

In reply to Christoph Hellwig

To avoid complex lock ordering issues loop_ctl_mutex should not be held over add_disk. Add a new Lo_new state for a loop device that has just been created but which is not live yet.

---
 drivers/block/loop.c | 16 ++++++++++++----
 drivers/block/loop.h |  1 +
 2 files changed, 13 insertions(+), 4 deletions(-)
drivers/block/loop.c +12 -4
index 043673bda8b3..80f462edc39f 100644
@@ -2329,7 +2329,7 @@ static int loop_add(int i)
 	lo = kzalloc(sizeof(*lo), GFP_KERNEL);
 	if (!lo)
 		goto out;
-	lo->lo_state = Lo_unbound;
+	lo->lo_state = Lo_new;
 
 	err = mutex_lock_killable(&loop_ctl_mutex);
 	if (err)
@@ -2343,8 +2343,9 @@ static int loop_add(int i)
 	} else {
 		err = idr_alloc(&loop_index_idr, lo, 0, 0, GFP_KERNEL);
 	}
+	mutex_unlock(&loop_ctl_mutex);
 	if (err < 0)
-		goto out_unlock;
+		goto out_free_dev;
 	i = err;
 
 	err = -ENOMEM;
@@ -2414,15 +2415,22 @@ static int loop_add(int i)
 	disk->event_flags	= DISK_EVENT_FLAG_UEVENT;
 	sprintf(disk->disk_name, "loop%d", i);
 	add_disk(disk);
+
+	err = mutex_lock_killable(&loop_ctl_mutex);
+	if (err)
+		goto out_del_gendisk;
+	lo->lo_state = Lo_unbound;
 	mutex_unlock(&loop_ctl_mutex);
+
 	return i;
 
+out_del_gendisk:
+	del_gendisk(lo->lo_disk);
+	blk_cleanup_disk(lo->lo_disk);
 out_cleanup_tags:
 	blk_mq_free_tag_set(&lo->tag_set);
 out_free_idr:
 	idr_remove(&loop_index_idr, i);
-out_unlock:
-	mutex_unlock(&loop_ctl_mutex);
 out_free_dev:
 	kfree(lo);
 out:
drivers/block/loop.h +1 -0
index d14ce6bdc014..608a20c23c64 100644
@@ -24,6 +24,7 @@ enum {
 	Lo_bound,
 	Lo_rundown,
 	Lo_deleting,
+	Lo_new,
 };
 
 struct loop_func_table;
Signature · 1 line
2.30.2
#9 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

Hi Jens,

this series sorts out lock order reversals involving the block layer open_mutex by drastically reducing the scope of loop_ctl_mutex. To do so it first merges the cryptoloop module into the main loop driver as the unregistrtion of transfers on live loop devices was causing some nasty locking interactions.

Quoted 8 lines

Diffstat:

 b/drivers/block/Kconfig    |    6 
 b/drivers/block/Makefile   |    1 
 b/drivers/block/loop.c     |  327 ++++++++++++++++++++++++---------------------
 b/drivers/block/loop.h     |   30 ----
 drivers/block/cryptoloop.c |  204 ----------------------------
 5 files changed, 188 insertions(+), 380 deletions(-)

Despite big change, this series introduces at least three bugs by ignoring what lock protects what resource and/or operation. Locking explanation is very very wrong. I never want this series merged for v5.14/v5.15 and stable kernels.

(1) Will crash at uninitialized mutex_lock_killable().
(2) Will crash at unprotected idr_remove().
(3) Will crash syzbot due to hitting WARNING messages (at least two different causes).