Skip to contentSkip to conversation

block: loop: set discard_granularity as PAGE_SIZE if sb->s_blocksize is 0

PATCH In review Ming Lei 3 participants 4 messages Jan 24, 2022 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: loop: set discard_granularity as PAGE_SIZE if sb->s_blocksize is 0

If backing file's filesystem has implemented ->fallocate(), we think the loop device can support discard, then pass sb->s_blocksize as discard_granularity. However, some underlying FS, such as overlayfs, doesn't set sb->s_blocksize, and causes discard_granularity to be set as zero, then the warning in __blkdev_issue_discard() is triggered.

Fix the issue by setting discard_granularity as PAGE_SIZE in this case since PAGE_SIZE is the most common data unit for FS.

---
 drivers/block/loop.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/block/loop.c +4 -0
index b1b05c45c07c..8c15bfab7e1a 100644
@@ -776,6 +776,10 @@ static void loop_config_discard(struct loop_device *lo)
 	} else {
 		max_discard_sectors = UINT_MAX >> 9;
 		granularity = inode->i_sb->s_blocksize;
+
+		/* Take PAGE_SIZE if the FS doesn't provide us one hint */
+		if (!granularity)
+			granularity = PAGE_SIZE;
 	}
 
 	if (max_discard_sectors) {
Signature · 1 line
2.31.1
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · Ming Lei

If backing file's filesystem has implemented ->fallocate(), we think the loop device can support discard, then pass sb->s_blocksize as discard_granularity. However, some underlying FS, such as overlayfs, doesn't set sb->s_blocksize, and causes discard_granularity to be set as zero, then the warning in __blkdev_issue_discard() is triggered.

[ Copying linux-unionfs and Miklos ]

Miklos mentioned that it might be ok to copy upper->s_blocksize into ovl->s_blocksize in overlayfs as other anonymous filesystem set it to some value (nfs, 9p, fuse, cifs) as well.

So it might be reasonable to fix overlayfs as well. But I think we need a block layer fix as well to deal with any filesystem which supports ->fallocate() and does not advertize ->s_blocksize. Not advertizing ->s_blocksize will probably only lead to suboptimial performance and nothing more.

Quoted 7 lines

Fix the issue by setting discard_granularity as PAGE_SIZE in this case since PAGE_SIZE is the most common data unit for FS.

Vivek

Quoted 22 lines
---
 drivers/block/loop.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/block/loop.c +4 -0
index b1b05c45c07c..8c15bfab7e1a 100644
@@ -776,6 +776,10 @@ static void loop_config_discard(struct loop_device *lo)
 	} else {
 		max_discard_sectors = UINT_MAX >> 9;
 		granularity = inode->i_sb->s_blocksize;
+
+		/* Take PAGE_SIZE if the FS doesn't provide us one hint */
+		if (!granularity)
+			granularity = PAGE_SIZE;
 	}
 
 	if (max_discard_sectors) {
Signature · 1 line
2.31.1
#2 lore raw
In reply to Ming Lei
Quoted 8 lines · Ming Lei

If backing file's filesystem has implemented ->fallocate(), we think the loop device can support discard, then pass sb->s_blocksize as discard_granularity. However, some underlying FS, such as overlayfs, doesn't set sb->s_blocksize, and causes discard_granularity to be set as zero, then the warning in __blkdev_issue_discard() is triggered.

Fix the issue by setting discard_granularity as PAGE_SIZE in this case since PAGE_SIZE is the most common data unit for FS.

sb->s_blocksize really does not mean anything. kstat.blksize might be a better choice, even if it someimes errs on the too large side.

#3 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Ming Lei

If backing file's filesystem has implemented ->fallocate(), we think the loop device can support discard, then pass sb->s_blocksize as discard_granularity. However, some underlying FS, such as overlayfs, doesn't set sb->s_blocksize, and causes discard_granularity to be set as zero, then the warning in __blkdev_issue_discard() is triggered.

Quoted 5 lines
Quoted 2 lines

Fix the issue by setting discard_granularity as PAGE_SIZE in this case since PAGE_SIZE is the most common data unit for FS.

sb->s_blocksize really does not mean anything. kstat.blksize might be a better choice, even if it someimes errs on the too large side.

[ CC linux-unionfs, Miklos ]

This should work well for overlayfs too. I see it just passes the query to underlying filesystem and that should report optimal I/O size.

On my overlayfs instance, I see.

# stat -c '%o' foo.txt
4096

Vivek