Skip to contentSkip to conversation

loop: LO_FLAGS_BLOCKSIZE fixes

PATCH Applied Omar Sandoval 4 participants 9 messages Aug 22, 2017 – Aug 23, 2017 Block layer

lore
Share & tools
lore
#0 lore raw

v3 cover loop: LO_FLAGS_BLOCKSIZE fixes

From: Omar Sandoval <[email protected]>

Patches 1, 3, and 4 are the same as v2, plus added reviewed-bys and tested-bys from Milan. Patch 2 is new.

Omar Sandoval (4):
loop: fix hang if LOOP_SET_STATUS gets invalid blocksize or encrypt
type
loop: set discard and write zeroes limits in 512 byte sectors
loop: use queue limit instead of private lo_logical_blocksize
loop: always return block size in LOOP_GET_STATUS

 drivers/block/loop.c | 75 ++++++++++++++++++++++++++--------------------------
 drivers/block/loop.h |  1 -
 2 files changed, 37 insertions(+), 39 deletions(-)
Signature · 1 line
2.14.1
#1 lore raw

PATCH v3 1/4 loop: fix hang if LOOP_SET_STATUS gets invalid blocksize or encrypt type

From: Omar Sandoval <[email protected]>

In both of these error cases, we need to make sure to unfreeze the queue before we return.

Fixes ecdd09597a57 ("block/loop: fix race between I/O and set_status")
Fixes f2c6df7dbf9a ("loop: support 4k physical blocksize")
---
 drivers/block/loop.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/block/loop.c +16 -8
index ef8334949b42..26548e07bc31 100644
@@ -1125,11 +1125,15 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 	if (info->lo_encrypt_type) {
 		unsigned int type = info->lo_encrypt_type;
 
-		if (type >= MAX_LO_CRYPT)
-			return -EINVAL;
+		if (type >= MAX_LO_CRYPT) {
+			err = -EINVAL;
+			goto exit;
+		}
 		xfer = xfer_funcs[type];
-		if (xfer == NULL)
-			return -EINVAL;
+		if (xfer == NULL) {
+			err = -EINVAL;
+			goto exit;
+		}
 	} else
 		xfer = NULL;
 
@@ -1144,10 +1148,14 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 		if (LO_INFO_BLOCKSIZE(info) != 512 &&
 		    LO_INFO_BLOCKSIZE(info) != 1024 &&
 		    LO_INFO_BLOCKSIZE(info) != 2048 &&
-		    LO_INFO_BLOCKSIZE(info) != 4096)
-			return -EINVAL;
-		if (LO_INFO_BLOCKSIZE(info) > lo->lo_blocksize)
-			return -EINVAL;
+		    LO_INFO_BLOCKSIZE(info) != 4096) {
+			err = -EINVAL;
+			goto exit;
+		}
+		if (LO_INFO_BLOCKSIZE(info) > lo->lo_blocksize) {
+			err = -EINVAL;
+			goto exit;
+		}
 	}
 
 	if (lo->lo_offset != info->lo_offset ||
Signature · 1 line
2.14.1
#2 lore raw

PATCH v3 2/4 loop: set discard and write zeroes limits in 512 byte sectors

In reply to Omar Sandoval

From: Omar Sandoval <[email protected]>

max_discard_sectors and max_write_zeroes_sectors are in 512 byte sectors, not device sectors.

Fixes f2c6df7dbf9a ("loop: support 4k physical blocksize")
---
 drivers/block/loop.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/loop.c +2 -6
index 26548e07bc31..a444dc2d5977 100644
@@ -820,7 +820,6 @@ static void loop_config_discard(struct loop_device *lo)
 	struct file *file = lo->lo_backing_file;
 	struct inode *inode = file->f_mapping->host;
 	struct request_queue *q = lo->lo_queue;
-	int lo_bits = 9;
 
 	/*
 	 * We use punch hole to reclaim the free space used by the
@@ -840,11 +839,8 @@ static void loop_config_discard(struct loop_device *lo)
 
 	q->limits.discard_granularity = inode->i_sb->s_blocksize;
 	q->limits.discard_alignment = 0;
-	if (lo->lo_flags & LO_FLAGS_BLOCKSIZE)
-		lo_bits = blksize_bits(lo->lo_logical_blocksize);
-
-	blk_queue_max_discard_sectors(q, UINT_MAX >> lo_bits);
-	blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> lo_bits);
+	blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
+	blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
 	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
 }
 
Signature · 1 line
2.14.1
Reviewed-by on v3 2/4
#3 lore raw

Re: [PATCH v3 2/4] loop: set discard and write zeroes limits in 512 byte sectors

Quoted 28 lines · Omar Sandoval

From: Omar Sandoval <[email protected]>

max_discard_sectors and max_write_zeroes_sectors are in 512 byte sectors, not device sectors.

Fixes f2c6df7dbf9a ("loop: support 4k physical blocksize")
---
 drivers/block/loop.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/block/loop.c +0 -1
index 26548e07bc31..a444dc2d5977 100644
@@ -820,7 +820,6 @@ static void loop_config_discard(struct loop_device *lo)
 	struct file *file = lo->lo_backing_file;
 	struct inode *inode = file->f_mapping->host;
 	struct request_queue *q = lo->lo_queue;
-	int lo_bits = 9;
 
 	/*
 	 * We use punch hole to reclaim the free space used by the
@@ -840,11 +839,8 @@ static void loop_config_discard(struct loop_device *lo)
 
 	q->limits.discard_granularity = inode->i_sb->s_blocksize;
 	q->limits.discard_alignment = 0;

drivers/block/loop.c static void loop_config_discard(struct loop_device *lo) · line 846

Quoted 10 lines
drivers/block/loop.c +2 -5
-	if (lo->lo_flags & LO_FLAGS_BLOCKSIZE)
-		lo_bits = blksize_bits(lo->lo_logical_blocksize);
-
-	blk_queue_max_discard_sectors(q, UINT_MAX >> lo_bits);
-	blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> lo_bits);
+	blk_queue_max_discard_sectors(q, UINT_MAX >> 9);
+	blk_queue_max_write_zeroes_sectors(q, UINT_MAX >> 9);
 	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
 }
 

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#4 lore raw

PATCH v3 3/4 loop: use queue limit instead of private lo_logical_blocksize

In reply to Omar Sandoval

From: Omar Sandoval <[email protected]>

There's no reason to track this separately; just use the logical_block_size queue limit. This also fixes an issue where the physical block size would get changed unnecessarily.

---
 drivers/block/loop.c | 40 +++++++++++++++++-----------------------
 drivers/block/loop.h |  1 -
 2 files changed, 17 insertions(+), 24 deletions(-)
drivers/block/loop.c +17 -23
index a444dc2d5977..28026f0abaa9 100644
@@ -221,8 +221,7 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 }
 
 static int
-figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit,
-		 loff_t logical_blocksize)
+figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
 {
 	loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
 	sector_t x = (sector_t)size;
@@ -234,12 +233,6 @@ figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit,
 		lo->lo_offset = offset;
 	if (lo->lo_sizelimit != sizelimit)
 		lo->lo_sizelimit = sizelimit;
-	if (lo->lo_flags & LO_FLAGS_BLOCKSIZE) {
-		lo->lo_logical_blocksize = logical_blocksize;
-		blk_queue_physical_block_size(lo->lo_queue, lo->lo_blocksize);
-		blk_queue_logical_block_size(lo->lo_queue,
-					     lo->lo_logical_blocksize);
-	}
 	set_capacity(lo->lo_disk, x);
 	bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9);
 	/* let user-space know about the new size */
@@ -934,7 +927,6 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 
 	lo->use_dio = false;
 	lo->lo_blocksize = lo_blocksize;
-	lo->lo_logical_blocksize = 512;
 	lo->lo_device = bdev;
 	lo->lo_flags = lo_flags;
 	lo->lo_backing_file = file;
@@ -946,6 +938,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 
 	if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
 		blk_queue_write_cache(lo->lo_queue, true, false);
+	blk_queue_logical_block_size(lo->lo_queue, 512);
+	blk_queue_physical_block_size(lo->lo_queue, 512);
+	blk_queue_io_min(lo->lo_queue, 512);
 
 	loop_update_dio(lo);
 	set_capacity(lo->lo_disk, size);
@@ -1133,14 +1128,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 	} else
 		xfer = NULL;
 
-	err = loop_init_xfer(lo, xfer, info);
-	if (err)
-		goto exit;
-
 	if (info->lo_flags & LO_FLAGS_BLOCKSIZE) {
-		if (!(lo->lo_flags & LO_FLAGS_BLOCKSIZE))
-			lo->lo_logical_blocksize = 512;
-		lo->lo_flags |= LO_FLAGS_BLOCKSIZE;
 		if (LO_INFO_BLOCKSIZE(info) != 512 &&
 		    LO_INFO_BLOCKSIZE(info) != 1024 &&
 		    LO_INFO_BLOCKSIZE(info) != 2048 &&
@@ -1154,18 +1142,25 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 		}
 	}
 
+	err = loop_init_xfer(lo, xfer, info);
+	if (err)
+		goto exit;
+
 	if (lo->lo_offset != info->lo_offset ||
 	    lo->lo_sizelimit != info->lo_sizelimit ||
-	    lo->lo_flags != lo_flags ||
-	    ((lo->lo_flags & LO_FLAGS_BLOCKSIZE) &&
-	     lo->lo_logical_blocksize != LO_INFO_BLOCKSIZE(info))) {
-		if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit,
-				     LO_INFO_BLOCKSIZE(info))) {
+	    lo->lo_flags != lo_flags) {
+		if (figure_loop_size(lo, info->lo_offset, info->lo_sizelimit)) {
 			err = -EFBIG;
 			goto exit;
 		}
 	}
 
+	if (info->lo_flags & LO_FLAGS_BLOCKSIZE) {
+		blk_queue_logical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_physical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_io_min(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+	}
+
 	loop_config_discard(lo);
 
 	memcpy(lo->lo_file_name, info->lo_file_name, LO_NAME_SIZE);
@@ -1352,8 +1347,7 @@ static int loop_set_capacity(struct loop_device *lo)
 	if (unlikely(lo->lo_state != Lo_bound))
 		return -ENXIO;
 
-	return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit,
-				lo->lo_logical_blocksize);
+	return figure_loop_size(lo, lo->lo_offset, lo->lo_sizelimit);
 }
 
 static int loop_set_dio(struct loop_device *lo, unsigned long arg)
drivers/block/loop.h +0 -1
index 2c096b9a17b8..fecd3f97ef8c 100644
@@ -49,7 +49,6 @@ struct loop_device {
 	struct file *	lo_backing_file;
 	struct block_device *lo_device;
 	unsigned	lo_blocksize;
-	unsigned	lo_logical_blocksize;
 	void		*key_data; 
 
 	gfp_t		old_gfp_mask;
Signature · 1 line
2.14.1
#5 lore raw

Re: [PATCH v3 3/4] loop: use queue limit instead of private lo_logical_blocksize

Quoted 7 lines · Omar Sandoval
drivers/block/loop.c +3 -0
@@ -946,6 +938,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 
 	if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
 		blk_queue_write_cache(lo->lo_queue, true, false);
+	blk_queue_logical_block_size(lo->lo_queue, 512);
+	blk_queue_physical_block_size(lo->lo_queue, 512);
+	blk_queue_io_min(lo->lo_queue, 512);

...

Quoted 1 line
(quoted diff) +0 -0
@@ -1133,14 +1128,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)

...

drivers/block/loop.c loop_set_status(struct loop_device *lo, const struct loop_info64 *info) · line 1163

Quoted 6 lines
drivers/block/loop.c +6 -0
+	if (info->lo_flags & LO_FLAGS_BLOCKSIZE) {
+		blk_queue_logical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_physical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_io_min(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+	}
+

I don't understand this.

* it seems the default is 512, but what about if the backing file is not a regular file? For example "losetup -f /dev/sda" where sda sector size is > 512.

* why the attributes in the /sys are affected by LO_FLAGS_BLOCKSIZE? Would be better to have a real sizes there (independently on the flag)?

* I think kernel lies in /sys about I/O size now. The phy sector size has never been 512, but PAGE_SIZE or real phy sector if backing file is a block device.

Your patch improves it for LO_FLAGS_BLOCKSIZE, but it's still lies about internally used I/O sizes.

Why we cannot use

    blk_queue_physical_block_size(lo->lo_queue, lo->lo_blocksize);
    blk_queue_logical_block_size(lo->lo_queue, lo->lo_logical_blocksize);

(as suggested by Hannes' original patch), but without

    if (info->lo_flags & LO_FLAGS_BLOCKSIZE)

condition?

Yes, result will be backwardly incompatible, but the current situation (all is 512) does not describe reality. It's legacy from time where all in kernel was 512...

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#6 lore raw

Re: [PATCH v3 3/4] loop: use queue limit instead of private lo_logical_blocksize

Quoted 22 lines · Karel Zak
Quoted 7 lines · Omar Sandoval
drivers/block/loop.c +3 -0
@@ -946,6 +938,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 
 	if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
 		blk_queue_write_cache(lo->lo_queue, true, false);
+	blk_queue_logical_block_size(lo->lo_queue, 512);
+	blk_queue_physical_block_size(lo->lo_queue, 512);
+	blk_queue_io_min(lo->lo_queue, 512);

...

Quoted 1 line
(quoted diff) +0 -0
@@ -1133,14 +1128,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)

...

drivers/block/loop.c loop_set_status(struct loop_device *lo, const struct loop_info64 *info) · line 1163

Quoted 6 lines
drivers/block/loop.c +6 -0
+	if (info->lo_flags & LO_FLAGS_BLOCKSIZE) {
+		blk_queue_logical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_physical_block_size(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+		blk_queue_io_min(lo->lo_queue, LO_INFO_BLOCKSIZE(info));
+	}
+

I don't understand this.

Quoted 3 lines

* it seems the default is 512, but what about if the backing file is not a regular file? For example "losetup -f /dev/sda" where sda sector size is > 512.

I didn't change the legacy behavior here, i.e., it's always 512 by default.

Quoted 2 lines

* why the attributes in the /sys are affected by LO_FLAGS_BLOCKSIZE? Would be better to have a real sizes there (independently on the flag)?

What do you mean? LO_FLAGS_BLOCKSIZE means I want to change the logical blocksize of the loop device, so why shouldn't it be reflected in sysfs?

Quoted 14 lines

* I think kernel lies in /sys about I/O size now. The phy sector size has never been 512, but PAGE_SIZE or real phy sector if backing file is a block device.

Your patch improves it for LO_FLAGS_BLOCKSIZE, but it's still lies about internally used I/O sizes.

Why we cannot use

    blk_queue_physical_block_size(lo->lo_queue, lo->lo_blocksize);
    blk_queue_logical_block_size(lo->lo_queue, lo->lo_logical_blocksize);

(as suggested by Hannes' original patch), but without

Quoted 7 lines
    if (info->lo_flags & LO_FLAGS_BLOCKSIZE)

condition?

Yes, result will be backwardly incompatible, but the current situation (all is 512) does not describe reality. It's legacy from time where all in kernel was 512...

I went back and forth on this. Yeah, the kernel is lying, and using the backing block size makes more sense, but backwards compatability is exactly why I didn't change this. Then again, the physical block size is more of a hint, so it might be safe to change this.

#7 lore raw

PATCH v3 4/4 loop: always return block size in LOOP_GET_STATUS

In reply to Omar Sandoval

From: Omar Sandoval <[email protected]>

When I was writing a test for the new loop device block size functionality, I noticed a couple of issues with how LOOP_GET_STATUS handles the block size:

- lo_init[0] is never filled in with the logical block size we previously set
- lo_flags returned from LOOP_GET_STATUS will have LO_FLAGS_BLOCKSIZE set if we previously called LOOP_SET_STATUS with LO_FLAGS_BLOCKSIZE set, which doesn't really mean anything

Instead, for LOOP_GET_STATUS, let's always fill in lo_init[0] and set the LO_FLAGS_BLOCKSIZE flag to indicate we support it.

---
 drivers/block/loop.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/block/loop.c +2 -1
index 28026f0abaa9..b55a1f82f561 100644
@@ -1222,7 +1222,7 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info)
 	info->lo_rdevice = huge_encode_dev(lo->lo_device ? stat.rdev : stat.dev);
 	info->lo_offset = lo->lo_offset;
 	info->lo_sizelimit = lo->lo_sizelimit;
-	info->lo_flags = lo->lo_flags;
+	info->lo_flags = lo->lo_flags | LO_FLAGS_BLOCKSIZE;
 	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 =
@@ -1232,6 +1232,7 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info)
 		memcpy(info->lo_encrypt_key, lo->lo_encrypt_key,
 		       lo->lo_encrypt_key_size);
 	}
+	LO_INFO_BLOCKSIZE(info) = queue_logical_block_size(lo->lo_queue);
 	return 0;
 }
 
Signature · 1 line
2.14.1
maintainer Applied
#8 lore raw
In reply to Omar Sandoval
Quoted 11 lines · Omar Sandoval

From: Omar Sandoval <[email protected]>

Patches 1, 3, and 4 are the same as v2, plus added reviewed-bys and tested-bys from Milan. Patch 2 is new.

Omar Sandoval (4):
loop: fix hang if LOOP_SET_STATUS gets invalid blocksize or encrypt
type
loop: set discard and write zeroes limits in 512 byte sectors
loop: use queue limit instead of private lo_logical_blocksize
loop: always return block size in LOOP_GET_STATUS

Queued up, thanks Omar.

Signature · 1 line
Jens Axboe